OSSP CVS Repository

ossp - Check-in [1103]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 1103
Date: 2001-Oct-08 15:40:22 (local)
2001-Oct-08 13:40:22 (UTC)
User:rse
Branch:
Comment: Change LMTP library from using a file-descriptor (int) to using a opaque handle (void *) and use this new feature to support STDIN/STDOUT and SA.
Tickets:
Inspections:
Files:
ossp-pkg/lmtp2nntp/lmtp.c      1.22 -> 1.23     16 inserted, 27 deleted
ossp-pkg/lmtp2nntp/lmtp.h      1.10 -> 1.11     10 inserted, 4 deleted
ossp-pkg/lmtp2nntp/lmtp2nntp.c      1.66 -> 1.67     35 inserted, 37 deleted

ossp-pkg/lmtp2nntp/lmtp.c 1.22 -> 1.23

--- lmtp.c       2001/09/07 15:02:08     1.22
+++ lmtp.c       2001/10/08 13:40:22     1.23
@@ -66,23 +66,18 @@
     lmtp_io_t         io;       /* select, read, write functions */
     lmtp_readline_t   rl;       /* a function to read in a single line */
     lmtp_dispatch_t **dispatch; /* LMTP commands to be dispatched */
-    int               rfd;      /* file descriptor for reading */
-    int               wfd;      /* file descriptor for writing */
 };
 
-static int lmtp_select(void *ctx, int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
+ssize_t lmtp_fd_read(void *_ctx, void *buf, size_t buflen)
 {
-    return select(nfds, rfds, wfds, efds, tv);
+    lmtp_fd_t *ctx = (lmtp_fd_t *)_ctx;
+    return read(ctx->fd, buf, buflen);
 }
 
-static ssize_t lmtp_read(void *ctx, int fd, void *buf, size_t buflen)
+ssize_t lmtp_fd_write(void *_ctx, const void *buf, size_t buflen)
 {
-    return read(fd, buf, buflen);
-}
-
-static ssize_t lmtp_write(void *ctx, int fd, const void *buf, size_t buflen)
-{
-    return write(fd, buf, buflen);
+    lmtp_fd_t *ctx = (lmtp_fd_t *)_ctx;
+    return write(ctx->fd, buf, buflen);
 }
 
 static int verbindex(lmtp_t *lmtp, char *verb)
@@ -108,7 +103,7 @@
     return rc;
 }
 
