1 /* Time-stamp: <01/08/01 21:01:12 keuchel@w2k> */
5 * (c) 1995 Microsoft Corporation. All rights reserved.
6 * Developed by hip communications inc., http://info.hip.com/info/
7 * Portions (c) 1993 Intergraph Corporation. All rights reserved.
9 * You may distribute under the terms of either the GNU General Public
10 * License or the Artistic License, as specified in the README file.
13 /* The socket calls use fd functions from celib... */
15 #define WIN32IO_IS_STDIO
16 #define WIN32SCK_IS_STDSCK
17 #define WIN32_LEAN_AND_MEAN
32 #include <sys/socket.h>
43 XCE_EXPORT struct servent *xcegetservbyname(const char *sname, const char *sproto);
44 XCE_EXPORT struct servent * xcegetservbyport(int aport, const char *sproto);
45 XCE_EXPORT struct protoent *xcegetprotobyname(const char *name);
46 XCE_EXPORT struct protoent *xcegetprotobynumber(int number);
48 #define getservbyname xcegetservbyname
49 #define getservbyport xcegetservbyport
50 #define getprotobyname xcegetprotobyname
51 #define getprotobynumber xcegetprotobynumber
54 #include "cesocket2.h"
58 #define TO_SOCKET(X) (X)
60 #define StartSockets() \
62 if (!wsock_started) { \
68 #define EndSockets() \
74 #define SOCKET_TEST(x, y) \
78 errno = WSAGetLastError(); \
81 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
83 static struct servent* win32_savecopyservent(struct servent*d,
87 static int wsock_started = 0;
93 unsigned short version;
98 * initalize the winsock interface and insure that it is
102 if(ret = WSAStartup(version, &retdata))
103 Perl_croak_nocontext("Unable to locate winsock library!\n");
104 if(retdata.wVersion != version)
105 Perl_croak_nocontext("Could not find version 1.1 of winsock dll\n");
107 /* atexit((void (*)(void)) EndSockets); */
117 win32_htonl(u_long hostlong)
120 return htonl(hostlong);
124 win32_htons(u_short hostshort)
127 return htons(hostshort);
131 win32_ntohl(u_long netlong)
134 return ntohl(netlong);
138 win32_ntohs(u_short netshort)
141 return ntohs(netshort);
145 win32_socket(int af, int type, int protocol)
148 return xcesocket(af, type, protocol);
152 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
155 return xceaccept(s, addr, addrlen);
159 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
162 return xcebind(s, addr, addrlen);
166 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
169 return xceconnect(s, addr, addrlen);
174 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
177 return xcegetpeername(s, addr, addrlen);
181 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
184 return xcegetsockname(s, addr, addrlen);
188 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
191 return xcegetsockopt(s, level, optname, optval, optlen);
195 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
198 return xceioctlsocket(s, cmd, argp);
202 win32_listen(SOCKET s, int backlog)
205 return xcelisten(s, backlog);
209 win32_recv(SOCKET s, char *buf, int len, int flags)
212 return xcerecv(s, buf, len, flags);
216 win32_recvfrom(SOCKET s, char *buf, int len, int flags,
217 struct sockaddr *from, int *fromlen)
220 return xcerecvfrom(s, buf, len, flags, from, fromlen);
224 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr,
225 Perl_fd_set* ex, const struct timeval* timeout)
228 /* select not yet fixed */
234 win32_send(SOCKET s, const char *buf, int len, int flags)
237 return xcesend(s, buf, len, flags);
241 win32_sendto(SOCKET s, const char *buf, int len, int flags,
242 const struct sockaddr *to, int tolen)
245 return xcesendto(s, buf, len, flags, to, tolen);
249 win32_setsockopt(SOCKET s, int level, int optname,
250 const char *optval, int optlen)
253 return xcesetsockopt(s, level, optname, optval, optlen);
257 win32_shutdown(SOCKET s, int how)
260 return xceshutdown(s, how);
264 win32_closesocket(SOCKET s)
267 return xceclosesocket(s);
271 win32_gethostbyaddr(const char *addr, int len, int type)
275 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
280 win32_gethostbyname(const char *name)
284 SOCKET_TEST(r = gethostbyname(name), NULL);
289 win32_gethostname(char *name, int len)
293 SOCKET_TEST_ERROR(r = gethostname(name, len));
298 win32_getprotobyname(const char *name)
302 SOCKET_TEST(r = getprotobyname(name), NULL);
307 win32_getprotobynumber(int num)
311 SOCKET_TEST(r = getprotobynumber(num), NULL);
316 win32_getservbyname(const char *name, const char *proto)
321 SOCKET_TEST(r = getservbyname(name, proto), NULL);
323 r = win32_savecopyservent(&w32_servent, r, proto);
329 win32_getservbyport(int port, const char *proto)
334 SOCKET_TEST(r = getservbyport(port, proto), NULL);
336 r = win32_savecopyservent(&w32_servent, r, proto);
342 win32_ioctl(int i, unsigned int u, char *data)
345 u_long argp = (u_long)data;
348 if (!wsock_started) {
349 Perl_croak_nocontext("ioctl implemented only on sockets");
353 retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
354 if (retval == SOCKET_ERROR) {
355 if (WSAGetLastError() == WSAENOTSOCK) {
356 Perl_croak_nocontext("ioctl implemented only on sockets");
359 errno = WSAGetLastError();
365 win32_inet_ntoa(struct in_addr in)
368 return inet_ntoa(in);
372 win32_inet_addr(const char FAR *cp)
375 return inet_addr(cp);
386 Perl_croak_nocontext("endhostent not implemented!\n");
393 Perl_croak_nocontext("endnetent not implemented!\n");
400 Perl_croak_nocontext("endprotoent not implemented!\n");
407 Perl_croak_nocontext("endservent not implemented!\n");
412 win32_getnetent(void)
415 Perl_croak_nocontext("getnetent not implemented!\n");
416 return (struct netent *) NULL;
420 win32_getnetbyname(char *name)
423 Perl_croak_nocontext("getnetbyname not implemented!\n");
424 return (struct netent *)NULL;
428 win32_getnetbyaddr(long net, int type)
431 Perl_croak_nocontext("getnetbyaddr not implemented!\n");
432 return (struct netent *)NULL;
436 win32_getprotoent(void)
439 Perl_croak_nocontext("getprotoent not implemented!\n");
440 return (struct protoent *) NULL;
444 win32_getservent(void)
447 Perl_croak_nocontext("getservent not implemented!\n");
448 return (struct servent *) NULL;
452 win32_sethostent(int stayopen)
455 Perl_croak_nocontext("sethostent not implemented!\n");
460 win32_setnetent(int stayopen)
463 Perl_croak_nocontext("setnetent not implemented!\n");
468 win32_setprotoent(int stayopen)
471 Perl_croak_nocontext("setprotoent not implemented!\n");
476 win32_setservent(int stayopen)
479 Perl_croak_nocontext("setservent not implemented!\n");
482 static struct servent*
483 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
485 d->s_name = s->s_name;
486 d->s_aliases = s->s_aliases;
487 d->s_port = s->s_port;
488 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
489 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
490 d->s_proto = s->s_proto;
493 if (proto && strlen(proto))
494 d->s_proto = (char *)proto;