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