From 6fc1641c06c2bedf2f6369267e0d99ebeb9872d6 Mon Sep 17 00:00:00 2001 From: malloc Date: Tue, 6 Mar 2018 16:54:31 -0600 Subject: [PATCH] ip man part 2 ip harder --- server/src/main.cpp | 5 +++- server/src/utils/net.cpp | 58 +++++++++++++++++++++++++++++++++------- server/src/utils/net.hpp | 5 +++- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/server/src/main.cpp b/server/src/main.cpp index daeda41..a239d56 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -18,7 +18,10 @@ int main(int argc, char **argv) { std::string net = HTONUL(test); test = NTOHUL(net); - std::string time = sosc::clk::pack_time(); + sosc::net::IpAddress ip1, ip2; + ip1.Parse("::*:FE:EF:F**F"); + ip2.Parse("::ABCD:F*:*:F00F"); + bool same = ip1 == ip2; return 0; } diff --git a/server/src/utils/net.cpp b/server/src/utils/net.cpp index a945796..a327dd0 100644 --- a/server/src/utils/net.cpp +++ b/server/src/utils/net.cpp @@ -3,33 +3,43 @@ // BEGIN IPADDRESS CLASS sosc::net::IpAddress::IpAddress() { + this->Reset(); +} + +void sosc::net::IpAddress::Reset() { for(int i = 0; i < 8; ++i) this->parts[i] = std::make_pair(0, 0); } +bool sosc::net::IpAddress::ParseError() { + this->Reset(); + return false; +} + bool sosc::net::IpAddress::Parse(const std::string& addr) { std::string addr_trim = str::trim(addr); auto parts = str::split(addr_trim, '.'); if(parts.size() == 4) - return ParseIPv4Parts(parts); + return ParseIPv4Parts(parts) + ? true : ParseError(); else if(parts.size() != 1) - return false; + return ParseError(); parts = str::split(addr_trim, "::"); if(parts.size() == 1) { if(ParseIPv6Part(parts[0], true) != 8) - return false; + return ParseError(); } else if(parts.size() == 2) { int left_cnt, right_cnt; if((left_cnt = ParseIPv6Part(parts[0], true)) == -1) - return false; + return ParseError(); if((right_cnt = ParseIPv6Part(parts[1], false)) == -1) - return false; + return ParseError(); if(left_cnt + right_cnt > 8) - return false; + return ParseError(); } else - return false; + return ParseError(); return true; } @@ -92,11 +102,15 @@ int sosc::net::IpAddress::ParseIPv6Part if(part[i] == '*') { part[i] = '0'; ptr->second |= 1 << (part_len - i - 1); + } else if(!(part[i] >= 'a' && part[i] <= 'f') + && !(part[i] >= 'A' && part[i] <= 'F') + && !(part[i] >= '0' && part[i] <= '9')) + { + return -1; } } - try { ptr->first = std::stoi(part, NULL, 16); } - catch(...) { return -1; } + ptr->first = std::stoi(part, NULL, 16); } return parts.size(); @@ -111,7 +125,31 @@ sosc::net::IpAddress::operator const char* () const { } std::string sosc::net::IpAddress::ToString(bool force_ipv6) const { - return "TODO THIS"; + // TODO THIS + return "::"; +} + +bool sosc::net::IpAddress::operator == (const IpAddress& rhs) const { + for(int i = 0; i < 8; ++i) { + uint8_t wildcards = this->parts[i].second | rhs.parts[i].second; + uint16_t mask = 0xFFFF; + + if(wildcards != 0) { + for(int j = 0; j < 4; ++j) { + if((wildcards & (1 << j)) != 0) + mask &= ~(0xF << (4*j)); + } + } + + if((this->parts[i].first & mask) != (rhs.parts[i].first & mask)) + return false; + } + + return true; +} + +bool sosc::net::IpAddress::operator != (const IpAddress& rhs) const { + return !(*this == rhs); } bool sosc::net::IpAddress::IsIdentical(const IpAddress& rhs) const { diff --git a/server/src/utils/net.hpp b/server/src/utils/net.hpp index 618ad77..ab24ffb 100644 --- a/server/src/utils/net.hpp +++ b/server/src/utils/net.hpp @@ -76,9 +76,12 @@ private: typedef std::pair addrpart_t; std::string tostr_cache; + bool tostr_cache_inval = true; addrpart_t parts[8]; - bool ParseIPv4Parts(const std::vector& parts); + void Reset(); + bool ParseError(); + bool ParseIPv4Parts(const std::vector& parts); int ParseIPv6Part(const std::string& addr_part, bool from_start); };