Connection.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 #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 time_t
143 Comm::Connection::timeLeft(const time_t idleTimeout) const
144 {
146  return idleTimeout;
147 
148  const time_t lifeTimeLeft = lifeTime() < Config.Timeout.pconnLifetime ? Config.Timeout.pconnLifetime - lifeTime() : 1;
149  return min(lifeTimeLeft, idleTimeout);
150 }
151 
154 {
155  if (!tlsHistory)
156  tlsHistory = new Security::NegotiationHistory;
157  return tlsHistory;
158 }
159 
160 time_t
161 Comm::Connection::connectTimeout(const time_t fwdStart) const
162 {
163  // a connection opening timeout (ignoring forwarding time limits for now)
164  const CachePeer *peer = getPeer();
165  const auto ctimeout = peer ? peer->connectTimeout() : Config.Timeout.connect;
166 
167  // time we have left to finish the whole forwarding process
168  const time_t fwdTimeLeft = FwdState::ForwardTimeout(fwdStart);
169 
170  // The caller decided to connect. If there is no time left, to protect
171  // connecting code from trying to establish a connection while a zero (i.e.,
172  // "immediate") timeout notification is firing, ensure a positive timeout.
173  // XXX: This hack gives some timed-out forwarding sequences more time than
174  // some sequences that have not quite reached the forwarding timeout yet!
175  const time_t ftimeout = fwdTimeLeft ? fwdTimeLeft : 5; // seconds
176 
177  return min(ctimeout, ftimeout);
178 }
179 
180 ScopedId
182  return id.detach();
183 }
184 
185 std::ostream &
186 Comm::Connection::detailCodeContext(std::ostream &os) const
187 {
188  return os << Debug::Extra << "connection: " << *this;
189 }
190 
191 std::ostream &
192 Comm::operator << (std::ostream &os, const Connection &conn)
193 {
194  os << conn.id;
195  if (!conn.local.isNoAddr() || conn.local.port())
196  os << " local=" << conn.local;
197  if (!conn.remote.isNoAddr() || conn.remote.port())
198  os << " remote=" << conn.remote;
199  if (conn.peerType)
200  os << ' ' << hier_code_str[conn.peerType];
201  if (conn.fd >= 0)
202  os << " FD " << conn.fd;
203  if (conn.flags != COMM_UNSET)
204  os << " flags=" << conn.flags;
205  return os;
206 }
207 
hier_code peerType
Definition: Connection.h:152
time_t connect
Definition: SquidConfig.h:115
~Connection() override
Definition: Connection.cc:43
InstanceId< Connection, uint64_t > id
Definition: Connection.h:181
#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:192
time_t connectTimeout(const time_t fwdStart) const
Definition: Connection.cc:161
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:143
@ 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:146
CachePeer * getPeer() const
Definition: Connection.cc:121
ScopedId codeContextGist() const override
Definition: Connection.cc:181
Ip::Address remote
Definition: Connection.h:149
#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:186
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:153
#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