1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2003-2008, 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 2, 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. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License distributed with GNAT; see file COPYING. If not, write *
19 * to the Free Software Foundation, 51 Franklin Street, Fifth Floor, *
20 * Boston, MA 02110-1301, USA. *
22 * As a special exception, if you link this file with other files to *
23 * produce an executable, this file does not by itself cause the resulting *
24 * executable to be covered by the GNU General Public License. This except- *
25 * ion does not however invalidate any other reasons why the executable *
26 * file might be covered by the GNU Public License. *
28 * GNAT was originally developed by the GNAT team at New York University. *
29 * Extensive contributions were provided by Ada Core Technologies Inc. *
31 ****************************************************************************/
33 /* This file provides a portable binding to the sockets API */
37 #if defined(HAVE_SOCKETS)
39 /* Include all the necessary system-specific headers and define the
40 * necessary macros (shared with gen-oscons).
43 #if !defined(SO_NOSIGPIPE) && !defined (MSG_NOSIGNAL)
46 /* Required if we will be calling signal() in __gnat_disable_all_sigpipes() */
49 /* Required for __gnat_malloc() */
52 /* Required for memcpy() */
54 extern void __gnat_disable_sigpipe (int fd
);
55 extern void __gnat_disable_all_sigpipes (void);
56 extern int __gnat_create_signalling_fds (int *fds
);
57 extern int __gnat_read_signalling_fd (int rsig
);
58 extern int __gnat_write_signalling_fd (int wsig
);
59 extern void __gnat_close_signalling_fd (int sig
);
60 extern void __gnat_free_socket_set (fd_set
*);
61 extern void __gnat_last_socket_in_set (fd_set
*, int *);
62 extern void __gnat_get_socket_from_set (fd_set
*, int *, int *);
63 extern void __gnat_insert_socket_in_set (fd_set
*, int);
64 extern int __gnat_is_socket_in_set (fd_set
*, int);
65 extern fd_set
*__gnat_new_socket_set (fd_set
*);
66 extern void __gnat_remove_socket_from_set (fd_set
*, int);
67 extern int __gnat_get_h_errno (void);
69 /* Disable the sending of SIGPIPE for writes on a broken stream */
72 __gnat_disable_sigpipe (int fd
)
76 (void) setsockopt (fd
, SOL_SOCKET
, SO_NOSIGPIPE
, &val
, sizeof val
);
81 __gnat_disable_all_sigpipes (void)
83 #if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) && defined(SIGPIPE)
84 (void) signal (SIGPIPE
, SIG_IGN
);
88 #if defined (_WIN32) || defined (__vxworks) || defined (VMS)
90 * Signalling FDs operations are implemented in Ada for these platforms
91 * (see subunit GNAT.Sockets.Thin.Signalling_Fds).
95 * Create a pair of connected file descriptors fds[0] and fds[1] used for
96 * signalling by a Selector object. fds[0] is the read end, and fds[1] the
100 __gnat_create_signalling_fds (int *fds
) {
105 * Read one byte of data from rsig, the read end of a pair of signalling fds
106 * created by __gnat_create_signalling_fds.
109 __gnat_read_signalling_fd (int rsig
) {
111 return read (rsig
, &c
, 1);
115 * Write one byte of data to wsig, the write end of a pair of signalling fds
116 * created by __gnat_create_signalling_fds.
119 __gnat_write_signalling_fd (int wsig
) {
121 return write (wsig
, &c
, 1);
125 * Close one end of a pair of signalling fds
128 __gnat_close_signalling_fd (int sig
) {
134 * GetXXXbyYYY wrappers
135 * These functions are used by the default implementation of g-socthi,
136 * and also by the Windows version.
138 * They can be used for any platform that either provides an intrinsically
139 * task safe implementation of getXXXbyYYY, or a reentrant variant
140 * getXXXbyYYY_r. Otherwise, a task safe wrapper, including proper mutual
141 * exclusion if appropriate, must be implemented in the target specific
142 * version of g-socthi.
145 #ifdef HAVE_THREAD_SAFE_GETxxxBYyyy
147 __gnat_safe_gethostbyname (const char *name
,
148 struct hostent
*ret
, char *buf
, size_t buflen
,
152 rh
= gethostbyname (name
);
163 __gnat_safe_gethostbyaddr (const char *addr
, int len
, int type
,
164 struct hostent
*ret
, char *buf
, size_t buflen
,
168 rh
= gethostbyaddr (addr
, len
, type
);
179 __gnat_safe_getservbyname (const char *name
, const char *proto
,
180 struct servent
*ret
, char *buf
, size_t buflen
)
183 rh
= getservbyname (name
, proto
);
191 __gnat_safe_getservbyport (int port
, const char *proto
,
192 struct servent
*ret
, char *buf
, size_t buflen
)
195 rh
= getservbyport (port
, proto
);
201 #elif HAVE_GETxxxBYyyy_R
203 __gnat_safe_gethostbyname (const char *name
,
204 struct hostent
*ret
, char *buf
, size_t buflen
,
210 #if defined(__linux__) || defined(__GLIBC__)
211 (void) gethostbyname_r (name
, ret
, buf
, buflen
, &rh
, h_errnop
);
213 rh
= gethostbyname_r (name
, ret
, buf
, buflen
, h_errnop
);
215 ri
= (rh
== NULL
) ? -1 : 0;
220 __gnat_safe_gethostbyaddr (const char *addr
, int len
, int type
,
221 struct hostent
*ret
, char *buf
, size_t buflen
,
227 #if defined(__linux__) || defined(__GLIBC__)
228 (void) gethostbyaddr_r (addr
, len
, type
, ret
, buf
, buflen
, &rh
, h_errnop
);
230 rh
= gethostbyaddr_r (addr
, len
, type
, ret
, buf
, buflen
, h_errnop
);
232 ri
= (rh
== NULL
) ? -1 : 0;
237 __gnat_safe_getservbyname (const char *name
, const char *proto
,
238 struct servent
*ret
, char *buf
, size_t buflen
)
243 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
244 (void) getservbyname_r (name
, proto
, ret
, buf
, buflen
, &rh
);
246 rh
= getservbyname_r (name
, proto
, ret
, buf
, buflen
);
248 ri
= (rh
== NULL
) ? -1 : 0;
253 __gnat_safe_getservbyport (int port
, const char *proto
,
254 struct servent
*ret
, char *buf
, size_t buflen
)
259 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
260 (void) getservbyport_r (port
, proto
, ret
, buf
, buflen
, &rh
);
262 rh
= getservbyport_r (port
, proto
, ret
, buf
, buflen
);
264 ri
= (rh
== NULL
) ? -1 : 0;
269 /* Free socket set. */
272 __gnat_free_socket_set (fd_set
*set
)
277 /* Find the largest socket in the socket set SET. This is needed for
278 `select'. LAST is the maximum value for the largest socket. This hint is
279 used to avoid scanning very large socket sets. On return, LAST is the
280 actual largest socket in the socket set. */
283 __gnat_last_socket_in_set (fd_set
*set
, int *last
)
290 /* More efficient method for NT. */
291 for (s
= 0; s
< set
->fd_count
; s
++)
292 if ((int) set
->fd_array
[s
] > l
)
293 l
= set
->fd_array
[s
];
297 for (s
= *last
; s
!= -1; s
--)
298 if (FD_ISSET (s
, set
))
308 /* Get last socket and remove it from the socket set SET. LAST is the
309 maximum value of the largest socket. This hint is used to avoid scanning
310 very large socket sets. On return, LAST is set to the actual largest
311 socket in the socket set. */
314 __gnat_get_socket_from_set (fd_set
*set
, int *last
, int *socket
)
317 FD_CLR (*socket
, set
);
318 __gnat_last_socket_in_set (set
, last
);
321 /* Insert SOCKET in the socket set SET. */
324 __gnat_insert_socket_in_set (fd_set
*set
, int socket
)
326 FD_SET (socket
, set
);
329 /* Check whether a given SOCKET is in the socket set SET. */
332 __gnat_is_socket_in_set (fd_set
*set
, int socket
)
334 return FD_ISSET (socket
, set
);
337 /* Allocate a new socket set and set it as empty. */
340 __gnat_new_socket_set (fd_set
*set
)
345 extern void *__gnat_malloc32 (__SIZE_TYPE__
);
346 new = (fd_set
*) __gnat_malloc32 (sizeof (fd_set
));
348 new = (fd_set
*) __gnat_malloc (sizeof (fd_set
));
352 memcpy (new, set
, sizeof (fd_set
));
359 /* Remove SOCKET from the socket set SET. */
362 __gnat_remove_socket_from_set (fd_set
*set
, int socket
)
364 FD_CLR (socket
, set
);
367 /* Get the value of the last host error */
370 __gnat_get_h_errno (void) {
372 int vxw_errno
= errno
;
378 case S_resolvLib_HOST_NOT_FOUND
:
379 case S_hostLib_UNKNOWN_HOST
:
380 return HOST_NOT_FOUND
;
382 case S_resolvLib_TRY_AGAIN
:
385 case S_resolvLib_NO_RECOVERY
:
386 case S_resolvLib_BUFFER_2_SMALL
:
387 case S_resolvLib_INVALID_PARAMETER
:
388 case S_resolvLib_INVALID_ADDRESS
:
389 case S_hostLib_INVALID_PARAMETER
:
392 case S_resolvLib_NO_DATA
:
400 /* h_errno is defined as follows in OpenVMS' version of <netdb.h>.
401 * However this header file is not available when building the GNAT
402 * runtime library using GCC, so we are hardcoding the definition
403 * directly. Note that the returned address is thread-specific.
405 extern int *decc$
h_errno_get_addr ();
406 return *decc$
h_errno_get_addr ();
408 #elif defined (__rtems__)
409 /* At this stage in the tool build, no networking .h files are available.
410 * Newlib does not provide networking .h files and RTEMS is not built yet.
411 * So we need to explicitly extern h_errno to access it.
422 #warning Sockets are not supported on this platform
423 #endif /* defined(HAVE_SOCKETS) */