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