1 /*
2  * Copyright (C) 2016-2019 Felix Weinrank
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: http_client_upcall remote_addr remote_port [local_port] [local_encaps_port] [remote_encaps_port] [uri]
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 #ifndef _WIN32
43 #include <unistd.h>
44 #endif
45 #include <sys/types.h>
46 #ifndef _WIN32
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <sys/time.h>
51 #else
52 #include <sys/types.h>
53 #include <sys/timeb.h>
54 #include <io.h>
55 #endif
56 #include <usrsctp.h>
57 #include "programs_helper.h"
58 
59 
60 #define RETVAL_CATCHALL     50
61 #define RETVAL_TIMEOUT      60
62 #define RETVAL_ECONNREFUSED 61
63 
64 int done = 0;
65 int writePending = 1;
66 int result = 0;
67 
68 static const char *request_prefix = "GET";
69 static const char *request_postfix = "HTTP/1.0\r\nUser-agent: libusrsctp\r\nConnection: close\r\n\r\n";
70 char request[512];
71 
72 #ifdef _WIN32
73 typedef char* caddr_t;
74 #endif
75 
76 #define BUFFERSIZE (1<<16)
77 
handle_upcall(struct socket * sock,void * arg,int flgs)78 static void handle_upcall(struct socket *sock, void *arg, int flgs)
79 {
80 	int events = usrsctp_get_events(sock);
81 	ssize_t bytesSent = 0;
82 	char *buf;
83 
84 	if ((events & SCTP_EVENT_READ) && !done) {
85 		struct sctp_recvv_rn rn;
86 		ssize_t n;
87 		struct sockaddr_in addr;
88 		buf = malloc(BUFFERSIZE);
89 		int flags = 0;
90 		socklen_t len = (socklen_t)sizeof(struct sockaddr_in);
91 		unsigned int infotype = 0;
92 		socklen_t infolen = sizeof(struct sctp_recvv_rn);
93 
94 		memset(&rn, 0, sizeof(struct sctp_recvv_rn));
95 		n = usrsctp_recvv(sock, buf, BUFFERSIZE, (struct sockaddr *) &addr, &len, (void *)&rn,
96 	                 &infolen, &infotype, &flags);
97 
98 		if (n < 0) {
99 			if (errno == ECONNREFUSED) {
100 				result = RETVAL_ECONNREFUSED;
101 			} else if (errno == ETIMEDOUT) {
102 				result = RETVAL_TIMEOUT;
103 			} else {
104 				result = RETVAL_CATCHALL;
105 			}
106 			perror("usrsctp_connect");
107 		}
108 
109 		if (n <= 0){
110 			done = 1;
111 			usrsctp_close(sock);
112 		} else {
113 #ifdef _WIN32
114 			_write(_fileno(stdout), buf, (unsigned int)n);
115 #else
116 			if (write(fileno(stdout), buf, n) < 0) {
117 				perror("write");
118 			}
119 #endif
120 		}
121 		free(buf);
122 	}
123 
124 	if ((events & SCTP_EVENT_WRITE) && writePending && !done) {
125 		writePending = 0;
126 		printf("\nHTTP request:\n%s\n", request);
127 		printf("\nHTTP response:\n");
128 
129 		/* send GET request */
130 		bytesSent = usrsctp_sendv(sock, request, strlen(request), NULL, 0, NULL, 0, SCTP_SENDV_NOINFO, 0);
131 		if (bytesSent < 0) {
132 			perror("usrsctp_sendv");
133 			usrsctp_close(sock);
134 		} else {
135 			printf("%d bytes sent\n", (int)bytesSent);
136 		}
137 	}
138 }
139 
140 int
main(int argc,char * argv[])141 main(int argc, char *argv[])
142 {
143 	struct socket *sock;
144 	struct sockaddr *addr;
145 	socklen_t addr_len;
146 	struct sockaddr_in addr4;
147 	struct sockaddr_in6 addr6;
148 	struct sockaddr_in bind4;
149 	struct sockaddr_in6 bind6;
150 	struct sctp_udpencaps encaps;
151 	struct sctp_rtoinfo rtoinfo;
152 	struct sctp_initmsg initmsg;
153 	uint8_t address_family = 0;
154 
155 	if (argc < 3) {
156 		printf("Usage: http_client_upcall remote_addr remote_port [local_port] [local_encaps_port] [remote_encaps_port] [uri]\n");
157 		return(EXIT_FAILURE);
158 	}
159 
160 	memset((void *)&addr4, 0, sizeof(struct sockaddr_in));
161 	memset((void *)&addr6, 0, sizeof(struct sockaddr_in6));
162 
163 	if (inet_pton(AF_INET, argv[1], &addr4.sin_addr) == 1) {
164 		address_family = AF_INET;
165 
166 		addr = (struct sockaddr *)&addr4;
167 		addr_len = sizeof(addr4);
168 #ifdef HAVE_SIN_LEN
169 		addr4.sin_len = sizeof(struct sockaddr_in);
170 #endif
171 		addr4.sin_family = AF_INET;
172 		addr4.sin_port = htons(atoi(argv[2]));
173 	} else if (inet_pton(AF_INET6, argv[1], &addr6.sin6_addr) == 1) {
174 		address_family = AF_INET6;
175 
176 		addr = (struct sockaddr *)&addr6;
177 		addr_len = sizeof(addr6);
178 #ifdef HAVE_SIN6_LEN
179 		addr6.sin6_len = sizeof(struct sockaddr_in6);
180 #endif
181 		addr6.sin6_family = AF_INET6;
182 		addr6.sin6_port = htons(atoi(argv[2]));
183 	} else {
184 		printf("Unsupported destination address - use IPv4 or IPv6 address\n");
185 		result = RETVAL_CATCHALL;
186 		goto out;
187 	}
188 
189 	if (argc > 4) {
190 		usrsctp_init(atoi(argv[4]), NULL, debug_printf_stack);
191 	} else {
192 		usrsctp_init(9899, NULL, debug_printf_stack);
193 	}
194 
195 #ifdef SCTP_DEBUG
196 	usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
197 #endif
198 
199 	usrsctp_sysctl_set_sctp_blackhole(2);
200 	usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
201 
202 	if ((sock = usrsctp_socket(address_family, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL)) == NULL) {
203 		perror("usrsctp_socket");
204 		result = RETVAL_CATCHALL;
205 		goto out;
206 	}
207 
208 	/* usrsctp_set_non_blocking(sock, 1); */
209 
210 	rtoinfo.srto_assoc_id = 0;
211 	rtoinfo.srto_initial = 1000;
212 	rtoinfo.srto_min = 1000;
213 	rtoinfo.srto_max = 8000;
214 	if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_RTOINFO, (const void *)&rtoinfo, (socklen_t)sizeof(struct sctp_rtoinfo)) < 0) {
215 		perror("setsockopt");
216 		usrsctp_close(sock);
217 		result = RETVAL_CATCHALL;
218 		goto out;
219 	}
220 	initmsg.sinit_num_ostreams = 1;
221 	initmsg.sinit_max_instreams = 1;
222 	initmsg.sinit_max_attempts = 5;
223 	initmsg.sinit_max_init_timeo = 4000;
224 	if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_INITMSG, (const void *)&initmsg, (socklen_t)sizeof(struct sctp_initmsg)) < 0) {
225 		perror("setsockopt");
226 		usrsctp_close(sock);
227 		result = RETVAL_CATCHALL;
228 		goto out;
229 	}
230 
231 	if (argc > 3) {
232 
233 		if (address_family == AF_INET) {
234 			memset((void *)&bind4, 0, sizeof(struct sockaddr_in));
235 #ifdef HAVE_SIN_LEN
236 			bind4.sin_len = sizeof(struct sockaddr_in6);
237 #endif
238 			bind4.sin_family = AF_INET;
239 			bind4.sin_port = htons(atoi(argv[3]));
240 			bind4.sin_addr.s_addr = htonl(INADDR_ANY);
241 
242 			if (usrsctp_bind(sock, (struct sockaddr *)&bind4, sizeof(bind4)) < 0) {
243 				perror("bind");
244 				usrsctp_close(sock);
245 				result = RETVAL_CATCHALL;
246 				goto out;
247 			}
248 		} else {
249 			memset((void *)&bind6, 0, sizeof(struct sockaddr_in6));
250 #ifdef HAVE_SIN6_LEN
251 			bind6.sin6_len = sizeof(struct sockaddr_in6);
252 #endif
253 			bind6.sin6_family = AF_INET6;
254 			bind6.sin6_port = htons(atoi(argv[3]));
255 			bind6.sin6_addr = in6addr_any;
256 			if (usrsctp_bind(sock, (struct sockaddr *)&bind6, sizeof(bind6)) < 0) {
257 				perror("bind");
258 				usrsctp_close(sock);
259 				result = RETVAL_CATCHALL;
260 				goto out;
261 			}
262 		}
263 	}
264 
265 	if (argc > 5) {
266 		memset(&encaps, 0, sizeof(struct sctp_udpencaps));
267 		encaps.sue_address.ss_family = address_family;
268 		encaps.sue_port = htons(atoi(argv[5]));
269 		if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void *)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
270 			perror("setsockopt");
271 			usrsctp_close(sock);
272 			result = RETVAL_CATCHALL;
273 			goto out;
274 		}
275 	}
276 
277 	if (argc > 6) {
278 #ifdef _WIN32
279 		if (_snprintf(request, sizeof(request), "%s %s %s", request_prefix, argv[6], request_postfix) < 0) {
280 #else
281 		if (snprintf(request, sizeof(request), "%s %s %s", request_prefix, argv[6], request_postfix) < 0) {
282 #endif
283 			request[0] = '\0';
284 		}
285 	} else {
286 #ifdef _WIN32
287 		if (_snprintf(request, sizeof(request), "%s %s %s", request_prefix, "/", request_postfix) < 0) {
288 #else
289 		if (snprintf(request, sizeof(request), "%s %s %s", request_prefix, "/", request_postfix) < 0) {
290 #endif
291 			request[0] = '\0';
292 		}
293 	}
294 
295 	printf("\nHTTP request:\n%s\n", request);
296 	printf("\nHTTP response:\n");
297 
298 
299 	usrsctp_set_upcall(sock, handle_upcall, NULL);
300 	usrsctp_set_non_blocking(sock, 1);
301 
302 	if (usrsctp_connect(sock, addr, addr_len) < 0) {
303 		if (errno != EINPROGRESS) {
304 			if (errno == ECONNREFUSED) {
305 				result = RETVAL_ECONNREFUSED;
306 			} else if (errno == ETIMEDOUT) {
307 				result = RETVAL_TIMEOUT;
308 			} else {
309 				result = RETVAL_CATCHALL;
310 			}
311 			perror("usrsctp_connect");
312 			usrsctp_close(sock);
313 
314 			goto out;
315 		}
316 	}
317 
318 	while (!done) {
319 #ifdef _WIN32
320 		Sleep(1*1000);
321 #else
322 		sleep(1);
323 #endif
324 	}
325 out:
326 	while (usrsctp_finish() != 0) {
327 #ifdef _WIN32
328 		Sleep(1000);
329 #else
330 		sleep(1);
331 #endif
332 	}
333 
334 	printf("Finished, returning with %d\n", result);
335 	return (result);
336 }
337