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