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