skalibs

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

socket_remote6.c (675B)


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