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