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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors