Skip to content

Commit

Permalink
Moved includes, extracted methods, new output format
Browse files Browse the repository at this point in the history
  • Loading branch information
R. Kaleta committed Oct 14, 2019
1 parent b042991 commit 7b7a319
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 148 deletions.
58 changes: 30 additions & 28 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# VERSION 8

AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignEscapedNewlines: DontAlign
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BinPackParameters: true
BreakBeforeBinaryOperators: NonAssignment
Expand All @@ -34,30 +36,30 @@ FixNamespaceComments: false
ForEachMacros: []
IncludeBlocks: Merge
IncludeCategories:
# C Standard library
- Regex: '^<cstdlib>'
Priority: 1
# C standard library
- Regex: '^<c(std(arg|def|int|io)|assert|(u|w)char|ctype|errno|fenv|float|inttypes|limits|locale|math|setjmp|signal|string|time|wctype)>'
Priority: 2
# C++ standard library: input-output, exceptions, memory, threads
- Regex: '^<(ios(fwd)?|(i|o|io|f|s)stream|streambuf|iomanip|exception|stdexcept|system_error|new|memory|atomic|thread|(shared_)?mutex|future|condition_variable)>'
Priority: 3
# C++ standard library: algorithms, containers, strings
- Regex: '^<(algorithm|array|bitset|deque|iterator|(forward_|initializer_)?list|(unordered_)?map|queue|(unordered_)?set|stack|string|tuple|vector)>'
Priority: 4
# C++ standard library: utilities, numerics, types
- Regex: '^<(complex|chrono|functional|limits|locale|numeric|random|ratio|regex|type(info|index|_traits)|valarray)>'
Priority: 5
# All other headers (C++ style)
- Regex: '^<.*>'
Priority: 6
# All other headers (C style)
- Regex: '^<.*\.(h|hpp)>'
Priority: 7
# All custom headers
- Regex: '^".*"'
Priority: 8
# C Standard library
- Regex: '^<cstdlib>'
Priority: 1
# C standard library
- Regex: '^<c(std(arg|def|int|io)|assert|(u|w)char|ctype|errno|fenv|float|inttypes|limits|locale|math|setjmp|signal|string|time|wctype)>'
Priority: 2
# C++ standard library: input-output, exceptions, memory, threads
- Regex: '^<(ios(fwd)?|(i|o|io|f|s)stream|streambuf|iomanip|exception|stdexcept|system_error|new|memory|atomic|thread|(shared_)?mutex|future|condition_variable)>'
Priority: 3
# C++ standard library: algorithms, containers, strings
- Regex: '^<(algorithm|array|bitset|deque|iterator|(forward_|initializer_)?list|(unordered_)?map|queue|(unordered_)?set|stack|string|tuple|vector)>'
Priority: 4
# C++ standard library: utilities, numerics, types
- Regex: '^<(complex|chrono|functional|limits|locale|numeric|random|ratio|regex|type(info|index|_traits)|valarray)>'
Priority: 5
# All other headers (C++ style)
- Regex: '^<.*>'
Priority: 6
# All other headers (C style)
- Regex: '^<.*\.(h|hpp)>'
Priority: 7
# All custom headers
- Regex: '^".*"'
Priority: 8
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
Expand All @@ -68,7 +70,7 @@ MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
PointerAlignment: Middle
ReflowComments: true
ReflowComments: false
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
Expand Down
17 changes: 8 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
cmake_minimum_required(VERSION 3.5)
project(Traceroute VERSION 0.5.190624)
project(Traceroute VERSION 0.6.191014)

# COMPILER
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-unknown-pragmas")

# SOURCES
include_directories("${PROJECT_SOURCE_DIR}/include")
include_directories(include)
set(SOURCES
"${PROJECT_SOURCE_DIR}/src/RawSocket.cpp"
"${PROJECT_SOURCE_DIR}/src/IPAddress.cpp"
"${PROJECT_SOURCE_DIR}/src/ICMPReceiver.cpp"
"${PROJECT_SOURCE_DIR}/src/ICMPSender.cpp"
"${PROJECT_SOURCE_DIR}/src/ICMPController.cpp"
"${PROJECT_SOURCE_DIR}/src/traceroute.cpp")
src/IPAddress.cpp
src/ICMPReceiver.cpp
src/ICMPSender.cpp
src/ICMPController.cpp
src/traceroute.cpp)

# OUTPUT
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
add_executable(traceroute ${SOURCES})
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ 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 *any* of the sent requests, then reply addresses are displayed with
When there are replies for *any* of the requests sent, then reply addresses are displayed with
average reply time with count of responses.
```
<step>. <reply addresses> [<average time> (<responses count)]
<step>. <reply addresses> -- <average time> (<responses count>)
```

When there are no replies, then a single asterisk is displayed.
When there are no replies, then a single asterisk character is displayed.
```
<step>. *
```
Expand All @@ -21,12 +21,12 @@ When there are no replies, then a single asterisk is displayed.
## Dependencies

