1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-27 20:24:26 +01:00

UDP checksum is now calculated always inside write_serialization.

This commit is contained in:
Matias Fontanini
2013-04-12 13:42:11 -03:00
parent f4522acd44
commit 30d6a4f2e0
3 changed files with 17 additions and 9 deletions

View File

@@ -91,13 +91,15 @@ namespace Tins {
*/
void dport(uint16_t new_dport);
/** \brief Set the source port.
/**
* \brief Set the source port.
*
* \param new_sport The new source port.
*/
void sport(uint16_t new_sport);
/** \brief Getter for the length field.
/**
* \brief Getter for the length field.
* \param new_len The new length field.
* \return The length field.
*/
@@ -115,7 +117,8 @@ namespace Tins {
*/
bool matches_response(uint8_t *ptr, uint32_t total_sz);
/** \brief Returns the header size.
/**
* \brief Returns the header size.
*
* This metod overrides PDU::header_size. This size includes the
* payload and options size. \sa PDU::header_size

View File

@@ -77,19 +77,24 @@ void UDP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *par
assert(total_sz >= sizeof(udphdr));
#endif
const Tins::IP *ip_packet = dynamic_cast<const Tins::IP*>(parent);
_udp.check = 0;
if(inner_pdu())
length(sizeof(udphdr) + inner_pdu()->size());
else
length(sizeof(udphdr));
std::memcpy(buffer, &_udp, sizeof(udphdr));
if(!_udp.check && ip_packet) {
uint32_t checksum = Utils::pseudoheader_checksum(ip_packet->src_addr(), ip_packet->dst_addr(), size(), Constants::IP::PROTO_UDP) +
Utils::do_checksum(buffer, buffer + total_sz);
if(ip_packet) {
uint32_t checksum = Utils::pseudoheader_checksum(
ip_packet->src_addr(),
ip_packet->dst_addr(),
size(),
Constants::IP::PROTO_UDP
) + Utils::do_checksum(buffer, buffer + total_sz);
while (checksum >> 16)
checksum = (checksum & 0xffff)+(checksum >> 16);
((udphdr*)buffer)->check = Endian::host_to_be<uint16_t>(~checksum);
_udp.check = Endian::host_to_be<uint16_t>(~checksum);
((udphdr*)buffer)->check = _udp.check;
}
_udp.check = 0;
}
bool UDP::matches_response(uint8_t *ptr, uint32_t total_sz) {

View File

@@ -20,7 +20,7 @@ public:
};
const uint8_t IPTest::expected_packet[] = {
40, 127, 0, 32, 0, 122, 0, 67, 21, 1, 251, 103, 84, 52, 254, 5, 192,
40, 127, 0, 32, 0, 122, 0, 67, 21, 1, 0, 0, 84, 52, 254, 5, 192,
168, 9, 43, 130, 11, 116, 106, 103, 171, 119, 171, 104, 101, 108, 0
};