mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
192 lines
6.2 KiB
C++
192 lines
6.2 KiB
C++
/*
|
|
* 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 <cassert>
|
|
#include <cstring>
|
|
#include <stdexcept>
|
|
#include <algorithm>
|
|
#ifndef WIN32
|
|
#include <net/ethernet.h>
|
|
#include <netinet/in.h>
|
|
#include <netpacket/packet.h>
|
|
#endif
|
|
#include "ethernetII.h"
|
|
#include "packet_sender.h"
|
|
#include "rawpdu.h"
|
|
#include "ip.h"
|
|
#include "ipv6.h"
|
|
#include "arp.h"
|
|
#include "constants.h"
|
|
|
|
namespace Tins {
|
|
const EthernetII::address_type EthernetII::BROADCAST("ff:ff:ff:ff:ff:ff");
|
|
|
|
EthernetII::EthernetII(const NetworkInterface& iface,
|
|
const address_type &dst_hw_addr, const address_type &src_hw_addr,
|
|
PDU* child)
|
|
: PDU(child)
|
|
{
|
|
memset(&_eth, 0, sizeof(ethhdr));
|
|
dst_addr(dst_hw_addr);
|
|
src_addr(src_hw_addr);
|
|
this->iface(iface);
|
|
_eth.payload_type = 0;
|
|
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
void EthernetII::dst_addr(const address_type &new_dst_addr) {
|
|
new_dst_addr.copy(_eth.dst_mac);
|
|
}
|
|
|
|
void EthernetII::src_addr(const address_type &new_src_addr) {
|
|
new_src_addr.copy(_eth.src_mac);
|
|
}
|
|
|
|
void EthernetII::iface(const NetworkInterface& new_iface) {
|
|
_iface = new_iface;
|
|
}
|
|
|
|
void EthernetII::payload_type(uint16_t new_payload_type) {
|
|
this->_eth.payload_type = Endian::host_to_be(new_payload_type);
|
|
}
|
|
|
|
uint32_t EthernetII::header_size() const {
|
|
return sizeof(ethhdr);
|
|
}
|
|
|
|
#ifndef WIN32
|
|
void EthernetII::send(PacketSender &sender) {
|
|
if(!_iface)
|
|
throw std::runtime_error("Interface has not been set");
|
|
struct sockaddr_ll addr;
|
|
|
|
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
|
|
|
addr.sll_family = Endian::host_to_be<uint16_t>(PF_PACKET);
|
|
addr.sll_protocol = Endian::host_to_be<uint16_t>(ETH_P_ALL);
|
|
addr.sll_halen = address_type::address_size;
|
|
addr.sll_ifindex = _iface.id();
|
|
memcpy(&(addr.sll_addr), _eth.dst_mac, address_type::address_size);
|
|
|
|
sender.send_l2(*this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
|
}
|
|
#endif // WIN32
|
|
|
|
bool EthernetII::matches_response(uint8_t *ptr, uint32_t total_sz) {
|
|
if(total_sz < sizeof(ethhdr))
|
|
return false;
|
|
ethhdr *eth_ptr = (ethhdr*)ptr;
|
|
if(!memcmp(eth_ptr->dst_mac, _eth.src_mac, address_type::address_size)) {
|
|
// chequear broadcast en destino original...
|
|
return (inner_pdu()) ? inner_pdu()->matches_response(ptr + sizeof(_eth), total_sz - sizeof(_eth)) : true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
|
|
uint32_t my_sz = header_size();
|
|
assert(total_sz >= my_sz);
|
|
|
|
/* 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);
|
|
}
|
|
memcpy(buffer, &_eth, sizeof(ethhdr));
|
|
}
|
|
|
|
#ifndef WIN32
|
|
PDU *EthernetII::recv_response(PacketSender &sender) {
|
|
struct sockaddr_ll addr;
|
|
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
|
|
|
addr.sll_family = Endian::host_to_be<uint16_t>(PF_PACKET);
|
|
addr.sll_protocol = Endian::host_to_be<uint16_t>(ETH_P_ALL);
|
|
addr.sll_halen = address_type::address_size;
|
|
addr.sll_ifindex = _iface.id();
|
|
memcpy(&(addr.sll_addr), _eth.dst_mac, address_type::address_size);
|
|
|
|
return sender.recv_l2(*this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
|
}
|
|
#endif // WIN32
|
|
|
|
PDU *EthernetII::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
|
|
if(total_sz < sizeof(_eth))
|
|
return 0;
|
|
PDU *child = 0, *cloned;
|
|
if(total_sz > sizeof(_eth)) {
|
|
if((child = PDU::clone_inner_pdu(ptr + sizeof(_eth), total_sz - sizeof(_eth))) == 0)
|
|
return 0;
|
|
}
|
|
cloned = new EthernetII(ptr, std::min(total_sz, (uint32_t)sizeof(_eth)));
|
|
cloned->inner_pdu(child);
|
|
return cloned;
|
|
}
|
|
}
|