From 2f02011061fc4697c1ed69af37ce8d21e1b1161e Mon Sep 17 00:00:00 2001 From: Damien Riegel <damien.riegel@gmail.com> Date: Wed, 8 Nov 2017 16:08:23 -0500 Subject: [PATCH] conn: add parameter to mutt_socket_new to specify socket type It's currently the responsibility of mutt_conn_find to setup the Connection, but this should really be done by mutt_socket_new, as it's part of the socket initialization. To let it know which kind of Connection it should create, add a parameter to allow callers to specify the type. --- conn/socket.c | 31 ++++++++++++++++++++++++++----- conn/socket.h | 9 ++++++++- mutt_socket.c | 29 +++++++++++++---------------- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/conn/socket.c b/conn/socket.c index 0e214adf0..01ec0f9c8 100644 --- a/conn/socket.c +++ b/conn/socket.c @@ -49,11 +49,10 @@ #include "globals.h" #include "options.h" #include "protos.h" -#ifdef USE_SSL -#include "ssl.h" -#endif #include "socket.h" +#include "ssl.h" +#include "tunnel.h" /* support for multiple socket connections */ static struct ConnectionList Connections = TAILQ_HEAD_INITIALIZER(Connections); @@ -343,14 +342,36 @@ int mutt_socket_readln_d(char *buf, size_t buflen, struct Connection *conn, int /** * mutt_socket_new - allocate and initialise a new connection + * @param type Type of the new Connection * @retval ptr New Connection */ -struct Connection *mutt_socket_new(void) +struct Connection *mutt_socket_new(enum ConnectionType type) { struct Connection *conn = mutt_mem_calloc(1, sizeof(struct Connection)); conn->fd = -1; - TAILQ_INSERT_HEAD(&Connections, conn, entries); + if (type == MUTT_CONNECTION_TUNNEL) + { + mutt_tunnel_socket_setup(conn); + } + else if (type == MUTT_CONNECTION_SSL) + { + int ret = mutt_ssl_socket_setup(conn); + + if (ret < 0) + FREE(&conn); + } + else + { + conn->conn_read = raw_socket_read; + conn->conn_write = raw_socket_write; + conn->conn_open = raw_socket_open; + conn->conn_close = raw_socket_close; + conn->conn_poll = raw_socket_poll; + } + + if (conn) + TAILQ_INSERT_HEAD(&Connections, conn, entries); return conn; } diff --git a/conn/socket.h b/conn/socket.h index 2d05777ca..d7b07c2c9 100644 --- a/conn/socket.h +++ b/conn/socket.h @@ -29,6 +29,13 @@ #include "mutt/queue.h" +enum ConnectionType +{ + MUTT_CONNECTION_SIMPLE, + MUTT_CONNECTION_TUNNEL, + MUTT_CONNECTION_SSL, +}; + struct Connection; /** @@ -39,7 +46,7 @@ TAILQ_HEAD(ConnectionList, Connection); /* stupid hack for imap_logout_all */ struct ConnectionList *mutt_socket_head(void); -struct Connection *mutt_socket_new(void); +struct Connection *mutt_socket_new(enum ConnectionType type); void mutt_socket_free(struct Connection *conn); int mutt_socket_open(struct Connection *conn); diff --git a/mutt_socket.c b/mutt_socket.c index 0a8c05ffa..4bb3a6fb9 100644 --- a/mutt_socket.c +++ b/mutt_socket.c @@ -41,6 +41,7 @@ */ struct Connection *mutt_conn_find(const struct Connection *start, const struct Account *account) { + enum ConnectionType conn_type; struct Url url; char hook[LONG_STRING]; @@ -58,32 +59,28 @@ struct Connection *mutt_conn_find(const struct Connection *start, const struct A conn = TAILQ_NEXT(conn, entries); } - conn = mutt_socket_new(); - memcpy(&conn->account, account, sizeof(struct Account)); - if (Tunnel && *Tunnel) - mutt_tunnel_socket_setup(conn); + conn_type = MUTT_CONNECTION_TUNNEL; else if (account->flags & MUTT_ACCT_SSL) + conn_type = MUTT_CONNECTION_SSL; + else + conn_type = MUTT_CONNECTION_SIMPLE; + + conn = mutt_socket_new(conn_type); + if (conn) + { + memcpy(&conn->account, account, sizeof(struct Account)); + } + else { - if (mutt_ssl_socket_setup(conn) < 0) + if (conn_type == MUTT_CONNECTION_SSL) { #ifndef USE_SSL /* that's probably why it failed */ mutt_error(_("SSL is unavailable, cannot connect to %s"), account->host); #endif - mutt_socket_free(conn); - - return NULL; } } - else - { - conn->conn_read = raw_socket_read; - conn->conn_write = raw_socket_write; - conn->conn_open = raw_socket_open; - conn->conn_close = raw_socket_close; - conn->conn_poll = raw_socket_poll; - } return conn; } -- 2.40.0