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