ongrep

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

commit adbad1fc4007a72b179d2f6f9bf3bccfdbb993f6
parent 3d597962ad880110f68260c37310b9609dec5bea
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date:   Sun, 14 Jun 2020 21:19:25 +1000

Replace all error reporting with the err(3) family

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

diff --git a/ngrep.c b/ngrep.c @@ -24,6 +24,7 @@ #include <arpa/inet.h> #include <ctype.h> +#include <err.h> #include <errno.h> #include <pcap.h> #include <pcre.h> @@ -117,7 +118,7 @@ main(int argc, char **argv) dump_func = &dump_unwrapped; dump_single = 1; } else { - printf("fatal: unknown wrap method '%s'\n", optarg); + warnx("invalid wrap method: %s", optarg); usage(); } break; @@ -217,7 +218,7 @@ main(int argc, char **argv) } if (show_hex && dump_func != &dump_formatted) { - printf("fatal: -x (hex dump) is incompatible with -W (alternate format)\n"); + warnx("-x is incompatible with -W"); usage(); } if (argv[optind]) @@ -235,7 +236,7 @@ main(int argc, char **argv) filter = NULL; } if (setup_bpf_filter(argv)) { - pcap_perror(pd, "pcap"); + warnx("pcap: %s", pcap_geterr(pd)); clean_exit(2); } } @@ -262,7 +263,7 @@ main(int argc, char **argv) if (dump_file) { pd_dump = pcap_dump_open(pd, dump_file); if (!pd_dump) { - fprintf(stderr, "fatal: %s\n", pcap_geterr(pd)); + warnx("pcap_dump_open: %s", pcap_geterr(pd)); clean_exit(2); } else printf("output: %s\n", dump_file); @@ -284,33 +285,34 @@ setup_pcap_source(void) { if (read_file) { if (!(pd = pcap_open_offline(read_file, pc_err))) { - perror(pc_err); + warnx("pcap_open_offline: %s", pc_err); return 1; } live_read = 0; printf("input: %s\n", read_file); } else { - char *dev = usedev ? usedev : pcap_lookupdev(pc_err); + char *dev; - if (!dev) { - perror(pc_err); + if (usedev != NULL) { + dev = usedev; + } else if ((dev = pcap_lookupdev(pc_err)) == NULL) { + warnx("pcap_lookupdev: %s", pc_err); return 1; } if ((pd = pcap_open_live(dev, snaplen, promisc, to, pc_err)) == NULL) { - perror(pc_err); + warnx("pcap_open_live: %s", pc_err); return 1; } if (pcap_lookupnet(dev, &net.s_addr, &mask.s_addr, pc_err) == -1) { - perror(pc_err); + warnx("pcap_lookupnet: %s", pc_err); memset(&net, 0, sizeof(net)); memset(&mask, 0, sizeof(mask)); } if (quiet < 2) { printf("interface: %s", dev); - if (net.s_addr && mask.s_addr) { - printf(" (%s/", inet_ntoa(net)); - printf("%s)", inet_ntoa(mask)); - } + if (net.s_addr && mask.s_addr) + printf(" (%s/%s)", + inet_ntoa(net), inet_ntoa(mask)); printf("\n"); } } @@ -348,7 +350,8 @@ setup_pcap_source(void) link_offset = PFLOGHDR_SIZE; break; default: - fprintf(stderr, "fatal: unsupported interface type %u\n", pcap_datalink(pd)); + warnx("fatal: unsupported interface type %u", + pcap_datalink(pd)); return 1; } @@ -363,7 +366,7 @@ setup_bpf_filter(char **argv) FILE *f = fopen(filter_file, "r"); if (!f || !fgets(buf, sizeof(buf) - 1, f)) { - fprintf(stderr, "fatal: unable to get filter from %s: %s\n", filter_file, strerror(errno)); + warn("cannot read filter from %s", filter_file); usage(); } fclose(f); @@ -406,12 +409,12 @@ setup_matcher(void) char *s, *d; if (re_match_word || re_ignore_case) { - fprintf(stderr, "fatal: regex switches are incompatible with binary matching\n"); + warn("fatal: regex switches are incompatible with binary matching"); return 1; } len = (uint32_t) strlen(match_data); if (len % 2 != 0 || !strishex(match_data)) { - fprintf(stderr, "fatal: invalid hex string specified\n"); + warnx("fatal: invalid hex string specified"); return 1; } bin_data = (char *) malloc(len / 2); @@ -449,7 +452,7 @@ setup_matcher(void) pattern = pcre_compile(match_data, pcre_options, (const char **) &re_err, &err_offset, 0); if (!pattern) { - fprintf(stderr, "compile failed: %s\n", re_err); + warnx("pcre_compile: %s", re_err); return 1; } pattern_extra = pcre_study(pattern, 0, (const char **) &re_err); @@ -755,7 +758,7 @@ re_match_func(unsigned char *data, uint32_t len, uint16_t *mindex, case PCRE_ERROR_BADMAGIC: case PCRE_ERROR_UNKNOWN_NODE: case PCRE_ERROR_NOMEMORY: - perror("she's dead, jim\n"); + warnx("pcre_exec: fatal error"); clean_exit(2); case PCRE_ERROR_NOMATCH: return 0;