Commit | Line | Data |
---|---|---|
326b05e3 | 1 | /* win32sck.c |
68dc0745 | 2 | * |
3 | * (c) 1995 Microsoft Corporation. All rights reserved. | |
0d130a44 | 4 | * Developed by hip communications inc. |
68dc0745 | 5 | * Portions (c) 1993 Intergraph Corporation. All rights reserved. |
6 | * | |
7 | * You may distribute under the terms of either the GNU General Public | |
8 | * License or the Artistic License, as specified in the README file. | |
9 | */ | |
0a753a76 | 10 | |
390b85e7 | 11 | #define WIN32IO_IS_STDIO |
55d25626 | 12 | #define WIN32SCK_IS_STDSCK |
0a753a76 | 13 | #define WIN32_LEAN_AND_MEAN |
8fada4f6 | 14 | #define PERLIO_NOT_STDIO 0 |
a835ef8a NIS |
15 | #ifdef __GNUC__ |
16 | #define Win32_Winsock | |
17 | #endif | |
390b85e7 | 18 | #include <windows.h> |
036c1c1e SH |
19 | #include <ws2spi.h> |
20 | ||
0a753a76 | 21 | #include "EXTERN.h" |
22 | #include "perl.h" | |
c69f6586 | 23 | |
58a50f62 | 24 | #include "Win32iop.h" |
0a753a76 | 25 | #include <sys/socket.h> |
26 | #include <fcntl.h> | |
27 | #include <sys/stat.h> | |
28 | #include <assert.h> | |
390b85e7 | 29 | #include <io.h> |
0a753a76 | 30 | |
68dc0745 | 31 | /* thanks to Beverly Brown (beverly@datacube.com) */ |
72e6b643 SH |
32 | #define OPEN_SOCKET(x) win32_open_osfhandle(x,O_RDWR|O_BINARY) |
33 | #define TO_SOCKET(x) _get_osfhandle(x) | |
0a753a76 | 34 | |
326b05e3 GS |
35 | #define StartSockets() \ |
36 | STMT_START { \ | |
37 | if (!wsock_started) \ | |
38 | start_sockets(); \ | |
39 | } STMT_END | |
40 | ||
326b05e3 GS |
41 | #define SOCKET_TEST(x, y) \ |
42 | STMT_START { \ | |
43 | StartSockets(); \ | |
44 | if((x) == (y)) \ | |
22e30cb2 SH |
45 | { \ |
46 | int wsaerr = WSAGetLastError(); \ | |
47 | errno = convert_wsa_error_to_errno(wsaerr); \ | |
48 | SetLastError(wsaerr); \ | |
49 | } \ | |
326b05e3 GS |
50 | } STMT_END |
51 | ||
52 | #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR) | |
53 | ||
137443ea | 54 | static struct servent* win32_savecopyservent(struct servent*d, |
55 | struct servent*s, | |
56 | const char *proto); | |
0a753a76 | 57 | |
326b05e3 | 58 | static int wsock_started = 0; |
68dc0745 | 59 | |
b47a847f DD |
60 | #ifdef WIN32_DYN_IOINFO_SIZE |
61 | EXTERN_C Size_t w32_ioinfo_size; | |
62 | #endif | |
63 | ||
b73db59c GS |
64 | EXTERN_C void |
65 | EndSockets(void) | |
66 | { | |
67 | if (wsock_started) | |
68 | WSACleanup(); | |
69 | } | |
70 | ||
d31f3d60 SH |
71 | /* Translate WSAExxx values to corresponding Exxx values where possible. Not all |
72 | * WSAExxx constants have corresponding Exxx constants in <errno.h> (even in | |
73 | * VC++ 2010 and above, which have expanded <errno.h> with more values), but | |
74 | * most missing constants are provided by win32/include/sys/errno2.h. The few | |
75 | * that are not are returned unchanged. | |
76 | * | |
c9beaf97 SH |
77 | * The list of possible WSAExxx values used here comes from the MSDN page |
78 | * titled "Windows Sockets Error Codes". | |
d31f3d60 | 79 | * |
c9beaf97 SH |
80 | * (Note: Only the WSAExxx values are handled here; other WSAxxx values are |
81 | * returned unchanged. The return value normally ends up in errno/$! and at | |
82 | * the Perl code level may be tested against the Exxx constants exported by | |
83 | * the Errno and POSIX modules, which have never handled the other WSAxxx | |
84 | * values themselves, apparently without any ill effect so far.) | |
85 | */ | |
86 | int | |
87 | convert_wsa_error_to_errno(int wsaerr) | |
88 | { | |
89 | switch (wsaerr) { | |
b0ba2190 SH |
90 | case WSAEINTR: |
91 | return EINTR; | |
92 | case WSAEBADF: | |
93 | return EBADF; | |
94 | case WSAEACCES: | |
95 | return EACCES; | |
96 | case WSAEFAULT: | |
97 | return EFAULT; | |
98 | case WSAEINVAL: | |
99 | return EINVAL; | |
100 | case WSAEMFILE: | |
101 | return EMFILE; | |
102 | case WSAEWOULDBLOCK: | |
103 | return EWOULDBLOCK; | |
104 | case WSAEINPROGRESS: | |
105 | return EINPROGRESS; | |
106 | case WSAEALREADY: | |
107 | return EALREADY; | |
108 | case WSAENOTSOCK: | |
109 | return ENOTSOCK; | |
110 | case WSAEDESTADDRREQ: | |
111 | return EDESTADDRREQ; | |
112 | case WSAEMSGSIZE: | |
113 | return EMSGSIZE; | |
114 | case WSAEPROTOTYPE: | |
115 | return EPROTOTYPE; | |
116 | case WSAENOPROTOOPT: | |
117 | return ENOPROTOOPT; | |
118 | case WSAEPROTONOSUPPORT: | |
119 | return EPROTONOSUPPORT; | |
120 | case WSAESOCKTNOSUPPORT: | |
121 | return ESOCKTNOSUPPORT; | |
122 | case WSAEOPNOTSUPP: | |
123 | return EOPNOTSUPP; | |
124 | case WSAEPFNOSUPPORT: | |
125 | return EPFNOSUPPORT; | |
126 | case WSAEAFNOSUPPORT: | |
127 | return EAFNOSUPPORT; | |
128 | case WSAEADDRINUSE: | |
129 | return EADDRINUSE; | |
130 | case WSAEADDRNOTAVAIL: | |
131 | return EADDRNOTAVAIL; | |
132 | case WSAENETDOWN: | |
133 | return ENETDOWN; | |
134 | case WSAENETUNREACH: | |
135 | return ENETUNREACH; | |
136 | case WSAENETRESET: | |
137 | return ENETRESET; | |
138 | case WSAECONNABORTED: | |
139 | return ECONNABORTED; | |
140 | case WSAECONNRESET: | |
141 | return ECONNRESET; | |
142 | case WSAENOBUFS: | |
143 | return ENOBUFS; | |
144 | case WSAEISCONN: | |
145 | return EISCONN; | |
146 | case WSAENOTCONN: | |
147 | return ENOTCONN; | |
148 | case WSAESHUTDOWN: | |
149 | return ESHUTDOWN; | |
150 | case WSAETOOMANYREFS: | |
151 | return ETOOMANYREFS; | |
152 | case WSAETIMEDOUT: | |
153 | return ETIMEDOUT; | |
154 | case WSAECONNREFUSED: | |
155 | return ECONNREFUSED; | |
156 | case WSAELOOP: | |
157 | return ELOOP; | |
158 | case WSAENAMETOOLONG: | |
159 | return ENAMETOOLONG; | |
160 | case WSAEHOSTDOWN: | |
d31f3d60 | 161 | return WSAEHOSTDOWN; /* EHOSTDOWN is not defined */ |
b0ba2190 SH |
162 | case WSAEHOSTUNREACH: |
163 | return EHOSTUNREACH; | |
164 | case WSAENOTEMPTY: | |
165 | return ENOTEMPTY; | |
166 | case WSAEPROCLIM: | |
167 | return EPROCLIM; | |
168 | case WSAEUSERS: | |
169 | return EUSERS; | |
170 | case WSAEDQUOT: | |
171 | return EDQUOT; | |
172 | case WSAESTALE: | |
173 | return ESTALE; | |
174 | case WSAEREMOTE: | |
175 | return EREMOTE; | |
176 | case WSAEDISCON: | |
d31f3d60 | 177 | return WSAEDISCON; /* EDISCON is not defined */ |
b0ba2190 | 178 | case WSAENOMORE: |
d31f3d60 | 179 | return WSAENOMORE; /* ENOMORE is not defined */ |
4eaf3102 SH |
180 | #ifdef WSAECANCELLED |
181 | case WSAECANCELLED: /* New in WinSock2 */ | |
cd6a3131 | 182 | return ECANCELED; |
4eaf3102 | 183 | #endif |
b0ba2190 | 184 | case WSAEINVALIDPROCTABLE: |
d31f3d60 | 185 | return WSAEINVALIDPROCTABLE; /* EINVALIDPROCTABLE is not defined */ |
b0ba2190 | 186 | case WSAEINVALIDPROVIDER: |
d31f3d60 | 187 | return WSAEINVALIDPROVIDER; /* EINVALIDPROVIDER is not defined */ |
b0ba2190 | 188 | case WSAEPROVIDERFAILEDINIT: |
d31f3d60 | 189 | return WSAEPROVIDERFAILEDINIT; /* EPROVIDERFAILEDINIT is not defined */ |
b0ba2190 | 190 | case WSAEREFUSED: |
d31f3d60 | 191 | return WSAEREFUSED; /* EREFUSED is not defined */ |
b0ba2190 SH |
192 | } |
193 | ||
c9beaf97 | 194 | return wsaerr; |
b0ba2190 SH |
195 | } |
196 | ||
e85fa3eb | 197 | #ifdef ERRNO_HAS_POSIX_SUPPLEMENT |
cd6a3131 SH |
198 | /* Translate Exxx values in the POSIX supplement range defined in VC++ 2010 and |
199 | * above (EADDRINUSE <= err <= EWOULDBLOCK) to corresponding WSAExxx values. Not | |
4eaf3102 | 200 | * all such Exxx constants have corresponding WSAExxx constants in <winsock*.h>; |
cd6a3131 | 201 | * we just use ERROR_INVALID_FUNCTION for those that are missing but do not |
e85fa3eb SH |
202 | * really expect to encounter them anyway in the context in which this function |
203 | * is called. | |
4f79e9b1 SH |
204 | * Some versions of MinGW/gcc-4.8 and above also define most, but not all, of |
205 | * these extra Exxx values. The missing ones are all cases for which there is no | |
206 | * corresponding WSAExxx constant anyway, so we simply omit the cases for them | |
207 | * here. | |
cd6a3131 SH |
208 | * Other Exxx values (err < sys_nerr) are returned unchanged. |
209 | */ | |
210 | int | |
211 | convert_errno_to_wsa_error(int err) | |
212 | { | |
213 | switch (err) { | |
214 | case EADDRINUSE: | |
215 | return WSAEADDRINUSE; | |
216 | case EADDRNOTAVAIL: | |
217 | return WSAEADDRNOTAVAIL; | |
218 | case EAFNOSUPPORT: | |
219 | return WSAEAFNOSUPPORT; | |
220 | case EALREADY: | |
221 | return WSAEALREADY; | |
4f79e9b1 SH |
222 | #ifdef EBADMSG |
223 | case EBADMSG: /* Not defined in gcc-4.8.0 */ | |
cd6a3131 | 224 | return ERROR_INVALID_FUNCTION; |
4f79e9b1 | 225 | #endif |
cd6a3131 | 226 | case ECANCELED: |
4eaf3102 SH |
227 | #ifdef WSAECANCELLED |
228 | return WSAECANCELLED; /* New in WinSock2 */ | |
229 | #else | |
230 | return ERROR_INVALID_FUNCTION; | |
231 | #endif | |
cd6a3131 SH |
232 | case ECONNABORTED: |
233 | return WSAECONNABORTED; | |
234 | case ECONNREFUSED: | |
235 | return WSAECONNREFUSED; | |
236 | case ECONNRESET: | |
237 | return WSAECONNRESET; | |
238 | case EDESTADDRREQ: | |
239 | return WSAEDESTADDRREQ; | |
240 | case EHOSTUNREACH: | |
241 | return WSAEHOSTUNREACH; | |
4f79e9b1 SH |
242 | #ifdef EIDRM |
243 | case EIDRM: /* Not defined in gcc-4.8.0 */ | |
cd6a3131 | 244 | return ERROR_INVALID_FUNCTION; |
4f79e9b1 | 245 | #endif |
cd6a3131 SH |
246 | case EINPROGRESS: |
247 | return WSAEINPROGRESS; | |
248 | case EISCONN: | |
249 | return WSAEISCONN; | |
250 | case ELOOP: | |
251 | return WSAELOOP; | |
252 | case EMSGSIZE: | |
253 | return WSAEMSGSIZE; | |
254 | case ENETDOWN: | |
255 | return WSAENETDOWN; | |
256 | case ENETRESET: | |
257 | return WSAENETRESET; | |
258 | case ENETUNREACH: | |
259 | return WSAENETUNREACH; | |
260 | case ENOBUFS: | |
261 | return WSAENOBUFS; | |
4f79e9b1 SH |
262 | #ifdef ENODATA |
263 | case ENODATA: /* Not defined in gcc-4.8.0 */ | |
cd6a3131 | 264 | return ERROR_INVALID_FUNCTION; |
4f79e9b1 SH |
265 | #endif |
266 | #ifdef ENOLINK | |
267 | case ENOLINK: /* Not defined in gcc-4.8.0 */ | |
cd6a3131 | 268 | return ERROR_INVALID_FUNCTION; |
4f79e9b1 SH |
269 | #endif |
270 | #ifdef ENOMSG | |
271 | case ENOMSG: /* Not defined in gcc-4.8.0 */ | |
cd6a3131 | 272 | return ERROR_INVALID_FUNCTION; |
4f79e9b1 | 273 | #endif |
cd6a3131 SH |
274 | case ENOPROTOOPT: |
275 | return WSAENOPROTOOPT; | |
4f79e9b1 SH |
276 | #ifdef ENOSR |
277 | case ENOSR: /* Not defined in gcc-4.8.0 */ | |
cd6a3131 | 278 | return ERROR_INVALID_FUNCTION; |
4f79e9b1 SH |
279 | #endif |
280 | #ifdef ENOSTR | |
281 | case ENOSTR: /* Not defined in gcc-4.8.0 */ | |
cd6a3131 | 282 | return ERROR_INVALID_FUNCTION; |
4f79e9b1 | 283 | #endif |
cd6a3131 SH |
284 | case ENOTCONN: |
285 | return WSAENOTCONN; | |
4f79e9b1 SH |
286 | #ifdef ENOTRECOVERABLE |
287 | case ENOTRECOVERABLE: /* Not defined in gcc-4.8.0 */ | |
cd6a3131 | 288 | return ERROR_INVALID_FUNCTION; |
4f79e9b1 | 289 | #endif |
cd6a3131 SH |
290 | case ENOTSOCK: |
291 | return WSAENOTSOCK; | |
292 | case ENOTSUP: | |
293 | return ERROR_INVALID_FUNCTION; | |
294 | case EOPNOTSUPP: | |
295 | return WSAEOPNOTSUPP; | |
4f79e9b1 SH |
296 | #ifdef EOTHER |
297 | case EOTHER: /* Not defined in gcc-4.8.0 */ | |
cd6a3131 | 298 | return ERROR_INVALID_FUNCTION; |
4f79e9b1 | 299 | #endif |
cd6a3131 SH |
300 | case EOVERFLOW: |
301 | return ERROR_INVALID_FUNCTION; | |
302 | case EOWNERDEAD: | |
303 | return ERROR_INVALID_FUNCTION; | |
304 | case EPROTO: | |
305 | return ERROR_INVALID_FUNCTION; | |
306 | case EPROTONOSUPPORT: | |
307 | return WSAEPROTONOSUPPORT; | |
308 | case EPROTOTYPE: | |
309 | return WSAEPROTOTYPE; | |
4f79e9b1 SH |
310 | #ifdef ETIME |
311 | case ETIME: /* Not defined in gcc-4.8.0 */ | |
cd6a3131 | 312 | return ERROR_INVALID_FUNCTION; |
4f79e9b1 | 313 | #endif |
cd6a3131 SH |
314 | case ETIMEDOUT: |
315 | return WSAETIMEDOUT; | |
4f79e9b1 SH |
316 | #ifdef ETXTBSY |
317 | case ETXTBSY: /* Not defined in gcc-4.8.0 */ | |
cd6a3131 | 318 | return ERROR_INVALID_FUNCTION; |
4f79e9b1 | 319 | #endif |
cd6a3131 SH |
320 | case EWOULDBLOCK: |
321 | return WSAEWOULDBLOCK; | |
322 | } | |
323 | ||
324 | return err; | |
325 | } | |
e85fa3eb | 326 | #endif /* ERRNO_HAS_POSIX_SUPPLEMENT */ |
cd6a3131 | 327 | |
68dc0745 | 328 | void |
326b05e3 | 329 | start_sockets(void) |
68dc0745 | 330 | { |
331 | unsigned short version; | |
332 | WSADATA retdata; | |
333 | int ret; | |
68dc0745 | 334 | |
68dc0745 | 335 | /* |
336 | * initalize the winsock interface and insure that it is | |
337 | * cleaned up at exit. | |
338 | */ | |
036c1c1e | 339 | version = 0x2; |
326b05e3 | 340 | if(ret = WSAStartup(version, &retdata)) |
4f63d024 | 341 | Perl_croak_nocontext("Unable to locate winsock library!\n"); |
68dc0745 | 342 | if(retdata.wVersion != version) |
036c1c1e | 343 | Perl_croak_nocontext("Could not find version 2.0 of winsock dll\n"); |
68dc0745 | 344 | |
345 | /* atexit((void (*)(void)) EndSockets); */ | |
401ef382 SN |
346 | wsock_started = 1; |
347 | } | |
0a753a76 | 348 | |
36bb5e1d NC |
349 | /* in no sockets Win32 builds, these use the inline functions defined in |
350 | * perl.h | |
19253ae6 | 351 | */ |
68dc0745 | 352 | u_long |
353 | win32_htonl(u_long hostlong) | |
0a753a76 | 354 | { |
36bb5e1d | 355 | #ifndef WIN32_NO_SOCKETS |
326b05e3 | 356 | StartSockets(); |
19253ae6 | 357 | #endif |
326b05e3 | 358 | return htonl(hostlong); |
0a753a76 | 359 | } |
360 | ||
68dc0745 | 361 | u_short |
362 | win32_htons(u_short hostshort) | |
0a753a76 | 363 | { |
36bb5e1d | 364 | #ifndef WIN32_NO_SOCKETS |
326b05e3 | 365 | StartSockets(); |
19253ae6 | 366 | #endif |
326b05e3 | 367 | return htons(hostshort); |
0a753a76 | 368 | } |
369 | ||
68dc0745 | 370 | u_long |
371 | win32_ntohl(u_long netlong) | |
0a753a76 | 372 | { |
36bb5e1d | 373 | #ifndef WIN32_NO_SOCKETS |
326b05e3 | 374 | StartSockets(); |
19253ae6 | 375 | #endif |
326b05e3 | 376 | return ntohl(netlong); |
0a753a76 | 377 | } |
378 | ||
68dc0745 | 379 | u_short |
380 | win32_ntohs(u_short netshort) | |
0a753a76 | 381 | { |
36bb5e1d | 382 | #ifndef WIN32_NO_SOCKETS |
326b05e3 | 383 | StartSockets(); |
19253ae6 | 384 | #endif |
326b05e3 | 385 | return ntohs(netshort); |
0a753a76 | 386 | } |
387 | ||
388 | ||
0a753a76 | 389 | |
68dc0745 | 390 | SOCKET |
391 | win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen) | |
0a753a76 | 392 | { |
68dc0745 | 393 | SOCKET r; |
0a753a76 | 394 | |
326b05e3 | 395 | SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET); |
68dc0745 | 396 | return OPEN_SOCKET(r); |
0a753a76 | 397 | } |
398 | ||
68dc0745 | 399 | int |
400 | win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen) | |
0a753a76 | 401 | { |
68dc0745 | 402 | int r; |
0a753a76 | 403 | |
326b05e3 | 404 | SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen)); |
68dc0745 | 405 | return r; |
0a753a76 | 406 | } |
407 | ||
68dc0745 | 408 | int |
409 | win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen) | |
0a753a76 | 410 | { |
68dc0745 | 411 | int r; |
0a753a76 | 412 | |
326b05e3 | 413 | SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen)); |
68dc0745 | 414 | return r; |
0a753a76 | 415 | } |
416 | ||
417 | ||
68dc0745 | 418 | int |
419 | win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen) | |
0a753a76 | 420 | { |
68dc0745 | 421 | int r; |
0a753a76 | 422 | |
326b05e3 | 423 | SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen)); |
68dc0745 | 424 | return r; |
0a753a76 | 425 | } |
426 | ||
68dc0745 | 427 | int |
428 | win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen) | |
0a753a76 | 429 | { |
68dc0745 | 430 | int r; |
0a753a76 | 431 | |
326b05e3 | 432 | SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen)); |
68dc0745 | 433 | return r; |
0a753a76 | 434 | } |
435 | ||
68dc0745 | 436 | int |
437 | win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen) | |
0a753a76 | 438 | { |
68dc0745 | 439 | int r; |
0a753a76 | 440 | |
326b05e3 | 441 | SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen)); |
68dc0745 | 442 | return r; |
0a753a76 | 443 | } |
444 | ||
2d7a9237 | 445 | int |
68dc0745 | 446 | win32_ioctlsocket(SOCKET s, long cmd, u_long *argp) |
0a753a76 | 447 | { |
68dc0745 | 448 | int r; |
0a753a76 | 449 | |
326b05e3 | 450 | SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp)); |
68dc0745 | 451 | return r; |
0a753a76 | 452 | } |
453 | ||
68dc0745 | 454 | int |
455 | win32_listen(SOCKET s, int backlog) | |
0a753a76 | 456 | { |
68dc0745 | 457 | int r; |
0a753a76 | 458 | |
326b05e3 | 459 | SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog)); |
68dc0745 | 460 | return r; |
0a753a76 | 461 | } |
462 | ||
68dc0745 | 463 | int |
464 | win32_recv(SOCKET s, char *buf, int len, int flags) | |
0a753a76 | 465 | { |
68dc0745 | 466 | int r; |
0a753a76 | 467 | |
326b05e3 | 468 | SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags)); |
68dc0745 | 469 | return r; |
0a753a76 | 470 | } |
471 | ||
68dc0745 | 472 | int |
473 | win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen) | |
0a753a76 | 474 | { |
68dc0745 | 475 | int r; |
e4449fe1 | 476 | int frombufsize = *fromlen; |
0a753a76 | 477 | |
326b05e3 | 478 | SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen)); |
e4449fe1 GS |
479 | /* Winsock's recvfrom() only returns a valid 'from' when the socket |
480 | * is connectionless. Perl expects a valid 'from' for all types | |
481 | * of sockets, so go the extra mile. | |
482 | */ | |
483 | if (r != SOCKET_ERROR && frombufsize == *fromlen) | |
484 | (void)win32_getpeername(s, from, fromlen); | |
68dc0745 | 485 | return r; |
0a753a76 | 486 | } |
487 | ||
68dc0745 | 488 | /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */ |
489 | int | |
55d25626 | 490 | win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout) |
0a753a76 | 491 | { |
55d25626 | 492 | int r; |
3ead22cb | 493 | int i, fd, save_errno = errno; |
8dc00b2f | 494 | FD_SET nrd, nwr, nex; |
e4d771f5 | 495 | bool just_sleep = TRUE; |
0a753a76 | 496 | |
f2bb5751 | 497 | StartSockets(); |
0a753a76 | 498 | |
68dc0745 | 499 | FD_ZERO(&nrd); |
500 | FD_ZERO(&nwr); | |
501 | FD_ZERO(&nex); | |
502 | for (i = 0; i < nfds; i++) { | |
f7bbabd3 JD |
503 | if (rd && PERL_FD_ISSET(i,rd)) { |
504 | fd = TO_SOCKET(i); | |
47660177 | 505 | FD_SET((unsigned)fd, &nrd); |
e4d771f5 | 506 | just_sleep = FALSE; |
f7bbabd3 JD |
507 | } |
508 | if (wr && PERL_FD_ISSET(i,wr)) { | |
509 | fd = TO_SOCKET(i); | |
47660177 | 510 | FD_SET((unsigned)fd, &nwr); |
e4d771f5 | 511 | just_sleep = FALSE; |
f7bbabd3 JD |
512 | } |
513 | if (ex && PERL_FD_ISSET(i,ex)) { | |
514 | fd = TO_SOCKET(i); | |
47660177 | 515 | FD_SET((unsigned)fd, &nex); |
e4d771f5 | 516 | just_sleep = FALSE; |
f7bbabd3 | 517 | } |
68dc0745 | 518 | } |
519 | ||
e4d771f5 JD |
520 | /* winsock seems incapable of dealing with all three fd_sets being empty, |
521 | * so do the (millisecond) sleep as a special case | |
522 | */ | |
523 | if (just_sleep) { | |
524 | if (timeout) | |
525 | Sleep(timeout->tv_sec * 1000 + | |
526 | timeout->tv_usec / 1000); /* do the best we can */ | |
527 | else | |
528 | Sleep(UINT_MAX); | |
529 | return 0; | |
530 | } | |
531 | ||
1c878075 | 532 | errno = save_errno; |
6fc8e913 | 533 | SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, (PTIMEVAL)timeout)); |
1c878075 | 534 | save_errno = errno; |
0a753a76 | 535 | |
68dc0745 | 536 | for (i = 0; i < nfds; i++) { |
f7bbabd3 JD |
537 | if (rd && PERL_FD_ISSET(i,rd)) { |
538 | fd = TO_SOCKET(i); | |
539 | if (!FD_ISSET(fd, &nrd)) | |
540 | PERL_FD_CLR(i,rd); | |
541 | } | |
542 | if (wr && PERL_FD_ISSET(i,wr)) { | |
543 | fd = TO_SOCKET(i); | |
544 | if (!FD_ISSET(fd, &nwr)) | |
545 | PERL_FD_CLR(i,wr); | |
546 | } | |
547 | if (ex && PERL_FD_ISSET(i,ex)) { | |
548 | fd = TO_SOCKET(i); | |
549 | if (!FD_ISSET(fd, &nex)) | |
550 | PERL_FD_CLR(i,ex); | |
551 | } | |
68dc0745 | 552 | } |
1c878075 | 553 | errno = save_errno; |
68dc0745 | 554 | return r; |
0a753a76 | 555 | } |
556 | ||
68dc0745 | 557 | int |
558 | win32_send(SOCKET s, const char *buf, int len, int flags) | |
0a753a76 | 559 | { |
68dc0745 | 560 | int r; |
0a753a76 | 561 | |
326b05e3 | 562 | SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags)); |
68dc0745 | 563 | return r; |
0a753a76 | 564 | } |
565 | ||
68dc0745 | 566 | int |
567 | win32_sendto(SOCKET s, const char *buf, int len, int flags, | |
568 | const struct sockaddr *to, int tolen) | |
0a753a76 | 569 | { |
68dc0745 | 570 | int r; |
0a753a76 | 571 | |
326b05e3 | 572 | SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen)); |
68dc0745 | 573 | return r; |
0a753a76 | 574 | } |
575 | ||
68dc0745 | 576 | int |
577 | win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen) | |
0a753a76 | 578 | { |
68dc0745 | 579 | int r; |
0a753a76 | 580 | |
326b05e3 | 581 | SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen)); |
68dc0745 | 582 | return r; |
0a753a76 | 583 | } |
584 | ||
68dc0745 | 585 | int |
586 | win32_shutdown(SOCKET s, int how) | |
0a753a76 | 587 | { |
68dc0745 | 588 | int r; |
0a753a76 | 589 | |
326b05e3 | 590 | SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how)); |
68dc0745 | 591 | return r; |
0a753a76 | 592 | } |
593 | ||
3a25acb4 GS |
594 | int |
595 | win32_closesocket(SOCKET s) | |
596 | { | |
597 | int r; | |
598 | ||
599 | SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s))); | |
600 | return r; | |
601 | } | |
602 | ||
036c1c1e SH |
603 | void |
604 | convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out) | |
605 | { | |
606 | Copy(in, out, 1, WSAPROTOCOL_INFOA); | |
607 | wcstombs(out->szProtocol, in->szProtocol, sizeof(out->szProtocol)); | |
608 | } | |
609 | ||
610 | SOCKET | |
611 | open_ifs_socket(int af, int type, int protocol) | |
612 | { | |
1c972609 SH |
613 | dTHX; |
614 | char *s; | |
036c1c1e SH |
615 | unsigned long proto_buffers_len = 0; |
616 | int error_code; | |
617 | SOCKET out = INVALID_SOCKET; | |
618 | ||
1c972609 SH |
619 | if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s)) |
620 | return WSASocket(af, type, protocol, NULL, 0, 0); | |
621 | ||
036c1c1e SH |
622 | if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR |
623 | && error_code == WSAENOBUFS) | |
624 | { | |
625 | WSAPROTOCOL_INFOW *proto_buffers; | |
626 | int protocols_available = 0; | |
627 | ||
a02a5408 | 628 | Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW), |
036c1c1e SH |
629 | WSAPROTOCOL_INFOW); |
630 | ||
631 | if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers, | |
632 | &proto_buffers_len, &error_code)) != SOCKET_ERROR) | |
633 | { | |
634 | int i; | |
635 | for (i = 0; i < protocols_available; i++) | |
636 | { | |
637 | WSAPROTOCOL_INFOA proto_info; | |
638 | ||
639 | if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily) | |
640 | || (type != proto_buffers[i].iSocketType) | |
f9169742 SH |
641 | || (protocol != 0 && proto_buffers[i].iProtocol != 0 && |
642 | protocol != proto_buffers[i].iProtocol)) | |
036c1c1e SH |
643 | continue; |
644 | ||
645 | if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0) | |
646 | continue; | |
647 | ||
648 | convert_proto_info_w2a(&(proto_buffers[i]), &proto_info); | |
649 | ||
650 | out = WSASocket(af, type, protocol, &proto_info, 0, 0); | |
651 | break; | |
652 | } | |
653 | } | |
654 | ||
655 | Safefree(proto_buffers); | |
656 | } | |
657 | ||
658 | return out; | |
659 | } | |
660 | ||
68dc0745 | 661 | SOCKET |
662 | win32_socket(int af, int type, int protocol) | |
0a753a76 | 663 | { |
68dc0745 | 664 | SOCKET s; |
0a753a76 | 665 | |
3a25acb4 | 666 | StartSockets(); |
036c1c1e | 667 | |
72e6b643 | 668 | if((s = open_ifs_socket(af, type, protocol)) == INVALID_SOCKET) |
22e30cb2 SH |
669 | { |
670 | int wsaerr = WSAGetLastError(); | |
671 | errno = convert_wsa_error_to_errno(wsaerr); | |
672 | SetLastError(wsaerr); | |
673 | } | |
68dc0745 | 674 | else |
675 | s = OPEN_SOCKET(s); | |
0a753a76 | 676 | |
68dc0745 | 677 | return s; |
0a753a76 | 678 | } |
679 | ||
00b02797 JH |
680 | /* |
681 | * close RTL fd while respecting sockets | |
682 | * added as temporary measure until PerlIO has real | |
683 | * Win32 native layer | |
684 | * -- BKS, 11-11-2000 | |
685 | */ | |
686 | ||
687 | int my_close(int fd) | |
688 | { | |
689 | int osf; | |
690 | if (!wsock_started) /* No WinSock? */ | |
691 | return(close(fd)); /* Then not a socket. */ | |
692 | osf = TO_SOCKET(fd);/* Get it now before it's gone! */ | |
693 | if (osf != -1) { | |
694 | int err; | |
695 | err = closesocket(osf); | |
696 | if (err == 0) { | |
1f664ef5 | 697 | #ifdef _set_osfhnd |
b47a847f DD |
698 | assert(_osfhnd(fd) == osf); /* catch a bad ioinfo struct def */ |
699 | /* don't close freed handle */ | |
700 | _set_osfhnd(fd, INVALID_HANDLE_VALUE); | |
701 | return close(fd); | |
1f664ef5 SH |
702 | #else |
703 | (void)close(fd); /* handle already closed, ignore error */ | |
704 | return 0; | |
705 | #endif | |
00b02797 JH |
706 | } |
707 | else if (err == SOCKET_ERROR) { | |
22e30cb2 SH |
708 | int wsaerr = WSAGetLastError(); |
709 | err = convert_wsa_error_to_errno(wsaerr); | |
b0ba2190 | 710 | if (err != ENOTSOCK) { |
00b02797 JH |
711 | (void)close(fd); |
712 | errno = err; | |
22e30cb2 | 713 | SetLastError(wsaerr); |
00b02797 JH |
714 | return EOF; |
715 | } | |
716 | } | |
717 | } | |
718 | return close(fd); | |
719 | } | |
720 | ||
fb73857a | 721 | #undef fclose |
722 | int | |
723 | my_fclose (FILE *pf) | |
724 | { | |
4e945249 | 725 | int osf; |
326b05e3 GS |
726 | if (!wsock_started) /* No WinSock? */ |
727 | return(fclose(pf)); /* Then not a socket. */ | |
00b02797 | 728 | osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */ |
d3d49e29 GS |
729 | if (osf != -1) { |
730 | int err; | |
731 | win32_fflush(pf); | |
732 | err = closesocket(osf); | |
733 | if (err == 0) { | |
1f664ef5 | 734 | #ifdef _set_osfhnd |
b47a847f DD |
735 | assert(_osfhnd(win32_fileno(pf)) == osf); /* catch a bad ioinfo struct def */ |
736 | /* don't close freed handle */ | |
737 | _set_osfhnd(win32_fileno(pf), INVALID_HANDLE_VALUE); | |
738 | return fclose(pf); | |
1f664ef5 SH |
739 | #else |
740 | (void)fclose(pf); /* handle already closed, ignore error */ | |
741 | return 0; | |
742 | #endif | |
d3d49e29 GS |
743 | } |
744 | else if (err == SOCKET_ERROR) { | |
22e30cb2 SH |
745 | int wsaerr = WSAGetLastError(); |
746 | err = convert_wsa_error_to_errno(wsaerr); | |
b0ba2190 | 747 | if (err != ENOTSOCK) { |
d3d49e29 GS |
748 | (void)fclose(pf); |
749 | errno = err; | |
22e30cb2 | 750 | SetLastError(wsaerr); |
d3d49e29 GS |
751 | return EOF; |
752 | } | |
753 | } | |
326b05e3 | 754 | } |
d3d49e29 | 755 | return fclose(pf); |
fb73857a | 756 | } |
757 | ||
68dc0745 | 758 | struct hostent * |
759 | win32_gethostbyaddr(const char *addr, int len, int type) | |
0a753a76 | 760 | { |
68dc0745 | 761 | struct hostent *r; |
0a753a76 | 762 | |
326b05e3 | 763 | SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL); |
68dc0745 | 764 | return r; |
0a753a76 | 765 | } |
766 | ||
68dc0745 | 767 | struct hostent * |
768 | win32_gethostbyname(const char *name) | |
0a753a76 | 769 | { |
68dc0745 | 770 | struct hostent *r; |
0a753a76 | 771 | |
326b05e3 | 772 | SOCKET_TEST(r = gethostbyname(name), NULL); |
68dc0745 | 773 | return r; |
0a753a76 | 774 | } |
775 | ||
68dc0745 | 776 | int |
777 | win32_gethostname(char *name, int len) | |
0a753a76 | 778 | { |
68dc0745 | 779 | int r; |
0a753a76 | 780 | |
326b05e3 | 781 | SOCKET_TEST_ERROR(r = gethostname(name, len)); |
68dc0745 | 782 | return r; |
0a753a76 | 783 | } |
784 | ||
68dc0745 | 785 | struct protoent * |
786 | win32_getprotobyname(const char *name) | |
0a753a76 | 787 | { |
68dc0745 | 788 | struct protoent *r; |
0a753a76 | 789 | |
326b05e3 | 790 | SOCKET_TEST(r = getprotobyname(name), NULL); |
68dc0745 | 791 | return r; |
0a753a76 | 792 | } |
793 | ||
68dc0745 | 794 | struct protoent * |
795 | win32_getprotobynumber(int num) | |
0a753a76 | 796 | { |
68dc0745 | 797 | struct protoent *r; |
0a753a76 | 798 | |
326b05e3 | 799 | SOCKET_TEST(r = getprotobynumber(num), NULL); |
68dc0745 | 800 | return r; |
0a753a76 | 801 | } |
802 | ||
68dc0745 | 803 | struct servent * |
804 | win32_getservbyname(const char *name, const char *proto) | |
0a753a76 | 805 | { |
04a2c3d9 | 806 | dTHXa(NULL); |
68dc0745 | 807 | struct servent *r; |
c53bd28a | 808 | |
326b05e3 | 809 | SOCKET_TEST(r = getservbyname(name, proto), NULL); |
68dc0745 | 810 | if (r) { |
04a2c3d9 | 811 | aTHXa(PERL_GET_THX); |
3352bfcb | 812 | r = win32_savecopyservent(&w32_servent, r, proto); |
68dc0745 | 813 | } |
814 | return r; | |
0a753a76 | 815 | } |
816 | ||
68dc0745 | 817 | struct servent * |
818 | win32_getservbyport(int port, const char *proto) | |
0a753a76 | 819 | { |
04a2c3d9 | 820 | dTHXa(NULL); |
68dc0745 | 821 | struct servent *r; |
0a753a76 | 822 | |
326b05e3 | 823 | SOCKET_TEST(r = getservbyport(port, proto), NULL); |
68dc0745 | 824 | if (r) { |
04a2c3d9 | 825 | aTHXa(PERL_GET_THX); |
3352bfcb | 826 | r = win32_savecopyservent(&w32_servent, r, proto); |
68dc0745 | 827 | } |
828 | return r; | |
0a753a76 | 829 | } |
830 | ||
f998180f GS |
831 | int |
832 | win32_ioctl(int i, unsigned int u, char *data) | |
833 | { | |
69a143a6 | 834 | u_long u_long_arg; |
f998180f | 835 | int retval; |
69a143a6 | 836 | |
f998180f | 837 | if (!wsock_started) { |
4f63d024 | 838 | Perl_croak_nocontext("ioctl implemented only on sockets"); |
f998180f GS |
839 | /* NOTREACHED */ |
840 | } | |
841 | ||
69a143a6 YO |
842 | /* mauke says using memcpy avoids alignment issues */ |
843 | memcpy(&u_long_arg, data, sizeof u_long_arg); | |
844 | retval = ioctlsocket(TO_SOCKET(i), (long)u, &u_long_arg); | |
845 | memcpy(data, &u_long_arg, sizeof u_long_arg); | |
846 | ||
f998180f | 847 | if (retval == SOCKET_ERROR) { |
22e30cb2 SH |
848 | int wsaerr = WSAGetLastError(); |
849 | int err = convert_wsa_error_to_errno(wsaerr); | |
b0ba2190 | 850 | if (err == ENOTSOCK) { |
4f63d024 | 851 | Perl_croak_nocontext("ioctl implemented only on sockets"); |
f998180f GS |
852 | /* NOTREACHED */ |
853 | } | |
b0ba2190 | 854 | errno = err; |
22e30cb2 | 855 | SetLastError(wsaerr); |
f998180f GS |
856 | } |
857 | return retval; | |
858 | } | |
859 | ||
68dc0745 | 860 | char FAR * |
861 | win32_inet_ntoa(struct in_addr in) | |
0a753a76 | 862 | { |
326b05e3 GS |
863 | StartSockets(); |
864 | return inet_ntoa(in); | |
0a753a76 | 865 | } |
866 | ||
68dc0745 | 867 | unsigned long |
868 | win32_inet_addr(const char FAR *cp) | |
0a753a76 | 869 | { |
326b05e3 GS |
870 | StartSockets(); |
871 | return inet_addr(cp); | |
0a753a76 | 872 | } |
68dc0745 | 873 | |
874 | /* | |
875 | * Networking stubs | |
876 | */ | |
0a753a76 | 877 | |
68dc0745 | 878 | void |
879 | win32_endhostent() | |
0a753a76 | 880 | { |
073dd035 | 881 | win32_croak_not_implemented("endhostent"); |
0a753a76 | 882 | } |
883 | ||
68dc0745 | 884 | void |
885 | win32_endnetent() | |
0a753a76 | 886 | { |
073dd035 | 887 | win32_croak_not_implemented("endnetent"); |
0a753a76 | 888 | } |
889 | ||
68dc0745 | 890 | void |
891 | win32_endprotoent() | |
0a753a76 | 892 | { |
073dd035 | 893 | win32_croak_not_implemented("endprotoent"); |
0a753a76 | 894 | } |
895 | ||
68dc0745 | 896 | void |
897 | win32_endservent() | |
0a753a76 | 898 | { |
073dd035 | 899 | win32_croak_not_implemented("endservent"); |
0a753a76 | 900 | } |
901 | ||
902 | ||
68dc0745 | 903 | struct netent * |
904 | win32_getnetent(void) | |
0a753a76 | 905 | { |
073dd035 | 906 | win32_croak_not_implemented("getnetent"); |
68dc0745 | 907 | return (struct netent *) NULL; |
0a753a76 | 908 | } |
909 | ||
68dc0745 | 910 | struct netent * |
911 | win32_getnetbyname(char *name) | |
0a753a76 | 912 | { |
073dd035 | 913 | win32_croak_not_implemented("getnetbyname"); |
68dc0745 | 914 | return (struct netent *)NULL; |
0a753a76 | 915 | } |
916 | ||
68dc0745 | 917 | struct netent * |
918 | win32_getnetbyaddr(long net, int type) | |
0a753a76 | 919 | { |
073dd035 | 920 | win32_croak_not_implemented("getnetbyaddr"); |
68dc0745 | 921 | return (struct netent *)NULL; |
0a753a76 | 922 | } |
923 | ||
68dc0745 | 924 | struct protoent * |
925 | win32_getprotoent(void) | |
0a753a76 | 926 | { |
073dd035 | 927 | win32_croak_not_implemented("getprotoent"); |
68dc0745 | 928 | return (struct protoent *) NULL; |
0a753a76 | 929 | } |
930 | ||
68dc0745 | 931 | struct servent * |
932 | win32_getservent(void) | |
0a753a76 | 933 | { |
073dd035 | 934 | win32_croak_not_implemented("getservent"); |
68dc0745 | 935 | return (struct servent *) NULL; |
0a753a76 | 936 | } |
937 | ||
68dc0745 | 938 | void |
939 | win32_sethostent(int stayopen) | |
0a753a76 | 940 | { |
073dd035 | 941 | win32_croak_not_implemented("sethostent"); |
0a753a76 | 942 | } |
943 | ||
944 | ||
68dc0745 | 945 | void |
946 | win32_setnetent(int stayopen) | |
0a753a76 | 947 | { |
073dd035 | 948 | win32_croak_not_implemented("setnetent"); |
0a753a76 | 949 | } |
950 | ||
951 | ||
68dc0745 | 952 | void |
953 | win32_setprotoent(int stayopen) | |
0a753a76 | 954 | { |
073dd035 | 955 | win32_croak_not_implemented("setprotoent"); |
0a753a76 | 956 | } |
957 | ||
958 | ||
68dc0745 | 959 | void |
960 | win32_setservent(int stayopen) | |
0a753a76 | 961 | { |
073dd035 | 962 | win32_croak_not_implemented("setservent"); |
0a753a76 | 963 | } |
964 | ||
137443ea | 965 | static struct servent* |
966 | win32_savecopyservent(struct servent*d, struct servent*s, const char *proto) | |
967 | { | |
968 | d->s_name = s->s_name; | |
969 | d->s_aliases = s->s_aliases; | |
970 | d->s_port = s->s_port; | |
8cbe99e5 | 971 | if (s->s_proto && strlen(s->s_proto)) |
137443ea | 972 | d->s_proto = s->s_proto; |
0af56dfe | 973 | else |
c69f6586 | 974 | if (proto && strlen(proto)) |
137443ea | 975 | d->s_proto = (char *)proto; |
976 | else | |
977 | d->s_proto = "tcp"; | |
978 | ||
979 | return d; | |
980 | } | |
981 | ||
0a753a76 | 982 |