Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Daniel Drown |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * dns64.c - find the nat64 prefix with a dns64 lookup |
| 17 | */ |
| 18 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 19 | #include <arpa/inet.h> |
| 20 | #include <netdb.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 21 | #include <netinet/in.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 24 | #include <strings.h> |
| 25 | #include <sys/socket.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 28 | #include "NetdClient.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 29 | #include "dns64.h" |
| 30 | #include "logging.h" |
Paul Jensen | 247dd71 | 2014-05-30 13:19:10 -0400 | [diff] [blame] | 31 | #include "resolv_netid.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 32 | |
| 33 | /* function: plat_prefix |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 34 | * looks up an ipv4-only hostname and looks for a nat64 /96 prefix, returns 1 on success, 0 on |
| 35 | * failure |
| 36 | * ipv4_name - name to lookup |
| 37 | * net_id - (optional) netId to use, NETID_UNSET indicates use of default network |
| 38 | * prefix - the plat /96 prefix |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 39 | */ |
Paul Jensen | 247dd71 | 2014-05-30 13:19:10 -0400 | [diff] [blame] | 40 | int plat_prefix(const char *ipv4_name, unsigned net_id, struct in6_addr *prefix) { |
Erik Kline | 89f49ae | 2014-09-22 13:32:27 +0900 | [diff] [blame] | 41 | const struct addrinfo hints = { |
| 42 | .ai_family = AF_INET6, |
| 43 | }; |
| 44 | int status; |
| 45 | struct addrinfo *result = NULL; |
| 46 | struct in6_addr plat_addr; |
Lorenzo Colitti | baf6299 | 2013-03-01 20:29:39 +0900 | [diff] [blame] | 47 | char plat_addr_str[INET6_ADDRSTRLEN]; |
| 48 | |
| 49 | logmsg(ANDROID_LOG_INFO, "Detecting NAT64 prefix from DNS..."); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 50 | |
Erik Kline | 0b1d19c | 2018-03-29 14:36:59 -0700 | [diff] [blame] | 51 | // Be sure to query local DNS64 servers, bypassing Private DNS (if enabled). |
| 52 | if (net_id != NETID_UNSET) { |
Erik Kline | f1c79f8 | 2018-03-29 23:38:49 -0700 | [diff] [blame] | 53 | net_id |= NETID_USE_LOCAL_NAMESERVERS; |
Erik Kline | 0b1d19c | 2018-03-29 14:36:59 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Paul Jensen | 247dd71 | 2014-05-30 13:19:10 -0400 | [diff] [blame] | 56 | status = android_getaddrinfofornet(ipv4_name, NULL, &hints, net_id, MARK_UNSET, &result); |
Erik Kline | 89f49ae | 2014-09-22 13:32:27 +0900 | [diff] [blame] | 57 | if (status != 0 || result == NULL) { |
junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 58 | logmsg(ANDROID_LOG_ERROR, "plat_prefix/dns(%s) status = %d/%s", ipv4_name, status, |
| 59 | gai_strerror(status)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
Erik Kline | 89f49ae | 2014-09-22 13:32:27 +0900 | [diff] [blame] | 63 | // Use only the first result. If other records are present, possibly with |
| 64 | // differing DNS64 prefixes they are ignored (there is very little sensible |
| 65 | // that could be done with them at this time anyway). |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 66 | |
Erik Kline | 89f49ae | 2014-09-22 13:32:27 +0900 | [diff] [blame] | 67 | if (result->ai_family != AF_INET6) { |
| 68 | logmsg(ANDROID_LOG_WARN, "plat_prefix/unexpected address family: %d", result->ai_family); |
| 69 | return 0; |
| 70 | } |
| 71 | plat_addr = ((struct sockaddr_in6 *)result->ai_addr)->sin6_addr; |
| 72 | // Only /96 DNS64 prefixes are supported at this time. |
| 73 | plat_addr.s6_addr32[3] = 0; |
| 74 | freeaddrinfo(result); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 75 | |
Erik Kline | 89f49ae | 2014-09-22 13:32:27 +0900 | [diff] [blame] | 76 | logmsg(ANDROID_LOG_INFO, "Detected NAT64 prefix %s/96", |
| 77 | inet_ntop(AF_INET6, &plat_addr, plat_addr_str, sizeof(plat_addr_str))); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 78 | *prefix = plat_addr; |
| 79 | return 1; |
| 80 | } |