skalibs

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

socket_local4.c (451B)


      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 
     12 int socket_local4 (int s, char *ip, uint16_t *port)
     13 {
     14   struct sockaddr_in sa ;
     15   socklen_t dummy = sizeof sa ;
     16   if (getsockname(s, (struct sockaddr *)&sa, &dummy) == -1) return -1 ;
     17   memcpy(ip, &sa.sin_addr.s_addr, 4) ;
     18   *port = uint16_big(sa.sin_port) ;
     19   return 0 ;
     20 }