Commit | Line | Data |
---|---|---|
326b05e3 | 1 | /* win32sck.c |
68dc0745 | 2 | * |
3 | * (c) 1995 Microsoft Corporation. All rights reserved. | |
4 | * Developed by hip communications inc., http://info.hip.com/info/ | |
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) */ |
326b05e3 | 32 | #ifdef USE_SOCKETS_AS_HANDLES |
58a50f62 | 33 | # define OPEN_SOCKET(x) win32_open_osfhandle(x,O_RDWR|O_BINARY) |
326b05e3 | 34 | # define TO_SOCKET(x) _get_osfhandle(x) |
0a753a76 | 35 | #else |
0a753a76 | 36 | # define OPEN_SOCKET(x) (x) |
37 | # define TO_SOCKET(x) (x) | |
68dc0745 | 38 | #endif /* USE_SOCKETS_AS_HANDLES */ |
0a753a76 | 39 | |
326b05e3 GS |
40 | #define StartSockets() \ |
41 | STMT_START { \ | |
42 | if (!wsock_started) \ | |
43 | start_sockets(); \ | |
44 | } STMT_END | |
45 | ||
326b05e3 GS |
46 | #define SOCKET_TEST(x, y) \ |
47 | STMT_START { \ | |
48 | StartSockets(); \ | |
49 | if((x) == (y)) \ | |
50 | errno = WSAGetLastError(); \ | |
51 | } STMT_END | |
52 | ||
53 | #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR) | |
54 | ||
137443ea | 55 | static struct servent* win32_savecopyservent(struct servent*d, |
56 | struct servent*s, | |
57 | const char *proto); | |
0a753a76 | 58 | |
326b05e3 | 59 | static int wsock_started = 0; |
68dc0745 | 60 | |
b73db59c GS |
61 | EXTERN_C void |
62 | EndSockets(void) | |
63 | { | |
64 | if (wsock_started) | |
65 | WSACleanup(); | |
66 | } | |
67 | ||
68dc0745 | 68 | void |
326b05e3 | 69 | start_sockets(void) |
68dc0745 | 70 | { |
acfe0abc | 71 | dTHX; |
68dc0745 | 72 | unsigned short version; |
73 | WSADATA retdata; | |
74 | int ret; | |
68dc0745 | 75 | |
68dc0745 | 76 | /* |
77 | * initalize the winsock interface and insure that it is | |
78 | * cleaned up at exit. | |
79 | */ | |
036c1c1e | 80 | version = 0x2; |
326b05e3 | 81 | if(ret = WSAStartup(version, &retdata)) |
4f63d024 | 82 | Perl_croak_nocontext("Unable to locate winsock library!\n"); |
68dc0745 | 83 | if(retdata.wVersion != version) |
036c1c1e | 84 | Perl_croak_nocontext("Could not find version 2.0 of winsock dll\n"); |
68dc0745 | 85 | |
86 | /* atexit((void (*)(void)) EndSockets); */ | |
401ef382 SN |
87 | wsock_started = 1; |
88 | } | |
0a753a76 | 89 | |
0a753a76 | 90 | #ifndef USE_SOCKETS_AS_HANDLES |
f3986ebb | 91 | #undef fdopen |
68dc0745 | 92 | FILE * |
f3986ebb | 93 | my_fdopen(int fd, char *mode) |
0a753a76 | 94 | { |
68dc0745 | 95 | FILE *fp; |
96 | char sockbuf[256]; | |
97 | int optlen = sizeof(sockbuf); | |
98 | int retval; | |
0a753a76 | 99 | |
326b05e3 | 100 | if (!wsock_started) |
fb73857a | 101 | return(fdopen(fd, mode)); |
0a753a76 | 102 | |
326b05e3 GS |
103 | retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen); |
104 | if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) { | |
fb73857a | 105 | return(fdopen(fd, mode)); |
68dc0745 | 106 | } |
0a753a76 | 107 | |
68dc0745 | 108 | /* |
109 | * If we get here, then fd is actually a socket. | |
110 | */ | |
a02a5408 | 111 | Newxz(fp, 1, FILE); /* XXX leak, good thing this code isn't used */ |
68dc0745 | 112 | if(fp == NULL) { |
113 | errno = ENOMEM; | |
114 | return NULL; | |
115 | } | |
0a753a76 | 116 | |
68dc0745 | 117 | fp->_file = fd; |
118 | if(*mode == 'r') | |
119 | fp->_flag = _IOREAD; | |
120 | else | |
121 | fp->_flag = _IOWRT; | |
122 | ||
123 | return fp; | |
0a753a76 | 124 | } |
68dc0745 | 125 | #endif /* USE_SOCKETS_AS_HANDLES */ |
0a753a76 | 126 | |
127 | ||
68dc0745 | 128 | u_long |
129 | win32_htonl(u_long hostlong) | |
0a753a76 | 130 | { |
326b05e3 GS |
131 | StartSockets(); |
132 | return htonl(hostlong); | |
0a753a76 | 133 | } |
134 | ||
68dc0745 | 135 | u_short |
136 | win32_htons(u_short hostshort) | |
0a753a76 | 137 | { |
326b05e3 GS |
138 | StartSockets(); |
139 | return htons(hostshort); | |
0a753a76 | 140 | } |
141 | ||
68dc0745 | 142 | u_long |
143 | win32_ntohl(u_long netlong) | |
0a753a76 | 144 | { |
326b05e3 GS |
145 | StartSockets(); |
146 | return ntohl(netlong); | |
0a753a76 | 147 | } |
148 | ||
68dc0745 | 149 | u_short |
150 | win32_ntohs(u_short netshort) | |
0a753a76 | 151 | { |
326b05e3 GS |
152 | StartSockets(); |
153 | return ntohs(netshort); | |
0a753a76 | 154 | } |
155 | ||
156 | ||
0a753a76 | 157 | |
68dc0745 | 158 | SOCKET |
159 | win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen) | |
0a753a76 | 160 | { |
68dc0745 | 161 | SOCKET r; |
0a753a76 | 162 | |
326b05e3 | 163 | SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET); |
68dc0745 | 164 | return OPEN_SOCKET(r); |
0a753a76 | 165 | } |
166 | ||
68dc0745 | 167 | int |
168 | win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen) | |
0a753a76 | 169 | { |
68dc0745 | 170 | int r; |
0a753a76 | 171 | |
326b05e3 | 172 | SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen)); |
68dc0745 | 173 | return r; |
0a753a76 | 174 | } |
175 | ||
68dc0745 | 176 | int |
177 | win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen) | |
0a753a76 | 178 | { |
68dc0745 | 179 | int r; |
0a753a76 | 180 | |
326b05e3 | 181 | SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen)); |
68dc0745 | 182 | return r; |
0a753a76 | 183 | } |
184 | ||
185 | ||
68dc0745 | 186 | int |
187 | win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen) | |
0a753a76 | 188 | { |
68dc0745 | 189 | int r; |
0a753a76 | 190 | |
326b05e3 | 191 | SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen)); |
68dc0745 | 192 | return r; |
0a753a76 | 193 | } |
194 | ||
68dc0745 | 195 | int |
196 | win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen) | |
0a753a76 | 197 | { |
68dc0745 | 198 | int r; |
0a753a76 | 199 | |
326b05e3 | 200 | SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen)); |
68dc0745 | 201 | return r; |
0a753a76 | 202 | } |
203 | ||
68dc0745 | 204 | int |
205 | win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen) | |
0a753a76 | 206 | { |
68dc0745 | 207 | int r; |
0a753a76 | 208 | |
326b05e3 | 209 | SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen)); |
68dc0745 | 210 | return r; |
0a753a76 | 211 | } |
212 | ||
2d7a9237 | 213 | int |
68dc0745 | 214 | win32_ioctlsocket(SOCKET s, long cmd, u_long *argp) |
0a753a76 | 215 | { |
68dc0745 | 216 | int r; |
0a753a76 | 217 | |
326b05e3 | 218 | SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp)); |
68dc0745 | 219 | return r; |
0a753a76 | 220 | } |
221 | ||
68dc0745 | 222 | int |
223 | win32_listen(SOCKET s, int backlog) | |
0a753a76 | 224 | { |
68dc0745 | 225 | int r; |
0a753a76 | 226 | |
326b05e3 | 227 | SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog)); |
68dc0745 | 228 | return r; |
0a753a76 | 229 | } |
230 | ||
68dc0745 | 231 | int |
232 | win32_recv(SOCKET s, char *buf, int len, int flags) | |
0a753a76 | 233 | { |
68dc0745 | 234 | int r; |
0a753a76 | 235 | |
326b05e3 | 236 | SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags)); |
68dc0745 | 237 | return r; |
0a753a76 | 238 | } |
239 | ||
68dc0745 | 240 | int |
241 | win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen) | |
0a753a76 | 242 | { |
68dc0745 | 243 | int r; |
e4449fe1 | 244 | int frombufsize = *fromlen; |
0a753a76 | 245 | |
326b05e3 | 246 | SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen)); |
e4449fe1 GS |
247 | /* Winsock's recvfrom() only returns a valid 'from' when the socket |
248 | * is connectionless. Perl expects a valid 'from' for all types | |
249 | * of sockets, so go the extra mile. | |
250 | */ | |
251 | if (r != SOCKET_ERROR && frombufsize == *fromlen) | |
252 | (void)win32_getpeername(s, from, fromlen); | |
68dc0745 | 253 | return r; |
0a753a76 | 254 | } |
255 | ||
68dc0745 | 256 | /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */ |
257 | int | |
55d25626 | 258 | win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout) |
0a753a76 | 259 | { |
55d25626 GS |
260 | int r; |
261 | #ifdef USE_SOCKETS_AS_HANDLES | |
3ead22cb | 262 | int i, fd, save_errno = errno; |
8dc00b2f | 263 | FD_SET nrd, nwr, nex; |
0a753a76 | 264 | |
7e3be867 GS |
265 | /* winsock seems incapable of dealing with all three null fd_sets, |
266 | * so do the (millisecond) sleep as a special case | |
267 | */ | |
268 | if (!(rd || wr || ex)) { | |
87c2f9c4 BZ |
269 | if (timeout) |
270 | Sleep(timeout->tv_sec * 1000 + | |
271 | timeout->tv_usec / 1000); /* do the best we can */ | |
272 | else | |
273 | Sleep(UINT_MAX); | |
7e3be867 GS |
274 | return 0; |
275 | } | |
f2bb5751 | 276 | StartSockets(); |
0a753a76 | 277 | |
68dc0745 | 278 | FD_ZERO(&nrd); |
279 | FD_ZERO(&nwr); | |
280 | FD_ZERO(&nex); | |
281 | for (i = 0; i < nfds; i++) { | |
282 | fd = TO_SOCKET(i); | |
8dc00b2f | 283 | if (rd && PERL_FD_ISSET(i,rd)) |
47660177 | 284 | FD_SET((unsigned)fd, &nrd); |
8dc00b2f | 285 | if (wr && PERL_FD_ISSET(i,wr)) |
47660177 | 286 | FD_SET((unsigned)fd, &nwr); |
8dc00b2f | 287 | if (ex && PERL_FD_ISSET(i,ex)) |
47660177 | 288 | FD_SET((unsigned)fd, &nex); |
68dc0745 | 289 | } |
290 | ||
1c878075 | 291 | errno = save_errno; |
8dc00b2f | 292 | SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, timeout)); |
1c878075 | 293 | save_errno = errno; |
0a753a76 | 294 | |
68dc0745 | 295 | for (i = 0; i < nfds; i++) { |
296 | fd = TO_SOCKET(i); | |
8dc00b2f | 297 | if (rd && PERL_FD_ISSET(i,rd) && !FD_ISSET(fd, &nrd)) |
55d25626 | 298 | PERL_FD_CLR(i,rd); |
8dc00b2f | 299 | if (wr && PERL_FD_ISSET(i,wr) && !FD_ISSET(fd, &nwr)) |
55d25626 | 300 | PERL_FD_CLR(i,wr); |
8dc00b2f | 301 | if (ex && PERL_FD_ISSET(i,ex) && !FD_ISSET(fd, &nex)) |
55d25626 | 302 | PERL_FD_CLR(i,ex); |
68dc0745 | 303 | } |
1c878075 | 304 | errno = save_errno; |
55d25626 GS |
305 | #else |
306 | SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout)); | |
307 | #endif | |
68dc0745 | 308 | return r; |
0a753a76 | 309 | } |
310 | ||
68dc0745 | 311 | int |
312 | win32_send(SOCKET s, const char *buf, int len, int flags) | |
0a753a76 | 313 | { |
68dc0745 | 314 | int r; |
0a753a76 | 315 | |
326b05e3 | 316 | SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags)); |
68dc0745 | 317 | return r; |
0a753a76 | 318 | } |
319 | ||
68dc0745 | 320 | int |
321 | win32_sendto(SOCKET s, const char *buf, int len, int flags, | |
322 | const struct sockaddr *to, int tolen) | |
0a753a76 | 323 | { |
68dc0745 | 324 | int r; |
0a753a76 | 325 | |
326b05e3 | 326 | SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen)); |
68dc0745 | 327 | return r; |
0a753a76 | 328 | } |
329 | ||
68dc0745 | 330 | int |
331 | win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen) | |
0a753a76 | 332 | { |
68dc0745 | 333 | int r; |
0a753a76 | 334 | |
326b05e3 | 335 | SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen)); |
68dc0745 | 336 | return r; |
0a753a76 | 337 | } |
338 | ||
68dc0745 | 339 | int |
340 | win32_shutdown(SOCKET s, int how) | |
0a753a76 | 341 | { |
68dc0745 | 342 | int r; |
0a753a76 | 343 | |
326b05e3 | 344 | SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how)); |
68dc0745 | 345 | return r; |
0a753a76 | 346 | } |
347 | ||
3a25acb4 GS |
348 | int |
349 | win32_closesocket(SOCKET s) | |
350 | { | |
351 | int r; | |
352 | ||
353 | SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s))); | |
354 | return r; | |
355 | } | |
356 | ||
036c1c1e SH |
357 | #ifdef USE_SOCKETS_AS_HANDLES |
358 | #define WIN32_OPEN_SOCKET(af, type, protocol) open_ifs_socket(af, type, protocol) | |
359 | ||
360 | void | |
361 | convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out) | |
362 | { | |
363 | Copy(in, out, 1, WSAPROTOCOL_INFOA); | |
364 | wcstombs(out->szProtocol, in->szProtocol, sizeof(out->szProtocol)); | |
365 | } | |
366 | ||
367 | SOCKET | |
368 | open_ifs_socket(int af, int type, int protocol) | |
369 | { | |
1c972609 SH |
370 | dTHX; |
371 | char *s; | |
036c1c1e SH |
372 | unsigned long proto_buffers_len = 0; |
373 | int error_code; | |
374 | SOCKET out = INVALID_SOCKET; | |
375 | ||
1c972609 SH |
376 | if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s)) |
377 | return WSASocket(af, type, protocol, NULL, 0, 0); | |
378 | ||
036c1c1e SH |
379 | if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR |
380 | && error_code == WSAENOBUFS) | |
381 | { | |
382 | WSAPROTOCOL_INFOW *proto_buffers; | |
383 | int protocols_available = 0; | |
384 | ||
a02a5408 | 385 | Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW), |
036c1c1e SH |
386 | WSAPROTOCOL_INFOW); |
387 | ||
388 | if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers, | |
389 | &proto_buffers_len, &error_code)) != SOCKET_ERROR) | |
390 | { | |
391 | int i; | |
392 | for (i = 0; i < protocols_available; i++) | |
393 | { | |
394 | WSAPROTOCOL_INFOA proto_info; | |
395 | ||
396 | if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily) | |
397 | || (type != proto_buffers[i].iSocketType) | |
f9169742 SH |
398 | || (protocol != 0 && proto_buffers[i].iProtocol != 0 && |
399 | protocol != proto_buffers[i].iProtocol)) | |
036c1c1e SH |
400 | continue; |
401 | ||
402 | if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0) | |
403 | continue; | |
404 | ||
405 | convert_proto_info_w2a(&(proto_buffers[i]), &proto_info); | |
406 | ||
407 | out = WSASocket(af, type, protocol, &proto_info, 0, 0); | |
408 | break; | |
409 | } | |
410 | } | |
411 | ||
412 | Safefree(proto_buffers); | |
413 | } | |
414 | ||
415 | return out; | |
416 | } | |
417 | ||
418 | #else | |
419 | #define WIN32_OPEN_SOCKET(af, type, protocol) socket(af, type, protocol) | |
420 | #endif | |
421 | ||
68dc0745 | 422 | SOCKET |
423 | win32_socket(int af, int type, int protocol) | |
0a753a76 | 424 | { |
68dc0745 | 425 | SOCKET s; |
0a753a76 | 426 | |
427 | #ifndef USE_SOCKETS_AS_HANDLES | |
326b05e3 | 428 | SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET); |
0a753a76 | 429 | #else |
3a25acb4 | 430 | StartSockets(); |
036c1c1e SH |
431 | |
432 | if((s = WIN32_OPEN_SOCKET(af, type, protocol)) == INVALID_SOCKET) | |
326b05e3 | 433 | errno = WSAGetLastError(); |
68dc0745 | 434 | else |
435 | s = OPEN_SOCKET(s); | |
436 | #endif /* USE_SOCKETS_AS_HANDLES */ | |
0a753a76 | 437 | |
68dc0745 | 438 | return s; |
0a753a76 | 439 | } |
440 | ||
00b02797 JH |
441 | /* |
442 | * close RTL fd while respecting sockets | |
443 | * added as temporary measure until PerlIO has real | |
444 | * Win32 native layer | |
445 | * -- BKS, 11-11-2000 | |
446 | */ | |
447 | ||
448 | int my_close(int fd) | |
449 | { | |
450 | int osf; | |
451 | if (!wsock_started) /* No WinSock? */ | |
452 | return(close(fd)); /* Then not a socket. */ | |
453 | osf = TO_SOCKET(fd);/* Get it now before it's gone! */ | |
454 | if (osf != -1) { | |
455 | int err; | |
456 | err = closesocket(osf); | |
457 | if (err == 0) { | |
458 | #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX) | |
459 | _set_osfhnd(fd, INVALID_HANDLE_VALUE); | |
460 | #endif | |
461 | (void)close(fd); /* handle already closed, ignore error */ | |
462 | return 0; | |
463 | } | |
464 | else if (err == SOCKET_ERROR) { | |
465 | err = WSAGetLastError(); | |
466 | if (err != WSAENOTSOCK) { | |
467 | (void)close(fd); | |
468 | errno = err; | |
469 | return EOF; | |
470 | } | |
471 | } | |
472 | } | |
473 | return close(fd); | |
474 | } | |
475 | ||
fb73857a | 476 | #undef fclose |
477 | int | |
478 | my_fclose (FILE *pf) | |
479 | { | |
4e945249 | 480 | int osf; |
326b05e3 GS |
481 | if (!wsock_started) /* No WinSock? */ |
482 | return(fclose(pf)); /* Then not a socket. */ | |
00b02797 | 483 | osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */ |
d3d49e29 GS |
484 | if (osf != -1) { |
485 | int err; | |
486 | win32_fflush(pf); | |
487 | err = closesocket(osf); | |
488 | if (err == 0) { | |
9114b071 | 489 | #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX) |
00b02797 | 490 | _set_osfhnd(win32_fileno(pf), INVALID_HANDLE_VALUE); |
9114b071 | 491 | #endif |
d3d49e29 GS |
492 | (void)fclose(pf); /* handle already closed, ignore error */ |
493 | return 0; | |
494 | } | |
495 | else if (err == SOCKET_ERROR) { | |
496 | err = WSAGetLastError(); | |
497 | if (err != WSAENOTSOCK) { | |
498 | (void)fclose(pf); | |
499 | errno = err; | |
500 | return EOF; | |
501 | } | |
502 | } | |
326b05e3 | 503 | } |
d3d49e29 | 504 | return fclose(pf); |
fb73857a | 505 | } |
506 | ||
ed59ec62 GS |
507 | #undef fstat |
508 | int | |
c623ac67 | 509 | my_fstat(int fd, Stat_t *sbufptr) |
ed59ec62 GS |
510 | { |
511 | /* This fixes a bug in fstat() on Windows 9x. fstat() uses the | |
512 | * GetFileType() win32 syscall, which will fail on Windows 9x. | |
513 | * So if we recognize a socket on Windows 9x, we return the | |
514 | * same results as on Windows NT/2000. | |
515 | * XXX this should be extended further to set S_IFSOCK on | |
516 | * sbufptr->st_mode. | |
517 | */ | |
518 | int osf; | |
c623ac67 GS |
519 | if (!wsock_started || IsWinNT()) { |
520 | #if defined(WIN64) || defined(USE_LARGE_FILES) | |
a810272a NS |
521 | #if defined(__BORLANDC__) /* buk */ |
522 | return win32_fstat(fd, sbufptr ); | |
523 | #else | |
c623ac67 | 524 | return _fstati64(fd, sbufptr); |
a810272a | 525 | #endif |
c623ac67 | 526 | #else |
ed59ec62 | 527 | return fstat(fd, sbufptr); |
c623ac67 GS |
528 | #endif |
529 | } | |
ed59ec62 GS |
530 | |
531 | osf = TO_SOCKET(fd); | |
532 | if (osf != -1) { | |
533 | char sockbuf[256]; | |
534 | int optlen = sizeof(sockbuf); | |
535 | int retval; | |
536 | ||
537 | retval = getsockopt((SOCKET)osf, SOL_SOCKET, SO_TYPE, sockbuf, &optlen); | |
538 | if (retval != SOCKET_ERROR || WSAGetLastError() != WSAENOTSOCK) { | |
24c02676 VK |
539 | #if defined(__BORLANDC__)&&(__BORLANDC__<=0x520) |
540 | sbufptr->st_mode = S_IFIFO; | |
541 | #else | |
ed59ec62 | 542 | sbufptr->st_mode = _S_IFIFO; |
24c02676 | 543 | #endif |
ed59ec62 GS |
544 | sbufptr->st_rdev = sbufptr->st_dev = (dev_t)fd; |
545 | sbufptr->st_nlink = 1; | |
546 | sbufptr->st_uid = sbufptr->st_gid = sbufptr->st_ino = 0; | |
547 | sbufptr->st_atime = sbufptr->st_mtime = sbufptr->st_ctime = 0; | |
c623ac67 | 548 | sbufptr->st_size = (Off_t)0; |
ed59ec62 GS |
549 | return 0; |
550 | } | |
551 | } | |
c623ac67 | 552 | #if defined(WIN64) || defined(USE_LARGE_FILES) |
a810272a NS |
553 | #if defined(__BORLANDC__) /* buk */ |
554 | return win32_fstat(fd, sbufptr ); | |
555 | #else | |
c623ac67 | 556 | return _fstati64(fd, sbufptr); |
a810272a | 557 | #endif |
c623ac67 | 558 | #else |
ed59ec62 | 559 | return fstat(fd, sbufptr); |
c623ac67 | 560 | #endif |
ed59ec62 GS |
561 | } |
562 | ||
68dc0745 | 563 | struct hostent * |
564 | win32_gethostbyaddr(const char *addr, int len, int type) | |
0a753a76 | 565 | { |
68dc0745 | 566 | struct hostent *r; |
0a753a76 | 567 | |
326b05e3 | 568 | SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL); |
68dc0745 | 569 | return r; |
0a753a76 | 570 | } |
571 | ||
68dc0745 | 572 | struct hostent * |
573 | win32_gethostbyname(const char *name) | |
0a753a76 | 574 | { |
68dc0745 | 575 | struct hostent *r; |
0a753a76 | 576 | |
326b05e3 | 577 | SOCKET_TEST(r = gethostbyname(name), NULL); |
68dc0745 | 578 | return r; |
0a753a76 | 579 | } |
580 | ||
68dc0745 | 581 | int |
582 | win32_gethostname(char *name, int len) | |
0a753a76 | 583 | { |
68dc0745 | 584 | int r; |
0a753a76 | 585 | |
326b05e3 | 586 | SOCKET_TEST_ERROR(r = gethostname(name, len)); |
68dc0745 | 587 | return r; |
0a753a76 | 588 | } |
589 | ||
68dc0745 | 590 | struct protoent * |
591 | win32_getprotobyname(const char *name) | |
0a753a76 | 592 | { |
68dc0745 | 593 | struct protoent *r; |
0a753a76 | 594 | |
326b05e3 | 595 | SOCKET_TEST(r = getprotobyname(name), NULL); |
68dc0745 | 596 | return r; |
0a753a76 | 597 | } |
598 | ||
68dc0745 | 599 | struct protoent * |
600 | win32_getprotobynumber(int num) | |
0a753a76 | 601 | { |
68dc0745 | 602 | struct protoent *r; |
0a753a76 | 603 | |
326b05e3 | 604 | SOCKET_TEST(r = getprotobynumber(num), NULL); |
68dc0745 | 605 | return r; |
0a753a76 | 606 | } |
607 | ||
68dc0745 | 608 | struct servent * |
609 | win32_getservbyname(const char *name, const char *proto) | |
0a753a76 | 610 | { |
acfe0abc | 611 | dTHX; |
68dc0745 | 612 | struct servent *r; |
c53bd28a | 613 | |
326b05e3 | 614 | SOCKET_TEST(r = getservbyname(name, proto), NULL); |
68dc0745 | 615 | if (r) { |
3352bfcb | 616 | r = win32_savecopyservent(&w32_servent, r, proto); |
68dc0745 | 617 | } |
618 | return r; | |
0a753a76 | 619 | } |
620 | ||
68dc0745 | 621 | struct servent * |
622 | win32_getservbyport(int port, const char *proto) | |
0a753a76 | 623 | { |
acfe0abc | 624 | dTHX; |
68dc0745 | 625 | struct servent *r; |
0a753a76 | 626 | |
326b05e3 | 627 | SOCKET_TEST(r = getservbyport(port, proto), NULL); |
68dc0745 | 628 | if (r) { |
3352bfcb | 629 | r = win32_savecopyservent(&w32_servent, r, proto); |
68dc0745 | 630 | } |
631 | return r; | |
0a753a76 | 632 | } |
633 | ||
f998180f GS |
634 | int |
635 | win32_ioctl(int i, unsigned int u, char *data) | |
636 | { | |
acfe0abc | 637 | dTHX; |
69a143a6 | 638 | u_long u_long_arg; |
f998180f | 639 | int retval; |
69a143a6 | 640 | |
f998180f | 641 | if (!wsock_started) { |
4f63d024 | 642 | Perl_croak_nocontext("ioctl implemented only on sockets"); |
f998180f GS |
643 | /* NOTREACHED */ |
644 | } | |
645 | ||
69a143a6 YO |
646 | /* mauke says using memcpy avoids alignment issues */ |
647 | memcpy(&u_long_arg, data, sizeof u_long_arg); | |
648 | retval = ioctlsocket(TO_SOCKET(i), (long)u, &u_long_arg); | |
649 | memcpy(data, &u_long_arg, sizeof u_long_arg); | |
650 | ||
f998180f GS |
651 | if (retval == SOCKET_ERROR) { |
652 | if (WSAGetLastError() == WSAENOTSOCK) { | |
4f63d024 | 653 | Perl_croak_nocontext("ioctl implemented only on sockets"); |
f998180f GS |
654 | /* NOTREACHED */ |
655 | } | |
656 | errno = WSAGetLastError(); | |
657 | } | |
658 | return retval; | |
659 | } | |
660 | ||
68dc0745 | 661 | char FAR * |
662 | win32_inet_ntoa(struct in_addr in) | |
0a753a76 | 663 | { |
326b05e3 GS |
664 | StartSockets(); |
665 | return inet_ntoa(in); | |
0a753a76 | 666 | } |
667 | ||
68dc0745 | 668 | unsigned long |
669 | win32_inet_addr(const char FAR *cp) | |
0a753a76 | 670 | { |
326b05e3 GS |
671 | StartSockets(); |
672 | return inet_addr(cp); | |
0a753a76 | 673 | } |
68dc0745 | 674 | |
675 | /* | |
676 | * Networking stubs | |
677 | */ | |
0a753a76 | 678 | |
68dc0745 | 679 | void |
680 | win32_endhostent() | |
0a753a76 | 681 | { |
acfe0abc | 682 | dTHX; |
4f63d024 | 683 | Perl_croak_nocontext("endhostent not implemented!\n"); |
0a753a76 | 684 | } |
685 | ||
68dc0745 | 686 | void |
687 | win32_endnetent() | |
0a753a76 | 688 | { |
acfe0abc | 689 | dTHX; |
4f63d024 | 690 | Perl_croak_nocontext("endnetent not implemented!\n"); |
0a753a76 | 691 | } |
692 | ||
68dc0745 | 693 | void |
694 | win32_endprotoent() | |
0a753a76 | 695 | { |
acfe0abc | 696 | dTHX; |
4f63d024 | 697 | Perl_croak_nocontext("endprotoent not implemented!\n"); |
0a753a76 | 698 | } |
699 | ||
68dc0745 | 700 | void |
701 | win32_endservent() | |
0a753a76 | 702 | { |
acfe0abc | 703 | dTHX; |
4f63d024 | 704 | Perl_croak_nocontext("endservent not implemented!\n"); |
0a753a76 | 705 | } |
706 | ||
707 | ||
68dc0745 | 708 | struct netent * |
709 | win32_getnetent(void) | |
0a753a76 | 710 | { |
acfe0abc | 711 | dTHX; |
4f63d024 | 712 | Perl_croak_nocontext("getnetent not implemented!\n"); |
68dc0745 | 713 | return (struct netent *) NULL; |
0a753a76 | 714 | } |
715 | ||
68dc0745 | 716 | struct netent * |
717 | win32_getnetbyname(char *name) | |
0a753a76 | 718 | { |
acfe0abc | 719 | dTHX; |
4f63d024 | 720 | Perl_croak_nocontext("getnetbyname not implemented!\n"); |
68dc0745 | 721 | return (struct netent *)NULL; |
0a753a76 | 722 | } |
723 | ||
68dc0745 | 724 | struct netent * |
725 | win32_getnetbyaddr(long net, int type) | |
0a753a76 | 726 | { |
acfe0abc | 727 | dTHX; |
4f63d024 | 728 | Perl_croak_nocontext("getnetbyaddr not implemented!\n"); |
68dc0745 | 729 | return (struct netent *)NULL; |
0a753a76 | 730 | } |
731 | ||
68dc0745 | 732 | struct protoent * |
733 | win32_getprotoent(void) | |
0a753a76 | 734 | { |
acfe0abc | 735 | dTHX; |
4f63d024 | 736 | Perl_croak_nocontext("getprotoent not implemented!\n"); |
68dc0745 | 737 | return (struct protoent *) NULL; |
0a753a76 | 738 | } |
739 | ||
68dc0745 | 740 | struct servent * |
741 | win32_getservent(void) | |
0a753a76 | 742 | { |
acfe0abc | 743 | dTHX; |
4f63d024 | 744 | Perl_croak_nocontext("getservent not implemented!\n"); |
68dc0745 | 745 | return (struct servent *) NULL; |
0a753a76 | 746 | } |
747 | ||
68dc0745 | 748 | void |
749 | win32_sethostent(int stayopen) | |
0a753a76 | 750 | { |
acfe0abc | 751 | dTHX; |
4f63d024 | 752 | Perl_croak_nocontext("sethostent not implemented!\n"); |
0a753a76 | 753 | } |
754 | ||
755 | ||
68dc0745 | 756 | void |
757 | win32_setnetent(int stayopen) | |
0a753a76 | 758 | { |
acfe0abc | 759 | dTHX; |
4f63d024 | 760 | Perl_croak_nocontext("setnetent not implemented!\n"); |
0a753a76 | 761 | } |
762 | ||
763 | ||
68dc0745 | 764 | void |
765 | win32_setprotoent(int stayopen) | |
0a753a76 | 766 | { |
acfe0abc | 767 | dTHX; |
4f63d024 | 768 | Perl_croak_nocontext("setprotoent not implemented!\n"); |
0a753a76 | 769 | } |
770 | ||
771 | ||
68dc0745 | 772 | void |
773 | win32_setservent(int stayopen) | |
0a753a76 | 774 | { |
acfe0abc | 775 | dTHX; |
4f63d024 | 776 | Perl_croak_nocontext("setservent not implemented!\n"); |
0a753a76 | 777 | } |
778 | ||
137443ea | 779 | static struct servent* |
780 | win32_savecopyservent(struct servent*d, struct servent*s, const char *proto) | |
781 | { | |
782 | d->s_name = s->s_name; | |
783 | d->s_aliases = s->s_aliases; | |
784 | d->s_port = s->s_port; | |
0af56dfe | 785 | #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */ |
137443ea | 786 | if (!IsWin95() && s->s_proto && strlen(s->s_proto)) |
787 | d->s_proto = s->s_proto; | |
0af56dfe GS |
788 | else |
789 | #endif | |
c69f6586 | 790 | if (proto && strlen(proto)) |
137443ea | 791 | d->s_proto = (char *)proto; |
792 | else | |
793 | d->s_proto = "tcp"; | |
794 | ||
795 | return d; | |
796 | } | |
797 | ||
0a753a76 | 798 |