-lmtp_t *lmtp_create(int rfd, int wfd, lmtp_io_t *io)
+lmtp_t *lmtp_create(lmtp_io_t *io)
 {
     /*  create a lmtp structure allocating memory for it and initializing it.
      *  A lmtp_cb_default() callback is registered for the default "" verb.
@@ -124,23 +119,17 @@
     if ((lmtp = (lmtp_t *)malloc(sizeof(lmtp_t))) == NULL) 
         return NULL;
 
-    if (io == NULL) {
-        lmtp->io.ctx    = NULL;
-        lmtp->io.select = lmtp_select;
-        lmtp->io.read   = lmtp_read;
-        lmtp->io.write  = lmtp_write;
-    } else {
-        lmtp->io.ctx    = io->ctx;
-        lmtp->io.select = io->select ? io->select : lmtp_select;
-        lmtp->io.read   = io->read   ? io->read   : lmtp_read;
-        lmtp->io.write  = io->write  ? io->write  : lmtp_write;
-    }
+    if (io == NULL) 
+        return NULL;
+
+    lmtp->io.ctx    = io->ctx;
+    lmtp->io.select = io->select;
+    lmtp->io.read   = io->read;
+    lmtp->io.write  = io->write;
 
     lmtp->rl.rl_cnt = 0;
     lmtp->rl.rl_bufptr = NULL;
     lmtp->rl.rl_buf[0] = NUL;
-    lmtp->rfd = rfd;
-    lmtp->wfd = wfd;
     
     if ((lmtp->dispatch = (lmtp_dispatch_t **)malloc(sizeof(void *)*LMTP_MAXVERBS)) == NULL)
         return NULL;
@@ -181,7 +170,7 @@
         /* fetch one character (but read more) */
         if (rl->rl_cnt <= 0) {
             do {
-                rl->rl_cnt = lmtp->io.read(lmtp->io.ctx, lmtp->rfd, rl->rl_buf, LMTP_LINE_MAXLEN);
+                rl->rl_cnt = lmtp->io.read(lmtp->io.ctx, rl->rl_buf, LMTP_LINE_MAXLEN);
             } while (rl->rl_cnt == -1 && errno == EINTR);
             if (rl->rl_cnt == -1)
                 return LMTP_ERR_SYSTEM;
@@ -370,7 +359,7 @@
         formatbuf[len++] = '\r';
         formatbuf[len++] = '\n';
         do {
-            rv = lmtp->io.write(lmtp->io.ctx, lmtp->wfd, formatbuf, len);
+            rv = lmtp->io.write(lmtp->io.ctx, formatbuf, len);
         } while (rv == -1 && errno == EINTR);
         if (rv == -1)
             return LMTP_ERR_SYSTEM;


ossp-pkg/lmtp2nntp/lmtp.h 1.10 -> 1.11

--- lmtp.h       2001/09/07 15:02:08     1.10
+++ lmtp.h       2001/10/08 13:40:22     1.11
@@ -37,9 +37,9 @@
 
 typedef struct {
     void    *ctx;
-    int     (*select)(void *, int, fd_set *, fd_set *, fd_set *, struct timeval *);
-    ssize_t (*read)(void *, int, void *, size_t);
-    ssize_t (*write)(void *, int, const void *, size_t);
+    int     (*select)(void *, fd_set *, fd_set *, fd_set *, struct timeval *);
+    ssize_t (*read)(void *, void *, size_t);
+    ssize_t (*write)(void *, const void *, size_t);
 } lmtp_io_t;
 
 typedef struct {
@@ -63,9 +63,13 @@
     LMTP_ERR_UNKNOWN
 } lmtp_rc_t;
 
+typedef struct {
+    int fd;
+} lmtp_fd_t;
+
 typedef lmtp_rc_t (*lmtp_cb_t)(lmtp_t *, lmtp_io_t *, lmtp_req_t *, void *);
 
-lmtp_t     *lmtp_create  (int, int, lmtp_io_t *);
+lmtp_t     *lmtp_create  (lmtp_io_t *);
 void        lmtp_destroy (lmtp_t *);
 lmtp_rc_t   lmtp_readline(lmtp_t *, char *, size_t);
 lmtp_rc_t   lmtp_readmsg (lmtp_t *, char **, size_t);
@@ -74,6 +78,8 @@
 char       *lmtp_error   (lmtp_rc_t);
 lmtp_rc_t   lmtp_register(lmtp_t *, char *, lmtp_cb_t, void *, lmtp_cb_t *, void **);
 lmtp_rc_t   lmtp_loop    (lmtp_t *);
+ssize_t     lmtp_fd_read (void *, void *, size_t);
+ssize_t     lmtp_fd_write(void *, const void *, size_t);
 
 #endif /* __LMTP_H__ */
 


ossp-pkg/lmtp2nntp/lmtp2nntp.c 1.66 -> 1.67

--- lmtp2nntp.c  2001/10/08 13:21:58     1.66
+++ lmtp2nntp.c  2001/10/08 13:40:22     1.67
@@ -141,6 +141,10 @@
     char           *cpBindp;
     sa_addr_t      *saaBind;
     sa_t           *saBind;
+    sa_addr_t      *saaIO;
+    sa_t           *saIO;
+    int             fdIOi;
+    int             fdIOo;
     int             nsc;
     struct ns       ns[MAXNEWSSERVICES];
     char           *azGroupargs;
@@ -195,28 +199,21 @@
     return;
 }
 
-static ssize_t trace_lmtp_read(void *_ctx, int d, void *buf, size_t nbytes)
+static ssize_t hook_lmtp_read(void *_ctx, void *buf, size_t nbytes)
 {
     lmtp2nntp_t *ctx = (lmtp2nntp_t *)_ctx;
     ssize_t rc;
-    sa_rc_t saRc;
-    size_t bufdone;
+    size_t n;
+    sa_rc_t rv;
 
-    log0(ctx, DEBUG, "trace_lmtp_read() begin");
-    if (ctx->saAltio == NULL) {
-        log0(ctx, DEBUG, "read()");
-        rc = read(d, buf, nbytes);
-    }
-    else {
-        log0(ctx, DEBUG, "saread()");
-        saRc = sa_read(ctx->saAltio, buf, nbytes, &bufdone);
-        if (saRc != SA_OK) {
-            log1(ctx, DEBUG, "LMTP saread error: %d", saRc);
+    if (ctx->saIO != NULL) {
+        if ((rv = sa_read(ctx->saIO, buf, nbytes, &n)) != SA_OK)
             rc = -1;
-        }
         else
-            rc = (ssize_t)bufdone;
+            rc = (ssize_t)n;
     }
+    else
+        rc = read(ctx->fdIOi, buf, nbytes);
     if (rc == -1)
         log0(ctx, TRACE, "LMTP read error: %m");
     else
@@ -225,13 +222,22 @@
     return rc;
 }
 
-static ssize_t trace_lmtp_write(void *_ctx, int d, const void *buf, size_t nbytes)
+static ssize_t hook_lmtp_write(void *_ctx, const void *buf, size_t nbytes)
 {
     lmtp2nntp_t *ctx = (lmtp2nntp_t *)_ctx;
     ssize_t rc;
+    size_t n;
+    sa_rc_t rv;
 
     log3(ctx, TRACE, "LMTP %5d >> \"%{text}D\"", nbytes, buf, nbytes);
-    rc = write(d, buf, nbytes);
+    if (ctx->saIO != NULL) {
+        if ((rv = sa_write(ctx->saIO, buf, nbytes, &n)) != SA_OK)
+            rc = -1;
+        else
+            rc = (ssize_t)n;
+    }
+    else
+        rc = write(ctx->fdIOo, buf, nbytes);
     if (rc == -1)
         log0(ctx, TRACE, "LMTP write error: %m");
     return rc;
@@ -332,9 +338,6 @@
     l2_channel_t *chPrefix;
     l2_channel_t *chBuf;
     l2_channel_t *chFile;
-    sa_addr_t    *caddr;
-    sa_t         *csa;
-    int           fd;
     pid_t         pid;
 
     /* library version check (run-time) */
@@ -698,13 +701,12 @@
     /* loop for LMTP protocol with support for alternate io through daemon */
     if (ctx->saAltio == NULL) {
         /* initialize LMTP context */
+        ctx->fdIOi = STDIN_FILENO;
+        ctx->fdIOo = STDOUT_FILENO;
         lmtp_io.ctx    = ctx;
-        lmtp_io.select = NULL;
-        lmtp_io.read   = trace_lmtp_read;
-        lmtp_io.write  = trace_lmtp_write;
-        if ((lmtp = lmtp_create(STDIN_FILENO, STDOUT_FILENO,
-                                (ctx->option_logfile && (ctx->option_levelmask >= L2_LEVEL_TRACE)) ?
-                                 &lmtp_io : NULL )) == NULL) {
+        lmtp_io.read   = hook_lmtp_read;
+        lmtp_io.write  = hook_lmtp_write;
+        if ((lmtp = lmtp_create(&lmtp_io)) == NULL) {
             fprintf(stderr, "%s:Error: Unable to initialize LMTP library\n", ctx->progname);
             CU(ERR_EXECUTION);
         }
@@ -738,10 +740,9 @@
         pid = getpid();
         daemonize();
         log1(ctx, NOTICE, "startup daemonized, previous pid[%d]", pid); l2_stream_flush(ctx->l2);
-        //FIXME sa_timeout(ctx->saAltio, SA_TIMEOUT_ALL,    3, 0);
+        sa_timeout(ctx->saAltio, SA_TIMEOUT_ALL,    9, 0);
         sa_timeout(ctx->saAltio, SA_TIMEOUT_ACCEPT, 0, 0);
-        while ((rc = sa_accept(ctx->saAltio, &caddr, &csa)) == SA_OK) {
-            sa_getfd(csa, &fd);
+        while ((rc = sa_accept(ctx->saAltio, &ctx->saaIO, &ctx->saIO)) == SA_OK) {
             l2_stream_flush(ctx->l2); /* must flush before fork() */
             pid = fork();
             if (pid == -1) {
@@ -750,19 +751,17 @@
             }
             if (pid != 0) {
                 log1(ctx, INFO, "daemon forked process, new child pid[%d]", pid); l2_stream_flush(ctx->l2);
-                close(fd);
+                sa_destroy(ctx->saIO);
+                sa_addr_destroy(ctx->saaIO);
                 continue;
             }
             log1(ctx, NOTICE, "startup new child process, parent pid[%d]", getppid());
             
             /* initialize LMTP context */
             lmtp_io.ctx    = ctx;
-            lmtp_io.select = NULL;
-            lmtp_io.read   = trace_lmtp_read;
-            lmtp_io.write  = trace_lmtp_write;
-            if ((lmtp = lmtp_create(fd, fd,
-                                    (ctx->option_logfile && (ctx->option_levelmask >= L2_LEVEL_TRACE)) ?
-                                     &lmtp_io : NULL )) == NULL) {
+            lmtp_io.read   = hook_lmtp_read;
+            lmtp_io.write  = hook_lmtp_write;
+            if ((lmtp = lmtp_create(&lmtp_io)) == NULL) {
                 fprintf(stderr, "%s:Error: Unable to initialize LMTP library\n", ctx->progname);
                 CU(ERR_EXECUTION);
             }
@@ -792,7 +791,6 @@
             lmtp_gfs_quit(ctx);
             lmtp_gfs_lhlo(ctx);
             lmtp_destroy(lmtp);
-            close(fd);
             CU(0);
         }
     }

CVSTrac 2.0.1