Fix wrong casts in pcapscan.cc

This commit is contained in:
Konstantinos Margaritis 2024-05-18 14:56:30 +03:00
parent a8373df48b
commit 80812ee5b3

View File

@ -106,7 +106,8 @@ struct FiveTuple {
dstAddr = iphdr->ip_dst.s_addr;
// UDP/TCP ports
const struct udphdr *uh = reinterpret_cast<const struct udphdr *>(iphdr) + (iphdr->ip_hl * 4);
const char * iphdr_base = reinterpret_cast<const char *>(iphdr);
const struct udphdr *uh = reinterpret_cast<const struct udphdr *>(iphdr_base + (iphdr->ip_hl * 4));
srcPort = uh->uh_sport;
dstPort = uh->uh_dport;
}
@ -231,7 +232,7 @@ public:
}
// Valid TCP or UDP packet
const struct ip *iphdr = reinterpret_cast<const struct ip *>(pktData) + sizeof(struct ether_header);
const struct ip *iphdr = reinterpret_cast<const struct ip *>(pktData + sizeof(struct ether_header));
const char *payload = reinterpret_cast<const char *>(pktData) + offset;
size_t id = stream_map.insert(std::make_pair(FiveTuple(iphdr),
@ -572,7 +573,8 @@ int main(int argc, char **argv) {
*/
static bool payloadOffset(const unsigned char *pkt_data, unsigned int *offset,
unsigned int *length) {
const ip *iph = reinterpret_cast<const ip *>(pkt_data) + sizeof(ether_header);
const ip *iph = reinterpret_cast<const ip *>(pkt_data + sizeof(ether_header));
const char *iph_base = reinterpret_cast<const char *>(iph);
const tcphdr *th = nullptr;
// Ignore packets that aren't IPv4
@ -591,7 +593,7 @@ static bool payloadOffset(const unsigned char *pkt_data, unsigned int *offset,
switch (iph->ip_p) {
case IPPROTO_TCP:
th = reinterpret_cast<const tcphdr *>(iph) + ihlen;
th = reinterpret_cast<const tcphdr *>(iph_base + ihlen);
thlen = th->th_off * 4;
break;
case IPPROTO_UDP: