2
0

tls-session-reuse.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * TLS session reuse
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include <inttypes.h>
  33. /* #include <error.h> */
  34. #include <errno.h>
  35. #include <curl/curl.h>
  36. #include <curl/mprintf.h>
  37. static void log_line_start(FILE *log, const char *idsbuf, curl_infotype type)
  38. {
  39. /*
  40. * This is the trace look that is similar to what libcurl makes on its
  41. * own.
  42. */
  43. static const char * const s_infotype[] = {
  44. "* ", "< ", "> ", "{ ", "} ", "{ ", "} "
  45. };
  46. if(idsbuf && *idsbuf)
  47. fprintf(log, "%s%s", idsbuf, s_infotype[type]);
  48. else
  49. fputs(s_infotype[type], log);
  50. }
  51. #define TRC_IDS_FORMAT_IDS_1 "[%" CURL_FORMAT_CURL_OFF_T "-x] "
  52. #define TRC_IDS_FORMAT_IDS_2 "[%" CURL_FORMAT_CURL_OFF_T "-%" \
  53. CURL_FORMAT_CURL_OFF_T "] "
  54. /*
  55. ** callback for CURLOPT_DEBUGFUNCTION
  56. */
  57. static int debug_cb(CURL *handle, curl_infotype type,
  58. char *data, size_t size,
  59. void *userdata)
  60. {
  61. FILE *output = stderr;
  62. static int newl = 0;
  63. static int traced_data = 0;
  64. char idsbuf[60];
  65. curl_off_t xfer_id, conn_id;
  66. (void)handle; /* not used */
  67. (void)userdata;
  68. if(!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
  69. if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
  70. conn_id >= 0) {
  71. curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_2,
  72. xfer_id, conn_id);
  73. }
  74. else {
  75. curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_1, xfer_id);
  76. }
  77. }
  78. else
  79. idsbuf[0] = 0;
  80. switch(type) {
  81. case CURLINFO_HEADER_OUT:
  82. if(size > 0) {
  83. size_t st = 0;
  84. size_t i;
  85. for(i = 0; i < size - 1; i++) {
  86. if(data[i] == '\n') { /* LF */
  87. if(!newl) {
  88. log_line_start(output, idsbuf, type);
  89. }
  90. (void)fwrite(data + st, i - st + 1, 1, output);
  91. st = i + 1;
  92. newl = 0;
  93. }
  94. }
  95. if(!newl)
  96. log_line_start(output, idsbuf, type);
  97. (void)fwrite(data + st, i - st + 1, 1, output);
  98. }
  99. newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
  100. traced_data = 0;
  101. break;
  102. case CURLINFO_TEXT:
  103. case CURLINFO_HEADER_IN:
  104. if(!newl)
  105. log_line_start(output, idsbuf, type);
  106. (void)fwrite(data, size, 1, output);
  107. newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
  108. traced_data = 0;
  109. break;
  110. case CURLINFO_DATA_OUT:
  111. case CURLINFO_DATA_IN:
  112. case CURLINFO_SSL_DATA_IN:
  113. case CURLINFO_SSL_DATA_OUT:
  114. if(!traced_data) {
  115. if(!newl)
  116. log_line_start(output, idsbuf, type);
  117. fprintf(output, "[%ld bytes data]\n", (long)size);
  118. newl = 0;
  119. traced_data = 1;
  120. }
  121. break;
  122. default: /* nada */
  123. newl = 0;
  124. traced_data = 1;
  125. break;
  126. }
  127. return 0;
  128. }
  129. static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *opaque)
  130. {
  131. (void)ptr;
  132. (void)opaque;
  133. return size * nmemb;
  134. }
  135. static void add_transfer(CURLM *multi, CURLSH *share,
  136. struct curl_slist *resolve,
  137. const char *url, int http_version)
  138. {
  139. CURL *easy;
  140. CURLMcode mc;
  141. easy = curl_easy_init();
  142. if(!easy) {
  143. fprintf(stderr, "curl_easy_init failed\n");
  144. exit(1);
  145. }
  146. curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L);
  147. curl_easy_setopt(easy, CURLOPT_DEBUGFUNCTION, debug_cb);
  148. curl_easy_setopt(easy, CURLOPT_URL, url);
  149. curl_easy_setopt(easy, CURLOPT_SHARE, share);
  150. curl_easy_setopt(easy, CURLOPT_NOSIGNAL, 1L);
  151. curl_easy_setopt(easy, CURLOPT_AUTOREFERER, 1L);
  152. curl_easy_setopt(easy, CURLOPT_FAILONERROR, 1L);
  153. curl_easy_setopt(easy, CURLOPT_HTTP_VERSION, http_version);
  154. curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, write_cb);
  155. curl_easy_setopt(easy, CURLOPT_WRITEDATA, NULL);
  156. curl_easy_setopt(easy, CURLOPT_HTTPGET, 1L);
  157. curl_easy_setopt(easy, CURLOPT_SSL_VERIFYPEER, 0L);
  158. if(resolve)
  159. curl_easy_setopt(easy, CURLOPT_RESOLVE, resolve);
  160. mc = curl_multi_add_handle(multi, easy);
  161. if(mc != CURLM_OK) {
  162. fprintf(stderr, "curl_multi_add_handle: %s\n",
  163. curl_multi_strerror(mc));
  164. exit(1);
  165. }
  166. }
  167. int main(int argc, char *argv[])
  168. {
  169. const char *url;
  170. CURLM *multi;
  171. CURLMcode mc;
  172. int running_handles = 0, numfds;
  173. CURLMsg *msg;
  174. CURLSH *share;
  175. CURLU *cu;
  176. struct curl_slist resolve;
  177. char resolve_buf[1024];
  178. int msgs_in_queue;
  179. int add_more, waits, ongoing = 0;
  180. char *host, *port;
  181. int http_version = CURL_HTTP_VERSION_1_1;
  182. if(argc != 3) {
  183. fprintf(stderr, "%s proto URL\n", argv[0]);
  184. exit(2);
  185. }
  186. if(!strcmp("h2", argv[1]))
  187. http_version = CURL_HTTP_VERSION_2;
  188. else if(!strcmp("h3", argv[1]))
  189. http_version = CURL_HTTP_VERSION_3ONLY;
  190. url = argv[2];
  191. cu = curl_url();
  192. if(!cu) {
  193. fprintf(stderr, "out of memory\n");
  194. exit(1);
  195. }
  196. if(curl_url_set(cu, CURLUPART_URL, url, 0)) {
  197. fprintf(stderr, "not a URL: '%s'\n", url);
  198. exit(1);
  199. }
  200. if(curl_url_get(cu, CURLUPART_HOST, &host, 0)) {
  201. fprintf(stderr, "could not get host of '%s'\n", url);
  202. exit(1);
  203. }
  204. if(curl_url_get(cu, CURLUPART_PORT, &port, 0)) {
  205. fprintf(stderr, "could not get port of '%s'\n", url);
  206. exit(1);
  207. }
  208. memset(&resolve, 0, sizeof(resolve));
  209. curl_msnprintf(resolve_buf, sizeof(resolve_buf)-1,
  210. "%s:%s:127.0.0.1", host, port);
  211. curl_slist_append(&resolve, resolve_buf);
  212. multi = curl_multi_init();
  213. if(!multi) {
  214. fprintf(stderr, "curl_multi_init failed\n");
  215. exit(1);
  216. }
  217. share = curl_share_init();
  218. if(!share) {
  219. fprintf(stderr, "curl_share_init failed\n");
  220. exit(1);
  221. }
  222. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
  223. add_transfer(multi, share, &resolve, url, http_version);
  224. ++ongoing;
  225. add_more = 6;
  226. waits = 3;
  227. do {
  228. mc = curl_multi_perform(multi, &running_handles);
  229. if(mc != CURLM_OK) {
  230. fprintf(stderr, "curl_multi_perform: %s\n",
  231. curl_multi_strerror(mc));
  232. exit(1);
  233. }
  234. if(running_handles) {
  235. mc = curl_multi_poll(multi, NULL, 0, 1000000, &numfds);
  236. if(mc != CURLM_OK) {
  237. fprintf(stderr, "curl_multi_poll: %s\n",
  238. curl_multi_strerror(mc));
  239. exit(1);
  240. }
  241. }
  242. if(waits) {
  243. --waits;
  244. }
  245. else {
  246. while(add_more) {
  247. add_transfer(multi, share, &resolve, url, http_version);
  248. ++ongoing;
  249. --add_more;
  250. }
  251. }
  252. /* Check for finished handles and remove. */
  253. while((msg = curl_multi_info_read(multi, &msgs_in_queue))) {
  254. if(msg->msg == CURLMSG_DONE) {
  255. long status = 0;
  256. curl_off_t xfer_id;
  257. curl_easy_getinfo(msg->easy_handle, CURLINFO_XFER_ID, &xfer_id);
  258. curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &status);
  259. if(msg->data.result == CURLE_SEND_ERROR ||
  260. msg->data.result == CURLE_RECV_ERROR) {
  261. /* We get these if the server had a GOAWAY in transit on
  262. * re-using a connection */
  263. }
  264. else if(msg->data.result) {
  265. fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T
  266. ": failed with %d\n", xfer_id, msg->data.result);
  267. exit(1);
  268. }
  269. else if(status != 200) {
  270. fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T
  271. ": wrong http status %ld (expected 200)\n", xfer_id, status);
  272. exit(1);
  273. }
  274. curl_multi_remove_handle(multi, msg->easy_handle);
  275. curl_easy_cleanup(msg->easy_handle);
  276. --ongoing;
  277. fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T" retiring "
  278. "(%d now running)\n", xfer_id, running_handles);
  279. }
  280. }
  281. fprintf(stderr, "running_handles=%d, yet_to_start=%d\n",
  282. running_handles, add_more);
  283. } while(ongoing || add_more);
  284. fprintf(stderr, "exiting\n");
  285. exit(EXIT_SUCCESS);
  286. }