/src/resiprocate/resip/stack/Tuple.hxx
Line | Count | Source |
1 | | #if !defined(RESIP_TUPLE_HXX) |
2 | | #define RESIP_TUPLE_HXX |
3 | | |
4 | | #ifdef HAVE_CONFIG_H |
5 | | #include "config.h" |
6 | | #endif |
7 | | |
8 | | #include <memory> |
9 | | |
10 | | #include "rutil/Socket.hxx" |
11 | | #include "rutil/compat.hxx" |
12 | | |
13 | | #include "rutil/HashMap.hxx" |
14 | | #include "rutil/TransportType.hxx" |
15 | | #include "rutil/HeapInstanceCounter.hxx" |
16 | | #include "rutil/Data.hxx" |
17 | | |
18 | | #if defined(WIN32) |
19 | | #include <Ws2tcpip.h> |
20 | | #else |
21 | | #include <netinet/in.h> |
22 | | #endif |
23 | | |
24 | | namespace resip |
25 | | { |
26 | | |
27 | | struct GenericIPAddress; |
28 | | |
29 | | // WARNING!! |
30 | | // When you change this structure, make sure to update the hash function, |
31 | | // operator== and operator< to be consistent with the new structure. Be |
32 | | // careful not to include members that change value in the Tuple over |
33 | | // its lifetime (they must not be included in the hash or comparisons). |
34 | | |
35 | | typedef unsigned long FlowKey; |
36 | | typedef unsigned long TransportKey; |
37 | | |
38 | | /** |
39 | | @ingroup resip_crit |
40 | | @brief Represents a network address. |
41 | | |
42 | | This includes: |
43 | | - IP address |
44 | | - port |
45 | | - protocol (TransportType) |
46 | | - TLS hostname (since this is integral to connection establishment) |
47 | | |
48 | | Internally the class is aware of the struct |
49 | | sockaddr/sin_addr/sin6addr binary representation of the address. |
50 | | The sa_family of struct sockaddr is used to keep track of whether a |
51 | | Tuple is representing a an IPv4 or IPv6 address. |
52 | | |
53 | | Also included are some comparator classes that can be used for |
54 | | containers of Tuple. |
55 | | */ |
56 | | class Tuple |
57 | | { |
58 | | public: |
59 | | RESIP_HeapCount(Tuple); |
60 | | |
61 | | Tuple(); |
62 | | |
63 | | explicit Tuple(const GenericIPAddress& genericAddress, |
64 | | TransportType type=UNKNOWN_TRANSPORT, |
65 | | const Data& targetDomain = Data::Empty); |
66 | | |
67 | | Tuple(const Data& printableAddress, |
68 | | int port, |
69 | | IpVersion ipVer, |
70 | | TransportType type=UNKNOWN_TRANSPORT, |
71 | | const Data& targetDomain = Data::Empty, |
72 | | const Data& netNs = Data::Empty); |
73 | | |
74 | | Tuple(const Data& printableAddress, |
75 | | int port, |
76 | | TransportType type, |
77 | | const Data& targetDomain = Data::Empty, |
78 | | const Data& netNs = Data::Empty); |
79 | | |
80 | | Tuple(const in_addr& pipv4, |
81 | | int pport, |
82 | | TransportType ptype, |
83 | | const Data& targetDomain = Data::Empty, |
84 | | const Data& netNs = Data::Empty); |
85 | | |
86 | | Tuple(const sockaddr& addr, |
87 | | TransportType ptype, |
88 | | const Data& targetDomain = Data::Empty); |
89 | | |
90 | | #ifdef IPPROTO_IPV6 |
91 | | // enable this if the current platform supports IPV6; the USE_IPV6 #define |
92 | | // will determine if this c'tor is actually implemented. |
93 | | // ?bwc? Is there a more standard preprocessor macro for this? |
94 | | // ?bwc? Is there a way we can add something more informative to the |
95 | | // linker error we'll see if we compiled without USE_IPV6, on a platform |
96 | | // with IPV6, and someone tries to invoke this c'tor? (ie; "This library |
97 | | // was built with IPV6 support disabled") |
98 | | Tuple(const in6_addr& pipv6, |
99 | | int pport, |
100 | | TransportType ptype, |
101 | | const Data& targetDomain = Data::Empty, |
102 | | const Data& netNs = Data::Empty); |
103 | | #endif |
104 | | |
105 | | /// @brief Retrieve a const binary representation of the socket address |
106 | | /// for this tuple. |
107 | 0 | const sockaddr& getSockaddr() const { return mSockaddr; } |
108 | | |
109 | | /// @brief Retrieve the binary representation of the socket address for |
110 | | /// this tuple. |
111 | 0 | sockaddr& getMutableSockaddr() { return mSockaddr; } |
112 | | /// @brief Get a copy of the socket address including the interface and |
113 | | /// not the port number |
114 | | void copySockaddrAnyPort(sockaddr *sa); |
115 | | |
116 | | /// @brief Set the internal binary representation of the socket address |
117 | | /// from the GenericIPAddress. |
118 | | void setSockaddr(const GenericIPAddress &); |
119 | | |
120 | 0 | TransportType getType() const { return mTransportType; } |
121 | 5.87k | void setType(TransportType type) { mTransportType = type; } |
122 | | void setPort(int port); |
123 | | int getPort() const; |
124 | 0 | inline FlowKey getFlowKey() const { return mFlowKey; } |
125 | 0 | void setFlowKey(FlowKey flowKey) { mFlowKey = flowKey; } |
126 | | |
127 | | /// @deprecated use ipVersion() |
128 | | /// @todo !dcm! -- should deprecate asap |
129 | | bool isV4() const; |
130 | | |
131 | | /// Returns V4 or V6 as appropriate. |
132 | | IpVersion ipVersion() const; |
133 | | |
134 | | /// @brief TRUE if this address is equal to the "INADDR_ANY" value for |
135 | | /// this address family. |
136 | | bool isAnyInterface() const; |
137 | | socklen_t length() const; // of sockaddr |
138 | | bool isLoopback() const; |
139 | | bool isPrivateAddress() const; // Return boolean based on definitions in RFC1918(v4) and RFC4193(v6) |
140 | | |
141 | | /// @brief Compares TransportType, the binary address, port, and |
142 | | /// address family of the Tuple. |
143 | | bool operator<(const Tuple& rhs) const; |
144 | | |
145 | | /// @brief Compares TransportType, the binary address, port, and |
146 | | /// address family of the Tuple. |
147 | | bool operator==(const Tuple& rhs) const; |
148 | | |
149 | | /// Wrapper around the inet_top() method. |
150 | | Data presentationFormat() const; |
151 | | |
152 | | /// @brief Converts a string representation of transport type, |
153 | | /// i.e. "UDP" to a TransportType |
154 | | static TransportType toTransport( const Data& ); |
155 | | |
156 | | /// @brief Converts the TransportType to a string representation of the |
157 | | /// transport type, e.g. "TCP" |
158 | | static const Data& toData( TransportType ); |
159 | | |
160 | | static const Data& toDataLower(TransportType type); |
161 | | |
162 | | /// @brief Converts the binary socket address to presentation format, |
163 | | /// via the DnsUtil::inet_ntop() method. |
164 | | static Data inet_ntop(const Tuple& tuple); |
165 | | |
166 | | // Creates a binary token from the provided Tuple - if salt is provided, then an HMAC is appended |
167 | | // to the end of the token |
168 | | static void writeBinaryToken(const Tuple& tuple, Data& container, const Data& salt=Data::Empty); |
169 | | // Creates a Tuple from the provided binary token - if salt is provided, then an HMAC is checked |
170 | | static Tuple makeTupleFromBinaryToken(const Data& binaryToken, const Data& salt=Data::Empty); |
171 | | |
172 | | GenericIPAddress toGenericIPAddress() const; |
173 | | |
174 | | /// This is a (largely) opaque key that subclasses of Transport will use |
175 | | /// to help record/find flows. For UDP and DTLS, this is just the FD, and |
176 | | /// the rest of the information about the flow is carried in the Tuple. |
177 | | /// For TCP and TLS, the FD of the connection is used. |
178 | | /// For protocols where using the FD would not be appropriate (SCTP), |
179 | | /// the transport may use whatever method to generate these it likes. |
180 | | /// (It is highly recommended that these ids are unique across all |
181 | | /// instances of a transport type) |
182 | | FlowKey mFlowKey; |
183 | | TransportKey mTransportKey; |
184 | | |
185 | | bool onlyUseExistingConnection; |
186 | | |
187 | | /// @brief compares this tuple with the one passed in for family, port |
188 | | /// and address equality using the passed in address mask (mask |
189 | | /// is specified by number of bits) |
190 | | bool isEqualWithMask(const Tuple& tuple, short mask, bool ignorePort=false, bool ignoreTransport=false) const; |
191 | | |
192 | | /// @brief A "less than" comparator for Tuple, for use in map |
193 | | /// containers etc. Comparison is based on transport type, and |
194 | | /// if those are equal, it is based on port number. |
195 | | class AnyInterfaceCompare |
196 | | { |
197 | | public: |
198 | | bool operator()(const Tuple& x, |
199 | | const Tuple& y) const; |
200 | | }; |
201 | | friend class AnyInterfaceCompare; |
202 | | |
203 | | /// @brief A "less than" comparator for Tuple, for use in map |
204 | | /// containers etc. Comparison is based on transport type, and |
205 | | /// if those are equal, it is based on the binary representation |
206 | | /// of the socket internet address (v4 or v6, whichever is |
207 | | /// appropriate). |
208 | | class AnyPortCompare |
209 | | { |
210 | | public: |
211 | | bool operator()(const Tuple& x, |
212 | | const Tuple& y) const; |
213 | | }; |
214 | | friend class AnyPortCompare; |
215 | | |
216 | | /// @brief A "less than" comparator for Tuple, for use in map |
217 | | /// containers etc. Comparison is based only on transport type |
218 | | class AnyPortAnyInterfaceCompare |
219 | | { |
220 | | public: |
221 | | bool operator()(const Tuple& x, |
222 | | const Tuple& y) const; |
223 | | }; |
224 | | friend class AnyPortAnyInterfaceCompare; |
225 | | |
226 | | class FlowKeyCompare |
227 | | { |
228 | | public: |
229 | | bool operator()(const Tuple& x, |
230 | | const Tuple& y) const; |
231 | | }; |
232 | | friend class FlowKeyCompare; |
233 | | |
234 | | /// @brief Set the domain name this address tuple intends to represent. |
235 | | void setTargetDomain(const Data& target) |
236 | 0 | { |
237 | 0 | mTargetDomain = target; |
238 | 0 | } |
239 | | |
240 | | /// @brief Get the domain name this address tuple intends to represent. |
241 | | /// Useful with DnsUtil, for example. |
242 | | const Data& getTargetDomain() const |
243 | 0 | { |
244 | 0 | return mTargetDomain; |
245 | 0 | } |
246 | | |
247 | | /// @brief Set the netns (network namespace) for this Tuple |
248 | | void setNetNs(const Data& netNs) |
249 | 0 | { |
250 | 0 | mNetNs = netNs; |
251 | 0 | } |
252 | | |
253 | | /// @brief Get the netns for this Tuple |
254 | | const Data& getNetNs() const |
255 | 0 | { |
256 | 0 | return(mNetNs); |
257 | 0 | } |
258 | | |
259 | | /** |
260 | | @brief Creates a 32-bit hash based on the contents of this Tuple. |
261 | | */ |
262 | | size_t hash() const; |
263 | | |
264 | | private: |
265 | | union |
266 | | { |
267 | | sockaddr mSockaddr; |
268 | | sockaddr_in m_anonv4; |
269 | | #ifdef IPPROTO_IPV6 |
270 | | // enable this if the current platform supports IPV6 |
271 | | // ?bwc? Is there a more standard preprocessor macro for this? |
272 | | sockaddr_in6 m_anonv6; |
273 | | #endif |
274 | | char pad[RESIP_MAX_SOCKADDR_SIZE]; //< this make union same size if v6 is in or out |
275 | | }; |
276 | | TransportType mTransportType; |
277 | | Data mTargetDomain; |
278 | | |
279 | | Data mNetNs; ///< The network namespace to which the address and port are scoped |
280 | | |
281 | | friend EncodeStream& operator<<(EncodeStream& strm, const Tuple& tuple); |
282 | | friend class DnsResult; |
283 | | }; |
284 | | |
285 | | |
286 | | EncodeStream& |
287 | | operator<<(EncodeStream& ostrm, const Tuple& tuple); |
288 | | |
289 | | } |
290 | | |
291 | | HashValue(resip::Tuple); |
292 | | |
293 | | #endif |
294 | | /* ==================================================================== |
295 | | * The Vovida Software License, Version 1.0 |
296 | | * |
297 | | * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. |
298 | | * |
299 | | * Redistribution and use in source and binary forms, with or without |
300 | | * modification, are permitted provided that the following conditions |
301 | | * are met: |
302 | | * |
303 | | * 1. Redistributions of source code must retain the above copyright |
304 | | * notice, this list of conditions and the following disclaimer. |
305 | | * |
306 | | * 2. Redistributions in binary form must reproduce the above copyright |
307 | | * notice, this list of conditions and the following disclaimer in |
308 | | * the documentation and/or other materials provided with the |
309 | | * distribution. |
310 | | * |
311 | | * 3. The names "VOCAL", "Vovida Open Communication Application Library", |
312 | | * and "Vovida Open Communication Application Library (VOCAL)" must |
313 | | * not be used to endorse or promote products derived from this |
314 | | * software without prior written permission. For written |
315 | | * permission, please contact vocal@vovida.org. |
316 | | * |
317 | | * 4. Products derived from this software may not be called "VOCAL", nor |
318 | | * may "VOCAL" appear in their name, without prior written |
319 | | * permission of Vovida Networks, Inc. |
320 | | * |
321 | | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED |
322 | | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
323 | | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND |
324 | | * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA |
325 | | * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES |
326 | | * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, |
327 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
328 | | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
329 | | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
330 | | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
331 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
332 | | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
333 | | * DAMAGE. |
334 | | * |
335 | | * ==================================================================== |
336 | | * |
337 | | * This software consists of voluntary contributions made by Vovida |
338 | | * Networks, Inc. and many individuals on behalf of Vovida Networks, |
339 | | * Inc. For more information on Vovida Networks, Inc., please see |
340 | | * <http://www.vovida.org/>. |
341 | | * |
342 | | */ |