Connection.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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->flags = flags;
66  c->startTime_ = startTime_;
67 
68  // ensure FD is not open in the new copy.
69  c->fd = -1;
70 
71  // ensure we have a cbdata reference to peer_ not a straight ptr copy.
72  c->peer_ = cbdataReference(getPeer());
73 
74  return c;
75 }
76 
77 void
79 {
80  if (isOpen()) {
81  comm_close(fd);
82  noteClosure();
83  }
84 }
85 
86 void
88 {
89  if (isOpen()) {
90  fd = -1;
91  if (CachePeer *p=getPeer())
93  }
94 }
95 
96 CachePeer *
98 {
99  if (cbdataReferenceValid(peer_))
100  return peer_;
101 
102  return NULL;
103 }
104 
105 void
107 {
108  /* set to self. nothing to do. */
109  if (getPeer() == p)
110  return;
111 
112  cbdataReferenceDone(peer_);
113  if (p) {
114  peer_ = cbdataReference(p);
115  }
116 }
117 
118 time_t
119 Comm::Connection::timeLeft(const time_t idleTimeout) const
120 {
122  return idleTimeout;
123 
124  const time_t lifeTimeLeft = lifeTime() < Config.Timeout.pconnLifetime ? Config.Timeout.pconnLifetime - lifeTime() : 1;
125  return min(lifeTimeLeft, idleTimeout);
126 }
127 
130 {
131  if (!tlsHistory)
132  tlsHistory = new Security::NegotiationHistory;
133  return tlsHistory;
134 }
135 
136 time_t
137 Comm::Connection::connectTimeout(const time_t fwdStart) const
138 {
139  // a connection opening timeout (ignoring forwarding time limits for now)
140  const CachePeer *peer = getPeer();
141  const time_t ctimeout = peer ? peerConnectTimeout(peer) : Config.Timeout.connect;
142 
143  // time we have left to finish the whole forwarding process
144  const time_t fwdTimeLeft = FwdState::ForwardTimeout(fwdStart);
145 
146  // The caller decided to connect. If there is no time left, to protect
147  // connecting code from trying to establish a connection while a zero (i.e.,
148  // "immediate") timeout notification is firing, ensure a positive timeout.
149  // XXX: This hack gives some timed-out forwarding sequences more time than
150  // some sequences that have not quite reached the forwarding timeout yet!
151  const time_t ftimeout = fwdTimeLeft ? fwdTimeLeft : 5; // seconds
152 
153  return min(ctimeout, ftimeout);
154 }
155 
156 std::ostream &
157 operator << (std::ostream &os, const Comm::Connection &conn)
158 {
159  os << "local=" << conn.local << " remote=" << conn.remote;
160  if (conn.peerType)
161  os << ' ' << hier_code_str[conn.peerType];
162  if (conn.fd >= 0)
163  os << " FD " << conn.fd;
164  if (conn.flags != COMM_UNSET)
165  os << " flags=" << conn.flags;
166 #if USE_IDENT
167  if (*conn.rfc931)
168  os << " IDENT::" << conn.rfc931;
169 #endif
170  return os;
171 }
172 
time_t timeLeft(const time_t idleTimeout) const
Definition: Connection.cc:119
bool isOpen(const int fd)
Definition: comm.cc:86
#define cbdataReferenceDone(var)
Definition: cbdata.h:350
Security::NegotiationHistory * tlsNegotiations()
Definition: Connection.cc:129
nfmark_t nfmark
Definition: Connection.h:150
time_t peerConnectTimeout(const CachePeer *peer)
Definition: neighbors.cc:1164
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
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:24
void peerConnClosed(CachePeer *p)
Notifies peer of an associated connection closure.
Definition: neighbors.cc:237
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
#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:409
time_t connectTimeout(const time_t fwdStart) const
Definition: Connection.cc:137
CachePeer * getPeer() const
Definition: Connection.cc:97
struct CachePeer::@32::@38 flags
Ip::Address local
Definition: Connection.h:135
char rfc931[USER_IDENT_SZ]
Definition: Connection.h:155
void setPeer(CachePeer *p)
Definition: Connection.cc:106
Ip::Address remote
Definition: Connection.h:138
time_t connect
Definition: SquidConfig.h:105
hier_code peerType
Definition: Connection.h:141
struct SquidConfig::@98 Timeout
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:157
void noteClosure()
Definition: Connection.cc:87
ConnectionPointer copyDetails() const
Definition: Connection.cc:57
CachePeer * peer_
Definition: Connection.h:164
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