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 methodes 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.
14 * Copyright (C) 2007-2009 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
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
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.
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/>.
33 #include "threadUtils.hpp"
34 #include "datatypes.h"
37 #include "networkAddress.h"
38 #include "anytunError.h"
40 NetworkAddress::NetworkAddress():ipv4_address_(),ipv6_address_()
42 network_address_type_=ipv4;
45 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_)
49 NetworkAddress::NetworkAddress(const std::string& address)
51 boost::asio::ip::address addr = boost::asio::ip::address::from_string(address);
53 network_address_type_=ipv4;
54 ipv4_address_ = addr.to_v4();
56 network_address_type_=ipv6;
57 ipv6_address_ = addr.to_v6();
61 NetworkAddress::NetworkAddress(boost::asio::ip::address_v6 ipv6_address)
63 network_address_type_=ipv6;
64 ipv6_address_ = ipv6_address;
67 NetworkAddress::NetworkAddress(boost::asio::ip::address_v4 ipv4_address)
69 network_address_type_=ipv4;
70 ipv4_address_ = ipv4_address;
73 NetworkAddress::NetworkAddress(uint64_t ethernet_address)
75 network_address_type_=ethernet;
76 ethernet_address_=ethernet_address;
80 NetworkAddress::~NetworkAddress()
84 NetworkAddress::NetworkAddress(const network_address_type_t type, const std::string& address)
86 setNetworkAddress(type, address);
89 void NetworkAddress::setNetworkAddress(const network_address_type_t type, const std::string& address)
92 ipv4_address_=boost::asio::ip::address_v4::from_string(address);
93 } else if(type==ipv6) {
94 ipv6_address_=boost::asio::ip::address_v6::from_string(address);
95 } else if(type==ethernet) {
100 network_address_type_ = type;
103 void NetworkAddress::setNetworkAddress(boost::asio::ip::address_v4 addr)
105 network_address_type_=ipv4;
106 ipv4_address_ = addr;
109 void NetworkAddress::setNetworkAddress(boost::asio::ip::address_v6 addr)
111 network_address_type_=ipv6;
112 ipv6_address_ = addr;
115 void NetworkAddress::setNetworkAddress(uint64_t addr)
117 network_address_type_=ethernet;
118 ethernet_address_=addr;
121 network_address_type_t NetworkAddress::getNetworkAddressType() const
123 return network_address_type_;
126 const boost::asio::ip::address_v4& NetworkAddress::getNetworkAddressV4() const
128 if(network_address_type_ != ipv4) {
129 AnytunError::throwErr() << "wrong address type";
132 return ipv4_address_;
135 const boost::asio::ip::address_v6& NetworkAddress::getNetworkAddressV6() const
137 if(network_address_type_ != ipv6) {
138 AnytunError::throwErr() << "wrong address type";
141 return ipv6_address_;
144 const uint64_t NetworkAddress::getNetworkAdrressEther() const
146 if(network_address_type_ != ethernet) {
147 AnytunError::throwErr() << "wrong address type";
150 return ethernet_address_;
153 std::string NetworkAddress::toString() const
155 if(network_address_type_==ipv4) {
156 return ipv4_address_.to_string();
157 } else if(network_address_type_==ipv6) {
158 return ipv6_address_.to_string();
159 } else if(network_address_type_==ethernet) {
162 return std::string("");
165 ipv4_bytes_type NetworkAddress::to_bytes_v4() const
167 return ipv4_address_.to_bytes();
170 ipv6_bytes_type NetworkAddress::to_bytes_v6() const
172 return ipv6_address_.to_bytes();
175 ethernet_bytes_type NetworkAddress::to_bytes_ethernet() const
177 boost::array<unsigned char,6> result;
178 uint64_t ether=ethernet_address_;
179 for(int i = 0; i < 6; i++) {
180 result[i] = (unsigned char)(ether && 0xff);
186 bool NetworkAddress::operator<(const NetworkAddress& right) const
188 if(network_address_type_!=right.network_address_type_) {
189 AnytunError::throwErr() << "NetworkAddress::operator<() address types don't match";
191 if(network_address_type_==ipv4) {
192 return (ipv4_address_ < right.ipv4_address_);
193 } else if(network_address_type_==ipv6) {
194 return (ipv6_address_ < right.ipv6_address_);
195 } else if(network_address_type_==ethernet) {
196 return (ethernet_address_ < right.ethernet_address_);