xf.li | f1aed28 | 2024-02-06 00:31:51 -0800 | [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" |
| 30 | //#include "NetdClient.h" |
| 31 | //#include "resolv_netid.h" |
| 32 | #include "my_header.h" |
| 33 | |
| 34 | /* function: plat_prefix |
| 35 | * looks up an ipv4-only hostname and looks for a nat64 /96 prefix, returns 1 on success, 0 on 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 |
| 39 | */ |
| 40 | int plat_prefix(const char *ipv4_name, unsigned net_id, struct in6_addr *prefix) { |
| 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; |
| 47 | char plat_addr_str[INET6_ADDRSTRLEN]; |
| 48 | |
| 49 | logmsg(ANDROID_LOG_INFO, "Detecting NAT64 prefix from DNS..."); |
| 50 | |
| 51 | // Be sure to query local DNS64 servers, bypassing Private DNS (if enabled). |
| 52 | if (net_id != NETID_UNSET) { |
| 53 | net_id |= NETID_USE_LOCAL_NAMESERVERS; |
| 54 | } |
| 55 | |
| 56 | //status = android_getaddrinfofornet(ipv4_name, NULL, &hints, net_id, MARK_UNSET, &result); |
| 57 | status = getaddrinfo(ipv4_name, NULL, &hints, &result); |
| 58 | |
| 59 | if (status != 0 || result == NULL) { |
| 60 | logmsg(ANDROID_LOG_ERROR, "plat_prefix/dns(%s) status = %d/%s", |
| 61 | ipv4_name, status, gai_strerror(status)); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | // Use only the first result. If other records are present, possibly with |
| 66 | // differing DNS64 prefixes they are ignored (there is very little sensible |
| 67 | // that could be done with them at this time anyway). |
| 68 | |
| 69 | if (result->ai_family != AF_INET6) { |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 70 | logmsg(ANDROID_LOG_WARN, "plat_prefix/unexpected address family: %d", result->ai_family); |
| 71 | freeaddrinfo(result); |
xf.li | f1aed28 | 2024-02-06 00:31:51 -0800 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | plat_addr = ((struct sockaddr_in6 *)result->ai_addr)->sin6_addr; |
| 75 | // Only /96 DNS64 prefixes are supported at this time. |
| 76 | plat_addr.s6_addr32[3] = 0; |
| 77 | freeaddrinfo(result); |
| 78 | |
| 79 | logmsg(ANDROID_LOG_INFO, "Detected NAT64 prefix %s/96", |
| 80 | inet_ntop(AF_INET6, &plat_addr, plat_addr_str, sizeof(plat_addr_str))); |
| 81 | *prefix = plat_addr; |
| 82 | return 1; |
| 83 | } |