working example server and client
[anytun.git] / networkAddress.cpp
blobcd93576c0c13aeeae3f90308a9e7b3d29c43137c
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 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING included with this
27 * distribution); if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "threadUtils.hpp"
32 #include "datatypes.h"
33 #include <exception>
35 #include "networkAddress.h"
37 NetworkAddress::NetworkAddress()
39 network_address_type_=ipv4;
40 ipv4_address_.s_addr=0;
43 NetworkAddress::NetworkAddress(const NetworkAddress & ref) : mutex_(),ipv4_address_(ref.ipv4_address_),ipv6_address_(ref.ipv6_address_),ethernet_address_(ref.ethernet_address_),network_address_type_(ref.network_address_type_)
47 NetworkAddress::NetworkAddress(in6_addr ipv6_address)
49 network_address_type_=ipv6;
50 ipv6_address_ = ipv6_address;
53 NetworkAddress::NetworkAddress(in_addr ipv4_address)
55 network_address_type_=ipv4;
56 ipv4_address_ = ipv4_address;
59 NetworkAddress::NetworkAddress(uint64_t ethernet_address)
61 network_address_type_=ethernet;
62 ethernet_address_=ethernet_address;
66 NetworkAddress::~NetworkAddress()
70 NetworkAddress::NetworkAddress(const network_address_type_t type, const char * address )
72 setNetworkAddress( type, address);
75 void NetworkAddress::setNetworkAddress(const network_address_type_t type, const char * address )
77 if (type==ipv4)
79 inet_pton(AF_INET, address, &ipv4_address_);
80 } else if (type==ipv6) {
81 inet_pton(AF_INET6, address, &ipv6_address_);
82 } else if (type==ethernet) {
83 //TODO
84 } else {
85 //TODO
87 network_address_type_ = type;
90 void NetworkAddress::getNetworkAddress(const char *)
94 network_address_type_t NetworkAddress::getNetworkAddressType()
96 return network_address_type_;
99 std::string NetworkAddress::toString() const
101 if (network_address_type_==ipv4){
102 char buf[INET_ADDRSTRLEN];
103 if(!inet_ntop(AF_INET, &ipv4_address_, buf, sizeof(buf)))
104 return std::string("");
105 return std::string(buf);
107 else if (network_address_type_==ipv6) {
108 char buf[INET6_ADDRSTRLEN];
109 if(!inet_ntop(AF_INET6, &ipv6_address_, buf, sizeof(buf)))
110 return std::string("");
111 return std::string(buf);
113 else if (network_address_type_==ethernet) {
114 // TODO
116 return std::string("");
119 bool NetworkAddress::operator<(const NetworkAddress &right) const
121 if (network_address_type_!=right.network_address_type_)
122 return false;
123 if (network_address_type_==ipv4)
125 return (ipv4_address_.s_addr < right.ipv4_address_.s_addr);
126 } else if (network_address_type_==ipv6) {
127 for(int i=0;i<4;i++)
128 if (ipv6_address_.s6_addr32[i]<right.ipv6_address_.s6_addr32[i])
129 return true;
130 return false;
131 } else if (network_address_type_==ethernet) {
132 //TODO
133 } else {
134 //TODO
136 return false;
140 NetworkAddress NetworkAddress::operator<<(uint8_t shift) const
142 if (network_address_type_==ipv4)
144 in_addr new_v4_addr;
145 new_v4_addr.s_addr = ipv4_address_.s_addr << shift;
146 return (NetworkAddress(new_v4_addr));
147 } else if (network_address_type_==ipv6) {
148 in6_addr new_v6_addr;
149 for(int i=0;i<4;i++)
151 new_v6_addr.s6_addr32[i]=ipv6_address_.s6_addr32[i]<<1;
152 if (i<3 && ipv6_address_.s6_addr32[i+1] || uint32_t (0x80000000))
153 new_v6_addr.s6_addr32[i] &=1;
155 return NetworkAddress(new_v6_addr);
156 } else if (network_address_type_==ethernet) {
157 //TODO
158 } else {
159 //TODO
161 return false;
164 NetworkAddress NetworkAddress::operator&(const NetworkAddress &right) const
166 if (network_address_type_!=right.network_address_type_)
167 throw std::runtime_error("network_address_types did not match");
168 if (network_address_type_==ipv4)
170 in_addr new_v4_addr;
171 new_v4_addr.s_addr = ipv4_address_.s_addr & right.ipv4_address_.s_addr;
172 return (NetworkAddress(new_v4_addr));
173 } else if (network_address_type_==ipv6) {
174 in6_addr new_v6_addr;
175 for(int i=0;i<4;i++)
176 new_v6_addr.s6_addr32[i]=ipv6_address_.s6_addr32[i]&right.ipv6_address_.s6_addr32[i];
177 return NetworkAddress(new_v6_addr);
178 } else if (network_address_type_==ethernet) {
179 //TODO
180 } else {
181 //TODO
183 return false;
186 NetworkAddress NetworkAddress::operator&=(const NetworkAddress &right)
188 if (network_address_type_!=right.network_address_type_)
189 throw std::runtime_error("network_address_types did not match");
190 if (network_address_type_==ipv4)
192 ipv4_address_.s_addr &= right.ipv4_address_.s_addr;
193 return *this;
194 } else if (network_address_type_==ipv6) {
195 for(int i=0;i<4;i++)
196 ipv6_address_.s6_addr32[i]&=right.ipv6_address_.s6_addr32[i];
197 return *this;
198 } else if (network_address_type_==ethernet) {
199 //TODO
200 } else {
201 //TODO
203 return false;