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"
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
)
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
) {
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
) {
116 return std::string("");
119 bool NetworkAddress::operator<(const NetworkAddress
&right
) const
121 if (network_address_type_
!=right
.network_address_type_
)
123 if (network_address_type_
==ipv4
)
125 return (ipv4_address_
.s_addr
< right
.ipv4_address_
.s_addr
);
126 } else if (network_address_type_
==ipv6
) {
128 if (ipv6_address_
.s6_addr32
[i
]<right
.ipv6_address_
.s6_addr32
[i
])
131 } else if (network_address_type_
==ethernet
) {
140 NetworkAddress
NetworkAddress::operator<<(uint8_t shift
) const
142 if (network_address_type_
==ipv4
)
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
;
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
) {
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
)
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
;
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
) {
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
;
194 } else if (network_address_type_
==ipv6
) {
196 ipv6_address_
.s6_addr32
[i
]&=right
.ipv6_address_
.s6_addr32
[i
];
198 } else if (network_address_type_
==ethernet
) {