1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Fixed PKTAP next layer interpretation.

This commit is contained in:
Matias Fontanini
2014-12-21 10:51:18 -08:00
parent a4c67e5acd
commit 65607b0eb5
6 changed files with 68 additions and 3 deletions

View File

@@ -113,6 +113,8 @@ PDU *pdu_from_flag(Constants::Ethernet::e flag, const uint8_t *buffer,
uint32_t size, bool rawpdu_on_no_match = true);
PDU *pdu_from_flag(Constants::IP::e flag, const uint8_t *buffer,
uint32_t size, bool rawpdu_on_no_match = true);
PDU *pdu_from_dlt_flag(int flag, const uint8_t *buffer,
uint32_t size, bool rawpdu_on_no_match = true);
PDU *pdu_from_flag(PDU::PDUType type, const uint8_t *buffer, uint32_t size);
Constants::Ethernet::e pdu_flag_to_ether_type(PDU::PDUType flag);

View File

@@ -27,6 +27,7 @@
*
*/
#include <pcap.h>
#include "internals.h"
#include "ip.h"
#include "ethernetII.h"
@@ -38,12 +39,16 @@
#include "udp.h"
#include "ipsec.h"
#include "icmp.h"
#include "loopback.h"
#include "sll.h"
#include "ppi.h"
#include "icmpv6.h"
#include "arp.h"
#include "eapol.h"
#include "rawpdu.h"
#include "dot1q.h"
#include "pppoe.h"
#include "exceptions.h"
#include "ip_address.h"
#include "ipv6_address.h"
#include "pdu_allocator.h"
@@ -134,6 +139,35 @@ Tins::PDU *pdu_from_flag(Constants::IP::e flag, const uint8_t *buffer,
return 0;
}
PDU *pdu_from_dlt_flag(int flag, const uint8_t *buffer,
uint32_t size, bool rawpdu_on_no_match)
{
switch (flag) {
case DLT_EN10MB:
return new EthernetII(buffer, size);
#ifdef HAVE_DOT11
case DLT_IEEE802_11_RADIO:
return new RadioTap(buffer, size);
case DLT_IEEE802_11:
return Dot11::from_bytes(buffer, size);
#else // HAVE_DOT11
case DLT_IEEE802_11_RADIO:
case DLT_IEEE802_11:
throw protocol_disabled();
#endif // HAVE_DOT11
case DLT_NULL:
return new Loopback(buffer, size);
case DLT_LINUX_SLL:
return new SLL(buffer, size);
case DLT_PPI:
return new PPI(buffer, size);
default:
return rawpdu_on_no_match ? new RawPDU(buffer, size) : 0;
};
}
Tins::PDU *pdu_from_flag(PDU::PDUType type, const uint8_t *buffer, uint32_t size)
{
switch(type) {

View File

@@ -49,8 +49,8 @@ PKTAP::PKTAP(const uint8_t* buffer, uint32_t total_sz)
total_sz -= header_length;
if (header_.next && total_sz > 0) {
inner_pdu(
Internals::pdu_from_flag(
(Constants::Ethernet::e)header_.dlt,
Internals::pdu_from_dlt_flag(
header_.dlt,
buffer,
total_sz
)

View File

@@ -162,7 +162,7 @@ PtrPacket BaseSniffer::next_packet() {
}
#ifdef DLT_PKTAP
else if (iface_type == DLT_PKTAP) {
handler = &sniff_loop_handler<PKTAP>();
handler = &sniff_loop_handler<PKTAP>;
}
#endif // DLT_PKTAP
else if(iface_type == DLT_NULL)

View File

@@ -57,6 +57,7 @@ ADD_CUSTOM_TARGET(
NetworkInterfaceTest
OfflinePacketFilterTest
PDUTest
PKTAPTest
PPITest
PPPoETest
RadioTapTest
@@ -98,6 +99,7 @@ ADD_EXECUTABLE(MatchesResponseTest EXCLUDE_FROM_ALL matches_response.cpp)
ADD_EXECUTABLE(NetworkInterfaceTest EXCLUDE_FROM_ALL network_interface.cpp)
ADD_EXECUTABLE(OfflinePacketFilterTest EXCLUDE_FROM_ALL offline_packet_filter.cpp)
ADD_EXECUTABLE(PDUTest EXCLUDE_FROM_ALL pdu.cpp)
ADD_EXECUTABLE(PKTAPTest EXCLUDE_FROM_ALL pktap.cpp)
ADD_EXECUTABLE(PPITest EXCLUDE_FROM_ALL ppi.cpp)
ADD_EXECUTABLE(PPPoETest EXCLUDE_FROM_ALL pppoe.cpp)
ADD_EXECUTABLE(RadioTapTest EXCLUDE_FROM_ALL radiotap.cpp)

27
tests/src/pktap.cpp Normal file
View File

@@ -0,0 +1,27 @@
#include <gtest/gtest.h>
#include <cstring>
#include <string>
#include <iostream>
#include <stdint.h>
#include "pktap.h"
using namespace std;
using namespace Tins;
class PKTAPTest : public testing::Test {
public:
static const uint8_t packet1[];
};
const uint8_t PKTAPTest::packet1[] = {
108, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 101, 110, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 178, 7, 0, 0, 111, 99, 115, 112, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 128, 57, 251, 101, 187, 44, 240, 238, 33, 128, 46, 8, 0, 69, 0, 0, 40, 188, 8, 64, 0, 64, 6, 70, 77, 10, 0, 0, 222, 17, 151, 28, 6, 196, 70, 0, 80, 63, 40, 147, 97, 101, 156, 12, 242, 80, 17, 64, 0, 45, 170, 0, 0
};
TEST_F(PKTAPTest, ConstructorFromBuffer) {
PKTAP pkt(packet1, sizeof(packet1));
PDU* inner = pkt.inner_pdu();
ASSERT_TRUE(inner);
EXPECT_EQ(PDU::ETHERNET_II, inner->pdu_type());
}