From 326048fed0816c5e99d6be79ec4883eed429153a Mon Sep 17 00:00:00 2001 From: chris mikkelson Date: Tue, 30 Mar 2010 15:54:29 -0500 Subject: [PATCH] Write nmsg datagrams to per-thread datagram sockets instead of shared (and locked) file. --- ssm_nmsg.c | 68 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/ssm_nmsg.c b/ssm_nmsg.c index 3a71023..61d9216 100644 --- a/ssm_nmsg.c +++ b/ssm_nmsg.c @@ -35,30 +35,29 @@ addurl(msgproc *m, char *s, void *data) url, len); } +static pthread_key_t nmsg_output_key; static nmsg_msgmod_t nmsg_emailmod; -static nmsg_output_t nmsg_out; -static char *nmsg_output_filename; -static int nmsg_output_fd = -1; +static struct sockaddr_in nmsg_outaddr = {0}; +static int do_logging = 0; -static pthread_mutex_t outmtx = PTHREAD_MUTEX_INITIALIZER; - -static void -ssm_nmsg_reload(void) +static nmsg_output_t +get_nmsg_out(void) { - pthread_mutex_lock(&outmtx); - - if (nmsg_out) nmsg_output_close(&nmsg_out); - - if (nmsg_output_fd >= 0) - close(nmsg_output_fd); - - nmsg_output_fd = open(nmsg_output_filename, - O_CREAT|O_WRONLY|O_APPEND, 0644); - - nmsg_out = nmsg_output_open_file(nmsg_output_fd, 4096); - nmsg_output_set_buffered(nmsg_out, 0); - - pthread_mutex_unlock(&outmtx); + nmsg_output_t o = pthread_getspecific(nmsg_output_key); + int fd; + + if (o) return o; + if (!do_logging) return NULL; + fd = socket(PF_INET, SOCK_DGRAM, 0); + if (fd < 0) return NULL; + if (connect(fd, (struct sockaddr *)&nmsg_outaddr, sizeof(nmsg_outaddr)) < 0) { + close(fd); + return NULL; + } + o = nmsg_output_open_sock(fd, 4096); + if (!o) close(fd); + pthread_setspecific(nmsg_output_key, o); + return o; } char *message_type_name = "spamtrap"; @@ -71,6 +70,8 @@ ssm_nmsg_init(char *config) nmsg_res res; nmsg_message_t m; + pthread_key_create(&nmsg_output_key, 0); + msgproc_module_init(&msgproc_message); res = nmsg_init(); @@ -84,15 +85,28 @@ ssm_nmsg_init(char *config) while ((t = strsep(&config, ", "))) { if (*t) { - if (!strncasecmp(t, "file=", 5)) { - nmsg_output_filename=strdup(t+5); + if (!strncasecmp(t, "log=", 4)) { + char *ip = t + 4; + int port = 1388; + + do_logging = 1; + t = strchr(ip, ':'); + if (t) { + *t++ = 0; + port = atoi(t); + if (port < 0) + errx(1, "invalid port '%s'", t); + } + nmsg_outaddr.sin_family = AF_INET; + nmsg_outaddr.sin_port = htons(port); + if (!inet_aton(ip, &nmsg_outaddr.sin_addr)) + errx(1, "invalid IP '%s'", ip); } else if (!strncasecmp(t, "type=", 5)) { nmsg_message_enum_name_to_value(m, "type", t+5, &message_type); } } } - ssm_nmsg_reload(); } struct smtpsink_module ss_module; @@ -173,10 +187,10 @@ static int ssm_nmsg_enddata(struct conn *c) { struct ssm_nmsg_state *nms = ssm_getpriv(&ss_module, c); + nmsg_output_t nmsg_out = get_nmsg_out(); + if (nms && nms->msg && nmsg_out) { - pthread_mutex_lock(&outmtx); nmsg_output_write(nmsg_out, nms->msg); - pthread_mutex_unlock(&outmtx); } ssm_nmsg_cleanup(c); return SSM_PASS; @@ -186,7 +200,7 @@ struct smtpsink_module ss_module = { 0, ssm_nmsg_init, - ssm_nmsg_reload, + 0, ssm_nmsg_connect, ssm_nmsg_helo, -- 2.50.1