OSDN Git Service

Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[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-2009, 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 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.                                     *
17  *                                                                          *
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.               *
21  *                                                                          *
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/>.                                          *
26  *                                                                          *
27  * GNAT was originally developed  by the GNAT team at  New York University. *
28  * Extensive contributions were provided by Ada Core Technologies Inc.      *
29  *                                                                          *
30  ****************************************************************************/
31
32 /*  This file provides a portable binding to the sockets API                */
33
34 #include "gsocket.h"
35
36 #if defined(HAVE_SOCKETS)
37
38 /* Include all the necessary system-specific headers and define the
39  * necessary macros (shared with gen-oscons).
40  */
41
42 #if !defined(SO_NOSIGPIPE) && !defined (MSG_NOSIGNAL)
43 #include <signal.h>
44 #endif
45 /* Required if we will be calling signal() in __gnat_disable_all_sigpipes() */
46
47 #include "raise.h"
48 /* Required for __gnat_malloc() */
49
50 #include <string.h>
51 /* Required for memcpy() */
52
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 *set);
66 extern int  __gnat_get_h_errno (void);
67 \f
68 /* Disable the sending of SIGPIPE for writes on a broken stream */
69
70 void
71 __gnat_disable_sigpipe (int fd)
72 {
73 #ifdef SO_NOSIGPIPE
74   int val = 1;
75   (void) setsockopt (fd, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof val);
76 #endif
77 }
78
79 void
80 __gnat_disable_all_sigpipes (void)
81 {
82 #if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) && defined(SIGPIPE)
83   (void) signal (SIGPIPE, SIG_IGN);
84 #endif
85 }
86 \f
87 #if defined (_WIN32) || defined (__vxworks) || defined (VMS)
88 /*
89  * Signalling FDs operations are implemented in Ada for these platforms
90  * (see subunit GNAT.Sockets.Thin.Signalling_Fds).
91  */
92 #else
93 /*
94  * Create a pair of connected file descriptors fds[0] and fds[1] used for
95  * signalling by a Selector object. fds[0] is the read end, and fds[1] the
96  * write end.
97  */
98 int
99 __gnat_create_signalling_fds (int *fds) {
100   return pipe (fds);
101 }
102 \f
103 /*
104  * Read one byte of data from rsig, the read end of a pair of signalling fds
105  * created by __gnat_create_signalling_fds.
106  */
107 int
108 __gnat_read_signalling_fd (int rsig) {
109   char c;
110   return read (rsig, &c, 1);
111 }
112 \f
113 /*
114  * Write one byte of data to wsig, the write end of a pair of signalling fds
115  * created by __gnat_create_signalling_fds.
116  */
117 int
118 __gnat_write_signalling_fd (int wsig) {
119   char c = 0;
120   return write (wsig, &c, 1);
121 }
122 \f
123 /*
124  * Close one end of a pair of signalling fds
125  */
126 void
127 __gnat_close_signalling_fd (int sig) {
128   (void) close (sig);
129 }
130 #endif
131 \f
132 /*
133  * GetXXXbyYYY wrappers
134  * These functions are used by the default implementation of g-socthi,
135  * and also by the Windows version.
136  *
137  * They can be used for any platform that either provides an intrinsically
138  * task safe implementation of getXXXbyYYY, or a reentrant variant
139  * getXXXbyYYY_r. Otherwise, a task safe wrapper, including proper mutual
140  * exclusion if appropriate, must be implemented in the target specific
141  * version of g-socthi.
142  */
143
144 #ifdef HAVE_THREAD_SAFE_GETxxxBYyyy
145 int
146 __gnat_safe_gethostbyname (const char *name,
147   struct hostent *ret, char *buf, size_t buflen,
148   int *h_errnop)
149 {
150   struct hostent *rh;
151   rh = gethostbyname (name);
152   if (rh == NULL) {
153     *h_errnop = h_errno;
154     return -1;
155   }
156   *ret = *rh;
157   *h_errnop = 0;
158   return 0;
159 }
160
161 int
162 __gnat_safe_gethostbyaddr (const char *addr, int len, int type,
163   struct hostent *ret, char *buf, size_t buflen,
164   int *h_errnop)
165 {
166   struct hostent *rh;
167   rh = gethostbyaddr (addr, len, type);
168   if (rh == NULL) {
169     *h_errnop = h_errno;
170     return -1;
171   }
172   *ret = *rh;
173   *h_errnop = 0;
174   return 0;
175 }
176
177 int
178 __gnat_safe_getservbyname (const char *name, const char *proto,
179   struct servent *ret, char *buf, size_t buflen)
180 {
181   struct servent *rh;
182   rh = getservbyname (name, proto);
183   if (rh == NULL)
184     return -1;
185   *ret = *rh;
186   return 0;
187 }
188
189 int
190 __gnat_safe_getservbyport (int port, const char *proto,
191   struct servent *ret, char *buf, size_t buflen)
192 {
193   struct servent *rh;
194   rh = getservbyport (port, proto);
195   if (rh == NULL)
196     return -1;
197   *ret = *rh;
198   return 0;
199 }
200 #elif HAVE_GETxxxBYyyy_R
201 int
202 __gnat_safe_gethostbyname (const char *name,
203   struct hostent *ret, char *buf, size_t buflen,
204   int *h_errnop)
205 {
206   struct hostent *rh;
207   int ri;
208
209 #if defined(__linux__) || defined(__GLIBC__)
210   (void) gethostbyname_r (name, ret, buf, buflen, &rh, h_errnop);
211 #else
212   rh = gethostbyname_r (name, ret, buf, buflen, h_errnop);
213 #endif
214   ri = (rh == NULL) ? -1 : 0;
215   return ri;
216 }
217
218 int
219 __gnat_safe_gethostbyaddr (const char *addr, int len, int type,
220   struct hostent *ret, char *buf, size_t buflen,
221   int *h_errnop)
222 {
223   struct hostent *rh;
224   int ri;
225
226 #if defined(__linux__) || defined(__GLIBC__)
227   (void) gethostbyaddr_r (addr, len, type, ret, buf, buflen, &rh, h_errnop);
228 #else
229   rh = gethostbyaddr_r (addr, len, type, ret, buf, buflen, h_errnop);
230 #endif
231   ri = (rh == NULL) ? -1 : 0;
232   return ri;
233 }
234
235 int
236 __gnat_safe_getservbyname (const char *name, const char *proto,
237   struct servent *ret, char *buf, size_t buflen)
238 {
239   struct servent *rh;
240   int ri;
241
242 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
243   (void) getservbyname_r (name, proto, ret, buf, buflen, &rh);
244 #else
245   rh = getservbyname_r (name, proto, ret, buf, buflen);
246 #endif
247   ri = (rh == NULL) ? -1 : 0;
248   return ri;
249 }
250
251 int
252 __gnat_safe_getservbyport (int port, const char *proto,
253   struct servent *ret, char *buf, size_t buflen)
254 {
255   struct servent *rh;
256   int ri;
257
258 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
259   (void) getservbyport_r (port, proto, ret, buf, buflen, &rh);
260 #else
261   rh = getservbyport_r (port, proto, ret, buf, buflen);
262 #endif
263   ri = (rh == NULL) ? -1 : 0;
264   return ri;
265 }
266 #endif
267 \f
268 /* Find the largest socket in the socket set SET. This is needed for
269    `select'.  LAST is the maximum value for the largest socket. This hint is
270    used to avoid scanning very large socket sets.  On return, LAST is the
271    actual largest socket in the socket set. */
272
273 void
274 __gnat_last_socket_in_set (fd_set *set, int *last)
275 {
276   int s;
277   int l;
278   l = -1;
279
280 #ifdef _WIN32
281   /* More efficient method for NT. */
282   for (s = 0; s < set->fd_count; s++)
283     if ((int) set->fd_array[s] > l)
284       l = set->fd_array[s];
285
286 #else
287
288   for (s = *last; s != -1; s--)
289     if (FD_ISSET (s, set))
290       {
291         l = s;
292         break;
293       }
294 #endif
295
296   *last = l;
297 }
298
299 /* Get last socket and remove it from the socket set SET.  LAST is the
300    maximum value of the largest socket.  This hint is used to avoid scanning
301    very large socket sets.  On return, LAST is set to the actual largest
302    socket in the socket set. */
303
304 void
305 __gnat_get_socket_from_set (fd_set *set, int *last, int *socket)
306 {
307   *socket = *last;
308   FD_CLR (*socket, set);
309   __gnat_last_socket_in_set (set, last);
310 }
311
312 /* Insert SOCKET in the socket set SET. */
313
314 void
315 __gnat_insert_socket_in_set (fd_set *set, int socket)
316 {
317   FD_SET (socket, set);
318 }
319
320 /* Check whether a given SOCKET is in the socket set SET. */
321
322 int
323 __gnat_is_socket_in_set (fd_set *set, int socket)
324 {
325   return FD_ISSET (socket, set);
326 }
327
328 /* Remove SOCKET from the socket set SET. */
329
330 void
331 __gnat_remove_socket_from_set (fd_set *set, int socket)
332 {
333   FD_CLR (socket, set);
334 }
335
336 /* Reset SET */
337 void
338 __gnat_reset_socket_set (fd_set *set)
339 {
340   FD_ZERO (set);
341 }
342
343 /* Get the value of the last host error */
344
345 int
346 __gnat_get_h_errno (void) {
347 #ifdef __vxworks
348   int vxw_errno = errno;
349
350   switch (vxw_errno) {
351     case 0:
352       return 0;
353
354 #ifdef S_resolvLib_HOST_NOT_FOUND
355     case S_resolvLib_HOST_NOT_FOUND:
356 #endif
357 #ifdef S_hostLib_HOST_NOT_FOUND
358     case S_hostLib_HOST_NOT_FOUND:
359 #endif
360     case S_hostLib_UNKNOWN_HOST:
361       return HOST_NOT_FOUND;
362
363 #ifdef S_resolvLib_TRY_AGAIN
364     case S_resolvLib_TRY_AGAIN:
365       return TRY_AGAIN;
366 #endif
367 #ifdef S_hostLib_TRY_AGAIN
368     case S_hostLib_TRY_AGAIN:
369       return TRY_AGAIN;
370 #endif
371
372 #ifdef S_resolvLib_NO_RECOVERY
373     case S_resolvLib_NO_RECOVERY:
374 #endif
375 #ifdef S_resolvLib_BUFFER_2_SMALL
376     case S_resolvLib_BUFFER_2_SMALL:
377 #endif
378 #ifdef S_resolvLib_INVALID_PARAMETER
379     case S_resolvLib_INVALID_PARAMETER:
380 #endif
381 #ifdef S_resolvLib_INVALID_ADDRESS
382     case S_resolvLib_INVALID_ADDRESS:
383 #endif
384 #ifdef S_hostLib_NO_RECOVERY
385     case S_hostLib_NO_RECOVERY:
386 #endif
387 #ifdef S_hostLib_NETDB_INTERNAL
388     case S_hostLib_NETDB_INTERNAL:
389 #endif
390     case S_hostLib_INVALID_PARAMETER:
391       return NO_RECOVERY;
392
393 #ifdef S_resolvLib_NO_DATA
394     case S_resolvLib_NO_DATA:
395       return NO_DATA;
396 #endif
397
398     default:
399       return -1;
400   }
401
402 #elif defined (VMS)
403   /* h_errno is defined as follows in OpenVMS' version of <netdb.h>.
404    * However this header file is not available when building the GNAT
405    * runtime library using GCC, so we are hardcoding the definition
406    * directly. Note that the returned address is thread-specific.
407    */
408   extern int *decc$h_errno_get_addr ();
409   return *decc$h_errno_get_addr ();
410
411 #elif defined (__rtems__)
412   /* At this stage in the tool build, no networking .h files are available.
413    * Newlib does not provide networking .h files and RTEMS is not built yet.
414    * So we need to explicitly extern h_errno to access it.
415    */
416   extern int h_errno;
417   return h_errno;
418
419 #else
420   return h_errno;
421 #endif
422 }
423
424 #else
425 #warning Sockets are not supported on this platform
426 #endif /* defined(HAVE_SOCKETS) */