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