{
int res;
int val;
+ bool ok;
- /* close fd on exec */
- res = fcntl(sock, F_SETFD, FD_CLOEXEC);
- if (res < 0)
- goto fail;
-
- /* when no data available, return EAGAIN instead blocking */
- socket_set_nonblocking(sock, 1);
-
-#ifdef SO_NOSIGPIPE
- /* disallow SIGPIPE, if possible */
- val = 1;
- res = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof(val));
- if (res < 0)
+ /*
+ * Generic stuff + nonblock.
+ */
+ ok = socket_setup(sock, true);
+ if (!ok)
goto fail;
-#endif
/*
* Following options are for network sockets
if (is_unix)
return true;
- /* the keepalive stuff needs some poking before enbling */
- if (cf_tcp_keepalive) {
- /* turn on socket keepalive */
- val = 1;
- res = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
- if (res < 0)
- goto fail;
-#ifdef __linux__
- /* set count of keepalive packets */
- if (cf_tcp_keepcnt > 0) {
- val = cf_tcp_keepcnt;
- res = setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val));
- if (res < 0)
- goto fail;
- }
- /* how long the connection can stay idle before sending keepalive pkts */
- if (cf_tcp_keepidle) {
- val = cf_tcp_keepidle;
- res = setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val));
- if (res < 0)
- goto fail;
- }
- /* time between packets */
- if (cf_tcp_keepintvl) {
- val = cf_tcp_keepintvl;
- res = setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val));
- if (res < 0)
- goto fail;
- }
-#else
-#ifdef TCP_KEEPALIVE
- if (cf_tcp_keepidle) {
- val = cf_tcp_keepidle;
- res = setsockopt(sock, IPPROTO_TCP, TCP_KEEPALIVE, &val, sizeof(val));
- if (res < 0)
- goto fail;
- }
-#endif
-#endif
- }
+ /*
+ * TCP Keepalive
+ */
+ ok = socket_set_keepalive(sock, cf_tcp_keepalive, cf_tcp_keepidle,
+ cf_tcp_keepintvl, cf_tcp_keepcnt);
+ if (!ok)
+ goto fail;
- /* set in-kernel socket buffer size */
+ /*
+ * set in-kernel socket buffer size
+ */
if (cf_tcp_socket_buffer) {
val = cf_tcp_socket_buffer;
res = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));