quic_rstream.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2022 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 "internal/common.h"
  10. #include "internal/time.h"
  11. #include "internal/quic_stream.h"
  12. #include "internal/quic_sf_list.h"
  13. struct quic_rstream_st {
  14. SFRAME_LIST fl;
  15. QUIC_RXFC *rxfc;
  16. OSSL_STATM *statm;
  17. };
  18. QUIC_RSTREAM *ossl_quic_rstream_new(OSSL_QRX *qrx, QUIC_RXFC *rxfc,
  19. OSSL_STATM *statm)
  20. {
  21. QUIC_RSTREAM *ret = OPENSSL_malloc(sizeof(*ret));
  22. if (ret == NULL)
  23. return NULL;
  24. ossl_sframe_list_init(&ret->fl, qrx);
  25. ret->rxfc = rxfc;
  26. ret->statm = statm;
  27. return ret;
  28. }
  29. void ossl_quic_rstream_free(QUIC_RSTREAM *qrs)
  30. {
  31. ossl_sframe_list_destroy(&qrs->fl);
  32. OPENSSL_free(qrs);
  33. }
  34. int ossl_quic_rstream_queue_data(QUIC_RSTREAM *qrs, OSSL_QRX_PKT_WRAP *pkt_wrap,
  35. uint64_t offset,
  36. const unsigned char *data, uint64_t data_len,
  37. int fin)
  38. {
  39. UINT_RANGE range;
  40. range.start = offset;
  41. range.end = offset + data_len;
  42. return ossl_sframe_list_insert(&qrs->fl, &range, pkt_wrap, data, fin);
  43. }
  44. static int read_internal(QUIC_RSTREAM *qrs, unsigned char *buf, size_t size,
  45. size_t *readbytes, int *fin, int drop)
  46. {
  47. void *iter = NULL;
  48. UINT_RANGE range;
  49. const unsigned char *data;
  50. uint64_t offset = 0;
  51. size_t readbytes_ = 0;
  52. int fin_ = 0, ret = 1;
  53. while (ossl_sframe_list_peek(&qrs->fl, &iter, &range, &data, &fin_)) {
  54. size_t l = (size_t)(range.end - range.start);
  55. if (l > size)
  56. l = size;
  57. memcpy(buf, data, l);
  58. offset = range.start + l;
  59. size -= l;
  60. buf += l;
  61. readbytes_ += l;
  62. if (size == 0)
  63. break;
  64. }
  65. if (drop && offset != 0)
  66. ret = ossl_sframe_list_drop_frames(&qrs->fl, offset);
  67. if (ret) {
  68. *readbytes = readbytes_;
  69. *fin = fin_;
  70. }
  71. return ret;
  72. }
  73. int ossl_quic_rstream_read(QUIC_RSTREAM *qrs, unsigned char *buf, size_t size,
  74. size_t *readbytes, int *fin)
  75. {
  76. OSSL_TIME rtt;
  77. if (qrs->statm != NULL) {
  78. OSSL_RTT_INFO rtt_info;
  79. ossl_statm_get_rtt_info(qrs->statm, &rtt_info);
  80. rtt = rtt_info.smoothed_rtt;
  81. } else {
  82. rtt = ossl_time_zero();
  83. }
  84. if (!read_internal(qrs, buf, size, readbytes, fin, 1))
  85. return 0;
  86. if (qrs->rxfc != NULL
  87. && !ossl_quic_rxfc_on_retire(qrs->rxfc, *readbytes, rtt))
  88. return 0;
  89. return 1;
  90. }
  91. int ossl_quic_rstream_peek(QUIC_RSTREAM *qrs, unsigned char *buf, size_t size,
  92. size_t *readbytes, int *fin)
  93. {
  94. return read_internal(qrs, buf, size, readbytes, fin, 0);
  95. }
  96. int ossl_quic_rstream_available(QUIC_RSTREAM *qrs, size_t *avail, int *fin)
  97. {
  98. void *iter = NULL;
  99. UINT_RANGE range;
  100. const unsigned char *data;
  101. uint64_t avail_ = 0;
  102. while (ossl_sframe_list_peek(&qrs->fl, &iter, &range, &data, fin))
  103. avail_ += range.end - range.start;
  104. #if SIZE_MAX < UINT64_MAX
  105. *avail = avail_ > SIZE_MAX ? SIZE_MAX : (size_t)avail_;
  106. #else
  107. *avail = (size_t)avail_;
  108. #endif
  109. return 1;
  110. }