1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2003-2012, 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 */
38 * For VMS, gsocket.h can't include sockets-related DEC C header files
39 * when building the runtime (because these files are in a DEC C text library
40 * (DECC$RTLDEF.TLB) not accessible to GCC). So, we generate a separate header
41 * file along with s-oscons.ads and include it here.
43 # include "s-oscons.h"
46 * We also need the declaration of struct hostent/servent, which s-oscons
47 * can't provide, so we copy it manually here. This needs to be kept in synch
48 * with the definition of that structure in the DEC C headers, which
49 * hopefully won't change frequently.
51 typedef char *__netdb_char_ptr
__attribute__ (( mode (SI
) ));
52 typedef __netdb_char_ptr
*__netdb_char_ptr_ptr
__attribute__ (( mode (SI
) ));
55 __netdb_char_ptr h_name
;
56 __netdb_char_ptr_ptr h_aliases
;
59 __netdb_char_ptr_ptr h_addr_list
;
63 __netdb_char_ptr s_name
;
64 __netdb_char_ptr_ptr s_aliases
;
66 __netdb_char_ptr s_proto
;
68 #elif defined(__FreeBSD__)
69 typedef unsigned int IOCTL_Req_T
;
71 typedef int IOCTL_Req_T
;
74 #if defined(HAVE_SOCKETS)
76 /* Include all the necessary system-specific headers and define the
77 * necessary macros (shared with gen-oscons).
80 #if !defined(SO_NOSIGPIPE) && !defined (MSG_NOSIGNAL)
83 /* Required if we will be calling signal() in __gnat_disable_all_sigpipes() */
86 /* Required for __gnat_malloc() */
89 /* Required for memcpy() */
91 extern void __gnat_disable_sigpipe (int fd
);
92 extern void __gnat_disable_all_sigpipes (void);
93 extern int __gnat_create_signalling_fds (int *fds
);
94 extern int __gnat_read_signalling_fd (int rsig
);
95 extern int __gnat_write_signalling_fd (int wsig
);
96 extern void __gnat_close_signalling_fd (int sig
);
97 extern void __gnat_last_socket_in_set (fd_set
*, int *);
98 extern void __gnat_get_socket_from_set (fd_set
*, int *, int *);
99 extern void __gnat_insert_socket_in_set (fd_set
*, int);
100 extern int __gnat_is_socket_in_set (fd_set
*, int);
101 extern fd_set
*__gnat_new_socket_set (fd_set
*);
102 extern void __gnat_remove_socket_from_set (fd_set
*, int);
103 extern void __gnat_reset_socket_set (fd_set
*);
104 extern int __gnat_get_h_errno (void);
105 extern int __gnat_socket_ioctl (int, IOCTL_Req_T
, int *);
107 extern char * __gnat_servent_s_name (struct servent
*);
108 extern char * __gnat_servent_s_alias (struct servent
*, int index
);
109 extern unsigned short __gnat_servent_s_port (struct servent
*);
110 extern char * __gnat_servent_s_proto (struct servent
*);
112 extern char * __gnat_hostent_h_name (struct hostent
*);
113 extern char * __gnat_hostent_h_alias (struct hostent
*, int);
114 extern int __gnat_hostent_h_addrtype (struct hostent
*);
115 extern int __gnat_hostent_h_length (struct hostent
*);
116 extern char * __gnat_hostent_h_addr (struct hostent
*, int);
118 #ifndef HAVE_INET_PTON
119 extern int __gnat_inet_pton (int, const char *, void *);
122 /* Disable the sending of SIGPIPE for writes on a broken stream */
125 __gnat_disable_sigpipe (int fd
)
129 (void) setsockopt (fd
, SOL_SOCKET
, SO_NOSIGPIPE
, &val
, sizeof val
);
134 __gnat_disable_all_sigpipes (void)
136 #if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) && defined(SIGPIPE)
137 (void) signal (SIGPIPE
, SIG_IGN
);
141 #if defined (_WIN32) || defined (__vxworks) || defined (VMS)
143 * Signalling FDs operations are implemented in Ada for these platforms
144 * (see subunit GNAT.Sockets.Thin.Signalling_Fds).
148 * Create a pair of connected file descriptors fds[0] and fds[1] used for
149 * signalling by a Selector object. fds[0] is the read end, and fds[1] the
153 __gnat_create_signalling_fds (int *fds
) {
158 * Read one byte of data from rsig, the read end of a pair of signalling fds
159 * created by __gnat_create_signalling_fds.
162 __gnat_read_signalling_fd (int rsig
) {
164 return read (rsig
, &c
, 1);
168 * Write one byte of data to wsig, the write end of a pair of signalling fds
169 * created by __gnat_create_signalling_fds.
172 __gnat_write_signalling_fd (int wsig
) {
174 return write (wsig
, &c
, 1);
178 * Close one end of a pair of signalling fds
181 __gnat_close_signalling_fd (int sig
) {
187 * Handling of gethostbyname, gethostbyaddr, getservbyname and getservbyport
188 * =========================================================================
190 * This module exposes __gnat_getXXXbyYYY operations with the same signature
191 * as the reentrant variant getXXXbyYYY_r.
193 * On platforms where getXXXbyYYY is intrinsically reentrant, the provided user
194 * buffer argument is ignored.
196 * When getXXXbyYYY is not reentrant but getXXXbyYYY_r exists, the latter is
197 * used, and the provided buffer argument must point to a valid, thread-local
198 * buffer (usually on the caller's stack).
200 * When getXXXbyYYY is not reentrant and no reentrant getXXXbyYYY_r variant
201 * is available, the non-reentrant getXXXbyYYY is called, the provided user
202 * buffer is ignored, and the caller is expected to take care of mutual
206 #ifdef HAVE_GETxxxBYyyy_R
208 __gnat_gethostbyname (const char *name
,
209 struct hostent
*ret
, char *buf
, size_t buflen
,
215 #if defined(__linux__) || defined(__GLIBC__)
216 (void) gethostbyname_r (name
, ret
, buf
, buflen
, &rh
, h_errnop
);
218 rh
= gethostbyname_r (name
, ret
, buf
, buflen
, h_errnop
);
220 ri
= (rh
== NULL
) ? -1 : 0;
225 __gnat_gethostbyaddr (const char *addr
, int len
, int type
,
226 struct hostent
*ret
, char *buf
, size_t buflen
,
232 #if defined(__linux__) || defined(__GLIBC__)
233 (void) gethostbyaddr_r (addr
, len
, type
, ret
, buf
, buflen
, &rh
, h_errnop
);
235 rh
= gethostbyaddr_r (addr
, len
, type
, ret
, buf
, buflen
, h_errnop
);
237 ri
= (rh
== NULL
) ? -1 : 0;
242 __gnat_getservbyname (const char *name
, const char *proto
,
243 struct servent
*ret
, char *buf
, size_t buflen
)
248 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
249 (void) getservbyname_r (name
, proto
, ret
, buf
, buflen
, &rh
);
251 rh
= getservbyname_r (name
, proto
, ret
, buf
, buflen
);
253 ri
= (rh
== NULL
) ? -1 : 0;
258 __gnat_getservbyport (int port
, const char *proto
,
259 struct servent
*ret
, char *buf
, size_t buflen
)
264 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
265 (void) getservbyport_r (port
, proto
, ret
, buf
, buflen
, &rh
);
267 rh
= getservbyport_r (port
, proto
, ret
, buf
, buflen
);
269 ri
= (rh
== NULL
) ? -1 : 0;
272 #elif defined (__vxworks)
273 static char vxw_h_name
[MAXHOSTNAMELEN
+ 1];
274 static char *vxw_h_aliases
[1] = { NULL
};
275 static int vxw_h_addr
;
276 static char *vxw_h_addr_list
[2] = { (char*) &vxw_h_addr
, NULL
};
279 __gnat_gethostbyname (const char *name
,
280 struct hostent
*ret
, char *buf
, size_t buflen
,
283 vxw_h_addr
= hostGetByName (name
);
284 if (vxw_h_addr
== ERROR
) {
285 *h_errnop
= __gnat_get_h_errno ();
289 ret
->h_aliases
= &vxw_h_aliases
;
290 ret
->h_addrtype
= AF_INET
;
292 ret
->h_addr_list
= &vxw_h_addr_list
;
297 __gnat_gethostbyaddr (const char *addr
, int len
, int type
,
298 struct hostent
*ret
, char *buf
, size_t buflen
,
301 if (type
!= AF_INET
) {
302 *h_errnop
= EAFNOSUPPORT
;
306 if (addr
== NULL
|| len
!= 4) {
311 if (hostGetByAddr (*(int*)addr
, &vxw_h_name
) != OK
) {
312 *h_errnop
= __gnat_get_h_errno ();
318 ret
->h_name
= &vxw_h_name
;
319 ret
->h_aliases
= &vxw_h_aliases
;
320 ret
->h_addrtype
= AF_INET
;
322 ret
->h_addr_list
= &vxw_h_addr_list
;
326 __gnat_getservbyname (const char *name
, const char *proto
,
327 struct servent
*ret
, char *buf
, size_t buflen
)
329 /* Not available under VxWorks */
334 __gnat_getservbyport (int port
, const char *proto
,
335 struct servent
*ret
, char *buf
, size_t buflen
)
337 /* Not available under VxWorks */
342 __gnat_gethostbyname (const char *name
,
343 struct hostent
*ret
, char *buf
, size_t buflen
,
347 rh
= gethostbyname (name
);
349 *h_errnop
= __gnat_get_h_errno ();
358 __gnat_gethostbyaddr (const char *addr
, int len
, int type
,
359 struct hostent
*ret
, char *buf
, size_t buflen
,
363 rh
= gethostbyaddr (addr
, len
, type
);
365 *h_errnop
= __gnat_get_h_errno ();
374 __gnat_getservbyname (const char *name
, const char *proto
,
375 struct servent
*ret
, char *buf
, size_t buflen
)
378 rh
= getservbyname (name
, proto
);
386 __gnat_getservbyport (int port
, const char *proto
,
387 struct servent
*ret
, char *buf
, size_t buflen
)
390 rh
= getservbyport (port
, proto
);
398 /* Find the largest socket in the socket set SET. This is needed for
399 `select'. LAST is the maximum value for the largest socket. This hint is
400 used to avoid scanning very large socket sets. On return, LAST is the
401 actual largest socket in the socket set. */
404 __gnat_last_socket_in_set (fd_set
*set
, int *last
)
411 /* More efficient method for NT. */
412 for (s
= 0; s
< set
->fd_count
; s
++)
413 if ((int) set
->fd_array
[s
] > l
)
414 l
= set
->fd_array
[s
];
418 for (s
= *last
; s
!= -1; s
--)
419 if (FD_ISSET (s
, set
))
429 /* Get last socket and remove it from the socket set SET. LAST is the
430 maximum value of the largest socket. This hint is used to avoid scanning
431 very large socket sets. On return, LAST is set to the actual largest
432 socket in the socket set. */
435 __gnat_get_socket_from_set (fd_set
*set
, int *last
, int *socket
)
438 FD_CLR (*socket
, set
);
439 __gnat_last_socket_in_set (set
, last
);
442 /* Insert SOCKET in the socket set SET. */
445 __gnat_insert_socket_in_set (fd_set
*set
, int socket
)
447 FD_SET (socket
, set
);
450 /* Check whether a given SOCKET is in the socket set SET. */
453 __gnat_is_socket_in_set (fd_set
*set
, int socket
)
455 return FD_ISSET (socket
, set
);
458 /* Remove SOCKET from the socket set SET. */
461 __gnat_remove_socket_from_set (fd_set
*set
, int socket
)
463 FD_CLR (socket
, set
);
468 __gnat_reset_socket_set (fd_set
*set
)
473 /* Get the value of the last host error */
476 __gnat_get_h_errno (void) {
478 int vxw_errno
= errno
;
484 #ifdef S_hostLib_HOST_NOT_FOUND
485 case S_hostLib_HOST_NOT_FOUND
:
487 case S_hostLib_UNKNOWN_HOST
:
488 return HOST_NOT_FOUND
;
490 #ifdef S_hostLib_TRY_AGAIN
491 case S_hostLib_TRY_AGAIN
:
495 #ifdef S_hostLib_NO_RECOVERY
496 case S_hostLib_NO_RECOVERY
:
498 #ifdef S_hostLib_NETDB_INTERNAL
499 case S_hostLib_NETDB_INTERNAL
:
501 case S_hostLib_INVALID_PARAMETER
:
509 /* h_errno is defined as follows in OpenVMS' version of <netdb.h>.
510 * However this header file is not available when building the GNAT
511 * runtime library using GCC, so we are hardcoding the definition
512 * directly. Note that the returned address is thread-specific.
514 extern int *decc$
h_errno_get_addr ();
515 return *decc$
h_errno_get_addr ();
517 #elif defined (__rtems__)
518 /* At this stage in the tool build, no networking .h files are available.
519 * Newlib does not provide networking .h files and RTEMS is not built yet.
520 * So we need to explicitly extern h_errno to access it.
530 /* Wrapper for ioctl(2), which is a variadic function */
533 __gnat_socket_ioctl (int fd
, IOCTL_Req_T req
, int *arg
) {
535 return ioctlsocket (fd
, req
, arg
);
536 #elif defined (__APPLE__)
538 * On Darwin, req is an unsigned long, and we want to convert without sign
539 * extension to get the proper bit pattern in the case of a 64 bit kernel.
541 return ioctl (fd
, (unsigned int) req
, arg
);
543 return ioctl (fd
, req
, arg
);
547 #ifndef HAVE_INET_PTON
550 # define in_addr_t int
551 # define inet_addr decc$inet_addr
555 __gnat_inet_pton (int af
, const char *src
, void *dst
) {
557 #if defined (_WIN32) && defined (AF_INET6)
563 errno
= EAFNOSUPPORT
;
567 #if defined (__vxworks)
568 return (inet_aton (src
, dst
) == OK
);
570 #elif defined (_WIN32)
571 struct sockaddr_storage ss
;
572 int sslen
= sizeof ss
;
576 rc
= WSAStringToAddressA (src
, af
, NULL
, (struct sockaddr
*)&ss
, &sslen
);
580 *(struct in_addr
*)dst
= ((struct sockaddr_in
*)&ss
)->sin_addr
;
584 *(struct in6_addr
*)dst
= ((struct sockaddr_in6
*)&ss
)->sin6_addr
;
591 #elif defined (__hpux__) || defined (VMS)
595 if (src
== NULL
|| dst
== NULL
) {
598 } else if (!strcmp (src
, "255.255.255.255")) {
603 addr
= inet_addr (src
);
604 rc
= (addr
!= 0xffffffff);
607 *(in_addr_t
*)dst
= addr
;
615 * Accessor functions for struct hostent.
618 char * __gnat_hostent_h_name (struct hostent
* h
) {
622 char * __gnat_hostent_h_alias (struct hostent
* h
, int index
) {
623 return h
->h_aliases
[index
];
626 int __gnat_hostent_h_addrtype (struct hostent
* h
) {
627 return h
->h_addrtype
;
630 int __gnat_hostent_h_length (struct hostent
* h
) {
634 char * __gnat_hostent_h_addr (struct hostent
* h
, int index
) {
635 return h
->h_addr_list
[index
];
639 * Accessor functions for struct servent.
641 * These are needed because servent has different representations on different
642 * platforms, and we don't want to deal with that on the Ada side. For example,
643 * on Linux, we have (see /usr/include netdb.h):
653 * and on Windows (see mingw's socket.h):
669 __gnat_servent_s_name (struct servent
* s
)
675 __gnat_servent_s_alias (struct servent
* s
, int index
)
677 return s
->s_aliases
[index
];
681 __gnat_servent_s_port (struct servent
* s
)
687 __gnat_servent_s_proto (struct servent
* s
)
693 # warning Sockets are not supported on this platform
694 #endif /* defined(HAVE_SOCKETS) */