blob: 6f812175f110a4f43646ba87fe9e25337dfe8453 [file] [log] [blame]
James Kuszmaul4cb043c2021-01-17 11:25:51 -08001/*
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
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <sys/types.h>
39#include <stdarg.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
53#define MAX_PACKET_SIZE (1<<16)
54
55#ifdef _WIN32
56static DWORD WINAPI
57#else
58static void *
59#endif
60handle_packets(void *arg)
61{
62#ifdef _WIN32
63 SOCKET *fdp;
64#else
65 int *fdp;
66#endif
67 char buf[MAX_PACKET_SIZE];
68 char *dump_buf;
69 ssize_t length;
70
71#ifdef _WIN32
72 fdp = (SOCKET *)arg;
73#else
74 fdp = (int *)arg;
75#endif
76 for (;;) {
77#if defined(__NetBSD__)
78 pthread_testcancel();
79#endif
80 length = recv(*fdp, buf, MAX_PACKET_SIZE, 0);
81 if (length > 0) {
82 if ((dump_buf = usrsctp_dumppacket(buf, (size_t)length, SCTP_DUMP_INBOUND)) != NULL) {
83 fprintf(stderr, "%s", dump_buf);
84 usrsctp_freedumpbuffer(dump_buf);
85 }
86 usrsctp_conninput(fdp, buf, (size_t)length, 0);
87 }
88 }
89#ifdef _WIN32
90 return 0;
91#else
92 return (NULL);
93#endif
94}
95
96static int
97conn_output(void *addr, void *buf, size_t length, uint8_t tos, uint8_t set_df)
98{
99 char *dump_buf;
100#ifdef _WIN32
101 SOCKET *fdp;
102#else
103 int *fdp;
104#endif
105
106#ifdef _WIN32
107 fdp = (SOCKET *)addr;
108#else
109 fdp = (int *)addr;
110#endif
111 if ((dump_buf = usrsctp_dumppacket(buf, length, SCTP_DUMP_OUTBOUND)) != NULL) {
112 fprintf(stderr, "%s", dump_buf);
113 usrsctp_freedumpbuffer(dump_buf);
114 }
115#ifdef _WIN32
116 if (send(*fdp, buf, (int)length, 0) == SOCKET_ERROR) {
117 return (WSAGetLastError());
118#else
119 if (send(*fdp, buf, length, 0) < 0) {
120 return (errno);
121#endif
122 } else {
123 return (0);
124 }
125}
126
127static int
128receive_cb(struct socket *s, union sctp_sockstore addr, void *data,
129 size_t datalen, struct sctp_rcvinfo rcv, int flags, void *ulp_info)
130{
131
132 if (data) {
133 if (flags & MSG_NOTIFICATION) {
134 printf("Notification of length %d received.\n", (int)datalen);
135 } else {
136 printf("Msg of length %d received via %p:%u on stream %d with SSN %u and TSN %u, PPID %u, context %u.\n",
137 (int)datalen,
138 addr.sconn.sconn_addr,
139 ntohs(addr.sconn.sconn_port),
140 rcv.rcv_sid,
141 rcv.rcv_ssn,
142 rcv.rcv_tsn,
143 ntohl(rcv.rcv_ppid),
144 rcv.rcv_context);
145 }
146 free(data);
147 } else {
148 usrsctp_close(s);
149 }
150 return (1);
151}
152
153void
154debug_printf(const char *format, ...)
155{
156 va_list ap;
157
158 va_start(ap, format);
159 vprintf(format, ap);
160 va_end(ap);
161}
162
163int
164main(int argc, char *argv[])
165{
166 struct sockaddr_in sin;
167 struct sockaddr_conn sconn;
168#ifdef _WIN32
169 SOCKET fd;
170#else
171 int fd;
172#endif
173 struct socket *s;
174#ifdef _WIN32
175 HANDLE tid;
176#else
177 pthread_t tid;
178#endif
179#ifdef _WIN32
180 WSADATA wsaData;
181#endif
182
183 if (argc < 4) {
184 printf("error: this program requires 4 arguments!\n");
185 exit(EXIT_FAILURE);
186 }
187
188#ifdef _WIN32
189 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
190 printf("WSAStartup failed\n");
191 exit (EXIT_FAILURE);
192 }
193#endif
194 usrsctp_init(0, conn_output, debug_printf);
195 /* set up a connected UDP socket */
196#ifdef _WIN32
197 if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {
198 printf("socket() failed with error: %d\n", WSAGetLastError());
199 }
200#else
201 if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
202 perror("socket");
203 }
204#endif
205 memset(&sin, 0, sizeof(struct sockaddr_in));
206 sin.sin_family = AF_INET;
207#ifdef HAVE_SIN_LEN
208 sin.sin_len = sizeof(struct sockaddr_in);
209#endif
210 sin.sin_port = htons(atoi(argv[2]));
211 if (!inet_pton(AF_INET, argv[1], &sin.sin_addr.s_addr)){
212 printf("error: invalid address\n");
213 exit(1);
214 }
215#ifdef _WIN32
216 if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
217 printf("bind() failed with error: %d\n", WSAGetLastError());
218 }
219#else
220 if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
221 perror("bind");
222 }
223#endif
224 memset(&sin, 0, sizeof(struct sockaddr_in));
225 sin.sin_family = AF_INET;
226#ifdef HAVE_SIN_LEN
227 sin.sin_len = sizeof(struct sockaddr_in);
228#endif
229 sin.sin_port = htons(atoi(argv[4]));
230 if (!inet_pton(AF_INET, argv[3], &sin.sin_addr.s_addr)){
231 printf("error: invalid address\n");
232 exit(1);
233 }
234#ifdef _WIN32
235 if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
236 printf("connect() failed with error: %d\n", WSAGetLastError());
237 }
238#else
239 if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
240 perror("connect");
241 }
242#endif
243#ifdef SCTP_DEBUG
244 usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
245#endif
246 usrsctp_sysctl_set_sctp_ecn_enable(0);
247 usrsctp_register_address((void *)&fd);
248#ifdef _WIN32
249 tid = CreateThread(NULL, 0, &handle_packets, (void *)&fd, 0, NULL);
250#else
251 pthread_create(&tid, NULL, &handle_packets, (void *)&fd);
252#endif
253 if ((s = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, receive_cb, NULL, 0, NULL)) == NULL) {
254 perror("usrsctp_socket");
255 }
256 memset(&sconn, 0, sizeof(struct sockaddr_conn));
257 sconn.sconn_family = AF_CONN;
258#ifdef HAVE_SCONN_LEN
259 sconn.sconn_len = sizeof(struct sockaddr_conn);
260#endif
261 sconn.sconn_port = htons(5001);
262 sconn.sconn_addr = (void *)&fd;
263 if (usrsctp_bind(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)) < 0) {
264 perror("usrsctp_bind");
265 }
266 if (usrsctp_listen(s, 1) < 0) {
267 perror("usrsctp_listen");
268 }
269 while (1) {
270 if (usrsctp_accept(s, NULL, NULL) == NULL) {
271 perror("usrsctp_accept");
272 }
273 }
274 usrsctp_close(s);
275 usrsctp_deregister_address((void *)&fd);
276 while (usrsctp_finish() != 0) {
277#ifdef _WIN32
278 Sleep(1000);
279#else
280 sleep(1);
281#endif
282 }
283#ifdef _WIN32
284 TerminateThread(tid, 0);
285 WaitForSingleObject(tid, INFINITE);
286 if (closesocket(fd) == SOCKET_ERROR) {
287 printf("closesocket() failed with error: %d\n", WSAGetLastError());
288 }
289 WSACleanup();
290#else
291 pthread_cancel(tid);
292 pthread_join(tid, NULL);
293 if (close(fd) < 0) {
294 perror("close");
295 }
296#endif
297 return (0);
298}