Debian Codegraph @

tests/isc/netmgr_common.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <inttypes.h>
15 #include <sched.h> /* IWYU pragma: keep */
16 #include <setjmp.h>
17 #include <signal.h>
18 #include <stdarg.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21
22 /*
23 * As a workaround, include an OpenSSL header file before including cmocka.h,
24 * because OpenSSL 3.1.0 uses __attribute__(malloc), conflicting with a
25 * redefined malloc in cmocka.h.
26 */
27 #include <openssl/err.h>
28
29 #define UNIT_TESTING
30 #include <cmocka.h>
31
32 #include <isc/async.h>
33 #include <isc/nonce.h>
34 #include <isc/os.h>
35 #include <isc/quota.h>
36 #include <isc/refcount.h>
37 #include <isc/sockaddr.h>
38 #include <isc/thread.h>
39 #include <isc/util.h>
40 #include <isc/uv.h>
41 #define KEEP_BEFORE
42
43 #include "netmgr_common.h"
44
45 #include <tests/isc.h>
46
47 isc_nm_t *listen_nm = NULL;
48 isc_nm_t *connect_nm = NULL;
49
50 isc_sockaddr_t tcp_listen_addr;
51 isc_sockaddr_t tcp_connect_addr;
52 isc_tlsctx_t *tcp_listen_tlsctx = NULL;
53 isc_tlsctx_t *tcp_connect_tlsctx = NULL;
54 isc_tlsctx_client_session_cache_t *tcp_tlsctx_client_sess_cache = NULL;
55
56 isc_sockaddr_t udp_listen_addr;
57 isc_sockaddr_t udp_connect_addr;
58
59 uint64_t send_magic = 0;
60
61 isc_region_t send_msg = { .base = (unsigned char *)&send_magic,
62 .length = sizeof(send_magic) };
63
64 atomic_bool do_send = false;
65
66 atomic_int_fast64_t nsends = 0;
67 int_fast64_t esends = 0; /* expected sends */
68
69 atomic_int_fast64_t ssends = 0;
70 atomic_int_fast64_t sreads = 0;
71 atomic_int_fast64_t saccepts = 0;
72
73 atomic_int_fast64_t cconnects = 0;
74 atomic_int_fast64_t csends = 0;
75 atomic_int_fast64_t creads = 0;
76 atomic_int_fast64_t ctimeouts = 0;
77
78 int expected_ssends;
79 int expected_sreads;
80 int expected_csends;
81 int expected_cconnects;
82 int expected_creads;
83 int expected_saccepts;
84 int expected_ctimeouts;
85
86 bool ssends_shutdown;
87 bool sreads_shutdown;
88 bool saccepts_shutdown;
89 bool csends_shutdown;
90 bool cconnects_shutdown;
91 bool creads_shutdown;
92 bool ctimeouts_shutdown;
93
94 isc_refcount_t active_cconnects = 0;
95 isc_refcount_t active_csends = 0;
96 isc_refcount_t active_creads = 0;
97 isc_refcount_t active_ssends = 0;
98 isc_refcount_t active_sreads = 0;
99
100 isc_nmsocket_t *listen_sock = NULL;
101
102 isc_quota_t listener_quota;
103 atomic_bool check_listener_quota = false;
104
105 bool allow_send_back = false;
106 bool noanswer = false;
107 bool stream_use_TLS = false;
108 bool stream_use_PROXY = false;
109 bool stream_PROXY_over_TLS = false;
110 bool stream = false;
111 in_port_t stream_port = 0;
112
113 bool udp_use_PROXY = false;
114
115 isc_nm_recv_cb_t connect_readcb = NULL;
116
117 isc_nm_proxyheader_info_t proxy_info_data;
118 isc_nm_proxyheader_info_t *proxy_info = NULL;
119 isc_sockaddr_t proxy_src;
120 isc_sockaddr_t proxy_dst;
121
122 int
123 setup_netmgr_test(void **state) {
124 struct in_addr in;
125 tcp_connect_addr = (isc_sockaddr_t){ .length = 0 };
126 isc_sockaddr_fromin6(&tcp_connect_addr, &in6addr_loopback, 0);
127
128 tcp_listen_addr = (isc_sockaddr_t){ .length = 0 };
129 isc_sockaddr_fromin6(&tcp_listen_addr, &in6addr_loopback, stream_port);
130
131 RUNTIME_CHECK(inet_pton(AF_INET, "1.2.3.4", &in) == 1);
132 isc_sockaddr_fromin(&proxy_src, &in, 1234);
133 RUNTIME_CHECK(inet_pton(AF_INET, "4.3.2.1", &in) == 1);
134 isc_sockaddr_fromin(&proxy_dst, &in, 4321);
135 isc_nm_proxyheader_info_init(&proxy_info_data, &proxy_src, &proxy_dst,
136 NULL);
137
138 esends = NSENDS * workers;
139
140 atomic_store(&nsends, esends);
141
142 atomic_store(&saccepts, 0);
143 atomic_store(&sreads, 0);
144 atomic_store(&ssends, 0);
145
146 atomic_store(&cconnects, 0);
147 atomic_store(&csends, 0);
148 atomic_store(&creads, 0);
149 atomic_store(&ctimeouts, 0);
150 allow_send_back = false;
151
152 expected_cconnects = -1;
153 expected_csends = -1;
154 expected_creads = -1;
155 expected_sreads = -1;
156 expected_ssends = -1;
157 expected_saccepts = -1;
158 expected_ctimeouts = -1;
159
160 ssends_shutdown = true;
161 sreads_shutdown = true;
162 saccepts_shutdown = true;
163 csends_shutdown = true;
164 cconnects_shutdown = true;
165 creads_shutdown = true;
166 ctimeouts_shutdown = true;
167
168 do_send = false;
169
170 isc_refcount_init(&active_cconnects, 0);
171 isc_refcount_init(&active_csends, 0);
172 isc_refcount_init(&active_creads, 0);
173 isc_refcount_init(&active_ssends, 0);
174 isc_refcount_init(&active_sreads, 0);
175
176 isc_nonce_buf(&send_magic, sizeof(send_magic));
177
178 setup_loopmgr(state);
179 isc_netmgr_create(mctx, loopmgr, &listen_nm);
180 assert_non_null(listen_nm);
181 isc_nm_settimeouts(listen_nm, T_INIT, T_IDLE, T_KEEPALIVE,
182 T_ADVERTISED);
183
184 isc_netmgr_create(mctx, loopmgr, &connect_nm);
185 assert_non_null(connect_nm);
186 isc_nm_settimeouts(connect_nm, T_INIT, T_IDLE, T_KEEPALIVE,
187 T_ADVERTISED);
188
189 isc_quota_init(&listener_quota, 0);
190 atomic_store(&check_listener_quota, false);
191
192 connect_readcb = connect_read_cb;
193 noanswer = false;
194
195 if (isc_tlsctx_createserver(NULL, NULL, &tcp_listen_tlsctx) !=
196 ISC_R_SUCCESS)
197 {
198 return -1;
199 }
200 if (isc_tlsctx_createclient(&tcp_connect_tlsctx) != ISC_R_SUCCESS) {
201 return -1;
202 }
203
204 isc_tlsctx_enable_dot_client_alpn(tcp_connect_tlsctx);
205
206 isc_tlsctx_client_session_cache_create(
207 mctx, tcp_connect_tlsctx,
208 ISC_TLSCTX_CLIENT_SESSION_CACHE_DEFAULT_SIZE,
209 &tcp_tlsctx_client_sess_cache);
210
211 return 0;
212 }
213
214 int
215 teardown_netmgr_test(void **state ISC_ATTR_UNUSED) {
216 UNUSED(state);
217
218 isc_tlsctx_client_session_cache_detach(&tcp_tlsctx_client_sess_cache);
219
220 isc_tlsctx_free(&tcp_connect_tlsctx);
221 isc_tlsctx_free(&tcp_listen_tlsctx);
222
223 isc_netmgr_destroy(&connect_nm);
224 assert_null(connect_nm);
225
226 isc_netmgr_destroy(&listen_nm);
227 assert_null(listen_nm);
228
229 teardown_loopmgr(state);
230
231 isc_refcount_destroy(&active_cconnects);
232 isc_refcount_destroy(&active_csends);
233 isc_refcount_destroy(&active_creads);
234 isc_refcount_destroy(&active_ssends);
235 isc_refcount_destroy(&active_sreads);
236
237 proxy_info = NULL;
238
239 return 0;
240 }
241
242 void
243 stop_listening(void *arg ISC_ATTR_UNUSED) {
244 isc_nm_stoplistening(listen_sock);
245 isc_nmsocket_close(&listen_sock);
246 assert_null(listen_sock);
247 }
248
249 /* Callbacks */
250
251 void
252 noop_recv_cb(isc_nmhandle_t *handle ISC_ATTR_UNUSED,
253 isc_result_t eresult ISC_ATTR_UNUSED,
254 isc_region_t *region ISC_ATTR_UNUSED,
255 void *cbarg ISC_ATTR_UNUSED) {
256 F();
257 }
258
259 isc_result_t
260 noop_accept_cb(isc_nmhandle_t *handle ISC_ATTR_UNUSED, isc_result_t eresult,
261 void *cbarg ISC_ATTR_UNUSED) {
262 F();
263
264 if (eresult == ISC_R_SUCCESS) {
265 (void)atomic_fetch_add(&saccepts, 1);
266 }
267
268 return ISC_R_SUCCESS;
269 }
270
271 void
272 connect_send_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
273
274 void
275 connect_send(isc_nmhandle_t *handle);
276
277 void
278 connect_send_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
279 isc_nmhandle_t *sendhandle = handle;
280
281 assert_non_null(sendhandle);
282
283 UNUSED(cbarg);
284
285 F();
286
287 switch (eresult) {
288 case ISC_R_EOF:
289 case ISC_R_SHUTTINGDOWN:
290 case ISC_R_CANCELED:
291 case ISC_R_CONNECTIONRESET:
292 /* Abort */
293 if (!stream) {
294 isc_nm_cancelread(handle);
295 }
296 break;
297 case ISC_R_SUCCESS:
298 if (have_expected_csends(atomic_fetch_add(&csends, 1) + 1)) {
299 do_csends_shutdown(loopmgr);
300 }
301 break;
302 default:
303 fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
304 isc_result_totext(eresult), cbarg);
305 assert_int_equal(eresult, ISC_R_SUCCESS);
306 }
307
308 isc_refcount_decrement(&active_csends);
309 isc_nmhandle_detach(&sendhandle);
310 }
311
312 void
313 connect_send(isc_nmhandle_t *handle) {
314 isc_nmhandle_t *sendhandle = NULL;
315 isc_refcount_increment0(&active_csends);
316 isc_nmhandle_attach(handle, &sendhandle);
317 isc_nmhandle_setwritetimeout(handle, T_IDLE);
318 isc_nm_send(sendhandle, &send_msg, connect_send_cb, NULL);
319 }
320
321 void
322 connect_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
323 isc_region_t *region, void *cbarg) {
324 uint64_t magic = 0;
325
326 UNUSED(cbarg);
327
328 assert_non_null(handle);
329
330 F();
331
332 switch (eresult) {
333 case ISC_R_SUCCESS:
334 assert_true(region->length >= sizeof(magic));
335
336 memmove(&magic, region->base, sizeof(magic));
337
338 assert_true(magic == send_magic);
339
340 if (have_expected_creads(atomic_fetch_add(&creads, 1) + 1)) {
341 do_creads_shutdown(loopmgr);
342 }
343
344 if (magic == send_magic && allow_send_back) {
345 connect_send(handle);
346 return;
347 }
348
349 /* This will initiate one more read callback */
350 if (stream) {
351 isc_nmhandle_close(handle);
352 }
353 break;
354 case ISC_R_TIMEDOUT:
355 case ISC_R_EOF:
356 case ISC_R_SHUTTINGDOWN:
357 case ISC_R_CANCELED:
358 case ISC_R_CONNECTIONRESET:
359 case ISC_R_CONNREFUSED:
360 break;
361 default:
362 fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
363 isc_result_totext(eresult), cbarg);
364 assert_int_equal(eresult, ISC_R_SUCCESS);
365 }
366
367 isc_refcount_decrement(&active_creads);
368 isc_nmhandle_detach(&handle);
369 }
370
371 void
372 connect_connect_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
373 isc_nmhandle_t *readhandle = NULL;
374
375 F();
376
377 isc_refcount_decrement(&active_cconnects);
378
379 if (eresult != ISC_R_SUCCESS || connect_readcb == NULL) {
380 return;
381 }
382
383 if (stream_use_PROXY) {
384 assert_true(isc_nm_is_proxy_handle(handle));
385 }
386
387 /* We are finished, initiate the shutdown */
388 if (have_expected_cconnects(atomic_fetch_add(&cconnects, 1) + 1)) {
389 do_cconnects_shutdown(loopmgr);
390 } else if (do_send) {
391 isc_async_current(stream_recv_send_connect,
392 (cbarg == NULL
393 ? get_stream_connect_function()
394 : (stream_connect_function)cbarg));
395 }
396
397 isc_refcount_increment0(&active_creads);
398 isc_nmhandle_attach(handle, &readhandle);
399 isc_nm_read(handle, connect_readcb, NULL);
400
401 connect_send(handle);
402 }
403
404 void
405 listen_send_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
406 isc_nmhandle_t *sendhandle = handle;
407
408 UNUSED(cbarg);
409 UNUSED(eresult);
410
411 assert_non_null(sendhandle);
412
413 F();
414
415 switch (eresult) {
416 case ISC_R_CANCELED:
417 case ISC_R_CONNECTIONRESET:
418 case ISC_R_EOF:
419 case ISC_R_SHUTTINGDOWN:
420 break;
421 case ISC_R_SUCCESS:
422 if (have_expected_ssends(atomic_fetch_add(&ssends, 1) + 1)) {
423 do_ssends_shutdown(loopmgr);
424 }
425 break;
426 default:
427 fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
428 isc_result_totext(eresult), cbarg);
429 assert_int_equal(eresult, ISC_R_SUCCESS);
430 }
431
432 isc_refcount_decrement(&active_ssends);
433 isc_nmhandle_detach(&sendhandle);
434 }
435
436 void
437 listen_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
438 isc_region_t *region, void *cbarg) {
439 uint64_t magic = 0;
440
441 assert_non_null(handle);
442
443 F();
444
445 switch (eresult) {
446 case ISC_R_SUCCESS:
447 if (udp_use_PROXY || stream_use_PROXY) {
448 assert_true(isc_nm_is_proxy_handle(handle));
449 proxy_verify_endpoints(handle);
450 }
451
452 memmove(&magic, region->base, sizeof(magic));
453 assert_true(magic == send_magic);
454
455 if (have_expected_sreads(atomic_fetch_add(&sreads, 1) + 1)) {
456 do_sreads_shutdown(loopmgr);
457 }
458
459 assert_true(region->length >= sizeof(magic));
460
461 memmove(&magic, region->base, sizeof(magic));
462 assert_true(magic == send_magic);
463
464 if (!noanswer) {
465 /* Answer and continue to listen */
466 isc_nmhandle_t *sendhandle = NULL;
467 isc_nmhandle_attach(handle, &sendhandle);
468 isc_refcount_increment0(&active_ssends);
469 isc_nmhandle_setwritetimeout(sendhandle, T_IDLE);
470 isc_nm_send(sendhandle, &send_msg, listen_send_cb,
471 cbarg);
472 }
473 /* Continue to listen */
474 return;
475 case ISC_R_CANCELED:
476 case ISC_R_CONNECTIONRESET:
477 case ISC_R_EOF:
478 case ISC_R_SHUTTINGDOWN:
479 break;
480 default:
481 fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
482 isc_result_totext(eresult), cbarg);
483 assert_int_equal(eresult, ISC_R_SUCCESS);
484 }
485
486 isc_refcount_decrement(&active_sreads);
487 isc_nmhandle_detach(&handle);
488 }
489
490 isc_result_t
491 listen_accept_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
492 UNUSED(handle);
493 UNUSED(cbarg);
494
495 F();
496
497 if (eresult != ISC_R_SUCCESS) {
498 return eresult;
499 }
500
501 if (have_expected_saccepts(atomic_fetch_add(&saccepts, 1) + 1)) {
502 do_saccepts_shutdown(loopmgr);
503 }
504
505 isc_nmhandle_attach(handle, &(isc_nmhandle_t *){ NULL });
506 isc_refcount_increment0(&active_sreads);
507
508 return eresult;
509 }
510
511 isc_result_t
512 stream_accept_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
513 isc_nmhandle_t *readhandle = NULL;
514
515 UNUSED(cbarg);
516
517 F();
518
519 if (eresult != ISC_R_SUCCESS) {
520 return eresult;
521 }
522
523 if (have_expected_saccepts(atomic_fetch_add(&saccepts, 1) + 1)) {
524 do_saccepts_shutdown(loopmgr);
525 }
526
527 if (stream_use_PROXY) {
528 assert_true(isc_nm_is_proxy_handle(handle));
529 proxy_verify_endpoints(handle);
530 }
531
532 isc_refcount_increment0(&active_sreads);
533
534 isc_nmhandle_attach(handle, &readhandle);
535 isc_nm_read(handle, listen_read_cb, readhandle);
536
537 return ISC_R_SUCCESS;
538 }
539
540 void
541 stream_recv_send_connect(void *arg) {
542 connect_func connect = (connect_func)arg;
543 isc_sockaddr_t connect_addr;
544
545 connect_addr = (isc_sockaddr_t){ .length = 0 };
546 isc_sockaddr_fromin6(&connect_addr, &in6addr_loopback, 0);
547
548 isc_refcount_increment0(&active_cconnects);
549 connect(connect_nm);
550 }
551
552 /* Common stream protocols code */
553
554 void
555 timeout_retry_cb(isc_nmhandle_t *handle, isc_result_t eresult,
556 isc_region_t *region, void *cbarg) {
557 UNUSED(region);
558 UNUSED(cbarg);
559
560 assert_non_null(handle);
561
562 F();
563
564 if (eresult == ISC_R_TIMEDOUT &&
565 atomic_fetch_add(&ctimeouts, 1) + 1 < expected_ctimeouts)
566 {
567 isc_nmhandle_settimeout(handle, T_SOFT);
568 connect_send(handle);
569 return;
570 }
571
572 isc_refcount_decrement(&active_creads);
573 isc_nmhandle_detach(&handle);
574
575 isc_loopmgr_shutdown(loopmgr);
576 }
577
578 isc_quota_t *
579 tcp_listener_init_quota(size_t nthreads) {
580 isc_quota_t *quotap = NULL;
581 if (atomic_load(&check_listener_quota)) {
582 unsigned int max_quota = ISC_MAX(nthreads / 2, 1);
583 isc_quota_max(&listener_quota, max_quota);
584 quotap = &listener_quota;
585 }
586 return quotap;
587 }
588
589 static void
590 tcp_connect(isc_nm_t *nm) {
591 isc_nm_tcpconnect(nm, &tcp_connect_addr, &tcp_listen_addr,
592 connect_connect_cb, NULL, T_CONNECT);
593 }
594
595 static void
596 tls_connect(isc_nm_t *nm) {
597 isc_nm_tlsconnect(nm, &tcp_connect_addr, &tcp_listen_addr,
598 connect_connect_cb, NULL, tcp_connect_tlsctx,
599 tcp_tlsctx_client_sess_cache, T_CONNECT,
600 stream_use_PROXY, NULL);
601 }
602
603 void
604 set_proxyheader_info(isc_nm_proxyheader_info_t *pi) {
605 proxy_info = pi;
606 }
607
608 isc_nm_proxyheader_info_t *
609 get_proxyheader_info(void) {
610 if (proxy_info != NULL) {
611 return proxy_info;
612 }
613
614 /*
615 * There is 50% chance to get the info: so we can test LOCAL headers,
616 * too.
617 */
618 if (isc_random_uniform(2)) {
619 return &proxy_info_data;
620 }
621
622 return NULL;
623 }
624
625 static void
626 proxystream_connect(isc_nm_t *nm) {
627 isc_tlsctx_t *tlsctx = stream_PROXY_over_TLS ? tcp_connect_tlsctx
628 : NULL;
629 isc_tlsctx_client_session_cache_t *sess_cache =
630 stream_PROXY_over_TLS ? tcp_tlsctx_client_sess_cache : NULL;
631
632 isc_nm_proxystreamconnect(nm, &tcp_connect_addr, &tcp_listen_addr,
633 connect_connect_cb, NULL, T_CONNECT, tlsctx,
634 sess_cache, get_proxyheader_info());
635 }
636
637 stream_connect_function
638 get_stream_connect_function(void) {
639 if (stream_use_TLS && !stream_PROXY_over_TLS) {
640 return tls_connect;
641 } else if (stream_use_PROXY) {
642 return proxystream_connect;
643 } else {
644 return tcp_connect;
645 }
646
647 UNREACHABLE();
648 }
649
650 isc_result_t
651 stream_listen(isc_nm_accept_cb_t accept_cb, void *accept_cbarg, int backlog,
652 isc_quota_t *quota, isc_nmsocket_t **sockp) {
653 isc_result_t result = ISC_R_SUCCESS;
654
655 if (stream_use_TLS && !stream_PROXY_over_TLS) {
656 result = isc_nm_listentls(
657 listen_nm, ISC_NM_LISTEN_ALL, &tcp_listen_addr,
658 accept_cb, accept_cbarg, backlog, quota,
659 tcp_listen_tlsctx, stream_use_PROXY, sockp);
660 return result;
661 } else if (stream_use_PROXY) {
662 isc_tlsctx_t *tlsctx = stream_PROXY_over_TLS ? tcp_listen_tlsctx
663 : NULL;
664 result = isc_nm_listenproxystream(
665 listen_nm, ISC_NM_LISTEN_ALL, &tcp_listen_addr,
666 accept_cb, accept_cbarg, backlog, quota, tlsctx, sockp);
667 return result;
668 } else {
669 result = isc_nm_listentcp(listen_nm, ISC_NM_LISTEN_ALL,
670 &tcp_listen_addr, accept_cb,
671 accept_cbarg, backlog, quota, sockp);
672 return result;
673 }
674
675 UNREACHABLE();
676 }
677
678 void
679 stream_connect(isc_nm_cb_t cb, void *cbarg, unsigned int timeout) {
680 isc_refcount_increment0(&active_cconnects);
681
682 if (stream_use_TLS && !stream_PROXY_over_TLS) {
683 isc_nm_tlsconnect(
684 connect_nm, &tcp_connect_addr, &tcp_listen_addr, cb,
685 cbarg, tcp_connect_tlsctx, tcp_tlsctx_client_sess_cache,
686 timeout, stream_use_PROXY, NULL);
687 return;
688 } else if (stream_use_PROXY) {
689 isc_tlsctx_t *tlsctx = stream_PROXY_over_TLS
690 ? tcp_connect_tlsctx
691 : NULL;
692 isc_tlsctx_client_session_cache_t *sess_cache =
693 stream_PROXY_over_TLS ? tcp_tlsctx_client_sess_cache
694 : NULL;
695 isc_nm_proxystreamconnect(connect_nm, &tcp_connect_addr,
696 &tcp_listen_addr, cb, cbarg, timeout,
697 tlsctx, sess_cache,
698 get_proxyheader_info());
699 return;
700 } else {
701 isc_nm_tcpconnect(connect_nm, &tcp_connect_addr,
702 &tcp_listen_addr, cb, cbarg, timeout);
703 return;
704 }
705 UNREACHABLE();
706 }
707
708 isc_nm_proxy_type_t
709 get_proxy_type(void) {
710 if (!stream_use_PROXY) {
711 return ISC_NM_PROXY_NONE;
712 } else if (stream_PROXY_over_TLS) {
713 return ISC_NM_PROXY_ENCRYPTED;
714 }
715
716 return ISC_NM_PROXY_PLAIN;
717 }
718
719 void
720 connect_success_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
721 UNUSED(handle);
722 UNUSED(cbarg);
723
724 F();
725
726 isc_refcount_decrement(&active_cconnects);
727 assert_int_equal(eresult, ISC_R_SUCCESS);
728
729 if (have_expected_cconnects(atomic_fetch_add(&cconnects, 1) + 1)) {
730 do_cconnects_shutdown(loopmgr);
731 return;
732 }
733 }
734
735 int
736 stream_noop_setup(void **state ISC_ATTR_UNUSED) {
737 int r = setup_netmgr_test(state);
738 expected_cconnects = 1;
739 return r;
740 }
741
742 int
743 proxystream_noop_setup(void **state) {
744 stream_use_PROXY = true;
745 return stream_noop_setup(state);
746 }
747
748 int
749 proxystreamtls_noop_setup(void **state) {
750 stream_PROXY_over_TLS = true;
751 return proxystream_noop_setup(state);
752 }
753
754 void
755 stream_noop(void **state ISC_ATTR_UNUSED) {
756 isc_result_t result = ISC_R_SUCCESS;
757
758 result = stream_listen(noop_accept_cb, NULL, 128, NULL, &listen_sock);
759 assert_int_equal(result, ISC_R_SUCCESS);
760 isc_loop_teardown(mainloop, stop_listening, listen_sock);
761
762 connect_readcb = NULL;
763 stream_connect(connect_success_cb, NULL, T_CONNECT);
764 }
765
766 int
767 stream_noop_teardown(void **state ISC_ATTR_UNUSED) {
768 atomic_assert_int_eq(cconnects, 1);
769 atomic_assert_int_eq(csends, 0);
770 atomic_assert_int_eq(creads, 0);
771 atomic_assert_int_eq(sreads, 0);
772 atomic_assert_int_eq(ssends, 0);
773 return teardown_netmgr_test(state);
774 }
775
776 int
777 proxystream_noop_teardown(void **state) {
778 int r = stream_noop_teardown(state);
779 stream_use_PROXY = false;
780
781 return r;
782 }
783
784 int
785 proxystreamtls_noop_teardown(void **state) {
786 int r = proxystream_noop_teardown(state);
787 stream_PROXY_over_TLS = false;
788
789 return r;
790 }
791
792 static void
793 noresponse_readcb(isc_nmhandle_t *handle, isc_result_t eresult,
794 isc_region_t *region, void *cbarg) {
795 UNUSED(handle);
796 UNUSED(region);
797 UNUSED(cbarg);
798
799 F();
800
801 assert_true(eresult == ISC_R_CANCELED ||
802 eresult == ISC_R_CONNECTIONRESET || eresult == ISC_R_EOF);
803
804 isc_refcount_decrement(&active_creads);
805 isc_nmhandle_detach(&handle);
806
807 isc_loopmgr_shutdown(loopmgr);
808 }
809
810 static void
811 noresponse_sendcb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
812 UNUSED(cbarg);
813 UNUSED(eresult);
814
815 F();
816
817 assert_non_null(handle);
818 atomic_fetch_add(&csends, 1);
819 isc_nmhandle_detach(&handle);
820 isc_refcount_decrement(&active_csends);
821 }
822
823 static void
824 noresponse_connectcb(isc_nmhandle_t *handle, isc_result_t eresult,
825 void *cbarg) {
826 isc_nmhandle_t *readhandle = NULL;
827 isc_nmhandle_t *sendhandle = NULL;
828
829 F();
830
831 isc_refcount_decrement(&active_cconnects);
832
833 assert_int_equal(eresult, ISC_R_SUCCESS);
834
835 atomic_fetch_add(&cconnects, 1);
836
837 isc_refcount_increment0(&active_creads);
838 isc_nmhandle_attach(handle, &readhandle);
839 isc_nm_read(handle, noresponse_readcb, NULL);
840
841 isc_refcount_increment0(&active_csends);
842 isc_nmhandle_attach(handle, &sendhandle);
843 isc_nmhandle_setwritetimeout(handle, T_IDLE);
844
845 isc_nm_send(handle, (isc_region_t *)&send_msg, noresponse_sendcb,
846 cbarg);
847 }
848
849 int
850 stream_noresponse_setup(void **state ISC_ATTR_UNUSED) {
851 int r = setup_netmgr_test(state);
852 expected_cconnects = 1;
853 expected_saccepts = 1;
854 return r;
855 }
856
857 int
858 proxystream_noresponse_setup(void **state) {
859 stream_use_PROXY = true;
860 return stream_noresponse_setup(state);
861 }
862
863 int
864 proxystream_noresponse_teardown(void **state) {
865 int r = stream_noresponse_teardown(state);
866 stream_use_PROXY = false;
867 return r;
868 }
869
870 int
871 proxystreamtls_noresponse_setup(void **state) {
872 stream_PROXY_over_TLS = true;
873 return proxystream_noresponse_setup(state);
874 }
875
876 int
877 proxystreamtls_noresponse_teardown(void **state) {
878 int r = proxystream_noresponse_teardown(state);
879 stream_PROXY_over_TLS = false;
880 return r;
881 }
882
883 void
884 stream_noresponse(void **state ISC_ATTR_UNUSED) {
885 isc_result_t result = ISC_R_SUCCESS;
886
887 result = stream_listen(noop_accept_cb, NULL, 128, NULL, &listen_sock);
888 assert_int_equal(result, ISC_R_SUCCESS);
889 isc_loop_teardown(mainloop, stop_listening, listen_sock);
890
891 stream_connect(noresponse_connectcb, NULL, T_CONNECT);
892 }
893
894 int
895 stream_noresponse_teardown(void **state ISC_ATTR_UNUSED) {
896 X(cconnects);
897 X(csends);
898 X(creads);
899 X(sreads);
900 X(ssends);
901
902 atomic_assert_int_eq(cconnects, 1);
903 atomic_assert_int_eq(creads, 0);
904 atomic_assert_int_eq(sreads, 0);
905 atomic_assert_int_eq(ssends, 0);
906
907 return teardown_netmgr_test(state);
908 }
909
910 int
911 stream_timeout_recovery_setup(void **state ISC_ATTR_UNUSED) {
912 int r = setup_netmgr_test(state);
913
914 expected_ctimeouts = 4;
915 ctimeouts_shutdown = false;
916
917 expected_sreads = 5;
918 sreads_shutdown = true;
919
920 return r;
921 }
922
923 typedef struct proxy_addrs {
924 isc_sockaddr_t src_addr;
925 isc_sockaddr_t dst_addr;
926 } proxy_addrs_t;
927
928 static void
929 proxy2_handler_save_addrs_cb(const isc_result_t result,
930 const isc_proxy2_command_t cmd, const int socktype,
931 const isc_sockaddr_t *restrict src_addr,
932 const isc_sockaddr_t *restrict dst_addr,
933 const isc_region_t *restrict tlv_data,
934 const isc_region_t *restrict extra, void *cbarg) {
935 proxy_addrs_t *addrs = (proxy_addrs_t *)cbarg;
936
937 UNUSED(cmd);
938 UNUSED(socktype);
939 UNUSED(tlv_data);
940 UNUSED(extra);
941
942 REQUIRE(result == ISC_R_SUCCESS);
943
944 if (src_addr != NULL) {
945 addrs->src_addr = *src_addr;
946 }
947
948 if (dst_addr != NULL) {
949 addrs->dst_addr = *dst_addr;
950 }
951 }
952
953 void
954 proxy_verify_endpoints(isc_nmhandle_t *handle) {
955 isc_sockaddr_t local, peer;
956 peer = isc_nmhandle_peeraddr(handle);
957 local = isc_nmhandle_localaddr(handle);
958
959 if (isc_nm_is_proxy_unspec(handle)) {
960 isc_sockaddr_t real_local, real_peer;
961 real_peer = isc_nmhandle_real_peeraddr(handle);
962 real_local = isc_nmhandle_real_localaddr(handle);
963
964 assert_true(isc_sockaddr_equal(&peer, &real_peer));
965 assert_true(isc_sockaddr_equal(&local, &real_local));
966 } else if (proxy_info == NULL) {
967 assert_true(isc_sockaddr_equal(&peer, &proxy_src));
968 assert_true(isc_sockaddr_equal(&local, &proxy_dst));
969 } else if (proxy_info != NULL && !proxy_info->complete) {
970 assert_true(isc_sockaddr_equal(
971 &peer, &proxy_info->proxy_info.src_addr));
972 assert_true(isc_sockaddr_equal(
973 &local, &proxy_info->proxy_info.dst_addr));
974 } else if (proxy_info != NULL && proxy_info->complete) {
975 proxy_addrs_t addrs = { 0 };
976 RUNTIME_CHECK(isc_proxy2_header_handle_directly(
977 &proxy_info->complete_header,
978 proxy2_handler_save_addrs_cb,
979 &addrs) == ISC_R_SUCCESS);
980
981 assert_true(isc_sockaddr_equal(&peer, &addrs.src_addr));
982 assert_true(isc_sockaddr_equal(&local, &addrs.dst_addr));
983 }
984 }
985
986 int
987 proxystream_timeout_recovery_setup(void **state) {
988 stream_use_PROXY = true;
989 return stream_timeout_recovery_setup(state);
990 }
991
992 int
993 proxystream_timeout_recovery_teardown(void **state) {
994 int r = stream_timeout_recovery_teardown(state);
995 stream_use_PROXY = false;
996 return r;
997 }
998
999 int
1000 proxystreamtls_timeout_recovery_setup(void **state) {
1001 stream_PROXY_over_TLS = true;
1002 return proxystream_timeout_recovery_setup(state);
1003 }
1004
1005 int
1006 proxystreamtls_timeout_recovery_teardown(void **state) {
1007 int r = proxystream_timeout_recovery_teardown(state);
1008 stream_PROXY_over_TLS = false;
1009 return r;
1010 }
1011
1012 void
1013 stream_timeout_recovery(void **state ISC_ATTR_UNUSED) {
1014 isc_result_t result = ISC_R_SUCCESS;
1015
1016 /*
1017 * Accept connections but don't send responses, forcing client
1018 * reads to time out.
1019 */
1020 noanswer = true;
1021 result = stream_listen(stream_accept_cb, NULL, 128, NULL, &listen_sock);
1022 assert_int_equal(result, ISC_R_SUCCESS);
1023 isc_loop_teardown(mainloop, stop_listening, listen_sock);
1024
1025 /*
1026 * Shorten all the client timeouts to 0.05 seconds.
1027 */
1028 isc_nm_settimeouts(connect_nm, T_SOFT, T_SOFT, T_SOFT, T_SOFT);
1029 connect_readcb = timeout_retry_cb;
1030 stream_connect(connect_connect_cb, NULL, T_CONNECT);
1031 }
1032
1033 int
1034 stream_timeout_recovery_teardown(void **state ISC_ATTR_UNUSED) {
1035 atomic_assert_int_eq(ctimeouts, expected_ctimeouts);
1036 return teardown_netmgr_test(state);
1037 }
1038
1039 int
1040 stream_recv_one_setup(void **state ISC_ATTR_UNUSED) {
1041 int r = setup_netmgr_test(state);
1042
1043 expected_cconnects = 1;
1044 cconnects_shutdown = false;
1045
1046 expected_csends = 1;
1047 csends_shutdown = false;
1048
1049 expected_saccepts = 1;
1050 saccepts_shutdown = false;
1051
1052 expected_sreads = 1;
1053 sreads_shutdown = false;
1054
1055 expected_ssends = 1;
1056 ssends_shutdown = false;
1057
1058 expected_creads = 1;
1059 creads_shutdown = true;
1060
1061 return r;
1062 }
1063
1064 int
1065 proxystream_recv_one_setup(void **state) {
1066 stream_use_PROXY = true;
1067 return stream_recv_one_setup(state);
1068 }
1069
1070 int
1071 proxystream_recv_one_teardown(void **state) {
1072 int r = stream_recv_one_teardown(state);
1073 stream_use_PROXY = false;
1074 return r;
1075 }
1076
1077 int
1078 proxystreamtls_recv_one_setup(void **state) {
1079 stream_PROXY_over_TLS = true;
1080 return proxystream_recv_one_setup(state);
1081 }
1082
1083 int
1084 proxystreamtls_recv_one_teardown(void **state) {
1085 int r = proxystream_recv_one_teardown(state);
1086 stream_PROXY_over_TLS = false;
1087 return r;
1088 }
1089
1090 void
1091 stream_recv_one(void **state ISC_ATTR_UNUSED) {
1092 isc_result_t result = ISC_R_SUCCESS;
1093 isc_quota_t *quotap = tcp_listener_init_quota(1);
1094
1095 atomic_store(&nsends, 1);
1096
1097 result = stream_listen(stream_accept_cb, NULL, 128, quotap,
1098 &listen_sock);
1099 assert_int_equal(result, ISC_R_SUCCESS);
1100 isc_loop_teardown(mainloop, stop_listening, listen_sock);
1101
1102 stream_connect(connect_connect_cb, NULL, T_CONNECT);
1103 }
1104
1105 int
1106 stream_recv_one_teardown(void **state ISC_ATTR_UNUSED) {
1107 atomic_assert_int_eq(cconnects, expected_cconnects);
1108 atomic_assert_int_eq(csends, expected_csends);
1109 atomic_assert_int_eq(saccepts, expected_saccepts);
1110 atomic_assert_int_eq(sreads, expected_sreads);
1111 atomic_assert_int_eq(ssends, expected_ssends);
1112 atomic_assert_int_eq(creads, expected_creads);
1113
1114 return teardown_netmgr_test(state);
1115 }
1116
1117 int
1118 stream_recv_two_setup(void **state ISC_ATTR_UNUSED) {
1119 int r = setup_netmgr_test(state);
1120
1121 expected_cconnects = 2;
1122 cconnects_shutdown = false;
1123
1124 expected_csends = 2;
1125 csends_shutdown = false;
1126
1127 expected_saccepts = 2;
1128 saccepts_shutdown = false;
1129
1130 expected_sreads = 2;
1131 sreads_shutdown = false;
1132
1133 expected_ssends = 2;
1134 ssends_shutdown = false;
1135
1136 expected_creads = 2;
1137 creads_shutdown = true;
1138
1139 return r;
1140 }
1141
1142 int
1143 proxystream_recv_two_setup(void **state) {
1144 stream_use_PROXY = true;
1145 return stream_recv_two_setup(state);
1146 }
1147
1148 int
1149 proxystream_recv_two_teardown(void **state) {
1150 int r = stream_recv_two_teardown(state);
1151 stream_use_PROXY = false;
1152 return r;
1153 }
1154
1155 int
1156 proxystreamtls_recv_two_setup(void **state) {
1157 stream_PROXY_over_TLS = true;
1158 return proxystream_recv_two_setup(state);
1159 }
1160
1161 int
1162 proxystreamtls_recv_two_teardown(void **state) {
1163 int r = proxystream_recv_two_teardown(state);
1164 stream_PROXY_over_TLS = false;
1165 return r;
1166 }
1167
1168 void
1169 stream_recv_two(void **state ISC_ATTR_UNUSED) {
1170 isc_result_t result = ISC_R_SUCCESS;
1171 isc_quota_t *quotap = tcp_listener_init_quota(1);
1172
1173 atomic_store(&nsends, 2);
1174
1175 result = stream_listen(stream_accept_cb, NULL, 128, quotap,
1176 &listen_sock);
1177 assert_int_equal(result, ISC_R_SUCCESS);
1178 isc_loop_teardown(mainloop, stop_listening, listen_sock);
1179
1180 stream_connect(connect_connect_cb, NULL, T_CONNECT);
1181
1182 stream_connect(connect_connect_cb, NULL, T_CONNECT);
1183 }
1184
1185 int
1186 stream_recv_two_teardown(void **state ISC_ATTR_UNUSED) {
1187 atomic_assert_int_eq(cconnects, expected_cconnects);
1188 atomic_assert_int_eq(csends, expected_csends);
1189 atomic_assert_int_eq(sreads, expected_saccepts);
1190 atomic_assert_int_eq(sreads, expected_sreads);
1191 atomic_assert_int_eq(ssends, expected_ssends);
1192 atomic_assert_int_eq(creads, expected_creads);
1193
1194 return teardown_netmgr_test(state);
1195 }
1196
1197 int
1198 stream_recv_send_setup(void **state ISC_ATTR_UNUSED) {
1199 int r = setup_netmgr_test(state);
1200 expected_cconnects = workers;
1201 cconnects_shutdown = false;
1202 nsends = expected_creads = workers;
1203 do_send = true;
1204
1205 return r;
1206 }
1207
1208 int
1209 proxystream_recv_send_setup(void **state) {
1210 stream_use_PROXY = true;
1211 return stream_recv_send_setup(state);
1212 }
1213
1214 int
1215 proxystream_recv_send_teardown(void **state) {
1216 int r = stream_recv_send_teardown(state);
1217 stream_use_PROXY = false;
1218 return r;
1219 }
1220
1221 int
1222 proxystreamtls_recv_send_setup(void **state) {
1223 stream_PROXY_over_TLS = true;
1224 return proxystream_recv_send_setup(state);
1225 }
1226
1227 int
1228 proxystreamtls_recv_send_teardown(void **state) {
1229 int r = proxystream_recv_send_teardown(state);
1230 stream_PROXY_over_TLS = false;
1231 return r;
1232 }
1233
1234 void
1235 stream_recv_send(void **state ISC_ATTR_UNUSED) {
1236 isc_result_t result = ISC_R_SUCCESS;
1237 isc_quota_t *quotap = tcp_listener_init_quota(workers);
1238
1239 result = stream_listen(stream_accept_cb, NULL, 128, quotap,
1240 &listen_sock);
1241 assert_int_equal(result, ISC_R_SUCCESS);
1242 isc_loop_teardown(mainloop, stop_listening, listen_sock);
1243
1244 for (size_t i = 0; i < workers; i++) {
1245 isc_async_run(isc_loop_get(loopmgr, i),
1246 stream_recv_send_connect,
1247 get_stream_connect_function());
1248 }
1249 }
1250
1251 int
1252 stream_recv_send_teardown(void **state ISC_ATTR_UNUSED) {
1253 X(cconnects);
1254 X(csends);
1255 X(creads);
1256 X(sreads);
1257 X(ssends);
1258
1259 CHECK_RANGE_FULL(csends);
1260 CHECK_RANGE_FULL(creads);
1261 CHECK_RANGE_FULL(sreads);
1262 CHECK_RANGE_FULL(ssends);
1263
1264 return teardown_netmgr_test(state);
1265 }
1266
1267 int
1268 setup_udp_test(void **state) {
1269 setup_loopmgr(state);
1270 setup_netmgr(state);
1271
1272 udp_connect_addr = (isc_sockaddr_t){ .length = 0 };
1273 isc_sockaddr_fromin6(&udp_connect_addr, &in6addr_loopback, 0);
1274
1275 udp_listen_addr = (isc_sockaddr_t){ .length = 0 };
1276 isc_sockaddr_fromin6(&udp_listen_addr, &in6addr_loopback,
1277 udp_use_PROXY ? PROXYUDP_TEST_PORT
1278 : UDP_TEST_PORT);
1279
1280 atomic_store(&sreads, 0);
1281 atomic_store(&ssends, 0);
1282
1283 atomic_store(&cconnects, 0);
1284 atomic_store(&csends, 0);
1285 atomic_store(&creads, 0);
1286 atomic_store(&ctimeouts, 0);
1287
1288 isc_refcount_init(&active_cconnects, 0);
1289 isc_refcount_init(&active_csends, 0);
1290 isc_refcount_init(&active_creads, 0);
1291 isc_refcount_init(&active_ssends, 0);
1292 isc_refcount_init(&active_sreads, 0);
1293
1294 expected_cconnects = -1;
1295 expected_csends = -1;
1296 expected_creads = -1;
1297 expected_sreads = -1;
1298 expected_ssends = -1;
1299 expected_ctimeouts = -1;
1300
1301 ssends_shutdown = true;
1302 sreads_shutdown = true;
1303 csends_shutdown = true;
1304 cconnects_shutdown = true;
1305 creads_shutdown = true;
1306
1307 isc_nonce_buf(&send_magic, sizeof(send_magic));
1308
1309 connect_readcb = connect_read_cb;
1310
1311 return 0;
1312 }
1313
1314 int
1315 teardown_udp_test(void **state) {
1316 UNUSED(state);
1317
1318 isc_refcount_destroy(&active_cconnects);
1319 isc_refcount_destroy(&active_csends);
1320 isc_refcount_destroy(&active_creads);
1321 isc_refcount_destroy(&active_ssends);
1322 isc_refcount_destroy(&active_sreads);
1323
1324 teardown_netmgr(state);
1325 teardown_loopmgr(state);
1326
1327 return 0;
1328 }
1329
1330 static void
1331 udp_connect(isc_nm_cb_t cb, void *cbarg, unsigned int timeout) {
1332 if (udp_use_PROXY) {
1333 isc_nm_proxyudpconnect(netmgr, &udp_connect_addr,
1334 &udp_listen_addr, cb, cbarg, timeout,
1335 NULL);
1336 } else {
1337 isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
1338 cb, cbarg, timeout);
1339 }
1340 }
1341
1342 static void
1343 udp_listen_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1344 isc_region_t *region, void *cbarg) {
1345 if (eresult != ISC_R_SUCCESS) {
1346 isc_refcount_increment0(&active_sreads);
1347 }
1348 listen_read_cb(handle, eresult, region, cbarg);
1349 }
1350
1351 static void
1352 udp_start_listening(uint32_t nworkers, isc_nm_recv_cb_t cb) {
1353 isc_result_t result;
1354
1355 if (udp_use_PROXY) {
1356 result = isc_nm_listenproxyudp(netmgr, nworkers,
1357 &udp_listen_addr, cb, NULL,
1358 &listen_sock);
1359 } else {
1360 result = isc_nm_listenudp(netmgr, nworkers, &udp_listen_addr,
1361 cb, NULL, &listen_sock);
1362 }
1363
1364 assert_int_equal(result, ISC_R_SUCCESS);
1365
1366 isc_loop_teardown(mainloop, stop_listening, listen_sock);
1367 }
1368
1369 static void
1370 udp__send_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
1371 isc_nmhandle_t *sendhandle = handle;
1372
1373 assert_non_null(sendhandle);
1374
1375 F();
1376
1377 switch (eresult) {
1378 case ISC_R_SUCCESS:
1379 if (have_expected_csends(atomic_fetch_add(&csends, 1) + 1)) {
1380 if (csends_shutdown) {
1381 isc_nm_cancelread(handle);
1382 isc_loopmgr_shutdown(loopmgr);
1383 }
1384 }
1385 break;
1386 case ISC_R_SHUTTINGDOWN:
1387 case ISC_R_CANCELED:
1388 break;
1389 default:
1390 fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
1391 isc_result_totext(eresult), cbarg);
1392 assert_int_equal(eresult, ISC_R_SUCCESS);
1393 }
1394
1395 isc_nmhandle_detach(&sendhandle);
1396 isc_refcount_decrement(&active_csends);
1397 }
1398
1399 static void
1400 udp__connect_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
1401
1402 static void
1403 udp_enqueue_connect(void *arg ISC_ATTR_UNUSED) {
1404 isc_sockaddr_t connect_addr;
1405
1406 connect_addr = (isc_sockaddr_t){ .length = 0 };
1407 isc_sockaddr_fromin6(&connect_addr, &in6addr_loopback, 0);
1408
1409 isc_refcount_increment0(&active_cconnects);
1410
1411 udp_connect(udp__connect_cb, NULL, T_CONNECT);
1412 }
1413
1414 static void
1415 udp__connect_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1416 isc_region_t *region, void *cbarg) {
1417 uint64_t magic = 0;
1418
1419 assert_non_null(handle);
1420
1421 F();
1422
1423 switch (eresult) {
1424 case ISC_R_TIMEDOUT:
1425 /*
1426 * We are operating on the localhost, UDP cannot get lost, but
1427 * it could be delayed, so we read again until we get the
1428 * answer.
1429 */
1430 isc_nm_read(handle, connect_readcb, cbarg);
1431 return;
1432 case ISC_R_SUCCESS:
1433 assert_true(region->length >= sizeof(magic));
1434
1435 memmove(&magic, region->base, sizeof(magic));
1436
1437 assert_true(magic == send_magic);
1438
1439 if (have_expected_creads(atomic_fetch_add(&creads, 1) + 1)) {
1440 do_creads_shutdown(loopmgr);
1441 }
1442
1443 if (magic == send_magic && allow_send_back) {
1444 connect_send(handle);
1445 return;
1446 }
1447
1448 break;
1449 default:
1450 fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
1451 isc_result_totext(eresult), cbarg);
1452 assert_int_equal(eresult, ISC_R_SUCCESS);
1453 }
1454
1455 isc_refcount_decrement(&active_creads);
1456
1457 isc_nmhandle_detach(&handle);
1458 }
1459
1460 static void
1461 udp__connect_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
1462 isc_nmhandle_t *readhandle = NULL;
1463 isc_nmhandle_t *sendhandle = NULL;
1464
1465 F();
1466
1467 isc_refcount_decrement(&active_cconnects);
1468
1469 switch (eresult) {
1470 case ISC_R_SUCCESS:
1471 if (udp_use_PROXY) {
1472 assert_true(isc_nm_is_proxy_handle(handle));
1473 }
1474
1475 if (have_expected_cconnects(atomic_fetch_add(&cconnects, 1) +
1476 1))
1477 {
1478 do_cconnects_shutdown(loopmgr);
1479 } else if (do_send) {
1480 isc_async_current(udp_enqueue_connect, cbarg);
1481 }
1482
1483 isc_refcount_increment0(&active_creads);
1484 isc_nmhandle_attach(handle, &readhandle);
1485 isc_nm_read(handle, connect_readcb, cbarg);
1486
1487 isc_refcount_increment0(&active_csends);
1488 isc_nmhandle_attach(handle, &sendhandle);
1489 isc_nmhandle_setwritetimeout(handle, T_IDLE);
1490
1491 isc_nm_send(sendhandle, (isc_region_t *)&send_msg, udp__send_cb,
1492 cbarg);
1493
1494 break;
1495 case ISC_R_ADDRINUSE:
1496 /* Try again */
1497 udp_enqueue_connect(NULL);
1498 break;
1499 case ISC_R_SHUTTINGDOWN:
1500 case ISC_R_CANCELED:
1501 break;
1502 default:
1503 fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
1504 isc_result_totext(eresult), cbarg);
1505 assert_int_equal(eresult, ISC_R_SUCCESS);
1506 }
1507 }
1508
1509 int
1510 udp_noop_setup(void **state) {
1511 setup_udp_test(state);
1512 expected_cconnects = 1;
1513 cconnects_shutdown = true;
1514 return 0;
1515 }
1516
1517 int
1518 udp_noop_teardown(void **state) {
1519 atomic_assert_int_eq(cconnects, 1);
1520 teardown_udp_test(state);
1521 return 0;
1522 }
1523
1524 void
1525 udp_noop(void **arg ISC_ATTR_UNUSED) {
1526 /* isc_result_t result = ISC_R_SUCCESS; */
1527
1528 /* result = isc_nm_listenudp(netmgr, ISC_NM_LISTEN_ALL,
1529 * &udp_listen_addr, */
1530 /* mock_recv_cb, NULL, &listen_sock); */
1531 /* assert_int_equal(result, ISC_R_SUCCESS); */
1532
1533 /* isc_nm_stoplistening(listen_sock); */
1534 /* isc_nmsocket_close(&listen_sock); */
1535 /* assert_null(listen_sock); */
1536
1537 isc_refcount_increment0(&active_cconnects);
1538 udp_connect(connect_success_cb, NULL, UDP_T_CONNECT);
1539 }
1540
1541 int
1542 proxyudp_noop_setup(void **state) {
1543 udp_use_PROXY = true;
1544 return udp_noop_setup(state);
1545 }
1546
1547 int
1548 proxyudp_noop_teardown(void **state) {
1549 int ret = udp_noop_teardown(state);
1550 udp_use_PROXY = false;
1551 return ret;
1552 }
1553
1554 static void
1555 udp_noresponse_recv_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1556 isc_region_t *region, void *cbarg) {
1557 UNUSED(handle);
1558 UNUSED(eresult);
1559 UNUSED(region);
1560 UNUSED(cbarg);
1561 }
1562
1563 static void
1564 udp_noresponse_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1565 isc_region_t *region, void *cbarg) {
1566 UNUSED(region);
1567 UNUSED(cbarg);
1568
1569 assert_int_equal(eresult, ISC_R_TIMEDOUT);
1570
1571 isc_refcount_decrement(&active_creads);
1572
1573 atomic_fetch_add(&creads, 1);
1574
1575 isc_nmhandle_detach(&handle);
1576
1577 isc_loopmgr_shutdown(loopmgr);
1578 }
1579
1580 static void
1581 udp_noresponse_send_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1582 void *cbarg) {
1583 UNUSED(cbarg);
1584
1585 assert_non_null(handle);
1586 assert_int_equal(eresult, ISC_R_SUCCESS);
1587 atomic_fetch_add(&csends, 1);
1588 isc_nmhandle_detach(&handle);
1589 isc_refcount_decrement(&active_csends);
1590 }
1591
1592 static void
1593 udp_noresponse_connect_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1594 void *cbarg) {
1595 isc_nmhandle_t *readhandle = NULL;
1596 isc_nmhandle_t *sendhandle = NULL;
1597
1598 isc_refcount_decrement(&active_cconnects);
1599
1600 assert_int_equal(eresult, ISC_R_SUCCESS);
1601
1602 /* Read */
1603 isc_refcount_increment0(&active_creads);
1604 isc_nmhandle_attach(handle, &readhandle);
1605 isc_nm_read(handle, udp_noresponse_read_cb, cbarg);
1606
1607 /* Send */
1608 isc_refcount_increment0(&active_csends);
1609 isc_nmhandle_attach(handle, &sendhandle);
1610 isc_nmhandle_setwritetimeout(handle, T_IDLE);
1611
1612 isc_nm_send(sendhandle, (isc_region_t *)&send_msg,
1613 udp_noresponse_send_cb, cbarg);
1614
1615 atomic_fetch_add(&cconnects, 1);
1616 }
1617
1618 int
1619 udp_noresponse_setup(void **state) {
1620 setup_udp_test(state);
1621 expected_csends = 1;
1622 return 0;
1623 }
1624
1625 int
1626 udp_noresponse_teardown(void **state) {
1627 atomic_assert_int_eq(csends, expected_csends);
1628 teardown_udp_test(state);
1629 return 0;
1630 }
1631
1632 void
1633 udp_noresponse(void **arg ISC_ATTR_UNUSED) {
1634 udp_start_listening(ISC_NM_LISTEN_ONE, udp_noresponse_recv_cb);
1635
1636 isc_refcount_increment0(&active_cconnects);
1637 udp_connect(udp_noresponse_connect_cb, listen_sock, UDP_T_SOFT);
1638 }
1639
1640 int
1641 proxyudp_noresponse_setup(void **state) {
1642 udp_use_PROXY = true;
1643 return udp_noresponse_setup(state);
1644 }
1645
1646 int
1647 proxyudp_noresponse_teardown(void **state) {
1648 int ret = udp_noresponse_teardown(state);
1649 udp_use_PROXY = false;
1650 return ret;
1651 }
1652
1653 static void
1654 udp_timeout_recovery_ssend_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1655 void *cbarg) {
1656 UNUSED(cbarg);
1657
1658 isc_refcount_decrement(&active_ssends);
1659 assert_non_null(handle);
1660 assert_int_equal(eresult, ISC_R_SUCCESS);
1661 atomic_fetch_add(&ssends, 1);
1662 isc_nmhandle_detach(&handle);
1663 }
1664
1665 static void
1666 udp_timeout_recovery_recv_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1667 isc_region_t *region, void *cbarg) {
1668 uint64_t magic = 0;
1669 isc_nmhandle_t *sendhandle = NULL;
1670 int _creads = atomic_fetch_add(&creads, 1) + 1;
1671
1672 assert_non_null(handle);
1673
1674 assert_int_equal(eresult, ISC_R_SUCCESS);
1675
1676 assert_true(region->length == sizeof(magic));
1677
1678 memmove(&magic, region->base, sizeof(magic));
1679 assert_true(magic == send_magic);
1680 assert_true(_creads < 6);
1681
1682 if (_creads == 5) {
1683 isc_nmhandle_attach(handle, &sendhandle);
1684 isc_refcount_increment0(&active_ssends);
1685 isc_nmhandle_setwritetimeout(sendhandle, T_IDLE);
1686 isc_nm_send(sendhandle, (isc_region_t *)&send_msg,
1687 udp_timeout_recovery_ssend_cb, cbarg);
1688 }
1689 }
1690
1691 static void
1692 udp_timeout_recovery_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1693 isc_region_t *region, void *cbarg) {
1694 UNUSED(region);
1695 UNUSED(cbarg);
1696
1697 assert_non_null(handle);
1698
1699 F();
1700
1701 if (eresult == ISC_R_TIMEDOUT &&
1702 atomic_fetch_add(&ctimeouts, 1) + 1 < expected_ctimeouts)
1703 {
1704 isc_nmhandle_settimeout(handle, T_SOFT);
1705 return;
1706 }
1707
1708 isc_refcount_decrement(&active_creads);
1709 isc_nmhandle_detach(&handle);
1710
1711 atomic_fetch_add(&creads, 1);
1712 isc_loopmgr_shutdown(loopmgr);
1713 }
1714
1715 static void
1716 udp_timeout_recovery_send_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1717 void *cbarg) {
1718 UNUSED(cbarg);
1719
1720 assert_non_null(handle);
1721 assert_int_equal(eresult, ISC_R_SUCCESS);
1722 atomic_fetch_add(&csends, 1);
1723
1724 isc_nmhandle_detach(&handle);
1725 isc_refcount_decrement(&active_csends);
1726 }
1727
1728 static void
1729 udp_timeout_recovery_connect_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1730 void *cbarg) {
1731 isc_nmhandle_t *readhandle = NULL;
1732 isc_nmhandle_t *sendhandle = NULL;
1733
1734 F();
1735
1736 isc_refcount_decrement(&active_cconnects);
1737
1738 assert_int_equal(eresult, ISC_R_SUCCESS);
1739
1740 /* Read */
1741 isc_refcount_increment0(&active_creads);
1742 isc_nmhandle_attach(handle, &readhandle);
1743 isc_nm_read(handle, udp_timeout_recovery_read_cb, cbarg);
1744
1745 /* Send */
1746 isc_refcount_increment0(&active_csends);
1747 isc_nmhandle_attach(handle, &sendhandle);
1748 isc_nmhandle_setwritetimeout(handle, T_IDLE);
1749 isc_nm_send(sendhandle, (isc_region_t *)&send_msg,
1750 udp_timeout_recovery_send_cb, cbarg);
1751
1752 atomic_fetch_add(&cconnects, 1);
1753 }
1754
1755 int
1756 udp_timeout_recovery_setup(void **state) {
1757 setup_udp_test(state);
1758 expected_cconnects = 1;
1759 expected_csends = 1;
1760 expected_creads = 1;
1761 expected_ctimeouts = 4;
1762 return 0;
1763 }
1764
1765 int
1766 udp_timeout_recovery_teardown(void **state) {
1767 atomic_assert_int_eq(cconnects, expected_cconnects);
1768 atomic_assert_int_eq(csends, expected_csends);
1769 atomic_assert_int_eq(csends, expected_creads);
1770 atomic_assert_int_eq(ctimeouts, expected_ctimeouts);
1771 teardown_udp_test(state);
1772 return 0;
1773 }
1774
1775 void
1776 udp_timeout_recovery(void **arg ISC_ATTR_UNUSED) {
1777 /*
1778 * Listen using the noop callback so that client reads will time out.
1779 */
1780 udp_start_listening(ISC_NM_LISTEN_ONE, udp_timeout_recovery_recv_cb);
1781
1782 /*
1783 * Connect with client timeout set to 0.05 seconds, then sleep for at
1784 * least a second for each 'tick'. timeout_retry_cb() will give up
1785 * after five timeouts.
1786 */
1787 isc_refcount_increment0(&active_cconnects);
1788 udp_connect(udp_timeout_recovery_connect_cb, listen_sock, UDP_T_SOFT);
1789 }
1790
1791 int
1792 proxyudp_timeout_recovery_setup(void **state) {
1793 udp_use_PROXY = true;
1794 return udp_timeout_recovery_setup(state);
1795 }
1796
1797 int
1798 proxyudp_timeout_recovery_teardown(void **state) {
1799 int ret = udp_timeout_recovery_teardown(state);
1800 udp_use_PROXY = false;
1801 return ret;
1802 }
1803
1804 static void
1805 udp_shutdown_connect_async_cb(void *arg ISC_ATTR_UNUSED);
1806
1807 static void
1808 udp_shutdown_connect_connect_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1809 void *cbarg) {
1810 UNUSED(handle);
1811 UNUSED(cbarg);
1812
1813 isc_refcount_decrement(&active_cconnects);
1814
1815 /*
1816 * The first UDP connect is faster than asynchronous shutdown procedure,
1817 * restart the UDP connect again and expect the failure only in the
1818 * second loop.
1819 */
1820 if (atomic_fetch_add(&cconnects, 1) == 0) {
1821 assert_int_equal(eresult, ISC_R_SUCCESS);
1822 isc_async_current(udp_shutdown_connect_async_cb, netmgr);
1823 } else {
1824 assert_int_equal(eresult, ISC_R_SHUTTINGDOWN);
1825 }
1826 }
1827
1828 static void
1829 udp_shutdown_connect_async_cb(void *arg ISC_ATTR_UNUSED) {
1830 isc_refcount_increment0(&active_cconnects);
1831 udp_connect(udp_shutdown_connect_connect_cb, NULL, T_SOFT);
1832 }
1833
1834 int
1835 udp_shutdown_connect_setup(void **state) {
1836 setup_udp_test(state);
1837 expected_cconnects = 2;
1838 return 0;
1839 }
1840
1841 int
1842 udp_shutdown_connect_teardown(void **state) {
1843 atomic_assert_int_eq(cconnects, expected_cconnects);
1844 teardown_udp_test(state);
1845 return 0;
1846 }
1847
1848 void
1849 udp_shutdown_connect(void **arg ISC_ATTR_UNUSED) {
1850 isc_loopmgr_shutdown(loopmgr);
1851 /*
1852 * isc_nm_udpconnect() is synchronous, so we need to launch this on the
1853 * async loop.
1854 */
1855 isc_async_current(udp_shutdown_connect_async_cb, netmgr);
1856 }
1857
1858 int
1859 proxyudp_shutdown_connect_setup(void **state) {
1860 udp_use_PROXY = true;
1861 return udp_shutdown_connect_setup(state);
1862 }
1863
1864 int
1865 proxyudp_shutdown_connect_teardown(void **state) {
1866 int ret = udp_shutdown_connect_teardown(state);
1867 udp_use_PROXY = false;
1868 return ret;
1869 }
1870
1871 static void
1872 udp_shutdown_read_recv_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1873 isc_region_t *region, void *cbarg) {
1874 uint64_t magic = 0;
1875
1876 UNUSED(cbarg);
1877
1878 assert_non_null(handle);
1879
1880 F();
1881
1882 assert_int_equal(eresult, ISC_R_SUCCESS);
1883
1884 assert_true(region->length == sizeof(magic));
1885
1886 memmove(&magic, region->base, sizeof(magic));
1887 assert_true(magic == send_magic);
1888 }
1889
1890 static void
1891 udp_shutdown_read_send_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1892 void *cbarg) {
1893 UNUSED(cbarg);
1894
1895 F();
1896
1897 assert_non_null(handle);
1898 assert_int_equal(eresult, ISC_R_SUCCESS);
1899
1900 atomic_fetch_add(&csends, 1);
1901
1902 isc_loopmgr_shutdown(loopmgr);
1903
1904 isc_nmhandle_detach(&handle);
1905 isc_refcount_decrement(&active_csends);
1906 }
1907
1908 static void
1909 udp_shutdown_read_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1910 isc_region_t *region, void *cbarg) {
1911 UNUSED(region);
1912 UNUSED(cbarg);
1913
1914 assert_true(eresult == ISC_R_SHUTTINGDOWN || eresult == ISC_R_TIMEDOUT);
1915
1916 isc_refcount_decrement(&active_creads);
1917
1918 atomic_fetch_add(&creads, 1);
1919
1920 isc_nmhandle_detach(&handle);
1921 }
1922
1923 static void
1924 udp_shutdown_read_connect_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1925 void *cbarg) {
1926 isc_nmhandle_t *readhandle = NULL;
1927 isc_nmhandle_t *sendhandle = NULL;
1928
1929 isc_refcount_decrement(&active_cconnects);
1930
1931 assert_int_equal(eresult, ISC_R_SUCCESS);
1932
1933 /* Read */
1934 isc_refcount_increment0(&active_creads);
1935 isc_nmhandle_attach(handle, &readhandle);
1936 isc_nm_read(handle, udp_shutdown_read_read_cb, cbarg);
1937 assert_true(handle->sock->reading);
1938
1939 /* Send */
1940 isc_refcount_increment0(&active_csends);
1941 isc_nmhandle_attach(handle, &sendhandle);
1942 isc_nmhandle_setwritetimeout(handle, T_IDLE);
1943 isc_nm_send(sendhandle, (isc_region_t *)&send_msg,
1944 udp_shutdown_read_send_cb, cbarg);
1945
1946 atomic_fetch_add(&cconnects, 1);
1947 }
1948
1949 int
1950 udp_shutdown_read_setup(void **state) {
1951 setup_udp_test(state);
1952 expected_cconnects = 1;
1953 expected_creads = 1;
1954 return 0;
1955 }
1956
1957 int
1958 udp_shutdown_read_teardown(void **state) {
1959 atomic_assert_int_eq(cconnects, expected_cconnects);
1960 atomic_assert_int_eq(creads, expected_creads);
1961 teardown_udp_test(state);
1962 return 0;
1963 }
1964
1965 void
1966 udp_shutdown_read(void **arg ISC_ATTR_UNUSED) {
1967 udp_start_listening(ISC_NM_LISTEN_ONE, udp_shutdown_read_recv_cb);
1968
1969 isc_refcount_increment0(&active_cconnects);
1970 udp_connect(udp_shutdown_read_connect_cb, NULL, UDP_T_SOFT);
1971 }
1972
1973 int
1974 proxyudp_shutdown_read_setup(void **state) {
1975 udp_use_PROXY = true;
1976 return udp_shutdown_read_setup(state);
1977 }
1978
1979 int
1980 proxyudp_shutdown_read_teardown(void **state) {
1981 int ret = udp_shutdown_read_teardown(state);
1982 udp_use_PROXY = false;
1983 return ret;
1984 }
1985
1986 static void
1987 udp_cancel_read_recv_cb(isc_nmhandle_t *handle, isc_result_t eresult,
1988 isc_region_t *region, void *cbarg) {
1989 uint64_t magic = 0;
1990
1991 UNUSED(cbarg);
1992
1993 assert_non_null(handle);
1994
1995 F();
1996
1997 assert_int_equal(eresult, ISC_R_SUCCESS);
1998
1999 assert_true(region->length == sizeof(magic));
2000
2001 memmove(&magic, region->base, sizeof(magic));
2002 assert_true(magic == send_magic);
2003 }
2004
2005 static void
2006 udp_cancel_read_send_cb(isc_nmhandle_t *handle, isc_result_t eresult,
2007 void *cbarg) {
2008 UNUSED(cbarg);
2009
2010 F();
2011
2012 assert_non_null(handle);
2013 assert_int_equal(eresult, ISC_R_SUCCESS);
2014
2015 atomic_fetch_add(&csends, 1);
2016
2017 isc_nm_cancelread(handle);
2018
2019 isc_nmhandle_detach(&handle);
2020 isc_refcount_decrement(&active_csends);
2021 }
2022
2023 static void
2024 udp_cancel_read_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
2025 isc_region_t *region, void *cbarg) {
2026 isc_nmhandle_t *sendhandle = NULL;
2027 isc_nmhandle_t *readhandle = NULL;
2028
2029 UNUSED(region);
2030
2031 F();
2032
2033 switch (eresult) {
2034 case ISC_R_TIMEDOUT:
2035
2036 /* Read again */
2037 isc_refcount_increment0(&active_creads);
2038 isc_nmhandle_attach(handle, &readhandle);
2039 isc_nm_read(handle, udp_cancel_read_read_cb, cbarg);
2040
2041 /* Send only once */
2042 if (isc_refcount_increment0(&active_csends) == 0) {
2043 isc_nmhandle_attach(handle, &sendhandle);
2044 isc_nmhandle_setwritetimeout(handle, T_IDLE);
2045 isc_nm_send(sendhandle, (isc_region_t *)&send_msg,
2046 udp_cancel_read_send_cb, cbarg);
2047 }
2048 break;
2049 case ISC_R_CANCELED:
2050 /* The read has been canceled */
2051 atomic_fetch_add(&creads, 1);
2052 isc_loopmgr_shutdown(loopmgr);
2053 break;
2054 default:
2055 UNREACHABLE();
2056 }
2057
2058 isc_refcount_decrement(&active_creads);
2059
2060 isc_nmhandle_detach(&handle);
2061 }
2062
2063 static void
2064 udp_cancel_read_connect_cb(isc_nmhandle_t *handle, isc_result_t eresult,
2065 void *cbarg) {
2066 isc_nmhandle_t *readhandle = NULL;
2067
2068 isc_refcount_decrement(&active_cconnects);
2069
2070 assert_int_equal(eresult, ISC_R_SUCCESS);
2071
2072 isc_refcount_increment0(&active_creads);
2073 isc_nmhandle_attach(handle, &readhandle);
2074 isc_nm_read(handle, udp_cancel_read_read_cb, cbarg);
2075
2076 atomic_fetch_add(&cconnects, 1);
2077 }
2078
2079 int
2080 udp_cancel_read_setup(void **state) {
2081 setup_udp_test(state);
2082 expected_cconnects = 1;
2083 expected_creads = 1;
2084 return 0;
2085 }
2086
2087 int
2088 udp_cancel_read_teardown(void **state) {
2089 atomic_assert_int_eq(cconnects, expected_cconnects);
2090 atomic_assert_int_eq(creads, expected_creads);
2091 teardown_udp_test(state);
2092 return 0;
2093 }
2094
2095 void
2096 udp_cancel_read(void **arg ISC_ATTR_UNUSED) {
2097 udp_start_listening(ISC_NM_LISTEN_ONE, udp_cancel_read_recv_cb);
2098
2099 isc_refcount_increment0(&active_cconnects);
2100 udp_connect(udp_cancel_read_connect_cb, NULL, UDP_T_SOFT);
2101 }
2102
2103 int
2104 proxyudp_cancel_read_setup(void **state) {
2105 udp_use_PROXY = true;
2106 return udp_cancel_read_setup(state);
2107 }
2108
2109 int
2110 proxyudp_cancel_read_teardown(void **state) {
2111 int ret = udp_cancel_read_teardown(state);
2112 udp_use_PROXY = false;
2113 return ret;
2114 }
2115
2116 int
2117 udp_recv_one_setup(void **state) {
2118 setup_udp_test(state);
2119
2120 connect_readcb = udp__connect_read_cb;
2121
2122 expected_cconnects = 1;
2123 cconnects_shutdown = false;
2124
2125 expected_csends = 1;
2126 csends_shutdown = false;
2127
2128 expected_sreads = 1;
2129 sreads_shutdown = false;
2130
2131 expected_ssends = 1;
2132 ssends_shutdown = false;
2133
2134 expected_creads = 1;
2135 creads_shutdown = true;
2136
2137 return 0;
2138 }
2139
2140 int
2141 udp_recv_one_teardown(void **state) {
2142 atomic_assert_int_eq(cconnects, expected_cconnects);
2143 atomic_assert_int_eq(csends, expected_csends);
2144 atomic_assert_int_eq(sreads, expected_sreads);
2145 atomic_assert_int_eq(ssends, expected_ssends);
2146 atomic_assert_int_eq(creads, expected_creads);
2147
2148 teardown_udp_test(state);
2149
2150 return 0;
2151 }
2152
2153 void
2154 udp_recv_one(void **arg ISC_ATTR_UNUSED) {
2155 udp_start_listening(ISC_NM_LISTEN_ONE, udp_listen_read_cb);
2156
2157 udp_enqueue_connect(NULL);
2158 }
2159
2160 int
2161 proxyudp_recv_one_setup(void **state) {
2162 udp_use_PROXY = true;
2163 return udp_recv_one_setup(state);
2164 }
2165
2166 int
2167 proxyudp_recv_one_teardown(void **state) {
2168 int ret = udp_recv_one_teardown(state);
2169 udp_use_PROXY = false;
2170 return ret;
2171 }
2172
2173 int
2174 udp_recv_two_setup(void **state) {
2175 setup_udp_test(state);
2176
2177 connect_readcb = udp__connect_read_cb;
2178
2179 expected_cconnects = 2;
2180 cconnects_shutdown = false;
2181
2182 expected_csends = 2;
2183 csends_shutdown = false;
2184
2185 expected_sreads = 2;
2186 sreads_shutdown = false;
2187
2188 expected_ssends = 2;
2189 ssends_shutdown = false;
2190
2191 expected_creads = 2;
2192 creads_shutdown = true;
2193
2194 return 0;
2195 }
2196
2197 int
2198 udp_recv_two_teardown(void **state) {
2199 atomic_assert_int_eq(cconnects, expected_cconnects);
2200 atomic_assert_int_eq(csends, expected_csends);
2201 atomic_assert_int_eq(sreads, expected_sreads);
2202 atomic_assert_int_eq(ssends, expected_ssends);
2203 atomic_assert_int_eq(creads, expected_creads);
2204
2205 teardown_udp_test(state);
2206 return 0;
2207 }
2208
2209 void
2210 udp_recv_two(void **arg ISC_ATTR_UNUSED) {
2211 udp_start_listening(ISC_NM_LISTEN_ONE, udp_listen_read_cb);
2212
2213 udp_enqueue_connect(NULL);
2214 udp_enqueue_connect(NULL);
2215 }
2216
2217 int
2218 proxyudp_recv_two_setup(void **state) {
2219 udp_use_PROXY = true;
2220 return udp_recv_two_setup(state);
2221 }
2222
2223 int
2224 proxyudp_recv_two_teardown(void **state) {
2225 int ret = udp_recv_two_teardown(state);
2226 udp_use_PROXY = false;
2227 return ret;
2228 }
2229
2230 int
2231 udp_recv_send_setup(void **state) {
2232 setup_udp_test(state);
2233
2234 /* Allow some leeway (+1) as datagram service is unreliable */
2235 expected_cconnects = (workers + 1) * NSENDS;
2236 cconnects_shutdown = false;
2237
2238 expected_creads = workers * NSENDS;
2239 do_send = true;
2240
2241 return 0;
2242 }
2243
2244 int
2245 udp_recv_send_teardown(void **state) {
2246 atomic_assert_int_ge(cconnects, expected_creads);
2247 atomic_assert_int_ge(csends, expected_creads);
2248 atomic_assert_int_ge(sreads, expected_creads);
2249 atomic_assert_int_ge(ssends, expected_creads);
2250 atomic_assert_int_ge(creads, expected_creads);
2251
2252 teardown_udp_test(state);
2253 return 0;
2254 }
2255
2256 void
2257 udp_recv_send(void **arg ISC_ATTR_UNUSED) {
2258 udp_start_listening(ISC_NM_LISTEN_ALL, udp_listen_read_cb);
2259
2260 for (size_t i = 0; i < workers; i++) {
2261 isc_async_run(isc_loop_get(loopmgr, i), udp_enqueue_connect,
2262 NULL);
2263 }
2264 }
2265
2266 int
2267 proxyudp_recv_send_setup(void **state) {
2268 udp_use_PROXY = true;
2269 return udp_recv_send_setup(state);
2270 }
2271
2272 int
2273 proxyudp_recv_send_teardown(void **state) {
2274 int ret = udp_recv_send_teardown(state);
2275 udp_use_PROXY = false;
2276 return ret;
2277 }
2278
2279 static void
2280 udp_double_read_send_cb(isc_nmhandle_t *handle, isc_result_t eresult,
2281 void *cbarg) {
2282 assert_non_null(handle);
2283
2284 F();
2285
2286 isc_refcount_decrement(&active_ssends);
2287
2288 switch (eresult) {
2289 case ISC_R_SUCCESS:
2290 if (have_expected_ssends(atomic_fetch_add(&ssends, 1) + 1)) {
2291 do_ssends_shutdown(loopmgr);
2292 } else {
2293 isc_nmhandle_t *sendhandle = NULL;
2294 isc_nmhandle_attach(handle, &sendhandle);
2295 isc_nmhandle_setwritetimeout(sendhandle, T_IDLE);
2296 isc_refcount_increment0(&active_ssends);
2297 isc_nm_send(sendhandle, &send_msg,
2298 udp_double_read_send_cb, cbarg);
2299 break;
2300 }
2301 break;
2302 case ISC_R_CANCELED:
2303 break;
2304 default:
2305 fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
2306 isc_result_totext(eresult), cbarg);
2307 assert_int_equal(eresult, ISC_R_SUCCESS);
2308 }
2309
2310 isc_nmhandle_detach(&handle);
2311 }
2312
2313 static void
2314 udp_double_read_listen_cb(isc_nmhandle_t *handle, isc_result_t eresult,
2315 isc_region_t *region, void *cbarg) {
2316 uint64_t magic = 0;
2317
2318 assert_non_null(handle);
2319
2320 F();
2321
2322 switch (eresult) {
2323 case ISC_R_EOF:
2324 case ISC_R_SHUTTINGDOWN:
2325 case ISC_R_CANCELED:
2326 break;
2327 case ISC_R_SUCCESS:
2328 memmove(&magic, region->base, sizeof(magic));
2329 assert_true(magic == send_magic);
2330
2331 assert_true(region->length >= sizeof(magic));
2332
2333 memmove(&magic, region->base, sizeof(magic));
2334 assert_true(magic == send_magic);
2335
2336 isc_nmhandle_t *sendhandle = NULL;
2337 isc_nmhandle_attach(handle, &sendhandle);
2338 isc_nmhandle_setwritetimeout(sendhandle, T_IDLE);
2339 isc_refcount_increment0(&active_ssends);
2340 isc_nm_send(sendhandle, &send_msg, udp_double_read_send_cb,
2341 cbarg);
2342 return;
2343 default:
2344 fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
2345 isc_result_totext(eresult), cbarg);
2346 assert_int_equal(eresult, ISC_R_SUCCESS);
2347 }
2348
2349 isc_refcount_decrement(&active_sreads);
2350
2351 isc_nmhandle_detach(&handle);
2352 }
2353
2354 static void
2355 udp_double_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
2356 isc_region_t *region, void *cbarg) {
2357 uint64_t magic = 0;
2358 bool detach = false;
2359
2360 assert_non_null(handle);
2361
2362 F();
2363
2364 switch (eresult) {
2365 case ISC_R_TIMEDOUT:
2366 /*
2367 * We are operating on the localhost, UDP cannot get lost, but
2368 * it could be delayed, so we read again until we get the
2369 * answer.
2370 */
2371 detach = false;
2372 break;
2373 case ISC_R_SUCCESS:
2374 assert_true(region->length >= sizeof(magic));
2375
2376 memmove(&magic, region->base, sizeof(magic));
2377
2378 assert_true(magic == send_magic);
2379
2380 if (have_expected_creads(atomic_fetch_add(&creads, 1) + 1)) {
2381 do_creads_shutdown(loopmgr);
2382 detach = true;
2383 }
2384
2385 if (magic == send_magic && allow_send_back) {
2386 connect_send(handle);
2387 return;
2388 }
2389
2390 break;
2391 case ISC_R_EOF:
2392 case ISC_R_SHUTTINGDOWN:
2393 case ISC_R_CANCELED:
2394 case ISC_R_CONNECTIONRESET:
2395 detach = true;
2396 break;
2397 default:
2398 fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
2399 isc_result_totext(eresult), cbarg);
2400 assert_int_equal(eresult, ISC_R_SUCCESS);
2401 }
2402
2403 if (detach) {
2404 isc_refcount_decrement(&active_creads);
2405 isc_nmhandle_detach(&handle);
2406 } else {
2407 isc_nm_read(handle, connect_readcb, cbarg);
2408 }
2409 }
2410
2411 int
2412 udp_double_read_setup(void **state) {
2413 setup_udp_test(state);
2414
2415 expected_cconnects = 1;
2416 cconnects_shutdown = false;
2417
2418 expected_csends = 1;
2419 csends_shutdown = false;
2420
2421 expected_sreads = 1;
2422 sreads_shutdown = false;
2423
2424 expected_ssends = 2;
2425 ssends_shutdown = false;
2426 expected_creads = 2;
2427 creads_shutdown = true;
2428
2429 connect_readcb = udp_double_read_cb;
2430
2431 return 0;
2432 }
2433
2434 int
2435 udp_double_read_teardown(void **state) {
2436 atomic_assert_int_eq(creads, expected_creads);
2437
2438 teardown_udp_test(state);
2439
2440 return 0;
2441 }
2442
2443 void
2444 udp_double_read(void **arg ISC_ATTR_UNUSED) {
2445 udp_start_listening(ISC_NM_LISTEN_ALL, udp_double_read_listen_cb);
2446
2447 udp_enqueue_connect(NULL);
2448 }
2449
2450 int
2451 proxyudp_double_read_setup(void **state) {
2452 udp_use_PROXY = true;
2453 return udp_double_read_setup(state);
2454 }
2455
2456 int
2457 proxyudp_double_read_teardown(void **state) {
2458 int ret = udp_double_read_teardown(state);
2459 udp_use_PROXY = false;
2460 return ret;
2461 }