nghttp3_frame.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * nghttp3
  3. *
  4. * Copyright (c) 2019 nghttp3 contributors
  5. * Copyright (c) 2013 nghttp2 contributors
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #ifndef NGHTTP3_FRAME_H
  27. #define NGHTTP3_FRAME_H
  28. #ifdef HAVE_CONFIG_H
  29. # include <config.h>
  30. #endif /* defined(HAVE_CONFIG_H) */
  31. #include <nghttp3/nghttp3.h>
  32. #include "nghttp3_buf.h"
  33. #define NGHTTP3_FRAME_DATA 0x00
  34. #define NGHTTP3_FRAME_HEADERS 0x01
  35. #define NGHTTP3_FRAME_CANCEL_PUSH 0x03
  36. #define NGHTTP3_FRAME_SETTINGS 0x04
  37. #define NGHTTP3_FRAME_PUSH_PROMISE 0x05
  38. #define NGHTTP3_FRAME_GOAWAY 0x07
  39. #define NGHTTP3_FRAME_MAX_PUSH_ID 0x0d
  40. /* PRIORITY_UPDATE: https://datatracker.ietf.org/doc/html/rfc9218 */
  41. #define NGHTTP3_FRAME_PRIORITY_UPDATE 0x0f0700
  42. #define NGHTTP3_FRAME_PRIORITY_UPDATE_PUSH_ID 0x0f0701
  43. /* Frame types that are reserved for HTTP/2, and must not be used in
  44. HTTP/3. */
  45. #define NGHTTP3_H2_FRAME_PRIORITY 0x02
  46. #define NGHTTP3_H2_FRAME_PING 0x06
  47. #define NGHTTP3_H2_FRAME_WINDOW_UPDATE 0x08
  48. #define NGHTTP3_H2_FRAME_CONTINUATION 0x9
  49. typedef struct nghttp3_frame_hd {
  50. int64_t type;
  51. int64_t length;
  52. } nghttp3_frame_hd;
  53. typedef struct nghttp3_frame_data {
  54. nghttp3_frame_hd hd;
  55. } nghttp3_frame_data;
  56. typedef struct nghttp3_frame_headers {
  57. nghttp3_frame_hd hd;
  58. nghttp3_nv *nva;
  59. size_t nvlen;
  60. } nghttp3_frame_headers;
  61. #define NGHTTP3_SETTINGS_ID_MAX_FIELD_SECTION_SIZE 0x06
  62. #define NGHTTP3_SETTINGS_ID_QPACK_MAX_TABLE_CAPACITY 0x01
  63. #define NGHTTP3_SETTINGS_ID_QPACK_BLOCKED_STREAMS 0x07
  64. #define NGHTTP3_SETTINGS_ID_ENABLE_CONNECT_PROTOCOL 0x08
  65. #define NGHTTP3_SETTINGS_ID_H3_DATAGRAM 0x33
  66. #define NGHTTP3_H2_SETTINGS_ID_ENABLE_PUSH 0x2
  67. #define NGHTTP3_H2_SETTINGS_ID_MAX_CONCURRENT_STREAMS 0x3
  68. #define NGHTTP3_H2_SETTINGS_ID_INITIAL_WINDOW_SIZE 0x4
  69. #define NGHTTP3_H2_SETTINGS_ID_MAX_FRAME_SIZE 0x5
  70. typedef struct nghttp3_settings_entry {
  71. uint64_t id;
  72. uint64_t value;
  73. } nghttp3_settings_entry;
  74. typedef struct nghttp3_frame_settings {
  75. nghttp3_frame_hd hd;
  76. size_t niv;
  77. nghttp3_settings_entry iv[1];
  78. } nghttp3_frame_settings;
  79. typedef struct nghttp3_frame_goaway {
  80. nghttp3_frame_hd hd;
  81. int64_t id;
  82. } nghttp3_frame_goaway;
  83. typedef struct nghttp3_frame_priority_update {
  84. nghttp3_frame_hd hd;
  85. /* pri_elem_id is stream ID if hd.type ==
  86. NGHTTP3_FRAME_PRIORITY_UPDATE. It is push ID if hd.type ==
  87. NGHTTP3_FRAME_PRIORITY_UPDATE_PUSH_ID. It is undefined
  88. otherwise. */
  89. int64_t pri_elem_id;
  90. /* When sending this frame, data should point to the buffer
  91. containing a serialized priority field value and its length is
  92. set to datalen. On reception, pri contains the decoded priority
  93. header value. */
  94. union {
  95. nghttp3_pri pri;
  96. struct {
  97. uint8_t *data;
  98. size_t datalen;
  99. };
  100. };
  101. } nghttp3_frame_priority_update;
  102. typedef union nghttp3_frame {
  103. nghttp3_frame_hd hd;
  104. nghttp3_frame_data data;
  105. nghttp3_frame_headers headers;
  106. nghttp3_frame_settings settings;
  107. nghttp3_frame_goaway goaway;
  108. nghttp3_frame_priority_update priority_update;
  109. } nghttp3_frame;
  110. /*
  111. * nghttp3_frame_write_hd writes frame header |hd| to |dest|. This
  112. * function assumes that |dest| has enough space to write |hd|.
  113. *
  114. * This function returns |dest| plus the number of bytes written.
  115. */
  116. uint8_t *nghttp3_frame_write_hd(uint8_t *dest, const nghttp3_frame_hd *hd);
  117. /*
  118. * nghttp3_frame_write_hd_len returns the number of bytes required to
  119. * write |hd|. hd->length must be set.
  120. */
  121. size_t nghttp3_frame_write_hd_len(const nghttp3_frame_hd *hd);
  122. /*
  123. * nghttp3_frame_write_settings writes SETTINGS frame |fr| to |dest|.
  124. * This function assumes that |dest| has enough space to write |fr|.
  125. *
  126. * This function returns |dest| plus the number of bytes written.
  127. */
  128. uint8_t *nghttp3_frame_write_settings(uint8_t *dest,
  129. const nghttp3_frame_settings *fr);
  130. /*
  131. * nghttp3_frame_write_settings_len returns the number of bytes
  132. * required to write |fr|. fr->hd.length is ignored. This function
  133. * stores payload length in |*ppayloadlen|.
  134. */
  135. size_t nghttp3_frame_write_settings_len(int64_t *pppayloadlen,
  136. const nghttp3_frame_settings *fr);
  137. /*
  138. * nghttp3_frame_write_goaway writes GOAWAY frame |fr| to |dest|.
  139. * This function assumes that |dest| has enough space to write |fr|.
  140. *
  141. * This function returns |dest| plus the number of bytes written.
  142. */
  143. uint8_t *nghttp3_frame_write_goaway(uint8_t *dest,
  144. const nghttp3_frame_goaway *fr);
  145. /*
  146. * nghttp3_frame_write_goaway_len returns the number of bytes required
  147. * to write |fr|. fr->hd.length is ignored. This function stores
  148. * payload length in |*ppayloadlen|.
  149. */
  150. size_t nghttp3_frame_write_goaway_len(int64_t *ppayloadlen,
  151. const nghttp3_frame_goaway *fr);
  152. /*
  153. * nghttp3_frame_write_priority_update writes PRIORITY_UPDATE frame
  154. * |fr| to |dest|. This function assumes that |dest| has enough space
  155. * to write |fr|.
  156. *
  157. * This function returns |dest| plus the number of bytes written;
  158. */
  159. uint8_t *
  160. nghttp3_frame_write_priority_update(uint8_t *dest,
  161. const nghttp3_frame_priority_update *fr);
  162. /*
  163. * nghttp3_frame_write_priority_update_len returns the number of bytes
  164. * required to write |fr|. fr->hd.length is ignored. This function
  165. * stores payload length in |*ppayloadlen|.
  166. */
  167. size_t nghttp3_frame_write_priority_update_len(
  168. int64_t *ppayloadlen, const nghttp3_frame_priority_update *fr);
  169. /*
  170. * nghttp3_nva_copy copies name/value pairs from |nva|, which contains
  171. * |nvlen| pairs, to |*nva_ptr|, which is dynamically allocated so
  172. * that all items can be stored. The resultant name and value in
  173. * nghttp2_nv are guaranteed to be NULL-terminated even if the input
  174. * is not null-terminated.
  175. *
  176. * The |*pnva| must be freed using nghttp3_nva_del().
  177. *
  178. * This function returns 0 if it succeeds or one of the following
  179. * negative error codes:
  180. *
  181. * NGHTTP3_ERR_NOMEM
  182. * Out of memory.
  183. */
  184. int nghttp3_nva_copy(nghttp3_nv **pnva, const nghttp3_nv *nva, size_t nvlen,
  185. const nghttp3_mem *mem);
  186. /*
  187. * nghttp3_nva_del frees |nva|.
  188. */
  189. void nghttp3_nva_del(nghttp3_nv *nva, const nghttp3_mem *mem);
  190. /*
  191. * nghttp3_frame_headers_free frees memory allocated for |fr|. It
  192. * assumes that fr->nva is created by nghttp3_nva_copy() or NULL.
  193. */
  194. void nghttp3_frame_headers_free(nghttp3_frame_headers *fr,
  195. const nghttp3_mem *mem);
  196. /*
  197. * nghttp3_frame_priority_update_free frees memory allocated for |fr|.
  198. * This function should only be called for an outgoing frame.
  199. */
  200. void nghttp3_frame_priority_update_free(nghttp3_frame_priority_update *fr,
  201. const nghttp3_mem *mem);
  202. #endif /* !defined(NGHTTP3_FRAME_H) */