Squashed 'third_party/rawrtc/usrsctp/' content from commit bd1a92db3

Change-Id: If227cd6edd3243ac26044056b7427ae5bca71ef8
git-subtree-dir: third_party/rawrtc/usrsctp
git-subtree-split: bd1a92db338ba1e57453637959a127032bb566ff
diff --git a/programs/daytime_server.c b/programs/daytime_server.c
new file mode 100644
index 0000000..18a0fcb
--- /dev/null
+++ b/programs/daytime_server.c
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2012-2013 Michael Tuexen
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.	IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Usage: daytime_server [local_encaps_port] [remote_encaps_port]
+ */
+
+#ifdef _WIN32
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include <sys/types.h>
+#include <time.h>
+#ifndef _WIN32
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#endif
+#include <usrsctp.h>
+
+void
+debug_printf(const char *format, ...)
+{
+	va_list ap;
+
+	va_start(ap, format);
+	vprintf(format, ap);
+	va_end(ap);
+}
+
+#define DAYTIME_PPID 40
+int
+main(int argc, char *argv[])
+{
+	struct socket *sock, *conn_sock;
+	struct sockaddr_in addr;
+	struct sctp_udpencaps encaps;
+	socklen_t addr_len;
+	char buffer[80];
+	time_t now;
+	struct sctp_sndinfo sndinfo;
+
+	if (argc > 1) {
+		usrsctp_init(atoi(argv[1]), NULL, debug_printf);
+	} else {
+		usrsctp_init(9899, NULL, debug_printf);
+	}
+#ifdef SCTP_DEBUG
+	usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
+#endif
+	usrsctp_sysctl_set_sctp_blackhole(2);
+
+	if ((sock = usrsctp_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL)) == NULL) {
+		perror("usrsctp_socket");
+	}
+	if (argc > 2) {
+		memset(&encaps, 0, sizeof(struct sctp_udpencaps));
+		encaps.sue_address.ss_family = AF_INET;
+		encaps.sue_port = htons(atoi(argv[2]));
+		if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
+			perror("setsockopt");
+		}
+	}
+	memset((void *)&addr, 0, sizeof(struct sockaddr_in));
+#ifdef HAVE_SIN_LEN
+	addr.sin_len = sizeof(struct sockaddr_in);
+#endif
+	addr.sin_family = AF_INET;
+	addr.sin_port = htons(13);
+	addr.sin_addr.s_addr = htonl(INADDR_ANY);
+	if (usrsctp_bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
+		perror("usrsctp_bind");
+	}
+	if (usrsctp_listen(sock, 1) < 0) {
+		perror("usrsctp_listen");
+	}
+	while (1) {
+		addr_len = 0;
+		if ((conn_sock = usrsctp_accept(sock, NULL, &addr_len)) == NULL) {
+			continue;
+		}
+		time(&now);
+#ifdef _WIN32
+		_snprintf(buffer, sizeof(buffer), "%s", ctime(&now));
+#else
+		snprintf(buffer, sizeof(buffer), "%s", ctime(&now));
+#endif
+		sndinfo.snd_sid = 0;
+		sndinfo.snd_flags = 0;
+		sndinfo.snd_ppid = htonl(DAYTIME_PPID);
+		sndinfo.snd_context = 0;
+		sndinfo.snd_assoc_id = 0;
+		usrsctp_sendv(conn_sock, buffer, strlen(buffer), NULL, 0, (void *)&sndinfo,
+		              (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0);
+		usrsctp_close(conn_sock);
+	}
+	usrsctp_close(sock);
+	while (usrsctp_finish() != 0) {
+#ifdef _WIN32
+		Sleep(1000);
+#else
+		sleep(1);
+#endif
+	}
+	return (0);
+}