Socket_t + buffer_t => conn_t

remove the layering, in favor of a "buffered connection" abstraction.
This commit is contained in:
Oswald Buddenhagen 2011-01-23 14:06:03 +01:00
parent 3447694c2b
commit 171f7d6cd3
3 changed files with 39 additions and 42 deletions

View File

@ -90,7 +90,7 @@ typedef struct imap_store {
} callbacks; } callbacks;
void *callback_aux; 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; } imap_store_t;
struct imap_cmd { struct imap_cmd {
@ -244,11 +244,11 @@ v_submit_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd,
else else
printf( ">>> %d LOGIN <user> <pass>\n", cmd->tag ); 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; goto bail;
if (litplus) { if (litplus) {
if (socket_write( &ctx->buf.sock, cmd->param.data, cmd->param.data_len ) != cmd->param.data_len || if (socket_write( &ctx->conn, cmd->param.data, cmd->param.data_len ) != cmd->param.data_len ||
socket_write( &ctx->buf.sock, "\r\n", 2 ) != 2) socket_write( &ctx->conn, "\r\n", 2 ) != 2)
goto bail; goto bail;
free( cmd->param.data ); free( cmd->param.data );
cmd->param.data = 0; cmd->param.data = 0;
@ -382,7 +382,7 @@ static int
process_imap_replies( imap_store_t *ctx ) process_imap_replies( imap_store_t *ctx )
{ {
while (ctx->num_in_progress > max_in_progress || 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) if (get_cmd_result( ctx, 0 ) == RESP_CANCEL)
return RESP_CANCEL; return RESP_CANCEL;
return RESP_OK; 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 ); s = cur->val = nfmalloc( cur->len );
/* dump whats left over in the input buffer */ /* 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) if (n > bytes)
/* the entire message fit in the buffer */ /* the entire message fit in the buffer */
n = bytes; n = bytes;
memcpy( s, ctx->buf.buf + ctx->buf.offset, n ); memcpy( s, ctx->conn.buf + ctx->conn.offset, n );
s += n; s += n;
bytes -= n; bytes -= n;
/* mark that we used part of the buffer */ /* mark that we used part of the buffer */
ctx->buf.offset += n; ctx->conn.offset += n;
/* now read the rest of the message */ /* now read the rest of the message */
while (bytes > 0) { while (bytes > 0) {
if ((n = socket_read( &ctx->buf.sock, s, bytes )) <= 0) if ((n = socket_read( &ctx->conn, s, bytes )) <= 0)
goto bail; goto bail;
s += n; s += n;
bytes -= n; bytes -= n;
@ -473,7 +473,7 @@ parse_imap_list_l( imap_store_t *ctx, char **sp, list_t **curp, int level )
puts( "=========" ); puts( "=========" );
} }
if (buffer_gets( &ctx->buf, &s )) if (buffer_gets( &ctx->conn, &s ))
goto bail; goto bail;
} else if (*s == '"') { } else if (*s == '"') {
/* quoted string */ /* quoted string */
@ -766,7 +766,7 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
greeted = ctx->greeting; greeted = ctx->greeting;
for (;;) { for (;;) {
if (buffer_gets( &ctx->buf, &cmd )) if (buffer_gets( &ctx->conn, &cmd ))
break; break;
arg = next_arg( &cmd ); 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.data) {
if (cmdp->param.to_trash) if (cmdp->param.to_trash)
ctx->trashnc = 0; /* Can't get NO [TRYCREATE] any more. */ 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 ); free( cmdp->param.data );
cmdp->param.data = 0; cmdp->param.data = 0;
if (n != (int)cmdp->param.data_len) 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" ); error( "IMAP error: unexpected command continuation request\n" );
break; break;
} }
if (socket_write( &ctx->buf.sock, "\r\n", 2 ) != 2) if (socket_write( &ctx->conn, "\r\n", 2 ) != 2)
break; break;
if (!cmdp->param.cont) if (!cmdp->param.cont)
ctx->literal_pending = 0; ctx->literal_pending = 0;
@ -926,7 +926,7 @@ imap_cancel_store( store_t *gctx )
{ {
imap_store_t *ctx = (imap_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_generic_messages( ctx->gen.msgs );
free_string_list( ctx->gen.boxes ); free_string_list( ctx->gen.boxes );
free_list( ctx->ns_personal ); 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) if (DFlags & VERBOSE)
printf( ">+> %s\n", resp ); printf( ">+> %s\n", resp );
n = socket_write( &ctx->buf.sock, resp, l ); n = socket_write( &ctx->conn, resp, l );
free( resp ); free( resp );
if (n != l) if (n != l)
return -1; return -1;
@ -1082,19 +1082,19 @@ imap_open_store( store_conf_t *conf,
ctx = nfcalloc( sizeof(*ctx) ); ctx = nfcalloc( sizeof(*ctx) );
ctx->gen.conf = conf; ctx->gen.conf = conf;
ctx->buf.sock.fd = -1; ctx->conn.fd = -1;
ctx->ref_count = 1; ctx->ref_count = 1;
ctx->callbacks.imap_open = cb; ctx->callbacks.imap_open = cb;
ctx->callback_aux = aux; ctx->callback_aux = aux;
set_bad_callback( &ctx->gen, (void (*)(void *))imap_open_store_bail, ctx ); set_bad_callback( &ctx->gen, (void (*)(void *))imap_open_store_bail, ctx );
ctx->in_progress_append = &ctx->in_progress; ctx->in_progress_append = &ctx->in_progress;
if (!socket_connect( &srvc->sconf, &ctx->buf.sock )) if (!socket_connect( &srvc->sconf, &ctx->conn ))
goto bail; goto bail;
#ifdef HAVE_LIBSSL #ifdef HAVE_LIBSSL
if (srvc->sconf.use_imaps) { 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 ); imap_open_store_ssl_bail( ctx );
return; return;
} }
@ -1168,7 +1168,7 @@ imap_open_store_authenticate_p2( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UN
{ {
if (response != RESP_OK) if (response != RESP_OK)
imap_open_store_bail( ctx ); 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 ); imap_open_store_ssl_bail( ctx );
else else
imap_exec( ctx, 0, imap_open_store_authenticate_p3, "CAPABILITY" ); imap_exec( ctx, 0, imap_open_store_authenticate_p3, "CAPABILITY" );
@ -1233,7 +1233,7 @@ imap_open_store_authenticate2( imap_store_t *ctx )
goto bail; goto bail;
} }
#ifdef HAVE_LIBSSL #ifdef HAVE_LIBSSL
if (!ctx->buf.sock.ssl) if (!ctx->conn.ssl)
#endif #endif
warn( "*** IMAP Warning *** Password is being sent in the clear\n" ); warn( "*** IMAP Warning *** Password is being sent in the clear\n" );
imap_exec( ctx, 0, imap_open_store_authenticate2_p2, imap_exec( ctx, 0, imap_open_store_authenticate2_p2,
@ -1307,7 +1307,7 @@ static void
imap_open_store_ssl_bail( imap_store_t *ctx ) imap_open_store_ssl_bail( imap_store_t *ctx )
{ {
/* This avoids that we try to send LOGOUT to an unusable socket. */ /* 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 ); imap_open_store_bail( ctx );
} }
#endif #endif

View File

@ -78,14 +78,11 @@ typedef struct {
#ifdef HAVE_LIBSSL #ifdef HAVE_LIBSSL
SSL *ssl; SSL *ssl;
#endif #endif
} Socket_t;
typedef struct {
Socket_t sock;
int bytes; int bytes;
int offset; int offset;
char buf[1024]; char buf[1024];
} buffer_t; } conn_t;
typedef struct { typedef struct {
const char *file; const char *file;
@ -332,14 +329,14 @@ extern const char *Home;
/* socket.c */ /* socket.c */
int socket_connect( const server_conf_t *conf, Socket_t *sock ); int socket_connect( const server_conf_t *conf, conn_t *sock );
int socket_start_tls( const server_conf_t *conf, Socket_t *sock ); int socket_start_tls( const server_conf_t *conf, conn_t *sock );
void socket_close( Socket_t *sock ); void socket_close( conn_t *sock );
int socket_read( Socket_t *sock, char *buf, int len ); int socket_read( conn_t *sock, char *buf, int len );
int socket_write( Socket_t *sock, char *buf, int len ); int socket_write( conn_t *sock, char *buf, int len );
int socket_pending( Socket_t *sock ); 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, void cram( const char *challenge, const char *user, const char *pass,
char **_final, int *_finallen ); char **_final, int *_finallen );

View File

@ -49,7 +49,7 @@
#include <netdb.h> #include <netdb.h>
static void 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 #ifdef HAVE_LIBSSL
int err; int err;
@ -112,7 +112,7 @@ compare_certificates( X509 *cert, X509 *peercert,
/* this gets called when a certificate is to be verified */ /* this gets called when a certificate is to be verified */
static int 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; server_conf_t *mconf = (server_conf_t *)conf;
SSL *ssl = sock->ssl; SSL *ssl = sock->ssl;
@ -242,7 +242,7 @@ init_ssl_ctx( const server_conf_t *conf )
} }
int 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; int ret;
static int ssl_inited; static int ssl_inited;
@ -274,7 +274,7 @@ socket_start_tls( const server_conf_t *conf, Socket_t *sock )
#endif /* HAVE_LIBSSL */ #endif /* HAVE_LIBSSL */
int 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 hostent *he;
struct sockaddr_in addr; struct sockaddr_in addr;
@ -339,7 +339,7 @@ socket_connect( const server_conf_t *conf, Socket_t *sock )
} }
void void
socket_close( Socket_t *sock ) socket_close( conn_t *sock )
{ {
if (sock->fd >= 0) { if (sock->fd >= 0) {
close( sock->fd ); close( sock->fd );
@ -354,7 +354,7 @@ socket_close( Socket_t *sock )
} }
int int
socket_read( Socket_t *sock, char *buf, int len ) socket_read( conn_t *sock, char *buf, int len )
{ {
int n; int n;
@ -373,7 +373,7 @@ socket_read( Socket_t *sock, char *buf, int len )
} }
int int
socket_write( Socket_t *sock, char *buf, int len ) socket_write( conn_t *sock, char *buf, int len )
{ {
int n; int n;
@ -392,7 +392,7 @@ socket_write( Socket_t *sock, char *buf, int len )
} }
int int
socket_pending( Socket_t *sock ) socket_pending( conn_t *sock )
{ {
int num = -1; int num = -1;
@ -409,7 +409,7 @@ socket_pending( Socket_t *sock )
/* simple line buffering */ /* simple line buffering */
int int
buffer_gets( buffer_t * b, char **s ) buffer_gets( conn_t *b, char **s )
{ {
int n; int n;
int start = b->offset; int start = b->offset;
@ -433,7 +433,7 @@ buffer_gets( buffer_t * b, char **s )
start = 0; start = 0;
} }
n = socket_read( &b->sock, b->buf + b->bytes, n = socket_read( b, b->buf + b->bytes,
sizeof(b->buf) - b->bytes ); sizeof(b->buf) - b->bytes );
if (n <= 0) if (n <= 0)