Connection.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 #include "squid.h"
10 #include "base/JobWait.h"
11 #include "CachePeer.h"
12 #include "cbdata.h"
13 #include "comm.h"
14 #include "comm/Connection.h"
15 #include "fde.h"
16 #include "FwdState.h"
17 #include "neighbors.h"
19 #include "SquidConfig.h"
20 
21 #include <ostream>
22 
23 InstanceIdDefinitions(Comm::Connection, "conn", uint64_t);
24 
25 class CachePeer;
26 bool
28 {
29  return conn != nullptr && conn->isOpen();
30 }
31 
33  peerType(HIER_NONE),
34  fd(-1),
35  tos(0),
36  nfmark(0),
38  peer_(nullptr),
39  startTime_(squid_curtime),
40  tlsHistory(nullptr)
41 {}
42 
44 {
45  if (fd >= 0) {
46  if (flags & COMM_ORPHANED) {
47  debugs(5, 5, "closing orphan: " << *this);
48  } else {
49  static uint64_t losses = 0;
50  ++losses;
51  debugs(5, 4, "BUG #3329: Lost orphan #" << losses << ": " << *this);
52  }
53  close();
54  }
55 
56  cbdataReferenceDone(peer_);
57 
58  delete tlsHistory;
59 }
60 
63 {
64  const ConnectionPointer clone = new Comm::Connection;
65  auto &c = *clone; // optimization
66 
67  /*
68  * Copy or excuse each data member. Excused members do not belong to a
69  * Connection configuration profile because their values cannot be reused
70  * across (co-existing) Connection objects and/or are tied to their own
71  * object lifetime.
72  */
73 
74  c.setAddrs(local, remote);
75  c.peerType = peerType;
76  // fd excused
77  c.tos = tos;
78  c.nfmark = nfmark;
79  c.nfConnmark = nfConnmark;
80  // COMM_ORPHANED is not a part of connection opening instructions
81  c.flags = flags & ~COMM_ORPHANED;
82 
83 #if USE_SQUID_EUI
84  // These are currently only set when accepting connections and never used
85  // for establishing new ones, so this copying is currently in vain, but,
86  // technically, they can be a part of connection opening instructions.
87  c.remoteEui48 = remoteEui48;
88  c.remoteEui64 = remoteEui64;
89 #endif
90 
91  // id excused
92  c.peer_ = cbdataReference(getPeer());
93  // startTime_ excused
94  // tlsHistory excused
95 
96  debugs(5, 5, this << " made " << c);
97  assert(!c.isOpen());
98  return clone;
99 }
100 
101 void
103 {
104  if (isOpen()) {
105  comm_close(fd);
106  noteClosure();
107  }
108 }
109 
110 void
112 {
113  if (isOpen()) {
114  fd = -1;
115  if (CachePeer *p=getPeer())
116  peerConnClosed(p);
117  }
118 }
119 
120 CachePeer *
122 {
123  if (cbdataReferenceValid(peer_))
124  return peer_;
125 
126  return nullptr;
127 }
128 
129 void
131 {
132  /* set to self. nothing to do. */
133  if (getPeer() == p)
134  return;
135 
136  cbdataReferenceDone(peer_);
137  if (p) {
138  peer_ = cbdataReference(p);
139  }
140 }
141 
142 bool
144 {
145  return peer_ && !cbdataReferenceValid(peer_);
146 }
147 
148 time_t
149 Comm::Connection::timeLeft(const time_t idleTimeout) const
150 {
152  return idleTimeout;
153 
154  const time_t lifeTimeLeft = lifeTime() < Config.Timeout.pconnLifetime ? Config.Timeout.pconnLifetime - lifeTime() : 1;
155  return min(lifeTimeLeft, idleTimeout);
156 }
157 
160 {
161  if (!tlsHistory)
162  tlsHistory = new Security::NegotiationHistory;
163  return tlsHistory;
164 }
165 
166 time_t
167 Comm::Connection::connectTimeout(const time_t fwdStart) const
168 {
169  // a connection opening timeout (ignoring forwarding time limits for now)
170  const CachePeer *peer = getPeer();
171  const auto ctimeout = peer ? peer->connectTimeout() : Config.Timeout.connect;
172 
173  // time we have left to finish the whole forwarding process
174  const time_t fwdTimeLeft = FwdState::ForwardTimeout(fwdStart);
175 
176  // The caller decided to connect. If there is no time left, to protect
177  // connecting code from trying to establish a connection while a zero (i.e.,
178  // "immediate") timeout notification is firing, ensure a positive timeout.
179  // XXX: This hack gives some timed-out forwarding sequences more time than
180  // some sequences that have not quite reached the forwarding timeout yet!
181  const time_t ftimeout = fwdTimeLeft ? fwdTimeLeft : 5; // seconds
182 
183  return min(ctimeout, ftimeout);
184 }
185 
186 ScopedId
188  return id.detach();
189 }
190 
191 std::ostream &
192 Comm::Connection::detailCodeContext(std::ostream &os) const
193 {
194  return os << Debug::Extra << "connection: " << *this;
195 }
196 
197 std::ostream &
198 Comm::operator << (std::ostream &os, const Connection &conn)
199 {
200  os << conn.id;
201  if (!conn.local.isNoAddr() || conn.local.port())
202  os << " local=" << conn.local;
203  if (!conn.remote.isNoAddr() || conn.remote.port())
204  os << " remote=" << conn.remote;
205  if (conn.peerType)
206  os << ' ' << hier_code_str[conn.peerType];
207  if (conn.fd >= 0)
208  os << " FD " << conn.fd;
209  if (conn.flags != COMM_UNSET)
210  os << " flags=" << conn.flags;
211  return os;
212 }
213 
hier_code peerType
Definition: Connection.h:155
time_t connect
Definition: SquidConfig.h:115
~Connection() override
Definition: Connection.cc:43
InstanceId< Connection, uint64_t > id
Definition: Connection.h:184
bool toGoneCachePeer() const
whether this is a connection to a cache_peer that was removed during reconfiguration
Definition: Connection.cc:143
#define comm_close(x)
Definition: comm.h:36
int cbdataReferenceValid(const void *p)
Definition: cbdata.cc:270
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:27
time_t connectTimeout() const
Definition: CachePeer.cc:120
#define cbdataReference(var)
Definition: cbdata.h:348
#define COMM_NONBLOCKING
Definition: Connection.h:46
std::ostream & operator<<(std::ostream &, const Connection &)
Definition: Connection.cc:198
time_t connectTimeout(const time_t fwdStart) const
Definition: Connection.cc:167
ConnectionPointer cloneProfile() const
Create a new closed Connection with the same configuration as this one.
Definition: Connection.cc:62
time_t timeLeft(const time_t idleTimeout) const
Definition: Connection.cc:149
@ HIER_NONE
Definition: hier_code.h:13
void peerConnClosed(CachePeer *p)
Notifies peer of an associated connection closure.
Definition: neighbors.cc:241
unsigned short port() const
Definition: Address.cc:798
Ip::Address local
Definition: Connection.h:149
CachePeer * getPeer() const
Definition: Connection.cc:121
ScopedId codeContextGist() const override
Definition: Connection.cc:187
Ip::Address remote
Definition: Connection.h:152
#define assert(EX)
Definition: assert.h:17
SSL Connection
Definition: Session.h:49
static time_t ForwardTimeout(const time_t fwdStart)
time left to finish the whole forwarding process (which started at fwdStart)
Definition: FwdState.cc:423
#define cbdataReferenceDone(var)
Definition: cbdata.h:357
struct CachePeer::@28::@34 flags
time_t squid_curtime
Definition: stub_libtime.cc:20
static std::ostream & Extra(std::ostream &)
Definition: debug.cc:1316
bool isNoAddr() const
Definition: Address.cc:304
time_t pconnLifetime
pconn_lifetime in squid.conf
Definition: SquidConfig.h:122
const char * hier_code_str[]
#define COMM_UNSET
Definition: Connection.h:45
InstanceIdDefinitions(Comm::Connection, "conn", uint64_t)
struct SquidConfig::@84 Timeout
std::ostream & detailCodeContext(std::ostream &os) const override
appends human-friendly context description line(s) to a cache.log record
Definition: Connection.cc:192
void setPeer(CachePeer *p)
Definition: Connection.cc:130
bool isOpen() const
Definition: Connection.h:101
bool isOpen(const int fd)
Definition: comm.cc:89
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
const A & min(A const &lhs, A const &rhs)
Security::NegotiationHistory * tlsNegotiations()
Definition: Connection.cc:159
#define COMM_ORPHANED
not registered with Comm and not owned by any connection-closing code
Definition: Connection.h:54
class SquidConfig Config
Definition: SquidConfig.cc:12

 

Introduction

Documentation

Support

Miscellaneous