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 !operator==(rhs); 00107 } 00108 00109 //public 00110 bool TransportAddress::operator<(const TransportAddress &rhs) const 00111 { 00112 assertUnspecified(rhs); 00113 if (ip < rhs.ip) { 00114 return true; 00115 } else if (rhs.ip < ip) { 00116 return false; 00117 } else if (port < rhs.port) { 00118 return true; 00119 } 00120 return false; 00121 } 00122 00123 //public 00124 bool TransportAddress::operator>(const TransportAddress &rhs) const 00125 { 00126 assertUnspecified(rhs); 00127 if (rhs.ip < ip) { 00128 return true; 00129 } else if (ip < rhs.ip) { 00130 return false; 00131 } else if (port > rhs.port) { 00132 return true; 00133 } 00134 return false; 00135 } 00136 00137 //public 00138 bool TransportAddress::operator<=(const TransportAddress &rhs) const 00139 { 00140 return !operator>(rhs); 00141 } 00142 00143 //public 00144 bool TransportAddress::operator>=(const TransportAddress &rhs) const 00145 { 00146 return !operator<(rhs); 00147 } 00148 00149 //public 00150 void TransportAddress::setAddress( const IPvXAddress& ip, int port, 00151 NatType natType) 00152 { 00153 this->ip = ip; 00154 if (port!=-1) 00155 this->port = port; 00156 if (natType != UNKNOWN_NAT) 00157 this->natType = natType; 00158 } 00159 00160 //public 00161 void TransportAddress::setPort( int port ) 00162 { 00163 this->port = port; 00164 } 00165 00166 //public 00167 const IPvXAddress& TransportAddress::getAddress() const 00168 { 00169 return ip; 00170 } 00171 00172 //public 00173 int TransportAddress::getPort() const 00174 { 00175 return port; 00176 } 00177 00178 //public 00179 TransportAddress::NatType TransportAddress::getNatType() const 00180 { 00181 return natType; 00182 } 00183 00184 //public 00185 size_t TransportAddress::getSourceRouteSize() const 00186 { 00187 return sourceRoute.size(); 00188 } 00189 00190 //public 00191 const TransportAddressVector& TransportAddress::getSourceRoute() const 00192 { 00193 return sourceRoute; 00194 } 00195 00196 //public 00197 void TransportAddress::appendSourceRoute(const TransportAddress& add) 00198 { 00199 sourceRoute.push_back(TransportAddress(add.ip, add.port, add.natType)); 00200 const TransportAddressVector& sr = add.getSourceRoute(); 00201 for (size_t i = 0; i < sr.size(); i++) { 00202 if (sr[i].getSourceRouteSize() > 0) { 00203 throw cRuntimeError("TransportAddress::appendSourceRoute(): " 00204 "Trying to add source route to source route!"); 00205 } 00206 sourceRoute.push_back(TransportAddress(sr[i].ip, sr[i].port, 00207 sr[i].natType)); 00208 } 00209 } 00210 00211 00212 //public 00213 size_t TransportAddress::hash() const 00214 { 00215 size_t iphash; 00216 if (ip.isIPv6()) { 00217 uint32_t* addr = (uint32_t*) ip.get6().words(); 00218 iphash = (size_t)(addr[0]^addr[1]^addr[2]^addr[3]); 00219 } else { 00220 iphash = (size_t)ip.get4().getInt(); 00221 } 00222 00223 return (size_t)(iphash^port); 00224 } 00225 00226 TransportAddress* TransportAddress::dup() const 00227 { 00228 return new TransportAddress(*this); 00229 } 00230 00231 //private 00232 inline void TransportAddress::assertUnspecified( const TransportAddress& handle ) const 00233 { 00234 if ( this->isUnspecified() || handle.isUnspecified() ) 00235 opp_error("TransportAddress: Trying to compare unspecified TransportAddress!"); 00236 }