Skip to content

Commit

Permalink
Display number of responses for each step
Browse files Browse the repository at this point in the history
  • Loading branch information
R. Kaleta committed Jun 23, 2019
1 parent 758a168 commit 7ea8b64
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ build

# CLion
.idea
cmake-build-debug/

# Visual Studio Code
.vscode
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.4.190618)
project(Traceroute VERSION 0.5.190624)

# COMPILER
set(CMAKE_CXX_STANDARD 14)
Expand Down
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ Simple ICMP traceroute
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 *any* of the sent requests, then reply addresses are displayed with
average reply time with count of responses.
```
<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> ???
<step>. <reply addresses> [<average time> (<responses count)]
```

When there are no replies, then a single asterisk is displayed.
Expand Down
6 changes: 3 additions & 3 deletions include/ICMPController.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _ICMP_CONTROLLER_HPP_
#define _ICMP_CONTROLLER_HPP_
#ifndef ICMP_CONTROLLER_HPP_
#define ICMP_CONTROLLER_HPP_

#include <cstdlib>
#include <cerrno>
Expand Down 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>, ssize_t> echo_reply(uint16_t id, uint16_t ttl);
std::tuple<std::set<IPAddress>, size_t, size_t> echo_reply(uint16_t id, uint16_t ttl);

private:
IPAddress recv_echo(uint16_t id, uint16_t ttl);
Expand Down
4 changes: 2 additions & 2 deletions include/ICMPReceiver.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _ICMP_RECEIVER_HPP_
#define _ICMP_RECEIVER_HPP_
#ifndef ICMP_RECEIVER_HPP_
#define ICMP_RECEIVER_HPP_

#include <cstdlib>
#include <cerrno>
Expand Down
4 changes: 2 additions & 2 deletions include/ICMPSender.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _ICMP_SENDER_HPP_
#define _ICMP_SENDER_HPP_
#ifndef ICMP_SENDER_HPP_
#define ICMP_SENDER_HPP_

#include <cstdlib>
#include <cerrno>
Expand Down
4 changes: 2 additions & 2 deletions include/IPAddress.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _IP_ADDRESS_HPP_
#define _IP_ADDRESS_HPP_
#ifndef IP_ADDRESS_HPP_
#define IP_ADDRESS_HPP_

#include <cstdlib>
#include <exception>
Expand Down
4 changes: 2 additions & 2 deletions include/RawSocket.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _RAW_SOCKET_HPP_
#define _RAW_SOCKET_HPP_
#ifndef RAW_SOCKET_HPP_
#define RAW_SOCKET_HPP_

#include <cstdlib>
#include <cerrno>
Expand Down
19 changes: 10 additions & 9 deletions src/ICMPController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ void ICMPController::echo_request(const IPAddress & addr, uint16_t id, uint16_t
}
}

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

FD_ZERO(&fd);
FD_SET(socket.descriptor(), &fd);
Expand All @@ -32,19 +33,19 @@ std::tuple<std::set<IPAddress>, ssize_t> ICMPController::echo_reply(uint16_t id,
throw SocketException(strerror(errno));

if(ready == 0)
return std::make_tuple(recvaddr, -1);
break;

IPAddress address = recv_echo(id, ttl);

if(address == IPAddress(0))
continue;

recvaddr.insert(address);
recv_addr.insert(address);
avg_time = (avg_time + 1000000 - timer.tv_usec) / 2;
++recvnum;
} while(recvnum < 3);
++recv_num;
} while(recv_num < 3);

return std::make_tuple(recvaddr, avg_time);
return std::make_tuple(recv_addr, avg_time, recv_num);
}

IPAddress ICMPController::recv_echo(uint16_t id, uint16_t ttl)
Expand Down
31 changes: 14 additions & 17 deletions src/traceroute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,19 @@
#include "IPAddress.hpp"
#include "RawSocket.hpp"

void print_results(uint16_t ttl, const std::set<IPAddress> & recvaddr, ssize_t avg_time)
void print_results(uint16_t ttl, const std::set<IPAddress> & recv_addr, size_t avg_time,
size_t recv_num)
{
std::cout << static_cast<unsigned int>(ttl) << ". ";

if(avg_time < 0 && recvaddr.size() == 0)
if(recv_addr.empty())
std::cout << "*\n";
else if(avg_time < 0)
{
for(auto addr : recvaddr)
std::cout << addr << " ";

std::cout << "???\n";
}
else
{
for(auto addr : recvaddr)
for(auto addr : recv_addr)
std::cout << addr << " ";

std::cout << "[" << avg_time / 1000 << " ms]\n";
std::cout << "[" << avg_time / 1000 << " ms (" << recv_num << ")]\n";
}
}

Expand All @@ -45,17 +39,20 @@ int main(int argc, char * argv[])

IPAddress addr(argv[1]);
uint16_t pid = getpid();
int steps = 30;

std::cout << "traceroute :: destination " << addr << " :: max " << steps << " steps\n";

for(int i = 1; i <= 30; ++i)
for(int i = 1; i <= steps; ++i)
{
std::set<IPAddress> recvaddr;
ssize_t avg_time;
std::set<IPAddress> recv_addr;
size_t avg_time, recv_num;

socket_ctrl.echo_request(addr, pid, i);
std::tie(recvaddr, avg_time) = socket_ctrl.echo_reply(pid, i);
print_results(i, recvaddr, avg_time);
std::tie(recv_addr, avg_time, recv_num) = socket_ctrl.echo_reply(pid, i);
print_results(i, recv_addr, avg_time, recv_num);

if(std::any_of(recvaddr.begin(), recvaddr.end(),
if(std::any_of(recv_addr.begin(), recv_addr.end(),
[addr](const IPAddress & a) { return a == addr; }))
break;
}
Expand Down

0 comments on commit 7ea8b64

Please sign in to comment.