From 01b2a9c7b288e83dd8d71e59f0e40ef7bf0157a9 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sun, 6 Oct 2013 23:00:20 -0300 Subject: [PATCH] Modified some examples fixed some doxygen documentation. --- examples/portscan.cpp | 6 +++--- examples/traceroute.cpp | 10 +++++++--- include/ip.h | 18 ++++-------------- include/packet_sender.h | 3 ++- include/pdu.h | 3 ++- include/sniffer.h | 27 ++++++++++++--------------- include/tcp.h | 8 -------- src/dot11/dot11_mgmt.cpp | 26 ++++++++++---------------- src/ethernetII.cpp | 3 +-- src/ip.cpp | 4 ++-- src/ip_address.cpp | 10 ++++++++++ src/packet_sender.cpp | 2 +- src/tcp.cpp | 4 ---- tests/src/allocators.cpp | 2 +- tests/src/hwaddress.cpp | 6 ++++++ tests/src/ip.cpp | 12 ------------ tests/src/ipaddress.cpp | 13 +++++++++++++ 17 files changed, 74 insertions(+), 83 deletions(-) diff --git a/examples/portscan.cpp b/examples/portscan.cpp index d30e2a5..0f69615 100644 --- a/examples/portscan.cpp +++ b/examples/portscan.cpp @@ -54,7 +54,7 @@ typedef std::pair sniffer_data; * the scanned port's status. */ bool handler(PDU &pdu) { - TCP &tcp = pdu.rfind_pdu(); + const TCP &tcp = pdu.rfind_pdu(); // 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(); // 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::const_iterator it = ips.begin(); it != ips.end(); ++it) { diff --git a/examples/traceroute.cpp b/examples/traceroute.cpp index 7743367..8d11d62 100644 --- a/examples/traceroute.cpp +++ b/examples/traceroute.cpp @@ -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(); + const IP &ip = pdu.rfind_pdu(); ttl_map::const_iterator iter; // Fetch the IP PDU attached to the ICMP response - IP inner_ip = pdu.rfind_pdu().to(); + const IP inner_ip = pdu.rfind_pdu().to(); // Critical section { std::lock_guard _(lock); diff --git a/include/ip.h b/include/ip.h index 14e244d..e6d729f 100644 --- a/include/ip.h +++ b/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(); diff --git a/include/packet_sender.h b/include/packet_sender.h index aa4ea43..940177a 100644 --- a/include/packet_sender.h +++ b/include/packet_sender.h @@ -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. diff --git a/include/pdu.h b/include/pdu.h index 11883cc..e1afb55 100644 --- a/include/pdu.h +++ b/include/pdu.h @@ -118,7 +118,8 @@ namespace Tins { DOT1Q, PPPOE, STP, - PPI + PPI, + USER_DEFINED_PDU = 1000 }; /** diff --git a/include/sniffer.h b/include/sniffer.h index 99a70fe..060083a 100644 --- a/include/sniffer.h +++ b/include/sniffer.h @@ -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), diff --git a/include/tcp.h b/include/tcp.h index 4797260..a5ff3b0 100644 --- a/include/tcp.h +++ b/include/tcp.h @@ -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. * diff --git a/src/dot11/dot11_mgmt.cpp b/src/dot11/dot11_mgmt.cpp index 8f64279..d4d0281 100644 --- a/src/dot11/dot11_mgmt.cpp +++ b/src/dot11/dot11_mgmt.cpp @@ -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 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); } diff --git a/src/ethernetII.cpp b/src/ethernetII.cpp index 5d28f79..a336f07 100644 --- a/src/ethernetII.cpp +++ b/src/ethernetII.cpp @@ -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; } diff --git a/src/ip.cpp b/src/ip.cpp index 54cb2cb..6acaba7 100644 --- a/src/ip.cpp +++ b/src/ip.cpp @@ -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(_ip.ihl * sizeof(uint32_t), total_sz); return inner_pdu() ? inner_pdu()->matches_response(ptr + sz, total_sz - sz) : true; } diff --git a/src/ip_address.cpp b/src/ip_address.cpp index 5dd7f97..0ee7f7d 100644 --- a/src/ip_address.cpp +++ b/src/ip_address.cpp @@ -36,6 +36,8 @@ using std::string; namespace Tins{ +const IPv4Address IPv4Address::broadcast("255.255.255.255"); + const AddressRange 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; +} } diff --git a/src/packet_sender.cpp b/src/packet_sender.cpp index 0b46293..e3cd246 100644 --- a/src/packet_sender.cpp +++ b/src/packet_sender.cpp @@ -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; } diff --git a/src/tcp.cpp b/src/tcp.cpp index b775f6a..1b6d516 100644 --- a/src/tcp.cpp +++ b/src/tcp.cpp @@ -197,10 +197,6 @@ TCP::AltChecksums TCP::altchecksum() const { return static_cast(generic_search(ALTCHK)); } -small_uint<1> TCP::get_flag(Flags tcp_flag) { - return static_cast(*this).get_flag(tcp_flag); -} - small_uint<1> TCP::get_flag(Flags tcp_flag) const { switch(tcp_flag) { case FIN: diff --git a/tests/src/allocators.cpp b/tests/src/allocators.cpp index fd93124..6d05674 100644 --- a/tests/src/allocators.cpp +++ b/tests/src/allocators.cpp @@ -59,7 +59,7 @@ public: std::vector buffer; }; -const PDU::PDUType DummyPDU::pdu_flag = static_cast(0xefff); +const PDU::PDUType DummyPDU::pdu_flag = USER_DEFINED_PDU; TEST_F(AllocatorsTest, LinkLayerPDUs) { Allocators::register_allocator(1638); diff --git a/tests/src/hwaddress.cpp b/tests/src/hwaddress.cpp index bcba206..2ab7dd1 100644 --- a/tests/src/hwaddress.cpp +++ b/tests/src/hwaddress.cpp @@ -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()); diff --git a/tests/src/ip.cpp b/tests/src/ip.cpp index 604b662..d5a1d91 100644 --- a/tests/src/ip.cpp +++ b/tests/src/ip.cpp @@ -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); diff --git a/tests/src/ipaddress.cpp b/tests/src/ipaddress.cpp index 54f21ba..cfae398 100644 --- a/tests/src/ipaddress.cpp +++ b/tests/src/ipaddress.cpp @@ -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()); +}