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 */
36 #if defined(HAVE_SOCKETS)
38 /* Include all the necessary system-specific headers and define the
39 * necessary macros (shared with gen-oscons).
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_last_socket_in_set (fd_set *, int *);
60 extern void __gnat_get_socket_from_set (fd_set *, int *, int *);
61 extern void __gnat_insert_socket_in_set (fd_set *, int);
62 extern int __gnat_is_socket_in_set (fd_set *, int);
63 extern fd_set *__gnat_new_socket_set (fd_set *);
64 extern void __gnat_remove_socket_from_set (fd_set *, int);
65 extern void __gnat_reset_socket_set (fd_set *);
66 extern int __gnat_get_h_errno (void);
67 #if defined (__vxworks) || defined (_WIN32)
68 extern int __gnat_inet_pton (int, const char *, void *);
71 /* Disable the sending of SIGPIPE for writes on a broken stream */
74 __gnat_disable_sigpipe (int fd)
78 (void) setsockopt (fd, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof val);
83 __gnat_disable_all_sigpipes (void)
85 #if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) && defined(SIGPIPE)
86 (void) signal (SIGPIPE, SIG_IGN);
90 #if defined (_WIN32) || defined (__vxworks) || defined (VMS)
92 * Signalling FDs operations are implemented in Ada for these platforms
93 * (see subunit GNAT.Sockets.Thin.Signalling_Fds).
97 * Create a pair of connected file descriptors fds[0] and fds[1] used for
98 * signalling by a Selector object. fds[0] is the read end, and fds[1] the
102 __gnat_create_signalling_fds (int *fds) {
107 * Read one byte of data from rsig, the read end of a pair of signalling fds
108 * created by __gnat_create_signalling_fds.
111 __gnat_read_signalling_fd (int rsig) {
113 return read (rsig, &c, 1);
117 * Write one byte of data to wsig, the write end of a pair of signalling fds
118 * created by __gnat_create_signalling_fds.
121 __gnat_write_signalling_fd (int wsig) {
123 return write (wsig, &c, 1);
127 * Close one end of a pair of signalling fds
130 __gnat_close_signalling_fd (int sig) {
136 * GetXXXbyYYY wrappers
137 * These functions are used by the default implementation of g-socthi,
138 * and also by the Windows version.
140 * They can be used for any platform that either provides an intrinsically
141 * task safe implementation of getXXXbyYYY, or a reentrant variant
142 * getXXXbyYYY_r. Otherwise, a task safe wrapper, including proper mutual
143 * exclusion if appropriate, must be implemented in the target specific
144 * version of g-socthi.
147 #ifdef HAVE_THREAD_SAFE_GETxxxBYyyy
149 __gnat_safe_gethostbyname (const char *name,
150 struct hostent *ret, char *buf, size_t buflen,
154 rh = gethostbyname (name);
165 __gnat_safe_gethostbyaddr (const char *addr, int len, int type,
166 struct hostent *ret, char *buf, size_t buflen,
170 rh = gethostbyaddr (addr, len, type);
181 __gnat_safe_getservbyname (const char *name, const char *proto,
182 struct servent *ret, char *buf, size_t buflen)
185 rh = getservbyname (name, proto);
193 __gnat_safe_getservbyport (int port, const char *proto,
194 struct servent *ret, char *buf, size_t buflen)
197 rh = getservbyport (port, proto);
203 #elif HAVE_GETxxxBYyyy_R
205 __gnat_safe_gethostbyname (const char *name,
206 struct hostent *ret, char *buf, size_t buflen,
212 #if defined(__linux__) || defined(__GLIBC__)
213 (void) gethostbyname_r (name, ret, buf, buflen, &rh, h_errnop);
215 rh = gethostbyname_r (name, ret, buf, buflen, h_errnop);
217 ri = (rh == NULL) ? -1 : 0;
222 __gnat_safe_gethostbyaddr (const char *addr, int len, int type,
223 struct hostent *ret, char *buf, size_t buflen,
229 #if defined(__linux__) || defined(__GLIBC__)
230 (void) gethostbyaddr_r (addr, len, type, ret, buf, buflen, &rh, h_errnop);
232 rh = gethostbyaddr_r (addr, len, type, ret, buf, buflen, h_errnop);
234 ri = (rh == NULL) ? -1 : 0;
239 __gnat_safe_getservbyname (const char *name, const char *proto,
240 struct servent *ret, char *buf, size_t buflen)
245 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
246 (void) getservbyname_r (name, proto, ret, buf, buflen, &rh);
248 rh = getservbyname_r (name, proto, ret, buf, buflen);
250 ri = (rh == NULL) ? -1 : 0;
255 __gnat_safe_getservbyport (int port, const char *proto,
256 struct servent *ret, char *buf, size_t buflen)
261 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
262 (void) getservbyport_r (port, proto, ret, buf, buflen, &rh);
264 rh = getservbyport_r (port, proto, ret, buf, buflen);
266 ri = (rh == NULL) ? -1 : 0;
271 /* Find the largest socket in the socket set SET. This is needed for
272 `select'. LAST is the maximum value for the largest socket. This hint is
273 used to avoid scanning very large socket sets. On return, LAST is the
274 actual largest socket in the socket set. */
277 __gnat_last_socket_in_set (fd_set *set, int *last)
284 /* More efficient method for NT. */
285 for (s = 0; s < set->fd_count; s++)
286 if ((int) set->fd_array[s] > l)
287 l = set->fd_array[s];
291 for (s = *last; s != -1; s--)
292 if (FD_ISSET (s, set))
302 /* Get last socket and remove it from the socket set SET. LAST is the
303 maximum value of the largest socket. This hint is used to avoid scanning
304 very large socket sets. On return, LAST is set to the actual largest
305 socket in the socket set. */
308 __gnat_get_socket_from_set (fd_set *set, int *last, int *socket)
311 FD_CLR (*socket, set);
312 __gnat_last_socket_in_set (set, last);
315 /* Insert SOCKET in the socket set SET. */
318 __gnat_insert_socket_in_set (fd_set *set, int socket)
320 FD_SET (socket, set);
323 /* Check whether a given SOCKET is in the socket set SET. */
326 __gnat_is_socket_in_set (fd_set *set, int socket)
328 return FD_ISSET (socket, set);
331 /* Remove SOCKET from the socket set SET. */
334 __gnat_remove_socket_from_set (fd_set *set, int socket)
336 FD_CLR (socket, set);
341 __gnat_reset_socket_set (fd_set *set)
346 /* Get the value of the last host error */
349 __gnat_get_h_errno (void) {
351 int vxw_errno = errno;
357 #ifdef S_hostLib_HOST_NOT_FOUND
358 case S_hostLib_HOST_NOT_FOUND:
360 case S_hostLib_UNKNOWN_HOST:
361 return HOST_NOT_FOUND;
363 #ifdef S_hostLib_TRY_AGAIN
364 case S_hostLib_TRY_AGAIN:
368 #ifdef S_hostLib_NO_RECOVERY
369 case S_hostLib_NO_RECOVERY:
371 #ifdef S_hostLib_NETDB_INTERNAL
372 case S_hostLib_NETDB_INTERNAL:
374 case S_hostLib_INVALID_PARAMETER:
382 /* h_errno is defined as follows in OpenVMS' version of <netdb.h>.
383 * However this header file is not available when building the GNAT
384 * runtime library using GCC, so we are hardcoding the definition
385 * directly. Note that the returned address is thread-specific.
387 extern int *decc$h_errno_get_addr ();
388 return *decc$h_errno_get_addr ();
390 #elif defined (__rtems__)
391 /* At this stage in the tool build, no networking .h files are available.
392 * Newlib does not provide networking .h files and RTEMS is not built yet.
393 * So we need to explicitly extern h_errno to access it.
403 #if defined (__vxworks) || defined (_WIN32)
405 __gnat_inet_pton (int af, const char *src, void *dst) {
407 #if defined (_WIN32) && defined (AF_INET6)
413 errno = EAFNOSUPPORT;
418 return (inet_aton (src, dst) == OK);
420 struct sockaddr_storage ss;
421 int sslen = sizeof ss;
425 rc = WSAStringToAddress (src, af, NULL, (struct sockaddr *)&ss, &sslen);
429 *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
433 *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
444 #warning Sockets are not supported on this platform
445 #endif /* defined(HAVE_SOCKETS) */