Connection.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 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 "CachePeer.h"
11 #include "cbdata.h"
12 #include "comm.h"
13 #include "comm/Connection.h"
14 #include "fde.h"
15 #include "FwdState.h"
16 #include "neighbors.h"
18 #include "SquidConfig.h"
19 #include "SquidTime.h"
20 #include <ostream>
21 
22 class CachePeer;
23 bool
25 {
26  return conn != NULL && conn->isOpen();
27 }
28 
30  peerType(HIER_NONE),
31  fd(-1),
32  tos(0),
33  nfmark(0),
35  peer_(nullptr),
36  startTime_(squid_curtime),
37  tlsHistory(nullptr)
38 {
39  *rfc931 = 0; // quick init the head. the rest does not matter.
40 }
41 
42 static int64_t lost_conn = 0;
44 {
45  if (fd >= 0) {
46  debugs(5, 4, "BUG #3329: Orphan Comm::Connection: " << *this);
47  debugs(5, 4, "NOTE: " << ++lost_conn << " Orphans since last started.");
48  close();
49  }
50 
51  cbdataReferenceDone(peer_);
52 
53  delete tlsHistory;
54 }
55 
58 {
60 
61  c->setAddrs(local, remote);
62  c->peerType = peerType;
63  c->tos = tos;
64  c->nfmark = nfmark;
65  c->nfConnmark = nfConnmark;
66  c->flags = flags;
67  c->startTime_ = startTime_;
68 
69  // ensure FD is not open in the new copy.
70  c->fd = -1;
71 
72  // ensure we have a cbdata reference to peer_ not a straight ptr copy.
73  c->peer_ = cbdataReference(getPeer());
74 
75  return c;
76 }
77 
78 void
80 {
81  if (isOpen()) {
82  comm_close(fd);
83  noteClosure();
84  }
85 }
86 
87 void
89 {
90  if (isOpen()) {
91  fd = -1;
92  if (CachePeer *p=getPeer())
94  }
95 }
96 
97 CachePeer *
99 {
100  if (cbdataReferenceValid(peer_))
101  return peer_;
102 
103  return NULL;
104 }
105 
106 void
108 {
109  /* set to self. nothing to do. */
110  if (getPeer() == p)
111  return;
112 
113  cbdataReferenceDone(peer_);
114  if (p) {
115  peer_ = cbdataReference(p);
116  }
117 }
118 
119 time_t
120 Comm::Connection::timeLeft(const time_t idleTimeout) const
121 {
123  return idleTimeout;
124 
125  const time_t lifeTimeLeft = lifeTime() < Config.Timeout.pconnLifetime ? Config.Timeout.pconnLifetime - lifeTime() : 1;
126  return min(lifeTimeLeft, idleTimeout);
127 }
128 
131 {
132  if (!tlsHistory)
133  tlsHistory = new Security::NegotiationHistory;
134  return tlsHistory;
135 }
136 
137 time_t
138 Comm::Connection::connectTimeout(const time_t fwdStart) const
139 {
140  // a connection opening timeout (ignoring forwarding time limits for now)
141  const CachePeer *peer = getPeer();
142  const time_t ctimeout = peer ? peerConnectTimeout(peer) : Config.Timeout.connect;
143 
144  // time we have left to finish the whole forwarding process
145  const time_t fwdTimeLeft = FwdState::ForwardTimeout(fwdStart);
146 
147  // The caller decided to connect. If there is no time left, to protect
148  // connecting code from trying to establish a connection while a zero (i.e.,
149  // "immediate") timeout notification is firing, ensure a positive timeout.
150  // XXX: This hack gives some timed-out forwarding sequences more time than
151  // some sequences that have not quite reached the forwarding timeout yet!
152  const time_t ftimeout = fwdTimeLeft ? fwdTimeLeft : 5; // seconds
153 
154  return min(ctimeout, ftimeout);
155 }
156 
157 std::ostream &
158 operator << (std::ostream &os, const Comm::Connection &conn)
159 {
160  os << "local=" << conn.local << " remote=" << conn.remote;
161  if (conn.peerType)
162  os << ' ' << hier_code_str[conn.peerType];
163  if (conn.fd >= 0)
164  os << " FD " << conn.fd;
165  if (conn.flags != COMM_UNSET)
166  os << " flags=" << conn.flags;
167 #if USE_IDENT
168  if (*conn.rfc931)
169  os << " IDENT::" << conn.rfc931;
170 #endif
171  return os;
172 }
173 
time_t timeLeft(const time_t idleTimeout) const
Definition: Connection.cc:120
bool isOpen(const int fd)
Definition: comm.cc:86
#define cbdataReferenceDone(var)
Definition: cbdata.h:350
Security::NegotiationHistory * tlsNegotiations()
Definition: Connection.cc:130
nfmark_t nfmark
Definition: Connection.h:152
time_t peerConnectTimeout(const CachePeer *peer)
Definition: neighbors.cc:1190
time_t pconnLifetime
pconn_lifetime in squid.conf
Definition: SquidConfig.h:112
#define COMM_UNSET
Definition: Connection.h:43
char * p
Definition: membanger.c:43
int conn
the current server connection FD
Definition: Transport.cc:26
static int64_t lost_conn
Definition: Connection.cc:42
time_t squid_curtime
Definition: stub_time.cc:17
nfmark_t nfConnmark
Definition: Connection.h:160
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:24
void peerConnClosed(CachePeer *p)
Notifies peer of an associated connection closure.
Definition: neighbors.cc:241
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:124
#define cbdataReference(var)
Definition: cbdata.h:341
static time_t ForwardTimeout(const time_t fwdStart)
time left to finish the whole forwarding process (which started at fwdStart)
Definition: FwdState.cc:410
time_t connectTimeout(const time_t fwdStart) const
Definition: Connection.cc:138
CachePeer * getPeer() const
Definition: Connection.cc:98
struct SquidConfig::@99 Timeout
struct CachePeer::@32::@38 flags
Ip::Address local
Definition: Connection.h:135
char rfc931[USER_IDENT_SZ]
Definition: Connection.h:165
void setPeer(CachePeer *p)
Definition: Connection.cc:107
Ip::Address remote
Definition: Connection.h:138
time_t connect
Definition: SquidConfig.h:105
hier_code peerType
Definition: Connection.h:141
int cbdataReferenceValid(const void *p)
Definition: cbdata.cc:412
const char * hier_code_str[]
#define COMM_NONBLOCKING
Definition: Connection.h:44
std::ostream & operator<<(std::ostream &os, const Comm::Connection &conn)
Definition: Connection.cc:158
void noteClosure()
Definition: Connection.cc:88
ConnectionPointer copyDetails() const
Definition: Connection.cc:57
CachePeer * peer_
Definition: Connection.h:174
void setAddrs(const Ip::Address &aLocal, const Ip::Address &aRemote)
Definition: Connection.h:92
class SquidConfig Config
Definition: SquidConfig.cc:12
#define comm_close(x)
Definition: comm.h:28
#define NULL
Definition: types.h:166
A const & min(A const &lhs, A const &rhs)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors