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