mirror of
https://github.com/mfontanini/libtins
synced 2026-01-26 20:01:35 +01:00
Code cleanup and use same syntax on the entire project
Initial code cleanup More code cleanup Cleanup more code Cleanup Dot11 code Fix OSX build issue Cleanup examples Fix ref and pointer declaration syntax Fix braces
This commit is contained in:
79
src/udp.cpp
79
src/udp.cpp
@@ -43,50 +43,49 @@ using Tins::Memory::OutputMemoryStream;
|
||||
|
||||
namespace Tins {
|
||||
|
||||
UDP::UDP(uint16_t dport, uint16_t sport) : _udp()
|
||||
{
|
||||
UDP::UDP(uint16_t dport, uint16_t sport)
|
||||
: header_() {
|
||||
this->dport(dport);
|
||||
this->sport(sport);
|
||||
}
|
||||
|
||||
UDP::UDP(const uint8_t *buffer, uint32_t total_sz)
|
||||
{
|
||||
UDP::UDP(const uint8_t* buffer, uint32_t total_sz) {
|
||||
InputMemoryStream stream(buffer, total_sz);
|
||||
stream.read(_udp);
|
||||
stream.read(header_);
|
||||
if (stream) {
|
||||
inner_pdu(new RawPDU(stream.pointer(), stream.size()));
|
||||
}
|
||||
}
|
||||
|
||||
void UDP::dport(uint16_t new_dport) {
|
||||
_udp.dport = Endian::host_to_be(new_dport);
|
||||
header_.dport = Endian::host_to_be(new_dport);
|
||||
}
|
||||
|
||||
void UDP::sport(uint16_t new_sport) {
|
||||
_udp.sport = Endian::host_to_be(new_sport);
|
||||
header_.sport = Endian::host_to_be(new_sport);
|
||||
}
|
||||
|
||||
void UDP::length(uint16_t new_len) {
|
||||
_udp.len = Endian::host_to_be(new_len);
|
||||
header_.len = Endian::host_to_be(new_len);
|
||||
}
|
||||
|
||||
uint32_t UDP::header_size() const {
|
||||
return sizeof(udphdr);
|
||||
return sizeof(udp_header);
|
||||
}
|
||||
|
||||
uint32_t sum_range(const uint8_t *start, const uint8_t *end) {
|
||||
uint32_t sum_range(const uint8_t* start, const uint8_t* end) {
|
||||
uint32_t checksum(0);
|
||||
const uint8_t *last = end;
|
||||
const uint8_t* last = end;
|
||||
uint16_t buffer = 0;
|
||||
uint16_t padding = 0;
|
||||
const uint8_t *ptr = start;
|
||||
const uint8_t* ptr = start;
|
||||
|
||||
if(((end - start) & 1) == 1) {
|
||||
if (((end - start) & 1) == 1) {
|
||||
last = end - 1;
|
||||
padding = Endian::host_to_le<uint16_t>(*(end - 1));
|
||||
}
|
||||
|
||||
while(ptr < last) {
|
||||
while (ptr < last) {
|
||||
memcpy(&buffer, ptr, sizeof(uint16_t));
|
||||
checksum += buffer;
|
||||
ptr += sizeof(uint16_t);
|
||||
@@ -104,26 +103,26 @@ uint32_t pseudoheader_checksum(IPv4Address source_ip, IPv4Address dest_ip, uint3
|
||||
stream.write(dest_ip);
|
||||
stream.write(Endian::host_to_be<uint16_t>(flag));
|
||||
stream.write(Endian::host_to_be<uint16_t>(len));
|
||||
uint16_t *ptr = (uint16_t*)buffer, *end = (uint16_t*)(buffer + sizeof(buffer));
|
||||
uint16_t* ptr = (uint16_t*)buffer, *end = (uint16_t*)(buffer + sizeof(buffer));
|
||||
while (ptr < end) {
|
||||
checksum += *ptr++;
|
||||
}
|
||||
return checksum;
|
||||
}
|
||||
|
||||
void UDP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
|
||||
void UDP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
// Set checksum to 0, we'll calculate it at the end
|
||||
_udp.check = 0;
|
||||
if(inner_pdu()) {
|
||||
length(static_cast<uint16_t>(sizeof(udphdr) + inner_pdu()->size()));
|
||||
header_.check = 0;
|
||||
if (inner_pdu()) {
|
||||
length(static_cast<uint16_t>(sizeof(udp_header) + inner_pdu()->size()));
|
||||
}
|
||||
else {
|
||||
length(static_cast<uint16_t>(sizeof(udphdr)));
|
||||
length(static_cast<uint16_t>(sizeof(udp_header)));
|
||||
}
|
||||
stream.write(_udp);
|
||||
stream.write(header_);
|
||||
uint32_t checksum = 0;
|
||||
if (const Tins::IP *ip_packet = tins_cast<const Tins::IP*>(parent)) {
|
||||
if (const Tins::IP* ip_packet = tins_cast<const Tins::IP*>(parent)) {
|
||||
checksum = Utils::pseudoheader_checksum(
|
||||
ip_packet->src_addr(),
|
||||
ip_packet->dst_addr(),
|
||||
@@ -131,7 +130,7 @@ void UDP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *par
|
||||
Constants::IP::PROTO_UDP
|
||||
) + Utils::sum_range(buffer, buffer + total_sz);
|
||||
}
|
||||
else if (const Tins::IPv6 *ip6_packet = tins_cast<const Tins::IPv6*>(parent)) {
|
||||
else if (const Tins::IPv6* ip6_packet = tins_cast<const Tins::IPv6*>(parent)) {
|
||||
checksum = Utils::pseudoheader_checksum(
|
||||
ip6_packet->src_addr(),
|
||||
ip6_packet->dst_addr(),
|
||||
@@ -145,27 +144,29 @@ void UDP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *par
|
||||
while (checksum >> 16) {
|
||||
checksum = (checksum & 0xffff)+(checksum >> 16);
|
||||
}
|
||||
_udp.check = ~checksum;
|
||||
header_.check = ~checksum;
|
||||
// If checksum is 0, it has to be set to 0xffff
|
||||
_udp.check = (_udp.check == 0) ? 0xffff : _udp.check;
|
||||
((udphdr*)buffer)->check = _udp.check;
|
||||
header_.check = (header_.check == 0) ? 0xffff : header_.check;
|
||||
((udp_header*)buffer)->check = header_.check;
|
||||
}
|
||||
|
||||
bool UDP::matches_response(const uint8_t *ptr, uint32_t total_sz) const {
|
||||
if(total_sz < sizeof(udphdr))
|
||||
bool UDP::matches_response(const uint8_t* ptr, uint32_t total_sz) const {
|
||||
if (total_sz < sizeof(udp_header)) {
|
||||
return false;
|
||||
const udphdr *udp_ptr = (const udphdr*)ptr;
|
||||
if(udp_ptr->sport == _udp.dport && udp_ptr->dport == _udp.sport) {
|
||||
return inner_pdu()
|
||||
?
|
||||
inner_pdu()->matches_response(
|
||||
ptr + sizeof(udphdr),
|
||||
total_sz - sizeof(udphdr)
|
||||
)
|
||||
:
|
||||
0;
|
||||
}
|
||||
const udp_header* udp_ptr = (const udp_header*)ptr;
|
||||
if (udp_ptr->sport == header_.dport && udp_ptr->dport == header_.sport) {
|
||||
if (inner_pdu()) {
|
||||
return inner_pdu()->matches_response(
|
||||
ptr + sizeof(udp_header),
|
||||
total_sz - sizeof(udp_header)
|
||||
);
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
} // Tins
|
||||
|
||||
Reference in New Issue
Block a user