blob: 4ec800e555be5374f73855fbfe12bab0c469439d [file] [log] [blame]
Robert Greenwalt9ac09492013-02-05 11:02:49 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
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
17#include <arpa/inet.h>
18#include <jni.h>
19#include <netdb.h>
20#include <stdio.h>
Elliott Hughes17e2c232015-01-29 23:02:17 -080021#include <string.h>
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +090022
23#include <android/log.h>
24
25#define LOG_TAG "NativeDns-JNI"
26#define LOGD(fmt, ...) \
27 __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##__VA_ARGS__)
Robert Greenwalt9ac09492013-02-05 11:02:49 -080028
Vinit Deshapndee52091e2013-09-23 16:21:04 -070029const char *GoogleDNSIpV4Address="8.8.8.8";
30const char *GoogleDNSIpV4Address2="8.8.4.4";
31const char *GoogleDNSIpV6Address="2001:4860:4860::8888";
32const char *GoogleDNSIpV6Address2="2001:4860:4860::8844";
33
Robert Greenwalt9ac09492013-02-05 11:02:49 -080034JNIEXPORT jboolean Java_android_net_cts_DnsTest_testNativeDns(JNIEnv* env, jclass class)
35{
36 const char *node = "www.google.com";
37 char *service = NULL;
38 struct addrinfo *answer;
39
40 int res = getaddrinfo(node, service, NULL, &answer);
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +090041 LOGD("getaddrinfo(www.google.com) gave res=%d (%s)", res, gai_strerror(res));
Robert Greenwalt9ac09492013-02-05 11:02:49 -080042 if (res != 0) return JNI_FALSE;
43
44 // check for v4 & v6
45 {
46 int foundv4 = 0;
47 int foundv6 = 0;
48 struct addrinfo *current = answer;
49 while (current != NULL) {
50 char buf[256];
51 if (current->ai_addr->sa_family == AF_INET) {
52 inet_ntop(current->ai_family, &((struct sockaddr_in *)current->ai_addr)->sin_addr,
53 buf, sizeof(buf));
54 foundv4 = 1;
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +090055 LOGD(" %s", buf);
Robert Greenwalt9ac09492013-02-05 11:02:49 -080056 } else if (current->ai_addr->sa_family == AF_INET6) {
57 inet_ntop(current->ai_family, &((struct sockaddr_in6 *)current->ai_addr)->sin6_addr,
58 buf, sizeof(buf));
59 foundv6 = 1;
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +090060 LOGD(" %s", buf);
Robert Greenwalt9ac09492013-02-05 11:02:49 -080061 }
62 current = current->ai_next;
63 }
64
65 freeaddrinfo(answer);
66 answer = NULL;
Vinit Deshapndee52091e2013-09-23 16:21:04 -070067 if (foundv4 != 1 && foundv6 != 1) {
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +090068 LOGD("getaddrinfo(www.google.com) didn't find either v4 or v6 address");
Robert Greenwalt9ac09492013-02-05 11:02:49 -080069 return JNI_FALSE;
70 }
71 }
72
73 node = "ipv6.google.com";
74 res = getaddrinfo(node, service, NULL, &answer);
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +090075 LOGD("getaddrinfo(ipv6.google.com) gave res=%d", res);
Robert Greenwalt9ac09492013-02-05 11:02:49 -080076 if (res != 0) return JNI_FALSE;
77
78 {
79 int foundv4 = 0;
80 int foundv6 = 0;
81 struct addrinfo *current = answer;
82 while (current != NULL) {
83 char buf[256];
84 if (current->ai_addr->sa_family == AF_INET) {
85 inet_ntop(current->ai_family, &((struct sockaddr_in *)current->ai_addr)->sin_addr,
86 buf, sizeof(buf));
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +090087 LOGD(" %s", buf);
Robert Greenwalt9ac09492013-02-05 11:02:49 -080088 foundv4 = 1;
89 } else if (current->ai_addr->sa_family == AF_INET6) {
90 inet_ntop(current->ai_family, &((struct sockaddr_in6 *)current->ai_addr)->sin6_addr,
91 buf, sizeof(buf));
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +090092 LOGD(" %s", buf);
Robert Greenwalt9ac09492013-02-05 11:02:49 -080093 foundv6 = 1;
94 }
95 current = current->ai_next;
96 }
97
98 freeaddrinfo(answer);
99 answer = NULL;
100 if (foundv4 == 1 || foundv6 != 1) {
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +0900101 LOGD("getaddrinfo(ipv6.google.com) didn't find only v6");
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800102 return JNI_FALSE;
103 }
104 }
105
106 // getnameinfo
107 struct sockaddr_in sa4;
108 sa4.sin_family = AF_INET;
109 sa4.sin_port = 0;
Vinit Deshapndee52091e2013-09-23 16:21:04 -0700110 inet_pton(AF_INET, GoogleDNSIpV4Address, &(sa4.sin_addr));
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800111
112 struct sockaddr_in6 sa6;
113 sa6.sin6_family = AF_INET6;
114 sa6.sin6_port = 0;
115 sa6.sin6_flowinfo = 0;
116 sa6.sin6_scope_id = 0;
Vinit Deshapndee52091e2013-09-23 16:21:04 -0700117 inet_pton(AF_INET6, GoogleDNSIpV6Address2, &(sa6.sin6_addr));
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800118
119 char buf[NI_MAXHOST];
120 int flags = NI_NAMEREQD;
121
122 res = getnameinfo((const struct sockaddr*)&sa4, sizeof(sa4), buf, sizeof(buf), NULL, 0, flags);
123 if (res != 0) {
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +0900124 LOGD("getnameinfo(%s (GoogleDNS) ) gave error %d (%s)", GoogleDNSIpV4Address, res,
Vinit Deshapndee52091e2013-09-23 16:21:04 -0700125 gai_strerror(res));
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800126 return JNI_FALSE;
127 }
Lorenzo Colitti3852fd92019-03-28 17:38:32 +0900128 if (strstr(buf, "google.com") == NULL && strstr(buf, "dns.google") == NULL) {
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +0900129 LOGD("getnameinfo(%s (GoogleDNS) ) didn't return google.com or dns.google: %s",
Vinit Deshapndee52091e2013-09-23 16:21:04 -0700130 GoogleDNSIpV4Address, buf);
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800131 return JNI_FALSE;
132 }
133
Edward Savage-Jones4b05fbd2016-02-25 00:36:09 +0100134 memset(buf, 0, sizeof(buf));
Vinit Deshapndee52091e2013-09-23 16:21:04 -0700135 res = getnameinfo((const struct sockaddr*)&sa6, sizeof(sa6), buf, sizeof(buf), NULL, 0, flags);
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800136 if (res != 0) {
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +0900137 LOGD("getnameinfo(%s (GoogleDNS) ) gave error %d (%s)", GoogleDNSIpV6Address2,
Vinit Deshapndee52091e2013-09-23 16:21:04 -0700138 res, gai_strerror(res));
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800139 return JNI_FALSE;
140 }
Lorenzo Colitti3852fd92019-03-28 17:38:32 +0900141 if (strstr(buf, "google.com") == NULL && strstr(buf, "dns.google") == NULL) {
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +0900142 LOGD("getnameinfo(%s (GoogleDNS) ) didn't return google.com or dns.google: %s",
Lorenzo Colitti3852fd92019-03-28 17:38:32 +0900143 GoogleDNSIpV6Address2, buf);
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800144 return JNI_FALSE;
145 }
146
147 // gethostbyname
Vinit Deshapndee52091e2013-09-23 16:21:04 -0700148 struct hostent *my_hostent = gethostbyname("www.youtube.com");
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800149 if (my_hostent == NULL) {
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +0900150 LOGD("gethostbyname(www.youtube.com) gave null response");
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800151 return JNI_FALSE;
152 }
153 if ((my_hostent->h_addr_list == NULL) || (*my_hostent->h_addr_list == NULL)) {
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +0900154 LOGD("gethostbyname(www.youtube.com) gave 0 addresses");
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800155 return JNI_FALSE;
156 }
157 {
158 char **current = my_hostent->h_addr_list;
159 while (*current != NULL) {
160 char buf[256];
161 inet_ntop(my_hostent->h_addrtype, *current, buf, sizeof(buf));
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +0900162 LOGD("gethostbyname(www.youtube.com) gave %s", buf);
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800163 current++;
164 }
165 }
166
167 // gethostbyaddr
168 char addr6[16];
Vinit Deshapndee52091e2013-09-23 16:21:04 -0700169 inet_pton(AF_INET6, GoogleDNSIpV6Address, addr6);
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800170 my_hostent = gethostbyaddr(addr6, sizeof(addr6), AF_INET6);
171 if (my_hostent == NULL) {
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +0900172 LOGD("gethostbyaddr(%s (GoogleDNS) ) gave null response", GoogleDNSIpV6Address);
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800173 return JNI_FALSE;
174 }
Vinit Deshapndee52091e2013-09-23 16:21:04 -0700175
Remi NGUYEN VANa7f8f2d2020-04-21 15:09:16 +0900176 LOGD("gethostbyaddr(%s (GoogleDNS) ) gave %s for name", GoogleDNSIpV6Address,
Vinit Deshapndee52091e2013-09-23 16:21:04 -0700177 my_hostent->h_name ? my_hostent->h_name : "null");
178
Robert Greenwalt9ac09492013-02-05 11:02:49 -0800179 if (my_hostent->h_name == NULL) return JNI_FALSE;
180 return JNI_TRUE;
181}