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