summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar hydazz2026-11-27 00:15:46 +1100
committerGravatar hydazz2026-11-27 00:15:46 +1100
commit864943c7ad090ca1d90f49d1cd1321a84e9db617 (patch)
treee3c5f38d6d36a9c4afd24bf750e2a8352d5775c0
parentbdf48cf4b567b05f73f8f2ed0c05735d2a4d0899 (diff)
downloadlibimobiledevice-glue-master.tar.gz
libimobiledevice-glue-master.tar.bz2
socket: Initialize Winsock before opening a socketmaster
-rw-r--r--src/socket.c41
1 files changed, 38 insertions, 3 deletions
diff --git a/src/socket.c b/src/socket.c
index 5d22d14..0b37e9f 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -95,12 +95,32 @@ static int verbose = 0;
fprintf(stderr, "[socket] " msg , ## __VA_ARGS__); \
}
-void socket_init(void)
-{
#ifdef _WIN32
+static INIT_ONCE wsa_once = INIT_ONCE_STATIC_INIT;
+
+static BOOL CALLBACK wsa_startup(PINIT_ONCE once, PVOID param, PVOID *context)
+{
WSADATA wsa_data;
- if (WSAStartup(MAKEWORD(2,2), &wsa_data) != ERROR_SUCCESS) {
+ return WSAStartup(MAKEWORD(2,2), &wsa_data) == ERROR_SUCCESS;
+}
+
+/* Start Winsock once, before the first socket. The library's initializer in
+ * glue.c does it too, but a static link leaves glue.o out when nothing calls
+ * into it, and its initializer with it. */
+static int wsa_init(void)
+{
+ if (!InitOnceExecuteOnce(&wsa_once, wsa_startup, NULL, NULL)) {
SOCKET_ERR(0, "WSAStartup failed!\n");
+ return -1;
+ }
+ return 0;
+}
+#endif
+
+void socket_init(void)
+{
+#ifdef _WIN32
+ if (wsa_init() < 0) {
ExitProcess(1);
}
#endif
@@ -472,6 +492,11 @@ int socket_connect_unix(const char *filename)
int socket_create(const char* addr, uint16_t port)
{
+#ifdef _WIN32
+ if (wsa_init() < 0) {
+ return -1;
+ }
+#endif
int sfd = -1;
int yes = 1;
int no = 0;
@@ -1040,6 +1065,11 @@ static int32_t _sockaddr_in6_scope_id(struct sockaddr_in6* addr)
int socket_connect_addr(struct sockaddr* addr, uint16_t port)
{
+#ifdef _WIN32
+ if (wsa_init() < 0) {
+ return -1;
+ }
+#endif
int sfd = -1;
int yes = 1;
int bufsize = 0x20000;
@@ -1183,6 +1213,11 @@ int socket_connect_addr(struct sockaddr* addr, uint16_t port)
int socket_connect(const char *addr, uint16_t port)
{
+#ifdef _WIN32
+ if (wsa_init() < 0) {
+ return -1;
+ }
+#endif
int sfd = -1;
int yes = 1;
int bufsize = 0x20000;