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: daytime_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 #include <time.h>
44 #ifndef _WIN32
45 #include <unistd.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #endif
50 #include <usrsctp.h>
51 #include "programs_helper.h"
52 
53 #define DAYTIME_PPID 40
54 #define PORT 13
55 
56 static void
handle_accept(struct socket * sock,void * data,int flags)57 handle_accept(struct socket *sock, void *data, int flags)
58 {
59 	struct socket *conn_sock;
60 	char buffer[80];
61 	time_t now;
62 	socklen_t addr_len = 0;
63 	struct sctp_sndinfo sndinfo;
64 
65 	if (((conn_sock = usrsctp_accept(sock, NULL, &addr_len)) == NULL)
66 	    && (errno != EINPROGRESS)) {
67 		perror("usrsctp_accept");
68 		return;
69 	}
70 	time(&now);
71 #ifdef _WIN32
72 		if (_snprintf(buffer, sizeof(buffer), "%s", ctime(&now)) < 0) {
73 #else
74 		if (snprintf(buffer, sizeof(buffer), "%s", ctime(&now)) < 0) {
75 #endif
76 			buffer[0] = '\0';
77 		}
78 		sndinfo.snd_sid = 0;
79 		sndinfo.snd_flags = 0;
80 		sndinfo.snd_ppid = htonl(DAYTIME_PPID);
81 		sndinfo.snd_context = 0;
82 		sndinfo.snd_assoc_id = 0;
83 		usrsctp_sendv(conn_sock, buffer, strlen(buffer), NULL, 0, (void *)&sndinfo,
84 		              (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0);
85 		usrsctp_close(conn_sock);
86 }
87 
88 int
89 main(int argc, char *argv[])
90 {
91 	struct socket *sock;
92 	struct sockaddr_in addr;
93 	struct sctp_udpencaps encaps;
94 
95 	if (argc > 1) {
96 		usrsctp_init(atoi(argv[1]), NULL, debug_printf_stack);
97 	} else {
98 		usrsctp_init(9899, NULL, debug_printf_stack);
99 	}
100 #ifdef SCTP_DEBUG
101 	usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
102 #endif
103 	usrsctp_sysctl_set_sctp_blackhole(2);
104 	usrsctp_sysctl_set_sctp_no_csum_on_loopback(0);
105 
106 	if ((sock = usrsctp_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL)) == NULL) {
107 		perror("usrsctp_socket");
108 	}
109 	usrsctp_set_non_blocking(sock, 1);
110 	if (argc > 2) {
111 		memset(&encaps, 0, sizeof(struct sctp_udpencaps));
112 		encaps.sue_address.ss_family = AF_INET;
113 		encaps.sue_port = htons(atoi(argv[2]));
114 		if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
115 			perror("setsockopt");
116 		}
117 	}
118 	memset((void *)&addr, 0, sizeof(struct sockaddr_in));
119 #ifdef HAVE_SIN_LEN
120 	addr.sin_len = sizeof(struct sockaddr_in);
121 #endif
122 	addr.sin_family = AF_INET;
123 	addr.sin_port = htons(PORT);
124 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
125 	if (usrsctp_bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
126 		perror("usrsctp_bind");
127 	}
128 	if (usrsctp_listen(sock, 1) < 0) {
129 		perror("usrsctp_listen");
130 	}
131 
132 	usrsctp_set_upcall(sock, handle_accept, NULL);
133 
134 	while (1) {
135 #ifdef _WIN32
136 		Sleep(1*1000);
137 #else
138 		sleep(1);
139 #endif
140 	}
141 	return (0);
142 }
143