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 00025 #include <omnetpp.h> 00026 00027 #include "TransportAddress.h" 00028 00029 // predefined transport address 00030 const TransportAddress TransportAddress::UNSPECIFIED_NODE; 00031 const std::vector<TransportAddress> TransportAddress::UNSPECIFIED_NODES; 00032 00033 std::ostream& operator<<(std::ostream& os, const TransportAddress& n) 00034 { 00035 if (n.isUnspecified()) { 00036 os << "<addr unspec>"; 00037 } else { 00038 os << n.ip << ":" << n.port; 00039 } 00040 00041 if (n.getSourceRouteSize() > 0) { 00042 os << "(SR:"; 00043 for (size_t i = 0; i < n.getSourceRouteSize(); i++) { 00044 os << " " << n.getSourceRoute()[i].ip << ":" 00045 << n.getSourceRoute()[i].port; 00046 } 00047 os << ")"; 00048 } 00049 00050 return os; 00051 }; 00052 00053 00054 //default-constructor 00055 TransportAddress::TransportAddress() 00056 { 00057 port = -1; 00058 natType = UNKNOWN_NAT; 00059 } 00060 00061 //copy constructor 00062 TransportAddress::TransportAddress( const TransportAddress& handle ) 00063 { 00064 port = handle.port; 00065 ip = handle.ip; 00066 natType = handle.natType; 00067 sourceRoute = handle.sourceRoute; 00068 } 00069 00070 00071 //complete constructor 00072 TransportAddress::TransportAddress( const IPvXAddress& ip, int port, 00073 NatType natType) 00074 { 00075 this->ip = ip; 00076 this->port = port; 00077 this->natType = natType; 00078 } 00079 00080 //public 00081 bool TransportAddress::isUnspecified() const 00082 { 00083 return (ip.isUnspecified() || (port == -1)); 00084 } 00085 00086 //public 00087 TransportAddress& TransportAddress::operator=(const TransportAddress& rhs) 00088 { 00089 this->ip = rhs.ip; 00090 this->port = rhs.port; 00091 00092 return *this; 00093 } 00094 00095 //public 00096 bool TransportAddress::operator==(const TransportAddress& rhs) const 00097 { 00098 assertUnspecified(rhs); 00099 return (this->ip == rhs.ip && this->port == rhs.port); 00100 } 00101 00102 //public 00103 bool TransportAddress::operator!=(const TransportAddress& rhs) const 00104 { 00105 assertUnspecified(rhs); 00106 return !(this->ip == rhs.ip && this->port == rhs.port ); 00107 } 00108 00109 //public 00110 bool TransportAddress::operator<(const TransportAddress &rhs) const 00111 { 00112 assertUnspecified(rhs); 00113 return this->ip < rhs.ip; 00114 } 00115 00116 //public 00117 bool TransportAddress::operator>(const TransportAddress &rhs) const 00118 { 00119 assertUnspecified(rhs); 00120 return !(this->ip < rhs.ip || this->ip == rhs.ip); 00121 } 00122 00123 //public 00124 bool TransportAddress::operator<=(const TransportAddress &rhs) const 00125 { 00126 assertUnspecified(rhs); 00127 return this->ip < rhs.ip || this->ip == rhs.ip; 00128 } 00129 00130 //public 00131 bool TransportAddress::operator>=(const TransportAddress &rhs) const 00132 { 00133 assertUnspecified(rhs); 00134 return !(this->ip < rhs.ip); 00135 } 00136 00137 //public 00138 void TransportAddress::setAddress( const IPvXAddress& ip, int port, 00139 NatType natType) 00140 { 00141 this->ip = ip; 00142 if (port!=-1) 00143 this->port = port; 00144 if (natType != UNKNOWN_NAT) 00145 this->natType = natType; 00146 } 00147 00148 //public 00149 void TransportAddress::setPort( int port ) 00150 { 00151 this->port = port; 00152 } 00153 00154 //public 00155 const IPvXAddress& TransportAddress::getAddress() const 00156 { 00157 return ip; 00158 } 00159 00160 //public 00161 int TransportAddress::getPort() const 00162 { 00163 return port; 00164 } 00165 00166 //public 00167 TransportAddress::NatType TransportAddress::getNatType() const 00168 { 00169 return natType; 00170 } 00171 00172 //public 00173 size_t TransportAddress::getSourceRouteSize() const 00174 { 00175 return sourceRoute.size(); 00176 } 00177 00178 //public 00179 const TransportAddressVector& TransportAddress::getSourceRoute() const 00180 { 00181 return sourceRoute; 00182 } 00183 00184 //public 00185 void TransportAddress::appendSourceRoute(const TransportAddress& add) 00186 { 00187 sourceRoute.push_back(TransportAddress(add.ip, add.port, add.natType)); 00188 const TransportAddressVector& sr = add.getSourceRoute(); 00189 for (size_t i = 0; i < sr.size(); i++) { 00190 if (sr[i].getSourceRouteSize() > 0) { 00191 throw cRuntimeError("TransportAddress::appendSourceRoute(): " 00192 "Trying to add source route to source route!"); 00193 } 00194 sourceRoute.push_back(TransportAddress(sr[i].ip, sr[i].port, 00195 sr[i].natType)); 00196 } 00197 } 00198 00199 00200 //public 00201 size_t TransportAddress::hash() const 00202 { 00203 size_t iphash; 00204 if (ip.isIPv6()) { 00205 uint32_t* addr = (uint32_t*) ip.get6().words(); 00206 iphash = (size_t)(addr[0]^addr[1]^addr[2]^addr[3]); 00207 } else { 00208 iphash = (size_t)ip.get4().getInt(); 00209 } 00210 00211 return (size_t)(iphash^port); 00212 } 00213 00214 TransportAddress* TransportAddress::dup() const 00215 { 00216 return new TransportAddress(*this); 00217 } 00218 00219 //private 00220 inline void TransportAddress::assertUnspecified( const TransportAddress& handle ) const 00221 { 00222 if ( this->isUnspecified() || handle.isUnspecified() ) 00223 opp_error("TransportAddress: Trying to compare unspecified TransportAddress!"); 00224 }
1.5.8