diff options
author | Guy Harris <guy@alum.mit.edu> | 2019-05-01 18:46:23 -0700 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2019-05-02 09:29:01 +0000 |
commit | 2ee483a222bc765f8a738dfa15124826d89dc543 (patch) | |
tree | b1011d49978414331d191c2190318cb1d92d634e /wsutil | |
parent | 7bc066aa0c74f8c595ba01678832f2ac17e279dc (diff) |
Move the Winsock initialization and cleanup to wsutil routines.
Those routines exist on both Windows and UN*X, but they don't do
anything on UN*X (they could if it were ever necessary).
That eliminates some #ifdefs, and also means that the gory details of
initializing Winsock, including the Winsock version being requested,
are buried in one routine.
The initialization routine returns NULL on success and a pointer to a
g_malloc()ated error message on failure; report the error to the user,
along with a "report this to the Wireshark developers" suggestion.
That means including wsutil/socket.h, which obviates the need to include
some headers for socket APIs, as it includes them for you.
Change-Id: I9327bbf25effbb441e4217edc5354a4d5ab07186
Reviewed-on: https://code.wireshark.org/review/33045
Petri-Dish: Guy Harris <guy@alum.mit.edu>
Tested-by: Petri Dish Buildbot
Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'wsutil')
-rw-r--r-- | wsutil/CMakeLists.txt | 1 | ||||
-rw-r--r-- | wsutil/socket.c | 49 | ||||
-rw-r--r-- | wsutil/socket.h | 22 |
3 files changed, 72 insertions, 0 deletions
diff --git a/wsutil/CMakeLists.txt b/wsutil/CMakeLists.txt index 2eb9e70d70..f2a90a341f 100644 --- a/wsutil/CMakeLists.txt +++ b/wsutil/CMakeLists.txt @@ -107,6 +107,7 @@ set(WSUTIL_COMMON_FILES privileges.c rsa.c sober128.c + socket.c strnatcmp.c str_util.c strtoi.c diff --git a/wsutil/socket.c b/wsutil/socket.c new file mode 100644 index 0000000000..af0c970fb5 --- /dev/null +++ b/wsutil/socket.c @@ -0,0 +1,49 @@ +/* socket.c + * Socket wrappers + * + * Copyright 2019, Gerald Combs + * + * Wireshark - Network traffic analyzer + * By Gerald Combs <gerald@wireshark.org> + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include <config.h> + +#include <glib.h> + +#include <wsutil/socket.h> + +#ifdef _WIN32 +#include <wsutil/win32-utils.h> +#endif + +gchar * +ws_init_sockets(void) +{ + char *errmsg = NULL; +#ifdef _WIN32 + int err; + WORD wVersionRequested; + WSADATA wsaData; + + wVersionRequested = MAKEWORD(2, 2); + err = WSAStartup(wVersionRequested, &wsaData); + if (err != 0) { + errmsg = g_strdup_printf("Couldn't initialize Windows Sockets: %s", + win32strerror(err)); + } +#endif + return errmsg; +} + +void +ws_cleanup_sockets(void) +{ +#ifdef _WIN32 + /* XXX - any reason to check the error return? */ + WSACleanup(); +#endif +} diff --git a/wsutil/socket.h b/wsutil/socket.h index ca30d807c2..7d99d7cf67 100644 --- a/wsutil/socket.h +++ b/wsutil/socket.h @@ -12,6 +12,8 @@ #ifndef __SOCKET_H__ #define __SOCKET_H__ +#include "ws_symbol_export.h" + #if defined(_WIN32) && !defined(__CYGWIN__) #include <windows.h> #include <ws2tcpip.h> @@ -48,6 +50,26 @@ #endif /* __SOCKET_H__ */ +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Initialize sockets. + * + * Returns NULL on success, a g_malloc()ed error message on failure. + */ +WS_DLL_PUBLIC gchar *ws_init_sockets(void); + +/* + * Clean up sockets. + */ +WS_DLL_PUBLIC void ws_cleanup_sockets(void); + +#ifdef __cplusplus +} +#endif + /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * |