]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm/Connection.h
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / comm / Connection.h
CommitLineData
ee0989f2 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
ee0989f2 3 *
bbc27441
AJ
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.
ee0989f2 7 */
8
bbc27441
AJ
9/* DEBUG: section 05 Socket Functions */
10
ee0989f2 11#ifndef _SQUIDCONNECTIONDETAIL_H_
12#define _SQUIDCONNECTIONDETAIL_H_
13
ccfbe8f4
AR
14#include "base/CodeContext.h"
15#include "base/InstanceId.h"
49ae8b95 16#include "comm/forward.h"
582c2af2 17#include "defines.h"
89aec9b6
AJ
18#if USE_SQUID_EUI
19#include "eui/Eui48.h"
20#include "eui/Eui64.h"
21#endif
d35851f1
FC
22#include "hier_code.h"
23#include "ip/Address.h"
24#include "ip/forward.h"
25#include "mem/forward.h"
98cacedb 26#include "time/gadgets.h"
cc192b50 27
5c336a3b 28#include <iosfwd>
5c336a3b 29#include <ostream>
5c336a3b 30
a3c6762c 31class CachePeer;
cfd66529 32
2bcab852
CT
33namespace Security
34{
35class NegotiationHistory;
36};
37
dc49061a
A
38namespace Comm
39{
cfd66529 40
27d1f0a0
AJ
41/* TODO: make these a struct of boolean flags members in the connection instead of a bitmap.
42 * we can't do that until all non-comm code uses Commm::Connection objects to create FD
43 * currently there is code still using comm_open() and comm_openex() synchronously!!
44 */
cfd66529 45#define COMM_UNSET 0x00
40d34a62 46#define COMM_NONBLOCKING 0x01 // default flag.
cfd66529 47#define COMM_NOCLOEXEC 0x02
40d34a62
AJ
48#define COMM_REUSEADDR 0x04 // shared FD may be both accept()ing and read()ing
49#define COMM_DOBIND 0x08 // requires a bind()
50#define COMM_TRANSPARENT 0x10 // arrived via TPROXY
51#define COMM_INTERCEPTION 0x20 // arrived via NAT
7d17a6a6 52#define COMM_REUSEPORT 0x40 //< needs SO_REUSEPORT
1c2b4465 53/// not registered with Comm and not owned by any connection-closing code
50546301 54#define COMM_ORPHANED 0x80
74d7d196 55/// Internal Comm optimization: Keep the source port unassigned until connect(2)
56#define COMM_DOBIND_PORT_LATER 0x100
62e76326 57
739b352a
AJ
58/**
59 * Store data about the physical and logical attributes of a connection.
60 *
2f8abb64 61 * Some link state can be inferred from the data, however this is not an
739b352a
AJ
62 * object for state data. But a semantic equivalent for FD with easily
63 * accessible cached properties not requiring repeated complex lookups.
64 *
50847dca 65 * Connection properties may be changed until the connection is opened.
e83cc785
AJ
66 * Properties should be considered read-only outside of the Comm layer
67 * code once the connection is open.
739b352a 68 *
1c8f25bb
AJ
69 * These objects should not be passed around directly,
70 * but a Comm::ConnectionPointer should be passed instead.
739b352a 71 */
ccfbe8f4 72class Connection: public CodeContext
cfd66529 73{
fd7b48b9
AJ
74 MEMPROXY_CLASS(Comm::Connection);
75
741c2986 76public:
cfd66529 77 Connection();
739b352a 78
aed188fd 79 /** Clear the connection properties and close any open socket. */
337b9aa4 80 ~Connection() override;
cfd66529 81
2b6b1bcb
AR
82 /// To prevent accidental copying of Connection objects that we started to
83 /// open or that are open, use cloneProfile() instead.
84 Connection(const Connection &&) = delete;
9b7992d9 85
2b6b1bcb
AR
86 /// Create a new closed Connection with the same configuration as this one.
87 ConnectionPointer cloneProfile() const;
aed188fd 88
1c2b4465
CT
89 /// close the still-open connection when its last reference is gone
90 void enterOrphanage() { flags |= COMM_ORPHANED; }
91 /// resume relying on owner(s) to initiate an explicit connection closure
92 void leaveOrphanage() { flags &= ~COMM_ORPHANED; }
93
aed188fd 94 /** Close any open socket. */
55cbb02b
AJ
95 void close();
96
b54a7c5a
CT
97 /** Synchronize with Comm: Somebody closed our connection. */
98 void noteClosure();
99
55cbb02b 100 /** determine whether this object describes an active connection or not. */
d6327017 101 bool isOpen() const { return (fd >= 0); }
55cbb02b 102
7fb5be3e
AJ
103 /** Alter the stored IP address pair.
104 * WARNING: Does not ensure matching IPv4/IPv6 are supplied.
105 */
106 void setAddrs(const Ip::Address &aLocal, const Ip::Address &aRemote) {local = aLocal; remote = aRemote;}
107
a3c6762c 108 /** retrieve the CachePeer pointer for use.
5229395c
AJ
109 * The caller is responsible for all CBDATA operations regarding the
110 * used of the pointer returned.
111 */
a3c6762c 112 CachePeer * getPeer() const;
5229395c 113
a3c6762c
FC
114 /** alter the stored CachePeer pointer.
115 * Perform appropriate CBDATA operations for locking the CachePeer pointer
5229395c 116 */
a3c6762c 117 void setPeer(CachePeer * p);
5229395c 118
8aec3e1b
CT
119 /** The time the connection started */
120 time_t startTime() const {return startTime_;}
121
c5c06f02
CT
122 /** The connection lifetime */
123 time_t lifeTime() const {return squid_curtime - startTime_;}
124
125 /** The time left for this connection*/
126 time_t timeLeft(const time_t idleTimeout) const;
127
0ce8e93b
EB
128 /// Connection establishment timeout for callers that have already decided
129 /// to connect(2), either for the first time or after checking
130 /// EnoughTimeToReForward() during any re-forwarding attempts.
131 /// \returns the time left for this connection to become connected
132 /// \param fwdStart The start time of the peer selection/connection process.
133 time_t connectTimeout(const time_t fwdStart) const;
134
8aec3e1b 135 void noteStart() {startTime_ = squid_curtime;}
2bcab852
CT
136
137 Security::NegotiationHistory *tlsNegotiations();
138 const Security::NegotiationHistory *hasTlsNegotiations() const {return tlsHistory;}
139
ccfbe8f4 140 /* CodeContext API */
337b9aa4
AR
141 ScopedId codeContextGist() const override;
142 std::ostream &detailCodeContext(std::ostream &os) const override;
ccfbe8f4 143
5229395c 144public:
cfd66529
AJ
145 /** Address/Port for the Squid end of a TCP link. */
146 Ip::Address local;
62e76326 147
cfd66529
AJ
148 /** Address for the Remote end of a TCP link. */
149 Ip::Address remote;
2d8c0b1a 150
cfd66529 151 /** Hierarchy code for this connection link */
5229395c 152 hier_code peerType;
cfd66529 153
e83cc785 154 /** Socket used by this connection. Negative if not open. */
cfd66529
AJ
155 int fd;
156
739b352a 157 /** Quality of Service TOS values currently sent on this connection */
b5523edc
AJ
158 tos_t tos;
159
244da4ad
AG
160 /** Netfilter MARK values currently sent on this connection
161 * In case of FTP, the MARK will be sent on data connections as well.
162 */
b5523edc 163 nfmark_t nfmark;
cfd66529 164
244da4ad
AG
165 /** Netfilter CONNMARK value previously retrieved from this connection
166 * In case of FTP, the CONNMARK will NOT be applied to data connections, for one main reason:
167 * the CONNMARK could be set by a third party like iptables and overwriting it in squid may
168 * cause side effects and break CONNMARK-based policy. In other words, data connection is
169 * related to control connection, but it's not the same.
170 */
171 nfmark_t nfConnmark = 0;
172
cfd66529
AJ
173 /** COMM flags set on this connection */
174 int flags;
739b352a 175
73c36fd9
AJ
176 char rfc931[USER_IDENT_SZ];
177
89aec9b6
AJ
178#if USE_SQUID_EUI
179 Eui::Eui48 remoteEui48;
180 Eui::Eui64 remoteEui64;
181#endif
182
a120b7a8 183 InstanceId<Connection, uint64_t> id;
ccfbe8f4 184
739b352a
AJ
185private:
186 /** cache_peer data object (if any) */
a3c6762c 187 CachePeer *peer_;
8aec3e1b
CT
188
189 /** The time the connection object was created */
190 time_t startTime_;
2bcab852
CT
191
192 /** TLS connection details*/
193 Security::NegotiationHistory *tlsHistory;
ee0989f2 194};
195
25ecffe5 196std::ostream &operator <<(std::ostream &, const Connection &);
5c336a3b
AJ
197
198inline std::ostream &
25ecffe5 199operator <<(std::ostream &os, const ConnectionPointer &conn)
5c336a3b 200{
aee3523a 201 if (conn != nullptr)
5c336a3b
AJ
202 os << *conn;
203 return os;
204}
205
25ecffe5
AR
206} // namespace Comm
207
ee0989f2 208#endif
f53969cc 209