Imported Upstream version 0.3.2
[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 {
53         ipv4=0,
54         ipv6=1,
55         ethernet=2
56 };
57
58 class NetworkAddress
59 {
60 public:
61         NetworkAddress();
62         NetworkAddress(const NetworkAddress &);
63         NetworkAddress(const std::string &);
64         NetworkAddress(boost::asio::ip::address_v6);
65         NetworkAddress(boost::asio::ip::address_v4);
66         NetworkAddress(u_int64_t);
67         NetworkAddress(const network_address_type_t type, const std::string & address );
68         ~NetworkAddress();
69         void setNetworkAddress(const network_address_type_t type, const std::string & address );
70         void setNetworkAddress(boost::asio::ip::address_v4);
71         void setNetworkAddress(boost::asio::ip::address_v6);
72         void setNetworkAddress(u_int64_t);
73         network_address_type_t getNetworkAddressType() const;
74   std::string toString() const;
75         bool operator<(const NetworkAddress &s) const;
76         ipv4_bytes_type to_bytes_v4() const;    
77         ipv6_bytes_type to_bytes_v6() const;    
78         ethernet_bytes_type to_bytes_ethernet() const;  
79   const boost::asio::ip::address_v4& getNetworkAddressV4() const;
80   const boost::asio::ip::address_v6& getNetworkAddressV6() const;
81   const u_int64_t getNetworkAdrressEther() const;
82 protected:
83   Mutex mutex_;
84         boost::asio::ip::address_v4 ipv4_address_;
85         boost::asio::ip::address_v6 ipv6_address_;
86         u_int64_t ethernet_address_;
87         network_address_type_t network_address_type_;
88 private:
89         NetworkAddress operator=(const NetworkAddress &s);
90   friend class boost::serialization::access;
91   template<class Archive>
92   void serialize(Archive & ar, const unsigned int version)
93   {
94     ar & network_address_type_;
95                 if (network_address_type_==ipv4)
96                 {
97                         std::string ip(ipv4_address_.to_string());
98                         ar & ip;
99                         ipv4_address_=boost::asio::ip::address_v4::from_string(ip);
100                 }
101                 if (network_address_type_==ipv6)
102                 {
103                         std::string ip(ipv6_address_.to_string());
104                         ar & ip;
105                         ipv6_address_=boost::asio::ip::address_v6::from_string(ip);
106                 }
107                 if (network_address_type_==ethernet)
108                         ar & ethernet_address_;
109    }
110 };
111
112 //                      for(int i=0;i<4;i++)
113 //#if defined(__GNUC__) && defined(__linux__)
114 //                              ar & ipv6_address_.s6_addr32;
115 //#elif defined(__GNUC__) && defined(__OpenBSD__)
116 //        ar & ipv6_address_.__u6_addr.__u6_addr32;
117 //#else
118 // #error Target not supported
119 //#endif
120 #endif