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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors