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 				if (_snprintf(namebuf, INET6_ADDRSTRLEN, "%p", addr.sconn.sconn_addr) < 0) {
90 #else
91 				if (snprintf(namebuf, INET6_ADDRSTRLEN, "%p", addr.sconn.sconn_addr) < 0) {
92 #endif
93 					namebuf[0] = '\0';
94 				}
95 				name = namebuf;
96 				port = ntohs(addr.sconn.sconn_port);
97 				break;
98 			default:
99 				name = NULL;
100 				port = 0;
101 				break;
102 			}
103 			printf("Msg of length %d received from %s:%u on stream %u with SSN %u and TSN %u, PPID %u, context %u.\n",
104 			       (int)datalen,
105 			       name,
106 			       port,
107 			       rcv.rcv_sid,
108 			       rcv.rcv_ssn,
109 			       rcv.rcv_tsn,
110 			       ntohl(rcv.rcv_ppid),
111 			       rcv.rcv_context);
112 		}
113 		free(data);
114 	}
115 	return (1);
116 }
117 
118 int
119 main(int argc, char *argv[])
120 {
121 	struct socket *sock;
122 	struct sockaddr_in6 addr;
123 	struct sctp_udpencaps encaps;
124 	struct sctp_event event;
125 	uint16_t event_types[] = {SCTP_ASSOC_CHANGE,
126 	                          SCTP_PEER_ADDR_CHANGE,
127 	                          SCTP_REMOTE_ERROR,
128 	                          SCTP_SHUTDOWN_EVENT,
129 	                          SCTP_ADAPTATION_INDICATION,
130 	                          SCTP_PARTIAL_DELIVERY_EVENT};
131 	unsigned int i;
132 	struct sctp_assoc_value av;
133 	const int on = 1;
134 	ssize_t n;
135 	int flags;
136 	socklen_t from_len;
137 	char buffer[BUFFER_SIZE];
138 	char name[INET6_ADDRSTRLEN];
139 	socklen_t infolen;
140 	struct sctp_rcvinfo rcv_info;
141 	unsigned int infotype;
142 
143 	if (argc > 1) {
144 		usrsctp_init(atoi(argv[1]), NULL, debug_printf_stack);
145 	} else {
146 		usrsctp_init(9899, NULL, debug_printf_stack);
147 	}
148 #ifdef SCTP_DEBUG
149 	usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
150 #endif
151 	usrsctp_sysctl_set_sctp_blackhole(2);
152 	usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
153 
154 	if ((sock = usrsctp_socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP, use_cb?receive_cb:NULL, NULL, 0, NULL)) == NULL) {
155 		perror("usrsctp_socket");
156 	}
157 	if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_I_WANT_MAPPED_V4_ADDR, (const void*)&on, (socklen_t)sizeof(int)) < 0) {
158 		perror("usrsctp_setsockopt SCTP_I_WANT_MAPPED_V4_ADDR");
159 	}
160 	memset(&av, 0, sizeof(struct sctp_assoc_value));
161 	av.assoc_id = SCTP_ALL_ASSOC;
162 	av.assoc_value = 47;
163 
164 	if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_CONTEXT, (const void*)&av, (socklen_t)sizeof(struct sctp_assoc_value)) < 0) {
165 		perror("usrsctp_setsockopt SCTP_CONTEXT");
166 	}
167 	if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) < 0) {
168 		perror("usrsctp_setsockopt SCTP_RECVRCVINFO");
169 	}
170 	if (argc > 2) {
171 		memset(&encaps, 0, sizeof(struct sctp_udpencaps));
172 		encaps.sue_address.ss_family = AF_INET6;
173 		encaps.sue_port = htons(atoi(argv[2]));
174 		if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
175 			perror("usrsctp_setsockopt SCTP_REMOTE_UDP_ENCAPS_PORT");
176 		}
177 	}
178 	memset(&event, 0, sizeof(event));
179 	event.se_assoc_id = SCTP_FUTURE_ASSOC;
180 	event.se_on = 1;
181 	for (i = 0; i < (unsigned int)(sizeof(event_types)/sizeof(uint16_t)); i++) {
182 		event.se_type = event_types[i];
183 		if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event)) < 0) {
184 			perror("usrsctp_setsockopt SCTP_EVENT");
185 		}
186 	}
187 	memset((void *)&addr, 0, sizeof(struct sockaddr_in6));
188 #ifdef HAVE_SIN6_LEN
189 	addr.sin6_len = sizeof(struct sockaddr_in6);
190 #endif
191 	addr.sin6_family = AF_INET6;
192 	addr.sin6_port = htons(PORT);
193 	addr.sin6_addr = in6addr_any;
194 	if (usrsctp_bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6)) < 0) {
195 		perror("usrsctp_bind");
196 	}
197 	if (usrsctp_listen(sock, 1) < 0) {
198 		perror("usrsctp_listen");
199 	}
200 	while (1) {
201 		if (use_cb) {
202 #ifdef _WIN32
203 			Sleep(SLEEP * 1000);
204 #else
205 			sleep(SLEEP);
206 #endif
207 		} else {
208 			from_len = (socklen_t)sizeof(struct sockaddr_in6);
209 			flags = 0;
210 			infolen = (socklen_t)sizeof(struct sctp_rcvinfo);
211 			n = usrsctp_recvv(sock, (void*)buffer, BUFFER_SIZE, (struct sockaddr *) &addr, &from_len, (void *)&rcv_info,
212 			                  &infolen, &infotype, &flags);
213 			if (n > 0) {
214 				if (flags & MSG_NOTIFICATION) {
215 					printf("Notification of length %llu received.\n", (unsigned long long)n);
216 				} else {
217 					if (infotype == SCTP_RECVV_RCVINFO) {
218 						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",
219 						        (unsigned long long)n,
220 						        inet_ntop(AF_INET6, &addr.sin6_addr, name, INET6_ADDRSTRLEN), ntohs(addr.sin6_port),
221 						        rcv_info.rcv_sid,
222 						        rcv_info.rcv_ssn,
223 						        rcv_info.rcv_tsn,
224 						        ntohl(rcv_info.rcv_ppid),
225 						        rcv_info.rcv_context,
226 						        (flags & MSG_EOR) ? 1 : 0);
227 					} else {
228 						printf("Msg of length %llu received from %s:%u, complete %d.\n",
229 						        (unsigned long long)n,
230 						        inet_ntop(AF_INET6, &addr.sin6_addr, name, INET6_ADDRSTRLEN), ntohs(addr.sin6_port),
231 						        (flags & MSG_EOR) ? 1 : 0);
232 					}
233 				}
234 			} else {
235 				break;
236 			}
237 		}
238 	}
239 	usrsctp_close(sock);
240 	while (usrsctp_finish() != 0) {
241 #ifdef _WIN32
242 		Sleep(SLEEP * 1000);
243 #else
244 		sleep(SLEEP);
245 #endif
246 	}
247 	return (0);
248 }
249