ws-data.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. * Websockets data echos
  26. * </DESC>
  27. */
  28. /* curl stuff */
  29. #include "curl_setup.h"
  30. #include <curl/curl.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. /* somewhat unix-specific */
  35. #include <sys/time.h>
  36. #include <unistd.h>
  37. #ifdef USE_WEBSOCKETS
  38. static
  39. void dump(const char *text, unsigned char *ptr, size_t size,
  40. char nohex)
  41. {
  42. size_t i;
  43. size_t c;
  44. unsigned int width = 0x10;
  45. if(nohex)
  46. /* without the hex output, we can fit more on screen */
  47. width = 0x40;
  48. fprintf(stderr, "%s, %lu bytes (0x%lx)\n",
  49. text, (unsigned long)size, (unsigned long)size);
  50. for(i = 0; i<size; i += width) {
  51. fprintf(stderr, "%4.4lx: ", (unsigned long)i);
  52. if(!nohex) {
  53. /* hex not disabled, show it */
  54. for(c = 0; c < width; c++)
  55. if(i + c < size)
  56. fprintf(stderr, "%02x ", ptr[i + c]);
  57. else
  58. fputs(" ", stderr);
  59. }
  60. for(c = 0; (c < width) && (i + c < size); c++) {
  61. /* check for 0D0A; if found, skip past and start a new line of output */
  62. if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
  63. ptr[i + c + 1] == 0x0A) {
  64. i += (c + 2 - width);
  65. break;
  66. }
  67. fprintf(stderr, "%c",
  68. (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
  69. /* check again for 0D0A, to avoid an extra \n if it's at width */
  70. if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
  71. ptr[i + c + 2] == 0x0A) {
  72. i += (c + 3 - width);
  73. break;
  74. }
  75. }
  76. fputc('\n', stderr); /* newline */
  77. }
  78. }
  79. static CURLcode send_binary(CURL *curl, char *buf, size_t buflen)
  80. {
  81. size_t nwritten;
  82. CURLcode result =
  83. curl_ws_send(curl, buf, buflen, &nwritten, 0, CURLWS_BINARY);
  84. fprintf(stderr, "ws: send_binary(len=%ld) -> %d, %ld\n",
  85. (long)buflen, result, (long)nwritten);
  86. return result;
  87. }
  88. static CURLcode recv_binary(CURL *curl, char *exp_data, size_t exp_len)
  89. {
  90. const struct curl_ws_frame *frame;
  91. char recvbuf[256];
  92. size_t r_offset, nread;
  93. CURLcode result;
  94. fprintf(stderr, "recv_binary: expected payload %ld bytes\n", (long)exp_len);
  95. r_offset = 0;
  96. while(1) {
  97. result = curl_ws_recv(curl, recvbuf, sizeof(recvbuf), &nread, &frame);
  98. if(result == CURLE_AGAIN) {
  99. fprintf(stderr, "EAGAIN, sleep, try again\n");
  100. usleep(100*1000);
  101. continue;
  102. }
  103. fprintf(stderr, "ws: curl_ws_recv(offset=%ld, len=%ld) -> %d, %ld\n",
  104. (long)r_offset, (long)sizeof(recvbuf), result, (long)nread);
  105. if(result) {
  106. return result;
  107. }
  108. if(!(frame->flags & CURLWS_BINARY)) {
  109. fprintf(stderr, "recv_data: wrong frame, got %ld bytes rflags %x\n",
  110. (long)nread, frame->flags);
  111. return CURLE_RECV_ERROR;
  112. }
  113. if(frame->offset != (curl_off_t)r_offset) {
  114. fprintf(stderr, "recv_data: frame offset, expected %ld, got %ld\n",
  115. (long)r_offset, (long)frame->offset);
  116. return CURLE_RECV_ERROR;
  117. }
  118. if(frame->bytesleft != (curl_off_t)(exp_len - r_offset - nread)) {
  119. fprintf(stderr, "recv_data: frame bytesleft, expected %ld, got %ld\n",
  120. (long)(exp_len - r_offset - nread), (long)frame->bytesleft);
  121. return CURLE_RECV_ERROR;
  122. }
  123. if(r_offset + nread > exp_len) {
  124. fprintf(stderr, "recv_data: data length, expected %ld, now at %ld\n",
  125. (long)exp_len, (long)(r_offset + nread));
  126. return CURLE_RECV_ERROR;
  127. }
  128. if(memcmp(exp_data + r_offset, recvbuf, nread)) {
  129. fprintf(stderr, "recv_data: data differs, offset=%ld, len=%ld\n",
  130. (long)r_offset, (long)nread);
  131. dump("expected:", (unsigned char *)exp_data + r_offset, nread, 0);
  132. dump("received:", (unsigned char *)recvbuf, nread, 0);
  133. return CURLE_RECV_ERROR;
  134. }
  135. r_offset += nread;
  136. if(r_offset >= exp_len) {
  137. fprintf(stderr, "recv_data: frame complete\n");
  138. break;
  139. }
  140. }
  141. return CURLE_OK;
  142. }
  143. /* just close the connection */
  144. static void websocket_close(CURL *curl)
  145. {
  146. size_t sent;
  147. CURLcode result =
  148. curl_ws_send(curl, "", 0, &sent, 0, CURLWS_CLOSE);
  149. fprintf(stderr,
  150. "ws: curl_ws_send returned %u, sent %u\n", (int)result, (int)sent);
  151. }
  152. static CURLcode data_echo(CURL *curl, size_t plen_min, size_t plen_max)
  153. {
  154. CURLcode res;
  155. size_t len;
  156. char *send_buf;
  157. size_t i;
  158. send_buf = calloc(1, plen_max);
  159. if(!send_buf)
  160. return CURLE_OUT_OF_MEMORY;
  161. for(i = 0; i < plen_max; ++i) {
  162. send_buf[i] = (char)('0' + ((int)i % 10));
  163. }
  164. for(len = plen_min; len <= plen_max; ++len) {
  165. res = send_binary(curl, send_buf, len);
  166. if(res)
  167. goto out;
  168. res = recv_binary(curl, send_buf, len);
  169. if(res) {
  170. fprintf(stderr, "recv_data(len=%ld) -> %d\n", (long)len, res);
  171. goto out;
  172. }
  173. }
  174. out:
  175. if(!res)
  176. websocket_close(curl);
  177. free(send_buf);
  178. return res;
  179. }
  180. #endif
  181. int main(int argc, char *argv[])
  182. {
  183. #ifdef USE_WEBSOCKETS
  184. CURL *curl;
  185. CURLcode res = CURLE_OK;
  186. const char *url;
  187. long l1, l2;
  188. size_t plen_min, plen_max;
  189. if(argc != 4) {
  190. fprintf(stderr, "usage: ws-data url minlen maxlen\n");
  191. return 2;
  192. }
  193. url = argv[1];
  194. l1 = strtol(argv[2], NULL, 10);
  195. if(l1 < 0) {
  196. fprintf(stderr, "minlen must be >= 0, got %ld\n", l1);
  197. return 2;
  198. }
  199. l2 = strtol(argv[3], NULL, 10);
  200. if(l2 < 0) {
  201. fprintf(stderr, "maxlen must be >= 0, got %ld\n", l2);
  202. return 2;
  203. }
  204. plen_min = l1;
  205. plen_max = l2;
  206. if(plen_max < plen_min) {
  207. fprintf(stderr, "maxlen must be >= minlen, got %ld-%ld\n",
  208. (long)plen_min, (long)plen_max);
  209. return 2;
  210. }
  211. curl_global_init(CURL_GLOBAL_ALL);
  212. curl = curl_easy_init();
  213. if(curl) {
  214. curl_easy_setopt(curl, CURLOPT_URL, url);
  215. /* use the callback style */
  216. curl_easy_setopt(curl, CURLOPT_USERAGENT, "ws-data");
  217. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  218. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */
  219. res = curl_easy_perform(curl);
  220. fprintf(stderr, "curl_easy_perform() returned %u\n", (int)res);
  221. if(res == CURLE_OK)
  222. res = data_echo(curl, plen_min, plen_max);
  223. /* always cleanup */
  224. curl_easy_cleanup(curl);
  225. }
  226. curl_global_cleanup();
  227. return (int)res;
  228. #else /* USE_WEBSOCKETS */
  229. (void)argc;
  230. (void)argv;
  231. fprintf(stderr, "websockets not enabled in libcurl\n");
  232. return 1;
  233. #endif /* !USE_WEBSOCKETS */
  234. }