commit 9bc99080de718a07986f23ecfb8b4c7fb2bffa61
Author: Oswald Buddenhagen <[email protected]>
Date:   Sun Jan 23 14:06:03 2011 +0100

    Socket_t + buffer_t => conn_t
    
    remove the layering, in favor of a "buffered connection" abstraction.

 src/drv_imap.c |   44 ++++++++++++++++++++++----------------------
 src/isync.h    |   19 ++++++++-----------
 src/socket.c   |   20 ++++++++++----------
 3 files changed, 40 insertions(+), 43 deletions(-)

diff --git a/src/drv_imap.c b/src/drv_imap.c
index cb58032..5cefd19 100644
--- a/src/drv_imap.c
+++ b/src/drv_imap.c
@@ -91,7 +91,7 @@ typedef struct imap_store {
        } callbacks;
        void *callback_aux;
 
-       buffer_t buf; /* this is BIG, so put it last */
+       conn_t conn; /* this is BIG, so put it last */
 } imap_store_t;
 
 struct imap_cmd {
@@ -227,7 +227,7 @@ v_submit_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd,
                        goto bail2;
        }
 
-       if (ctx->buf.sock.fd < 0)
+       if (ctx->conn.fd < 0)
                goto bail; /* We got disconnected and had no chance to report 
it yet. */
 
        cmd->tag = ++ctx->nexttag;
@@ -253,15 +253,15 @@ v_submit_imap_cmd( imap_store_t *ctx, struct imap_cmd 
*cmd,
                else
                        printf( ">>> %d LOGIN <user> <pass>\n", cmd->tag );
        }
