Imported Upstream version 0.3
[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 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.
12  *
13  *
14  *  Copyright (C) 2007-2008 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 version 3 as
21  *  published by the Free Software Foundation.
22  *
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.
27  *
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/>.
30  */
31
32 #include "threadUtils.hpp"
33 #include "datatypes.h"
34 #include <exception>
35
36 #include "networkAddress.h"
37 #include "anytunError.h"
38
39 NetworkAddress::NetworkAddress():ipv4_address_(),ipv6_address_()
40 {
41         network_address_type_=ipv4;
42 }
43
44 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_)
45 {
46 }
47
48 NetworkAddress::NetworkAddress(const std::string & address)
49 {
50         boost::asio::ip::address addr = boost::asio::ip::address::from_string(address);
51         if (addr.is_v4())
52         {
53                 network_address_type_=ipv4;
54                 ipv4_address_ = addr.to_v4();
55         } else {
56                 network_address_type_=ipv6;
57                 ipv6_address_ = addr.to_v6();
58         }
59 }
60
61 NetworkAddress::NetworkAddress(boost::asio::ip::address_v6 ipv6_address)
62 {
63         network_address_type_=ipv6;
64         ipv6_address_ = ipv6_address;
65 }
66
67 NetworkAddress::NetworkAddress(boost::asio::ip::address_v4 ipv4_address)
68 {
69         network_address_type_=ipv4;
70         ipv4_address_ = ipv4_address;
71 }
72
73 NetworkAddress::NetworkAddress(u_int64_t ethernet_address)
74 {
75         network_address_type_=ethernet;
76         ethernet_address_=ethernet_address;
77 }
78
79
80 NetworkAddress::~NetworkAddress()
81 {
82 }
83
84 NetworkAddress::NetworkAddress(const network_address_type_t type, const std::string & address )
85 {
86         setNetworkAddress( type, address);
87 }
88
89 void NetworkAddress::setNetworkAddress(const network_address_type_t type, const std::string & address )
90 {
91         if (type==ipv4)
92         {
93                 ipv4_address_=boost::asio::ip::address_v4::from_string(address);
94         } else if (type==ipv6) {
95                 ipv6_address_=boost::asio::ip::address_v6::from_string(address);
96         } else if (type==ethernet) {
97                 //TODO
98         } else {
99                 //TODO
100         }
101         network_address_type_ = type;
102 }
103
104 void NetworkAddress::setNetworkAddress(boost::asio::ip::address_v4 addr)
105 {
106         network_address_type_=ipv4;
107         ipv4_address_ = addr;
108 }
109
110 void NetworkAddress::setNetworkAddress(boost::asio::ip::address_v6 addr)
111 {
112         network_address_type_=ipv6;
113         ipv6_address_ = addr;
114 }
115
116 void NetworkAddress::setNetworkAddress(u_int64_t addr)
117 {
118         network_address_type_=ethernet;
119         ethernet_address_=addr;
120 }
121
122 network_address_type_t NetworkAddress::getNetworkAddressType() const
123 {
124         return network_address_type_;
125 }
126
127 const boost::asio::ip::address_v4& NetworkAddress::getNetworkAddressV4() const
128 {
129   if(network_address_type_ != ipv4)
130     AnytunError::throwErr() << "wrong address type";
131   
132   return ipv4_address_;
133 }
134
135 const boost::asio::ip::address_v6& NetworkAddress::getNetworkAddressV6() const
136 {
137   if(network_address_type_ != ipv6)
138     AnytunError::throwErr() << "wrong address type";
139   
140   return ipv6_address_;
141 }
142
143 const u_int64_t NetworkAddress::getNetworkAdrressEther() const
144 {
145   if(network_address_type_ != ethernet)
146     AnytunError::throwErr() << "wrong address type";
147   
148   return ethernet_address_;
149 }
150
151 std::string NetworkAddress::toString() const
152 {
153         if (network_address_type_==ipv4){
154                 return ipv4_address_.to_string();
155         } 
156   else if (network_address_type_==ipv6) {
157                 return ipv6_address_.to_string();
158         } 
159   else if (network_address_type_==ethernet) {
160         // TODO
161         } 
162   return std::string("");
163 }
164
165 ipv4_bytes_type NetworkAddress::to_bytes_v4() const
166 {
167         return ipv4_address_.to_bytes();
168 }
169
170 ipv6_bytes_type NetworkAddress::to_bytes_v6() const
171 {
172         return ipv6_address_.to_bytes();
173 }
174
175 ethernet_bytes_type     NetworkAddress::to_bytes_ethernet() const
176 {
177         boost::array<unsigned char,6> result;
178         u_int64_t ether=ethernet_address_;
179         for (int i = 0; i < 6; i++)
180         {
181                 result[i] = (unsigned char) (ether && 0xff);
182                         ether >>= 8;
183         }
184         return result;
185 }
186
187 bool NetworkAddress::operator<(const NetworkAddress &right) const
188 {
189         if (network_address_type_!=right.network_address_type_)
190                 AnytunError::throwErr() << "NetworkAddress::operator<() address types don't match";
191         if (network_address_type_==ipv4)
192         {
193                 return (ipv4_address_ < right.ipv4_address_);
194         } else if (network_address_type_==ipv6) {
195                 return (ipv6_address_ < right.ipv6_address_);
196         } else if (network_address_type_==ethernet) {
197                  return (ethernet_address_ < right.ethernet_address_);
198         } else {
199                 //TODO
200         }
201         return false;
202 }
203