1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2003-2009, 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 */
37 * For VMS, gsocket.h can't include sockets-related DEC C header files
38 * when building the runtime (because these files are in DEC C archives,
39 * not accessable to GCC). So, we generate a separate header file along
40 * with s-oscons.ads and include it here.
42 # include "s-oscons.h"
45 #if defined(HAVE_SOCKETS)
47 /* Include all the necessary system-specific headers and define the
48 * necessary macros (shared with gen-oscons).
51 #if !defined(SO_NOSIGPIPE) && !defined (MSG_NOSIGNAL)
54 /* Required if we will be calling signal() in __gnat_disable_all_sigpipes() */
57 /* Required for __gnat_malloc() */
60 /* Required for memcpy() */
62 extern void __gnat_disable_sigpipe (int fd
);
63 extern void __gnat_disable_all_sigpipes (void);
64 extern int __gnat_create_signalling_fds (int *fds
);
65 extern int __gnat_read_signalling_fd (int rsig
);
66 extern int __gnat_write_signalling_fd (int wsig
);
67 extern void __gnat_close_signalling_fd (int sig
);
68 extern void __gnat_last_socket_in_set (fd_set
*, int *);
69 extern void __gnat_get_socket_from_set (fd_set
*, int *, int *);
70 extern void __gnat_insert_socket_in_set (fd_set
*, int);
71 extern int __gnat_is_socket_in_set (fd_set
*, int);
72 extern fd_set
*__gnat_new_socket_set (fd_set
*);
73 extern void __gnat_remove_socket_from_set (fd_set
*, int);
74 extern void __gnat_reset_socket_set (fd_set
*);
75 extern int __gnat_get_h_errno (void);
76 extern int __gnat_socket_ioctl (int, int, int *);
77 #if defined (__vxworks) || defined (_WIN32)
78 extern int __gnat_inet_pton (int, const char *, void *);
81 /* Disable the sending of SIGPIPE for writes on a broken stream */
84 __gnat_disable_sigpipe (int fd
)
88 (void) setsockopt (fd
, SOL_SOCKET
, SO_NOSIGPIPE
, &val
, sizeof val
);
93 __gnat_disable_all_sigpipes (void)
95 #if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) && defined(SIGPIPE)
96 (void) signal (SIGPIPE
, SIG_IGN
);
100 #if defined (_WIN32) || defined (__vxworks) || defined (VMS)
102 * Signalling FDs operations are implemented in Ada for these platforms
103 * (see subunit GNAT.Sockets.Thin.Signalling_Fds).
107 * Create a pair of connected file descriptors fds[0] and fds[1] used for
108 * signalling by a Selector object. fds[0] is the read end, and fds[1] the
112 __gnat_create_signalling_fds (int *fds
) {
117 * Read one byte of data from rsig, the read end of a pair of signalling fds
118 * created by __gnat_create_signalling_fds.
121 __gnat_read_signalling_fd (int rsig
) {
123 return read (rsig
, &c
, 1);
127 * Write one byte of data to wsig, the write end of a pair of signalling fds
128 * created by __gnat_create_signalling_fds.
131 __gnat_write_signalling_fd (int wsig
) {
133 return write (wsig
, &c
, 1);
137 * Close one end of a pair of signalling fds
140 __gnat_close_signalling_fd (int sig
) {
146 * GetXXXbyYYY wrappers
147 * These functions are used by the default implementation of g-socthi,
148 * and also by the Windows version.
150 * They can be used for any platform that either provides an intrinsically
151 * task safe implementation of getXXXbyYYY, or a reentrant variant
152 * getXXXbyYYY_r. Otherwise, a task safe wrapper, including proper mutual
153 * exclusion if appropriate, must be implemented in the target specific
154 * version of g-socthi.
157 #ifdef HAVE_THREAD_SAFE_GETxxxBYyyy
159 __gnat_safe_gethostbyname (const char *name
,
160 struct hostent
*ret
, char *buf
, size_t buflen
,
164 rh
= gethostbyname (name
);
175 __gnat_safe_gethostbyaddr (const char *addr
, int len
, int type
,
176 struct hostent
*ret
, char *buf
, size_t buflen
,
180 rh
= gethostbyaddr (addr
, len
, type
);
191 __gnat_safe_getservbyname (const char *name
, const char *proto
,
192 struct servent
*ret
, char *buf
, size_t buflen
)
195 rh
= getservbyname (name
, proto
);
203 __gnat_safe_getservbyport (int port
, const char *proto
,
204 struct servent
*ret
, char *buf
, size_t buflen
)
207 rh
= getservbyport (port
, proto
);
213 #elif HAVE_GETxxxBYyyy_R
215 __gnat_safe_gethostbyname (const char *name
,
216 struct hostent
*ret
, char *buf
, size_t buflen
,
222 #if defined(__linux__) || defined(__GLIBC__)
223 (void) gethostbyname_r (name
, ret
, buf
, buflen
, &rh
, h_errnop
);
225 rh
= gethostbyname_r (name
, ret
, buf
, buflen
, h_errnop
);
227 ri
= (rh
== NULL
) ? -1 : 0;
232 __gnat_safe_gethostbyaddr (const char *addr
, int len
, int type
,
233 struct hostent
*ret
, char *buf
, size_t buflen
,
239 #if defined(__linux__) || defined(__GLIBC__)
240 (void) gethostbyaddr_r (addr
, len
, type
, ret
, buf
, buflen
, &rh
, h_errnop
);
242 rh
= gethostbyaddr_r (addr
, len
, type
, ret
, buf
, buflen
, h_errnop
);
244 ri
= (rh
== NULL
) ? -1 : 0;
249 __gnat_safe_getservbyname (const char *name
, const char *proto
,
250 struct servent
*ret
, char *buf
, size_t buflen
)
255 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
256 (void) getservbyname_r (name
, proto
, ret
, buf
, buflen
, &rh
);
258 rh
= getservbyname_r (name
, proto
, ret
, buf
, buflen
);
260 ri
= (rh
== NULL
) ? -1 : 0;
265 __gnat_safe_getservbyport (int port
, const char *proto
,
266 struct servent
*ret
, char *buf
, size_t buflen
)
271 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
272 (void) getservbyport_r (port
, proto
, ret
, buf
, buflen
, &rh
);
274 rh
= getservbyport_r (port
, proto
, ret
, buf
, buflen
);
276 ri
= (rh
== NULL
) ? -1 : 0;
281 /* Find the largest socket in the socket set SET. This is needed for
282 `select'. LAST is the maximum value for the largest socket. This hint is
283 used to avoid scanning very large socket sets. On return, LAST is the
284 actual largest socket in the socket set. */
287 __gnat_last_socket_in_set (fd_set
*set
, int *last
)
294 /* More efficient method for NT. */
295 for (s
= 0; s
< set
->fd_count
; s
++)
296 if ((int) set
->fd_array
[s
] > l
)
297 l
= set
->fd_array
[s
];
301 for (s
= *last
; s
!= -1; s
--)
302 if (FD_ISSET (s
, set
))
312 /* Get last socket and remove it from the socket set SET. LAST is the
313 maximum value of the largest socket. This hint is used to avoid scanning
314 very large socket sets. On return, LAST is set to the actual largest
315 socket in the socket set. */
318 __gnat_get_socket_from_set (fd_set
*set
, int *last
, int *socket
)
321 FD_CLR (*socket
, set
);
322 __gnat_last_socket_in_set (set
, last
);
325 /* Insert SOCKET in the socket set SET. */
328 __gnat_insert_socket_in_set (fd_set
*set
, int socket
)
330 FD_SET (socket
, set
);
333 /* Check whether a given SOCKET is in the socket set SET. */
336 __gnat_is_socket_in_set (fd_set
*set
, int socket
)
338 return FD_ISSET (socket
, set
);
341 /* Remove SOCKET from the socket set SET. */
344 __gnat_remove_socket_from_set (fd_set
*set
, int socket
)
346 FD_CLR (socket
, set
);
351 __gnat_reset_socket_set (fd_set
*set
)
356 /* Get the value of the last host error */
359 __gnat_get_h_errno (void) {
361 int vxw_errno
= errno
;
367 #ifdef S_hostLib_HOST_NOT_FOUND
368 case S_hostLib_HOST_NOT_FOUND
:
370 case S_hostLib_UNKNOWN_HOST
:
371 return HOST_NOT_FOUND
;
373 #ifdef S_hostLib_TRY_AGAIN
374 case S_hostLib_TRY_AGAIN
:
378 #ifdef S_hostLib_NO_RECOVERY
379 case S_hostLib_NO_RECOVERY
:
381 #ifdef S_hostLib_NETDB_INTERNAL
382 case S_hostLib_NETDB_INTERNAL
:
384 case S_hostLib_INVALID_PARAMETER
:
392 /* h_errno is defined as follows in OpenVMS' version of <netdb.h>.
393 * However this header file is not available when building the GNAT
394 * runtime library using GCC, so we are hardcoding the definition
395 * directly. Note that the returned address is thread-specific.
397 extern int *decc$
h_errno_get_addr ();
398 return *decc$
h_errno_get_addr ();
400 #elif defined (__rtems__)
401 /* At this stage in the tool build, no networking .h files are available.
402 * Newlib does not provide networking .h files and RTEMS is not built yet.
403 * So we need to explicitly extern h_errno to access it.
413 /* Wrapper for ioctl(2), which is a variadic function */
416 __gnat_socket_ioctl (int fd
, int req
, int *arg
) {
418 return ioctlsocket (fd
, req
, arg
);
420 return ioctl (fd
, req
, arg
);
424 #ifndef HAVE_INET_PTON
427 # define in_addr_t int
428 # define inet_addr decc$inet_addr
432 __gnat_inet_pton (int af
, const char *src
, void *dst
) {
434 #if defined (_WIN32) && defined (AF_INET6)
440 errno
= EAFNOSUPPORT
;
444 #if defined (__vxworks)
445 return (inet_aton (src
, dst
) == OK
);
447 #elif defined (_WIN32)
448 struct sockaddr_storage ss
;
449 int sslen
= sizeof ss
;
453 rc
= WSAStringToAddressA (src
, af
, NULL
, (struct sockaddr
*)&ss
, &sslen
);
457 *(struct in_addr
*)dst
= ((struct sockaddr_in
*)&ss
)->sin_addr
;
461 *(struct in6_addr
*)dst
= ((struct sockaddr_in6
*)&ss
)->sin6_addr
;
468 #elif defined (__hpux__) || defined (VMS)
472 if (src
== NULL
|| dst
== NULL
) {
475 } else if (!strcmp (src
, "255.255.255.255")) {
480 addr
= inet_addr (src
);
481 rc
= (addr
!= 0xffffffff);
484 *(in_addr_t
*)dst
= addr
;
492 # warning Sockets are not supported on this platform
493 #endif /* defined(HAVE_SOCKETS) */