mirror of
https://github.com/mfontanini/libtins
synced 2026-01-30 05:24:26 +01:00
Added IPv4Address class.
This commit is contained in:
47
src/arp.cpp
47
src/arp.cpp
@@ -34,7 +34,8 @@
|
||||
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) {
|
||||
Tins::ARP::ARP(IPv4Address target_ip, IPv4Address sender_ip,
|
||||
const uint8_t *target_hw, const uint8_t *sender_hw) : PDU(0x0608) {
|
||||
memset(&_arp, 0, sizeof(arphdr));
|
||||
hw_addr_format((uint16_t)Tins::Constants::ARP::ETHER);
|
||||
prot_addr_format((uint16_t)Tins::Constants::Ethernet::IP);
|
||||
@@ -65,26 +66,18 @@ 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?
|
||||
}
|
||||
|
||||
void Tins::ARP::sender_ip_addr(uint32_t new_snd_ip_addr) {
|
||||
void Tins::ARP::sender_ip_addr(IPv4Address new_snd_ip_addr) {
|
||||
this->_arp.ar_sip = Utils::net_to_host_l(new_snd_ip_addr);
|
||||
}
|
||||
|
||||
void Tins::ARP::sender_ip_addr(const string& new_snd_ip_addr) {
|
||||
this->_arp.ar_sip = Utils::net_to_host_l(Utils::resolve_ip(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?
|
||||
}
|
||||
|
||||
void Tins::ARP::target_ip_addr(uint32_t new_tgt_ip_addr) {
|
||||
void Tins::ARP::target_ip_addr(IPv4Address new_tgt_ip_addr) {
|
||||
this->_arp.ar_tip = Utils::net_to_host_l(new_tgt_ip_addr);
|
||||
}
|
||||
|
||||
void Tins::ARP::target_ip_addr(const std::string& new_tgt_ip_addr) {
|
||||
this->_arp.ar_tip = Utils::net_to_host_l(Utils::resolve_ip(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);
|
||||
}
|
||||
@@ -155,18 +148,9 @@ Tins::PDU *Tins::ARP::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
|
||||
return cloned;
|
||||
}
|
||||
|
||||
Tins::PDU* Tins::ARP::make_arp_request(const string& iface,
|
||||
const string& target,
|
||||
const string& sender,
|
||||
const uint8_t* hw_snd) {
|
||||
uint32_t target_ip = Tins::Utils::resolve_ip(target);
|
||||
uint32_t sender_ip = Tins::Utils::resolve_ip(sender);
|
||||
return make_arp_request(iface, target_ip, sender_ip, hw_snd);
|
||||
}
|
||||
|
||||
Tins::PDU* Tins::ARP::make_arp_request(const std::string& iface,
|
||||
uint32_t target,
|
||||
uint32_t sender,
|
||||
IPv4Address target,
|
||||
IPv4Address sender,
|
||||
const uint8_t* hw_snd) {
|
||||
|
||||
/* Create ARP packet and set its attributes */
|
||||
@@ -183,23 +167,8 @@ Tins::PDU* Tins::ARP::make_arp_request(const std::string& iface,
|
||||
return eth;
|
||||
}
|
||||
|
||||
Tins::PDU* Tins::ARP::make_arp_reply(const string& iface,
|
||||
const string& target,
|
||||
const string& sender,
|
||||
const uint8_t* hw_tgt,
|
||||
const uint8_t* hw_snd) {
|
||||
|
||||
uint32_t target_ip = Tins::Utils::resolve_ip(target);
|
||||
uint32_t sender_ip = Tins::Utils::resolve_ip(sender);
|
||||
return make_arp_reply(iface, target_ip, sender_ip, hw_tgt, hw_snd);
|
||||
}
|
||||
|
||||
Tins::PDU* Tins::ARP::make_arp_reply(const string& iface,
|
||||
uint32_t target,
|
||||
uint32_t sender,
|
||||
const uint8_t* hw_tgt,
|
||||
const uint8_t* hw_snd) {
|
||||
|
||||
Tins::PDU* Tins::ARP::make_arp_reply(const string& iface, IPv4Address target,
|
||||
IPv4Address sender, const uint8_t* hw_tgt, const uint8_t* hw_snd) {
|
||||
/* Create ARP packet and set its attributes */
|
||||
ARP* arp = new ARP(target, sender, hw_tgt, hw_snd);
|
||||
arp->opcode(REPLY);
|
||||
|
||||
@@ -274,7 +274,7 @@ bool Tins::DHCP::generic_search(Options opt, std::list<uint32_t> *container) {
|
||||
if((len % sizeof(uint32_t)) != 0)
|
||||
return false;
|
||||
while(len) {
|
||||
container->push_back(*(ptr++));
|
||||
container->push_back(Utils::net_to_host_l(*(ptr++)));
|
||||
len -= sizeof(uint32_t);
|
||||
}
|
||||
return true;
|
||||
|
||||
32
src/ip.cpp
32
src/ip.cpp
@@ -40,13 +40,11 @@ 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(Constants::IP::PROTO_IP, child) {
|
||||
Tins::IP::IP(IPv4Address ip_dst, IPv4Address ip_src, PDU *child) :
|
||||
PDU(Constants::IP::PROTO_IP, child) {
|
||||
init_ip_fields();
|
||||
if(ip_dst.size())
|
||||
this->dst_addr(ip_dst);
|
||||
if(ip_src.size())
|
||||
this->src_addr(ip_src);
|
||||
|
||||
this->dst_addr(ip_dst);
|
||||
this->src_addr(ip_src);
|
||||
}
|
||||
|
||||
Tins::IP::IP(const IP &other) : PDU(other) {
|
||||
@@ -144,12 +142,6 @@ Tins::IP::IP(const uint8_t *buffer, uint32_t total_sz) : PDU(Constants::IP::PROT
|
||||
}
|
||||
}
|
||||
|
||||
Tins::IP::IP(uint32_t ip_dst, uint32_t ip_src, PDU *child) : PDU(Constants::IP::PROTO_IP, child) {
|
||||
init_ip_fields();
|
||||
this->dst_addr(ip_dst);
|
||||
this->src_addr(ip_src);
|
||||
}
|
||||
|
||||
Tins::IP::~IP() {
|
||||
cleanup();
|
||||
}
|
||||
@@ -199,20 +191,14 @@ void Tins::IP::check(uint16_t new_check) {
|
||||
_ip.check = Utils::net_to_host_s(new_check);
|
||||
}
|
||||
|
||||
void Tins::IP::src_addr(const string &ip) {
|
||||
_ip.saddr = Utils::net_to_host_l(Utils::resolve_ip(ip));
|
||||
|
||||
void Tins::IP::src_addr(IPv4Address ip) {
|
||||
_ip.saddr = ip;
|
||||
}
|
||||
|
||||
void Tins::IP::src_addr(uint32_t ip) {
|
||||
_ip.saddr = Utils::net_to_host_l(ip);
|
||||
}
|
||||
|
||||
void Tins::IP::dst_addr(const string &ip) {
|
||||
_ip.daddr = Utils::net_to_host_l(Utils::resolve_ip(ip));
|
||||
}
|
||||
|
||||
void Tins::IP::dst_addr(uint32_t ip) {
|
||||
_ip.daddr = Utils::net_to_host_l(ip);
|
||||
void Tins::IP::dst_addr(IPv4Address ip) {
|
||||
_ip.daddr = ip;
|
||||
}
|
||||
|
||||
void Tins::IP::head_len(uint8_t new_head_len) {
|
||||
|
||||
Reference in New Issue
Block a user