skalibs

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

socket_recv4.c (568B)


      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 #include <errno.h>
      9 
     10 #include <skalibs/uint16.h>
     11 #include <skalibs/socket.h>
     12 
     13 ssize_t socket_recv4 (int s, char *buf, size_t len, char *ip, uint16_t *port)
     14 {
     15   ssize_t r ;
     16   struct sockaddr_in sa ;
     17   socklen_t dummy = sizeof sa ;
     18   do r = recvfrom(s, buf, len, 0, (struct sockaddr *)&sa, &dummy) ;
     19   while ((r == -1) && (errno == EINTR)) ;
     20   if (r == -1) return -1 ;
     21   memcpy(ip, &sa.sin_addr, 4) ;
     22   *port = uint16_big(sa.sin_port) ;
     23   return r ;
     24 }