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() \
66 #define SOCKET_TEST(x, y) \
70 errno = WSAGetLastError(); \
73 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
75 static struct servent* win32_savecopyservent(struct servent*d,
79 static int wsock_started = 0;
92 unsigned short version;
97 * initalize the winsock interface and insure that it is
101 if(ret = WSAStartup(version, &retdata))
102 Perl_croak_nocontext("Unable to locate winsock library!\n");
103 if(retdata.wVersion != version)
104 Perl_croak_nocontext("Could not find version 1.1 of winsock dll\n");
106 /* atexit((void (*)(void)) EndSockets); */
111 win32_htonl(u_long hostlong)
114 return htonl(hostlong);
118 win32_htons(u_short hostshort)
121 return htons(hostshort);
125 win32_ntohl(u_long netlong)
128 return ntohl(netlong);
132 win32_ntohs(u_short netshort)
135 return ntohs(netshort);
139 win32_socket(int af, int type, int protocol)
142 return xcesocket(af, type, protocol);
146 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
149 return xceaccept(s, addr, addrlen);
153 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
156 return xcebind(s, addr, addrlen);
160 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
163 return xceconnect(s, addr, addrlen);
168 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
171 return xcegetpeername(s, addr, addrlen);
175 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
178 return xcegetsockname(s, addr, addrlen);
182 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
185 return xcegetsockopt(s, level, optname, optval, optlen);
189 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
192 return xceioctlsocket(s, cmd, argp);
196 win32_listen(SOCKET s, int backlog)
199 return xcelisten(s, backlog);
203 win32_recv(SOCKET s, char *buf, int len, int flags)
206 return xcerecv(s, buf, len, flags);
210 win32_recvfrom(SOCKET s, char *buf, int len, int flags,
211 struct sockaddr *from, int *fromlen)
214 return xcerecvfrom(s, buf, len, flags, from, fromlen);
218 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr,
219 Perl_fd_set* ex, const struct timeval* timeout)
222 /* select not yet fixed */
228 win32_send(SOCKET s, const char *buf, int len, int flags)
231 return xcesend(s, buf, len, flags);
235 win32_sendto(SOCKET s, const char *buf, int len, int flags,
236 const struct sockaddr *to, int tolen)
239 return xcesendto(s, buf, len, flags, to, tolen);
243 win32_setsockopt(SOCKET s, int level, int optname,
244 const char *optval, int optlen)
247 return xcesetsockopt(s, level, optname, optval, optlen);
251 win32_shutdown(SOCKET s, int how)
254 return xceshutdown(s, how);
258 win32_closesocket(SOCKET s)
261 return xceclosesocket(s);
265 win32_gethostbyaddr(const char *addr, int len, int type)
269 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
274 win32_gethostbyname(const char *name)
278 SOCKET_TEST(r = gethostbyname(name), NULL);
283 win32_gethostname(char *name, int len)
287 SOCKET_TEST_ERROR(r = gethostname(name, len));
292 win32_getprotobyname(const char *name)
296 SOCKET_TEST(r = getprotobyname(name), NULL);
301 win32_getprotobynumber(int num)
305 SOCKET_TEST(r = getprotobynumber(num), NULL);
310 win32_getservbyname(const char *name, const char *proto)
315 SOCKET_TEST(r = getservbyname(name, proto), NULL);
317 r = win32_savecopyservent(&w32_servent, r, proto);
323 win32_getservbyport(int port, const char *proto)
328 SOCKET_TEST(r = getservbyport(port, proto), NULL);
330 r = win32_savecopyservent(&w32_servent, r, proto);
336 win32_ioctl(int i, unsigned int u, char *data)
342 if (!wsock_started) {
343 Perl_croak_nocontext("ioctl implemented only on sockets");
347 /* mauke says using memcpy avoids alignment issues */
348 memcpy(&u_long_arg, data, sizeof u_long_arg);
349 retval = ioctlsocket(TO_SOCKET(i), (long)u, &u_long_arg);
350 memcpy(data, &u_long_arg, sizeof u_long_arg);
352 if (retval == SOCKET_ERROR) {
353 if (WSAGetLastError() == WSAENOTSOCK) {
354 Perl_croak_nocontext("ioctl implemented only on sockets");
357 errno = WSAGetLastError();
363 win32_inet_ntoa(struct in_addr in)
366 return inet_ntoa(in);
370 win32_inet_addr(const char FAR *cp)
373 return inet_addr(cp);
384 Perl_croak_nocontext("endhostent not implemented!\n");
391 Perl_croak_nocontext("endnetent not implemented!\n");
398 Perl_croak_nocontext("endprotoent not implemented!\n");
405 Perl_croak_nocontext("endservent not implemented!\n");
410 win32_getnetent(void)
413 Perl_croak_nocontext("getnetent not implemented!\n");
414 return (struct netent *) NULL;
418 win32_getnetbyname(char *name)
421 Perl_croak_nocontext("getnetbyname not implemented!\n");
422 return (struct netent *)NULL;
426 win32_getnetbyaddr(long net, int type)
429 Perl_croak_nocontext("getnetbyaddr not implemented!\n");
430 return (struct netent *)NULL;
434 win32_getprotoent(void)
437 Perl_croak_nocontext("getprotoent not implemented!\n");
438 return (struct protoent *) NULL;
442 win32_getservent(void)
445 Perl_croak_nocontext("getservent not implemented!\n");
446 return (struct servent *) NULL;
450 win32_sethostent(int stayopen)
453 Perl_croak_nocontext("sethostent not implemented!\n");
458 win32_setnetent(int stayopen)
461 Perl_croak_nocontext("setnetent not implemented!\n");
466 win32_setprotoent(int stayopen)
469 Perl_croak_nocontext("setprotoent not implemented!\n");
474 win32_setservent(int stayopen)
477 Perl_croak_nocontext("setservent not implemented!\n");
480 static struct servent*
481 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
483 d->s_name = s->s_name;
484 d->s_aliases = s->s_aliases;
485 d->s_port = s->s_port;
486 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
487 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
488 d->s_proto = s->s_proto;
491 if (proto && strlen(proto))
492 d->s_proto = (char *)proto;