From bb2bf54fe80c58cf9ed9cb71447743060970d198 Mon Sep 17 00:00:00 2001 From: Simon Josefsson Date: Thu, 24 Jan 2008 15:55:56 +0100 Subject: [PATCH] Add module sockets. --- ChangeLog | 11 +++++++++ lib/sockets.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ lib/sockets.h | 32 +++++++++++++++++++++++++ m4/sockets.m4 | 35 +++++++++++++++++++++++++++ modules/sockets | 25 +++++++++++++++++++ tests/test-sockets.c | 44 ++++++++++++++++++++++++++++++++++ 6 files changed, 204 insertions(+) create mode 100644 lib/sockets.c create mode 100644 lib/sockets.h create mode 100644 m4/sockets.m4 create mode 100644 modules/sockets create mode 100644 tests/test-sockets.c diff --git a/ChangeLog b/ChangeLog index 137021fe34..a638b321d3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2008-01-24 Simon Josefsson + + * 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 * doc/posix-headers: Renamed from doc/headers. diff --git a/lib/sockets.c b/lib/sockets.c new file mode 100644 index 0000000000..658119ea43 --- /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 . */ + +/* Written by Simon Josefsson */ + +#include + +/* This includes winsock2.h on MinGW. */ +#include + +#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 0000000000..3ab16a0d50 --- /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 . */ + +/* 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 0000000000..615b6e8720 --- /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 +#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 0000000000..a11789b4c9 --- /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 0000000000..514409d95b --- /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 . */ + +#include + +#include + +#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; +} -- 2.30.2