Tcp.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 05 TCP Socket Functions */
10
11#include "squid.h"
12#include "comm/Tcp.h"
13#include "debug/Stream.h"
14
15#if HAVE_NETINET_TCP_H
16#include <netinet/tcp.h>
17#endif
18#if HAVE_NETINET_IN_H
19#include <netinet/in.h>
20#endif
21#if HAVE_SYS_SOCKET_H
22#include <sys/socket.h>
23#endif
24#include <type_traits>
25
27template <typename Option>
28static bool
29SetSocketOption(const int fd, const int level, const int optName, const Option &optValue)
30{
31 static_assert(std::is_trivially_copyable<Option>::value, "setsockopt() expects POD-like options");
32 static_assert(!std::is_same<Option, bool>::value, "setsockopt() uses int to represent boolean options");
33 if (setsockopt(fd, level, optName, &optValue, sizeof(optValue)) < 0) {
34 const auto xerrno = errno;
35 debugs(5, DBG_IMPORTANT, "ERROR: setsockopt(2) failure: " << xstrerr(xerrno));
36 // TODO: Generalize to throw on errors when some callers need that.
37 return false;
38 }
39 return true;
40}
41
43static bool
44SetBooleanSocketOption(const int fd, const int level, const int optName, const bool enable)
45{
46 const int optValue = enable ? 1 :0;
47 return SetSocketOption(fd, level, optName, optValue);
48}
49
50void
52{
53 if (!cfg.enabled)
54 return;
55
56#if defined(TCP_KEEPCNT)
57 if (cfg.timeout && cfg.interval) {
58 const int count = (cfg.timeout + cfg.interval - 1) / cfg.interval; // XXX: unsigned-to-signed conversion
59 (void)SetSocketOption(fd, IPPROTO_TCP, TCP_KEEPCNT, count);
60 }
61#endif
62#if defined(TCP_KEEPIDLE)
63 if (cfg.idle) {
64 // XXX: TCP_KEEPIDLE expects an int; cfg.idle is unsigned
65 (void)SetSocketOption(fd, IPPROTO_TCP, TCP_KEEPIDLE, cfg.idle);
66 }
67#endif
68#if defined(TCP_KEEPINTVL)
69 if (cfg.interval) {
70 // XXX: TCP_KEEPINTVL expects an int; cfg.interval is unsigned
71 (void)SetSocketOption(fd, IPPROTO_TCP, TCP_KEEPINTVL, cfg.interval);
72 }
73#endif
74 (void)SetBooleanSocketOption(fd, SOL_SOCKET, SO_KEEPALIVE, true);
75}
static bool SetSocketOption(const int fd, const int level, const int optName, const Option &optValue)
setsockopt(2) wrapper
Definition: Tcp.cc:29
static bool SetBooleanSocketOption(const int fd, const int level, const int optName, const bool enable)
setsockopt(2) wrapper for setting typical on/off options
Definition: Tcp.cc:44
Configuration settings for the TCP keep-alive feature.
Definition: Tcp.h:17
unsigned int idle
Definition: Tcp.h:19
unsigned int timeout
Definition: Tcp.h:21
unsigned int interval
Definition: Tcp.h:20
bool enabled
Definition: Tcp.h:22
#define DBG_IMPORTANT
Definition: Stream.h:38
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:194
void ApplyTcpKeepAlive(int fd, const TcpKeepAlive &)
apply configured TCP keep-alive settings to the given FD socket
Definition: Tcp.cc:51
const char * xstrerr(int error)
Definition: xstrerror.cc:83

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors