Deal better with displaying IPv6 addresses

This commit is contained in:
Přemysl Eric Janouch 2014-08-17 16:17:11 +02:00
parent 8632e5fe83
commit 3afd81df9b
3 changed files with 30 additions and 12 deletions

View File

@ -1604,6 +1604,15 @@ xssl_get_error (SSL *ssl, int result, const char **error_info)
} }
} }
static char *
format_host_port_pair (const char *host, const char *port)
{
// IPv6 addresses mess with the "colon notation"; let's go with RFC 2732
if (strchr (host, ':'))
return xstrdup_printf ("[%s]:%s", host, port);
return xstrdup_printf ("%s:%s", host, port);
}
// --- Regular expressions ----------------------------------------------------- // --- Regular expressions -----------------------------------------------------
static regex_t * static regex_t *

28
kike.c
View File

@ -2661,7 +2661,10 @@ on_irc_client_available (const struct pollfd *pfd, void *user_data)
host, sizeof host, port, sizeof port, NI_NUMERICSERV); host, sizeof host, port, sizeof port, NI_NUMERICSERV);
if (err) if (err)
print_debug ("%s: %s", "getnameinfo", gai_strerror (err)); print_debug ("%s: %s", "getnameinfo", gai_strerror (err));
print_debug ("accepted connection from %s:%s", host, port);
char *address = format_host_port_pair (host, port);
print_debug ("accepted connection from %s", address);
free (address);
struct client *c = xmalloc (sizeof *c); struct client *c = xmalloc (sizeof *c);
client_init (c); client_init (c);
@ -2922,26 +2925,27 @@ irc_listen (struct addrinfo *gai_iter)
soft_assert (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, soft_assert (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
&yes, sizeof yes) != -1); &yes, sizeof yes) != -1);
char real_host[NI_MAXHOST], real_port[NI_MAXSERV]; char host[NI_MAXHOST], port[NI_MAXSERV];
real_host[0] = real_port[0] = '\0'; host[0] = port[0] = '\0';
int err = getnameinfo (gai_iter->ai_addr, gai_iter->ai_addrlen, int err = getnameinfo (gai_iter->ai_addr, gai_iter->ai_addrlen,
real_host, sizeof real_host, real_port, sizeof real_port, host, sizeof host, port, sizeof port,
NI_NUMERICHOST | NI_NUMERICSERV); NI_NUMERICHOST | NI_NUMERICSERV);
if (err) if (err)
print_debug ("%s: %s", "getnameinfo", gai_strerror (err)); print_debug ("%s: %s", "getnameinfo", gai_strerror (err));
char *address = format_host_port_pair (host, port);
if (bind (fd, gai_iter->ai_addr, gai_iter->ai_addrlen)) if (bind (fd, gai_iter->ai_addr, gai_iter->ai_addrlen))
print_error ("bind to %s:%s failed: %s", print_error ("bind to %s failed: %s", address, strerror (errno));
real_host, real_port, strerror (errno));
else if (listen (fd, 16 /* arbitrary number */)) else if (listen (fd, 16 /* arbitrary number */))
print_error ("listen on %s:%s failed: %s", print_error ("listen on %s failed: %s", address, strerror (errno));
real_host, real_port, strerror (errno));
else else
{ {
print_status ("listening on %s:%s", real_host, real_port); print_status ("listening on %s", address);
free (address);
return fd; return fd;
} }
free (address);
xclose (fd); xclose (fd);
return -1; return -1;
} }
@ -2954,8 +2958,10 @@ irc_listen_resolve (struct server_context *ctx,
int err = getaddrinfo (host, port, gai_hints, &gai_result); int err = getaddrinfo (host, port, gai_hints, &gai_result);
if (err) if (err)
{ {
print_error ("bind to %s:%s failed: %s: %s", char *address = format_host_port_pair (host, port);
host, port, "getaddrinfo", gai_strerror (err)); print_error ("bind to %s failed: %s: %s",
address, "getaddrinfo", gai_strerror (err));
free (address);
return; return;
} }

View File

@ -355,7 +355,10 @@ irc_establish_connection (struct bot_context *ctx,
real_host = buf; real_host = buf;
// XXX: we shouldn't mix these statuses with `struct error'; choose 1! // XXX: we shouldn't mix these statuses with `struct error'; choose 1!
print_status ("connecting to `%s:%s'...", real_host, port); char *address = format_host_port_pair (real_host, port);
print_status ("connecting to %s...", address);
free (address);
if (!connect (sockfd, gai_iter->ai_addr, gai_iter->ai_addrlen)) if (!connect (sockfd, gai_iter->ai_addr, gai_iter->ai_addrlen))
break; break;