OSSP CVS Repository

ossp - Difference in ossp-pkg/lmtp2nntp/lmtp2nntp.c versions 1.62 and 1.63
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [History

ossp-pkg/lmtp2nntp/lmtp2nntp.c 1.62 -> 1.63

--- lmtp2nntp.c  2001/09/13 14:24:49     1.62
+++ lmtp2nntp.c  2001/10/02 14:34:51     1.63
@@ -106,18 +106,17 @@
 };
 
 static void catchsignal(int sig, ...);
-static int connect_nonb(int, const struct sockaddr *, socklen_t, int);
 static void initsession(struct session *session);
 static void resetsession(struct session *session);
 int groupmatch(char *, size_t, char *);
 
 struct ns {
-    char *h;        /* host */
-    char *p;        /* port */
-    sa_t *sa;
-    int s;          /* socket */
-    nntp_t *nntp;
-    nntp_rc_t rc;
+    char           *h;    /* host */
+    char           *p;    /* port */
+    sa_addr_t      *saa;  /* socket address abstraction */
+    sa_t           *sa;   /* socket abstraction */
+    nntp_t         *nntp;
+    nntp_rc_t       rc;
 };
 
 typedef struct {
@@ -135,6 +134,7 @@
     l2_stream_t    *l2;
     char           *cpBindh;
     char           *cpBindp;
+    sa_addr_t      *saaBind;
     sa_t           *saBind;
     int             nsc;
     struct ns       ns[MAXNEWSSERVICES];
@@ -299,7 +299,6 @@
     size_t        asHosts;
     char         *cpHost;
     char         *cpPort;
-    sa_t         *sa;
     l2_channel_t *chPrefix;
     l2_channel_t *chBuf;
     l2_channel_t *chFile;
@@ -331,13 +330,14 @@
     ctx->l2 = NULL;
     ctx->cpBindh = NULL;
     ctx->cpBindp = NULL;
+    ctx->saaBind = NULL;
     ctx->saBind = NULL;
     ctx->nsc = 0;
     for (i=0; i < MAXNEWSSERVICES; i++) {
         ctx->ns[i].h = NULL;
         ctx->ns[i].p = NULL;
+        ctx->ns[i].saa = NULL;
         ctx->ns[i].sa = NULL;
-        ctx->ns[i].s = -1;
         ctx->ns[i].nntp = NULL;
         ctx->ns[i].rc = LMTP_ERR_UNKNOWN;
     }
@@ -369,13 +369,10 @@
                 }
                 else 
                     ctx->cpBindp = strdup("0");
-
-                if ((ctx->saBind = sa_create(SA_IP, "tcp", ctx->cpBindh, ctx->cpBindp)) == NULL) {
-                    fprintf(stderr, "%s:Error: creating TCP socket address failed for \"%s:%s\": %s\n", 
-                            ctx->progname, 
-                            ctx->cpBindh, 
-                            ctx->cpBindp, 
-                            strerror(errno));
+                //FIXME
+                if ((rc = sa_u2a(&ctx->saaBind, "tcp://%s:%s", ctx->cpBindh, ctx->cpBindp)) != SA_OK) {
+                    fprintf(stderr, "%s:Error: Parsing bind address failed for \"%s:%s\" (%d)\n", 
+                            ctx->progname, ctx->cpBindh, ctx->cpBindp, rc);
                     CU(ERR_EXECUTION);
                 }
                 break;
@@ -452,19 +449,17 @@
                     ctx->ns[ctx->nsc].h = cpHost;
                     ctx->ns[ctx->nsc].p = cpPort;
 
-                    if ((sa = sa_create(SA_IP, "tcp",
-                                        ctx->ns[ctx->nsc].h,
-                                        ctx->ns[ctx->nsc].p)) == NULL) {
-                        fprintf(stderr, "%s:Error: creating TCP socket address failed for \"%s:%s\": %s\n", 
+                    if ((rc = sa_u2a(&ctx->ns[ctx->nsc].saa, "tcp://%s:%s", 
+                                     ctx->ns[ctx->nsc].h, ctx->ns[ctx->nsc].p)) != SA_OK) {
+                        fprintf(stderr, "%s:Error: Parsing host address failed for \"%s:%s\" (%d)\n", 
                                 ctx->progname, 
-                                ctx->ns[ctx->nsc].h, 
-                                ctx->ns[ctx->nsc].p, 
-                                strerror(errno));
+                                ctx->ns[ctx->nsc].h,
+                                ctx->ns[ctx->nsc].p,
+                                rc);
                         CU(ERR_EXECUTION);
                     }
-                    ctx->ns[ctx->nsc].sa = sa;
-                    if ((ctx->ns[ctx->nsc].s =
-                         socket(sa->sa_buf->sa_family, SOCK_STREAM, sa->sa_proto)) == -1) {
+
+                    if ((rc = sa_create(&ctx->ns[ctx->nsc].sa)) != SA_OK) {
                         fprintf(stderr, "%s:Error: Creating TCP socket failed for \"%s:%s\": %s\n", 
                                 ctx->progname, 
                                 ctx->ns[ctx->nsc].h, 
@@ -697,61 +692,6 @@
     return rc;
 }
 
-/* taken from "UNIX Network Programming", Volume 1, second edition W. Richard
- * Stevens, connect_nonb.c from section 15.4 "Nonblocking connect", page 411,
- * http://www.kohala.com/start/
- */
-int connect_nonb(int sockfd, const struct sockaddr *saptr, socklen_t salen, int nsec)
-{
-        int                             flags, n, error;
-        socklen_t               len;
-        fd_set                  rset, wset;
-        struct timeval  tval;
-
-        flags = fcntl(sockfd, F_GETFL, 0);
-        fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
-
-        error = 0;
-        if ( (n = connect(sockfd, (struct sockaddr *) saptr, salen)) < 0)
-                if (errno != EINPROGRESS)
-                        return(-1);
-
-        /* Do whatever we want while the connect is taking place. */
-
-        if (n == 0)
-                goto done;      /* connect completed immediately */
-
-        FD_ZERO(&rset);
-        FD_SET(sockfd, &rset);
-        wset = rset;
-        tval.tv_sec = nsec;
-        tval.tv_usec = 0;
-
-        if ( (n = select(sockfd+1, &rset, &wset, NULL,
-                                         nsec ? &tval : NULL)) == 0) {
-                close(sockfd);          /* timeout */
-                errno = ETIMEDOUT;
-                return(-1);
-        }
-
-        if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) {
-                len = sizeof(error);
-                if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
-                        return(-1);                     /* Solaris pending error */
-        } else
-                return(-1); /* err_quit("select error: sockfd not set"); */
-
-done:
-        fcntl(sockfd, F_SETFL, flags);  /* restore file status flags */
-
-        if (error) {
-                close(sockfd);          /* just in case */
-                errno = error;
-                return(-1);
-        }
-        return(0);
-}
-
 static void resetsession(struct session *session)
 {
     if (session->lhlo_domain != NULL)
@@ -796,6 +736,7 @@
     int          bOk;
     int          i;
     nntp_io_t    nntp_io;
+    int          s; //FIXME
 
     log1(ctx, INFO, "LMTP service executing LHLO command < %s", req->msg);
 
@@ -852,37 +793,33 @@
         bOk = TRUE;
         log2(ctx, TRACE, "try %s:%s", ctx->ns[i].h, ctx->ns[i].p);
 
-        if (bOk && (ctx->saBind != NULL)) {
+        if (bOk && (ctx->saaBind != NULL)) {
             log2(ctx, DEBUG, "bind local socket to %s:%s", ctx->cpBindh, ctx->cpBindp);
-            if (bind(ctx->ns[i].s, ctx->saBind->sa_buf, ctx->saBind->sa_len) < 0) {
+            if (sa_bind(ctx->ns[i].sa, ctx->saaBind) != SA_OK) {
                 bOk = FALSE;
                 log2(ctx, ERROR, "binding NNTP client to local address %s:%s failed, %m", ctx->cpBindh, ctx->cpBindp);
             }
         }
 
-        if (bOk) {
-            if(ctx->option_waittime > 0) {
-                log1(ctx, DEBUG, "connect_nonb with waittime=%d", ctx->option_waittime);
-                if (connect_nonb(ctx->ns[i].s, ctx->ns[i].sa->sa_buf, ctx->ns[i].sa->sa_len, 
-                             ctx->option_waittime) < 0) {
-                    bOk = FALSE;
-                    log3(ctx, WARNING, "nonblocking connect to %s:%s with waittime=%d failed, %m",
-                         ctx->ns[i].h, ctx->ns[i].p, ctx->option_waittime);
-                }
+        if (bOk && (ctx->option_waittime > 0)) {
+            log1(ctx, DEBUG, "timeout configured through waittime=%d", ctx->option_waittime);
+            sa_timeout(ctx->ns[i].sa, ctx->option_waittime, 0);
             }
-            else {
-                log0(ctx, DEBUG, "connect");
-                if (connect(ctx->ns[i].s, ctx->ns[i].sa->sa_buf, ctx->ns[i].sa->sa_len) < 0) {
-                    bOk = FALSE;
-                    log2(ctx, WARNING, "connect to %s:%s failed, %m",
-                         ctx->ns[i].h, ctx->ns[i].p);
-                }
+
+        if (bOk) {
+            log0(ctx, DEBUG, "connect");
+            if (sa_connect(ctx->ns[i].sa, ctx->ns[i].saa) != SA_OK) {
+                bOk = FALSE;
+                log2(ctx, WARNING, "connect to %s:%s failed, %m",
+                     ctx->ns[i].h, ctx->ns[i].p);
             }
         }
 
         if (bOk) {
             log0(ctx, DEBUG, "nntp_create");
-            if ((ctx->ns[i].nntp = nntp_create(ctx->ns[i].s, ctx->ns[i].s, 
+            //FIXME
+            sa_getfd(ctx->ns[i].sa, &s);
+            if ((ctx->ns[i].nntp = nntp_create(s, s, 
                                                (ctx->option_logfile && (ctx->option_levelmask >= L2_LEVEL_TRACE)) ?
                                                 &nntp_io : NULL)) == NULL) {
                 bOk = FALSE;
@@ -963,14 +900,14 @@
         nntp_destroy(ns->nntp);
         ns->nntp = NULL;
     }
-    if (ns->s != -1) {
-        close(ns->s);
-        ns->s = -1;
-    }
     if (ns->sa != NULL) {
         sa_destroy(ns->sa);
         ns->sa = NULL;
     }
+    if (ns->saa != NULL) {
+        free(ns->saa);
+        ns->saa = NULL;
+    }
     if (ns->p != NULL) {
         free(ns->p);
         ns->p = NULL;

CVSTrac 2.0.1