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: discard_server_upcall [local_encaps_port] [remote_encaps_port]
33 */
34
35 #ifdef _WIN32
36 #define _CRT_SECURE_NO_WARNINGS
37 #include <io.h>
38 #endif
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <stdarg.h>
43 #include <sys/types.h>
44 #ifndef _WIN32
45 #include <unistd.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #endif
50 #include <usrsctp.h>
51 #include "programs_helper.h"
52
53 #define BUFFERSIZE 10240
54 #define PORT 7
55
56 static void
handle_upcall(struct socket * sock,void * data,int flgs)57 handle_upcall(struct socket *sock, void *data, int flgs)
58 {
59 char namebuf[INET6_ADDRSTRLEN];
60 const char *name;
61 uint16_t port;
62 char *buf;
63 int events;
64
65 while ((events = usrsctp_get_events(sock)) && (events & SCTP_EVENT_READ)) {
66 struct sctp_recvv_rn rn;
67 ssize_t n;
68 struct sockaddr_storage addr;
69 buf = malloc(BUFFERSIZE);
70 int flags = 0;
71 socklen_t len = (socklen_t)sizeof(struct sockaddr_storage);
72 unsigned int infotype = 0;
73 socklen_t infolen = sizeof(struct sctp_recvv_rn);
74 memset(&rn, 0, sizeof(struct sctp_recvv_rn));
75 n = usrsctp_recvv(sock, buf, BUFFERSIZE, (struct sockaddr *) &addr, &len, (void *)&rn,
76 &infolen, &infotype, &flags);
77 if (n < 0) {
78 perror("usrsctp_recvv");
79 }
80 if (n == 0) {
81 usrsctp_close(sock);
82 return;
83 }
84 if (n > 0) {
85 if (flags & MSG_NOTIFICATION) {
86 printf("Notification of length %d received.\n", (int)n);
87 } else {
88 #ifdef _WIN32
89 _write(_fileno(stdout), buf, (unsigned int)n);
90 #else
91 if (write(fileno(stdout), buf, n) < 0) {
92 perror("write");
93 }
94 #endif
95 switch (addr.ss_family) {
96 #ifdef INET
97 case AF_INET: {
98 struct sockaddr_in addr4;
99 memcpy(&addr4, (struct sockaddr_in *)&addr, sizeof(struct sockaddr_in));
100 name = inet_ntop(AF_INET, &addr4.sin_addr, namebuf, INET_ADDRSTRLEN);
101 port = ntohs(addr4.sin_port);
102 break;
103 }
104 #endif
105 #ifdef INET6
106 case AF_INET6: {
107 struct sockaddr_in6 addr6;
108 memcpy(&addr6, (struct sockaddr_in6 *)&addr, sizeof(struct sockaddr_in6));
109 name = inet_ntop(AF_INET6, &addr6.sin6_addr, namebuf, INET6_ADDRSTRLEN),
110 port = ntohs(addr6.sin6_port);
111 break;
112 }
113 #endif
114 default:
115 name = NULL;
116 port = 0;
117 break;
118 }
119
120 if (name == NULL) {
121 printf("inet_ntop failed\n");
122 free(buf);
123 return;
124 }
125
126 printf("Msg of length %d received from %s:%u on stream %d with SSN %u and TSN %u, PPID %u, context %u.\n",
127 (int)n,
128 namebuf,
129 port,
130 rn.recvv_rcvinfo.rcv_sid,
131 rn.recvv_rcvinfo.rcv_ssn,
132 rn.recvv_rcvinfo.rcv_tsn,
133 ntohl(rn.recvv_rcvinfo.rcv_ppid),
134 rn.recvv_rcvinfo.rcv_context);
135 if (flags & MSG_EOR) {
136 struct sctp_sndinfo snd_info;
137
138 snd_info.snd_sid = rn.recvv_rcvinfo.rcv_sid;
139 snd_info.snd_flags = 0;
140 if (rn.recvv_rcvinfo.rcv_flags & SCTP_UNORDERED) {
141 snd_info.snd_flags |= SCTP_UNORDERED;
142 }
143 snd_info.snd_ppid = rn.recvv_rcvinfo.rcv_ppid;
144 snd_info.snd_context = 0;
145 snd_info.snd_assoc_id = rn.recvv_rcvinfo.rcv_assoc_id;
146 if (usrsctp_sendv(sock, buf, (size_t) n, NULL, 0, &snd_info, sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
147 perror("sctp_sendv");
148 }
149 }
150 }
151 }
152 free(buf);
153 }
154 return;
155 }
156
157 int
main(int argc,char * argv[])158 main(int argc, char *argv[])
159 {
160 struct socket *sock;
161 struct sockaddr_in6 addr;
162 struct sctp_udpencaps encaps;
163 struct sctp_event event;
164 uint16_t event_types[] = {SCTP_ASSOC_CHANGE,
165 SCTP_PEER_ADDR_CHANGE,
166 SCTP_REMOTE_ERROR,
167 SCTP_SHUTDOWN_EVENT,
168 SCTP_ADAPTATION_INDICATION,
169 SCTP_PARTIAL_DELIVERY_EVENT};
170 unsigned int i;
171 struct sctp_assoc_value av;
172 const int on = 1;
173
174 if (argc > 1) {
175 usrsctp_init(atoi(argv[1]), NULL, debug_printf_stack);
176 } else {
177 usrsctp_init(9899, NULL, debug_printf_stack);
178 }
179 #ifdef SCTP_DEBUG
180 usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
181 #endif
182 usrsctp_sysctl_set_sctp_blackhole(2);
183 usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
184
185 if ((sock = usrsctp_socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP, NULL, NULL, 0, NULL)) == NULL) {
186 perror("usrsctp_socket");
187 }
188 usrsctp_set_non_blocking(sock, 1);
189 if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_I_WANT_MAPPED_V4_ADDR, (const void*)&on, (socklen_t)sizeof(int)) < 0) {
190 perror("usrsctp_setsockopt SCTP_I_WANT_MAPPED_V4_ADDR");
191 }
192 memset(&av, 0, sizeof(struct sctp_assoc_value));
193 av.assoc_id = SCTP_ALL_ASSOC;
194 av.assoc_value = 47;
195
196 if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_CONTEXT, (const void*)&av, (socklen_t)sizeof(struct sctp_assoc_value)) < 0) {
197 perror("usrsctp_setsockopt SCTP_CONTEXT");
198 }
199 if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) < 0) {
200 perror("usrsctp_setsockopt SCTP_RECVRCVINFO");
201 }
202 if (argc > 2) {
203 memset(&encaps, 0, sizeof(struct sctp_udpencaps));
204 encaps.sue_address.ss_family = AF_INET6;
205 encaps.sue_port = htons(atoi(argv[2]));
206 if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
207 perror("usrsctp_setsockopt SCTP_REMOTE_UDP_ENCAPS_PORT");
208 }
209 }
210 memset(&event, 0, sizeof(event));
211 event.se_assoc_id = SCTP_FUTURE_ASSOC;
212 event.se_on = 1;
213 for (i = 0; i < (unsigned int)(sizeof(event_types)/sizeof(uint16_t)); i++) {
214 event.se_type = event_types[i];
215 if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event)) < 0) {
216 perror("usrsctp_setsockopt SCTP_EVENT");
217 }
218 }
219
220 usrsctp_set_upcall(sock, handle_upcall, NULL);
221
222 memset((void *)&addr, 0, sizeof(struct sockaddr_in6));
223 #ifdef HAVE_SIN6_LEN
224 addr.sin6_len = sizeof(struct sockaddr_in6);
225 #endif
226 addr.sin6_family = AF_INET6;
227 addr.sin6_port = htons(PORT);
228 addr.sin6_addr = in6addr_any;
229 if (usrsctp_bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6)) < 0) {
230 perror("usrsctp_bind");
231 }
232 if (usrsctp_listen(sock, 1) < 0) {
233 perror("usrsctp_listen");
234 }
235 while (1) {
236 #ifdef _WIN32
237 Sleep(1*1000);
238 #else
239 sleep(1);
240 #endif
241 }
242 usrsctp_close(sock);
243 while (usrsctp_finish() != 0) {
244 #ifdef _WIN32
245 Sleep(1000);
246 #else
247 sleep(1);
248 #endif
249 }
250 return (0);
251 }
252