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