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 setIp(const IPvXAddress& ip, int port = -1,
00192                NatType natType = UNKNOWN_NAT);
00193 
00201     void setAddress(const IPvXAddress& ip, int port = -1,
00202                     NatType natType = UNKNOWN_NAT) __attribute ((deprecated))
00203                     { setIp(ip, port, natType); };
00204 
00210     void appendSourceRoute(const TransportAddress& sourceRoute);
00211 
00216     void clearSourceRoute() { sourceRoute.clear(); };
00217 
00223     void setPort( int port );
00224 
00230     const IPvXAddress& getIp() const;
00231 
00237     const IPvXAddress& getAddress() const __attribute ((deprecated))
00238             { return getIp(); };
00239 
00245     int getPort() const;
00246 
00252     NatType getNatType() const;
00253 
00259     size_t getSourceRouteSize() const;
00260 
00266     const TransportAddressVector& getSourceRoute() const;
00267 
00273     bool isUnspecified() const;
00274 
00275 public://methods: hashing
00276 
00282     size_t hash() const;
00283 
00284 public://methods: c++ streaming
00285 
00294     friend std::ostream& operator<<(std::ostream& os, const TransportAddress& n);
00295 
00296 private://methods:
00297 
00303     void assertUnspecified( const TransportAddress& handle ) const;
00304 
00305 public:
00306 
00312     virtual TransportAddress* dup() const;
00313 };
00314 
00321 inline void doPacking(cCommBuffer *buf, TransportAddress& addr)
00322 {
00323     doPacking(buf, addr.getIp());
00324     doPacking(buf, addr.getPort());
00325     doPacking(buf, static_cast<uint8_t>(addr.getNatType()));
00326     doPacking(buf, static_cast<uint8_t>(addr.getSourceRouteSize()));
00327     for (size_t i = 0; i < addr.getSourceRouteSize(); i++) {
00328         // TODO: ugly const_cast should be avoided
00329         doPacking(buf, const_cast<TransportAddress&>(addr.getSourceRoute()[i]));
00330     }
00331 }
00332 
00339 inline void doUnpacking(cCommBuffer *buf, TransportAddress& addr)
00340 {
00341     IPvXAddress ip;
00342     int port;
00343     uint8_t natType = 0;
00344     doUnpacking(buf, ip);
00345     doUnpacking(buf, port);
00346     doUnpacking(buf, natType);
00347     addr.setIp(ip, port, static_cast<TransportAddress::NatType>(natType));
00348 
00349     uint8_t sourceRouteSize;
00350     TransportAddress sourceRouteEntry;
00351     doUnpacking(buf, sourceRouteSize);
00352     for (size_t i = 0; i < sourceRouteSize; i++) {
00353         doUnpacking(buf, sourceRouteEntry);
00354         addr.appendSourceRoute(sourceRouteEntry);
00355     }
00356 }
00357 
00358 
00359 #endif