OSDN Git Service

d02763a9548ca04b4fa4896f2a32ffc085ff5039
[pf3gnuchains/gcc-fork.git] / gcc / ada / socket.c
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  ****************************************************************************/
32
33 /*  This file provides a portable binding to the sockets API                */
34
35 #include "gsocket.h"
36 /* Include all the necessary system-specific headers and define the
37    necessary macros (shared with gen-soccon). */
38
39 #include "raise.h"
40 /* Required for __gnat_malloc() */
41
42 #include <string.h>
43 /* Required for memcpy() */
44
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);
54 \f
55 /* Disable the sending of SIGPIPE for writes on a broken stream */
56
57 void
58 __gnat_disable_sigpipe (int fd)
59 {
60 #ifdef SO_NOSIGPIPE
61   int val = 1;
62   (void) setsockopt (fd, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof val);
63 #endif
64 }
65
66 /* Free socket set. */
67
68 void
69 __gnat_free_socket_set (fd_set *set)
70 {
71   __gnat_free (set);
72 }
73
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. */
78
79 void
80 __gnat_last_socket_in_set (fd_set *set, int *last)
81 {
82   int s;
83   int l;
84   l = -1;
85
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];
91
92 #else
93
94   for (s = *last; s != -1; s--)
95     if (FD_ISSET (s, set))
96       {
97         l = s;
98         break;
99       }
100 #endif
101
102   *last = l;
103 }
104
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. */
109
110 void
111 __gnat_get_socket_from_set (fd_set *set, int *last, int *socket)
112 {
113   *socket = *last;
114   FD_CLR (*socket, set);
115   __gnat_last_socket_in_set (set, last);
116 }
117
118 /* Insert SOCKET in the socket set SET. */
119
120 void
121 __gnat_insert_socket_in_set (fd_set *set, int socket)
122 {
123   FD_SET (socket, set);
124 }
125
126 /* Check whether a given SOCKET is in the socket set SET. */
127
128 int
129 __gnat_is_socket_in_set (fd_set *set, int socket)
130 {
131   return FD_ISSET (socket, set);
132 }
133
134 /* Allocate a new socket set and set it as empty.  */
135
136 fd_set *
137 __gnat_new_socket_set (fd_set *set)
138 {
139   fd_set *new;
140
141   new = (fd_set *) __gnat_malloc (sizeof (fd_set));
142
143   if (set)
144     memcpy (new, set, sizeof (fd_set));
145   else
146     FD_ZERO (new);
147
148   return new;
149 }
150
151 /* Remove SOCKET from the socket set SET. */
152
153 void
154 __gnat_remove_socket_from_set (fd_set *set, int socket)
155 {
156   FD_CLR (socket, set);
157 }
158
159 /* Get the value of the last host error */
160
161 int
162 __gnat_get_h_errno (void) {
163 #ifdef __vxworks
164   int vxw_errno = errno;
165
166   switch (vxw_errno) {
167     case 0:
168       return 0;
169
170     case S_resolvLib_HOST_NOT_FOUND:
171     case S_hostLib_UNKNOWN_HOST:
172       return HOST_NOT_FOUND;
173
174     case S_resolvLib_TRY_AGAIN:
175       return TRY_AGAIN;
176
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;
183
184     case S_resolvLib_NO_DATA:
185       return NO_DATA;
186
187     default:
188       return -1;
189   }
190 #elif defined(VMS)
191   return errno;
192 #else
193   return h_errno;
194 #endif
195 }