mirror of
https://github.com/mfontanini/libtins
synced 2026-01-27 04:11:35 +01:00
Documented many header files. Done some minor code refactoring over PDU::clone_packet.
This commit is contained in:
26
src/arp.cpp
26
src/arp.cpp
@@ -52,7 +52,7 @@ Tins::PDU* Tins::ARP::make_arp_request(const std::string& iface,
|
||||
if (hw_snd) {
|
||||
memcpy(arp->_arp.ar_sha, hw_snd, 6);
|
||||
}
|
||||
arp->_arp.ar_op = Tins::ARP::REQUEST;
|
||||
arp->_arp.ar_op = Utils::net_to_host_s(REQUEST);
|
||||
|
||||
/* Create the EthernetII PDU with the ARP PDU as its inner PDU */
|
||||
EthernetII* eth = new EthernetII(iface, Tins::EthernetII::BROADCAST, hw_snd, arp);
|
||||
@@ -82,7 +82,7 @@ Tins::PDU* Tins::ARP::make_arp_reply(const string& iface,
|
||||
arp->_arp.ar_sip = sender;
|
||||
memcpy(arp->_arp.ar_sha, hw_snd, 6);
|
||||
memcpy(arp->_arp.ar_tha, hw_tgt, 6);
|
||||
arp->_arp.ar_op = Tins::ARP::REPLY;
|
||||
arp->_arp.ar_op = Utils::net_to_host_s(REPLY);
|
||||
|
||||
/* Create the EthernetII PDU with the ARP PDU as its inner PDU */
|
||||
EthernetII* eth = new EthernetII(iface, hw_tgt, hw_snd, arp);
|
||||
@@ -91,13 +91,13 @@ Tins::PDU* Tins::ARP::make_arp_reply(const string& iface,
|
||||
|
||||
Tins::ARP::ARP() : PDU(0x0608) {
|
||||
std::memset(&_arp, 0, sizeof(arphdr));
|
||||
_arp.ar_hrd = 0x0100;
|
||||
_arp.ar_pro = 0x0008;
|
||||
_arp.ar_hrd = Utils::net_to_host_s(0x0001);
|
||||
_arp.ar_pro = Utils::net_to_host_s(0x0800);
|
||||
_arp.ar_hln = 6;
|
||||
_arp.ar_pln = 4;
|
||||
}
|
||||
|
||||
Tins::ARP::ARP(arphdr *arp_ptr) : PDU(0x0608) {
|
||||
Tins::ARP::ARP(arphdr *arp_ptr) : PDU(Utils::net_to_host_s(0x0806)) {
|
||||
memcpy(&_arp, arp_ptr, sizeof(arphdr));
|
||||
}
|
||||
|
||||
@@ -140,10 +140,9 @@ void Tins::ARP::opcode(Flags new_opcode) {
|
||||
void Tins::ARP::set_arp_request(const std::string& ip_tgt, const std::string& ip_snd, const uint8_t* hw_snd) {
|
||||
this->_arp.ar_tip = Utils::resolve_ip(ip_tgt);
|
||||
this->_arp.ar_sip = Utils::resolve_ip(ip_snd);
|
||||
if (hw_snd) {
|
||||
if (hw_snd)
|
||||
memcpy(this->_arp.ar_sha, hw_snd, 6);
|
||||
}
|
||||
this->_arp.ar_op = REQUEST;
|
||||
this->_arp.ar_op = Utils::net_to_host_s(REQUEST);
|
||||
}
|
||||
|
||||
void Tins::ARP::set_arp_reply(const std::string& ip_tgt,
|
||||
@@ -155,7 +154,7 @@ void Tins::ARP::set_arp_reply(const std::string& ip_tgt,
|
||||
this->_arp.ar_sip = Utils::resolve_ip(ip_snd);
|
||||
memcpy(this->_arp.ar_tha, hw_tgt, 6);
|
||||
memcpy(this->_arp.ar_sha, hw_snd, 6);
|
||||
this->_arp.ar_op = REPLY;
|
||||
this->_arp.ar_op = Utils::net_to_host_s(REPLY);
|
||||
|
||||
}
|
||||
|
||||
@@ -181,13 +180,8 @@ Tins::PDU *Tins::ARP::clone_packet(uint8_t *ptr, uint32_t total_sz) {
|
||||
arphdr *arp_ptr = (arphdr*)ptr;
|
||||
PDU *child = 0, *cloned;
|
||||
if(total_sz > sizeof(arphdr)) {
|
||||
if(inner_pdu()) {
|
||||
child = inner_pdu()->clone_packet(ptr + sizeof(arphdr), total_sz - sizeof(arphdr));
|
||||
if(!child)
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
child = new RawPDU(ptr + sizeof(arphdr), total_sz - sizeof(arphdr));
|
||||
if((child = PDU::clone_inner_pdu(ptr + sizeof(arphdr), total_sz - sizeof(arphdr))) == 0)
|
||||
return 0;
|
||||
}
|
||||
cloned = new ARP(arp_ptr);
|
||||
cloned->inner_pdu(child);
|
||||
|
||||
@@ -108,9 +108,8 @@ bool Tins::EthernetII::matches_response(uint8_t *ptr, uint32_t total_sz) {
|
||||
void Tins::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 ((this->_eth.payload_type == 0) && this->inner_pdu()) {
|
||||
uint16_t type = ETHERTYPE_IP;
|
||||
switch (this->inner_pdu()->pdu_type()) {
|
||||
@@ -125,15 +124,11 @@ void Tins::EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, c
|
||||
}
|
||||
this->_eth.payload_type = Utils::net_to_host_s(type);
|
||||
}
|
||||
|
||||
|
||||
memcpy(buffer, &this->_eth, sizeof(ethhdr));
|
||||
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::EthernetII::recv_response(PacketSender *sender) {
|
||||
struct sockaddr_ll addr;
|
||||
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
@@ -151,13 +146,8 @@ Tins::PDU *Tins::EthernetII::clone_packet(uint8_t *ptr, uint32_t total_sz) {
|
||||
ethhdr *eth_ptr = (ethhdr*)ptr;
|
||||
PDU *child = 0, *cloned;
|
||||
if(total_sz > sizeof(ethhdr)) {
|
||||
if(inner_pdu()) {
|
||||
child = inner_pdu()->clone_packet(ptr + sizeof(ethhdr), total_sz - sizeof(ethhdr));
|
||||
if(!child)
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
child = new RawPDU(ptr + sizeof(ethhdr), total_sz - sizeof(ethhdr));
|
||||
if((child = PDU::clone_inner_pdu(ptr + sizeof(ethhdr), total_sz - sizeof(ethhdr))) == 0)
|
||||
return 0;
|
||||
}
|
||||
cloned = new EthernetII(eth_ptr);
|
||||
cloned->inner_pdu(child);
|
||||
|
||||
10
src/icmp.cpp
10
src/icmp.cpp
@@ -163,14 +163,8 @@ Tins::PDU *Tins::ICMP::clone_packet(uint8_t *ptr, uint32_t total_sz) {
|
||||
icmphdr *icmp_ptr = (icmphdr*)ptr;
|
||||
PDU *child = 0, *cloned;
|
||||
if(total_sz > sizeof(icmphdr)) {
|
||||
if(inner_pdu()) {
|
||||
child = inner_pdu()->clone_packet(ptr + sizeof(icmphdr), total_sz - sizeof(icmphdr));
|
||||
if(!child)
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
child = new RawPDU(ptr + sizeof(icmphdr), total_sz - sizeof(icmphdr));
|
||||
|
||||
if((child = PDU::clone_inner_pdu(ptr + sizeof(icmphdr), total_sz - sizeof(icmphdr))) == 0)
|
||||
return 0;
|
||||
}
|
||||
cloned = new ICMP(icmp_ptr);
|
||||
cloned->inner_pdu(child);
|
||||
|
||||
13
src/pdu.cpp
13
src/pdu.cpp
@@ -22,6 +22,7 @@
|
||||
#include <cassert>
|
||||
#include "utils.h"
|
||||
#include "pdu.h"
|
||||
#include "rawpdu.h"
|
||||
|
||||
|
||||
Tins::PDU::PDU(uint32_t flag, PDU *next_pdu) : _flag(flag), _inner_pdu(next_pdu) {
|
||||
@@ -67,6 +68,18 @@ void Tins::PDU::serialize(uint8_t *buffer, uint32_t total_sz, const PDU *parent)
|
||||
write_serialization(buffer, total_sz, parent);
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::PDU::clone_inner_pdu(uint8_t *ptr, uint32_t total_sz) {
|
||||
PDU *child = 0;
|
||||
if(inner_pdu()) {
|
||||
child = inner_pdu()->clone_packet(ptr, total_sz);
|
||||
if(!child)
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
child = new RawPDU(ptr, total_sz);
|
||||
return child;
|
||||
}
|
||||
|
||||
/* Static methods */
|
||||
uint32_t Tins::PDU::do_checksum(uint8_t *start, uint8_t *end) {
|
||||
uint32_t checksum(0);
|
||||
|
||||
Reference in New Issue
Block a user