1 /*
2 * Copyright (C) 2011-2013 Michael Tuexen
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the project nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #ifdef _WIN32
32 #define _CRT_SECURE_NO_WARNINGS
33 #endif
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdarg.h>
39 #include <sys/types.h>
40 #ifndef _WIN32
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <errno.h>
45 #include <pthread.h>
46 #include <unistd.h>
47 #else
48 #include <winsock2.h>
49 #include <ws2tcpip.h>
50 #endif
51 #include <usrsctp.h>
52 #include "programs_helper.h"
53
54 #define MAX_PACKET_SIZE (1<<16)
55 #define LINE_LENGTH 80
56 #define DISCARD_PPID 39
57
58 #ifdef _WIN32
59 static DWORD WINAPI
60 #else
61 static void *
62 #endif
handle_packets(void * arg)63 handle_packets(void *arg)
64 {
65 #ifdef _WIN32
66 SOCKET *fdp;
67 #else
68 int *fdp;
69 #endif
70 ssize_t length;
71 char buf[MAX_PACKET_SIZE];
72
73 #ifdef _WIN32
74 fdp = (SOCKET *)arg;
75 #else
76 fdp = (int *)arg;
77 #endif
78 for (;;) {
79 #if defined(__NetBSD__)
80 pthread_testcancel();
81 #endif
82 length = recv(*fdp, buf, MAX_PACKET_SIZE, 0);
83 if (length > 0) {
84 usrsctp_conninput(fdp, buf, (size_t)length, 0);
85 }
86 }
87 #ifdef _WIN32
88 return 0;
89 #else
90 return (NULL);
91 #endif
92 }
93
94 static int
conn_output(void * addr,void * buffer,size_t length,uint8_t tos,uint8_t set_df)95 conn_output(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df)
96 {
97 #ifdef _WIN32
98 SOCKET *fdp;
99 #else
100 int *fdp;
101 #endif
102
103 #ifdef _WIN32
104 fdp = (SOCKET *)addr;
105 #else
106 fdp = (int *)addr;
107 #endif
108 #ifdef _WIN32
109 if (send(*fdp, buffer, (int)length, 0) == SOCKET_ERROR) {
110 return (WSAGetLastError());
111 #else
112 if (send(*fdp, buffer, length, 0) < 0) {
113 return (errno);
114 #endif
115 } else {
116 return (0);
117 }
118 }
119
120 static int
121 receive_cb(struct socket *sock, union sctp_sockstore addr, void *data,
122 size_t datalen, struct sctp_rcvinfo rcv, int flags, void *ulp_info)
123 {
124 if (data) {
125 if (flags & MSG_NOTIFICATION) {
126 handle_notification((union sctp_notification *)data, datalen);
127 } else {
128 printf("Msg of length %d received via %p:%u on stream %u with SSN %u and TSN %u, PPID %u, context %u.\n",
129 (int)datalen,
130 addr.sconn.sconn_addr,
131 ntohs(addr.sconn.sconn_port),
132 rcv.rcv_sid,
133 rcv.rcv_ssn,
134 rcv.rcv_tsn,
135 ntohl(rcv.rcv_ppid),
136 rcv.rcv_context);
137 }
138 free(data);
139 } else {
140 usrsctp_deregister_address(ulp_info);
141 usrsctp_close(sock);
142 }
143 return (1);
144 }
145
146 int
147 main(int argc, char *argv[])
148 {
149 struct sockaddr_in sin;
150 struct sockaddr_conn sconn;
151 struct sctp_event event;
152 uint16_t event_types[] = {SCTP_ASSOC_CHANGE,
153 SCTP_PEER_ADDR_CHANGE,
154 SCTP_SEND_FAILED_EVENT};
155 unsigned int i;
156 #ifdef _WIN32
157 SOCKET fd;
158 #else
159 int fd, rc;
160 #endif
161 struct socket *s;
162 #ifdef _WIN32
163 HANDLE tid;
164 #else
165 pthread_t tid;
166 #endif
167 struct sctp_sndinfo sndinfo;
168 char line[LINE_LENGTH];
169 #ifdef _WIN32
170 WSADATA wsaData;
171 #endif
172
173 if (argc < 4) {
174 printf("error: this program requires 4 arguments!\n");
175 exit(EXIT_FAILURE);
176 }
177
178 #ifdef _WIN32
179 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
180 fprintf(stderr, "WSAStartup failed\n");
181 exit(EXIT_FAILURE);
182 }
183 #endif
184 usrsctp_init(0, conn_output, debug_printf_stack);
185 /* set up a connected UDP socket */
186 #ifdef _WIN32
187 if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {
188 fprintf(stderr, "socket() failed with error: %d\n", WSAGetLastError());
189 exit(EXIT_FAILURE);
190 }
191 #else
192 if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
193 perror("socket");
194 exit(EXIT_FAILURE);
195 }
196 #endif
197 memset(&sin, 0, sizeof(struct sockaddr_in));
198 sin.sin_family = AF_INET;
199 #ifdef HAVE_SIN_LEN
200 sin.sin_len = sizeof(struct sockaddr_in);
201 #endif
202 sin.sin_port = htons(atoi(argv[2]));
203 if (!inet_pton(AF_INET, argv[1], &sin.sin_addr.s_addr)){
204 printf("error: invalid address\n");
205 exit(EXIT_FAILURE);
206 }
207 #ifdef _WIN32
208 if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
209 fprintf(stderr, "bind() failed with error: %d\n", WSAGetLastError());
210 exit(EXIT_FAILURE);
211 }
212 #else
213 if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
214 perror("bind");
215 exit(EXIT_FAILURE);
216 }
217 #endif
218 memset(&sin, 0, sizeof(struct sockaddr_in));
219 sin.sin_family = AF_INET;
220 #ifdef HAVE_SIN_LEN
221 sin.sin_len = sizeof(struct sockaddr_in);
222 #endif
223 sin.sin_port = htons(atoi(argv[4]));
224 if (!inet_pton(AF_INET, argv[3], &sin.sin_addr.s_addr)){
225 printf("error: invalid address\n");
226 exit(EXIT_FAILURE);
227 }
228 #ifdef _WIN32
229 if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
230 fprintf(stderr, "connect() failed with error: %d\n", WSAGetLastError());
231 exit(EXIT_FAILURE);
232 }
233 #else
234 if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
235 perror("connect");
236 exit(EXIT_FAILURE);
237 }
238 #endif
239 #ifdef _WIN32
240 if ((tid = CreateThread(NULL, 0, &handle_packets, (void *)&fd, 0, NULL)) == NULL) {
241 fprintf(stderr, "CreateThread() failed with error: %d\n", GetLastError());
242 exit(EXIT_FAILURE);
243 }
244 #else
245 if ((rc = pthread_create(&tid, NULL, &handle_packets, (void *)&fd)) != 0) {
246 fprintf(stderr, "pthread_create: %s\n", strerror(rc));
247 exit(EXIT_FAILURE);
248 }
249 #endif
250 #ifdef SCTP_DEBUG
251 usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
252 #endif
253 usrsctp_register_address((void *)&fd);
254 usrsctp_sysctl_set_sctp_ecn_enable(0);
255 if ((s = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, receive_cb, NULL, 0, &fd)) == NULL) {
256 perror("usrsctp_socket");
257 }
258 /* Enable the events of interest. */
259 if (usrsctp_set_non_blocking(s, 1) < 0) {
260 perror("usrsctp_set_non_blocking");
261 }
262 memset(&event, 0, sizeof(event));
263 event.se_assoc_id = SCTP_ALL_ASSOC;
264 event.se_on = 1;
265 for (i = 0; i < sizeof(event_types)/sizeof(uint16_t); i++) {
266 event.se_type = event_types[i];
267 if (usrsctp_setsockopt(s, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(event)) < 0) {
268 perror("setsockopt SCTP_EVENT");
269 }
270 }
271 memset(&sconn, 0, sizeof(struct sockaddr_conn));
272 sconn.sconn_family = AF_CONN;
273 #ifdef HAVE_SCONN_LEN
274 sconn.sconn_len = sizeof(struct sockaddr_conn);
275 #endif
276 sconn.sconn_port = htons(atoi(argv[5]));
277 sconn.sconn_addr = &fd;
278 if (usrsctp_bind(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)) < 0) {
279 perror("usrsctp_bind");
280 }
281 memset(&sconn, 0, sizeof(struct sockaddr_conn));
282 sconn.sconn_family = AF_CONN;
283 #ifdef HAVE_SCONN_LEN
284 sconn.sconn_len = sizeof(struct sockaddr_conn);
285 #endif
286 sconn.sconn_port = htons(atoi(argv[6]));
287 sconn.sconn_addr = &fd;
288 if (usrsctp_connect(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)) < 0) {
289 perror("usrsctp_connect");
290 }
291 for (;;) {
292 #if defined(_WIN32) && !defined(__MINGW32__)
293 if (gets_s(line, LINE_LENGTH) == NULL) {
294 #else
295 if (fgets(line, LINE_LENGTH, stdin) == NULL) {
296 #endif
297 if (usrsctp_shutdown(s, SHUT_WR) < 0) {
298 perror("usrsctp_shutdown");
299 }
300 while (usrsctp_finish() != 0) {
301 #ifdef _WIN32
302 Sleep(1000);
303 #else
304 sleep(1);
305 #endif
306 }
307 break;
308 }
309 sndinfo.snd_sid = 1;
310 sndinfo.snd_flags = 0;
311 sndinfo.snd_ppid = htonl(DISCARD_PPID);
312 sndinfo.snd_context = 0;
313 sndinfo.snd_assoc_id = 0;
314 if (usrsctp_sendv(s, line, strlen(line), NULL, 0, (void *)&sndinfo,
315 (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
316 perror("usrsctp_sendv");
317 }
318 }
319 while (usrsctp_finish() != 0) {
320 #ifdef _WIN32
321 Sleep(1000);
322 #else
323 sleep(1);
324 #endif
325 }
326 #ifdef _WIN32
327 TerminateThread(tid, 0);
328 WaitForSingleObject(tid, INFINITE);
329 if (closesocket(fd) == SOCKET_ERROR) {
330 fprintf(stderr, "closesocket() failed with error: %d\n", WSAGetLastError());
331 }
332 WSACleanup();
333 #else
334 pthread_cancel(tid);
335 pthread_join(tid, NULL);
336 if (close(fd) < 0) {
337 perror("close");
338 }
339 #endif
340 return (0);
341 }
342