Squid Web Cache master
Loading...
Searching...
No Matches
Tcp.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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 "compat/socket.h"
14#include "debug/Stream.h"
15
16#if HAVE_NETINET_TCP_H
17#include <netinet/tcp.h>
18#endif
19#if HAVE_NETINET_IN_H
20#include <netinet/in.h>
21#endif
22#include <type_traits>
23
25template <typename Option>
26static bool
27SetSocketOption(const int fd, const int level, const int optName, const Option &optValue)
28{
29 static_assert(std::is_trivially_copyable<Option>::value, "setsockopt() expects POD-like options");
30 static_assert(!std::is_same<Option, bool>::value, "setsockopt() uses int to represent boolean options");
31 if (xsetsockopt(fd, level, optName, &optValue, sizeof(optValue)) < 0) {
32 const auto xerrno = errno;
33 debugs(5, DBG_IMPORTANT, "ERROR: setsockopt(2) failure: " << xstrerr(xerrno));
34 // TODO: Generalize to throw on errors when some callers need that.
35 return false;
36 }
37 return true;
38}
39
41static bool
42SetBooleanSocketOption(const int fd, const int level, const int optName, const bool enable)
43{
44 const int optValue = enable ? 1 :0;
45 return SetSocketOption(fd, level, optName, optValue);
46}
47
48void
50{
51 if (!cfg.enabled)
52 return;
53
54#if defined(TCP_KEEPCNT)
55 if (cfg.timeout && cfg.interval) {
56 const int count = (cfg.timeout + cfg.interval - 1) / cfg.interval; // XXX: unsigned-to-signed conversion
57 (void)SetSocketOption(fd, IPPROTO_TCP, TCP_KEEPCNT, count);
58 }
59#endif
60#if defined(TCP_KEEPIDLE)
61 if (cfg.idle) {
62 // XXX: TCP_KEEPIDLE expects an int; cfg.idle is unsigned
63 (void)SetSocketOption(fd, IPPROTO_TCP, TCP_KEEPIDLE, cfg.idle);
64 }
65#endif
66#if defined(TCP_KEEPINTVL)
67 if (cfg.interval) {
68 // XXX: TCP_KEEPINTVL expects an int; cfg.interval is unsigned
69 (void)SetSocketOption(fd, IPPROTO_TCP, TCP_KEEPINTVL, cfg.interval);
70 }
71#endif
72 (void)SetBooleanSocketOption(fd, SOL_SOCKET, SO_KEEPALIVE, true);
73}
static bool SetSocketOption(const int fd, const int level, const int optName, const Option &optValue)
xsetsockopt(2) wrapper
Definition Tcp.cc:27
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:42
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
#define DBG_IMPORTANT
Definition Stream.h:38
#define debugs(SECTION, LEVEL, CONTENT)
Definition Stream.h:192
void ApplyTcpKeepAlive(int fd, const TcpKeepAlive &)
apply configured TCP keep-alive settings to the given FD socket
Definition Tcp.cc:49
int xsetsockopt(int socketFd, int level, int option, const void *value, socklen_t valueLength)
POSIX setsockopt(2) equivalent.
Definition socket.h:122
const char * xstrerr(int error)
Definition xstrerror.cc:83