sh2lib.c 12 KB

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