forked from insomniacslk/dublin-traceroute
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored traceroute code into udpv4 probe class
- Loading branch information
1 parent
e47892e
commit 942a421
Showing
4 changed files
with
163 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* \file udpv4probe.h | ||
* \Author Andrea Barberio <[email protected]> | ||
* \date 2017 | ||
* \brief Definition of the UDPProbev4 class | ||
* | ||
* This file contains the definition of the UDPv4Probe class, which represents | ||
* an UDP probe that will be sent over IPv4. | ||
* | ||
* \sa udpv4probe.cc | ||
*/ | ||
|
||
#ifndef _UDPV4PROBE_H | ||
#define _UDPV4PROBE_H | ||
|
||
#include <tins/tins.h> | ||
|
||
using namespace Tins; | ||
|
||
|
||
class UDPv4Probe { | ||
private: | ||
IPv4Address local_addr_; | ||
IPv4Address remote_addr_; | ||
uint16_t local_port_; | ||
uint16_t remote_port_; | ||
uint8_t ttl_; | ||
public: | ||
const IPv4Address local_addr() const { return local_addr_; } | ||
const IPv4Address remote_addr() const { return remote_addr_; } | ||
const uint16_t local_port() const { return local_port_; }; | ||
const uint16_t remote_port() const { return remote_port_; }; | ||
|
||
UDPv4Probe( | ||
IPv4Address remote_addr, | ||
uint16_t remote_port, | ||
uint16_t local_port, | ||
uint8_t ttl, | ||
IPv4Address local_addr = 0): | ||
remote_addr_(remote_addr), | ||
remote_port_(remote_port), | ||
local_port_(local_port), | ||
ttl_(ttl), | ||
local_addr_(local_addr) { }; | ||
IP& send(); | ||
}; | ||
|
||
#endif /* _UDPV4PROBE_H */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* \file udpv4probe.cc | ||
* \Author Andrea Barberio <[email protected]> | ||
* \date 2017 | ||
* \brief Definition of the UDPv4Probe class | ||
* | ||
* This file contains the definition of the UDPv4Probe class, which represents | ||
* an UDP probe that will be sent over IPv4. | ||
* | ||
* \sa udpv4probe.h | ||
*/ | ||
|
||
#include <memory> | ||
#include <netinet/in.h> | ||
#include <arpa/inet.h> | ||
#include <netdb.h> | ||
#include <sys/socket.h> | ||
#include <iostream> | ||
#include <iomanip> | ||
|
||
#include "dublintraceroute/udpv4probe.h" | ||
#include "dublintraceroute/common.h" | ||
#include "dublintraceroute/exceptions.h" | ||
#include "dublintraceroute/icmp_messages.h" | ||
|
||
|
||
/** \brief method that sends the probe to the specified destination | ||
*/ | ||
IP& UDPv4Probe::send() { | ||
/* The payload is used to manipulate the UDP checksum, that will be | ||
* used as hop identifier. | ||
* The last two bytes will be adjusted to influence the hop identifier, | ||
* which for UDP traceroutes is the UDP checksum. | ||
*/ | ||
unsigned char payload[] = {'N', 'S', 'M', 'N', 'C', 0x00, 0x00}; | ||
|
||
/* The identifier is used to identify and match a response packet to | ||
* the corresponding sent packet | ||
*/ | ||
uint16_t identifier = remote_port_ + ttl_; | ||
|
||
payload[5] = ((unsigned char *)&identifier)[0]; | ||
payload[6] = ((unsigned char *)&identifier)[1]; | ||
IP *packet = new IP(remote_addr_, local_addr_) / | ||
UDP(remote_port_, local_port_) / | ||
RawPDU((char *)payload); | ||
packet->ttl(ttl_); | ||
packet->flags(IP::DONT_FRAGMENT); | ||
|
||
// serialize the packet so we can extract source IP and checksum | ||
packet->serialize(); | ||
|
||
packet->id(packet->rfind_pdu<UDP>().checksum()); | ||
|
||
NetworkInterface iface = NetworkInterface::default_interface(); | ||
PacketSender sender; | ||
sender.send(*packet, iface.name()); | ||
return *packet; | ||
} | ||
|