added endian.h
[anytun.git] / src / networkAddress.h
blobf866cca56d059d69649485b4afa67506f6cc50cf
1 /*
2 * anytun
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.
14 * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
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.
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.
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/>.
32 #ifndef _NETWORK_ADDRESS_H
33 #define _NETWORK_ADDRESS_H
34 #include <boost/archive/text_oarchive.hpp>
35 #include <boost/archive/text_iarchive.hpp>
37 #include "threadUtils.hpp"
38 #include "datatypes.h"
40 #include <string>
41 #include <boost/asio.hpp>
42 #include <boost/array.hpp>
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;
48 enum network_address_type_t
50 ipv4=0,
51 ipv6=1,
52 ethernet=2
55 class NetworkAddress
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 network_address_type_t getNetworkAddressType() const;
68 std::string toString() const;
69 bool operator<(const NetworkAddress &s) const;
70 ipv4_bytes_type to_bytes_v4() const;
71 ipv6_bytes_type to_bytes_v6() const;
72 ethernet_bytes_type to_bytes_ethernet() const;
73 protected:
74 Mutex mutex_;
75 boost::asio::ip::address_v4 ipv4_address_;
76 boost::asio::ip::address_v6 ipv6_address_;
77 u_int64_t ethernet_address_;
78 network_address_type_t network_address_type_;
79 private:
80 NetworkAddress operator=(const NetworkAddress &s);
81 friend class boost::serialization::access;
82 template<class Archive>
83 void serialize(Archive & ar, const unsigned int version)
85 ar & network_address_type_;
86 if (network_address_type_==ipv4)
88 std::string ip(ipv4_address_.to_string());
89 ar & ip;
90 ipv4_address_=boost::asio::ip::address_v4::from_string(ip);
92 if (network_address_type_==ipv6)
94 std::string ip(ipv6_address_.to_string());
95 ar & ip;
96 ipv6_address_=boost::asio::ip::address_v6::from_string(ip);
98 if (network_address_type_==ethernet)
99 ar & ethernet_address_;
103 // for(int i=0;i<4;i++)
104 //#if defined(__GNUC__) && defined(__linux__)
105 // ar & ipv6_address_.s6_addr32;
106 //#elif defined(__GNUC__) && defined(__OpenBSD__)
107 // ar & ipv6_address_.__u6_addr.__u6_addr32;
108 //#else
109 // #error Target not supported
110 //#endif
111 #endif