reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
Tuple.hxx
Go to the documentation of this file.
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#include <vector>
10
11#include "rutil/Socket.hxx"
12#include "rutil/compat.hxx"
13
14#include "rutil/HashMap.hxx"
17#include "rutil/Data.hxx"
18
19#if defined(WIN32)
20#include <Ws2tcpip.h>
21#else
22#include <netinet/in.h>
23#endif
24
25namespace resip
26{
27
28struct GenericIPAddress;
29struct AddressMask; // defined below (embeds a Tuple, so it follows the Tuple class)
30
31// WARNING!!
32// When you change this structure, make sure to update the hash function,
33// operator== and operator< to be consistent with the new structure. Be
34// careful not to include members that change value in the Tuple over
35// its lifetime (they must not be included in the hash or comparisons).
36
37typedef unsigned long FlowKey;
38typedef unsigned long TransportKey;
39
58class Tuple
59{
60 public:
62
63 Tuple();
64
65 explicit Tuple(const GenericIPAddress& genericAddress,
67 const Data& targetDomain = Data::Empty);
68
69 Tuple(const Data& printableAddress,
70 int port,
71 IpVersion ipVer,
73 const Data& targetDomain = Data::Empty,
74 const Data& netNs = Data::Empty);
75
76 Tuple(const Data& printableAddress,
77 int port,
78 TransportType type,
79 const Data& targetDomain = Data::Empty,
80 const Data& netNs = Data::Empty);
81
82 Tuple(const in_addr& pipv4,
83 int pport,
84 TransportType ptype,
85 const Data& targetDomain = Data::Empty,
86 const Data& netNs = Data::Empty);
87
88 Tuple(const sockaddr& addr,
89 TransportType ptype,
90 const Data& targetDomain = Data::Empty);
91
92#ifdef IPPROTO_IPV6
93 // enable this if the current platform supports IPV6; the USE_IPV6 #define
94 // will determine if this c'tor is actually implemented.
95 // ?bwc? Is there a more standard preprocessor macro for this?
96 // ?bwc? Is there a way we can add something more informative to the
97 // linker error we'll see if we compiled without USE_IPV6, on a platform
98 // with IPV6, and someone tries to invoke this c'tor? (ie; "This library
99 // was built with IPV6 support disabled")
100 Tuple(const in6_addr& pipv6,
101 int pport,
102 TransportType ptype,
103 const Data& targetDomain = Data::Empty,
104 const Data& netNs = Data::Empty);
105#endif
106
109 const sockaddr& getSockaddr() const { return mSockaddr; }
110
113 sockaddr& getMutableSockaddr() { return mSockaddr; }
116 void copySockaddrAnyPort(sockaddr *sa);
117
120 void setSockaddr(const GenericIPAddress &);
121
122 TransportType getType() const { return mTransportType; }
123 void setType(TransportType type) { mTransportType = type; }
124 void setPort(int port);
125 int getPort() const;
126 inline FlowKey getFlowKey() const { return mFlowKey; }
127 void setFlowKey(FlowKey flowKey) { mFlowKey = flowKey; }
128
131 bool isV4() const;
132
134 IpVersion ipVersion() const;
135
138 bool isAnyInterface() const;
139 socklen_t length() const; // of sockaddr
140 bool isLoopback() const;
141 bool isPrivateAddress() const; // Return boolean based on definitions in RFC1918(v4) and RFC4193(v6)
142 bool isSpecialPurposeAddress() const; // Return boolean based on definitions in RFC6890
143
146 bool operator<(const Tuple& rhs) const;
147
150 bool operator==(const Tuple& rhs) const;
151
154 bool isEqual(const Tuple& tuple, bool ignorePort = false, bool ignoreTransport = false) const;
155
157 Data presentationFormat() const;
158
161 static TransportType toTransport( const Data& );
162
165 static const Data& toData( TransportType );
166
167 static const Data& toDataLower(TransportType type);
168
171 static Data inet_ntop(const Tuple& tuple);
172
173 // Creates a binary token from the provided Tuple - if salt is provided, then an HMAC is appended
174 // to the end of the token
175 static void writeBinaryToken(const Tuple& tuple, Data& container, const Data& salt=Data::Empty);
176 // Creates a Tuple from the provided binary token - if salt is provided, then an HMAC is checked
177 static Tuple makeTupleFromBinaryToken(const Data& binaryToken, const Data& salt=Data::Empty);
178
180
191
193
197 bool isEqualWithMask(const Tuple& tuple, short mask, bool ignorePort=false, bool ignoreTransport=false) const;
198
206 bool isAddressAllowed(const std::vector<AddressMask>& allowed,
207 const std::vector<AddressMask>& denied) const;
208
213 {
214 public:
215 bool operator()(const Tuple& x,
216 const Tuple& y) const;
217 };
219
226 {
227 public:
228 bool operator()(const Tuple& x,
229 const Tuple& y) const;
230 };
231 friend class AnyPortCompare;
232
236 {
237 public:
238 bool operator()(const Tuple& x,
239 const Tuple& y) const;
240 };
242
244 {
245 public:
246 bool operator()(const Tuple& x,
247 const Tuple& y) const;
248 };
249 friend class FlowKeyCompare;
250
252 void setTargetDomain(const Data& target)
253 {
254 mTargetDomain = target;
255 }
256
259 const Data& getTargetDomain() const
260 {
261 return mTargetDomain;
262 }
263
265 void setNetNs(const Data& netNs)
266 {
267 mNetNs = netNs;
268 }
269
271 const Data& getNetNs() const
272 {
273 return(mNetNs);
274 }
275
279 size_t hash() const;
280
281private:
282 union
283 {
284 sockaddr mSockaddr;
285 sockaddr_in m_anonv4;
286#ifdef IPPROTO_IPV6
287 // enable this if the current platform supports IPV6
288 // ?bwc? Is there a more standard preprocessor macro for this?
289 sockaddr_in6 m_anonv6;
290#endif
291 char pad[RESIP_MAX_SOCKADDR_SIZE]; //< this make union same size if v6 is in or out
292 };
293 TransportType mTransportType;
294 Data mTargetDomain;
295
296 Data mNetNs;
297
298 friend EncodeStream& operator<<(EncodeStream& strm, const Tuple& tuple);
299 friend class DnsResult;
300};
301
302
304operator<<(EncodeStream& ostrm, const Tuple& tuple);
305
314{
316 short mMaskBits = 0;
317};
318
320operator<<(EncodeStream& ostrm, const AddressMask& addressMask);
321
322}
323
325
326#endif
327/* ====================================================================
328 * The Vovida Software License, Version 1.0
329 *
330 * Copyright (c) 2026 SIP Spectrum, Inc. https://www.sipspectrum.com
331 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
332 *
333 * Redistribution and use in source and binary forms, with or without
334 * modification, are permitted provided that the following conditions
335 * are met:
336 *
337 * 1. Redistributions of source code must retain the above copyright
338 * notice, this list of conditions and the following disclaimer.
339 *
340 * 2. Redistributions in binary form must reproduce the above copyright
341 * notice, this list of conditions and the following disclaimer in
342 * the documentation and/or other materials provided with the
343 * distribution.
344 *
345 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
346 * and "Vovida Open Communication Application Library (VOCAL)" must
347 * not be used to endorse or promote products derived from this
348 * software without prior written permission. For written
349 * permission, please contact vocal@vovida.org.
350 *
351 * 4. Products derived from this software may not be called "VOCAL", nor
352 * may "VOCAL" appear in their name, without prior written
353 * permission of Vovida Networks, Inc.
354 *
355 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
356 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
357 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
358 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
359 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
360 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
361 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
362 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
363 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
364 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
365 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
366 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
367 * DAMAGE.
368 *
369 * ====================================================================
370 *
371 * This software consists of voluntary contributions made by Vovida
372 * Networks, Inc. and many individuals on behalf of Vovida Networks,
373 * Inc. For more information on Vovida Networks, Inc., please see
374 * <http://www.vovida.org/>.
375 *
376 */
Wraps your platform's implementation of hash_map, hash_multimap, and hash_set.
#define HashValue(type)
Definition HashMap.hxx:18
Handles cross-platform sockets compatibility.
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
static const Data Empty
Constant that represents a zero-length data.
Definition Data.hxx:882
A "less than" comparator for Tuple, for use in map containers etc.
Definition Tuple.hxx:213
bool operator()(const Tuple &x, const Tuple &y) const
Definition Tuple.cxx:1031
A "less than" comparator for Tuple, for use in map containers etc.
Definition Tuple.hxx:236
bool operator()(const Tuple &x, const Tuple &y) const
Definition Tuple.cxx:1189
A "less than" comparator for Tuple, for use in map containers etc.
Definition Tuple.hxx:226
bool operator()(const Tuple &x, const Tuple &y) const
Definition Tuple.cxx:1084
bool operator()(const Tuple &x, const Tuple &y) const
Definition Tuple.cxx:1158
Represents a network address.
Definition Tuple.hxx:59
RESIP_HeapCount(Tuple)
friend EncodeStream & operator<<(EncodeStream &strm, const Tuple &tuple)
IpVersion ipVersion() const
Returns V4 or V6 as appropriate.
Definition Tuple.cxx:537
Data presentationFormat() const
Wrapper around the inet_top() method.
Definition Tuple.cxx:426
static Tuple makeTupleFromBinaryToken(const Data &binaryToken, const Data &salt=Data::Empty)
Definition Tuple.cxx:328
size_t hash() const
Creates a 32-bit hash based on the contents of this Tuple.
Definition Tuple.cxx:887
char pad[RESIP_MAX_SOCKADDR_SIZE]
Definition Tuple.hxx:291
sockaddr & getMutableSockaddr()
Retrieve the binary representation of the socket address for this tuple.
Definition Tuple.hxx:113
void setTargetDomain(const Data &target)
Set the domain name this address tuple intends to represent.
Definition Tuple.hxx:252
void setPort(int port)
Definition Tuple.cxx:449
void setNetNs(const Data &netNs)
Set the netns (network namespace) for this Tuple.
Definition Tuple.hxx:265
static const Data & toData(TransportType)
Converts the TransportType to a string representation of the transport type, e.g.
Definition Tuple.cxx:927
bool isLoopback() const
Definition Tuple.cxx:503
bool operator==(const Tuple &rhs) const
Compares TransportType, the binary address, port, and address family of the Tuple.
Definition Tuple.cxx:674
const Data & getNetNs() const
Get the netns for this Tuple.
Definition Tuple.hxx:271
GenericIPAddress toGenericIPAddress() const
Definition Tuple.cxx:1169
bool isAnyInterface() const
TRUE if this address is equal to the "INADDR_ANY" value for this address family.
Definition Tuple.cxx:485
bool onlyUseExistingConnection
Definition Tuple.hxx:192
bool isPrivateAddress() const
Definition Tuple.cxx:552
FlowKey mFlowKey
This is a (largely) opaque key that subclasses of Transport will use to help record/find flows.
Definition Tuple.hxx:189
sockaddr_in m_anonv4
Definition Tuple.hxx:285
bool isAddressAllowed(const std::vector< AddressMask > &allowed, const std::vector< AddressMask > &denied) const
Tests whether this address is permitted by the supplied allow/deny address lists.
Definition Tuple.cxx:858
static const Data & toDataLower(TransportType type)
Definition Tuple.cxx:933
void setType(TransportType type)
Definition Tuple.hxx:123
static void writeBinaryToken(const Tuple &tuple, Data &container, const Data &salt=Data::Empty)
Definition Tuple.cxx:266
bool isEqual(const Tuple &tuple, bool ignorePort=false, bool ignoreTransport=false) const
compares this tuple with the one passed in for family, port and address equality.
Definition Tuple.cxx:680
sockaddr mSockaddr
Definition Tuple.hxx:284
void copySockaddrAnyPort(sockaddr *sa)
Get a copy of the socket address including the interface and not the port number.
Definition Tuple.cxx:218
socklen_t length() const
Definition Tuple.cxx:655
bool isEqualWithMask(const Tuple &tuple, short mask, bool ignorePort=false, bool ignoreTransport=false) const
compares this tuple with the one passed in for family, port and address equality using the passed in ...
Definition Tuple.cxx:956
bool isV4() const
Definition Tuple.cxx:531
int getPort() const
Definition Tuple.cxx:466
TransportType getType() const
Definition Tuple.hxx:122
static Data inet_ntop(const Tuple &tuple)
Converts the binary socket address to presentation format, via the DnsUtil::inet_ntop() method.
Definition Tuple.cxx:939
const sockaddr & getSockaddr() const
Retrieve a const binary representation of the socket address for this tuple.
Definition Tuple.hxx:109
static TransportType toTransport(const Data &)
Converts a string representation of transport type, i.e.
Definition Tuple.cxx:921
bool isSpecialPurposeAddress() const
Definition Tuple.cxx:611
void setSockaddr(const GenericIPAddress &)
Set the internal binary representation of the socket address from the GenericIPAddress.
Definition Tuple.cxx:239
Tuple(const sockaddr &addr, TransportType ptype, const Data &targetDomain=Data::Empty)
const Data & getTargetDomain() const
Get the domain name this address tuple intends to represent.
Definition Tuple.hxx:259
FlowKey getFlowKey() const
Definition Tuple.hxx:126
TransportKey mTransportKey
Definition Tuple.hxx:190
bool operator<(const Tuple &rhs) const
Compares TransportType, the binary address, port, and address family of the Tuple.
Definition Tuple.cxx:711
void setFlowKey(FlowKey flowKey)
Definition Tuple.hxx:127
This file is used to handle compatibility fixes/tweaks so reSIProcate can function on multiple platfo...
#define RESIP_MAX_SOCKADDR_SIZE
Definition compat.hxx:107
dcm! – add UnusedChecking(_enum) below;
unsigned long FlowKey
Definition Tuple.hxx:37
unsigned long TransportKey
Definition Tuple.hxx:38
TransportType
An enumeration of transport protocols.
@ UNKNOWN_TRANSPORT
IpVersion
An enumeration of IP versions.
EncodeStream & operator<<(EncodeStream &strm, const BaseException &e)
#define EncodeStream
An IP address paired with a CIDR prefix length (number of mask bits), for use in address-based access...
Definition Tuple.hxx:314
Represents an IP-address and port (V4 or V6).
void list()
Definition userAdmin.cxx:66