ongrep

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

commit e17b2d0a09dbaf75c6e6c335b5313cc66fc9c264
parent 3c0d050784006bb98448d218a4fbabefc2b789a7
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date:   Wed, 16 Sep 2020 14:56:19 +1000

replace all unsigned char types to uint8_t

The fact that all three (unsigned char, u_char, uint8_t) were being used
drived me crazy.

Diffstat:
Mngrep.c | 76+++++++++++++++++++++++++++++++++++-----------------------------------------
Mngrep.h | 21++++++++++-----------
2 files changed, 45 insertions(+), 52 deletions(-)

diff --git a/ngrep.c b/ngrep.c @@ -599,7 +599,7 @@ setup_pattern_match(void) } static inline size_t -vlan_frame_count(u_char *p, uint32_t caplen) +vlan_frame_count(uint8_t *p, uint32_t caplen) { uint8_t *et = p + 12; uint16_t ether_type = EXTRACT_16BITS(et); @@ -617,9 +617,9 @@ vlan_frame_count(u_char *p, uint32_t caplen) } void -process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p) +process(UNUSED uint8_t *d, struct pcap_pkthdr *h, uint8_t *p) { - unsigned char *data; + uint8_t *data; struct ip *ip4_pkt; struct ip6_hdr *ip6_pkt; size_t vlan_offset = 0; @@ -674,8 +674,7 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p) if (ip_proto == IPPROTO_FRAGMENT) { struct ip6_frag *ip6_fraghdr = - (struct ip6_frag *)((unsigned char *)ip6_pkt + - ip_hl); + (struct ip6_frag *)((uint8_t *)ip6_pkt + ip_hl); ip_hl += sizeof(struct ip6_frag); ip_proto = ip6_fraghdr->ip6f_nxt; @@ -696,10 +695,10 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p) switch (ip_proto) { case IPPROTO_TCP: { struct tcphdr *tcp_pkt = - (struct tcphdr *)((unsigned char *)ip4_pkt + ip_hl); + (struct tcphdr *)((uint8_t *)ip4_pkt + ip_hl); uint16_t tcphdr_offset = frag_offset ? 0 : tcp_pkt->th_off * 4; - data = (unsigned char *)tcp_pkt + tcphdr_offset; + data = (uint8_t *)tcp_pkt + tcphdr_offset; len -= link_offset + ip_hl + tcphdr_offset; if ((int32_t)len < 0) @@ -712,10 +711,10 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p) } break; case IPPROTO_UDP: { struct udphdr *udp_pkt = - (struct udphdr *)((unsigned char *)ip4_pkt + ip_hl); + (struct udphdr *)((uint8_t *)ip4_pkt + ip_hl); uint16_t udphdr_offset = frag_offset ? 0 : sizeof(*udp_pkt); - data = (unsigned char *)udp_pkt + udphdr_offset; + data = (uint8_t *)udp_pkt + udphdr_offset; len -= link_offset + ip_hl + udphdr_offset; if ((int32_t)len < 0) @@ -727,10 +726,10 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p) } break; case IPPROTO_ICMP: { struct icmp *icmp4_pkt = - (struct icmp *)((unsigned char *)ip4_pkt + ip_hl); + (struct icmp *)((uint8_t *)ip4_pkt + ip_hl); uint16_t icmp4hdr_offset = frag_offset ? 0 : 4; - data = (unsigned char *)icmp4_pkt + icmp4hdr_offset; + data = (uint8_t *)icmp4_pkt + icmp4hdr_offset; len -= link_offset + ip_hl + icmp4hdr_offset; if ((int32_t)len < 0) @@ -742,10 +741,10 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p) } break; case IPPROTO_ICMPV6: { struct icmp6_hdr *icmp6_pkt = - (struct icmp6_hdr *)((unsigned char *)ip6_pkt + ip_hl); + (struct icmp6_hdr *)((uint8_t *)ip6_pkt + ip_hl); uint16_t icmp6hdr_offset = frag_offset ? 0 : 4; - data = (unsigned char *)icmp6_pkt + icmp6hdr_offset; + data = (uint8_t *)icmp6_pkt + icmp6hdr_offset; len -= link_offset + ip_hl + icmp6hdr_offset; if ((int32_t)len < 0) @@ -757,10 +756,10 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p) } break; case IPPROTO_IGMP: { struct igmp *igmp_pkt = - (struct igmp *)((unsigned char *)ip4_pkt + ip_hl); + (struct igmp *)((uint8_t *)ip4_pkt + ip_hl); uint16_t igmphdr_offset = frag_offset ? 0 : 4; - data = (unsigned char *)igmp_pkt + igmphdr_offset; + data = (uint8_t *)igmp_pkt + igmphdr_offset; len -= link_offset + ip_hl + igmphdr_offset; if ((int32_t)len < 0) @@ -771,7 +770,7 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p) igmphdr_offset, fragmented, frag_offset, frag_id); } break; default: { - data = (unsigned char *)ip4_pkt + ip_hl; + data = (uint8_t *)ip4_pkt + ip_hl; len -= link_offset + ip_hl; if ((int32_t)len < 0) @@ -790,11 +789,10 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p) } void -dump_packet(struct pcap_pkthdr *h, u_char *p, uint8_t proto, - unsigned char *data, uint32_t len, const char *ip_src, - const char *ip_dst, uint16_t sport, uint16_t dport, uint8_t flags, - uint16_t hdr_offset, bool frag, uint16_t frag_offset, - uint32_t frag_id) +dump_packet(struct pcap_pkthdr *h, uint8_t *p, uint8_t proto, uint8_t *data, + uint32_t len, const char *ip_src, const char *ip_dst, + uint16_t sport, uint16_t dport, uint8_t flags, uint16_t hdr_offset, + bool frag, uint16_t frag_offset, uint32_t frag_id) { size_t match_size, match_index; char ident = '?'; @@ -879,12 +877,11 @@ dump_packet(struct pcap_pkthdr *h, u_char *p, uint8_t proto, dumper(data, len, match_index, match_size); if (pd_dump) - pcap_dump((u_char *)pd_dump, h, p); + pcap_dump((uint8_t *)pd_dump, h, p); } bool -re_match_func(unsigned char *data, uint32_t len, size_t *mindex, - size_t *msize) +re_match_func(uint8_t *data, uint32_t len, size_t *mindex, size_t *msize) { static int matchpos[2] = {0}; int did_match = pcre_exec(pattern, 0, (const char *)data, (int)len, @@ -909,11 +906,10 @@ re_match_func(unsigned char *data, uint32_t len, size_t *mindex, } bool -bin_match_func(unsigned char *data, uint32_t len, size_t *mindex, - size_t *msize) +bin_match_func(uint8_t *data, uint32_t len, size_t *mindex, size_t *msize) { int32_t stop = len - match_len; - unsigned char *p; + uint8_t *p; if (stop < 0) return false; @@ -934,8 +930,8 @@ bin_match_func(unsigned char *data, uint32_t len, size_t *mindex, } bool -blank_match_func(UNUSED unsigned char *data, UNUSED uint32_t len, - size_t *mindex, size_t *msize) +blank_match_func(UNUSED uint8_t *data, UNUSED uint32_t len, size_t *mindex, + size_t *msize) { matches++; @@ -946,12 +942,12 @@ blank_match_func(UNUSED unsigned char *data, UNUSED uint32_t len, } void -dump_byline(unsigned char *data, uint32_t len, size_t mindex, size_t msize) +dump_byline(uint8_t *data, uint32_t len, size_t mindex, size_t msize) { - const unsigned char *s = data; + const uint8_t *s = data; bool should_hilite = (msize && enable_hilite); - unsigned char *hilite_start = data + mindex; - unsigned char *hilite_end = hilite_start + msize; + uint8_t *hilite_start = data + mindex; + uint8_t *hilite_end = hilite_start + msize; while (s < data + len) { if (should_hilite && s == hilite_start) @@ -968,13 +964,12 @@ dump_byline(unsigned char *data, uint32_t len, size_t mindex, size_t msize) } void -dump_unwrapped(unsigned char *data, uint32_t len, size_t mindex, - size_t msize) +dump_unwrapped(uint8_t *data, uint32_t len, size_t mindex, size_t msize) { - const unsigned char *s = data; + const uint8_t *s = data; bool should_hilite = (msize && enable_hilite); - unsigned char *hilite_start = data + mindex; - unsigned char *hilite_end = hilite_start + msize; + uint8_t *hilite_start = data + mindex; + uint8_t *hilite_end = hilite_start + msize; while (s < data + len) { if (should_hilite && s == hilite_start) @@ -991,11 +986,10 @@ dump_unwrapped(unsigned char *data, uint32_t len, size_t mindex, } void -dump_formatted(unsigned char *data, uint32_t len, size_t mindex, - size_t msize) +dump_formatted(uint8_t *data, uint32_t len, size_t mindex, size_t msize) { bool should_hilite = (msize && enable_hilite); - unsigned char *str = data; + uint8_t *str = data; bool hiliting = false; uint8_t width = show_hex ? 16 : (ws_col - 5); uint32_t i = 0, j = 0; diff --git a/ngrep.h b/ngrep.h @@ -56,9 +56,8 @@ #define WORD_REGEX "((^%s\\W)|(\\W%s$)|(\\W%s\\W))" -typedef void (*dump_func)(unsigned char *, uint32_t, size_t, size_t); -typedef bool (*match_func)(unsigned char *, uint32_t, size_t *, - size_t *); +typedef void (*dump_func)(uint8_t *, uint32_t, size_t, size_t); +typedef bool (*match_func)(uint8_t *, uint32_t, size_t *, size_t *); typedef void (*delay_func)(struct pcap_pkthdr *); typedef void (*ts_func)(struct pcap_pkthdr *); @@ -69,26 +68,26 @@ int setup_hex_match(void); int setup_pattern_match(void); int setup_matcher(void); -void process(u_char *, struct pcap_pkthdr *, u_char *); +void process(uint8_t *, struct pcap_pkthdr *, uint8_t *); __dead void usage(void); void update_windowsize(int32_t); __dead void clean_exit(int); -void dump_packet(struct pcap_pkthdr *, u_char *, uint8_t, unsigned char *, uint32_t, +void dump_packet(struct pcap_pkthdr *, uint8_t *, uint8_t, uint8_t *, uint32_t, const char *, const char *, uint16_t, uint16_t, uint8_t, uint16_t, bool, uint16_t, uint32_t); -void dump_unwrapped(unsigned char *, uint32_t, size_t, size_t); -void dump_formatted(unsigned char *, uint32_t, size_t, size_t); -void dump_byline(unsigned char *, uint32_t, size_t, size_t); +void dump_unwrapped(uint8_t *, uint32_t, size_t, size_t); +void dump_formatted(uint8_t *, uint32_t, size_t, size_t); +void dump_byline(uint8_t *, uint32_t, size_t, size_t); void dump_delay_proc_init(struct pcap_pkthdr *); void dump_delay_proc(struct pcap_pkthdr *); -bool re_match_func(unsigned char *, uint32_t, size_t *, size_t *); -bool bin_match_func(unsigned char *, uint32_t, size_t *, size_t *); -bool blank_match_func(unsigned char *, uint32_t, size_t *, size_t *); +bool re_match_func(uint8_t *, uint32_t, size_t *, size_t *); +bool bin_match_func(uint8_t *, uint32_t, size_t *, size_t *); +bool blank_match_func(uint8_t *, uint32_t, size_t *, size_t *); void print_time_absolute(struct pcap_pkthdr *); void print_time_diff(struct pcap_pkthdr *);