OSDN Git Service

6f5067fcabecf69848b4960e55fe887152c55000
[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, 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,  59 Temple Place - Suite 330,  Boston, *
20  * MA 02111-1307, 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 fd set functions           */
34
35 #ifdef __vxworks
36 #include "vxWorks.h"
37 #endif
38
39 #ifdef IN_RTS
40 #include "tconfig.h"
41 #include "tsystem.h"
42
43 #if defined (WINNT)
44 #define FD_SETSIZE 1024
45 #include <windows.h>
46
47 #ifdef __MINGW32__
48 #include "mingw32.h"
49 #if STD_MINGW
50 #include <winsock.h>
51 #else
52 #include <windows32/sockets.h>
53 #endif
54 #endif
55 #endif
56
57 #if defined (VMS)
58 #define FD_SETSIZE 4096
59 #include <sys/time.h>
60 #endif
61
62 #else
63 #include "config.h"
64 #include "system.h"
65 #endif
66
67 #if !(defined (VMS) || defined (__MINGW32__))
68 # include <sys/socket.h>
69 #endif
70
71 #include "raise.h"
72
73 extern void __gnat_disable_sigpipe (int fd);
74 extern void __gnat_free_socket_set (fd_set *);
75 extern void __gnat_last_socket_in_set (fd_set *, int *);
76 extern void __gnat_get_socket_from_set (fd_set *, int *, int *);
77 extern void __gnat_insert_socket_in_set (fd_set *, int);
78 extern int __gnat_is_socket_in_set (fd_set *, int);
79 extern fd_set *__gnat_new_socket_set (fd_set *);
80 extern void __gnat_remove_socket_from_set (fd_set *, int);
81 \f
82 /* Disable the sending of SIGPIPE for writes on a broken stream */
83 void
84 __gnat_disable_sigpipe (int fd)
85 {
86 #ifdef SO_NOSIGPIPE
87   int val = 1;
88   (void) setsockopt (fd, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof val);
89 #endif
90 }
91
92 /* Free socket set. */
93
94 void
95 __gnat_free_socket_set (fd_set *set)
96 {
97   __gnat_free (set);
98 }
99
100 /* Find the largest socket in the socket set SET. This is needed for
101    `select'.  LAST is the maximum value for the largest socket. This hint is
102    used to avoid scanning very large socket sets.  On return, LAST is the
103    actual largest socket in the socket set. */
104
105 void
106 __gnat_last_socket_in_set (fd_set *set, int *last)
107 {
108   int s;
109   int l;
110   l = -1;
111
112 #ifdef WINNT
113   /* More efficient method for NT. */
114   for (s = 0; s < set->fd_count; s++)
115     if ((int) set->fd_array[s] > l)
116       l = set->fd_array[s];
117
118 #else
119
120   for (s = *last; s != -1; s--)
121     if (FD_ISSET (s, set))
122       {
123         l = s;
124         break;
125       }
126 #endif
127
128   *last = l;
129 }
130
131 /* Get last socket and remove it from the socket set SET.  LAST is the
132    maximum value of the largest socket.  This hint is used to avoid scanning
133    very large socket sets.  On return, LAST is set to the actual largest
134    socket in the socket set. */
135
136 void
137 __gnat_get_socket_from_set (fd_set *set, int *last, int *socket)
138 {
139   *socket = *last;
140   FD_CLR (*socket, set);
141   __gnat_last_socket_in_set (set, last);
142 }
143
144 /* Insert SOCKET in the socket set SET. */
145
146 void
147 __gnat_insert_socket_in_set (fd_set *set, int socket)
148 {
149   FD_SET (socket, set);
150 }
151
152 /* Check whether a given SOCKET is in the socket set SET. */
153
154 int
155 __gnat_is_socket_in_set (fd_set *set, int socket)
156 {
157   return FD_ISSET (socket, set);
158 }
159
160 /* Allocate a new socket set and set it as empty.  */
161
162 fd_set *
163 __gnat_new_socket_set (fd_set *set)
164 {
165   fd_set *new;
166
167   new = (fd_set *) __gnat_malloc (sizeof (fd_set));
168
169   if (set)
170     memcpy (new, set, sizeof (fd_set));
171   else
172     FD_ZERO (new);
173
174   return new;
175 }
176
177 /* Remove SOCKET from the socket set SET. */
178
179 void
180 __gnat_remove_socket_from_set (fd_set *set, int socket)
181 {
182   FD_CLR (socket, set);
183 }