commit 03a6f52ae9e64345f1b1571c9cab24de5fa869a2 from: Martijn van Duren date: Fri Mar 29 07:02:04 2019 UTC Make the interface more to what a programmer would expect. Kill some useless code in the process commit - 76e3b8e2062bd83badd8e162307739afa8094e19 commit + 03a6f52ae9e64345f1b1571c9cab24de5fa869a2 blob - c0f2f0b1c7fc7046f28a70dc68e14fdef013417d blob + 3f8c0c98f6f645d86408130fc996f5915263d38e --- main.c +++ main.c @@ -35,7 +35,7 @@ static size_t nblacklists = 0; void usage(void); void dnsbl_connect(char *, int, struct timespec *, char *, char *, uint64_t, - uint64_t, struct smtp_filter_connect *); + uint64_t, char *, struct inx_addr *); void dnsbl_resolve(struct asr_result *, void *); void dnsbl_timeout(int, short, void *); void dnsbl_session_free(struct dnsbl_session *); @@ -82,8 +82,8 @@ main(int argc, char *argv[]) void dnsbl_connect(char *type, int version, struct timespec *tm, char *direction, - char *phase, uint64_t reqid, uint64_t token, - struct smtp_filter_connect *params) + char *phase, uint64_t reqid, uint64_t token, char *hostname, + struct inx_addr *xaddr) { struct dnsbl_session *session; struct timeval timeout = {1, 0}; @@ -99,14 +99,17 @@ dnsbl_connect(char *type, int version, struct timespec session->reqid = reqid; session->token = token; - addr = (u_char *)&(params->addr); + if (xaddr->af == AF_INET) + addr = (u_char *)&(xaddr->addr); + else + addr = (u_char *)&(xaddr->addr6); for (i = 0; i < nblacklists; i++) { - if (params->af == AF_INET) { + if (xaddr->af == AF_INET) { if (snprintf(query, sizeof(query), "%u.%u.%u.%u.%s", addr[3], addr[2], addr[1], addr[0], blacklists[i]) >= sizeof(query)) errx(1, "Can't create query, domain too long"); - } else if (params->af == AF_INET6) { + } else if (xaddr->af == AF_INET6) { if (snprintf(query, sizeof(query), "%hhx.%hhx.%hhx.%hhx" ".%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx" ".%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx" blob - 96d943ce505fbd9c14b4c290260c7d42ca4db6ac blob + c85df01c0873425f605da34a290d4fb520a94f6b --- smtp_proc.c +++ smtp_proc.c @@ -17,18 +17,13 @@ #define NITEMS(x) (sizeof(x) / sizeof(*x)) -typedef int (*smtp_cb)(char *, int, struct timespec *, char *, char *, uint64_t, - uint64_t, void *); - struct smtp_callback; struct smtp_request; -static int smtp_register(char *, char *, char *, smtp_cb); +static int smtp_register(char *, char *, char *, void *); static void smtp_newline(int, short, void *); static void smtp_connect(struct smtp_callback *, int, struct timespec *, uint64_t, uint64_t, char *); -static void smtp_handle_filter(struct smtp_callback *, int, struct timespec *, - uint64_t, uint64_t, void *); struct smtp_callback { char *type; @@ -36,7 +31,7 @@ struct smtp_callback { char *direction; void (*smtp_parse)(struct smtp_callback *, int, struct timespec *, uint64_t, uint64_t, char *); - smtp_cb cb; + void *cb; } smtp_callbacks[] = { {"filter", "connect", "smtp-in", smtp_connect, NULL} }; @@ -45,9 +40,9 @@ static int ready = 0; int smtp_register_filter_connect(void (*cb)(char *, int, struct timespec *, char *, - char *, uint64_t, uint64_t, struct smtp_filter_connect *)) + char *, uint64_t, uint64_t, char *, struct inx_addr *)) { - return smtp_register("filter", "connect", "smtp-in", (smtp_cb) cb); + return smtp_register("filter", "connect", "smtp-in", (void *)cb); } void @@ -150,56 +145,36 @@ static void smtp_connect(struct smtp_callback *cb, int version, struct timespec *tm, uint64_t reqid, uint64_t token, char *params) { - struct smtp_filter_connect sfconnect; + struct inx_addr addrx; + char *hostname; char *address; int ret; + void (*f)(char *, int, struct timespec *,char *, char *, uint64_t, + uint64_t, char *, struct inx_addr *); - sfconnect.hostname = params; + hostname = params; if ((address = strchr(params, '|')) == NULL) errx(1, "Invalid line received: missing address"); address++[0] = '\0'; - sfconnect.af = AF_INET; + addrx.af = AF_INET; if (strncasecmp(address, "ipv6:", 5) == 0) { - sfconnect.af = AF_INET6; + addrx.af = AF_INET6; address += 5; } - ret = inet_pton(sfconnect.af, address, sfconnect.af == AF_INET ? - (void *)&(sfconnect.addr) : (void *)&(sfconnect.addr6)); + ret = inet_pton(addrx.af, address, addrx.af == AF_INET ? + (void *)&(addrx.addr) : (void *)&(addrx.addr6)); if (ret == 0) errx(1, "Invalid line received: Couldn't parse address"); if (ret == -1) err(1, "Couldn't convert address"); - smtp_handle_filter(cb, version, tm, reqid, token, (void *)(&sfconnect)); + f = cb->cb; + f(cb->type, version, tm, cb->direction, cb->phase, reqid, token, + hostname, &addrx); } -static void -smtp_handle_filter(struct smtp_callback *cb, int version, struct timespec *tm, - uint64_t reqid, uint64_t token, void *params) -{ - enum filter_decision fdes; - - fdes = cb->cb(cb->type, version, tm, cb->direction, cb->phase, reqid, - token, params); - switch (fdes) { - case FILTER_PROCEED: - smtp_filter_proceed(reqid, token); - break; - case FILTER_REJECT: - smtp_filter_reject(reqid, token, 451, - "Rejected by filter"); - break; - case FILTER_DISCONNECT: - smtp_filter_disconnect(reqid, token, - "Rejected by filter"); - break; - case FILTER_REWRITE: - errx(1, "Not sure what is intended here yet"); - } -} - void smtp_filter_proceed(uint64_t reqid, uint64_t token) { @@ -241,7 +216,7 @@ smtp_filter_disconnect(uint64_t reqid, uint64_t token, } static int -smtp_register(char *type, char *phase, char *direction, smtp_cb cb) +smtp_register(char *type, char *phase, char *direction, void *cb) { int i; blob - fa683bd047374ac2553016d10a3e68943857d0b8 blob + eb1d00f093efc93ba2f5c600652e21aa9a983230 --- smtp_proc.h +++ smtp_proc.h @@ -1,7 +1,8 @@ #include -struct smtp_filter_connect { - char *hostname; +#include + +struct inx_addr { int af; union { struct in_addr addr; @@ -9,15 +10,8 @@ struct smtp_filter_connect { }; }; -enum filter_decision { - FILTER_PROCEED = 0, - FILTER_REJECT, - FILTER_DISCONNECT, - FILTER_REWRITE -}; - int smtp_register_filter_connect(void (*cb)(char *, int, struct timespec *, - char *, char *, uint64_t, uint64_t, struct smtp_filter_connect *)); + char *, char *, uint64_t, uint64_t, char *, struct inx_addr *)); void smtp_filter_proceed(uint64_t, uint64_t); void smtp_filter_reject(uint64_t, uint64_t, int, const char *, ...) __attribute__((__format__ (printf, 4, 5)));