nghttp3_conn.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * nghttp3
  3. *
  4. * Copyright (c) 2019 nghttp3 contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef NGHTTP3_CONN_H
  26. #define NGHTTP3_CONN_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <nghttp3/nghttp3.h>
  31. #include "nghttp3_stream.h"
  32. #include "nghttp3_map.h"
  33. #include "nghttp3_qpack.h"
  34. #include "nghttp3_tnode.h"
  35. #include "nghttp3_idtr.h"
  36. #include "nghttp3_gaptr.h"
  37. #define NGHTTP3_VARINT_MAX ((1ull << 62) - 1)
  38. /* NGHTTP3_QPACK_ENCODER_MAX_TABLE_CAPACITY is the maximum dynamic
  39. table size for QPACK encoder. */
  40. #define NGHTTP3_QPACK_ENCODER_MAX_TABLE_CAPACITY 16384
  41. /* NGHTTP3_QPACK_ENCODER_MAX_BLOCK_STREAMS is the maximum number of
  42. blocked streams for QPACK encoder. */
  43. #define NGHTTP3_QPACK_ENCODER_MAX_BLOCK_STREAMS 100
  44. /* NGHTTP3_CONN_FLAG_NONE indicates that no flag is set. */
  45. #define NGHTTP3_CONN_FLAG_NONE 0x0000u
  46. /* NGHTTP3_CONN_FLAG_SETTINGS_RECVED is set when SETTINGS frame has
  47. been received. */
  48. #define NGHTTP3_CONN_FLAG_SETTINGS_RECVED 0x0001u
  49. /* NGHTTP3_CONN_FLAG_CONTROL_OPENED is set when a control stream has
  50. opened. */
  51. #define NGHTTP3_CONN_FLAG_CONTROL_OPENED 0x0002u
  52. /* NGHTTP3_CONN_FLAG_QPACK_ENCODER_OPENED is set when a QPACK encoder
  53. stream has opened. */
  54. #define NGHTTP3_CONN_FLAG_QPACK_ENCODER_OPENED 0x0004u
  55. /* NGHTTP3_CONN_FLAG_QPACK_DECODER_OPENED is set when a QPACK decoder
  56. stream has opened. */
  57. #define NGHTTP3_CONN_FLAG_QPACK_DECODER_OPENED 0x0008u
  58. /* NGHTTP3_CONN_FLAG_SHUTDOWN_COMMENCED is set when graceful shutdown
  59. has started. */
  60. #define NGHTTP3_CONN_FLAG_SHUTDOWN_COMMENCED 0x0010u
  61. /* NGHTTP3_CONN_FLAG_GOAWAY_RECVED indicates that GOAWAY frame has
  62. received. */
  63. #define NGHTTP3_CONN_FLAG_GOAWAY_RECVED 0x0020u
  64. /* NGHTTP3_CONN_FLAG_GOAWAY_QUEUED indicates that GOAWAY frame has
  65. been submitted for transmission. */
  66. #define NGHTTP3_CONN_FLAG_GOAWAY_QUEUED 0x0040u
  67. typedef struct nghttp3_chunk {
  68. nghttp3_opl_entry oplent;
  69. } nghttp3_chunk;
  70. nghttp3_objalloc_decl(chunk, nghttp3_chunk, oplent)
  71. struct nghttp3_conn {
  72. nghttp3_objalloc out_chunk_objalloc;
  73. nghttp3_objalloc stream_objalloc;
  74. nghttp3_callbacks callbacks;
  75. nghttp3_map streams;
  76. nghttp3_qpack_decoder qdec;
  77. nghttp3_qpack_encoder qenc;
  78. nghttp3_pq qpack_blocked_streams;
  79. struct {
  80. nghttp3_pq spq;
  81. } sched[NGHTTP3_URGENCY_LEVELS];
  82. const nghttp3_mem *mem;
  83. void *user_data;
  84. int server;
  85. uint16_t flags;
  86. struct {
  87. nghttp3_settings settings;
  88. struct {
  89. /* max_pushes is the number of push IDs that local endpoint can
  90. issue. This field is used by server only and used just for
  91. validation */
  92. uint64_t max_pushes;
  93. } uni;
  94. } local;
  95. struct {
  96. struct {
  97. nghttp3_idtr idtr;
  98. /* max_client_streams is the cumulative number of client
  99. initiated bidirectional stream ID the remote endpoint can
  100. issue. This field is used on server side only. */
  101. uint64_t max_client_streams;
  102. /* num_streams is the number of client initiated bidirectional
  103. streams that are currently open. This field is for server
  104. use only. */
  105. size_t num_streams;
  106. } bidi;
  107. nghttp3_settings settings;
  108. } remote;
  109. struct {
  110. /* goaway_id is the latest ID received in GOAWAY frame. */
  111. int64_t goaway_id;
  112. int64_t max_stream_id_bidi;
  113. /* pri_fieldbuf is a buffer to store incoming Priority Field Value
  114. in PRIORITY_UPDATE frame. */
  115. uint8_t pri_fieldbuf[8];
  116. /* pri_fieldlen is the number of bytes written into
  117. pri_fieldbuf. */
  118. size_t pri_fieldbuflen;
  119. } rx;
  120. struct {
  121. struct {
  122. nghttp3_buf rbuf;
  123. nghttp3_buf ebuf;
  124. } qpack;
  125. nghttp3_stream *ctrl;
  126. nghttp3_stream *qenc;
  127. nghttp3_stream *qdec;
  128. /* goaway_id is the latest ID sent in GOAWAY frame. */
  129. int64_t goaway_id;
  130. } tx;
  131. };
  132. nghttp3_stream *nghttp3_conn_find_stream(nghttp3_conn *conn, int64_t stream_id);
  133. int nghttp3_conn_create_stream(nghttp3_conn *conn, nghttp3_stream **pstream,
  134. int64_t stream_id);
  135. nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc,
  136. nghttp3_stream *stream, const uint8_t *src,
  137. size_t srclen, int fin);
  138. nghttp3_ssize nghttp3_conn_read_uni(nghttp3_conn *conn, nghttp3_stream *stream,
  139. const uint8_t *src, size_t srclen, int fin);
  140. nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn,
  141. nghttp3_stream *stream,
  142. const uint8_t *src, size_t srclen);
  143. nghttp3_ssize nghttp3_conn_read_qpack_encoder(nghttp3_conn *conn,
  144. const uint8_t *src,
  145. size_t srclen);
  146. nghttp3_ssize nghttp3_conn_read_qpack_decoder(nghttp3_conn *conn,
  147. const uint8_t *src,
  148. size_t srclen);
  149. int nghttp3_conn_on_data(nghttp3_conn *conn, nghttp3_stream *stream,
  150. const uint8_t *data, size_t datalen);
  151. int nghttp3_conn_on_priority_update(nghttp3_conn *conn,
  152. const nghttp3_frame_priority_update *fr);
  153. nghttp3_ssize nghttp3_conn_on_headers(nghttp3_conn *conn,
  154. nghttp3_stream *stream,
  155. const uint8_t *data, size_t datalen,
  156. int fin);
  157. int nghttp3_conn_on_settings_entry_received(nghttp3_conn *conn,
  158. const nghttp3_frame_settings *fr);
  159. int nghttp3_conn_qpack_blocked_streams_push(nghttp3_conn *conn,
  160. nghttp3_stream *stream);
  161. void nghttp3_conn_qpack_blocked_streams_pop(nghttp3_conn *conn);
  162. int nghttp3_conn_schedule_stream(nghttp3_conn *conn, nghttp3_stream *stream);
  163. int nghttp3_conn_ensure_stream_scheduled(nghttp3_conn *conn,
  164. nghttp3_stream *stream);
  165. void nghttp3_conn_unschedule_stream(nghttp3_conn *conn, nghttp3_stream *stream);
  166. int nghttp3_conn_reject_stream(nghttp3_conn *conn, nghttp3_stream *stream);
  167. /*
  168. * nghttp3_conn_get_next_tx_stream returns next stream to send. It
  169. * returns NULL if there is no such stream.
  170. */
  171. nghttp3_stream *nghttp3_conn_get_next_tx_stream(nghttp3_conn *conn);
  172. #endif /* !defined(NGHTTP3_CONN_H) */