### Standard build & run
> *versions used by the author are in double parentheses and italic*
> *versions last used by the author are in double parentheses and italic*
Build process:
+ Linux-based operating system *((Debian testing))*
+ C++ 14 compiler *((g++ 8.3.0))*
+ [CMake](https://cmake.org/) *((3.13.4))*
+ C++ 14 compiler *((g++ 9.2.1))*
+ [GNU Make](https://www.gnu.org/software/make) *((4.2.1))*

-----
Expand Down
10 changes: 0 additions & 10 deletions include/ICMPController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@
#define ICMP_CONTROLLER_HPP_

#include <cstdlib>
#include <cerrno>
#include <cstring>
#include <set>
#include <string>
#include <tuple>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <sys/time.h>
#include "ICMPReceiver.hpp"
#include "ICMPSender.hpp"
#include "IPAddress.hpp"
#include "RawSocket.hpp"

class ICMPController
{
Expand All @@ -33,7 +24,6 @@ class ICMPController

private:
IPAddress recv_echo(uint16_t id, uint16_t ttl);
std::tuple<iphdr *, icmphdr *, uint8_t *> take_headers(uint8_t * ptr);
};

#endif
8 changes: 1 addition & 7 deletions include/ICMPReceiver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@
#define ICMP_RECEIVER_HPP_

#include <cstdlib>
#include <cerrno>
#include <cstring>
#include <algorithm>
#include <string>
#include <tuple>
#include <vector>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include "IPAddress.hpp"
#include "RawSocket.hpp"
Expand All @@ -21,7 +15,7 @@ class ICMPReceiver
sockaddr_in sender_address;

public:
explicit ICMPReceiver(RawSocket & s) : socket{s}
explicit ICMPReceiver(RawSocket & s) : socket{s}, sender_address{}
{
}

Expand Down
11 changes: 2 additions & 9 deletions include/ICMPSender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
#define ICMP_SENDER_HPP_

#include <cstdlib>
#include <cerrno>
#include <cstring>
#include <string>
#include <cinttypes>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include "IPAddress.hpp"
#include "RawSocket.hpp"
Expand All @@ -18,16 +15,12 @@ class ICMPSender
sockaddr_in receiver_address;

public:
explicit ICMPSender(RawSocket & s) : socket{s}
explicit ICMPSender(RawSocket & s) : socket{s}, receiver_address{}
{
}

void send(const void * msg_buf, int msg_size, uint16_t ttl);
void set_receiver(const IPAddress & addr);
icmphdr prepare_icmp(uint16_t id, uint16_t seq);

private:
uint16_t count_checksum(const uint16_t * hdr, int length);
};

#endif
13 changes: 6 additions & 7 deletions include/IPAddress.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@
#define IP_ADDRESS_HPP_

#include <cstdlib>
#include <exception>
#include <cinttypes>
#include <iostream>
#include <stdexcept>
#include <algorithm>
#include <string>
#include <tuple>
#include <vector>
#include <numeric>

using addr_t = unsigned int;

class IPAddress
{
private:
using addr_t = uint32_t;

public:
explicit IPAddress(addr_t a) : address{a}
{
Expand All @@ -39,6 +36,8 @@ class IPAddress
explicit operator std::string() const;

private:
std::vector<unsigned int> quadruple() const;

addr_t address;
};

Expand Down
2 changes: 0 additions & 2 deletions include/RawSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <cstring>
#include <exception>
#include <stdexcept>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <unistd.h>

class SocketException : public std::logic_error
Expand Down
59 changes: 48 additions & 11 deletions src/ICMPController.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,56 @@
#include "ICMPController.hpp"
#include <cerrno>
#include <cstring>
#include <ctime>
#include <string>

uint16_t count_checksum(const uint16_t * hdr, int length)
{
if(length % 2 == 1)
throw SocketException("Incorrect length of ICMP header.");

uint32_t sum = 0;
const uint16_t * ptr = hdr;

for(int i = 0; i < length; i += 2)
{
sum += *ptr;
++ptr;
}

sum = (sum >> 16U) + (sum & 0xFFFFU);

return (uint16_t)(~(sum + (sum >> 16U)));
}

icmphdr prepare_icmp(uint16_t id, uint16_t seq)
{
icmphdr header = {};

header.type = ICMP_ECHO;
header.code = 0;
header.un.echo.id = id;
header.un.echo.sequence = seq;
header.checksum = 0;
header.checksum = count_checksum((uint16_t *)&header, sizeof(header));

return header;
}

std::tuple<iphdr *, icmphdr *, uint8_t *> take_headers(uint8_t * ptr)
{
auto * hIP = reinterpret_cast<iphdr *>(ptr);
auto * hICMP = reinterpret_cast<icmphdr *>(ptr + 4U * hIP->ihl);
uint8_t * rest = ptr + 4U * hIP->ihl + sizeof(icmphdr);

return std::make_tuple(hIP, hICMP, rest);
}

void ICMPController::echo_request(const IPAddress & addr, uint16_t id, uint16_t ttl)
{
for(int i = 0; i < 3; ++i)
{
icmphdr header = sender.prepare_icmp(id, 3 * ttl + i);
icmphdr header = prepare_icmp(id, 3 * ttl + i);

sender.set_receiver(addr);
sender.send(&header, sizeof(header), ttl);
Expand All @@ -16,7 +62,7 @@ std::tuple<std::set<IPAddress>, size_t, size_t> ICMPController::echo_reply(uint1
{
std::set<IPAddress> recv_addr;
fd_set fd;
timeval timer;
timeval timer = {};
size_t avg_time = 0;
size_t recv_num = 0;

Expand Down Expand Up @@ -75,12 +121,3 @@ IPAddress ICMPController::recv_echo(uint16_t id, uint16_t ttl)

return receiver.take_address();
}

std::tuple<iphdr *, icmphdr *, uint8_t *> ICMPController::take_headers(uint8_t * ptr)
{
iphdr * hIP = (iphdr *)ptr;
icmphdr * hICMP = (icmphdr *)(ptr + 4 * hIP->ihl);
uint8_t * rest = ptr + 4 * hIP->ihl + sizeof(icmphdr);

return std::make_tuple(hIP, hICMP, rest);
}
4 changes: 4 additions & 0 deletions src/ICMPReceiver.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include "ICMPReceiver.hpp"
#include <cerrno>
#include <cstring>
#include <algorithm>
#include <string>

std::vector<uint8_t> ICMPReceiver::receive()
{
Expand Down
Loading

0 comments on commit 7b7a319

Please sign in to comment.