skalibs

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

socket_send4.c (578B)


      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_send4 (int s, char const *buf, size_t len, char const *ip, uint16_t port)
     14 {
     15   ssize_t r ;
     16   struct sockaddr_in sa ;
     17   memset(&sa, 0, sizeof sa) ;
     18   sa.sin_family = AF_INET ;
     19   sa.sin_port = uint16_big(port) ;
     20   memcpy(&sa.sin_addr, ip, 4) ;
     21   do r = sendto(s, buf, len, 0, (struct sockaddr *)&sa, sizeof sa) ;
     22   while ((r == -1) && (errno == EINTR)) ;
     23   return r ;
     24 }