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 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <netinet/in.h> |
| 21 | #include <arpa/inet.h> |
| 22 | #include <netdb.h> |
| 23 | #include <strings.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include "dns64.h" |
| 29 | #include "logging.h" |
Paul Jensen | 247dd71 | 2014-05-30 13:19:10 -0400 | [diff] [blame] | 30 | #include "resolv_netid.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 31 | |
| 32 | /* function: plat_prefix |
| 33 | * looks up an ipv4-only hostname and looks for a nat64 /96 prefix, returns 1 on success, 0 on temporary failure, -1 on permanent failure |
Paul Jensen | 247dd71 | 2014-05-30 13:19:10 -0400 | [diff] [blame] | 34 | * ipv4_name - name to lookup |
| 35 | * net_id - (optional) netId to use, NETID_UNSET indicates use of default network |
| 36 | * prefix - the plat /96 prefix |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 37 | */ |
Paul Jensen | 247dd71 | 2014-05-30 13:19:10 -0400 | [diff] [blame] | 38 | int plat_prefix(const char *ipv4_name, unsigned net_id, struct in6_addr *prefix) { |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 39 | struct addrinfo hints, *result, *p; |
| 40 | int status, plat_addr_set, ipv4_records, ipv6_records; |
| 41 | struct in6_addr plat_addr, this_plat_addr; |
| 42 | struct sockaddr_in6 *this_addr; |
Lorenzo Colitti | baf6299 | 2013-03-01 20:29:39 +0900 | [diff] [blame] | 43 | char plat_addr_str[INET6_ADDRSTRLEN]; |
| 44 | |
| 45 | logmsg(ANDROID_LOG_INFO, "Detecting NAT64 prefix from DNS..."); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 46 | |
| 47 | result = NULL; |
| 48 | plat_addr_set = 0; |
| 49 | ipv4_records = ipv6_records = 0; |
| 50 | |
| 51 | bzero(&hints, sizeof(hints)); |
| 52 | hints.ai_family = AF_UNSPEC; |
Paul Jensen | 247dd71 | 2014-05-30 13:19:10 -0400 | [diff] [blame] | 53 | status = android_getaddrinfofornet(ipv4_name, NULL, &hints, net_id, MARK_UNSET, &result); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 54 | if(status != 0) { |
| 55 | logmsg(ANDROID_LOG_ERROR,"plat_prefix/dns(%s) status = %d/%s\n", ipv4_name, status, gai_strerror(status)); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | for(p = result; p; p = p->ai_next) { |
| 60 | if(p->ai_family == AF_INET) { |
| 61 | ipv4_records++; |
| 62 | continue; |
| 63 | } |
| 64 | if(p->ai_family != AF_INET6) { |
| 65 | logmsg(ANDROID_LOG_WARN,"plat_prefix/unexpected address family: %d\n", p->ai_family); |
| 66 | continue; |
| 67 | } |
| 68 | ipv6_records++; |
| 69 | this_addr = (struct sockaddr_in6 *)p->ai_addr; |
| 70 | this_plat_addr = this_addr->sin6_addr; |
| 71 | this_plat_addr.s6_addr32[3] = 0; |
| 72 | |
| 73 | if(!plat_addr_set) { |
| 74 | plat_addr = this_plat_addr; |
| 75 | plat_addr_set = 1; |
| 76 | continue; |
| 77 | } |
| 78 | |
Lorenzo Colitti | baf6299 | 2013-03-01 20:29:39 +0900 | [diff] [blame] | 79 | 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] | 80 | if(!IN6_ARE_ADDR_EQUAL(&plat_addr, &this_plat_addr)) { |
Lorenzo Colitti | baf6299 | 2013-03-01 20:29:39 +0900 | [diff] [blame] | 81 | char this_plat_addr_str[INET6_ADDRSTRLEN]; |
| 82 | inet_ntop(AF_INET6, &this_plat_addr, this_plat_addr_str, sizeof(this_plat_addr_str)); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 83 | logmsg(ANDROID_LOG_ERROR,"plat_prefix/two different plat addrs = %s,%s", |
Lorenzo Colitti | baf6299 | 2013-03-01 20:29:39 +0900 | [diff] [blame] | 84 | plat_addr_str,this_plat_addr_str); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | if(result != NULL) { |
| 88 | freeaddrinfo(result); |
| 89 | } |
| 90 | if(ipv4_records > 0 && ipv6_records == 0) { |
| 91 | logmsg(ANDROID_LOG_WARN,"plat_prefix/no dns64 detected\n"); |
| 92 | return -1; |
| 93 | } |
Lorenzo Colitti | baf6299 | 2013-03-01 20:29:39 +0900 | [diff] [blame] | 94 | |
| 95 | logmsg(ANDROID_LOG_INFO, "Detected NAT64 prefix %s/96", plat_addr_str); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 96 | *prefix = plat_addr; |
| 97 | return 1; |
| 98 | } |