1 /*
2  * Copyright (C) 2012-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: chargen_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 <sys/socket.h>
45 #include <unistd.h>
46 #include <time.h>
47 #include <errno.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #endif
51 #include <usrsctp.h>
52 #include "programs_helper.h"
53 
54 #define BUFFERSIZE 10240
55 #define PORT 19
56 
57 char buffer[95];
58 int done = 0;
59 int send_done = 0;
60 
61 static void
initBuffer(void)62 initBuffer(void) {
63 	int i, j;
64 	for (i = 32, j = 0; i < 126; i++, j++) {
65 		buffer[j] = i;
66 	}
67 }
68 
69 unsigned int signCounter = 0;
70 static void
71 handle_upcall(struct socket *upcall_socket, void *upcall_data, int upcall_flags);
72 
73 static void
handle_accept(struct socket * upcall_socket,void * upcall_data,int upcall_flags)74 handle_accept(struct socket *upcall_socket, void *upcall_data, int upcall_flags)
75 {
76 	struct socket *conn_sock;
77 
78 	if (((conn_sock = usrsctp_accept(upcall_socket, NULL, NULL)) == NULL)
79 	    && (errno != EINPROGRESS)) {
80 		perror("usrsctp_accept");
81 		return;
82 	}
83 	done = 0;
84 	printf("connection accepted from socket %p\n", (void *)conn_sock);
85 	usrsctp_set_upcall(conn_sock, handle_upcall, NULL);
86 }
87 
88 static void
handle_upcall(struct socket * upcall_socket,void * upcall_data,int upcall_flags)89 handle_upcall(struct socket *upcall_socket, void *upcall_data, int upcall_flags)
90 {
91 	int events = usrsctp_get_events(upcall_socket);
92 
93 	if (events & SCTP_EVENT_READ && !send_done) {
94 		char *buf;
95 		struct sctp_recvv_rn rn;
96 		ssize_t n;
97 		struct sockaddr_storage addr;
98 		buf = malloc(BUFFERSIZE);
99 		int recv_flags = 0;
100 		socklen_t len = (socklen_t)sizeof(struct sockaddr_storage);
101 		unsigned int infotype = 0;
102 		socklen_t infolen = sizeof(struct sctp_recvv_rn);
103 		memset(&rn, 0, sizeof(struct sctp_recvv_rn));
104 
105 		n = usrsctp_recvv(upcall_socket, buf, BUFFERSIZE, (struct sockaddr *) &addr, &len, (void *)&rn,
106 				 &infolen, &infotype, &recv_flags);
107 		if (n < 0) {
108 			perror("usrsctp_recvv");
109 			done = 1;
110 			usrsctp_close(upcall_socket);
111 			printf("client socket %p closed\n", (void *)upcall_socket);
112 			upcall_socket = NULL;
113 			return;
114 		}
115 		if (n == 0) {
116 			done = 1;
117 			usrsctp_close(upcall_socket);
118 			printf("client socket %p closed\n", (void *)upcall_socket);
119 			upcall_socket = NULL;
120 			return;
121 		}
122 		if (n > 0) {
123 			if (recv_flags & MSG_NOTIFICATION) {
124 				printf("Notification of length %d received.\n", (int)n);
125 			} else {
126 				printf("data of size %d received\n", (int)n);
127 			}
128 		}
129 		free(buf);
130 	}
131 
132 	if ((events & SCTP_EVENT_WRITE) && !done) {
133 		struct sctp_sndinfo snd_info;
134 		snd_info.snd_sid = 0;
135 		snd_info.snd_flags = 0;
136 		snd_info.snd_ppid = 0;
137 		snd_info.snd_context = 0;
138 		snd_info.snd_assoc_id = 0;
139 		if (usrsctp_sendv(upcall_socket, buffer, strlen(buffer), NULL, 0, &snd_info, (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
140 			if (errno != EAGAIN && errno != EWOULDBLOCK) {
141 				send_done = 1;
142 				usrsctp_close(upcall_socket);
143 				printf("client socket %p closed\n", (void *)upcall_socket);
144 				return;
145 			}
146 		}
147 	}
148 
149 	return;
150 }
151 
152 int
main(int argc,char * argv[])153 main(int argc, char *argv[])
154 {
155 	struct socket *listening_socket;
156 	struct sockaddr_in6 addr;
157 	struct sctp_udpencaps encaps;
158 	struct sctp_assoc_value av;
159 	const int on = 1;
160 
161 	if (argc > 1) {
162 		usrsctp_init(atoi(argv[1]), NULL, debug_printf_stack);
163 	} else {
164 		usrsctp_init(9899, NULL, debug_printf_stack);
165 	}
166 #ifdef SCTP_DEBUG
167 	usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
168 #endif
169 	usrsctp_sysctl_set_sctp_blackhole(2);
170 	usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
171 
172 	if ((listening_socket = usrsctp_socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL)) == NULL) {
173 		perror("usrsctp_socket");
174 	}
175 	usrsctp_set_non_blocking(listening_socket, 1);
176 	if (usrsctp_setsockopt(listening_socket, IPPROTO_SCTP, SCTP_I_WANT_MAPPED_V4_ADDR, (const void*)&on, (socklen_t)sizeof(int)) < 0) {
177 		perror("usrsctp_setsockopt SCTP_I_WANT_MAPPED_V4_ADDR");
178 	}
179 	memset(&av, 0, sizeof(struct sctp_assoc_value));
180 	av.assoc_id = SCTP_ALL_ASSOC;
181 	av.assoc_value = 47;
182 
183 	if (usrsctp_setsockopt(listening_socket, IPPROTO_SCTP, SCTP_CONTEXT, (const void*)&av, (socklen_t)sizeof(struct sctp_assoc_value)) < 0) {
184 		perror("usrsctp_setsockopt SCTP_CONTEXT");
185 	}
186 	if (usrsctp_setsockopt(listening_socket, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) < 0) {
187 		perror("usrsctp_setsockopt SCTP_RECVRCVINFO");
188 	}
189 	if (argc > 2) {
190 		memset(&encaps, 0, sizeof(struct sctp_udpencaps));
191 		encaps.sue_address.ss_family = AF_INET6;
192 		encaps.sue_port = htons(atoi(argv[2]));
193 		if (usrsctp_setsockopt(listening_socket, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
194 			perror("usrsctp_setsockopt SCTP_REMOTE_UDP_ENCAPS_PORT");
195 		}
196 	}
197 
198 	initBuffer();
199 
200 	memset((void *)&addr, 0, sizeof(struct sockaddr_in6));
201 #ifdef HAVE_SIN6_LEN
202 	addr.sin6_len = sizeof(struct sockaddr_in6);
203 #endif
204 	addr.sin6_family = AF_INET6;
205 	addr.sin6_port = htons(PORT);
206 	addr.sin6_addr = in6addr_any;
207 	if (usrsctp_bind(listening_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6)) < 0) {
208 		perror("usrsctp_bind");
209 	}
210 	if (usrsctp_listen(listening_socket, 1) < 0) {
211 		perror("usrsctp_listen");
212 	}
213 	usrsctp_set_upcall(listening_socket, handle_accept, NULL);
214 
215 	while (1) {
216 #ifdef _WIN32
217 		Sleep(1*1000);
218 #else
219 		sleep(1);
220 #endif
221 	}
222 	return (0);
223 }
224