[inseparable changes from match from perl-5.003_97g to perl-5.003_97h]
[p5sagit/p5-mst-13.2.git] / win32 / win32sck.c
CommitLineData
68dc0745 1/* NTSock.C
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
11#include <windows.h>
12#define WIN32_LEAN_AND_MEAN
13#include "EXTERN.h"
14#include "perl.h"
15#include <sys/socket.h>
16#include <fcntl.h>
17#include <sys/stat.h>
18#include <assert.h>
19
20#define CROAK croak
21
22#ifdef USE_SOCKETS_AS_HANDLES
68dc0745 23/* thanks to Beverly Brown (beverly@datacube.com) */
0a753a76 24
137443ea 25#define OPEN_SOCKET(x) _open_osfhandle(x,_O_RDWR|_O_BINARY)
26#define TO_SOCKET(x) _get_osfhandle(x)
0a753a76 27
28#else
29
30# define OPEN_SOCKET(x) (x)
31# define TO_SOCKET(x) (x)
32
68dc0745 33#endif /* USE_SOCKETS_AS_HANDLES */
0a753a76 34
137443ea 35static struct servent* win32_savecopyservent(struct servent*d,
36 struct servent*s,
37 const char *proto);
0a753a76 38#define SOCKETAPI PASCAL
39
40typedef SOCKET (SOCKETAPI *LPSOCKACCEPT)(SOCKET, struct sockaddr *, int *);
41typedef int (SOCKETAPI *LPSOCKBIND)(SOCKET, const struct sockaddr *, int);
42typedef int (SOCKETAPI *LPSOCKCLOSESOCKET)(SOCKET);
43typedef int (SOCKETAPI *LPSOCKCONNECT)(SOCKET, const struct sockaddr *, int);
44typedef int (SOCKETAPI *LPSOCKIOCTLSOCKET)(SOCKET, long, u_long *);
45typedef int (SOCKETAPI *LPSOCKGETPEERNAME)(SOCKET, struct sockaddr *, int *);
46typedef int (SOCKETAPI *LPSOCKGETSOCKNAME)(SOCKET, struct sockaddr *, int *);
47typedef int (SOCKETAPI *LPSOCKGETSOCKOPT)(SOCKET, int, int, char *, int *);
48typedef u_long (SOCKETAPI *LPSOCKHTONL)(u_long);
49typedef u_short (SOCKETAPI *LPSOCKHTONS)(u_short);
50typedef int (SOCKETAPI *LPSOCKLISTEN)(SOCKET, int);
51typedef u_long (SOCKETAPI *LPSOCKNTOHL)(u_long);
52typedef u_short (SOCKETAPI *LPSOCKNTOHS)(u_short);
53typedef int (SOCKETAPI *LPSOCKRECV)(SOCKET, char *, int, int);
54typedef int (SOCKETAPI *LPSOCKRECVFROM)(SOCKET, char *, int, int, struct sockaddr *, int *);
55typedef int (SOCKETAPI *LPSOCKSELECT)(int, fd_set *, fd_set *, fd_set *, const struct timeval *);
56typedef int (SOCKETAPI *LPSOCKSEND)(SOCKET, const char *, int, int);
57typedef int (SOCKETAPI *LPSOCKSENDTO)(SOCKET, const char *, int, int, const struct sockaddr *, int);
58typedef int (SOCKETAPI *LPSOCKSETSOCKOPT)(SOCKET, int, int, const char *, int);
59typedef int (SOCKETAPI *LPSOCKSHUTDOWN)(SOCKET, int);
60typedef SOCKET (SOCKETAPI *LPSOCKSOCKET)(int, int, int);
61typedef char FAR *(SOCKETAPI *LPSOCKINETNTOA)(struct in_addr in);
62typedef unsigned long (SOCKETAPI *LPSOCKINETADDR)(const char FAR * cp);
63
64
65/* Database function prototypes */
66typedef struct hostent *(SOCKETAPI *LPSOCKGETHOSTBYADDR)(const char *, int, int);
67typedef struct hostent *(SOCKETAPI *LPSOCKGETHOSTBYNAME)(const char *);
68typedef int (SOCKETAPI *LPSOCKGETHOSTNAME)(char *, int);
69typedef struct servent *(SOCKETAPI *LPSOCKGETSERVBYPORT)(int, const char *);
70typedef struct servent *(SOCKETAPI *LPSOCKGETSERVBYNAME)(const char *, const char *);
71typedef struct protoent *(SOCKETAPI *LPSOCKGETPROTOBYNUMBER)(int);
72typedef struct protoent *(SOCKETAPI *LPSOCKGETPROTOBYNAME)(const char *);
73
74/* Microsoft Windows Extension function prototypes */
75typedef int (SOCKETAPI *LPSOCKWSASTARTUP)(unsigned short, LPWSADATA);
76typedef int (SOCKETAPI *LPSOCKWSACLEANUP)(void);
77typedef int (SOCKETAPI *LPSOCKWSAGETLASTERROR)(void);
78typedef int (SOCKETAPI *LPWSAFDIsSet)(SOCKET, fd_set *);
79
80static HINSTANCE hWinSockDll = 0;
68dc0745 81/* extern CRITICAL_SECTION csSock; */
0a753a76 82
83static LPSOCKACCEPT paccept = 0;
84static LPSOCKBIND pbind = 0;
85static LPSOCKCLOSESOCKET pclosesocket = 0;
86static LPSOCKCONNECT pconnect = 0;
87static LPSOCKIOCTLSOCKET pioctlsocket = 0;
88static LPSOCKGETPEERNAME pgetpeername = 0;
89static LPSOCKGETSOCKNAME pgetsockname = 0;
90static LPSOCKGETSOCKOPT pgetsockopt = 0;
91static LPSOCKHTONL phtonl = 0;
92static LPSOCKHTONS phtons = 0;
93static LPSOCKLISTEN plisten = 0;
94static LPSOCKNTOHL pntohl = 0;
95static LPSOCKNTOHS pntohs = 0;
96static LPSOCKRECV precv = 0;
97static LPSOCKRECVFROM precvfrom = 0;
98static LPSOCKSELECT pselect = 0;
99static LPSOCKSEND psend = 0;
100static LPSOCKSENDTO psendto = 0;
101static LPSOCKSETSOCKOPT psetsockopt = 0;
102static LPSOCKSHUTDOWN pshutdown = 0;
103static LPSOCKSOCKET psocket = 0;
104static LPSOCKGETHOSTBYADDR pgethostbyaddr = 0;
105static LPSOCKGETHOSTBYNAME pgethostbyname = 0;
106static LPSOCKGETHOSTNAME pgethostname = 0;
107static LPSOCKGETSERVBYPORT pgetservbyport = 0;
108static LPSOCKGETSERVBYNAME pgetservbyname = 0;
109static LPSOCKGETPROTOBYNUMBER pgetprotobynumber = 0;
110static LPSOCKGETPROTOBYNAME pgetprotobyname = 0;
111static LPSOCKWSASTARTUP pWSAStartup = 0;
112static LPSOCKWSACLEANUP pWSACleanup = 0;
113static LPSOCKWSAGETLASTERROR pWSAGetLastError = 0;
114static LPWSAFDIsSet pWSAFDIsSet = 0;
115static LPSOCKINETNTOA pinet_ntoa = 0;
116static LPSOCKINETADDR pinet_addr = 0;
117
118__declspec(thread) struct servent myservent;
119
120
68dc0745 121void *
122GetAddress(HINSTANCE hInstance, char *lpFunctionName)
123{
124 char buffer[512];
125 FARPROC proc = GetProcAddress(hInstance, lpFunctionName);
126 if(proc == 0) {
127 sprintf(buffer, "Unable to get address of %s in WSock32.dll", lpFunctionName);
128 CROAK(buffer);
129 }
130 return proc;
131}
132
133void
134LoadWinSock(void)
135{
136/* EnterCriticalSection(&csSock); */
137 if(hWinSockDll == NULL) {
138 HINSTANCE hLib = LoadLibrary("WSock32.DLL");
139 if(hLib == NULL)
140 CROAK("Could not load WSock32.dll\n");
141
142 paccept = (LPSOCKACCEPT)GetAddress(hLib, "accept");
143 pbind = (LPSOCKBIND)GetAddress(hLib, "bind");
144 pclosesocket = (LPSOCKCLOSESOCKET)GetAddress(hLib, "closesocket");
145 pconnect = (LPSOCKCONNECT)GetAddress(hLib, "connect");
146 pioctlsocket = (LPSOCKIOCTLSOCKET)GetAddress(hLib, "ioctlsocket");
147 pgetpeername = (LPSOCKGETPEERNAME)GetAddress(hLib, "getpeername");
148 pgetsockname = (LPSOCKGETSOCKNAME)GetAddress(hLib, "getsockname");
149 pgetsockopt = (LPSOCKGETSOCKOPT)GetAddress(hLib, "getsockopt");
150 phtonl = (LPSOCKHTONL)GetAddress(hLib, "htonl");
151 phtons = (LPSOCKHTONS)GetAddress(hLib, "htons");
152 plisten = (LPSOCKLISTEN)GetAddress(hLib, "listen");
153 pntohl = (LPSOCKNTOHL)GetAddress(hLib, "ntohl");
154 pntohs = (LPSOCKNTOHS)GetAddress(hLib, "ntohs");
155 precv = (LPSOCKRECV)GetAddress(hLib, "recv");
156 precvfrom = (LPSOCKRECVFROM)GetAddress(hLib, "recvfrom");
157 pselect = (LPSOCKSELECT)GetAddress(hLib, "select");
158 psend = (LPSOCKSEND)GetAddress(hLib, "send");
159 psendto = (LPSOCKSENDTO)GetAddress(hLib, "sendto");
160 psetsockopt = (LPSOCKSETSOCKOPT)GetAddress(hLib, "setsockopt");
161 pshutdown = (LPSOCKSHUTDOWN)GetAddress(hLib, "shutdown");
162 psocket = (LPSOCKSOCKET)GetAddress(hLib, "socket");
163 pgethostbyaddr = (LPSOCKGETHOSTBYADDR)GetAddress(hLib, "gethostbyaddr");
164 pgethostbyname = (LPSOCKGETHOSTBYNAME)GetAddress(hLib, "gethostbyname");
165 pgethostname = (LPSOCKGETHOSTNAME)GetAddress(hLib, "gethostname");
166 pgetservbyport = (LPSOCKGETSERVBYPORT)GetAddress(hLib, "getservbyport");
167 pgetservbyname = (LPSOCKGETSERVBYNAME)GetAddress(hLib, "getservbyname");
168 pgetprotobynumber = (LPSOCKGETPROTOBYNUMBER)GetAddress(hLib, "getprotobynumber");
169 pgetprotobyname = (LPSOCKGETPROTOBYNAME)GetAddress(hLib, "getprotobyname");
170 pWSAStartup = (LPSOCKWSASTARTUP)GetAddress(hLib, "WSAStartup");
171 pWSACleanup = (LPSOCKWSACLEANUP)GetAddress(hLib, "WSACleanup");
172 pWSAGetLastError = (LPSOCKWSAGETLASTERROR)GetAddress(hLib, "WSAGetLastError");
173 pWSAFDIsSet = (LPWSAFDIsSet)GetAddress(hLib, "__WSAFDIsSet");
174 pinet_addr = (LPSOCKINETADDR)GetAddress(hLib,"inet_addr");
175 pinet_ntoa = (LPSOCKINETNTOA)GetAddress(hLib,"inet_ntoa");
176
177 hWinSockDll = hLib;
178 }
179/* LeaveCriticalSection(&csSock); */
180}
181
182void
183EndSockets(void)
184{
185 if(hWinSockDll != NULL) {
186 pWSACleanup();
187 FreeLibrary(hWinSockDll);
188 }
189 hWinSockDll = NULL;
190}
191
192void
193StartSockets(void)
194{
195 unsigned short version;
196 WSADATA retdata;
197 int ret;
198 int iSockOpt = SO_SYNCHRONOUS_NONALERT;
199
200 LoadWinSock();
201 /*
202 * initalize the winsock interface and insure that it is
203 * cleaned up at exit.
204 */
205 version = 0x101;
206 if(ret = pWSAStartup(version, &retdata))
207 CROAK("Unable to locate winsock library!\n");
208 if(retdata.wVersion != version)
209 CROAK("Could not find version 1.1 of winsock dll\n");
210
211 /* atexit((void (*)(void)) EndSockets); */
0a753a76 212
213#ifdef USE_SOCKETS_AS_HANDLES
68dc0745 214 /*
215 * Enable the use of sockets as filehandles
216 */
217 psetsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
218 (char *)&iSockOpt, sizeof(iSockOpt));
219#endif /* USE_SOCKETS_AS_HANDLES */
0a753a76 220}
221
222
223#ifndef USE_SOCKETS_AS_HANDLES
68dc0745 224FILE *
225myfdopen(int fd, char *mode)
0a753a76 226{
68dc0745 227 FILE *fp;
228 char sockbuf[256];
229 int optlen = sizeof(sockbuf);
230 int retval;
0a753a76 231
68dc0745 232 if (hWinSockDll == 0)
233 LoadWinSock();
0a753a76 234
68dc0745 235 retval = pgetsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
236 if(retval == SOCKET_ERROR && pWSAGetLastError() == WSAENOTSOCK) {
237 return(_fdopen(fd, mode));
238 }
0a753a76 239
68dc0745 240 /*
241 * If we get here, then fd is actually a socket.
242 */
243 Newz(1601, fp, 1, FILE);
244 if(fp == NULL) {
245 errno = ENOMEM;
246 return NULL;
247 }
0a753a76 248
68dc0745 249 fp->_file = fd;
250 if(*mode == 'r')
251 fp->_flag = _IOREAD;
252 else
253 fp->_flag = _IOWRT;
254
255 return fp;
0a753a76 256}
68dc0745 257#endif /* USE_SOCKETS_AS_HANDLES */
0a753a76 258
259
68dc0745 260u_long
261win32_htonl(u_long hostlong)
0a753a76 262{
68dc0745 263 if(hWinSockDll == 0)
264 LoadWinSock();
0a753a76 265
68dc0745 266 return phtonl(hostlong);
0a753a76 267}
268
68dc0745 269u_short
270win32_htons(u_short hostshort)
0a753a76 271{
68dc0745 272 if(hWinSockDll == 0)
273 LoadWinSock();
0a753a76 274
68dc0745 275 return phtons(hostshort);
0a753a76 276}
277
68dc0745 278u_long
279win32_ntohl(u_long netlong)
0a753a76 280{
68dc0745 281 if(hWinSockDll == 0)
282 LoadWinSock();
0a753a76 283
68dc0745 284 return pntohl(netlong);
0a753a76 285}
286
68dc0745 287u_short
288win32_ntohs(u_short netshort)
0a753a76 289{
68dc0745 290 if(hWinSockDll == 0)
291 LoadWinSock();
0a753a76 292
68dc0745 293 return pntohs(netshort);
0a753a76 294}
295
296
297#define SOCKET_TEST(x, y) if(hWinSockDll == 0) StartSockets();\
68dc0745 298 if((x) == (y)) errno = pWSAGetLastError()
0a753a76 299
68dc0745 300#define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
0a753a76 301
68dc0745 302SOCKET
303win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
0a753a76 304{
68dc0745 305 SOCKET r;
0a753a76 306
68dc0745 307 SOCKET_TEST((r = paccept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
308 return OPEN_SOCKET(r);
0a753a76 309}
310
68dc0745 311int
312win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
0a753a76 313{
68dc0745 314 int r;
0a753a76 315
68dc0745 316 SOCKET_TEST_ERROR(r = pbind(TO_SOCKET(s), addr, addrlen));
317 return r;
0a753a76 318}
319
68dc0745 320int
321win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
0a753a76 322{
68dc0745 323 int r;
0a753a76 324
68dc0745 325 SOCKET_TEST_ERROR(r = pconnect(TO_SOCKET(s), addr, addrlen));
326 return r;
0a753a76 327}
328
329
68dc0745 330int
331win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
0a753a76 332{
68dc0745 333 int r;
0a753a76 334
68dc0745 335 SOCKET_TEST_ERROR(r = pgetpeername(TO_SOCKET(s), addr, addrlen));
336 return r;
0a753a76 337}
338
68dc0745 339int
340win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
0a753a76 341{
68dc0745 342 int r;
0a753a76 343
68dc0745 344 SOCKET_TEST_ERROR(r = pgetsockname(TO_SOCKET(s), addr, addrlen));
345 return r;
0a753a76 346}
347
68dc0745 348int
349win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
0a753a76 350{
68dc0745 351 int r;
0a753a76 352
68dc0745 353 SOCKET_TEST_ERROR(r = pgetsockopt(TO_SOCKET(s), level, optname, optval, optlen));
354 return r;
0a753a76 355}
356
68dc0745 357int
358win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
0a753a76 359{
68dc0745 360 int r;
0a753a76 361
68dc0745 362 SOCKET_TEST_ERROR(r = pioctlsocket(TO_SOCKET(s), cmd, argp));
363 return r;
0a753a76 364}
365
68dc0745 366int
367win32_listen(SOCKET s, int backlog)
0a753a76 368{
68dc0745 369 int r;
0a753a76 370
68dc0745 371 SOCKET_TEST_ERROR(r = plisten(TO_SOCKET(s), backlog));
372 return r;
0a753a76 373}
374
68dc0745 375int
376win32_recv(SOCKET s, char *buf, int len, int flags)
0a753a76 377{
68dc0745 378 int r;
0a753a76 379
68dc0745 380 SOCKET_TEST_ERROR(r = precv(TO_SOCKET(s), buf, len, flags));
381 return r;
0a753a76 382}
383
68dc0745 384int
385win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
0a753a76 386{
68dc0745 387 int r;
0a753a76 388
68dc0745 389 SOCKET_TEST_ERROR(r = precvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
390 return r;
0a753a76 391}
392
68dc0745 393/* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
394int
395win32_select(int nfds, int* rd, int* wr, int* ex, const struct timeval* timeout)
0a753a76 396{
68dc0745 397 long r;
398 int dummy = 0;
399 int i, fd, bit, offset;
400 FD_SET nrd, nwr, nex,*prd,*pwr,*pex;
0a753a76 401
68dc0745 402 if (!rd)
403 rd = &dummy, prd = NULL;
404 else
405 prd = &nrd;
406 if (!wr)
407 wr = &dummy, pwr = NULL;
408 else
409 pwr = &nwr;
410 if (!ex)
411 ex = &dummy, pex = NULL;
412 else
413 pex = &nex;
0a753a76 414
68dc0745 415 FD_ZERO(&nrd);
416 FD_ZERO(&nwr);
417 FD_ZERO(&nex);
418 for (i = 0; i < nfds; i++) {
419 fd = TO_SOCKET(i);
420 bit = 1L<<(i % (sizeof(int)*8));
421 offset = i / (sizeof(int)*8);
422 if (rd[offset] & bit)
423 FD_SET(fd, &nrd);
424 if (wr[offset] & bit)
425 FD_SET(fd, &nwr);
426 if (ex[offset] & bit)
427 FD_SET(fd, &nex);
428 }
429
430 SOCKET_TEST_ERROR(r = pselect(nfds, prd, pwr, pex, timeout));
0a753a76 431
68dc0745 432 for (i = 0; i < nfds; i++) {
433 fd = TO_SOCKET(i);
434 bit = 1L<<(i % (sizeof(int)*8));
435 offset = i / (sizeof(int)*8);
436 if (rd[offset] & bit) {
437 if (!pWSAFDIsSet(fd, &nrd))
438 rd[offset] &= ~bit;
439 }
440 if (wr[offset] & bit) {
441 if (!pWSAFDIsSet(fd, &nwr))
442 wr[offset] &= ~bit;
443 }
444 if (ex[offset] & bit) {
445 if (!pWSAFDIsSet(fd, &nex))
446 ex[offset] &= ~bit;
0a753a76 447 }
68dc0745 448 }
449 return r;
0a753a76 450}
451
68dc0745 452int
453win32_send(SOCKET s, const char *buf, int len, int flags)
0a753a76 454{
68dc0745 455 int r;
0a753a76 456
68dc0745 457 SOCKET_TEST_ERROR(r = psend(TO_SOCKET(s), buf, len, flags));
458 return r;
0a753a76 459}
460
68dc0745 461int
462win32_sendto(SOCKET s, const char *buf, int len, int flags,
463 const struct sockaddr *to, int tolen)
0a753a76 464{
68dc0745 465 int r;
0a753a76 466
68dc0745 467 SOCKET_TEST_ERROR(r = psendto(TO_SOCKET(s), buf, len, flags, to, tolen));
468 return r;
0a753a76 469}
470
68dc0745 471int
472win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
0a753a76 473{
68dc0745 474 int r;
0a753a76 475
68dc0745 476 SOCKET_TEST_ERROR(r = psetsockopt(TO_SOCKET(s), level, optname, optval, optlen));
477 return r;
0a753a76 478}
479
68dc0745 480int
481win32_shutdown(SOCKET s, int how)
0a753a76 482{
68dc0745 483 int r;
0a753a76 484
68dc0745 485 SOCKET_TEST_ERROR(r = pshutdown(TO_SOCKET(s), how));
486 return r;
0a753a76 487}
488
68dc0745 489SOCKET
490win32_socket(int af, int type, int protocol)
0a753a76 491{
68dc0745 492 SOCKET s;
0a753a76 493
494#ifndef USE_SOCKETS_AS_HANDLES
68dc0745 495 SOCKET_TEST(s = psocket(af, type, protocol), INVALID_SOCKET);
0a753a76 496#else
68dc0745 497 if(hWinSockDll == 0)
498 StartSockets();
0a753a76 499
68dc0745 500 if((s = psocket(af, type, protocol)) == INVALID_SOCKET)
501 errno = pWSAGetLastError();
502 else
503 s = OPEN_SOCKET(s);
504#endif /* USE_SOCKETS_AS_HANDLES */
0a753a76 505
68dc0745 506 return s;
0a753a76 507}
508
68dc0745 509struct hostent *
510win32_gethostbyaddr(const char *addr, int len, int type)
0a753a76 511{
68dc0745 512 struct hostent *r;
0a753a76 513
68dc0745 514 SOCKET_TEST(r = pgethostbyaddr(addr, len, type), NULL);
515 return r;
0a753a76 516}
517
68dc0745 518struct hostent *
519win32_gethostbyname(const char *name)
0a753a76 520{
68dc0745 521 struct hostent *r;
0a753a76 522
68dc0745 523 SOCKET_TEST(r = pgethostbyname(name), NULL);
524 return r;
0a753a76 525}
526
68dc0745 527int
528win32_gethostname(char *name, int len)
0a753a76 529{
68dc0745 530 int r;
0a753a76 531
68dc0745 532 SOCKET_TEST_ERROR(r = pgethostname(name, len));
533 return r;
0a753a76 534}
535
68dc0745 536struct protoent *
537win32_getprotobyname(const char *name)
0a753a76 538{
68dc0745 539 struct protoent *r;
0a753a76 540
68dc0745 541 SOCKET_TEST(r = pgetprotobyname(name), NULL);
542 return r;
0a753a76 543}
544
68dc0745 545struct protoent *
546win32_getprotobynumber(int num)
0a753a76 547{
68dc0745 548 struct protoent *r;
0a753a76 549
68dc0745 550 SOCKET_TEST(r = pgetprotobynumber(num), NULL);
551 return r;
0a753a76 552}
553
68dc0745 554struct servent *
555win32_getservbyname(const char *name, const char *proto)
0a753a76 556{
68dc0745 557 struct servent *r;
558
559 SOCKET_TEST(r = pgetservbyname(name, proto), NULL);
560 if (r) {
561 r = win32_savecopyservent(&myservent, r, proto);
562 }
563 return r;
0a753a76 564}
565
68dc0745 566struct servent *
567win32_getservbyport(int port, const char *proto)
0a753a76 568{
68dc0745 569 struct servent *r;
0a753a76 570
68dc0745 571 SOCKET_TEST(r = pgetservbyport(port, proto), NULL);
572 if (r) {
573 r = win32_savecopyservent(&myservent, r, proto);
574 }
575 return r;
0a753a76 576}
577
68dc0745 578char FAR *
579win32_inet_ntoa(struct in_addr in)
0a753a76 580{
68dc0745 581 if(hWinSockDll == 0)
582 LoadWinSock();
0a753a76 583
68dc0745 584 return pinet_ntoa(in);
0a753a76 585}
586
68dc0745 587unsigned long
588win32_inet_addr(const char FAR *cp)
0a753a76 589{
68dc0745 590 if(hWinSockDll == 0)
591 LoadWinSock();
0a753a76 592
68dc0745 593 return pinet_addr(cp);
0a753a76 594
595}
68dc0745 596
597/*
598 * Networking stubs
599 */
0a753a76 600#undef CROAK
601#define CROAK croak
602
68dc0745 603void
604win32_endhostent()
0a753a76 605{
68dc0745 606 CROAK("endhostent not implemented!\n");
0a753a76 607}
608
68dc0745 609void
610win32_endnetent()
0a753a76 611{
68dc0745 612 CROAK("endnetent not implemented!\n");
0a753a76 613}
614
68dc0745 615void
616win32_endprotoent()
0a753a76 617{
68dc0745 618 CROAK("endprotoent not implemented!\n");
0a753a76 619}
620
68dc0745 621void
622win32_endservent()
0a753a76 623{
68dc0745 624 CROAK("endservent not implemented!\n");
0a753a76 625}
626
627
68dc0745 628struct netent *
629win32_getnetent(void)
0a753a76 630{
68dc0745 631 CROAK("getnetent not implemented!\n");
632 return (struct netent *) NULL;
0a753a76 633}
634
68dc0745 635struct netent *
636win32_getnetbyname(char *name)
0a753a76 637{
68dc0745 638 CROAK("getnetbyname not implemented!\n");
639 return (struct netent *)NULL;
0a753a76 640}
641
68dc0745 642struct netent *
643win32_getnetbyaddr(long net, int type)
0a753a76 644{
68dc0745 645 CROAK("getnetbyaddr not implemented!\n");
646 return (struct netent *)NULL;
0a753a76 647}
648
68dc0745 649struct protoent *
650win32_getprotoent(void)
0a753a76 651{
68dc0745 652 CROAK("getprotoent not implemented!\n");
653 return (struct protoent *) NULL;
0a753a76 654}
655
68dc0745 656struct servent *
657win32_getservent(void)
0a753a76 658{
68dc0745 659 CROAK("getservent not implemented!\n");
660 return (struct servent *) NULL;
0a753a76 661}
662
68dc0745 663void
664win32_sethostent(int stayopen)
0a753a76 665{
68dc0745 666 CROAK("sethostent not implemented!\n");
0a753a76 667}
668
669
68dc0745 670void
671win32_setnetent(int stayopen)
0a753a76 672{
68dc0745 673 CROAK("setnetent not implemented!\n");
0a753a76 674}
675
676
68dc0745 677void
678win32_setprotoent(int stayopen)
0a753a76 679{
68dc0745 680 CROAK("setprotoent not implemented!\n");
0a753a76 681}
682
683
68dc0745 684void
685win32_setservent(int stayopen)
0a753a76 686{
68dc0745 687 CROAK("setservent not implemented!\n");
0a753a76 688}
689
137443ea 690#define WIN32IO_IS_STDIO
691#include <io.h>
692#include "win32iop.h"
693
694static struct servent*
695win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
696{
697 d->s_name = s->s_name;
698 d->s_aliases = s->s_aliases;
699 d->s_port = s->s_port;
700 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
701 d->s_proto = s->s_proto;
702 else if (proto && strlen(proto))
703 d->s_proto = (char *)proto;
704 else
705 d->s_proto = "tcp";
706
707 return d;
708}
709
0a753a76 710