changeset 9642:a71ffb6bd47e

Add module sockets.
author Simon Josefsson <simon@josefsson.org>
date Thu, 24 Jan 2008 15:55:56 +0100
parents 617cfe9ea53c
children 73fbf8ba5948
files ChangeLog lib/sockets.c lib/sockets.h m4/sockets.m4 modules/sockets tests/test-sockets.c
diffstat 6 files changed, 204 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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.
new file mode 100644
--- /dev/null
+++ b/lib/sockets.c
@@ -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;
+}
new file mode 100644
--- /dev/null
+++ b/lib/sockets.h
@@ -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
new file mode 100644
--- /dev/null
+++ b/m4/sockets.m4
@@ -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], [
+  :
+])
new file mode 100644
--- /dev/null
+++ b/modules/sockets
@@ -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
new file mode 100644
--- /dev/null
+++ b/tests/test-sockets.c
@@ -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;
+}