ongrep

A cleaned up fork of ngrep for OpenBSD
git clone git://git.sgregoratto.me/ongrep
Log | Files | Refs | README | LICENSE

commit 76234008a274c2d6d33e1e3f96afa6f93d110858
parent cadfe37f609d97ab2d77d78a0e90b70387477a32
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date:   Wed, 16 Sep 2020 14:08:37 +1000

simplify packet offset math

- All possible inttypes used for calculating offsets changed to size_t.
- Stop doing pointer counting on void * in vlan_frame_count. This is
  illegal C.
- Better seperate variable declaration and setting in process.
- Axe ip_ver, switch on packet->ip_v instead.
- Reassign ip6_pkt if the datalink is an 802.11 radio. This makes sense,
  right?

Diffstat:
Mngrep.c | 41+++++++++++++++++++++--------------------
1 file changed, 21 insertions(+), 20 deletions(-)

diff --git a/ngrep.c b/ngrep.c @@ -93,7 +93,7 @@ dump_func dumper = &dump_formatted; char *filter = NULL; char *filter_file = NULL; char pc_err[PCAP_ERRBUF_SIZE]; -uint8_t link_offset; +size_t link_offset; bool radiotap_present = false; bool include_vlan = true; @@ -597,14 +597,14 @@ setup_pattern_match(void) return 0; } -static inline uint8_t -vlan_frame_count(u_char *p, uint16_t limit) +static inline size_t +vlan_frame_count(u_char *p, uint32_t caplen) { - uint8_t *et = (uint8_t *)(p + 12); + uint8_t *et = p + 12; uint16_t ether_type = EXTRACT_16BITS(et); - uint8_t count = 0; + size_t count = 0; - while ((void *)et < (void *)(p + limit) && + while (et < (p + caplen) && ether_type != ETHERTYPE_IP && ether_type != ETHERTYPE_IPV6) { count++; @@ -618,14 +618,11 @@ vlan_frame_count(u_char *p, uint16_t limit) void process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p) { - uint8_t vlan_offset = - include_vlan ? vlan_frame_count(p, h->caplen) * VLANHDR_SIZE : 0; - - struct ip *ip4_pkt = (struct ip *)(p + link_offset + vlan_offset); - struct ip6_hdr *ip6_pkt = - (struct ip6_hdr *)(p + link_offset + vlan_offset); - - uint32_t ip_ver; + unsigned char *data; + struct ip *ip4_pkt; + struct ip6_hdr *ip6_pkt; + size_t vlan_offset = 0; + uint32_t len = 0; uint8_t ip_proto = 0; uint32_t ip_hl = 0; @@ -638,21 +635,25 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p) char ip_src[INET6_ADDRSTRLEN + 1]; char ip_dst[INET6_ADDRSTRLEN + 1]; - unsigned char *data; - uint32_t len = h->caplen - vlan_offset; - seen_frames++; + if (include_vlan) + vlan_offset = vlan_frame_count(p, h->caplen) * VLANHDR_SIZE; + len = h->caplen - vlan_offset; + + ip4_pkt = (struct ip *) (p + link_offset + vlan_offset); + ip6_pkt = (struct ip6_hdr *)(p + link_offset + vlan_offset); + if (radiotap_present) { struct ieee80211_radiotap_header *rh = (struct ieee80211_radiotap_header*)p; uint16_t radio_len = rh->it_len; - ip4_pkt = (struct ip *)(p + link_offset + radio_len); + ip4_pkt = (struct ip *) (p + link_offset + radio_len); + ip6_pkt = (struct ip6_hdr *)(p + link_offset + radio_len); len -= radio_len; } - ip_ver = ip4_pkt->ip_v; - switch (ip_ver) { + switch (ip4_pkt->ip_v) { case 4: { ip_hl = ip4_pkt->ip_hl * 4; ip_proto = ip4_pkt->ip_p;