mirror of
https://github.com/mfontanini/libtins
synced 2026-01-30 05:24:26 +01:00
Added constants header.
This commit is contained in:
@@ -36,9 +36,15 @@ namespace Tins {
|
||||
|
||||
public:
|
||||
|
||||
/** \brief Represents the ethernetII broadcast address.
|
||||
/**
|
||||
* \brief Represents the ethernetII broadcast address.
|
||||
*/
|
||||
static const uint8_t* BROADCAST;
|
||||
|
||||
/**
|
||||
* \brief Ethernet II hardware address size.
|
||||
*/
|
||||
static const uint32_t ADDR_SIZE = 6;
|
||||
|
||||
/**
|
||||
* \brief Constructor for creating an ethernet PDU
|
||||
@@ -198,8 +204,8 @@ namespace Tins {
|
||||
* Struct that represents the Ethernet II header
|
||||
*/
|
||||
struct ethhdr {
|
||||
uint8_t dst_mac[6];
|
||||
uint8_t src_mac[6];
|
||||
uint8_t dst_mac[ADDR_SIZE];
|
||||
uint8_t src_mac[ADDR_SIZE];
|
||||
uint16_t payload_type;
|
||||
} __attribute__((__packed__));
|
||||
|
||||
|
||||
@@ -38,6 +38,11 @@ namespace Tins {
|
||||
*/
|
||||
class IP : public PDU {
|
||||
public:
|
||||
/**
|
||||
* \brief IP address size.
|
||||
*/
|
||||
static const uint32_t ADDR_SIZE = 4;
|
||||
|
||||
/**
|
||||
* \brief Enum indicating the option's class.
|
||||
*
|
||||
|
||||
15
src/arp.cpp
15
src/arp.cpp
@@ -22,11 +22,12 @@
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#include <netinet/in.h>
|
||||
#include "arp.h"
|
||||
#include "ip.h"
|
||||
#include "ethernetII.h"
|
||||
#include "rawpdu.h"
|
||||
#include "utils.h"
|
||||
#include "constants.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
@@ -34,10 +35,10 @@ using namespace std;
|
||||
|
||||
Tins::ARP::ARP(uint32_t target_ip, uint32_t sender_ip, const uint8_t *target_hw, const uint8_t *sender_hw) : PDU(0x0608) {
|
||||
memset(&_arp, 0, sizeof(arphdr));
|
||||
hw_addr_format(1);
|
||||
prot_addr_format(0x0800);
|
||||
hw_addr_length(6);
|
||||
prot_addr_length(4);
|
||||
hw_addr_format((uint16_t)Tins::Constants::ARP::ETHER);
|
||||
prot_addr_format((uint16_t)Tins::Constants::Ethernet::IP);
|
||||
hw_addr_length(EthernetII::ADDR_SIZE);
|
||||
prot_addr_length(IP::ADDR_SIZE);
|
||||
sender_ip_addr(sender_ip);
|
||||
target_ip_addr(target_ip);
|
||||
if(sender_hw)
|
||||
@@ -46,7 +47,7 @@ Tins::ARP::ARP(uint32_t target_ip, uint32_t sender_ip, const uint8_t *target_hw,
|
||||
target_hw_addr(target_hw);
|
||||
}
|
||||
|
||||
Tins::ARP::ARP(const uint8_t *buffer, uint32_t total_sz) : PDU(0x0608) {
|
||||
Tins::ARP::ARP(const uint8_t *buffer, uint32_t total_sz) : PDU(Utils::net_to_host_s(Constants::Ethernet::ARP)) {
|
||||
if(total_sz < sizeof(arphdr))
|
||||
throw std::runtime_error("Not enough size for an ARP header in the buffer.");
|
||||
memcpy(&_arp, buffer, sizeof(arphdr));
|
||||
@@ -55,7 +56,7 @@ Tins::ARP::ARP(const uint8_t *buffer, uint32_t total_sz) : PDU(0x0608) {
|
||||
inner_pdu(new RawPDU(buffer + sizeof(arphdr), total_sz));
|
||||
}
|
||||
|
||||
Tins::ARP::ARP(const arphdr *arp_ptr) : PDU(Utils::net_to_host_s(0x0806)) {
|
||||
Tins::ARP::ARP(const arphdr *arp_ptr) : PDU(Utils::net_to_host_s(Constants::Ethernet::ARP)) {
|
||||
memcpy(&_arp, arp_ptr, sizeof(arphdr));
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
#ifndef WIN32
|
||||
#include <net/ethernet.h>
|
||||
#include <netpacket/packet.h>
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
#include "ethernetII.h"
|
||||
#include "rawpdu.h"
|
||||
@@ -34,6 +33,7 @@
|
||||
#include "utils.h"
|
||||
|
||||
const uint8_t* Tins::EthernetII::BROADCAST = (const uint8_t*)"\xff\xff\xff\xff\xff\xff";
|
||||
//const uint32_t Tins::EthernetII::ADDR_SIZE = 6;
|
||||
|
||||
Tins::EthernetII::EthernetII(const std::string& iface, const uint8_t* dst_hw_addr, const uint8_t* src_hw_addr, PDU* child) throw (std::runtime_error) : PDU(ETHERTYPE_IP, child) {
|
||||
memset(&_eth, 0, sizeof(ethhdr));
|
||||
@@ -92,15 +92,15 @@ uint16_t Tins::EthernetII::payload_type() const {
|
||||
}
|
||||
|
||||
void Tins::EthernetII::dst_addr(const uint8_t* new_dst_mac) {
|
||||
memcpy(this->_eth.dst_mac, new_dst_mac, 6);
|
||||
memcpy(_eth.dst_mac, new_dst_mac, sizeof(_eth.dst_mac));
|
||||
}
|
||||
|
||||
void Tins::EthernetII::src_addr(const uint8_t* new_src_mac) {
|
||||
memcpy(this->_eth.src_mac, new_src_mac, 6);
|
||||
memcpy(_eth.src_mac, new_src_mac, sizeof(_eth.src_mac));
|
||||
}
|
||||
|
||||
void Tins::EthernetII::iface(uint32_t new_iface_index) {
|
||||
this->_iface_index = new_iface_index;
|
||||
_iface_index = new_iface_index;
|
||||
}
|
||||
|
||||
void Tins::EthernetII::iface(const std::string& new_iface) throw (std::runtime_error) {
|
||||
@@ -120,9 +120,9 @@ bool Tins::EthernetII::send(PacketSender* sender) {
|
||||
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_ALL);
|
||||
addr.sll_halen = 6;
|
||||
addr.sll_ifindex = this->_iface_index;
|
||||
memcpy(&(addr.sll_addr), this->_eth.dst_mac, 6);
|
||||
addr.sll_halen = ADDR_SIZE;
|
||||
addr.sll_ifindex = _iface_index;
|
||||
memcpy(&(addr.sll_addr), _eth.dst_mac, ADDR_SIZE);
|
||||
|
||||
return sender->send_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
||||
}
|
||||
@@ -131,7 +131,7 @@ bool Tins::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, 6)) {
|
||||
if(!memcmp(eth_ptr->dst_mac, _eth.src_mac, ADDR_SIZE)) {
|
||||
// chequear broadcast en destino original...
|
||||
return true;
|
||||
}
|
||||
@@ -166,20 +166,20 @@ Tins::PDU *Tins::EthernetII::recv_response(PacketSender *sender) {
|
||||
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_ALL);
|
||||
addr.sll_halen = 6;
|
||||
addr.sll_ifindex = this->_iface_index;
|
||||
memcpy(&(addr.sll_addr), this->_eth.dst_mac, 6);
|
||||
addr.sll_halen = ADDR_SIZE;
|
||||
addr.sll_ifindex = _iface_index;
|
||||
memcpy(&(addr.sll_addr), _eth.dst_mac, ADDR_SIZE);
|
||||
|
||||
return sender->recv_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::EthernetII::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
|
||||
if(total_sz < sizeof(ethhdr))
|
||||
if(total_sz < sizeof(_eth))
|
||||
return 0;
|
||||
const ethhdr *eth_ptr = (ethhdr*)ptr;
|
||||
PDU *child = 0, *cloned;
|
||||
if(total_sz > sizeof(ethhdr)) {
|
||||
if((child = PDU::clone_inner_pdu(ptr + sizeof(ethhdr), total_sz - sizeof(ethhdr))) == 0)
|
||||
if(total_sz > sizeof(_eth)) {
|
||||
if((child = PDU::clone_inner_pdu(ptr + sizeof(_eth), total_sz - sizeof(_eth))) == 0)
|
||||
return 0;
|
||||
}
|
||||
cloned = new EthernetII(eth_ptr);
|
||||
|
||||
26
src/ip.cpp
26
src/ip.cpp
@@ -23,7 +23,8 @@
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#ifndef WIN32
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
#include "ip.h"
|
||||
#include "tcp.h"
|
||||
@@ -31,13 +32,14 @@
|
||||
#include "icmp.h"
|
||||
#include "rawpdu.h"
|
||||
#include "utils.h"
|
||||
#include "constants.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
const uint8_t Tins::IP::DEFAULT_TTL = 128;
|
||||
|
||||
Tins::IP::IP(const string &ip_dst, const string &ip_src, PDU *child) : PDU(IPPROTO_IP, child) {
|
||||
Tins::IP::IP(const string &ip_dst, const string &ip_src, PDU *child) : PDU(Constants::IP::PROTO_IP, child) {
|
||||
init_ip_fields();
|
||||
if(ip_dst.size())
|
||||
_ip.daddr = Utils::resolve_ip(ip_dst);
|
||||
@@ -60,7 +62,7 @@ Tins::IP &Tins::IP::operator= (const IP &other) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
Tins::IP::IP(const uint8_t *buffer, uint32_t total_sz) : PDU(IPPROTO_IP) {
|
||||
Tins::IP::IP(const uint8_t *buffer, uint32_t total_sz) : PDU(Constants::IP::PROTO_IP) {
|
||||
static const char *msg("Not enough size for an IP header in the buffer.");
|
||||
if(total_sz < sizeof(iphdr))
|
||||
throw std::runtime_error(msg);
|
||||
@@ -141,12 +143,12 @@ Tins::IP::IP(const uint8_t *buffer, uint32_t total_sz) : PDU(IPPROTO_IP) {
|
||||
}
|
||||
}
|
||||
|
||||
Tins::IP::IP(const iphdr *ptr) : PDU(IPPROTO_IP) {
|
||||
Tins::IP::IP(const iphdr *ptr) : PDU(Constants::IP::PROTO_IP) {
|
||||
std::memcpy(&_ip, ptr, sizeof(iphdr));
|
||||
/* Options... */
|
||||
}
|
||||
|
||||
Tins::IP::IP(uint32_t ip_dst, uint32_t ip_src, PDU *child) : PDU(IPPROTO_IP, child) {
|
||||
Tins::IP::IP(uint32_t ip_dst, uint32_t ip_src, PDU *child) : PDU(Constants::IP::PROTO_IP, child) {
|
||||
init_ip_fields();
|
||||
_ip.daddr = ip_dst;
|
||||
_ip.saddr = ip_src;
|
||||
@@ -311,23 +313,21 @@ void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU
|
||||
this->tot_len(total_sz);
|
||||
this->head_len (my_sz / sizeof(uint32_t));
|
||||
|
||||
memcpy(buffer, &_ip, sizeof(iphdr));
|
||||
memcpy(buffer, &_ip, sizeof(_ip));
|
||||
|
||||
uint8_t* ptr_buffer = buffer + sizeof(iphdr);
|
||||
for (uint32_t i = 0; i < _ip_options.size(); ++i)
|
||||
uint8_t* ptr_buffer = buffer + sizeof(_ip);
|
||||
for(uint32_t i = 0; i < _ip_options.size(); ++i)
|
||||
ptr_buffer = _ip_options[i].write(ptr_buffer);
|
||||
|
||||
memset(buffer + sizeof(iphdr) + this->_options_size, 0, this->_padded_options_size - this->_options_size);
|
||||
memset(buffer + sizeof(_ip) + _options_size, 0, _padded_options_size - _options_size);
|
||||
|
||||
if (parent && !_ip.check) {
|
||||
uint32_t checksum = Utils::do_checksum(buffer, buffer + sizeof(iphdr) + _padded_options_size);
|
||||
if(parent && !_ip.check) {
|
||||
uint32_t checksum = Utils::do_checksum(buffer, buffer + sizeof(_ip) + _padded_options_size);
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||
((iphdr*)buffer)->check = Utils::net_to_host_s(~checksum);
|
||||
this->check(0);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
bool Tins::IP::matches_response(uint8_t *ptr, uint32_t total_sz) {
|
||||
|
||||
13
src/snap.cpp
13
src/snap.cpp
@@ -26,6 +26,7 @@
|
||||
#include <net/ethernet.h>
|
||||
#endif
|
||||
#include "snap.h"
|
||||
#include "constants.h"
|
||||
#include "utils.h"
|
||||
#include "arp.h"
|
||||
#include "ip.h"
|
||||
@@ -45,13 +46,13 @@ Tins::SNAP::SNAP(const uint8_t *buffer, uint32_t total_sz) : PDU(0xff) {
|
||||
buffer += sizeof(_snap);
|
||||
total_sz -= sizeof(_snap);
|
||||
switch(Utils::net_to_host_s(_snap.eth_type)) {
|
||||
case ETHERTYPE_IP:
|
||||
case Tins::Constants::Ethernet::IP:
|
||||
inner_pdu(new Tins::IP(buffer, total_sz));
|
||||
break;
|
||||
case ETHERTYPE_ARP:
|
||||
case Tins::Constants::Ethernet::ARP:
|
||||
inner_pdu(new Tins::ARP(buffer, total_sz));
|
||||
break;
|
||||
case 0x888e:
|
||||
case Tins::Constants::Ethernet::EAPOL:
|
||||
inner_pdu(Tins::EAPOL::from_bytes(buffer, total_sz));
|
||||
break;
|
||||
};
|
||||
@@ -77,13 +78,13 @@ void Tins::SNAP::write_serialization(uint8_t *buffer, uint32_t total_sz, const P
|
||||
uint16_t type = ETHERTYPE_IP;
|
||||
switch (inner_pdu()->pdu_type()) {
|
||||
case PDU::IP:
|
||||
type = ETHERTYPE_IP;
|
||||
type = Tins::Constants::Ethernet::IP;
|
||||
break;
|
||||
case PDU::ARP:
|
||||
type = ETHERTYPE_ARP;
|
||||
type = Tins::Constants::Ethernet::ARP;
|
||||
break;
|
||||
case PDU::EAPOL:
|
||||
type = 0x888e;
|
||||
type = Tins::Constants::Ethernet::EAPOL;
|
||||
break;
|
||||
default:
|
||||
type = 0;
|
||||
|
||||
15
src/tcp.cpp
15
src/tcp.cpp
@@ -22,24 +22,21 @@
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#ifndef WIN32
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
#include "tcp.h"
|
||||
#include "ip.h"
|
||||
#include "constants.h"
|
||||
#include "rawpdu.h"
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
const uint16_t Tins::TCP::DEFAULT_WINDOW = 32678;
|
||||
|
||||
Tins::TCP::TCP(uint16_t dport, uint16_t sport) : PDU(IPPROTO_TCP), _options_size(0), _total_options_size(0) {
|
||||
Tins::TCP::TCP(uint16_t dport, uint16_t sport) : PDU(Constants::IP::PROTO_TCP), _options_size(0), _total_options_size(0) {
|
||||
std::memset(&_tcp, 0, sizeof(tcphdr));
|
||||
this->dport(dport);
|
||||
this->sport(sport);
|
||||
this->data_offset(sizeof(tcphdr) / sizeof(uint32_t));
|
||||
this->window(DEFAULT_WINDOW);
|
||||
this->check(0);
|
||||
data_offset(sizeof(tcphdr) / sizeof(uint32_t));
|
||||
window(DEFAULT_WINDOW);
|
||||
}
|
||||
|
||||
Tins::TCP::TCP(const TCP &other) : PDU(other) {
|
||||
@@ -52,7 +49,7 @@ Tins::TCP &Tins::TCP::operator= (const TCP &other) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
Tins::TCP::TCP(const uint8_t *buffer, uint32_t total_sz) : PDU(IPPROTO_TCP) {
|
||||
Tins::TCP::TCP(const uint8_t *buffer, uint32_t total_sz) : PDU(Constants::IP::PROTO_TCP) {
|
||||
if(total_sz < sizeof(tcphdr))
|
||||
throw std::runtime_error("Not enough size for an TCP header in the buffer.");
|
||||
std::memcpy(&_tcp, buffer, sizeof(tcphdr));
|
||||
@@ -252,7 +249,7 @@ void Tins::TCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PD
|
||||
const Tins::IP *ip_packet = dynamic_cast<const Tins::IP*>(parent);
|
||||
memcpy(tcp_start, &_tcp, sizeof(tcphdr));
|
||||
if(!_tcp.check && ip_packet) {
|
||||
uint32_t checksum = Utils::pseudoheader_checksum(ip_packet->src_addr(), ip_packet->dst_addr(), size(), IPPROTO_TCP) +
|
||||
uint32_t checksum = Utils::pseudoheader_checksum(ip_packet->src_addr(), ip_packet->dst_addr(), size(), Constants::IP::PROTO_TCP) +
|
||||
Utils::do_checksum(tcp_start, tcp_start + total_sz);
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||
|
||||
10
src/udp.cpp
10
src/udp.cpp
@@ -22,22 +22,20 @@
|
||||
#include <stdexcept>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#ifndef WIN32
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
#include "utils.h"
|
||||
#include "udp.h"
|
||||
#include "constants.h"
|
||||
#include "ip.h"
|
||||
#include "rawpdu.h"
|
||||
|
||||
Tins::UDP::UDP(uint16_t dport, uint16_t sport, PDU *child) : PDU(IPPROTO_UDP, child) {
|
||||
Tins::UDP::UDP(uint16_t dport, uint16_t sport, PDU *child) : PDU(Constants::IP::PROTO_TCP, child) {
|
||||
this->dport(dport);
|
||||
this->sport(sport);
|
||||
_udp.check = 0;
|
||||
_udp.len = 0;
|
||||
}
|
||||
|
||||
Tins::UDP::UDP(const uint8_t *buffer, uint32_t total_sz) : PDU(IPPROTO_UDP) {
|
||||
Tins::UDP::UDP(const uint8_t *buffer, uint32_t total_sz) : PDU(Constants::IP::PROTO_TCP) {
|
||||
if(total_sz < sizeof(udphdr))
|
||||
throw std::runtime_error("Not enough size for an UDP header in the buffer.");
|
||||
std::memcpy(&_udp, buffer, sizeof(udphdr));
|
||||
@@ -83,7 +81,7 @@ void Tins::UDP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PD
|
||||
length(sizeof(udphdr) + inner_pdu()->size());
|
||||
std::memcpy(buffer, &_udp, sizeof(udphdr));
|
||||
if(!_udp.check && ip_packet) {
|
||||
uint32_t checksum = Utils::pseudoheader_checksum(ip_packet->src_addr(), ip_packet->dst_addr(), size(), IPPROTO_UDP) +
|
||||
uint32_t checksum = Utils::pseudoheader_checksum(ip_packet->src_addr(), ip_packet->dst_addr(), size(), Constants::IP::PROTO_TCP) +
|
||||
Utils::do_checksum(buffer, buffer + total_sz);
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff)+(checksum >> 16);
|
||||
|
||||
Reference in New Issue
Block a user