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.
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.
11 #define WIN32IO_IS_STDIO
12 #define WIN32SCK_IS_STDSCK
13 #define WIN32_LEAN_AND_MEAN
21 #if defined(PERL_OBJECT)
27 #include <sys/socket.h>
35 /* thanks to Beverly Brown (beverly@datacube.com) */
36 #ifdef USE_SOCKETS_AS_HANDLES
37 # define OPEN_SOCKET(x) win32_open_osfhandle(x,O_RDWR|O_BINARY)
38 # define TO_SOCKET(x) _get_osfhandle(x)
40 # define OPEN_SOCKET(x) (x)
41 # define TO_SOCKET(x) (x)
42 #endif /* USE_SOCKETS_AS_HANDLES */
44 #if defined(USE_THREADS) || defined(USE_ITHREADS)
45 #define StartSockets() \
52 #define StartSockets() \
54 if (!wsock_started) { \
61 #define SOCKET_TEST(x, y) \
65 errno = CALL(WSAGetLastError)(); \
68 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
70 static struct servent* win32_savecopyservent(struct servent*d,
74 static int wsock_started = 0;
76 #ifdef PERL_WIN32_SOCK_DLOAD /* we load the socket libraries when needed -- BKS 5-29-2000 */
77 #define CALL(x) (*p ## x)
78 typedef SOCKET (PASCAL *Paccept)(SOCKET,struct sockaddr*,int*);
79 typedef int (PASCAL *Pbind)(SOCKET,const struct sockaddr*,int);
80 typedef int (PASCAL *Pclosesocket)(SOCKET);
81 typedef int (PASCAL *Pconnect)(SOCKET,const struct sockaddr*,int);
82 typedef int (PASCAL *Pioctlsocket)(SOCKET,long,u_long *);
83 typedef int (PASCAL *Pgetpeername)(SOCKET,struct sockaddr*,int*);
84 typedef int (PASCAL *Pgetsockname)(SOCKET,struct sockaddr*,int*);
85 typedef int (PASCAL *Pgetsockopt)(SOCKET,int,int,char*,int*);
86 typedef unsigned long (PASCAL *Pinet_addr)(const char*);
87 typedef char * (PASCAL *Pinet_ntoa)(struct in_addr);
88 typedef int (PASCAL *Plisten)(SOCKET,int);
89 typedef int (PASCAL *Precv)(SOCKET,char*,int,int);
90 typedef int (PASCAL *Precvfrom)(SOCKET,char*,int,int,struct sockaddr*,int*);
91 typedef int (PASCAL *Psend)(SOCKET,const char*,int,int);
92 typedef int (PASCAL *Psendto)(SOCKET,const char*,int,int,const struct sockaddr*,int);
93 typedef int (PASCAL *Psetsockopt)(SOCKET,int,int,const char*,int);
94 typedef int (PASCAL *Pshutdown)(SOCKET,int);
95 typedef SOCKET (PASCAL *Psocket)(int,int,int);
96 typedef struct hostent* (PASCAL *Pgethostbyaddr)(const char*,int,int);
97 typedef struct hostent* (PASCAL *Pgethostbyname)(const char*);
98 typedef struct servent* (PASCAL *Pgetservbyport)(int,const char*);
99 typedef struct servent* (PASCAL *Pgetservbyname)(const char*,const char*);
100 typedef struct protoent* (PASCAL *Pgetprotobynumber)(int);
101 typedef struct protoent* (PASCAL *Pgetprotobyname)(const char*);
102 typedef int (PASCAL *PWSACleanup)(void);
103 typedef int (PASCAL *PWSAStartup)(unsigned short, WSADATA*);
104 typedef void (PASCAL *PWSASetLastError)(int);
105 typedef int (PASCAL *PWSAGetLastError)(void);
106 typedef int (PASCAL *P__WSAFDIsSet)(SOCKET,fd_set*);
107 typedef int (PASCAL *Pselect)(int nfds,fd_set*,fd_set*,fd_set*,const struct timeval*);
108 typedef int (PASCAL *Pgethostname)(char*,int);
109 typedef u_long (PASCAL *Phtonl)(u_long), (PASCAL *Pntohl)(u_long);
110 typedef u_short (PASCAL *Phtons)(u_short), (PASCAL *Pntohs)(u_short);
111 static Paccept paccept;
113 static Pclosesocket pclosesocket;
114 static Pconnect pconnect;
115 static Pioctlsocket pioctlsocket;
116 static Pgetpeername pgetpeername;
117 static Pgetsockname pgetsockname;
118 static Pgetsockopt pgetsockopt;
119 static Pinet_addr pinet_addr;
120 static Pinet_ntoa pinet_ntoa;
121 static Plisten plisten;
123 static Precvfrom precvfrom;
125 static Psendto psendto;
126 static Psetsockopt psetsockopt;
127 static Pshutdown pshutdown;
128 static Psocket psocket;
129 static Pgethostbyaddr pgethostbyaddr;
130 static Pgethostbyname pgethostbyname;
131 static Pgetservbyport pgetservbyport;
132 static Pgetservbyname pgetservbyname;
133 static Pgetprotobynumber pgetprotobynumber;
134 static Pgetprotobyname pgetprotobyname;
135 static PWSAStartup pWSAStartup;
136 static PWSACleanup pWSACleanup;
137 static PWSASetLastError pWSASetLastError;
138 static PWSAGetLastError pWSAGetLastError;
139 static P__WSAFDIsSet p__WSAFDIsSet;
140 static Pselect pselect;
141 static Pgethostname pgethostname;
142 #if BYTEORDER != 0x1234
143 static Phtons phtons;
144 static Pntohs pntohs;
145 static Phtonl phtonl;
146 static Pntohl pntohl;
148 void end_sockets(pTHXo_ void *ptr)
156 #endif /* PERL_WIN32_SOCK_DLOAD */
161 unsigned short version;
164 #ifdef PERL_WIN32_SOCK_DLOAD
165 HANDLE hDll = LoadLibraryA("wsock32.dll");
168 * initalize the winsock interface and insure that it is
169 * cleaned up at exit.
170 * Also, only load the DLL when needed -- BKS, 4-2-2000
173 (paccept = (Paccept)GetProcAddress(hDll, "accept")) &&
174 (pbind = (Pbind)GetProcAddress(hDll, "bind")) &&
175 (pclosesocket = (Pclosesocket)GetProcAddress(hDll, "closesocket")) &&
176 (pconnect = (Pconnect)GetProcAddress(hDll, "connect")) &&
177 (pioctlsocket = (Pioctlsocket)GetProcAddress(hDll, "ioctlsocket")) &&
178 (pgetpeername = (Pgetpeername)GetProcAddress(hDll, "getpeername")) &&
179 (pgetsockname = (Pgetsockname)GetProcAddress(hDll, "getsockname")) &&
180 (pgetsockopt = (Pgetsockopt)GetProcAddress(hDll, "getsockopt")) &&
181 (pinet_addr = (Pinet_addr)GetProcAddress(hDll, "inet_addr")) &&
182 (pinet_ntoa = (Pinet_ntoa)GetProcAddress(hDll, "inet_ntoa")) &&
183 (plisten = (Plisten)GetProcAddress(hDll, "listen")) &&
184 (precv = (Precv)GetProcAddress(hDll, "recv")) &&
185 (precvfrom = (Precvfrom)GetProcAddress(hDll, "recvfrom")) &&
186 (psend = (Psend)GetProcAddress(hDll, "send")) &&
187 (psendto = (Psendto)GetProcAddress(hDll, "sendto")) &&
188 (psetsockopt = (Psetsockopt)GetProcAddress(hDll, "setsockopt")) &&
189 (pshutdown = (Pshutdown)GetProcAddress(hDll, "shutdown")) &&
190 (psocket = (Psocket)GetProcAddress(hDll, "socket")) &&
191 (pgethostbyaddr = (Pgethostbyaddr)GetProcAddress(hDll, "gethostbyaddr")) &&
192 (pgethostbyname = (Pgethostbyname)GetProcAddress(hDll, "gethostbyname")) &&
193 (pgetservbyport = (Pgetservbyport)GetProcAddress(hDll, "getservbyport")) &&
194 (pgetservbyname = (Pgetservbyname)GetProcAddress(hDll, "getservbyname")) &&
195 (pgetprotobynumber = (Pgetprotobynumber)GetProcAddress(hDll, "getprotobynumber")) &&
196 (pgetprotobyname = (Pgetprotobyname)GetProcAddress(hDll, "getprotobyname")) &&
197 (pWSAStartup = (PWSAStartup)GetProcAddress(hDll, "WSAStartup")) &&
198 (pWSACleanup = (PWSACleanup)GetProcAddress(hDll, "WSACleanup")) &&
199 (pWSASetLastError = (PWSASetLastError)GetProcAddress(hDll, "WSASetLastError")) &&
200 (pWSAGetLastError = (PWSAGetLastError)GetProcAddress(hDll, "WSAGetLastError")) &&
201 (p__WSAFDIsSet = (P__WSAFDIsSet)GetProcAddress(hDll, "__WSAFDIsSet")) &&
202 (pselect = (Pselect)GetProcAddress(hDll, "select")) &&
203 (pgethostname = (Pgethostname)GetProcAddress(hDll, "gethostname")) &&
204 #if BYTEORDER != 0x1234
205 (phtonl = (Phtonl)GetProcAddress(hDll, "htonl")) &&
206 (pntohl = (Pntohl)GetProcAddress(hDll, "ntohl")) &&
207 (phtons = (Pntohs)GetProcAddress(hDll, "htons")) &&
208 (pntohs = (Pntohs)GetProcAddress(hDll, "ntohs")
213 Perl_croak(aTHX_ "Unable to load winsock library!\n");
215 #endif /* PERL_WIN32_SOCK_DLOAD */
217 if(ret = CALL(WSAStartup)(version, &retdata))
218 Perl_croak(aTHX_ "Unable to initialize winsock library!\n");
219 if(retdata.wVersion != version)
220 Perl_croak(aTHX_ "Could not find version 1.1 of winsock dll\n");
222 #ifdef PERL_WIN32_SOCK_DLOAD
223 call_atexit(end_sockets, hDll);
231 #ifdef USE_SOCKETS_AS_HANDLES
234 if (!w32_init_socktype) {
236 int iSockOpt = SO_SYNCHRONOUS_NONALERT;
238 * Enable the use of sockets as filehandles
240 CALL(setsockopt)(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
241 (char *)&iSockOpt, sizeof(iSockOpt));
243 w32_init_socktype = 1;
246 #endif /* USE_SOCKETS_AS_HANDLES */
250 #ifndef USE_SOCKETS_AS_HANDLES
253 my_fdopen(int fd, char *mode)
257 int optlen = sizeof(sockbuf);
261 return(fdopen(fd, mode));
263 retval = CALL(getsockopt)((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
264 if(retval == SOCKET_ERROR && CALL(WSAGetLastError)() == WSAENOTSOCK) {
265 return(fdopen(fd, mode));
269 * If we get here, then fd is actually a socket.
271 Newz(1310, fp, 1, FILE);
285 #endif /* USE_SOCKETS_AS_HANDLES */
287 /* ntoh* and hton* are implemented here so that ByteLoader doesn't need to load WinSock;
288 these functions are simply copied from util.c */
291 win32_htonl(u_long l)
293 #if BYTEORDER == 0x1234
294 union { u_long r; char c[4]; } u;
295 u.c[0] = (l >> 24) & 255;
296 u.c[1] = (l >> 16) & 255;
297 u.c[2] = (l >> 8) & 255;
302 return CALL(htonl)(l);
307 win32_htons(u_short s)
309 #if BYTEORDER == 0x1234
310 return (((s >> 8) & 255) | ((s & 255) << 8));
313 return CALL(htons)(s);
318 win32_ntohl(u_long l)
320 #if BYTEORDER == 0x1234
321 union { u_long r; char c[4]; } u;
322 u.c[0] = (l >> 24) & 255;
323 u.c[1] = (l >> 16) & 255;
324 u.c[2] = (l >> 8) & 255;
329 return CALL(ntohl)(l);
334 win32_ntohs(u_short s)
336 #if BYTEORDER == 0x1234
337 return (((s >> 8) & 255) | ((s & 255) << 8));
340 return CALL(ntohs)(s);
347 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
351 SOCKET_TEST((r = CALL(accept)(TO_SOCKET(s), addr, addrlen)), INVALID_SOCKET);
352 return OPEN_SOCKET(r);
356 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
360 SOCKET_TEST_ERROR(r = CALL(bind)(TO_SOCKET(s), addr, addrlen));
365 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
369 SOCKET_TEST_ERROR(r = CALL(connect)(TO_SOCKET(s), addr, addrlen));
375 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
379 SOCKET_TEST_ERROR(r = CALL(getpeername)(TO_SOCKET(s), addr, addrlen));
384 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
388 SOCKET_TEST_ERROR(r = CALL(getsockname)(TO_SOCKET(s), addr, addrlen));
393 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
397 SOCKET_TEST_ERROR(r = CALL(getsockopt)(TO_SOCKET(s), level, optname, optval, optlen));
402 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
406 SOCKET_TEST_ERROR(r = CALL(ioctlsocket)(TO_SOCKET(s), cmd, argp));
411 win32_listen(SOCKET s, int backlog)
415 SOCKET_TEST_ERROR(r = CALL(listen)(TO_SOCKET(s), backlog));
420 win32_recv(SOCKET s, char *buf, int len, int flags)
424 SOCKET_TEST_ERROR(r = CALL(recv)(TO_SOCKET(s), buf, len, flags));
429 win32_recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
432 int frombufsize = *fromlen;
434 SOCKET_TEST_ERROR(r = CALL(recvfrom)(TO_SOCKET(s), buf, len, flags, from, fromlen));
435 /* Winsock's recvfrom() only returns a valid 'from' when the socket
436 * is connectionless. Perl expects a valid 'from' for all types
437 * of sockets, so go the extra mile.
439 if (r != SOCKET_ERROR && frombufsize == *fromlen)
440 (void)win32_getpeername(s, from, fromlen);
444 /* select contributed by Vincent R. Slyngstad (vrs@ibeam.intel.com) */
446 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
449 #ifdef USE_SOCKETS_AS_HANDLES
451 int i, fd, bit, offset;
452 FD_SET nrd, nwr, nex, *prd, *pwr, *pex;
454 /* winsock seems incapable of dealing with all three null fd_sets,
455 * so do the (millisecond) sleep as a special case
457 if (!(rd || wr || ex)) {
459 Sleep(timeout->tv_sec * 1000 +
460 timeout->tv_usec / 1000); /* do the best we can */
466 PERL_FD_ZERO(&dummy);
468 rd = &dummy, prd = NULL;
472 wr = &dummy, pwr = NULL;
476 ex = &dummy, pex = NULL;
483 for (i = 0; i < nfds; i++) {
485 if (PERL_FD_ISSET(i,rd))
487 if (PERL_FD_ISSET(i,wr))
489 if (PERL_FD_ISSET(i,ex))
493 SOCKET_TEST_ERROR(r = CALL(select)(nfds, prd, pwr, pex, timeout));
495 for (i = 0; i < nfds; i++) {
497 if (PERL_FD_ISSET(i,rd) && !CALL(__WSAFDIsSet)(fd, &nrd))
499 if (PERL_FD_ISSET(i,wr) && !CALL(__WSAFDIsSet)(fd, &nwr))
501 if (PERL_FD_ISSET(i,ex) && !CALL(__WSAFDIsSet)(fd, &nex))
505 SOCKET_TEST_ERROR(r = CALL(select)(nfds, rd, wr, ex, timeout));
511 win32_send(SOCKET s, const char *buf, int len, int flags)
515 SOCKET_TEST_ERROR(r = CALL(send)(TO_SOCKET(s), buf, len, flags));
520 win32_sendto(SOCKET s, const char *buf, int len, int flags,
521 const struct sockaddr *to, int tolen)
525 SOCKET_TEST_ERROR(r = CALL(sendto)(TO_SOCKET(s), buf, len, flags, to, tolen));
530 win32_setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
534 SOCKET_TEST_ERROR(r = CALL(setsockopt)(TO_SOCKET(s), level, optname, optval, optlen));
539 win32_shutdown(SOCKET s, int how)
543 SOCKET_TEST_ERROR(r = CALL(shutdown)(TO_SOCKET(s), how));
548 win32_closesocket(SOCKET s)
552 SOCKET_TEST_ERROR(r = CALL(closesocket)(TO_SOCKET(s)));
557 win32_socket(int af, int type, int protocol)
561 #ifndef USE_SOCKETS_AS_HANDLES
562 SOCKET_TEST(s = CALL(socket)(af, type, protocol), INVALID_SOCKET);
565 if((s = CALL(socket)(af, type, protocol)) == INVALID_SOCKET)
566 errno = CALL(WSAGetLastError)();
569 #endif /* USE_SOCKETS_AS_HANDLES */
579 if (!wsock_started) /* No WinSock? */
580 return(fclose(pf)); /* Then not a socket. */
581 osf = TO_SOCKET(fileno(pf));/* Get it now before it's gone! */
582 retval = fclose(pf); /* Must fclose() before closesocket() */
584 && CALL(closesocket)(osf) == SOCKET_ERROR
585 && CALL(WSAGetLastError)() != WSAENOTSOCK)
593 win32_gethostbyaddr(const char *addr, int len, int type)
597 SOCKET_TEST(r = CALL(gethostbyaddr)(addr, len, type), NULL);
602 win32_gethostbyname(const char *name)
606 SOCKET_TEST(r = CALL(gethostbyname)(name), NULL);
611 win32_gethostname(char *name, int len)
615 SOCKET_TEST_ERROR(r = CALL(gethostname)(name, len));
620 win32_getprotobyname(const char *name)
624 SOCKET_TEST(r = CALL(getprotobyname)(name), NULL);
629 win32_getprotobynumber(int num)
633 SOCKET_TEST(r = CALL(getprotobynumber)(num), NULL);
638 win32_getservbyname(const char *name, const char *proto)
643 SOCKET_TEST(r = CALL(getservbyname)(name, proto), NULL);
645 r = win32_savecopyservent(&w32_servent, r, proto);
651 win32_getservbyport(int port, const char *proto)
656 SOCKET_TEST(r = CALL(getservbyport)(port, proto), NULL);
658 r = win32_savecopyservent(&w32_servent, r, proto);
664 win32_ioctl(int i, unsigned int u, char *data)
667 u_long argp = (u_long)data;
670 if (!wsock_started) {
671 Perl_croak_nocontext("ioctl implemented only on sockets");
675 retval = CALL(ioctlsocket)(TO_SOCKET(i), (long)u, &argp);
676 if (retval == SOCKET_ERROR) {
677 if (CALL(WSAGetLastError)() == WSAENOTSOCK) {
678 Perl_croak_nocontext("ioctl implemented only on sockets");
681 errno = CALL(WSAGetLastError)();
687 win32_inet_ntoa(struct in_addr in)
690 return CALL(inet_ntoa)(in);
694 win32_inet_addr(const char FAR *cp)
697 return CALL(inet_addr)(cp);
708 Perl_croak_nocontext("endhostent not implemented!\n");
715 Perl_croak_nocontext("endnetent not implemented!\n");
722 Perl_croak_nocontext("endprotoent not implemented!\n");
729 Perl_croak_nocontext("endservent not implemented!\n");
734 win32_getnetent(void)
737 Perl_croak_nocontext("getnetent not implemented!\n");
738 return (struct netent *) NULL;
742 win32_getnetbyname(char *name)
745 Perl_croak_nocontext("getnetbyname not implemented!\n");
746 return (struct netent *)NULL;
750 win32_getnetbyaddr(long net, int type)
753 Perl_croak_nocontext("getnetbyaddr not implemented!\n");
754 return (struct netent *)NULL;
758 win32_getprotoent(void)
761 Perl_croak_nocontext("getprotoent not implemented!\n");
762 return (struct protoent *) NULL;
766 win32_getservent(void)
769 Perl_croak_nocontext("getservent not implemented!\n");
770 return (struct servent *) NULL;
774 win32_sethostent(int stayopen)
777 Perl_croak_nocontext("sethostent not implemented!\n");
782 win32_setnetent(int stayopen)
785 Perl_croak_nocontext("setnetent not implemented!\n");
790 win32_setprotoent(int stayopen)
793 Perl_croak_nocontext("setprotoent not implemented!\n");
798 win32_setservent(int stayopen)
801 Perl_croak_nocontext("setservent not implemented!\n");
804 static struct servent*
805 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
807 d->s_name = s->s_name;
808 d->s_aliases = s->s_aliases;
809 d->s_port = s->s_port;
810 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
811 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
812 d->s_proto = s->s_proto;
815 if (proto && strlen(proto))
816 d->s_proto = (char *)proto;