1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2003-2005 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 */
36 /* Include all the necessary system-specific headers and define the
37 necessary macros (shared with gen-soccon). */
40 /* Required for __gnat_malloc() */
43 /* Required for memcpy() */
45 extern void __gnat_disable_sigpipe (int fd
);
46 extern void __gnat_free_socket_set (fd_set
*);
47 extern void __gnat_last_socket_in_set (fd_set
*, int *);
48 extern void __gnat_get_socket_from_set (fd_set
*, int *, int *);
49 extern void __gnat_insert_socket_in_set (fd_set
*, int);
50 extern int __gnat_is_socket_in_set (fd_set
*, int);
51 extern fd_set
*__gnat_new_socket_set (fd_set
*);
52 extern void __gnat_remove_socket_from_set (fd_set
*, int);
53 extern int __gnat_get_h_errno (void);
55 /* Disable the sending of SIGPIPE for writes on a broken stream */
58 __gnat_disable_sigpipe (int fd
)
62 (void) setsockopt (fd
, SOL_SOCKET
, SO_NOSIGPIPE
, &val
, sizeof val
);
66 /* Free socket set. */
69 __gnat_free_socket_set (fd_set
*set
)
74 /* Find the largest socket in the socket set SET. This is needed for
75 `select'. LAST is the maximum value for the largest socket. This hint is
76 used to avoid scanning very large socket sets. On return, LAST is the
77 actual largest socket in the socket set. */
80 __gnat_last_socket_in_set (fd_set
*set
, int *last
)
87 /* More efficient method for NT. */
88 for (s
= 0; s
< set
->fd_count
; s
++)
89 if ((int) set
->fd_array
[s
] > l
)
94 for (s
= *last
; s
!= -1; s
--)
95 if (FD_ISSET (s
, set
))
105 /* Get last socket and remove it from the socket set SET. LAST is the
106 maximum value of the largest socket. This hint is used to avoid scanning
107 very large socket sets. On return, LAST is set to the actual largest
108 socket in the socket set. */
111 __gnat_get_socket_from_set (fd_set
*set
, int *last
, int *socket
)
114 FD_CLR (*socket
, set
);
115 __gnat_last_socket_in_set (set
, last
);
118 /* Insert SOCKET in the socket set SET. */
121 __gnat_insert_socket_in_set (fd_set
*set
, int socket
)
123 FD_SET (socket
, set
);
126 /* Check whether a given SOCKET is in the socket set SET. */
129 __gnat_is_socket_in_set (fd_set
*set
, int socket
)
131 return FD_ISSET (socket
, set
);
134 /* Allocate a new socket set and set it as empty. */
137 __gnat_new_socket_set (fd_set
*set
)
141 new = (fd_set
*) __gnat_malloc (sizeof (fd_set
));
144 memcpy (new, set
, sizeof (fd_set
));
151 /* Remove SOCKET from the socket set SET. */
154 __gnat_remove_socket_from_set (fd_set
*set
, int socket
)
156 FD_CLR (socket
, set
);
159 /* Get the value of the last host error */
162 __gnat_get_h_errno (void) {
164 int vxw_errno
= errno
;
170 case S_resolvLib_HOST_NOT_FOUND
:
171 case S_hostLib_UNKNOWN_HOST
:
172 return HOST_NOT_FOUND
;
174 case S_resolvLib_TRY_AGAIN
:
177 case S_resolvLib_NO_RECOVERY
:
178 case S_resolvLib_BUFFER_2_SMALL
:
179 case S_resolvLib_INVALID_PARAMETER
:
180 case S_resolvLib_INVALID_ADDRESS
:
181 case S_hostLib_INVALID_PARAMETER
:
184 case S_resolvLib_NO_DATA
:
192 #elif defined(__rtems__)
193 /* At this stage in the tool build, no networking .h files are available.
194 Newlib does not provide networking .h files and RTEMS is not built yet.
195 So we need to explicitly extern h_errno to access it.