rework sources: add notion of jobs and tasks
authorPierre Habouzit <madcoder@debian.org>
Sat, 3 Feb 2007 00:37:58 +0000 (01:37 +0100)
committerPierre Habouzit <madcoder@debian.org>
Sat, 3 Feb 2007 00:37:58 +0000 (01:37 +0100)
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Makefile
buffer.c
buffer.h
job.c [new file with mode: 0644]
job.h [moved from policy.h with 75% similarity]
postfix.c [new file with mode: 0644]
postfix.h [moved from policy.c with 89% similarity]

index 49ac809..d7a5422 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -34,8 +34,8 @@ include mk/cflags.mk
 PROGRAMS = postlicyd
 
 postlicyd_SOURCES = \
-               str.h buffer.h policy.h        \
-               str.c buffer.c policy.c        \
+               str.h buffer.h job.h postfix.h        \
+               str.c buffer.c job.c postfix.c        \
                postlicyd.c
 
 postlicyd_LIBADD = -ludns
index 8342162..a0cf7b8 100644 (file)
--- a/buffer.c
+++ b/buffer.c
@@ -30,7 +30,7 @@
 /******************************************************************************/
 
 /*
- * Copyright © 2006 Pierre Habouzit
+ * Copyright © 2006-2007 Pierre Habouzit
  */
 
 #include <stdio.h>
index d0e1c57..d58ab17 100644 (file)
--- a/buffer.h
+++ b/buffer.h
@@ -30,7 +30,7 @@
 /******************************************************************************/
 
 /*
- * Copyright © 2006 Pierre Habouzit
+ * Copyright © 2006-2007 Pierre Habouzit
  */
 
 #ifndef POSTLICYD_BUFFER_H
diff --git a/job.c b/job.c
new file mode 100644 (file)
index 0000000..fc0c1b8
--- /dev/null
+++ b/job.c
@@ -0,0 +1,37 @@
+/******************************************************************************/
+/*          postlicyd: a postfix policy daemon with a lot of features         */
+/*          ~~~~~~~~~                                                         */
+/*  ________________________________________________________________________  */
+/*                                                                            */
+/*  Redistribution and use in source and binary forms, with or without        */
+/*  modification, are permitted provided that the following conditions        */
+/*  are met:                                                                  */
+/*                                                                            */
+/*  1. Redistributions of source code must retain the above copyright         */
+/*     notice, this list of conditions and the following disclaimer.          */
+/*  2. Redistributions in binary form must reproduce the above copyright      */
+/*     notice, this list of conditions and the following disclaimer in the    */
+/*     documentation and/or other materials provided with the distribution.   */
+/*  3. The names of its contributors may not be used to endorse or promote    */
+/*     products derived from this software without specific prior written     */
+/*     permission.                                                            */
+/*                                                                            */
+/*  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND   */
+/*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE     */
+/*  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR        */
+/*  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS    */
+/*  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR    */
+/*  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF      */
+/*  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  */
+/*  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN   */
+/*  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)   */
+/*  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF    */
+/*  THE POSSIBILITY OF SUCH DAMAGE.                                           */
+/******************************************************************************/
+
+/*
+ * Copyright © 2007 Pierre Habouzit
+ */
+
+#include "job.h"
+
diff --git a/policy.h b/job.h
similarity index 75%
rename from policy.h
rename to job.h
index 1ac3c98..ad7d649 100644 (file)
--- a/policy.h
+++ b/job.h
 /******************************************************************************/
 
 /*
- * Copyright © 2006 Pierre Habouzit
+ * Copyright © 2007 Pierre Habouzit
  */
 
-#ifndef POSTLICYD_POLICY_H
-#define POSTLICYD_POLICY_H
+#ifndef POSTLICYD_JOB_H
+#define POSTLICYD_JOB_H
 
 #include "buffer.h"
 
