aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2019-05-01 18:46:23 -0700
committerGuy Harris <guy@alum.mit.edu>2019-05-02 09:29:01 +0000
commit2ee483a222bc765f8a738dfa15124826d89dc543 (patch)
treeb1011d49978414331d191c2190318cb1d92d634e
parent7bc066aa0c74f8c595ba01678832f2ac17e279dc (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>
-rw-r--r--debian/libwsutil0.symbols2
-rw-r--r--dumpcap.c31
-rw-r--r--extcap/CMakeLists.txt2
-rw-r--r--extcap/androiddump.c25
-rw-r--r--extcap/ciscodump.c25
-rw-r--r--extcap/randpktdump.c26
-rw-r--r--extcap/sshdump.c25
-rw-r--r--extcap/udpdump.c33
-rw-r--r--rawshark.c29
-rw-r--r--sharkd_daemon.c19
-rw-r--r--tshark.c28
-rw-r--r--ui/qt/main.cpp22
-rw-r--r--wsutil/CMakeLists.txt1
-rw-r--r--wsutil/socket.c49
-rw-r--r--wsutil/socket.h22
15 files changed, 189 insertions, 150 deletions
diff --git a/debian/libwsutil0.symbols b/debian/libwsutil0.symbols
index 6d10242f1e..da52be6c98 100644
--- a/debian/libwsutil0.symbols
+++ b/debian/libwsutil0.symbols
@@ -181,6 +181,7 @@ libwsutil.so.0 libwsutil0 #MINVER#
ws_buffer_free@Base 1.99.0
ws_buffer_init@Base 1.99.0
ws_buffer_remove_start@Base 1.99.0
+ ws_cleanup_sockets@Base 3.1.0
ws_cmac_buffer@Base 3.1.0
ws_buffer_cleanup@Base 2.3.0
ws_hexstrtou16@Base 2.3.0
@@ -192,6 +193,7 @@ libwsutil.so.0 libwsutil0 #MINVER#
ws_inet_ntop6@Base 2.1.2
ws_inet_pton4@Base 2.1.2
ws_inet_pton6@Base 2.1.2
+ ws_init_sockets@Base 3.1.0
ws_mempbrk_compile@Base 1.99.4
ws_mempbrk_exec@Base 1.99.4
ws_pipe_close@Base 2.6.5
diff --git a/dumpcap.c b/dumpcap.c
index 24fea2676f..2922471a4b 100644
--- a/dumpcap.c
+++ b/dumpcap.c
@@ -17,10 +17,6 @@
#include <sys/types.h>
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
-
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
@@ -29,10 +25,6 @@
#include <getopt.h>
#endif
-#ifdef HAVE_ARPA_INET_H
-#include <arpa/inet.h>
-#endif
-
#if defined(__APPLE__) && defined(__LP64__)
#include <sys/utsname.h>
#endif
@@ -45,6 +37,8 @@
#include <cli_main.h>
#include <version_info.h>
+#include <wsutil/socket.h>
+
#ifndef HAVE_GETOPT_LONG
#include "wsutil/wsgetopt.h"
#endif
@@ -1131,10 +1125,9 @@ report_counts_siginfo(int signum _U_)
static void
exit_main(int status)
{
-#ifdef _WIN32
- /* Shutdown windows sockets */
- WSACleanup();
+ ws_cleanup_sockets();
+#ifdef _WIN32
/* can be helpful for debugging */
#ifdef DEBUG_DUMPCAP
printf("Press any key\n");
@@ -4631,6 +4624,7 @@ get_dumpcap_runtime_info(GString *str)
int
main(int argc, char *argv[])
{
+ char *err_msg;
int opt;
static const struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
@@ -4814,19 +4808,20 @@ main(int argc, char *argv[])
/* ... and also load the packet.dll from wpcap */
/* XXX - currently not required, may change later. */
/*wpcap_packet_load();*/
+#endif
- DWORD result;
- WSADATA wsaData;
-
- /* Start windows sockets */
- result = WSAStartup( MAKEWORD(2, 2), &wsaData );
- if (result != 0)
+ err_msg = ws_init_sockets();
+ if (err_msg != NULL)
{
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_ERROR,
- "ERROR: WSAStartup failed with error %d: %s", result, win32strerror(result));
+ "ERROR: %s", err_msg);
+ g_free(err_msg);
+ g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_ERROR,
+ "%s", please_report_bug());
exit_main(1);
}
+#ifdef _WIN32
/* Set handler for Ctrl+C key */
SetConsoleCtrlHandler(capture_cleanup_handler, TRUE);
#else
diff --git a/extcap/CMakeLists.txt b/extcap/CMakeLists.txt
index 9ed9be36e4..398d712efe 100644
--- a/extcap/CMakeLists.txt
+++ b/extcap/CMakeLists.txt
@@ -200,6 +200,7 @@ endif()
if(BUILD_udpdump)
set(udpdump_LIBS
+ wsutil
${GLIB2_LIBRARIES}
${CMAKE_DL_LIBS}
${WIN_WS2_32_LIBRARY}
@@ -224,6 +225,7 @@ if(BUILD_randpktdump)
set(randpktdump_LIBS
randpkt_core
wiretap
+ wsutil
${GLIB2_LIBRARIES}
${ZLIB_LIBRARIES}
${CMAKE_DL_LIBS}
diff --git a/extcap/androiddump.c b/extcap/androiddump.c
index 6ede174d52..8ca775d496 100644
--- a/extcap/androiddump.c
+++ b/extcap/androiddump.c
@@ -21,6 +21,7 @@
#include <wsutil/strtoi.h>
#include <wsutil/filesystem.h>
#include <wsutil/privileges.h>
+#include <wsutil/please_report_bug.h>
#include <ui/cmdarg_err.h>
#include <wsutil/inet_addr.h>
@@ -2482,7 +2483,7 @@ static int capture_android_tcpdump(char *interface, char *fifo,
}
int main(int argc, char *argv[]) {
- char *init_progfile_dir_error;
+ char *err_msg;
int ret = EXIT_CODE_GENERIC;
int option_idx = 0;
int result;
@@ -2507,10 +2508,6 @@ int main(int argc, char *argv[]) {
char *help_url;
char *help_header = NULL;
-#ifdef _WIN32
- WSADATA wsaData;
-#endif /* _WIN32 */
-
cmdarg_err_init(failure_warning_message, failure_warning_message);
/*
@@ -2522,11 +2519,11 @@ int main(int argc, char *argv[]) {
* Attempt to get the pathname of the directory containing the
* executable file.
*/
- init_progfile_dir_error = init_progfile_dir(argv[0]);
- if (init_progfile_dir_error != NULL) {
+ err_msg = init_progfile_dir(argv[0]);
+ if (err_msg != NULL) {
g_warning("Can't get pathname of directory containing the captype program: %s.",
- init_progfile_dir_error);
- g_free(init_progfile_dir_error);
+ err_msg);
+ g_free(err_msg);
}
extcap_conf = g_new0(extcap_parameters, 1);
@@ -2689,13 +2686,13 @@ int main(int argc, char *argv[]) {
if (!bt_local_tcp_port)
bt_local_tcp_port = &default_bt_local_tcp_port;
-#ifdef _WIN32
- result = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (result != 0) {
- g_warning("WSAStartup failed with %d", result);
+ err_msg = ws_init_sockets();
+ if (err_msg != NULL) {
+ g_warning("ERROR: %s", err_msg);
+ g_free(err_msg);
+ g_warning("%s", please_report_bug());
goto end;
}
-#endif /* _WIN32 */
extcap_cmdline_debug(argv, argc);
diff --git a/extcap/ciscodump.c b/extcap/ciscodump.c
index b125353251..778d83a2b6 100644
--- a/extcap/ciscodump.c
+++ b/extcap/ciscodump.c
@@ -17,6 +17,7 @@
#include <wsutil/strtoi.h>
#include <wsutil/filesystem.h>
#include <wsutil/privileges.h>
+#include <wsutil/please_report_bug.h>
#include <extcap/ssh-base.h>
#include <writecap/pcapio.h>
@@ -518,7 +519,7 @@ static int list_config(char *interface, unsigned int remote_port)
int main(int argc, char *argv[])
{
- char* init_progfile_dir_error;
+ char* err_msg;
int result;
int option_idx = 0;
ssh_params_t* ssh_params = ssh_params_new();
@@ -530,10 +531,6 @@ int main(int argc, char *argv[])
char* help_url;
char* help_header = NULL;
-#ifdef _WIN32
- WSADATA wsaData;
-#endif /* _WIN32 */
-
/*
* Get credential information for later use.
*/
@@ -543,11 +540,11 @@ int main(int argc, char *argv[])
* Attempt to get the pathname of the directory containing the
* executable file.
*/
- init_progfile_dir_error = init_progfile_dir(argv[0]);
- if (init_progfile_dir_error != NULL) {
+ err_msg = init_progfile_dir(argv[0]);
+ if (err_msg != NULL) {
g_warning("Can't get pathname of directory containing the captype program: %s.",
- init_progfile_dir_error);
- g_free(init_progfile_dir_error);
+ err_msg);
+ g_free(err_msg);
}
help_url = data_file_url("ciscodump.html");
@@ -688,13 +685,13 @@ int main(int argc, char *argv[])
goto end;
}
-#ifdef _WIN32
- result = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (result != 0) {
- g_warning("ERROR: WSAStartup failed with error: %d", result);
+ err_msg = ws_init_sockets();
+ if (err_msg != NULL) {
+ g_warning("ERROR: %s", err_msg);
+ g_free(err_msg);
+ g_warning("%s", please_report_bug());
goto end;
}
-#endif /* _WIN32 */
if (extcap_conf->capture) {
if (!ssh_params->host) {
diff --git a/extcap/randpktdump.c b/extcap/randpktdump.c
index b4eec55da6..b035392e6d 100644
--- a/extcap/randpktdump.c
+++ b/extcap/randpktdump.c
@@ -18,6 +18,8 @@
#include <wsutil/strtoi.h>
#include <wsutil/filesystem.h>
#include <wsutil/privileges.h>
+#include <wsutil/socket.h>
+#include <wsutil/please_report_bug.h>
#include <cli_main.h>
@@ -124,7 +126,7 @@ static int list_config(char *interface)
int main(int argc, char *argv[])
{
- char* init_progfile_dir_error;
+ char* err_msg;
int option_idx = 0;
int result;
guint16 maxbytes = 5000;
@@ -138,10 +140,6 @@ int main(int argc, char *argv[])
wtap_dumper* savedump;
int ret = EXIT_FAILURE;
-#ifdef _WIN32
- WSADATA wsaData;
-#endif /* _WIN32 */
-
extcap_parameters * extcap_conf = g_new0(extcap_parameters, 1);
char* help_url;
char* help_header = NULL;
@@ -155,11 +153,11 @@ int main(int argc, char *argv[])
* Attempt to get the pathname of the directory containing the
* executable file.
*/
- init_progfile_dir_error = init_progfile_dir(argv[0]);
- if (init_progfile_dir_error != NULL) {
+ err_msg = init_progfile_dir(argv[0]);
+ if (err_msg != NULL) {
g_warning("Can't get pathname of directory containing the captype program: %s.",
- init_progfile_dir_error);
- g_free(init_progfile_dir_error);
+ err_msg);
+ g_free(err_msg);
}
help_url = data_file_url("randpktdump.html");
@@ -278,13 +276,13 @@ int main(int argc, char *argv[])
type = NULL;
}
-#ifdef _WIN32
- result = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (result != 0) {
- g_warning("ERROR: WSAStartup failed with error: %d", result);
+ err_msg = ws_init_sockets();
+ if (err_msg != NULL) {
+ g_warning("ERROR: %s", err_msg);
+ g_free(err_msg);
+ g_warning("%s", please_report_bug());
goto end;
}
-#endif /* _WIN32 */
if (extcap_conf->capture) {
diff --git a/extcap/sshdump.c b/extcap/sshdump.c
index a635834800..7f91ff231a 100644
--- a/extcap/sshdump.c
+++ b/extcap/sshdump.c
@@ -19,6 +19,7 @@
#include <wsutil/strtoi.h>
#include <wsutil/filesystem.h>
#include <wsutil/privileges.h>
+#include <wsutil/please_report_bug.h>
#include <errno.h>
#include <string.h>
@@ -337,7 +338,7 @@ static char* concat_filters(const char* extcap_filter, const char* remote_filter
int main(int argc, char *argv[])
{
- char* init_progfile_dir_error;
+ char* err_msg;
int result;
int option_idx = 0;
ssh_params_t* ssh_params = ssh_params_new();
@@ -352,10 +353,6 @@ int main(int argc, char *argv[])
gboolean use_sudo = FALSE;
gboolean noprom = FALSE;
-#ifdef _WIN32
- WSADATA wsaData;
-#endif /* _WIN32 */
-
/*
* Get credential information for later use.
*/
@@ -365,11 +362,11 @@ int main(int argc, char *argv[])
* Attempt to get the pathname of the directory containing the
* executable file.
*/
- init_progfile_dir_error = init_progfile_dir(argv[0]);
- if (init_progfile_dir_error != NULL) {
+ err_msg = init_progfile_dir(argv[0]);
+ if (err_msg != NULL) {
g_warning("Can't get pathname of directory containing the captype program: %s.",
- init_progfile_dir_error);
- g_free(init_progfile_dir_error);
+ err_msg);
+ g_free(err_msg);
}
help_url = data_file_url("sshdump.html");
@@ -521,13 +518,13 @@ int main(int argc, char *argv[])
goto end;
}
-#ifdef _WIN32
- result = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (result != 0) {
- g_warning("ERROR: WSAStartup failed with error: %d", result);
+ err_msg = ws_init_sockets();
+ if (err_msg != NULL) {
+ g_warning("ERROR: %s", err_msg);
+ g_free(err_msg);
+ g_warning("%s", please_report_bug());
goto end;
}
-#endif /* _WIN32 */
if (extcap_conf->capture) {
char* filter;
diff --git a/extcap/udpdump.c b/extcap/udpdump.c
index fb1cca1734..a57a371f34 100644
--- a/extcap/udpdump.c
+++ b/extcap/udpdump.c
@@ -23,10 +23,6 @@
#include <sys/time.h>
#endif
-#ifdef HAVE_SYS_SOCKET_H
- #include <sys/socket.h>
-#endif
-
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
@@ -38,16 +34,14 @@
#include <unistd.h>
#endif
-#ifdef HAVE_ARPA_INET_H
- #include <arpa/inet.h>
-#endif
-
#include <writecap/pcapio.h>
#include <wiretap/wtap.h>
#include <wsutil/strtoi.h>
#include <wsutil/inet_addr.h>
#include <wsutil/filesystem.h>
#include <wsutil/privileges.h>
+#include <wsutil/socket.h>
+#include <wsutil/please_report_bug.h>
#include <cli_main.h>
@@ -359,7 +353,7 @@ static void run_listener(const char* fifo, const guint16 port, const char* proto
int main(int argc, char *argv[])
{
- char* init_progfile_dir_error;
+ char* err_msg;
int option_idx = 0;
int result;
guint16 port = 0;
@@ -369,9 +363,6 @@ int main(int argc, char *argv[])
char* help_header = NULL;
char* payload = NULL;
char* port_msg = NULL;
-#ifdef _WIN32
- WSADATA wsaData;
-#endif /* _WIN32 */
/*
* Get credential information for later use.
@@ -382,11 +373,11 @@ int main(int argc, char *argv[])
* Attempt to get the pathname of the directory containing the
* executable file.
*/
- init_progfile_dir_error = init_progfile_dir(argv[0]);
- if (init_progfile_dir_error != NULL) {
+ err_msg = init_progfile_dir(argv[0]);
+ if (err_msg != NULL) {
g_warning("Can't get pathname of directory containing the captype program: %s.",
- init_progfile_dir_error);
- g_free(init_progfile_dir_error);
+ err_msg);
+ g_free(err_msg);
}
help_url = data_file_url("udpdump.html");
@@ -474,13 +465,13 @@ int main(int argc, char *argv[])
if (!payload)
payload = g_strdup("data");
-#ifdef _WIN32
- result = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (result != 0) {
- g_warning("Error: WSAStartup failed with error: %d", result);
+ err_msg = ws_init_sockets();
+ if (err_msg != NULL) {
+ g_warning("Error: %s", err_msg);
+ g_free(err_msg);
+ g_warning("%s", please_report_bug());
goto end;
}
-#endif /* _WIN32 */
if (port == 0)
port = UDPDUMP_DEFAULT_PORT;
diff --git a/rawshark.c b/rawshark.c
index d82dbc5f7a..f8586a1579 100644
--- a/rawshark.c
+++ b/rawshark.c
@@ -49,9 +49,11 @@
#include <ui/cmdarg_err.h>
#include <wsutil/filesystem.h>
#include <wsutil/file_util.h>
+#include <wsutil/socket.h>
#include <wsutil/plugins.h>
#include <wsutil/privileges.h>
#include <wsutil/report_message.h>
+#include <wsutil/please_report_bug.h>
#include <ui/clopts_common.h>
#include "globals.h"
@@ -405,15 +407,12 @@ set_link_type(const char *lt_arg) {
int
main(int argc, char *argv[])
{
- char *init_progfile_dir_error;
+ char *err_msg;
int opt, i;
-#ifdef _WIN32
- int result;
- WSADATA wsaData;
-#else
+#ifndef _WIN32
struct rlimit limit;
-#endif /* _WIN32 */
+#endif /* !_WIN32 */
gchar *pipe_name = NULL;
gchar *rfilters[64];
@@ -470,10 +469,10 @@ main(int argc, char *argv[])
* Attempt to get the pathname of the directory containing the
* executable file.
*/
- init_progfile_dir_error = init_progfile_dir(argv[0]);
- if (init_progfile_dir_error != NULL) {
+ err_msg = init_progfile_dir(argv[0]);
+ if (err_msg != NULL) {
fprintf(stderr, "rawshark: Can't get pathname of rawshark program: %s.\n",
- init_progfile_dir_error);
+ err_msg);
}
/* nothing more than the standard GLib handler, but without a warning */
@@ -725,15 +724,15 @@ main(int argc, char *argv[])
goto clean_exit;
}
-#ifdef _WIN32
- /* Start windows sockets */
- result = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
- if (result != 0)
+ err_msg = ws_init_sockets();
+ if (err_msg != NULL)
{
+ cmdarg_err("%s", err_msg);
+ g_free(err_msg);
+ cmdarg_err_cont("%s", please_report_bug());
ret = INIT_ERROR;
goto clean_exit;
}
-#endif /* _WIN32 */
/*
* Enabled and disabled protocols and heuristic dissectors as per
@@ -746,8 +745,6 @@ main(int argc, char *argv[])
if (n_rfilters != 0) {
for (i = 0; i < n_rfilters; i++) {
- gchar *err_msg;
-
if (!dfilter_compile(rfilters[i], &rfcodes[n_rfcodes], &err_msg)) {
cmdarg_err("%s", err_msg);
g_free(err_msg);
diff --git a/sharkd_daemon.c b/sharkd_daemon.c
index 63898636c3..f2397821bf 100644
--- a/sharkd_daemon.c
+++ b/sharkd_daemon.c
@@ -30,6 +30,7 @@
#include <wsutil/socket.h>
#include <wsutil/inet_addr.h>
+#include <wsutil/please_report_bug.h>
#ifndef _WIN32
#include <sys/un.h>
@@ -56,17 +57,15 @@ static socket_handle_t
socket_init(char *path)
{
socket_handle_t fd = INVALID_SOCKET;
-
-#ifdef _WIN32
- WSADATA wsaData;
- int result;
-
- result = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (result != 0) {
- g_warning("ERROR: WSAStartup failed with error: %d", result);
- return INVALID_SOCKET;
+ char *err_msg;
+
+ err_msg = ws_init_sockets();
+ if (err_msg != NULL) {
+ g_warning("ERROR: %s", err_msg);
+ g_free(err_msg);
+ g_warning("%s", please_report_bug());
+ return fd;
}
-#endif
#ifdef SHARKD_UNIX_SUPPORT
if (!strncmp(path, "unix:", 5))
diff --git a/tshark.c b/tshark.c
index a7834a8d7f..bbfc343cd7 100644
--- a/tshark.c
+++ b/tshark.c
@@ -49,8 +49,10 @@
#include <ui/cmdarg_err.h>
#include <wsutil/filesystem.h>
#include <wsutil/file_util.h>
+#include <wsutil/socket.h>
#include <wsutil/privileges.h>
#include <wsutil/report_message.h>
+#include <wsutil/please_report_bug.h>
#include <cli_main.h>
#include <version_info.h>
#include <wiretap/wtap_opttypes.h>
@@ -680,7 +682,7 @@ must_do_dissection(dfilter_t *rfcode, dfilter_t *dfcode,
int
main(int argc, char *argv[])
{
- char *init_progfile_dir_error;
+ char *err_msg;
int opt;
static const struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
@@ -696,11 +698,6 @@ main(int argc, char *argv[])
};
gboolean arg_error = FALSE;
-#ifdef _WIN32
- int result;
- WSADATA wsaData;
-#endif /* _WIN32 */
-
int err;
volatile process_file_status_t status;
volatile gboolean draw_taps = FALSE;
@@ -730,7 +727,6 @@ main(int argc, char *argv[])
#endif
dfilter_t *rfcode = NULL;
dfilter_t *dfcode = NULL;
- gchar *err_msg;
e_prefs *prefs_p;
int log_flags;
gchar *output_only = NULL;
@@ -786,14 +782,14 @@ main(int argc, char *argv[])
* Attempt to get the pathname of the directory containing the
* executable file.
*/
- init_progfile_dir_error = init_progfile_dir(argv[0]);
- if (init_progfile_dir_error != NULL) {
+ err_msg = init_progfile_dir(argv[0]);
+ if (err_msg != NULL) {
fprintf(stderr,
"tshark: Can't get pathname of directory containing the tshark program: %s.\n"
"It won't be possible to capture traffic.\n"
"Report this to the Wireshark developers.",
- init_progfile_dir_error);
- g_free(init_progfile_dir_error);
+ err_msg);
+ g_free(err_msg);
}
initialize_funnel_ops();
@@ -1784,15 +1780,15 @@ main(int argc, char *argv[])
}
#endif
-#ifdef _WIN32
- /* Start windows sockets */
- result = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
- if (result != 0)
+ err_msg = ws_init_sockets();
+ if (err_msg != NULL)
{
+ cmdarg_err("%s", err_msg);
+ g_free(err_msg);
+ cmdarg_err_cont("%s", please_report_bug());
exit_status = INIT_FAILED;
goto clean_exit;
}
-#endif /* _WIN32 */
/* Notify all registered modules that have had any of their preferences
changed either from one of the preferences file or from the command
diff --git a/ui/qt/main.cpp b/ui/qt/main.cpp
index 71d4b8eabd..db0a123ffe 100644
--- a/ui/qt/main.cpp
+++ b/ui/qt/main.cpp
@@ -32,10 +32,12 @@
#include <ui/cmdarg_err.h>
#include <wsutil/filesystem.h>
#include <wsutil/privileges.h>
+#include <wsutil/socket.h>
#ifdef HAVE_PLUGINS
#include <wsutil/plugins.h>
#endif
#include <wsutil/report_message.h>
+#include <wsutil/please_report_bug.h>
#include <wsutil/unicode-utils.h>
#include <version_info.h>
@@ -383,11 +385,6 @@ int main(int argc, char *qt_argv[])
int ret_val = EXIT_SUCCESS;
char **argv = qt_argv;
-#ifdef _WIN32
- int result;
- WSADATA wsaData;
-#endif /* _WIN32 */
-
char *rf_path;
int rf_open_errno;
#ifdef HAVE_LIBPCAP
@@ -569,15 +566,15 @@ int main(int argc, char *qt_argv[])
QString cf_name;
unsigned int in_file_type = WTAP_TYPE_AUTO;
-#ifdef _WIN32
- /* Start windows sockets */
- result = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
- if (result != 0)
+ err_msg = ws_init_sockets();
+ if (err_msg != NULL)
{
+ cmdarg_err("%s", err_msg);
+ g_free(err_msg);
+ cmdarg_err_cont("%s", please_report_bug());
ret_val = INIT_FAILED;
goto clean_exit;
}
-#endif /* _WIN32 */
/* Read the profile dependent (static part) of the recent file. */
/* Only the static part of it will be read, as we don't have the gui now to fill the */
@@ -940,10 +937,9 @@ int main(int argc, char *qt_argv[])
Dot11DecryptDestroyContext(&dot11decrypt_ctx);
-#ifdef _WIN32
- /* Shutdown windows sockets */
- WSACleanup();
+ ws_cleanup_sockets();
+#ifdef _WIN32
/* For some unknown reason, the "atexit()" call in "create_console()"
doesn't arrange that "destroy_console()" be called when we exit,
so we call it here if a console was created. */
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
*