From 05d93a9e50307cf8837c569b2982f658ac4d8a81 Mon Sep 17 00:00:00 2001 From: chris mikkelson Date: Fri, 27 Mar 2009 00:01:33 -0500 Subject: [PATCH] Made API for module -- module_api.c now contains respond() and accessors for module-private per-connection data. --- Makefile | 7 ++++++- module_api.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ smtp.c | 28 --------------------------- smtpsink.h | 3 +++ 4 files changed, 62 insertions(+), 29 deletions(-) create mode 100644 module_api.c diff --git a/Makefile b/Makefile index 3bccc70..73abcb5 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ CFLAGS=-g -Wall -I/usr/local/include -SMTPSINK_OBJS=conn_pool.o conn_queue.o io.o smtp.o module.o smtpsink.o +SMTPSINK_OBJS=conn_pool.o conn_queue.o io.o smtp.o \ + module.o module_api.o smtpsink.o default: smtpsink @@ -22,6 +23,10 @@ smtp.o: smtp.c smtpsink.h conn_queue.h smtpsink-int.h responses.inc module.o: module.c smtpsink.h cc $(CFLAGS) -c module.c +module_api.o: module_api.c smtpsink.h + cc $(CFLAGS) -c module_api.c + + smtpsink.o: smtpsink.c smtpsink.h conn_queue.h smtpsink-int.h clean: diff --git a/module_api.c b/module_api.c new file mode 100644 index 0000000..ec0bbf3 --- /dev/null +++ b/module_api.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include "smtpsink.h" + +int +respond(struct conn *c, const char *fmt, ...) +{ + va_list ap; + char buf[2048]; + int ret, nw; + + va_start(ap, fmt); + ret = vsnprintf(buf, sizeof(buf), fmt, ap); + if (ret < 0) assert(0); + nw = write(c->fd, buf, ret); + if (nw < 0) { + c->state = SMTP_CLOSED; + return 0; + } + if (nw < ret) { + if (!(c->wdata.s = malloc(ret - nw))) { + c->state = SMTP_CLOSED; + return 0; + } + memcpy(c->wdata.s, buf + nw, ret - nw); + c->wdata.off = 0; + c->wdata.len = ret - nw; + return 0; + } + return 1; +} + +void * +ssm_getpriv(struct smtpsink_module *m, struct conn *c) +{ + assert(c->priv_data); + return c->priv_data[m->mod_index]; +} + +void +ssm_setpriv(struct smtpsink_module *m, struct conn *c, void *data) +{ + assert(c->priv_data); + c->priv_data[m->mod_index] = data; +} diff --git a/smtp.c b/smtp.c index 7b16300..9e4cc41 100644 --- a/smtp.c +++ b/smtp.c @@ -27,34 +27,6 @@ smtp_init(int tstart) } } -int -respond(struct conn *c, const char *fmt, ...) -{ - va_list ap; - char buf[2048]; - int ret, nw; - - va_start(ap, fmt); - ret = vsnprintf(buf, sizeof(buf), fmt, ap); - if (ret < 0) assert(0); - nw = write(c->fd, buf, ret); - if (nw < 0) { - c->state = SMTP_CLOSED; - return 0; - } - if (nw < ret) { - if (!(c->wdata.s = malloc(ret - nw))) { - c->state = SMTP_CLOSED; - return 0; - } - memcpy(c->wdata.s, buf + nw, ret - nw); - c->wdata.off = 0; - c->wdata.len = ret - nw; - return 0; - } - return 1; -} - #include "responses.inc" int diff --git a/smtpsink.h b/smtpsink.h index c67eb23..7863038 100644 --- a/smtpsink.h +++ b/smtpsink.h @@ -65,3 +65,6 @@ struct smtpsink_module { /* module index (filled in by module_init) */ unsigned mod_index; }; + +void *ssm_getpriv(struct smtpsink_module *, struct conn *); +void ssm_setpriv(struct smtpsink_module *, struct conn *, void *); -- 2.50.1