/[resiprocate]/main/resip/stack/Tuple.hxx
ViewVC logotype

Contents of /main/resip/stack/Tuple.hxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11153 - (show annotations) (download)
Wed Apr 23 17:25:02 2014 UTC (5 years, 9 months ago) by dpetrie
File MIME type: text/plain
File size: 12355 byte(s)
added netns scope/context to Uri and Tuple

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 const sockaddr& getSockaddr() const { return mSockaddr; }
108
109 /// @brief Retrieve the binary representation of the socket address for
110 /// this tuple.
111 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 TransportType getType() const { return mTransportType; }
121 void setType(TransportType type) { mTransportType = type; }
122 void setPort(int port);
123 int getPort() const;
124 inline FlowKey getFlowKey() const { return mFlowKey; }
125
126 /// @deprecated use ipVersion()
127 /// @todo !dcm! -- should deprecate asap
128 bool isV4() const;
129
130 /// Returns V4 or V6 as appropriate.
131 IpVersion ipVersion() const;
132
133 /// @brief TRUE if this address is equal to the "INADDR_ANY" value for
134 /// this address family.
135 bool isAnyInterface() const;
136 socklen_t length() const; // of sockaddr
137 bool isLoopback() const;
138 bool isPrivateAddress() const; // Return boolean based on definitions in RFC1918(v4) and RFC4193(v6)
139
140 /// @brief Compares TransportType, the binary address, port, and
141 /// address family of the Tuple.
142 bool operator<(const Tuple& rhs) const;
143
144 /// @brief Compares TransportType, the binary address, port, and
145 /// address family of the Tuple.
146 bool operator==(const Tuple& rhs) const;
147
148 /// Wrapper around the inet_top() method.
149 Data presentationFormat() const;
150
151 /// @brief Converts a string representation of transport type,
152 /// i.e. "UDP" to a TransportType
153 static TransportType toTransport( const Data& );
154
155 /// @brief Converts the TransportType to a string representation of the
156 /// transport type, e.g. "TCP"
157 static const Data& toData( TransportType );
158
159 static const Data& toDataLower(TransportType type);
160
161 /// @brief Converts the binary socket address to presentation format,
162 /// via the DnsUtil::inet_ntop() method.
163 static Data inet_ntop(const Tuple& tuple);
164
165 // Creates a binary token from the provided Tuple - if salt is provided, then an HMAC is appended
166 // to the end of the token
167 static void writeBinaryToken(const Tuple& tuple, Data& container, const Data& salt=Data::Empty);
168 // Creates a Tuple from the provided binary token - if salt is provided, then an HMAC is checked
169 static Tuple makeTupleFromBinaryToken(const Data& binaryToken, const Data& salt=Data::Empty);
170
171 GenericIPAddress toGenericIPAddress() const;
172
173 /// This is a (largely) opaque key that subclasses of Transport will use
174 /// to help record/find flows. For UDP and DTLS, this is just the FD, and
175 /// the rest of the information about the flow is carried in the Tuple.
176 /// For TCP and TLS, the FD of the connection is used.
177 /// For protocols where using the FD would not be appropriate (SCTP),
178 /// the transport may use whatever method to generate these it likes.
179 /// (It is highly recommended that these ids are unique across all
180 /// instances of a transport type)
181 FlowKey mFlowKey;
182 TransportKey mTransportKey;
183
184 bool onlyUseExistingConnection;
185
186 /// @brief compares this tuple with the one passed in for family, port
187 /// and address equality using the passed in address mask (mask
188 /// is specified by number of bits)
189 bool isEqualWithMask(const Tuple& tuple, short mask, bool ignorePort=false, bool ignoreTransport=false) const;
190
191 /// @brief A "less than" comparator for Tuple, for use in map
192 /// containers etc. Comparison is based on transport type, and
193 /// if those are equal, it is based on port number.
194 class AnyInterfaceCompare
195 {
196 public:
197 bool operator()(const Tuple& x,
198 const Tuple& y) const;
199 };
200 friend class AnyInterfaceCompare;
201
202 /// @brief A "less than" comparator for Tuple, for use in map
203 /// containers etc. Comparison is based on transport type, and
204 /// if those are equal, it is based on the binary representation
205 /// of the socket internet address (v4 or v6, whichever is
206 /// appropriate).
207 class AnyPortCompare
208 {
209 public:
210 bool operator()(const Tuple& x,
211 const Tuple& y) const;
212 };
213 friend class AnyPortCompare;
214
215 /// @brief A "less than" comparator for Tuple, for use in map
216 /// containers etc. Comparison is based only on transport type
217 class AnyPortAnyInterfaceCompare
218 {
219 public:
220 bool operator()(const Tuple& x,
221 const Tuple& y) const;
222 };
223 friend class AnyPortAnyInterfaceCompare;
224
225 class FlowKeyCompare
226 {
227 public:
228 bool operator()(const Tuple& x,
229 const Tuple& y) const;
230 };
231 friend class FlowKeyCompare;
232
233 /// @brief Set the domain name this address tuple intends to represent.
234 void setTargetDomain(const Data& target)
235 {
236 mTargetDomain = target;
237 }
238
239 /// @brief Get the domain name this address tuple intends to represent.
240 /// Useful with DnsUtil, for example.
241 const Data& getTargetDomain() const
242 {
243 return mTargetDomain;
244 }
245
246 /// @brief Set the netns (network namespace) for this Tuple
247 void setNetNs(const Data& netNs)
248 {
249 mNetNs = netNs;
250 }
251
252 /// @brief Get the netns for this Tuple
253 const Data& getNetNs() const
254 {
255 return(mNetNs);
256 }
257
258 /**
259 @brief Creates a 32-bit hash based on the contents of this Tuple.
260 */
261 size_t hash() const;
262
263 private:
264 union
265 {
266 sockaddr mSockaddr;
267 sockaddr_in m_anonv4;
268 #ifdef IPPROTO_IPV6
269 // enable this if the current platform supports IPV6
270 // ?bwc? Is there a more standard preprocessor macro for this?
271 sockaddr_in6 m_anonv6;
272 #endif
273 char pad[RESIP_MAX_SOCKADDR_SIZE]; //< this make union same size if v6 is in or out
274 };
275 TransportType mTransportType;
276 Data mTargetDomain;
277
278 Data mNetNs; ///< The network namespace to which the address and port are scoped
279
280 friend EncodeStream& operator<<(EncodeStream& strm, const Tuple& tuple);
281 friend class DnsResult;
282 };
283
284
285 EncodeStream&
286 operator<<(EncodeStream& ostrm, const Tuple& tuple);
287
288 }
289
290 HashValue(resip::Tuple);
291
292 #endif
293 /* ====================================================================
294 * The Vovida Software License, Version 1.0
295 *
296 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
297 *
298 * Redistribution and use in source and binary forms, with or without
299 * modification, are permitted provided that the following conditions
300 * are met:
301 *
302 * 1. Redistributions of source code must retain the above copyright
303 * notice, this list of conditions and the following disclaimer.
304 *
305 * 2. Redistributions in binary form must reproduce the above copyright
306 * notice, this list of conditions and the following disclaimer in
307 * the documentation and/or other materials provided with the
308 * distribution.
309 *
310 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
311 * and "Vovida Open Communication Application Library (VOCAL)" must
312 * not be used to endorse or promote products derived from this
313 * software without prior written permission. For written
314 * permission, please contact vocal@vovida.org.
315 *
316 * 4. Products derived from this software may not be called "VOCAL", nor
317 * may "VOCAL" appear in their name, without prior written
318 * permission of Vovida Networks, Inc.
319 *
320 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
321 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
322 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
323 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
324 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
325 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
326 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
327 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
328 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
329 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
330 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
331 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
332 * DAMAGE.
333 *
334 * ====================================================================
335 *
336 * This software consists of voluntary contributions made by Vovida
337 * Networks, Inc. and many individuals on behalf of Vovida Networks,
338 * Inc. For more information on Vovida Networks, Inc., please see
339 * <http://www.vovida.org/>.
340 *
341 */

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27