some cosmetic fixes at the manpages
[anytun.git] / src / networkAddress.cpp
blobdce80547294a75567d7f474f8097f263328a732e
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 defined(__GNUC__) && defined(__linux__)
129 if (ipv6_address_.s6_addr32[i]<right.ipv6_address_.s6_addr32[i])
130 #elif defined(__GNUC__) && defined(__OpenBSD__)
131 if (ipv6_address_.__u6_addr.__u6_addr32[i]<right.ipv6_address_.__u6_addr.__u6_addr32[i])
132 #else
133 #error Target not supported
134 #endif
135 return true;
136 return false;
137 } else if (network_address_type_==ethernet) {
138 //TODO
139 } else {
140 //TODO
142 return false;
146 NetworkAddress NetworkAddress::operator<<(uint8_t shift) const
148 if (network_address_type_==ipv4)
150 in_addr new_v4_addr;
151 new_v4_addr.s_addr = ipv4_address_.s_addr << shift;
152 return (NetworkAddress(new_v4_addr));
153 } else if (network_address_type_==ipv6) {
154 in6_addr new_v6_addr;
155 for(int i=0;i<4;i++)
157 #if defined(__GNUC__) && defined(__linux__)
158 new_v6_addr.s6_addr32[i]=ipv6_address_.s6_addr32[i]<<1;
159 if (i<3 && ipv6_address_.s6_addr32[i+1] || uint32_t (0x80000000))
160 new_v6_addr.s6_addr32[i] &=1;
161 #elif defined(__GNUC__) && defined(__OpenBSD__)
162 new_v6_addr.__u6_addr.__u6_addr32[i]=ipv6_address_.__u6_addr.__u6_addr32[i]<<1;
163 if (i<3 && ipv6_address_.__u6_addr.__u6_addr32[i+1] || uint32_t (0x80000000))
164 new_v6_addr.__u6_addr.__u6_addr32[i] &=1;
165 #else
166 #error Target not supported
167 #endif
170 return NetworkAddress(new_v6_addr);
171 } else if (network_address_type_==ethernet) {
172 //TODO
173 } else {
174 //TODO
176 return false;
179 NetworkAddress NetworkAddress::operator&(const NetworkAddress &right) const
181 if (network_address_type_!=right.network_address_type_)
182 throw std::runtime_error("network_address_types did not match");
183 if (network_address_type_==ipv4)
185 in_addr new_v4_addr;
186 new_v4_addr.s_addr = ipv4_address_.s_addr & right.ipv4_address_.s_addr;
187 return (NetworkAddress(new_v4_addr));
188 } else if (network_address_type_==ipv6) {
189 in6_addr new_v6_addr;
190 for(int i=0;i<4;i++)
191 #if defined(__GNUC__) && defined(__linux__)
192 new_v6_addr.s6_addr32[i]=ipv6_address_.s6_addr32[i]&right.ipv6_address_.s6_addr32[i];
193 #elif defined(__GNUC__) && defined(__OpenBSD__)
194 new_v6_addr.__u6_addr.__u6_addr32[i]=ipv6_address_.__u6_addr.__u6_addr32[i]&right.ipv6_address_.__u6_addr.__u6_addr32[i];
195 #else
196 #error Target not supported
197 #endif
199 return NetworkAddress(new_v6_addr);
200 } else if (network_address_type_==ethernet) {
201 //TODO
202 } else {
203 //TODO
205 return false;
208 NetworkAddress NetworkAddress::operator&=(const NetworkAddress &right)
210 if (network_address_type_!=right.network_address_type_)
211 throw std::runtime_error("network_address_types did not match");
212 if (network_address_type_==ipv4)
214 ipv4_address_.s_addr &= right.ipv4_address_.s_addr;
215 return *this;
216 } else if (network_address_type_==ipv6) {
217 for(int i=0;i<4;i++)
218 #if defined(__GNUC__) && defined(__linux__)
219 ipv6_address_.s6_addr32[i]&=right.ipv6_address_.s6_addr32[i];
220 #elif defined(__GNUC__) && defined(__OpenBSD__)
221 ipv6_address_.__u6_addr.__u6_addr32[i]&=right.ipv6_address_.__u6_addr.__u6_addr32[i];
222 #else
223 #error Target not supported
224 #endif
226 return *this;
227 } else if (network_address_type_==ethernet) {
228 //TODO
229 } else {
230 //TODO
232 return false;