11#include " sniffer.h"
22
33#include < algorithm>
4+ #include < array>
45#include < cerrno>
56#include < chrono>
67#include < iostream>
@@ -191,21 +192,21 @@ bool Sniffer::capture()
191192{
192193 // First 14 bytes are a fake ethernet header with IPv4 as the protocol.
193194 // `char` type is using for the compatibility with Windows.
194- char buffer[ BUFFER_SIZE_HDR + BUFFER_SIZE_PKT] = { 0 } ;
195- // 0x08 - IP protocol type in the Ethernet frame protocolol type field (offset = 12).
196- buffer[BUFFER_OFFSET_ETH + ethernet_proto_type_offset] = 0x08 ;
197- pcap_sf_pkthdr* pkt = reinterpret_cast <struct pcap_sf_pkthdr *>(buffer);
195+ std::array< char , BUFFER_SIZE_HDR + BUFFER_SIZE_PKT> buffer ;
196+ // 0x08 - IP protocol type in the Ethernet frame protocol type field (offset = 12).
197+ buffer[BUFFER_OFFSET_ETH + ethernet_proto_type_offset] = 0x08 ; // cppcheck-suppress containerOutOfBounds
198+ pcap_sf_pkthdr* pkt = reinterpret_cast <pcap_sf_pkthdr*>(buffer. data () );
198199
199200 // Read the next packet, blocking forever.
200- int rc = recv (sock_, buffer + BUFFER_WRITE_OFFSET, BUFFER_SIZE_IP, 0 );
201+ const int rc = recv (sock_, buffer. data () + BUFFER_WRITE_OFFSET, BUFFER_SIZE_IP, 0 );
201202
202203 if (INVALID_SOCKET == rc)
203204 {
204205 std::cerr << " recv() failed: " << sock_wrap_.get_last_error_string () << std::endl;
205206 return false ;
206207 }
207208
208- // End of file for some strange reason , so stop reading packets.
209+ // Data receiving error , so stop reading packets.
209210 if (!rc) return false ;
210211
211212 std::cout << rc << " bytes received..." << std::endl;
@@ -224,7 +225,7 @@ bool Sniffer::capture()
224225 pkt->len = rc + BUFFER_ADD_HEADER_SIZE;
225226
226227 // Write packet.
227- of_.write (reinterpret_cast < const char *>( buffer), rc + BUFFER_SIZE_HDR + BUFFER_ADD_HEADER_SIZE);
228+ of_.write (buffer. data ( ), rc + BUFFER_SIZE_HDR + BUFFER_ADD_HEADER_SIZE);
228229 of_.flush ();
229230
230231 return true ;
0 commit comments