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