This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Devel::PPPort 3.08_06
[perl5.git] / win32 / wincesck.c
CommitLineData
cb69f87a 1/* Time-stamp: <01/08/01 21:01:12 keuchel@w2k> */
e1caacb4
JH
2
3/* wincesck.c
4 *
f4257e4d 5 * (c) 1995 Microsoft Corporation. All rights reserved.
e1caacb4
JH
6 * Developed by hip communications inc., http://info.hip.com/info/
7 * Portions (c) 1993 Intergraph Corporation. All rights reserved.
8 *
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.
11 */
12
cb69f87a 13/* The socket calls use fd functions from celib... */
e1caacb4
JH
14
15#define WIN32IO_IS_STDIO
16#define WIN32SCK_IS_STDSCK
17#define WIN32_LEAN_AND_MEAN
18
19#ifdef __GNUC__
20#define Win32_Winsock
21#endif
22
23#include <windows.h>
24
25#define wince_private
26#include "errno.h"
27
28#include "EXTERN.h"
29#include "perl.h"
30
e1caacb4
JH
31#include "Win32iop.h"
32#include <sys/socket.h>
33
34#ifndef UNDER_CE
35#include <fcntl.h>
36#include <sys/stat.h>
37#include <assert.h>
38#include <io.h>
39#endif
40
41#ifdef UNDER_CE
42
43XCE_EXPORT struct servent *xcegetservbyname(const char *sname, const char *sproto);
44XCE_EXPORT struct servent * xcegetservbyport(int aport, const char *sproto);
45XCE_EXPORT struct protoent *xcegetprotobyname(const char *name);
46XCE_EXPORT struct protoent *xcegetprotobynumber(int number);
47
48#define getservbyname xcegetservbyname
49#define getservbyport xcegetservbyport
50#define getprotobyname xcegetprotobyname
51#define getprotobynumber xcegetprotobynumber
52
cb69f87a 53/* uses fdtab... */
e1caacb4
JH
54#include "cesocket2.h"
55
56#endif
57
58#define TO_SOCKET(X) (X)
59
e1caacb4
JH
60#define StartSockets() \
61 STMT_START { \
62 if (!wsock_started) { \
63 start_sockets(); \
64 set_socktype(); \
65 } \
66 } STMT_END
e1caacb4 67
e1caacb4
JH
68#define SOCKET_TEST(x, y) \
69 STMT_START { \
70 StartSockets(); \
71 if((x) == (y)) \
72 errno = WSAGetLastError(); \
73 } STMT_END
74
75#define SOCKET_TEST_ERROR(x) SOCKET_TEST(x, SOCKET_ERROR)
76
77static struct servent* win32_savecopyservent(struct servent*d,
78 struct servent*s,
79 const char *proto);
80
81static int wsock_started = 0;
82
f4257e4d
YO
83EXTERN_C void
84EndSockets(void)
85{
86 if (wsock_started)
87 WSACleanup();
88}
89
e1caacb4 90void
f4257e4d 91start_sockets(void)
e1caacb4 92{
acfe0abc 93 dTHX;
e1caacb4
JH
94 unsigned short version;
95 WSADATA retdata;
96 int ret;
97
98 /*
99 * initalize the winsock interface and insure that it is
100 * cleaned up at exit.
101 */
102 version = 0x101;
103 if(ret = WSAStartup(version, &retdata))
104 Perl_croak_nocontext("Unable to locate winsock library!\n");
105 if(retdata.wVersion != version)
106 Perl_croak_nocontext("Could not find version 1.1 of winsock dll\n");
107
108 /* atexit((void (*)(void)) EndSockets); */
109 wsock_started = 1;
110}
111
112void
113set_socktype(void)
114{
115}
116
117u_long
118win32_htonl(u_long hostlong)
119{
120 StartSockets();
121 return htonl(hostlong);
122}
123
124u_short
125win32_htons(u_short hostshort)
126{
127 StartSockets();
128 return htons(hostshort);
129}
130
131u_long
132win32_ntohl(u_long netlong)
133{
134 StartSockets();
135 return ntohl(netlong);
136}
137
138u_short
139win32_ntohs(u_short netshort)
140{
141 StartSockets();
142 return ntohs(netshort);
143}
144
145SOCKET
146win32_socket(int af, int type, int protocol)
147{
148 StartSockets();
149 return xcesocket(af, type, protocol);
150}
151
152SOCKET
153win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
154{
155 StartSockets();
156 return xceaccept(s, addr, addrlen);
157}
158
159int
160win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen)
161{
162 StartSockets();
163 return xcebind(s, addr, addrlen);
164}
165
166int
167win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen)
168{
169 StartSockets();
170 return xceconnect(s, addr, addrlen);
171}
172
173
174int
175win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen)
176{
177 StartSockets();
178 return xcegetpeername(s, addr, addrlen);
179}
180
181int
182win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen)
183{
184 StartSockets();
185 return xcegetsockname(s, addr, addrlen);
186}
187
188int
189win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
190{
191 StartSockets();
192 return xcegetsockopt(s, level, optname, optval, optlen);
193}
194
195int
196win32_ioctlsocket(SOCKET s, long cmd, u_long *argp)
197{
198 StartSockets();
199 return xceioctlsocket(s, cmd, argp);
200}
201
202int
203win32_listen(SOCKET s, int backlog)
204{
205 StartSockets();
206 return xcelisten(s, backlog);
207}
208
209int
210win32_recv(SOCKET s, char *buf, int len, int flags)
211{
212 StartSockets();
213 return xcerecv(s, buf, len, flags);
214}
215
216int
f4257e4d 217win32_recvfrom(SOCKET s, char *buf, int len, int flags,
e1caacb4
JH
218 struct sockaddr *from, int *fromlen)
219{
220 StartSockets();
221 return xcerecvfrom(s, buf, len, flags, from, fromlen);
222}
223
224int
f4257e4d 225win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr,
e1caacb4
JH
226 Perl_fd_set* ex, const struct timeval* timeout)
227{
228 StartSockets();
cb69f87a 229 /* select not yet fixed */
e1caacb4
JH
230 errno = ENOSYS;
231 return -1;
232}
233
234int
235win32_send(SOCKET s, const char *buf, int len, int flags)
236{
237 StartSockets();
238 return xcesend(s, buf, len, flags);
239}
240
241int
242win32_sendto(SOCKET s, const char *buf, int len, int flags,
243 const struct sockaddr *to, int tolen)
244{
245 StartSockets();
246 return xcesendto(s, buf, len, flags, to, tolen);
247}
248
249int
f4257e4d 250win32_setsockopt(SOCKET s, int level, int optname,
e1caacb4
JH
251 const char *optval, int optlen)
252{
253 StartSockets();
254 return xcesetsockopt(s, level, optname, optval, optlen);
255}
f4257e4d 256
e1caacb4
JH
257int
258win32_shutdown(SOCKET s, int how)
259{
260 StartSockets();
261 return xceshutdown(s, how);
262}
263
264int
265win32_closesocket(SOCKET s)
266{
267 StartSockets();
268 return xceclosesocket(s);
269}
270
271struct hostent *
272win32_gethostbyaddr(const char *addr, int len, int type)
273{
274 struct hostent *r;
275
276 SOCKET_TEST(r = gethostbyaddr(addr, len, type), NULL);
277 return r;
278}
279
280struct hostent *
281win32_gethostbyname(const char *name)
282{
283 struct hostent *r;
284
285 SOCKET_TEST(r = gethostbyname(name), NULL);
286 return r;
287}
288
289int
290win32_gethostname(char *name, int len)
291{
292 int r;
293
294 SOCKET_TEST_ERROR(r = gethostname(name, len));
295 return r;
296}
297
298struct protoent *
299win32_getprotobyname(const char *name)
300{
301 struct protoent *r;
302
303 SOCKET_TEST(r = getprotobyname(name), NULL);
304 return r;
305}
306
307struct protoent *
308win32_getprotobynumber(int num)
309{
310 struct protoent *r;
311
312 SOCKET_TEST(r = getprotobynumber(num), NULL);
313 return r;
314}
315
316struct servent *
317win32_getservbyname(const char *name, const char *proto)
318{
f4257e4d 319 dTHX;
e1caacb4
JH
320 struct servent *r;
321
322 SOCKET_TEST(r = getservbyname(name, proto), NULL);
323 if (r) {
324 r = win32_savecopyservent(&w32_servent, r, proto);
325 }
326 return r;
327}
328
329struct servent *
330win32_getservbyport(int port, const char *proto)
331{
f4257e4d 332 dTHX;
e1caacb4
JH
333 struct servent *r;
334
335 SOCKET_TEST(r = getservbyport(port, proto), NULL);
336 if (r) {
337 r = win32_savecopyservent(&w32_servent, r, proto);
338 }
339 return r;
340}
341
342int
343win32_ioctl(int i, unsigned int u, char *data)
344{
acfe0abc 345 dTHX;
e1caacb4
JH
346 u_long argp = (u_long)data;
347 int retval;
348
349 if (!wsock_started) {
350 Perl_croak_nocontext("ioctl implemented only on sockets");
351 /* NOTREACHED */
352 }
353
354 retval = ioctlsocket(TO_SOCKET(i), (long)u, &argp);
355 if (retval == SOCKET_ERROR) {
356 if (WSAGetLastError() == WSAENOTSOCK) {
357 Perl_croak_nocontext("ioctl implemented only on sockets");
358 /* NOTREACHED */
359 }
360 errno = WSAGetLastError();
361 }
362 return retval;
363}
364
365char FAR *
366win32_inet_ntoa(struct in_addr in)
367{
368 StartSockets();
369 return inet_ntoa(in);
370}
371
372unsigned long
373win32_inet_addr(const char FAR *cp)
374{
375 StartSockets();
376 return inet_addr(cp);
377}
378
379/*
380 * Networking stubs
381 */
382
383void
f4257e4d 384win32_endhostent()
e1caacb4 385{
acfe0abc 386 dTHX;
e1caacb4
JH
387 Perl_croak_nocontext("endhostent not implemented!\n");
388}
389
390void
391win32_endnetent()
392{
acfe0abc 393 dTHX;
e1caacb4
JH
394 Perl_croak_nocontext("endnetent not implemented!\n");
395}
396
397void
398win32_endprotoent()
399{
acfe0abc 400 dTHX;
e1caacb4
JH
401 Perl_croak_nocontext("endprotoent not implemented!\n");
402}
403
404void
405win32_endservent()
406{
acfe0abc 407 dTHX;
e1caacb4
JH
408 Perl_croak_nocontext("endservent not implemented!\n");
409}
410
411
412struct netent *
f4257e4d 413win32_getnetent(void)
e1caacb4 414{
acfe0abc 415 dTHX;
e1caacb4
JH
416 Perl_croak_nocontext("getnetent not implemented!\n");
417 return (struct netent *) NULL;
418}
419
420struct netent *
f4257e4d 421win32_getnetbyname(char *name)
e1caacb4 422{
acfe0abc 423 dTHX;
e1caacb4
JH
424 Perl_croak_nocontext("getnetbyname not implemented!\n");
425 return (struct netent *)NULL;
426}
427
428struct netent *
f4257e4d 429win32_getnetbyaddr(long net, int type)
e1caacb4 430{
acfe0abc 431 dTHX;
e1caacb4
JH
432 Perl_croak_nocontext("getnetbyaddr not implemented!\n");
433 return (struct netent *)NULL;
434}
435
436struct protoent *
f4257e4d 437win32_getprotoent(void)
e1caacb4 438{
acfe0abc 439 dTHX;
e1caacb4
JH
440 Perl_croak_nocontext("getprotoent not implemented!\n");
441 return (struct protoent *) NULL;
442}
443
444struct servent *
f4257e4d 445win32_getservent(void)
e1caacb4 446{
acfe0abc 447 dTHX;
e1caacb4
JH
448 Perl_croak_nocontext("getservent not implemented!\n");
449 return (struct servent *) NULL;
450}
451
452void
453win32_sethostent(int stayopen)
454{
acfe0abc 455 dTHX;
e1caacb4
JH
456 Perl_croak_nocontext("sethostent not implemented!\n");
457}
458
459
460void
461win32_setnetent(int stayopen)
462{
acfe0abc 463 dTHX;
e1caacb4
JH
464 Perl_croak_nocontext("setnetent not implemented!\n");
465}
466
467
468void
469win32_setprotoent(int stayopen)
470{
acfe0abc 471 dTHX;
e1caacb4
JH
472 Perl_croak_nocontext("setprotoent not implemented!\n");
473}
474
475
476void
477win32_setservent(int stayopen)
478{
acfe0abc 479 dTHX;
e1caacb4
JH
480 Perl_croak_nocontext("setservent not implemented!\n");
481}
482
483static struct servent*
484win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
485{
486 d->s_name = s->s_name;
487 d->s_aliases = s->s_aliases;
488 d->s_port = s->s_port;
489#ifndef __BORLANDC__ /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
490 if (!IsWin95() && s->s_proto && strlen(s->s_proto))
491 d->s_proto = s->s_proto;
492 else
493#endif
494 if (proto && strlen(proto))
495 d->s_proto = (char *)proto;
496 else
497 d->s_proto = "tcp";
f4257e4d 498
e1caacb4
JH
499 return d;
500}