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