socket_accept6.c (1299B)
1 /* ISC license. */ 2 3 #include <skalibs/sysdeps.h> 4 #include <skalibs/nonposix.h> 5 6 #include <sys/socket.h> 7 #include <netinet/in.h> 8 #include <string.h> 9 #include <errno.h> 10 11 #include <skalibs/uint16.h> 12 #include <skalibs/fcntl.h> 13 #include <skalibs/djbunix.h> 14 #include <skalibs/ip46.h> 15 #include <skalibs/socket.h> 16 17 #ifdef SKALIBS_IPV6_ENABLED 18 19 int socket_accept6_internal (int s, char *ip6, uint16_t *port, unsigned int options) 20 { 21 int fd ; 22 struct sockaddr_in6 sa ; 23 socklen_t dummy = sizeof sa ; 24 do 25 #ifdef SKALIBS_HASACCEPT4 26 fd = accept4(s, (struct sockaddr *)&sa, &dummy, ((options & O_NONBLOCK) ? SOCK_NONBLOCK : 0) | ((options & O_CLOEXEC) ? SOCK_CLOEXEC : 0)) ; 27 #else 28 fd = accept(s, (struct sockaddr *)&sa, &dummy) ; 29 #endif 30 while ((fd < 0) && (errno == EINTR)) ; 31 if (fd < 0) return -1 ; 32 #ifndef SKALIBS_HASACCEPT4 33 if ((((options & O_NONBLOCK) ? ndelay_on(fd) : ndelay_off(fd)) < 0) 34 || (((options & O_CLOEXEC) ? coe(fd) : uncoe(fd)) < 0)) 35 { 36 fd_close(fd) ; 37 return -1 ; 38 } 39 #endif 40 memcpy(ip6, sa.sin6_addr.s6_addr, 16) ; 41 *port = uint16_big(sa.sin6_port) ; 42 return fd ; 43 } 44 45 #else 46 47 int socket_accept6_internal (int s, char *ip6, uint16_t *port, unsigned int options) 48 { 49 (void)s ; 50 (void)ip6 ; 51 (void)port ; 52 (void)options ; 53 return (errno = ENOSYS, -1) ; 54 } 55 56 #endif