]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/Connection.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / comm / Connection.h
1 /*
2 * Copyright (C) 1996-2014 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 #include "hier_code.h"
17 #include "ip/Address.h"
18 #include "mem/forward.h"
19 #include "typedefs.h"
20 #if USE_SQUID_EUI
21 #include "eui/Eui48.h"
22 #include "eui/Eui64.h"
23 #endif
24 #include "SquidTime.h"
25
26 #include <iosfwd>
27 #include <ostream>
28
29 class CachePeer;
30
31 namespace Comm
32 {
33
34 /* TODO: make these a struct of boolean flags members in the connection instead of a bitmap.
35 * we can't do that until all non-comm code uses Commm::Connection objects to create FD
36 * currently there is code still using comm_open() and comm_openex() synchronously!!
37 */
38 #define COMM_UNSET 0x00
39 #define COMM_NONBLOCKING 0x01 // default flag.
40 #define COMM_NOCLOEXEC 0x02
41 #define COMM_REUSEADDR 0x04 // shared FD may be both accept()ing and read()ing
42 #define COMM_DOBIND 0x08 // requires a bind()
43 #define COMM_TRANSPARENT 0x10 // arrived via TPROXY
44 #define COMM_INTERCEPTION 0x20 // arrived via NAT
45
46 /**
47 * Store data about the physical and logical attributes of a connection.
48 *
49 * Some link state can be infered from the data, however this is not an
50 * object for state data. But a semantic equivalent for FD with easily
51 * accessible cached properties not requiring repeated complex lookups.
52 *
53 * Connection properties may be changed until the connection is opened.
54 * Properties should be considered read-only outside of the Comm layer
55 * code once the connection is open.
56 *
57 * These objects should not be passed around directly,
58 * but a Comm::ConnectionPointer should be passed instead.
59 */
60 class Connection : public RefCountable
61 {
62 MEMPROXY_CLASS(Comm::Connection);
63
64 public:
65 Connection();
66
67 /** Clear the connection properties and close any open socket. */
68 ~Connection();
69
70 /** Copy an existing connections IP and properties.
71 * This excludes the FD. The new copy will be a closed connection.
72 */
73 ConnectionPointer copyDetails() const;
74
75 /** Close any open socket. */
76 void close();
77
78 /** determine whether this object describes an active connection or not. */
79 bool isOpen() const { return (fd >= 0); }
80
81 /** Alter the stored IP address pair.
82 * WARNING: Does not ensure matching IPv4/IPv6 are supplied.
83 */
84 void setAddrs(const Ip::Address &aLocal, const Ip::Address &aRemote) {local = aLocal; remote = aRemote;}
85
86 /** retrieve the CachePeer pointer for use.
87 * The caller is responsible for all CBDATA operations regarding the
88 * used of the pointer returned.
89 */
90 CachePeer * getPeer() const;
91
92 /** alter the stored CachePeer pointer.
93 * Perform appropriate CBDATA operations for locking the CachePeer pointer
94 */
95 void setPeer(CachePeer * p);
96
97 /** The time the connection started */
98 time_t startTime() const {return startTime_;}
99
100 void noteStart() {startTime_ = squid_curtime;}
101 private:
102 /** These objects may not be exactly duplicated. Use copyDetails() instead. */
103 Connection(const Connection &c);
104
105 /** These objects may not be exactly duplicated. Use copyDetails() instead. */
106 Connection & operator =(const Connection &c);
107
108 public:
109 /** Address/Port for the Squid end of a TCP link. */
110 Ip::Address local;
111
112 /** Address for the Remote end of a TCP link. */
113 Ip::Address remote;
114
115 /** Hierarchy code for this connection link */
116 hier_code peerType;
117
118 /** Socket used by this connection. Negative if not open. */
119 int fd;
120
121 /** Quality of Service TOS values currently sent on this connection */
122 tos_t tos;
123
124 /** Netfilter MARK values currently sent on this connection */
125 nfmark_t nfmark;
126
127 /** COMM flags set on this connection */
128 int flags;
129
130 char rfc931[USER_IDENT_SZ];
131
132 #if USE_SQUID_EUI
133 Eui::Eui48 remoteEui48;
134 Eui::Eui64 remoteEui64;
135 #endif
136
137 private:
138 /** cache_peer data object (if any) */
139 CachePeer *peer_;
140
141 /** The time the connection object was created */
142 time_t startTime_;
143 };
144
145 }; // namespace Comm
146
147 // NP: Order and namespace here is very important.
148 // * The second define inlines the first.
149 // * Stream inheritance overloading is searched in the global scope first.
150
151 inline std::ostream &
152 operator << (std::ostream &os, const Comm::Connection &conn)
153 {
154 os << "local=" << conn.local << " remote=" << conn.remote;
155 if (conn.fd >= 0)
156 os << " FD " << conn.fd;
157 if (conn.flags != COMM_UNSET)
158 os << " flags=" << conn.flags;
159 #if USE_IDENT
160 if (*conn.rfc931)
161 os << " IDENT::" << conn.rfc931;
162 #endif
163 return os;
164 }
165
166 inline std::ostream &
167 operator << (std::ostream &os, const Comm::ConnectionPointer &conn)
168 {
169 if (conn != NULL)
170 os << *conn;
171 return os;
172 }
173
174 #endif
175