]>
Commit | Line | Data |
---|---|---|
bbc27441 | 1 | /* |
4ac4a490 | 2 | * Copyright (C) 1996-2017 The Squid Software Foundation and contributors |
bbc27441 AJ |
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 | ||
f7f3304a | 9 | #include "squid.h" |
a011edee | 10 | #include "CachePeer.h" |
cfd66529 AJ |
11 | #include "cbdata.h" |
12 | #include "comm.h" | |
13 | #include "comm/Connection.h" | |
be364179 | 14 | #include "fde.h" |
e8dca475 | 15 | #include "neighbors.h" |
4b307ad4 | 16 | #include "security/NegotiationHistory.h" |
c5c06f02 | 17 | #include "SquidConfig.h" |
be364179 | 18 | #include "SquidTime.h" |
cfd66529 | 19 | |
a011edee | 20 | class CachePeer; |
97c81191 AJ |
21 | bool |
22 | Comm::IsConnOpen(const Comm::ConnectionPointer &conn) | |
23 | { | |
24 | return conn != NULL && conn->isOpen(); | |
25 | } | |
26 | ||
cfd66529 | 27 | Comm::Connection::Connection() : |
f53969cc SM |
28 | peerType(HIER_NONE), |
29 | fd(-1), | |
30 | tos(0), | |
31 | nfmark(0), | |
32 | flags(COMM_NONBLOCKING), | |
d59e4742 | 33 | peer_(nullptr), |
2bcab852 CT |
34 | startTime_(squid_curtime), |
35 | tlsHistory(nullptr) | |
73c36fd9 AJ |
36 | { |
37 | *rfc931 = 0; // quick init the head. the rest does not matter. | |
38 | } | |
cfd66529 | 39 | |
be364179 | 40 | static int64_t lost_conn = 0; |
aed188fd | 41 | Comm::Connection::~Connection() |
739b352a | 42 | { |
be364179 | 43 | if (fd >= 0) { |
c460b3d7 AJ |
44 | debugs(5, 4, "BUG #3329: Orphan Comm::Connection: " << *this); |
45 | debugs(5, 4, "NOTE: " << ++lost_conn << " Orphans since last started."); | |
42a61e53 | 46 | close(); |
be364179 AJ |
47 | } |
48 | ||
a3c6762c | 49 | cbdataReferenceDone(peer_); |
2bcab852 CT |
50 | |
51 | delete tlsHistory; | |
739b352a AJ |
52 | } |
53 | ||
5ae21d99 | 54 | Comm::ConnectionPointer |
aed188fd | 55 | Comm::Connection::copyDetails() const |
739b352a | 56 | { |
aed188fd | 57 | ConnectionPointer c = new Comm::Connection; |
739b352a | 58 | |
7fb5be3e | 59 | c->setAddrs(local, remote); |
5229395c | 60 | c->peerType = peerType; |
aed188fd | 61 | c->tos = tos; |
f123f5e9 | 62 | c->nfmark = nfmark; |
aed188fd | 63 | c->flags = flags; |
8aec3e1b | 64 | c->startTime_ = startTime_; |
802540f2 | 65 | |
aed188fd AJ |
66 | // ensure FD is not open in the new copy. |
67 | c->fd = -1; | |
f9b72e0c | 68 | |
a3c6762c FC |
69 | // ensure we have a cbdata reference to peer_ not a straight ptr copy. |
70 | c->peer_ = cbdataReference(getPeer()); | |
cfd66529 | 71 | |
aed188fd | 72 | return c; |
cfd66529 | 73 | } |
739b352a | 74 | |
55cbb02b AJ |
75 | void |
76 | Comm::Connection::close() | |
77 | { | |
5229395c | 78 | if (isOpen()) { |
55cbb02b | 79 | comm_close(fd); |
b54a7c5a CT |
80 | noteClosure(); |
81 | } | |
82 | } | |
83 | ||
84 | void | |
85 | Comm::Connection::noteClosure() | |
86 | { | |
87 | if (isOpen()) { | |
5229395c | 88 | fd = -1; |
a3c6762c | 89 | if (CachePeer *p=getPeer()) |
e8dca475 | 90 | peerConnClosed(p); |
5229395c | 91 | } |
55cbb02b AJ |
92 | } |
93 | ||
a3c6762c | 94 | CachePeer * |
418b3087 AJ |
95 | Comm::Connection::getPeer() const |
96 | { | |
a3c6762c FC |
97 | if (cbdataReferenceValid(peer_)) |
98 | return peer_; | |
418b3087 AJ |
99 | |
100 | return NULL; | |
101 | } | |
102 | ||
739b352a | 103 | void |
a3c6762c | 104 | Comm::Connection::setPeer(CachePeer *p) |
739b352a AJ |
105 | { |
106 | /* set to self. nothing to do. */ | |
418b3087 | 107 | if (getPeer() == p) |
739b352a AJ |
108 | return; |
109 | ||
a3c6762c | 110 | cbdataReferenceDone(peer_); |
746beefe | 111 | if (p) { |
a3c6762c | 112 | peer_ = cbdataReference(p); |
746beefe | 113 | } |
739b352a | 114 | } |
f53969cc | 115 | |
c5c06f02 CT |
116 | time_t |
117 | Comm::Connection::timeLeft(const time_t idleTimeout) const | |
118 | { | |
119 | if (!Config.Timeout.pconnLifetime) | |
120 | return idleTimeout; | |
121 | ||
122 | const time_t lifeTimeLeft = lifeTime() < Config.Timeout.pconnLifetime ? Config.Timeout.pconnLifetime - lifeTime() : 1; | |
123 | return min(lifeTimeLeft, idleTimeout); | |
124 | } | |
6a515ac4 | 125 | |
2bcab852 CT |
126 | Security::NegotiationHistory * |
127 | Comm::Connection::tlsNegotiations() | |
128 | { | |
129 | if (!tlsHistory) | |
130 | tlsHistory = new Security::NegotiationHistory; | |
131 | return tlsHistory; | |
132 | } | |
133 |