From: chris mikkelson Date: Wed, 25 Mar 2009 03:06:28 +0000 (-0500) Subject: After a few scattered changes, it all compiles and links. X-Git-Url: https://git.mikk.net/?a=commitdiff_plain;h=09484b315aeb22762d9881257ae10ce511e74a3e;p=smtpsink After a few scattered changes, it all compiles and links. --- diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d7e6048 --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +CFLAGS=-g -Wall -I/usr/local/include + +SMTPSINK_OBJS=conn_pool.o conn_queue.o io.o smtp.o smtpsink.o + +default: smtpsink + +smtpsink: $(SMTPSINK_OBJS) + cc -o smtpsink $(SMTPSINK_OBJS) -L/usr/local/lib -levent -lpthread + +conn_pool.o: conn_pool.c smtpsink.h conn_queue.h smtpsink-int.h + cc $(CFLAGS) -c conn_pool.c + +conn_queue.o: conn_queue.c smtpsink.h conn_queue.h + cc $(CFLAGS) -c conn_queue.c + +io.o: io.c smtpsink.h conn_queue.h smtpsink-int.h + cc $(CFLAGS) -c io.c + +smtp.o: smtp.c smtpsink.h conn_queue.h smtpsink-int.h responses.inc + cc $(CFLAGS) -c smtp.c + +smtpsink.o: smtpsink.c smtpsink.h conn_queue.h smtpsink-int.h + +clean: + rm -f $(SMTPSINK_OBJS) smtpsink diff --git a/conn_pool.c b/conn_pool.c index 84976bf..daf10e4 100644 --- a/conn_pool.c +++ b/conn_pool.c @@ -102,7 +102,7 @@ listener_loop(void *data) * before all queues are initialized. */ if (pthread_mutex_lock(&lmtx)) assert(0); - if (pthread_cond_wait(&lc)) assert(0); + if (pthread_cond_wait(&lc, &lmtx)) assert(0); pthread_mutex_unlock(&lmtx); while (1) { @@ -118,7 +118,7 @@ listener_loop(void *data) c->peer = addr; c->fd = fd; io_initconn(c); - respond(c, greeting); + greeting(c); if (c->wdata.s) { c->event = EV_WRITE; } else { diff --git a/io.c b/io.c index 5fcd673..e47c202 100644 --- a/io.c +++ b/io.c @@ -43,7 +43,7 @@ read_cb(int fd, short event, void *data) smtp_queue.enqueue(&smtp_queue, c); break; case EV_TIMEOUT: - respond(c, readtimeoutresp); + read_timeout(c); if (c->wdata.s) { c->state = SMTP_CLOSED; event_add(&c->we, &timeout); @@ -92,7 +92,7 @@ write_cb(int fd, short event, void *data) } void -conn_startio(struct conn *c) +io_initconn(struct conn *c) { event_set(&c->re, c->fd, EV_READ, read_cb, (void *)c); event_set(&c->re, c->fd, EV_WRITE, write_cb, (void *)c); diff --git a/smtp.c b/smtp.c index 86b28fc..5035464 100644 --- a/smtp.c +++ b/smtp.c @@ -57,13 +57,25 @@ respond(struct conn *c, const char *fmt, ...) #include "responses.inc" +int +greeting(struct conn *c) +{ + return respond(c, banner, banner_hostname); +} + +int +read_timeout(struct conn *c) +{ + return respond(c, err_timeout); + +} + static int smtpd_helo(struct conn *c, char *cmd, int len, int argoff) { const char *resp = helo_ok; if (cmd[0] == 'e' || cmd[0] == 'E') resp = ehlo_ok; - } /* if (c->state == SMTP_GREET) { call-hook; } */ @@ -248,14 +260,7 @@ handle_smtp(struct conn *c) len += nr; } else { - if (nr == 0) - close_conn(c); - else if (respond(c, readerrresp)) - close_conn(c); - else { - c->event = EV_WRITE; - io_queue.enqueue(&io_queue, c); - } + close_conn(c); return; } } diff --git a/smtpsink-int.h b/smtpsink-int.h index 992af30..fcc4529 100644 --- a/smtpsink-int.h +++ b/smtpsink-int.h @@ -1,17 +1,19 @@ /* internal functions and interfaces */ -void conn_pool_init(int); -void close_conn(struct conn *); -void new_listener(char *); -void start_listeners(void); +extern void conn_pool_init(int); +extern void close_conn(struct conn *); +extern void new_listener(char *); +extern void start_listeners(void); -void io_init(void); -void io_initconn(struct conn *); +extern void io_init(void); +extern void io_initconn(struct conn *); -int conn_queue_init(struct conn_queue *, void (*)(struct conn *), int); -int conn_queue_set_tpool_size(struct conn_queue *, int); +extern int conn_queue_init(struct conn_queue *, void (*)(struct conn *), int); +extern int conn_queue_set_tpool_size(struct conn_queue *, int); -void smtp_init(int); +extern void smtp_init(int); +extern int greeting(struct conn *); +extern int read_timeout(struct conn *); extern struct conn_queue io_queue, smtp_queue; extern char *banner_hostname; diff --git a/smtpsink.c b/smtpsink.c index 4cc64fb..6f6d2ea 100644 --- a/smtpsink.c +++ b/smtpsink.c @@ -1,27 +1,41 @@ -/* #include system header files */ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include #include "smtpsink.h" -#include "queues.h" +#include "conn_queue.h" +#include "smtpsink-int.h" static void -sig_exit(int fd, short event, void *arg) +sigev_exit(int fd, short event, void *arg) { - exit 0; + exit(0); } int -main(char **argv, int argc) +main(int argc, char **argv) { /* defaults */ int concurrency = 10000; - int defaultport = "0.0.0.0:25"; + char *l_default = "0.0.0.0:25"; int daemonize = 1; + int pidfd = -1; uid_t uid = 0; gid_t gid = 0; + + struct event sigint, sigterm; struct group *gr; struct passwd *pw; - int tstart = 1, tmax = 1, tspare = 1; + int tstart = 1; /* parse cmdline args */ @@ -33,18 +47,7 @@ main(char **argv, int argc) write (pidfd, pid, strlen(pid)); } - s_addr.sin_family=AF_INET; - s_addr.sin_port=htons(listen_port); - s_addr.sin_addr.s_addr = listen_ip; - - lsock = socket(PF_INET, SOCK_STREAM, 0); - if (lsock < 0) err (1, "socket"); - setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); - - if (bind(lsock, (struct sockaddr *)&s_addr, sizeof s_addr) < 0) - err (1, "bind"); - - if (listen(lsock, -1) < 0) errx (1, "listen"); + new_listener(l_default); if (gid) { if (setgid(gid) < 0) { @@ -68,7 +71,8 @@ main(char **argv, int argc) conn_pool_init(concurrency); io_init(); - smtp_init(tstart, tmax, tspare); + smtp_init(tstart); + start_listeners(); /* modules_init(); */ event_dispatch(); }