1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * S O C K E T *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 2003-2015, 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 /* Ensure access to errno is thread safe. */
35 #define _REENTRANT
36 #define _THREAD_SAFE
37
38 #include "gsocket.h"
39
40 #if defined (__FreeBSD__) || defined (__DragonFly__) \
41 || defined (__NetBSD__) || defined (__OpenBSD__)
42 typedef unsigned int IOCTL_Req_T;
43 #else
44 typedef int IOCTL_Req_T;
45 #endif
46
47 #if defined(HAVE_SOCKETS)
48
49 /* Include all the necessary system-specific headers and define the
50 * necessary macros (shared with gen-oscons).
51 */
52
53 #if !defined(SO_NOSIGPIPE) && !defined (MSG_NOSIGNAL)
54 #include <signal.h>
55 #endif
56 /* Required if we will be calling signal() in __gnat_disable_all_sigpipes() */
57
58 #include "raise.h"
59 /* Required for __gnat_malloc() */
60
61 #include <string.h>
62 /* Required for memcpy() */
63
64 extern void __gnat_disable_sigpipe (int fd);
65 extern void __gnat_disable_all_sigpipes (void);
66 extern int __gnat_create_signalling_fds (int *fds);
67 extern int __gnat_read_signalling_fd (int rsig);
68 extern int __gnat_write_signalling_fd (int wsig);
69 extern void __gnat_close_signalling_fd (int sig);
70 extern void __gnat_last_socket_in_set (fd_set *, int *);
71 extern void __gnat_get_socket_from_set (fd_set *, int *, int *);
72 extern void __gnat_insert_socket_in_set (fd_set *, int);
73 extern int __gnat_is_socket_in_set (fd_set *, int);
74 extern fd_set *__gnat_new_socket_set (fd_set *);
75 extern void __gnat_remove_socket_from_set (fd_set *, int);
76 extern void __gnat_reset_socket_set (fd_set *);
77 extern int __gnat_get_h_errno (void);
78 extern int __gnat_socket_ioctl (int, IOCTL_Req_T, int *);
79
80 extern char * __gnat_servent_s_name (struct servent *);
81 extern char * __gnat_servent_s_alias (struct servent *, int index);
82 extern unsigned short __gnat_servent_s_port (struct servent *);
83 extern char * __gnat_servent_s_proto (struct servent *);
84
85 extern char * __gnat_hostent_h_name (struct hostent *);
86 extern char * __gnat_hostent_h_alias (struct hostent *, int);
87 extern int __gnat_hostent_h_addrtype (struct hostent *);
88 extern int __gnat_hostent_h_length (struct hostent *);
89 extern char * __gnat_hostent_h_addr (struct hostent *, int);
90
91 #ifndef HAVE_INET_PTON
92 extern int __gnat_inet_pton (int, const char *, void *);
93 #endif
94
95 /* Disable the sending of SIGPIPE for writes on a broken stream */
96
97 void
__gnat_disable_sigpipe(int fd)98 __gnat_disable_sigpipe (int fd)
99 {
100 #ifdef SO_NOSIGPIPE
101 int val = 1;
102 (void) setsockopt (fd, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof val);
103 #endif
104 }
105
106 void
__gnat_disable_all_sigpipes(void)107 __gnat_disable_all_sigpipes (void)
108 {
109 #if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) && defined(SIGPIPE)
110 (void) signal (SIGPIPE, SIG_IGN);
111 #endif
112 }
113
114 #if defined (_WIN32) || defined (__vxworks)
115 /*
116 * Signalling FDs operations are implemented in Ada for these platforms
117 * (see subunit GNAT.Sockets.Thin.Signalling_Fds).
118 */
119 #else
120 /*
121 * Create a pair of connected file descriptors fds[0] and fds[1] used for
122 * signalling by a Selector object. fds[0] is the read end, and fds[1] the
123 * write end.
124 */
125 int
__gnat_create_signalling_fds(int * fds)126 __gnat_create_signalling_fds (int *fds) {
127 return pipe (fds);
128 }
129
130 /*
131 * Read one byte of data from rsig, the read end of a pair of signalling fds
132 * created by __gnat_create_signalling_fds.
133 */
134 int
__gnat_read_signalling_fd(int rsig)135 __gnat_read_signalling_fd (int rsig) {
136 char c;
137 return read (rsig, &c, 1);
138 }
139
140 /*
141 * Write one byte of data to wsig, the write end of a pair of signalling fds
142 * created by __gnat_create_signalling_fds.
143 */
144 int
__gnat_write_signalling_fd(int wsig)145 __gnat_write_signalling_fd (int wsig) {
146 char c = 0;
147 return write (wsig, &c, 1);
148 }
149
150 /*
151 * Close one end of a pair of signalling fds
152 */
153 void
__gnat_close_signalling_fd(int sig)154 __gnat_close_signalling_fd (int sig) {
155 (void) close (sig);
156 }
157 #endif
158
159 /*
160 * Handling of gethostbyname, gethostbyaddr, getservbyname and getservbyport
161 * =========================================================================
162 *
163 * This module exposes __gnat_getXXXbyYYY operations with the same signature
164 * as the reentrant variant getXXXbyYYY_r.
165 *
166 * On platforms where getXXXbyYYY is intrinsically reentrant, the provided user
167 * buffer argument is ignored.
168 *
169 * When getXXXbyYYY is not reentrant but getXXXbyYYY_r exists, the latter is
170 * used, and the provided buffer argument must point to a valid, thread-local
171 * buffer (usually on the caller's stack).
172 *
173 * When getXXXbyYYY is not reentrant and no reentrant getXXXbyYYY_r variant
174 * is available, the non-reentrant getXXXbyYYY is called, the provided user
175 * buffer is ignored, and the caller is expected to take care of mutual
176 * exclusion.
177 */
178
179 #ifdef HAVE_GETxxxBYyyy_R
180 int
__gnat_gethostbyname(const char * name,struct hostent * ret,char * buf,size_t buflen,int * h_errnop)181 __gnat_gethostbyname (const char *name,
182 struct hostent *ret, char *buf, size_t buflen,
183 int *h_errnop)
184 {
185 struct hostent *rh;
186 int ri;
187
188 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
189 (void) gethostbyname_r (name, ret, buf, buflen, &rh, h_errnop);
190 #else
191 rh = gethostbyname_r (name, ret, buf, buflen, h_errnop);
192 #endif
193 ri = (rh == NULL) ? -1 : 0;
194 return ri;
195 }
196
197 int
__gnat_gethostbyaddr(const char * addr,int len,int type,struct hostent * ret,char * buf,size_t buflen,int * h_errnop)198 __gnat_gethostbyaddr (const char *addr, int len, int type,
199 struct hostent *ret, char *buf, size_t buflen,
200 int *h_errnop)
201 {
202 struct hostent *rh;
203 int ri;
204
205 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
206 (void) gethostbyaddr_r (addr, len, type, ret, buf, buflen, &rh, h_errnop);
207 #else
208 rh = gethostbyaddr_r (addr, len, type, ret, buf, buflen, h_errnop);
209 #endif
210 ri = (rh == NULL) ? -1 : 0;
211 return ri;
212 }
213
214 int
__gnat_getservbyname(const char * name,const char * proto,struct servent * ret,char * buf,size_t buflen)215 __gnat_getservbyname (const char *name, const char *proto,
216 struct servent *ret, char *buf, size_t buflen)
217 {
218 struct servent *rh;
219 int ri;
220
221 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
222 (void) getservbyname_r (name, proto, ret, buf, buflen, &rh);
223 #else
224 rh = getservbyname_r (name, proto, ret, buf, buflen);
225 #endif
226 ri = (rh == NULL) ? -1 : 0;
227 return ri;
228 }
229
230 int
__gnat_getservbyport(int port,const char * proto,struct servent * ret,char * buf,size_t buflen)231 __gnat_getservbyport (int port, const char *proto,
232 struct servent *ret, char *buf, size_t buflen)
233 {
234 struct servent *rh;
235 int ri;
236
237 #if defined(__linux__) || defined(__GLIBC__) || defined(__rtems__)
238 (void) getservbyport_r (port, proto, ret, buf, buflen, &rh);
239 #else
240 rh = getservbyport_r (port, proto, ret, buf, buflen);
241 #endif
242 ri = (rh == NULL) ? -1 : 0;
243 return ri;
244 }
245 #elif defined (__vxworks)
246 static char vxw_h_name[MAXHOSTNAMELEN + 1];
247 static char *vxw_h_aliases[1] = { NULL };
248 static int vxw_h_addr;
249 static char *vxw_h_addr_list[2] = { (char*) &vxw_h_addr, NULL };
250
251 int
__gnat_gethostbyname(const char * name,struct hostent * ret,char * buf,size_t buflen,int * h_errnop)252 __gnat_gethostbyname (const char *name,
253 struct hostent *ret, char *buf, size_t buflen,
254 int *h_errnop)
255 {
256 vxw_h_addr = hostGetByName (name);
257 if (vxw_h_addr == ERROR) {
258 *h_errnop = __gnat_get_h_errno ();
259 return -1;
260 }
261 ret->h_name = name;
262 ret->h_aliases = &vxw_h_aliases;
263 ret->h_addrtype = AF_INET;
264 ret->h_length = 4;
265 ret->h_addr_list = &vxw_h_addr_list;
266 return 0;
267 }
268
269 int
__gnat_gethostbyaddr(const char * addr,int len,int type,struct hostent * ret,char * buf,size_t buflen,int * h_errnop)270 __gnat_gethostbyaddr (const char *addr, int len, int type,
271 struct hostent *ret, char *buf, size_t buflen,
272 int *h_errnop)
273 {
274 if (type != AF_INET) {
275 *h_errnop = EAFNOSUPPORT;
276 return -1;
277 }
278
279 if (addr == NULL || len != 4) {
280 *h_errnop = EINVAL;
281 return -1;
282 }
283
284 if (hostGetByAddr (*(int*)addr, &vxw_h_name) != OK) {
285 *h_errnop = __gnat_get_h_errno ();
286 return -1;
287 }
288
289 vxw_h_addr = addr;
290
291 ret->h_name = &vxw_h_name;
292 ret->h_aliases = &vxw_h_aliases;
293 ret->h_addrtype = AF_INET;
294 ret->h_length = 4;
295 ret->h_addr_list = &vxw_h_addr_list;
296 }
297
298 int
__gnat_getservbyname(const char * name,const char * proto,struct servent * ret,char * buf,size_t buflen)299 __gnat_getservbyname (const char *name, const char *proto,
300 struct servent *ret, char *buf, size_t buflen)
301 {
302 /* Not available under VxWorks */
303 return -1;
304 }
305
306 int
__gnat_getservbyport(int port,const char * proto,struct servent * ret,char * buf,size_t buflen)307 __gnat_getservbyport (int port, const char *proto,
308 struct servent *ret, char *buf, size_t buflen)
309 {
310 /* Not available under VxWorks */
311 return -1;
312 }
313 #else
314 int
__gnat_gethostbyname(const char * name,struct hostent * ret,char * buf,size_t buflen,int * h_errnop)315 __gnat_gethostbyname (const char *name,
316 struct hostent *ret, char *buf, size_t buflen,
317 int *h_errnop)
318 {
319 struct hostent *rh;
320 rh = gethostbyname (name);
321 if (rh == NULL) {
322 *h_errnop = __gnat_get_h_errno ();
323 return -1;
324 }
325 *ret = *rh;
326 *h_errnop = 0;
327 return 0;
328 }
329
330 int
__gnat_gethostbyaddr(const char * addr,int len,int type,struct hostent * ret,char * buf,size_t buflen,int * h_errnop)331 __gnat_gethostbyaddr (const char *addr, int len, int type,
332 struct hostent *ret, char *buf, size_t buflen,
333 int *h_errnop)
334 {
335 struct hostent *rh;
336 rh = gethostbyaddr (addr, len, type);
337 if (rh == NULL) {
338 *h_errnop = __gnat_get_h_errno ();
339 return -1;
340 }
341 *ret = *rh;
342 *h_errnop = 0;
343 return 0;
344 }
345
346 int
__gnat_getservbyname(const char * name,const char * proto,struct servent * ret,char * buf,size_t buflen)347 __gnat_getservbyname (const char *name, const char *proto,
348 struct servent *ret, char *buf, size_t buflen)
349 {
350 struct servent *rh;
351 rh = getservbyname (name, proto);
352 if (rh == NULL)
353 return -1;
354 *ret = *rh;
355 return 0;
356 }
357
358 int
__gnat_getservbyport(int port,const char * proto,struct servent * ret,char * buf,size_t buflen)359 __gnat_getservbyport (int port, const char *proto,
360 struct servent *ret, char *buf, size_t buflen)
361 {
362 struct servent *rh;
363 rh = getservbyport (port, proto);
364 if (rh == NULL)
365 return -1;
366 *ret = *rh;
367 return 0;
368 }
369 #endif
370
371 /* Find the largest socket in the socket set SET. This is needed for
372 `select'. LAST is the maximum value for the largest socket. This hint is
373 used to avoid scanning very large socket sets. On return, LAST is the
374 actual largest socket in the socket set. */
375
376 void
__gnat_last_socket_in_set(fd_set * set,int * last)377 __gnat_last_socket_in_set (fd_set *set, int *last)
378 {
379 int s;
380 int l;
381 l = -1;
382
383 #ifdef _WIN32
384 /* More efficient method for NT. */
385 for (s = 0; s < set->fd_count; s++)
386 if ((int) set->fd_array[s] > l)
387 l = set->fd_array[s];
388
389 #else
390
391 for (s = *last; s != -1; s--)
392 if (FD_ISSET (s, set))
393 {
394 l = s;
395 break;
396 }
397 #endif
398
399 *last = l;
400 }
401
402 /* Get last socket and remove it from the socket set SET. LAST is the
403 maximum value of the largest socket. This hint is used to avoid scanning
404 very large socket sets. On return, LAST is set to the actual largest
405 socket in the socket set. */
406
407 void
__gnat_get_socket_from_set(fd_set * set,int * last,int * socket)408 __gnat_get_socket_from_set (fd_set *set, int *last, int *socket)
409 {
410 *socket = *last;
411 FD_CLR (*socket, set);
412 __gnat_last_socket_in_set (set, last);
413 }
414
415 /* Insert SOCKET in the socket set SET. */
416
417 void
__gnat_insert_socket_in_set(fd_set * set,int socket)418 __gnat_insert_socket_in_set (fd_set *set, int socket)
419 {
420 FD_SET (socket, set);
421 }
422
423 /* Check whether a given SOCKET is in the socket set SET. */
424
425 int
__gnat_is_socket_in_set(fd_set * set,int socket)426 __gnat_is_socket_in_set (fd_set *set, int socket)
427 {
428 return FD_ISSET (socket, set);
429 }
430
431 /* Remove SOCKET from the socket set SET. */
432
433 void
__gnat_remove_socket_from_set(fd_set * set,int socket)434 __gnat_remove_socket_from_set (fd_set *set, int socket)
435 {
436 FD_CLR (socket, set);
437 }
438
439 /* Reset SET */
440 void
__gnat_reset_socket_set(fd_set * set)441 __gnat_reset_socket_set (fd_set *set)
442 {
443 FD_ZERO (set);
444 }
445
446 /* Get the value of the last host error */
447
448 int
__gnat_get_h_errno(void)449 __gnat_get_h_errno (void) {
450 #ifdef __vxworks
451 int vxw_errno = errno;
452
453 switch (vxw_errno) {
454 case 0:
455 return 0;
456
457 #ifdef S_hostLib_HOST_NOT_FOUND
458 case S_hostLib_HOST_NOT_FOUND:
459 #endif
460 case S_hostLib_UNKNOWN_HOST:
461 return HOST_NOT_FOUND;
462
463 #ifdef S_hostLib_TRY_AGAIN
464 case S_hostLib_TRY_AGAIN:
465 return TRY_AGAIN;
466 #endif
467
468 #ifdef S_hostLib_NO_RECOVERY
469 case S_hostLib_NO_RECOVERY:
470 #endif
471 #ifdef S_hostLib_NETDB_INTERNAL
472 case S_hostLib_NETDB_INTERNAL:
473 #endif
474 case S_hostLib_INVALID_PARAMETER:
475 return NO_RECOVERY;
476
477 default:
478 return -1;
479 }
480
481 #elif defined (__rtems__)
482 /* At this stage in the tool build, no networking .h files are available.
483 * Newlib does not provide networking .h files and RTEMS is not built yet.
484 * So we need to explicitly extern h_errno to access it.
485 */
486 extern int h_errno;
487 return h_errno;
488
489 #else
490 return h_errno;
491 #endif
492 }
493
494 /* Wrapper for ioctl(2), which is a variadic function */
495
496 int
__gnat_socket_ioctl(int fd,IOCTL_Req_T req,int * arg)497 __gnat_socket_ioctl (int fd, IOCTL_Req_T req, int *arg) {
498 #if defined (_WIN32)
499 return ioctlsocket (fd, req, arg);
500 #elif defined (__APPLE__)
501 /*
502 * On Darwin, req is an unsigned long, and we want to convert without sign
503 * extension to get the proper bit pattern in the case of a 64 bit kernel.
504 */
505 return ioctl (fd, (unsigned int) req, arg);
506 #else
507 return ioctl (fd, req, arg);
508 #endif
509 }
510
511 #ifndef HAVE_INET_PTON
512
513 int
__gnat_inet_pton(int af,const char * src,void * dst)514 __gnat_inet_pton (int af, const char *src, void *dst) {
515 switch (af) {
516 #if defined (_WIN32) && defined (AF_INET6)
517 case AF_INET6:
518 #endif
519 case AF_INET:
520 break;
521 default:
522 errno = EAFNOSUPPORT;
523 return -1;
524 }
525
526 #if defined (__vxworks)
527 return (inet_aton (src, dst) == OK);
528
529 #elif defined (_WIN32)
530 struct sockaddr_storage ss;
531 int sslen = sizeof ss;
532 int rc;
533
534 ss.ss_family = af;
535 rc = WSAStringToAddressA (src, af, NULL, (struct sockaddr *)&ss, &sslen);
536 if (rc == 0) {
537 switch (af) {
538 case AF_INET:
539 *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
540 break;
541 #ifdef AF_INET6
542 case AF_INET6:
543 *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
544 break;
545 #endif
546 }
547 }
548 return (rc == 0);
549
550 #elif defined (__hpux__)
551 in_addr_t addr;
552 int rc = -1;
553
554 if (src == NULL || dst == NULL) {
555 errno = EINVAL;
556
557 } else if (!strcmp (src, "255.255.255.255")) {
558 addr = 0xffffffff;
559 rc = 1;
560
561 } else {
562 addr = inet_addr (src);
563 rc = (addr != 0xffffffff);
564 }
565 if (rc == 1) {
566 *(in_addr_t *)dst = addr;
567 }
568 return rc;
569 #endif
570 }
571 #endif
572
573 /*
574 * Accessor functions for struct hostent.
575 */
576
__gnat_hostent_h_name(struct hostent * h)577 char * __gnat_hostent_h_name (struct hostent * h) {
578 return h->h_name;
579 }
580
__gnat_hostent_h_alias(struct hostent * h,int index)581 char * __gnat_hostent_h_alias (struct hostent * h, int index) {
582 return h->h_aliases[index];
583 }
584
__gnat_hostent_h_addrtype(struct hostent * h)585 int __gnat_hostent_h_addrtype (struct hostent * h) {
586 return h->h_addrtype;
587 }
588
__gnat_hostent_h_length(struct hostent * h)589 int __gnat_hostent_h_length (struct hostent * h) {
590 return h->h_length;
591 }
592
__gnat_hostent_h_addr(struct hostent * h,int index)593 char * __gnat_hostent_h_addr (struct hostent * h, int index) {
594 return h->h_addr_list[index];
595 }
596
597 /*
598 * Accessor functions for struct servent.
599 *
600 * These are needed because servent has different representations on different
601 * platforms, and we don't want to deal with that on the Ada side. For example,
602 * on Linux, we have (see /usr/include netdb.h):
603 *
604 * struct servent
605 * {
606 * char *s_name;
607 * char **s_aliases;
608 * int s_port;
609 * char *s_proto;
610 * };
611 *
612 * and on Windows (see mingw's socket.h):
613 *
614 * struct servent {
615 * char *s_name;
616 * char **s_aliases;
617 * #ifdef _WIN64
618 * char *s_proto;
619 * short s_port;
620 * #else
621 * short s_port;
622 * char *s_proto;
623 * #endif
624 * };
625 */
626
627 char *
__gnat_servent_s_name(struct servent * s)628 __gnat_servent_s_name (struct servent * s)
629 {
630 return s->s_name;
631 }
632
633 char *
__gnat_servent_s_alias(struct servent * s,int index)634 __gnat_servent_s_alias (struct servent * s, int index)
635 {
636 return s->s_aliases[index];
637 }
638
639 unsigned short
__gnat_servent_s_port(struct servent * s)640 __gnat_servent_s_port (struct servent * s)
641 {
642 return s->s_port;
643 }
644
645 char *
__gnat_servent_s_proto(struct servent * s)646 __gnat_servent_s_proto (struct servent * s)
647 {
648 return s->s_proto;
649 }
650
651 #endif /* defined(HAVE_SOCKETS) */
652