mirror of
https://github.com/mfontanini/libtins
synced 2026-01-27 04:11:35 +01:00
Added RawPDU class. Removed payload member from TCP and UDP. They're not represented by RawPDUs.
This commit is contained in:
39
src/udp.cpp
39
src/udp.cpp
@@ -1,3 +1,24 @@
|
||||
/*
|
||||
* libtins is a net packet wrapper library for crafting and
|
||||
* interpreting sniffed packets.
|
||||
*
|
||||
* Copyright (C) 2011 Nasel
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef WIN32
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
@@ -6,8 +27,9 @@
|
||||
#include "utils.h"
|
||||
#include "udp.h"
|
||||
#include "ip.h"
|
||||
#include "rawpdu.h"
|
||||
|
||||
Tins::UDP::UDP(uint16_t sport, uint16_t dport) : PDU(IPPROTO_UDP), _payload(0), _payload_size(0) {
|
||||
Tins::UDP::UDP(uint16_t sport, uint16_t dport) : PDU(IPPROTO_UDP) {
|
||||
_udp.sport = sport;
|
||||
_udp.dport = dport;
|
||||
_udp.check = 0;
|
||||
@@ -15,9 +37,8 @@ Tins::UDP::UDP(uint16_t sport, uint16_t dport) : PDU(IPPROTO_UDP), _payload(0),
|
||||
}
|
||||
|
||||
void Tins::UDP::payload(uint8_t *new_payload, uint32_t new_payload_size) {
|
||||
_payload = new_payload;
|
||||
_payload_size = new_payload_size;
|
||||
_udp.len = Utils::net_to_host_s(sizeof(udphdr) + _payload_size);
|
||||
inner_pdu(new RawPDU(new_payload, new_payload_size));
|
||||
_udp.len = Utils::net_to_host_s(sizeof(udphdr) + new_payload_size);
|
||||
}
|
||||
|
||||
void Tins::UDP::dport(uint16_t new_dport) {
|
||||
@@ -29,21 +50,19 @@ void Tins::UDP::sport(uint16_t new_sport) {
|
||||
}
|
||||
|
||||
uint32_t Tins::UDP::header_size() const {
|
||||
/* Round? */
|
||||
return sizeof(udphdr) + _payload_size;
|
||||
return sizeof(udphdr);
|
||||
}
|
||||
|
||||
void Tins::UDP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
|
||||
assert(total_sz >= sizeof(udphdr) + _payload_size);
|
||||
assert(total_sz >= sizeof(udphdr));
|
||||
const IP *ip_packet = dynamic_cast<const IP*>(parent);
|
||||
if(!_udp.check && ip_packet) {
|
||||
uint32_t checksum = PDU::pseudoheader_checksum(ip_packet->source_address(), ip_packet->dest_address(), header_size(), IPPROTO_UDP) +
|
||||
PDU::do_checksum(_payload, _payload + _payload_size) + PDU::do_checksum((uint8_t*)&_udp, ((uint8_t*)&_udp) + sizeof(udphdr));
|
||||
uint32_t checksum = PDU::pseudoheader_checksum(ip_packet->source_address(), ip_packet->dest_address(), size(), IPPROTO_UDP) +
|
||||
PDU::do_checksum(buffer + sizeof(udphdr), buffer + total_sz) + PDU::do_checksum((uint8_t*)&_udp, ((uint8_t*)&_udp) + sizeof(udphdr));
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff)+(checksum >> 16);
|
||||
_udp.check = Utils::net_to_host_s(~checksum);
|
||||
}
|
||||
std::memcpy(buffer, &_udp, sizeof(udphdr));
|
||||
std::memcpy(buffer + sizeof(udphdr), _payload, _payload_size);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user