skalibs

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

socket_local6.c (674B)


      1 /* ISC license. */
      2 
      3 #include <skalibs/nonposix.h>
      4 
      5 #include <sys/socket.h>
      6 #include <netinet/in.h>
      7 #include <string.h>
      8 
      9 #include <skalibs/uint16.h>
     10 #include <skalibs/socket.h>
     11 #include <skalibs/ip46.h>
     12 
     13 #ifdef SKALIBS_IPV6_ENABLED
     14 
     15 int socket_local6 (int s, char *ip, uint16_t *port)
     16 {
     17   struct sockaddr_in6 sa ;
     18   socklen_t dummy = sizeof sa ;
     19   if (getsockname(s, (struct sockaddr *)&sa, &dummy) == -1) return -1 ;
     20   memcpy(ip, sa.sin6_addr.s6_addr, 16) ;
     21   *port = uint16_big(sa.sin6_port) ;
     22   return 0 ;
     23 }
     24 
     25 #else
     26 
     27 #include <errno.h>
     28 
     29 int socket_local6 (int s, char *ip, uint16_t *port)
     30 {
     31   (void)s ;
     32   (void)ip ;
     33   (void)port ;
     34   return (errno = ENOSYS, -1) ;
     35 }
     36 
     37 #endif