• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #endif
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdarg.h>
42 #include <sys/types.h>
43 #ifndef _WIN32
44 #include <unistd.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #endif
49 #include <usrsctp.h>
50 #include "programs_helper.h"
51 
52 #define BUFFERSIZE 10240
53 #define PORT 9
54 
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 			if (flags & MSG_NOTIFICATION) {
82 				printf("Notification of length %d received.\n", (int)n);
83 			} else {
84 /*
85 #ifdef _WIN32
86 				_write(_fileno(stdout), buf, (unsigned int)n);
87 #else
88 				if (write(fileno(stdout), buf, n) < 0) {
89 					perror("write");
90 				}
91 #endif
92 */
93 				switch (addr.ss_family) {
94 #ifdef INET
95 				case AF_INET: {
96 					struct sockaddr_in addr4;
97 					memcpy(&addr4, (struct sockaddr_in *)&addr, sizeof(struct sockaddr_in));
98 					name = inet_ntop(AF_INET, &addr4.sin_addr, namebuf, INET_ADDRSTRLEN);
99 					port = ntohs(addr4.sin_port);
100 					break;
101 					}
102 #endif
103 #ifdef INET6
104 				case AF_INET6: {
105 					struct sockaddr_in6 addr6;
106 					memcpy(&addr6, (struct sockaddr_in6 *)&addr, sizeof(struct sockaddr_in6));
107 					name = inet_ntop(AF_INET6, &addr6.sin6_addr, namebuf, INET6_ADDRSTRLEN),
108 					port = ntohs(addr6.sin6_port);
109 					break;
110 					}
111 #endif
112 				default:
113 					name = NULL;
114 					port = 0;
115 					break;
116 				}
117 
118 				if (name == NULL) {
119 					printf("inet_ntop failed\n");
120 					free(buf);
121 					return;
122 				}
123 
124 				printf("Msg of length %d received from %s:%u on stream %d with SSN %u and TSN %u, PPID %u, context %u.\n",
125 				       (int)n,
126 				       namebuf,
127 				       port,
128 				       rn.recvv_rcvinfo.rcv_sid,
129 				       rn.recvv_rcvinfo.rcv_ssn,
130 				       rn.recvv_rcvinfo.rcv_tsn,
131 				       ntohl(rn.recvv_rcvinfo.rcv_ppid),
132 				       rn.recvv_rcvinfo.rcv_context);
133 			}
134 		}
135 		free(buf);
136 	}
137 	return;
138 }
139 
140 int
main(int argc,char * argv[])141 main(int argc, char *argv[])
142 {
143 	struct socket *sock;
144 	struct sockaddr_in6 addr;
145 	struct sctp_udpencaps encaps;
146 	struct sctp_event event;
147 	uint16_t event_types[] = {SCTP_ASSOC_CHANGE,
148 	                          SCTP_PEER_ADDR_CHANGE,
149 	                          SCTP_REMOTE_ERROR,
150 	                          SCTP_SHUTDOWN_EVENT,
151 	                          SCTP_ADAPTATION_INDICATION,
152 	                          SCTP_PARTIAL_DELIVERY_EVENT};
153 	unsigned int i;
154 	struct sctp_assoc_value av;
155 	const int on = 1;
156 
157 	if (argc > 1) {
158 		usrsctp_init(atoi(argv[1]), NULL, debug_printf_stack);
159 	} else {
160 		usrsctp_init(9899, NULL, debug_printf_stack);
161 	}
162 #ifdef SCTP_DEBUG
163 	usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
164 #endif
165 	usrsctp_sysctl_set_sctp_blackhole(2);
166 	usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
167 
168 	if ((sock = usrsctp_socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP, NULL, NULL, 0, NULL)) == NULL) {
169 		perror("usrsctp_socket");
170 	}
171 	usrsctp_set_non_blocking(sock, 1);
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 
203 	usrsctp_set_upcall(sock, handle_upcall, NULL);
204 
205 	memset((void *)&addr, 0, sizeof(struct sockaddr_in6));
206 #ifdef HAVE_SIN6_LEN
207 	addr.sin6_len = sizeof(struct sockaddr_in6);
208 #endif
209 	addr.sin6_family = AF_INET6;
210 	addr.sin6_port = htons(PORT);
211 	addr.sin6_addr = in6addr_any;
212 	if (usrsctp_bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6)) < 0) {
213 		perror("usrsctp_bind");
214 	}
215 	if (usrsctp_listen(sock, 1) < 0) {
216 		perror("usrsctp_listen");
217 	}
218 	while (1) {
219 #ifdef _WIN32
220 			Sleep(1*1000);
221 #else
222 			sleep(1);
223 #endif
224 	}
225 	usrsctp_close(sock);
226 	while (usrsctp_finish() != 0) {
227 #ifdef _WIN32
228 		Sleep(1000);
229 #else
230 		sleep(1);
231 #endif
232 	}
233 	return (0);
234 }
235