From e738b94481b24201ed6605d648955d0edb33e5fe Mon Sep 17 00:00:00 2001 From: "R. Kaleta" Date: Tue, 18 Jun 2019 22:21:04 +0200 Subject: [PATCH] Refactoring --- CMakeLists.txt | 2 +- README.md | 16 ++++++++++++++++ include/ICMPController.hpp | 2 +- include/IPAddress.hpp | 39 +++++++++++++++++++++----------------- include/RawSocket.hpp | 8 ++++---- src/ICMPController.cpp | 4 ++-- src/IPAddress.cpp | 12 ++++++------ src/traceroute.cpp | 21 +++++++++----------- 8 files changed, 61 insertions(+), 43 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f3a57dd..9fb0d76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.5) -project(traceroute VERSION 0.3.190423) +project(traceroute VERSION 0.4.190618) # COMPILER set(CMAKE_CXX_STANDARD 14) diff --git a/README.md b/README.md index 180cf87..7c5a9d1 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,22 @@ Simple ICMP traceroute ## About Traceroute shows a path through the Internet from your computer to a specified address. This implementation uses ICMP packages and raw sockets. +### Output format +When there are replies for *all* of the sent requests, then reply addresses are displayed with average reply time. +``` +. [] +``` + +When there are replies for *some* of the sent requests, then reply addresses are displayed with question marks indicating unknown reply time. +``` +. ??? +``` + +When there are no replies, then a single asterisk is displayed. +``` +. * +``` + ----- ## Dependencies diff --git a/include/ICMPController.hpp b/include/ICMPController.hpp index 0239873..87990fe 100644 --- a/include/ICMPController.hpp +++ b/include/ICMPController.hpp @@ -29,7 +29,7 @@ class ICMPController } void echo_request(const IPAddress & addr, uint16_t id, uint16_t ttl); - std::tuple, int> echo_reply(uint16_t id, uint16_t ttl); + std::tuple, ssize_t> echo_reply(uint16_t id, uint16_t ttl); private: IPAddress recv_echo(uint16_t id, uint16_t ttl); diff --git a/include/IPAddress.hpp b/include/IPAddress.hpp index ed1a6b1..7eb0055 100644 --- a/include/IPAddress.hpp +++ b/include/IPAddress.hpp @@ -16,15 +16,20 @@ using addr_t = unsigned int; class IPAddress { public: - explicit IPAddress(addr_t addr) : address{addr} + explicit IPAddress(addr_t a) : address{a} { } - explicit IPAddress(const std::string & addr); + explicit IPAddress(const std::string & st); - friend bool operator==(const IPAddress & addr1, const IPAddress & addr2); - friend bool operator<(const IPAddress & addr1, const IPAddress & addr2); - friend std::ostream & operator<<(std::ostream & os, const IPAddress & addr); + IPAddress(const IPAddress & a) = default; + IPAddress(IPAddress && a) = default; + IPAddress & operator=(const IPAddress & a) = default; + IPAddress & operator=(IPAddress && a) = default; + + friend bool operator==(const IPAddress & a1, const IPAddress & a2); + friend bool operator<(const IPAddress & a1, const IPAddress & a2); + friend std::ostream & operator<<(std::ostream & os, const IPAddress & a); explicit operator addr_t() const { @@ -37,34 +42,34 @@ class IPAddress addr_t address; }; -inline bool operator==(const IPAddress & addr1, const IPAddress & addr2) +inline bool operator==(const IPAddress & a1, const IPAddress & a2) { - return addr1.address == addr2.address; + return a1.address == a2.address; } -inline bool operator!=(const IPAddress & addr1, const IPAddress & addr2) +inline bool operator!=(const IPAddress & a1, const IPAddress & a2) { - return !(addr1 == addr2); + return !(a1 == a2); } -inline bool operator<(const IPAddress & addr1, const IPAddress & addr2) +inline bool operator<(const IPAddress & a1, const IPAddress & a2) { - return addr1.address < addr2.address; + return a1.address < a2.address; } -inline bool operator<=(const IPAddress & addr1, const IPAddress & addr2) +inline bool operator<=(const IPAddress & a1, const IPAddress & a2) { - return (addr1 < addr2) || (addr1 == addr2); + return (a1 < a2) || (a1 == a2); } -inline bool operator>(const IPAddress & addr1, const IPAddress & addr2) +inline bool operator>(const IPAddress & a1, const IPAddress & a2) { - return !(addr1 <= addr2); + return !(a1 <= a2); } -inline bool operator>=(const IPAddress & addr1, const IPAddress & addr2) +inline bool operator>=(const IPAddress & a1, const IPAddress & a2) { - return !(addr1 < addr2); + return !(a1 < a2); } std::ostream & operator<<(std::ostream & os, const IPAddress & addr); diff --git a/include/RawSocket.hpp b/include/RawSocket.hpp index 6887767..8718f94 100644 --- a/include/RawSocket.hpp +++ b/include/RawSocket.hpp @@ -32,10 +32,10 @@ class RawSocket close(descr); } - RawSocket(const RawSocket & raw_sck) = delete; - RawSocket(RawSocket && raw_sck) = default; - RawSocket & operator=(const RawSocket & raw_sck) = delete; - RawSocket & operator=(RawSocket && raw_sck) = default; + RawSocket(const RawSocket & r) = delete; + RawSocket(RawSocket && r) = default; + RawSocket & operator=(const RawSocket & r) = delete; + RawSocket & operator=(RawSocket && r) = default; int descriptor() const { diff --git a/src/ICMPController.cpp b/src/ICMPController.cpp index bf9dcf5..020d572 100644 --- a/src/ICMPController.cpp +++ b/src/ICMPController.cpp @@ -11,12 +11,12 @@ void ICMPController::echo_request(const IPAddress & addr, uint16_t id, uint16_t } } -std::tuple, int> ICMPController::echo_reply(uint16_t id, uint16_t ttl) +std::tuple, ssize_t> ICMPController::echo_reply(uint16_t id, uint16_t ttl) { std::set recvaddr; fd_set fd; timeval timer; - int avg_time = 0; + ssize_t avg_time = 0; int recvnum = 0; FD_ZERO(&fd); diff --git a/src/IPAddress.cpp b/src/IPAddress.cpp index c59ada6..3cb13c5 100644 --- a/src/IPAddress.cpp +++ b/src/IPAddress.cpp @@ -1,6 +1,6 @@ #include "IPAddress.hpp" -IPAddress::IPAddress(const std::string & addr) +IPAddress::IPAddress(const std::string & st) { std::vector splitted; std::vector addr_bytes; @@ -8,16 +8,16 @@ IPAddress::IPAddress(const std::string & addr) while(begin_pos != std::string::npos) { - size_t end_pos = addr.find(".", begin_pos); + size_t end_pos = st.find(".", begin_pos); if(end_pos != std::string::npos) { - splitted.push_back(addr.substr(begin_pos, end_pos - begin_pos)); + splitted.push_back(st.substr(begin_pos, end_pos - begin_pos)); begin_pos = end_pos + 1; } else { - splitted.push_back(addr.substr(begin_pos)); + splitted.push_back(st.substr(begin_pos)); begin_pos = end_pos; } } @@ -49,9 +49,9 @@ IPAddress::operator std::string() const + std::to_string(address & 0x000000FF); } -std::ostream & operator<<(std::ostream & os, const IPAddress & addr) +std::ostream & operator<<(std::ostream & os, const IPAddress & a) { - os << static_cast(addr); + os << static_cast(a); return os; } diff --git a/src/traceroute.cpp b/src/traceroute.cpp index 7fcf843..1810ae5 100644 --- a/src/traceroute.cpp +++ b/src/traceroute.cpp @@ -9,28 +9,25 @@ #include "IPAddress.hpp" #include "RawSocket.hpp" -void print_results(uint16_t ttl, const std::set & recvaddr, int avg_time) +void print_results(uint16_t ttl, const std::set & recvaddr, ssize_t avg_time) { std::cout << static_cast(ttl) << ". "; - if(avg_time < 0) + if(avg_time < 0 && recvaddr.size() == 0) + std::cout << "*\n"; + else if(avg_time < 0) { - if(recvaddr.size() == 0) - std::cout << "*\n"; - else - { - for(auto addr : recvaddr) - std::cout << addr << " "; + for(auto addr : recvaddr) + std::cout << addr << " "; - std::cout << "???\n"; - } + std::cout << "???\n"; } else { for(auto addr : recvaddr) std::cout << addr << " "; - std::cout << avg_time / 1000 << "ms\n"; + std::cout << "[" << avg_time / 1000 << " ms]\n"; } } @@ -52,7 +49,7 @@ int main(int argc, char * argv[]) for(int i = 1; i <= 30; ++i) { std::set recvaddr; - int avg_time; + ssize_t avg_time; socket_ctrl.echo_request(addr, pid, i); std::tie(recvaddr, avg_time) = socket_ctrl.echo_reply(pid, i);