-
Notifications
You must be signed in to change notification settings - Fork 309
/
network-windows.c
157 lines (125 loc) · 2.99 KB
/
network-windows.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2021 Analog Devices, Inc.
* Author: Paul Cercueil
*/
#include "network.h"
#include <errno.h>
#include <ws2tcpip.h>
#define close(s) closesocket(s)
int set_blocking_mode(int s, bool blocking)
{
unsigned long nonblock;
int ret;
nonblock = blocking ? 0 : 1;
ret = ioctlsocket(s, FIONBIO, &nonblock);
if (ret == SOCKET_ERROR) {
ret = -WSAGetLastError();
return ret;
}
return 0;
}
int setup_cancel(struct iiod_client_pdata *io_ctx)
{
io_ctx->events[0] = WSACreateEvent();
if (io_ctx->events[0] == WSA_INVALID_EVENT)
return -ENOMEM; /* Pretty much the only error that can happen */
io_ctx->events[1] = WSACreateEvent();
if (io_ctx->events[1] == WSA_INVALID_EVENT) {
WSACloseEvent(io_ctx->events[0]);
return -ENOMEM;
}
return 0;
}
void cleanup_cancel(struct iiod_client_pdata *io_ctx)
{
WSACloseEvent(io_ctx->events[0]);
WSACloseEvent(io_ctx->events[1]);
}
void do_cancel(struct iiod_client_pdata *io_ctx)
{
WSASetEvent(io_ctx->events[1]);
}
int wait_cancellable(struct iiod_client_pdata *io_ctx,
bool read, unsigned int timeout_ms)
{
long wsa_events = FD_CLOSE;
DWORD ret, timeout = timeout_ms > 0 ? (DWORD) timeout_ms : WSA_INFINITE;
if (read)
wsa_events |= FD_READ;
else
wsa_events |= FD_WRITE;
WSAEventSelect(io_ctx->fd, NULL, 0);
WSAResetEvent(io_ctx->events[0]);
WSAEventSelect(io_ctx->fd, io_ctx->events[0], wsa_events);
ret = WSAWaitForMultipleEvents(2, io_ctx->events, FALSE,
timeout, FALSE);
if (ret == WSA_WAIT_TIMEOUT)
return -ETIMEDOUT;
if (ret == WSA_WAIT_EVENT_0 + 1)
return -EBADF;
return 0;
}
int network_get_error(void)
{
return -WSAGetLastError();
}
bool network_should_retry(int err)
{
return err == -WSAEWOULDBLOCK || err == -WSAETIMEDOUT;
}
bool network_is_interrupted(int err)
{
return false;
}
bool network_connect_in_progress(int err)
{
return err == -WSAEWOULDBLOCK;
}
/* Use it if available */
#ifndef WSA_FLAG_NO_HANDLE_INHERIT
#define WSA_FLAG_NO_HANDLE_INHERIT 0
#endif
int do_create_socket(const struct addrinfo *addrinfo)
{
SOCKET s;
s = WSASocketW(addrinfo->ai_family, addrinfo->ai_socktype, 0, NULL, 0,
WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED);
if (s == INVALID_SOCKET)
return -WSAGetLastError();
return (int) s;
}
int do_select(int fd, unsigned int timeout)
{
struct timeval tv;
struct timeval *ptv;
fd_set set;
int ret;
#ifdef _MSC_BUILD
/*
* This is so stupid, but studio emits a signed/unsigned mismatch
* on their own FD_ZERO macro, so turn the warning off/on
*/
#pragma warning(disable : 4389)
#endif
FD_ZERO(&set);
FD_SET(fd, &set);
#ifdef _MSC_BUILD
#pragma warning(default: 4389)
#endif
if (timeout != 0) {
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
ptv = &tv;
} else {
ptv = NULL;
}
ret = select(fd + 1, NULL, &set, &set, ptv);
if (ret < 0)
return -WSAGetLastError();
if (ret == 0)
return -WSAETIMEDOUT;
return 0;
}