Implement new environment variable to allow the use of non-IFS
[p5sagit/p5-mst-13.2.git] / win32 / win32sck.c
1 /* win32sck.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 #define WIN32IO_IS_STDIO
12 #define WIN32SCK_IS_STDSCK
13 #define WIN32_LEAN_AND_MEAN
14 #define PERLIO_NOT_STDIO 0
15 #ifdef __GNUC__
16 #define Win32_Winsock
17 #endif
18 #include <windows.h>
19 #include <ws2spi.h>
20
21 #include "EXTERN.h"
22 #include "perl.h"
23
24 #include "Win32iop.h"
25 #include <sys/socket.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <assert.h>
29 #include <io.h>
30
31 /* thanks to Beverly Brown      (beverly@datacube.com) */
32 #ifdef USE_SOCKETS_AS_HANDLES
33 #       define OPEN_SOCKET(x)   win32_open_osfhandle(x,O_RDWR|O_BINARY)
34 #       define TO_SOCKET(x)     _get_osfhandle(x)
35 #else
36 #       define OPEN_SOCKET(x)   (x)
37 #       define TO_SOCKET(x)     (x)
38 #endif  /* USE_SOCKETS_AS_HANDLES */
39
40 #if defined(USE_ITHREADS)
41 #define StartSockets() \
42     STMT_START {                                        \
43         if (!wsock_started)                             \
44             start_sockets();                            \
45         set_socktype();                                 \
46     } STMT_END
47 #else
48 #define StartSockets() \
49     STMT_START {                                        \
50         if (!wsock_started) {                           \
51             start_sockets();                            \
52             set_socktype();                             \
53         }                                               \
54     } STMT_END
55 #endif
56
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
66 static struct servent* win32_savecopyservent(struct servent*d,
67                                              struct servent*s,
68                                              const char *proto);
69
70 static int wsock_started = 0;
71
72 EXTERN_C void
73 EndSockets(void)
74 {
75     if (wsock_started)
76         WSACleanup();
77 }
78
79 void
80 start_sockets(void) 
81 {
82     dTHX;
83     unsigned short version;
84     WSADATA retdata;
85     int ret;
86
87     /*
88      * initalize the winsock interface and insure that it is
89      * cleaned up at exit.
90      */
91     version = 0x2;
92     if(ret = WSAStartup(version, &retdata))
93         Perl_croak_nocontext("Unable to locate winsock library!\n");
94     if(retdata.wVersion != version)
95         Perl_croak_nocontext("Could not find version 2.0 of winsock dll\n");
96
97     /* atexit((void (*)(void)) EndSockets); */
98     wsock_started = 1;
99 }
100
101 void
102 set_socktype(void)
103 {
104 }
105
106
107 #ifndef USE_SOCKETS_AS_HANDLES
108 #undef fdopen
109 FILE *
110 my_fdopen(int fd, char *mode)
111 {
112     FILE *fp;
113     char sockbuf[256];
114     int optlen = sizeof(sockbuf);
115     int retval;
116
117     if (!wsock_started)
118         return(fdopen(fd, mode));
119
120     retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
121     if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
122         return(fdopen(fd, mode));
123     }
124
125     /*
126      * If we get here, then fd is actually a socket.
127      */
128     Newz(1310, fp, 1, FILE);    /* XXX leak, good thing this code isn't used */
129     if(fp == NULL) {
130         errno = ENOMEM;
131         return NULL;
132     }
133
134     fp->_file = fd;
135     if(*mode == 'r')
136         fp->_flag = _IOREAD;
137     else
138         fp->_flag = _IOWRT;
139    
140     return fp;
141 }
142 #endif  /* USE_SOCKETS_AS_HANDLES */
143
144
145 u_long
146 win32_htonl(u_long hostlong)
147 {
148     StartSockets();
149     return htonl(hostlong);
150 }
151
152 u_short
153 win32_htons(u_short hostshort)
154 {
155     StartSockets();
156     return htons(hostshort);
157 }
158
159 u_long
160 win32_ntohl(u_long netlong)
161 {
162     StartSockets();
163     return ntohl(netlong);
164 }
165
166 u_short
167 win32_ntohs(u_short netshort)
168 {
169     StartSockets();
170     return ntohs(netshort);
171 }
172
173
174
175 SOCKET
176 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
177 {
178     SOCKET r;
179
180     SOCKET_TEST((r = accept(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
181     return OPEN_SOCKET(r);
182 }
183
184 int
185 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
186 {
187     int r;
188
189     SOCKET_TEST_ERROR(r = bind(TO_SOCKET(s), addr, addrlen));
190     return r;
191 }
192
193 int
194 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
195 {
196     int r;
197
198     SOCKET_TEST_ERROR(r = connect(TO_SOCKET(s), addr, addrlen));
199     return r;
200 }
201
202
203 int
204 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
205 {
206     int r;
207
208     SOCKET_TEST_ERROR(r = getpeername(TO_SOCKET(s), addr, addrlen));
209     return r;
210 }
211
212 int
213 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
214 {
215     int r;
216
217     SOCKET_TEST_ERROR(r = getsockname(TO_SOCKET(s), addr, addrlen));
218     return r;
219 }
220
221 int
222 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
223 {
224     int r;
225
226     SOCKET_TEST_ERROR(r = getsockopt(TO_SOCKET(s), level, optname, optval, optlen));
227     return r;
228 }
229
230 int
231 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
232 {
233     int r;
234
235     SOCKET_TEST_ERROR(r = ioctlsocket(TO_SOCKET(s), cmd, argp));
236     return r;
237 }
238
239 int
240 win32_listen(SOCKET s, int backlog)
241 {
242     int r;
243
244     SOCKET_TEST_ERROR(r = listen(TO_SOCKET(s), backlog));
245     return r;
246 }
247
248 int
249 win32_recv(SOCKET s, char *buf, int len, int flags)
250 {
251     int r;
252
253     SOCKET_TEST_ERROR(r = recv(TO_SOCKET(s), buf, len, flags));
254     return r;
255 }
256
257 int
258 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
259 {
260     int r;
261     int frombufsize = *fromlen;
262
263     SOCKET_TEST_ERROR(r = recvfrom(TO_SOCKET(s), buf, len, flags, from, fromlen));
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);
270     return r;
271 }
272
273 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
274 int
275 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
276 {
277     int r;
278 #ifdef USE_SOCKETS_AS_HANDLES
279     Perl_fd_set dummy;
280     int i, fd, save_errno = errno;
281     FD_SET nrd, nwr, nex, *prd, *pwr, *pex;
282
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)) {
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);
292         return 0;
293     }
294     StartSockets();
295     PERL_FD_ZERO(&dummy);
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;
308
309     FD_ZERO(&nrd);
310     FD_ZERO(&nwr);
311     FD_ZERO(&nex);
312     for (i = 0; i < nfds; i++) {
313         fd = TO_SOCKET(i);
314         if (PERL_FD_ISSET(i,rd))
315             FD_SET(fd, &nrd);
316         if (PERL_FD_ISSET(i,wr))
317             FD_SET(fd, &nwr);
318         if (PERL_FD_ISSET(i,ex))
319             FD_SET(fd, &nex);
320     }
321
322     errno = save_errno;
323     SOCKET_TEST_ERROR(r = select(nfds, prd, pwr, pex, timeout));
324     save_errno = errno;
325
326     for (i = 0; i < nfds; i++) {
327         fd = TO_SOCKET(i);
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);
334     }
335     errno = save_errno;
336 #else
337     SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
338 #endif
339     return r;
340 }
341
342 int
343 win32_send(SOCKET s, const char *buf, int len, int flags)
344 {
345     int r;
346
347     SOCKET_TEST_ERROR(r = send(TO_SOCKET(s), buf, len, flags));
348     return r;
349 }
350
351 int
352 win32_sendto(SOCKET s, const char *buf, int len, int flags,
353              const struct sockaddr *to, int tolen)
354 {
355     int r;
356
357     SOCKET_TEST_ERROR(r = sendto(TO_SOCKET(s), buf, len, flags, to, tolen));
358     return r;
359 }
360
361 int
362 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
363 {
364     int r;
365
366     SOCKET_TEST_ERROR(r = setsockopt(TO_SOCKET(s), level, optname, optval, optlen));
367     return r;
368 }
369     
370 int
371 win32_shutdown(SOCKET s, int how)
372 {
373     int r;
374
375     SOCKET_TEST_ERROR(r = shutdown(TO_SOCKET(s), how));
376     return r;
377 }
378
379 int
380 win32_closesocket(SOCKET s)
381 {
382     int r;
383
384     SOCKET_TEST_ERROR(r = closesocket(TO_SOCKET(s)));
385     return r;
386 }
387
388 #ifdef USE_SOCKETS_AS_HANDLES
389 #define WIN32_OPEN_SOCKET(af, type, protocol) open_ifs_socket(af, type, protocol)
390
391 void
392 convert_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
398 SOCKET
399 open_ifs_socket(int af, int type, int protocol)
400 {
401     dTHX;
402     char *s;
403     unsigned long proto_buffers_len = 0;
404     int error_code;
405     SOCKET out = INVALID_SOCKET;
406
407     if ((s = PerlEnv_getenv("PERL_ALLOW_NON_IFS_LSP")) && atoi(s))
408         return WSASocket(af, type, protocol, NULL, 0, 0);
409
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
452 SOCKET
453 win32_socket(int af, int type, int protocol)
454 {
455     SOCKET s;
456
457 #ifndef USE_SOCKETS_AS_HANDLES
458     SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
459 #else
460     StartSockets();
461
462     if((s = WIN32_OPEN_SOCKET(af, type, protocol)) == INVALID_SOCKET)
463         errno = WSAGetLastError();
464     else
465         s = OPEN_SOCKET(s);
466 #endif  /* USE_SOCKETS_AS_HANDLES */
467
468     return s;
469 }
470
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
478 int 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
506 #undef fclose
507 int
508 my_fclose (FILE *pf)
509 {
510     int osf;
511     if (!wsock_started)         /* No WinSock? */
512         return(fclose(pf));     /* Then not a socket. */
513     osf = TO_SOCKET(win32_fileno(pf));/* Get it now before it's gone! */
514     if (osf != -1) {
515         int err;
516         win32_fflush(pf);
517         err = closesocket(osf);
518         if (err == 0) {
519 #if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
520             _set_osfhnd(win32_fileno(pf), INVALID_HANDLE_VALUE);
521 #endif
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         }
533     }
534     return fclose(pf);
535 }
536
537 #undef fstat
538 int
539 my_fstat(int fd, Stat_t *sbufptr)
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;
549     if (!wsock_started || IsWinNT()) {
550 #if defined(WIN64) || defined(USE_LARGE_FILES)
551 #if defined(__BORLANDC__) /* buk */
552         return win32_fstat(fd, sbufptr );
553 #else
554         return _fstati64(fd, sbufptr);
555 #endif
556 #else
557         return fstat(fd, sbufptr);
558 #endif
559     }
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) {
569 #if defined(__BORLANDC__)&&(__BORLANDC__<=0x520)
570             sbufptr->st_mode = S_IFIFO;
571 #else
572             sbufptr->st_mode = _S_IFIFO;
573 #endif
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;
578             sbufptr->st_size = (Off_t)0;
579             return 0;
580         }
581     }
582 #if defined(WIN64) || defined(USE_LARGE_FILES)
583 #if defined(__BORLANDC__) /* buk */
584     return win32_fstat(fd, sbufptr );
585 #else
586     return _fstati64(fd, sbufptr);
587 #endif
588 #else
589     return fstat(fd, sbufptr);
590 #endif
591 }
592
593 struct hostent *
594 win32_gethostbyaddr(const char *addr, int len, int type)
595 {
596     struct hostent *r;
597
598     SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
599     return r;
600 }
601
602 struct hostent *
603 win32_gethostbyname(const char *name)
604 {
605     struct hostent *r;
606
607     SOCKET_TEST(r = gethostbyname(name), NULL);
608     return r;
609 }
610
611 int
612 win32_gethostname(char *name, int len)
613 {
614     int r;
615
616     SOCKET_TEST_ERROR(r = gethostname(name, len));
617     return r;
618 }
619
620 struct protoent *
621 win32_getprotobyname(const char *name)
622 {
623     struct protoent *r;
624
625     SOCKET_TEST(r = getprotobyname(name), NULL);
626     return r;
627 }
628
629 struct protoent *
630 win32_getprotobynumber(int num)
631 {
632     struct protoent *r;
633
634     SOCKET_TEST(r = getprotobynumber(num), NULL);
635     return r;
636 }
637
638 struct servent *
639 win32_getservbyname(const char *name, const char *proto)
640 {
641     dTHX;    
642     struct servent *r;
643
644     SOCKET_TEST(r = getservbyname(name, proto), NULL);
645     if (r) {
646         r = win32_savecopyservent(&w32_servent, r, proto);
647     }
648     return r;
649 }
650
651 struct servent *
652 win32_getservbyport(int port, const char *proto)
653 {
654     dTHX; 
655     struct servent *r;
656
657     SOCKET_TEST(r = getservbyport(port, proto), NULL);
658     if (r) {
659         r = win32_savecopyservent(&w32_servent, r, proto);
660     }
661     return r;
662 }
663
664 int
665 win32_ioctl(int i, unsigned int u, char *data)
666 {
667     dTHX;
668     u_long argp = (u_long)data;
669     int retval;
670
671     if (!wsock_started) {
672         Perl_croak_nocontext("ioctl implemented only on sockets");
673         /* NOTREACHED */
674     }
675
676     retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
677     if (retval == SOCKET_ERROR) {
678         if (WSAGetLastError() == WSAENOTSOCK) {
679             Perl_croak_nocontext("ioctl implemented only on sockets");
680             /* NOTREACHED */
681         }
682         errno = WSAGetLastError();
683     }
684     return retval;
685 }
686
687 char FAR *
688 win32_inet_ntoa(struct in_addr in)
689 {
690     StartSockets();
691     return inet_ntoa(in);
692 }
693
694 unsigned long
695 win32_inet_addr(const char FAR *cp)
696 {
697     StartSockets();
698     return inet_addr(cp);
699 }
700
701 /*
702  * Networking stubs
703  */
704
705 void
706 win32_endhostent() 
707 {
708     dTHX;
709     Perl_croak_nocontext("endhostent not implemented!\n");
710 }
711
712 void
713 win32_endnetent()
714 {
715     dTHX;
716     Perl_croak_nocontext("endnetent not implemented!\n");
717 }
718
719 void
720 win32_endprotoent()
721 {
722     dTHX;
723     Perl_croak_nocontext("endprotoent not implemented!\n");
724 }
725
726 void
727 win32_endservent()
728 {
729     dTHX;
730     Perl_croak_nocontext("endservent not implemented!\n");
731 }
732
733
734 struct netent *
735 win32_getnetent(void) 
736 {
737     dTHX;
738     Perl_croak_nocontext("getnetent not implemented!\n");
739     return (struct netent *) NULL;
740 }
741
742 struct netent *
743 win32_getnetbyname(char *name) 
744 {
745     dTHX;
746     Perl_croak_nocontext("getnetbyname not implemented!\n");
747     return (struct netent *)NULL;
748 }
749
750 struct netent *
751 win32_getnetbyaddr(long net, int type) 
752 {
753     dTHX;
754     Perl_croak_nocontext("getnetbyaddr not implemented!\n");
755     return (struct netent *)NULL;
756 }
757
758 struct protoent *
759 win32_getprotoent(void) 
760 {
761     dTHX;
762     Perl_croak_nocontext("getprotoent not implemented!\n");
763     return (struct protoent *) NULL;
764 }
765
766 struct servent *
767 win32_getservent(void) 
768 {
769     dTHX;
770     Perl_croak_nocontext("getservent not implemented!\n");
771     return (struct servent *) NULL;
772 }
773
774 void
775 win32_sethostent(int stayopen)
776 {
777     dTHX;
778     Perl_croak_nocontext("sethostent not implemented!\n");
779 }
780
781
782 void
783 win32_setnetent(int stayopen)
784 {
785     dTHX;
786     Perl_croak_nocontext("setnetent not implemented!\n");
787 }
788
789
790 void
791 win32_setprotoent(int stayopen)
792 {
793     dTHX;
794     Perl_croak_nocontext("setprotoent not implemented!\n");
795 }
796
797
798 void
799 win32_setservent(int stayopen)
800 {
801     dTHX;
802     Perl_croak_nocontext("setservent not implemented!\n");
803 }
804
805 static struct servent*
806 win32_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;
811 #ifndef __BORLANDC__    /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
812     if (!IsWin95() && s->s_proto && strlen(s->s_proto))
813         d->s_proto = s->s_proto;
814     else
815 #endif
816     if (proto && strlen(proto))
817         d->s_proto = (char *)proto;
818     else
819         d->s_proto = "tcp";
820    
821     return d;
822 }
823
824