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/ekr_server.c b/programs/ekr_server.c
new file mode 100644
index 0000000..6f81217
--- /dev/null
+++ b/programs/ekr_server.c
@@ -0,0 +1,298 @@
+/*
+ * Copyright (C) 2011-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.
+ */
+
+#ifdef _WIN32
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <stdarg.h>
+#ifndef _WIN32
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <pthread.h>
+#include <unistd.h>
+#else
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#endif
+#include <usrsctp.h>
+
+#define MAX_PACKET_SIZE (1<<16)
+
+#ifdef _WIN32
+static DWORD WINAPI
+#else
+static void *
+#endif
+handle_packets(void *arg)
+{
+#ifdef _WIN32
+	SOCKET *fdp;
+#else
+	int *fdp;
+#endif
+	char buf[MAX_PACKET_SIZE];
+	char *dump_buf;
+	ssize_t length;
+
+#ifdef _WIN32
+	fdp = (SOCKET *)arg;
+#else
+	fdp = (int *)arg;
+#endif
+	for (;;) {
+#if defined(__NetBSD__)
+		pthread_testcancel();
+#endif
+		length = recv(*fdp, buf, MAX_PACKET_SIZE, 0);
+		if (length > 0) {
+			if ((dump_buf = usrsctp_dumppacket(buf, (size_t)length, SCTP_DUMP_INBOUND)) != NULL) {
+				fprintf(stderr, "%s", dump_buf);
+				usrsctp_freedumpbuffer(dump_buf);
+			}
+			usrsctp_conninput(fdp, buf, (size_t)length, 0);
+		}
+	}
+#ifdef _WIN32
+	return 0;
+#else
+	return (NULL);
+#endif
+}
+
+static int
+conn_output(void *addr, void *buf, size_t length, uint8_t tos, uint8_t set_df)
+{
+	char *dump_buf;
+#ifdef _WIN32
+	SOCKET *fdp;
+#else
+	int *fdp;
+#endif
+
+#ifdef _WIN32
+	fdp = (SOCKET *)addr;
+#else
+	fdp = (int *)addr;
+#endif
+	if ((dump_buf = usrsctp_dumppacket(buf, length, SCTP_DUMP_OUTBOUND)) != NULL) {
+		fprintf(stderr, "%s", dump_buf);
+		usrsctp_freedumpbuffer(dump_buf);
+	}
+#ifdef _WIN32
+	if (send(*fdp, buf, (int)length, 0) == SOCKET_ERROR) {
+		return (WSAGetLastError());
+#else
+	if (send(*fdp, buf, length, 0) < 0) {
+		return (errno);
+#endif
+	} else {
+		return (0);
+	}
+}
+
+static int
+receive_cb(struct socket *s, union sctp_sockstore addr, void *data,
+           size_t datalen, struct sctp_rcvinfo rcv, int flags, void *ulp_info)
+{
+
+	if (data) {
+		if (flags & MSG_NOTIFICATION) {
+			printf("Notification of length %d received.\n", (int)datalen);
+		} else {
+			printf("Msg of length %d received via %p:%u on stream %d with SSN %u and TSN %u, PPID %u, context %u.\n",
+			       (int)datalen,
+			       addr.sconn.sconn_addr,
+			       ntohs(addr.sconn.sconn_port),
+			       rcv.rcv_sid,
+			       rcv.rcv_ssn,
+			       rcv.rcv_tsn,
+			       ntohl(rcv.rcv_ppid),
+			       rcv.rcv_context);
+		}
+		free(data);
+	} else {
+		usrsctp_close(s);
+	}
+	return (1);
+}
+
+void
+debug_printf(const char *format, ...)
+{
+	va_list ap;
+
+	va_start(ap, format);
+	vprintf(format, ap);
+	va_end(ap);
+}
+
+int
+main(int argc, char *argv[])
+{
+	struct sockaddr_in sin;
+	struct sockaddr_conn sconn;
+#ifdef _WIN32
+	SOCKET fd;
+#else
+	int fd;
+#endif
+	struct socket *s;
+#ifdef _WIN32
+	HANDLE tid;
+#else
+	pthread_t tid;
+#endif
+#ifdef _WIN32
+	WSADATA wsaData;
+#endif
+
+	if (argc < 4) {
+		printf("error: this program requires 4 arguments!\n");
+		exit(EXIT_FAILURE);
+	}
+
+#ifdef _WIN32
+	if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
+		printf("WSAStartup failed\n");
+		exit (EXIT_FAILURE);
+	}
+#endif
+	usrsctp_init(0, conn_output, debug_printf);
+	/* set up a connected UDP socket */
+#ifdef _WIN32
+	if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {
+		printf("socket() failed with error: %d\n", WSAGetLastError());
+	}
+#else
+	if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
+		perror("socket");
+	}
+#endif
+	memset(&sin, 0, sizeof(struct sockaddr_in));
+	sin.sin_family = AF_INET;
+#ifdef HAVE_SIN_LEN
+	sin.sin_len = sizeof(struct sockaddr_in);
+#endif
+	sin.sin_port = htons(atoi(argv[2]));
+	if (!inet_pton(AF_INET, argv[1], &sin.sin_addr.s_addr)){
+		printf("error: invalid address\n");
+		exit(1);
+	}
+#ifdef _WIN32
+	if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
+		printf("bind() failed with error: %d\n", WSAGetLastError());
+	}
+#else
+	if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
+		perror("bind");
+	}
+#endif
+	memset(&sin, 0, sizeof(struct sockaddr_in));
+	sin.sin_family = AF_INET;
+#ifdef HAVE_SIN_LEN
+	sin.sin_len = sizeof(struct sockaddr_in);
+#endif
+	sin.sin_port = htons(atoi(argv[4]));
+	if (!inet_pton(AF_INET, argv[3], &sin.sin_addr.s_addr)){
+		printf("error: invalid address\n");
+		exit(1);
+	}
+#ifdef _WIN32
+	if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
+		printf("connect() failed with error: %d\n", WSAGetLastError());
+	}
+#else
+	if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
+		perror("connect");
+	}
+#endif
+#ifdef SCTP_DEBUG
+	usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE);
+#endif
+	usrsctp_sysctl_set_sctp_ecn_enable(0);
+	usrsctp_register_address((void *)&fd);
+#ifdef _WIN32
+	tid = CreateThread(NULL, 0, &handle_packets, (void *)&fd, 0, NULL);
+#else
+	pthread_create(&tid, NULL, &handle_packets, (void *)&fd);
+#endif
+	if ((s = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, receive_cb, NULL, 0, NULL)) == NULL) {
+		perror("usrsctp_socket");
+	}
+	memset(&sconn, 0, sizeof(struct sockaddr_conn));
+	sconn.sconn_family = AF_CONN;
+#ifdef HAVE_SCONN_LEN
+	sconn.sconn_len = sizeof(struct sockaddr_conn);
+#endif
+	sconn.sconn_port = htons(5001);
+	sconn.sconn_addr = (void *)&fd;
+	if (usrsctp_bind(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)) < 0) {
+		perror("usrsctp_bind");
+	}
+	if (usrsctp_listen(s, 1) < 0) {
+		perror("usrsctp_listen");
+	}
+	while (1) {
+		if (usrsctp_accept(s, NULL, NULL) == NULL) {
+			perror("usrsctp_accept");
+		}
+	}
+	usrsctp_close(s);
+	usrsctp_deregister_address((void *)&fd);
+	while (usrsctp_finish() != 0) {
+#ifdef _WIN32
+		Sleep(1000);
+#else
+		sleep(1);
+#endif
+	}
+#ifdef _WIN32
+	TerminateThread(tid, 0);
+	WaitForSingleObject(tid, INFINITE);
+	if (closesocket(fd) == SOCKET_ERROR) {
+		printf("closesocket() failed with error: %d\n", WSAGetLastError());
+	}
+	WSACleanup();
+#else
+	pthread_cancel(tid);
+	pthread_join(tid, NULL);
+	if (close(fd) < 0) {
+		perror("close");
+	}
+#endif
+	return (0);
+}