Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/ssl_sock_stress.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "ssl_sock_test.h"
20
21
22 #if INCLUDE_SSLSOCK_TEST
23
24 /*
25 * SSL send load test: blast many sends, verify all callbacks fire.
26 * Uses small SO_SNDBUF to force async (PJ_EPENDING) send path.
27 */
28 #define SEND_LOAD_COUNT 100
29 #define SEND_LOAD_PKT_LEN 512
30
31 struct send_load_state
32 {
33 pj_pool_t *pool;
34 pj_ssl_sock_t *accepted_ssock;
35 pj_bool_t is_server;
36 pj_bool_t echo;
37 pj_status_t err;
38 pj_size_t sent;
39 pj_size_t recv;
40 pj_uint8_t read_buf[8192];
41 pj_bool_t done;
42 int pending_cnt;
43 int sent_cb_cnt;
44 int send_idx;
45 int renego_at;
46 pj_bool_t renego_done;
47 pj_ioqueue_op_key_t op_keys[SEND_LOAD_COUNT];
48 char send_data[SEND_LOAD_PKT_LEN];
49 };
50
51 static pj_bool_t load_on_connect_complete(pj_ssl_sock_t *ssock,
52 pj_status_t status)
53 {
54 struct send_load_state *st = (struct send_load_state *)
55 pj_ssl_sock_get_user_data(ssock);
56 void *read_buf[1];
57 int i;
58
59 if (status != PJ_SUCCESS) {
60 st->err = status;
61 return PJ_FALSE;
62 }
63
64 /* Start reading */
65 read_buf[0] = st->read_buf;
66 status = pj_ssl_sock_start_read2(ssock, st->pool,
67 sizeof(st->read_buf),
68 (void **)read_buf, 0);
69 if (status != PJ_SUCCESS) {
70 st->err = status;
71 return PJ_FALSE;
72 }
73
74 /* Blast sends. When renego_at > 0, only send the first half here;
75 * the second half is sent from on_data_read after renegotiation
76 * completes (during polling, so handshake messages can be exchanged).
77 */
78 {
79 int count = (st->renego_at > 0) ? st->renego_at : SEND_LOAD_COUNT;
80 for (i = 0; i < count; i++) {
81 pj_ssize_t len = SEND_LOAD_PKT_LEN;
82
83 status = pj_ssl_sock_send(ssock, &st->op_keys[i],
84 st->send_data, &len, 0);
85 if (status == PJ_EPENDING || status == PJ_EBUSY) {
86 st->pending_cnt++;
87 } else if (status == PJ_SUCCESS) {
88 st->sent += len;
89 } else {
90 st->err = status;
91 return PJ_FALSE;
92 }
93 st->send_idx++;
94 }
95 }
96
97 return PJ_TRUE;
98 }
99
100 static pj_bool_t load_on_accept_complete(pj_ssl_sock_t *ssock,
101 pj_ssl_sock_t *newsock,
102 const pj_sockaddr_t *src_addr,
103 int src_addr_len,
104 pj_status_t accept_status)
105 {
106 struct send_load_state *parent_st = (struct send_load_state *)
107 pj_ssl_sock_get_user_data(ssock);
108 struct send_load_state *st;
109 void *read_buf[1];
110 pj_status_t status;
111
112 PJ_UNUSED_ARG(src_addr);
113 PJ_UNUSED_ARG(src_addr_len);
114
115 if (accept_status != PJ_SUCCESS)
116 return PJ_FALSE;
117
118 st = (struct send_load_state *)pj_pool_zalloc(parent_st->pool,
119 sizeof(struct send_load_state));
120 *st = *parent_st;
121 pj_ssl_sock_set_user_data(newsock, st);
122
123 /* Track accepted socket for cleanup */
124 parent_st->accepted_ssock = newsock;
125
126 read_buf[0] = st->read_buf;
127 status = pj_ssl_sock_start_read2(newsock, st->pool,
128 sizeof(st->read_buf),
129 (void **)read_buf, 0);
130 if (status != PJ_SUCCESS) {
131 st->err = status;
132 return PJ_FALSE;
133 }
134
135 return PJ_TRUE;
136 }
137
138 static pj_bool_t load_on_data_read(pj_ssl_sock_t *ssock,
139 void *data,
140 pj_size_t size,
141 pj_status_t status,
142 pj_size_t *remainder)
143 {
144 struct send_load_state *st = (struct send_load_state *)
145 pj_ssl_sock_get_user_data(ssock);
146
147 if (remainder)
148 *remainder = 0;
149
150 if (size > 0) {
151 st->recv += size;
152
153 /* Server: trigger renegotiation if configured */
154 if (st->is_server && st->renego_at > 0 && !st->renego_done) {
155 pj_size_t threshold = (pj_size_t)st->renego_at *
156 SEND_LOAD_PKT_LEN;
157 if (st->recv >= threshold) {
158 pj_status_t s = pj_ssl_sock_renegotiate(ssock);
159 if (s != PJ_SUCCESS && s != PJ_EPENDING) {
160 PJ_LOG(3, ("", "...server renegotiate: %d", s));
161 }
162 st->renego_done = PJ_TRUE;
163 }
164 }
165
166 /* Server echoes data back */
167 if (st->echo) {
168 pj_ssize_t sz = (pj_ssize_t)size;
169 pj_status_t s;
170
171 s = pj_ssl_sock_send(ssock, &st->op_keys[0], data, &sz, 0);
172 if (s != PJ_SUCCESS && s != PJ_EPENDING &&
173 s != PJ_EBUSY)
174 {
175 st->err = s;
176 }
177 }
178
179 /* Client: trigger renegotiation + send second half when first
180 * half has been echoed back (during polling, so handshake can
181 * complete via ioqueue event processing).
182 */
183 if (!st->is_server && st->renego_at > 0 && !st->renego_done) {
184 pj_size_t threshold = (pj_size_t)st->renego_at *
185 SEND_LOAD_PKT_LEN;
186 if (st->recv >= threshold) {
187 pj_status_t s;
188 int i;
189
190 s = pj_ssl_sock_renegotiate(ssock);
191 if (s != PJ_SUCCESS && s != PJ_EPENDING) {
192 PJ_LOG(3, ("", "...client renegotiate: %d", s));
193 st->err = s;
194 }
195 st->renego_done = PJ_TRUE;
196
197 /* Send second half */
198 for (i = st->send_idx; i < SEND_LOAD_COUNT; i++) {
199 pj_ssize_t len = SEND_LOAD_PKT_LEN;
200
201 s = pj_ssl_sock_send(ssock, &st->op_keys[i],
202 st->send_data, &len, 0);
203 if (s == PJ_EPENDING || s == PJ_EBUSY) {
204 st->pending_cnt++;
205 } else if (s == PJ_SUCCESS) {
206 st->sent += len;
207 } else {
208 st->err = s;
209 return PJ_FALSE;
210 }
211 st->send_idx++;
212 }
213 }
214 }
215
216 /* Client: check if all echoed data received */
217 if (!st->is_server) {
218 pj_size_t expected = (pj_size_t)st->send_idx *
219 SEND_LOAD_PKT_LEN;
220 if (st->recv >= expected)
221 st->done = PJ_TRUE;
222 }
223 }
224
225 if (status != PJ_SUCCESS) {
226 if (status == PJ_EEOF) {
227 st->done = PJ_TRUE;
228 } else {
229 st->err = status;
230 }
231 }
232
233 if (st->err != PJ_SUCCESS || st->done)
234 return PJ_FALSE;
235
236 return PJ_TRUE;
237 }
238
239 static pj_bool_t load_on_data_sent(pj_ssl_sock_t *ssock,
240 pj_ioqueue_op_key_t *op_key,
241 pj_ssize_t sent)
242 {
243 struct send_load_state *st = (struct send_load_state *)
244 pj_ssl_sock_get_user_data(ssock);
245 PJ_UNUSED_ARG(op_key);
246
247 if (sent < 0) {
248 st->err = (pj_status_t)-sent;
249 return PJ_FALSE;
250 }
251
252 st->sent += sent;
253 st->sent_cb_cnt++;
254 return PJ_TRUE;
255 }
256
257 /* renego_at: trigger renegotiation after this many sends/packets.
258 * renego_srv: PJ_TRUE = server-initiated, PJ_FALSE = client-initiated.
259 */
260 static int send_load_test_inner(int renego_at, pj_bool_t renego_srv)
261 {
262 pj_pool_t *pool = NULL;
263 pj_ioqueue_t *ioqueue = NULL;
264 pj_timer_heap_t *timer = NULL;
265 pj_ssl_sock_t *ssock_serv = NULL;
266 pj_ssl_sock_t *ssock_cli = NULL;
267 pj_ssl_sock_param param;
268 struct send_load_state state_serv;
269 struct send_load_state state_cli;
270 pj_sockaddr addr, listen_addr;
271
272 pj_status_t status;
273 int i;
274
275 pool = pj_pool_create(mem, "ssl_load", 4096, 4096, NULL);
276
277 pj_bzero(&state_serv, sizeof(state_serv));
278 pj_bzero(&state_cli, sizeof(state_cli));
279
280 status = pj_ioqueue_create(pool, 4, &ioqueue);
281 if (status != PJ_SUCCESS) {
282 app_perror("...send_load_test: ioqueue create", status);
283 goto on_return;
284 }
285
286 status = pj_timer_heap_create(pool, 4, &timer);
287 if (status != PJ_SUCCESS) {
288 app_perror("...send_load_test: timer create", status);
289 goto on_return;
290 }
291
292 pj_ssl_sock_param_default(&param);
293 param.cb.on_accept_complete2 = &load_on_accept_complete;
294 param.cb.on_connect_complete = &load_on_connect_complete;
295 param.cb.on_data_read = &load_on_data_read;
296 param.cb.on_data_sent = &load_on_data_sent;
297 param.ioqueue = ioqueue;
298 param.timer_heap = timer;
299 param.proto = PJ_SSL_SOCK_PROTO_TLS1_2;
300 param.ciphers_num = 0;
301
302 {
303 pj_str_t tmp_st;
304 pj_sockaddr_init(PJ_AF_INET, &addr,
305 pj_strset2(&tmp_st, "127.0.0.1"), 0);
306 }
307
308 /* Fill send data with repeating byte pattern. */
309 for (i = 0; i < SEND_LOAD_PKT_LEN; i++)
310 state_cli.send_data[i] = (char)(i & 0xFF);
311
312 /* SERVER */
313 state_serv.pool = pool;
314 state_serv.echo = PJ_TRUE;
315 state_serv.is_server = PJ_TRUE;
316 state_serv.renego_at = renego_srv ? renego_at : 0;
317 param.user_data = &state_serv;
318 param.require_client_cert = PJ_FALSE;
319
320 listen_addr = addr;
321 status = ssl_test_create_server(pool, &param, "send_load_test",
322 &ssock_serv, &listen_addr);
323 if (status != PJ_SUCCESS)
324 goto on_return;
325
326 /* CLIENT */
327 param.user_data = &state_cli;
328 param.require_client_cert = PJ_FALSE;
329
330 state_cli.pool = pool;
331 state_cli.echo = PJ_FALSE;
332 state_cli.is_server = PJ_FALSE;
333 state_cli.renego_at = renego_srv ? 0 : renego_at;
334
335 status = pj_ssl_sock_create(pool, &param, &ssock_cli);
336 if (status != PJ_SUCCESS) {
337 app_perror("...send_load_test: client create", status);
338 goto on_return;
339 }
340
341 status = pj_ssl_sock_start_connect(ssock_cli, pool, &addr,
342 &listen_addr,
343 pj_sockaddr_get_len(&addr));
344 if (status == PJ_SUCCESS) {
345 load_on_connect_complete(ssock_cli, PJ_SUCCESS);
346 } else if (status != PJ_EPENDING) {
347 app_perror("...send_load_test: connect", status);
348 goto on_return;
349 }
350
351 /* Poll until done or error */
352 {
353 pj_timestamp t_start, t_now;
354 pj_uint32_t elapsed;
355
356 pj_get_timestamp(&t_start);
357 while (!state_cli.err && !state_cli.done) {
358 pj_time_val delay = {0, 100};
359 pj_ioqueue_poll(ioqueue, &delay);
360 pj_timer_heap_poll(timer, NULL);
361
362 pj_get_timestamp(&t_now);
363 elapsed = pj_elapsed_msec(&t_start, &t_now);
364 if (elapsed > 30000) {
365 PJ_LOG(1, ("", "...send_load_test TIMEOUT after 30s "
366 "(sent=%lu recv=%lu pend=%d cb=%d)",
367 (unsigned long)state_cli.sent,
368 (unsigned long)state_cli.recv,
369 state_cli.pending_cnt,
370 state_cli.sent_cb_cnt));
371 status = PJ_ETIMEDOUT;
372 goto on_return;
373 }
374 }
375 }
376
377 if (state_cli.err) {
378 status = state_cli.err;
379 app_perror("...send_load_test client error", status);
380 goto on_return;
381 }
382
383 /* Verify results */
384 PJ_LOG(3, ("", "...send_load_test%s: sent=%lu, recv=%lu, "
385 "pending=%d, sent_cb=%d",
386 renego_at ? " (renego)" : "",
387 (unsigned long)state_cli.sent,
388 (unsigned long)state_cli.recv,
389 state_cli.pending_cnt,
390 state_cli.sent_cb_cnt));
391
392 if (state_cli.pending_cnt == 0 && !renego_at) {
393 PJ_LOG(3, ("", "...NOTE: all sends completed synchronously, "
394 "async path NOT tested. Set PJ_IOQUEUE_FAST_TRACK=0 "
395 "in config_site.h to force async path."));
396 }
397
398 if (state_cli.sent_cb_cnt != state_cli.pending_cnt) {
399 PJ_LOG(1, ("", "...ERROR: sent callback count (%d) != "
400 "pending count (%d)",
401 state_cli.sent_cb_cnt, state_cli.pending_cnt));
402 status = PJ_EBUG;
403 goto on_return;
404 }
405
406 if (state_cli.sent != (pj_size_t)SEND_LOAD_COUNT * SEND_LOAD_PKT_LEN) {
407 PJ_LOG(1, ("", "...ERROR: total sent (%lu) != expected (%lu)",
408 (unsigned long)state_cli.sent,
409 (unsigned long)(SEND_LOAD_COUNT * SEND_LOAD_PKT_LEN)));
410 status = PJ_EBUG;
411 goto on_return;
412 }
413
414 status = PJ_SUCCESS;
415
416 on_return:
417 if (ssock_cli)
418 pj_ssl_sock_close(ssock_cli);
419 if (state_serv.accepted_ssock)
420 pj_ssl_sock_close(state_serv.accepted_ssock);
421 if (ssock_serv)
422 pj_ssl_sock_close(ssock_serv);
423
424 /* Poll to drain pending events after close */
425 if (ioqueue) {
426 pj_time_val delay = {0, 500};
427 int n = 50;
428 while (n-- > 0 && pj_ioqueue_poll(ioqueue, &delay) > 0)
429 ;
430 }
431
432 if (timer)
433 pj_timer_heap_destroy(timer);
434 if (ioqueue)
435 pj_ioqueue_destroy(ioqueue);
436 if (pool)
437 pj_pool_release(pool);
438
439 return (status == PJ_SUCCESS) ? 0 : -1;
440 }
441
442 /*
443 * Close under pending sends test: blast sends then immediately close socket.
444 * Verifies no crash or use-after-free.
445 */
446 struct close_pending_state
447 {
448 pj_pool_t *pool;
449 pj_ssl_sock_t *accepted_ssock;
450 pj_bool_t is_server;
451 pj_bool_t echo;
452 pj_status_t err;
453 pj_size_t sent;
454 pj_size_t recv;
455 pj_uint8_t read_buf[8192];
456 pj_bool_t done;
457 int pending_cnt;
458 int sent_cb_cnt;
459 int err_cb_cnt;
460 int send_idx;
461 int renego_at;
462 pj_bool_t renego_done;
463 pj_ioqueue_op_key_t op_keys[SEND_LOAD_COUNT];
464 char send_data[SEND_LOAD_PKT_LEN];
465 pj_bool_t blast_done;
466 };
467
468 static pj_bool_t cp_on_connect_complete(pj_ssl_sock_t *ssock,
469 pj_status_t status)
470 {
471 struct close_pending_state *st = (struct close_pending_state *)
472 pj_ssl_sock_get_user_data(ssock);
473 void *read_buf[1];
474 pj_ssize_t len;
475 int i;
476
477 if (status != PJ_SUCCESS) {
478 st->err = status;
479 return PJ_FALSE;
480 }
481
482 read_buf[0] = st->read_buf;
483 status = pj_ssl_sock_start_read2(ssock, st->pool,
484 sizeof(st->read_buf),
485 (void **)read_buf, 0);
486 if (status != PJ_SUCCESS) {
487 st->err = status;
488 return PJ_FALSE;
489 }
490
491 /* Blast sends, with optional renegotiation mid-stream */
492 for (i = 0; i < SEND_LOAD_COUNT; i++) {
493 if (st->renego_at > 0 && i == st->renego_at) {
494 pj_status_t s = pj_ssl_sock_renegotiate(ssock);
495 if (s != PJ_SUCCESS && s != PJ_EPENDING) {
496 st->err = s;
497 break;
498 }
499 }
500
501 len = SEND_LOAD_PKT_LEN;
502 status = pj_ssl_sock_send(ssock, &st->op_keys[i],
503 st->send_data, &len, 0);
504 if (status == PJ_EPENDING) {
505 st->pending_cnt++;
506 } else if (status == PJ_SUCCESS) {
507 st->sent += len;
508 } else {
509 /* Send error during blast, stop but don't fail test */
510 break;
511 }
512 st->send_idx++;
513 }
514
515 st->blast_done = PJ_TRUE;
516 return PJ_TRUE;
517 }
518
519 static pj_bool_t cp_on_data_read(pj_ssl_sock_t *ssock,
520 void *data,
521 pj_size_t size,
522 pj_status_t status,
523 pj_size_t *remainder)
524 {
525 struct close_pending_state *st = (struct close_pending_state *)
526 pj_ssl_sock_get_user_data(ssock);
527
528 PJ_UNUSED_ARG(data);
529
530 if (remainder)
531 *remainder = 0;
532
533 if (size > 0)
534 st->recv += size;
535
536 /* Server: just consume data, no echo needed for this test */
537 if (status != PJ_SUCCESS && status != PJ_EEOF) {
538 st->err = status;
539 return PJ_FALSE;
540 }
541
542 return PJ_TRUE;
543 }
544
545 static pj_bool_t cp_on_data_sent(pj_ssl_sock_t *ssock,
546 pj_ioqueue_op_key_t *op_key,
547 pj_ssize_t sent)
548 {
549 struct close_pending_state *st = (struct close_pending_state *)
550 pj_ssl_sock_get_user_data(ssock);
551 PJ_UNUSED_ARG(op_key);
552
553 /* Tolerate errors — socket may be closing under us */
554 if (sent > 0)
555 st->sent += sent;
556 else
557 st->err_cb_cnt++;
558 st->sent_cb_cnt++;
559
560 return PJ_TRUE;
561 }
562
563 static int close_pending_test(void)
564 {
565 pj_pool_t *pool = NULL;
566 pj_ioqueue_t *ioqueue = NULL;
567 pj_timer_heap_t *timer = NULL;
568 pj_ssl_sock_t *ssock_serv = NULL;
569 pj_ssl_sock_t *ssock_cli = NULL;
570 pj_ssl_sock_param param;
571 struct close_pending_state state_serv;
572 struct close_pending_state state_cli;
573 pj_sockaddr addr, listen_addr;
574
575 pj_status_t status;
576 int i;
577
578 pool = pj_pool_create(mem, "ssl_closep", 8192, 4096, NULL);
579
580 pj_bzero(&state_serv, sizeof(state_serv));
581 pj_bzero(&state_cli, sizeof(state_cli));
582
583 status = pj_ioqueue_create(pool, 4, &ioqueue);
584 if (status != PJ_SUCCESS) {
585 app_perror("...close_pending_test: ioqueue create", status);
586 goto on_return;
587 }
588
589 status = pj_timer_heap_create(pool, 4, &timer);
590 if (status != PJ_SUCCESS) {
591 app_perror("...close_pending_test: timer create", status);
592 goto on_return;
593 }
594
595 pj_ssl_sock_param_default(&param);
596 param.cb.on_accept_complete2 = &load_on_accept_complete;
597 param.cb.on_connect_complete = &cp_on_connect_complete;
598 param.cb.on_data_read = &cp_on_data_read;
599 param.cb.on_data_sent = &cp_on_data_sent;
600 param.ioqueue = ioqueue;
601 param.timer_heap = timer;
602 param.proto = PJ_SSL_SOCK_PROTO_TLS1_2;
603 param.ciphers_num = 0;
604
605 {
606 pj_str_t tmp_st;
607 pj_sockaddr_init(PJ_AF_INET, &addr,
608 pj_strset2(&tmp_st, "127.0.0.1"), 0);
609 }
610
611 for (i = 0; i < SEND_LOAD_PKT_LEN; i++)
612 state_cli.send_data[i] = (char)(i & 0xFF);
613
614 /* SERVER */
615 state_serv.pool = pool;
616 state_serv.is_server = PJ_TRUE;
617 param.user_data = &state_serv;
618 param.require_client_cert = PJ_FALSE;
619
620 listen_addr = addr;
621 status = ssl_test_create_server(pool, &param, "close_pending_test",
622 &ssock_serv, &listen_addr);
623 if (status != PJ_SUCCESS)
624 goto on_return;
625
626 /* CLIENT */
627 state_cli.pool = pool;
628 state_cli.is_server = PJ_FALSE;
629 param.user_data = &state_cli;
630
631 status = pj_ssl_sock_create(pool, &param, &ssock_cli);
632 if (status != PJ_SUCCESS) {
633 app_perror("...close_pending_test: client create", status);
634 goto on_return;
635 }
636
637 status = pj_ssl_sock_start_connect(ssock_cli, pool, &addr,
638 &listen_addr,
639 pj_sockaddr_get_len(&addr));
640 if (status == PJ_SUCCESS) {
641 cp_on_connect_complete(ssock_cli, PJ_SUCCESS);
642 } else if (status != PJ_EPENDING) {
643 app_perror("...close_pending_test: connect", status);
644 goto on_return;
645 }
646
647 /* Poll until blast is done */
648 {
649 pj_timestamp t_start, t_now;
650 pj_uint32_t elapsed;
651
652 pj_get_timestamp(&t_start);
653 while (!state_cli.blast_done && !state_cli.err) {
654 pj_time_val delay = {0, 100};
655 pj_ioqueue_poll(ioqueue, &delay);
656 pj_timer_heap_poll(timer, NULL);
657
658 pj_get_timestamp(&t_now);
659 elapsed = pj_elapsed_msec(&t_start, &t_now);
660 if (elapsed > 30000) {
661 PJ_LOG(1, ("", "...close_pending_test TIMEOUT"));
662 status = PJ_ETIMEDOUT;
663 goto on_return;
664 }
665 }
666 }
667
668 /* Close client while sends may still be pending */
669 PJ_LOG(3, ("", "...close_pending_test: closing with pending=%d "
670 "sent_cb=%d", state_cli.pending_cnt,
671 state_cli.sent_cb_cnt));
672 pj_ssl_sock_close(ssock_cli);
673 ssock_cli = NULL;
674
675 /* Poll briefly to let server-side and async cleanup drain */
676 {
677 pj_time_val delay = {0, 500};
678 int n = 20;
679 while (n-- > 0)
680 pj_ioqueue_poll(ioqueue, &delay);
681 }
682
683 PJ_LOG(3, ("", "...close_pending_test: pending=%d sent_cb=%d "
684 "err_cb=%d",
685 state_cli.pending_cnt, state_cli.sent_cb_cnt,
686 state_cli.err_cb_cnt));
687
688 /* Every send that returned PJ_EPENDING must get a callback:
689 * either a successful completion or an error/cancel on destroy.
690 */
691 if (state_cli.pending_cnt > 0 &&
692 state_cli.pending_cnt != state_cli.sent_cb_cnt)
693 {
694 PJ_LOG(1, ("", "...close_pending_test: pending=%d != sent_cb=%d "
695 "(callbacks lost on destroy!)",
696 state_cli.pending_cnt, state_cli.sent_cb_cnt));
697 status = PJ_EBUG;
698 goto on_return;
699 }
700
701 status = PJ_SUCCESS;
702
703 on_return:
704 if (ssock_cli)
705 pj_ssl_sock_close(ssock_cli);
706 if (state_serv.accepted_ssock)
707 pj_ssl_sock_close(state_serv.accepted_ssock);
708 if (ssock_serv)
709 pj_ssl_sock_close(ssock_serv);
710
711 /* Poll to drain pending events after close */
712 if (ioqueue) {
713 pj_time_val delay = {0, 500};
714 int n = 50;
715 while (n-- > 0 && pj_ioqueue_poll(ioqueue, &delay) > 0)
716 ;
717 }
718
719 if (timer)
720 pj_timer_heap_destroy(timer);
721 if (ioqueue)
722 pj_ioqueue_destroy(ioqueue);
723 if (pool)
724 pj_pool_release(pool);
725
726 return (status == PJ_SUCCESS) ? 0 : -1;
727 }
728
729
730 /* Renegotiation support check — only OpenSSL and GnuTLS actually
731 * support initiating renegotiation. Must use TLS 1.2 (already the
732 * default for all stress tests). */
733 /* Client-initiated renegotiation: OpenSSL only.
734 * GnuTLS does not support client-initiated renegotiation. */
735 #if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_OPENSSL)
736 # define HAS_CLIENT_RENEGO_TEST 1
737 #else
738 # define HAS_CLIENT_RENEGO_TEST 0
739 #endif
740
741 /* Server-initiated renegotiation: OpenSSL and GnuTLS. */
742 #if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_OPENSSL) || \
743 (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_GNUTLS)
744 # define HAS_RENEGO_TEST 1
745 #else
746 # define HAS_RENEGO_TEST 0
747 #endif
748
749 /*
750 * Destroy-during-renegotiation test: close socket while sends are delayed
751 * due to renegotiation. Verifies on_data_sent callbacks fire with error.
752 */
753 #if HAS_CLIENT_RENEGO_TEST
754 static int destroy_during_renego_test(void)
755 {
756 pj_pool_t *pool = NULL;
757 pj_ioqueue_t *ioqueue = NULL;
758 pj_timer_heap_t *timer = NULL;
759 pj_ssl_sock_t *ssock_serv = NULL;
760 pj_ssl_sock_t *ssock_cli = NULL;
761 pj_ssl_sock_param param;
762 struct close_pending_state state_serv;
763 struct close_pending_state state_cli;
764 pj_sockaddr addr, listen_addr;
765 pj_status_t status;
766 int i;
767
768 pool = pj_pool_create(mem, "ssl_drenego", 8192, 4096, NULL);
769 pj_bzero(&state_serv, sizeof(state_serv));
770 pj_bzero(&state_cli, sizeof(state_cli));
771
772 status = pj_ioqueue_create(pool, 4, &ioqueue);
773 if (status != PJ_SUCCESS) goto on_return;
774
775 status = pj_timer_heap_create(pool, 4, &timer);
776 if (status != PJ_SUCCESS) goto on_return;
777
778 pj_ssl_sock_param_default(&param);
779 param.cb.on_accept_complete2 = &load_on_accept_complete;
780 param.cb.on_connect_complete = &cp_on_connect_complete;
781 param.cb.on_data_read = &cp_on_data_read;
782 param.cb.on_data_sent = &cp_on_data_sent;
783 param.ioqueue = ioqueue;
784 param.timer_heap = timer;
785 param.proto = PJ_SSL_SOCK_PROTO_TLS1_2;
786 param.ciphers_num = 0;
787
788 {
789 pj_str_t tmp_st;
790 pj_sockaddr_init(PJ_AF_INET, &addr,
791 pj_strset2(&tmp_st, "127.0.0.1"), 0);
792 }
793
794 for (i = 0; i < SEND_LOAD_PKT_LEN; i++)
795 state_cli.send_data[i] = (char)(i & 0xFF);
796
797 /* SERVER */
798 state_serv.pool = pool;
799 state_serv.is_server = PJ_TRUE;
800 param.user_data = &state_serv;
801 param.require_client_cert = PJ_FALSE;
802
803 listen_addr = addr;
804 status = ssl_test_create_server(pool, &param, "destroy_renego",
805 &ssock_serv, &listen_addr);
806 if (status != PJ_SUCCESS) goto on_return;
807
808 /* CLIENT — renegotiate mid-blast so second half gets delayed */
809 state_cli.pool = pool;
810 state_cli.is_server = PJ_FALSE;
811 state_cli.renego_at = SEND_LOAD_COUNT / 2;
812 param.user_data = &state_cli;
813
814 status = pj_ssl_sock_create(pool, &param, &ssock_cli);
815 if (status != PJ_SUCCESS) goto on_return;
816
817 status = pj_ssl_sock_start_connect(ssock_cli, pool, &addr,
818 &listen_addr,
819 pj_sockaddr_get_len(&addr));
820 if (status == PJ_SUCCESS)
821 cp_on_connect_complete(ssock_cli, PJ_SUCCESS);
822 else if (status != PJ_EPENDING)
823 goto on_return;
824
825 /* Poll until blast is done (all sends attempted, some delayed) */
826 {
827 pj_timestamp t_start, t_now;
828 pj_uint32_t elapsed;
829 pj_get_timestamp(&t_start);
830 while (!state_cli.blast_done && !state_cli.err) {
831 pj_time_val delay = {0, 100};
832 pj_ioqueue_poll(ioqueue, &delay);
833 pj_timer_heap_poll(timer, NULL);
834 pj_get_timestamp(&t_now);
835 elapsed = pj_elapsed_msec(&t_start, &t_now);
836 if (elapsed > 30000) {
837 status = PJ_ETIMEDOUT;
838 goto on_return;
839 }
840 }
841 }
842
843 /* Close immediately — DO NOT wait for renegotiation to complete.
844 * Delayed sends in write_pending must get callbacks on destroy.
845 */
846 PJ_LOG(3, ("", "...destroy_renego: closing with pending=%d",
847 state_cli.pending_cnt));
848 pj_ssl_sock_close(ssock_cli);
849 ssock_cli = NULL;
850
851 {
852 pj_time_val delay = {0, 500};
853 int n = 20;
854 while (n-- > 0)
855 pj_ioqueue_poll(ioqueue, &delay);
856 }
857
858 PJ_LOG(3, ("", "...destroy_renego: pending=%d sent_cb=%d err_cb=%d",
859 state_cli.pending_cnt, state_cli.sent_cb_cnt,
860 state_cli.err_cb_cnt));
861
862 if (state_cli.pending_cnt == 0) {
863 PJ_LOG(3, ("", "...NOTE: all sends completed synchronously, "
864 "destroy callback path NOT tested"));
865 }
866
867 if (state_cli.pending_cnt != state_cli.sent_cb_cnt) {
868 PJ_LOG(1, ("", "...destroy_renego: pending=%d != sent_cb=%d",
869 state_cli.pending_cnt, state_cli.sent_cb_cnt));
870 status = PJ_EBUG;
871 goto on_return;
872 }
873
874 if (state_cli.pending_cnt > 0 && state_cli.err_cb_cnt == 0) {
875 PJ_LOG(1, ("", "...destroy_renego: pending=%d but 0 error "
876 "callbacks", state_cli.pending_cnt));
877 status = PJ_EBUG;
878 goto on_return;
879 }
880
881 status = PJ_SUCCESS;
882
883 on_return:
884 if (ssock_cli) pj_ssl_sock_close(ssock_cli);
885 if (state_serv.accepted_ssock)
886 pj_ssl_sock_close(state_serv.accepted_ssock);
887 if (ssock_serv) pj_ssl_sock_close(ssock_serv);
888
889 if (ioqueue) {
890 pj_time_val delay = {0, 500};
891 int n = 50;
892 while (n-- > 0 && pj_ioqueue_poll(ioqueue, &delay) > 0)
893 ;
894 }
895 if (timer) pj_timer_heap_destroy(timer);
896 if (ioqueue) pj_ioqueue_destroy(ioqueue);
897 if (pool) pj_pool_release(pool);
898
899 return (status == PJ_SUCCESS) ? 0 : -1;
900 }
901 #endif /* HAS_CLIENT_RENEGO_TEST */
902
903
904 /*
905 * Bidirectional simultaneous load test: both sides send independent data.
906 */
907 #define BIDIR_SEND_COUNT 100
908 #define BIDIR_PKT_LEN 1024
909
910 struct bidir_state
911 {
912 pj_pool_t *pool;
913 pj_ssl_sock_t *accepted_ssock;
914 pj_bool_t is_server;
915 pj_status_t err;
916 pj_size_t sent;
917 pj_size_t recv;
918 pj_uint8_t read_buf[8192];
919 pj_bool_t done;
920 pj_bool_t recv_done;
921 int pending_cnt;
922 int sent_cb_cnt;
923 int send_idx;
924 int renego_at;
925 pj_ioqueue_op_key_t op_keys[BIDIR_SEND_COUNT];
926 char send_data[BIDIR_PKT_LEN];
927 pj_size_t expected_recv;
928 };
929
930 static pj_bool_t bidir_blast_sends(pj_ssl_sock_t *ssock,
931 struct bidir_state *st)
932 {
933 int i;
934
935 for (i = st->send_idx; i < BIDIR_SEND_COUNT; i++) {
936 pj_ssize_t len = BIDIR_PKT_LEN;
937 pj_status_t s;
938
939 /* Trigger renegotiation mid-stream if configured */
940 if (st->renego_at > 0 && i == st->renego_at) {
941 s = pj_ssl_sock_renegotiate(ssock);
942 if (s != PJ_SUCCESS && s != PJ_EPENDING) {
943 st->err = s;
944 return PJ_FALSE;
945 }
946 }
947
948 s = pj_ssl_sock_send(ssock, &st->op_keys[i],
949 st->send_data, &len, 0);
950 if (s == PJ_EPENDING || s == PJ_EBUSY) {
951 st->pending_cnt++;
952 } else if (s == PJ_SUCCESS) {
953 st->sent += len;
954 } else {
955 st->err = s;
956 return PJ_FALSE;
957 }
958 st->send_idx++;
959 }
960 return PJ_TRUE;
961 }
962
963 static pj_bool_t bidir_on_connect_complete(pj_ssl_sock_t *ssock,
964 pj_status_t status)
965 {
966 struct bidir_state *st = (struct bidir_state *)
967 pj_ssl_sock_get_user_data(ssock);
968 void *read_buf[1];
969
970 if (status != PJ_SUCCESS) {
971 st->err = status;
972 return PJ_FALSE;
973 }
974
975 read_buf[0] = st->read_buf;
976 status = pj_ssl_sock_start_read2(ssock, st->pool,
977 sizeof(st->read_buf),
978 (void **)read_buf, 0);
979 if (status != PJ_SUCCESS) {
980 st->err = status;
981 return PJ_FALSE;
982 }
983
984 return bidir_blast_sends(ssock, st);
985 }
986
987 static pj_bool_t bidir_on_accept_complete(pj_ssl_sock_t *ssock,
988 pj_ssl_sock_t *newsock,
989 const pj_sockaddr_t *src_addr,
990 int src_addr_len,
991 pj_status_t accept_status)
992 {
993 struct bidir_state *parent_st = (struct bidir_state *)
994 pj_ssl_sock_get_user_data(ssock);
995 struct bidir_state *st;
996 void *read_buf[1];
997 pj_status_t status;
998
999 PJ_UNUSED_ARG(src_addr);
1000 PJ_UNUSED_ARG(src_addr_len);
1001
1002 if (accept_status != PJ_SUCCESS)
1003 return PJ_FALSE;
1004
1005 st = (struct bidir_state *)pj_pool_zalloc(parent_st->pool,
1006 sizeof(struct bidir_state));
1007 *st = *parent_st;
1008 st->send_idx = 0;
1009 st->pending_cnt = 0;
1010 st->sent_cb_cnt = 0;
1011 st->sent = 0;
1012 st->recv = 0;
1013 st->done = PJ_FALSE;
1014 st->err = PJ_SUCCESS;
1015 pj_ssl_sock_set_user_data(newsock, st);
1016
1017 /* Track accepted socket for cleanup */
1018 parent_st->accepted_ssock = newsock;
1019
1020 read_buf[0] = st->read_buf;
1021 status = pj_ssl_sock_start_read2(newsock, st->pool,
1022 sizeof(st->read_buf),
1023 (void **)read_buf, 0);
1024 if (status != PJ_SUCCESS) {
1025 st->err = status;
1026 return PJ_FALSE;
1027 }
1028
1029 /* Server also blast-sends its own data */
1030 return bidir_blast_sends(newsock, st);
1031 }
1032
1033 static pj_bool_t bidir_on_data_read(pj_ssl_sock_t *ssock,
1034 void *data,
1035 pj_size_t size,
1036 pj_status_t status,
1037 pj_size_t *remainder)
1038 {
1039 struct bidir_state *st = (struct bidir_state *)
1040 pj_ssl_sock_get_user_data(ssock);
1041
1042 PJ_UNUSED_ARG(data);
1043
1044 if (remainder)
1045 *remainder = 0;
1046
1047 if (size > 0) {
1048 st->recv += size;
1049 if (st->recv >= st->expected_recv) {
1050 st->recv_done = PJ_TRUE;
1051 /* If all pending sends have completed too, we're fully done */
1052 if (st->pending_cnt == st->sent_cb_cnt)
1053 st->done = PJ_TRUE;
1054 }
1055 }
1056
1057 if (status != PJ_SUCCESS) {
1058 if (status == PJ_EEOF)
1059 st->done = PJ_TRUE;
1060 else
1061 st->err = status;
1062 }
1063
1064 if (st->err != PJ_SUCCESS || st->done)
1065 return PJ_FALSE;
1066
1067 return PJ_TRUE;
1068 }
1069
1070 static pj_bool_t bidir_on_data_sent(pj_ssl_sock_t *ssock,
1071 pj_ioqueue_op_key_t *op_key,
1072 pj_ssize_t sent)
1073 {
1074 struct bidir_state *st = (struct bidir_state *)
1075 pj_ssl_sock_get_user_data(ssock);
1076 PJ_UNUSED_ARG(op_key);
1077
1078 if (sent < 0) {
1079 st->err = (pj_status_t)-sent;
1080 return PJ_FALSE;
1081 }
1082
1083 st->sent += sent;
1084 st->sent_cb_cnt++;
1085
1086 /* Check if all pending sends completed while recv was already done */
1087 if (st->recv_done && st->pending_cnt == st->sent_cb_cnt)
1088 st->done = PJ_TRUE;
1089
1090 return PJ_TRUE;
1091 }
1092
1093 static int bidir_test_inner(int renego_at)
1094 {
1095 pj_pool_t *pool = NULL;
1096 pj_ioqueue_t *ioqueue = NULL;
1097 pj_timer_heap_t *timer = NULL;
1098 pj_ssl_sock_t *ssock_serv = NULL;
1099 pj_ssl_sock_t *ssock_cli = NULL;
1100 pj_ssl_sock_param param;
1101 struct bidir_state state_serv;
1102 struct bidir_state state_cli;
1103 pj_sockaddr addr, listen_addr;
1104
1105 pj_status_t status;
1106 int i;
1107
1108 pool = pj_pool_create(mem, "ssl_bidir", 8192, 4096, NULL);
1109
1110 pj_bzero(&state_serv, sizeof(state_serv));
1111 pj_bzero(&state_cli, sizeof(state_cli));
1112
1113 status = pj_ioqueue_create(pool, 4, &ioqueue);
1114 if (status != PJ_SUCCESS) {
1115 app_perror("...bidir_test: ioqueue create", status);
1116 goto on_return;
1117 }
1118
1119 status = pj_timer_heap_create(pool, 4, &timer);
1120 if (status != PJ_SUCCESS) {
1121 app_perror("...bidir_test: timer create", status);
1122 goto on_return;
1123 }
1124
1125 pj_ssl_sock_param_default(&param);
1126 param.cb.on_accept_complete2 = &bidir_on_accept_complete;
1127 param.cb.on_connect_complete = &bidir_on_connect_complete;
1128 param.cb.on_data_read = &bidir_on_data_read;
1129 param.cb.on_data_sent = &bidir_on_data_sent;
1130 param.ioqueue = ioqueue;
1131 param.timer_heap = timer;
1132 param.proto = PJ_SSL_SOCK_PROTO_TLS1_2;
1133 param.ciphers_num = 0;
1134
1135 {
1136 pj_str_t tmp_st;
1137 pj_sockaddr_init(PJ_AF_INET, &addr,
1138 pj_strset2(&tmp_st, "127.0.0.1"), 0);
1139 }
1140
1141 /* Fill send data with unique patterns per side */
1142 for (i = 0; i < BIDIR_PKT_LEN; i++) {
1143 state_serv.send_data[i] = (char)(0xAA ^ (i & 0xFF));
1144 state_cli.send_data[i] = (char)(0xBB ^ (i & 0xFF));
1145 }
1146
1147 /* SERVER */
1148 state_serv.pool = pool;
1149 state_serv.is_server = PJ_TRUE;
1150 state_serv.expected_recv = BIDIR_SEND_COUNT * BIDIR_PKT_LEN;
1151 param.user_data = &state_serv;
1152 param.require_client_cert = PJ_FALSE;
1153
1154 listen_addr = addr;
1155 status = ssl_test_create_server(pool, &param, "bidir_test",
1156 &ssock_serv, &listen_addr);
1157 if (status != PJ_SUCCESS)
1158 goto on_return;
1159
1160 /* CLIENT */
1161 state_cli.pool = pool;
1162 state_cli.is_server = PJ_FALSE;
1163 state_cli.expected_recv = BIDIR_SEND_COUNT * BIDIR_PKT_LEN;
1164 state_cli.renego_at = renego_at;
1165 param.user_data = &state_cli;
1166
1167 status = pj_ssl_sock_create(pool, &param, &ssock_cli);
1168 if (status != PJ_SUCCESS) {
1169 app_perror("...bidir_test: client create", status);
1170 goto on_return;
1171 }
1172
1173 status = pj_ssl_sock_start_connect(ssock_cli, pool, &addr,
1174 &listen_addr,
1175 pj_sockaddr_get_len(&addr));
1176 if (status == PJ_SUCCESS) {
1177 bidir_on_connect_complete(ssock_cli, PJ_SUCCESS);
1178 } else if (status != PJ_EPENDING) {
1179 app_perror("...bidir_test: connect", status);
1180 goto on_return;
1181 }
1182
1183 /* Poll until client done */
1184 {
1185 pj_timestamp t_start, t_now;
1186 pj_uint32_t elapsed;
1187
1188 pj_get_timestamp(&t_start);
1189 while (!state_cli.err && !state_cli.done) {
1190 pj_time_val delay = {0, 100};
1191 pj_ioqueue_poll(ioqueue, &delay);
1192 pj_timer_heap_poll(timer, NULL);
1193
1194 pj_get_timestamp(&t_now);
1195 elapsed = pj_elapsed_msec(&t_start, &t_now);
1196 if (elapsed > 30000) {
1197 PJ_LOG(1, ("", "...bidir_test TIMEOUT after 30s"));
1198 status = PJ_ETIMEDOUT;
1199 goto on_return;
1200 }
1201 }
1202 }
1203
1204 if (state_cli.err) {
1205 status = state_cli.err;
1206 app_perror("...bidir_test client error", status);
1207 goto on_return;
1208 }
1209
1210 PJ_LOG(3, ("", "...bidir_test: cli sent=%lu recv=%lu "
1211 "pending=%d sent_cb=%d",
1212 (unsigned long)state_cli.sent,
1213 (unsigned long)state_cli.recv,
1214 state_cli.pending_cnt,
1215 state_cli.sent_cb_cnt));
1216
1217 if (state_cli.recv < state_cli.expected_recv) {
1218 PJ_LOG(1, ("", "...bidir_test: recv=%lu < expected=%lu",
1219 (unsigned long)state_cli.recv,
1220 (unsigned long)state_cli.expected_recv));
1221 status = PJ_EINVAL;
1222 goto on_return;
1223 }
1224
1225 if (state_cli.pending_cnt != state_cli.sent_cb_cnt) {
1226 PJ_LOG(1, ("", "...bidir_test: cli pending=%d != sent_cb=%d",
1227 state_cli.pending_cnt, state_cli.sent_cb_cnt));
1228 status = PJ_EBUG;
1229 goto on_return;
1230 }
1231
1232 status = PJ_SUCCESS;
1233
1234 on_return:
1235 if (ssock_cli)
1236 pj_ssl_sock_close(ssock_cli);
1237 if (state_serv.accepted_ssock)
1238 pj_ssl_sock_close(state_serv.accepted_ssock);
1239 if (ssock_serv)
1240 pj_ssl_sock_close(ssock_serv);
1241
1242 /* Poll to drain pending events after close */
1243 if (ioqueue) {
1244 pj_time_val delay = {0, 500};
1245 int n = 50;
1246 while (n-- > 0 && pj_ioqueue_poll(ioqueue, &delay) > 0)
1247 ;
1248 }
1249
1250 if (timer)
1251 pj_timer_heap_destroy(timer);
1252 if (ioqueue)
1253 pj_ioqueue_destroy(ioqueue);
1254 if (pool)
1255 pj_pool_release(pool);
1256
1257 return (status == PJ_SUCCESS) ? 0 : -1;
1258 }
1259
1260
1261 /*
1262 * Multi-threaded send load test: multiple clients with worker threads.
1263 * This is the closest simulation to production (multiple SIP registrations
1264 * over TLS with worker threads polling ioqueue).
1265 */
1266 #if PJ_HAS_THREADS
1267
1268 #define MT_WORKER_THREADS 3
1269 #define MT_CLIENTS 3
1270 #define MT_SEND_COUNT 100
1271 #define MT_SEND_PKT_LEN 512
1272
1273 struct mt_state
1274 {
1275 pj_pool_t *pool;
1276 pj_ssl_sock_t **accepted_arr;
1277 int accepted_cnt;
1278 pj_bool_t is_server;
1279 pj_bool_t echo;
1280 pj_status_t err;
1281 pj_size_t sent;
1282 pj_size_t recv;
1283 pj_uint8_t read_buf[8192];
1284 pj_bool_t done;
1285 int pending_cnt;
1286 int sent_cb_cnt;
1287 int send_idx;
1288 pj_ioqueue_op_key_t op_keys[MT_SEND_COUNT];
1289 char send_data[MT_SEND_PKT_LEN];
1290 };
1291
1292 struct mt_test_ctx
1293 {
1294 pj_ioqueue_t *ioqueue;
1295 pj_timer_heap_t *timer;
1296 pj_bool_t quit_flag;
1297 pj_atomic_t *clients_done;
1298 };
1299
1300 static pj_bool_t mt_on_connect_complete(pj_ssl_sock_t *ssock,
1301 pj_status_t status)
1302 {
1303 struct mt_state *st = (struct mt_state *)
1304 pj_ssl_sock_get_user_data(ssock);
1305 void *read_buf[1];
1306 pj_ssize_t len;
1307 int i;
1308
1309 if (status != PJ_SUCCESS) {
1310 st->err = status;
1311 return PJ_FALSE;
1312 }
1313
1314 read_buf[0] = st->read_buf;
1315 status = pj_ssl_sock_start_read2(ssock, st->pool,
1316 sizeof(st->read_buf),
1317 (void **)read_buf, 0);
1318 if (status != PJ_SUCCESS) {
1319 st->err = status;
1320 return PJ_FALSE;
1321 }
1322
1323 for (i = 0; i < MT_SEND_COUNT; i++) {
1324 len = MT_SEND_PKT_LEN;
1325 status = pj_ssl_sock_send(ssock, &st->op_keys[i],
1326 st->send_data, &len, 0);
1327 if (status == PJ_EPENDING) {
1328 st->pending_cnt++;
1329 } else if (status == PJ_SUCCESS) {
1330 st->sent += len;
1331 } else {
1332 st->err = status;
1333 return PJ_FALSE;
1334 }
1335 st->send_idx++;
1336 }
1337
1338 return PJ_TRUE;
1339 }
1340
1341 static pj_bool_t mt_on_accept_complete(pj_ssl_sock_t *ssock,
1342 pj_ssl_sock_t *newsock,
1343 const pj_sockaddr_t *src_addr,
1344 int src_addr_len,
1345 pj_status_t accept_status)
1346 {
1347 struct mt_state *parent_st = (struct mt_state *)
1348 pj_ssl_sock_get_user_data(ssock);
1349 struct mt_state *st;
1350 void *read_buf[1];
1351 pj_status_t status;
1352
1353 PJ_UNUSED_ARG(src_addr);
1354 PJ_UNUSED_ARG(src_addr_len);
1355
1356 if (accept_status != PJ_SUCCESS)
1357 return PJ_FALSE;
1358
1359 st = (struct mt_state *)pj_pool_zalloc(parent_st->pool,
1360 sizeof(struct mt_state));
1361 *st = *parent_st;
1362 st->sent = 0;
1363 st->recv = 0;
1364 st->done = PJ_FALSE;
1365 st->err = PJ_SUCCESS;
1366 pj_ssl_sock_set_user_data(newsock, st);
1367
1368 /* Track accepted socket for cleanup */
1369 if (parent_st->accepted_arr &&
1370 parent_st->accepted_cnt < MT_CLIENTS)
1371 {
1372 parent_st->accepted_arr[parent_st->accepted_cnt++] = newsock;
1373 }
1374
1375 read_buf[0] = st->read_buf;
1376 status = pj_ssl_sock_start_read2(newsock, st->pool,
1377 sizeof(st->read_buf),
1378 (void **)read_buf, 0);
1379 if (status != PJ_SUCCESS) {
1380 st->err = status;
1381 return PJ_FALSE;
1382 }
1383
1384 return PJ_TRUE;
1385 }
1386
1387 static pj_bool_t mt_on_data_read(pj_ssl_sock_t *ssock,
1388 void *data,
1389 pj_size_t size,
1390 pj_status_t status,
1391 pj_size_t *remainder)
1392 {
1393 struct mt_state *st = (struct mt_state *)
1394 pj_ssl_sock_get_user_data(ssock);
1395
1396 if (remainder)
1397 *remainder = 0;
1398
1399 if (size > 0) {
1400 st->recv += size;
1401
1402 /* Server echoes data back */
1403 if (st->echo) {
1404 pj_ssize_t sz = (pj_ssize_t)size;
1405 pj_status_t s;
1406
1407 s = pj_ssl_sock_send(ssock, &st->op_keys[0], data, &sz, 0);
1408 if (s != PJ_SUCCESS && s != PJ_EPENDING)
1409 st->err = s;
1410 }
1411
1412 /* Client: check completion */
1413 if (!st->is_server) {
1414 pj_size_t expected = (pj_size_t)MT_SEND_COUNT * MT_SEND_PKT_LEN;
1415 if (st->recv >= expected)
1416 st->done = PJ_TRUE;
1417 }
1418 }
1419
1420 if (status != PJ_SUCCESS) {
1421 if (status == PJ_EEOF)
1422 st->done = PJ_TRUE;
1423 else
1424 st->err = status;
1425 }
1426
1427 if (st->err != PJ_SUCCESS || st->done)
1428 return PJ_FALSE;
1429
1430 return PJ_TRUE;
1431 }
1432
1433 static pj_bool_t mt_on_data_sent(pj_ssl_sock_t *ssock,
1434 pj_ioqueue_op_key_t *op_key,
1435 pj_ssize_t sent)
1436 {
1437 struct mt_state *st = (struct mt_state *)
1438 pj_ssl_sock_get_user_data(ssock);
1439 PJ_UNUSED_ARG(op_key);
1440
1441 if (sent < 0) {
1442 st->err = (pj_status_t)-sent;
1443 return PJ_FALSE;
1444 }
1445
1446 st->sent += sent;
1447 st->sent_cb_cnt++;
1448 return PJ_TRUE;
1449 }
1450
1451 static int mt_worker_proc(void *arg)
1452 {
1453 struct mt_test_ctx *ctx = (struct mt_test_ctx *)arg;
1454
1455 while (!ctx->quit_flag) {
1456 pj_time_val delay = {0, 20};
1457 pj_ioqueue_poll(ctx->ioqueue, &delay);
1458 pj_timer_heap_poll(ctx->timer, NULL);
1459 }
1460
1461 return 0;
1462 }
1463
1464 static int mt_send_load_test(void)
1465 {
1466 pj_pool_t *pool = NULL;
1467 pj_ioqueue_t *ioqueue = NULL;
1468 pj_timer_heap_t *timer = NULL;
1469 pj_ssl_sock_t *ssock_serv = NULL;
1470 pj_ssl_sock_t *ssock_cli[MT_CLIENTS];
1471 pj_ssl_sock_t *ssock_accepted[MT_CLIENTS];
1472 pj_thread_t *threads[MT_WORKER_THREADS];
1473 pj_ssl_sock_param param;
1474 struct mt_state state_serv;
1475 struct mt_state state_cli[MT_CLIENTS];
1476 struct mt_test_ctx ctx;
1477 pj_sockaddr addr, listen_addr;
1478
1479 pj_status_t status;
1480 int i;
1481
1482 pj_bzero(ssock_cli, sizeof(ssock_cli));
1483 pj_bzero(ssock_accepted, sizeof(ssock_accepted));
1484 pj_bzero(threads, sizeof(threads));
1485 pj_bzero(&ctx, sizeof(ctx));
1486
1487 pool = pj_pool_create(mem, "ssl_mt", 32000, 4096, NULL);
1488
1489 pj_bzero(&state_serv, sizeof(state_serv));
1490 pj_bzero(state_cli, sizeof(state_cli));
1491
1492 status = pj_ioqueue_create(pool, 4 + MT_CLIENTS * 2, &ioqueue);
1493 if (status != PJ_SUCCESS) {
1494 app_perror("...mt_send_load_test: ioqueue create", status);
1495 goto on_return;
1496 }
1497
1498 status = pj_timer_heap_create(pool, 4 + MT_CLIENTS * 2, &timer);
1499 if (status != PJ_SUCCESS) {
1500 app_perror("...mt_send_load_test: timer create", status);
1501 goto on_return;
1502 }
1503
1504 ctx.ioqueue = ioqueue;
1505 ctx.timer = timer;
1506 ctx.quit_flag = PJ_FALSE;
1507
1508 pj_ssl_sock_param_default(&param);
1509 param.cb.on_accept_complete2 = &mt_on_accept_complete;
1510 param.cb.on_connect_complete = &mt_on_connect_complete;
1511 param.cb.on_data_read = &mt_on_data_read;
1512 param.cb.on_data_sent = &mt_on_data_sent;
1513 param.ioqueue = ioqueue;
1514 param.timer_heap = timer;
1515 param.proto = PJ_SSL_SOCK_PROTO_TLS1_2;
1516 param.ciphers_num = 0;
1517
1518 {
1519 pj_str_t tmp_st;
1520 pj_sockaddr_init(PJ_AF_INET, &addr,
1521 pj_strset2(&tmp_st, "127.0.0.1"), 0);
1522 }
1523
1524 /* SERVER */
1525 state_serv.pool = pool;
1526 state_serv.echo = PJ_TRUE;
1527 state_serv.is_server = PJ_TRUE;
1528 state_serv.accepted_arr = ssock_accepted;
1529 state_serv.accepted_cnt = 0;
1530 param.user_data = &state_serv;
1531 param.require_client_cert = PJ_FALSE;
1532
1533 listen_addr = addr;
1534 status = ssl_test_create_server(pool, &param, "mt_send_load_test",
1535 &ssock_serv, &listen_addr);
1536 if (status != PJ_SUCCESS)
1537 goto on_return;
1538
1539 /* Start worker threads */
1540 for (i = 0; i < MT_WORKER_THREADS; i++) {
1541 status = pj_thread_create(pool, "ssl_mt_w", &mt_worker_proc,
1542 &ctx, 0, 0, &threads[i]);
1543 if (status != PJ_SUCCESS) {
1544 app_perror("...mt_send_load_test: thread create", status);
1545 goto on_return;
1546 }
1547 }
1548
1549 /* Create and connect clients */
1550 for (i = 0; i < MT_CLIENTS; i++) {
1551 int k;
1552
1553 state_cli[i].pool = pool;
1554 state_cli[i].is_server = PJ_FALSE;
1555 for (k = 0; k < MT_SEND_PKT_LEN; k++)
1556 state_cli[i].send_data[k] = (char)((i + k) & 0xFF);
1557
1558 param.user_data = &state_cli[i];
1559
1560 status = pj_ssl_sock_create(pool, &param, &ssock_cli[i]);
1561 if (status != PJ_SUCCESS) {
1562 app_perror("...mt_send_load_test: client create", status);
1563 goto on_return;
1564 }
1565
1566 status = pj_ssl_sock_start_connect(ssock_cli[i], pool, &addr,
1567 &listen_addr,
1568 pj_sockaddr_get_len(&addr));
1569 if (status == PJ_SUCCESS) {
1570 mt_on_connect_complete(ssock_cli[i], PJ_SUCCESS);
1571 } else if (status != PJ_EPENDING) {
1572 app_perror("...mt_send_load_test: connect", status);
1573 goto on_return;
1574 }
1575 }
1576
1577 /* Main thread also polls */
1578 {
1579 pj_timestamp t_start, t_now;
1580 pj_uint32_t elapsed;
1581 int all_done;
1582
1583 pj_get_timestamp(&t_start);
1584 for (;;) {
1585 pj_time_val delay = {0, 100};
1586 pj_ioqueue_poll(ioqueue, &delay);
1587 pj_timer_heap_poll(timer, NULL);
1588
1589 all_done = PJ_TRUE;
1590 for (i = 0; i < MT_CLIENTS; i++) {
1591 if (!state_cli[i].done && !state_cli[i].err) {
1592 all_done = PJ_FALSE;
1593 break;
1594 }
1595 }
1596 if (all_done)
1597 break;
1598
1599 pj_get_timestamp(&t_now);
1600 elapsed = pj_elapsed_msec(&t_start, &t_now);
1601 if (elapsed > 60000) {
1602 PJ_LOG(1, ("", "...mt_send_load_test TIMEOUT after 60s"));
1603 status = PJ_ETIMEDOUT;
1604 goto on_return;
1605 }
1606 }
1607 }
1608
1609 /* Stop workers */
1610 ctx.quit_flag = PJ_TRUE;
1611 for (i = 0; i < MT_WORKER_THREADS; i++) {
1612 if (threads[i]) {
1613 pj_thread_join(threads[i]);
1614 pj_thread_destroy(threads[i]);
1615 threads[i] = NULL;
1616 }
1617 }
1618
1619 /* Verify */
1620 for (i = 0; i < MT_CLIENTS; i++) {
1621 pj_size_t expected = (pj_size_t)MT_SEND_COUNT * MT_SEND_PKT_LEN;
1622
1623 if (state_cli[i].err) {
1624 PJ_LOG(1, ("", "...mt_send_load_test: client %d error", i));
1625 status = state_cli[i].err;
1626 app_perror("...mt_send_load_test: client error", status);
1627 goto on_return;
1628 }
1629
1630 PJ_LOG(3, ("", "...mt_send_load_test: cli[%d] sent=%lu recv=%lu "
1631 "pending=%d sent_cb=%d",
1632 i,
1633 (unsigned long)state_cli[i].sent,
1634 (unsigned long)state_cli[i].recv,
1635 state_cli[i].pending_cnt,
1636 state_cli[i].sent_cb_cnt));
1637
1638 if (state_cli[i].pending_cnt != state_cli[i].sent_cb_cnt) {
1639 PJ_LOG(1, ("", "...mt_send_load_test: cli[%d] pending=%d "
1640 "!= sent_cb=%d", i,
1641 state_cli[i].pending_cnt,
1642 state_cli[i].sent_cb_cnt));
1643 status = PJ_EBUG;
1644 goto on_return;
1645 }
1646
1647 if (state_cli[i].sent < expected) {
1648 PJ_LOG(1, ("", "...mt_send_load_test: cli[%d] sent=%lu "
1649 "< expected=%lu", i,
1650 (unsigned long)state_cli[i].sent,
1651 (unsigned long)expected));
1652 status = PJ_EBUG;
1653 goto on_return;
1654 }
1655 }
1656
1657 status = PJ_SUCCESS;
1658
1659 on_return:
1660 ctx.quit_flag = PJ_TRUE;
1661 for (i = 0; i < MT_WORKER_THREADS; i++) {
1662 if (threads[i]) {
1663 pj_thread_join(threads[i]);
1664 pj_thread_destroy(threads[i]);
1665 }
1666 }
1667
1668 for (i = 0; i < MT_CLIENTS; i++) {
1669 if (ssock_cli[i])
1670 pj_ssl_sock_close(ssock_cli[i]);
1671 }
1672 for (i = 0; i < state_serv.accepted_cnt; i++) {
1673 if (ssock_accepted[i])
1674 pj_ssl_sock_close(ssock_accepted[i]);
1675 }
1676 if (ssock_serv)
1677 pj_ssl_sock_close(ssock_serv);
1678
1679 /* Poll to drain pending events after close */
1680 if (ioqueue) {
1681 pj_time_val delay = {0, 500};
1682 int n = 50;
1683 while (n-- > 0 && pj_ioqueue_poll(ioqueue, &delay) > 0)
1684 ;
1685 }
1686
1687 if (timer)
1688 pj_timer_heap_destroy(timer);
1689 if (ioqueue)
1690 pj_ioqueue_destroy(ioqueue);
1691 if (pool)
1692 pj_pool_release(pool);
1693
1694 return (status == PJ_SUCCESS) ? 0 : -1;
1695 }
1696
1697 /*
1698 * Concurrent send test: multiple threads blast-send on the SAME SSL socket.
1699 * This simulates production SIP where worker threads share a TLS transport.
1700 * Detects out-of-order TLS records (causes connection reset) and data
1701 * corruption from interleaved ssl_write_buf access.
1702 */
1703 #define CS_SENDER_THREADS 3
1704 #define CS_SENDS_PER_THREAD 30
1705 #define CS_PKT_LEN 512
1706
1707 struct cs_sender_arg
1708 {
1709 pj_ssl_sock_t *ssock;
1710 pj_ioqueue_op_key_t op_keys[CS_SENDS_PER_THREAD];
1711 char send_data[CS_PKT_LEN];
1712 int send_cnt;
1713 int pending_cnt;
1714 int sent_cb_cnt;
1715 pj_size_t sent;
1716 pj_status_t err;
1717 pj_bool_t done;
1718 };
1719
1720 struct cs_state
1721 {
1722 pj_pool_t *pool;
1723 pj_ssl_sock_t *accepted_ssock;
1724 pj_bool_t is_server;
1725 pj_bool_t echo;
1726 pj_status_t err;
1727 pj_size_t sent;
1728 pj_size_t recv;
1729 pj_uint8_t read_buf[8192];
1730 pj_bool_t done;
1731 int pending_cnt;
1732 int sent_cb_cnt;
1733 int send_idx;
1734 int renego_at;
1735 pj_bool_t renego_done;
1736 pj_ioqueue_op_key_t send_key;
1737 char send_data[CS_PKT_LEN];
1738 pj_size_t expected_recv;
1739 };
1740
1741 static pj_bool_t cs_on_connect_complete(pj_ssl_sock_t *ssock,
1742 pj_status_t status)
1743 {
1744 struct cs_state *st = (struct cs_state *)
1745 pj_ssl_sock_get_user_data(ssock);
1746 void *read_buf[1];
1747
1748 if (status != PJ_SUCCESS) {
1749 st->err = status;
1750 return PJ_FALSE;
1751 }
1752
1753 read_buf[0] = st->read_buf;
1754 status = pj_ssl_sock_start_read2(ssock, st->pool,
1755 sizeof(st->read_buf),
1756 (void **)read_buf, 0);
1757 if (status != PJ_SUCCESS) {
1758 st->err = status;
1759 return PJ_FALSE;
1760 }
1761
1762 /* Sends will happen from worker threads, not here */
1763 st->done = PJ_FALSE;
1764 return PJ_TRUE;
1765 }
1766
1767 static pj_bool_t cs_on_accept_complete(pj_ssl_sock_t *ssock,
1768 pj_ssl_sock_t *newsock,
1769 const pj_sockaddr_t *src_addr,
1770 int src_addr_len,
1771 pj_status_t accept_status)
1772 {
1773 struct cs_state *parent_st = (struct cs_state *)
1774 pj_ssl_sock_get_user_data(ssock);
1775 struct cs_state *st;
1776 void *read_buf[1];
1777 pj_status_t status;
1778
1779 PJ_UNUSED_ARG(src_addr);
1780 PJ_UNUSED_ARG(src_addr_len);
1781
1782 if (accept_status != PJ_SUCCESS)
1783 return PJ_FALSE;
1784
1785 st = (struct cs_state *)pj_pool_zalloc(parent_st->pool,
1786 sizeof(struct cs_state));
1787 *st = *parent_st;
1788 pj_ssl_sock_set_user_data(newsock, st);
1789 parent_st->accepted_ssock = newsock;
1790
1791 read_buf[0] = st->read_buf;
1792 status = pj_ssl_sock_start_read2(newsock, st->pool,
1793 sizeof(st->read_buf),
1794 (void **)read_buf, 0);
1795 if (status != PJ_SUCCESS) {
1796 st->err = status;
1797 return PJ_FALSE;
1798 }
1799
1800 return PJ_TRUE;
1801 }
1802
1803 static pj_bool_t cs_on_data_read(pj_ssl_sock_t *ssock,
1804 void *data,
1805 pj_size_t size,
1806 pj_status_t status,
1807 pj_size_t *remainder)
1808 {
1809 struct cs_state *st = (struct cs_state *)
1810 pj_ssl_sock_get_user_data(ssock);
1811
1812 if (remainder)
1813 *remainder = 0;
1814
1815 if (size > 0) {
1816 st->recv += size;
1817
1818 /* Server echoes data back */
1819 if (st->echo) {
1820 pj_ssize_t sz = (pj_ssize_t)size;
1821 pj_status_t s;
1822
1823 s = pj_ssl_sock_send(ssock, &st->send_key, data, &sz, 0);
1824 if (s != PJ_SUCCESS && s != PJ_EPENDING && s != PJ_EBUSY)
1825 st->err = s;
1826 }
1827
1828 /* Client: check completion */
1829 if (!st->is_server && st->recv >= st->expected_recv)
1830 st->done = PJ_TRUE;
1831 }
1832
1833 if (status != PJ_SUCCESS) {
1834 if (status == PJ_EEOF)
1835 st->done = PJ_TRUE;
1836 else
1837 st->err = status;
1838 }
1839
1840 if (st->err != PJ_SUCCESS || st->done)
1841 return PJ_FALSE;
1842
1843 return PJ_TRUE;
1844 }
1845
1846 static pj_bool_t cs_on_data_sent(pj_ssl_sock_t *ssock,
1847 pj_ioqueue_op_key_t *op_key,
1848 pj_ssize_t sent)
1849 {
1850 struct cs_state *st = (struct cs_state *)
1851 pj_ssl_sock_get_user_data(ssock);
1852 PJ_UNUSED_ARG(op_key);
1853
1854 if (sent < 0) {
1855 st->err = (pj_status_t)-sent;
1856 return PJ_FALSE;
1857 }
1858
1859 st->sent += sent;
1860 st->sent_cb_cnt++;
1861 return PJ_TRUE;
1862 }
1863
1864 static int cs_sender_proc(void *arg)
1865 {
1866 struct cs_sender_arg *sa = (struct cs_sender_arg *)arg;
1867 int i;
1868
1869 for (i = 0; i < CS_SENDS_PER_THREAD; i++) {
1870 pj_ssize_t len = CS_PKT_LEN;
1871 pj_status_t status;
1872
1873 status = pj_ssl_sock_send(sa->ssock, &sa->op_keys[i],
1874 sa->send_data, &len, 0);
1875 if (status == PJ_EPENDING || status == PJ_EBUSY) {
1876 sa->pending_cnt++;
1877 } else if (status == PJ_SUCCESS) {
1878 sa->sent += len;
1879 } else {
1880 PJ_LOG(3, ("", "...cs_sender[%p] send %d err=%d",
1881 arg, i, status));
1882 sa->err = status;
1883 break;
1884 }
1885 sa->send_cnt++;
1886 }
1887 sa->done = PJ_TRUE;
1888 return 0;
1889 }
1890
1891 static int concurrent_send_test(void)
1892 {
1893 pj_pool_t *pool = NULL;
1894 pj_ioqueue_t *ioqueue = NULL;
1895 pj_timer_heap_t *timer = NULL;
1896 pj_ssl_sock_t *ssock_serv = NULL;
1897 pj_ssl_sock_t *ssock_cli = NULL;
1898 pj_ssl_sock_param param;
1899 struct cs_state state_serv;
1900 struct cs_state state_cli;
1901 struct cs_sender_arg senders[CS_SENDER_THREADS];
1902 pj_thread_t *sender_threads[CS_SENDER_THREADS];
1903 pj_thread_t *poll_thread = NULL;
1904 struct mt_test_ctx poll_ctx;
1905 pj_sockaddr addr, listen_addr;
1906
1907 pj_status_t status;
1908 pj_size_t total_expected;
1909 int i;
1910
1911 pool = pj_pool_create(mem, "ssl_cs", 32000, 4096, NULL);
1912
1913 pj_bzero(&state_serv, sizeof(state_serv));
1914 pj_bzero(&state_cli, sizeof(state_cli));
1915 pj_bzero(senders, sizeof(senders));
1916 pj_bzero(sender_threads, sizeof(sender_threads));
1917 pj_bzero(&poll_ctx, sizeof(poll_ctx));
1918
1919 status = pj_ioqueue_create(pool, 4, &ioqueue);
1920 if (status != PJ_SUCCESS) {
1921 app_perror("...concurrent_send_test: ioqueue create", status);
1922 goto on_return;
1923 }
1924
1925 status = pj_timer_heap_create(pool, 4, &timer);
1926 if (status != PJ_SUCCESS) {
1927 app_perror("...concurrent_send_test: timer create", status);
1928 goto on_return;
1929 }
1930
1931 pj_ssl_sock_param_default(&param);
1932 param.cb.on_accept_complete2 = &cs_on_accept_complete;
1933 param.cb.on_connect_complete = &cs_on_connect_complete;
1934 param.cb.on_data_read = &cs_on_data_read;
1935 param.cb.on_data_sent = &cs_on_data_sent;
1936 param.ioqueue = ioqueue;
1937 param.timer_heap = timer;
1938 param.proto = PJ_SSL_SOCK_PROTO_TLS1_2;
1939 param.ciphers_num = 0;
1940
1941 {
1942 pj_str_t tmp_st;
1943 pj_sockaddr_init(PJ_AF_INET, &addr,
1944 pj_strset2(&tmp_st, "127.0.0.1"), 0);
1945 }
1946
1947 total_expected = (pj_size_t)CS_SENDER_THREADS * CS_SENDS_PER_THREAD *
1948 CS_PKT_LEN;
1949
1950 /* SERVER */
1951 state_serv.pool = pool;
1952 state_serv.echo = PJ_TRUE;
1953 state_serv.is_server = PJ_TRUE;
1954 param.user_data = &state_serv;
1955 param.require_client_cert = PJ_FALSE;
1956
1957 listen_addr = addr;
1958 status = ssl_test_create_server(pool, &param, "concurrent_send_test",
1959 &ssock_serv, &listen_addr);
1960 if (status != PJ_SUCCESS)
1961 goto on_return;
1962
1963 /* CLIENT */
1964 state_cli.pool = pool;
1965 state_cli.is_server = PJ_FALSE;
1966 state_cli.expected_recv = total_expected;
1967 param.user_data = &state_cli;
1968
1969 status = pj_ssl_sock_create(pool, &param, &ssock_cli);
1970 if (status != PJ_SUCCESS) {
1971 app_perror("...concurrent_send_test: client create", status);
1972 goto on_return;
1973 }
1974
1975 status = pj_ssl_sock_start_connect(ssock_cli, pool, &addr,
1976 &listen_addr,
1977 pj_sockaddr_get_len(&addr));
1978 if (status == PJ_SUCCESS) {
1979 cs_on_connect_complete(ssock_cli, PJ_SUCCESS);
1980 } else if (status != PJ_EPENDING) {
1981 app_perror("...concurrent_send_test: connect", status);
1982 goto on_return;
1983 }
1984
1985 /* Poll until handshake completes (connect callback fires) */
1986 {
1987 pj_ssl_sock_info info;
1988 pj_timestamp t_start, t_now;
1989
1990 pj_get_timestamp(&t_start);
1991 for (;;) {
1992 pj_time_val delay = {0, 100};
1993 pj_ioqueue_poll(ioqueue, &delay);
1994 pj_timer_heap_poll(timer, NULL);
1995
1996 if (state_cli.err) {
1997 status = state_cli.err;
1998 app_perror("...concurrent_send_test: connect error",
1999 status);
2000 goto on_return;
2001 }
2002
2003 status = pj_ssl_sock_get_info(ssock_cli, &info);
2004 if (status == PJ_SUCCESS && info.established)
2005 break;
2006
2007 pj_get_timestamp(&t_now);
2008 if (pj_elapsed_msec(&t_start, &t_now) > 10000) {
2009 PJ_LOG(1, ("", "...concurrent_send_test: "
2010 "connect TIMEOUT"));
2011 status = PJ_ETIMEDOUT;
2012 goto on_return;
2013 }
2014 }
2015 }
2016
2017 /* Start poll thread */
2018 poll_ctx.ioqueue = ioqueue;
2019 poll_ctx.timer = timer;
2020 poll_ctx.quit_flag = PJ_FALSE;
2021 status = pj_thread_create(pool, "cs_poll", &mt_worker_proc,
2022 &poll_ctx, 0, 0, &poll_thread);
2023 if (status != PJ_SUCCESS) {
2024 app_perror("...concurrent_send_test: poll thread create", status);
2025 goto on_return;
2026 }
2027
2028 /* Launch sender threads — all send on the SAME ssock_cli */
2029 for (i = 0; i < CS_SENDER_THREADS; i++) {
2030 int k;
2031
2032 senders[i].ssock = ssock_cli;
2033 for (k = 0; k < CS_PKT_LEN; k++)
2034 senders[i].send_data[k] = (char)((i + k) & 0xFF);
2035
2036 status = pj_thread_create(pool, "cs_send", &cs_sender_proc,
2037 &senders[i], 0, 0, &sender_threads[i]);
2038 if (status != PJ_SUCCESS) {
2039 app_perror("...concurrent_send_test: sender create", status);
2040 goto on_return;
2041 }
2042 }
2043
2044 /* Wait for all senders to finish */
2045 for (i = 0; i < CS_SENDER_THREADS; i++) {
2046 if (sender_threads[i]) {
2047 pj_thread_join(sender_threads[i]);
2048 pj_thread_destroy(sender_threads[i]);
2049 sender_threads[i] = NULL;
2050 }
2051 }
2052
2053 /* Poll until client receives all echoed data or error */
2054 {
2055 pj_timestamp t_start, t_now;
2056
2057 pj_get_timestamp(&t_start);
2058 while (!state_cli.err && !state_cli.done) {
2059 pj_time_val delay = {0, 100};
2060
2061 /* Main thread also polls */
2062 pj_ioqueue_poll(ioqueue, &delay);
2063 pj_timer_heap_poll(timer, NULL);
2064
2065 pj_get_timestamp(&t_now);
2066 if (pj_elapsed_msec(&t_start, &t_now) > 30000) {
2067 PJ_LOG(1, ("", "...concurrent_send_test TIMEOUT "
2068 "(recv=%lu expected=%lu err=%d)",
2069 (unsigned long)state_cli.recv,
2070 (unsigned long)total_expected,
2071 (int)state_cli.err));
2072 status = PJ_ETIMEDOUT;
2073 goto on_return;
2074 }
2075 }
2076 }
2077
2078 if (state_cli.err) {
2079 PJ_LOG(1, ("", "...concurrent_send_test: connection error=%d "
2080 "(recv=%lu/%lu) — likely out-of-order TLS records",
2081 (int)state_cli.err,
2082 (unsigned long)state_cli.recv,
2083 (unsigned long)total_expected));
2084 status = state_cli.err;
2085 goto on_return;
2086 }
2087
2088 /* Verify */
2089 {
2090 int total_sends = 0;
2091 int total_pending = 0;
2092 pj_size_t total_sent = 0;
2093
2094 for (i = 0; i < CS_SENDER_THREADS; i++) {
2095 total_sends += senders[i].send_cnt;
2096 total_pending += senders[i].pending_cnt;
2097 total_sent += senders[i].sent;
2098 if (senders[i].err) {
2099 PJ_LOG(1, ("", "...sender[%d] error=%d after %d sends",
2100 i, senders[i].err, senders[i].send_cnt));
2101 status = senders[i].err;
2102 goto on_return;
2103 }
2104 }
2105
2106 PJ_LOG(3, ("", "...concurrent_send_test: threads=%d "
2107 "total_sends=%d recv=%lu/%lu",
2108 CS_SENDER_THREADS, total_sends,
2109 (unsigned long)state_cli.recv,
2110 (unsigned long)total_expected));
2111 }
2112
2113 status = PJ_SUCCESS;
2114
2115 on_return:
2116 poll_ctx.quit_flag = PJ_TRUE;
2117 if (poll_thread) {
2118 pj_thread_join(poll_thread);
2119 pj_thread_destroy(poll_thread);
2120 }
2121 for (i = 0; i < CS_SENDER_THREADS; i++) {
2122 if (sender_threads[i]) {
2123 pj_thread_join(sender_threads[i]);
2124 pj_thread_destroy(sender_threads[i]);
2125 }
2126 }
2127
2128 if (ssock_cli)
2129 pj_ssl_sock_close(ssock_cli);
2130 if (state_serv.accepted_ssock)
2131 pj_ssl_sock_close(state_serv.accepted_ssock);
2132 if (ssock_serv)
2133 pj_ssl_sock_close(ssock_serv);
2134
2135 if (ioqueue) {
2136 pj_time_val delay = {0, 500};
2137 int n = 50;
2138 while (n-- > 0 && pj_ioqueue_poll(ioqueue, &delay) > 0)
2139 ;
2140 }
2141
2142 if (timer)
2143 pj_timer_heap_destroy(timer);
2144 if (ioqueue)
2145 pj_ioqueue_destroy(ioqueue);
2146 if (pool)
2147 pj_pool_release(pool);
2148
2149 return (status == PJ_SUCCESS) ? 0 : -1;
2150 }
2151
2152 #endif /* PJ_HAS_THREADS */
2153
2154
2155 static int send_load_test(void) { return send_load_test_inner(0, PJ_FALSE); }
2156 static int bidir_test(void) { return bidir_test_inner(0); }
2157
2158 #define RUN_SUBTEST(name, call) \
2159 do { \
2160 PJ_LOG(3,("", ".." name)); \
2161 ret = call; \
2162 if (ret != 0) { \
2163 PJ_LOG(1,("", "FAILED: " name " (rc=%d)", ret)); \
2164 return ret; \
2165 } \
2166 } while (0)
2167
2168 int ssl_sock_stress_test(void)
2169 {
2170 int ret;
2171
2172 RUN_SUBTEST("send load test", send_load_test());
2173 RUN_SUBTEST("close under pending sends test", close_pending_test());
2174 RUN_SUBTEST("bidirectional simultaneous load test", bidir_test());
2175
2176 #if PJ_HAS_THREADS
2177 RUN_SUBTEST("multi-threaded send load test", mt_send_load_test());
2178 RUN_SUBTEST("concurrent send (same socket) test",
2179 concurrent_send_test());
2180 #endif
2181
2182 #if HAS_CLIENT_RENEGO_TEST
2183 RUN_SUBTEST("send load + client renegotiation test",
2184 send_load_test_inner(SEND_LOAD_COUNT / 2, PJ_FALSE));
2185 RUN_SUBTEST("bidirectional + renegotiation test",
2186 bidir_test_inner(BIDIR_SEND_COUNT / 2));
2187 RUN_SUBTEST("destroy during renegotiation test",
2188 destroy_during_renego_test());
2189 #endif
2190
2191 #if HAS_RENEGO_TEST
2192 RUN_SUBTEST("send load + server renegotiation test",
2193 send_load_test_inner(SEND_LOAD_COUNT / 2, PJ_TRUE));
2194 #endif
2195
2196 return 0;
2197 }
2198
2199 #else /* INCLUDE_SSLSOCK_TEST */
2200 int dummy_ssl_sock_stress_test;
2201 #endif /* INCLUDE_SSLSOCK_TEST */