Skip to content

Commit b371521

Browse files
committed
MODIFIED: IMPLEMENTATION OF TRANSMISSION CONTROL PROTOCOL
1 parent 9cbfd8f commit b371521

1 file changed

Lines changed: 50 additions & 10 deletions

File tree

src/entry.cpp

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "utils/decIPv4.h"
55
#include "parsers/arp.h"
66
#include "utils/decEthernet.h"
7+
#include "parsers/tcp.h"
78

89
#include <iostream>
910
#include <unistd.h>
@@ -72,17 +73,56 @@ int run_entry() {
7273
std::cout << " Source IP: " << parsers::ipv4_to_string(ipv4.src_ip) << "\n";
7374
std::cout << " Destination IP: " << parsers::ipv4_to_string(ipv4.dest_ip) << "\n";
7475
std::cout << " Payload Length: " << ipv4.payload_length << " bytes\n";
75-
if (ipv4.payload_length > 0) {
76-
std::cout << " Payload (hex): \n";
77-
std::cout << " ";
78-
for (size_t i = 0; i < ipv4.payload_length && i < 64; ++i) {
79-
std::cout << std::hex << std::setw(2) << std::setfill('0')
80-
<< static_cast<int>(ipv4.payload[i]) << " ";
81-
if ((i + 1) % 16 == 0) std::cout << "\n ";
76+
// if (ipv4.payload_length > 0) {
77+
// std::cout << " Payload (hex): \n";
78+
// std::cout << " ";
79+
// for (size_t i = 0; i < ipv4.payload_length && i < 64; ++i) {
80+
// std::cout << std::hex << std::setw(2) << std::setfill('0')
81+
// << static_cast<int>(ipv4.payload[i]) << " ";
82+
// if ((i + 1) % 16 == 0) std::cout << "\n ";
83+
// }
84+
// std::cout << "\n";
85+
// } else {
86+
// std::cout << " No Payload\n";
87+
// }
88+
89+
if ((ipv4.protocol) == 6) {
90+
parsers::TCPHeader tcp = parsers::parse_tcp_header(ipv4.payload, ipv4.payload_length);
91+
92+
std::cout << " TCP Packet: \n";
93+
std::cout << " Source Port: " << tcp.src_port << "\n";
94+
std::cout << " Destination Port: " << tcp.dest_port << "\n";
95+
std::cout << " Sequence Number: " << tcp.seq_num << "\n";
96+
std::cout << " Acknowledgment Number: " << tcp.ack_num << "\n";
97+
std::cout << " Data Offset (header length in bytes): " << (tcp.data_offset * 4) << "\n";
98+
99+
// Print flags individually
100+
std::cout << " Flags:\n";
101+
std::cout << " NS: " << tcp.ns_flag << "\n";
102+
std::cout << " CWR: " << tcp.cwr_flag << "\n";
103+
std::cout << " ECE: " << tcp.ece_flag << "\n";
104+
std::cout << " URG: " << tcp.urg_flag << "\n";
105+
std::cout << " ACK: " << tcp.ack_flag << "\n";
106+
std::cout << " PSH: " << tcp.psh_flag << "\n";
107+
std::cout << " RST: " << tcp.rst_flag << "\n";
108+
std::cout << " SYN: " << tcp.syn_flag << "\n";
109+
std::cout << " FIN: " << tcp.fin_flag << "\n";
110+
111+
std::cout << " Window Size: " << tcp.window_size << "\n";
112+
std::cout << " Checksum: " << decoders::checksum_to_string(tcp.checksum) << "\n";
113+
std::cout << " Urgent Pointer: " << tcp.urgent_pointer << "\n";
114+
std::cout << " Options Length: " << static_cast<int>(tcp.options_length) << "\n";
115+
116+
if (tcp.options_length > 0) {
117+
std::cout << " Options (hex): ";
118+
for (size_t i = 0; i < tcp.options.size(); ++i) {
119+
std::cout << std::hex << std::setw(2) << std::setfill('0')
120+
<< static_cast<int>(tcp.options[i]) << " ";
121+
}
122+
std::cout << std::dec << "\n"; // reset stream to decimal
123+
} else {
124+
std::cout << " No TCP Options\n";
82125
}
83-
std::cout << "\n";
84-
} else {
85-
std::cout << " No Payload\n";
86126
}
87127
}
88128
// ARP Protocol Implementation

0 commit comments

Comments
 (0)