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 |
15 | #ifdef __GNUC__ |
16 | #define Win32_Winsock |
17 | #endif |
390b85e7 |
18 | #include <windows.h> |
036c1c1e |
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 |
40 | #define StartSockets() \ |
41 | STMT_START { \ |
42 | if (!wsock_started) \ |
43 | start_sockets(); \ |
44 | } STMT_END |
45 | |
326b05e3 |
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 |
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 |
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 |
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 |
131 | StartSockets(); |
132 | return htonl(hostlong); |
0a753a76 |
133 | } |
134 | |
68dc0745 |
135 | u_short |
136 | win32_htons(u_short hostshort) |
0a753a76 |
137 | { |
326b05e3 |
138 | StartSockets(); |
139 | return htons(hostshort); |
0a753a76 |
140 | } |
141 | |
68dc0745 |
142 | u_long |
143 | win32_ntohl(u_long netlong) |
0a753a76 |
144 | { |
326b05e3 |
145 | StartSockets(); |
146 | return ntohl(netlong); |
0a753a76 |
147 | } |
148 | |
68dc0745 |
149 | u_short |
150 | win32_ntohs(u_short netshort) |
0a753a76 |
151 | { |
326b05e3 |
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 |
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 |
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 |
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 |
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 |
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++) { |
f7bbabd3 |
282 | if (rd && PERL_FD_ISSET(i,rd)) { |
283 | fd = TO_SOCKET(i); |
47660177 |
284 | FD_SET((unsigned)fd, &nrd); |
f7bbabd3 |
285 | } |
286 | if (wr && PERL_FD_ISSET(i,wr)) { |
287 | fd = TO_SOCKET(i); |
47660177 |
288 | FD_SET((unsigned)fd, &nwr); |
f7bbabd3 |
289 | } |
290 | if (ex && PERL_FD_ISSET(i,ex)) { |
291 | fd = TO_SOCKET(i); |
47660177 |
292 | FD_SET((unsigned)fd, &nex); |
f7bbabd3 |
293 | } |
68dc0745 |
294 | } |
295 | |
1c878075 |
296 | errno = save_errno; |
8dc00b2f |
297 | SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, timeout)); |
1c878075 |
298 | save_errno = errno; |
0a753a76 |
299 | |
68dc0745 |
300 | for (i = 0; i < nfds; i++) { |
f7bbabd3 |
301 | if (rd && PERL_FD_ISSET(i,rd)) { |
302 | fd = TO_SOCKET(i); |
303 | if (!FD_ISSET(fd, &nrd)) |
304 | PERL_FD_CLR(i,rd); |
305 | } |
306 | if (wr && PERL_FD_ISSET(i,wr)) { |
307 | fd = TO_SOCKET(i); |
308 | if (!FD_ISSET(fd, &nwr)) |
309 | PERL_FD_CLR(i,wr); |
310 | } |
311 | if (ex && PERL_FD_ISSET(i,ex)) { |
312 | fd = TO_SOCKET(i); |
313 | if (!FD_ISSET(fd, &nex)) |
314 | PERL_FD_CLR(i,ex); |
315 | } |
68dc0745 |
316 | } |
1c878075 |
317 | errno = save_errno; |
55d25626 |
318 | #else |
319 | SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout)); |
320 | #endif |
68dc0745 |
321 | return r; |
0a753a76 |
322 | } |
323 | |
68dc0745 |
324 | int |
325 | win32_send(SOCKET s, const char *buf, int len, int flags) |
0a753a76 |
326 | { |
68dc0745 |
327 | int r; |
0a753a76 |
328 | |
326b05e3 |
329 | SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags)); |
68dc0745 |
330 | return r; |
0a753a76 |
331 | } |
332 | |
68dc0745 |
333 | int |
334 | win32_sendto(SOCKET s, const char *buf, int len, int flags, |
335 | const struct sockaddr *to, int tolen) |
0a753a76 |
336 | { |
68dc0745 |
337 | int r; |
0a753a76 |
338 | |
326b05e3 |
339 | SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen)); |
68dc0745 |
340 | return r; |
0a753a76 |
341 | } |
342 | |
68dc0745 |
343 | int |
344 | win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen) |
0a753a76 |
345 | { |
68dc0745 |
346 | int r; |
0a753a76 |
347 | |
326b05e3 |
348 | SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen)); |
68dc0745 |
349 | return r; |
0a753a76 |
350 | } |
351 | |
68dc0745 |
352 | int |
353 | win32_shutdown(SOCKET s, int how) |
0a753a76 |
354 | { |
68dc0745 |
355 | int r; |
0a753a76 |
356 | |
326b05e3 |
357 | SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how)); |
68dc0745 |
358 | return r; |
0a753a76 |
359 | } |
360 | |
3a25acb4 |
361 | int |
362 | win32_closesocket(SOCKET s) |
363 | { |
364 | int r; |
365 | |
366 | SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s))); |
367 | return r; |
368 | } |
369 | |
036c1c1e |
370 | #ifdef USE_SOCKETS_AS_HANDLES |
371 | #define WIN32_OPEN_SOCKET(af, type, protocol) open_ifs_socket(af, type, protocol) |
372 | |
373 | void |
374 | convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out) |
375 | { |
376 | Copy(in, out, 1, WSAPROTOCOL_INFOA); |
377 | wcstombs(out->szProtocol, in->szProtocol, sizeof(out->szProtocol)); |
378 | } |
379 | |
380 | SOCKET |
381 | open_ifs_socket(int af, int type, int protocol) |
382 | { |
1c972609 |
383 | dTHX; |
384 | char *s; |
036c1c1e |
385 | unsigned long proto_buffers_len = 0; |
386 | int error_code; |
387 | SOCKET out = INVALID_SOCKET; |
388 | |
1c972609 |
389 | if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s)) |
390 | return WSASocket(af, type, protocol, NULL, 0, 0); |
391 | |
036c1c1e |
392 | if (WSCEnumProtocols(NULL, NULL, &proto_buffers_len, &error_code) == SOCKET_ERROR |
393 | && error_code == WSAENOBUFS) |
394 | { |
395 | WSAPROTOCOL_INFOW *proto_buffers; |
396 | int protocols_available = 0; |
397 | |
a02a5408 |
398 | Newx(proto_buffers, proto_buffers_len / sizeof(WSAPROTOCOL_INFOW), |
036c1c1e |
399 | WSAPROTOCOL_INFOW); |
400 | |
401 | if ((protocols_available = WSCEnumProtocols(NULL, proto_buffers, |
402 | &proto_buffers_len, &error_code)) != SOCKET_ERROR) |
403 | { |
404 | int i; |
405 | for (i = 0; i < protocols_available; i++) |
406 | { |
407 | WSAPROTOCOL_INFOA proto_info; |
408 | |
409 | if ((af != AF_UNSPEC && af != proto_buffers[i].iAddressFamily) |
410 | || (type != proto_buffers[i].iSocketType) |
f9169742 |
411 | || (protocol != 0 && proto_buffers[i].iProtocol != 0 && |
412 | protocol != proto_buffers[i].iProtocol)) |
036c1c1e |
413 | continue; |
414 | |
415 | if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0) |
416 | continue; |
417 | |
418 | convert_proto_info_w2a(&(proto_buffers[i]), &proto_info); |
419 | |
420 | out = WSASocket(af, type, protocol, &proto_info, 0, 0); |
421 | break; |
422 | } |
423 | } |
424 | |
425 | Safefree(proto_buffers); |
426 | } |
427 | |
428 | return out; |
429 | } |
430 | |
431 | #else |
432 | #define WIN32_OPEN_SOCKET(af, type, protocol) socket(af, type, protocol) |
433 | #endif |
434 | |
68dc0745 |
435 | SOCKET |
436 | win32_socket(int af, int type, int protocol) |
0a753a76 |
437 | { |
68dc0745 |
438 | SOCKET s; |
0a753a76 |
439 | |
440 | #ifndef USE_SOCKETS_AS_HANDLES |
326b05e3 |
441 | SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET); |
0a753a76 |
442 | #else |
3a25acb4 |
443 | StartSockets(); |
036c1c1e |
444 | |
445 | if((s = WIN32_OPEN_SOCKET(af, type, protocol)) == INVALID_SOCKET) |
326b05e3 |
446 | errno = WSAGetLastError(); |
68dc0745 |
447 | else |
448 | s = OPEN_SOCKET(s); |
449 | #endif /* USE_SOCKETS_AS_HANDLES */ |
0a753a76 |
450 | |
68dc0745 |
451 | return s; |
0a753a76 |
452 | } |
453 | |
00b02797 |
454 | /* |
455 | * close RTL fd while respecting sockets |
456 | * added as temporary measure until PerlIO has real |
457 | * Win32 native layer |
458 | * -- BKS, 11-11-2000 |
459 | */ |
460 | |
461 | int my_close(int fd) |
462 | { |
463 | int osf; |
464 | if (!wsock_started) /* No WinSock? */ |
465 | return(close(fd)); /* Then not a socket. */ |
466 | osf = TO_SOCKET(fd);/* Get it now before it's gone! */ |
467 | if (osf != -1) { |
468 | int err; |
469 | err = closesocket(osf); |
470 | if (err == 0) { |
471 | #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX) |
472 | _set_osfhnd(fd, INVALID_HANDLE_VALUE); |
473 | #endif |
474 | (void)close(fd); /* handle already closed, ignore error */ |
475 | return 0; |
476 | } |
477 | else if (err == SOCKET_ERROR) { |
478 | err = WSAGetLastError(); |
479 | if (err != WSAENOTSOCK) { |
480 | (void)close(fd); |
481 | errno = err; |
482 | return EOF; |
483 | } |
484 | } |
485 | } |
486 | return close(fd); |
487 | } |
488 | |
fb73857a |
489 | #undef fclose |
490 | int |
491 | my_fclose (FILE *pf) |
492 | { |
4e945249 |
493 | int osf; |
326b05e3 |
494 | if (!wsock_started) /* No WinSock? */ |
495 | return(fclose(pf)); /* Then not a socket. */ |
00b02797 |
496 | osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */ |
d3d49e29 |
497 | if (osf != -1) { |
498 | int err; |
499 | win32_fflush(pf); |
500 | err = closesocket(osf); |
501 | if (err == 0) { |
9114b071 |
502 | #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX) |
00b02797 |
503 | _set_osfhnd(win32_fileno(pf), INVALID_HANDLE_VALUE); |
9114b071 |
504 | #endif |
d3d49e29 |
505 | (void)fclose(pf); /* handle already closed, ignore error */ |
506 | return 0; |
507 | } |
508 | else if (err == SOCKET_ERROR) { |
509 | err = WSAGetLastError(); |
510 | if (err != WSAENOTSOCK) { |
511 | (void)fclose(pf); |
512 | errno = err; |
513 | return EOF; |
514 | } |
515 | } |
326b05e3 |
516 | } |
d3d49e29 |
517 | return fclose(pf); |
fb73857a |
518 | } |
519 | |
ed59ec62 |
520 | #undef fstat |
521 | int |
c623ac67 |
522 | my_fstat(int fd, Stat_t *sbufptr) |
ed59ec62 |
523 | { |
524 | /* This fixes a bug in fstat() on Windows 9x. fstat() uses the |
525 | * GetFileType() win32 syscall, which will fail on Windows 9x. |
526 | * So if we recognize a socket on Windows 9x, we return the |
527 | * same results as on Windows NT/2000. |
528 | * XXX this should be extended further to set S_IFSOCK on |
529 | * sbufptr->st_mode. |
530 | */ |
531 | int osf; |
c623ac67 |
532 | if (!wsock_started || IsWinNT()) { |
533 | #if defined(WIN64) || defined(USE_LARGE_FILES) |
a810272a |
534 | #if defined(__BORLANDC__) /* buk */ |
535 | return win32_fstat(fd, sbufptr ); |
536 | #else |
c623ac67 |
537 | return _fstati64(fd, sbufptr); |
a810272a |
538 | #endif |
c623ac67 |
539 | #else |
ed59ec62 |
540 | return fstat(fd, sbufptr); |
c623ac67 |
541 | #endif |
542 | } |
ed59ec62 |
543 | |
544 | osf = TO_SOCKET(fd); |
545 | if (osf != -1) { |
546 | char sockbuf[256]; |
547 | int optlen = sizeof(sockbuf); |
548 | int retval; |
549 | |
550 | retval = getsockopt((SOCKET)osf, SOL_SOCKET, SO_TYPE, sockbuf, &optlen); |
551 | if (retval != SOCKET_ERROR || WSAGetLastError() != WSAENOTSOCK) { |
24c02676 |
552 | #if defined(__BORLANDC__)&&(__BORLANDC__<=0x520) |
553 | sbufptr->st_mode = S_IFIFO; |
554 | #else |
ed59ec62 |
555 | sbufptr->st_mode = _S_IFIFO; |
24c02676 |
556 | #endif |
ed59ec62 |
557 | sbufptr->st_rdev = sbufptr->st_dev = (dev_t)fd; |
558 | sbufptr->st_nlink = 1; |
559 | sbufptr->st_uid = sbufptr->st_gid = sbufptr->st_ino = 0; |
560 | sbufptr->st_atime = sbufptr->st_mtime = sbufptr->st_ctime = 0; |
c623ac67 |
561 | sbufptr->st_size = (Off_t)0; |
ed59ec62 |
562 | return 0; |
563 | } |
564 | } |
c623ac67 |
565 | #if defined(WIN64) || defined(USE_LARGE_FILES) |
a810272a |
566 | #if defined(__BORLANDC__) /* buk */ |
567 | return win32_fstat(fd, sbufptr ); |
568 | #else |
c623ac67 |
569 | return _fstati64(fd, sbufptr); |
a810272a |
570 | #endif |
c623ac67 |
571 | #else |
ed59ec62 |
572 | return fstat(fd, sbufptr); |
c623ac67 |
573 | #endif |
ed59ec62 |
574 | } |
575 | |
68dc0745 |
576 | struct hostent * |
577 | win32_gethostbyaddr(const char *addr, int len, int type) |
0a753a76 |
578 | { |
68dc0745 |
579 | struct hostent *r; |
0a753a76 |
580 | |
326b05e3 |
581 | SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL); |
68dc0745 |
582 | return r; |
0a753a76 |
583 | } |
584 | |
68dc0745 |
585 | struct hostent * |
586 | win32_gethostbyname(const char *name) |
0a753a76 |
587 | { |
68dc0745 |
588 | struct hostent *r; |
0a753a76 |
589 | |
326b05e3 |
590 | SOCKET_TEST(r = gethostbyname(name), NULL); |
68dc0745 |
591 | return r; |
0a753a76 |
592 | } |
593 | |
68dc0745 |
594 | int |
595 | win32_gethostname(char *name, int len) |
0a753a76 |
596 | { |
68dc0745 |
597 | int r; |
0a753a76 |
598 | |
326b05e3 |
599 | SOCKET_TEST_ERROR(r = gethostname(name, len)); |
68dc0745 |
600 | return r; |
0a753a76 |
601 | } |
602 | |
68dc0745 |
603 | struct protoent * |
604 | win32_getprotobyname(const char *name) |
0a753a76 |
605 | { |
68dc0745 |
606 | struct protoent *r; |
0a753a76 |
607 | |
326b05e3 |
608 | SOCKET_TEST(r = getprotobyname(name), NULL); |
68dc0745 |
609 | return r; |
0a753a76 |
610 | } |
611 | |
68dc0745 |
612 | struct protoent * |
613 | win32_getprotobynumber(int num) |
0a753a76 |
614 | { |
68dc0745 |
615 | struct protoent *r; |
0a753a76 |
616 | |
326b05e3 |
617 | SOCKET_TEST(r = getprotobynumber(num), NULL); |
68dc0745 |
618 | return r; |
0a753a76 |
619 | } |
620 | |
68dc0745 |
621 | struct servent * |
622 | win32_getservbyname(const char *name, const char *proto) |
0a753a76 |
623 | { |
acfe0abc |
624 | dTHX; |
68dc0745 |
625 | struct servent *r; |
c53bd28a |
626 | |
326b05e3 |
627 | SOCKET_TEST(r = getservbyname(name, proto), NULL); |
68dc0745 |
628 | if (r) { |
3352bfcb |
629 | r = win32_savecopyservent(&w32_servent, r, proto); |
68dc0745 |
630 | } |
631 | return r; |
0a753a76 |
632 | } |
633 | |
68dc0745 |
634 | struct servent * |
635 | win32_getservbyport(int port, const char *proto) |
0a753a76 |
636 | { |
acfe0abc |
637 | dTHX; |
68dc0745 |
638 | struct servent *r; |
0a753a76 |
639 | |
326b05e3 |
640 | SOCKET_TEST(r = getservbyport(port, proto), NULL); |
68dc0745 |
641 | if (r) { |
3352bfcb |
642 | r = win32_savecopyservent(&w32_servent, r, proto); |
68dc0745 |
643 | } |
644 | return r; |
0a753a76 |
645 | } |
646 | |
f998180f |
647 | int |
648 | win32_ioctl(int i, unsigned int u, char *data) |
649 | { |
acfe0abc |
650 | dTHX; |
69a143a6 |
651 | u_long u_long_arg; |
f998180f |
652 | int retval; |
69a143a6 |
653 | |
f998180f |
654 | if (!wsock_started) { |
4f63d024 |
655 | Perl_croak_nocontext("ioctl implemented only on sockets"); |
f998180f |
656 | /* NOTREACHED */ |
657 | } |
658 | |
69a143a6 |
659 | /* mauke says using memcpy avoids alignment issues */ |
660 | memcpy(&u_long_arg, data, sizeof u_long_arg); |
661 | retval = ioctlsocket(TO_SOCKET(i), (long)u, &u_long_arg); |
662 | memcpy(data, &u_long_arg, sizeof u_long_arg); |
663 | |
f998180f |
664 | if (retval == SOCKET_ERROR) { |
665 | if (WSAGetLastError() == WSAENOTSOCK) { |
4f63d024 |
666 | Perl_croak_nocontext("ioctl implemented only on sockets"); |
f998180f |
667 | /* NOTREACHED */ |
668 | } |
669 | errno = WSAGetLastError(); |
670 | } |
671 | return retval; |
672 | } |
673 | |
68dc0745 |
674 | char FAR * |
675 | win32_inet_ntoa(struct in_addr in) |
0a753a76 |
676 | { |
326b05e3 |
677 | StartSockets(); |
678 | return inet_ntoa(in); |
0a753a76 |
679 | } |
680 | |
68dc0745 |
681 | unsigned long |
682 | win32_inet_addr(const char FAR *cp) |
0a753a76 |
683 | { |
326b05e3 |
684 | StartSockets(); |
685 | return inet_addr(cp); |
0a753a76 |
686 | } |
68dc0745 |
687 | |
688 | /* |
689 | * Networking stubs |
690 | */ |
0a753a76 |
691 | |
68dc0745 |
692 | void |
693 | win32_endhostent() |
0a753a76 |
694 | { |
acfe0abc |
695 | dTHX; |
4f63d024 |
696 | Perl_croak_nocontext("endhostent not implemented!\n"); |
0a753a76 |
697 | } |
698 | |
68dc0745 |
699 | void |
700 | win32_endnetent() |
0a753a76 |
701 | { |
acfe0abc |
702 | dTHX; |
4f63d024 |
703 | Perl_croak_nocontext("endnetent not implemented!\n"); |
0a753a76 |
704 | } |
705 | |
68dc0745 |
706 | void |
707 | win32_endprotoent() |
0a753a76 |
708 | { |
acfe0abc |
709 | dTHX; |
4f63d024 |
710 | Perl_croak_nocontext("endprotoent not implemented!\n"); |
0a753a76 |
711 | } |
712 | |
68dc0745 |
713 | void |
714 | win32_endservent() |
0a753a76 |
715 | { |
acfe0abc |
716 | dTHX; |
4f63d024 |
717 | Perl_croak_nocontext("endservent not implemented!\n"); |
0a753a76 |
718 | } |
719 | |
720 | |
68dc0745 |
721 | struct netent * |
722 | win32_getnetent(void) |
0a753a76 |
723 | { |
acfe0abc |
724 | dTHX; |
4f63d024 |
725 | Perl_croak_nocontext("getnetent not implemented!\n"); |
68dc0745 |
726 | return (struct netent *) NULL; |
0a753a76 |
727 | } |
728 | |
68dc0745 |
729 | struct netent * |
730 | win32_getnetbyname(char *name) |
0a753a76 |
731 | { |
acfe0abc |
732 | dTHX; |
4f63d024 |
733 | Perl_croak_nocontext("getnetbyname not implemented!\n"); |
68dc0745 |
734 | return (struct netent *)NULL; |
0a753a76 |
735 | } |
736 | |
68dc0745 |
737 | struct netent * |
738 | win32_getnetbyaddr(long net, int type) |
0a753a76 |
739 | { |
acfe0abc |
740 | dTHX; |
4f63d024 |
741 | Perl_croak_nocontext("getnetbyaddr not implemented!\n"); |
68dc0745 |
742 | return (struct netent *)NULL; |
0a753a76 |
743 | } |
744 | |
68dc0745 |
745 | struct protoent * |
746 | win32_getprotoent(void) |
0a753a76 |
747 | { |
acfe0abc |
748 | dTHX; |
4f63d024 |
749 | Perl_croak_nocontext("getprotoent not implemented!\n"); |
68dc0745 |
750 | return (struct protoent *) NULL; |
0a753a76 |
751 | } |
752 | |
68dc0745 |
753 | struct servent * |
754 | win32_getservent(void) |
0a753a76 |
755 | { |
acfe0abc |
756 | dTHX; |
4f63d024 |
757 | Perl_croak_nocontext("getservent not implemented!\n"); |
68dc0745 |
758 | return (struct servent *) NULL; |
0a753a76 |
759 | } |
760 | |
68dc0745 |
761 | void |
762 | win32_sethostent(int stayopen) |
0a753a76 |
763 | { |
acfe0abc |
764 | dTHX; |
4f63d024 |
765 | Perl_croak_nocontext("sethostent not implemented!\n"); |
0a753a76 |
766 | } |
767 | |
768 | |
68dc0745 |
769 | void |
770 | win32_setnetent(int stayopen) |
0a753a76 |
771 | { |
acfe0abc |
772 | dTHX; |
4f63d024 |
773 | Perl_croak_nocontext("setnetent not implemented!\n"); |
0a753a76 |
774 | } |
775 | |
776 | |
68dc0745 |
777 | void |
778 | win32_setprotoent(int stayopen) |
0a753a76 |
779 | { |
acfe0abc |
780 | dTHX; |
4f63d024 |
781 | Perl_croak_nocontext("setprotoent not implemented!\n"); |
0a753a76 |
782 | } |
783 | |
784 | |
68dc0745 |
785 | void |
786 | win32_setservent(int stayopen) |
0a753a76 |
787 | { |
acfe0abc |
788 | dTHX; |
4f63d024 |
789 | Perl_croak_nocontext("setservent not implemented!\n"); |
0a753a76 |
790 | } |
791 | |
137443ea |
792 | static struct servent* |
793 | win32_savecopyservent(struct servent*d, struct servent*s, const char *proto) |
794 | { |
795 | d->s_name = s->s_name; |
796 | d->s_aliases = s->s_aliases; |
797 | d->s_port = s->s_port; |
0af56dfe |
798 | #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */ |
137443ea |
799 | if (!IsWin95() && s->s_proto && strlen(s->s_proto)) |
800 | d->s_proto = s->s_proto; |
0af56dfe |
801 | else |
802 | #endif |
c69f6586 |
803 | if (proto && strlen(proto)) |
137443ea |
804 | d->s_proto = (char *)proto; |
805 | else |
806 | d->s_proto = "tcp"; |
807 | |
808 | return d; |
809 | } |
810 | |
0a753a76 |
811 | |