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_last_socket_in_set (fd_set *, int *);
61 extern void __gnat_get_socket_from_set (fd_set *, int *, int *);
62 extern void __gnat_insert_socket_in_set (fd_set *, int);
63 extern int __gnat_is_socket_in_set (fd_set *, int);
64 extern fd_set *__gnat_new_socket_set (fd_set *);
65 extern void __gnat_remove_socket_from_set (fd_set *, int);
66 extern void __gnat_reset_socket_set (fd_set *set);
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 /* Find the largest socket in the socket set SET. This is needed for
270 `select'. LAST is the maximum value for the largest socket. This hint is
271 used to avoid scanning very large socket sets. On return, LAST is the
272 actual largest socket in the socket set. */
275 __gnat_last_socket_in_set (fd_set *set, int *last)
282 /* More efficient method for NT. */
283 for (s = 0; s < set->fd_count; s++)
284 if ((int) set->fd_array[s] > l)
285 l = set->fd_array[s];
289 for (s = *last; s != -1; s--)
290 if (FD_ISSET (s, set))
300 /* Get last socket and remove it from the socket set SET. LAST is the
301 maximum value of the largest socket. This hint is used to avoid scanning
302 very large socket sets. On return, LAST is set to the actual largest
303 socket in the socket set. */
306 __gnat_get_socket_from_set (fd_set *set, int *last, int *socket)
309 FD_CLR (*socket, set);
310 __gnat_last_socket_in_set (set, last);
313 /* Insert SOCKET in the socket set SET. */
316 __gnat_insert_socket_in_set (fd_set *set, int socket)
318 FD_SET (socket, set);
321 /* Check whether a given SOCKET is in the socket set SET. */
324 __gnat_is_socket_in_set (fd_set *set, int socket)
326 return FD_ISSET (socket, set);
329 /* Remove SOCKET from the socket set SET. */
332 __gnat_remove_socket_from_set (fd_set *set, int socket)
334 FD_CLR (socket, set);
339 __gnat_reset_socket_set (fd_set *set)
344 /* Get the value of the last host error */
347 __gnat_get_h_errno (void) {
349 int vxw_errno = errno;
355 case S_resolvLib_HOST_NOT_FOUND:
356 case S_hostLib_UNKNOWN_HOST:
357 return HOST_NOT_FOUND;
359 case S_resolvLib_TRY_AGAIN:
362 case S_resolvLib_NO_RECOVERY:
363 case S_resolvLib_BUFFER_2_SMALL:
364 case S_resolvLib_INVALID_PARAMETER:
365 case S_resolvLib_INVALID_ADDRESS:
366 case S_hostLib_INVALID_PARAMETER:
369 case S_resolvLib_NO_DATA:
377 /* h_errno is defined as follows in OpenVMS' version of <netdb.h>.
378 * However this header file is not available when building the GNAT
379 * runtime library using GCC, so we are hardcoding the definition
380 * directly. Note that the returned address is thread-specific.
382 extern int *decc$h_errno_get_addr ();
383 return *decc$h_errno_get_addr ();
385 #elif defined (__rtems__)
386 /* At this stage in the tool build, no networking .h files are available.
387 * Newlib does not provide networking .h files and RTEMS is not built yet.
388 * So we need to explicitly extern h_errno to access it.
399 #warning Sockets are not supported on this platform
400 #endif /* defined(HAVE_SOCKETS) */