Avoid superfluous name lookups

This commit is contained in:
Pierre Pronchery 2016-03-31 00:54:22 +02:00
parent 2bc9c0222a
commit a40ded2dd9
2 changed files with 35 additions and 42 deletions

View File

@ -148,6 +148,7 @@ typedef struct _AccountPlugin
AccountConfig * config; AccountConfig * config;
struct addrinfo * ai;
int fd; int fd;
SSL * ssl; SSL * ssl;
guint source; guint source;
@ -277,6 +278,7 @@ static IMAP4 * _imap4_init(AccountPluginHelper * helper)
return NULL; return NULL;
} }
memcpy(imap4->config, &_imap4_config, sizeof(_imap4_config)); memcpy(imap4->config, &_imap4_config, sizeof(_imap4_config));
imap4->ai = NULL;
imap4->fd = -1; imap4->fd = -1;
return imap4; return imap4;
} }
@ -357,6 +359,9 @@ static void _imap4_stop(IMAP4 * imap4)
if(imap4->fd >= 0) if(imap4->fd >= 0)
close(imap4->fd); close(imap4->fd);
imap4->fd = -1; imap4->fd = -1;
if(imap4->ai != NULL)
freeaddrinfo(imap4->ai);
imap4->ai = NULL;
_imap4_event(imap4, AET_STOPPED); _imap4_event(imap4, AET_STOPPED);
} }
@ -1181,7 +1186,6 @@ static gboolean _on_connect(gpointer data)
char const * hostname; char const * hostname;
char const * p; char const * p;
uint16_t port; uint16_t port;
struct addrinfo * ai;
int res; int res;
char buf[128]; char buf[128];
char * q; char * q;
@ -1200,14 +1204,14 @@ static gboolean _on_connect(gpointer data)
return FALSE; return FALSE;
port = (unsigned long)p; port = (unsigned long)p;
/* lookup the address */ /* lookup the address */
if(_common_lookup(hostname, port, &ai) != 0) if(_common_lookup(hostname, port, &imap4->ai) != 0)
{ {
helper->error(helper->account, error_get(NULL), 1); helper->error(helper->account, error_get(NULL), 1);
return FALSE; return FALSE;
} }
/* create the socket */ /* create the socket */
if((imap4->fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) if((imap4->fd = socket(imap4->ai->ai_family, imap4->ai->ai_socktype,
== -1) imap4->ai->ai_protocol)) == -1)
{ {
helper->error(helper->account, strerror(errno), 1); helper->error(helper->account, strerror(errno), 1);
_imap4_stop(imap4); _imap4_stop(imap4);
@ -1219,7 +1223,7 @@ static gboolean _on_connect(gpointer data)
/* FIXME report properly as a warning instead */ /* FIXME report properly as a warning instead */
helper->error(NULL, strerror(errno), 1); helper->error(NULL, strerror(errno), 1);
/* report the current status */ /* report the current status */
if((q = _common_lookup_print(ai)) != NULL) if((q = _common_lookup_print(imap4->ai)) != NULL)
snprintf(buf, sizeof(buf), "Connecting to %s (%s:%u)", hostname, snprintf(buf, sizeof(buf), "Connecting to %s (%s:%u)", hostname,
q, port); q, port);
else else
@ -1227,7 +1231,7 @@ static gboolean _on_connect(gpointer data)
free(q); free(q);
_imap4_event_status(imap4, AS_CONNECTING, buf); _imap4_event_status(imap4, AS_CONNECTING, buf);
/* connect to the remote host */ /* connect to the remote host */
if((connect(imap4->fd, ai->ai_addr, ai->ai_addrlen) != 0 if((connect(imap4->fd, imap4->ai->ai_addr, imap4->ai->ai_addrlen) != 0
&& errno != EINPROGRESS && errno != EINTR) && errno != EINPROGRESS && errno != EINTR)
|| _connect_channel(imap4) != 0) || _connect_channel(imap4) != 0)
{ {
@ -1235,12 +1239,10 @@ static gboolean _on_connect(gpointer data)
strerror(errno)); strerror(errno));
helper->error(helper->account, buf, 1); helper->error(helper->account, buf, 1);
_imap4_stop(imap4); _imap4_stop(imap4);
freeaddrinfo(ai);
return FALSE; return FALSE;
} }
imap4->wr_source = g_io_add_watch(imap4->channel, G_IO_OUT, imap4->wr_source = g_io_add_watch(imap4->channel, G_IO_OUT,
_on_watch_can_connect, imap4); _on_watch_can_connect, imap4);
freeaddrinfo(ai);
return FALSE; return FALSE;
} }
@ -1291,7 +1293,6 @@ static gboolean _on_watch_can_connect(GIOChannel * source,
socklen_t s = sizeof(res); socklen_t s = sizeof(res);
char const * hostname = imap4->config[I4CV_HOSTNAME].value; char const * hostname = imap4->config[I4CV_HOSTNAME].value;
uint16_t port = (unsigned long)imap4->config[I4CV_PORT].value; uint16_t port = (unsigned long)imap4->config[I4CV_PORT].value;
struct addrinfo * ai;
SSL_CTX * ssl_ctx; SSL_CTX * ssl_ctx;
char buf[128]; char buf[128];
char * q; char * q;
@ -1310,18 +1311,13 @@ static gboolean _on_watch_can_connect(GIOChannel * source,
_imap4_stop(imap4); _imap4_stop(imap4);
return FALSE; return FALSE;
} }
/* XXX remember the address instead */ if((q = _common_lookup_print(imap4->ai)) != NULL)
if(_common_lookup(hostname, port, &ai) == 0) snprintf(buf, sizeof(buf), "Connected to %s (%s:%u)", hostname,
{ q, port);
if((q = _common_lookup_print(ai)) != NULL) else
snprintf(buf, sizeof(buf), "Connected to %s (%s:%u)", snprintf(buf, sizeof(buf), "Connected to %s", hostname);
hostname, q, port); _imap4_event_status(imap4, AS_CONNECTED, buf);
else free(q);
snprintf(buf, sizeof(buf), "Connected to %s", hostname);
_imap4_event_status(imap4, AS_CONNECTED, buf);
free(q);
freeaddrinfo(ai);
}
imap4->wr_source = 0; imap4->wr_source = 0;
/* setup SSL */ /* setup SSL */
if(imap4->config[I4CV_SSL].value != NULL) if(imap4->config[I4CV_SSL].value != NULL)

