Merge from trunk @222673.
[official-gcc.git] / gcc / ada / socket.c
blobad88268f3698aa838ad4884de19d2500b6b67c5c
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * S O C K E T *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 2003-2014, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. *
17 * *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
21 * *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
26 * *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
29 * *
30 ****************************************************************************/
32 /* This file provides a portable binding to the sockets API */
34 /* Ensure access to errno is thread safe. */
35 #define _REENTRANT
36 #define _THREAD_SAFE
38 #include "gsocket.h"
40 #if defined(__FreeBSD__)
41 typedef unsigned int IOCTL_Req_T;
42 #else
43 typedef int IOCTL_Req_T;
44 #endif
46 #if defined(HAVE_SOCKETS)
48 /* Include all the necessary system-specific headers and define the
49 * necessary macros (shared with gen-oscons).
52 #if !defined(SO_NOSIGPIPE) && !defined (MSG_NOSIGNAL)
53 #include <signal.h>
54 #endif
55 /* Required if we will be calling signal() in __gnat_disable_all_sigpipes() */
57 #include "raise.h"
58 /* Required for __gnat_malloc() */
60 #include <string.h>
61 /* Required for memcpy() */
63 extern void __gnat_disable_sigpipe (int fd);
64 extern void __gnat_disable_all_sigpipes (void);
65 extern int __gnat_create_signalling_fds (int *fds);
66 extern int __gnat_read_signalling_fd (int rsig);
67 extern int __gnat_write_signalling_fd (int wsig);
68 extern void __gnat_close_signalling_fd (int sig);
69 extern void __gnat_last_socket_in_set (fd_set *, int *);
70 extern void __gnat_get_socket_from_set (fd_set *, int *, int *);
71 extern void __gnat_insert_socket_in_set (fd_set *, int);
72 extern int __gnat_is_socket_in_set (fd_set *, int);
73 extern fd_set *__gnat_new_socket_set (fd_set *);
74 extern void __gnat_remove_socket_from_set (fd_set *, int);
75 extern void __gnat_reset_socket_set (fd_set *);
76 extern int __gnat_get_h_errno (void);
77 extern int __gnat_socket_ioctl (int, IOCTL_Req_T, int *);
79 extern char * __gnat_servent_s_name (struct servent *);
80 extern char * __gnat_servent_s_alias (struct servent *, int index);
81 extern unsigned short __gnat_servent_s_port (struct servent *);
82 extern char * __gnat_servent_s_proto (struct servent *);
84 extern char * __gnat_hostent_h_name (struct hostent *);
85 extern char * __gnat_hostent_h_alias (struct hostent *, int);
86 extern int __gnat_hostent_h_addrtype (struct hostent *);
87 extern int __gnat_hostent_h_length (struct hostent *);
88 extern char * __gnat_hostent_h_addr (struct hostent *, int);
90 #ifndef HAVE_INET_PTON
91 extern int __gnat_inet_pton (int, const char *, void *);
92 #endif
94 /* Disable the sending of SIGPIPE for writes on a broken stream */
96 void
97 __gnat_disable_sigpipe (int fd)
99 #ifdef SO_NOSIGPIPE
100 int val = 1;
101 (void) setsockopt (fd, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof val);
102 #endif
105 void
106 __gnat_disable_all_sigpipes (void)
108 #if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) && defined(SIGPIPE)
109 (void) signal (SIGPIPE, SIG_IGN);
110 #endif
113 #if defined (_WIN32) || defined (__vxworks)
115 * Signalling FDs operations are implemented in Ada for these platforms
116 * (see subunit GNAT.Sockets.Thin.Signalling_Fds).
118 #else
120 * Create a pair of connected file descriptors fds[0] and fds[1] used for
121 * signalling by a Selector object. fds[0] is the read end, and fds[1] the
122 * write end.
125 __gnat_create_signalling_fds (int *fds) {
126 return pipe (fds);
130 * Read one byte of data from rsig, the read end of a pair of signalling fds
131 * created by __gnat_create_signalling_fds.
134 __gnat_read_signalling_fd (int rsig) {
135 char c;
136 return read (rsig, &c, 1);
140 * Write one byte of data to wsig, the write end of a pair of signalling fds
141 * created by __gnat_create_signalling_fds.
144 __gnat_write_signalling_fd (int wsig) {
145 char c = 0;
146 return write (wsig, &c, 1);
150 * Close one end of a pair of signalling fds
152 void
153 __gnat_close_signalling_fd (int sig) {
154 (void) close (sig);
156 #endif
159 * Handling of gethostbyname, gethostbyaddr, getservbyname and getservbyport
160 * =========================================================================
162 * This module exposes __gnat_getXXXbyYYY operations with the same signature
163 * as the reentrant variant getXXXbyYYY_r.
165 * On platforms where getXXXbyYYY is intrinsically reentrant, the provided user
166 * buffer argument is ignored.
168 * When getXXXbyYYY is not reentrant but getXXXbyYYY_r exists, the latter is
169 * used, and the provided buffer argument must point to a valid, thread-local
170 * buffer (usually on the caller's stack).
172 * When getXXXbyYYY is not reentrant and no reentrant getXXXbyYYY_r variant
173 * is available, the non-reentrant getXXXbyYYY is called, the provided user
174 * buffer is ignored, and the caller is expected to take care of mutual
175 * exclusion.
178 #ifdef HAVE_GETxxxBYyyy_R
180 __gnat_gethostbyname (const char *name,
181 struct hostent *ret, char *buf, size_t buflen,
182 int *h_errnop)
184 struct hostent *rh;
185 int ri;
187 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
188 (void) gethostbyname_r (name, ret, buf, buflen, &rh, h_errnop);
189 #else
190 rh = gethostbyname_r (name, ret, buf, buflen, h_errnop);
191 #endif
192 ri = (rh == NULL) ? -1 : 0;
193 return ri;
197 __gnat_gethostbyaddr (const char *addr, int len, int type,
198 struct hostent *ret, char *buf, size_t buflen,
199 int *h_errnop)
201 struct hostent *rh;
202 int ri;
204 #if defined(__linux__) || defined(__GLIBC__)
205 (void) gethostbyaddr_r (addr, len, type, ret, buf, buflen, &rh, h_errnop);
206 #else
207 rh = gethostbyaddr_r (addr, len, type, ret, buf, buflen, h_errnop);
208 #endif
209 ri = (rh == NULL) ? -1 : 0;
210 return ri;
214 __gnat_getservbyname (const char *name, const char *proto,
215 struct servent *ret, char *buf, size_t buflen)
217 struct servent *rh;
218 int ri;
220 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
221 (void) getservbyname_r (name, proto, ret, buf, buflen, &rh);
222 #else
223 rh = getservbyname_r (name, proto, ret, buf, buflen);
224 #endif
225 ri = (rh == NULL) ? -1 : 0;
226 return ri;
230 __gnat_getservbyport (int port, const char *proto,
231 struct servent *ret, char *buf, size_t buflen)
233 struct servent *rh;
234 int ri;
236 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
237 (void) getservbyport_r (port, proto, ret, buf, buflen, &rh);
238 #else
239 rh = getservbyport_r (port, proto, ret, buf, buflen);
240 #endif
241 ri = (rh == NULL) ? -1 : 0;
242 return ri;
244 #elif defined (__vxworks)
245 static char vxw_h_name[MAXHOSTNAMELEN + 1];
246 static char *vxw_h_aliases[1] = { NULL };
247 static int vxw_h_addr;
248 static char *vxw_h_addr_list[2] = { (char*) &vxw_h_addr, NULL };
251 __gnat_gethostbyname (const char *name,
252 struct hostent *ret, char *buf, size_t buflen,
253 int *h_errnop)
255 vxw_h_addr = hostGetByName (name);
256 if (vxw_h_addr == ERROR) {
257 *h_errnop = __gnat_get_h_errno ();
258 return -1;
260 ret->h_name = name;
261 ret->h_aliases = &vxw_h_aliases;
262 ret->h_addrtype = AF_INET;
263 ret->h_length = 4;
264 ret->h_addr_list = &vxw_h_addr_list;
265 return 0;
269 __gnat_gethostbyaddr (const char *addr, int len, int type,
270 struct hostent *ret, char *buf, size_t buflen,
271 int *h_errnop)
273 if (type != AF_INET) {
274 *h_errnop = EAFNOSUPPORT;
275 return -1;
278 if (addr == NULL || len != 4) {
279 *h_errnop = EINVAL;
280 return -1;
283 if (hostGetByAddr (*(int*)addr, &vxw_h_name) != OK) {
284 *h_errnop = __gnat_get_h_errno ();
285 return -1;
288 vxw_h_addr = addr;
290 ret->h_name = &vxw_h_name;
291 ret->h_aliases = &vxw_h_aliases;
292 ret->h_addrtype = AF_INET;
293 ret->h_length = 4;
294 ret->h_addr_list = &vxw_h_addr_list;
298 __gnat_getservbyname (const char *name, const char *proto,
299 struct servent *ret, char *buf, size_t buflen)
301 /* Not available under VxWorks */
302 return -1;
306 __gnat_getservbyport (int port, const char *proto,
307 struct servent *ret, char *buf, size_t buflen)
309 /* Not available under VxWorks */
310 return -1;
312 #else
314 __gnat_gethostbyname (const char *name,
315 struct hostent *ret, char *buf, size_t buflen,
316 int *h_errnop)
318 struct hostent *rh;
319 rh = gethostbyname (name);
320 if (rh == NULL) {
321 *h_errnop = __gnat_get_h_errno ();
322 return -1;
324 *ret = *rh;
325 *h_errnop = 0;
326 return 0;
330 __gnat_gethostbyaddr (const char *addr, int len, int type,
331 struct hostent *ret, char *buf, size_t buflen,
332 int *h_errnop)
334 struct hostent *rh;
335 rh = gethostbyaddr (addr, len, type);
336 if (rh == NULL) {
337 *h_errnop = __gnat_get_h_errno ();
338 return -1;
340 *ret = *rh;
341 *h_errnop = 0;
342 return 0;
346 __gnat_getservbyname (const char *name, const char *proto,
347 struct servent *ret, char *buf, size_t buflen)
349 struct servent *rh;
350 rh = getservbyname (name, proto);
351 if (rh == NULL)
352 return -1;
353 *ret = *rh;
354 return 0;
358 __gnat_getservbyport (int port, const char *proto,
359 struct servent *ret, char *buf, size_t buflen)
361 struct servent *rh;
362 rh = getservbyport (port, proto);
363 if (rh == NULL)
364 return -1;
365 *ret = *rh;
366 return 0;
368 #endif
370 /* Find the largest socket in the socket set SET. This is needed for
371 `select'. LAST is the maximum value for the largest socket. This hint is
372 used to avoid scanning very large socket sets. On return, LAST is the
373 actual largest socket in the socket set. */
375 void
376 __gnat_last_socket_in_set (fd_set *set, int *last)
378 int s;
379 int l;
380 l = -1;
382 #ifdef _WIN32
383 /* More efficient method for NT. */
384 for (s = 0; s < set->fd_count; s++)
385 if ((int) set->fd_array[s] > l)
386 l = set->fd_array[s];
388 #else
390 for (s = *last; s != -1; s--)
391 if (FD_ISSET (s, set))
393 l = s;
394 break;
396 #endif
398 *last = l;
401 /* Get last socket and remove it from the socket set SET. LAST is the
402 maximum value of the largest socket. This hint is used to avoid scanning
403 very large socket sets. On return, LAST is set to the actual largest
404 socket in the socket set. */
406 void
407 __gnat_get_socket_from_set (fd_set *set, int *last, int *socket)
409 *socket = *last;
410 FD_CLR (*socket, set);
411 __gnat_last_socket_in_set (set, last);
414 /* Insert SOCKET in the socket set SET. */
416 void
417 __gnat_insert_socket_in_set (fd_set *set, int socket)
419 FD_SET (socket, set);
422 /* Check whether a given SOCKET is in the socket set SET. */
425 __gnat_is_socket_in_set (fd_set *set, int socket)
427 return FD_ISSET (socket, set);
430 /* Remove SOCKET from the socket set SET. */
432 void
433 __gnat_remove_socket_from_set (fd_set *set, int socket)
435 FD_CLR (socket, set);
438 /* Reset SET */
439 void
440 __gnat_reset_socket_set (fd_set *set)
442 FD_ZERO (set);
445 /* Get the value of the last host error */
448 __gnat_get_h_errno (void) {
449 #ifdef __vxworks
450 int vxw_errno = errno;
452 switch (vxw_errno) {
453 case 0:
454 return 0;
456 #ifdef S_hostLib_HOST_NOT_FOUND
457 case S_hostLib_HOST_NOT_FOUND:
458 #endif
459 case S_hostLib_UNKNOWN_HOST:
460 return HOST_NOT_FOUND;
462 #ifdef S_hostLib_TRY_AGAIN
463 case S_hostLib_TRY_AGAIN:
464 return TRY_AGAIN;
465 #endif
467 #ifdef S_hostLib_NO_RECOVERY
468 case S_hostLib_NO_RECOVERY:
469 #endif
470 #ifdef S_hostLib_NETDB_INTERNAL
471 case S_hostLib_NETDB_INTERNAL:
472 #endif
473 case S_hostLib_INVALID_PARAMETER:
474 return NO_RECOVERY;
476 default:
477 return -1;
480 #elif defined (__rtems__)
481 /* At this stage in the tool build, no networking .h files are available.
482 * Newlib does not provide networking .h files and RTEMS is not built yet.
483 * So we need to explicitly extern h_errno to access it.
485 extern int h_errno;
486 return h_errno;
488 #else
489 return h_errno;
490 #endif
493 /* Wrapper for ioctl(2), which is a variadic function */
496 __gnat_socket_ioctl (int fd, IOCTL_Req_T req, int *arg) {
497 #if defined (_WIN32)
498 return ioctlsocket (fd, req, arg);
499 #elif defined (__APPLE__)
501 * On Darwin, req is an unsigned long, and we want to convert without sign
502 * extension to get the proper bit pattern in the case of a 64 bit kernel.
504 return ioctl (fd, (unsigned int) req, arg);
505 #else
506 return ioctl (fd, req, arg);
507 #endif
510 #ifndef HAVE_INET_PTON
513 __gnat_inet_pton (int af, const char *src, void *dst) {
514 switch (af) {
515 #if defined (_WIN32) && defined (AF_INET6)
516 case AF_INET6:
517 #endif
518 case AF_INET:
519 break;
520 default:
521 errno = EAFNOSUPPORT;
522 return -1;
525 #if defined (__vxworks)
526 return (inet_aton (src, dst) == OK);
528 #elif defined (_WIN32)
529 struct sockaddr_storage ss;
530 int sslen = sizeof ss;
531 int rc;
533 ss.ss_family = af;
534 rc = WSAStringToAddressA (src, af, NULL, (struct sockaddr *)&ss, &sslen);
535 if (rc == 0) {
536 switch (af) {
537 case AF_INET:
538 *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
539 break;
540 #ifdef AF_INET6
541 case AF_INET6:
542 *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
543 break;
544 #endif
547 return (rc == 0);
549 #elif defined (__hpux__)
550 in_addr_t addr;
551 int rc = -1;
553 if (src == NULL || dst == NULL) {
554 errno = EINVAL;
556 } else if (!strcmp (src, "255.255.255.255")) {
557 addr = 0xffffffff;
558 rc = 1;
560 } else {
561 addr = inet_addr (src);
562 rc = (addr != 0xffffffff);
564 if (rc == 1) {
565 *(in_addr_t *)dst = addr;
567 return rc;
568 #endif
570 #endif
573 * Accessor functions for struct hostent.
576 char * __gnat_hostent_h_name (struct hostent * h) {
577 return h->h_name;
580 char * __gnat_hostent_h_alias (struct hostent * h, int index) {
581 return h->h_aliases[index];
584 int __gnat_hostent_h_addrtype (struct hostent * h) {
585 return h->h_addrtype;
588 int __gnat_hostent_h_length (struct hostent * h) {
589 return h->h_length;
592 char * __gnat_hostent_h_addr (struct hostent * h, int index) {
593 return h->h_addr_list[index];
597 * Accessor functions for struct servent.
599 * These are needed because servent has different representations on different
600 * platforms, and we don't want to deal with that on the Ada side. For example,
601 * on Linux, we have (see /usr/include netdb.h):
603 * struct servent
605 * char *s_name;
606 * char **s_aliases;
607 * int s_port;
608 * char *s_proto;
609 * };
611 * and on Windows (see mingw's socket.h):
613 * struct servent {
614 * char *s_name;
615 * char **s_aliases;
616 * #ifdef _WIN64
617 * char *s_proto;
618 * short s_port;
619 * #else
620 * short s_port;
621 * char *s_proto;
622 * #endif
623 * };
626 char *
627 __gnat_servent_s_name (struct servent * s)
629 return s->s_name;
632 char *
633 __gnat_servent_s_alias (struct servent * s, int index)
635 return s->s_aliases[index];
638 unsigned short
639 __gnat_servent_s_port (struct servent * s)
641 return s->s_port;
644 char *
645 __gnat_servent_s_proto (struct servent * s)
647 return s->s_proto;
650 #endif /* defined(HAVE_SOCKETS) */