34 #include <net/ethernet.h> 35 #include <netinet/ip.h> 36 #include <netinet/tcp.h> 54 node_(node), m_dataBuff{buffer}
71 PCAP_NETMASK_UNKNOWN) != 0)
95 struct pcap_pkthdr*
header;
96 const u_char* pktData;
99 result = pcap_next_ex(
m_device, &header, &pktData);
104 reinterpret_cast<const iphdr*
>(pktData +
sizeof(
struct ethhdr));
105 uint32_t ipHdrLen = ipHdr->ihl * 4u;
107 switch (ipHdr->protocol)
111 if (header->len == 54)
115 bool storePkt =
true;
119 auto tcpHdr =
reinterpret_cast<const tcphdr*
>(
120 pktData + ipHdrLen +
sizeof(
struct ethhdr));
122 auto lastIpHdr =
reinterpret_cast<const iphdr*
>(&(
m_lastPkt[0]));
123 uint32_t lastIpHdrLen = lastIpHdr->ihl * 4u;
124 auto lastTcpHdr =
reinterpret_cast<const tcphdr*
>(
127 ntohs(static_cast<uint16_t>(lastIpHdr->tot_len));
128 uint16_t newLen = ntohs(static_cast<uint16_t>(ipHdr->tot_len));
129 uint32_t lastSeq = ntohl(lastTcpHdr->seq);
130 uint32_t newSeq = ntohl(tcpHdr->seq);
132 if (newSeq != lastSeq)
134 uint32_t dataOffset = lastTcpHdr->doff * 4;
139 }
else if (newLen <= lastLen)
149 pktData +
sizeof(
struct ethhdr),
150 pktData + header->len);
158 "Skipping protocol: " + std::to_string(result));
162 std::this_thread::sleep_for(std::chrono::milliseconds(1));
165 }
else if (result == -2)
171 auto lastIpHdr =
reinterpret_cast<const iphdr*
>(&(
m_lastPkt[0]));
172 uint32_t ipHdrLength = lastIpHdr->ihl * 4u;
175 reinterpret_cast<const tcphdr*
>(&(
m_lastPkt[0]) + ipHdrLength);
176 uint32_t dataOffset = lastTcpHdr->doff * 4u;
179 m_lastPkt.begin() + ipHdrLength + dataOffset,
ReadResult read()
Attempt to read a packet and store data to buffer.
std::vector< uint8_t > buffer_t
~PcapDevice()
Destructor for PcapDevice.
void disconnect()
Close connected file.
bool isConnected() const
Check if file is open and healthy.
void log(LogLevel logLevel, const std::string &s)
Log function to provide abstraction of ROS loggers.
bool connect(const char *device)
Try to open a pcap file.
ROSaicNodeBase * node_
Pointer to the node.
Declares a class for handling pcap files.
ReadResult
Read operation status.
buffer_t & m_dataBuff
Reference to raw data buffer to write to.
This class is the base class for abstraction.
PcapDevice(ROSaicNodeBase *node, buffer_t &buffer)
Constructor for PcapDevice.
pcap_t * m_device
File handle to pcap file.