mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Modified some examples fixed some doxygen documentation.
This commit is contained in:
@@ -54,7 +54,7 @@ typedef std::pair<Sniffer*, std::string> sniffer_data;
|
||||
* the scanned port's status.
|
||||
*/
|
||||
bool handler(PDU &pdu) {
|
||||
TCP &tcp = pdu.rfind_pdu<TCP>();
|
||||
const TCP &tcp = pdu.rfind_pdu<TCP>();
|
||||
// Ok, it's a TCP PDU. Is RST flag on? Then port is closed.
|
||||
if(tcp.get_flag(TCP::RST)) {
|
||||
// This indicates we should stop sniffing.
|
||||
@@ -63,7 +63,7 @@ bool handler(PDU &pdu) {
|
||||
cout << "Port: " << setw(5) << tcp.sport() << " closed\n";
|
||||
}
|
||||
// Is SYN flag on? Then port is open!
|
||||
else if(tcp.get_flag(TCP::SYN) && tcp.get_flag(TCP::ACK))
|
||||
else if(tcp.flags() == (TCP::SYN | TCP::ACK))
|
||||
cout << "Port: " << setw(5) << tcp.sport() << " open\n";
|
||||
return true;
|
||||
}
|
||||
@@ -79,7 +79,7 @@ void send_syns(const NetworkInterface &iface, IPv4Address dest_ip, const vector<
|
||||
TCP &tcp = ip.rfind_pdu<TCP>();
|
||||
// Set the SYN flag on.
|
||||
tcp.set_flag(TCP::SYN, 1);
|
||||
// Just some arbitrary port.
|
||||
// Just some random port.
|
||||
tcp.sport(1337);
|
||||
cout << "Sending SYNs..." << endl;
|
||||
for(vector<string>::const_iterator it = ips.begin(); it != ips.end(); ++it) {
|
||||
|
||||
@@ -54,7 +54,11 @@ public:
|
||||
|
||||
PacketSender sender;
|
||||
// Create our handler
|
||||
auto handler = make_sniffer_handler(this, &Traceroute::sniff_callback);
|
||||
auto handler = std::bind(
|
||||
&Traceroute::sniff_callback,
|
||||
this,
|
||||
std::placeholders::_1
|
||||
);
|
||||
// We're running
|
||||
running = true;
|
||||
// Start the sniff thread
|
||||
@@ -98,10 +102,10 @@ private:
|
||||
}
|
||||
|
||||
bool sniff_callback(PDU &pdu) {
|
||||
IP &ip = pdu.rfind_pdu<IP>();
|
||||
const IP &ip = pdu.rfind_pdu<IP>();
|
||||
ttl_map::const_iterator iter;
|
||||
// Fetch the IP PDU attached to the ICMP response
|
||||
IP inner_ip = pdu.rfind_pdu<RawPDU>().to<IP>();
|
||||
const IP inner_ip = pdu.rfind_pdu<RawPDU>().to<IP>();
|
||||
// Critical section
|
||||
{
|
||||
std::lock_guard<std::mutex> _(lock);
|
||||
|
||||
18
include/ip.h
18
include/ip.h
@@ -333,13 +333,6 @@ namespace Tins {
|
||||
|
||||
/* Setters */
|
||||
|
||||
/**
|
||||
* \brief Setter for the header length field.
|
||||
*
|
||||
* \param new_head_len The new header length.
|
||||
*/
|
||||
void head_len(small_uint<4> new_head_len);
|
||||
|
||||
/**
|
||||
* \brief Setter for the type of service field.
|
||||
*
|
||||
@@ -347,13 +340,6 @@ namespace Tins {
|
||||
*/
|
||||
void tos(uint8_t new_tos);
|
||||
|
||||
/**
|
||||
* \brief Setter for the total length field.
|
||||
*
|
||||
* \param new_tot_len The new total length.
|
||||
*/
|
||||
void tot_len(uint16_t new_tot_len);
|
||||
|
||||
/**
|
||||
* \brief Setter for the id field.
|
||||
*
|
||||
@@ -618,6 +604,10 @@ namespace Tins {
|
||||
/*The options start here. */
|
||||
} TINS_END_PACK;
|
||||
|
||||
|
||||
void head_len(small_uint<4> new_head_len);
|
||||
void tot_len(uint16_t new_tot_len);
|
||||
|
||||
void prepare_for_serialize(const PDU *parent);
|
||||
void internal_add_option(const option &option);
|
||||
void init_ip_fields();
|
||||
|
||||
@@ -76,6 +76,7 @@ namespace Tins {
|
||||
/**
|
||||
* \brief Constructor for PacketSender objects.
|
||||
*
|
||||
* \param iface The default interface in which to send the packets.
|
||||
* \param recv_timeout The timeout which will be used when receiving responses.
|
||||
*/
|
||||
PacketSender(const NetworkInterface &iface = NetworkInterface(),
|
||||
@@ -168,7 +169,7 @@ namespace Tins {
|
||||
*
|
||||
* \sa PacketSender::default_interface
|
||||
*/
|
||||
const NetworkInterface& default_interface();
|
||||
const NetworkInterface& default_interface() const;
|
||||
|
||||
/**
|
||||
* \brief Sends a PDU.
|
||||
|
||||
@@ -118,7 +118,8 @@ namespace Tins {
|
||||
DOT1Q,
|
||||
PPPOE,
|
||||
STP,
|
||||
PPI
|
||||
PPI,
|
||||
USER_DEFINED_PDU = 1000
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -103,7 +103,7 @@ namespace Tins {
|
||||
/**
|
||||
* \brief Compiles a filter and uses it to capture one packet.
|
||||
*
|
||||
* This method returns the first sniffed packet that matches the
|
||||
* This method returns the first valid sniffed packet that matches the
|
||||
* sniffer's filter, or the first sniffed packet if no filter has
|
||||
* been set.
|
||||
*
|
||||
@@ -123,39 +123,36 @@ namespace Tins {
|
||||
* \code
|
||||
* // bad!!
|
||||
* PtrPacket p = s.next_packet();
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* Is not, since PtrPacket can't be copy constructed.
|
||||
*
|
||||
* \sa Packet::release_pdu
|
||||
*
|
||||
* \return The captured packet, matching the given filter.
|
||||
* If an error occured(probably compiling the filter), PtrPacket::pdu
|
||||
* will return 0. Caller takes ownership of the PDU * stored in
|
||||
* \return A captured packet. If an error occured, PtrPacket::pdu
|
||||
* will return 0. Caller takes ownership of the PDU pointer stored in
|
||||
* the PtrPacket.
|
||||
*/
|
||||
PtrPacket next_packet();
|
||||
|
||||
/**
|
||||
* \brief Starts a sniffing loop, using a callback object for every
|
||||
* \brief Starts a sniffing loop, using a callback functor for every
|
||||
* sniffed packet.
|
||||
*
|
||||
* The callback object must implement an operator with one of the
|
||||
* The functor must implement an operator with one of the
|
||||
* following signatures:
|
||||
*
|
||||
* \code
|
||||
* bool operator()(PDU&);
|
||||
* bool operator()(const PDU&);
|
||||
* bool(PDU&);
|
||||
* bool(const PDU&);
|
||||
* \endcode
|
||||
*
|
||||
* This operator will be called using the sniffed packets
|
||||
* as arguments. You can modify the parameter argument as you wish.
|
||||
* Calling PDU methods like PDU::release_inner_pdu is perfectly
|
||||
* valid.
|
||||
* This functor will be called using the each of the sniffed packets
|
||||
* as its argument. Using PDU member functions that modify the PDU,
|
||||
* such as PDU::release_inner_pdu, is perfectly valid.
|
||||
*
|
||||
* Note that the Functor object will be copied using its copy
|
||||
* constructor, so that object should be some kind of proxy to
|
||||
* Note that if you're using a functor object, it will be copied using
|
||||
* its copy constructor, so it should be some kind of proxy to
|
||||
* another object which will process the packets(e.g. std::bind).
|
||||
*
|
||||
* Sniffing will stop when either max_packets are sniffed(if it is != 0),
|
||||
|
||||
@@ -201,14 +201,6 @@ namespace Tins {
|
||||
*/
|
||||
const options_type &options() const { return _options; }
|
||||
|
||||
/**
|
||||
* \brief Gets the value of a flag.
|
||||
*
|
||||
* \param tcp_flag The polled flag.
|
||||
* \return The value of the flag.
|
||||
*/
|
||||
small_uint<1> get_flag(Flags tcp_flag);
|
||||
|
||||
/**
|
||||
* \brief Gets the value of a flag.
|
||||
*
|
||||
|
||||
@@ -159,14 +159,13 @@ void Dot11ManagementFrame::power_capability(uint8_t min_power, uint8_t max_power
|
||||
}
|
||||
|
||||
void Dot11ManagementFrame::supported_channels(const channels_type &new_channels) {
|
||||
uint8_t* buffer = new uint8_t[new_channels.size() * 2];
|
||||
uint8_t* ptr = buffer;
|
||||
std::vector<uint8_t> buffer(new_channels.size() * 2);
|
||||
uint8_t* ptr = &buffer[0];
|
||||
for(channels_type::const_iterator it = new_channels.begin(); it != new_channels.end(); ++it) {
|
||||
*(ptr++) = it->first;
|
||||
*(ptr++) = it->second;
|
||||
}
|
||||
add_tagged_option(SUPPORTED_CHANNELS, new_channels.size() * 2, buffer);
|
||||
delete[] buffer;
|
||||
add_tagged_option(SUPPORTED_CHANNELS, buffer.size(), &buffer[0]);
|
||||
}
|
||||
|
||||
void Dot11ManagementFrame::edca_parameter_set(uint32_t ac_be, uint32_t ac_bk, uint32_t ac_vi, uint32_t ac_vo) {
|
||||
@@ -182,16 +181,13 @@ void Dot11ManagementFrame::edca_parameter_set(uint32_t ac_be, uint32_t ac_bk, ui
|
||||
}
|
||||
|
||||
void Dot11ManagementFrame::request_information(const request_info_type elements) {
|
||||
uint8_t *buffer = new uint8_t[elements.size()], *ptr = buffer;
|
||||
for (request_info_type::const_iterator it = elements.begin(); it != elements.end(); ++it)
|
||||
*(ptr++) = *it;
|
||||
add_tagged_option(REQUEST_INFORMATION, elements.size(), buffer);
|
||||
delete[] buffer;
|
||||
add_tagged_option(REQUEST_INFORMATION, elements.size(), &elements[0]);
|
||||
}
|
||||
|
||||
void Dot11ManagementFrame::fh_parameter_set(const fh_params_set &fh_params) {
|
||||
uint8_t data[5];
|
||||
*(uint16_t*)data = Endian::host_to_le(fh_params.dwell_time);
|
||||
uint16_t dwell = Endian::host_to_le(fh_params.dwell_time);
|
||||
std::memcpy(data, &dwell, sizeof(dwell));
|
||||
data[2] = fh_params.hop_set;
|
||||
data[3] = fh_params.hop_pattern;
|
||||
data[4] = fh_params.hop_index;
|
||||
@@ -207,12 +203,10 @@ void Dot11ManagementFrame::cf_parameter_set(const cf_params_set ¶ms) {
|
||||
uint8_t data[6];
|
||||
data[0] = params.cfp_count;
|
||||
data[1] = params.cfp_period;
|
||||
*(uint16_t*)&data[2] = Endian::host_to_le(params.cfp_max_duration);
|
||||
*(uint16_t*)&data[4] = Endian::host_to_le(params.cfp_dur_remaining);
|
||||
/*params.cfp_count = params.cfp_count;
|
||||
params.cfp_period = params.cfp_period;
|
||||
params.cfp_max_duration = Endian::host_to_le(params.cfp_max_duration);
|
||||
params.cfp_dur_remaining = Endian::host_to_le(params.cfp_dur_remaining);*/
|
||||
uint16_t dummy = Endian::host_to_le(params.cfp_max_duration);
|
||||
std::memcpy(data + 2, &dummy, sizeof(uint16_t));
|
||||
dummy = Endian::host_to_le(params.cfp_dur_remaining);
|
||||
std::memcpy(data + 4, &dummy, sizeof(uint16_t));
|
||||
add_tagged_option(CF_SET, sizeof(data), data);
|
||||
}
|
||||
|
||||
|
||||
@@ -140,8 +140,7 @@ bool EthernetII::matches_response(const uint8_t *ptr, uint32_t total_sz) const {
|
||||
const size_t addr_sz = address_type::address_size;
|
||||
const ethhdr *eth_ptr = (const ethhdr*)ptr;
|
||||
if(std::equal(_eth.src_mac, _eth.src_mac + addr_sz, eth_ptr->dst_mac)) {
|
||||
if(std::equal(_eth.src_mac, _eth.src_mac + addr_sz, eth_ptr->dst_mac) || dst_addr() == BROADCAST ||
|
||||
(_eth.src_mac[0] == 0x33 && _eth.src_mac[1] == 0x33))
|
||||
if(std::equal(_eth.src_mac, _eth.src_mac + addr_sz, eth_ptr->dst_mac) || !dst_addr().is_unicast())
|
||||
{
|
||||
return (inner_pdu()) ? inner_pdu()->matches_response(ptr + sizeof(_eth), total_sz - sizeof(_eth)) : true;
|
||||
}
|
||||
|
||||
@@ -447,8 +447,8 @@ bool IP::matches_response(const uint8_t *ptr, uint32_t total_sz) const {
|
||||
return false;
|
||||
const iphdr *ip_ptr = (const iphdr*)ptr;
|
||||
// checks for broadcast addr
|
||||
if((_ip.saddr == ip_ptr->daddr && (_ip.daddr == ip_ptr->saddr || _ip.daddr == 0xffffffff)) ||
|
||||
(_ip.daddr == 0xffffffff && _ip.saddr == 0)) {
|
||||
if((_ip.saddr == ip_ptr->daddr && (_ip.daddr == ip_ptr->saddr || dst_addr().is_broadcast())) ||
|
||||
(dst_addr().is_broadcast() && _ip.saddr == 0)) {
|
||||
uint32_t sz = std::min<uint32_t>(_ip.ihl * sizeof(uint32_t), total_sz);
|
||||
return inner_pdu() ? inner_pdu()->matches_response(ptr + sz, total_sz - sz) : true;
|
||||
}
|
||||
|
||||
@@ -36,6 +36,8 @@
|
||||
using std::string;
|
||||
|
||||
namespace Tins{
|
||||
const IPv4Address IPv4Address::broadcast("255.255.255.255");
|
||||
|
||||
const AddressRange<IPv4Address> private_ranges[] = {
|
||||
IPv4Address("192.168.0.0") / 16,
|
||||
IPv4Address("10.0.0.0") / 8,
|
||||
@@ -122,4 +124,12 @@ bool IPv4Address::is_loopback() const {
|
||||
bool IPv4Address::is_multicast() const {
|
||||
return multicast_range.contains(*this);
|
||||
}
|
||||
|
||||
bool IPv4Address::is_unicast() const {
|
||||
return !is_multicast() && !is_broadcast();
|
||||
}
|
||||
|
||||
bool IPv4Address::is_broadcast() const {
|
||||
return *this == broadcast;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ void PacketSender::default_interface(const NetworkInterface &iface) {
|
||||
default_iface = iface;
|
||||
}
|
||||
|
||||
const NetworkInterface& PacketSender::default_interface() {
|
||||
const NetworkInterface& PacketSender::default_interface() const {
|
||||
return default_iface;
|
||||
}
|
||||
|
||||
|
||||
@@ -197,10 +197,6 @@ TCP::AltChecksums TCP::altchecksum() const {
|
||||
return static_cast<AltChecksums>(generic_search<uint8_t>(ALTCHK));
|
||||
}
|
||||
|
||||
small_uint<1> TCP::get_flag(Flags tcp_flag) {
|
||||
return static_cast<const TCP&>(*this).get_flag(tcp_flag);
|
||||
}
|
||||
|
||||
small_uint<1> TCP::get_flag(Flags tcp_flag) const {
|
||||
switch(tcp_flag) {
|
||||
case FIN:
|
||||
|
||||
@@ -59,7 +59,7 @@ public:
|
||||
std::vector<uint8_t> buffer;
|
||||
};
|
||||
|
||||
const PDU::PDUType DummyPDU::pdu_flag = static_cast<PDU::PDUType>(0xefff);
|
||||
const PDU::PDUType DummyPDU::pdu_flag = USER_DEFINED_PDU;
|
||||
|
||||
TEST_F(AllocatorsTest, LinkLayerPDUs) {
|
||||
Allocators::register_allocator<EthernetII, DummyPDU>(1638);
|
||||
|
||||
@@ -71,6 +71,12 @@ TEST_F(HWAddressTest, IsBroadcast) {
|
||||
EXPECT_TRUE(HWAddress<6>("ff:ff:ff:ff:ff:ff").is_broadcast());
|
||||
}
|
||||
|
||||
TEST_F(HWAddressTest, IsUnicast) {
|
||||
EXPECT_FALSE(HWAddress<6>("ff:ff:ff:ff:ff:ff").is_unicast());
|
||||
EXPECT_FALSE(HWAddress<6>("03:02:03:04:05:06").is_unicast());
|
||||
EXPECT_TRUE(HWAddress<6>("de:ad:be:ef:00:00").is_unicast());
|
||||
}
|
||||
|
||||
TEST_F(HWAddressTest, IsMulticast) {
|
||||
EXPECT_TRUE(HWAddress<6>("01:02:03:04:05:06").is_multicast());
|
||||
EXPECT_TRUE(HWAddress<6>("09:02:03:04:05:06").is_multicast());
|
||||
|
||||
@@ -63,24 +63,12 @@ TEST_F(IPTest, Constructor) {
|
||||
EXPECT_EQ(ip.id(), 1);
|
||||
}
|
||||
|
||||
TEST_F(IPTest, HeadLen) {
|
||||
IP ip;
|
||||
ip.head_len(14);
|
||||
EXPECT_EQ(ip.head_len(), 14);
|
||||
}
|
||||
|
||||
TEST_F(IPTest, TOS) {
|
||||
IP ip;
|
||||
ip.tos(0x7a);
|
||||
EXPECT_EQ(ip.tos(), 0x7a);
|
||||
}
|
||||
|
||||
TEST_F(IPTest, TotLen) {
|
||||
IP ip;
|
||||
ip.tot_len(0x7f1a);
|
||||
EXPECT_EQ(ip.tot_len(), 0x7f1a);
|
||||
}
|
||||
|
||||
TEST_F(IPTest, ID) {
|
||||
IP ip;
|
||||
ip.id(0x7f1a);
|
||||
|
||||
@@ -88,3 +88,16 @@ TEST(IPAddressTest, IsMulticast) {
|
||||
EXPECT_FALSE(IPv4Address("223.255.255.255").is_multicast());
|
||||
EXPECT_FALSE(IPv4Address("240.0.0.0").is_multicast());
|
||||
}
|
||||
|
||||
TEST(IPAddressTest, IsBroadcast) {
|
||||
EXPECT_TRUE(IPv4Address("255.255.255.255").is_broadcast());
|
||||
EXPECT_FALSE(IPv4Address("226.3.54.132").is_broadcast());
|
||||
EXPECT_FALSE(IPv4Address("127.0.0.1").is_broadcast());
|
||||
}
|
||||
|
||||
TEST(IPAddressTest, IsUnicast) {
|
||||
EXPECT_FALSE(IPv4Address("255.255.255.255").is_unicast());
|
||||
EXPECT_FALSE(IPv4Address("224.0.0.1").is_unicast());
|
||||
EXPECT_TRUE(IPv4Address("240.0.0.0").is_unicast());
|
||||
EXPECT_TRUE(IPv4Address("127.0.0.1").is_unicast());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user