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, 0 /* is_nonblocking */);
27 vterr ("vppcom_session_create()", ts->fd);
31 /* Connect is blocking */
32 rv = vppcom_session_connect (ts->fd, endpt);
35 vterr ("vppcom_session_connect()", rv);
39 ts->read = vcl_test_read;
40 ts->write = vcl_test_write;
42 flen = sizeof (flags);
43 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
44 vtinf ("Test session %d (fd %d) connected.", ts->session_index, ts->fd);
50 vt_tcp_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
54 ts->fd = vppcom_session_create (VPPCOM_PROTO_TCP, 1 /* is_nonblocking */);
57 vterr ("vppcom_session_create()", ts->fd);
61 rv = vppcom_session_bind (ts->fd, endpt);
64 vterr ("vppcom_session_bind()", rv);
68 rv = vppcom_session_listen (ts->fd, 10);
71 vterr ("vppcom_session_listen()", rv);
79 vt_tcp_accept (int listen_fd, vcl_test_session_t *ts)
83 client_fd = vppcom_session_accept (listen_fd, &ts->endpt, 0);
86 vterr ("vppcom_session_accept()", client_fd);
91 ts->read = vcl_test_read;
92 ts->write = vcl_test_write;
97 static const vcl_test_proto_vft_t vcl_test_tcp = {
98 .open = vt_tcp_connect,
99 .listen = vt_tcp_listen,
100 .accept = vt_tcp_accept,
103 VCL_TEST_REGISTER_PROTO (VPPCOM_PROTO_TCP, vcl_test_tcp);
106 vt_udp_connect (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
108 uint32_t flags, flen;
111 ts->fd = vppcom_session_create (VPPCOM_PROTO_UDP, 0 /* is_nonblocking */);
114 vterr ("vppcom_session_create()", ts->fd);
118 /* Connect is blocking */
119 rv = vppcom_session_connect (ts->fd, endpt);
122 vterr ("vppcom_session_connect()", rv);
126 ts->read = vcl_test_read;
127 ts->write = vcl_test_write;
129 flen = sizeof (flags);
130 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
131 vtinf ("Test session %d (fd %d) connected.", ts->session_index, ts->fd);
137 vt_udp_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
141 ts->fd = vppcom_session_create (VPPCOM_PROTO_UDP, 1 /* is_nonblocking */);
144 vterr ("vppcom_session_create()", ts->fd);
148 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_CONNECTED, 0, 0);
150 /* Listen is implicit */
151 rv = vppcom_session_bind (ts->fd, endpt);
154 vterr ("vppcom_session_bind()", rv);
162 vt_udp_accept (int listen_fd, vcl_test_session_t *ts)
166 client_fd = vppcom_session_accept (listen_fd, &ts->endpt, 0);
169 vterr ("vppcom_session_accept()", client_fd);
174 ts->read = vcl_test_read;
175 ts->write = vcl_test_write;
180 static const vcl_test_proto_vft_t vcl_test_udp = {
181 .open = vt_udp_connect,
182 .listen = vt_udp_listen,
183 .accept = vt_udp_accept,
186 VCL_TEST_REGISTER_PROTO (VPPCOM_PROTO_UDP, vcl_test_udp);
189 * TLS server cert and keys to be used for testing only
191 static char vcl_test_crt_rsa[] =
192 "-----BEGIN CERTIFICATE-----\r\n"
193 "MIID5zCCAs+gAwIBAgIJALeMYCEHrTtJMA0GCSqGSIb3DQEBCwUAMIGJMQswCQYD\r\n"
194 "VQQGEwJVUzELMAkGA1UECAwCQ0ExETAPBgNVBAcMCFNhbiBKb3NlMQ4wDAYDVQQK\r\n"
195 "DAVDaXNjbzEOMAwGA1UECwwFZmQuaW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DTrOKG\r\n"
210 "rK0hbkSFBo+i6I8u4HiiQ4rYmG0Hv6+sXn3of0HsbtDPGgWZoipPWDljPYEURu3e\r\n"
211 "3HRe/Dtsj9CakBoSDzs8ndWaBR+f4sM9Tk1cjD46Gq2T/qpSPXqKxEUXlzhdCAn4\r\n"
212 "twub17Bq2kykHpppCwPg5M+v30tHG/R2Go15MeFWbEJthFk3TZMjKL7UFs7fH+x2\r\n"
213 "wSonXb++jY+KmCb93C+soABBizE57g/KmiR2IxQ/LMjDik01RSUIaM0lLA==\r\n"
214 "-----END CERTIFICATE-----\r\n";
215 static uint32_t vcl_test_crt_rsa_len = sizeof (vcl_test_crt_rsa);
217 static char vcl_test_key_rsa[] =
218 "-----BEGIN PRIVATE KEY-----\r\n"
219 "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDgLWTxrUO5K2CC\r\n"
220 "CpPijT18/2wnZ0LnhvGFL9eTZUR9ohnbbZZKjhf+zJFV9XL1hqFykjXMn9EPFplI\r\n"
221 "WGAD6c9kGsUyytQuSt0A6k5WPx4MSn0m5Z07Y4NKgWcK8vFxhYFhzmQevImkRoYm\r\n"
222 "XWwVBJ6yfMwW3L/n21rSy2TQWnyvk2EiNa9Ki8bM+joGHP5VLZx5shmHjHW3aEdo\r\n"
223 "VyCbSomgnoNx5F5YJbzD7I5Wi9OiUYar5ulvYefsydFJnYrY10v5otemRltL8A4M\r\n"
224 "/UUXa9M7WZgvb3Qs7v/nW8mY9c0pINgzLkS8guvn9Wq2OwULBEtDiNwz/4Z4dIAV\r\n"
225 "bHioi/QHAgMBAAECggEBAMzGipP8+oT166U+NlJXRFifFVN1DvdhG9PWnOxGL+c3\r\n"
226 "ILmBBC08WQzmHshPemBvR6DZkA1H23cV5JTiLWrFtC00CvhXsLRMrE5+uWotI6yE\r\n"
227 "iofybMroHvD6/X5R510UX9hQ6MHu5ShLR5VZ9zXHz5MpTmB/60jG5dLx+jgcwBK8\r\n"
228 "LuGv2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C3VRC3G5oV\r\n"
243 "Mj1tlhDWQ/qhvKdCKNdIVQYDE75nw+FRWV8yYkHAnXYW3tNoweDIwixE0hkPR1bc\r\n"
244 "oEjPLVNtx8SOj/M4rhaPT3I=\r\n"
245 "-----END PRIVATE KEY-----\r\n";
246 static uint32_t vcl_test_key_rsa_len = sizeof (vcl_test_key_rsa);
249 vt_add_cert_key_pair ()
251 vcl_test_main_t *vt = &vcl_test_main;
252 vppcom_cert_key_pair_t ckpair;
255 vtinf ("Adding tls certs ...");
257 ckpair.cert = vcl_test_crt_rsa;
258 ckpair.key = vcl_test_key_rsa;
259 ckpair.cert_len = vcl_test_crt_rsa_len;
260 ckpair.key_len = vcl_test_key_rsa_len;
261 ckp_index = vppcom_add_cert_key_pair (&ckpair);
264 vterr ("vppcom_add_cert_key_pair()", ckp_index);
268 vt->ckpair_index = ckp_index;
273 vt_tls_init (vcl_test_cfg_t *cfg)
275 return vt_add_cert_key_pair ();
279 vt_tls_connect (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
281 vcl_test_main_t *vt = &vcl_test_main;
282 uint32_t flags, flen, ckp_len;
285 ts->fd = vppcom_session_create (VPPCOM_PROTO_TLS, 0 /* is_nonblocking */);
288 vterr ("vppcom_session_create()", ts->fd);
292 ckp_len = sizeof (vt->ckpair_index);
293 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_CKPAIR, &vt->ckpair_index,
296 /* Connect is blocking */
297 rv = vppcom_session_connect (ts->fd, endpt);
300 vterr ("vppcom_session_connect()", rv);
304 ts->read = vcl_test_read;
305 ts->write = vcl_test_write;
307 flen = sizeof (flags);
308 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
309 vtinf ("Test session %d (fd %d) connected.", ts->session_index, ts->fd);
315 vt_tls_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
317 vcl_test_main_t *vt = &vcl_test_main;
321 ts->fd = vppcom_session_create (VPPCOM_PROTO_TLS, 1 /* is_nonblocking */);
324 vterr ("vppcom_session_create()", ts->fd);
328 ckp_len = sizeof (vt->ckpair_index);
329 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_CKPAIR, &vt->ckpair_index,
332 rv = vppcom_session_bind (ts->fd, endpt);
335 vterr ("vppcom_session_bind()", rv);
339 rv = vppcom_session_listen (ts->fd, 10);
342 vterr ("vppcom_session_listen()", rv);
350 vt_tls_accept (int listen_fd, vcl_test_session_t *ts)
354 client_fd = vppcom_session_accept (listen_fd, &ts->endpt, 0);
357 vterr ("vppcom_session_accept()", client_fd);
362 ts->read = vcl_test_read;
363 ts->write = vcl_test_write;
368 static const vcl_test_proto_vft_t vcl_test_tls = {
370 .open = vt_tls_connect,
371 .listen = vt_tls_listen,
372 .accept = vt_tls_accept,
375 VCL_TEST_REGISTER_PROTO (VPPCOM_PROTO_TLS, vcl_test_tls);
378 vt_dtls_init (vcl_test_cfg_t *cfg)
380 return vt_add_cert_key_pair ();
384 vt_dtls_connect (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
386 vcl_test_main_t *vt = &vcl_test_main;
387 uint32_t flags, flen, ckp_len;
390 ts->fd = vppcom_session_create (VPPCOM_PROTO_DTLS, 0 /* is_nonblocking */);
393 vterr ("vppcom_session_create()", ts->fd);
397 ckp_len = sizeof (vt->ckpair_index);
398 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_CKPAIR, &vt->ckpair_index,
401 /* Connect is blocking */
402 rv = vppcom_session_connect (ts->fd, endpt);
405 vterr ("vppcom_session_connect()", rv);
409 ts->read = vcl_test_read;
410 ts->write = vcl_test_write;
412 flen = sizeof (flags);
413 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
414 vtinf ("Test session %d (fd %d) connected.", ts->session_index, ts->fd);
420 vt_dtls_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
422 vcl_test_main_t *vt = &vcl_test_main;
426 ts->fd = vppcom_session_create (VPPCOM_PROTO_DTLS, 1 /* is_nonblocking */);
429 vterr ("vppcom_session_create()", ts->fd);
433 ckp_len = sizeof (vt->ckpair_index);
434 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_CKPAIR, &vt->ckpair_index,
437 rv = vppcom_session_bind (ts->fd, endpt);
440 vterr ("vppcom_session_bind()", rv);
444 rv = vppcom_session_listen (ts->fd, 10);
447 vterr ("vppcom_session_listen()", rv);
455 vt_dtls_accept (int listen_fd, vcl_test_session_t *ts)
459 client_fd = vppcom_session_accept (listen_fd, &ts->endpt, 0);
462 vterr ("vppcom_session_accept()", client_fd);
467 ts->read = vcl_test_read;
468 ts->write = vcl_test_write;
473 static const vcl_test_proto_vft_t vcl_test_dtls = {
474 .init = vt_dtls_init,
475 .open = vt_dtls_connect,
476 .listen = vt_dtls_listen,
477 .accept = vt_dtls_accept,
480 VCL_TEST_REGISTER_PROTO (VPPCOM_PROTO_DTLS, vcl_test_dtls);
483 vt_quic_init (vcl_test_cfg_t *cfg)
485 vcl_test_main_t *vt = &vcl_test_main;
490 return vt_add_cert_key_pair ();
494 vt_quic_maybe_init_wrk (vcl_test_main_t *vt, vcl_test_wrk_t *wrk,
495 vppcom_endpt_t *endpt)
497 uint32_t size, i, flags, flen, ckp_len;
498 vcl_test_session_t *tq;
501 /* Test already initialized */
502 if (wrk->n_qsessions == vt->cfg.num_test_qsessions)
505 /* Make sure pool is large enough */
509 calloc (vt->cfg.num_test_qsessions, sizeof (vcl_test_session_t));
513 size = vt->cfg.num_test_qsessions * sizeof (vcl_test_session_t);
514 wrk->qsessions = realloc (wrk->qsessions, size);
519 vterr ("failed to alloc Qsessions", -errno);
523 for (i = 0; i < vt->cfg.num_test_qsessions; i++)
525 tq = &wrk->qsessions[i];
527 vppcom_session_create (VPPCOM_PROTO_QUIC, 0 /* is_nonblocking */);
528 tq->session_index = i;
531 vterr ("vppcom_session_create()", tq->fd);
535 ckp_len = sizeof (vt->ckpair_index);
536 vppcom_session_attr (tq->fd, VPPCOM_ATTR_SET_CKPAIR, &vt->ckpair_index,
539 /* Connect is blocking */
540 rv = vppcom_session_connect (tq->fd, endpt);
543 vterr ("vppcom_session_connect()", rv);
547 flen = sizeof (flags);
548 vppcom_session_attr (tq->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
549 vtinf ("Test Qsession %d (fd %d) connected.", i, tq->fd);
551 wrk->n_qsessions = vt->cfg.num_test_qsessions;
557 vt_quic_connect (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
559 vcl_test_main_t *vt = &vcl_test_main;
560 uint32_t wrk_index, flags, flen;
561 vcl_test_session_t *tq;
565 wrk_index = vcl_test_worker_index ();
566 wrk = &vt->wrk[wrk_index];
568 /* Make sure qsessions are initialized */
569 vt_quic_maybe_init_wrk (vt, wrk, endpt);
571 ts->fd = vppcom_session_create (VPPCOM_PROTO_QUIC, 0 /* is_nonblocking */);
574 vterr ("vppcom_session_create()", ts->fd);
578 /* Choose qession to use for stream */
579 tq = &wrk->qsessions[ts->session_index / vt->cfg.num_test_sessions_perq];
581 rv = vppcom_session_stream_connect (ts->fd, tq->fd);
584 vterr ("vppcom_session_stream_connect()", rv);
589 flen = sizeof (flags);
590 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
592 ts->read = vcl_test_read;
593 ts->write = vcl_test_write;
595 vtinf ("Test (quic stream) session %d (fd %d) connected.", ts->session_index,
602 vt_quic_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
604 vcl_test_main_t *vt = &vcl_test_main;
608 ts->fd = vppcom_session_create (VPPCOM_PROTO_QUIC, 1 /* is_nonblocking */);
611 vterr ("vppcom_session_create()", ts->fd);
615 ckp_len = sizeof (vt->ckpair_index);
616 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_CKPAIR, &vt->ckpair_index,
619 rv = vppcom_session_bind (ts->fd, endpt);
622 vterr ("vppcom_session_bind()", rv);
626 rv = vppcom_session_listen (ts->fd, 10);
629 vterr ("vppcom_session_listen()", rv);
637 vt_quic_accept (int listen_fd, vcl_test_session_t *ts)
641 client_fd = vppcom_session_accept (listen_fd, &ts->endpt, 0);
644 vterr ("vppcom_session_accept()", client_fd);
649 ts->read = vcl_test_read;
650 ts->write = vcl_test_write;
656 vt_quic_close (vcl_test_session_t *ts)
658 int listener_fd = vppcom_session_listener (ts->fd);
660 if ((vppcom_session_n_accepted (listener_fd) == 0) &
661 vppcom_session_is_connectable_listener (listener_fd))
663 vtinf ("Connected Listener fd %x has no more sessions", listener_fd);
664 vppcom_session_close (listener_fd);
670 static const vcl_test_proto_vft_t vcl_test_quic = {
671 .init = vt_quic_init,
672 .open = vt_quic_connect,
673 .listen = vt_quic_listen,
674 .accept = vt_quic_accept,
675 .close = vt_quic_close,
678 VCL_TEST_REGISTER_PROTO (VPPCOM_PROTO_QUIC, vcl_test_quic);
680 static unsigned char test_key[46] = {
681 0xe1, 0xf9, 0x7a, 0x0d, 0x3e, 0x01, 0x8b, 0xe0, 0xd6, 0x4f, 0xa3, 0x2c,
682 0x06, 0xde, 0x41, 0x39, 0x0e, 0xc6, 0x75, 0xad, 0x49, 0x8a, 0xfe, 0xeb,
683 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6, 0xc1, 0x73, 0xc3, 0x17, 0xf2, 0xda,
684 0xbe, 0x35, 0x77, 0x93, 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6
689 unsigned char cc : 4;
692 unsigned char version : 2;
693 unsigned char pt : 7;
706 typedef struct transport_endpt_cfg_srtp_policy
710 uint32_t window_size;
711 uint8_t allow_repeat_tx;
714 } transport_endpt_cfg_srtp_policy_t;
716 typedef struct transport_endpt_cfg_srtp
718 transport_endpt_cfg_srtp_policy_t policy[2];
719 } transport_endpt_cfg_srtp_t;
722 vt_session_add_srtp_policy (vcl_test_session_t *ts, int is_connect)
724 transport_endpt_cfg_srtp_t *srtp_cfg;
725 transport_endpt_cfg_srtp_policy_t *test_policy;
726 uint32_t rx_ssrc, tx_ssrc;
729 rx_ssrc = is_connect ? 0xcafebeef : 0xbeefcafe;
730 tx_ssrc = is_connect ? 0xbeefcafe : 0xcafebeef;
732 cfg_size = sizeof (transport_endpt_cfg_srtp_t);
733 srtp_cfg = malloc (cfg_size);
734 memset (srtp_cfg, 0, cfg_size);
736 test_policy = &srtp_cfg->policy[0];
737 test_policy->ssrc_type = 1 /* ssrc_specific */;
738 test_policy->ssrc_value = rx_ssrc;
739 memcpy (test_policy->key, test_key, sizeof (test_key));
740 test_policy->key_len = sizeof (test_key);
741 test_policy->window_size = 128;
742 test_policy->allow_repeat_tx = 1;
744 test_policy = &srtp_cfg->policy[1];
745 test_policy->ssrc_type = 1 /* ssrc_specific */;
746 test_policy->ssrc_value = tx_ssrc;
747 memcpy (test_policy->key, test_key, sizeof (test_key));
748 test_policy->key_len = sizeof (test_key);
749 test_policy->window_size = 128;
750 test_policy->allow_repeat_tx = 1;
752 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_ENDPT_EXT_CFG, srtp_cfg,
758 vt_srtp_session_init (vcl_test_session_t *ts, int is_connect)
760 uint32_t rx_ssrc, tx_ssrc;
761 rtp_headers_t *rtp_hdrs;
764 rx_ssrc = is_connect ? 0xcafebeef : 0xbeefcafe;
765 tx_ssrc = is_connect ? 0xbeefcafe : 0xcafebeef;
767 rtp_hdrs = malloc (sizeof (rtp_headers_t));
768 memset (rtp_hdrs, 0, sizeof (*rtp_hdrs));
769 ts->opaque = rtp_hdrs;
771 hdr = &rtp_hdrs->rx_hdr;
780 hdr->ssrc = htonl (rx_ssrc);
782 hdr = &rtp_hdrs->tx_hdr;
791 hdr->ssrc = htonl (tx_ssrc);
795 vt_srtp_write (vcl_test_session_t *ts, void *buf, uint32_t nbytes)
797 int tx_bytes = 0, nbytes_left = nbytes, rv;
798 vcl_test_stats_t *stats = &ts->stats;
801 hdr = &((rtp_headers_t *) ts->opaque)->tx_hdr;
802 hdr->seq = htons (ntohs (hdr->seq) + 1);
803 hdr->ts = htonl (ntohl (hdr->ts) + 1);
805 memcpy (buf, hdr, sizeof (*hdr));
810 rv = vppcom_session_write (ts->fd, buf, nbytes_left);
813 if ((rv == VPPCOM_EAGAIN || rv == VPPCOM_EWOULDBLOCK))
818 nbytes_left = nbytes_left - rv;
822 while (tx_bytes != nbytes);
827 stats->tx_bytes += tx_bytes;
833 vt_srtp_read (vcl_test_session_t *ts, void *buf, uint32_t nbytes)
835 vcl_test_stats_t *stats = &ts->stats;
840 rx_bytes = vppcom_session_read (ts->fd, buf, nbytes);
844 if (rx_bytes == VPPCOM_EAGAIN || rx_bytes == VPPCOM_EWOULDBLOCK)
850 if (rx_bytes < nbytes)
853 stats->rx_bytes += rx_bytes;
855 hdr = &((rtp_headers_t *) ts->opaque)->rx_hdr;
856 if (((rtp_hdr_t *) buf)->ssrc != hdr->ssrc)
857 hdr->ssrc = ((rtp_hdr_t *) buf)->ssrc;
862 vt_srtp_connect (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
864 uint32_t flags, flen;
867 ts->fd = vppcom_session_create (VPPCOM_PROTO_SRTP, 0 /* is_nonblocking */);
870 vterr ("vppcom_session_create()", ts->fd);
874 vt_session_add_srtp_policy (ts, 1 /* is connect */);
876 /* Connect is blocking */
877 rv = vppcom_session_connect (ts->fd, endpt);
880 vterr ("vppcom_session_connect()", rv);
884 ts->read = vt_srtp_read;
885 ts->write = vt_srtp_write;
887 flen = sizeof (flags);
888 vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
889 vtinf ("Test session %d (fd %d) connected.", ts->session_index, ts->fd);
891 vt_srtp_session_init (ts, 1 /* is connect */);
897 vt_srtp_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
901 ts->fd = vppcom_session_create (VPPCOM_PROTO_SRTP, 1 /* is_nonblocking */);
904 vterr ("vppcom_session_create()", ts->fd);
908 vt_session_add_srtp_policy (ts, 0 /* is connect */);
910 rv = vppcom_session_bind (ts->fd, endpt);
913 vterr ("vppcom_session_bind()", rv);
917 rv = vppcom_session_listen (ts->fd, 10);
920 vterr ("vppcom_session_listen()", rv);
928 vt_srtp_accept (int listen_fd, vcl_test_session_t *ts)
932 client_fd = vppcom_session_accept (listen_fd, &ts->endpt, 0);
935 vterr ("vppcom_session_accept()", client_fd);
940 ts->read = vt_srtp_read;
941 ts->write = vt_srtp_write;
943 vt_srtp_session_init (ts, 0 /* is connect */);
949 vt_srtp_close (vcl_test_session_t *ts)
955 static const vcl_test_proto_vft_t vcl_test_srtp = {
956 .open = vt_srtp_connect,
957 .listen = vt_srtp_listen,
958 .accept = vt_srtp_accept,
959 .close = vt_srtp_close,
962 VCL_TEST_REGISTER_PROTO (VPPCOM_PROTO_SRTP, vcl_test_srtp);
965 * fd.io coding-style-patch-verification: ON
968 * eval: (c-set-style "gnu")