Add module sockets.
authorSimon Josefsson <simon@josefsson.org>
Thu, 24 Jan 2008 14:55:56 +0000 (15:55 +0100)
committerSimon Josefsson <simon@josefsson.org>
Thu, 24 Jan 2008 14:55:56 +0000 (15:55 +0100)
ChangeLog
lib/sockets.c [new file with mode: 0644]
lib/sockets.h [new file with mode: 0644]
m4/sockets.m4 [new file with mode: 0644]
modules/sockets [new file with mode: 0644]
tests/test-sockets.c [new file with mode: 0644]

index 137021fe34bc327fe05e9aa541d28dc8f86f7107..a638b321d3674db0855cca5b1a7cf9715788ba17 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2008-01-24  Simon Josefsson  <simon@josefsson.org>
+
+       * modules/sockets: New module, can be used to call WSA_Startup and
+       WSA_Cleanup when needed.
+
+       * lib/sockets.h, lib/sockets.c: New files.
+
+       * m4/sockets.m4: New file.
+
+       * tests/test-sockets.c: New file.
+
 2008-01-19  Bruno Haible  <bruno@clisp.org>
 
        * doc/posix-headers: Renamed from doc/headers.
diff --git a/lib/sockets.c b/lib/sockets.c
new file mode 100644 (file)
index 0000000..658119e
--- /dev/null
@@ -0,0 +1,57 @@
+/* sockets.c --- wrappers for Windows socket functions
+
+   Copyright (C) 2008 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Simon Josefsson */
+
+#include <config.h>
+
+/* This includes winsock2.h on MinGW. */
+#include <sys/socket.h>
+
+#include "sockets.h"
+
+int
+gl_sockets_startup (int version)
+{
+#if WINDOWS_SOCKETS
+  WSADATA data;
+  int err;
+
+  err = WSAStartup (version, &data);
+  if (err != 0)
+    return 1;
+
+  if (data.wVersion < version)
+    return 2;
+#endif
+
+  return 0;
+}
+
+int
+gl_sockets_cleanup (void)
+{
+#if WINDOWS_SOCKETS
+  int err;
+
+  err = WSACleanup ();
+  if (err != 0)
+    return 1;
+#endif
+
+  return 0;
+}
diff --git a/lib/sockets.h b/lib/sockets.h
new file mode 100644 (file)
index 0000000..3ab16a0
--- /dev/null
@@ -0,0 +1,32 @@
+/* sockets.h - wrappers for Windows socket functions
+
+   Copyright (C) 2008 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Simon Josefsson */
+
+#ifndef SOCKETS_H
+# define SOCKETS_H 1
+
+#define SOCKETS_1_0 0x100
+#define SOCKETS_1_1 0x101
+#define SOCKETS_2_0 0x200
+#define SOCKETS_2_1 0x201
+#define SOCKETS_2_2 0x202
+
+int gl_sockets_startup (int version);
+int gl_sockets_cleanup (void);
+
+#endif
diff --git a/m4/sockets.m4 b/m4/sockets.m4
new file mode 100644 (file)
index 0000000..615b6e8
--- /dev/null
@@ -0,0 +1,35 @@
+# sockets.m4 serial 1
+dnl Copyright (C) 2008 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_SOCKETS],
+[
+  AC_REQUIRE([gl_HEADER_SYS_SOCKET])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H
+
+  AC_CACHE_CHECK([if we need to call WSAStartup in winsock2.h and -lws2_32],
+                 [gl_cv_func_wsastartup], [
+    am_save_LIBS="$LIBS"
+    LIBS="$LIBS -lws2_32"
+    AC_TRY_LINK([
+#ifdef HAVE_WINSOCK2_H
+# include <winsock2.h>
+#endif], [
+      WORD wVersionRequested = MAKEWORD(1, 1);
+      WSADATA wsaData;
+      int err = WSAStartup(wVersionRequested, &wsaData);
+      WSACleanup ();],
+      gl_cv_func_wsastartup=yes, gl_cv_func_wsastartup=no)
+    LIBS="$am_save_LIBS"])
+  if test "$gl_cv_func_wsastartup" = "yes"; then
+    AC_DEFINE([WINDOWS_SOCKETS], 1, [Define if WSAStartup is needed.])
+    LIBS="$LIBS -lws2_32"
+  fi
+  gl_PREREQ_SOCKETS
+])
+
+# Prerequisites of lib/sockets.c.
+AC_DEFUN([gl_PREREQ_SOCKETS], [
+  :
+])
diff --git a/modules/sockets b/modules/sockets
new file mode 100644 (file)
index 0000000..a11789b
--- /dev/null
@@ -0,0 +1,25 @@
+Description:
+Wrappers for Windows socket functions
+
+Files:
+lib/sockets.c
+lib/sockets.h
+m4/sockets.m4
+
+Depends-on:
+sys_socket
+
+configure.ac:
+gl_SOCKETS
+
+Makefile.am:
+lib_SOURCES += sockets.h sockets.c
+
+Include:
+"sockets.h"
+
+License:
+LGPL
+
+Maintainer:
+Simon Josefsson
diff --git a/tests/test-sockets.c b/tests/test-sockets.c
new file mode 100644 (file)
index 0000000..514409d
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2008 Free Software Foundation
+ * Written by Simon Josefsson.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include <stdio.h>
+
+#include "sockets.h"
+
+int
+main (int argc, char *argv[])
+{
+  int err;
+
+  err = gl_sockets_startup (SOCKETS_1_1);
+  if (err != 0)
+    {
+      printf ("wsastartup failed %d\n", err);
+      return 1;
+    }
+
+  err = gl_sockets_cleanup ();
+  if (err != 0)
+    {
+      printf ("wsacleanup failed %d\n", err);
+      return 1;
+    }
+
+  return 0;
+}