rbl.c .. a command link RBL checking for IP addresses

Since I’ll probably being shutdown MYTURL.COM, I’ll be release some smaller apps that I used to keep it up and running. The first one is rbl.c, a command line application for UNIX that allowed me to query realtime blacklist to get the status of an IP address:
mek@xibm:~/build$ ./rbl zen.spamhaus.org 127.0.0.1 127.0.0.2
I used libowfat (http://www.fefe.de/libowfat) for this small app.

 

#include #include "dns.h"#include "buffer.h"#include "stralloc.h"#include "ip4.h"

stralloc txt = { 0 };

void die_nomem(void){    buffer_putsflush(buffer_2, "no memory\n");    _exit(111);}

/* Modified ip4_scan to do the right thing for us here */unsigned int ip4_reverse_scan(const char *s, char ip[4]){    unsigned int len;    unsigned long u;    int i;

    len = 0;    for (i = 0; i < 4; ++i) {        register unsigned int j;        len += (j = scan_ulong(s, &u)) + 1;        if (!j)            return 0;        ip[3 - i] = u;        s += j;        if (i < 3 && *s != '.')            return 0;        ++s;    }    return len - 1;}

unsigned int rbl(char *rblhost, char *ip){    stralloc dnsinfo = { 0 };

    if (!stralloc_ready(&txt, 0))        die_nomem();    txt.len = 0;

    if (!stralloc_copys(&dnsinfo, ip))        die_nomem();    if (!stralloc_cats(&dnsinfo, "."))        die_nomem();    if (!stralloc_cats(&dnsinfo, rblhost))        die_nomem();

    if (!stralloc_cats(&dnsinfo, rblhost))        die_nomem();

    if (dns_txt(&txt, &dnsinfo) == -1) {        if (!stralloc_copys(&txt, "temp. rbl lookup failure"))            die_nomem();        return 100;    }    if (txt.len)        return 1;    else        return 0;}

int main(int argc, char **argv){

    static char seed[128];    static char ipaddr[4];    char str_ipaddr[IP4_FMT];    char *rbl_host;

    dns_random_init(seed);

    if (*argv)        ++argv;

    rbl_host = *argv;    ++argv;

    while (*argv) {        if (!ip4_reverse_scan(*argv, ipaddr)) {            buffer_puts(buffer_2, "WARN:unable to parse IP address:");            buffer_puts(buffer_2, *argv);            buffer_putsflush(buffer_2, "\n");        } else {

            str_ipaddr[ip4_fmt(str_ipaddr, ipaddr)] = 0;

            if (rbl(rbl_host, str_ipaddr)) {                buffer_puts(buffer_1, *argv);                buffer_puts(buffer_1, ":");                buffer_put(buffer_1, txt.s, txt.len);                buffer_putsflush(buffer_1, "\n");            }        }        ++argv;    }    _exit(0);}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: