TransportAddress.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00026 #ifndef __TRANSPORTADDRESS_H_
00027 #define __TRANSPORTADDRESS_H_
00028 
00029 //#include <stdint.h>
00030 #include <oversim_mapset.h>
00031 
00032 #include <omnetpp.h>
00033 #include <IPvXAddress.h>
00034 
00035 class TransportAddress;
00036 typedef std::vector<TransportAddress> TransportAddressVector;
00037 
00049 class TransportAddress
00050 {
00051 public:
00052 
00056     class hashFcn
00057     {
00058     public:
00059         size_t operator()( const TransportAddress& h1 ) const
00060         {
00061             return h1.hash();
00062         }
00063     };
00064 
00065     // TODO: maybe we should use const uint8_t instead to save memory
00066     enum NatType {
00067         UNKNOWN_NAT = 0,
00068         NO_NAT = 1,
00069         FULL_CONE_NAT = 2,
00070         PORT_RESTRICTED_NAT = 3,
00071         SYMMETRIC_NAT = 4
00072     };
00073 
00074 public://collection typedefs
00075 
00076     typedef UNORDERED_SET<TransportAddress, hashFcn> Set; 
00078 protected://fields
00079 
00080     IPvXAddress ip; 
00081     int port; 
00083 private:
00084     NatType natType; 
00085     // TODO: as soon as sourceRoute is used, we need to calculate the correct TransportAddress length for statistics */
00086     TransportAddressVector sourceRoute; 
00088 public://construction
00089 
00093     TransportAddress( );
00094 
00098     virtual ~TransportAddress( ) {};
00099 
00105     TransportAddress( const TransportAddress& handle );
00106 
00114     TransportAddress(const IPvXAddress& ip, int port = -1,
00115                      NatType natType = UNKNOWN_NAT);
00116 
00117 public://static fields
00118 
00119     static const TransportAddress UNSPECIFIED_NODE; 
00120     static const TransportAddressVector UNSPECIFIED_NODES;
00121 
00122 public://methods: delegates to OverlayKey and IPvXAddress
00123 
00130     bool operator==(const TransportAddress& rhs) const;
00131 
00138     bool operator!=(const TransportAddress& rhs) const;
00139 
00146     bool operator< (const TransportAddress& rhs) const;
00147 
00154     bool operator> (const TransportAddress& rhs) const;
00155 
00162     bool operator<=(const TransportAddress& rhs) const;
00163 
00170     bool operator>=(const TransportAddress& rhs) const;
00171 
00172 public://methods: operators
00173 
00180     TransportAddress& operator=(const TransportAddress& rhs);
00181 
00182 public://methods: setters and getters
00183 
00191     void setAddress( const IPvXAddress& ip, int port = -1,
00192                      NatType natType = UNKNOWN_NAT);
00193 
00199     void appendSourceRoute(const TransportAddress& sourceRoute);
00200 
00205     void clearSourceRoute() { sourceRoute.clear(); };
00206 
00212     void setPort( int port );
00213 
00219     const IPvXAddress& getAddress() const;
00220 
00226     int getPort() const;
00227 
00233     NatType getNatType() const;
00234 
00240     size_t getSourceRouteSize() const;
00241 
00247     const TransportAddressVector& getSourceRoute() const;
00248 
00254     bool isUnspecified() const;
00255 
00256 public://methods: hashing
00257 
00263     size_t hash() const;
00264 
00265 public://methods: c++ streaming
00266 
00275     friend std::ostream& operator<<(std::ostream& os, const TransportAddress& n);
00276 
00277 private://methods:
00278 
00284     void assertUnspecified( const TransportAddress& handle ) const;
00285 
00286 public:
00287 
00293     virtual TransportAddress* dup() const;
00294 };
00295 
00302 inline void doPacking(cCommBuffer *buf, TransportAddress& addr)
00303 {
00304     doPacking(buf, addr.getAddress());
00305     doPacking(buf, addr.getPort());
00306     doPacking(buf, static_cast<uint8_t>(addr.getNatType()));
00307     doPacking(buf, static_cast<uint8_t>(addr.getSourceRouteSize()));
00308     for (size_t i = 0; i < addr.getSourceRouteSize(); i++) {
00309         // TODO: ugly const_cast should be avoided
00310         doPacking(buf, const_cast<TransportAddress&>(addr.getSourceRoute()[i]));
00311     }
00312 }
00313 
00320 inline void doUnpacking(cCommBuffer *buf, TransportAddress& addr)
00321 {
00322     IPvXAddress ip;
00323     int port;
00324     uint8_t natType = 0;
00325     doUnpacking(buf, ip);
00326     doUnpacking(buf, port);
00327     doUnpacking(buf, natType);
00328     addr.setAddress(ip, port, static_cast<TransportAddress::NatType>(natType));
00329 
00330     uint8_t sourceRouteSize;
00331     TransportAddress sourceRouteEntry;
00332     doUnpacking(buf, sourceRouteSize);
00333     for (size_t i = 0; i < sourceRouteSize; i++) {
00334         doUnpacking(buf, sourceRouteEntry);
00335         addr.appendSourceRoute(sourceRouteEntry);
00336     }
00337 }
00338 
00339 
00340 #endif
Generated on Wed May 26 16:21:15 2010 for OverSim by  doxygen 1.6.3