]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
Fix wrong casts in pcapscan.cc
authorKonstantinos Margaritis <konstantinos@vectorcamp.gr>
Sat, 18 May 2024 11:56:30 +0000 (14:56 +0300)
committerKonstantinos Margaritis <konstantinos@vectorcamp.gr>
Sat, 18 May 2024 11:56:30 +0000 (14:56 +0300)
examples/pcapscan.cc

index 92db5cdf1041d4671dd6b30201b120883e953089..3ad20b1ae0decd53225e87ad95b246f1fb2465f4 100644 (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: