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