mirror of
https://github.com/mfontanini/libtins
synced 2026-01-30 13:34:27 +01:00
Added SLL pseudo protocol header.
This commit is contained in:
@@ -48,6 +48,7 @@
|
||||
#include "ipv6.h"
|
||||
#include "arp.h"
|
||||
#include "constants.h"
|
||||
#include "internals.h"
|
||||
|
||||
namespace Tins {
|
||||
const EthernetII::address_type EthernetII::BROADCAST("ff:ff:ff:ff:ff:ff");
|
||||
@@ -70,25 +71,16 @@ EthernetII::EthernetII(const uint8_t *buffer, uint32_t total_sz)
|
||||
if(total_sz < sizeof(ethhdr))
|
||||
throw std::runtime_error("Not enough size for an ethernetII header in the buffer.");
|
||||
memcpy(&_eth, buffer, sizeof(ethhdr));
|
||||
PDU *next = 0;
|
||||
buffer += sizeof(ethhdr);
|
||||
total_sz -= sizeof(ethhdr);
|
||||
if(total_sz) {
|
||||
switch(payload_type()) {
|
||||
case Constants::Ethernet::IP:
|
||||
next = new Tins::IP(buffer, total_sz);
|
||||
break;
|
||||
case Constants::Ethernet::IPV6:
|
||||
next = new Tins::IPv6(buffer, total_sz);
|
||||
break;
|
||||
case Constants::Ethernet::ARP:
|
||||
next = new Tins::ARP(buffer, total_sz);
|
||||
break;
|
||||
default:
|
||||
next = new Tins::RawPDU(buffer, total_sz);
|
||||
// Other protos plz
|
||||
}
|
||||
inner_pdu(next);
|
||||
inner_pdu(
|
||||
Internals::pdu_from_flag(
|
||||
(Constants::Ethernet::e)payload_type(),
|
||||
buffer,
|
||||
total_sz
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,21 +144,10 @@ void EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, const P
|
||||
|
||||
/* Inner type defaults to IP */
|
||||
if ((_eth.payload_type == 0) && inner_pdu()) {
|
||||
uint16_t type = Constants::Ethernet::IP;
|
||||
switch (inner_pdu()->pdu_type()) {
|
||||
case PDU::IP:
|
||||
type = Constants::Ethernet::IP;
|
||||
break;
|
||||
case PDU::IPv6:
|
||||
type = Constants::Ethernet::IPV6;
|
||||
break;
|
||||
case PDU::ARP:
|
||||
type = Constants::Ethernet::ARP;
|
||||
break;
|
||||
default:
|
||||
type = 0;
|
||||
}
|
||||
_eth.payload_type = Endian::host_to_be(type);
|
||||
Constants::Ethernet::e flag = Internals::pdu_flag_to_ether_type(
|
||||
inner_pdu()->pdu_type()
|
||||
);
|
||||
payload_type(static_cast<uint16_t>(flag));
|
||||
}
|
||||
memcpy(buffer, &_eth, sizeof(ethhdr));
|
||||
}
|
||||
|
||||
@@ -28,6 +28,11 @@
|
||||
*/
|
||||
|
||||
#include "internals.h"
|
||||
#include "ip.h"
|
||||
#include "ipv6.h"
|
||||
#include "arp.h"
|
||||
#include "eapol.h"
|
||||
#include "rawpdu.h"
|
||||
|
||||
using std::string;
|
||||
|
||||
@@ -55,5 +60,35 @@ void skip_line(std::istream &input) {
|
||||
while(c != '\n' && input)
|
||||
c = input.get();
|
||||
}
|
||||
|
||||
Tins::PDU *pdu_from_flag(Constants::Ethernet::e flag, const uint8_t *buffer,
|
||||
uint32_t size, bool rawpdu_on_no_match)
|
||||
{
|
||||
switch(flag) {
|
||||
case Tins::Constants::Ethernet::IP:
|
||||
return new Tins::IP(buffer, size);
|
||||
case Constants::Ethernet::IPV6:
|
||||
return new Tins::IPv6(buffer, size);
|
||||
case Tins::Constants::Ethernet::ARP:
|
||||
return new Tins::ARP(buffer, size);
|
||||
case Tins::Constants::Ethernet::EAPOL:
|
||||
return Tins::EAPOL::from_bytes(buffer, size);
|
||||
default:
|
||||
return rawpdu_on_no_match ? new RawPDU(buffer, size) : 0;
|
||||
};
|
||||
}
|
||||
|
||||
Constants::Ethernet::e pdu_flag_to_ether_type(PDU::PDUType flag) {
|
||||
switch (flag) {
|
||||
case PDU::IP:
|
||||
return Constants::Ethernet::IP;
|
||||
case PDU::IPv6:
|
||||
return Constants::Ethernet::IPV6;
|
||||
case PDU::ARP:
|
||||
return Constants::Ethernet::ARP;
|
||||
default:
|
||||
return Constants::Ethernet::UNKNOWN;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
91
src/sll.cpp
Normal file
91
src/sll.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (c) 2012, Nasel
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following disclaimer
|
||||
* in the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include "sll.h"
|
||||
#include "internals.h"
|
||||
|
||||
namespace Tins {
|
||||
SLL::SLL() : _header() {
|
||||
|
||||
}
|
||||
|
||||
SLL::SLL(const uint8_t *buffer, uint32_t total_sz) {
|
||||
const char *err_msg = "Not enough size for a SLL header";
|
||||
if(total_sz < sizeof(_header))
|
||||
throw std::runtime_error(err_msg);
|
||||
std::memcpy(&_header, buffer, sizeof(_header));
|
||||
buffer += sizeof(_header);
|
||||
total_sz -= sizeof(_header);
|
||||
if(total_sz) {
|
||||
inner_pdu(
|
||||
Internals::pdu_from_flag(
|
||||
(Constants::Ethernet::e)protocol(),
|
||||
buffer,
|
||||
total_sz
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void SLL::packet_type(uint16_t new_packet_type) {
|
||||
_header.packet_type = Endian::host_to_be(new_packet_type);
|
||||
}
|
||||
|
||||
void SLL::lladdr_type(uint16_t new_lladdr_type) {
|
||||
_header.lladdr_type = Endian::host_to_be(new_lladdr_type);
|
||||
}
|
||||
|
||||
void SLL::lladdr_len(uint16_t new_lladdr_len) {
|
||||
_header.lladdr_len = Endian::host_to_be(new_lladdr_len);
|
||||
}
|
||||
|
||||
void SLL::address(const address_type &new_address) {
|
||||
new_address.copy(_header.address);
|
||||
}
|
||||
|
||||
void SLL::protocol(uint16_t new_protocol) {
|
||||
_header.protocol = Endian::host_to_be(new_protocol);
|
||||
}
|
||||
|
||||
uint32_t SLL::header_size() const {
|
||||
return sizeof(_header);
|
||||
}
|
||||
|
||||
void SLL::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *) {
|
||||
if(inner_pdu()) {
|
||||
Constants::Ethernet::e flag = Internals::pdu_flag_to_ether_type(
|
||||
inner_pdu()->pdu_type()
|
||||
);
|
||||
protocol(static_cast<uint16_t>(flag));
|
||||
}
|
||||
std::memcpy(buffer, &_header, sizeof(_header));
|
||||
}
|
||||
}
|
||||
12
src/snap.cpp
12
src/snap.cpp
@@ -38,6 +38,7 @@
|
||||
#include "arp.h"
|
||||
#include "ip.h"
|
||||
#include "eapol.h"
|
||||
#include "internals.h"
|
||||
|
||||
|
||||
Tins::SNAP::SNAP(PDU *child) : PDU(child)
|
||||
@@ -55,7 +56,7 @@ Tins::SNAP::SNAP(const uint8_t *buffer, uint32_t total_sz)
|
||||
buffer += sizeof(_snap);
|
||||
total_sz -= sizeof(_snap);
|
||||
if(total_sz) {
|
||||
switch(eth_type()) {
|
||||
/*switch(eth_type()) {
|
||||
case Tins::Constants::Ethernet::IP:
|
||||
inner_pdu(new Tins::IP(buffer, total_sz));
|
||||
break;
|
||||
@@ -65,7 +66,14 @@ Tins::SNAP::SNAP(const uint8_t *buffer, uint32_t total_sz)
|
||||
case Tins::Constants::Ethernet::EAPOL:
|
||||
inner_pdu(Tins::EAPOL::from_bytes(buffer, total_sz));
|
||||
break;
|
||||
};
|
||||
};*/
|
||||
inner_pdu(
|
||||
Internals::pdu_from_flag(
|
||||
(Constants::Ethernet::e)eth_type(),
|
||||
buffer,
|
||||
total_sz
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -95,6 +95,8 @@ PtrPacket BaseSniffer::next_packet() {
|
||||
ret = Dot11::from_bytes((const uint8_t*)content, header.caplen);
|
||||
else if(iface_type == DLT_LOOP)
|
||||
ret = new Tins::Loopback((const uint8_t*)content, header.caplen);
|
||||
else if(iface_type == DLT_LINUX_SLL)
|
||||
ret = new Tins::SLL((const uint8_t*)content, header.caplen);
|
||||
}
|
||||
return PtrPacket(ret, header.ts);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user