Imported Upstream version 0.3.5
[anytun.git] / src / networkAddress.h
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 #ifndef ANYTUN_networkAddress_h_INCLUDED
47 #define ANYTUN_networkAddress_h_INCLUDED
48
49 // TODO not required here
50 #include <boost/archive/text_oarchive.hpp>
51 #include <boost/archive/text_iarchive.hpp>
52
53 #include "threadUtils.hpp"
54 #include "datatypes.h"
55
56 #include <string>
57 #include <boost/asio.hpp>
58 #include <boost/array.hpp>
59
60 typedef boost::array<unsigned char,6> ethernet_bytes_type;
61 typedef boost::asio::ip::address_v4::bytes_type ipv4_bytes_type;
62 typedef boost::asio::ip::address_v6::bytes_type ipv6_bytes_type;
63
64 enum network_address_type_t {
65   ipv4=0,
66   ipv6=1,
67   ethernet=2
68 };
69
70 class NetworkAddress
71 {
72 public:
73   NetworkAddress();
74   NetworkAddress(const NetworkAddress&);
75   NetworkAddress(const std::string&);
76   NetworkAddress(boost::asio::ip::address_v6);
77   NetworkAddress(boost::asio::ip::address_v4);
78   NetworkAddress(uint64_t);
79   NetworkAddress(const network_address_type_t type, const std::string& address);
80   ~NetworkAddress();
81   void setNetworkAddress(const network_address_type_t type, const std::string& address);
82   void setNetworkAddress(boost::asio::ip::address_v4);
83   void setNetworkAddress(boost::asio::ip::address_v6);
84   void setNetworkAddress(uint64_t);
85   network_address_type_t getNetworkAddressType() const;
86   std::string toString() const;
87   bool operator<(const NetworkAddress& s) const;
88   ipv4_bytes_type to_bytes_v4() const;
89   ipv6_bytes_type to_bytes_v6() const;
90   ethernet_bytes_type to_bytes_ethernet() const;
91   const boost::asio::ip::address_v4& getNetworkAddressV4() const;
92   const boost::asio::ip::address_v6& getNetworkAddressV6() const;
93   const uint64_t getNetworkAdrressEther() const;
94 protected:
95   Mutex mutex_;
96   boost::asio::ip::address_v4 ipv4_address_;
97   boost::asio::ip::address_v6 ipv6_address_;
98   uint64_t ethernet_address_;
99   network_address_type_t network_address_type_;
100 private:
101   NetworkAddress operator=(const NetworkAddress& s);
102   friend class boost::serialization::access;
103   template<class Archive>
104   void serialize(Archive& ar, const unsigned int version) {
105     ar& network_address_type_;
106     if(network_address_type_==ipv4) {
107       std::string ip(ipv4_address_.to_string());
108       ar& ip;
109       ipv4_address_=boost::asio::ip::address_v4::from_string(ip);
110     }
111     if(network_address_type_==ipv6) {
112       std::string ip(ipv6_address_.to_string());
113       ar& ip;
114       ipv6_address_=boost::asio::ip::address_v6::from_string(ip);
115     }
116     if(network_address_type_==ethernet) {
117       ar& ethernet_address_;
118     }
119   }
120 };
121
122 //                      for(int i=0;i<4;i++)
123 //#if defined(__GNUC__) && defined(__linux__)
124 //                              ar & ipv6_address_.s6_addr32;
125 //#elif defined(__GNUC__) && defined(__OpenBSD__)
126 //        ar & ipv6_address_.__u6_addr.__u6_addr32;
127 //#else
128 // #error Target not supported
129 //#endif
130 #endif