An Overlay-based
Virtual Network Substrate
SpoVNet

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

Last change on this file since 6919 was 6919, checked in by mies, 14 years ago

Fixed tons of warnings when using CXXFLAGS="-Wall"!

File size: 5.0 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        ip_address(const address& addr) : addr(addr) {
35
36        }
37
38        ip_address(const address_v4& addr) : addr(addr) {
39
40        }
41
42        ip_address(const address_v6& addr) : addr(addr) {
43
44        }
45
46        ip_address(const std::string& text) {
47                assign(text);
48        }
49
50        ip_address(const char* text) {
51                assign(std::string(text));
52        }
53
54        ip_address(const uint8_t* bytes, size_t size) {
55                assign(bytes, size);
56        }
57
58        //--- compare operations --------------------------------------------------
59
60        /// implements comparison operators
61        int compare_to(const ip_address& rhs) const {
62                if (addr == rhs.addr) return 0;
63                if (addr < rhs.addr) return -1; else return 1;
64        }
65
66        //--- byte representation -------------------------------------------------
67
68        /// returns true, if this address has a fixed size in bytes
69        bool is_bytes_size_static() const {
70                return false;
71        }
72
73        /// returns the number of bytes used for serialization of this address
74        size_t to_bytes_size() const {
75                return addr.is_v4() ? 4 : 16;
76        }
77
78        /// converts this address to a binary representation
79        void to_bytes(uint8_t* bytes) const {
80                if (addr.is_v4()) {
81                        address_v4::bytes_type bytes_ = addr.to_v4().to_bytes();
82                        for (size_t i=0; i<bytes_.size(); i++) bytes[i] = bytes_[i];
83                } else {
84                        address_v6::bytes_type bytes_ = addr.to_v6().to_bytes();
85                        for (size_t i=0; i<bytes_.size(); i++) bytes[i] = bytes_[i];
86                }
87        }
88
89        /// Assigns an address using a bunch of bytes
90        bool assign(const uint8_t* bytes, size_t size) {
91                if (size == 4) {
92                        address_v4::bytes_type bytes_;
93                        for (size_t i=0; i<bytes_.size(); i++) bytes_[i] = bytes[i];
94                        addr = address_v4(bytes_);
95                        return false;
96                } else
97                if (size == 16) {
98                        address_v6::bytes_type bytes_;
99                        for (size_t i=0; i<bytes_.size(); i++) bytes_[i] = bytes[i];
100                        addr = address_v6(bytes_);
101                        return false;
102                }
103                return true;
104        }
105
106        //--- string representation -----------------------------------------------
107
108        /// convert address to a string that can be used to reconstruct the address
109        std::string to_string() const {
110                if (addr.is_v6() && (addr.to_v6().is_v4_compatible()
111                        || addr.to_v6().is_v4_mapped()))
112                        return addr.to_v6().to_v4().to_string();
113                return addr.to_string();
114        }
115
116        /// Assigns an address using a human-readable
117        bool assign(const std::string& text) {
118                addr = address::from_string(text);
119                return false;
120        }
121
122        //--- assignment ----------------------------------------------------------
123
124        /// Assigns an address
125        bool assign(const ip_address& rhs) {
126                addr = rhs.addr;
127                return false;
128        }
129
130        //--- address info --------------------------------------------------------
131
132        static const std::string& type_name() {
133                return type_name_ip;
134        }
135
136        static const uint16_t type_id() {
137                return 0x81DD;
138        }
139
140        //--- address delegates ---------------------------------------------------
141
142        bool is_multicast() const {
143                if (addr.is_v4()) return addr.to_v4().is_multicast();
144                return addr.to_v6().is_multicast();
145        }
146
147        bool is_loopback() const {
148                if (addr.is_v4()) return addr.to_v4() == address_v4::loopback();
149                return addr.to_v6().is_loopback();
150        }
151
152        bool is_link_local() const {
153                if (addr.is_v4()) return false;
154                return addr.to_v6().is_link_local();
155        }
156
157        bool is_multicast_link_local() const {
158                if (addr.is_v4()) return false;
159                return addr.to_v6().is_multicast_link_local();
160
161        }
162
163        bool is_multicast_node_local() const {
164                if (addr.is_v4()) return false;
165                return addr.to_v6().is_multicast_node_local();
166        }
167
168        bool is_multicast_site_local() const {
169                if (addr.is_v4()) return false;
170                return addr.to_v6().is_multicast_site_local();
171        }
172
173        bool is_any() const {
174                if (addr.is_v4()) return addr.to_v4() == address_v4::any();
175                return addr.to_v6() == address_v6::any();
176        }
177
178        bool is_v4_compatible() const {
179                if (addr.is_v4()) return true;
180                return addr.to_v6().is_v4_compatible();
181        }
182
183        bool is_v4_mapped() {
184                if (addr.is_v4()) return true;
185                return addr.to_v6().is_v4_mapped();
186        }
187
188        bool is_v4() const {
189                return addr.is_v4();
190        }
191
192        bool is_v6() const {
193                return addr.is_v6();
194        }
195
196        //--- address conversions -------------------------------------------------
197
198        const address& asio() const {
199                return addr;
200        }
201
202        void asio( const address& addr ) {
203                this->addr = addr;
204        }
205};
206
207}} // namespace ariba::addressing
208
209namespace boost {
210
211// boost hash function
212template<>
213struct hash<ariba::addressing::ip_address>: public std::unary_function<ariba::addressing::ip_address, std::size_t> {
214        std::size_t operator()(const ariba::addressing::ip_address& ep) const {
215                return hash_value(ep.to_string());
216        }
217};
218
219}
220
221
222#endif /* IP_ADDRESS_HPP_ */
Note: See TracBrowser for help on using the repository browser.