pktsplitbio.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/bio.h>
  10. #include "quictestlib.h"
  11. #include "../testutil.h"
  12. static long pkt_split_dgram_ctrl(BIO *bio, int cmd, long num, void *ptr)
  13. {
  14. long ret;
  15. BIO *next = BIO_next(bio);
  16. if (next == NULL)
  17. return 0;
  18. switch (cmd) {
  19. case BIO_CTRL_DUP:
  20. ret = 0L;
  21. break;
  22. default:
  23. ret = BIO_ctrl(next, cmd, num, ptr);
  24. break;
  25. }
  26. return ret;
  27. }
  28. static int pkt_split_dgram_sendmmsg(BIO *bio, BIO_MSG *msg, size_t stride,
  29. size_t num_msg, uint64_t flags,
  30. size_t *msgs_processed)
  31. {
  32. BIO *next = BIO_next(bio);
  33. if (next == NULL)
  34. return 0;
  35. /*
  36. * We only introduce noise when receiving messages. We just pass this on
  37. * to the underlying BIO.
  38. */
  39. return BIO_sendmmsg(next, msg, stride, num_msg, flags, msgs_processed);
  40. }
  41. static int pkt_split_dgram_recvmmsg(BIO *bio, BIO_MSG *msg, size_t stride,
  42. size_t num_msg, uint64_t flags,
  43. size_t *msgs_processed)
  44. {
  45. BIO *next = BIO_next(bio);
  46. size_t i, j, data_len = 0, msg_cnt = 0;
  47. BIO_MSG *thismsg;
  48. if (!TEST_ptr(next))
  49. return 0;
  50. /*
  51. * For simplicity we assume that all elements in the msg array have the
  52. * same data_len. They are not required to by the API, but it would be quite
  53. * strange for that not to be the case - and our code that calls
  54. * BIO_recvmmsg does do this (which is all that is important for this test
  55. * code). We test the invariant here.
  56. */
  57. for (i = 0; i < num_msg; i++) {
  58. if (i == 0)
  59. data_len = msg[i].data_len;
  60. else if (!TEST_size_t_eq(msg[i].data_len, data_len))
  61. return 0;
  62. }
  63. if (!BIO_recvmmsg(next, msg, stride, num_msg, flags, msgs_processed))
  64. return 0;
  65. msg_cnt = *msgs_processed;
  66. if (msg_cnt == num_msg)
  67. return 1; /* We've used all our slots and can't split any more */
  68. assert(msg_cnt < num_msg);
  69. for (i = 0, thismsg = msg; i < msg_cnt; i++, thismsg++) {
  70. QUIC_PKT_HDR hdr;
  71. PACKET pkt;
  72. size_t remain;
  73. if (!PACKET_buf_init(&pkt, thismsg->data, thismsg->data_len))
  74. return 0;
  75. /* Decode the packet header */
  76. /*
  77. * TODO(QUIC SERVER): We need to query the short connection id len
  78. * here, e.g. via some API SSL_get_short_conn_id_len()
  79. */
  80. if (ossl_quic_wire_decode_pkt_hdr(&pkt, 0, 0, 0, &hdr, NULL) != 1)
  81. return 0;
  82. remain = PACKET_remaining(&pkt);
  83. if (remain > 0) {
  84. for (j = msg_cnt; j > i; j--) {
  85. if (!bio_msg_copy(&msg[j], &msg[j - 1]))
  86. return 0;
  87. }
  88. thismsg->data_len -= remain;
  89. msg[i + 1].data_len = remain;
  90. memmove(msg[i + 1].data,
  91. (unsigned char *)msg[i + 1].data + thismsg->data_len,
  92. remain);
  93. msg_cnt++;
  94. }
  95. }
  96. *msgs_processed = msg_cnt;
  97. return 1;
  98. }
  99. /* Choose a sufficiently large type likely to be unused for this custom BIO */
  100. #define BIO_TYPE_PKT_SPLIT_DGRAM_FILTER (0x81 | BIO_TYPE_FILTER)
  101. static BIO_METHOD *method_pkt_split_dgram = NULL;
  102. /* Note: Not thread safe! */
  103. const BIO_METHOD *bio_f_pkt_split_dgram_filter(void)
  104. {
  105. if (method_pkt_split_dgram == NULL) {
  106. method_pkt_split_dgram = BIO_meth_new(BIO_TYPE_PKT_SPLIT_DGRAM_FILTER,
  107. "Packet splitting datagram filter");
  108. if (method_pkt_split_dgram == NULL
  109. || !BIO_meth_set_ctrl(method_pkt_split_dgram, pkt_split_dgram_ctrl)
  110. || !BIO_meth_set_sendmmsg(method_pkt_split_dgram,
  111. pkt_split_dgram_sendmmsg)
  112. || !BIO_meth_set_recvmmsg(method_pkt_split_dgram,
  113. pkt_split_dgram_recvmmsg))
  114. return NULL;
  115. }
  116. return method_pkt_split_dgram;
  117. }
  118. void bio_f_pkt_split_dgram_filter_free(void)
  119. {
  120. BIO_meth_free(method_pkt_split_dgram);
  121. }