include/fetch/ft_win.h

filter: Statementstmt 80.9% 17 / 21 Branchbran 41.6% 5 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 66.6% 22 / 33 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/fetch/ft_tls.h ↑ Summary xs/abi.xs »
1 #ifndef FT_WIN_H
2 #define FT_WIN_H
3  
4 /* Portability shim for the socket/IO surface. On a POSIX host these are
5 * zero-overhead passthroughs, so the Unix build is unchanged. On native
6 * Windows they map onto Winsock: every socket is wrapped in a C-runtime fd via
7 * _open_osfhandle() so the rest of Fetch keeps its dense "int fd" model (the
8 * loop's per-fd callback arrays and the backend all stay as-is), and the real
9 * SOCKET is recovered with _get_osfhandle() only at the Winsock call boundary.
10 *
11 * Included first from ft_core.h, before ft_http.h et al. */
12  
13 #ifdef _WIN32
14  
15 /* raise the select() descriptor set before winsock2.h fixes FD_SETSIZE */
16 #ifndef FD_SETSIZE
17 #define FD_SETSIZE 4096
18 #endif
19 /* rand_s() is gated behind _CRT_RAND_S, which must be set before the FIRST
20 * <stdlib.h> - and perl.h already pulled that in long before us. So the real
21 * define lives at the very top of Fetch.xs (before any include); this is a
22 * guarded backstop that never clashes with it. */
23 #ifndef _CRT_RAND_S
24 #define _CRT_RAND_S
25 #endif
26 #include <winsock2.h>
27 #include <ws2tcpip.h>
28 #include <io.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <stdlib.h>
32  
33 /* Which POSIX socket errno names <errno.h> carries varies by toolchain: MSVC
34 * and current mingw-w64 have the full set, while the mingw shipped with older
35 * Strawberry perls (gcc 4.4 - 4.6) stops short of ECONNREFUSED/ETIMEDOUT. Any
36 * name that is missing gets its Winsock number, which is consistent because
37 * ft_win_seterrno is the only thing that ever sets these and every test in the
38 * core compares against the same macro. */
39 #ifndef EINPROGRESS
40 #define EINPROGRESS WSAEINPROGRESS
41 #endif
42 #ifndef EWOULDBLOCK
43 #define EWOULDBLOCK WSAEWOULDBLOCK
44 #endif
45 #ifndef ECONNRESET
46 #define ECONNRESET WSAECONNRESET
47 #endif
48 #ifndef ECONNREFUSED
49 #define ECONNREFUSED WSAECONNREFUSED
50 #endif
51 #ifndef ETIMEDOUT
52 #define ETIMEDOUT WSAETIMEDOUT
53 #endif
54  
55 /* MSVC's CRT has no ssize_t at all (mingw's <sys/types.h> does), and the core
56 * returns one from every read/write wrapper. SSIZE_T is the same pointer-width
57 * signed type. _SSIZE_T_DEFINED is the guard the CRT headers themselves test,
58 * so setting it keeps a later toolchain typedef from colliding. */
59 #if defined(_MSC_VER) && !defined(ssize_t) && !defined(_SSIZE_T_DEFINED)
60 #include <basetsd.h>
61 typedef SSIZE_T ssize_t;
62 #define _SSIZE_T_DEFINED
63 #endif
64  
65 /* one-time Winsock startup, run lazily from the first socket() */
66 static int ft_win_started = 0;
67 static void ft_win_init(void) {
68 if (!ft_win_started) {
69 WSADATA wsa;
70 WSAStartup(MAKEWORD(2, 2), &wsa);
71 ft_win_started = 1;
72 }
73 }
74  
75 /* translate the last Winsock error into the errno value the core checks */
76 static void ft_win_seterrno(void) {
77 switch (WSAGetLastError()) {
78 case WSAEWOULDBLOCK: errno = EWOULDBLOCK; break;
79 case WSAEINPROGRESS:
80 case WSAEALREADY: errno = EINPROGRESS; break;
81 case WSAEINTR: errno = EINTR; break;
82 case WSAECONNRESET: errno = ECONNRESET; break;
83 case WSAECONNREFUSED:errno = ECONNREFUSED;break;
84 case WSAETIMEDOUT: errno = ETIMEDOUT; break;
85 default: errno = EIO; break;
86 }
87 }
88  
89 /* the real SOCKET behind a Fetch fd (for Winsock calls / SSL_set_fd) */
90 #define FT_SOCK(fd) ((SOCKET)_get_osfhandle(fd))
91  
92 static int ft_os_socket(int af, int type, int proto) {
93 SOCKET s;
94 int fd;
95 ft_win_init();
96 s = socket(af, type, proto);
97 if (s == INVALID_SOCKET) { ft_win_seterrno(); return -1; }
98 fd = _open_osfhandle((intptr_t)s, O_RDWR | O_BINARY);
99 if (fd < 0) { closesocket(s); errno = EMFILE; return -1; }
100 return fd;
101 }
102  
103 static void ft_os_set_nonblock(int fd) {
104 u_long on = 1;
105 ioctlsocket(FT_SOCK(fd), FIONBIO, &on);
106 }
107  
108 /* Winsock reports a non-blocking connect still in flight as WSAEWOULDBLOCK
109 * where POSIX reports EINPROGRESS, and the dial loops in ft_http.h / ft_abi.h
110 * test EINPROGRESS to decide whether to keep the fd. Translate here so those
111 * callers stay platform-neutral; without it every connect looks like a hard
112 * failure and the socket is closed before it ever finishes. */
113 static int ft_os_connect(int fd, const struct sockaddr *a, int alen) {
114 if (connect(FT_SOCK(fd), a, alen) == 0) return 0;
115 ft_win_seterrno();
116 if (errno == EWOULDBLOCK) errno = EINPROGRESS;
117 return -1;
118 }
119  
120 static ssize_t ft_os_recv(int fd, void *buf, size_t n, int flags) {
121 int r = recv(FT_SOCK(fd), (char *)buf, (int)n, flags);
122 if (r == SOCKET_ERROR) { ft_win_seterrno(); return -1; }
123 return r;
124 }
125  
126 static ssize_t ft_os_send(int fd, const void *buf, size_t n, int flags) {
127 int r = send(FT_SOCK(fd), (const char *)buf, (int)n, flags);
128 if (r == SOCKET_ERROR) { ft_win_seterrno(); return -1; }
129 return r;
130 }
131  
132 static int ft_os_getsockopt(int fd, int lvl, int opt, void *val, socklen_t *len) {
133 int r = getsockopt(FT_SOCK(fd), lvl, opt, (char *)val, (int *)len);
134 if (r == SOCKET_ERROR) { ft_win_seterrno(); return -1; }
135 return r;
136 }
137  
138 /* _close releases both the CRT fd slot and (via CloseHandle) the socket */
139 static int ft_os_close(int fd) { return _close(fd); }
140  
141 /* OpenSSL on Windows wants the SOCKET, not the CRT fd (socket handle values
142 * fit an int in practice, so the cast is safe) */
143 #define FT_SSL_FD(fd) ((int)FT_SOCK(fd))
144  
145 /* random bytes (WebSocket key + frame masks) via the CRT's rand_s */
146 static void ft_os_random(unsigned char *out, size_t n) {
147 size_t i;
148 for (i = 0; i < n; i++) {
149 unsigned int v;
150 if (rand_s(&v) != 0) v = (unsigned int)rand();
151 out[i] = (unsigned char)v;
152 }
153 }
154  
155 #else /* ---- POSIX: exact passthroughs, the Unix build is unchanged ---- */
156  
157 #include <sys/socket.h>
158 #include <netdb.h>
159 #include <netinet/in.h>
160 #include <unistd.h>
161 #include <fcntl.h>
162 #include <errno.h>
163 #include <stdint.h>
164 #include <time.h>
165  
166 #define ft_win_init() ((void)0)
167 #define ft_os_socket(af, ty, pr) socket((af), (ty), (pr))
168 #define ft_os_connect(fd, a, l) connect((fd), (a), (l))
169 #define ft_os_recv(fd, b, n, f) recv((fd), (b), (n), (f))
170 #define ft_os_send(fd, b, n, f) send((fd), (b), (n), (f))
171 #define ft_os_getsockopt(fd, lv, op, v, l) getsockopt((fd), (lv), (op), (v), (l))
172 #define ft_os_close(fd) close((fd))
173 #define FT_SSL_FD(fd) (fd)
174  
175 119 static void ft_os_set_nonblock(int fd) {
176 119 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
177 119 }
178  
179 /* random bytes via /dev/urandom, falling back to a seeded LCG */
180 7 static void ft_os_random(unsigned char *out, size_t n) {
181 7 int fd = open("/dev/urandom", O_RDONLY);
182 7 size_t got = 0;
183 7 ▶ if (fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= 0) { 7 0 7
184 14 ▶ while (got < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (got < n) { 7 7 14
185 7 ssize_t r = read(fd, out + got, n - got);
186 7 ▶ if (r <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (r <= 0) break; 0 7 7
187 7 got += (size_t)r;
188 }
189 7 close(fd);
190 }
191 7 ▶ if (got < n) {
Branch: 1 branch
BranchTrueFalseTotal
if (got < n) { 0 7 7
192 static uint32_t seed;
193 size_t i;
194 0 ▶ if (!seed) seed = (uint32_t)(time(NULL) ^ (uintptr_t)&fd ^ (uintptr_t)getpid());
Branch: 1 branch
BranchTrueFalseTotal
if (!seed) seed = (uint32_t)(time(NULL) ^ (uintptr_t)&fd ^ (uintptr_t)getpid()); 0 0 0
195 0 ▶ for (i = got; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = got; i < n; i++) { 0 0 0
196 0 seed = seed * 1103515245u + 12345u;
197 0 out[i] = (unsigned char)(seed >> 16);
198 }
199 }
200 7 }
201  
202 #endif /* _WIN32 */
203  
204 /* Monotonic seconds, for deadlines and cache expiry. clock_gettime and
205 * CLOCK_MONOTONIC are POSIX, and no Windows toolchain has both: MSVC before
206 * VS2015 has neither the function nor struct timespec, and the mingw of the
207 * older Strawberry perls declares timespec but no CLOCK_MONOTONIC.
208 *
209 * QueryPerformanceCounter is declared unconditionally, is monotonic and has
210 * better resolution. GetTickCount64 would be the obvious choice and is not
211 * usable: the SDK hides it unless _WIN32_WINNT >= 0x0600, which perl's build
212 * does not set, so cl only warns C4013 and assumes it returns int - truncating
213 * the tick count to 24 days and making every deadline nonsense. */
214 #if defined(_WIN32)
215 static double ft_monotonic(void) {
216 static LARGE_INTEGER freq; /* idempotent: a race just recomputes it */
217 LARGE_INTEGER now;
218 if (freq.QuadPart == 0 && !QueryPerformanceFrequency(&freq))
219 freq.QuadPart = 1;
220 QueryPerformanceCounter(&now);
221 return (double)now.QuadPart / (double)freq.QuadPart;
222 }
223 #else
224 70 static double ft_monotonic(void) {
225 struct timespec ts;
226 70 clock_gettime(CLOCK_MONOTONIC, &ts);
227 70 return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9;
228 }
229 #endif
230  
231 /* memmem is a GNU/BSD extension the MinGW CRT lacks; use the C library's where
232 * it exists and a plain scan on Windows. The needles here (\r\n, \r\n\r\n) are
233 * tiny, so the fallback's O(n*m) is fine. */
234 #include <string.h>
235 #if defined(_WIN32)
236 static void *ft_memmem(const void *hay, size_t hlen, const void *ndl, size_t nlen) {
237 const unsigned char *h = (const unsigned char *)hay;
238 const unsigned char *n = (const unsigned char *)ndl;
239 size_t i;
240 if (nlen == 0) return (void *)hay;
241 if (hlen < nlen) return NULL;
242 for (i = 0; i + nlen <= hlen; i++)
243 if (h[i] == n[0] && memcmp(h + i, n, nlen) == 0) return (void *)(h + i);
244 return NULL;
245 }
246 #else
247 #define ft_memmem memmem
248 #endif
249  
250 /* strcasecmp/strncasecmp are POSIX, declared by <strings.h>. mingw has both
251 * the header and the names; MSVC has neither, only the underscore-prefixed CRT
252 * spellings out of <string.h>. Header parsing (ft_http.h, ft_cookiejar.h,
253 * ft_ua.h) leans on these, and under cl an undeclared function is only warning
254 * C4013 - it compiles, then fails at link - so the mapping has to be here. */
255 #if defined(_MSC_VER)
256 #define strcasecmp _stricmp
257 #define strncasecmp _strnicmp
258 #else
259 #include <strings.h>
260 #endif
261  
262 #endif /* FT_WIN_H */
« include/fetch/ft_tls.h ↑ Summary xs/abi.xs »