OverSim
TransportAddress.h
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 
26 #ifndef __TRANSPORTADDRESS_H_
27 #define __TRANSPORTADDRESS_H_
28 
29 //#include <stdint.h>
30 #include <oversim_mapset.h>
31 
32 #include <omnetpp.h>
33 #include <IPvXAddress.h>
34 
36 typedef std::vector<TransportAddress> TransportAddressVector;
37 
50 {
51 public:
52 
56  class hashFcn
57  {
58  public:
59  size_t operator()( const TransportAddress& h1 ) const
60  {
61  return h1.hash();
62  }
63  };
64 
65  // TODO: maybe we should use const uint8_t instead to save memory
66  enum NatType {
68  NO_NAT = 1,
72  };
73 
74 public://collection typedefs
75 
76  typedef UNORDERED_SET<TransportAddress, hashFcn> Set;
78 protected://fields
79 
80  IPvXAddress ip;
81  int port;
83 private:
85  // TODO: as soon as sourceRoute is used, we need to calculate the correct TransportAddress length for statistics */
88 public://construction
89 
94 
98  virtual ~TransportAddress() {};
99 
105  TransportAddress(const TransportAddress& handle);
106 
114  TransportAddress(const IPvXAddress& ip, int port = -1,
116 
117 public://static fields
118 
121 
122 public://methods: delegates to OverlayKey and IPvXAddress
123 
130  bool operator==(const TransportAddress& rhs) const;
131 
138  bool operator!=(const TransportAddress& rhs) const;
139 
146  bool operator< (const TransportAddress& rhs) const;
147 
154  bool operator> (const TransportAddress& rhs) const;
155 
162  bool operator<=(const TransportAddress& rhs) const;
163 
170  bool operator>=(const TransportAddress& rhs) const;
171 
172 public://methods: operators
173 
181 
182 public://methods: setters and getters
183 
191  void setIp(const IPvXAddress& ip, int port = -1,
193 
201  void setAddress(const IPvXAddress& ip, int port = -1,
202  NatType natType = UNKNOWN_NAT) __attribute ((deprecated))
203  { setIp(ip, port, natType); };
204 
211 
216  void clearSourceRoute() { sourceRoute.clear(); };
217 
223  void setPort( int port );
224 
230  const IPvXAddress& getIp() const;
231 
237  const IPvXAddress& getAddress() const __attribute ((deprecated))
238  { return getIp(); };
239 
245  int getPort() const;
246 
252  NatType getNatType() const;
253 
259  size_t getSourceRouteSize() const;
260 
266  const TransportAddressVector& getSourceRoute() const;
267 
273  bool isUnspecified() const;
274 
275 public://methods: hashing
276 
282  size_t hash() const;
283 
284 public://methods: c++ streaming
285 
294  friend std::ostream& operator<<(std::ostream& os, const TransportAddress& n);
295 
296 private://methods:
297 
303  void assertUnspecified( const TransportAddress& handle ) const;
304 
305 public:
306 
312  virtual TransportAddress* dup() const;
313 };
314 
321 inline void doPacking(cCommBuffer *buf, TransportAddress& addr)
322 {
323  doPacking(buf, addr.getIp());
324  doPacking(buf, addr.getPort());
325  doPacking(buf, static_cast<uint8_t>(addr.getNatType()));
326  doPacking(buf, static_cast<uint8_t>(addr.getSourceRouteSize()));
327  for (size_t i = 0; i < addr.getSourceRouteSize(); i++) {
328  // TODO: ugly const_cast should be avoided
329  doPacking(buf, const_cast<TransportAddress&>(addr.getSourceRoute()[i]));
330  }
331 }
332 
339 inline void doUnpacking(cCommBuffer *buf, TransportAddress& addr)
340 {
341  IPvXAddress ip;
342  int port;
343  uint8_t natType = 0;
344  doUnpacking(buf, ip);
345  doUnpacking(buf, port);
346  doUnpacking(buf, natType);
347  addr.setIp(ip, port, static_cast<TransportAddress::NatType>(natType));
348 
349  uint8_t sourceRouteSize;
350  TransportAddress sourceRouteEntry;
351  doUnpacking(buf, sourceRouteSize);
352  for (size_t i = 0; i < sourceRouteSize; i++) {
353  doUnpacking(buf, sourceRouteEntry);
354  addr.appendSourceRoute(sourceRouteEntry);
355  }
356 }
357 
358 
359 #endif