This is a followup from the gethostname thread, see there for more discussions.
The idea is that this module will be used by other gnulib replacement modules for functions that need socket functionality under Windows, such as the upcoming gethostname which will have a rpl_gethostname for mingw. The module may implement WSAGetLastError to errno conversion in the future, but let's wait until we know exactly how that should work first. Thoughts? 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. /Simon
# 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 LIBS="$LIBS -lws2_32" AC_LIBOBJ(sockets) gl_PREREQ_SOCKETS fi ]) # Prerequisites of lib/sockets.c. AC_DEFUN([gl_PREREQ_SOCKETS], [ : ])
/* 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
/* 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) { WSADATA data; int err; err = WSAStartup (version, &data); if (err != 0) return 1; if (data.wVersion < version) return 2; return 0; } int gl_sockets_cleanup (void) { int err; err = WSACleanup (); if (err != 0) return 1; return 0; }
/* * 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; }
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: Include: "sockets.h" License: LGPL Maintainer: Simon Josefsson
Files: tests/test-sockets.c Depends-on: configure.ac: Makefile.am: TESTS += test-sockets check_PROGRAMS += test-sockets