Robert Greenwalt | 9ac0949 | 2013-02-05 11:02:49 -0800 | [diff] [blame] | 1 | /* |
| 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> |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | JNIEXPORT jboolean Java_android_net_cts_DnsTest_testNativeDns(JNIEnv* env, jclass class) |
| 24 | { |
| 25 | const char *node = "www.google.com"; |
| 26 | char *service = NULL; |
| 27 | struct addrinfo *answer; |
| 28 | |
| 29 | int res = getaddrinfo(node, service, NULL, &answer); |
| 30 | ALOGD("getaddrinfo(www.google.com) gave res=%d (%s)", res, gai_strerror(res)); |
| 31 | if (res != 0) return JNI_FALSE; |
| 32 | |
| 33 | // check for v4 & v6 |
| 34 | { |
| 35 | int foundv4 = 0; |
| 36 | int foundv6 = 0; |
| 37 | struct addrinfo *current = answer; |
| 38 | while (current != NULL) { |
| 39 | char buf[256]; |
| 40 | if (current->ai_addr->sa_family == AF_INET) { |
| 41 | inet_ntop(current->ai_family, &((struct sockaddr_in *)current->ai_addr)->sin_addr, |
| 42 | buf, sizeof(buf)); |
| 43 | foundv4 = 1; |
| 44 | ALOGD(" %s", buf); |
| 45 | } else if (current->ai_addr->sa_family == AF_INET6) { |
| 46 | inet_ntop(current->ai_family, &((struct sockaddr_in6 *)current->ai_addr)->sin6_addr, |
| 47 | buf, sizeof(buf)); |
| 48 | foundv6 = 1; |
| 49 | ALOGD(" %s", buf); |
| 50 | } |
| 51 | current = current->ai_next; |
| 52 | } |
| 53 | |
| 54 | freeaddrinfo(answer); |
| 55 | answer = NULL; |
| 56 | if (foundv4 != 1 || foundv6 != 1) { |
| 57 | ALOGD("getaddrinfo(www.google.com) didn't find both v4 and v6"); |
| 58 | return JNI_FALSE; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | node = "ipv6.google.com"; |
| 63 | res = getaddrinfo(node, service, NULL, &answer); |
| 64 | ALOGD("getaddrinfo(ipv6.google.com) gave res=%d", res); |
| 65 | if (res != 0) return JNI_FALSE; |
| 66 | |
| 67 | { |
| 68 | int foundv4 = 0; |
| 69 | int foundv6 = 0; |
| 70 | struct addrinfo *current = answer; |
| 71 | while (current != NULL) { |
| 72 | char buf[256]; |
| 73 | if (current->ai_addr->sa_family == AF_INET) { |
| 74 | inet_ntop(current->ai_family, &((struct sockaddr_in *)current->ai_addr)->sin_addr, |
| 75 | buf, sizeof(buf)); |
| 76 | ALOGD(" %s", buf); |
| 77 | foundv4 = 1; |
| 78 | } else if (current->ai_addr->sa_family == AF_INET6) { |
| 79 | inet_ntop(current->ai_family, &((struct sockaddr_in6 *)current->ai_addr)->sin6_addr, |
| 80 | buf, sizeof(buf)); |
| 81 | ALOGD(" %s", buf); |
| 82 | foundv6 = 1; |
| 83 | } |
| 84 | current = current->ai_next; |
| 85 | } |
| 86 | |
| 87 | freeaddrinfo(answer); |
| 88 | answer = NULL; |
| 89 | if (foundv4 == 1 || foundv6 != 1) { |
| 90 | ALOGD("getaddrinfo(ipv6.google.com) didn't find only v6"); |
| 91 | return JNI_FALSE; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // getnameinfo |
| 96 | struct sockaddr_in sa4; |
| 97 | sa4.sin_family = AF_INET; |
| 98 | sa4.sin_port = 0; |
| 99 | inet_pton(AF_INET, "173.252.110.27", &(sa4.sin_addr)); |
| 100 | |
| 101 | struct sockaddr_in6 sa6; |
| 102 | sa6.sin6_family = AF_INET6; |
| 103 | sa6.sin6_port = 0; |
| 104 | sa6.sin6_flowinfo = 0; |
| 105 | sa6.sin6_scope_id = 0; |
| 106 | inet_pton(AF_INET6, "2001:4860:4001:802::1008", &(sa6.sin6_addr)); |
| 107 | |
| 108 | char buf[NI_MAXHOST]; |
| 109 | int flags = NI_NAMEREQD; |
| 110 | |
| 111 | res = getnameinfo((const struct sockaddr*)&sa4, sizeof(sa4), buf, sizeof(buf), NULL, 0, flags); |
| 112 | if (res != 0) { |
| 113 | ALOGD("getnameinfo(173.252.110.27 (facebook) ) gave error %d (%s)", res, gai_strerror(res)); |
| 114 | return JNI_FALSE; |
| 115 | } |
| 116 | if (strstr(buf, "facebook.com") == NULL) { |
| 117 | ALOGD("getnameinfo(173.252.110.27 (facebook) ) didn't return facebook.com: %s", buf); |
| 118 | return JNI_FALSE; |
| 119 | } |
| 120 | |
| 121 | memset(buf, sizeof(buf), 0); |
| 122 | res = getnameinfo((const struct sockaddr*)&sa6, sizeof(sa6), buf, sizeof(buf), |
| 123 | NULL, 0, flags); |
| 124 | if (res != 0) { |
| 125 | ALOGD("getnameinfo(2a03:2880:2110:df01:face:b00c::8 (facebook) ) gave error %d (%s)", |
| 126 | res, gai_strerror(res)); |
| 127 | return JNI_FALSE; |
| 128 | } |
| 129 | if (strstr(buf, "1e100.net") == NULL) { |
| 130 | ALOGD("getnameinfo(2a03:2880:2110:df01:face:b00c::8) didn't return facebook.com: %s", buf); |
| 131 | return JNI_FALSE; |
| 132 | } |
| 133 | |
| 134 | // gethostbyname |
| 135 | struct hostent *my_hostent = gethostbyname("www.mit.edu"); |
| 136 | if (my_hostent == NULL) { |
| 137 | ALOGD("gethostbyname(www.mit.edu) gave null response"); |
| 138 | return JNI_FALSE; |
| 139 | } |
| 140 | if ((my_hostent->h_addr_list == NULL) || (*my_hostent->h_addr_list == NULL)) { |
| 141 | ALOGD("gethostbyname(www.mit.edu) gave 0 addresses"); |
| 142 | return JNI_FALSE; |
| 143 | } |
| 144 | { |
| 145 | char **current = my_hostent->h_addr_list; |
| 146 | while (*current != NULL) { |
| 147 | char buf[256]; |
| 148 | inet_ntop(my_hostent->h_addrtype, *current, buf, sizeof(buf)); |
| 149 | ALOGD("gethostbyname(www.mit.edu) gave %s", buf); |
| 150 | current++; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // gethostbyaddr |
| 155 | char addr6[16]; |
| 156 | inet_pton(AF_INET6, "2001:4b10:bbc::2", addr6); |
| 157 | my_hostent = gethostbyaddr(addr6, sizeof(addr6), AF_INET6); |
| 158 | if (my_hostent == NULL) { |
| 159 | ALOGD("gethostbyaddr(2001:4b10:bbc::2 (bbc) ) gave null response"); |
| 160 | return JNI_FALSE; |
| 161 | } |
| 162 | ALOGD("gethostbyaddr(2001:4b10:bbc::2 (bbc) ) gave %s for name", |
| 163 | my_hostent->h_name ? my_hostent->h_name : "null"); |
| 164 | if (my_hostent->h_name == NULL) return JNI_FALSE; |
| 165 | return JNI_TRUE; |
| 166 | } |