isync

mailbox synchronization program
git clone https://git.code.sf.net/p/isync/isync
Log | Files | Refs | README | LICENSE

commit 171f7d6cd3f6b836e41accd6b47edcfa2a0b748c
parent 3447694c2bc71d034bb3f510703c2a4bd86a7d15
Author: Oswald Buddenhagen <ossi@users.sf.net>
Date:   Sun, 23 Jan 2011 14:06:03 +0100

Socket_t + buffer_t => conn_t

remove the layering, in favor of a "buffered connection" abstraction.

Diffstat:
Msrc/drv_imap.c | 42+++++++++++++++++++++---------------------
Msrc/isync.h | 19++++++++-----------
Msrc/socket.c | 20++++++++++----------
3 files changed, 39 insertions(+), 42 deletions(-)

diff --git a/src/drv_imap.c b/src/drv_imap.c @@ -90,7 +90,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 { @@ -244,11 +244,11 @@ 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) goto bail; if (litplus) { - if (socket_write( &ctx->buf.sock, cmd->param.data, cmd->param.data_len ) != cmd->param.data_len || - socket_write( &ctx->buf.sock, "\r\n", 2 ) != 2) + if (socket_write( &ctx->conn, cmd->param.data, cmd->param.data_len ) != cmd->param.data_len || + socket_write( &ctx->conn, "\r\n", 2 ) != 2) goto bail; free( cmd->param.data ); cmd->param.data = 0; @@ -382,7 +382,7 @@ static int process_imap_replies( imap_store_t *ctx ) { while (ctx->num_in_progress > max_in_progress || - socket_pending( &ctx->buf.sock )) + socket_pending( &ctx->conn )) if (get_cmd_result( ctx, 0 ) == RESP_CANCEL) return RESP_CANCEL; return RESP_OK; @@ -447,22 +447,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; @@ -473,7 +473,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 */ @@ -766,7 +766,7 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd ) greeted = ctx->greeting; for (;;) { - if (buffer_gets( &ctx->buf, &cmd )) + if (buffer_gets( &ctx->conn, &cmd )) break; arg = next_arg( &cmd ); @@ -824,7 +824,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) @@ -836,7 +836,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; @@ -926,7 +926,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( ctx->gen.msgs ); free_string_list( ctx->gen.boxes ); free_list( ctx->ns_personal ); @@ -1029,7 +1029,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; @@ -1082,19 +1082,19 @@ imap_open_store( store_conf_t *conf, ctx = nfcalloc( sizeof(*ctx) ); ctx->gen.conf = conf; - ctx->buf.sock.fd = -1; + ctx->conn.fd = -1; ctx->ref_count = 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; #ifdef 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; } @@ -1168,7 +1168,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" ); @@ -1233,7 +1233,7 @@ imap_open_store_authenticate2( imap_store_t *ctx ) goto bail; } #ifdef HAVE_LIBSSL - if (!ctx->buf.sock.ssl) + if (!ctx->conn.ssl) #endif warn( "*** IMAP Warning *** Password is being sent in the clear\n" ); imap_exec( ctx, 0, imap_open_store_authenticate2_p2, @@ -1307,7 +1307,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 @@ -78,14 +78,11 @@ typedef struct { #ifdef HAVE_LIBSSL SSL *ssl; #endif -} Socket_t; -typedef struct { - Socket_t sock; int bytes; int offset; char buf[1024]; -} buffer_t; +} conn_t; typedef struct { const char *file; @@ -332,14 +329,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 @@ -49,7 +49,7 @@ #include <netdb.h> static void -socket_perror( const char *func, Socket_t *sock, int ret ) +socket_perror( const char *func, conn_t *sock, int ret ) { #ifdef 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; @@ -274,7 +274,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; @@ -339,7 +339,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 ); @@ -354,7 +354,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; @@ -373,7 +373,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; @@ -392,7 +392,7 @@ socket_write( Socket_t *sock, char *buf, int len ) } int -socket_pending( Socket_t *sock ) +socket_pending( conn_t *sock ) { int num = -1; @@ -409,7 +409,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; @@ -433,7 +433,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)