blob: 9a5ab9c422fac92bc1b56e4e78400d31a252225f [file] [log] [blame]
Erik Kline1c542da2015-05-14 17:20:39 +09001/*
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
35static const char kHostname[] = "connectivitycheck.android.com";
36
37
38JNIEXPORT 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
55JNIEXPORT 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
69JNIEXPORT 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
91static const int kSockaddrStrLen = INET6_ADDRSTRLEN + strlen("[]:65535");
92
93void 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
112JNIEXPORT 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
125 // Quoth Ian Swett:
126 // "QUIC always uses 80 and 443, but only 443 is used for secure(HTTPS) traffic."
127 int rval = android_getaddrinfofornetwork(handle, kHostname, "80", &kHints, &res);
128 if (rval != 0) {
129 ALOGD("android_getaddrinfofornetwork(%llu, %s) returned rval=%d errno=%d",
130 handle, kHostname, rval, errno);
131 freeaddrinfo(res);
132 return -errno;
133 }
134
135 // Rely upon getaddrinfo sorting the best destination to the front.
136 int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
137 if (fd < 0) {
138 ALOGD("socket(%d, %d, %d) failed, errno=%d",
139 res->ai_family, res->ai_socktype, res->ai_protocol, errno);
140 freeaddrinfo(res);
141 return -errno;
142 }
143
144 rval = android_setsocknetwork(handle, fd);
145 ALOGD("android_setprocnetwork(%llu, %d) returned rval=%d errno=%d",
146 handle, fd, rval, errno);
147 if (rval != 0) {
148 close(fd);
149 freeaddrinfo(res);
150 return -errno;
151 }
152
153 char addrstr[kSockaddrStrLen];
154 sockaddr_ntop(res->ai_addr, res->ai_addrlen, addrstr, sizeof(addrstr));
155 ALOGD("Attempting connect() to %s...", addrstr);
156
157 rval = connect(fd, res->ai_addr, res->ai_addrlen);
158 if (rval != 0) {
159 close(fd);
160 freeaddrinfo(res);
161 return -errno;
162 }
163 freeaddrinfo(res);
164
165 struct sockaddr_storage src_addr;
166 socklen_t src_addrlen = sizeof(src_addr);
167 if (getsockname(fd, (struct sockaddr *)&src_addr, &src_addrlen) != 0) {
168 close(fd);
169 return -errno;
170 }
171 sockaddr_ntop((const struct sockaddr *)&src_addr, sizeof(src_addr), addrstr, sizeof(addrstr));
172 ALOGD("... from %s", addrstr);
173
174 // Don't let reads or writes block indefinitely.
175 const struct timeval timeo = { 5, 0 }; // 5 seconds
176 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
177 setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo));
178
179 uint8_t quic_packet[] = {
180 0x0c, // public flags: 64bit conn ID, 8bit sequence number
181 0, 0, 0, 0, 0, 0, 0, 0, // 64bit connection ID
182 0x01, // sequence number
183 0x00, // private flags
184 0x07, // type: regular frame type "PING"
185 };
186
187 arc4random_buf(quic_packet + 1, 8); // random connection ID
188
189 ssize_t sent = send(fd, quic_packet, sizeof(quic_packet), 0);
190 if (sent < (ssize_t)sizeof(quic_packet)) {
191 ALOGD("send(QUIC packet) returned sent=%zd, errno=%d", sent, errno);
192 close(fd);
193 return -errno;
194 }
195
196 uint8_t response[1500];
197 ssize_t rcvd = recv(fd, response, sizeof(response), 0);
198 if (rcvd < sent) {
199 ALOGD("recv() returned rcvd=%zd, errno=%d", rcvd, errno);
200 close(fd);
201 return -errno;
202 }
203
204 int conn_id_cmp = memcmp(quic_packet + 1, response + 1, 8);
205 if (conn_id_cmp != 0) {
206 ALOGD("sent and received connection IDs do not match");
207 close(fd);
208 return -EPROTO;
209 }
210
211 // TODO: log, and compare to the IP address encoded in the
212 // response, since this should be a public reset packet.
213
214 close(fd);
215 return 0;
216}