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 a DEC C text library
39 * (DECC$RTLDEF.TLB) not accessable to GCC). So, we generate a separate header
40 * file along with s-oscons.ads and include it here.
42 # include "s-oscons.h"
45 * We also need the declaration of struct servent, which s-oscons can't
46 * provide, so we copy it manually here. This needs to be kept in synch
47 * with the definition of that structure in the DEC C headers, which
48 * hopefully won't change frequently.
51 char *s_name; /* official service name */
52 char **s_aliases; /* alias list */
53 int s_port; /* port # */
54 char *s_proto; /* protocol to use */
58 #if defined(HAVE_SOCKETS)
60 /* Include all the necessary system-specific headers and define the
61 * necessary macros (shared with gen-oscons).
64 #if !defined(SO_NOSIGPIPE) && !defined (MSG_NOSIGNAL)
67 /* Required if we will be calling signal() in __gnat_disable_all_sigpipes() */
70 /* Required for __gnat_malloc() */
73 /* Required for memcpy() */
75 extern void __gnat_disable_sigpipe (int fd);
76 extern void __gnat_disable_all_sigpipes (void);
77 extern int __gnat_create_signalling_fds (int *fds);
78 extern int __gnat_read_signalling_fd (int rsig);
79 extern int __gnat_write_signalling_fd (int wsig);
80 extern void __gnat_close_signalling_fd (int sig);
81 extern void __gnat_last_socket_in_set (fd_set *, int *);
82 extern void __gnat_get_socket_from_set (fd_set *, int *, int *);
83 extern void __gnat_insert_socket_in_set (fd_set *, int);
84 extern int __gnat_is_socket_in_set (fd_set *, int);
85 extern fd_set *__gnat_new_socket_set (fd_set *);
86 extern void __gnat_remove_socket_from_set (fd_set *, int);
87 extern void __gnat_reset_socket_set (fd_set *);
88 extern int __gnat_get_h_errno (void);
89 extern int __gnat_socket_ioctl (int, int, int *);
90 extern char * __gnat_servent_s_name (struct servent *);
91 extern char ** __gnat_servent_s_aliases (struct servent *);
92 extern int __gnat_servent_s_port (struct servent *);
93 extern char * __gnat_servent_s_proto (struct servent *);
94 extern void __gnat_servent_set_s_name (struct servent *, char *);
95 extern void __gnat_servent_set_s_aliases (struct servent *, char **);
96 extern void __gnat_servent_set_s_port (struct servent *, int);
97 extern void __gnat_servent_set_s_proto (struct servent *, char *);
98 #if defined (__vxworks) || defined (_WIN32)
99 extern int __gnat_inet_pton (int, const char *, void *);
102 /* Disable the sending of SIGPIPE for writes on a broken stream */
105 __gnat_disable_sigpipe (int fd)
109 (void) setsockopt (fd, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof val);
114 __gnat_disable_all_sigpipes (void)
116 #if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) && defined(SIGPIPE)
117 (void) signal (SIGPIPE, SIG_IGN);
121 #if defined (_WIN32) || defined (__vxworks) || defined (VMS)
123 * Signalling FDs operations are implemented in Ada for these platforms
124 * (see subunit GNAT.Sockets.Thin.Signalling_Fds).
128 * Create a pair of connected file descriptors fds[0] and fds[1] used for
129 * signalling by a Selector object. fds[0] is the read end, and fds[1] the
133 __gnat_create_signalling_fds (int *fds) {
138 * Read one byte of data from rsig, the read end of a pair of signalling fds
139 * created by __gnat_create_signalling_fds.
142 __gnat_read_signalling_fd (int rsig) {
144 return read (rsig, &c, 1);
148 * Write one byte of data to wsig, the write end of a pair of signalling fds
149 * created by __gnat_create_signalling_fds.
152 __gnat_write_signalling_fd (int wsig) {
154 return write (wsig, &c, 1);
158 * Close one end of a pair of signalling fds
161 __gnat_close_signalling_fd (int sig) {
167 * GetXXXbyYYY wrappers
168 * These functions are used by the default implementation of g-socthi,
169 * and also by the Windows version.
171 * They can be used for any platform that either provides an intrinsically
172 * task safe implementation of getXXXbyYYY, or a reentrant variant
173 * getXXXbyYYY_r. Otherwise, a task safe wrapper, including proper mutual
174 * exclusion if appropriate, must be implemented in the target specific
175 * version of g-socthi.
178 #ifdef HAVE_THREAD_SAFE_GETxxxBYyyy
180 __gnat_safe_gethostbyname (const char *name,
181 struct hostent *ret, char *buf, size_t buflen,
185 rh = gethostbyname (name);
196 __gnat_safe_gethostbyaddr (const char *addr, int len, int type,
197 struct hostent *ret, char *buf, size_t buflen,
201 rh = gethostbyaddr (addr, len, type);
212 __gnat_safe_getservbyname (const char *name, const char *proto,
213 struct servent *ret, char *buf, size_t buflen)
216 rh = getservbyname (name, proto);
224 __gnat_safe_getservbyport (int port, const char *proto,
225 struct servent *ret, char *buf, size_t buflen)
228 rh = getservbyport (port, proto);
234 #elif HAVE_GETxxxBYyyy_R
236 __gnat_safe_gethostbyname (const char *name,
237 struct hostent *ret, char *buf, size_t buflen,
243 #if defined(__linux__) || defined(__GLIBC__)
244 (void) gethostbyname_r (name, ret, buf, buflen, &rh, h_errnop);
246 rh = gethostbyname_r (name, ret, buf, buflen, h_errnop);
248 ri = (rh == NULL) ? -1 : 0;
253 __gnat_safe_gethostbyaddr (const char *addr, int len, int type,
254 struct hostent *ret, char *buf, size_t buflen,
260 #if defined(__linux__) || defined(__GLIBC__)
261 (void) gethostbyaddr_r (addr, len, type, ret, buf, buflen, &rh, h_errnop);
263 rh = gethostbyaddr_r (addr, len, type, ret, buf, buflen, h_errnop);
265 ri = (rh == NULL) ? -1 : 0;
270 __gnat_safe_getservbyname (const char *name, const char *proto,
271 struct servent *ret, char *buf, size_t buflen)
276 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
277 (void) getservbyname_r (name, proto, ret, buf, buflen, &rh);
279 rh = getservbyname_r (name, proto, ret, buf, buflen);
281 ri = (rh == NULL) ? -1 : 0;
286 __gnat_safe_getservbyport (int port, const char *proto,
287 struct servent *ret, char *buf, size_t buflen)
292 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
293 (void) getservbyport_r (port, proto, ret, buf, buflen, &rh);
295 rh = getservbyport_r (port, proto, ret, buf, buflen);
297 ri = (rh == NULL) ? -1 : 0;
302 /* Find the largest socket in the socket set SET. This is needed for
303 `select'. LAST is the maximum value for the largest socket. This hint is
304 used to avoid scanning very large socket sets. On return, LAST is the
305 actual largest socket in the socket set. */
308 __gnat_last_socket_in_set (fd_set *set, int *last)
315 /* More efficient method for NT. */
316 for (s = 0; s < set->fd_count; s++)
317 if ((int) set->fd_array[s] > l)
318 l = set->fd_array[s];
322 for (s = *last; s != -1; s--)
323 if (FD_ISSET (s, set))
333 /* Get last socket and remove it from the socket set SET. LAST is the
334 maximum value of the largest socket. This hint is used to avoid scanning
335 very large socket sets. On return, LAST is set to the actual largest
336 socket in the socket set. */
339 __gnat_get_socket_from_set (fd_set *set, int *last, int *socket)
342 FD_CLR (*socket, set);
343 __gnat_last_socket_in_set (set, last);
346 /* Insert SOCKET in the socket set SET. */
349 __gnat_insert_socket_in_set (fd_set *set, int socket)
351 FD_SET (socket, set);
354 /* Check whether a given SOCKET is in the socket set SET. */
357 __gnat_is_socket_in_set (fd_set *set, int socket)
359 return FD_ISSET (socket, set);
362 /* Remove SOCKET from the socket set SET. */
365 __gnat_remove_socket_from_set (fd_set *set, int socket)
367 FD_CLR (socket, set);
372 __gnat_reset_socket_set (fd_set *set)
377 /* Get the value of the last host error */
380 __gnat_get_h_errno (void) {
382 int vxw_errno = errno;
388 #ifdef S_hostLib_HOST_NOT_FOUND
389 case S_hostLib_HOST_NOT_FOUND:
391 case S_hostLib_UNKNOWN_HOST:
392 return HOST_NOT_FOUND;
394 #ifdef S_hostLib_TRY_AGAIN
395 case S_hostLib_TRY_AGAIN:
399 #ifdef S_hostLib_NO_RECOVERY
400 case S_hostLib_NO_RECOVERY:
402 #ifdef S_hostLib_NETDB_INTERNAL
403 case S_hostLib_NETDB_INTERNAL:
405 case S_hostLib_INVALID_PARAMETER:
413 /* h_errno is defined as follows in OpenVMS' version of <netdb.h>.
414 * However this header file is not available when building the GNAT
415 * runtime library using GCC, so we are hardcoding the definition
416 * directly. Note that the returned address is thread-specific.
418 extern int *decc$h_errno_get_addr ();
419 return *decc$h_errno_get_addr ();
421 #elif defined (__rtems__)
422 /* At this stage in the tool build, no networking .h files are available.
423 * Newlib does not provide networking .h files and RTEMS is not built yet.
424 * So we need to explicitly extern h_errno to access it.
434 /* Wrapper for ioctl(2), which is a variadic function */
437 __gnat_socket_ioctl (int fd, int req, int *arg) {
439 return ioctlsocket (fd, req, arg);
441 return ioctl (fd, req, arg);
445 #ifndef HAVE_INET_PTON
448 # define in_addr_t int
449 # define inet_addr decc$inet_addr
453 __gnat_inet_pton (int af, const char *src, void *dst) {
455 #if defined (_WIN32) && defined (AF_INET6)
461 errno = EAFNOSUPPORT;
465 #if defined (__vxworks)
466 return (inet_aton (src, dst) == OK);
468 #elif defined (_WIN32)
469 struct sockaddr_storage ss;
470 int sslen = sizeof ss;
474 rc = WSAStringToAddressA (src, af, NULL, (struct sockaddr *)&ss, &sslen);
478 *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
482 *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
489 #elif defined (__hpux__) || defined (VMS)
493 if (src == NULL || dst == NULL) {
496 } else if (!strcmp (src, "255.255.255.255")) {
501 addr = inet_addr (src);
502 rc = (addr != 0xffffffff);
505 *(in_addr_t *)dst = addr;
513 * Accessor functions for struct servent.
515 * These are needed because servent has different representations on different
516 * platforms, and we don't want to deal with that on the Ada side. For example,
517 * on Linux, we have (see /usr/include netdb.h):
527 * and on Windows (see mingw's socket.h):
545 __gnat_servent_s_name (struct servent * s)
551 __gnat_servent_s_aliases (struct servent * s)
557 __gnat_servent_s_port (struct servent * s)
563 __gnat_servent_s_proto (struct servent * s)
571 __gnat_servent_set_s_name (struct servent * s, char * s_name)
577 __gnat_servent_set_s_aliases (struct servent * s, char ** s_aliases)
579 s->s_aliases = s_aliases;
583 __gnat_servent_set_s_port (struct servent * s, int s_port)
589 __gnat_servent_set_s_proto (struct servent * s, char * s_proto)
591 s->s_proto = s_proto;
595 # warning Sockets are not supported on this platform
596 #endif /* defined(HAVE_SOCKETS) */