From 087b73d87ebab807ba38e2d543756ac2bc17becc Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Mon, 27 Apr 2009 20:15:49 +1000 Subject: libbb/xconnect: initialize winsock before using it --- libbb/Kbuild | 2 +- libbb/mingw.c | 20 ++++++++++++++------ libbb/xconnect.c | 8 ++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/libbb/Kbuild b/libbb/Kbuild index dca58d6f7..e335a95dc 100644 --- a/libbb/Kbuild +++ b/libbb/Kbuild @@ -22,7 +22,6 @@ lib-y += setup_environment.o lib-y += speed_table.o lib-y += vfork_daemon_rexec.o lib-y += vinfo_msg.o -lib-y += xconnect.o lib-y += xgethostbyname.o endif @@ -99,6 +98,7 @@ lib-y += wfopen_input.o lib-y += xatonum.o lib-y += xfuncs.o lib-y += xgetcwd.o +lib-y += xconnect.o lib-y += xreadlink.o # conditionally compiled objects: diff --git a/libbb/mingw.c b/libbb/mingw.c index 4b267e2ba..1293dc7eb 100644 --- a/libbb/mingw.c +++ b/libbb/mingw.c @@ -997,16 +997,24 @@ char **env_setenv(char **env, const char *name) return env; } +void winsock_init() +{ + static int init = 0; + if (!init) { + WSADATA wsa; + if (WSAStartup(MAKEWORD(2,2), &wsa)) + die("unable to initialize winsock subsystem, error %d", + WSAGetLastError()); + atexit((void(*)(void)) WSACleanup); + init = 1; + } +} + /* this is the first function to call into WS_32; initialize it */ #undef gethostbyname struct hostent *mingw_gethostbyname(const char *host) { - WSADATA wsa; - - if (WSAStartup(MAKEWORD(2,2), &wsa)) - die("unable to initialize winsock subsystem, error %d", - WSAGetLastError()); - atexit((void(*)(void)) WSACleanup); + winsock_init(); return gethostbyname(host); } diff --git a/libbb/xconnect.c b/libbb/xconnect.c index e7d510678..6e9b29298 100644 --- a/libbb/xconnect.c +++ b/libbb/xconnect.c @@ -6,7 +6,14 @@ * */ +#ifdef __MINGW32__ +#define WINVER 0x0501 +#include +#include +#undef s_addr +#else #include +#endif #include "libbb.h" int setsockopt_reuseaddr(int fd) @@ -162,6 +169,7 @@ USE_FEATURE_IPV6(sa_family_t af,) * for each possible socket type (tcp,udp,raw...): */ hint.ai_socktype = SOCK_STREAM; hint.ai_flags = ai_flags & ~DIE_ON_ERROR; + winsock_init(); rc = getaddrinfo(host, NULL, &hint, &result); if (rc || !result) { bb_error_msg("bad address '%s'", org_host); -- cgit v1.2.3-55-g6feb