OverSim
TransportAddress.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
25 #include <omnetpp.h>
26 
27 #include "TransportAddress.h"
28 
29 // predefined transport address
31 const std::vector<TransportAddress> TransportAddress::UNSPECIFIED_NODES;
32 
33 std::ostream& operator<<(std::ostream& os, const TransportAddress& n)
34 {
35  if (n.isUnspecified()) {
36  os << "<addr unspec>";
37  } else {
38  os << n.ip << ":" << n.port;
39  }
40 
41  if (n.getSourceRouteSize() > 0) {
42  os << "(SR:";
43  for (size_t i = 0; i < n.getSourceRouteSize(); i++) {
44  os << " " << n.getSourceRoute()[i].ip << ":"
45  << n.getSourceRoute()[i].port;
46  }
47  os << ")";
48  }
49 
50  return os;
51 };
52 
53 
54 //default-constructor
56 {
57  port = -1;
59 }
60 
61 //copy constructor
63 {
64  port = handle.port;
65  ip = handle.ip;
66  natType = handle.natType;
67  sourceRoute = handle.sourceRoute;
68 }
69 
70 
71 //complete constructor
72 TransportAddress::TransportAddress( const IPvXAddress& ip, int port,
73  NatType natType)
74 {
75  this->ip = ip;
76  this->port = port;
77  this->natType = natType;
78 }
79 
80 //public
82 {
83  return (ip.isUnspecified() || (port == -1));
84 }
85 
86 //public
88 {
89  this->ip = rhs.ip;
90  this->port = rhs.port;
91 
92  return *this;
93 }
94 
95 //public
97 {
98  assertUnspecified(rhs);
99  return (this->ip == rhs.ip && this->port == rhs.port);
100 }
101 
102 //public
104 {
105  assertUnspecified(rhs);
106  return !operator==(rhs);
107 }
108 
109 //public
111 {
112  assertUnspecified(rhs);
113  if (ip < rhs.ip) {
114  return true;
115  } else if (rhs.ip < ip) {
116  return false;
117  } else if (port < rhs.port) {
118  return true;
119  }
120  return false;
121 }
122 
123 //public
125 {
126  assertUnspecified(rhs);
127  if (rhs.ip < ip) {
128  return true;
129  } else if (ip < rhs.ip) {
130  return false;
131  } else if (port > rhs.port) {
132  return true;
133  }
134  return false;
135 }
136 
137 //public
139 {
140  return !operator>(rhs);
141 }
142 
143 //public
145 {
146  return !operator<(rhs);
147 }
148 
149 //public
150 void TransportAddress::setIp(const IPvXAddress& ip, int port,
151  NatType natType)
152 {
153  this->ip = ip;
154  if (port!=-1)
155  this->port = port;
156  if (natType != UNKNOWN_NAT)
157  this->natType = natType;
158 }
159 
160 //public
162 {
163  this->port = port;
164 }
165 
166 //public
167 const IPvXAddress& TransportAddress::getIp() const
168 {
169  return ip;
170 }
171 
172 //public
174 {
175  return port;
176 }
177 
178 //public
180 {
181  return natType;
182 }
183 
184 //public
186 {
187  return sourceRoute.size();
188 }
189 
190 //public
192 {
193  return sourceRoute;
194 }
195 
196 //public
198 {
199  sourceRoute.push_back(TransportAddress(add.ip, add.port, add.natType));
200  const TransportAddressVector& sr = add.getSourceRoute();
201  for (size_t i = 0; i < sr.size(); i++) {
202  if (sr[i].getSourceRouteSize() > 0) {
203  throw cRuntimeError("TransportAddress::appendSourceRoute(): "
204  "Trying to add source route to source route!");
205  }
206  sourceRoute.push_back(TransportAddress(sr[i].ip, sr[i].port,
207  sr[i].natType));
208  }
209 }
210 
211 
212 //public
214 {
215  size_t iphash;
216  if (ip.isIPv6()) {
217  uint32_t* addr = (uint32_t*) ip.get6().words();
218  iphash = (size_t)(addr[0]^addr[1]^addr[2]^addr[3]);
219  } else {
220  iphash = (size_t)ip.get4().getInt();
221  }
222 
223  return (size_t)(iphash^port);
224 }
225 
227 {
228  return new TransportAddress(*this);
229 }
230 
231 //private
232 inline void TransportAddress::assertUnspecified( const TransportAddress& handle ) const
233 {
234  if ( this->isUnspecified() || handle.isUnspecified() )
235  opp_error("TransportAddress: Trying to compare unspecified TransportAddress!");
236 }