Skip to content

Commit 6b6be67

Browse files
authored
Merge pull request #16 from insomniacslk/min_ttl
Added support for --min-ttl
2 parents 97c145f + 3ea7077 commit 6b6be67

7 files changed

Lines changed: 46 additions & 12 deletions

File tree

documentation/readme/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ Usage:
227227
dublin-traceroute <target> [--sport=SRC_PORT]
228228
[--dport=dest_base_port]
229229
[--npaths=num_paths]
230+
[--min-ttl=min_ttl]
230231
[--max-ttl=max_ttl]
231232
[--broken-nat]
232233
[--help]
@@ -238,7 +239,8 @@ Options:
238239
-s SRC_PORT --sport=SRC_PORT the source port to send packets from
239240
-d DST_PORT --dport=DST_PORT the base destination port to send packets to
240241
-n NPATHS --npaths=NPATHS the number of paths to probe
241-
-t MAX_TTL --max-ttl=MAX_TTL the maximum TTL to probe
242+
-t MIN_TTL --min-ttl=MIN_TTL the MINIMUM TTL to probe. Must be greater or equal than the minimum TTL
243+
-T MAX_TTL --max-ttl=MAX_TTL the maximum TTL to probe
242244
-b --broken-nat the network has a broken NAT configuration (e.g. no payload fixup). Try this if you see less hops than expected
243245

244246

