]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/Connection.h
Reworked packet/connection marking (#170)
[thirdparty/squid.git] / src / comm / Connection.h
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 /* DEBUG: section 05 Socket Functions */
10
11 #ifndef _SQUIDCONNECTIONDETAIL_H_
12 #define _SQUIDCONNECTIONDETAIL_H_
13
14 #include "comm/forward.h"
15 #include "defines.h"
16 #if USE_SQUID_EUI
17 #include "eui/Eui48.h"
18 #include "eui/Eui64.h"
19 #endif
20 #include "hier_code.h"
21 #include "ip/Address.h"
22 #include "ip/forward.h"
23 #include "mem/forward.h"
24 #include "SquidTime.h"
25
26 #include <iosfwd>
27 #include <ostream>
28
29 class CachePeer;
30
31 namespace Security
32 {
33 class NegotiationHistory;
34 };
35
36 namespace Comm
37 {
38
39 /* TODO: make these a struct of boolean flags members in the connection instead of a bitmap.
40 * we can't do that until all non-comm code uses Commm::Connection objects to create FD
41 * currently there is code still using comm_open() and comm_openex() synchronously!!
42 */
43 #define COMM_UNSET 0x00
44 #define COMM_NONBLOCKING 0x01 // default flag.
45 #define COMM_NOCLOEXEC 0x02
46 #define COMM_REUSEADDR 0x04 // shared FD may be both accept()ing and read()ing
47 #define COMM_DOBIND 0x08 // requires a bind()
48 #define COMM_TRANSPARENT 0x10 // arrived via TPROXY
49 #define COMM_INTERCEPTION 0x20 // arrived via NAT
50
51 /**
52 * Store data about the physical and logical attributes of a connection.
53 *
54 * Some link state can be infered from the data, however this is not an
55 * object for state data. But a semantic equivalent for FD with easily
56 * accessible cached properties not requiring repeated complex lookups.
57 *
58 * Connection properties may be changed until the connection is opened.
59 * Properties should be considered read-only outside of the Comm layer
60 * code once the connection is open.
61 *
62 * These objects should not be passed around directly,
63 * but a Comm::ConnectionPointer should be passed instead.
64 */
65 class Connection : public RefCountable
66 {
67 MEMPROXY_CLASS(Comm::Connection);
68
69 public:
70 Connection();
71
72 /** Clear the connection properties and close any open socket. */
73 ~Connection();
74
75 /** Copy an existing connections IP and properties.
76 * This excludes the FD. The new copy will be a closed connection.
77 */
78 ConnectionPointer copyDetails() const;
79
80 /** Close any open socket. */
81 void close();
82
83 /** Synchronize with Comm: Somebody closed our connection. */
84 void noteClosure();
85
86 /** determine whether this object describes an active connection or not. */
87 bool isOpen() const { return (fd >= 0); }
88
89 /** Alter the stored IP address pair.
90 * WARNING: Does not ensure matching IPv4/IPv6 are supplied.
91 */
92 void setAddrs(const Ip::Address &aLocal, const Ip::Address &aRemote) {local = aLocal; remote = aRemote;}
93
94 /** retrieve the CachePeer pointer for use.
95 * The caller is responsible for all CBDATA operations regarding the
96 * used of the pointer returned.
97 */
98 CachePeer * getPeer() const;
99
100 /** alter the stored CachePeer pointer.
101 * Perform appropriate CBDATA operations for locking the CachePeer pointer
102 */
103 void setPeer(CachePeer * p);
104
105 /** The time the connection started */
106 time_t startTime() const {return startTime_;}
107
108 /** The connection lifetime */
109 time_t lifeTime() const {return squid_curtime - startTime_;}
110
111 /** The time left for this connection*/
112 time_t timeLeft(const time_t idleTimeout) const;
113
114 /// Connection establishment timeout for callers that have already decided
115 /// to connect(2), either for the first time or after checking
116 /// EnoughTimeToReForward() during any re-forwarding attempts.
117 /// \returns the time left for this connection to become connected
118 /// \param fwdStart The start time of the peer selection/connection process.
119 time_t connectTimeout(const time_t fwdStart) const;
120
121 void noteStart() {startTime_ = squid_curtime;}
122
123 Security::NegotiationHistory *tlsNegotiations();
124 const Security::NegotiationHistory *hasTlsNegotiations() const {return tlsHistory;}
125
126 private:
127 /** These objects may not be exactly duplicated. Use copyDetails() instead. */
128 Connection(const Connection &c);
129
130 /** These objects may not be exactly duplicated. Use copyDetails() instead. */
131 Connection & operator =(const Connection &c);
132
133 public:
134 /** Address/Port for the Squid end of a TCP link. */
135 Ip::Address local;
136
137 /** Address for the Remote end of a TCP link. */
138 Ip::Address remote;
139
140 /** Hierarchy code for this connection link */
141 hier_code peerType;
142
143 /** Socket used by this connection. Negative if not open. */
144 int fd;
145
146 /** Quality of Service TOS values currently sent on this connection */
147 tos_t tos;
148
149 /** Netfilter MARK values currently sent on this connection
150 * In case of FTP, the MARK will be sent on data connections as well.
151 */
152 nfmark_t nfmark;
153
154 /** Netfilter CONNMARK value previously retrieved from this connection
155 * In case of FTP, the CONNMARK will NOT be applied to data connections, for one main reason:
156 * the CONNMARK could be set by a third party like iptables and overwriting it in squid may
157 * cause side effects and break CONNMARK-based policy. In other words, data connection is
158 * related to control connection, but it's not the same.
159 */
160 nfmark_t nfConnmark = 0;
161
162 /** COMM flags set on this connection */
163 int flags;
164
165 char rfc931[USER_IDENT_SZ];
166
167 #if USE_SQUID_EUI
168 Eui::Eui48 remoteEui48;
169 Eui::Eui64 remoteEui64;
170 #endif
171
172 private:
173 /** cache_peer data object (if any) */
174 CachePeer *peer_;
175
176 /** The time the connection object was created */
177 time_t startTime_;
178
179 /** TLS connection details*/
180 Security::NegotiationHistory *tlsHistory;
181 };
182
183 }; // namespace Comm
184
185 std::ostream &operator << (std::ostream &os, const Comm::Connection &conn);
186
187 inline std::ostream &
188 operator << (std::ostream &os, const Comm::ConnectionPointer &conn)
189 {
190 if (conn != NULL)
191 os << *conn;
192 return os;
193 }
194
195 #endif
196