reSIProcate/stack  9694
Tuple.hxx
Go to the documentation of this file.
00001 #if !defined(RESIP_TUPLE_HXX)
00002 #define RESIP_TUPLE_HXX
00003 
00004 #ifdef HAVE_CONFIG_H
00005 #include "config.h"
00006 #endif
00007 
00008 
00009 #include "rutil/Socket.hxx"
00010 #include "rutil/compat.hxx"
00011 
00012 #include "rutil/HashMap.hxx"
00013 #include "rutil/TransportType.hxx"
00014 #include "rutil/HeapInstanceCounter.hxx"
00015 #include "rutil/Data.hxx"
00016 
00017 #if defined(WIN32)
00018 #include <Ws2tcpip.h>
00019 #else
00020 #include <netinet/in.h>
00021 #endif
00022 
00023 namespace resip
00024 {
00025 
00026 struct GenericIPAddress;
00027 class Transport;
00028 
00029 // WARNING!!
00030 // When you change this structure, make sure to update the hash function,
00031 // operator== and operator< to be consistent with the new structure. For
00032 // instance, the Connection* and Transport* change value in the Tuple over
00033 // its lifetime so they must not be included in the hash or comparisons. 
00034 
00035 typedef unsigned long FlowKey;
00036 typedef unsigned long TransportKey;
00037 
00058 class Tuple
00059 {
00060    public:
00061       RESIP_HeapCount(Tuple);
00062 
00063       Tuple();
00064 
00065       explicit Tuple(const GenericIPAddress& genericAddress, 
00066                      TransportType type=UNKNOWN_TRANSPORT, 
00067                      const Data& targetDomain = Data::Empty);
00068 
00069       Tuple(const Data& printableAddress, 
00070             int port, 
00071             IpVersion ipVer, 
00072             TransportType type=UNKNOWN_TRANSPORT, 
00073             const Data& targetDomain = Data::Empty);
00074 
00075       Tuple(const Data& printableAddress, 
00076             int port, 
00077             TransportType type, 
00078             const Data& targetDomain = Data::Empty);
00079 
00080       Tuple(const in_addr& pipv4, 
00081             int pport,
00082             TransportType ptype, 
00083             const Data& targetDomain = Data::Empty);
00084 
00085       Tuple(const sockaddr& addr, 
00086             TransportType ptype, 
00087             const Data& targetDomain = Data::Empty);
00088 
00089 #ifdef IPPROTO_IPV6
00090       // enable this if the current platform supports IPV6; the USE_IPV6 #define
00091       // will determine if this c'tor is actually implemented.
00092       // ?bwc? Is there a more standard preprocessor macro for this?
00093       // ?bwc? Is there a way we can add something more informative to the 
00094       // linker error we'll see if we compiled without USE_IPV6, on a platform
00095       // with IPV6, and someone tries to invoke this c'tor? (ie; "This library
00096       // was built with IPV6 support disabled")
00097       Tuple(const in6_addr& pipv6,  
00098             int pport, 
00099             TransportType ptype, 
00100             const Data& targetDomain = Data::Empty);
00101 #endif
00102       
00105       const sockaddr& getSockaddr() const { return mSockaddr; }
00106 
00109       sockaddr& getMutableSockaddr() { return mSockaddr; }
00110 
00113       void setSockaddr(const GenericIPAddress &);
00114 
00115       TransportType getType() const { return mTransportType; }
00116       void setType(TransportType type) { mTransportType = type ;}
00117       void setPort(int port);
00118       int getPort() const;
00119       inline FlowKey getFlowKey() const { return mFlowKey;} 
00120 
00123       bool isV4() const; 
00124 
00126       IpVersion ipVersion() const;
00127       void setIpVersion(IpVersion version);
00128 
00131       bool isAnyInterface() const;
00132       socklen_t length() const; // of sockaddr
00133       bool isLoopback() const;
00134       bool isPrivateAddress() const;  // Return boolean based on definitions in RFC1918(v4) and RFC4193(v6)
00135       
00138       bool operator<(const Tuple& rhs) const;
00139 
00142       bool operator==(const Tuple& rhs) const;
00143       
00145       Data presentationFormat() const;
00146       
00149       static TransportType toTransport( const Data& );
00150 
00153       static const Data& toData( TransportType );
00154 
00155       static const Data& toDataLower(TransportType type);
00156 
00159       static Data inet_ntop(const Tuple& tuple);
00160 
00161       // Creates a binary token from the provided Tuple - if salt is provided, then an HMAC is appended
00162       // to the end of the token
00163       static void writeBinaryToken(const Tuple& tuple, Data& container, const Data& salt=Data::Empty);
00164       // Creates a Tuple from the provided binary token - if salt is provided, then an HMAC is checked
00165       static Tuple makeTupleFromBinaryToken(const Data& binaryToken, const Data& salt=Data::Empty);
00166 
00167       GenericIPAddress toGenericIPAddress() const;
00168 
00177       FlowKey mFlowKey;
00178       TransportKey transportKey;
00179 
00180       // deprecate
00181       Transport* transport;
00182       bool onlyUseExistingConnection;
00183 
00187       bool isEqualWithMask(const Tuple& tuple, short mask, bool ignorePort=false, bool ignoreTransport=false) const;
00188 
00192       class AnyInterfaceCompare
00193       {
00194          public:
00195             bool operator()(const Tuple& x,
00196                             const Tuple& y) const;
00197       };
00198       friend class AnyInterfaceCompare;
00199 
00205       class AnyPortCompare
00206       {
00207          public:
00208             bool operator()(const Tuple& x,
00209                             const Tuple& y) const;
00210       };
00211       friend class AnyPortCompare;
00212 
00215       class AnyPortAnyInterfaceCompare
00216       {
00217          public:
00218             bool operator()(const Tuple& x,
00219                             const Tuple& y) const;
00220       };
00221       friend class AnyPortAnyInterfaceCompare;
00222 
00223       class FlowKeyCompare
00224       {
00225          public:
00226             bool operator()(const Tuple& x,
00227                             const Tuple& y) const;
00228       };
00229       friend class FlowKeyCompare;
00230 
00232       void setTargetDomain(const Data& target)
00233       {
00234          mTargetDomain = target;
00235       }
00236       
00239       const Data& getTargetDomain() const
00240       {
00241          return mTargetDomain;
00242       }
00243       
00247       size_t hash() const;   
00248 
00249 private:
00250       union 
00251       {
00252             sockaddr mSockaddr;
00253             sockaddr_in m_anonv4;
00254 #ifdef IPPROTO_IPV6
00255             // enable this if the current platform supports IPV6
00256             // ?bwc? Is there a more standard preprocessor macro for this?
00257             sockaddr_in6 m_anonv6;
00258 #endif
00259             char pad[28]; //< this make union same size if v6 is in or out
00260       };
00261       TransportType mTransportType;
00262       Data mTargetDomain; 
00263 
00264       friend EncodeStream& operator<<(EncodeStream& strm, const Tuple& tuple);
00265       friend class DnsResult;
00266 };
00267 
00268 
00269 EncodeStream&
00270 operator<<(EncodeStream& ostrm, const Tuple& tuple);
00271 
00272 }
00273 
00274 HashValue(resip::Tuple);
00275 
00276 #endif
00277 /* ====================================================================
00278  * The Vovida Software License, Version 1.0 
00279  * 
00280  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00281  * 
00282  * Redistribution and use in source and binary forms, with or without
00283  * modification, are permitted provided that the following conditions
00284  * are met:
00285  * 
00286  * 1. Redistributions of source code must retain the above copyright
00287  *    notice, this list of conditions and the following disclaimer.
00288  * 
00289  * 2. Redistributions in binary form must reproduce the above copyright
00290  *    notice, this list of conditions and the following disclaimer in
00291  *    the documentation and/or other materials provided with the
00292  *    distribution.
00293  * 
00294  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00295  *    and "Vovida Open Communication Application Library (VOCAL)" must
00296  *    not be used to endorse or promote products derived from this
00297  *    software without prior written permission. For written
00298  *    permission, please contact vocal@vovida.org.
00299  *
00300  * 4. Products derived from this software may not be called "VOCAL", nor
00301  *    may "VOCAL" appear in their name, without prior written
00302  *    permission of Vovida Networks, Inc.
00303  * 
00304  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00305  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00306  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00307  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00308  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00309  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00310  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00311  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00312  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00313  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00314  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00315  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00316  * DAMAGE.
00317  * 
00318  * ====================================================================
00319  * 
00320  * This software consists of voluntary contributions made by Vovida
00321  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00322  * Inc.  For more information on Vovida Networks, Inc., please see
00323  * <http://www.vovida.org/>.
00324  *
00325  */