2 * Copyright (c) 2021 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <hs_apps/vcl/vcl_test.h>
19 vt_tcp_connect (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
24 ts->fd = vppcom_session_create (VPPCOM_PROTO_TCP, ts->noblk_connect);
27 vterr ("vppcom_session_create()", ts->fd);
31 rv = vppcom_session_connect (ts->fd, endpt);
32 if (rv < 0 && rv != VPPCOM_EINPROGRESS)
34 vterr ("vppcom_session_connect()", rv);
38 ts->read = vcl_test_read;
39 ts->write = vcl_test_write;
41 if (!ts->noblk_connect)
44 flen = sizeof (flags);
45 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
46 vtinf ("Test session %d (fd %d) connected.", ts->session_index, ts->fd);
53 vt_tcp_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
57 ts->fd = vppcom_session_create (VPPCOM_PROTO_TCP, 1 /* is_nonblocking */);
60 vterr ("vppcom_session_create()", ts->fd);
64 rv = vppcom_session_bind (ts->fd, endpt);
67 vterr ("vppcom_session_bind()", rv);
71 rv = vppcom_session_listen (ts->fd, 10);
74 vterr ("vppcom_session_listen()", rv);
82 vt_tcp_accept (int listen_fd, vcl_test_session_t *ts)
86 client_fd = vppcom_session_accept (listen_fd, &ts->endpt, 0);
89 vterr ("vppcom_session_accept()", client_fd);
94 ts->read = vcl_test_read;
95 ts->write = vcl_test_write;
100 static const vcl_test_proto_vft_t vcl_test_tcp = {
101 .open = vt_tcp_connect,
102 .listen = vt_tcp_listen,
103 .accept = vt_tcp_accept,
106 VCL_TEST_REGISTER_PROTO (VPPCOM_PROTO_TCP, vcl_test_tcp);
109 vt_udp_connect (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
111 uint32_t flags, flen;
114 ts->fd = vppcom_session_create (VPPCOM_PROTO_UDP, ts->noblk_connect);
117 vterr ("vppcom_session_create()", ts->fd);
121 rv = vppcom_session_connect (ts->fd, endpt);
122 if (rv < 0 && rv != VPPCOM_EINPROGRESS)
124 vterr ("vppcom_session_connect()", rv);
128 ts->read = vcl_test_read;
129 ts->write = vcl_test_write;
131 if (!ts->noblk_connect)
134 flen = sizeof (flags);
135 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
136 vtinf ("Test session %d (fd %d) connected.", ts->session_index, ts->fd);
143 vt_udp_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
147 ts->fd = vppcom_session_create (VPPCOM_PROTO_UDP, 1 /* is_nonblocking */);
150 vterr ("vppcom_session_create()", ts->fd);
154 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_CONNECTED, 0, 0);
156 /* Listen is implicit */
157 rv = vppcom_session_bind (ts->fd, endpt);
160 vterr ("vppcom_session_bind()", rv);
168 vt_udp_accept (int listen_fd, vcl_test_session_t *ts)
172 client_fd = vppcom_session_accept (listen_fd, &ts->endpt, 0);
175 vterr ("vppcom_session_accept()", client_fd);
180 ts->read = vcl_test_read;
181 ts->write = vcl_test_write;
186 static const vcl_test_proto_vft_t vcl_test_udp = {
187 .open = vt_udp_connect,
188 .listen = vt_udp_listen,
189 .accept = vt_udp_accept,
192 VCL_TEST_REGISTER_PROTO (VPPCOM_PROTO_UDP, vcl_test_udp);
195 * TLS server cert and keys to be used for testing only
197 static char vcl_test_crt_rsa[] =
198 "-----BEGIN CERTIFICATE-----\r\n"
199 "MIID5zCCAs+gAwIBAgIJALeMYCEHrTtJMA0GCSqGSIb3DQEBCwUAMIGJMQswCQYD\r\n"
200 "VQQGEwJVUzELMAkGA1UECAwCQ0ExETAPBgNVBAcMCFN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\n"
215 "3xbPFk02FaGz/WupPu276zGlzJAZrbuDcQowwwU1Ni1Yygxl96s1c2M5rHDTrOKG\r\n"
216 "rK0hbkSFBo+i6I8u4HiiQ4rYmG0Hv6+sXn3of0HsbtDPGgWZoipPWDljPYEURu3e\r\n"
217 "3HRe/Dtsj9CakBoSDzs8ndWaBR+f4sM9Tk1cjD46Gq2T/qpSPXqKxEUXlzhdCAn4\r\n"
218 "twub17Bq2kykHpppCwPg5M+v30tHG/R2Go15MeFWbEJthFk3TZMjKL7UFs7fH+x2\r\n"
219 "wSonXb++jY+KmCb93C+soABBizE57g/KmiR2IxQ/LMjDik01RSUIaM0lLA==\r\n"
220 "-----END CERTIFICATE-----\r\n";
221 static uint32_t vcl_test_crt_rsa_len = sizeof (vcl_test_crt_rsa);
223 static char vcl_test_key_rsa[] =
224 "-----BEGIN PRIVATE KEY-----\r\n"
225 "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDgLWTxrUO5K2CC\r\n"
226 "CpPijT18/2wnZ0LnhvGFL9eTZUR9ohnbbZZKjhf+zJFV9XL1hqFykjXMn9EPFplI\r\n"
227 "WGAD6c9kGsUyytQuSt0A6k5WPx4MSn0m5Z07Y4NKgWcK8vFxhYFhzmQevImkRoYm\r\n"
228 "XWwVBJ6yfMwW3L/n21rSy2TQWnyvk2EiNa9Ki8bM+joGHP5VLZx5shmHjHW3aEdo\r\n"
229 "VyCbSomgnoNx5F5YJbzD7I5Wi9OiUYar5ulvYefsydFJnYrY10v5otemRltL8A4M\r\n"
230 "/UUXa9M7WZgvb3Qs7v/nW8mY9c0pINgzLkS8guvn9Wq2OwULBEtDiNwz/4Z4dIAV\r\n"
231 "bHioi/QHAgMBAAECggEBAMzGipP8+oT166U+NlJXRFifFVN1DvdhG9PWnOxGL+c3\r\n"
232 "ILmBBC08WQzmHshPemBvR6DZkA1H23cV5JTiLWrFtC00CvhXsLRMrE5+uWotI6yE\r\n"
233 "iofybMroHvD6/X5R510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\r\n"
248 "PBkZX17kNWv+iVM1WyFjbe++vfKZCrOJiyiqhDeEqgrP3AuNMlaaduC3VRC3G5oV\r\n"
249 "Mj1tlhDWQ/qhvKdCKNdIVQYDE75nw+FRWV8yYkHAnXYW3tNoweDIwixE0hkPR1bc\r\n"
250 "oEjPLVNtx8SOj/M4rhaPT3I=\r\n"
251 "-----END PRIVATE KEY-----\r\n";
252 static uint32_t vcl_test_key_rsa_len = sizeof (vcl_test_key_rsa);
255 vt_add_cert_key_pair ()
257 vcl_test_main_t *vt = &vcl_test_main;
258 vppcom_cert_key_pair_t ckpair;
261 vtinf ("Adding tls certs ...");
263 ckpair.cert = vcl_test_crt_rsa;
264 ckpair.key = vcl_test_key_rsa;
265 ckpair.cert_len = vcl_test_crt_rsa_len;
266 ckpair.key_len = vcl_test_key_rsa_len;
267 ckp_index = vppcom_add_cert_key_pair (&ckpair);
270 vterr ("vppcom_add_cert_key_pair()", ckp_index);
274 vt->ckpair_index = ckp_index;
279 vt_tls_init (vcl_test_cfg_t *cfg)
281 return vt_add_cert_key_pair ();
285 vt_tls_connect (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
287 vcl_test_main_t *vt = &vcl_test_main;
288 uint32_t flags, flen, ckp_len;
291 ts->fd = vppcom_session_create (VPPCOM_PROTO_TLS, ts->noblk_connect);
294 vterr ("vppcom_session_create()", ts->fd);
298 ckp_len = sizeof (vt->ckpair_index);
299 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_CKPAIR, &vt->ckpair_index,
302 rv = vppcom_session_connect (ts->fd, endpt);
303 if (rv < 0 && rv != VPPCOM_EINPROGRESS)
305 vterr ("vppcom_session_connect()", rv);
309 ts->read = vcl_test_read;
310 ts->write = vcl_test_write;
312 if (!ts->noblk_connect)
315 flen = sizeof (flags);
316 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
317 vtinf ("Test session %d (fd %d) connected.", ts->session_index, ts->fd);
324 vt_tls_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
326 vcl_test_main_t *vt = &vcl_test_main;
330 ts->fd = vppcom_session_create (VPPCOM_PROTO_TLS, 1 /* is_nonblocking */);
333 vterr ("vppcom_session_create()", ts->fd);
337 ckp_len = sizeof (vt->ckpair_index);
338 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_CKPAIR, &vt->ckpair_index,
341 rv = vppcom_session_bind (ts->fd, endpt);
344 vterr ("vppcom_session_bind()", rv);
348 rv = vppcom_session_listen (ts->fd, 10);
351 vterr ("vppcom_session_listen()", rv);
359 vt_tls_accept (int listen_fd, vcl_test_session_t *ts)
363 client_fd = vppcom_session_accept (listen_fd, &ts->endpt, 0);
366 vterr ("vppcom_session_accept()", client_fd);
371 ts->read = vcl_test_read;
372 ts->write = vcl_test_write;
377 static const vcl_test_proto_vft_t vcl_test_tls = {
379 .open = vt_tls_connect,
380 .listen = vt_tls_listen,
381 .accept = vt_tls_accept,
384 VCL_TEST_REGISTER_PROTO (VPPCOM_PROTO_TLS, vcl_test_tls);
387 vt_dtls_init (vcl_test_cfg_t *cfg)
389 return vt_add_cert_key_pair ();
393 vt_dtls_connect (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
395 vcl_test_main_t *vt = &vcl_test_main;
396 uint32_t flags, flen, ckp_len;
399 ts->fd = vppcom_session_create (VPPCOM_PROTO_DTLS, ts->noblk_connect);
402 vterr ("vppcom_session_create()", ts->fd);
406 ckp_len = sizeof (vt->ckpair_index);
407 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_CKPAIR, &vt->ckpair_index,
410 rv = vppcom_session_connect (ts->fd, endpt);
411 if (rv < 0 && rv != VPPCOM_EINPROGRESS)
413 vterr ("vppcom_session_connect()", rv);
417 ts->read = vcl_test_read;
418 ts->write = vcl_test_write;
420 if (!ts->noblk_connect)
423 flen = sizeof (flags);
424 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
425 vtinf ("Test session %d (fd %d) connected.", ts->session_index, ts->fd);
432 vt_dtls_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
434 vcl_test_main_t *vt = &vcl_test_main;
438 ts->fd = vppcom_session_create (VPPCOM_PROTO_DTLS, 1 /* is_nonblocking */);
441 vterr ("vppcom_session_create()", ts->fd);
445 ckp_len = sizeof (vt->ckpair_index);
446 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_CKPAIR, &vt->ckpair_index,
449 rv = vppcom_session_bind (ts->fd, endpt);
452 vterr ("vppcom_session_bind()", rv);
456 rv = vppcom_session_listen (ts->fd, 10);
459 vterr ("vppcom_session_listen()", rv);
467 vt_dtls_accept (int listen_fd, vcl_test_session_t *ts)
471 client_fd = vppcom_session_accept (listen_fd, &ts->endpt, 0);
474 vterr ("vppcom_session_accept()", client_fd);
479 ts->read = vcl_test_read;
480 ts->write = vcl_test_write;
485 static const vcl_test_proto_vft_t vcl_test_dtls = {
486 .init = vt_dtls_init,
487 .open = vt_dtls_connect,
488 .listen = vt_dtls_listen,
489 .accept = vt_dtls_accept,
492 VCL_TEST_REGISTER_PROTO (VPPCOM_PROTO_DTLS, vcl_test_dtls);
495 vt_quic_init (vcl_test_cfg_t *cfg)
497 vcl_test_main_t *vt = &vcl_test_main;
502 return vt_add_cert_key_pair ();
506 vt_quic_maybe_init_wrk (vcl_test_main_t *vt, vcl_test_wrk_t *wrk,
507 vppcom_endpt_t *endpt)
509 uint32_t size, i, flags, flen, ckp_len;
510 vcl_test_session_t *tq;
513 /* Test already initialized */
514 if (wrk->n_qsessions == vt->cfg.num_test_qsessions)
517 /* Make sure pool is large enough */
521 calloc (vt->cfg.num_test_qsessions, sizeof (vcl_test_session_t));
525 size = vt->cfg.num_test_qsessions * sizeof (vcl_test_session_t);
526 wrk->qsessions = realloc (wrk->qsessions, size);
531 vterr ("failed to alloc Qsessions", -errno);
535 for (i = 0; i < vt->cfg.num_test_qsessions; i++)
537 tq = &wrk->qsessions[i];
539 vppcom_session_create (VPPCOM_PROTO_QUIC, 0 /* is_nonblocking */);
540 tq->session_index = i;
543 vterr ("vppcom_session_create()", tq->fd);
547 ckp_len = sizeof (vt->ckpair_index);
548 vppcom_session_attr (tq->fd, VPPCOM_ATTR_SET_CKPAIR, &vt->ckpair_index,
551 /* Connect is blocking */
552 rv = vppcom_session_connect (tq->fd, endpt);
555 vterr ("vppcom_session_connect()", rv);
559 flen = sizeof (flags);
560 vppcom_session_attr (tq->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
561 vtinf ("Test Qsession %d (fd %d) connected.", i, tq->fd);
563 wrk->n_qsessions = vt->cfg.num_test_qsessions;
569 vt_quic_connect (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
571 vcl_test_main_t *vt = &vcl_test_main;
572 uint32_t wrk_index, flags, flen;
573 vcl_test_session_t *tq;
577 wrk_index = vcl_test_worker_index ();
578 wrk = &vt->wrk[wrk_index];
580 /* Make sure qsessions are initialized */
581 vt_quic_maybe_init_wrk (vt, wrk, endpt);
583 ts->fd = vppcom_session_create (VPPCOM_PROTO_QUIC, ts->noblk_connect);
586 vterr ("vppcom_session_create()", ts->fd);
590 /* Choose qession to use for stream */
591 tq = &wrk->qsessions[ts->session_index / vt->cfg.num_test_sessions_perq];
593 rv = vppcom_session_stream_connect (ts->fd, tq->fd);
594 if (rv < 0 && rv != VPPCOM_EINPROGRESS)
596 vterr ("vppcom_session_stream_connect()", rv);
600 ts->read = vcl_test_read;
601 ts->write = vcl_test_write;
603 if (!ts->noblk_connect)
606 flen = sizeof (flags);
607 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
608 vtinf ("Test (quic stream) session %d (fd %d) connected.",
609 ts->session_index, ts->fd);
616 vt_quic_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
618 vcl_test_main_t *vt = &vcl_test_main;
622 ts->fd = vppcom_session_create (VPPCOM_PROTO_QUIC, 1 /* is_nonblocking */);
625 vterr ("vppcom_session_create()", ts->fd);
629 ckp_len = sizeof (vt->ckpair_index);
630 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_CKPAIR, &vt->ckpair_index,
633 rv = vppcom_session_bind (ts->fd, endpt);
636 vterr ("vppcom_session_bind()", rv);
640 rv = vppcom_session_listen (ts->fd, 10);
643 vterr ("vppcom_session_listen()", rv);
651 vt_quic_accept (int listen_fd, vcl_test_session_t *ts)
655 client_fd = vppcom_session_accept (listen_fd, &ts->endpt, 0);
658 vterr ("vppcom_session_accept()", client_fd);
663 ts->read = vcl_test_read;
664 ts->write = vcl_test_write;
670 vt_quic_close (vcl_test_session_t *ts)
672 int listener_fd = vppcom_session_listener (ts->fd);
674 if ((vppcom_session_n_accepted (listener_fd) == 0) &
675 vppcom_session_is_connectable_listener (listener_fd))
677 vtinf ("Connected Listener fd %x has no more sessions", listener_fd);
678 vppcom_session_close (listener_fd);
684 static const vcl_test_proto_vft_t vcl_test_quic = {
685 .init = vt_quic_init,
686 .open = vt_quic_connect,
687 .listen = vt_quic_listen,
688 .accept = vt_quic_accept,
689 .close = vt_quic_close,
692 VCL_TEST_REGISTER_PROTO (VPPCOM_PROTO_QUIC, vcl_test_quic);
694 static unsigned char test_key[46] = {
695 0xe1, 0xf9, 0x7a, 0x0d, 0x3e, 0x01, 0x8b, 0xe0, 0xd6, 0x4f, 0xa3, 0x2c,
696 0x06, 0xde, 0x41, 0x39, 0x0e, 0xc6, 0x75, 0xad, 0x49, 0x8a, 0xfe, 0xeb,
697 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6, 0xc1, 0x73, 0xc3, 0x17, 0xf2, 0xda,
698 0xbe, 0x35, 0x77, 0x93, 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6
703 unsigned char cc : 4;
706 unsigned char version : 2;
707 unsigned char pt : 7;
720 typedef struct transport_endpt_cfg_srtp_policy
724 uint32_t window_size;
725 uint8_t allow_repeat_tx;
728 } transport_endpt_cfg_srtp_policy_t;
730 typedef struct transport_endpt_cfg_srtp
732 transport_endpt_cfg_srtp_policy_t policy[2];
733 } transport_endpt_cfg_srtp_t;
736 vt_session_add_srtp_policy (vcl_test_session_t *ts, int is_connect)
738 transport_endpt_cfg_srtp_t *srtp_cfg;
739 transport_endpt_cfg_srtp_policy_t *test_policy;
740 uint32_t rx_ssrc, tx_ssrc;
743 rx_ssrc = is_connect ? 0xcafebeef : 0xbeefcafe;
744 tx_ssrc = is_connect ? 0xbeefcafe : 0xcafebeef;
746 cfg_size = sizeof (transport_endpt_cfg_srtp_t);
747 srtp_cfg = malloc (cfg_size);
748 memset (srtp_cfg, 0, cfg_size);
750 test_policy = &srtp_cfg->policy[0];
751 test_policy->ssrc_type = 1 /* ssrc_specific */;
752 test_policy->ssrc_value = rx_ssrc;
753 memcpy (test_policy->key, test_key, sizeof (test_key));
754 test_policy->key_len = sizeof (test_key);
755 test_policy->window_size = 128;
756 test_policy->allow_repeat_tx = 1;
758 test_policy = &srtp_cfg->policy[1];
759 test_policy->ssrc_type = 1 /* ssrc_specific */;
760 test_policy->ssrc_value = tx_ssrc;
761 memcpy (test_policy->key, test_key, sizeof (test_key));
762 test_policy->key_len = sizeof (test_key);
763 test_policy->window_size = 128;
764 test_policy->allow_repeat_tx = 1;
766 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_ENDPT_EXT_CFG, srtp_cfg,
772 vt_srtp_session_init (vcl_test_session_t *ts, int is_connect)
774 uint32_t rx_ssrc, tx_ssrc;
775 rtp_headers_t *rtp_hdrs;
778 rx_ssrc = is_connect ? 0xcafebeef : 0xbeefcafe;
779 tx_ssrc = is_connect ? 0xbeefcafe : 0xcafebeef;
781 rtp_hdrs = malloc (sizeof (rtp_headers_t));
782 memset (rtp_hdrs, 0, sizeof (*rtp_hdrs));
783 ts->opaque = rtp_hdrs;
785 hdr = &rtp_hdrs->rx_hdr;
794 hdr->ssrc = htonl (rx_ssrc);
796 hdr = &rtp_hdrs->tx_hdr;
805 hdr->ssrc = htonl (tx_ssrc);
809 vt_srtp_write (vcl_test_session_t *ts, void *buf, uint32_t nbytes)
811 int tx_bytes = 0, nbytes_left = nbytes, rv;
812 vcl_test_stats_t *stats = &ts->stats;
815 hdr = &((rtp_headers_t *) ts->opaque)->tx_hdr;
816 hdr->seq = htons (ntohs (hdr->seq) + 1);
817 hdr->ts = htonl (ntohl (hdr->ts) + 1);
819 memcpy (buf, hdr, sizeof (*hdr));
824 rv = vppcom_session_write (ts->fd, buf, nbytes_left);
827 if ((rv == VPPCOM_EAGAIN || rv == VPPCOM_EWOULDBLOCK))
832 nbytes_left = nbytes_left - rv;
836 while (tx_bytes != nbytes);
841 stats->tx_bytes += tx_bytes;
847 vt_srtp_read (vcl_test_session_t *ts, void *buf, uint32_t nbytes)
849 vcl_test_stats_t *stats = &ts->stats;
854 rx_bytes = vppcom_session_read (ts->fd, buf, nbytes);
858 if (rx_bytes == VPPCOM_EAGAIN || rx_bytes == VPPCOM_EWOULDBLOCK)
864 if (rx_bytes < nbytes)
867 stats->rx_bytes += rx_bytes;
869 hdr = &((rtp_headers_t *) ts->opaque)->rx_hdr;
870 if (((rtp_hdr_t *) buf)->ssrc != hdr->ssrc)
871 hdr->ssrc = ((rtp_hdr_t *) buf)->ssrc;
876 vt_srtp_connect (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
878 uint32_t flags, flen;
881 ts->fd = vppcom_session_create (VPPCOM_PROTO_SRTP, ts->noblk_connect);
884 vterr ("vppcom_session_create()", ts->fd);
888 vt_session_add_srtp_policy (ts, 1 /* is connect */);
890 rv = vppcom_session_connect (ts->fd, endpt);
891 if (rv < 0 && rv != VPPCOM_EINPROGRESS)
893 vterr ("vppcom_session_connect()", rv);
897 ts->read = vt_srtp_read;
898 ts->write = vt_srtp_write;
900 if (!ts->noblk_connect)
903 flen = sizeof (flags);
904 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
905 vtinf ("Test session %d (fd %d) connected.", ts->session_index, ts->fd);
908 vt_srtp_session_init (ts, 1 /* is connect */);
914 vt_srtp_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
918 ts->fd = vppcom_session_create (VPPCOM_PROTO_SRTP, 1 /* is_nonblocking */);
921 vterr ("vppcom_session_create()", ts->fd);
925 vt_session_add_srtp_policy (ts, 0 /* is connect */);
927 rv = vppcom_session_bind (ts->fd, endpt);
930 vterr ("vppcom_session_bind()", rv);
934 rv = vppcom_session_listen (ts->fd, 10);
937 vterr ("vppcom_session_listen()", rv);
945 vt_srtp_accept (int listen_fd, vcl_test_session_t *ts)
949 client_fd = vppcom_session_accept (listen_fd, &ts->endpt, 0);
952 vterr ("vppcom_session_accept()", client_fd);
957 ts->read = vt_srtp_read;
958 ts->write = vt_srtp_write;
960 vt_srtp_session_init (ts, 0 /* is connect */);
966 vt_srtp_close (vcl_test_session_t *ts)
972 static const vcl_test_proto_vft_t vcl_test_srtp = {
973 .open = vt_srtp_connect,
974 .listen = vt_srtp_listen,
975 .accept = vt_srtp_accept,
976 .close = vt_srtp_close,
979 VCL_TEST_REGISTER_PROTO (VPPCOM_PROTO_SRTP, vcl_test_srtp);
982 * fd.io coding-style-patch-verification: ON
985 * eval: (c-set-style "gnu")