1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2003-2020, Free Software Foundation, Inc. *
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. *
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. *
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/>. *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 ****************************************************************************/
32 /* This file provides a portable binding to the sockets API */
34 #define ATTRIBUTE_UNUSED __attribute__((unused))
36 /* Ensure access to errno is thread safe. */
44 #if defined (__FreeBSD__) || defined (__DragonFly__) \
45 || defined (__NetBSD__) || defined (__OpenBSD__)
46 typedef unsigned int IOCTL_Req_T
;
48 typedef int IOCTL_Req_T
;
51 #if defined(HAVE_SOCKETS)
53 /* Include all the necessary system-specific headers and define the
54 * necessary macros (shared with gen-oscons).
57 #if !defined(SO_NOSIGPIPE) && !defined (MSG_NOSIGNAL)
60 /* Required if we will be calling signal() in __gnat_disable_all_sigpipes() */
63 /* Required for __gnat_malloc() */
66 /* Required for memcpy() */
68 extern void __gnat_disable_sigpipe (int fd
);
69 extern void __gnat_disable_all_sigpipes (void);
70 extern int __gnat_create_signalling_fds (int *fds
);
71 extern int __gnat_read_signalling_fd (int rsig
);
72 extern int __gnat_write_signalling_fd (int wsig
);
73 extern void __gnat_close_signalling_fd (int sig
);
74 extern void __gnat_last_socket_in_set (fd_set
*, int *);
75 extern void __gnat_get_socket_from_set (fd_set
*, int *, int *);
76 extern void __gnat_insert_socket_in_set (fd_set
*, int);
77 extern int __gnat_is_socket_in_set (fd_set
*, int);
78 extern fd_set
*__gnat_new_socket_set (fd_set
*);
79 extern void __gnat_remove_socket_from_set (fd_set
*, int);
80 extern void __gnat_reset_socket_set (fd_set
*);
81 extern int __gnat_get_h_errno (void);
82 extern int __gnat_socket_ioctl (int, IOCTL_Req_T
, int *);
84 extern char * __gnat_servent_s_name (struct servent
*);
85 extern char * __gnat_servent_s_alias (struct servent
*, int index
);
86 extern unsigned short __gnat_servent_s_port (struct servent
*);
87 extern char * __gnat_servent_s_proto (struct servent
*);
89 extern char * __gnat_hostent_h_name (struct hostent
*);
90 extern char * __gnat_hostent_h_alias (struct hostent
*, int);
91 extern int __gnat_hostent_h_addrtype (struct hostent
*);
92 extern int __gnat_hostent_h_length (struct hostent
*);
93 extern char * __gnat_hostent_h_addr (struct hostent
*, int);
95 extern int __gnat_getaddrinfo(
98 const struct addrinfo
*hints
,
99 struct addrinfo
**res
);
100 int __gnat_getnameinfo(
101 const struct sockaddr
*sa
, socklen_t salen
,
102 char *host
, size_t hostlen
,
103 char *serv
, size_t servlen
, int flags
);
104 extern void __gnat_freeaddrinfo(struct addrinfo
*res
);
105 extern const char * __gnat_gai_strerror(int errcode
);
107 #ifndef HAVE_INET_PTON
108 extern int __gnat_inet_pton (int, const char *, void *);
111 #ifndef HAVE_INET_NTOP
113 __gnat_inet_ntop(int, const void *, char *, socklen_t
);
116 /* Disable the sending of SIGPIPE for writes on a broken stream */
119 __gnat_disable_sigpipe (int fd ATTRIBUTE_UNUSED
)
123 (void) setsockopt (fd
, SOL_SOCKET
, SO_NOSIGPIPE
, &val
, sizeof val
);
128 __gnat_disable_all_sigpipes (void)
130 #if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) && defined(SIGPIPE)
131 (void) signal (SIGPIPE
, SIG_IGN
);
135 #if defined (_WIN32) || defined (__vxworks)
137 * Signalling FDs operations are implemented in Ada for these platforms
138 * (see subunit GNAT.Sockets.Thin.Signalling_Fds).
142 * Create a pair of connected file descriptors fds[0] and fds[1] used for
143 * signalling by a Selector object. fds[0] is the read end, and fds[1] the
147 __gnat_create_signalling_fds (int *fds
) {
152 * Read one byte of data from rsig, the read end of a pair of signalling fds
153 * created by __gnat_create_signalling_fds.
156 __gnat_read_signalling_fd (int rsig
) {
158 return read (rsig
, &c
, 1);
162 * Write one byte of data to wsig, the write end of a pair of signalling fds
163 * created by __gnat_create_signalling_fds.
166 __gnat_write_signalling_fd (int wsig
) {
168 return write (wsig
, &c
, 1);
172 * Close one end of a pair of signalling fds
175 __gnat_close_signalling_fd (int sig
) {
181 * Handling of gethostbyname, gethostbyaddr, getservbyname and getservbyport
182 * =========================================================================
184 * This module exposes __gnat_getXXXbyYYY operations with the same signature
185 * as the reentrant variant getXXXbyYYY_r.
187 * On platforms where getXXXbyYYY is intrinsically reentrant, the provided user
188 * buffer argument is ignored.
190 * When getXXXbyYYY is not reentrant but getXXXbyYYY_r exists, the latter is
191 * used, and the provided buffer argument must point to a valid, thread-local
192 * buffer (usually on the caller's stack).
194 * When getXXXbyYYY is not reentrant and no reentrant getXXXbyYYY_r variant
195 * is available, the non-reentrant getXXXbyYYY is called, the provided user
196 * buffer is ignored, and the caller is expected to take care of mutual
200 #ifdef HAVE_GETxxxBYyyy_R
202 __gnat_gethostbyname (const char *name
,
203 struct hostent
*ret
, char *buf
, size_t buflen
,
209 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
210 (void) gethostbyname_r (name
, ret
, buf
, buflen
, &rh
, h_errnop
);
212 rh
= gethostbyname_r (name
, ret
, buf
, buflen
, h_errnop
);
214 ri
= (rh
== NULL
) ? -1 : 0;
219 __gnat_gethostbyaddr (const char *addr
, int len
, int type
,
220 struct hostent
*ret
, char *buf
, size_t buflen
,
226 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
227 (void) gethostbyaddr_r (addr
, len
, type
, ret
, buf
, buflen
, &rh
, h_errnop
);
229 rh
= gethostbyaddr_r (addr
, len
, type
, ret
, buf
, buflen
, h_errnop
);
231 ri
= (rh
== NULL
) ? -1 : 0;
236 __gnat_getservbyname (const char *name
, const char *proto
,
237 struct servent
*ret
, char *buf
, size_t buflen
)
242 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
243 (void) getservbyname_r (name
, proto
, ret
, buf
, buflen
, &rh
);
245 rh
= getservbyname_r (name
, proto
, ret
, buf
, buflen
);
247 ri
= (rh
== NULL
) ? -1 : 0;
252 __gnat_getservbyport (int port
, const char *proto
,
253 struct servent
*ret
, char *buf
, size_t buflen
)
258 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
259 (void) getservbyport_r (port
, proto
, ret
, buf
, buflen
, &rh
);
261 rh
= getservbyport_r (port
, proto
, ret
, buf
, buflen
);
263 ri
= (rh
== NULL
) ? -1 : 0;
266 #elif defined (__vxworks)
267 static char vxw_h_name
[MAXHOSTNAMELEN
+ 1];
268 static char *vxw_h_aliases
[1] = { NULL
};
269 static int vxw_h_addr
;
270 static char *vxw_h_addr_list
[2] = { (char*) &vxw_h_addr
, NULL
};
273 __gnat_gethostbyname (const char *name
,
274 struct hostent
*ret
, char *buf
, size_t buflen
,
277 vxw_h_addr
= hostGetByName (name
);
278 if (vxw_h_addr
== ERROR
) {
279 *h_errnop
= __gnat_get_h_errno ();
283 ret
->h_aliases
= &vxw_h_aliases
;
284 ret
->h_addrtype
= AF_INET
;
286 ret
->h_addr_list
= &vxw_h_addr_list
;
291 __gnat_gethostbyaddr (const char *addr
, int len
, int type
,
292 struct hostent
*ret
, char *buf
, size_t buflen
,
295 if (type
!= AF_INET
) {
296 *h_errnop
= EAFNOSUPPORT
;
300 if (addr
== NULL
|| len
!= 4) {
305 if (hostGetByAddr (*(int*)addr
, &vxw_h_name
) != OK
) {
306 *h_errnop
= __gnat_get_h_errno ();
312 ret
->h_name
= &vxw_h_name
;
313 ret
->h_aliases
= &vxw_h_aliases
;
314 ret
->h_addrtype
= AF_INET
;
316 ret
->h_addr_list
= &vxw_h_addr_list
;
320 __gnat_getservbyname (const char *name
, const char *proto
,
321 struct servent
*ret
, char *buf
, size_t buflen
)
323 /* Not available under VxWorks */
328 __gnat_getservbyport (int port
, const char *proto
,
329 struct servent
*ret
, char *buf
, size_t buflen
)
331 /* Not available under VxWorks */
336 __gnat_gethostbyname (const char *name
, struct hostent
*ret
,
337 char *buf ATTRIBUTE_UNUSED
, size_t buflen ATTRIBUTE_UNUSED
,
341 rh
= gethostbyname (name
);
343 *h_errnop
= __gnat_get_h_errno ();
352 __gnat_gethostbyaddr (const char *addr
, int len
, int type
, struct hostent
*ret
,
353 char *buf ATTRIBUTE_UNUSED
, size_t buflen ATTRIBUTE_UNUSED
,
357 rh
= gethostbyaddr (addr
, len
, type
);
359 *h_errnop
= __gnat_get_h_errno ();
368 __gnat_getservbyname (const char *name
, const char *proto
, struct servent
*ret
,
369 char *buf ATTRIBUTE_UNUSED
, size_t buflen ATTRIBUTE_UNUSED
)
372 rh
= getservbyname (name
, proto
);
380 __gnat_getservbyport (int port
, const char *proto
, struct servent
*ret
,
381 char *buf ATTRIBUTE_UNUSED
, size_t buflen ATTRIBUTE_UNUSED
)
384 rh
= getservbyport (port
, proto
);
392 /* Find the largest socket in the socket set SET. This is needed for
393 `select'. LAST is the maximum value for the largest socket. This hint is
394 used to avoid scanning very large socket sets. On return, LAST is the
395 actual largest socket in the socket set. */
398 __gnat_last_socket_in_set (fd_set
*set
, int *last
)
404 /* More efficient method for NT. */
405 for (unsigned int s
= 0; s
< set
->fd_count
; s
++)
406 if ((int) set
->fd_array
[s
] > l
)
407 l
= set
->fd_array
[s
];
411 for (int s
= *last
; s
!= -1; s
--)
412 if (FD_ISSET (s
, set
))
422 /* Get last socket and remove it from the socket set SET. LAST is the
423 maximum value of the largest socket. This hint is used to avoid scanning
424 very large socket sets. On return, LAST is set to the actual largest
425 socket in the socket set. */
428 __gnat_get_socket_from_set (fd_set
*set
, int *last
, int *socket
)
431 FD_CLR (*socket
, set
);
432 __gnat_last_socket_in_set (set
, last
);
435 /* Insert SOCKET in the socket set SET. */
438 __gnat_insert_socket_in_set (fd_set
*set
, int socket
)
440 FD_SET (socket
, set
);
443 /* Check whether a given SOCKET is in the socket set SET. */
446 __gnat_is_socket_in_set (fd_set
*set
, int socket
)
448 return FD_ISSET (socket
, set
);
451 /* Remove SOCKET from the socket set SET. */
454 __gnat_remove_socket_from_set (fd_set
*set
, int socket
)
456 FD_CLR (socket
, set
);
461 __gnat_reset_socket_set (fd_set
*set
)
466 /* Get the value of the last host error */
469 __gnat_get_h_errno (void) {
471 int vxw_errno
= errno
;
477 #ifdef S_hostLib_HOST_NOT_FOUND
478 case S_hostLib_HOST_NOT_FOUND
:
480 case S_hostLib_UNKNOWN_HOST
:
481 return HOST_NOT_FOUND
;
483 #ifdef S_hostLib_TRY_AGAIN
484 case S_hostLib_TRY_AGAIN
:
488 #ifdef S_hostLib_NO_RECOVERY
489 case S_hostLib_NO_RECOVERY
:
491 #ifdef S_hostLib_NETDB_INTERNAL
492 case S_hostLib_NETDB_INTERNAL
:
494 case S_hostLib_INVALID_PARAMETER
:
501 #elif defined (__rtems__)
502 /* At this stage in the tool build, no networking .h files are available.
503 * Newlib does not provide networking .h files and RTEMS is not built yet.
504 * So we need to explicitly extern h_errno to access it.
514 /* Wrapper for ioctl(2), which is a variadic function */
517 __gnat_socket_ioctl (int fd
, IOCTL_Req_T req
, int *arg
) {
519 return ioctlsocket (fd
, req
, (unsigned long *)arg
);
520 #elif defined (__APPLE__)
522 * On Darwin, req is an unsigned long, and we want to convert without sign
523 * extension to get the proper bit pattern in the case of a 64 bit kernel.
525 return ioctl (fd
, (unsigned int) req
, arg
);
527 return ioctl (fd
, req
, arg
);
531 #ifndef HAVE_INET_PTON
534 __gnat_inet_pton (int af
, const char *src
, void *dst
) {
536 #if defined (_WIN32) && defined (AF_INET6)
542 errno
= EAFNOSUPPORT
;
546 #if defined (__vxworks)
547 return (inet_aton (src
, dst
) == OK
);
549 #elif defined (_WIN32)
550 struct sockaddr_storage ss
;
551 int sslen
= sizeof ss
;
555 rc
= WSAStringToAddressA ((char *)src
, af
, NULL
, (struct sockaddr
*)&ss
,
560 *(struct in_addr
*)dst
= ((struct sockaddr_in
*)&ss
)->sin_addr
;
564 *(struct in6_addr
*)dst
= ((struct sockaddr_in6
*)&ss
)->sin6_addr
;
571 #elif defined (__hpux__)
575 if (src
== NULL
|| dst
== NULL
) {
578 } else if (!strcmp (src
, "255.255.255.255")) {
583 addr
= inet_addr (src
);
584 rc
= (addr
!= 0xffffffff);
587 *(in_addr_t
*)dst
= addr
;
594 #ifndef HAVE_INET_NTOP
597 __gnat_inet_ntop(int af
, const void *src
, char *dst
, socklen_t size
)
600 struct sockaddr_storage ss
;
601 int sslen
= sizeof ss
;
602 memset(&ss
, 0, sslen
);
607 ((struct sockaddr_in6
*)&ss
)->sin6_addr
= *(struct in6_addr
*)src
;
610 ((struct sockaddr_in
*)&ss
)->sin_addr
= *(struct in_addr
*)src
;
613 errno
= EAFNOSUPPORT
;
619 if (WSAAddressToStringA((struct sockaddr
*)&ss
, sslen
, 0, dst
, &sz
) != 0) {
630 * Accessor functions for struct hostent.
633 char * __gnat_hostent_h_name (struct hostent
* h
) {
637 char * __gnat_hostent_h_alias (struct hostent
* h
, int index
) {
638 return h
->h_aliases
[index
];
641 int __gnat_hostent_h_addrtype (struct hostent
* h
) {
642 return h
->h_addrtype
;
645 int __gnat_hostent_h_length (struct hostent
* h
) {
649 char * __gnat_hostent_h_addr (struct hostent
* h
, int index
) {
650 return h
->h_addr_list
[index
];
654 * Accessor functions for struct servent.
656 * These are needed because servent has different representations on different
657 * platforms, and we don't want to deal with that on the Ada side. For example,
658 * on Linux, we have (see /usr/include netdb.h):
668 * and on Windows (see mingw's socket.h):
684 __gnat_servent_s_name (struct servent
* s
)
690 __gnat_servent_s_alias (struct servent
* s
, int index
)
692 return s
->s_aliases
[index
];
696 __gnat_servent_s_port (struct servent
* s
)
702 __gnat_servent_s_proto (struct servent
* s
)
707 #if defined(AF_INET6) && !defined(__rtems__)
709 int __gnat_getaddrinfo(
712 const struct addrinfo
*hints
,
713 struct addrinfo
**res
)
715 return getaddrinfo(node
, service
, hints
, res
);
718 int __gnat_getnameinfo(
719 const struct sockaddr
*sa
, socklen_t salen
,
720 char *host
, size_t hostlen
,
721 char *serv
, size_t servlen
, int flags
)
723 return getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
);
726 void __gnat_freeaddrinfo(struct addrinfo
*res
) {
730 const char * __gnat_gai_strerror(int errcode
) {
731 #if defined(_WIN32) || defined(__vxworks)
732 // gai_strerror thread usafe on Windows and is not available on some vxWorks
737 return "Temporary failure in name resolution.";
739 return "Invalid value for ai_flags.";
741 return "Nonrecoverable failure in name resolution.";
743 return "The ai_family member is not supported.";
745 return "Memory allocation failure.";
747 // Could be not defined under the vxWorks
749 return "No address associated with nodename.";
751 #if EAI_NODATA != EAI_NONAME
752 /* with mingw64 runtime EAI_NODATA and EAI_NONAME have the same value.
753 This applies to both win32 and win64 */
755 return "Neither nodename nor servname provided, or not known.";
758 return "The servname parameter is not supported for ai_socktype.";
760 return "The ai_socktype member is not supported.";
762 // Could be not defined, at least on Windows
764 return "System error returned in errno";
767 return "Unknown error.";
770 return gai_strerror(errcode
);
776 int __gnat_getaddrinfo(
779 const struct addrinfo
*hints
,
780 struct addrinfo
**res
)
785 int __gnat_getnameinfo(
786 const struct sockaddr
*sa
, socklen_t salen
,
787 char *host
, size_t hostlen
,
788 char *serv
, size_t servlen
, int flags
)
793 void __gnat_freeaddrinfo(struct addrinfo
*res
) {
796 const char * __gnat_gai_strerror(int errcode
) {
797 return "getaddinfo functions family is not supported";
802 int __gnat_minus_500ms() {
804 // Windows Server 2019 and Windows 8.0 do not need 500 millisecond socket
805 // timeout correction.
806 if (IsWindowsServer()) {
808 ZeroMemory(&osvi
, sizeof(OSVERSIONINFO
));
809 osvi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
810 // Documentation proposes to use IsWindowsVersionOrGreater(10, 0, 17763)
811 // but it does not compare by the build number (last parameter).
813 return osvi
.dwMajorVersion
< 10
814 || (osvi
.dwMajorVersion
== 10
815 && osvi
.dwMinorVersion
== 0
816 && osvi
.dwBuildNumber
< 17763);
818 return !IsWindows8OrGreater();
825 #endif /* defined(HAVE_SOCKETS) */