nghttp2_frame.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #ifndef NGHTTP2_FRAME_H
  5. #define NGHTTP2_FRAME_H
  6. #ifdef HAVE_CONFIG_H
  7. #include <config.h>
  8. #endif /* HAVE_CONFIG_H */
  9. #include "nghttp2.h"
  10. #include "nghttp2_hd.h"
  11. #include "nghttp2_buf.h"
  12. #define NGHTTP2_STREAM_ID_MASK ((1u << 31) - 1)
  13. #define NGHTTP2_PRI_GROUP_ID_MASK ((1u << 31) - 1)
  14. #define NGHTTP2_PRIORITY_MASK ((1u << 31) - 1)
  15. #define NGHTTP2_WINDOW_SIZE_INCREMENT_MASK ((1u << 31) - 1)
  16. #define NGHTTP2_SETTINGS_ID_MASK ((1 << 24) - 1)
  17. /* The number of bytes of frame header. */
  18. #define NGHTTP2_FRAME_HDLEN 9
  19. #define NGHTTP2_MAX_FRAME_SIZE_MAX ((1 << 24) - 1)
  20. #define NGHTTP2_MAX_FRAME_SIZE_MIN (1 << 14)
  21. #define NGHTTP2_MAX_PAYLOADLEN 16384
  22. /* The one frame buffer length for tranmission. We may use several of
  23. them to support CONTINUATION. To account for Pad Length field, we
  24. allocate extra 1 byte, which saves extra large memcopying. */
  25. #define NGHTTP2_FRAMEBUF_CHUNKLEN \
  26. (NGHTTP2_FRAME_HDLEN + 1 + NGHTTP2_MAX_PAYLOADLEN)
  27. /* The default length of DATA frame payload. */
  28. #define NGHTTP2_DATA_PAYLOADLEN NGHTTP2_MAX_FRAME_SIZE_MIN
  29. /* Maximum headers block size to send, calculated using
  30. nghttp2_hd_deflate_bound(). This is the default value, and can be
  31. overridden by nghttp2_option_set_max_send_header_block_size(). */
  32. #define NGHTTP2_MAX_HEADERSLEN 65536
  33. /* The number of bytes for each SETTINGS entry */
  34. #define NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH 6
  35. /* Length of priority related fields in HEADERS/PRIORITY frames */
  36. #define NGHTTP2_PRIORITY_SPECLEN 5
  37. /* Maximum length of padding in bytes. */
  38. #define NGHTTP2_MAX_PADLEN 256
  39. /* Union of extension frame payload */
  40. typedef union {
  41. nghttp2_ext_altsvc altsvc;
  42. } nghttp2_ext_frame_payload;
  43. void nghttp2_frame_pack_frame_hd(uint8_t *buf, const nghttp2_frame_hd *hd);
  44. void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t *buf);
  45. /**
  46. * Initializes frame header |hd| with given parameters. Reserved bit
  47. * is set to 0.
  48. */
  49. void nghttp2_frame_hd_init(nghttp2_frame_hd *hd, size_t length, uint8_t type,
  50. uint8_t flags, int32_t stream_id);
  51. /**
  52. * Returns the number of priority field depending on the |flags|. If
  53. * |flags| has neither NGHTTP2_FLAG_PRIORITY_GROUP nor
  54. * NGHTTP2_FLAG_PRIORITY_DEPENDENCY set, return 0.
  55. */
  56. size_t nghttp2_frame_priority_len(uint8_t flags);
  57. /**
  58. * Packs the |pri_spec| in |buf|. This function assumes |buf| has
  59. * enough space for serialization.
  60. */
  61. void nghttp2_frame_pack_priority_spec(uint8_t *buf,
  62. const nghttp2_priority_spec *pri_spec);
  63. /**
  64. * Unpacks the priority specification from payload |payload| of length
  65. * |payloadlen| to |pri_spec|. The |flags| is used to determine what
  66. * kind of priority specification is in |payload|. This function
  67. * assumes the |payload| contains whole priority specification.
  68. */
  69. void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
  70. const uint8_t *payload);
  71. /*
  72. * Returns the offset from the HEADERS frame payload where the
  73. * compressed header block starts. The frame payload does not include
  74. * frame header.
  75. */
  76. size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame);
  77. /*
  78. * Packs HEADERS frame |frame| in wire format and store it in |bufs|.
  79. * This function expands |bufs| as necessary to store frame.
  80. *
  81. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  82. * before calling this function.
  83. *
  84. * frame->hd.length is assigned after length is determined during
  85. * packing process. CONTINUATION frames are also serialized in this
  86. * function. This function does not handle padding.
  87. *
  88. * This function returns 0 if it succeeds, or returns one of the
  89. * following negative error codes:
  90. *
  91. * NGHTTP2_ERR_HEADER_COMP
  92. * The deflate operation failed.
  93. * NGHTTP2_ERR_NOMEM
  94. * Out of memory.
  95. */
  96. int nghttp2_frame_pack_headers(nghttp2_bufs *bufs, nghttp2_headers *frame,
  97. nghttp2_hd_deflater *deflater);
  98. /*
  99. * Unpacks HEADERS frame byte sequence into |frame|. This function
  100. * only unapcks bytes that come before name/value header block and
  101. * after possible Pad Length field.
  102. *
  103. * This function always succeeds and returns 0.
  104. */
  105. int nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
  106. const uint8_t *payload);
  107. /*
  108. * Packs PRIORITY frame |frame| in wire format and store it in
  109. * |bufs|.
  110. *
  111. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  112. * before calling this function.
  113. *
  114. * This function always succeeds and returns 0.
  115. */
  116. int nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame);
  117. /*
  118. * Unpacks PRIORITY wire format into |frame|.
  119. */
  120. void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
  121. const uint8_t *payload);
  122. /*
  123. * Packs RST_STREAM frame |frame| in wire frame format and store it in
  124. * |bufs|.
  125. *
  126. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  127. * before calling this function.
  128. *
  129. * This function always succeeds and returns 0.
  130. */
  131. int nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
  132. nghttp2_rst_stream *frame);
  133. /*
  134. * Unpacks RST_STREAM frame byte sequence into |frame|.
  135. */
  136. void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
  137. const uint8_t *payload);
  138. /*
  139. * Packs SETTINGS frame |frame| in wire format and store it in
  140. * |bufs|.
  141. *
  142. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  143. * before calling this function.
  144. *
  145. * This function returns 0 if it succeeds, or returns one of the
  146. * following negative error codes:
  147. *
  148. * NGHTTP2_ERR_FRAME_SIZE_ERROR
  149. * The length of the frame is too large.
  150. */
  151. int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame);
  152. /*
  153. * Packs the |iv|, which includes |niv| entries, in the |buf|,
  154. * assuming the |buf| has at least 8 * |niv| bytes.
  155. *
  156. * Returns the number of bytes written into the |buf|.
  157. */
  158. size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
  159. const nghttp2_settings_entry *iv,
  160. size_t niv);
  161. void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
  162. const uint8_t *payload);
  163. /*
  164. * Initializes payload of frame->settings. The |frame| takes
  165. * ownership of |iv|.
  166. */
  167. void nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
  168. nghttp2_settings_entry *iv,
  169. size_t niv);
  170. /*
  171. * Unpacks SETTINGS payload into |*iv_ptr|. The number of entries are
  172. * assigned to the |*niv_ptr|. This function allocates enough memory
  173. * to store the result in |*iv_ptr|. The caller is responsible to free
  174. * |*iv_ptr| after its use.
  175. *
  176. * This function returns 0 if it succeeds or one of the following
  177. * negative error codes:
  178. *
  179. * NGHTTP2_ERR_NOMEM
  180. * Out of memory.
  181. */
  182. int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
  183. size_t *niv_ptr,
  184. const uint8_t *payload,
  185. size_t payloadlen, nghttp2_mem *mem);
  186. /*
  187. * Packs PUSH_PROMISE frame |frame| in wire format and store it in
  188. * |bufs|. This function expands |bufs| as necessary to store
  189. * frame.
  190. *
  191. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  192. * before calling this function.
  193. *
  194. * frame->hd.length is assigned after length is determined during
  195. * packing process. CONTINUATION frames are also serialized in this
  196. * function. This function does not handle padding.
  197. *
  198. * This function returns 0 if it succeeds, or returns one of the
  199. * following negative error codes:
  200. *
  201. * NGHTTP2_ERR_HEADER_COMP
  202. * The deflate operation failed.
  203. * NGHTTP2_ERR_NOMEM
  204. * Out of memory.
  205. */
  206. int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
  207. nghttp2_push_promise *frame,
  208. nghttp2_hd_deflater *deflater);
  209. /*
  210. * Unpacks PUSH_PROMISE frame byte sequence into |frame|. This
  211. * function only unapcks bytes that come before name/value header
  212. * block and after possible Pad Length field.
  213. *
  214. * This function returns 0 if it succeeds or one of the following
  215. * negative error codes:
  216. *
  217. * NGHTTP2_ERR_PROTO
  218. * TODO END_HEADERS flag is not set
  219. */
  220. int nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
  221. const uint8_t *payload);
  222. /*
  223. * Packs PING frame |frame| in wire format and store it in
  224. * |bufs|.
  225. *
  226. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  227. * before calling this function.
  228. *
  229. * This function always succeeds and returns 0.
  230. */
  231. int nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame);
  232. /*
  233. * Unpacks PING wire format into |frame|.
  234. */
  235. void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
  236. const uint8_t *payload);
  237. /*
  238. * Packs GOAWAY frame |frame| in wire format and store it in |bufs|.
  239. * This function expands |bufs| as necessary to store frame.
  240. *
  241. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  242. * before calling this function.
  243. *
  244. * This function returns 0 if it succeeds or one of the following
  245. * negative error codes:
  246. *
  247. * NGHTTP2_ERR_NOMEM
  248. * Out of memory.
  249. * NGHTTP2_ERR_FRAME_SIZE_ERROR
  250. * The length of the frame is too large.
  251. */
  252. int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame);
  253. /*
  254. * Unpacks GOAWAY wire format into |frame|. The |payload| of length
  255. * |payloadlen| contains first 8 bytes of payload. The
  256. * |var_gift_payload| of length |var_gift_payloadlen| contains
  257. * remaining payload and its buffer is gifted to the function and then
  258. * |frame|. The |var_gift_payloadlen| must be freed by
  259. * nghttp2_frame_goaway_free().
  260. */
  261. void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
  262. const uint8_t *payload,
  263. uint8_t *var_gift_payload,
  264. size_t var_gift_payloadlen);
  265. /*
  266. * Unpacks GOAWAY wire format into |frame|. This function only exists
  267. * for unit test. After allocating buffer for debug data, this
  268. * function internally calls nghttp2_frame_unpack_goaway_payload().
  269. *
  270. * This function returns 0 if it succeeds, or one of the following
  271. * negative error codes:
  272. *
  273. * NGHTTP2_ERR_NOMEM
  274. * Out of memory.
  275. */
  276. int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
  277. const uint8_t *payload,
  278. size_t payloadlen, nghttp2_mem *mem);
  279. /*
  280. * Packs WINDOW_UPDATE frame |frame| in wire frame format and store it
  281. * in |bufs|.
  282. *
  283. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  284. * before calling this function.
  285. *
  286. * This function always succeeds and returns 0.
  287. */
  288. int nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
  289. nghttp2_window_update *frame);
  290. /*
  291. * Unpacks WINDOW_UPDATE frame byte sequence into |frame|.
  292. */
  293. void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
  294. const uint8_t *payload);
  295. /*
  296. * Packs ALTSVC frame |frame| in wire frame format and store it in
  297. * |bufs|.
  298. *
  299. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  300. * before calling this function.
  301. *
  302. * This function always succeeds and returns 0.
  303. */
  304. int nghttp2_frame_pack_altsvc(nghttp2_bufs *bufs, nghttp2_extension *ext);
  305. /*
  306. * Unpacks ALTSVC wire format into |frame|. The |payload| of
  307. * |payloadlen| bytes contains frame payload. This function assumes
  308. * that frame->payload points to the nghttp2_ext_altsvc object.
  309. *
  310. * This function always succeeds and returns 0.
  311. */
  312. void nghttp2_frame_unpack_altsvc_payload(nghttp2_extension *frame,
  313. size_t origin_len, uint8_t *payload,
  314. size_t payloadlen);
  315. /*
  316. * Unpacks ALTSVC wire format into |frame|. This function only exists
  317. * for unit test. After allocating buffer for fields, this function
  318. * internally calls nghttp2_frame_unpack_altsvc_payload().
  319. *
  320. * This function returns 0 if it succeeds, or one of the following
  321. * negative error codes:
  322. *
  323. * NGHTTP2_ERR_NOMEM
  324. * Out of memory.
  325. * NGHTTP2_ERR_FRAME_SIZE_ERROR
  326. * The payload is too small.
  327. */
  328. int nghttp2_frame_unpack_altsvc_payload2(nghttp2_extension *frame,
  329. const uint8_t *payload,
  330. size_t payloadlen, nghttp2_mem *mem);
  331. /*
  332. * Initializes HEADERS frame |frame| with given values. |frame| takes
  333. * ownership of |nva|, so caller must not free it. If |stream_id| is
  334. * not assigned yet, it must be -1.
  335. */
  336. void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags,
  337. int32_t stream_id, nghttp2_headers_category cat,
  338. const nghttp2_priority_spec *pri_spec,
  339. nghttp2_nv *nva, size_t nvlen);
  340. void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem);
  341. void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
  342. const nghttp2_priority_spec *pri_spec);
  343. void nghttp2_frame_priority_free(nghttp2_priority *frame);
  344. void nghttp2_frame_rst_stream_init(nghttp2_rst_stream *frame, int32_t stream_id,
  345. uint32_t error_code);
  346. void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame);
  347. /*
  348. * Initializes PUSH_PROMISE frame |frame| with given values. |frame|
  349. * takes ownership of |nva|, so caller must not free it.
  350. */
  351. void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags,
  352. int32_t stream_id,
  353. int32_t promised_stream_id,
  354. nghttp2_nv *nva, size_t nvlen);
  355. void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame,
  356. nghttp2_mem *mem);
  357. /*
  358. * Initializes SETTINGS frame |frame| with given values. |frame| takes
  359. * ownership of |iv|, so caller must not free it. The |flags| are
  360. * bitwise-OR of one or more of nghttp2_settings_flag.
  361. */
  362. void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
  363. nghttp2_settings_entry *iv, size_t niv);
  364. void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem);
  365. /*
  366. * Initializes PING frame |frame| with given values. If the
  367. * |opqeue_data| is not NULL, it must point to 8 bytes memory region
  368. * of data. The data pointed by |opaque_data| is copied. It can be
  369. * NULL. In this case, 8 bytes NULL is used.
  370. */
  371. void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags,
  372. const uint8_t *opque_data);
  373. void nghttp2_frame_ping_free(nghttp2_ping *frame);
  374. /*
  375. * Initializes GOAWAY frame |frame| with given values. On success,
  376. * this function takes ownership of |opaque_data|, so caller must not
  377. * free it. If the |opaque_data_len| is 0, opaque_data could be NULL.
  378. */
  379. void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id,
  380. uint32_t error_code, uint8_t *opaque_data,
  381. size_t opaque_data_len);
  382. void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem);
  383. void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
  384. uint8_t flags, int32_t stream_id,
  385. int32_t window_size_increment);
  386. void nghttp2_frame_window_update_free(nghttp2_window_update *frame);
  387. void nghttp2_frame_extension_init(nghttp2_extension *frame, uint8_t type,
  388. uint8_t flags, int32_t stream_id,
  389. void *payload);
  390. void nghttp2_frame_extension_free(nghttp2_extension *frame);
  391. /*
  392. * Initializes ALTSVC frame |frame| with given values. This function
  393. * assumes that frame->payload points to nghttp2_ext_altsvc object.
  394. * Also |origin| and |field_value| are allocated in single buffer,
  395. * starting |origin|. On success, this function takes ownership of
  396. * |origin|, so caller must not free it.
  397. */
  398. void nghttp2_frame_altsvc_init(nghttp2_extension *frame, int32_t stream_id,
  399. uint8_t *origin, size_t origin_len,
  400. uint8_t *field_value, size_t field_value_len);
  401. /*
  402. * Frees up resources under |frame|. This function does not free
  403. * nghttp2_ext_altsvc object pointed by frame->payload. This function
  404. * only frees origin pointed by nghttp2_ext_altsvc.origin. Therefore,
  405. * other fields must be allocated in the same buffer with origin.
  406. */
  407. void nghttp2_frame_altsvc_free(nghttp2_extension *frame, nghttp2_mem *mem);
  408. /*
  409. * Returns the number of padding bytes after payload. The total
  410. * padding length is given in the |padlen|. The returned value does
  411. * not include the Pad Length field. If |padlen| is 0, this function
  412. * returns 0, regardless of frame->hd.flags.
  413. */
  414. size_t nghttp2_frame_trail_padlen(nghttp2_frame *frame, size_t padlen);
  415. void nghttp2_frame_data_init(nghttp2_data *frame, uint8_t flags,
  416. int32_t stream_id);
  417. void nghttp2_frame_data_free(nghttp2_data *frame);
  418. /*
  419. * Makes copy of |iv| and return the copy. The |niv| is the number of
  420. * entries in |iv|. This function returns the pointer to the copy if
  421. * it succeeds, or NULL.
  422. */
  423. nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
  424. size_t niv, nghttp2_mem *mem);
  425. /*
  426. * Sorts the |nva| in ascending order of name and value. If names are
  427. * equivalent, sort them by value.
  428. */
  429. void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen);
  430. /*
  431. * Copies name/value pairs from |nva|, which contains |nvlen| pairs,
  432. * to |*nva_ptr|, which is dynamically allocated so that all items can
  433. * be stored. The resultant name and value in nghttp2_nv are
  434. * guaranteed to be NULL-terminated even if the input is not
  435. * null-terminated.
  436. *
  437. * The |*nva_ptr| must be freed using nghttp2_nv_array_del().
  438. *
  439. * This function returns 0 if it succeeds or one of the following
  440. * negative error codes:
  441. *
  442. * NGHTTP2_ERR_NOMEM
  443. * Out of memory.
  444. */
  445. int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
  446. size_t nvlen, nghttp2_mem *mem);
  447. /*
  448. * Returns nonzero if the name/value pair |a| equals to |b|. The name
  449. * is compared in case-sensitive, because we ensure that this function
  450. * is called after the name is lower-cased.
  451. */
  452. int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b);
  453. /*
  454. * Frees |nva|.
  455. */
  456. void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem);
  457. /*
  458. * Checks that the |iv|, which includes |niv| entries, does not have
  459. * invalid values.
  460. *
  461. * This function returns nonzero if it succeeds, or 0.
  462. */
  463. int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv);
  464. /*
  465. * Sets Pad Length field and flags and adjusts frame header position
  466. * of each buffers in |bufs|. The number of padding is given in the
  467. * |padlen| including Pad Length field. The |hd| is the frame header
  468. * for the serialized data. This function fills zeros padding region
  469. * unless framehd_only is nonzero.
  470. *
  471. * This function returns 0 if it succeeds, or one of the following
  472. * negative error codes:
  473. *
  474. * NGHTTP2_ERR_NOMEM
  475. * Out of memory.
  476. * NGHTTP2_ERR_FRAME_SIZE_ERROR
  477. * The length of the resulting frame is too large.
  478. */
  479. int nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
  480. size_t padlen, int framehd_only);
  481. #endif /* NGHTTP2_FRAME_H */