1 /*
2 * Copyright (C) 2019 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
20 #include <arpa/inet.h>
21 #include <arpa/nameser.h>
22 #include <errno.h>
23 #include <inttypes.h>
24 #include <jni.h>
25 #include <netdb.h>
26 #include <poll.h> /* poll */
27 #include <resolv.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/socket.h>
32 #include <sys/time.h>
33
34 #include <string>
35
36 #include <android/log.h>
37 #include <android/multinetwork.h>
38 #include <nativehelper/JNIHelp.h>
39
40 #define LOGD(fmt, ...) \
41 __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##__VA_ARGS__)
42
43 // Since the tests in this file commonly pass expression statements as parameters to these macros,
44 // get the returned value of the statements to avoid statement double-called.
45 // By checking ExceptionCheck(), these macros don't throw another exception if an exception has
46 // been thrown, because ART's JNI disallows to throw another exception while an exception is
47 // pending (See CheckThread in check_jni.cc).
48 #define EXPECT_GE(env, actual_stmt, expected_stmt, msg) \
49 do { \
50 const auto expected = (expected_stmt); \
51 const auto actual = (actual_stmt); \
52 if (actual < expected && !env->ExceptionCheck()) { \
53 jniThrowExceptionFmt(env, "java/lang/AssertionError", \
54 "%s:%d: %s EXPECT_GE: expected %d, got %d", \
55 __FILE__, __LINE__, msg, expected, actual); \
56 } \
57 } while (0)
58
59 #define EXPECT_GT(env, actual_stmt, expected_stmt, msg) \
60 do { \
61 const auto expected = (expected_stmt); \
62 const auto actual = (actual_stmt); \
63 if (actual <= expected && !env->ExceptionCheck()) { \
64 jniThrowExceptionFmt(env, "java/lang/AssertionError", \
65 "%s:%d: %s EXPECT_GT: expected %d, got %d", \
66 __FILE__, __LINE__, msg, expected, actual); \
67 } \
68 } while (0)
69
70 #define EXPECT_EQ(env, expected_stmt, actual_stmt, msg) \
71 do { \
72 const auto expected = (expected_stmt); \
73 const auto actual = (actual_stmt); \
74 if (actual != expected && !env->ExceptionCheck()) { \
75 jniThrowExceptionFmt(env, "java/lang/AssertionError", \
76 "%s:%d: %s EXPECT_EQ: expected %d, got %d", \
77 __FILE__, __LINE__, msg, expected, actual); \
78 } \
79 } while (0)
80
81 static const int MAXPACKET = 8 * 1024;
82 static const int TIMEOUT_MS = 15000;
83 static const char kHostname[] = "connectivitycheck.android.com";
84 static const char kNxDomainName[] = "test1-nx.metric.gstatic.com";
85 static const char kGoogleName[] = "www.google.com";
86
makeQuery(const char * name,int qtype,uint8_t * buf,size_t buflen)87 int makeQuery(const char* name, int qtype, uint8_t* buf, size_t buflen) {
88 return res_mkquery(ns_o_query, name, ns_c_in, qtype, NULL, 0, NULL, buf, buflen);
89 }
90
getAsyncResponse(JNIEnv * env,int fd,int timeoutMs,int * rcode,uint8_t * buf,size_t bufLen)91 int getAsyncResponse(JNIEnv* env, int fd, int timeoutMs, int* rcode, uint8_t* buf, size_t bufLen) {
92 struct pollfd wait_fd = { .fd = fd, .events = POLLIN };
93
94 poll(&wait_fd, 1, timeoutMs);
95 if (wait_fd.revents & POLLIN) {
96 int n = android_res_nresult(fd, rcode, buf, bufLen);
97 // Verify that android_res_nresult() closed the fd
98 char dummy;
99 EXPECT_EQ(env, -1, read(fd, &dummy, sizeof(dummy)), "res_nresult check for closing fd");
100 EXPECT_EQ(env, EBADF, errno, "res_nresult check for errno");
101 return n;
102 }
103
104 return -ETIMEDOUT;
105 }
106
extractIpAddressAnswers(uint8_t * buf,size_t bufLen,int family)107 int extractIpAddressAnswers(uint8_t* buf, size_t bufLen, int family) {
108 ns_msg handle;
109 if (ns_initparse((const uint8_t*) buf, bufLen, &handle) < 0) {
110 return -errno;
111 }
112 const int ancount = ns_msg_count(handle, ns_s_an);
113 // Answer count = 0 is valid(e.g. response of query with root)
114 if (!ancount) {
115 return 0;
116 }
117 ns_rr rr;
118 bool hasValidAns = false;
119 for (int i = 0; i < ancount; i++) {
120 if (ns_parserr(&handle, ns_s_an, i, &rr) < 0) {
121 // If there is no valid answer, test will fail.
122 continue;
123 }
124
125 const int rtype = ns_rr_type(rr);
126 if (family == AF_INET) {
127 // If there is no expected address type, test will fail.
128 if (rtype != ns_t_a) continue;
129 } else if (family == AF_INET6) {
130 // If there is no expected address type, test will fail.
131 if (rtype != ns_t_aaaa) continue;
132 } else {
133 return -EAFNOSUPPORT;
134 }
135
136 const uint8_t* rdata = ns_rr_rdata(rr);
137 char buffer[INET6_ADDRSTRLEN];
138 if (inet_ntop(family, (const char*) rdata, buffer, sizeof(buffer)) == NULL) {
139 return -errno;
140 }
141 hasValidAns = true;
142 }
143 return hasValidAns ? 0 : -EBADMSG;
144 }
145
expectAnswersValid(JNIEnv * env,int fd,int family,int expectedRcode)146 int expectAnswersValid(JNIEnv* env, int fd, int family, int expectedRcode) {
147 int rcode = -1;
148 uint8_t buf[MAXPACKET] = {};
149 int res = getAsyncResponse(env, fd, TIMEOUT_MS, &rcode, buf, MAXPACKET);
150 if (res < 0) {
151 return res;
152 }
153
154 EXPECT_EQ(env, expectedRcode, rcode, "rcode is not expected");
155
156 if (expectedRcode == ns_r_noerror && res > 0) {
157 return extractIpAddressAnswers(buf, res, family);
158 }
159 return 0;
160 }
161
expectAnswersNotValid(JNIEnv * env,int fd,int expectedErrno)162 int expectAnswersNotValid(JNIEnv* env, int fd, int expectedErrno) {
163 int rcode = -1;
164 uint8_t buf[MAXPACKET] = {};
165 int res = getAsyncResponse(env, fd, TIMEOUT_MS, &rcode, buf, MAXPACKET);
166 if (res != expectedErrno) {
167 LOGD("res:%d, expectedErrno = %d", res, expectedErrno);
168 return (res > 0) ? -EREMOTEIO : res;
169 }
170 return 0;
171 }
172
173 extern "C"
Java_android_net_cts_MultinetworkApiTest_runResNqueryCheck(JNIEnv * env,jclass,jlong nethandle)174 JNIEXPORT void Java_android_net_cts_MultinetworkApiTest_runResNqueryCheck(
175 JNIEnv* env, jclass, jlong nethandle) {
176 net_handle_t handle = (net_handle_t) nethandle;
177
178 // V4
179 int fd = android_res_nquery(handle, kHostname, ns_c_in, ns_t_a, 0);
180 EXPECT_GE(env, fd, 0, "v4 res_nquery");
181 EXPECT_EQ(env, 0, expectAnswersValid(env, fd, AF_INET, ns_r_noerror),
182 "v4 res_nquery check answers");
183
184 // V6
185 fd = android_res_nquery(handle, kHostname, ns_c_in, ns_t_aaaa, 0);
186 EXPECT_GE(env, fd, 0, "v6 res_nquery");
187 EXPECT_EQ(env, 0, expectAnswersValid(env, fd, AF_INET6, ns_r_noerror),
188 "v6 res_nquery check answers");
189 }
190
191 extern "C"
Java_android_net_cts_MultinetworkApiTest_runResNsendCheck(JNIEnv * env,jclass,jlong nethandle)192 JNIEXPORT void Java_android_net_cts_MultinetworkApiTest_runResNsendCheck(
193 JNIEnv* env, jclass, jlong nethandle) {
194 net_handle_t handle = (net_handle_t) nethandle;
195 // V4
196 uint8_t buf1[MAXPACKET] = {};
197
198 int len1 = makeQuery(kGoogleName, ns_t_a, buf1, sizeof(buf1));
199 EXPECT_GT(env, len1, 0, "v4 res_mkquery 1st");
200
201 uint8_t buf2[MAXPACKET] = {};
202 int len2 = makeQuery(kHostname, ns_t_a, buf2, sizeof(buf2));
203 EXPECT_GT(env, len2, 0, "v4 res_mkquery 2nd");
204
205 int fd1 = android_res_nsend(handle, buf1, len1, 0);
206 EXPECT_GE(env, fd1, 0, "v4 res_nsend 1st");
207 int fd2 = android_res_nsend(handle, buf2, len2, 0);
208 EXPECT_GE(env, fd2, 0, "v4 res_nsend 2nd");
209
210 EXPECT_EQ(env, 0, expectAnswersValid(env, fd2, AF_INET, ns_r_noerror),
211 "v4 res_nsend 2nd check answers");
212 EXPECT_EQ(env, 0, expectAnswersValid(env, fd1, AF_INET, ns_r_noerror),
213 "v4 res_nsend 1st check answers");
214
215 // V6
216 memset(buf1, 0, sizeof(buf1));
217 memset(buf2, 0, sizeof(buf2));
218 len1 = makeQuery(kGoogleName, ns_t_aaaa, buf1, sizeof(buf1));
219 EXPECT_GT(env, len1, 0, "v6 res_mkquery 1st");
220 len2 = makeQuery(kHostname, ns_t_aaaa, buf2, sizeof(buf2));
221 EXPECT_GT(env, len2, 0, "v6 res_mkquery 2nd");
222
223 fd1 = android_res_nsend(handle, buf1, len1, 0);
224 EXPECT_GE(env, fd1, 0, "v6 res_nsend 1st");
225 fd2 = android_res_nsend(handle, buf2, len2, 0);
226 EXPECT_GE(env, fd2, 0, "v6 res_nsend 2nd");
227
228 EXPECT_EQ(env, 0, expectAnswersValid(env, fd2, AF_INET6, ns_r_noerror),
229 "v6 res_nsend 2nd check answers");
230 EXPECT_EQ(env, 0, expectAnswersValid(env, fd1, AF_INET6, ns_r_noerror),
231 "v6 res_nsend 1st check answers");
232 }
233
234 extern "C"
Java_android_net_cts_MultinetworkApiTest_runResNnxDomainCheck(JNIEnv * env,jclass,jlong nethandle)235 JNIEXPORT void Java_android_net_cts_MultinetworkApiTest_runResNnxDomainCheck(
236 JNIEnv* env, jclass, jlong nethandle) {
237 net_handle_t handle = (net_handle_t) nethandle;
238
239 // res_nquery V4 NXDOMAIN
240 int fd = android_res_nquery(handle, kNxDomainName, ns_c_in, ns_t_a, 0);
241 EXPECT_GE(env, fd, 0, "v4 res_nquery NXDOMAIN");
242 EXPECT_EQ(env, 0, expectAnswersValid(env, fd, AF_INET, ns_r_nxdomain),
243 "v4 res_nquery NXDOMAIN check answers");
244
245 // res_nquery V6 NXDOMAIN
246 fd = android_res_nquery(handle, kNxDomainName, ns_c_in, ns_t_aaaa, 0);
247 EXPECT_GE(env, fd, 0, "v6 res_nquery NXDOMAIN");
248 EXPECT_EQ(env, 0, expectAnswersValid(env, fd, AF_INET6, ns_r_nxdomain),
249 "v6 res_nquery NXDOMAIN check answers");
250
251 uint8_t buf[MAXPACKET] = {};
252 // res_nsend V4 NXDOMAIN
253 int len = makeQuery(kNxDomainName, ns_t_a, buf, sizeof(buf));
254 EXPECT_GT(env, len, 0, "v4 res_mkquery NXDOMAIN");
255 fd = android_res_nsend(handle, buf, len, 0);
256 EXPECT_GE(env, fd, 0, "v4 res_nsend NXDOMAIN");
257 EXPECT_EQ(env, 0, expectAnswersValid(env, fd, AF_INET, ns_r_nxdomain),
258 "v4 res_nsend NXDOMAIN check answers");
259
260 // res_nsend V6 NXDOMAIN
261 memset(buf, 0, sizeof(buf));
262 len = makeQuery(kNxDomainName, ns_t_aaaa, buf, sizeof(buf));
263 EXPECT_GT(env, len, 0, "v6 res_mkquery NXDOMAIN");
264 fd = android_res_nsend(handle, buf, len, 0);
265 EXPECT_GE(env, fd, 0, "v6 res_nsend NXDOMAIN");
266 EXPECT_EQ(env, 0, expectAnswersValid(env, fd, AF_INET6, ns_r_nxdomain),
267 "v6 res_nsend NXDOMAIN check answers");
268 }
269
270
271 extern "C"
Java_android_net_cts_MultinetworkApiTest_runResNcancelCheck(JNIEnv * env,jclass,jlong nethandle)272 JNIEXPORT void Java_android_net_cts_MultinetworkApiTest_runResNcancelCheck(
273 JNIEnv* env, jclass, jlong nethandle) {
274 net_handle_t handle = (net_handle_t) nethandle;
275
276 int fd = android_res_nquery(handle, kGoogleName, ns_c_in, ns_t_a, 0);
277 errno = 0;
278 android_res_cancel(fd);
279 int err = errno;
280 EXPECT_EQ(env, 0, err, "res_cancel");
281 // DO NOT call cancel or result with the same fd more than once,
282 // otherwise it will hit fdsan double-close fd.
283 }
284
285 extern "C"
Java_android_net_cts_MultinetworkApiTest_runResNapiMalformedCheck(JNIEnv * env,jclass,jlong nethandle)286 JNIEXPORT void Java_android_net_cts_MultinetworkApiTest_runResNapiMalformedCheck(
287 JNIEnv* env, jclass, jlong nethandle) {
288 net_handle_t handle = (net_handle_t) nethandle;
289
290 // It is the equivalent of "dig . a", Query with an empty name.
291 int fd = android_res_nquery(handle, "", ns_c_in, ns_t_a, 0);
292 EXPECT_GE(env, fd, 0, "res_nquery root");
293 EXPECT_EQ(env, 0, expectAnswersValid(env, fd, AF_INET, ns_r_noerror),
294 "res_nquery root check answers");
295
296 // Label limit 63
297 std::string exceedingLabelQuery = "www." + std::string(70, 'g') + ".com";
298 // Name limit 255
299 std::string exceedingDomainQuery = "www." + std::string(255, 'g') + ".com";
300
301 fd = android_res_nquery(handle, exceedingLabelQuery.c_str(), ns_c_in, ns_t_a, 0);
302 EXPECT_EQ(env, -EMSGSIZE, fd, "res_nquery exceedingLabelQuery");
303 fd = android_res_nquery(handle, exceedingDomainQuery.c_str(), ns_c_in, ns_t_aaaa, 0);
304 EXPECT_EQ(env, -EMSGSIZE, fd, "res_nquery exceedingDomainQuery");
305
306 uint8_t buf[10] = {};
307 // empty BLOB
308 fd = android_res_nsend(handle, buf, 10, 0);
309 EXPECT_GE(env, fd, 0, "res_nsend empty BLOB");
310 EXPECT_EQ(env, 0, expectAnswersNotValid(env, fd, -EINVAL),
311 "res_nsend empty BLOB check answers");
312
313 uint8_t largeBuf[2 * MAXPACKET] = {};
314 // A buffer larger than 8KB
315 fd = android_res_nsend(handle, largeBuf, sizeof(largeBuf), 0);
316 EXPECT_EQ(env, -EMSGSIZE, fd, "res_nsend buffer larger than 8KB");
317
318 // 5000 bytes filled with 0. This returns EMSGSIZE because FrameworkListener limits the size of
319 // commands to 4096 bytes.
320 fd = android_res_nsend(handle, largeBuf, 5000, 0);
321 EXPECT_EQ(env, -EMSGSIZE, fd, "res_nsend 5000 bytes filled with 0");
322
323 // 500 bytes filled with 0
324 fd = android_res_nsend(handle, largeBuf, 500, 0);
325 EXPECT_GE(env, fd, 0, "res_nsend 500 bytes filled with 0");
326 EXPECT_EQ(env, 0, expectAnswersNotValid(env, fd, -EINVAL),
327 "res_nsend 500 bytes filled with 0 check answers");
328
329 // 5000 bytes filled with 0xFF
330 uint8_t ffBuf[5001] = {};
331 memset(ffBuf, 0xFF, sizeof(ffBuf));
332 ffBuf[5000] = '\0';
333 fd = android_res_nsend(handle, ffBuf, sizeof(ffBuf), 0);
334 EXPECT_EQ(env, -EMSGSIZE, fd, "res_nsend 5000 bytes filled with 0xFF");
335
336 // 500 bytes filled with 0xFF
337 ffBuf[500] = '\0';
338 fd = android_res_nsend(handle, ffBuf, 501, 0);
339 EXPECT_GE(env, fd, 0, "res_nsend 500 bytes filled with 0xFF");
340 EXPECT_EQ(env, 0, expectAnswersNotValid(env, fd, -EINVAL),
341 "res_nsend 500 bytes filled with 0xFF check answers");
342 }
343
344 extern "C"
Java_android_net_cts_MultinetworkApiTest_runGetaddrinfoCheck(JNIEnv *,jclass,jlong nethandle)345 JNIEXPORT jint Java_android_net_cts_MultinetworkApiTest_runGetaddrinfoCheck(
346 JNIEnv*, jclass, jlong nethandle) {
347 net_handle_t handle = (net_handle_t) nethandle;
348 struct addrinfo *res = NULL;
349
350 errno = 0;
351 int rval = android_getaddrinfofornetwork(handle, kHostname, NULL, NULL, &res);
352 const int saved_errno = errno;
353 freeaddrinfo(res);
354
355 LOGD("android_getaddrinfofornetwork(%" PRIu64 ", %s) returned rval=%d errno=%d",
356 handle, kHostname, rval, saved_errno);
357 return rval == 0 ? 0 : -saved_errno;
358 }
359
360 extern "C"
Java_android_net_cts_MultinetworkApiTest_runSetprocnetwork(JNIEnv *,jclass,jlong nethandle)361 JNIEXPORT jint Java_android_net_cts_MultinetworkApiTest_runSetprocnetwork(
362 JNIEnv*, jclass, jlong nethandle) {
363 net_handle_t handle = (net_handle_t) nethandle;
364
365 errno = 0;
366 int rval = android_setprocnetwork(handle);
367 const int saved_errno = errno;
368 LOGD("android_setprocnetwork(%" PRIu64 ") returned rval=%d errno=%d",
369 handle, rval, saved_errno);
370 return rval == 0 ? 0 : -saved_errno;
371 }
372
373 extern "C"
Java_android_net_cts_MultinetworkApiTest_runSetsocknetwork(JNIEnv *,jclass,jlong nethandle)374 JNIEXPORT jint Java_android_net_cts_MultinetworkApiTest_runSetsocknetwork(
375 JNIEnv*, jclass, jlong nethandle) {
376 net_handle_t handle = (net_handle_t) nethandle;
377
378 errno = 0;
379 int fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
380 if (fd < 0) {
381 LOGD("socket() failed, errno=%d", errno);
382 return -errno;
383 }
384
385 errno = 0;
386 int rval = android_setsocknetwork(handle, fd);
387 const int saved_errno = errno;
388 LOGD("android_setprocnetwork(%" PRIu64 ", %d) returned rval=%d errno=%d",
389 handle, fd, rval, saved_errno);
390 close(fd);
391 return rval == 0 ? 0 : -saved_errno;
392 }
393
394 // Use sizeof("x") - 1 because we need a compile-time constant, and strlen("x")
395 // isn't guaranteed to fold to a constant.
396 static const int kSockaddrStrLen = INET6_ADDRSTRLEN + sizeof("[]:65535") - 1;
397
sockaddr_ntop(const struct sockaddr * sa,socklen_t salen,char * dst,const size_t size)398 void sockaddr_ntop(const struct sockaddr *sa, socklen_t salen, char *dst, const size_t size) {
399 char addrstr[INET6_ADDRSTRLEN];
400 char portstr[sizeof("65535")];
401 char buf[kSockaddrStrLen+1];
402
403 int ret = getnameinfo(sa, salen,
404 addrstr, sizeof(addrstr),
405 portstr, sizeof(portstr),
406 NI_NUMERICHOST | NI_NUMERICSERV);
407 if (ret == 0) {
408 snprintf(buf, sizeof(buf),
409 (sa->sa_family == AF_INET6) ? "[%s]:%s" : "%s:%s",
410 addrstr, portstr);
411 } else {
412 sprintf(buf, "???");
413 }
414
415 strlcpy(dst, buf, size);
416 }
417
418 extern "C"
Java_android_net_cts_MultinetworkApiTest_runDatagramCheck(JNIEnv *,jclass,jlong nethandle)419 JNIEXPORT jint Java_android_net_cts_MultinetworkApiTest_runDatagramCheck(
420 JNIEnv*, jclass, jlong nethandle) {
421 const struct addrinfo kHints = {
422 .ai_flags = AI_ADDRCONFIG,
423 .ai_family = AF_UNSPEC,
424 .ai_socktype = SOCK_DGRAM,
425 .ai_protocol = IPPROTO_UDP,
426 };
427 struct addrinfo *res = NULL;
428 net_handle_t handle = (net_handle_t) nethandle;
429
430 static const char kPort[] = "443";
431 int rval = android_getaddrinfofornetwork(handle, kHostname, kPort, &kHints, &res);
432 if (rval != 0) {
433 LOGD("android_getaddrinfofornetwork(%llu, %s) returned rval=%d errno=%d",
434 handle, kHostname, rval, errno);
435 freeaddrinfo(res);
436 return -errno;
437 }
438
439 // Rely upon getaddrinfo sorting the best destination to the front.
440 int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
441 if (fd < 0) {
442 LOGD("socket(%d, %d, %d) failed, errno=%d",
443 res->ai_family, res->ai_socktype, res->ai_protocol, errno);
444 freeaddrinfo(res);
445 return -errno;
446 }
447
448 rval = android_setsocknetwork(handle, fd);
449 LOGD("android_setprocnetwork(%llu, %d) returned rval=%d errno=%d",
450 handle, fd, rval, errno);
451 if (rval != 0) {
452 close(fd);
453 freeaddrinfo(res);
454 return -errno;
455 }
456
457 char addrstr[kSockaddrStrLen+1];
458 sockaddr_ntop(res->ai_addr, res->ai_addrlen, addrstr, sizeof(addrstr));
459 LOGD("Attempting connect() to %s ...", addrstr);
460
461 rval = connect(fd, res->ai_addr, res->ai_addrlen);
462 if (rval != 0) {
463 close(fd);
464 freeaddrinfo(res);
465 return -errno;
466 }
467 freeaddrinfo(res);
468
469 struct sockaddr_storage src_addr;
470 socklen_t src_addrlen = sizeof(src_addr);
471 if (getsockname(fd, (struct sockaddr *)&src_addr, &src_addrlen) != 0) {
472 close(fd);
473 return -errno;
474 }
475 sockaddr_ntop((const struct sockaddr *)&src_addr, sizeof(src_addr), addrstr, sizeof(addrstr));
476 LOGD("... from %s", addrstr);
477
478 // Don't let reads or writes block indefinitely.
479 const struct timeval timeo = { 2, 0 }; // 2 seconds
480 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
481 setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo));
482
483 // For reference see:
484 // https://datatracker.ietf.org/doc/html/draft-ietf-quic-invariants
485 uint8_t quic_packet[1200] = {
486 0xc0, // long header
487 0xaa, 0xda, 0xca, 0xca, // reserved-space version number
488 0x08, // destination connection ID length
489 0, 0, 0, 0, 0, 0, 0, 0, // 64bit connection ID
490 0x00, // source connection ID length
491 };
492
493 arc4random_buf(quic_packet + 6, 8); // random connection ID
494
495 uint8_t response[1500];
496 ssize_t sent, rcvd;
497 static const int MAX_RETRIES = 5;
498 int i, errnum = 0;
499
500 for (i = 0; i < MAX_RETRIES; i++) {
501 sent = send(fd, quic_packet, sizeof(quic_packet), 0);
502 if (sent < (ssize_t)sizeof(quic_packet)) {
503 errnum = errno;
504 LOGD("send(QUIC packet) returned sent=%zd, errno=%d", sent, errnum);
505 close(fd);
506 return -errnum;
507 }
508
509 rcvd = recv(fd, response, sizeof(response), 0);
510 if (rcvd > 0) {
511 break;
512 } else {
513 errnum = errno;
514 LOGD("[%d/%d] recv(QUIC response) returned rcvd=%zd, errno=%d",
515 i + 1, MAX_RETRIES, rcvd, errnum);
516 }
517 }
518 if (rcvd < 15) {
519 LOGD("QUIC UDP %s: sent=%zd but rcvd=%zd, errno=%d", kPort, sent, rcvd, errnum);
520 if (rcvd <= 0) {
521 LOGD("Does this network block UDP port %s?", kPort);
522 }
523 close(fd);
524 return -EPROTO;
525 }
526
527 int conn_id_cmp = memcmp(quic_packet + 6, response + 7, 8);
528 if (conn_id_cmp != 0) {
529 LOGD("sent and received connection IDs do not match");
530 close(fd);
531 return -EPROTO;
532 }
533
534 // TODO: Replace this quick 'n' dirty test with proper QUIC-capable code.
535
536 close(fd);
537 return 0;
538 }
539