diff --git a/src/libckpool.c b/src/libckpool.c index ff502a58..ec07b449 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -16,11 +16,9 @@ #include #include #include -#include #include #include #include -#include #include "ckpool.h" #include "libckpool.h" @@ -41,6 +39,20 @@ void keep_sockalive(int fd) setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &tcp_keepintvl, sizeof(tcp_keepintvl)); } +void noblock_socket(int fd) +{ + int flags = fcntl(fd, F_GETFL, 0); + + fcntl(fd, F_SETFL, O_NONBLOCK | flags); +} + +void block_socket(int fd) +{ + int flags = fcntl(fd, F_GETFL, 0); + + fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); +} + /* Align a size_t to 4 byte boundaries for fussy arches */ void align_len(size_t *len) { diff --git a/src/libckpool.h b/src/libckpool.h index 7b7dadbf..d0feeec2 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -13,8 +13,30 @@ #define LIBCKPOOL_H #include +#include + +static inline bool sock_connecting(void) +{ + return errno == EINPROGRESS; +} + +static inline bool sock_blocks(void) +{ + return (errno == EAGAIN || errno == EWOULDBLOCK); +} +static inline bool sock_timeout(void) +{ + return (errno == ETIMEDOUT); +} +static inline bool interrupted(void) +{ + return (errno == EINTR); +} void keep_sockalive(int fd); +void noblock_socket(int fd); +void block_socket(int fd); +bool sock_connecting(void); void align_len(size_t *len); void __bin2hex(uchar *s, const uchar *p, size_t len); void *bin2hex(const uchar *p, size_t len);