2
0

ws-data.c 7.7 KB

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