Correct the server address in status messages

This commit is contained in:
Pierre Pronchery 2016-03-31 00:41:04 +02:00
parent 1cd225f16d
commit 2bc9c0222a
3 changed files with 45 additions and 18 deletions

View File

@ -15,6 +15,7 @@
#include <string.h>
#include <netdb.h>
@ -44,3 +45,31 @@ static int _common_lookup(char const * hostname, uint16_t port,
return -error_set_code(1, "%s", gai_strerror(res));
return 0;
}
/* common_lookup_print */
static char * _common_lookup_print(struct addrinfo * ai)
{
char buf[128];
struct sockaddr_in * sin;
struct sockaddr_in6 * sin6;
switch(ai->ai_family)
{
case AF_INET:
sin = (struct sockaddr_in *)ai->ai_addr;
if(inet_ntop(ai->ai_family, &sin->sin_addr, buf,
sizeof(buf)) == NULL)
return NULL;
break;
case AF_INET6:
sin6 = (struct sockaddr_in6 *)ai->ai_addr;
if(inet_ntop(ai->ai_family, &sin6->sin6_addr, buf,
sizeof(buf)) == NULL)
return NULL;
break;
default:
return NULL;
}
return strdup(buf);
}

View File

@ -1184,7 +1184,7 @@ static gboolean _on_connect(gpointer data)
struct addrinfo * ai;
int res;
char buf[128];
char buf2[128];
char * q;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s()\n", __func__);
@ -1219,12 +1219,12 @@ static gboolean _on_connect(gpointer data)
/* FIXME report properly as a warning instead */
helper->error(NULL, strerror(errno), 1);
/* report the current status */
if((p = inet_ntop(ai->ai_family, ai->ai_addr, buf2, sizeof(buf2)))
!= NULL)
if((q = _common_lookup_print(ai)) != NULL)
snprintf(buf, sizeof(buf), "Connecting to %s (%s:%u)", hostname,
p, port);
q, port);
else
snprintf(buf, sizeof(buf), "Connecting to %s", hostname);
free(q);
_imap4_event_status(imap4, AS_CONNECTING, buf);
/* connect to the remote host */
if((connect(imap4->fd, ai->ai_addr, ai->ai_addrlen) != 0
@ -1292,10 +1292,9 @@ static gboolean _on_watch_can_connect(GIOChannel * source,
char const * hostname = imap4->config[I4CV_HOSTNAME].value;
uint16_t port = (unsigned long)imap4->config[I4CV_PORT].value;
struct addrinfo * ai;
char const * p;
SSL_CTX * ssl_ctx;
char buf[128];
char buf2[128];
char * q;
if(condition != G_IO_OUT || source != imap4->channel)
return FALSE; /* should not happen */
@ -1314,13 +1313,13 @@ static gboolean _on_watch_can_connect(GIOChannel * source,
/* XXX remember the address instead */
if(_common_lookup(hostname, port, &ai) == 0)
{
if((p = inet_ntop(ai->ai_family, ai->ai_addr, buf2,
sizeof(buf2))) != NULL)
if((q = _common_lookup_print(ai)) != NULL)
snprintf(buf, sizeof(buf), "Connected to %s (%s:%u)",
hostname, p, port);
hostname, q, port);
else
snprintf(buf, sizeof(buf), "Connected to %s", hostname);
_imap4_event_status(imap4, AS_CONNECTED, buf);
free(q);
freeaddrinfo(ai);
}
imap4->wr_source = 0;

View File

@ -644,7 +644,7 @@ static gboolean _on_connect(gpointer data)
struct addrinfo * ai;
int res;
char buf[128];
char buf2[128];
char * q;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s()\n", __func__);
@ -679,12 +679,12 @@ static gboolean _on_connect(gpointer data)
/* FIXME report properly as a warning instead */
helper->error(NULL, strerror(errno), 1);
/* report the current status */
if((p = inet_ntop(ai->ai_family, ai->ai_addr, buf2, sizeof(buf2)))
!= NULL)
if((q = _common_lookup_print(ai)) != NULL)
snprintf(buf, sizeof(buf), "Connecting to %s (%s:%u)", hostname,
p, port);
q, port);
else
snprintf(buf, sizeof(buf), "Connecting to %s", hostname);
free(q);
_pop3_event_status(pop3, AS_CONNECTING, buf);
/* connect to the remote host */
if((connect(pop3->fd, ai->ai_addr, ai->ai_addrlen) != 0
@ -751,9 +751,8 @@ static gboolean _on_watch_can_connect(GIOChannel * source,
uint16_t port = (unsigned long)pop3->config[P3CV_PORT].value;
struct addrinfo * ai;
SSL_CTX * ssl_ctx;
char const * p;
char buf[128];
char buf2[128];
char * q;
if(condition != G_IO_OUT || source != pop3->channel)
return FALSE; /* should not happen */
@ -772,13 +771,13 @@ static gboolean _on_watch_can_connect(GIOChannel * source,
/* XXX remember the address instead */
if(_common_lookup(hostname, port, &ai) == 0)
{
if((p = inet_ntop(ai->ai_family, ai->ai_addr, buf2,
sizeof(buf2))) != NULL)
if((q = _common_lookup_print(ai)) != NULL)
snprintf(buf, sizeof(buf), "Connected to %s (%s:%u)",
hostname, p, port);
hostname, q, port);
else
snprintf(buf, sizeof(buf), "Connected to %s", hostname);
_pop3_event_status(pop3, AS_CONNECTED, buf);
free(q);
freeaddrinfo(ai);
}
pop3->wr_source = 0;