Imported Upstream version 0.3.5
[anytun.git] / src / networkAddress.cpp
1 /*
2  *  anytun
3  *
4  *  The secure anycast tunneling protocol (satp) defines a protocol used
5  *  for communication between any combination of unicast and anycast
6  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
7  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
8  *  ethernet, ip, arp ...). satp directly includes cryptography and
9  *  message authentication based on the methods used by SRTP.  It is
10  *  intended to deliver a generic, scaleable and secure solution for
11  *  tunneling and relaying of packets of any protocol.
12  *
13  *
14  *  Copyright (C) 2007-2014 Markus Grüneis, Othmar Gsenger, Erwin Nindl,
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
19  *  Anytun is free software: you can redistribute it and/or modify
20  *  it under the terms of the GNU General Public License as published by
21  *  the Free Software Foundation, either version 3 of the License, or
22  *  any later version.
23  *
24  *  Anytun is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with Anytun.  If not, see <http://www.gnu.org/licenses/>.
31  *
32  *  In addition, as a special exception, the copyright holders give
33  *  permission to link the code of portions of this program with the
34  *  OpenSSL library under certain conditions as described in each
35  *  individual source file, and distribute linked combinations
36  *  including the two.
37  *  You must obey the GNU General Public License in all respects
38  *  for all of the code used other than OpenSSL.  If you modify
39  *  file(s) with this exception, you may extend this exception to your
40  *  version of the file(s), but you are not obligated to do so.  If you
41  *  do not wish to do so, delete this exception statement from your
42  *  version.  If you delete this exception statement from all source
43  *  files in the program, then also delete it here.
44  */
45
46 #include "threadUtils.hpp"
47 #include "datatypes.h"
48 #include <exception>
49
50 #include "networkAddress.h"
51 #include "anytunError.h"
52
53 NetworkAddress::NetworkAddress():ipv4_address_(),ipv6_address_()
54 {
55   network_address_type_=ipv4;
56 }
57
58 NetworkAddress::NetworkAddress(const NetworkAddress& ref) : mutex_(),ipv4_address_(ref.ipv4_address_),ipv6_address_(ref.ipv6_address_),ethernet_address_(ref.ethernet_address_),network_address_type_(ref.network_address_type_)
59 {
60 }
61
62 NetworkAddress::NetworkAddress(const std::string& address)
63 {
64   boost::asio::ip::address addr = boost::asio::ip::address::from_string(address);
65   if(addr.is_v4()) {
66     network_address_type_=ipv4;
67     ipv4_address_ = addr.to_v4();
68   } else {
69     network_address_type_=ipv6;
70     ipv6_address_ = addr.to_v6();
71   }
72 }
73
74 NetworkAddress::NetworkAddress(boost::asio::ip::address_v6 ipv6_address)
75 {
76   network_address_type_=ipv6;
77   ipv6_address_ = ipv6_address;
78 }
79
80 NetworkAddress::NetworkAddress(boost::asio::ip::address_v4 ipv4_address)
81 {
82   network_address_type_=ipv4;
83   ipv4_address_ = ipv4_address;
84 }
85
86 NetworkAddress::NetworkAddress(uint64_t ethernet_address)
87 {
88   network_address_type_=ethernet;
89   ethernet_address_=ethernet_address;
90 }
91
92
93 NetworkAddress::~NetworkAddress()
94 {
95 }
96
97 NetworkAddress::NetworkAddress(const network_address_type_t type, const std::string& address)
98 {
99   setNetworkAddress(type, address);
100 }
101
102 void NetworkAddress::setNetworkAddress(const network_address_type_t type, const std::string& address)
103 {
104   if(type==ipv4) {
105     ipv4_address_=boost::asio::ip::address_v4::from_string(address);
106   } else if(type==ipv6) {
107     ipv6_address_=boost::asio::ip::address_v6::from_string(address);
108   } else if(type==ethernet) {
109     //TODO
110   } else {
111     //TODO
112   }
113   network_address_type_ = type;
114 }
115
116 void NetworkAddress::setNetworkAddress(boost::asio::ip::address_v4 addr)
117 {
118   network_address_type_=ipv4;
119   ipv4_address_ = addr;
120 }
121
122 void NetworkAddress::setNetworkAddress(boost::asio::ip::address_v6 addr)
123 {
124   network_address_type_=ipv6;
125   ipv6_address_ = addr;
126 }
127
128 void NetworkAddress::setNetworkAddress(uint64_t addr)
129 {
130   network_address_type_=ethernet;
131   ethernet_address_=addr;
132 }
133
134 network_address_type_t NetworkAddress::getNetworkAddressType() const
135 {
136   return network_address_type_;
137 }
138
139 const boost::asio::ip::address_v4& NetworkAddress::getNetworkAddressV4() const
140 {
141   if(network_address_type_ != ipv4) {
142     AnytunError::throwErr() << "wrong address type";
143   }
144
145   return ipv4_address_;
146 }
147
148 const boost::asio::ip::address_v6& NetworkAddress::getNetworkAddressV6() const
149 {
150   if(network_address_type_ != ipv6) {
151     AnytunError::throwErr() << "wrong address type";
152   }
153
154   return ipv6_address_;
155 }
156
157 const uint64_t NetworkAddress::getNetworkAdrressEther() const
158 {
159   if(network_address_type_ != ethernet) {
160     AnytunError::throwErr() << "wrong address type";
161   }
162
163   return ethernet_address_;
164 }
165
166 std::string NetworkAddress::toString() const
167 {
168   if(network_address_type_==ipv4) {
169     return ipv4_address_.to_string();
170   } else if(network_address_type_==ipv6) {
171     return ipv6_address_.to_string();
172   } else if(network_address_type_==ethernet) {
173     // TODO
174   }
175   return std::string("");
176 }
177
178 ipv4_bytes_type NetworkAddress::to_bytes_v4() const
179 {
180   return ipv4_address_.to_bytes();
181 }
182
183 ipv6_bytes_type NetworkAddress::to_bytes_v6() const
184 {
185   return ipv6_address_.to_bytes();
186 }
187
188 ethernet_bytes_type     NetworkAddress::to_bytes_ethernet() const
189 {
190   boost::array<unsigned char,6> result;
191   uint64_t ether=ethernet_address_;
192   for(int i = 0; i < 6; i++) {
193     result[i] = (unsigned char)(ether & 0xff);
194     ether >>= 8;
195   }
196   return result;
197 }
198
199 bool NetworkAddress::operator<(const NetworkAddress& right) const
200 {
201   if(network_address_type_!=right.network_address_type_) {
202     AnytunError::throwErr() << "NetworkAddress::operator<() address types don't match";
203   }
204   if(network_address_type_==ipv4) {
205     return (ipv4_address_ < right.ipv4_address_);
206   } else if(network_address_type_==ipv6) {
207     return (ipv6_address_ < right.ipv6_address_);
208   } else if(network_address_type_==ethernet) {
209     return (ethernet_address_ < right.ethernet_address_);
210   } else {
211     //TODO
212   }
213   return false;
214 }
215