blob: 4e9252dc2ecb93fb1307b34affc514d7f108e93a [file] [log] [blame]
Daniel Drowna45056e2012-03-23 10:42:54 -05001/*
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 Jensen247dd712014-05-30 13:19:10 -040030#include "resolv_netid.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050031
32/* function: plat_prefix
Erik Kline89f49ae2014-09-22 13:32:27 +090033 * looks up an ipv4-only hostname and looks for a nat64 /96 prefix, returns 1 on success, 0 on failure
Paul Jensen247dd712014-05-30 13:19:10 -040034 * 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 Drowna45056e2012-03-23 10:42:54 -050037 */
Paul Jensen247dd712014-05-30 13:19:10 -040038int plat_prefix(const char *ipv4_name, unsigned net_id, struct in6_addr *prefix) {
Erik Kline89f49ae2014-09-22 13:32:27 +090039 const struct addrinfo hints = {
40 .ai_family = AF_INET6,
41 };
42 int status;
43 struct addrinfo *result = NULL;
44 struct in6_addr plat_addr;
Lorenzo Colittibaf62992013-03-01 20:29:39 +090045 char plat_addr_str[INET6_ADDRSTRLEN];
46
47 logmsg(ANDROID_LOG_INFO, "Detecting NAT64 prefix from DNS...");
Daniel Drowna45056e2012-03-23 10:42:54 -050048
Paul Jensen247dd712014-05-30 13:19:10 -040049 status = android_getaddrinfofornet(ipv4_name, NULL, &hints, net_id, MARK_UNSET, &result);
Erik Kline89f49ae2014-09-22 13:32:27 +090050 if (status != 0 || result == NULL) {
51 logmsg(ANDROID_LOG_ERROR, "plat_prefix/dns(%s) status = %d/%s",
52 ipv4_name, status, gai_strerror(status));
Daniel Drowna45056e2012-03-23 10:42:54 -050053 return 0;
54 }
55
Erik Kline89f49ae2014-09-22 13:32:27 +090056 // Use only the first result. If other records are present, possibly with
57 // differing DNS64 prefixes they are ignored (there is very little sensible
58 // that could be done with them at this time anyway).
Daniel Drowna45056e2012-03-23 10:42:54 -050059
Erik Kline89f49ae2014-09-22 13:32:27 +090060 if (result->ai_family != AF_INET6) {
61 logmsg(ANDROID_LOG_WARN, "plat_prefix/unexpected address family: %d", result->ai_family);
62 return 0;
63 }
64 plat_addr = ((struct sockaddr_in6 *)result->ai_addr)->sin6_addr;
65 // Only /96 DNS64 prefixes are supported at this time.
66 plat_addr.s6_addr32[3] = 0;
67 freeaddrinfo(result);
Daniel Drowna45056e2012-03-23 10:42:54 -050068
Erik Kline89f49ae2014-09-22 13:32:27 +090069 logmsg(ANDROID_LOG_INFO, "Detected NAT64 prefix %s/96",
70 inet_ntop(AF_INET6, &plat_addr, plat_addr_str, sizeof(plat_addr_str)));
Daniel Drowna45056e2012-03-23 10:42:54 -050071 *prefix = plat_addr;
72 return 1;
73}