Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
R. Kaleta committed Jun 18, 2019
1 parent 01fc30d commit e738b94
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 43 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```
<step>. <reply addresses> [<average time>]
```

When there are replies for *some* of the sent requests, then reply addresses are displayed with question marks indicating unknown reply time.
```
<step>. <reply addresses> ???
```

When there are no replies, then a single asterisk is displayed.
```
<step>. *
```

-----

## Dependencies
Expand Down
2 changes: 1 addition & 1 deletion include/ICMPController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ICMPController
}

void echo_request(const IPAddress & addr, uint16_t id, uint16_t ttl);
std::tuple<std::set<IPAddress>, int> echo_reply(uint16_t id, uint16_t ttl);
std::tuple<std::set<IPAddress>, ssize_t> echo_reply(uint16_t id, uint16_t ttl);

private:
IPAddress recv_echo(uint16_t id, uint16_t ttl);
Expand Down
39 changes: 22 additions & 17 deletions include/IPAddress.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions include/RawSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions src/ICMPController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ void ICMPController::echo_request(const IPAddress & addr, uint16_t id, uint16_t
}
}

std::tuple<std::set<IPAddress>, int> ICMPController::echo_reply(uint16_t id, uint16_t ttl)
std::tuple<std::set<IPAddress>, ssize_t> ICMPController::echo_reply(uint16_t id, uint16_t ttl)
{
std::set<IPAddress> recvaddr;
fd_set fd;
timeval timer;
int avg_time = 0;
ssize_t avg_time = 0;
int recvnum = 0;

FD_ZERO(&fd);
Expand Down
12 changes: 6 additions & 6 deletions src/IPAddress.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#include "IPAddress.hpp"

IPAddress::IPAddress(const std::string & addr)
IPAddress::IPAddress(const std::string & st)
{
std::vector<std::string> splitted;
std::vector<addr_t> addr_bytes;
size_t begin_pos = 0;

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;
}
}
Expand Down Expand Up @@ -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<std::string>(addr);
os << static_cast<std::string>(a);

return os;
}
21 changes: 9 additions & 12 deletions src/traceroute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,25 @@
#include "IPAddress.hpp"
#include "RawSocket.hpp"

void print_results(uint16_t ttl, const std::set<IPAddress> & recvaddr, int avg_time)
void print_results(uint16_t ttl, const std::set<IPAddress> & recvaddr, ssize_t avg_time)
{
std::cout << static_cast<unsigned int>(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";
}
}

Expand All @@ -52,7 +49,7 @@ int main(int argc, char * argv[])
for(int i = 1; i <= 30; ++i)
{
std::set<IPAddress> 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);
Expand Down

0 comments on commit e738b94

Please sign in to comment.