bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sockets


From: Simon Josefsson
Subject: Re: sockets
Date: Thu, 24 Jan 2008 15:58:20 +0100
User-agent: Gnus/5.110007 (No Gnus v0.7) Emacs/22.1 (gnu/linux)

Eric Blake <address@hidden> writes:

> According to Simon Josefsson on 1/18/2008 7:45 AM:
> | One thing worries me though; cygwin.  I don't know if this is needed on
> | cygwin.  I would expect the answer is no?  The m4 check should fail on
> | cygwin, but I'm not sure it does right now.
>
> You are correct - the module is not needed on cygwin (cygwin apps should
> neither link against -lws2_32, nor call WSA*, when using POSIX socket
> functions).  The m4 file is correct:
>
> checking whether <sys/socket.h> is self-contained... yes
> checking if we need to call WSAStartup in winsock2.h and -lws2_32... no

Thanks for confirming this!

> However, the test file is incorrect:
>
> gcc  -g -O2   -o test-sockets.exe test-sockets.o ../gllib/libgnu.a
> test-sockets.o: In function `main':
> /home/eblake/gnulib/testdir2736/build/gltests/../../gltests/test-sockets.c:29:
> undefined reference to `_gl_sockets_startup'
> /home/eblake/gnulib/testdir2736/build/gltests/../../gltests/test-sockets.c:36:
> undefined reference to `_gl_sockets_cleanup'
> collect2: ld returned 1 exit status
>
> Were you intending that all apps using sockets call
> gl_sockets_{start,clean}up (in which case, you need to declare them,
> perhaps as a no-op define, for non-mingw platforms), or were you intending
> these functions to be called under the hood when using a socket-based API
> in compliance with POSIX (in which case, your test should not call them
> directly, but call a function that uses them under the hood)?

I intended the gl_sockets_* functions to be used:

 1) inside gnulib replacement modules for mingw, such as the gethostname
 replacement, and

 2) optionally by application code to optimize socket startup on mingw.
 I suspect that calling WSAStartup and WSACleanup a lot of time will be
 inefficient, so applications may want to call WSAStartup+WSACleanup to
 make the gnulib replacement faster.

The simplest appear to be to always make gl_sockets_* available, so that
other gnulib replacements can use them.  This updated complete patch
takes care of this.  It seems the self-test should succeed with this
change.

I'd appreciate if you could check whether the installed 'sockets' module
continue to work under cygwin.  Btw, could you quote the part from
config.log which explains _why_ the m4 macro fails under cygwin?  It
isn't clear from the M4 check that it would fail under cygwin, but maybe
it is just my lack of knowledge with cygwin.  Still, if it is obscure,
we might add a comment.

Since there weren't any other comments after close to a week, and the
'sockets' module doesn't affect anyone that isn't strictly using it, I
have committed the patch below.

Thanks,
/Simon

diff --git a/ChangeLog b/ChangeLog
index 137021f..a638b32 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2008-01-24  Simon Josefsson  <address@hidden>
+
+       * 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  <address@hidden>
 
        * doc/posix-headers: Renamed from doc/headers.
diff --git a/lib/sockets.c b/lib/sockets.c
new file mode 100644
index 0000000..658119e
--- /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;
+}
diff --git a/lib/sockets.h b/lib/sockets.h
new file mode 100644
index 0000000..3ab16a0
--- /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
diff --git a/m4/sockets.m4 b/m4/sockets.m4
new file mode 100644
index 0000000..615b6e8
--- /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], [
+  :
+])
diff --git a/modules/sockets b/modules/sockets
new file mode 100644
index 0000000..a11789b
--- /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
diff --git a/tests/test-sockets.c b/tests/test-sockets.c
new file mode 100644
index 0000000..514409d
--- /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;
+}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]