socket_connect4.c (601B)
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 int socket_connect4 (int s, char const *ip, uint16_t port) 14 { 15 int 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.s_addr, ip, 4) ; 21 do r = connect(s, (struct sockaddr *)&sa, sizeof sa) ; 22 while ((r == -1) && (errno == EINTR)) ; 23 if ((r == -1) && (errno == EALREADY)) errno = EINPROGRESS ; 24 return r ; 25 }