Imported Upstream version 0.3
[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-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 #ifndef _NETWORK_ADDRESS_H
33 #define _NETWORK_ADDRESS_H
34 #include <boost/archive/text_oarchive.hpp>
35 #include <boost/archive/text_iarchive.hpp>
36
37 #include "threadUtils.hpp"
38 #include "datatypes.h"
39
40 #include <string>
41 #include <boost/asio.hpp>
42 #include <boost/array.hpp>
43
44 typedef boost::array<unsigned char,6> ethernet_bytes_type;
45 typedef boost::asio::ip::address_v4::bytes_type ipv4_bytes_type;
46 typedef boost::asio::ip::address_v6::bytes_type ipv6_bytes_type;
47
48 enum network_address_type_t
49 {
50         ipv4=0,
51         ipv6=1,
52         ethernet=2
53 };
54
55 class NetworkAddress
56 {
57 public:
58         NetworkAddress();
59         NetworkAddress(const NetworkAddress &);
60         NetworkAddress(const std::string &);
61         NetworkAddress(boost::asio::ip::address_v6);
62         NetworkAddress(boost::asio::ip::address_v4);
63         NetworkAddress(u_int64_t);
64         NetworkAddress(const network_address_type_t type, const std::string & address );
65         ~NetworkAddress();
66         void setNetworkAddress(const network_address_type_t type, const std::string & address );
67         void setNetworkAddress(boost::asio::ip::address_v4);
68         void setNetworkAddress(boost::asio::ip::address_v6);
69         void setNetworkAddress(u_int64_t);
70         network_address_type_t getNetworkAddressType() const;
71   std::string toString() const;
72         bool operator<(const NetworkAddress &s) const;
73         ipv4_bytes_type to_bytes_v4() const;    
74         ipv6_bytes_type to_bytes_v6() const;    
75         ethernet_bytes_type to_bytes_ethernet() const;  
76   const boost::asio::ip::address_v4& getNetworkAddressV4() const;
77   const boost::asio::ip::address_v6& getNetworkAddressV6() const;
78   const u_int64_t getNetworkAdrressEther() const;
79 protected:
80   Mutex mutex_;
81         boost::asio::ip::address_v4 ipv4_address_;
82         boost::asio::ip::address_v6 ipv6_address_;
83         u_int64_t ethernet_address_;
84         network_address_type_t network_address_type_;
85 private:
86         NetworkAddress operator=(const NetworkAddress &s);
87   friend class boost::serialization::access;
88   template<class Archive>
89   void serialize(Archive & ar, const unsigned int version)
90   {
91     ar & network_address_type_;
92                 if (network_address_type_==ipv4)
93                 {
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                 {
100                         std::string ip(ipv6_address_.to_string());
101                         ar & ip;
102                         ipv6_address_=boost::asio::ip::address_v6::from_string(ip);
103                 }
104                 if (network_address_type_==ethernet)
105                         ar & ethernet_address_;
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