ongrep

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

commit 114776ff6530d243579319ca3aacdddeb993e34d
parent f46276e28689d20b3a7b2ab4f0a1c741e6273880
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date:   Tue, 15 Sep 2020 18:30:46 +1000

begin the boolification, mwahahah!

Change 0/1 to false/true, and change some variables from int types to
size_t/plain int.
Reorders the global arguments so that they're listed first.
Also, some variable renames.

Diffstat:
Mngrep.c | 140++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mngrep.h | 18+++++++++---------
2 files changed, 79 insertions(+), 79 deletions(-)

diff --git a/ngrep.c b/ngrep.c @@ -34,6 +34,7 @@ #include <pcre.h> #include <pwd.h> #include <signal.h> +#include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -44,35 +45,34 @@ #include "ngrep.h" /* Configuration Options */ -uint32_t snaplen = 65535; +bool re_match_word = false; +bool re_ignore_case = false; +bool re_multiline_match = true; +bool promisc = true; +bool show_empty = false; +bool show_hex = false; +bool show_proto = false; +bool invert_match = false; +bool bin_match = false; +bool live_read = true; +bool want_delay = false; +bool enable_hilite = false; + +uint32_t keep_matching = 0; uint32_t limitlen = 65535; -uint32_t promisc = 1; -uint32_t to = 100; uint32_t match_after = 0; -uint32_t keep_matching = 0; uint32_t matches = 0; uint32_t max_matches = 0; +char nonprint_char = '.'; uint32_t seen_frames = 0; - -uint8_t re_match_word = 0; -uint8_t re_ignore_case = 0; -uint8_t re_multiline_match = 1; -uint8_t show_empty = 0; -uint8_t show_hex = 0; -uint8_t show_proto = 0; -uint8_t quiet = 0; -uint8_t invert_match = 0; -uint8_t bin_match = 0; -uint8_t live_read = 1; -uint8_t want_delay = 0; -uint8_t enable_hilite = 0; +uint32_t snaplen = 65535; +uint32_t to = 100; +int quiet = 0; char *read_file = NULL; char *dump_file = NULL; char *usedev = NULL; -char nonprint_char = '.'; - /* PCRE and matching */ int err_offset; const char *re_err = NULL; @@ -82,11 +82,11 @@ pcre_extra *pattern_extra = NULL; char *match_data = NULL; char *bin_data = NULL; -int hex_exp_pfx = 1; +bool add_hex_exp_pfx = true; uint16_t match_len = 0; match_func matcher = blank_match_func; -int8_t dump_single = 0; +bool dump_single = false; dump_func dumper = &dump_formatted; /* BPF/Network */ @@ -94,8 +94,8 @@ char *filter = NULL; char *filter_file = NULL; char pc_err[PCAP_ERRBUF_SIZE]; uint8_t link_offset; -uint8_t radiotap_present = 0; -uint8_t include_vlan = 1; +bool radiotap_present = false; +bool include_vlan = true; pcap_t *pd = NULL; pcap_t *pd_dumppcap = NULL; @@ -162,16 +162,16 @@ main(int argc, char **argv) errx(2, "cols is %s: %s", errstr, optarg); break; case 'C': - enable_hilite = 1; + enable_hilite = true; break; case 'd': usedev = optarg; break; case 'D': - want_delay = 1; + want_delay = true; break; case 'e': - show_empty++; + show_empty = true; break; case 'F': filter_file = optarg; @@ -190,7 +190,7 @@ main(int argc, char **argv) setvbuf(stdout, NULL, _IOLBF, 0); break; case 'M': - re_multiline_match = 0; + re_multiline_match = false; break; case 'n': max_matches = strtonum(optarg, 1, UINT32_MAX, &errstr); @@ -198,7 +198,7 @@ main(int argc, char **argv) errx(2, "num is %s: %s", errstr, optarg); break; case 'N': - show_proto++; + show_proto = true; break; case 'O': dump_file = optarg; @@ -206,7 +206,7 @@ main(int argc, char **argv) err(2, "unveil"); break; case 'p': - promisc = 0; + promisc = false; break; case 'P': nonprint_char = *optarg; @@ -227,7 +227,7 @@ main(int argc, char **argv) print_time = &print_time_absolute; break; case 'v': - invert_match++; + invert_match = true; break; case 'T': if (print_time == &print_time_diff) { @@ -239,7 +239,7 @@ main(int argc, char **argv) } break; case 'w': - re_match_word++; + re_match_word = true; break; case 'W': if (!strcasecmp(optarg, "normal")) @@ -250,17 +250,17 @@ main(int argc, char **argv) dumper = &dump_unwrapped; else if (!strcasecmp(optarg, "single")) { dumper = &dump_unwrapped; - dump_single = 1; + dump_single = true; } else { warnx("invalid wrap method: %s", optarg); usage(); } break; case 'x': - show_hex++; + show_hex = true; break; case 'X': - bin_match++; + bin_match = true; break; case 'h': /* FALLTHROUGH */ @@ -280,7 +280,7 @@ main(int argc, char **argv) if (re_ignore_case) { warnx("-i is incompatible with -X"); usage(); - } else if (re_multiline_match == 0) { + } else if (!re_multiline_match) { warnx("-M is incompatible with -X"); usage(); } else if (re_match_word) { @@ -324,7 +324,7 @@ main(int argc, char **argv) if (quiet < 2) printf("%smatch: %s%s\n", invert_match ? "don't " : "", - (bin_match && hex_exp_pfx) ? "0x" : "", + (bin_match && add_hex_exp_pfx) ? "0x" : "", match_data); if (re_match_word) @@ -359,7 +359,7 @@ setup_pcap_source(void) warnx("pcap_open_offline: %s", pc_err); return -1; } - live_read = 0; + live_read = false; printf("input: %s\n", read_file); } else { char *dev; @@ -427,7 +427,7 @@ setup_pcap_source(void) link_offset = RAWHDR_SIZE; break; case DLT_IEEE802_11_RADIO: - radiotap_present = 1; + radiotap_present = true; case DLT_IEEE802_11: link_offset = IEEE80211HDR_SIZE; break; @@ -523,7 +523,7 @@ setup_hex_match(void) } if (str[0] == '0' && (str[1] == 'X' || str[1] == 'x')) { - hex_exp_pfx = 0; + add_hex_exp_pfx = false; str += 2; len -= 2; } @@ -798,7 +798,7 @@ dump_packet(struct pcap_pkthdr *h, u_char *p, uint8_t proto, uint16_t hdr_offset, uint8_t frag, uint16_t frag_offset, uint32_t frag_id) { - uint16_t match_size, match_index; + size_t match_size, match_index; char ident = '?'; if (!show_empty && len == 0) @@ -884,41 +884,41 @@ dump_packet(struct pcap_pkthdr *h, u_char *p, uint8_t proto, pcap_dump((u_char *)pd_dump, h, p); } -int8_t -re_match_func(unsigned char *data, uint32_t len, uint16_t *mindex, - uint16_t *msize) +bool +re_match_func(unsigned char *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, 0, 0, matchpos, 2); if (did_match < 0) { if (did_match == PCRE_ERROR_NOMATCH) { - return 0; + return false; } else { warnx("pcre_exec: returned %d", did_match); clean_exit(-1); } } - *mindex = matchpos[0]; - *msize = matchpos[1] - matchpos[0]; + *mindex = (size_t)matchpos[0]; + *msize = (size_t)matchpos[1] - matchpos[0]; matches++; if (match_after && keep_matching != match_after) keep_matching = match_after; - return 1; + return true; } -int8_t -bin_match_func(unsigned char *data, uint32_t len, uint16_t *mindex, - uint16_t *msize) +bool +bin_match_func(unsigned char *data, uint32_t len, size_t *mindex, + size_t *msize) { int32_t stop = len - match_len; unsigned char *p; if (stop < 0) - return 0; + return false; if ((p = memmem(data, len, bin_data, match_len)) != NULL) { matches++; @@ -926,32 +926,32 @@ bin_match_func(unsigned char *data, uint32_t len, uint16_t *mindex, if (match_after && keep_matching != match_after) keep_matching = match_after; - *mindex = (uint16_t)(p - data); + *mindex = (size_t)(p - data); *msize = match_len; - return 1; + return true; } - return 0; + return false; } -int8_t +bool blank_match_func(UNUSED unsigned char *data, UNUSED uint32_t len, - uint16_t *mindex, uint16_t *msize) + size_t *mindex, size_t *msize) { matches++; *mindex = 0; *msize = 0; - return 1; + return true; } void -dump_byline(unsigned char *data, uint32_t len, uint16_t mindex, uint16_t msize) +dump_byline(unsigned char *data, uint32_t len, size_t mindex, size_t msize) { const unsigned char *s = data; - uint8_t should_hilite = (msize && enable_hilite); + bool should_hilite = (msize && enable_hilite); unsigned char *hilite_start = data + mindex; unsigned char *hilite_end = hilite_start + msize; @@ -970,11 +970,11 @@ dump_byline(unsigned char *data, uint32_t len, uint16_t mindex, uint16_t msize) } void -dump_unwrapped(unsigned char *data, uint32_t len, uint16_t mindex, - uint16_t msize) +dump_unwrapped(unsigned char *data, uint32_t len, size_t mindex, + size_t msize) { const unsigned char *s = data; - uint8_t should_hilite = (msize && enable_hilite); + bool should_hilite = (msize && enable_hilite); unsigned char *hilite_start = data + mindex; unsigned char *hilite_end = hilite_start + msize; @@ -993,12 +993,12 @@ dump_unwrapped(unsigned char *data, uint32_t len, uint16_t mindex, } void -dump_formatted(unsigned char *data, uint32_t len, uint16_t mindex, - uint16_t msize) +dump_formatted(unsigned char *data, uint32_t len, size_t mindex, + size_t msize) { - uint8_t should_hilite = (msize && enable_hilite); + bool should_hilite = (msize && enable_hilite); unsigned char *str = data; - uint8_t hiliting = 0; + bool hiliting = false; uint8_t width = show_hex ? 16 : (ws_col - 5); uint32_t i = 0, j = 0; @@ -1009,7 +1009,7 @@ dump_formatted(unsigned char *data, uint32_t len, uint16_t mindex, for (j = 0; j < width; j++) { if (should_hilite && mindex <= i + j && i + j < mindex + msize) { - hiliting = 1; + hiliting = true; printf("%s", ANSI_hilite); } @@ -1022,7 +1022,7 @@ dump_formatted(unsigned char *data, uint32_t len, uint16_t mindex, printf(" "); if (hiliting) { - hiliting = 0; + hiliting = false; printf("%s", ANSI_off); } } @@ -1031,7 +1031,7 @@ dump_formatted(unsigned char *data, uint32_t len, uint16_t mindex, for (j = 0; j < width; j++) { if (should_hilite && mindex <= i + j && i + j < mindex + msize) { - hiliting = 1; + hiliting = true; printf("%s", ANSI_hilite); } @@ -1042,7 +1042,7 @@ dump_formatted(unsigned char *data, uint32_t len, uint16_t mindex, printf(" "); if (hiliting) { - hiliting = 0; + hiliting = false; printf("%s", ANSI_off); } } diff --git a/ngrep.h b/ngrep.h @@ -56,9 +56,9 @@ #define WORD_REGEX "((^%s\\W)|(\\W%s$)|(\\W%s\\W))" -typedef void (*dump_func)(unsigned char *, uint32_t, uint16_t, uint16_t); -typedef int8_t (*match_func)(unsigned char *, uint32_t, uint16_t *, - uint16_t *); +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 (*delay_func)(struct pcap_pkthdr *); typedef void (*ts_func)(struct pcap_pkthdr *); @@ -79,16 +79,16 @@ void dump_packet(struct pcap_pkthdr *, u_char *, uint8_t, unsigned char *, uint3 const char *, const char *, uint16_t, uint16_t, uint8_t, uint16_t, uint8_t, uint16_t, uint32_t); -void dump_unwrapped(unsigned char *, uint32_t, uint16_t, uint16_t); -void dump_formatted(unsigned char *, uint32_t, uint16_t, uint16_t); -void dump_byline(unsigned char *, uint32_t, uint16_t, uint16_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_delay_proc_init(struct pcap_pkthdr *); void dump_delay_proc(struct pcap_pkthdr *); -int8_t re_match_func(unsigned char *, uint32_t, uint16_t *, uint16_t *); -int8_t bin_match_func(unsigned char *, uint32_t, uint16_t *, uint16_t *); -int8_t blank_match_func(unsigned char *, uint32_t, uint16_t *, uint16_t *); +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 *); void print_time_absolute(struct pcap_pkthdr *); void print_time_diff(struct pcap_pkthdr *);