-enum protocol_state {
+enum job_state {
+    JOB_FREE   = 0x00,
+    JOB_READ   = 0x01,
+    JOB_WRITE  = 0x02,
+    JOB_RDWR   = 0x03,
+    JOB_CONN   = 0x04,
+    JOB_LISTEN = 0x08,
+    JOB_IDLE   = 0x10,
+};
+
+enum smtp_state {
     STATE_CONNECT,
     STATE_HELO, /* or EHLO */
     STATE_MAIL,
@@ -49,7 +59,33 @@ enum protocol_state {
     STATE_ETRN,
 };
 
-typedef struct policy_request {
+typedef struct job_t   job_t;
+typedef struct jpriv_t jpriv_t;
+typedef struct task_t  task_t;
+typedef struct query_t query_t;
+
+struct task_t {
+    task_t *(*create)(void);
+    void (*release)(task_t **);
+
+    void (*run)(job_t *, query_t *);
+    void (*done)(job_t *);
+    void (*cancel)(job_t *);
+    void (*process)(job_t *);
+};
+
+struct job_t {
+    unsigned state : 6;
+    unsigned done  : 1;
+    unsigned error : 1;
+
+    int fd;
+
+    task_t *task;
+    jpriv_t *jdata;
+};
+
+struct query_t {
     unsigned state : 4;
     unsigned esmtp : 1;
 
@@ -78,14 +114,21 @@ typedef struct policy_request {
     const char *encryption_keysize;
     const char *etrn_domain;
 
-    ssize_t  rqsize;
-    buffer_t ibuf;
-    buffer_t obuf;
-} policy_request;
+    buffer_t data;
+
+    job_t *postfix;
+    job_t *current;
+};
 
-policy_request *pcyrq_init(policy_request *rq);
-void pcyrq_wipe(policy_request *rq);
-DO_NEW(policy_request, pcyrq);
-DO_DELETE(policy_request, pcyrq);
+static inline query_t *query_init(query_t *rq) {
+    p_clear(rq, 1);
+    buffer_init(&rq->data);
+    return rq;
+}
+static inline void query_wipe(query_t *rq) {
+    buffer_wipe(&rq->data);
+}
+DO_NEW(query_t, query);
+DO_DELETE(query_t, query);
 
 #endif
diff --git a/postfix.c b/postfix.c
new file mode 100644 (file)
index 0000000..d9b4930
--- /dev/null
+++ b/postfix.c
@@ -0,0 +1,112 @@
+/******************************************************************************/
+/*          postlicyd: a postfix policy daemon with a lot of features         */
+/*          ~~~~~~~~~                                                         */
+/*  ________________________________________________________________________  */
+/*                                                                            */
+/*  Redistribution and use in source and binary forms, with or without        */
+/*  modification, are permitted provided that the following conditions        */
+/*  are met:                                                                  */
+/*                                                                            */
+/*  1. Redistributions of source code must retain the above copyright         */
+/*     notice, this list of conditions and the following disclaimer.          */
+/*  2. Redistributions in binary form must reproduce the above copyright      */
+/*     notice, this list of conditions and the following disclaimer in the    */
+/*     documentation and/or other materials provided with the distribution.   */
+/*  3. The names of its contributors may not be used to endorse or promote    */
+/*     products derived from this software without specific prior written     */
+/*     permission.                                                            */
+/*                                                                            */
+/*  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND   */
+/*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE     */
+/*  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR        */
+/*  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS    */
+/*  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR    */
+/*  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF      */
+/*  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  */
+/*  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN   */
+/*  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)   */
+/*  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF    */
+/*  THE POSSIBILITY OF SUCH DAMAGE.                                           */
+/******************************************************************************/
+
+/*
+ * Copyright © 2006-2007 Pierre Habouzit
+ */
+
+#include <errno.h>
+#include <stdbool.h>
+#include <unistd.h>
+
+#include "postfix.h"
+
+struct jpriv_t {
+    buffer_t ibuf;
+    buffer_t obuf;
+};
+
+task_t *postfix_create(void)
+{
+    return NULL;
+}
+
+void postfix_release(task_t **task)
+{
+    if (task) {
+        *task = NULL;
+    }
+}
+
+void postfix_run(job_t *job, query_t *query)
+{
+}
+
+void postfix_done(job_t *job)
+{
+}
+
+void postfix_process(job_t *job)
+{
+    if (job->state & JOB_LISTEN) {
+        // TODO
+    }
+
+    if (job->state & JOB_WRITE) {
+        int nbwritten;
+
+        nbwritten = write(job->fd, job->jdata->obuf.data, job->jdata->obuf.len);
+        if (nbwritten < 0) {
+            job->error = errno != EINTR && errno != EAGAIN;
+            return;
+        }
+
+        buffer_consume(&job->jdata->obuf, nbwritten);
+    }
+
+    if (job->state & JOB_READ) {
+        int nbread;
+
+        nbread = buffer_read(&job->jdata->ibuf, job->fd, -1);
+        if (nbread < 0) {
+            job->error = errno != EINTR && errno != EAGAIN;
+            return;
+        }
+        if (nbread == 0) {
+            job->error = true;
+            return;
+        }
+
+        if (!strstr(job->jdata->ibuf.data, "\r\n\r\n"))
+            return;
+
+        /* TODO: do the parse */
+    }
+}
+
+task_t task_postfix = {
+    postfix_create,
+    postfix_release,
+    postfix_run,
+    postfix_done,
+    NULL,
+    postfix_process
+};
similarity index 89%
rename from policy.c
rename to postfix.h
index fcc0020..029991c 100644 (file)
--- a/policy.c
+++ b/postfix.h
 /******************************************************************************/
 
 /*
- * Copyright © 2006 Pierre Habouzit
+ * Copyright © 2006-2007 Pierre Habouzit
  */
 
-#include "policy.h"
+#ifndef POSTLICYD_POSTFIX_H
+#define POSTLICYD_POSTFIX_H
 
-policy_request *pcyrq_init(policy_request *rq)
-{
-    p_clear(rq, 1);
-    buffer_init(&rq->ibuf);
-    buffer_init(&rq->obuf);
-    return rq;
-}
+#include "job.h"
 
-void policy_wipe(policy_request *rq)
-{
-    buffer_wipe(&rq->ibuf);
-    buffer_wipe(&rq->obuf);
-}
+extern task_t task_postfix;
+
+#endif