Commit | Line | Data |
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 | |
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); |
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 | |
97 | static struct servent* win32_savecopyservent(struct servent*d, |
98 | struct servent*s, |
99 | const char *proto); |
100 | |
101 | static int wsock_started = 0; |
102 | |
103 | void |
104 | start_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 | |
125 | void |
126 | set_socktype(void) |
127 | { |
128 | } |
129 | |
130 | u_long |
131 | win32_htonl(u_long hostlong) |
132 | { |
133 | StartSockets(); |
134 | return htonl(hostlong); |
135 | } |
136 | |
137 | u_short |
138 | win32_htons(u_short hostshort) |
139 | { |
140 | StartSockets(); |
141 | return htons(hostshort); |
142 | } |
143 | |
144 | u_long |
145 | win32_ntohl(u_long netlong) |
146 | { |
147 | StartSockets(); |
148 | return ntohl(netlong); |
149 | } |
150 | |
151 | u_short |
152 | win32_ntohs(u_short netshort) |
153 | { |
154 | StartSockets(); |
155 | return ntohs(netshort); |
156 | } |
157 | |
158 | SOCKET |
159 | win32_socket(int af, int type, int protocol) |
160 | { |
161 | StartSockets(); |
162 | return xcesocket(af, type, protocol); |
163 | } |
164 | |
165 | SOCKET |
166 | win32_accept(SOCKET s, struct sockaddr *addr, int *addrlen) |
167 | { |
168 | StartSockets(); |
169 | return xceaccept(s, addr, addrlen); |
170 | } |
171 | |
172 | int |
173 | win32_bind(SOCKET s, const struct sockaddr *addr, int addrlen) |
174 | { |
175 | StartSockets(); |
176 | return xcebind(s, addr, addrlen); |
177 | } |
178 | |
179 | int |
180 | win32_connect(SOCKET s, const struct sockaddr *addr, int addrlen) |
181 | { |
182 | StartSockets(); |
183 | return xceconnect(s, addr, addrlen); |
184 | } |
185 | |
186 | |
187 | int |
188 | win32_getpeername(SOCKET s, struct sockaddr *addr, int *addrlen) |
189 | { |
190 | StartSockets(); |
191 | return xcegetpeername(s, addr, addrlen); |
192 | } |
193 | |
194 | int |
195 | win32_getsockname(SOCKET s, struct sockaddr *addr, int *addrlen) |
196 | { |
197 | StartSockets(); |
198 | return xcegetsockname(s, addr, addrlen); |
199 | } |
200 | |
201 | int |
202 | win32_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen) |
203 | { |
204 | StartSockets(); |
205 | return xcegetsockopt(s, level, optname, optval, optlen); |
206 | } |
207 | |
208 | int |
209 | win32_ioctlsocket(SOCKET s, long cmd, u_long *argp) |
210 | { |
211 | StartSockets(); |
212 | return xceioctlsocket(s, cmd, argp); |
213 | } |
214 | |
215 | int |
216 | win32_listen(SOCKET s, int backlog) |
217 | { |
218 | StartSockets(); |
219 | return xcelisten(s, backlog); |
220 | } |
221 | |
222 | int |
223 | win32_recv(SOCKET s, char *buf, int len, int flags) |
224 | { |
225 | StartSockets(); |
226 | return xcerecv(s, buf, len, flags); |
227 | } |
228 | |
229 | int |
230 | win32_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 | |
237 | int |
238 | win32_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 | |
247 | int |
248 | win32_send(SOCKET s, const char *buf, int len, int flags) |
249 | { |
250 | StartSockets(); |
251 | return xcesend(s, buf, len, flags); |
252 | } |
253 | |
254 | int |
255 | win32_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 | |
262 | int |
263 | win32_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 | |
270 | int |
271 | win32_shutdown(SOCKET s, int how) |
272 | { |
273 | StartSockets(); |
274 | return xceshutdown(s, how); |
275 | } |
276 | |
277 | int |
278 | win32_closesocket(SOCKET s) |
279 | { |
280 | StartSockets(); |
281 | return xceclosesocket(s); |
282 | } |
283 | |
284 | struct hostent * |
285 | win32_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 | |
293 | struct hostent * |
294 | win32_gethostbyname(const char *name) |
295 | { |
296 | struct hostent *r; |
297 | |
298 | SOCKET_TEST(r = gethostbyname(name), NULL); |
299 | return r; |
300 | } |
301 | |
302 | int |
303 | win32_gethostname(char *name, int len) |
304 | { |
305 | int r; |
306 | |
307 | SOCKET_TEST_ERROR(r = gethostname(name, len)); |
308 | return r; |
309 | } |
310 | |
311 | struct protoent * |
312 | win32_getprotobyname(const char *name) |
313 | { |
314 | struct protoent *r; |
315 | |
316 | SOCKET_TEST(r = getprotobyname(name), NULL); |
317 | return r; |
318 | } |
319 | |
320 | struct protoent * |
321 | win32_getprotobynumber(int num) |
322 | { |
323 | struct protoent *r; |
324 | |
325 | SOCKET_TEST(r = getprotobynumber(num), NULL); |
326 | return r; |
327 | } |
328 | |
329 | struct servent * |
330 | win32_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 | |
342 | struct servent * |
343 | win32_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 | |
355 | int |
356 | win32_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 | |
378 | char FAR * |
379 | win32_inet_ntoa(struct in_addr in) |
380 | { |
381 | StartSockets(); |
382 | return inet_ntoa(in); |
383 | } |
384 | |
385 | unsigned long |
386 | win32_inet_addr(const char FAR *cp) |
387 | { |
388 | StartSockets(); |
389 | return inet_addr(cp); |
390 | } |
391 | |
392 | /* |
393 | * Networking stubs |
394 | */ |
395 | |
396 | void |
397 | win32_endhostent() |
398 | { |
399 | dTHXo; |
400 | Perl_croak_nocontext("endhostent not implemented!\n"); |
401 | } |
402 | |
403 | void |
404 | win32_endnetent() |
405 | { |
406 | dTHXo; |
407 | Perl_croak_nocontext("endnetent not implemented!\n"); |
408 | } |
409 | |
410 | void |
411 | win32_endprotoent() |
412 | { |
413 | dTHXo; |
414 | Perl_croak_nocontext("endprotoent not implemented!\n"); |
415 | } |
416 | |
417 | void |
418 | win32_endservent() |
419 | { |
420 | dTHXo; |
421 | Perl_croak_nocontext("endservent not implemented!\n"); |
422 | } |
423 | |
424 | |
425 | struct netent * |
426 | win32_getnetent(void) |
427 | { |
428 | dTHXo; |
429 | Perl_croak_nocontext("getnetent not implemented!\n"); |
430 | return (struct netent *) NULL; |
431 | } |
432 | |
433 | struct netent * |
434 | win32_getnetbyname(char *name) |
435 | { |
436 | dTHXo; |
437 | Perl_croak_nocontext("getnetbyname not implemented!\n"); |
438 | return (struct netent *)NULL; |
439 | } |
440 | |
441 | struct netent * |
442 | win32_getnetbyaddr(long net, int type) |
443 | { |
444 | dTHXo; |
445 | Perl_croak_nocontext("getnetbyaddr not implemented!\n"); |
446 | return (struct netent *)NULL; |
447 | } |
448 | |
449 | struct protoent * |
450 | win32_getprotoent(void) |
451 | { |
452 | dTHXo; |
453 | Perl_croak_nocontext("getprotoent not implemented!\n"); |
454 | return (struct protoent *) NULL; |
455 | } |
456 | |
457 | struct servent * |
458 | win32_getservent(void) |
459 | { |
460 | dTHXo; |
461 | Perl_croak_nocontext("getservent not implemented!\n"); |
462 | return (struct servent *) NULL; |
463 | } |
464 | |
465 | void |
466 | win32_sethostent(int stayopen) |
467 | { |
468 | dTHXo; |
469 | Perl_croak_nocontext("sethostent not implemented!\n"); |
470 | } |
471 | |
472 | |
473 | void |
474 | win32_setnetent(int stayopen) |
475 | { |
476 | dTHXo; |
477 | Perl_croak_nocontext("setnetent not implemented!\n"); |
478 | } |
479 | |
480 | |
481 | void |
482 | win32_setprotoent(int stayopen) |
483 | { |
484 | dTHXo; |
485 | Perl_croak_nocontext("setprotoent not implemented!\n"); |
486 | } |
487 | |
488 | |
489 | void |
490 | win32_setservent(int stayopen) |
491 | { |
492 | dTHXo; |
493 | Perl_croak_nocontext("setservent not implemented!\n"); |
494 | } |
495 | |
496 | static struct servent* |
497 | win32_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 | } |