From 5ea460a32a9882906c7ee3656b8fb0dcdbce2abc Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 1 Jun 2020 08:46:17 +0100 Subject: win32: only initialise networking if necessary A call to initialise networking is made during start up even if the applet doesn't need it. Instead, only initialise networking when a call is made to a function that definitely requires it. --- include/mingw.h | 8 +++++++- libbb/appletlib.c | 4 ---- win32/net.c | 41 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/include/mingw.h b/include/mingw.h index 26f0688d7..fa8f705b0 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -229,6 +229,10 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz); int mingw_select (int nfds, fd_set *rfds, fd_set *wfds, fd_set *xfds, struct timeval *timeout); int mingw_getpeername(int fd, struct sockaddr *sa, socklen_t *sz); +int mingw_gethostname(char *host, int namelen); +int mingw_getaddrinfo(const char *node, const char *service, + const struct addrinfo *hints, struct addrinfo **res); +struct hostent *mingw_gethostbyaddr(const void *addr, socklen_t len, int type); NOIMPL(mingw_sendto,SOCKET s UNUSED_PARAM, const char *buf UNUSED_PARAM, int len UNUSED_PARAM, int flags UNUSED_PARAM, const struct sockaddr *sa UNUSED_PARAM, int salen UNUSED_PARAM); @@ -242,6 +246,9 @@ NOIMPL(mingw_sendto,SOCKET s UNUSED_PARAM, const char *buf UNUSED_PARAM, int len #define accept mingw_accept #define select mingw_select #define getpeername mingw_getpeername +#define gethostname mingw_gethostname +#define getaddrinfo mingw_getaddrinfo +#define gethostbyaddr mingw_gethostbyaddr /* * sys/time.h @@ -502,7 +509,6 @@ void qsort_string_vector_case(char **sv, unsigned count) FAST_FUNC; */ const char *get_busybox_exec_path(void); -void init_winsock(void); void init_codepage(void); int has_bat_suffix(const char *p); diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 6e1947228..b2c85905c 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -327,10 +327,6 @@ void lbb_prepare(const char *applet if (ENABLE_LOCALE_SUPPORT) setlocale(LC_ALL, ""); -#if ENABLE_PLATFORM_MINGW32 - init_winsock(); -#endif - #if ENABLE_FEATURE_INDIVIDUAL /* Redundant for busybox (run_applet_and_exit covers that case) * but needed for "individual applet" mode */ diff --git a/win32/net.c b/win32/net.c index 01fa16a4e..880b807b7 100644 --- a/win32/net.c +++ b/win32/net.c @@ -10,19 +10,50 @@ int inet_aton(const char *cp, struct in_addr *inp) return 1; } -void init_winsock(void) +static void init_winsock(void) { WSADATA wsa; + static int initialized = 0; + + if (initialized) + return; + if (WSAStartup(MAKEWORD(2,2), &wsa)) - bb_error_msg_and_die("unable to initialize winsock subsystem, error %d", - WSAGetLastError()); - atexit((void(*)(void)) WSACleanup); /* may conflict with other atexit? */ + bb_error_msg_and_die("WSAStartup failed, error %d", WSAGetLastError()); + + atexit((void(*)(void)) WSACleanup); + initialized = 1; +} + +#undef gethostname +int mingw_gethostname(char *name, int namelen) +{ + init_winsock(); + return gethostname(name, namelen); +} + +#undef gethostbyaddr +struct hostent *mingw_gethostbyaddr(const void *addr, socklen_t len, int type) +{ + init_winsock(); + return gethostbyaddr(addr, len, type); +} + +#undef getaddrinfo +int mingw_getaddrinfo(const char *node, const char *service, + const struct addrinfo *hints, struct addrinfo **res) +{ + init_winsock(); + return getaddrinfo(node, service, hints, res); } int mingw_socket(int domain, int type, int protocol) { int sockfd; - SOCKET s = WSASocket(domain, type, protocol, NULL, 0, 0); + SOCKET s; + + init_winsock(); + s = WSASocket(domain, type, protocol, NULL, 0, 0); if (s == INVALID_SOCKET) { /* * WSAGetLastError() values are regular BSD error codes -- cgit v1.2.3-55-g6feb