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