mirror of
https://github.com/mfontanini/libtins
synced 2026-01-29 04:54:28 +01:00
Fixed IP packet.
This commit is contained in:
@@ -39,7 +39,6 @@ namespace Tins {
|
||||
|
||||
/* Virtual methods */
|
||||
uint32_t header_size() const;
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz);
|
||||
private:
|
||||
struct iphdr {
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
@@ -63,6 +62,9 @@ namespace Tins {
|
||||
/*The options start here. */
|
||||
} __attribute__((packed));
|
||||
|
||||
void init_ip_fields();
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz);
|
||||
|
||||
iphdr _ip;
|
||||
};
|
||||
};
|
||||
|
||||
24
src/ip.cpp
24
src/ip.cpp
@@ -10,19 +10,28 @@ using namespace std;
|
||||
|
||||
|
||||
Tins::IP::IP(const string &ip_dst, const string &ip_src) : PDU(IPPROTO_IP) {
|
||||
memset(&_ip, 0, sizeof(iphdr));
|
||||
init_ip_fields();
|
||||
if(ip_dst.size())
|
||||
_ip.daddr = Utils::ip_to_int(ip_dst);
|
||||
if(ip_src.size())
|
||||
_ip.saddr = Utils::ip_to_int(ip_src);
|
||||
|
||||
}
|
||||
|
||||
Tins::IP::IP(uint32_t ip_dst, uint32_t ip_src) : PDU(IPPROTO_IP) {
|
||||
memset(&_ip, 0, sizeof(iphdr));
|
||||
init_ip_fields();
|
||||
_ip.daddr = ip_dst;
|
||||
_ip.saddr = ip_src;
|
||||
}
|
||||
|
||||
void Tins::IP::init_ip_fields() {
|
||||
memset(&_ip, 0, sizeof(iphdr));
|
||||
_ip.version = 4;
|
||||
_ip.ihl = sizeof(iphdr) / sizeof(uint32_t);
|
||||
}
|
||||
|
||||
/* Setters */
|
||||
|
||||
void Tins::IP::tos(uint8_t new_tos) {
|
||||
_ip.tos = new_tos;
|
||||
}
|
||||
@@ -67,11 +76,20 @@ void Tins::IP::dest_address(uint32_t ip) {
|
||||
_ip.daddr = ip;
|
||||
}
|
||||
|
||||
/* Virtual method overriding. */
|
||||
|
||||
uint32_t Tins::IP::header_size() const {
|
||||
return sizeof(iphdr);
|
||||
}
|
||||
|
||||
void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz) {
|
||||
assert(total_sz >= sizeof(iphdr));
|
||||
uint32_t my_sz = header_size() + trailer_size(), new_flag = inner_pdu()->flag();
|
||||
assert(total_sz >= my_sz);
|
||||
if(new_flag == IPPROTO_IP)
|
||||
new_flag = IPPROTO_IPIP;
|
||||
flag(new_flag);
|
||||
_ip.protocol = new_flag;
|
||||
_ip.tot_len = total_sz;
|
||||
memcpy(buffer, &_ip, sizeof(iphdr));
|
||||
/* IP Options here... */
|
||||
}
|
||||
|
||||
@@ -37,10 +37,10 @@ uint8_t *Tins::PDU::serialize(uint32_t &sz) {
|
||||
|
||||
void Tins::PDU::serialize(uint8_t *buffer, uint32_t total_sz) {
|
||||
uint32_t sz = header_size() + trailer_size();
|
||||
write_serialization(buffer, total_sz);
|
||||
/* Must not happen... */
|
||||
assert(total_sz >= sz);
|
||||
if(_inner_pdu)
|
||||
_inner_pdu->serialize(buffer + header_size(), total_sz - sz);
|
||||
write_serialization(buffer, total_sz);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user