Erik Kline | 1c542da | 2015-05-14 17:20:39 +0900 | [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 | |
| 18 | #define LOG_TAG "MultinetworkApiTest" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <arpa/inet.h> |
| 22 | #include <errno.h> |
| 23 | #include <inttypes.h> |
| 24 | #include <jni.h> |
| 25 | #include <netdb.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/socket.h> |
| 30 | #include <sys/time.h> |
| 31 | #include <android/multinetwork.h> |
| 32 | |
| 33 | #define UNUSED(X) ((void) X) |
| 34 | |
| 35 | static const char kHostname[] = "connectivitycheck.android.com"; |
| 36 | |
| 37 | |
| 38 | JNIEXPORT jint Java_android_net_cts_MultinetworkApiTest_runGetaddrinfoCheck( |
| 39 | JNIEnv* env, jclass class, jlong nethandle) { |
| 40 | UNUSED(env); |
| 41 | UNUSED(class); |
| 42 | net_handle_t handle = (net_handle_t) nethandle; |
| 43 | struct addrinfo *res = NULL; |
| 44 | |
| 45 | errno = 0; |
| 46 | int rval = android_getaddrinfofornetwork(handle, kHostname, NULL, NULL, &res); |
| 47 | const int saved_errno = errno; |
| 48 | freeaddrinfo(res); |
| 49 | |
| 50 | ALOGD("android_getaddrinfofornetwork(%llu, %s) returned rval=%d errno=%d", |
| 51 | handle, kHostname, rval, saved_errno); |
| 52 | return rval == 0 ? 0 : -saved_errno; |
| 53 | } |
| 54 | |
| 55 | JNIEXPORT jint Java_android_net_cts_MultinetworkApiTest_runSetprocnetwork( |
| 56 | JNIEnv* env, jclass class, jlong nethandle) { |
| 57 | UNUSED(env); |
| 58 | UNUSED(class); |
| 59 | net_handle_t handle = (net_handle_t) nethandle; |
| 60 | |
| 61 | errno = 0; |
| 62 | int rval = android_setprocnetwork(handle); |
| 63 | const int saved_errno = errno; |
| 64 | ALOGD("android_setprocnetwork(%llu) returned rval=%d errno=%d", |
| 65 | handle, rval, saved_errno); |
| 66 | return rval == 0 ? 0 : -saved_errno; |
| 67 | } |
| 68 | |
| 69 | JNIEXPORT jint Java_android_net_cts_MultinetworkApiTest_runSetsocknetwork( |
| 70 | JNIEnv* env, jclass class, jlong nethandle) { |
| 71 | UNUSED(env); |
| 72 | UNUSED(class); |
| 73 | net_handle_t handle = (net_handle_t) nethandle; |
| 74 | |
| 75 | errno = 0; |
| 76 | int fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); |
| 77 | if (fd < 0) { |
| 78 | ALOGD("socket() failed, errno=%d", errno); |
| 79 | return -errno; |
| 80 | } |
| 81 | |
| 82 | errno = 0; |
| 83 | int rval = android_setsocknetwork(handle, fd); |
| 84 | const int saved_errno = errno; |
| 85 | ALOGD("android_setprocnetwork(%llu, %d) returned rval=%d errno=%d", |
| 86 | handle, fd, rval, saved_errno); |
| 87 | close(fd); |
| 88 | return rval == 0 ? 0 : -saved_errno; |
| 89 | } |
| 90 | |
| 91 | static const int kSockaddrStrLen = INET6_ADDRSTRLEN + strlen("[]:65535"); |
| 92 | |
| 93 | void sockaddr_ntop(const struct sockaddr *sa, socklen_t salen, char *dst, const size_t size) { |
| 94 | char addrstr[INET6_ADDRSTRLEN]; |
| 95 | char portstr[sizeof("65535")]; |
| 96 | char buf[sizeof(addrstr) + sizeof(portstr) + sizeof("[]:")]; |
| 97 | int ret = getnameinfo(sa, salen, |
| 98 | addrstr, sizeof(addrstr), |
| 99 | portstr, sizeof(portstr), |
| 100 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 101 | if (ret == 0) { |
| 102 | snprintf(buf, sizeof(buf), |
| 103 | (sa->sa_family == AF_INET6) ? "[%s]:%s" : "%s:%s", |
| 104 | addrstr, portstr); |
| 105 | } else { |
| 106 | sprintf(buf, "???"); |
| 107 | } |
| 108 | |
| 109 | strlcpy(dst, buf, (strlen(buf) < size - 1) ? strlen(buf) : size - 1); |
| 110 | } |
| 111 | |
| 112 | JNIEXPORT jint Java_android_net_cts_MultinetworkApiTest_runDatagramCheck( |
| 113 | JNIEnv* env, jclass class, jlong nethandle) { |
| 114 | UNUSED(env); |
| 115 | UNUSED(class); |
| 116 | const struct addrinfo kHints = { |
| 117 | .ai_flags = AI_ADDRCONFIG, |
| 118 | .ai_family = AF_UNSPEC, |
| 119 | .ai_socktype = SOCK_DGRAM, |
| 120 | .ai_protocol = IPPROTO_UDP, |
| 121 | }; |
| 122 | struct addrinfo *res = NULL; |
| 123 | net_handle_t handle = (net_handle_t) nethandle; |
| 124 | |
Erik Kline | e2815d3 | 2015-07-24 02:23:15 +0900 | [diff] [blame] | 125 | int rval = android_getaddrinfofornetwork(handle, kHostname, "443", &kHints, &res); |
Erik Kline | 1c542da | 2015-05-14 17:20:39 +0900 | [diff] [blame] | 126 | if (rval != 0) { |
| 127 | ALOGD("android_getaddrinfofornetwork(%llu, %s) returned rval=%d errno=%d", |
| 128 | handle, kHostname, rval, errno); |
| 129 | freeaddrinfo(res); |
| 130 | return -errno; |
| 131 | } |
| 132 | |
| 133 | // Rely upon getaddrinfo sorting the best destination to the front. |
| 134 | int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 135 | if (fd < 0) { |
| 136 | ALOGD("socket(%d, %d, %d) failed, errno=%d", |
| 137 | res->ai_family, res->ai_socktype, res->ai_protocol, errno); |
| 138 | freeaddrinfo(res); |
| 139 | return -errno; |
| 140 | } |
| 141 | |
| 142 | rval = android_setsocknetwork(handle, fd); |
| 143 | ALOGD("android_setprocnetwork(%llu, %d) returned rval=%d errno=%d", |
| 144 | handle, fd, rval, errno); |
| 145 | if (rval != 0) { |
| 146 | close(fd); |
| 147 | freeaddrinfo(res); |
| 148 | return -errno; |
| 149 | } |
| 150 | |
| 151 | char addrstr[kSockaddrStrLen]; |
| 152 | sockaddr_ntop(res->ai_addr, res->ai_addrlen, addrstr, sizeof(addrstr)); |
| 153 | ALOGD("Attempting connect() to %s...", addrstr); |
| 154 | |
| 155 | rval = connect(fd, res->ai_addr, res->ai_addrlen); |
| 156 | if (rval != 0) { |
| 157 | close(fd); |
| 158 | freeaddrinfo(res); |
| 159 | return -errno; |
| 160 | } |
| 161 | freeaddrinfo(res); |
| 162 | |
| 163 | struct sockaddr_storage src_addr; |
| 164 | socklen_t src_addrlen = sizeof(src_addr); |
| 165 | if (getsockname(fd, (struct sockaddr *)&src_addr, &src_addrlen) != 0) { |
| 166 | close(fd); |
| 167 | return -errno; |
| 168 | } |
| 169 | sockaddr_ntop((const struct sockaddr *)&src_addr, sizeof(src_addr), addrstr, sizeof(addrstr)); |
| 170 | ALOGD("... from %s", addrstr); |
| 171 | |
| 172 | // Don't let reads or writes block indefinitely. |
| 173 | const struct timeval timeo = { 5, 0 }; // 5 seconds |
| 174 | setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)); |
| 175 | setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)); |
| 176 | |
| 177 | uint8_t quic_packet[] = { |
| 178 | 0x0c, // public flags: 64bit conn ID, 8bit sequence number |
| 179 | 0, 0, 0, 0, 0, 0, 0, 0, // 64bit connection ID |
| 180 | 0x01, // sequence number |
| 181 | 0x00, // private flags |
| 182 | 0x07, // type: regular frame type "PING" |
| 183 | }; |
| 184 | |
| 185 | arc4random_buf(quic_packet + 1, 8); // random connection ID |
| 186 | |
| 187 | ssize_t sent = send(fd, quic_packet, sizeof(quic_packet), 0); |
| 188 | if (sent < (ssize_t)sizeof(quic_packet)) { |
| 189 | ALOGD("send(QUIC packet) returned sent=%zd, errno=%d", sent, errno); |
| 190 | close(fd); |
| 191 | return -errno; |
| 192 | } |
| 193 | |
| 194 | uint8_t response[1500]; |
| 195 | ssize_t rcvd = recv(fd, response, sizeof(response), 0); |
| 196 | if (rcvd < sent) { |
| 197 | ALOGD("recv() returned rcvd=%zd, errno=%d", rcvd, errno); |
| 198 | close(fd); |
| 199 | return -errno; |
| 200 | } |
| 201 | |
| 202 | int conn_id_cmp = memcmp(quic_packet + 1, response + 1, 8); |
| 203 | if (conn_id_cmp != 0) { |
| 204 | ALOGD("sent and received connection IDs do not match"); |
| 205 | close(fd); |
| 206 | return -EPROTO; |
| 207 | } |
| 208 | |
| 209 | // TODO: log, and compare to the IP address encoded in the |
| 210 | // response, since this should be a public reset packet. |
| 211 | |
| 212 | close(fd); |
| 213 | return 0; |
| 214 | } |