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 /*
32 * Usage: client_upcall remote_addr remote_port [local_port] [local_encaps_port] [remote_encaps_port]
33 */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdarg.h>
39
40 #if !defined(_WIN32)
41 #include <unistd.h>
42 #include <sys/time.h>
43 #endif /* !defined(_WIN32) */
44
45 #include <sys/types.h>
46
47 #if !defined(_WIN32)
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
51 #else /* !defined(_WIN32) */
52 #include <io.h>
53 #endif
54
55 #include <usrsctp.h>
56 #include "programs_helper.h"
57 #include <fcntl.h>
58
59 #define BUFFERSIZE (1<<16)
60
61 int done = 0, input_done = 0, connected = 0;
62
63 #ifdef _WIN32
64 typedef char* caddr_t;
65 #endif
66
inputAvailable(void)67 int inputAvailable(void)
68 {
69 struct timeval tv;
70 fd_set fds;
71 tv.tv_sec = 0;
72 tv.tv_usec = 0;
73 FD_ZERO(&fds);
74 #if defined(_WIN32) && !defined(__MINGW32__)
75 FD_SET(_fileno(stdin), &fds);
76 select(_fileno(stdin) + 1, &fds, NULL, NULL, &tv);
77 #else
78 FD_SET(STDIN_FILENO, &fds);
79 select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
80 #endif
81 return (FD_ISSET(0, &fds));
82 }
83
84 static void
handle_upcall(struct socket * sock,void * arg,int flgs)85 handle_upcall(struct socket *sock, void *arg, int flgs)
86 {
87 int events = usrsctp_get_events(sock);
88
89 if (events & SCTP_EVENT_WRITE && !done && !connected) {
90 connected = 1;
91 printf("socket connected\n");
92 return;
93 }
94
95 while (events & SCTP_EVENT_READ && !done && connected) {
96 struct sctp_recvv_rn rn;
97 ssize_t n;
98 struct sockaddr_in addr;
99 char *buf = calloc(1, BUFFERSIZE);
100 int flags = 0;
101 socklen_t len = (socklen_t)sizeof(struct sockaddr_in);
102 unsigned int infotype = 0;
103 socklen_t infolen = sizeof(struct sctp_recvv_rn);
104 memset(&rn, 0, sizeof(struct sctp_recvv_rn));
105 n = usrsctp_recvv(sock, buf, BUFFERSIZE, (struct sockaddr *) &addr, &len, (void *)&rn,
106 &infolen, &infotype, &flags);
107
108 if (n > 0) {
109 #ifdef _WIN32
110 _write(_fileno(stdout), buf, (unsigned int)n);
111 #else
112 if (write(fileno(stdout), buf, n) < 0) {
113 perror("write");
114 }
115 #endif
116 } else if (n == 0) {
117 done = 1;
118 input_done = 1;
119 free(buf);
120 break;
121 } else {
122 perror("\nusrsctp_recvv");
123 free (buf);
124 break;
125 }
126 free(buf);
127 events = usrsctp_get_events(sock);
128 }
129 return;
130 }
131
132 int
main(int argc,char * argv[])133 main(int argc, char *argv[])
134 {
135 struct socket *sock;
136 struct sockaddr *addr, *addrs;
137 struct sockaddr_in addr4;
138 struct sockaddr_in6 addr6;
139 struct sctp_udpencaps encaps;
140 struct sctpstat stat;
141 char buffer[200];
142 int i, n;
143
144 if (argc > 4) {
145 usrsctp_init(atoi(argv[4]), NULL, debug_printf_stack);
146 } else {
147 usrsctp_init(9899, NULL, debug_printf_stack);
148 }
149 #ifdef SCTP_DEBUG
150 usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
151 #endif
152 usrsctp_sysctl_set_sctp_blackhole(2);
153 usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
154
155 if ((sock = usrsctp_socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL)) == NULL) {
156 perror("usrsctp_socket");
157 exit(1);
158 }
159
160 usrsctp_set_non_blocking(sock, 1);
161
162 if (argc > 3) {
163 memset((void *)&addr6, 0, sizeof(struct sockaddr_in6));
164 #ifdef HAVE_SIN6_LEN
165 addr6.sin6_len = sizeof(struct sockaddr_in6);
166 #endif
167 addr6.sin6_family = AF_INET6;
168 addr6.sin6_port = htons(atoi(argv[3]));
169 addr6.sin6_addr = in6addr_any;
170 if (usrsctp_bind(sock, (struct sockaddr *)&addr6, sizeof(struct sockaddr_in6)) < 0) {
171 perror("bind");
172 usrsctp_close(sock);
173 exit(1);
174 }
175 }
176 if (argc > 5) {
177 memset(&encaps, 0, sizeof(struct sctp_udpencaps));
178 encaps.sue_address.ss_family = AF_INET6;
179 encaps.sue_port = htons(atoi(argv[5]));
180 if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
181 perror("setsockopt");
182 usrsctp_close(sock);
183 exit(1);
184 }
185 }
186
187 memset((void *)&addr4, 0, sizeof(struct sockaddr_in));
188 memset((void *)&addr6, 0, sizeof(struct sockaddr_in6));
189 #ifdef HAVE_SIN_LEN
190 addr4.sin_len = sizeof(struct sockaddr_in);
191 #endif
192 #ifdef HAVE_SIN6_LEN
193 addr6.sin6_len = sizeof(struct sockaddr_in6);
194 #endif
195 addr4.sin_family = AF_INET;
196 addr6.sin6_family = AF_INET6;
197 addr4.sin_port = htons(atoi(argv[2]));
198 addr6.sin6_port = htons(atoi(argv[2]));
199 if (inet_pton(AF_INET6, argv[1], &addr6.sin6_addr) == 1) {
200 if (usrsctp_connect(sock, (struct sockaddr *)&addr6, sizeof(struct sockaddr_in6)) < 0) {
201 perror("usrsctp_connect");
202 }
203 } else if (inet_pton(AF_INET, argv[1], &addr4.sin_addr) == 1) {
204 if (usrsctp_connect(sock, (struct sockaddr *)&addr4, sizeof(struct sockaddr_in)) < 0) {
205 perror("usrsctp_connect");
206 }
207 } else {
208 printf("Illegal destination address.\n");
209 }
210
211 usrsctp_set_upcall(sock, handle_upcall, NULL);
212 if ((n = usrsctp_getladdrs(sock, 0, &addrs)) < 0) {
213 perror("usrsctp_getladdrs");
214 } else {
215 addr = addrs;
216 printf("Local addresses: ");
217 for (i = 0; i < n; i++) {
218 if (i > 0) {
219 printf("%s", ", ");
220 }
221 switch (addr->sa_family) {
222 case AF_INET:
223 {
224 struct sockaddr_in *sin;
225 char buf[INET_ADDRSTRLEN];
226 const char *name;
227
228 sin = (struct sockaddr_in *)addr;
229 name = inet_ntop(AF_INET, &sin->sin_addr, buf, INET_ADDRSTRLEN);
230 printf("%s", name);
231 #ifndef HAVE_SA_LEN
232 addr = (struct sockaddr *)((caddr_t)addr + sizeof(struct sockaddr_in));
233 #endif
234 break;
235 }
236 case AF_INET6:
237 {
238 struct sockaddr_in6 *sin6;
239 char buf[INET6_ADDRSTRLEN];
240 const char *name;
241
242 sin6 = (struct sockaddr_in6 *)addr;
243 name = inet_ntop(AF_INET6, &sin6->sin6_addr, buf, INET6_ADDRSTRLEN);
244 printf("%s", name);
245 #ifndef HAVE_SA_LEN
246 addr = (struct sockaddr *)((caddr_t)addr + sizeof(struct sockaddr_in6));
247 #endif
248 break;
249 }
250 default:
251 break;
252 }
253 #ifdef HAVE_SA_LEN
254 addr = (struct sockaddr *)((caddr_t)addr + addr->sa_len);
255 #endif
256 }
257 printf(".\n");
258 usrsctp_freeladdrs(addrs);
259 }
260 if ((n = usrsctp_getpaddrs(sock, 0, &addrs)) < 0) {
261 perror("usrsctp_getpaddrs");
262 } else {
263 addr = addrs;
264 printf("Peer addresses: ");
265 for (i = 0; i < n; i++) {
266 if (i > 0) {
267 printf("%s", ", ");
268 }
269 switch (addr->sa_family) {
270 case AF_INET:
271 {
272 struct sockaddr_in *sin;
273 char buf[INET_ADDRSTRLEN];
274 const char *name;
275
276 sin = (struct sockaddr_in *)addr;
277 name = inet_ntop(AF_INET, &sin->sin_addr, buf, INET_ADDRSTRLEN);
278 printf("%s", name);
279 #ifndef HAVE_SA_LEN
280 addr = (struct sockaddr *)((caddr_t)addr + sizeof(struct sockaddr_in));
281 #endif
282 break;
283 }
284 case AF_INET6:
285 {
286 struct sockaddr_in6 *sin6;
287 char buf[INET6_ADDRSTRLEN];
288 const char *name;
289
290 sin6 = (struct sockaddr_in6 *)addr;
291 name = inet_ntop(AF_INET6, &sin6->sin6_addr, buf, INET6_ADDRSTRLEN);
292 printf("%s", name);
293 #ifndef HAVE_SA_LEN
294 addr = (struct sockaddr *)((caddr_t)addr + sizeof(struct sockaddr_in6));
295 #endif
296 break;
297 }
298 default:
299 break;
300 }
301 #ifdef HAVE_SA_LEN
302 addr = (struct sockaddr *)((caddr_t)addr + addr->sa_len);
303 #endif
304 }
305 printf(".\n");
306 usrsctp_freepaddrs(addrs);
307 }
308 while (!done && !input_done) {
309 if (inputAvailable()) {
310 if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
311 buffer[strlen(buffer)] = '\0';
312 usrsctp_sendv(sock, buffer, strlen(buffer), NULL, 0, NULL, 0, SCTP_SENDV_NOINFO, 0);
313 } else {
314 if (usrsctp_shutdown(sock, SHUT_WR) < 0) {
315 perror("usrsctp_shutdown");
316 }
317 break;
318 }
319 }
320 }
321 #ifdef _WIN32
322 Sleep(1000);
323 #else
324 sleep(1);
325 #endif
326 usrsctp_close(sock);
327
328 usrsctp_get_stat(&stat);
329 printf("Number of packets (sent/received): (%u/%u).\n",
330 stat.sctps_outpackets, stat.sctps_inpackets);
331 while (usrsctp_finish() != 0) {
332 #ifdef _WIN32
333 Sleep(1000);
334 #else
335 sleep(1);
336 #endif
337 }
338 printf("Client finished\n");
339 return(0);
340 }
341