From b2a333e3731174e38c94cf1c2609335c3d3b8368 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 8 Apr 2014 20:28:17 +1000 Subject: [PATCH] Add more defines and begin libckpool with sock_keepalive --- configure.ac | 4 +++- src/ckpool.h | 46 ++++++++++++++++++++++------------------------ src/libckpool.c | 28 ++++++++++++++++++++++++++++ src/libckpool.h | 2 ++ 4 files changed, 55 insertions(+), 25 deletions(-) diff --git a/configure.ac b/configure.ac index 10fd9111..ea90ec33 100644 --- a/configure.ac +++ b/configure.ac @@ -32,8 +32,10 @@ AC_FUNC_ALLOCA PKG_PROG_PKG_CONFIG() AC_CHECK_HEADERS(stdio.h stdlib.h fcntl.h sys/time.h unistd.h) -AC_CHECK_HEADERS(ctype.h errno.h) +AC_CHECK_HEADERS(ctype.h errno.h byteswap.h) AC_CHECK_HEADERS(endian.h sys/endian.h arpa/inet.h) AC_CHECK_HEADERS(alloca.h pthread.h) +AC_CHECK_HEADERS(sys/types.h sys/socket.h) +AC_CHECK_HEADERS(stdint.h netinet/in.h netinet/tcp.h) AC_OUTPUT([Makefile] [src/Makefile]) diff --git a/src/ckpool.h b/src/ckpool.h index 85a61dbd..7b6f4ff8 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -14,6 +14,8 @@ #include "config.h" +#include + #if HAVE_BYTESWAP_H # include #endif @@ -24,6 +26,12 @@ # include #endif +#ifndef bswap_16 + #define bswap_16 __builtin_bswap16 + #define bswap_32 __builtin_bswap32 + #define bswap_64 __builtin_bswap64 +#endif + /* This assumes htobe32 is a macro in endian.h, and if it doesn't exist, then * htobe64 also won't exist */ #ifndef htobe32 @@ -59,7 +67,7 @@ #define __maybe_unused __attribute__((unused)) #define uninitialised_var(x) x = x -static inline void swap256(void *dest_p, const void *src_p) +static inline void swap_256(void *dest_p, const void *src_p) { uint32_t *dest = dest_p; const uint32_t *src = src_p; @@ -74,48 +82,38 @@ static inline void swap256(void *dest_p, const void *src_p) dest[7] = src[0]; } -static inline void bswap256(void *dest_p, const void *src_p) +static inline void bswap_256(void *dest_p, const void *src_p) { uint32_t *dest = dest_p; const uint32_t *src = src_p; - dest[0] = bswap32(src[7]); - dest[1] = bswap32(src[6]); - dest[2] = bswap32(src[5]); - dest[3] = bswap32(src[4]); - dest[4] = bswap32(src[3]); - dest[5] = bswap32(src[2]); - dest[6] = bswap32(src[1]); - dest[7] = bswap32(src[0]); -} - -static inline void flip80(void *dest_p, const void *src_p) -{ - uint32_t *dest = dest_p; - const uint32_t *src = src_p; - int i; - - for (i = 0; i < 20; i++) - dest[i] = swab32(src[i]); + dest[0] = bswap_32(src[7]); + dest[1] = bswap_32(src[6]); + dest[2] = bswap_32(src[5]); + dest[3] = bswap_32(src[4]); + dest[4] = bswap_32(src[3]); + dest[5] = bswap_32(src[2]); + dest[6] = bswap_32(src[1]); + dest[7] = bswap_32(src[0]); } -static inline void flip32(void *dest_p, const void *src_p) +static inline void flip_32(void *dest_p, const void *src_p) { uint32_t *dest = dest_p; const uint32_t *src = src_p; int i; for (i = 0; i < 8; i++) - dest[i] = swab32(src[i]); + dest[i] = bswap_32(src[i]); } -static inline void flip80(void *dest_p, const void *src_p) +static inline void flip_80(void *dest_p, const void *src_p) { uint32_t *dest = dest_p; const uint32_t *src = src_p; int i; for (i = 0; i < 20; i++) - dest[i] = swab32(src[i]); + dest[i] = bswap_32(src[i]); } #endif /* CKPOOL_H */ diff --git a/src/libckpool.c b/src/libckpool.c index f13af11e..7e5167d0 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -6,3 +6,31 @@ * Software Foundation; either version 3 of the License, or (at your option) * any later version. See COPYING for more details. */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include + +#include "ckpool.h" +#include "libckpool.h" + +void keep_sockalive(int fd) +{ + const int tcp_one = 1; + const int tcp_keepidle = 45; + const int tcp_keepintvl = 30; + int flags = fcntl(fd, F_GETFL, 0); + + fcntl(fd, F_SETFL, O_NONBLOCK | flags); + + setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (const void *)&tcp_one, sizeof(tcp_one)); + setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&tcp_one, sizeof(tcp_one)); + setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &tcp_one, sizeof(tcp_one)); + setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &tcp_keepidle, sizeof(tcp_keepidle)); + setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &tcp_keepintvl, sizeof(tcp_keepintvl)); +} diff --git a/src/libckpool.h b/src/libckpool.h index 9bf4fa0a..a169170c 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -12,4 +12,6 @@ #ifndef LIBCKPOOL_H #define LIBCKPOOL_H +void keep_sockalive(int fd); + #endif /* LIBCKPOOL_H */