💾 Archived View for thrig.me › blog › 2024 › 06 › 18 › spamhaus-drop.c captured on 2024-06-20 at 11:51:02.
-=-=-=-=-=-=-
// spamhaus DROP parser // doas pkg_add jansson // CFLAGS="`pkg-config --cflags --libs jansson` -static" make spamhaus-drop #include <err.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <jansson.h> // input should contain one or more cidr objects, and then a coda entry: // ... // {"cidr":"223.254.0.0/16","sblid":"SBL212803","rir":"apnic"} // {"type":"metadata","timestamp":1718576134,"size":85375,"records":1399,"copyright":"(c) 2024 The Spamhaus Project SLU","terms":"https://www.spamhaus.org/drop/terms/"} inline static void emit_cidr(const json_t *obj, size_t linenum) { json_t *cidr = json_object_get(obj, "cidr"); if (!cidr) { json_t *type = json_object_get(obj, "type"); if (type) return; errx(1, "unknown entry -:%zu", linenum); } const char *addr = json_string_value(cidr); if (!addr) errx(1, "no string -:%zu", linenum); printf("%s\n", addr); } int main(int argc, char *argv[]) { char *line = NULL; size_t linesize = 0; ssize_t linelen; size_t linenum = 1; #ifdef __OpenBSD__ if (pledge("stdio", NULL) == -1) err(1, "pledge"); #endif while ((linelen = getline(&line, &linesize, stdin)) != -1) { json_error_t error; json_t *obj; obj = json_loads(line, 0, &error); if (!obj) errx(1, "json error -:%d: %s", error.line, error.text); emit_cidr(obj, linenum); json_decref(obj); ++linenum; } //free(line); exit(EXIT_SUCCESS); }