mirror of
https://github.com/mfontanini/libtins
synced 2026-01-29 13:04:28 +01:00
Merge branch 'master' of ssh://git.code.sf.net/p/libtins/code
This commit is contained in:
48
src/arp.cpp
48
src/arp.cpp
@@ -47,10 +47,10 @@ Tins::PDU* Tins::ARP::make_arp_request(const std::string& iface,
|
||||
|
||||
/* Create ARP packet and set its attributes */
|
||||
ARP* arp = new ARP();
|
||||
arp->_arp.ar_tip = target;
|
||||
arp->_arp.ar_sip = sender;
|
||||
arp->target_ip_addr(target);
|
||||
arp->sender_ip_addr(sender);
|
||||
if (hw_snd) {
|
||||
memcpy(arp->_arp.ar_sha, hw_snd, 6);
|
||||
arp->sender_hw_addr(hw_snd);
|
||||
}
|
||||
arp->opcode(REQUEST);
|
||||
|
||||
@@ -78,10 +78,10 @@ Tins::PDU* Tins::ARP::make_arp_reply(const string& iface,
|
||||
|
||||
/* Create ARP packet and set its attributes */
|
||||
ARP* arp = new ARP();
|
||||
arp->_arp.ar_tip = target;
|
||||
arp->_arp.ar_sip = sender;
|
||||
memcpy(arp->_arp.ar_sha, hw_snd, 6);
|
||||
memcpy(arp->_arp.ar_tha, hw_tgt, 6);
|
||||
arp->target_ip_addr(target);
|
||||
arp->sender_ip_addr(sender);
|
||||
arp->target_hw_addr(hw_tgt);
|
||||
arp->sender_hw_addr(hw_snd);
|
||||
arp->opcode(REPLY);
|
||||
|
||||
/* Create the EthernetII PDU with the ARP PDU as its inner PDU */
|
||||
@@ -91,17 +91,17 @@ Tins::PDU* Tins::ARP::make_arp_reply(const string& iface,
|
||||
|
||||
Tins::ARP::ARP() : PDU(0x0608) {
|
||||
std::memset(&_arp, 0, sizeof(arphdr));
|
||||
_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;
|
||||
this->hw_addr_format(1);
|
||||
this->prot_addr_format(0x0800);
|
||||
this->hw_addr_length(6);
|
||||
this->prot_addr_length(4);
|
||||
}
|
||||
|
||||
Tins::ARP::ARP(arphdr *arp_ptr) : PDU(Utils::net_to_host_s(0x0806)) {
|
||||
memcpy(&_arp, arp_ptr, sizeof(arphdr));
|
||||
}
|
||||
|
||||
void Tins::ARP::sender_hw_addr(uint8_t* new_snd_hw_addr) {
|
||||
void Tins::ARP::sender_hw_addr(const uint8_t* new_snd_hw_addr) {
|
||||
memcpy(this->_arp.ar_sha, new_snd_hw_addr, 6); //Should this use hardware address' length?
|
||||
}
|
||||
|
||||
@@ -109,7 +109,11 @@ void Tins::ARP::sender_ip_addr(uint32_t new_snd_ip_addr) {
|
||||
this->_arp.ar_sip = new_snd_ip_addr;
|
||||
}
|
||||
|
||||
void Tins::ARP::target_hw_addr(uint8_t* new_tgt_hw_addr) {
|
||||
void Tins::ARP::sender_ip_addr(const string& new_snd_ip_addr) {
|
||||
this->_arp.ar_sip = Utils::ip_to_int(new_snd_ip_addr);
|
||||
}
|
||||
|
||||
void Tins::ARP::target_hw_addr(const uint8_t* new_tgt_hw_addr) {
|
||||
memcpy(this->_arp.ar_tha, new_tgt_hw_addr, 6); //Should this use hardware address' length?
|
||||
}
|
||||
|
||||
@@ -117,6 +121,10 @@ void Tins::ARP::target_ip_addr(uint32_t new_tgt_ip_addr) {
|
||||
this->_arp.ar_tip = new_tgt_ip_addr;
|
||||
}
|
||||
|
||||
void Tins::ARP::target_ip_addr(const std::string& new_tgt_ip_addr) {
|
||||
this->_arp.ar_tip = Utils::ip_to_int(new_tgt_ip_addr);
|
||||
}
|
||||
|
||||
void Tins::ARP::hw_addr_format(uint16_t new_hw_addr_fmt) {
|
||||
this->_arp.ar_hrd = Utils::net_to_host_s(new_hw_addr_fmt);
|
||||
}
|
||||
@@ -138,10 +146,10 @@ 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);
|
||||
this->target_ip_addr(ip_tgt);
|
||||
this->sender_ip_addr(ip_snd);
|
||||
if (hw_snd)
|
||||
memcpy(this->_arp.ar_sha, hw_snd, 6);
|
||||
this->sender_hw_addr(hw_snd);
|
||||
this->opcode(REQUEST);
|
||||
}
|
||||
|
||||
@@ -150,10 +158,10 @@ void Tins::ARP::set_arp_reply(const std::string& ip_tgt,
|
||||
const uint8_t* hw_tgt,
|
||||
const uint8_t* hw_snd) {
|
||||
|
||||
this->_arp.ar_tip = Utils::resolve_ip(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->target_ip_addr(ip_tgt);
|
||||
this->sender_ip_addr(ip_snd);
|
||||
this->sender_hw_addr(hw_snd);
|
||||
this->target_hw_addr(hw_tgt);
|
||||
this->opcode(REPLY);
|
||||
|
||||
}
|
||||
|
||||
47
src/ip.cpp
47
src/ip.cpp
@@ -53,14 +53,20 @@ Tins::IP::IP(uint32_t ip_dst, uint32_t ip_src, PDU *child) : PDU(IPPROTO_IP, chi
|
||||
_ip.saddr = ip_src;
|
||||
}
|
||||
|
||||
Tins::IP::~IP() {
|
||||
for (vector<IpOption>::iterator it = this->_ip_options.begin(); it != this->_ip_options.end(); it++) {
|
||||
if (it->optional_data)
|
||||
delete[] it->optional_data;
|
||||
}
|
||||
}
|
||||
|
||||
void Tins::IP::init_ip_fields() {
|
||||
memset(&_ip, 0, sizeof(iphdr));
|
||||
_ip.version = 4;
|
||||
_ip.ihl = sizeof(iphdr) / sizeof(uint32_t);
|
||||
_ip.ttl = DEFAULT_TTL;
|
||||
_ip.id = Utils::net_to_host_s(1);
|
||||
_options_size = 0;
|
||||
_padded_options_size = 0;
|
||||
this->_ip.version = 4;
|
||||
this->ttl(DEFAULT_TTL);
|
||||
this->id(1);
|
||||
this->_options_size = 0;
|
||||
this->_padded_options_size = 0;
|
||||
}
|
||||
|
||||
/* Setters */
|
||||
@@ -109,6 +115,10 @@ void Tins::IP::dest_address(uint32_t ip) {
|
||||
_ip.daddr = ip;
|
||||
}
|
||||
|
||||
void Tins::IP::head_len(uint8_t new_head_len) {
|
||||
this->_ip.ihl = new_head_len;
|
||||
}
|
||||
|
||||
void Tins::IP::set_option_eol() {
|
||||
this->set_option(0, IP::CONTROL, IP::IPOPT_END);
|
||||
}
|
||||
@@ -196,21 +206,28 @@ void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU
|
||||
}
|
||||
else
|
||||
new_flag = IPPROTO_IP;
|
||||
flag(new_flag);
|
||||
_ip.protocol = new_flag;
|
||||
_ip.tot_len = Utils::net_to_host_s(total_sz);
|
||||
_ip.ihl = my_sz / sizeof(uint32_t);
|
||||
this->flag(new_flag);
|
||||
this->protocol(new_flag);
|
||||
this->tot_len(total_sz);
|
||||
this->head_len (my_sz / sizeof(uint32_t));
|
||||
|
||||
memcpy(buffer, &_ip, sizeof(iphdr));
|
||||
|
||||
uint8_t* ptr_buffer = buffer + sizeof(iphdr);
|
||||
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);
|
||||
|
||||
if (parent && !_ip.check) {
|
||||
uint32_t checksum = PDU::do_checksum(buffer, buffer + sizeof(iphdr));
|
||||
uint32_t checksum = PDU::do_checksum(buffer, buffer + sizeof(iphdr) + _padded_options_size);
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||
((iphdr*)buffer)->check = Utils::net_to_host_s(~checksum);
|
||||
this->check(0);
|
||||
}
|
||||
/* IP Options here... */
|
||||
buffer += sizeof(iphdr);
|
||||
for (uint32_t i = 0; i < _ip_options.size(); ++i)
|
||||
buffer = _ip_options[i].write(buffer);
|
||||
|
||||
|
||||
}
|
||||
|
||||
bool Tins::IP::matches_response(uint8_t *ptr, uint32_t total_sz) {
|
||||
|
||||
Reference in New Issue
Block a user