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: echo_server [local_encaps_port] [remote_encaps_port]
33  *
34  * Example
35  * Server: $ ./echo_server 11111 22222
36  * Client: $ ./client 127.0.0.1 7 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 7
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 = "???";
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 			       (uint32_t)ntohl(rcv.rcv_ppid),
111 			       rcv.rcv_context);
112 			if (flags & MSG_EOR) {
113 				struct sctp_sndinfo snd_info;
114 
115 				snd_info.snd_sid = rcv.rcv_sid;
116 				snd_info.snd_flags = 0;
117 				if (rcv.rcv_flags & SCTP_UNORDERED) {
118 					snd_info.snd_flags |= SCTP_UNORDERED;
119 				}
120 				snd_info.snd_ppid = rcv.rcv_ppid;
121 				snd_info.snd_context = 0;
122 				snd_info.snd_assoc_id = rcv.rcv_assoc_id;
123 				if (usrsctp_sendv(sock, data, datalen, NULL, 0, &snd_info, sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
124 					perror("sctp_sendv");
125 				}
126 			}
127 		}
128 		free(data);
129 	}
130 	return (1);
131 }
132 
133 int
134 main(int argc, char *argv[])
135 {
136 	struct socket *sock;
137 	struct sockaddr_in6 addr;
138 	struct sctp_udpencaps encaps;
139 	struct sctp_event event;
140 	uint16_t event_types[] = {SCTP_ASSOC_CHANGE,
141 	                          SCTP_PEER_ADDR_CHANGE,
142 	                          SCTP_REMOTE_ERROR,
143 	                          SCTP_SHUTDOWN_EVENT,
144 	                          SCTP_ADAPTATION_INDICATION,
145 	                          SCTP_PARTIAL_DELIVERY_EVENT};
146 	unsigned int i;
147 	struct sctp_assoc_value av;
148 	const int on = 1;
149 	ssize_t n;
150 	int flags;
151 	socklen_t from_len;
152 	char buffer[BUFFER_SIZE];
153 	char name[INET6_ADDRSTRLEN];
154 	socklen_t infolen;
155 	struct sctp_rcvinfo rcv_info;
156 	unsigned int infotype;
157 
158 	if (argc > 1) {
159 		usrsctp_init(atoi(argv[1]), NULL, debug_printf_stack);
160 	} else {
161 		usrsctp_init(9899, NULL, debug_printf_stack);
162 	}
163 #ifdef SCTP_DEBUG
164 	usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
165 #endif
166 	usrsctp_sysctl_set_sctp_blackhole(2);
167 	usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
168 
169 	if ((sock = usrsctp_socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP, use_cb?receive_cb:NULL, NULL, 0, NULL)) == NULL) {
170 		perror("usrsctp_socket");
171 	}
172 	if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_I_WANT_MAPPED_V4_ADDR, (const void*)&on, (socklen_t)sizeof(int)) < 0) {
173 		perror("usrsctp_setsockopt SCTP_I_WANT_MAPPED_V4_ADDR");
174 	}
175 	memset(&av, 0, sizeof(struct sctp_assoc_value));
176 	av.assoc_id = SCTP_ALL_ASSOC;
177 	av.assoc_value = 47;
178 
179 	if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_CONTEXT, (const void*)&av, (socklen_t)sizeof(struct sctp_assoc_value)) < 0) {
180 		perror("usrsctp_setsockopt SCTP_CONTEXT");
181 	}
182 	if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) < 0) {
183 		perror("usrsctp_setsockopt SCTP_RECVRCVINFO");
184 	}
185 	if (argc > 2) {
186 		memset(&encaps, 0, sizeof(struct sctp_udpencaps));
187 		encaps.sue_address.ss_family = AF_INET6;
188 		encaps.sue_port = htons(atoi(argv[2]));
189 		if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
190 			perror("usrsctp_setsockopt SCTP_REMOTE_UDP_ENCAPS_PORT");
191 		}
192 	}
193 	memset(&event, 0, sizeof(event));
194 	event.se_assoc_id = SCTP_FUTURE_ASSOC;
195 	event.se_on = 1;
196 	for (i = 0; i < (unsigned int)(sizeof(event_types)/sizeof(uint16_t)); i++) {
197 		event.se_type = event_types[i];
198 		if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event)) < 0) {
199 			perror("usrsctp_setsockopt SCTP_EVENT");
200 		}
201 	}
202 	memset((void *)&addr, 0, sizeof(struct sockaddr_in6));
203 #ifdef HAVE_SIN6_LEN
204 	addr.sin6_len = sizeof(struct sockaddr_in6);
205 #endif
206 	addr.sin6_family = AF_INET6;
207 	addr.sin6_port = htons(PORT);
208 	addr.sin6_addr = in6addr_any;
209 	if (usrsctp_bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6)) < 0) {
210 		perror("usrsctp_bind");
211 	}
212 	if (usrsctp_listen(sock, 1) < 0) {
213 		perror("usrsctp_listen");
214 	}
215 	while (1) {
216 		if (use_cb) {
217 #ifdef _WIN32
218 		Sleep(SLEEP * 1000);
219 #else
220 		sleep(SLEEP);
221 #endif
222 		} else {
223 			from_len = (socklen_t)sizeof(struct sockaddr_in6);
224 			flags = 0;
225 			infolen = (socklen_t)sizeof(struct sctp_rcvinfo);
226 			n = usrsctp_recvv(sock, (void*)buffer, BUFFER_SIZE, (struct sockaddr *) &addr, &from_len, (void *)&rcv_info,
227 			                  &infolen, &infotype, &flags);
228 			if (n > 0) {
229 				if (flags & MSG_NOTIFICATION) {
230 					printf("Notification of length %llu received.\n", (unsigned long long)n);
231 				} else {
232 					if (infotype == SCTP_RECVV_RCVINFO) {
233 						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",
234 						        (unsigned long long)n,
235 						        inet_ntop(AF_INET6, &addr.sin6_addr, name, INET6_ADDRSTRLEN), ntohs(addr.sin6_port),
236 						        rcv_info.rcv_sid,
237 						        rcv_info.rcv_ssn,
238 						        rcv_info.rcv_tsn,
239 						        (uint32_t)ntohl(rcv_info.rcv_ppid),
240 						        rcv_info.rcv_context,
241 						        (flags & MSG_EOR) ? 1 : 0);
242 						if (flags & MSG_EOR) {
243 							struct sctp_sndinfo snd_info;
244 
245 							snd_info.snd_sid = rcv_info.rcv_sid;
246 							snd_info.snd_flags = 0;
247 							if (rcv_info.rcv_flags & SCTP_UNORDERED) {
248 								snd_info.snd_flags |= SCTP_UNORDERED;
249 							}
250 							snd_info.snd_ppid = rcv_info.rcv_ppid;
251 							snd_info.snd_context = 0;
252 							snd_info.snd_assoc_id = rcv_info.rcv_assoc_id;
253 							if (usrsctp_sendv(sock, buffer, (size_t)n, NULL, 0, &snd_info, (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
254 								perror("sctp_sendv");
255 							}
256 						}
257 					} else {
258 						printf("Msg of length %llu received from %s:%u, complete %d.\n",
259 						        (unsigned long long)n,
260 						        inet_ntop(AF_INET6, &addr.sin6_addr, name, INET6_ADDRSTRLEN), ntohs(addr.sin6_port),
261 						        (flags & MSG_EOR) ? 1 : 0);
262 					}
263 				}
264 			} else {
265 				break;
266 			}
267 		}
268 	}
269 	usrsctp_close(sock);
270 	while (usrsctp_finish() != 0) {
271 #ifdef _WIN32
272 		Sleep(SLEEP * 1000);
273 #else
274 		sleep(SLEEP);
275 #endif
276 	}
277 	return (0);
278 }
279