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
31 #if defined(PERL_OBJECT)
37 #include <sys/socket.h>
48 XCE_EXPORT struct servent *xcegetservbyname(const char *sname, const char *sproto);
49 XCE_EXPORT struct servent * xcegetservbyport(int aport, const char *sproto);
50 XCE_EXPORT struct protoent *xcegetprotobyname(const char *name);
51 XCE_EXPORT struct protoent *xcegetprotobynumber(int number);
53 #define getservbyname xcegetservbyname
54 #define getservbyport xcegetservbyport
55 #define getprotobyname xcegetprotobyname
56 #define getprotobynumber xcegetprotobynumber
59 #include "cesocket2.h"
63 #define TO_SOCKET(X) (X)
66 #define StartSockets() \
73 #define StartSockets() \
75 if (!wsock_started) { \
82 #define EndSockets() \
88 #define SOCKET_TEST(x, y) \
92 errno = WSAGetLastError(); \
95 #define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
97 static struct servent* win32_savecopyservent(struct servent*d,
101 static int wsock_started = 0;
107 unsigned short version;
112 * initalize the winsock interface and insure that it is
113 * cleaned up at exit.
116 if(ret = WSAStartup(version, &retdata))
117 Perl_croak_nocontext("Unable to locate winsock library!\n");
118 if(retdata.wVersion != version)
119 Perl_croak_nocontext("Could not find version 1.1 of winsock dll\n");
121 /* atexit((void (*)(void)) EndSockets); */
131 win32_htonl(u_long hostlong)
134 return htonl(hostlong);
138 win32_htons(u_short hostshort)
141 return htons(hostshort);
145 win32_ntohl(u_long netlong)
148 return ntohl(netlong);
152 win32_ntohs(u_short netshort)
155 return ntohs(netshort);
159 win32_socket(int af, int type, int protocol)
162 return xcesocket(af, type, protocol);
166 win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
169 return xceaccept(s, addr, addrlen);
173 win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
176 return xcebind(s, addr, addrlen);
180 win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
183 return xceconnect(s, addr, addrlen);
188 win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
191 return xcegetpeername(s, addr, addrlen);
195 win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
198 return xcegetsockname(s, addr, addrlen);
202 win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
205 return xcegetsockopt(s, level, optname, optval, optlen);
209 win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
212 return xceioctlsocket(s, cmd, argp);
216 win32_listen(SOCKET s, int backlog)
219 return xcelisten(s, backlog);
223 win32_recv(SOCKET s, char *buf, int len, int flags)
226 return xcerecv(s, buf, len, flags);
230 win32_recvfrom(SOCKET s, char *buf, int len, int flags,
231 struct sockaddr *from, int *fromlen)
234 return xcerecvfrom(s, buf, len, flags, from, fromlen);
238 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr,
239 Perl_fd_set* ex, const struct timeval* timeout)
242 // select not yet fixed
248 win32_send(SOCKET s, const char *buf, int len, int flags)
251 return xcesend(s, buf, len, flags);
255 win32_sendto(SOCKET s, const char *buf, int len, int flags,
256 const struct sockaddr *to, int tolen)
259 return xcesendto(s, buf, len, flags, to, tolen);
263 win32_setsockopt(SOCKET s, int level, int optname,
264 const char *optval, int optlen)
267 return xcesetsockopt(s, level, optname, optval, optlen);
271 win32_shutdown(SOCKET s, int how)
274 return xceshutdown(s, how);
278 win32_closesocket(SOCKET s)
281 return xceclosesocket(s);
285 win32_gethostbyaddr(const char *addr, int len, int type)
289 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
294 win32_gethostbyname(const char *name)
298 SOCKET_TEST(r = gethostbyname(name), NULL);
303 win32_gethostname(char *name, int len)
307 SOCKET_TEST_ERROR(r = gethostname(name, len));
312 win32_getprotobyname(const char *name)
316 SOCKET_TEST(r = getprotobyname(name), NULL);
321 win32_getprotobynumber(int num)
325 SOCKET_TEST(r = getprotobynumber(num), NULL);
330 win32_getservbyname(const char *name, const char *proto)
335 SOCKET_TEST(r = getservbyname(name, proto), NULL);
337 r = win32_savecopyservent(&w32_servent, r, proto);
343 win32_getservbyport(int port, const char *proto)
348 SOCKET_TEST(r = getservbyport(port, proto), NULL);
350 r = win32_savecopyservent(&w32_servent, r, proto);
356 win32_ioctl(int i, unsigned int u, char *data)
359 u_long argp = (u_long)data;
362 if (!wsock_started) {
363 Perl_croak_nocontext("ioctl implemented only on sockets");
367 retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
368 if (retval == SOCKET_ERROR) {
369 if (WSAGetLastError() == WSAENOTSOCK) {
370 Perl_croak_nocontext("ioctl implemented only on sockets");
373 errno = WSAGetLastError();
379 win32_inet_ntoa(struct in_addr in)
382 return inet_ntoa(in);
386 win32_inet_addr(const char FAR *cp)
389 return inet_addr(cp);
400 Perl_croak_nocontext("endhostent not implemented!\n");
407 Perl_croak_nocontext("endnetent not implemented!\n");
414 Perl_croak_nocontext("endprotoent not implemented!\n");
421 Perl_croak_nocontext("endservent not implemented!\n");
426 win32_getnetent(void)
429 Perl_croak_nocontext("getnetent not implemented!\n");
430 return (struct netent *) NULL;
434 win32_getnetbyname(char *name)
437 Perl_croak_nocontext("getnetbyname not implemented!\n");
438 return (struct netent *)NULL;
442 win32_getnetbyaddr(long net, int type)
445 Perl_croak_nocontext("getnetbyaddr not implemented!\n");
446 return (struct netent *)NULL;
450 win32_getprotoent(void)
453 Perl_croak_nocontext("getprotoent not implemented!\n");
454 return (struct protoent *) NULL;
458 win32_getservent(void)
461 Perl_croak_nocontext("getservent not implemented!\n");
462 return (struct servent *) NULL;
466 win32_sethostent(int stayopen)
469 Perl_croak_nocontext("sethostent not implemented!\n");
474 win32_setnetent(int stayopen)
477 Perl_croak_nocontext("setnetent not implemented!\n");
482 win32_setprotoent(int stayopen)
485 Perl_croak_nocontext("setprotoent not implemented!\n");
490 win32_setservent(int stayopen)
493 Perl_croak_nocontext("setservent not implemented!\n");
496 static struct servent*
497 win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
499 d->s_name = s->s_name;
500 d->s_aliases = s->s_aliases;
501 d->s_port = s->s_port;
502 #ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
503 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
504 d->s_proto = s->s_proto;
507 if (proto && strlen(proto))
508 d->s_proto = (char *)proto;