]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm/Connection.h
Fix clang-6.0 build on FreeBSD 12 [-Wformat] (#577)
[thirdparty/squid.git] / src / comm / Connection.h
CommitLineData
ee0989f2 1/*
77b1029d 2 * Copyright (C) 1996-2020 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"
8aec3e1b 26#include "SquidTime.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
62e76326 53
739b352a
AJ
54/**
55 * Store data about the physical and logical attributes of a connection.
56 *
57 * Some link state can be infered from the data, however this is not an
58 * object for state data. But a semantic equivalent for FD with easily
59 * accessible cached properties not requiring repeated complex lookups.
60 *
50847dca 61 * Connection properties may be changed until the connection is opened.
e83cc785
AJ
62 * Properties should be considered read-only outside of the Comm layer
63 * code once the connection is open.
739b352a 64 *
1c8f25bb
AJ
65 * These objects should not be passed around directly,
66 * but a Comm::ConnectionPointer should be passed instead.
739b352a 67 */
ccfbe8f4 68class Connection: public CodeContext
cfd66529 69{
fd7b48b9
AJ
70 MEMPROXY_CLASS(Comm::Connection);
71
741c2986 72public:
cfd66529 73 Connection();
739b352a 74
aed188fd 75 /** Clear the connection properties and close any open socket. */
ccfbe8f4 76 virtual ~Connection();
cfd66529 77
aed188fd
AJ
78 /** Copy an existing connections IP and properties.
79 * This excludes the FD. The new copy will be a closed connection.
739b352a 80 */
5ae21d99 81 ConnectionPointer copyDetails() const;
aed188fd 82
aed188fd 83 /** Close any open socket. */
55cbb02b
AJ
84 void close();
85
b54a7c5a
CT
86 /** Synchronize with Comm: Somebody closed our connection. */
87 void noteClosure();
88
55cbb02b 89 /** determine whether this object describes an active connection or not. */
d6327017 90 bool isOpen() const { return (fd >= 0); }
55cbb02b 91
7fb5be3e
AJ
92 /** Alter the stored IP address pair.
93 * WARNING: Does not ensure matching IPv4/IPv6 are supplied.
94 */
95 void setAddrs(const Ip::Address &aLocal, const Ip::Address &aRemote) {local = aLocal; remote = aRemote;}
96
a3c6762c 97 /** retrieve the CachePeer pointer for use.
5229395c
AJ
98 * The caller is responsible for all CBDATA operations regarding the
99 * used of the pointer returned.
100 */
a3c6762c 101 CachePeer * getPeer() const;
5229395c 102
a3c6762c
FC
103 /** alter the stored CachePeer pointer.
104 * Perform appropriate CBDATA operations for locking the CachePeer pointer
5229395c 105 */
a3c6762c 106 void setPeer(CachePeer * p);
5229395c 107
8aec3e1b
CT
108 /** The time the connection started */
109 time_t startTime() const {return startTime_;}
110
c5c06f02
CT
111 /** The connection lifetime */
112 time_t lifeTime() const {return squid_curtime - startTime_;}
113
114 /** The time left for this connection*/
115 time_t timeLeft(const time_t idleTimeout) const;
116
0ce8e93b
EB
117 /// Connection establishment timeout for callers that have already decided
118 /// to connect(2), either for the first time or after checking
119 /// EnoughTimeToReForward() during any re-forwarding attempts.
120 /// \returns the time left for this connection to become connected
121 /// \param fwdStart The start time of the peer selection/connection process.
122 time_t connectTimeout(const time_t fwdStart) const;
123
8aec3e1b 124 void noteStart() {startTime_ = squid_curtime;}
2bcab852
CT
125
126 Security::NegotiationHistory *tlsNegotiations();
127 const Security::NegotiationHistory *hasTlsNegotiations() const {return tlsHistory;}
128
ccfbe8f4
AR
129 /* CodeContext API */
130 virtual ScopedId codeContextGist() const override;
131 virtual std::ostream &detailCodeContext(std::ostream &os) const override;
132
5229395c
AJ
133private:
134 /** These objects may not be exactly duplicated. Use copyDetails() instead. */
135 Connection(const Connection &c);
136
137 /** These objects may not be exactly duplicated. Use copyDetails() instead. */
138 Connection & operator =(const Connection &c);
139
140public:
cfd66529
AJ
141 /** Address/Port for the Squid end of a TCP link. */
142 Ip::Address local;
62e76326 143
cfd66529
AJ
144 /** Address for the Remote end of a TCP link. */
145 Ip::Address remote;
2d8c0b1a 146
cfd66529 147 /** Hierarchy code for this connection link */
5229395c 148 hier_code peerType;
cfd66529 149
e83cc785 150 /** Socket used by this connection. Negative if not open. */
cfd66529
AJ
151 int fd;
152
739b352a 153 /** Quality of Service TOS values currently sent on this connection */
b5523edc
AJ
154 tos_t tos;
155
244da4ad
AG
156 /** Netfilter MARK values currently sent on this connection
157 * In case of FTP, the MARK will be sent on data connections as well.
158 */
b5523edc 159 nfmark_t nfmark;
cfd66529 160
244da4ad
AG
161 /** Netfilter CONNMARK value previously retrieved from this connection
162 * In case of FTP, the CONNMARK will NOT be applied to data connections, for one main reason:
163 * the CONNMARK could be set by a third party like iptables and overwriting it in squid may
164 * cause side effects and break CONNMARK-based policy. In other words, data connection is
165 * related to control connection, but it's not the same.
166 */
167 nfmark_t nfConnmark = 0;
168
cfd66529
AJ
169 /** COMM flags set on this connection */
170 int flags;
739b352a 171
73c36fd9
AJ
172 char rfc931[USER_IDENT_SZ];
173
89aec9b6
AJ
174#if USE_SQUID_EUI
175 Eui::Eui48 remoteEui48;
176 Eui::Eui64 remoteEui64;
177#endif
178
a120b7a8 179 InstanceId<Connection, uint64_t> id;
ccfbe8f4 180
739b352a
AJ
181private:
182 /** cache_peer data object (if any) */
a3c6762c 183 CachePeer *peer_;
8aec3e1b
CT
184
185 /** The time the connection object was created */
186 time_t startTime_;
2bcab852
CT
187
188 /** TLS connection details*/
189 Security::NegotiationHistory *tlsHistory;
ee0989f2 190};
191
cfd66529
AJ
192}; // namespace Comm
193
6043e368 194std::ostream &operator << (std::ostream &os, const Comm::Connection &conn);
5c336a3b
AJ
195
196inline std::ostream &
197operator << (std::ostream &os, const Comm::ConnectionPointer &conn)
198{
199 if (conn != NULL)
200 os << *conn;
201 return os;
202}
203
ee0989f2 204#endif
f53969cc 205