source: source/ariba/utility/addressing/ip_address.hpp@ 5284

Last change on this file since 5284 was 5284, checked in by mies, 15 years ago

+ added new transport modules and adapted ariba to them
+ exchange endpoint descriptors an link establishment
+ clean up of base communication
+ link establishment with in the presence of multiple endpoints
+ local discovery for ipv6, ipv4 and bluetooth mac addresses

File size: 4.2 KB
Line 
1#ifndef IP_ADDRESS_HPP_
2#define IP_ADDRESS_HPP_
3
4#include <string>
5#include <boost/tr1/functional.hpp>
6#include <boost/asio/ip/address.hpp>
7
8#include "detail/address_convenience.hpp"
9
10namespace ariba {
11namespace addressing {
12
13using boost::asio::ip::address;
14using boost::asio::ip::address_v4;
15using boost::asio::ip::address_v6;
16
17/**
18 * TODO: Doc
19 *
20 * @author Sebastian Mies <mies@tm.uka.de>
21 */
22class ip_address: public detail::address_convenience<ip_address> {
23private:
24 address addr;
25 static const std::string type_name_ip;
26
27public:
28 ip_address() : addr() {
29 }
30
31 ip_address(const ip_address& copy) : addr(copy.addr) {
32
33 }
34
35 ip_address(const address& addr) : addr(addr) {
36
37 }
38
39 ip_address(const address_v4& addr) : addr(addr) {
40
41 }
42
43 ip_address(const address_v6& addr) : addr(addr) {
44
45 }
46
47 ip_address(const std::string& text) {
48 assign(text);
49 }
50
51 ip_address(const char* text) {
52 assign(std::string(text));
53 }
54
55 ip_address(const uint8_t* bytes, size_t size) {
56 assign(bytes, size);
57 }
58
59 //--- compare operations --------------------------------------------------
60
61 /// implements comparison operators
62 int compare_to(const ip_address& rhs) const {
63 if (addr == rhs.addr) return 0;
64 if (addr < rhs.addr) return -1; else return 1;
65 }
66
67 //--- byte representation -------------------------------------------------
68
69 /// returns true, if this address has a fixed size in bytes
70 bool is_bytes_size_static() const {
71 return false;
72 }
73
74 /// returns the number of bytes used for serialization of this address
75 size_t to_bytes_size() const {
76 return addr.is_v4() ? 4 : 16;
77 }
78
79 /// converts this address to a binary representation
80 void to_bytes(uint8_t* bytes) const {
81 if (addr.is_v4()) {
82 address_v4::bytes_type bytes_ = addr.to_v4().to_bytes();
83 for (size_t i=0; i<bytes_.size(); i++) bytes[i] = bytes_[i];
84 } else {
85 address_v6::bytes_type bytes_ = addr.to_v6().to_bytes();
86 for (size_t i=0; i<bytes_.size(); i++) bytes[i] = bytes_[i];
87 }
88 }
89
90 /// Assigns an address using a bunch of bytes
91 bool assign(const uint8_t* bytes, size_t size) {
92 if (size == 4) {
93 address_v4::bytes_type bytes_;
94 for (size_t i=0; i<bytes_.size(); i++) bytes_[i] = bytes[i];
95 addr = address_v4(bytes_);
96 return false;
97 } else
98 if (size == 16) {
99 address_v6::bytes_type bytes_;
100 for (size_t i=0; i<bytes_.size(); i++) bytes_[i] = bytes[i];
101 addr = address_v6(bytes_);
102 return false;
103 }
104 return true;
105 }
106
107 //--- string representation -----------------------------------------------
108
109 /// convert address to a string that can be used to reconstruct the address
110 std::string to_string() const {
111 return addr.to_string();
112 }
113
114 /// Assigns an address using a human-readable
115 bool assign(const std::string& text) {
116 addr = address::from_string(text);
117 return false;
118 }
119
120 //--- assignment ----------------------------------------------------------
121
122 /// Assigns an address
123 bool assign(const ip_address& rhs) {
124 addr = rhs.addr;
125 return false;
126 }
127
128 //--- address info --------------------------------------------------------
129
130 const std::string& type_name() const {
131 return type_name_ip;
132 }
133
134 const uint16_t type_id() const {
135 return 0x81DD;
136 }
137
138 //--- address delegates ---------------------------------------------------
139
140 bool is_multicast() const {
141 if (addr.is_v4()) return addr.to_v4().is_multicast();
142 return addr.to_v6().is_multicast();
143 }
144
145 bool is_loopback() const {
146 if (addr.is_v4()) return addr.to_v4() == address_v4::loopback();
147 return addr.to_v6().is_loopback();
148 }
149
150 bool is_any() const {
151 if (addr.is_v4()) return addr.to_v4() == address_v4::any();
152 return addr.to_v6() == address_v6::any();
153 }
154
155 bool is_v4() const {
156 return addr.is_v4();
157 }
158
159 bool is_v6() const {
160 return addr.is_v6();
161 }
162
163 //--- address conversions -------------------------------------------------
164
165 const address& asio() const {
166 return addr;
167 }
168
169 void asio( const address& addr ) {
170 this->addr = addr;
171 }
172};
173
174}} // namespace ariba::addressing
175
176namespace boost {
177
178template<>
179struct hash<ariba::addressing::ip_address>: public std::unary_function<ariba::addressing::ip_address, std::size_t> {
180 std::size_t operator()(const ariba::addressing::ip_address& ep) const {
181 return hash_value(ep.to_string());
182 }
183};
184
185}
186
187
188#endif /* IP_ADDRESS_HPP_ */
Note: See TracBrowser for help on using the repository browser.