1 /****************************************************************************
2  *                                                                          *
3  *                         GNAT COMPILER COMPONENTS                         *
4  *                                                                          *
5  *                              G S O C K E T                               *
6  *                                                                          *
7  *                              C Header File                               *
8  *                                                                          *
9  *         Copyright (C) 2004-2019, 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 #if defined(VTHREADS) || defined(__PikeOS__) || defined(__DJGPP__)
33 
34 /* Sockets not supported on these platforms.  */
35 #undef HAVE_SOCKETS
36 
37 #else
38 
39 #define HAVE_SOCKETS
40 
41 #ifndef _XOPEN_SOURCE_EXTENDED
42 #define _XOPEN_SOURCE_EXTENDED 1
43 /* For HP-UX */
44 #endif
45 
46 #ifndef BSD_COMP
47 #define BSD_COMP 1
48 /* For Solaris */
49 #endif
50 
51 #ifndef _ALL_SOURCE
52 #define _ALL_SOURCE 1
53 /* For AIX */
54 #endif
55 
56 /** No system header may be included prior to this point since on some targets
57  ** we need to redefine FD_SETSIZE.
58  **/
59 
60 /* Target-specific includes and definitions */
61 
62 #if defined(__vxworks)
63 #include <vxWorks.h>
64 #include <ioLib.h>
65 #include <hostLib.h>
66 
67 #define SHUT_RD		0
68 #define SHUT_WR		1
69 #define SHUT_RDWR	2
70 
71 #ifndef IPV6_ADD_MEMBERSHIP
72 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
73 #endif
74 
75 #ifndef IPV6_DROP_MEMBERSHIP
76 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
77 #endif
78 
79 #elif defined (WINNT)
80 #define FD_SETSIZE 1024
81 
82 #ifdef __MINGW32__
83 #include <winsock2.h>
84 #include <ws2tcpip.h>
85 
86 #undef  EACCES
87 #define EACCES          WSAEACCES
88 #undef  EADDRINUSE
89 #define EADDRINUSE      WSAEADDRINUSE
90 #undef  EADDRNOTAVAIL
91 #define EADDRNOTAVAIL   WSAEADDRNOTAVAIL
92 #undef  EAFNOSUPPORT
93 #define EAFNOSUPPORT    WSAEAFNOSUPPORT
94 #undef  EALREADY
95 #define EALREADY        WSAEALREADY
96 #undef  EBADF
97 #define EBADF           WSAEBADF
98 #undef  ECONNABORTED
99 #define ECONNABORTED    WSAECONNABORTED
100 #undef  ECONNREFUSED
101 #define ECONNREFUSED    WSAECONNREFUSED
102 #undef  ECONNRESET
103 #define ECONNRESET      WSAECONNRESET
104 #undef  EDESTADDRREQ
105 #define EDESTADDRREQ    WSAEDESTADDRREQ
106 #undef  EFAULT
107 #define EFAULT          WSAEFAULT
108 #undef  EHOSTDOWN
109 #define EHOSTDOWN       WSAEHOSTDOWN
110 #undef  EHOSTUNREACH
111 #define EHOSTUNREACH    WSAEHOSTUNREACH
112 #undef  EINPROGRESS
113 #define EINPROGRESS     WSAEINPROGRESS
114 #undef  EINTR
115 #define EINTR           WSAEINTR
116 #undef  EINVAL
117 #define EINVAL          WSAEINVAL
118 #undef  EIO
119 #define EIO             WSAEDISCON
120 #undef  EISCONN
121 #define EISCONN         WSAEISCONN
122 #undef  ELOOP
123 #define ELOOP           WSAELOOP
124 #undef  EMFILE
125 #define EMFILE          WSAEMFILE
126 #undef  EMSGSIZE
127 #define EMSGSIZE        WSAEMSGSIZE
128 #undef  ENAMETOOLONG
129 #define ENAMETOOLONG    WSAENAMETOOLONG
130 #undef  ENETDOWN
131 #define ENETDOWN        WSAENETDOWN
132 #undef  ENETRESET
133 #define ENETRESET       WSAENETRESET
134 #undef  ENETUNREACH
135 #define ENETUNREACH     WSAENETUNREACH
136 #undef  ENOBUFS
137 #define ENOBUFS         WSAENOBUFS
138 #undef  ENOPROTOOPT
139 #define ENOPROTOOPT     WSAENOPROTOOPT
140 #undef  ENOTCONN
141 #define ENOTCONN        WSAENOTCONN
142 #undef  ENOTSOCK
143 #define ENOTSOCK        WSAENOTSOCK
144 #undef  EOPNOTSUPP
145 #define EOPNOTSUPP      WSAEOPNOTSUPP
146 #undef  EPFNOSUPPORT
147 #define EPFNOSUPPORT    WSAEPFNOSUPPORT
148 #undef  EPROTONOSUPPORT
149 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
150 #undef  EPROTOTYPE
151 #define EPROTOTYPE      WSAEPROTOTYPE
152 #undef  ESHUTDOWN
153 #define ESHUTDOWN       WSAESHUTDOWN
154 #undef  ESOCKTNOSUPPORT
155 #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
156 #undef  ETIMEDOUT
157 #define ETIMEDOUT       WSAETIMEDOUT
158 #undef  ETOOMANYREFS
159 #define ETOOMANYREFS    WSAETOOMANYREFS
160 #undef  EWOULDBLOCK
161 #define EWOULDBLOCK     WSAEWOULDBLOCK
162 
163 #define SHUT_RD		SD_RECEIVE
164 #define SHUT_WR		SD_SEND
165 #define SHUT_RDWR	SD_BOTH
166 
167 #endif
168 
169 #include <windows.h>
170 
171 #elif defined(VMS)
172 /* Allow a large number of fds for select.  */
173 #define FD_SETSIZE 4096
174 #ifndef IN_RTS
175 /* These DEC C headers are not available when building with GCC.  Order is
176    important.  */
177 #include <time.h>
178 #include <tcp.h>
179 #include <in.h>
180 #include <ioctl.h>
181 #include <netdb.h>
182 #endif
183 
184 #endif
185 
186 #include <limits.h>
187 #include <errno.h>
188 
189 #if defined (__vxworks) && ! defined (__RTP__)
190 #include <sys/times.h>
191 #else
192 #include <sys/time.h>
193 #endif
194 
195 #if defined(__rtems__)
196 #include <unistd.h>
197 /* Required, for read(), write(), and close() */
198 #endif
199 
200 /*
201  * RTEMS has these .h files but not until you have built and installed RTEMS.
202  * When building a C/C++ toolset, you also build the newlib C library, so the
203  * build procedure for an RTEMS GNAT toolset requires that you build a C/C++
204  * toolset, then build and install RTEMS with --enable-multilib, and finally
205  * build the Ada part of the toolset.
206  */
207 #if !(defined (VMS) || defined (__MINGW32__))
208 #include <sys/socket.h>
209 #include <netinet/in.h>
210 #include <netinet/tcp.h>
211 #include <sys/ioctl.h>
212 #include <netdb.h>
213 #include <unistd.h>
214 #endif
215 
216 #ifdef __ANDROID__
217 #include <unistd.h>
218 #include <sys/select.h>
219 #endif
220 
221 #if defined (_AIX) || defined (__FreeBSD__) || defined (__hpux__) || \
222     defined (_WIN32) || defined (__APPLE__) || defined (__ANDROID__) || \
223     defined (__DragonFly__) || defined (__NetBSD__) || defined (__OpenBSD__)
224 # define HAVE_THREAD_SAFE_GETxxxBYyyy 1
225 
226 #elif defined (__linux__) || defined (__GLIBC__) || \
227      (defined (__sun__) && !defined (__vxworks)) || \
228       defined (__rtems__)
229 # define HAVE_GETxxxBYyyy_R 1
230 #endif
231 
232 /*
233  * Properties of the unerlying NetDB library:
234  *   Need_Netdb_Buffer __gnat_getXXXbyYYY expects a caller-supplied buffer
235  *   Need_Netdb_Lock   __gnat_getXXXbyYYY expects the caller to ensure
236  *                     mutual exclusion
237  *
238  * See "Handling of gethostbyname, gethostbyaddr, getservbyname and
239  * getservbyport" in socket.c for details.
240  */
241 
242 #if defined (HAVE_GETxxxBYyyy_R)
243 # define Need_Netdb_Buffer 1
244 # define Need_Netdb_Lock 0
245 
246 #else
247 # define Need_Netdb_Buffer 0
248 # if !defined (HAVE_THREAD_SAFE_GETxxxBYyyy)
249 #  define Need_Netdb_Lock 1
250 # else
251 #  define Need_Netdb_Lock 0
252 # endif
253 #endif
254 
255 #if defined (__FreeBSD__) || defined (__vxworks) || defined(__rtems__) \
256  || defined (__DragonFly__) || defined (__NetBSD__) || defined (__OpenBSD__)
257 # define Has_Sockaddr_Len 1
258 #else
259 # define Has_Sockaddr_Len 0
260 #endif
261 
262 #if !(defined (_WIN32) || defined (__hpux__) || defined (VMS))
263 # define HAVE_INET_PTON
264 # define HAVE_INET_NTOP
265 #endif
266 
267 #endif /* defined(VTHREADS) */
268