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