Squashed 'third_party/rawrtc/re/' content from commit f3163ce8b

Change-Id: I6a235e6ac0f03269d951026f9d195da05c40fdab
git-subtree-dir: third_party/rawrtc/re
git-subtree-split: f3163ce8b526a13b35ef71ce4dd6f43585064d8a
diff --git a/src/net/sock.c b/src/net/sock.c
new file mode 100644
index 0000000..7b74dee
--- /dev/null
+++ b/src/net/sock.c
@@ -0,0 +1,91 @@
+/**
+ * @file net/sock.c  Networking sockets code
+ *
+ * Copyright (C) 2010 Creytiv.com
+ */
+#include <re_types.h>
+#include <re_fmt.h>
+#include <re_mbuf.h>
+#include <re_net.h>
+
+
+#define DEBUG_MODULE "netsock"
+#define DEBUG_LEVEL 5
+#include <re_dbg.h>
+
+
+static bool inited = false;
+
+
+#ifdef WIN32
+static int wsa_init(void)
+{
+	WORD wVersionRequested = MAKEWORD(2, 2);
+	WSADATA wsaData;
+	int err;
+
+	err = WSAStartup(wVersionRequested, &wsaData);
+	if (err != 0) {
+		DEBUG_WARNING("Could not load winsock (%m)\n", err);
+		return err;
+	}
+
+	/* Confirm that the WinSock DLL supports 2.2.*/
+	/* Note that if the DLL supports versions greater    */
+	/* than 2.2 in addition to 2.2, it will still return */
+	/* 2.2 in wVersion since that is the version we      */
+	/* requested.                                        */
+	if (LOBYTE(wsaData.wVersion) != 2 ||
+	    HIBYTE(wsaData.wVersion) != 2 ) {
+		WSACleanup();
+		DEBUG_WARNING("Bad winsock verion (%d.%d)\n",
+			      HIBYTE(wsaData.wVersion),
+			      LOBYTE(wsaData.wVersion));
+		return EINVAL;
+	}
+
+	return 0;
+}
+#endif
+
+
+/**
+ * Initialise network sockets
+ *
+ * @return 0 if success, otherwise errorcode
+ */
+int net_sock_init(void)
+{
+	int err = 0;
+
+	DEBUG_INFO("sock init: inited=%d\n", inited);
+
+	if (inited)
+		return 0;
+
+#ifdef WIN32
+	err = wsa_init();
+#endif
+
+	inited = true;
+
+	return err;
+}
+
+
+/**
+ * Cleanup network sockets
+ */
+void net_sock_close(void)
+{
+#ifdef WIN32
+	const int err = WSACleanup();
+	if (0 != err) {
+		DEBUG_WARNING("sock close: WSACleanup (%d)\n", err);
+	}
+#endif
+
+	inited = false;
+
+	DEBUG_INFO("sock close\n");
+}