1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-25 19:51:34 +01:00

Started fixing endianess issues.

This commit is contained in:
Matias Fontanini
2012-08-15 12:04:13 -03:00
parent 68ab7b094a
commit 892bc0ecd3
18 changed files with 301 additions and 142 deletions

View File

@@ -31,7 +31,9 @@
const uint16_t Tins::TCP::DEFAULT_WINDOW = 32678;
Tins::TCP::TCP(uint16_t dport, uint16_t sport) : PDU(Constants::IP::PROTO_TCP), _options_size(0), _total_options_size(0) {
Tins::TCP::TCP(uint16_t dport, uint16_t sport)
: PDU(Constants::IP::PROTO_TCP), _options_size(0), _total_options_size(0)
{
std::memset(&_tcp, 0, sizeof(tcphdr));
this->dport(dport);
this->sport(sport);
@@ -39,7 +41,9 @@ Tins::TCP::TCP(uint16_t dport, uint16_t sport) : PDU(Constants::IP::PROTO_TCP),
window(DEFAULT_WINDOW);
}
Tins::TCP::TCP(const uint8_t *buffer, uint32_t total_sz) : PDU(Constants::IP::PROTO_TCP) {
Tins::TCP::TCP(const uint8_t *buffer, uint32_t total_sz)
: PDU(Constants::IP::PROTO_TCP)
{
if(total_sz < sizeof(tcphdr))
throw std::runtime_error("Not enough size for an TCP header in the buffer.");
std::memcpy(&_tcp, buffer, sizeof(tcphdr));
@@ -84,31 +88,31 @@ Tins::TCP::TCP(const uint8_t *buffer, uint32_t total_sz) : PDU(Constants::IP::PR
}
void Tins::TCP::dport(uint16_t new_dport) {
_tcp.dport = Utils::net_to_host_s(new_dport);
_tcp.dport = Utils::to_be(new_dport);
}
void Tins::TCP::sport(uint16_t new_sport) {
_tcp.sport = Utils::net_to_host_s(new_sport);
_tcp.sport = Utils::to_be(new_sport);
}
void Tins::TCP::seq(uint32_t new_seq) {
_tcp.seq = Utils::net_to_host_l(new_seq);
_tcp.seq = Utils::to_be(new_seq);
}
void Tins::TCP::ack_seq(uint32_t new_ack_seq) {
_tcp.ack_seq = Utils::net_to_host_l(new_ack_seq);
_tcp.ack_seq = Utils::to_be(new_ack_seq);
}
void Tins::TCP::window(uint16_t new_window) {
_tcp.window = Utils::net_to_host_s(new_window);
_tcp.window = Utils::to_be(new_window);
}
void Tins::TCP::check(uint16_t new_check) {
_tcp.check = Utils::net_to_host_s(new_check);
_tcp.check = Utils::to_be(new_check);
}
void Tins::TCP::urg_ptr(uint16_t new_urg_ptr) {
_tcp.urg_ptr = Utils::net_to_host_s(new_urg_ptr);
_tcp.urg_ptr = Utils::to_be(new_urg_ptr);
}
void Tins::TCP::payload(uint8_t *new_payload, uint32_t new_payload_size) {
@@ -120,14 +124,14 @@ void Tins::TCP::data_offset(uint8_t new_doff) {
}
void Tins::TCP::add_mss_option(uint16_t value) {
value = Utils::net_to_host_s(value);
value = Utils::to_be(value);
add_option(MSS, 2, (uint8_t*)&value);
}
bool Tins::TCP::search_mss_option(uint16_t *value) {
if(!generic_search(MSS, value))
return false;
*value = Utils::net_to_host_s(*value);
*value = Utils::to_be(*value);
return true;
}
@@ -153,7 +157,7 @@ void Tins::TCP::add_sack_option(const std::list<uint32_t> &edges) {
value = new uint32_t[edges.size()];
uint32_t *ptr = value;
for(std::list<uint32_t>::const_iterator it = edges.begin(); it != edges.end(); ++it)
*(ptr++) = Utils::net_to_host_l(*it);
*(ptr++) = Utils::to_be(*it);
}
add_option(SACK, (uint8_t)(sizeof(uint32_t) * edges.size()), (const uint8_t*)value);
delete[] value;
@@ -166,12 +170,12 @@ bool Tins::TCP::search_sack_option(std::list<uint32_t> *edges) {
const uint32_t *ptr = (const uint32_t*)&option->value[0];
const uint32_t *end = ptr + (option->value.size() / sizeof(uint32_t));
while(ptr < end)
edges->push_back(Utils::net_to_host_l(*(ptr++)));
edges->push_back(Utils::to_be(*(ptr++)));
return true;
}
void Tins::TCP::add_timestamp_option(uint32_t value, uint32_t reply) {
uint64_t buffer = ((uint64_t)Utils::net_to_host_l(reply) << 32) | Utils::net_to_host_l(value);
uint64_t buffer = ((uint64_t)Utils::to_be(reply) << 32) | Utils::to_be(value);
add_option(TSOPT, 8, (uint8_t*)&buffer);
}
@@ -180,8 +184,8 @@ bool Tins::TCP::search_timestamp_option(uint32_t *value, uint32_t *reply) {
if(!option || option->value.size() != (sizeof(uint32_t) << 1))
return false;
const uint32_t *ptr = (const uint32_t*)&option->value[0];
*value = Utils::net_to_host_l(*(ptr++));
*reply = Utils::net_to_host_l(*(ptr));
*value = Utils::to_be(*(ptr++));
*reply = Utils::to_be(*(ptr));
return true;
}
@@ -293,13 +297,14 @@ void Tins::TCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PD
const Tins::IP *ip_packet = dynamic_cast<const Tins::IP*>(parent);
memcpy(tcp_start, &_tcp, sizeof(tcphdr));
if(!_tcp.check && ip_packet) {
uint32_t checksum = Utils::pseudoheader_checksum(Utils::net_to_host_l(ip_packet->src_addr()),
Utils::net_to_host_l(ip_packet->dst_addr()),
uint32_t checksum = Utils::pseudoheader_checksum(ip_packet->src_addr(),
ip_packet->dst_addr(),
size(), Constants::IP::PROTO_TCP) +
Utils::do_checksum(tcp_start, tcp_start + total_sz);
while (checksum >> 16)
checksum = (checksum & 0xffff) + (checksum >> 16);
((tcphdr*)tcp_start)->check = Utils::net_to_host_s(~checksum);
((tcphdr*)tcp_start)->check = Utils::to_be<uint16_t>(~checksum);
}
_tcp.check = 0;
}