sh2lib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <stdint.h>
  8. #include <stddef.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <ctype.h>
  13. #include <netdb.h>
  14. #include <esp_log.h>
  15. #include <http_parser.h>
  16. #include "sh2lib.h"
  17. static const char *TAG = "sh2lib";
  18. #define DBG_FRAME_SEND 1
  19. /*
  20. * The implementation of nghttp2_send_callback type. Here we write
  21. * |data| with size |length| to the network and return the number of
  22. * bytes actually written. See the documentation of
  23. * nghttp2_send_callback for the details.
  24. */
  25. static ssize_t callback_send_inner(struct sh2lib_handle *hd, const uint8_t *data,
  26. size_t length)
  27. {
  28. int rv = esp_tls_conn_write(hd->http2_tls, data, length);
  29. if (rv <= 0) {
  30. if (rv == ESP_TLS_ERR_SSL_WANT_READ || rv == ESP_TLS_ERR_SSL_WANT_WRITE) {
  31. rv = NGHTTP2_ERR_WOULDBLOCK;
  32. } else {
  33. rv = NGHTTP2_ERR_CALLBACK_FAILURE;
  34. }
  35. }
  36. return rv;
  37. }
  38. static ssize_t callback_send(nghttp2_session *session, const uint8_t *data,
  39. size_t length, int flags, void *user_data)
  40. {
  41. int rv = 0;
  42. struct sh2lib_handle *hd = user_data;
  43. int copy_offset = 0;
  44. int pending_data = length;
  45. /* Send data in 1000 byte chunks */
  46. while (copy_offset != length) {
  47. int chunk_len = pending_data > 1000 ? 1000 : pending_data;
  48. int subrv = callback_send_inner(hd, data + copy_offset, chunk_len);
  49. if (subrv <= 0) {
  50. if (copy_offset == 0) {
  51. /* If no data is transferred, send the error code */
  52. rv = subrv;
  53. }
  54. break;
  55. }
  56. copy_offset += subrv;
  57. pending_data -= subrv;
  58. rv += subrv;
  59. }
  60. return rv;
  61. }
  62. /*
  63. * The implementation of nghttp2_recv_callback type. Here we read data
  64. * from the network and write them in |buf|. The capacity of |buf| is
  65. * |length| bytes. Returns the number of bytes stored in |buf|. See
  66. * the documentation of nghttp2_recv_callback for the details.
  67. */
  68. static ssize_t callback_recv(nghttp2_session *session, uint8_t *buf,
  69. size_t length, int flags, void *user_data)
  70. {
  71. struct sh2lib_handle *hd = user_data;
  72. int rv;
  73. rv = esp_tls_conn_read(hd->http2_tls, (char *)buf, (int)length);
  74. if (rv < 0) {
  75. if (rv == ESP_TLS_ERR_SSL_WANT_READ || rv == ESP_TLS_ERR_SSL_WANT_WRITE) {
  76. rv = NGHTTP2_ERR_WOULDBLOCK;
  77. } else {
  78. rv = NGHTTP2_ERR_CALLBACK_FAILURE;
  79. }
  80. } else if (rv == 0) {
  81. rv = NGHTTP2_ERR_EOF;
  82. }
  83. return rv;
  84. }
  85. const char *sh2lib_frame_type_str(int type)
  86. {
  87. switch (type) {
  88. case NGHTTP2_HEADERS:
  89. return "HEADERS";
  90. break;
  91. case NGHTTP2_RST_STREAM:
  92. return "RST_STREAM";
  93. break;
  94. case NGHTTP2_GOAWAY:
  95. return "GOAWAY";
  96. break;
  97. case NGHTTP2_DATA:
  98. return "DATA";
  99. break;
  100. case NGHTTP2_SETTINGS:
  101. return "SETTINGS";
  102. break;
  103. case NGHTTP2_PUSH_PROMISE:
  104. return "PUSH_PROMISE";
  105. break;
  106. case NGHTTP2_PING:
  107. return "PING";
  108. break;
  109. default:
  110. return "other";
  111. break;
  112. }
  113. }
  114. static int callback_on_frame_send(nghttp2_session *session,
  115. const nghttp2_frame *frame, void *user_data)
  116. {
  117. ESP_LOGD(TAG, "[frame-send] frame type %s", sh2lib_frame_type_str(frame->hd.type));
  118. switch (frame->hd.type) {
  119. case NGHTTP2_HEADERS:
  120. if (nghttp2_session_get_stream_user_data(session, frame->hd.stream_id)) {
  121. ESP_LOGD(TAG, "[frame-send] C ----------------------------> S (HEADERS)");
  122. #if DBG_FRAME_SEND
  123. ESP_LOGD(TAG, "[frame-send] headers nv-len = %d", frame->headers.nvlen);
  124. const nghttp2_nv *nva = frame->headers.nva;
  125. size_t i;
  126. for (i = 0; i < frame->headers.nvlen; ++i) {
  127. ESP_LOGD(TAG, "[frame-send] %s : %s", nva[i].name, nva[i].value);
  128. }
  129. #endif
  130. }
  131. break;
  132. }
  133. return 0;
  134. }
  135. static int callback_on_frame_recv(nghttp2_session *session,
  136. const nghttp2_frame *frame, void *user_data)
  137. {
  138. ESP_LOGD(TAG, "[frame-recv][sid: %d] frame type %s", frame->hd.stream_id, sh2lib_frame_type_str(frame->hd.type));
  139. if (frame->hd.type != NGHTTP2_DATA) {
  140. return 0;
  141. }
  142. /* Subsequent processing only for data frame */
  143. sh2lib_frame_data_recv_cb_t data_recv_cb = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id);
  144. if (data_recv_cb) {
  145. struct sh2lib_handle *h2 = user_data;
  146. (*data_recv_cb)(h2, NULL, 0, DATA_RECV_FRAME_COMPLETE);
  147. }
  148. return 0;
  149. }
  150. static int callback_on_stream_close(nghttp2_session *session, int32_t stream_id,
  151. uint32_t error_code, void *user_data)
  152. {
  153. ESP_LOGD(TAG, "[stream-close][sid %d]", stream_id);
  154. sh2lib_frame_data_recv_cb_t data_recv_cb = nghttp2_session_get_stream_user_data(session, stream_id);
  155. if (data_recv_cb) {
  156. struct sh2lib_handle *h2 = user_data;
  157. (*data_recv_cb)(h2, NULL, 0, DATA_RECV_RST_STREAM);
  158. }
  159. return 0;
  160. }
  161. static int callback_on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
  162. int32_t stream_id, const uint8_t *data,
  163. size_t len, void *user_data)
  164. {
  165. sh2lib_frame_data_recv_cb_t data_recv_cb;
  166. ESP_LOGD(TAG, "[data-chunk][sid:%d]", stream_id);
  167. data_recv_cb = nghttp2_session_get_stream_user_data(session, stream_id);
  168. if (data_recv_cb) {
  169. ESP_LOGD(TAG, "[data-chunk] C <---------------------------- S (DATA chunk)"
  170. "%lu bytes",
  171. (unsigned long int)len);
  172. struct sh2lib_handle *h2 = user_data;
  173. (*data_recv_cb)(h2, (char *)data, len, 0);
  174. /* TODO: What to do with the return value: look for pause/abort */
  175. }
  176. return 0;
  177. }
  178. static int callback_on_header(nghttp2_session *session, const nghttp2_frame *frame,
  179. const uint8_t *name, size_t namelen, const uint8_t *value,
  180. size_t valuelen, uint8_t flags, void *user_data)
  181. {
  182. ESP_LOGD(TAG, "[hdr-recv][sid:%d] %s : %s", frame->hd.stream_id, name, value);
  183. return 0;
  184. }
  185. static int do_http2_connect(struct sh2lib_handle *hd)
  186. {
  187. int ret;
  188. nghttp2_session_callbacks *callbacks;
  189. nghttp2_session_callbacks_new(&callbacks);
  190. nghttp2_session_callbacks_set_send_callback(callbacks, callback_send);
  191. nghttp2_session_callbacks_set_recv_callback(callbacks, callback_recv);
  192. nghttp2_session_callbacks_set_on_frame_send_callback(callbacks, callback_on_frame_send);
  193. nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks, callback_on_frame_recv);
  194. nghttp2_session_callbacks_set_on_stream_close_callback(callbacks, callback_on_stream_close);
  195. nghttp2_session_callbacks_set_on_data_chunk_recv_callback(callbacks, callback_on_data_chunk_recv);
  196. nghttp2_session_callbacks_set_on_header_callback(callbacks, callback_on_header);
  197. ret = nghttp2_session_client_new(&hd->http2_sess, callbacks, hd);
  198. if (ret != 0) {
  199. ESP_LOGE(TAG, "[sh2-connect] New http2 session failed");
  200. nghttp2_session_callbacks_del(callbacks);
  201. return -1;
  202. }
  203. nghttp2_session_callbacks_del(callbacks);
  204. /* Create the SETTINGS frame */
  205. ret = nghttp2_submit_settings(hd->http2_sess, NGHTTP2_FLAG_NONE, NULL, 0);
  206. if (ret != 0) {
  207. ESP_LOGE(TAG, "[sh2-connect] Submit settings failed");
  208. return -1;
  209. }
  210. return 0;
  211. }
  212. int sh2lib_connect(struct sh2lib_config_t *cfg, struct sh2lib_handle *hd)
  213. {
  214. memset(hd, 0, sizeof(*hd));
  215. if (cfg == NULL) {
  216. ESP_LOGE(TAG, "[sh2-connect] pointer to sh2lib configurations cannot be NULL");
  217. goto error;
  218. }
  219. const char *proto[] = {"h2", NULL};
  220. esp_tls_cfg_t tls_cfg = {
  221. .alpn_protos = proto,
  222. .cacert_buf = cfg->cacert_buf,
  223. .cacert_bytes = cfg->cacert_bytes,
  224. .non_block = true,
  225. .timeout_ms = 10 * 1000,
  226. };
  227. if ((hd->http2_tls = esp_tls_conn_http_new(cfg->uri, &tls_cfg)) == NULL) {
  228. ESP_LOGE(TAG, "[sh2-connect] esp-tls connection failed");
  229. goto error;
  230. }
  231. struct http_parser_url u;
  232. http_parser_url_init(&u);
  233. http_parser_parse_url(cfg->uri, strlen(cfg->uri), 0, &u);
  234. hd->hostname = strndup(&cfg->uri[u.field_data[UF_HOST].off], u.field_data[UF_HOST].len);
  235. /* HTTP/2 Connection */
  236. if (do_http2_connect(hd) != 0) {
  237. ESP_LOGE(TAG, "[sh2-connect] HTTP2 Connection failed with %s", cfg->uri);
  238. goto error;
  239. }
  240. return 0;
  241. error:
  242. sh2lib_free(hd);
  243. return -1;
  244. }
  245. void sh2lib_free(struct sh2lib_handle *hd)
  246. {
  247. if (hd->http2_sess) {
  248. nghttp2_session_del(hd->http2_sess);
  249. hd->http2_sess = NULL;
  250. }
  251. if (hd->http2_tls) {
  252. esp_tls_conn_destroy(hd->http2_tls);
  253. hd->http2_tls = NULL;
  254. }
  255. if (hd->hostname) {
  256. free(hd->hostname);
  257. hd->hostname = NULL;
  258. }
  259. }
  260. int sh2lib_execute(struct sh2lib_handle *hd)
  261. {
  262. int ret;
  263. ret = nghttp2_session_send(hd->http2_sess);
  264. if (ret != 0) {
  265. ESP_LOGE(TAG, "[sh2-execute] HTTP2 session send failed %d", ret);
  266. return -1;
  267. }
  268. ret = nghttp2_session_recv(hd->http2_sess);
  269. if (ret != 0) {
  270. ESP_LOGE(TAG, "[sh2-execute] HTTP2 session recv failed %d", ret);
  271. return -1;
  272. }
  273. return 0;
  274. }
  275. int sh2lib_do_get_with_nv(struct sh2lib_handle *hd, const nghttp2_nv *nva, size_t nvlen, sh2lib_frame_data_recv_cb_t recv_cb)
  276. {
  277. int ret = nghttp2_submit_request(hd->http2_sess, NULL, nva, nvlen, NULL, recv_cb);
  278. if (ret < 0) {
  279. ESP_LOGE(TAG, "[sh2-do-get] HEADERS call failed");
  280. return -1;
  281. }
  282. return ret;
  283. }
  284. int sh2lib_do_get(struct sh2lib_handle *hd, const char *path, sh2lib_frame_data_recv_cb_t recv_cb)
  285. {
  286. const nghttp2_nv nva[] = { SH2LIB_MAKE_NV(":method", "GET"),
  287. SH2LIB_MAKE_NV(":scheme", "https"),
  288. SH2LIB_MAKE_NV(":authority", hd->hostname),
  289. SH2LIB_MAKE_NV(":path", path),
  290. };
  291. return sh2lib_do_get_with_nv(hd, nva, sizeof(nva) / sizeof(nva[0]), recv_cb);
  292. }
  293. ssize_t sh2lib_data_provider_cb(nghttp2_session *session, int32_t stream_id, uint8_t *buf,
  294. size_t length, uint32_t *data_flags,
  295. nghttp2_data_source *source, void *user_data)
  296. {
  297. struct sh2lib_handle *h2 = user_data;
  298. sh2lib_putpost_data_cb_t data_cb = source->ptr;
  299. return (*data_cb)(h2, (char *)buf, length, data_flags);
  300. }
  301. int sh2lib_do_putpost_with_nv(struct sh2lib_handle *hd, const nghttp2_nv *nva, size_t nvlen,
  302. sh2lib_putpost_data_cb_t send_cb,
  303. sh2lib_frame_data_recv_cb_t recv_cb)
  304. {
  305. nghttp2_data_provider sh2lib_data_provider;
  306. sh2lib_data_provider.read_callback = sh2lib_data_provider_cb;
  307. sh2lib_data_provider.source.ptr = send_cb;
  308. int ret = nghttp2_submit_request(hd->http2_sess, NULL, nva, nvlen, &sh2lib_data_provider, recv_cb);
  309. if (ret < 0) {
  310. ESP_LOGE(TAG, "[sh2-do-putpost] HEADERS call failed");
  311. return -1;
  312. }
  313. return ret;
  314. }
  315. int sh2lib_do_post(struct sh2lib_handle *hd, const char *path,
  316. sh2lib_putpost_data_cb_t send_cb,
  317. sh2lib_frame_data_recv_cb_t recv_cb)
  318. {
  319. const nghttp2_nv nva[] = { SH2LIB_MAKE_NV(":method", "POST"),
  320. SH2LIB_MAKE_NV(":scheme", "https"),
  321. SH2LIB_MAKE_NV(":authority", hd->hostname),
  322. SH2LIB_MAKE_NV(":path", path),
  323. };
  324. return sh2lib_do_putpost_with_nv(hd, nva, sizeof(nva) / sizeof(nva[0]), send_cb, recv_cb);
  325. }
  326. int sh2lib_do_put(struct sh2lib_handle *hd, const char *path,
  327. sh2lib_putpost_data_cb_t send_cb,
  328. sh2lib_frame_data_recv_cb_t recv_cb)
  329. {
  330. const nghttp2_nv nva[] = { SH2LIB_MAKE_NV(":method", "PUT"),
  331. SH2LIB_MAKE_NV(":scheme", "https"),
  332. SH2LIB_MAKE_NV(":authority", hd->hostname),
  333. SH2LIB_MAKE_NV(":path", path),
  334. };
  335. return sh2lib_do_putpost_with_nv(hd, nva, sizeof(nva) / sizeof(nva[0]), send_cb, recv_cb);
  336. }