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