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" 30 31 /* function: plat_prefix 32 * 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 33 * ipv4_name - name to lookup 34 * prefix - the plat /96 prefix 35 */ 36 int plat_prefix(const char *ipv4_name, struct in6_addr *prefix) { 37 struct addrinfo hints, *result, *p; 38 int status, plat_addr_set, ipv4_records, ipv6_records; 39 struct in6_addr plat_addr, this_plat_addr; 40 struct sockaddr_in6 *this_addr; 41 char plat_addr_str[INET6_ADDRSTRLEN]; 42 43 logmsg(ANDROID_LOG_INFO, "Detecting NAT64 prefix from DNS..."); 44 45 result = NULL; 46 plat_addr_set = 0; 47 ipv4_records = ipv6_records = 0; 48 49 bzero(&hints, sizeof(hints)); 50 hints.ai_family = AF_UNSPEC; 51 status = getaddrinfo(ipv4_name, NULL, &hints, &result); 52 if(status != 0) { 53 logmsg(ANDROID_LOG_ERROR,"plat_prefix/dns(%s) status = %d/%s\n", ipv4_name, status, gai_strerror(status)); 54 return 0; 55 } 56 57 for(p = result; p; p = p->ai_next) { 58 if(p->ai_family == AF_INET) { 59 ipv4_records++; 60 continue; 61 } 62 if(p->ai_family != AF_INET6) { 63 logmsg(ANDROID_LOG_WARN,"plat_prefix/unexpected address family: %d\n", p->ai_family); 64 continue; 65 } 66 ipv6_records++; 67 this_addr = (struct sockaddr_in6 *)p->ai_addr; 68 this_plat_addr = this_addr->sin6_addr; 69 this_plat_addr.s6_addr32[3] = 0; 70 71 if(!plat_addr_set) { 72 plat_addr = this_plat_addr; 73 plat_addr_set = 1; 74 continue; 75 } 76 77 inet_ntop(AF_INET6, &plat_addr, plat_addr_str, sizeof(plat_addr_str)); 78 if(!IN6_ARE_ADDR_EQUAL(&plat_addr, &this_plat_addr)) { 79 char this_plat_addr_str[INET6_ADDRSTRLEN]; 80 inet_ntop(AF_INET6, &this_plat_addr, this_plat_addr_str, sizeof(this_plat_addr_str)); 81 logmsg(ANDROID_LOG_ERROR,"plat_prefix/two different plat addrs = %s,%s", 82 plat_addr_str,this_plat_addr_str); 83 } 84 } 85 if(result != NULL) { 86 freeaddrinfo(result); 87 } 88 if(ipv4_records > 0 && ipv6_records == 0) { 89 logmsg(ANDROID_LOG_WARN,"plat_prefix/no dns64 detected\n"); 90 return -1; 91 } 92 93 logmsg(ANDROID_LOG_INFO, "Detected NAT64 prefix %s/96", plat_addr_str); 94 *prefix = plat_addr; 95 return 1; 96 } 97