1 /*
2 * Copyright (C) 2011-2013 Michael Tuexen
3 * Copyright (C) 2011-2015 Colin Caughie
4 * Copyright (C) 2011-2019 Felix Weinrank
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Usage: st_client local_addr local_port remote_addr remote_port remote_sctp_port
35 */
36
37 #ifdef _WIN32
38 #define _CRT_SECURE_NO_WARNINGS
39 #endif
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <stdarg.h>
44 #include <sys/types.h>
45 #ifndef _WIN32
46 #include <sys/socket.h>
47 #include <sys/time.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <errno.h>
51 #include <pthread.h>
52 #include <unistd.h>
53 #else
54 #include <winsock2.h>
55 #include <ws2tcpip.h>
56 #endif
57 #include <usrsctp.h>
58 #include "programs_helper.h"
59
60 #define MAX_PACKET_SIZE (1<<16)
61 #define BUFFER_SIZE 80
62 #define DISCARD_PPID 39
63 #define HTTP_PPID 63
64
65 #define TIMER_INTERVAL_MSECS 10
66
67 static int connecting = 0;
68 static int finish = 0;
69
70 static unsigned int
get_milliseconds_count(void)71 get_milliseconds_count(void)
72 {
73 #ifdef _WIN32
74 // obtain number of milliseconds since system started
75 return GetTickCount();
76 #else
77 struct timeval tv;
78 unsigned int milliseconds;
79
80 gettimeofday(&tv, NULL); /* get current time */
81 milliseconds = tv.tv_sec*1000LL + tv.tv_usec/1000; /* calculate milliseconds */
82 return (milliseconds);
83 #endif
84 }
85
86 static void
handle_events(int sock,struct socket * s,void * sconn_addr)87 handle_events(int sock, struct socket* s, void* sconn_addr)
88 {
89 char *dump_buf;
90 ssize_t length;
91 char buf[MAX_PACKET_SIZE];
92
93 fd_set rfds;
94 struct timeval tv;
95
96 unsigned next_fire_time = get_milliseconds_count();
97 unsigned last_fire_time = next_fire_time;
98 unsigned now = get_milliseconds_count();
99 int wait_time;
100
101 while (!finish) {
102 if ((int) (now - next_fire_time) > 0) {
103 usrsctp_handle_timers(now - last_fire_time);
104 last_fire_time = now;
105 next_fire_time = now + TIMER_INTERVAL_MSECS;
106 }
107
108 wait_time = next_fire_time - now;
109 tv.tv_sec = wait_time / 1000;
110 tv.tv_usec = (wait_time % 1000) * 1000;
111
112 FD_ZERO(&rfds);
113 FD_SET(sock, &rfds);
114
115 select(sock + 1, &rfds, NULL, NULL, &tv);
116
117 if (FD_ISSET(sock, &rfds)) {
118 length = recv(sock, buf, MAX_PACKET_SIZE, 0);
119
120 if (length > 0) {
121 if ((dump_buf = usrsctp_dumppacket(buf, (size_t)length, SCTP_DUMP_INBOUND)) != NULL) {
122 fprintf(stderr, "%s", dump_buf);
123 usrsctp_freedumpbuffer(dump_buf);
124 }
125 usrsctp_conninput(sconn_addr, buf, (size_t)length, 0);
126 }
127 }
128 }
129 }
130
131 static void
on_connect(struct socket * s)132 on_connect(struct socket* s)
133 {
134 struct sctp_sndinfo sndinfo;
135 char buffer[BUFFER_SIZE];
136 int bufferlen;
137
138 /* memset(buffer, 'A', BUFFER_SIZE); */
139 /* bufferlen = BUFFER_SIZE; */
140 bufferlen = snprintf(buffer, BUFFER_SIZE, "GET / HTTP/1.0\r\nUser-agent: libusrsctp\r\nConnection: close\r\n\r\n");
141 if (bufferlen < 0) {
142 return;
143 }
144 sndinfo.snd_sid = 0;
145 sndinfo.snd_flags = 0;
146 sndinfo.snd_ppid = htonl(DISCARD_PPID);
147 sndinfo.snd_context = 0;
148 sndinfo.snd_assoc_id = 0;
149 if (usrsctp_sendv(s, buffer, bufferlen, NULL, 0, (void *)&sndinfo,
150 (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
151 perror("usrsctp_sendv");
152 }
153 }
154
155 static void
on_socket_readable(struct socket * s)156 on_socket_readable(struct socket* s) {
157 char buffer[BUFFER_SIZE];
158 union sctp_sockstore addr;
159 socklen_t fromlen = sizeof(addr);
160 struct sctp_rcvinfo rcv_info;
161 socklen_t infolen = sizeof(rcv_info);
162 unsigned int infotype = 0;
163 int flags = 0;
164 ssize_t retval;
165
166 /* Keep reading until there is no more data */
167 for (;;) {
168 retval = usrsctp_recvv(s, buffer, sizeof(buffer), (struct sockaddr*) &addr,
169 &fromlen, &rcv_info, &infolen, &infotype, &flags);
170
171 if (retval < 0) {
172 if (errno != EWOULDBLOCK) {
173 perror("usrsctp_recvv");
174 finish = 1;
175 }
176 return;
177 } else if (retval == 0) {
178 printf("socket was disconnected\n");
179 finish = 1;
180 return;
181 }
182
183 if (flags & MSG_NOTIFICATION) {
184 printf("Notification of length %d received.\n", (int)retval);
185 } else {
186 printf("Msg of length %d received via %p:%u on stream %d with SSN %u and TSN %u, PPID %u, context %u.\n",
187 (int)retval,
188 addr.sconn.sconn_addr,
189 ntohs(addr.sconn.sconn_port),
190 rcv_info.rcv_sid,
191 rcv_info.rcv_ssn,
192 rcv_info.rcv_tsn,
193 ntohl(rcv_info.rcv_ppid),
194 rcv_info.rcv_context);
195 }
196 }
197 }
198
199 static void
handle_upcall(struct socket * s,void * arg,int flags)200 handle_upcall(struct socket *s, void *arg, int flags)
201 {
202 int events = usrsctp_get_events(s);
203
204 if (connecting) {
205 if (events & SCTP_EVENT_ERROR) {
206 connecting = 0;
207 finish = 1;
208 } else if (events & SCTP_EVENT_WRITE) {
209 connecting = 0;
210 on_connect(s);
211 }
212
213 return;
214 }
215
216 if (events & SCTP_EVENT_READ) {
217 on_socket_readable(s);
218 }
219 }
220
221 static int
conn_output(void * addr,void * buf,size_t length,uint8_t tos,uint8_t set_df)222 conn_output(void *addr, void *buf, size_t length, uint8_t tos, uint8_t set_df)
223 {
224 char *dump_buf;
225 #ifdef _WIN32
226 SOCKET *fdp;
227 #else
228 int *fdp;
229 #endif
230
231 #ifdef _WIN32
232 fdp = (SOCKET *)addr;
233 #else
234 fdp = (int *)addr;
235 #endif
236 if ((dump_buf = usrsctp_dumppacket(buf, length, SCTP_DUMP_OUTBOUND)) != NULL) {
237 fprintf(stderr, "%s", dump_buf);
238 usrsctp_freedumpbuffer(dump_buf);
239 }
240 #ifdef _WIN32
241 if (send(*fdp, buf, (int)length, 0) == SOCKET_ERROR) {
242 return (WSAGetLastError());
243 #else
244 if (send(*fdp, buf, length, 0) < 0) {
245 return (errno);
246 #endif
247 } else {
248 return (0);
249 }
250 }
251
252 /* Usage: st_client local_addr local_port remote_addr remote_port remote_sctp_port */
253 int
254 main(int argc, char *argv[])
255 {
256 struct sockaddr_in sin;
257 struct sockaddr_conn sconn;
258 #ifdef _WIN32
259 SOCKET fd;
260 #else
261 int fd;
262 #endif
263 struct socket *s;
264 int retval;
265 #ifdef _WIN32
266 WSADATA wsaData;
267 #endif
268 const int on = 1;
269
270 if (argc < 6) {
271 printf("Usage: st_client local_addr local_port remote_addr remote_port remote_sctp_port\n");
272 return (-1);
273 }
274
275 #ifdef _WIN32
276 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
277 printf("WSAStartup failed\n");
278 exit(EXIT_FAILURE);
279 }
280 #endif
281 usrsctp_init_nothreads(0, conn_output, debug_printf_stack);
282 /* set up a connected UDP socket */
283 #ifdef _WIN32
284 if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {
285 printf("socket() failed with error: %d\n", WSAGetLastError());
286 exit(EXIT_FAILURE);
287 }
288 #else
289 if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
290 perror("socket");
291 exit(EXIT_FAILURE);
292 }
293 #endif
294 memset(&sin, 0, sizeof(struct sockaddr_in));
295 sin.sin_family = AF_INET;
296 #ifdef HAVE_SIN_LEN
297 sin.sin_len = sizeof(struct sockaddr_in);
298 #endif
299 sin.sin_port = htons(atoi(argv[2]));
300 if (!inet_pton(AF_INET, argv[1], &sin.sin_addr.s_addr)){
301 printf("error: invalid address\n");
302 exit(EXIT_FAILURE);
303 }
304 #ifdef _WIN32
305 if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
306 printf("bind() failed with error: %d\n", WSAGetLastError());
307 exit(EXIT_FAILURE);
308 }
309 #else
310 if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
311 perror("bind");
312 exit(EXIT_FAILURE);
313 }
314 #endif
315 memset(&sin, 0, sizeof(struct sockaddr_in));
316 sin.sin_family = AF_INET;
317 #ifdef HAVE_SIN_LEN
318 sin.sin_len = sizeof(struct sockaddr_in);
319 #endif
320 sin.sin_port = htons(atoi(argv[4]));
321 if (!inet_pton(AF_INET, argv[3], &sin.sin_addr.s_addr)){
322 printf("error: invalid address\n");
323 exit(EXIT_FAILURE);
324 }
325 #ifdef _WIN32
326 if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
327 printf("connect() failed with error: %d\n", WSAGetLastError());
328 exit(EXIT_FAILURE);
329 }
330 #else
331 if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
332 perror("connect");
333 exit(EXIT_FAILURE);
334 }
335 #endif
336 #ifdef SCTP_DEBUG
337 usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
338 #endif
339 usrsctp_sysctl_set_sctp_ecn_enable(0);
340 usrsctp_register_address((void *)&fd);
341
342 if ((s = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL)) == NULL) {
343 perror("usrsctp_socket");
344 exit(EXIT_FAILURE);
345 }
346
347 usrsctp_setsockopt(s, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int));
348 usrsctp_set_non_blocking(s, 1);
349 usrsctp_set_upcall(s, handle_upcall, NULL);
350
351 memset(&sconn, 0, sizeof(struct sockaddr_conn));
352 sconn.sconn_family = AF_CONN;
353 #ifdef HAVE_SCONN_LEN
354 sconn.sconn_len = sizeof(struct sockaddr_conn);
355 #endif
356 sconn.sconn_port = htons(0);
357 sconn.sconn_addr = NULL;
358 if (usrsctp_bind(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)) < 0) {
359 perror("usrsctp_bind");
360 exit(EXIT_FAILURE);
361 }
362
363 memset(&sconn, 0, sizeof(struct sockaddr_conn));
364 sconn.sconn_family = AF_CONN;
365 #ifdef HAVE_SCONN_LEN
366 sconn.sconn_len = sizeof(struct sockaddr_conn);
367 #endif
368 sconn.sconn_port = htons(atoi(argv[5]));
369 sconn.sconn_addr = &fd;
370
371 retval = usrsctp_connect(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn));
372
373 if (retval < 0 && errno != EWOULDBLOCK && errno != EINPROGRESS) {
374 perror("usrsctp_connect");
375 exit(EXIT_FAILURE);
376 }
377
378 connecting = 1;
379
380 handle_events(fd, s, sconn.sconn_addr);
381
382 return (0);
383 }
384