Coverage Report

Created: 2026-04-01 07:00

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