Makefile.in: Rebuilt.
[official-gcc.git] / gcc / ada / socket.c
blobbb79ac30a72f155f27b858d8875555cecfd7ff87
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * S O C K E T *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 2003-2005 Free Software Foundation, Inc. *
10 * *
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. *
21 * *
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. *
27 * *
28 * GNAT was originally developed by the GNAT team at New York University. *
29 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 * *
31 ****************************************************************************/
33 /* This file provides a portable binding to the sockets API */
35 #include "gsocket.h"
36 /* Include all the necessary system-specific headers and define the
37 necessary macros (shared with gen-soccon). */
39 #include "raise.h"
40 /* Required for __gnat_malloc() */
42 #include <string.h>
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 */
57 void
58 __gnat_disable_sigpipe (int fd)
60 #ifdef SO_NOSIGPIPE
61 int val = 1;
62 (void) setsockopt (fd, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof val);
63 #endif
66 /* Free socket set. */
68 void
69 __gnat_free_socket_set (fd_set *set)
71 __gnat_free (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. */
79 void
80 __gnat_last_socket_in_set (fd_set *set, int *last)
82 int s;
83 int l;
84 l = -1;
86 #ifdef WINNT
87 /* More efficient method for NT. */
88 for (s = 0; s < set->fd_count; s++)
89 if ((int) set->fd_array[s] > l)
90 l = set->fd_array[s];
92 #else
94 for (s = *last; s != -1; s--)
95 if (FD_ISSET (s, set))
97 l = s;
98 break;
100 #endif
102 *last = l;
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. */
110 void
111 __gnat_get_socket_from_set (fd_set *set, int *last, int *socket)
113 *socket = *last;
114 FD_CLR (*socket, set);
115 __gnat_last_socket_in_set (set, last);
118 /* Insert SOCKET in the socket set SET. */
120 void
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. */
136 fd_set *
137 __gnat_new_socket_set (fd_set *set)
139 fd_set *new;
141 new = (fd_set *) __gnat_malloc (sizeof (fd_set));
143 if (set)
144 memcpy (new, set, sizeof (fd_set));
145 else
146 FD_ZERO (new);
148 return new;
151 /* Remove SOCKET from the socket set SET. */
153 void
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) {
163 #ifdef __vxworks
164 int vxw_errno = errno;
166 switch (vxw_errno) {
167 case 0:
168 return 0;
170 case S_resolvLib_HOST_NOT_FOUND:
171 case S_hostLib_UNKNOWN_HOST:
172 return HOST_NOT_FOUND;
174 case S_resolvLib_TRY_AGAIN:
175 return 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:
182 return NO_RECOVERY;
184 case S_resolvLib_NO_DATA:
185 return NO_DATA;
187 default:
188 return -1;
190 #elif defined(VMS)
191 return errno;
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.
197 extern int h_errno;
198 return h_errno;
199 #else
200 return h_errno;
201 #endif