|
| 1 | +#include "parsers/tcp.h" |
| 2 | + |
| 3 | +#include <sstream> |
| 4 | +#include <iomanip> |
| 5 | +#include <stdexcept> |
| 6 | +#include <cstring> |
| 7 | +#include <arpa/inet.h> |
| 8 | +#include <iostream> |
| 9 | + |
| 10 | +namespace parsers { |
| 11 | + |
| 12 | + TCPHeader parse_tcp_header(const uint8_t* buffer, std::size_t length) { |
| 13 | + TCPHeader tcp{}; |
| 14 | + |
| 15 | + // Minimum TCP header size is 20 bytes (without options) |
| 16 | + if (length < 20) { |
| 17 | + std::cerr << "Buffer too short for TCP header\n"; |
| 18 | + return tcp; |
| 19 | + } |
| 20 | + |
| 21 | + // Parse fixed header fields |
| 22 | + tcp.src_port = ntohs(*reinterpret_cast<const uint16_t*>(buffer)); |
| 23 | + tcp.dest_port = ntohs(*reinterpret_cast<const uint16_t*>(buffer + 2)); |
| 24 | + tcp.seq_num = ntohl(*reinterpret_cast<const uint32_t*>(buffer + 4)); |
| 25 | + tcp.ack_num = ntohl(*reinterpret_cast<const uint32_t*>(buffer + 8)); |
| 26 | + |
| 27 | + uint8_t data_offset_reserved_ns = buffer[12]; |
| 28 | + uint8_t flags_byte = buffer[13]; |
| 29 | + |
| 30 | + // Extract Data Offset (upper 4 bits) - number of 32-bit words |
| 31 | + tcp.data_offset = (data_offset_reserved_ns >> 4) & 0x0F; |
| 32 | + // Calculate header length in bytes |
| 33 | + size_t header_length_bytes = tcp.data_offset * 4; |
| 34 | + |
| 35 | + // Extract Reserved (3 bits) |
| 36 | + tcp.reserved = (data_offset_reserved_ns >> 1) & 0x07; |
| 37 | + |
| 38 | + // Extract NS flag (least significant bit of byte 12) |
| 39 | + tcp.ns_flag = (data_offset_reserved_ns & 0x01) != 0; |
| 40 | + |
| 41 | + // Extract flags (byte 13) |
| 42 | + tcp.cwr_flag = (flags_byte & 0x80) != 0; |
| 43 | + tcp.ece_flag = (flags_byte & 0x40) != 0; |
| 44 | + tcp.urg_flag = (flags_byte & 0x20) != 0; |
| 45 | + tcp.ack_flag = (flags_byte & 0x10) != 0; |
| 46 | + tcp.psh_flag = (flags_byte & 0x08) != 0; |
| 47 | + tcp.rst_flag = (flags_byte & 0x04) != 0; |
| 48 | + tcp.syn_flag = (flags_byte & 0x02) != 0; |
| 49 | + tcp.fin_flag = (flags_byte & 0x01) != 0; |
| 50 | + |
| 51 | + tcp.window_size = ntohs(*reinterpret_cast<const uint16_t*>(buffer + 14)); |
| 52 | + tcp.checksum = ntohs(*reinterpret_cast<const uint16_t*>(buffer + 16)); |
| 53 | + tcp.urgent_pointer = ntohs(*reinterpret_cast<const uint16_t*>(buffer + 18)); |
| 54 | + |
| 55 | + // Validate header length |
| 56 | + if (length < header_length_bytes) { |
| 57 | + std::cerr << "TCP header length field exceeds buffer length\n"; |
| 58 | + return tcp; |
| 59 | + } |
| 60 | + |
| 61 | + // Calculate options length (header length minus fixed 20 bytes) |
| 62 | + tcp.options_length = static_cast<uint8_t>(header_length_bytes - 20); |
| 63 | + |
| 64 | + // Copy options if present |
| 65 | + if (tcp.options_length > 0) { |
| 66 | + tcp.options.assign(buffer + 20, buffer + 20 + tcp.options_length); |
| 67 | + } |
| 68 | + |
| 69 | + // Set payload pointer and length |
| 70 | + tcp.payload = buffer + header_length_bytes; |
| 71 | + tcp.payload_length = static_cast<uint16_t>(length - header_length_bytes); |
| 72 | + |
| 73 | + return tcp; |
| 74 | + } |
| 75 | + |
| 76 | +} // namespace parsers |
0 commit comments