added a workaround for strerror_r problem
[anytun.git] / src / networkAddress.cpp
blobf922c000850f44f69f499b7ea9d52d3cf33ad4fa
1 /*
2 * anytun
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-2008 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 version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
32 #include "threadUtils.hpp"
33 #include "datatypes.h"
34 #include <exception>
36 #include "networkAddress.h"
38 NetworkAddress::NetworkAddress():ipv4_address_(),ipv6_address_()
40 network_address_type_=ipv4;
43 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_)
47 NetworkAddress::NetworkAddress(const std::string & address)
49 boost::asio::ip::address addr = boost::asio::ip::address::from_string(address);
50 if (addr.is_v4())
52 network_address_type_=ipv4;
53 ipv4_address_ = addr.to_v4();
54 } else {
55 network_address_type_=ipv6;
56 ipv6_address_ = addr.to_v6();
60 NetworkAddress::NetworkAddress(boost::asio::ip::address_v6 ipv6_address)
62 network_address_type_=ipv6;
63 ipv6_address_ = ipv6_address;
66 NetworkAddress::NetworkAddress(boost::asio::ip::address_v4 ipv4_address)
68 network_address_type_=ipv4;
69 ipv4_address_ = ipv4_address;
72 NetworkAddress::NetworkAddress(u_int64_t ethernet_address)
74 network_address_type_=ethernet;
75 ethernet_address_=ethernet_address;
79 NetworkAddress::~NetworkAddress()
83 NetworkAddress::NetworkAddress(const network_address_type_t type, const std::string & address )
85 setNetworkAddress( type, address);
88 void NetworkAddress::setNetworkAddress(const network_address_type_t type, const std::string & address )
90 if (type==ipv4)
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) {
96 //TODO
97 } else {
98 //TODO
100 network_address_type_ = type;
103 network_address_type_t NetworkAddress::getNetworkAddressType() const
105 return network_address_type_;
108 std::string NetworkAddress::toString() const
110 if (network_address_type_==ipv4){
111 return ipv4_address_.to_string();
113 else if (network_address_type_==ipv6) {
114 return ipv6_address_.to_string();
116 else if (network_address_type_==ethernet) {
117 // TODO
119 return std::string("");
122 ipv4_bytes_type NetworkAddress::to_bytes_v4() const
124 return ipv4_address_.to_bytes();
127 ipv6_bytes_type NetworkAddress::to_bytes_v6() const
129 return ipv6_address_.to_bytes();
132 ethernet_bytes_type NetworkAddress::to_bytes_ethernet() const
134 boost::array<unsigned char,6> result;
135 u_int64_t ether=ethernet_address_;
136 for (int i = 0; i < 6; i++)
138 result[i] = (unsigned char) (ether && 0xff);
139 ether >>= 8;
141 return result;
144 bool NetworkAddress::operator<(const NetworkAddress &right) const
146 if (network_address_type_!=right.network_address_type_)
147 throw std::runtime_error("NetworkAddress::operator<() address types don't match");
148 if (network_address_type_==ipv4)
150 return (ipv4_address_ < right.ipv4_address_);
151 } else if (network_address_type_==ipv6) {
152 return (ipv6_address_ < right.ipv6_address_);
153 } else if (network_address_type_==ethernet) {
154 return (ethernet_address_ < right.ethernet_address_);
155 } else {
156 //TODO
158 return false;