View File

@ -112,6 +112,7 @@ typedef struct _AccountPlugin
AccountConfig * config; AccountConfig * config;
struct addrinfo * ai;
int fd; int fd;
SSL * ssl; SSL * ssl;
guint source; guint source;
@ -228,6 +229,7 @@ static POP3 * _pop3_init(AccountPluginHelper * helper)
return NULL; return NULL;
} }
memcpy(pop3->config, &_pop3_config, sizeof(_pop3_config)); memcpy(pop3->config, &_pop3_config, sizeof(_pop3_config));
pop3->ai = NULL;
pop3->fd = -1; pop3->fd = -1;
pop3->inbox.folder = pop3->helper->folder_new(pop3->helper->account, pop3->inbox.folder = pop3->helper->folder_new(pop3->helper->account,
&pop3->inbox, NULL, FT_INBOX, "Inbox"); &pop3->inbox, NULL, FT_INBOX, "Inbox");
@ -298,6 +300,10 @@ static void _pop3_stop(POP3 * pop3)
free(pop3->queue); free(pop3->queue);
if(pop3->fd >= 0) if(pop3->fd >= 0)
close(pop3->fd); close(pop3->fd);
pop3->fd = -1;
if(pop3->ai != NULL)
freeaddrinfo(pop3->ai);
pop3->ai = NULL;
_pop3_event(pop3, AET_STOPPED); _pop3_event(pop3, AET_STOPPED);
} }
@ -641,7 +647,6 @@ static gboolean _on_connect(gpointer data)
char const * hostname; char const * hostname;
char const * p; char const * p;
uint16_t port; uint16_t port;
struct addrinfo * ai;
int res; int res;
char buf[128]; char buf[128];
char * q; char * q;
@ -660,14 +665,14 @@ static gboolean _on_connect(gpointer data)
return FALSE; return FALSE;
port = (unsigned long)p; port = (unsigned long)p;
/* lookup the address */ /* lookup the address */
if(_common_lookup(hostname, port, &ai) != 0) if(_common_lookup(hostname, port, &pop3->ai) != 0)
{ {
helper->error(helper->account, error_get(NULL), 1); helper->error(helper->account, error_get(NULL), 1);
return FALSE; return FALSE;
} }
/* create the socket */ /* create the socket */
if((pop3->fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) if((pop3->fd = socket(pop3->ai->ai_family, pop3->ai->ai_socktype,
== -1) pop3->ai->ai_protocol)) == -1)
{ {
helper->error(helper->account, strerror(errno), 1); helper->error(helper->account, strerror(errno), 1);
_pop3_stop(pop3); _pop3_stop(pop3);
@ -679,7 +684,7 @@ static gboolean _on_connect(gpointer data)
/* FIXME report properly as a warning instead */ /* FIXME report properly as a warning instead */
helper->error(NULL, strerror(errno), 1); helper->error(NULL, strerror(errno), 1);
/* report the current status */ /* report the current status */
if((q = _common_lookup_print(ai)) != NULL) if((q = _common_lookup_print(pop3->ai)) != NULL)
snprintf(buf, sizeof(buf), "Connecting to %s (%s:%u)", hostname, snprintf(buf, sizeof(buf), "Connecting to %s (%s:%u)", hostname,
q, port); q, port);
else else
@ -687,7 +692,7 @@ static gboolean _on_connect(gpointer data)
free(q); free(q);
_pop3_event_status(pop3, AS_CONNECTING, buf); _pop3_event_status(pop3, AS_CONNECTING, buf);
/* connect to the remote host */ /* connect to the remote host */
if((connect(pop3->fd, ai->ai_addr, ai->ai_addrlen) != 0 if((connect(pop3->fd, pop3->ai->ai_addr, pop3->ai->ai_addrlen) != 0
&& errno != EINPROGRESS && errno != EINTR) && errno != EINPROGRESS && errno != EINTR)
|| _connect_channel(pop3) != 0) || _connect_channel(pop3) != 0)
{ {
@ -695,12 +700,10 @@ static gboolean _on_connect(gpointer data)
strerror(errno)); strerror(errno));
helper->error(helper->account, buf, 1); helper->error(helper->account, buf, 1);
_pop3_stop(pop3); _pop3_stop(pop3);
freeaddrinfo(ai);
return FALSE; return FALSE;
} }
pop3->wr_source = g_io_add_watch(pop3->channel, G_IO_OUT, pop3->wr_source = g_io_add_watch(pop3->channel, G_IO_OUT,
_on_watch_can_connect, pop3); _on_watch_can_connect, pop3);
freeaddrinfo(ai);
return FALSE; return FALSE;
} }
@ -749,7 +752,6 @@ static gboolean _on_watch_can_connect(GIOChannel * source,
socklen_t s = sizeof(res); socklen_t s = sizeof(res);
char const * hostname = pop3->config[P3CV_HOSTNAME].value; char const * hostname = pop3->config[P3CV_HOSTNAME].value;
uint16_t port = (unsigned long)pop3->config[P3CV_PORT].value; uint16_t port = (unsigned long)pop3->config[P3CV_PORT].value;
struct addrinfo * ai;
SSL_CTX * ssl_ctx; SSL_CTX * ssl_ctx;
char buf[128]; char buf[128];
char * q; char * q;
@ -768,18 +770,13 @@ static gboolean _on_watch_can_connect(GIOChannel * source,
_pop3_stop(pop3); _pop3_stop(pop3);
return FALSE; return FALSE;
} }
/* XXX remember the address instead */ if((q = _common_lookup_print(pop3->ai)) != NULL)
if(_common_lookup(hostname, port, &ai) == 0) snprintf(buf, sizeof(buf), "Connected to %s (%s:%u)", hostname,
{ q, port);
if((q = _common_lookup_print(ai)) != NULL) else
snprintf(buf, sizeof(buf), "Connected to %s (%s:%u)", snprintf(buf, sizeof(buf), "Connected to %s", hostname);
hostname, q, port); _pop3_event_status(pop3, AS_CONNECTED, buf);
else free(q);
snprintf(buf, sizeof(buf), "Connected to %s", hostname);
_pop3_event_status(pop3, AS_CONNECTED, buf);
free(q);
freeaddrinfo(ai);
}
pop3->wr_source = 0; pop3->wr_source = 0;
/* setup SSL */ /* setup SSL */
if(pop3->config[P3CV_SSL].value != NULL) if(pop3->config[P3CV_SSL].value != NULL)