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