documentation/readme/TODO.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* break on destination unreachable
1616
* improve documentation
1717
* put everything under a namespace
18-
* implement command line parser in main.cc
18+
* ~~implement command line parser in main.cc~~ done in [commit 8a3ae75](https://github.com/insomniacslk/dublin-traceroute/commit/8a3ae7513645afdad5eabd8d6f368383dff98c8b)
1919
* Add an uninstall target in the Makefile for the python extension
2020
* Fix the memory leak where TracerouteResults is not freed
2121
* IP_ID_MATCHING must become a constructor parameter
@@ -30,3 +30,4 @@
3030
* Integrate the ASN graph with a world map (e.g. openstreetmap or google maps)
3131
* Add --webserver to the python CLI to expose a SimpleHTTPServer that serves a PNG with the traceroute diagram
3232
* heat map/flame graph of the network latencies over time (links history)
33+
* ~~improve the build system (there is just a static Makefile now)~~ done in [commit ffa9d3c](https://github.com/insomniacslk/dublin-traceroute/commit/ffa9d3c306fb772e2c95963a94cdc386b0126206), using CMake

include/dublintraceroute/dublin_traceroute.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class DublinTraceroute {
4545
const std::string dst_;
4646
IPv4Address target_;
4747
const uint8_t npaths_,
48+
min_ttl_,
4849
max_ttl_;
4950
const bool broken_nat_;
5051
std::mutex mutex_tracerouting,
@@ -56,20 +57,23 @@ class DublinTraceroute {
5657
static const uint16_t default_srcport = 12345;
5758
static const uint16_t default_dstport = 33434;
5859
static const uint8_t default_npaths = 20;
60+
static const uint8_t default_min_ttl = 1;
5961
static const uint8_t default_max_ttl = 30;
6062
static const bool default_broken_nat = false;
6163
DublinTraceroute(
6264
const std::string &dst,
6365
const uint16_t srcport = default_srcport,
6466
const uint16_t dstport = default_dstport,
6567
const uint8_t npaths = default_npaths,
68+
const uint8_t min_ttl = default_min_ttl,
6669
const uint8_t max_ttl = default_max_ttl,
6770
const bool broken_nat = default_broken_nat
6871
):
6972
srcport_(srcport),
7073
dstport_(dstport),
7174
dst_(dst),
7275
npaths_(npaths),
76+
min_ttl_(min_ttl),
7377
max_ttl_(max_ttl),
7478
broken_nat_(broken_nat)
7579
{ }
@@ -78,13 +82,15 @@ class DublinTraceroute {
7882
const uint16_t srcport = default_srcport,
7983
const uint16_t dstport = default_dstport,
8084
const uint8_t npaths = default_npaths,
85+
const uint8_t min_ttl = default_min_ttl,
8186
const uint8_t max_ttl = default_max_ttl,
8287
const bool broken_nat = default_broken_nat
8388
):
8489
srcport_(srcport),
8590
dstport_(dstport),
8691
dst_(std::string(dst)),
8792
npaths_(npaths),
93+
min_ttl_(min_ttl),
8894
max_ttl_(max_ttl),
8995
broken_nat_(broken_nat)
9096
{ }
@@ -94,13 +100,15 @@ class DublinTraceroute {
94100
dstport_(source.dstport_),
95101
dst_(source.dst_),
96102
npaths_(source.npaths_),
103+
min_ttl_(source.min_ttl_),
97104
max_ttl_(source.max_ttl_),
98105
broken_nat_(source.broken_nat_)
99106
{ }
100107

101108
inline const uint16_t srcport() const { return srcport_; }
102109
inline const uint16_t dstport() const { return dstport_; }
103110
inline const uint8_t npaths() const { return npaths_; }
111+
inline const uint8_t min_ttl() const { return min_ttl_; }
104112
inline const uint8_t max_ttl() const { return max_ttl_; }
105113
inline const bool broken_nat() const { return broken_nat_; }
106114
inline const std::string &dst() const { return dst_; }

include/dublintraceroute/traceroute_results.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
class TracerouteResults {
2424
private:
2525
std::shared_ptr<flow_map_t> flows_;
26+
uint8_t min_ttl = 1;
2627
bool compressed_;
2728
bool broken_nat_;
2829

2930
public:
30-
TracerouteResults(std::shared_ptr<flow_map_t> flows, const bool broken_nat /* = false */);
31+
TracerouteResults(std::shared_ptr<flow_map_t> flows, const uint8_t min_ttl /* = 1 */, const bool broken_nat /* = false */);
3132
~TracerouteResults() { };
3233
inline flow_map_t &flows() { return *flows_; }
3334
std::shared_ptr<IP> match_packet(const Packet &packet);

src/dublin_traceroute.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ std::shared_ptr<flow_map_t> DublinTraceroute::generate_per_flow_packets() {
6969
target(IPv4Address(dst()));
7070
}
7171

72+
// check for valid min and max TTL
73+
if (min_ttl_ > max_ttl_) {
74+
throw std::invalid_argument("max_ttl must be greater or equal than min_ttl");
75+
}
76+
7277
// forge the packets to send
7378
for (uint16_t dport = dstport(); dport < dstport() + npaths(); dport++) {
7479
hops_t hops(new std::vector<Hop>());
@@ -86,7 +91,7 @@ std::shared_ptr<flow_map_t> DublinTraceroute::generate_per_flow_packets() {
8691
* UDP.sport
8792
* UDP.dport
8893
*/
89-
for (uint8_t ttl = 1; ttl <= max_ttl_; ttl++) {
94+
for (uint8_t ttl = min_ttl_; ttl <= max_ttl_; ttl++) {
9095
/*
9196
* Adjust the payload for each flow to obtain the same UDP
9297
* checksum. The UDP checksum is used to identify the flow.
@@ -170,7 +175,7 @@ TracerouteResults &DublinTraceroute::traceroute() {
170175
}
171176
std::shared_ptr<Sniffer> sniffer(_sniffer);
172177

173-
TracerouteResults *results = new TracerouteResults(flows, broken_nat());
178+
TracerouteResults *results = new TracerouteResults(flows, min_ttl_, broken_nat());
174179

175180
// configure the sniffing handler
176181
auto handler = std::bind(

src/main.cc

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515

1616
#include <dublintraceroute/dublin_traceroute.h>
1717

18-
const char *shortopts = "hvs:d:n:t:b";
18+
const char *shortopts = "hvs:d:n:t:T:b";
1919
const struct option longopts[] = {
2020
{"help", no_argument, NULL, 'h'},
2121
{"version", no_argument, NULL, 'v'},
2222
{"sport", required_argument, NULL, 's'},
2323
{"dport", required_argument, NULL, 'd'},
2424
{"npaths", required_argument, NULL, 'n'},
25-
{"max-ttl", required_argument, NULL, 't'},
25+
{"min-ttl", required_argument, NULL, 't'},
26+
{"max-ttl", required_argument, NULL, 'T'},
2627
{"broken-nat", no_argument, NULL, 'b'},
2728
{NULL, 0, NULL, 0},
2829
};
@@ -36,6 +37,7 @@ Written by Andrea Barberio - https://insomniac.slackware.it
3637
dublin-traceroute <target> [--sport=SRC_PORT]
3738
[--dport=dest_base_port]
3839
[--npaths=num_paths]
40+
[--min-ttl=min_ttl]
3941
[--max-ttl=max_ttl]
4042
[--broken-nat]
4143
[--help]
@@ -47,7 +49,8 @@ Written by Andrea Barberio - https://insomniac.slackware.it
4749
-s SRC_PORT --sport=SRC_PORT the source port to send packets from
4850
-d DST_PORT --dport=DST_PORT the base destination port to send packets to
4951
-n NPATHS --npaths=NPATHS the number of paths to probe
50-
-t MAX_TTL --max-ttl=MAX_TTL the maximum TTL to probe
52+
-t MIN_TTL --min-ttl=MIN_TTL the minimum TTL to probe
53+
-T MAX_TTL --max-ttl=MAX_TTL the maximum TTL to probe. Must be greater or equal than the minimum TTL
5154
-b --broken-nat the network has a broken NAT configuration (e.g. no payload fixup). Try this if you see less hops than expected
5255
5356
@@ -64,6 +67,7 @@ main(int argc, char **argv) {
6467
long sport = DublinTraceroute::default_srcport;
6568
long dport = DublinTraceroute::default_dstport;
6669
long npaths = DublinTraceroute::default_npaths;
70+
long min_ttl = DublinTraceroute::default_min_ttl;
6771
long max_ttl = DublinTraceroute::default_max_ttl;
6872
bool broken_nat = DublinTraceroute::default_broken_nat;
6973

@@ -102,6 +106,9 @@ main(int argc, char **argv) {
102106
TO_LONG(npaths, optarg);
103107
break;
104108
case 't':
109+
TO_LONG(min_ttl, optarg);
110+
break;
111+
case 'T':
105112
TO_LONG(max_ttl, optarg);
106113
break;
107114
case 'b':
@@ -136,10 +143,18 @@ main(int argc, char **argv) {
136143
std::cerr << "Number of paths must be between 1 and 65535" << std::endl;
137144
std::exit(EXIT_FAILURE);
138145
}
146+
if (min_ttl < 1 || min_ttl > 255) {
147+
std::cerr << "Min TTL must be between 1 and 255" << std::endl;
148+
std::exit(EXIT_FAILURE);
149+
}
139150
if (max_ttl < 1 || max_ttl > 255) {
140151
std::cerr << "Max TTL must be between 1 and 255" << std::endl;
141152
std::exit(EXIT_FAILURE);
142153
}
154+
if (min_ttl > max_ttl) {
155+
std::cerr << "Min TTL must be smaller or equal than max TTL" << std::endl;
156+
std::exit(EXIT_FAILURE);
157+
}
143158
if (dport + npaths - 1 > 65535) {
144159
std::cerr << "Destination port + number of paths must not exceed 65535" << std::endl;
145160
std::exit(EXIT_FAILURE);
@@ -152,6 +167,7 @@ main(int argc, char **argv) {
152167
sport,
153168
dport,
154169
npaths,
170+
min_ttl,
155171
max_ttl,
156172
broken_nat
157173
);
@@ -160,6 +176,7 @@ main(int argc, char **argv) {
160176
<< " to " << Dublin.dst()
161177
<< ":" << Dublin.dstport() << "~" << (Dublin.dstport() + npaths - 1)
162178
<< " (probing " << npaths << " path" << (npaths == 1 ? "" : "s")
179+
<< ", min TTL is " << min_ttl
163180
<< ", max TTL is " << max_ttl << ")"
164181
<< std::endl;
165182

src/traceroute_results.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
#include "dublintraceroute/icmp_messages.h"
2222

2323

24-
TracerouteResults::TracerouteResults(std::shared_ptr<flow_map_t> flows, const bool broken_nat = true):
25-
flows_(flows), compressed_(false), broken_nat_(broken_nat) {
24+
TracerouteResults::TracerouteResults(std::shared_ptr<flow_map_t> flows, const uint8_t min_ttl = 1, const bool broken_nat = true):
25+
flows_(flows), min_ttl(min_ttl), compressed_(false), broken_nat_(broken_nat) {
2626
}
2727

2828

@@ -107,7 +107,7 @@ void TracerouteResults::show(std::ostream &stream) {
107107
icmpmessages icmpm;
108108

109109
for (auto &iter: flows()) {
110-
unsigned int hopnum = 1;
110+
unsigned int hopnum = min_ttl;
111111
unsigned int index = 0;
112112
uint16_t prev_nat_id = 0;
113113
stream << "== Flow ID " << iter.first << " ==" << std::endl;
@@ -117,7 +117,7 @@ void TracerouteResults::show(std::ostream &stream) {
117117
stream << "*" << std::endl;
118118
} else {
119119
// print the IP address of the hop
120-
stream << hop.received()->src_addr() << " (" << *hop.name() << ")";
120+
stream << hop.received()->src_addr() << " (" << *hop.name() << ")";
121121

122122
// print the response IP ID, useful to detect
123123
// loops due to NATs, fake hops, etc

0 commit comments

Comments
 (0)