skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

socket_accept4.c (1042B)


      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/socket.h>
     15 
     16 int socket_accept4_internal (int s, char *ip, uint16_t *port, unsigned int options)
     17 {
     18   int fd ;
     19   struct sockaddr_in sa ;
     20   socklen_t dummy = sizeof sa ;
     21   do
     22 #ifdef SKALIBS_HASACCEPT4
     23     fd = accept4(s, (struct sockaddr *)&sa, &dummy, ((options & O_NONBLOCK) ? SOCK_NONBLOCK : 0) | ((options & O_CLOEXEC) ? SOCK_CLOEXEC : 0)) ;
     24 #else
     25     fd = accept(s, (struct sockaddr *)&sa, &dummy) ;
     26 #endif
     27   while ((fd < 0) && (errno == EINTR)) ;
     28   if (fd < 0) return -1 ;
     29 #ifndef SKALIBS_HASACCEPT4
     30   if ((((options & O_NONBLOCK) ? ndelay_on(fd) : ndelay_off(fd)) < 0)
     31    || (((options & O_CLOEXEC) ? coe(fd) : uncoe(fd)) < 0))
     32   {
     33     fd_close(fd) ;
     34     return -1 ;
     35   }
     36 #endif
     37   memcpy(ip, &sa.sin_addr.s_addr, 4) ;
     38   *port = uint16_big(sa.sin_port) ;
     39   return fd ;
     40 }