1+ // src/entry.cpp
12#include " config/interface.h"
23#include " parsers/ethernet.h"
34#include " parsers/ipv4.h"
89#include " parsers/http.h"
910#include " utils/decHttp.h"
1011
11-
1212#include < iostream>
1313#include < unistd.h>
1414#include < cstring>
@@ -27,10 +27,11 @@ int run_entry() {
2727 }
2828
2929 // Load interface from config file in build/config/interface.cfg
30- config::Interface iface;
30+ config::Interface iface;
3131 std::string iface_name = iface.get_interface ();
3232
33- if (setsockopt (sock, SOL_SOCKET , SO_BINDTODEVICE , iface_name.c_str (), iface_name.length ()) < 0 ) {
33+ // SO_BINDTODEVICE expects a char*; include terminating NUL
34+ if (setsockopt (sock, SOL_SOCKET , SO_BINDTODEVICE , iface_name.c_str (), iface_name.length () + 1 ) < 0 ) {
3435 perror ((" [-] Failed to bind to interface: " + iface_name).c_str ());
3536 close (sock);
3637 return 1 ;
@@ -47,18 +48,16 @@ int run_entry() {
4748 if (len < 0 ) {
4849 perror (" [-] Packet receive failed" );
4950 break ;
50-
5151 }
5252
5353 packet_number++;
5454
5555 // Step 3: Parse Ethernet frame (which dispatches to IPv4, etc.)
56- parsers::EthernetHeader eth = parsers::parse_ethernet_header (buffer, len);
56+ parsers::EthernetHeader eth = parsers::parse_ethernet_header (buffer, static_cast < size_t >( len) );
5757
5858 if (eth.eth_proto == " IPv4" ) {
5959 parsers::IPv4Header ipv4 = parsers::parse_ipv4_header (eth.payload , eth.payload_len );
6060
61-
6261 std::cout << " [+] Packet # " << packet_number << " " << parsers::ipv4_to_string (ipv4.src_ip ) << " ==> " << parsers::ipv4_to_string (ipv4.dest_ip ) << " \n " ;
6362 std::cout << " Ethernet Frame:\n " ;
6463 std::cout << " Destination MAC: " << eth.dest_mac_str << " \n " ;
@@ -74,24 +73,13 @@ int run_entry() {
7473 std::cout << " Fragment Offset: " << decoders::fragment_offset_to_string ((ipv4.flags_fragment_offset )) << " \n " ;
7574 std::cout << " Time To Live: " << decoders::ttl_to_string (static_cast <int >(ipv4.ttl )) << " \n " ;
7675 std::cout << " Protocol: " << decoders::protocol_to_string (static_cast <int >(ipv4.protocol )) << " \n " ;
77- std::cout << " Header Checksum: " << decoders::checksum_to_string (ipv4.header_checksum )<< " \n " ;
76+ std::cout << " Header Checksum: " << decoders::checksum_to_string (ipv4.header_checksum ) << " \n " ;
7877 std::cout << " Source IP: " << parsers::ipv4_to_string (ipv4.src_ip ) << " \n " ;
7978 std::cout << " Destination IP: " << parsers::ipv4_to_string (ipv4.dest_ip ) << " \n " ;
8079 std::cout << " Payload Length: " << ipv4.payload_length << " bytes\n " ;
81- // if (ipv4.payload_length > 0) {
82- // std::cout << " Payload (hex): \n";
83- // std::cout << " ";
84- // for (size_t i = 0; i < ipv4.payload_length && i < 64; ++i) {
85- // std::cout << std::hex << std::setw(2) << std::setfill('0')
86- // << static_cast<int>(ipv4.payload[i]) << " ";
87- // if ((i + 1) % 16 == 0) std::cout << "\n ";
88- // }
89- // std::cout << "\n";
90- // } else {
91- // std::cout << " No Payload\n";
92- // }
93-
94- if ((ipv4.protocol ) == 6 ) {
80+
81+ // If TCP (protocol number 6)
82+ if (ipv4.protocol == 6 ) {
9583 parsers::TCPHeader tcp = parsers::parse_tcp_header (ipv4.payload , ipv4.payload_length );
9684
9785 std::cout << " TCP Packet: \n " ;
@@ -117,36 +105,28 @@ int run_entry() {
117105 std::cout << " Checksum: " << decoders::checksum_to_string (tcp.checksum ) << " \n " ;
118106 std::cout << " Urgent Pointer: " << tcp.urgent_pointer << " \n " ;
119107 std::cout << " Options Length: " << static_cast <int >(tcp.options_length ) << " \n " ;
120-
121- // --- HTTP Parsing ---
122- parsers::HTTPHeader http = parsers::parse_http_header (tcp.payload , tcp.payload_length );
123- if (http.is_http ) {
124- std::cout << " [HTTP] Parsed Request/Response:\n "
125- << http.to_string () << std::endl;
126- }
127108
128109 if (tcp.options_length > 0 ) {
129110 std::cout << " Options (hex): " ;
130111 for (size_t i = 0 ; i < tcp.options .size (); ++i) {
131- std::cout << std::hex << std::setw (2 ) << std::setfill (' 0' )
132- << static_cast <int >(tcp.options [i]) << " " ;
112+ std::cout << std::hex << std::setw (2 ) << std::setfill (' 0' )
113+ << static_cast <int >(tcp.options [i]) << " " ;
133114 }
134115 std::cout << std::dec << " \n " ; // reset stream to decimal
135116 } else {
136117 std::cout << " No TCP Options\n " ;
137118 }
138- // HTTP parsing if any TCP payload exists
139- if (tcp.payload_length > 0 && tcp.payload != nullptr ) {
140- parsers::HTTPHeader http = parsers::parse_http_header (tcp.payload , tcp.payload_length );
141- if (http.is_http ) {
142- std::cout << http.to_string ();
143- }
144- }
145119
146-
147- }
148- }
149- // ARP Protocol Implementation
120+ // HTTP parsing if any TCP payload exists
121+ if (tcp.payload_length > 0 && tcp.payload != nullptr ) {
122+ parsers::HTTPHeader http = parsers::parse_http_header (tcp.payload , tcp.payload_length );
123+ if (http.is_http ) {
124+ std::cout << " [HTTP] Parsed Request/Response:\n "
125+ << http.to_string () << std::endl;
126+ }
127+ }
128+ } // end if tcp
129+ } // end if IPv4
150130 else if (eth.eth_proto == " ARP" ) {
151131 parsers::ARPHeader arp = parsers::parse_arp_header (eth.payload , eth.payload_len );
152132
@@ -162,23 +142,23 @@ if (tcp.payload_length > 0 && tcp.payload != nullptr) {
162142 std::cout << " Hardware Size: " << static_cast <int >(arp.hardware_size ) << " \n " ;
163143 std::cout << " Protocol Size: " << static_cast <int >(arp.protocol_size ) << " \n " ;
164144 std::cout << " Operation: " << ((arp.operation == 1 ) ? " Request (1)" : " Reply (2)" ) << " \n " ;
145+
165146 std::cout << " Sender MAC: " ;
166147 for (const auto & byte : arp.sender_mac )
167148 std::cout << std::hex << std::setw (2 ) << std::setfill (' 0' ) << static_cast <int >(byte) << " :" ;
168- std::cout << " \b \n " ; // Remove last colon
149+ std::cout << std::dec << " \b \n " ; // Remove last colon and reset to decimal
169150
170151 std::cout << " Sender IP: " << arp.sender_ip << " \n " ;
171152
172153 std::cout << " Target MAC: " ;
173154 for (const auto & byte : arp.target_mac )
174155 std::cout << std::hex << std::setw (2 ) << std::setfill (' 0' ) << static_cast <int >(byte) << " :" ;
175- std::cout << " \b \n " ;
156+ std::cout << std::dec << " \b \n " ;
176157
177158 std::cout << " Target IP: " << arp.target_ip << " \n " ;
178- }
179-
159+ } // end else if ARP
180160
181- }
161+ } // end while
182162
183163 close (sock);
184164 return 0 ;
0 commit comments