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 */
34 #if defined (__nucleus__)
35 /* ??? Need proper implementation */
36 #warning Sockets not yet supported on Nucleus
39 /* Include all the necessary system-specific headers and define the
40 necessary macros (shared with gen-soccon). */
42 #if !defined(SO_NOSIGPIPE) && !defined (MSG_NOSIGNAL)
45 /* Required if we will be calling signal() in __gnat_disable_all_sigpipes() */
48 /* Required for __gnat_malloc() */
51 /* Required for memcpy() */
53 extern void __gnat_disable_sigpipe (int fd
);
54 extern void __gnat_disable_all_sigpipes (void);
55 extern int __gnat_create_signalling_fds (int *fds
);
56 extern int __gnat_read_signalling_fd (int rsig
);
57 extern int __gnat_write_signalling_fd (int wsig
);
58 extern void __gnat_close_signalling_fd (int sig
);
59 extern void __gnat_free_socket_set (fd_set
*);
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 int __gnat_get_h_errno (void);
68 /* Disable the sending of SIGPIPE for writes on a broken stream */
71 __gnat_disable_sigpipe (int fd
)
75 (void) setsockopt (fd
, SOL_SOCKET
, SO_NOSIGPIPE
, &val
, sizeof val
);
80 __gnat_disable_all_sigpipes (void)
82 #if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) && defined(SIGPIPE)
83 (void) signal (SIGPIPE
, SIG_IGN
);
87 #if defined (_WIN32) || defined (__vxworks) || defined (VMS)
89 * Signalling FDs operations are implemented in Ada for these platforms
90 * (see subunit GNAT.Sockets.Thin.Signalling_Fds).
94 * Create a pair of connected file descriptors fds[0] and fds[1] used for
95 * signalling by a Selector object. fds[0] is the read end, and fds[1] the
99 __gnat_create_signalling_fds (int *fds
) {
104 * Read one byte of data from rsig, the read end of a pair of signalling fds
105 * created by __gnat_create_signalling_fds.
108 __gnat_read_signalling_fd (int rsig
) {
110 return read (rsig
, &c
, 1);
114 * Write one byte of data to wsig, the write end of a pair of signalling fds
115 * created by __gnat_create_signalling_fds.
118 __gnat_write_signalling_fd (int wsig
) {
120 return write (wsig
, &c
, 1);
124 * Close one end of a pair of signalling fds
127 __gnat_close_signalling_fd (int sig
) {
133 * GetXXXbyYYY wrappers
134 * These functions are used by the default implementation of g-socthi,
135 * and also by the Windows version.
137 * They can be used for any platform that either provides an intrinsically
138 * task safe implementation of getXXXbyYYY, or a reentrant variant
139 * getXXXbyYYY_r. Otherwise, a task safe wrapper, including proper mutual
140 * exclusion if appropriate, must be implemented in the target specific
141 * version of g-socthi.
144 #ifdef HAVE_THREAD_SAFE_GETxxxBYyyy
146 __gnat_safe_gethostbyname (const char *name
,
147 struct hostent
*ret
, char *buf
, size_t buflen
,
151 rh
= gethostbyname (name
);
162 __gnat_safe_gethostbyaddr (const char *addr
, int len
, int type
,
163 struct hostent
*ret
, char *buf
, size_t buflen
,
167 rh
= gethostbyaddr (addr
, len
, type
);
178 __gnat_safe_getservbyname (const char *name
, const char *proto
,
179 struct servent
*ret
, char *buf
, size_t buflen
)
182 rh
= getservbyname (name
, proto
);
190 __gnat_safe_getservbyport (int port
, const char *proto
,
191 struct servent
*ret
, char *buf
, size_t buflen
)
194 rh
= getservbyport (port
, proto
);
200 #elif HAVE_GETxxxBYyyy_R
202 __gnat_safe_gethostbyname (const char *name
,
203 struct hostent
*ret
, char *buf
, size_t buflen
,
209 #if defined(__linux__) || defined(__GLIBC__)
210 (void) gethostbyname_r (name
, ret
, buf
, buflen
, &rh
, h_errnop
);
212 rh
= gethostbyname_r (name
, ret
, buf
, buflen
, h_errnop
);
214 ri
= (rh
== NULL
) ? -1 : 0;
219 __gnat_safe_gethostbyaddr (const char *addr
, int len
, int type
,
220 struct hostent
*ret
, char *buf
, size_t buflen
,
226 #if defined(__linux__) || defined(__GLIBC__)
227 (void) gethostbyaddr_r (addr
, len
, type
, ret
, buf
, buflen
, &rh
, h_errnop
);
229 rh
= gethostbyaddr_r (addr
, len
, type
, ret
, buf
, buflen
, h_errnop
);
231 ri
= (rh
== NULL
) ? -1 : 0;
236 __gnat_safe_getservbyname (const char *name
, const char *proto
,
237 struct servent
*ret
, char *buf
, size_t buflen
)
242 #if defined(__linux__) || defined(__GLIBC__)
243 (void) getservbyname_r (name
, proto
, ret
, buf
, buflen
, &rh
);
245 rh
= getservbyname_r (name
, proto
, ret
, buf
, buflen
);
247 ri
= (rh
== NULL
) ? -1 : 0;
252 __gnat_safe_getservbyport (int port
, const char *proto
,
253 struct servent
*ret
, char *buf
, size_t buflen
)
258 #if defined(__linux__) || defined(__GLIBC__)
259 (void) getservbyport_r (port
, proto
, ret
, buf
, buflen
, &rh
);
261 rh
= getservbyport_r (port
, proto
, ret
, buf
, buflen
);
263 ri
= (rh
== NULL
) ? -1 : 0;
268 /* Free socket set. */
271 __gnat_free_socket_set (fd_set
*set
)
276 /* Find the largest socket in the socket set SET. This is needed for
277 `select'. LAST is the maximum value for the largest socket. This hint is
278 used to avoid scanning very large socket sets. On return, LAST is the
279 actual largest socket in the socket set. */
282 __gnat_last_socket_in_set (fd_set
*set
, int *last
)
289 /* More efficient method for NT. */
290 for (s
= 0; s
< set
->fd_count
; s
++)
291 if ((int) set
->fd_array
[s
] > l
)
292 l
= set
->fd_array
[s
];
296 for (s
= *last
; s
!= -1; s
--)
297 if (FD_ISSET (s
, set
))
307 /* Get last socket and remove it from the socket set SET. LAST is the
308 maximum value of the largest socket. This hint is used to avoid scanning
309 very large socket sets. On return, LAST is set to the actual largest
310 socket in the socket set. */
313 __gnat_get_socket_from_set (fd_set
*set
, int *last
, int *socket
)
316 FD_CLR (*socket
, set
);
317 __gnat_last_socket_in_set (set
, last
);
320 /* Insert SOCKET in the socket set SET. */
323 __gnat_insert_socket_in_set (fd_set
*set
, int socket
)
325 FD_SET (socket
, set
);
328 /* Check whether a given SOCKET is in the socket set SET. */
331 __gnat_is_socket_in_set (fd_set
*set
, int socket
)
333 return FD_ISSET (socket
, set
);
336 /* Allocate a new socket set and set it as empty. */
339 __gnat_new_socket_set (fd_set
*set
)
344 extern void *__gnat_malloc32 (__SIZE_TYPE__
);
345 new = (fd_set
*) __gnat_malloc32 (sizeof (fd_set
));
347 new = (fd_set
*) __gnat_malloc (sizeof (fd_set
));
351 memcpy (new, set
, sizeof (fd_set
));
358 /* Remove SOCKET from the socket set SET. */
361 __gnat_remove_socket_from_set (fd_set
*set
, int socket
)
363 FD_CLR (socket
, set
);
366 /* Get the value of the last host error */
369 __gnat_get_h_errno (void) {
371 int vxw_errno
= errno
;
377 case S_resolvLib_HOST_NOT_FOUND
:
378 case S_hostLib_UNKNOWN_HOST
:
379 return HOST_NOT_FOUND
;
381 case S_resolvLib_TRY_AGAIN
:
384 case S_resolvLib_NO_RECOVERY
:
385 case S_resolvLib_BUFFER_2_SMALL
:
386 case S_resolvLib_INVALID_PARAMETER
:
387 case S_resolvLib_INVALID_ADDRESS
:
388 case S_hostLib_INVALID_PARAMETER
:
391 case S_resolvLib_NO_DATA
:
399 /* h_errno is defined as follows in OpenVMS' version of <netdb.h>.
400 * However this header file is not available when building the GNAT
401 * runtime library using GCC, so we are hardcoding the definition
402 * directly. Note that the returned address is thread-specific.
404 extern int *decc$
h_errno_get_addr ();
405 return *decc$
h_errno_get_addr ();
407 #elif defined (__rtems__)
408 /* At this stage in the tool build, no networking .h files are available.
409 * Newlib does not provide networking .h files and RTEMS is not built yet.
410 * So we need to explicitly extern h_errno to access it.
419 #endif /* __nucleus__ */