]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/Connection.h
Merged from trunk
[thirdparty/squid.git] / src / comm / Connection.h
1 /*
2 * DEBUG: section 05 Socket Functions
3 * AUTHOR: Amos Jeffries
4 * AUTHOR: Robert Collins
5 *
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
8 *
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
31 *
32 *
33 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
34 * Copyright (c) 2010, Amos Jeffries <amosjeffries@squid-cache.org>
35 */
36
37 #ifndef _SQUIDCONNECTIONDETAIL_H_
38 #define _SQUIDCONNECTIONDETAIL_H_
39
40 #include "comm/forward.h"
41 #include "defines.h"
42 #include "hier_code.h"
43 #include "ip/Address.h"
44 #include "MemPool.h"
45 #include "RefCount.h"
46 #include "typedefs.h"
47 #if USE_SQUID_EUI
48 #include "eui/Eui48.h"
49 #include "eui/Eui64.h"
50 #endif
51
52 #if HAVE_IOSFWD
53 #include <iosfwd>
54 #endif
55 #if HAVE_OSTREAM
56 #include <ostream>
57 #endif
58
59 class CachePeer;
60
61 namespace Comm
62 {
63
64 /* TODO: make these a struct of boolean flags members in the connection instead of a bitmap.
65 * we can't do that until all non-comm code uses Commm::Connection objects to create FD
66 * currently there is code still using comm_open() and comm_openex() synchronously!!
67 */
68 #define COMM_UNSET 0x00
69 #define COMM_NONBLOCKING 0x01 // default flag.
70 #define COMM_NOCLOEXEC 0x02
71 #define COMM_REUSEADDR 0x04 // shared FD may be both accept()ing and read()ing
72 #define COMM_DOBIND 0x08 // requires a bind()
73 #define COMM_TRANSPARENT 0x10 // arrived via TPROXY
74 #define COMM_INTERCEPTION 0x20 // arrived via NAT
75
76 /**
77 * Store data about the physical and logical attributes of a connection.
78 *
79 * Some link state can be infered from the data, however this is not an
80 * object for state data. But a semantic equivalent for FD with easily
81 * accessible cached properties not requiring repeated complex lookups.
82 *
83 * Connection properties may be changed until the connection is opened.
84 * Properties should be considered read-only outside of the Comm layer
85 * code once the connection is open.
86 *
87 * These objects should not be passed around directly,
88 * but a Comm::ConnectionPointer should be passed instead.
89 */
90 class Connection : public RefCountable
91 {
92 public:
93 MEMPROXY_CLASS(Comm::Connection);
94
95 Connection();
96
97 /** Clear the connection properties and close any open socket. */
98 ~Connection();
99
100 /** Copy an existing connections IP and properties.
101 * This excludes the FD. The new copy will be a closed connection.
102 */
103 ConnectionPointer copyDetails() const;
104
105 /** Close any open socket. */
106 void close();
107
108 /** determine whether this object describes an active connection or not. */
109 bool isOpen() const { return (fd >= 0); }
110
111 /** retrieve the CachePeer pointer for use.
112 * The caller is responsible for all CBDATA operations regarding the
113 * used of the pointer returned.
114 */
115 CachePeer * getPeer() const;
116
117 /** alter the stored CachePeer pointer.
118 * Perform appropriate CBDATA operations for locking the CachePeer pointer
119 */
120 void setPeer(CachePeer * p);
121
122 private:
123 /** These objects may not be exactly duplicated. Use copyDetails() instead. */
124 Connection(const Connection &c);
125
126 /** These objects may not be exactly duplicated. Use copyDetails() instead. */
127 Connection & operator =(const Connection &c);
128
129 public:
130 /** Address/Port for the Squid end of a TCP link. */
131 Ip::Address local;
132
133 /** Address for the Remote end of a TCP link. */
134 Ip::Address remote;
135
136 /** Hierarchy code for this connection link */
137 hier_code peerType;
138
139 /** Socket used by this connection. Negative if not open. */
140 int fd;
141
142 /** Quality of Service TOS values currently sent on this connection */
143 tos_t tos;
144
145 /** Netfilter MARK values currently sent on this connection */
146 nfmark_t nfmark;
147
148 /** COMM flags set on this connection */
149 int flags;
150
151 char rfc931[USER_IDENT_SZ];
152
153 #if USE_SQUID_EUI
154 Eui::Eui48 remoteEui48;
155 Eui::Eui64 remoteEui64;
156 #endif
157
158 private:
159 /** cache_peer data object (if any) */
160 CachePeer *peer_;
161 };
162
163 }; // namespace Comm
164
165 MEMPROXY_CLASS_INLINE(Comm::Connection);
166
167 // NP: Order and namespace here is very important.
168 // * The second define inlines the first.
169 // * Stream inheritance overloading is searched in the global scope first.
170
171 inline std::ostream &
172 operator << (std::ostream &os, const Comm::Connection &conn)
173 {
174 os << "local=" << conn.local << " remote=" << conn.remote;
175 if (conn.fd >= 0)
176 os << " FD " << conn.fd;
177 if (conn.flags != COMM_UNSET)
178 os << " flags=" << conn.flags;
179 #if USE_IDENT
180 if (*conn.rfc931)
181 os << " IDENT::" << conn.rfc931;
182 #endif
183 return os;
184 }
185
186 inline std::ostream &
187 operator << (std::ostream &os, const Comm::ConnectionPointer &conn)
188 {
189 if (conn != NULL)
190 os << *conn;
191 return os;
192 }
193
194 #endif