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