summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/networkAddress.cpp144
-rw-r--r--src/networkAddress.h47
-rw-r--r--src/networkPrefix.cpp22
-rw-r--r--src/plainPacket.cpp59
-rw-r--r--src/plainPacket.h2
5 files changed, 84 insertions, 190 deletions
diff --git a/src/networkAddress.cpp b/src/networkAddress.cpp
index 5dc1f9f..9f22e4b 100644
--- a/src/networkAddress.cpp
+++ b/src/networkAddress.cpp
@@ -35,23 +35,35 @@
#include "networkAddress.h"
-NetworkAddress::NetworkAddress()
+NetworkAddress::NetworkAddress():ipv4_address_(),ipv6_address_()
{
network_address_type_=ipv4;
- ipv4_address_.s_addr=0;
}
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_)
{
}
-NetworkAddress::NetworkAddress(in6_addr ipv6_address)
+NetworkAddress::NetworkAddress(const std::string & address)
+{
+ boost::asio::ip::address addr = boost::asio::ip::address::from_string(address);
+ if (addr.is_v4())
+ {
+ network_address_type_=ipv4;
+ ipv4_address_ = addr.to_v4();
+ } else {
+ network_address_type_=ipv6;
+ ipv6_address_ = addr.to_v6();
+ }
+}
+
+NetworkAddress::NetworkAddress(boost::asio::ip::address_v6 ipv6_address)
{
network_address_type_=ipv6;
ipv6_address_ = ipv6_address;
}
-NetworkAddress::NetworkAddress(in_addr ipv4_address)
+NetworkAddress::NetworkAddress(boost::asio::ip::address_v4 ipv4_address)
{
network_address_type_=ipv4;
ipv4_address_ = ipv4_address;
@@ -73,13 +85,13 @@ NetworkAddress::NetworkAddress(const network_address_type_t type, const char * a
setNetworkAddress( type, address);
}
-void NetworkAddress::setNetworkAddress(const network_address_type_t type, const char * address )
+void NetworkAddress::setNetworkAddress(const network_address_type_t type, const std::string & address )
{
if (type==ipv4)
{
- inet_pton(AF_INET, address, &ipv4_address_);
+ ipv4_address_=boost::asio::ip::address_v4::from_string(address);
} else if (type==ipv6) {
- inet_pton(AF_INET6, address, &ipv6_address_);
+ ipv6_address_=boost::asio::ip::address_v6::from_string(address);
} else if (type==ethernet) {
//TODO
} else {
@@ -88,10 +100,6 @@ void NetworkAddress::setNetworkAddress(const network_address_type_t type, const
network_address_type_ = type;
}
-void NetworkAddress::getNetworkAddress(const char *)
-{
-}
-
network_address_type_t NetworkAddress::getNetworkAddressType()
{
return network_address_type_;
@@ -100,16 +108,10 @@ network_address_type_t NetworkAddress::getNetworkAddressType()
std::string NetworkAddress::toString() const
{
if (network_address_type_==ipv4){
- char buf[INET_ADDRSTRLEN];
- if(!inet_ntop(AF_INET, &ipv4_address_, buf, sizeof(buf)))
- return std::string("");
- return std::string(buf);
+ return ipv4_address_.to_string();
}
else if (network_address_type_==ipv6) {
- char buf[INET6_ADDRSTRLEN];
- if(!inet_ntop(AF_INET6, &ipv6_address_, buf, sizeof(buf)))
- return std::string("");
- return std::string(buf);
+ return ipv6_address_.to_string();
}
else if (network_address_type_==ethernet) {
// TODO
@@ -123,112 +125,14 @@ bool NetworkAddress::operator<(const NetworkAddress &right) const
return false;
if (network_address_type_==ipv4)
{
- return (ipv4_address_.s_addr < right.ipv4_address_.s_addr);
+ return (ipv4_address_ < right.ipv4_address_);
} else if (network_address_type_==ipv6) {
- for(int i=0;i<4;i++)
-#if defined(__GNUC__) && defined(__linux__)
- if (ipv6_address_.s6_addr32[i]<right.ipv6_address_.s6_addr32[i])
-#elif defined(__GNUC__) && defined(__OpenBSD__)
- if (ipv6_address_.__u6_addr.__u6_addr32[i]<right.ipv6_address_.__u6_addr.__u6_addr32[i])
-#else
- #error Target not supported
-#endif
- return true;
- return false;
+ return (ipv6_address_ < right.ipv6_address_);
} else if (network_address_type_==ethernet) {
- //TODO
+ return (ethernet_address_ < right.ethernet_address_);
} else {
//TODO
}
return false;
}
-
-NetworkAddress NetworkAddress::operator<<(uint8_t shift) const
-{
- if (network_address_type_==ipv4)
- {
- in_addr new_v4_addr;
- new_v4_addr.s_addr = ipv4_address_.s_addr << shift;
- return (NetworkAddress(new_v4_addr));
- } else if (network_address_type_==ipv6) {
- in6_addr new_v6_addr;
- for(int i=0;i<4;i++)
- {
-#if defined(__GNUC__) && defined(__linux__)
- new_v6_addr.s6_addr32[i]=ipv6_address_.s6_addr32[i]<<1;
- if (i<3 && (ipv6_address_.s6_addr32[i+1] & uint32_t (0x80000000)))
- new_v6_addr.s6_addr32[i] &=1;
-#elif defined(__GNUC__) && defined(__OpenBSD__)
- new_v6_addr.__u6_addr.__u6_addr32[i]=ipv6_address_.__u6_addr.__u6_addr32[i]<<1;
- if (i<3 && (ipv6_address_.__u6_addr.__u6_addr32[i+1] & uint32_t (0x80000000)))
- new_v6_addr.__u6_addr.__u6_addr32[i] &=1;
-#else
- #error Target not supported
-#endif
-
- }
- return NetworkAddress(new_v6_addr);
- } else if (network_address_type_==ethernet) {
- //TODO
- } else {
- //TODO
- }
- return false;
-}
-
-NetworkAddress NetworkAddress::operator&(const NetworkAddress &right) const
-{
- if (network_address_type_!=right.network_address_type_)
- throw std::runtime_error("network_address_types did not match");
- if (network_address_type_==ipv4)
- {
- in_addr new_v4_addr;
- new_v4_addr.s_addr = ipv4_address_.s_addr & right.ipv4_address_.s_addr;
- return (NetworkAddress(new_v4_addr));
- } else if (network_address_type_==ipv6) {
- in6_addr new_v6_addr;
- for(int i=0;i<4;i++)
-#if defined(__GNUC__) && defined(__linux__)
- new_v6_addr.s6_addr32[i]=ipv6_address_.s6_addr32[i]&right.ipv6_address_.s6_addr32[i];
-#elif defined(__GNUC__) && defined(__OpenBSD__)
- new_v6_addr.__u6_addr.__u6_addr32[i]=ipv6_address_.__u6_addr.__u6_addr32[i]&right.ipv6_address_.__u6_addr.__u6_addr32[i];
-#else
- #error Target not supported
-#endif
-
- return NetworkAddress(new_v6_addr);
- } else if (network_address_type_==ethernet) {
- //TODO
- } else {
- //TODO
- }
- return false;
-}
-
-NetworkAddress NetworkAddress::operator&=(const NetworkAddress &right)
-{
- if (network_address_type_!=right.network_address_type_)
- throw std::runtime_error("network_address_types did not match");
- if (network_address_type_==ipv4)
- {
- ipv4_address_.s_addr &= right.ipv4_address_.s_addr;
- return *this;
- } else if (network_address_type_==ipv6) {
- for(int i=0;i<4;i++)
-#if defined(__GNUC__) && defined(__linux__)
- ipv6_address_.s6_addr32[i]&=right.ipv6_address_.s6_addr32[i];
-#elif defined(__GNUC__) && defined(__OpenBSD__)
- ipv6_address_.__u6_addr.__u6_addr32[i]&=right.ipv6_address_.__u6_addr.__u6_addr32[i];
-#else
- #error Target not supported
-#endif
-
- return *this;
- } else if (network_address_type_==ethernet) {
- //TODO
- } else {
- //TODO
- }
- return false;
-}
diff --git a/src/networkAddress.h b/src/networkAddress.h
index 47fa8b4..774a025 100644
--- a/src/networkAddress.h
+++ b/src/networkAddress.h
@@ -41,6 +41,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
+#include <boost/asio.hpp>
enum network_address_type_t
{
@@ -54,24 +55,20 @@ class NetworkAddress
public:
NetworkAddress();
NetworkAddress(const NetworkAddress &);
- NetworkAddress(in6_addr);
- NetworkAddress(in_addr);
+ NetworkAddress(const std::string &);
+ NetworkAddress(boost::asio::ip::address_v6);
+ NetworkAddress(boost::asio::ip::address_v4);
NetworkAddress(uint64_t);
NetworkAddress(const network_address_type_t type, const char * address );
~NetworkAddress();
- void setNetworkAddress(const network_address_type_t type, const char * address );
- void getNetworkAddress(const char *);
+ void setNetworkAddress(const network_address_type_t type, const std::string & address );
network_address_type_t getNetworkAddressType();
std::string toString() const;
- bool operator<(const NetworkAddress &s) const;
- NetworkAddress operator&(const NetworkAddress &s) const;
- NetworkAddress operator&=(const NetworkAddress &s);
- NetworkAddress operator<<(uint8_t shift) const;
-
+ bool operator<(const NetworkAddress &s) const;
protected:
Mutex mutex_;
- in_addr ipv4_address_;
- in6_addr ipv6_address_;
+ boost::asio::ip::address_v4 ipv4_address_;
+ boost::asio::ip::address_v6 ipv6_address_;
uint64_t ethernet_address_;
network_address_type_t network_address_type_;
private:
@@ -82,20 +79,28 @@ private:
{
ar & network_address_type_;
if (network_address_type_==ipv4)
- ar & ipv4_address_.s_addr;
+ {
+ std::string ip(ipv4_address_.to_string());
+ ar & ip;
+ ipv4_address_=boost::asio::ip::address_v4::from_string(ip);
+ }
if (network_address_type_==ipv6)
- for(int i=0;i<4;i++)
-#if defined(__GNUC__) && defined(__linux__)
- ar & ipv6_address_.s6_addr32;
-#elif defined(__GNUC__) && defined(__OpenBSD__)
- ar & ipv6_address_.__u6_addr.__u6_addr32;
-#else
- #error Target not supported
-#endif
-
+ {
+ std::string ip(ipv6_address_.to_string());
+ ar & ip;
+ ipv6_address_=boost::asio::ip::address_v6::from_string(ip);
+ }
if (network_address_type_==ethernet)
ar & ethernet_address_;
}
};
+// for(int i=0;i<4;i++)
+//#if defined(__GNUC__) && defined(__linux__)
+// ar & ipv6_address_.s6_addr32;
+//#elif defined(__GNUC__) && defined(__OpenBSD__)
+// ar & ipv6_address_.__u6_addr.__u6_addr32;
+//#else
+// #error Target not supported
+//#endif
#endif
diff --git a/src/networkPrefix.cpp b/src/networkPrefix.cpp
index cc0e57e..7ba47ea 100644
--- a/src/networkPrefix.cpp
+++ b/src/networkPrefix.cpp
@@ -50,28 +50,6 @@ NetworkPrefix::NetworkPrefix(const NetworkPrefix & src): NetworkAddress(src),len
void NetworkPrefix::setNetworkPrefixLength(uint8_t length )
{
length_ = length;
- if (network_address_type_==ipv4)
- {
- in_addr v4addr;
- v4addr.s_addr=0xFFFFFFFF;
- *this &= (NetworkAddress(v4addr)<<(32-length));
- } else if (network_address_type_==ipv6) {
- in6_addr v6addr;
- for(int i=0;i<4;i++)
-#if defined(__GNUC__) && defined(__linux__)
- ipv6_address_.s6_addr32[i]=0xFFFFFFFF;
-#elif defined(__GNUC__) && defined(__OpenBSD__)
- ipv6_address_.__u6_addr.__u6_addr32[i]=0xFFFFFFFF;
-#else
- #error Target not supported
-#endif
-
- *this &= (NetworkAddress(v6addr)<<(128-length));
- } else if (network_address_type_==ethernet) {
- //TODO
- } else {
- //TODO
- }
}
uint8_t NetworkPrefix::getNetworkPrefixLength()
diff --git a/src/plainPacket.cpp b/src/plainPacket.cpp
index b3441e0..46016a5 100644
--- a/src/plainPacket.cpp
+++ b/src/plainPacket.cpp
@@ -115,6 +115,7 @@ u_int8_t* PlainPacket::getPayload()
return payload_;
}
+/*
NetworkAddress PlainPacket::getSrcAddr() const
{
if(!payload_type_ || !payload_)
@@ -142,33 +143,39 @@ NetworkAddress PlainPacket::getSrcAddr() const
return NetworkAddress(hdr->ip6_src);
}
return NetworkAddress();
-}
+}*/
NetworkAddress PlainPacket::getDstAddr() const
{
- if(!payload_type_ || !payload_)
- return NetworkAddress();
-
- payload_type_t type = PAYLOAD_TYPE_T_NTOH(*payload_type_);
-
- if(type == PAYLOAD_TYPE_TAP) // Ehternet
- {
- // TODO
- return NetworkAddress();
- }
- else if(type == PAYLOAD_TYPE_TUN4) // IPv4
- {
- if(length_ < (sizeof(payload_type_t)+sizeof(struct ip)))
- return NetworkAddress();
- struct ip* hdr = reinterpret_cast<struct ip*>(payload_);
- return NetworkAddress(hdr->ip_dst);
- }
- else if(type == PAYLOAD_TYPE_TUN6) // IPv6
- {
- if(length_ < (sizeof(payload_type_t)+sizeof(struct ip6_hdr)))
- return NetworkAddress();
- struct ip6_hdr* hdr = reinterpret_cast<struct ip6_hdr*>(payload_);
- return NetworkAddress(hdr->ip6_dst);
- }
- return NetworkAddress();
+ if(!payload_type_ || !payload_)
+ return NetworkAddress();
+
+ payload_type_t type = PAYLOAD_TYPE_T_NTOH(*payload_type_);
+
+ if(type == PAYLOAD_TYPE_TAP) // Ehternet
+ {
+ // TODO
+ return NetworkAddress();
+ }
+ else if(type == PAYLOAD_TYPE_TUN4) // IPv4
+ {
+ if(length_ < (sizeof(payload_type_t)+5*4))
+ return NetworkAddress();
+ char * hdr = reinterpret_cast<char *>(payload_);
+ boost::asio::ip::address_v4::bytes_type ip_octets;
+ for (int i=0; i<4;i++)
+ ip_octets[i]=hdr[4*4+i];
+ return NetworkAddress(boost::asio::ip::address_v4(ip_octets));
+ }
+ else if(type == PAYLOAD_TYPE_TUN6) // IPv6
+ {
+ if(length_ < (sizeof(payload_type_t)+2*16+2*4))
+ return NetworkAddress();
+ char * hdr = reinterpret_cast<char *>(payload_);
+ boost::asio::ip::address_v6::bytes_type ip_octets;
+ for (int i=0; i<16;i++)
+ ip_octets[i]=hdr[2*4+16+i];
+ return NetworkAddress(boost::asio::ip::address_v6(ip_octets));
+ }
+ return NetworkAddress();
}
diff --git a/src/plainPacket.h b/src/plainPacket.h
index 667c723..a66f3fc 100644
--- a/src/plainPacket.h
+++ b/src/plainPacket.h
@@ -93,7 +93,7 @@ public:
*/
u_int8_t* getPayload();
- NetworkAddress getSrcAddr() const;
+// NetworkAddress getSrcAddr() const;
NetworkAddress getDstAddr() const;
private: