1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2003-2018, 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 /* Ensure access to errno is thread safe. */
40 #if defined (__FreeBSD__) || defined (__DragonFly__) \
41 || defined (__NetBSD__) || defined (__OpenBSD__)
42 typedef unsigned int IOCTL_Req_T
;
44 typedef int IOCTL_Req_T
;
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)
56 /* Required if we will be calling signal() in __gnat_disable_all_sigpipes() */
59 /* Required for __gnat_malloc() */
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 *);
95 /* Disable the sending of SIGPIPE for writes on a broken stream */
98 __gnat_disable_sigpipe (int fd
)
102 (void) setsockopt (fd
, SOL_SOCKET
, SO_NOSIGPIPE
, &val
, sizeof val
);
107 __gnat_disable_all_sigpipes (void)
109 #if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) && defined(SIGPIPE)
110 (void) signal (SIGPIPE
, SIG_IGN
);
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).
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
126 __gnat_create_signalling_fds (int *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
) {
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
) {
147 return write (wsig
, &c
, 1);
151 * Close one end of a pair of signalling fds
154 __gnat_close_signalling_fd (int sig
) {
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
179 #ifdef HAVE_GETxxxBYyyy_R
181 __gnat_gethostbyname (const char *name
,
182 struct hostent
*ret
, char *buf
, size_t buflen
,
188 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
189 (void) gethostbyname_r (name
, ret
, buf
, buflen
, &rh
, h_errnop
);
191 rh
= gethostbyname_r (name
, ret
, buf
, buflen
, h_errnop
);
193 ri
= (rh
== NULL
) ? -1 : 0;
198 __gnat_gethostbyaddr (const char *addr
, int len
, int type
,
199 struct hostent
*ret
, char *buf
, size_t buflen
,
205 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
206 (void) gethostbyaddr_r (addr
, len
, type
, ret
, buf
, buflen
, &rh
, h_errnop
);
208 rh
= gethostbyaddr_r (addr
, len
, type
, ret
, buf
, buflen
, h_errnop
);
210 ri
= (rh
== NULL
) ? -1 : 0;
215 __gnat_getservbyname (const char *name
, const char *proto
,
216 struct servent
*ret
, char *buf
, size_t buflen
)
221 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
222 (void) getservbyname_r (name
, proto
, ret
, buf
, buflen
, &rh
);
224 rh
= getservbyname_r (name
, proto
, ret
, buf
, buflen
);
226 ri
= (rh
== NULL
) ? -1 : 0;
231 __gnat_getservbyport (int port
, const char *proto
,
232 struct servent
*ret
, char *buf
, size_t buflen
)
237 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
238 (void) getservbyport_r (port
, proto
, ret
, buf
, buflen
, &rh
);
240 rh
= getservbyport_r (port
, proto
, ret
, buf
, buflen
);
242 ri
= (rh
== NULL
) ? -1 : 0;
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
,
256 vxw_h_addr
= hostGetByName (name
);
257 if (vxw_h_addr
== ERROR
) {
258 *h_errnop
= __gnat_get_h_errno ();
262 ret
->h_aliases
= &vxw_h_aliases
;
263 ret
->h_addrtype
= AF_INET
;
265 ret
->h_addr_list
= &vxw_h_addr_list
;
270 __gnat_gethostbyaddr (const char *addr
, int len
, int type
,
271 struct hostent
*ret
, char *buf
, size_t buflen
,
274 if (type
!= AF_INET
) {
275 *h_errnop
= EAFNOSUPPORT
;
279 if (addr
== NULL
|| len
!= 4) {
284 if (hostGetByAddr (*(int*)addr
, &vxw_h_name
) != OK
) {
285 *h_errnop
= __gnat_get_h_errno ();
291 ret
->h_name
= &vxw_h_name
;
292 ret
->h_aliases
= &vxw_h_aliases
;
293 ret
->h_addrtype
= AF_INET
;
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 */
307 __gnat_getservbyport (int port
, const char *proto
,
308 struct servent
*ret
, char *buf
, size_t buflen
)
310 /* Not available under VxWorks */
315 __gnat_gethostbyname (const char *name
,
316 struct hostent
*ret
, char *buf
, size_t buflen
,
320 rh
= gethostbyname (name
);
322 *h_errnop
= __gnat_get_h_errno ();
331 __gnat_gethostbyaddr (const char *addr
, int len
, int type
,
332 struct hostent
*ret
, char *buf
, size_t buflen
,
336 rh
= gethostbyaddr (addr
, len
, type
);
338 *h_errnop
= __gnat_get_h_errno ();
347 __gnat_getservbyname (const char *name
, const char *proto
,
348 struct servent
*ret
, char *buf
, size_t buflen
)
351 rh
= getservbyname (name
, proto
);
359 __gnat_getservbyport (int port
, const char *proto
,
360 struct servent
*ret
, char *buf
, size_t buflen
)
363 rh
= getservbyport (port
, proto
);
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. */
377 __gnat_last_socket_in_set (fd_set
*set
, int *last
)
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
];
391 for (s
= *last
; s
!= -1; s
--)
392 if (FD_ISSET (s
, set
))
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. */
408 __gnat_get_socket_from_set (fd_set
*set
, int *last
, int *socket
)
411 FD_CLR (*socket
, set
);
412 __gnat_last_socket_in_set (set
, last
);
415 /* Insert SOCKET in the socket set SET. */
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. */
434 __gnat_remove_socket_from_set (fd_set
*set
, int socket
)
436 FD_CLR (socket
, set
);
441 __gnat_reset_socket_set (fd_set
*set
)
446 /* Get the value of the last host error */
449 __gnat_get_h_errno (void) {
451 int vxw_errno
= errno
;
457 #ifdef S_hostLib_HOST_NOT_FOUND
458 case S_hostLib_HOST_NOT_FOUND
:
460 case S_hostLib_UNKNOWN_HOST
:
461 return HOST_NOT_FOUND
;
463 #ifdef S_hostLib_TRY_AGAIN
464 case S_hostLib_TRY_AGAIN
:
468 #ifdef S_hostLib_NO_RECOVERY
469 case S_hostLib_NO_RECOVERY
:
471 #ifdef S_hostLib_NETDB_INTERNAL
472 case S_hostLib_NETDB_INTERNAL
:
474 case S_hostLib_INVALID_PARAMETER
:
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.
494 /* Wrapper for ioctl(2), which is a variadic function */
497 __gnat_socket_ioctl (int fd
, IOCTL_Req_T req
, int *arg
) {
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
);
507 return ioctl (fd
, req
, arg
);
511 #ifndef HAVE_INET_PTON
514 __gnat_inet_pton (int af
, const char *src
, void *dst
) {
516 #if defined (_WIN32) && defined (AF_INET6)
522 errno
= EAFNOSUPPORT
;
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
;
535 rc
= WSAStringToAddressA (src
, af
, NULL
, (struct sockaddr
*)&ss
, &sslen
);
539 *(struct in_addr
*)dst
= ((struct sockaddr_in
*)&ss
)->sin_addr
;
543 *(struct in6_addr
*)dst
= ((struct sockaddr_in6
*)&ss
)->sin6_addr
;
550 #elif defined (__hpux__)
554 if (src
== NULL
|| dst
== NULL
) {
557 } else if (!strcmp (src
, "255.255.255.255")) {
562 addr
= inet_addr (src
);
563 rc
= (addr
!= 0xffffffff);
566 *(in_addr_t
*)dst
= addr
;
574 * Accessor functions for struct hostent.
577 char * __gnat_hostent_h_name (struct hostent
* h
) {
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
) {
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):
612 * and on Windows (see mingw's socket.h):
628 __gnat_servent_s_name (struct servent
* s
)
634 __gnat_servent_s_alias (struct servent
* s
, int index
)
636 return s
->s_aliases
[index
];
640 __gnat_servent_s_port (struct servent
* s
)
646 __gnat_servent_s_proto (struct servent
* s
)
651 #endif /* defined(HAVE_SOCKETS) */