PR tree-optimization/82929
[official-gcc.git] / gcc / ada / socket.c
blob5cdd656e34b340fa2d8a97107f67504bd74d6529
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * S O C K E T *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 2003-2015, 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__) || defined (__DragonFly__) \
41 || defined (__NetBSD__) || defined (__OpenBSD__)
42 typedef unsigned int IOCTL_Req_T;
43 #else
44 typedef int IOCTL_Req_T;
45 #endif
47 #if defined(HAVE_SOCKETS)
49 /* Include all the necessary system-specific headers and define the
50 * necessary macros (shared with gen-oscons).
53 #if !defined(SO_NOSIGPIPE) && !defined (MSG_NOSIGNAL)
54 #include <signal.h>
55 #endif
56 /* Required if we will be calling signal() in __gnat_disable_all_sigpipes() */
58 #include "raise.h"
59 /* Required for __gnat_malloc() */
61 #include <string.h>
62 /* Required for memcpy() */
64 extern void __gnat_disable_sigpipe (int fd);
65 extern void __gnat_disable_all_sigpipes (void);
66 extern int __gnat_create_signalling_fds (int *fds);
67 extern int __gnat_read_signalling_fd (int rsig);
68 extern int __gnat_write_signalling_fd (int wsig);
69 extern void __gnat_close_signalling_fd (int sig);
70 extern void __gnat_last_socket_in_set (fd_set *, int *);
71 extern void __gnat_get_socket_from_set (fd_set *, int *, int *);
72 extern void __gnat_insert_socket_in_set (fd_set *, int);
73 extern int __gnat_is_socket_in_set (fd_set *, int);
74 extern fd_set *__gnat_new_socket_set (fd_set *);
75 extern void __gnat_remove_socket_from_set (fd_set *, int);
76 extern void __gnat_reset_socket_set (fd_set *);
77 extern int __gnat_get_h_errno (void);
78 extern int __gnat_socket_ioctl (int, IOCTL_Req_T, int *);
80 extern char * __gnat_servent_s_name (struct servent *);
81 extern char * __gnat_servent_s_alias (struct servent *, int index);
82 extern unsigned short __gnat_servent_s_port (struct servent *);
83 extern char * __gnat_servent_s_proto (struct servent *);
85 extern char * __gnat_hostent_h_name (struct hostent *);
86 extern char * __gnat_hostent_h_alias (struct hostent *, int);
87 extern int __gnat_hostent_h_addrtype (struct hostent *);
88 extern int __gnat_hostent_h_length (struct hostent *);
89 extern char * __gnat_hostent_h_addr (struct hostent *, int);
91 #ifndef HAVE_INET_PTON
92 extern int __gnat_inet_pton (int, const char *, void *);
93 #endif
95 /* Disable the sending of SIGPIPE for writes on a broken stream */
97 void
98 __gnat_disable_sigpipe (int fd)
100 #ifdef SO_NOSIGPIPE
101 int val = 1;
102 (void) setsockopt (fd, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof val);
103 #endif
106 void
107 __gnat_disable_all_sigpipes (void)
109 #if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) && defined(SIGPIPE)
110 (void) signal (SIGPIPE, SIG_IGN);
111 #endif
114 #if defined (_WIN32) || defined (__vxworks)
116 * Signalling FDs operations are implemented in Ada for these platforms
117 * (see subunit GNAT.Sockets.Thin.Signalling_Fds).
119 #else
121 * Create a pair of connected file descriptors fds[0] and fds[1] used for
122 * signalling by a Selector object. fds[0] is the read end, and fds[1] the
123 * write end.
126 __gnat_create_signalling_fds (int *fds) {
127 return pipe (fds);
131 * Read one byte of data from rsig, the read end of a pair of signalling fds
132 * created by __gnat_create_signalling_fds.
135 __gnat_read_signalling_fd (int rsig) {
136 char c;
137 return read (rsig, &c, 1);
141 * Write one byte of data to wsig, the write end of a pair of signalling fds
142 * created by __gnat_create_signalling_fds.
145 __gnat_write_signalling_fd (int wsig) {
146 char c = 0;
147 return write (wsig, &c, 1);
151 * Close one end of a pair of signalling fds
153 void
154 __gnat_close_signalling_fd (int sig) {
155 (void) close (sig);
157 #endif
160 * Handling of gethostbyname, gethostbyaddr, getservbyname and getservbyport
161 * =========================================================================
163 * This module exposes __gnat_getXXXbyYYY operations with the same signature
164 * as the reentrant variant getXXXbyYYY_r.
166 * On platforms where getXXXbyYYY is intrinsically reentrant, the provided user
167 * buffer argument is ignored.
169 * When getXXXbyYYY is not reentrant but getXXXbyYYY_r exists, the latter is
170 * used, and the provided buffer argument must point to a valid, thread-local
171 * buffer (usually on the caller's stack).
173 * When getXXXbyYYY is not reentrant and no reentrant getXXXbyYYY_r variant
174 * is available, the non-reentrant getXXXbyYYY is called, the provided user
175 * buffer is ignored, and the caller is expected to take care of mutual
176 * exclusion.
179 #ifdef HAVE_GETxxxBYyyy_R
181 __gnat_gethostbyname (const char *name,
182 struct hostent *ret, char *buf, size_t buflen,
183 int *h_errnop)
185 struct hostent *rh;
186 int ri;
188 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
189 (void) gethostbyname_r (name, ret, buf, buflen, &rh, h_errnop);
190 #else
191 rh = gethostbyname_r (name, ret, buf, buflen, h_errnop);
192 #endif
193 ri = (rh == NULL) ? -1 : 0;
194 return ri;
198 __gnat_gethostbyaddr (const char *addr, int len, int type,
199 struct hostent *ret, char *buf, size_t buflen,
200 int *h_errnop)
202 struct hostent *rh;
203 int ri;
205 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
206 (void) gethostbyaddr_r (addr, len, type, ret, buf, buflen, &rh, h_errnop);
207 #else
208 rh = gethostbyaddr_r (addr, len, type, ret, buf, buflen, h_errnop);
209 #endif
210 ri = (rh == NULL) ? -1 : 0;
211 return ri;
215 __gnat_getservbyname (const char *name, const char *proto,
216 struct servent *ret, char *buf, size_t buflen)
218 struct servent *rh;
219 int ri;
221 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
222 (void) getservbyname_r (name, proto, ret, buf, buflen, &rh);
223 #else
224 rh = getservbyname_r (name, proto, ret, buf, buflen);
225 #endif
226 ri = (rh == NULL) ? -1 : 0;
227 return ri;
231 __gnat_getservbyport (int port, const char *proto,
232 struct servent *ret, char *buf, size_t buflen)
234 struct servent *rh;
235 int ri;
237 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
238 (void) getservbyport_r (port, proto, ret, buf, buflen, &rh);
239 #else
240 rh = getservbyport_r (port, proto, ret, buf, buflen);
241 #endif
242 ri = (rh == NULL) ? -1 : 0;
243 return ri;
245 #elif defined (__vxworks)
246 static char vxw_h_name[MAXHOSTNAMELEN + 1];
247 static char *vxw_h_aliases[1] = { NULL };
248 static int vxw_h_addr;
249 static char *vxw_h_addr_list[2] = { (char*) &vxw_h_addr, NULL };
252 __gnat_gethostbyname (const char *name,
253 struct hostent *ret, char *buf, size_t buflen,
254 int *h_errnop)
256 vxw_h_addr = hostGetByName (name);
257 if (vxw_h_addr == ERROR) {
258 *h_errnop = __gnat_get_h_errno ();
259 return -1;
261 ret->h_name = name;
262 ret->h_aliases = &vxw_h_aliases;
263 ret->h_addrtype = AF_INET;
264 ret->h_length = 4;
265 ret->h_addr_list = &vxw_h_addr_list;
266 return 0;
270 __gnat_gethostbyaddr (const char *addr, int len, int type,
271 struct hostent *ret, char *buf, size_t buflen,
272 int *h_errnop)
274 if (type != AF_INET) {
275 *h_errnop = EAFNOSUPPORT;
276 return -1;
279 if (addr == NULL || len != 4) {
280 *h_errnop = EINVAL;
281 return -1;
284 if (hostGetByAddr (*(int*)addr, &vxw_h_name) != OK) {
285 *h_errnop = __gnat_get_h_errno ();
286 return -1;
289 vxw_h_addr = addr;
291 ret->h_name = &vxw_h_name;
292 ret->h_aliases = &vxw_h_aliases;
293 ret->h_addrtype = AF_INET;
294 ret->h_length = 4;
295 ret->h_addr_list = &vxw_h_addr_list;
299 __gnat_getservbyname (const char *name, const char *proto,
300 struct servent *ret, char *buf, size_t buflen)
302 /* Not available under VxWorks */
303 return -1;
307 __gnat_getservbyport (int port, const char *proto,
308 struct servent *ret, char *buf, size_t buflen)
310 /* Not available under VxWorks */
311 return -1;
313 #else
315 __gnat_gethostbyname (const char *name,
316 struct hostent *ret, char *buf, size_t buflen,
317 int *h_errnop)
319 struct hostent *rh;
320 rh = gethostbyname (name);
321 if (rh == NULL) {
322 *h_errnop = __gnat_get_h_errno ();
323 return -1;
325 *ret = *rh;
326 *h_errnop = 0;
327 return 0;
331 __gnat_gethostbyaddr (const char *addr, int len, int type,
332 struct hostent *ret, char *buf, size_t buflen,
333 int *h_errnop)
335 struct hostent *rh;
336 rh = gethostbyaddr (addr, len, type);
337 if (rh == NULL) {
338 *h_errnop = __gnat_get_h_errno ();
339 return -1;
341 *ret = *rh;
342 *h_errnop = 0;
343 return 0;
347 __gnat_getservbyname (const char *name, const char *proto,
348 struct servent *ret, char *buf, size_t buflen)
350 struct servent *rh;
351 rh = getservbyname (name, proto);
352 if (rh == NULL)
353 return -1;
354 *ret = *rh;
355 return 0;
359 __gnat_getservbyport (int port, const char *proto,
360 struct servent *ret, char *buf, size_t buflen)
362 struct servent *rh;
363 rh = getservbyport (port, proto);
364 if (rh == NULL)
365 return -1;
366 *ret = *rh;
367 return 0;
369 #endif
371 /* Find the largest socket in the socket set SET. This is needed for
372 `select'. LAST is the maximum value for the largest socket. This hint is
373 used to avoid scanning very large socket sets. On return, LAST is the
374 actual largest socket in the socket set. */
376 void
377 __gnat_last_socket_in_set (fd_set *set, int *last)
379 int s;
380 int l;
381 l = -1;
383 #ifdef _WIN32
384 /* More efficient method for NT. */
385 for (s = 0; s < set->fd_count; s++)
386 if ((int) set->fd_array[s] > l)
387 l = set->fd_array[s];
389 #else
391 for (s = *last; s != -1; s--)
392 if (FD_ISSET (s, set))
394 l = s;
395 break;
397 #endif
399 *last = l;
402 /* Get last socket and remove it from the socket set SET. LAST is the
403 maximum value of the largest socket. This hint is used to avoid scanning
404 very large socket sets. On return, LAST is set to the actual largest
405 socket in the socket set. */
407 void
408 __gnat_get_socket_from_set (fd_set *set, int *last, int *socket)
410 *socket = *last;
411 FD_CLR (*socket, set);
412 __gnat_last_socket_in_set (set, last);
415 /* Insert SOCKET in the socket set SET. */
417 void
418 __gnat_insert_socket_in_set (fd_set *set, int socket)
420 FD_SET (socket, set);
423 /* Check whether a given SOCKET is in the socket set SET. */
426 __gnat_is_socket_in_set (fd_set *set, int socket)
428 return FD_ISSET (socket, set);
431 /* Remove SOCKET from the socket set SET. */
433 void
434 __gnat_remove_socket_from_set (fd_set *set, int socket)
436 FD_CLR (socket, set);
439 /* Reset SET */
440 void
441 __gnat_reset_socket_set (fd_set *set)
443 FD_ZERO (set);
446 /* Get the value of the last host error */
449 __gnat_get_h_errno (void) {
450 #ifdef __vxworks
451 int vxw_errno = errno;
453 switch (vxw_errno) {
454 case 0:
455 return 0;
457 #ifdef S_hostLib_HOST_NOT_FOUND
458 case S_hostLib_HOST_NOT_FOUND:
459 #endif
460 case S_hostLib_UNKNOWN_HOST:
461 return HOST_NOT_FOUND;
463 #ifdef S_hostLib_TRY_AGAIN
464 case S_hostLib_TRY_AGAIN:
465 return TRY_AGAIN;
466 #endif
468 #ifdef S_hostLib_NO_RECOVERY
469 case S_hostLib_NO_RECOVERY:
470 #endif
471 #ifdef S_hostLib_NETDB_INTERNAL
472 case S_hostLib_NETDB_INTERNAL:
473 #endif
474 case S_hostLib_INVALID_PARAMETER:
475 return NO_RECOVERY;
477 default:
478 return -1;
481 #elif defined (__rtems__)
482 /* At this stage in the tool build, no networking .h files are available.
483 * Newlib does not provide networking .h files and RTEMS is not built yet.
484 * So we need to explicitly extern h_errno to access it.
486 extern int h_errno;
487 return h_errno;
489 #else
490 return h_errno;
491 #endif
494 /* Wrapper for ioctl(2), which is a variadic function */
497 __gnat_socket_ioctl (int fd, IOCTL_Req_T req, int *arg) {
498 #if defined (_WIN32)
499 return ioctlsocket (fd, req, arg);
500 #elif defined (__APPLE__)
502 * On Darwin, req is an unsigned long, and we want to convert without sign
503 * extension to get the proper bit pattern in the case of a 64 bit kernel.
505 return ioctl (fd, (unsigned int) req, arg);
506 #else
507 return ioctl (fd, req, arg);
508 #endif
511 #ifndef HAVE_INET_PTON
514 __gnat_inet_pton (int af, const char *src, void *dst) {
515 switch (af) {
516 #if defined (_WIN32) && defined (AF_INET6)
517 case AF_INET6:
518 #endif
519 case AF_INET:
520 break;
521 default:
522 errno = EAFNOSUPPORT;
523 return -1;
526 #if defined (__vxworks)
527 return (inet_aton (src, dst) == OK);
529 #elif defined (_WIN32)
530 struct sockaddr_storage ss;
531 int sslen = sizeof ss;
532 int rc;
534 ss.ss_family = af;
535 rc = WSAStringToAddressA (src, af, NULL, (struct sockaddr *)&ss, &sslen);
536 if (rc == 0) {
537 switch (af) {
538 case AF_INET:
539 *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
540 break;
541 #ifdef AF_INET6
542 case AF_INET6:
543 *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
544 break;
545 #endif
548 return (rc == 0);
550 #elif defined (__hpux__)
551 in_addr_t addr;
552 int rc = -1;
554 if (src == NULL || dst == NULL) {
555 errno = EINVAL;
557 } else if (!strcmp (src, "255.255.255.255")) {
558 addr = 0xffffffff;
559 rc = 1;
561 } else {
562 addr = inet_addr (src);
563 rc = (addr != 0xffffffff);
565 if (rc == 1) {
566 *(in_addr_t *)dst = addr;
568 return rc;
569 #endif
571 #endif
574 * Accessor functions for struct hostent.
577 char * __gnat_hostent_h_name (struct hostent * h) {
578 return h->h_name;
581 char * __gnat_hostent_h_alias (struct hostent * h, int index) {
582 return h->h_aliases[index];
585 int __gnat_hostent_h_addrtype (struct hostent * h) {
586 return h->h_addrtype;
589 int __gnat_hostent_h_length (struct hostent * h) {
590 return h->h_length;
593 char * __gnat_hostent_h_addr (struct hostent * h, int index) {
594 return h->h_addr_list[index];
598 * Accessor functions for struct servent.
600 * These are needed because servent has different representations on different
601 * platforms, and we don't want to deal with that on the Ada side. For example,
602 * on Linux, we have (see /usr/include netdb.h):
604 * struct servent
606 * char *s_name;
607 * char **s_aliases;
608 * int s_port;
609 * char *s_proto;
610 * };
612 * and on Windows (see mingw's socket.h):
614 * struct servent {
615 * char *s_name;
616 * char **s_aliases;
617 * #ifdef _WIN64
618 * char *s_proto;
619 * short s_port;
620 * #else
621 * short s_port;
622 * char *s_proto;
623 * #endif
624 * };
627 char *
628 __gnat_servent_s_name (struct servent * s)
630 return s->s_name;
633 char *
634 __gnat_servent_s_alias (struct servent * s, int index)
636 return s->s_aliases[index];
639 unsigned short
640 __gnat_servent_s_port (struct servent * s)
642 return s->s_port;
645 char *
646 __gnat_servent_s_proto (struct servent * s)
648 return s->s_proto;
651 #endif /* defined(HAVE_SOCKETS) */