-       if (socket_write( &ctx->buf.sock, buf, bufl ) != bufl) {
+       if (socket_write( &ctx->conn, buf, bufl ) != bufl) {
                free( cmd->param.data );
                goto bail;
        }
        if (litplus) {
-               n = socket_write( &ctx->buf.sock, cmd->param.data, 
cmd->param.data_len );
+               n = socket_write( &ctx->conn, cmd->param.data, 
cmd->param.data_len );
                free( cmd->param.data );
                if (n != cmd->param.data_len ||
-                   (n = socket_write( &ctx->buf.sock, "\r\n", 2 )) != 2)
+                   (n = socket_write( &ctx->conn, "\r\n", 2 )) != 2)
                        goto bail;
                cmd->param.data = 0;
        } else if (cmd->param.cont || cmd->param.data) {
@@ -375,7 +375,7 @@ static void
 process_imap_replies( imap_store_t *ctx )
 {
        while (ctx->num_in_progress > max_in_progress ||
-              socket_pending( &ctx->buf.sock )) {
+              socket_pending( &ctx->conn )) {
                get_cmd_result( ctx, 0 );
                if (ctx->store_canceled)
                        break;
@@ -441,22 +441,22 @@ parse_imap_list_l( imap_store_t *ctx, char **sp, list_t 
**curp, int level )
                        s = cur->val = nfmalloc( cur->len );
 
                        /* dump whats left over in the input buffer */
-                       n = ctx->buf.bytes - ctx->buf.offset;
+                       n = ctx->conn.bytes - ctx->conn.offset;
 
                        if (n > bytes)
                                /* the entire message fit in the buffer */
                                n = bytes;
 
-                       memcpy( s, ctx->buf.buf + ctx->buf.offset, n );
+                       memcpy( s, ctx->conn.buf + ctx->conn.offset, n );
                        s += n;
                        bytes -= n;
 
                        /* mark that we used part of the buffer */
-                       ctx->buf.offset += n;
+                       ctx->conn.offset += n;
 
                        /* now read the rest of the message */
                        while (bytes > 0) {
-                               if ((n = socket_read( &ctx->buf.sock, s, bytes 
)) <= 0)
+                               if ((n = socket_read( &ctx->conn, s, bytes )) 
<= 0)
                                        goto bail;
                                s += n;
                                bytes -= n;
@@ -467,7 +467,7 @@ parse_imap_list_l( imap_store_t *ctx, char **sp, list_t 
**curp, int level )
                                puts( "=========" );
                        }
 
-                       if (buffer_gets( &ctx->buf, &s ))
+                       if (buffer_gets( &ctx->conn, &s ))
                                goto bail;
                } else if (*s == '"') {
                        /* quoted string */
@@ -759,7 +759,7 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
        int n, resp, resp2, tag, greeted;
 
        greeted = ctx->greeting;
-       while (!buffer_gets( &ctx->buf, &cmd )) {
+       while (!buffer_gets( &ctx->conn, &cmd )) {
                arg = next_arg( &cmd );
                if (*arg == '*') {
                        arg = next_arg( &cmd );
@@ -816,7 +816,7 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
                        if (cmdp->param.data) {
                                if (cmdp->param.to_trash)
                                        ctx->trashnc = 0; /* Can't get NO 
[TRYCREATE] any more. */
-                               n = socket_write( &ctx->buf.sock, 
cmdp->param.data, cmdp->param.data_len );
+                               n = socket_write( &ctx->conn, cmdp->param.data, 
cmdp->param.data_len );
                                free( cmdp->param.data );
                                cmdp->param.data = 0;
                                if (n != (int)cmdp->param.data_len)
@@ -828,7 +828,7 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
                                error( "IMAP error: unexpected command 
continuation request\n" );
                                break;
                        }
-                       if (socket_write( &ctx->buf.sock, "\r\n", 2 ) != 2)
+                       if (socket_write( &ctx->conn, "\r\n", 2 ) != 2)
                                break;
                        if (!cmdp->param.cont)
                                ctx->literal_pending = 0;
@@ -924,7 +924,7 @@ imap_cancel_store( store_t *gctx )
 {
        imap_store_t *ctx = (imap_store_t *)gctx;
 
-       socket_close( &ctx->buf.sock );
+       socket_close( &ctx->conn );
        free_generic_messages( gctx->msgs );
        free_string_list( ctx->gen.boxes );
        free_list( ctx->ns_personal );
@@ -997,7 +997,7 @@ do_cram_auth( imap_store_t *ctx, struct imap_cmd *cmdp, 
const char *prompt )
 
        if (DFlags & VERBOSE)
                printf( ">+> %s\n", resp );
-       n = socket_write( &ctx->buf.sock, resp, l );
+       n = socket_write( &ctx->conn, resp, l );
        free( resp );
        if (n != l)
                return -1;
@@ -1047,18 +1047,18 @@ imap_open_store( store_conf_t *conf,
        ctx = nfcalloc( sizeof(*ctx) );
        ctx->ref_count = 1;
        ctx->gen.conf = conf;
-       ctx->buf.sock.fd = -1;
+       ctx->conn.fd = -1;
        ctx->callbacks.imap_open = cb;
        ctx->callback_aux = aux;
        set_bad_callback( &ctx->gen, (void (*)(void *))imap_open_store_bail, 
ctx );
        ctx->in_progress_append = &ctx->in_progress;
 
-       if (!socket_connect( &srvc->sconf, &ctx->buf.sock ))
+       if (!socket_connect( &srvc->sconf, &ctx->conn ))
                goto bail;
 
 #if HAVE_LIBSSL
        if (srvc->sconf.use_imaps) {
-               if (socket_start_tls( &srvc->sconf, &ctx->buf.sock )) {
+               if (socket_start_tls( &srvc->sconf, &ctx->conn )) {
                        imap_open_store_ssl_bail( ctx );
                        return;
                }
@@ -1131,7 +1131,7 @@ imap_open_store_authenticate_p2( imap_store_t *ctx, 
struct imap_cmd *cmd ATTR_UN
 {
        if (response != RESP_OK)
                imap_open_store_bail( ctx );
-       else if (socket_start_tls( &((imap_server_conf_t 
*)ctx->gen.conf)->sconf, &ctx->buf.sock ))
+       else if (socket_start_tls( &((imap_server_conf_t 
*)ctx->gen.conf)->sconf, &ctx->conn ))
                imap_open_store_ssl_bail( ctx );
        else
                imap_exec( ctx, 0, imap_open_store_authenticate_p3, 
"CAPABILITY" );
@@ -1196,7 +1196,7 @@ imap_open_store_authenticate2( imap_store_t *ctx )
                goto bail;
        }
 #if HAVE_LIBSSL
-       if (!ctx->buf.sock.use_ssl)
+       if (!ctx->conn.use_ssl)
 #endif
                warn( "*** IMAP Warning *** Password is being sent in the 
clear\n" );
        imap_exec( ctx, 0, imap_open_store_authenticate2_p2,
@@ -1270,7 +1270,7 @@ static void
 imap_open_store_ssl_bail( imap_store_t *ctx )
 {
        /* This avoids that we try to send LOGOUT to an unusable socket. */
-       socket_close( &ctx->buf.sock );
+       socket_close( &ctx->conn );
        imap_open_store_bail( ctx );
 }
 #endif
diff --git a/src/isync.h b/src/isync.h
index 420e0c8..a5a3818 100644
--- a/src/isync.h
+++ b/src/isync.h
@@ -79,14 +79,11 @@ typedef struct {
        SSL *ssl;
        unsigned int use_ssl:1;
 #endif
-} Socket_t;
 
-typedef struct {
-       Socket_t sock;
        int bytes;
        int offset;
        char buf[1024];
-} buffer_t;
+} conn_t;
 
 typedef struct {
        const char *file;
@@ -330,14 +327,14 @@ extern const char *Home;
 
 /* socket.c */
 
-int socket_connect( const server_conf_t *conf, Socket_t *sock );
-int socket_start_tls( const server_conf_t *conf, Socket_t *sock );
-void socket_close( Socket_t *sock );
-int socket_read( Socket_t *sock, char *buf, int len );
-int socket_write( Socket_t *sock, char *buf, int len );
-int socket_pending( Socket_t *sock );
+int socket_connect( const server_conf_t *conf, conn_t *sock );
+int socket_start_tls( const server_conf_t *conf, conn_t *sock );
+void socket_close( conn_t *sock );
+int socket_read( conn_t *sock, char *buf, int len );
+int socket_write( conn_t *sock, char *buf, int len );
+int socket_pending( conn_t *sock );
 
-int buffer_gets( buffer_t *b, char **s );
+int buffer_gets( conn_t *b, char **s );
 
 void cram( const char *challenge, const char *user, const char *pass,
            char **_final, int *_finallen );
diff --git a/src/socket.c b/src/socket.c
index e91552e..84210b9 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -52,7 +52,7 @@
 /* Some of this code is inspired by / lifted from mutt. */
 
 static void
-socket_perror( const char *func, Socket_t *sock, int ret )
+socket_perror( const char *func, conn_t *sock, int ret )
 {
 #if HAVE_LIBSSL
        int err;
@@ -112,7 +112,7 @@ compare_certificates( X509 *cert, X509 *peercert,
 
 /* this gets called when a certificate is to be verified */
 static int
-verify_cert( const server_conf_t *conf, Socket_t *sock )
+verify_cert( const server_conf_t *conf, conn_t *sock )
 {
        server_conf_t *mconf = (server_conf_t *)conf;
        SSL *ssl = sock->ssl;
@@ -242,7 +242,7 @@ init_ssl_ctx( const server_conf_t *conf )
 }
 
 int
-socket_start_tls( const server_conf_t *conf, Socket_t *sock )
+socket_start_tls( const server_conf_t *conf, conn_t *sock )
 {
        int ret;
        static int ssl_inited;
@@ -275,7 +275,7 @@ socket_start_tls( const server_conf_t *conf, Socket_t *sock 
)
 #endif /* HAVE_LIBSSL */
 
 int
-socket_connect( const server_conf_t *conf, Socket_t *sock )
+socket_connect( const server_conf_t *conf, conn_t *sock )
 {
        struct hostent *he;
        struct sockaddr_in addr;
@@ -340,7 +340,7 @@ socket_connect( const server_conf_t *conf, Socket_t *sock )
 }
 
 void
-socket_close( Socket_t *sock )
+socket_close( conn_t *sock )
 {
        if (sock->fd >= 0) {
                close( sock->fd );
@@ -358,7 +358,7 @@ socket_close( Socket_t *sock )
 }
 
 int
-socket_read( Socket_t *sock, char *buf, int len )
+socket_read( conn_t *sock, char *buf, int len )
 {
        int n;
 
@@ -377,7 +377,7 @@ socket_read( Socket_t *sock, char *buf, int len )
 }
 
 int
-socket_write( Socket_t *sock, char *buf, int len )
+socket_write( conn_t *sock, char *buf, int len )
 {
        int n;
 
@@ -396,7 +396,7 @@ socket_write( Socket_t *sock, char *buf, int len )
 }
 
 int
-socket_pending( Socket_t *sock )
+socket_pending( conn_t *sock )
 {
        int num = -1;
 
@@ -413,7 +413,7 @@ socket_pending( Socket_t *sock )
 
 /* simple line buffering */
 int
-buffer_gets( buffer_t * b, char **s )
+buffer_gets( conn_t *b, char **s )
 {
        int n;
        int start = b->offset;
@@ -437,7 +437,7 @@ buffer_gets( buffer_t * b, char **s )
                                start = 0;
                        }
 
-                       n = socket_read( &b->sock, b->buf + b->bytes,
+                       n = socket_read( b, b->buf + b->bytes,
                                         sizeof(b->buf) - b->bytes );
 
                        if (n <= 0)

------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d
_______________________________________________
isync-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/isync-devel

Reply via email to