From 3afd81df9b58a1a0356a1104bb80d17165f5787b Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Sun, 17 Aug 2014 16:17:11 +0200 Subject: Deal better with displaying IPv6 addresses --- common.c | 9 +++++++++ kike.c | 28 +++++++++++++++++----------- zyklonb.c | 5 ++++- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/common.c b/common.c index 782572d..25bf761 100644 --- a/common.c +++ b/common.c @@ -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 ----------------------------------------------------- static regex_t * diff --git a/kike.c b/kike.c index 39b824a..d66b012 100644 --- a/kike.c +++ b/kike.c @@ -2661,7 +2661,10 @@ on_irc_client_available (const struct pollfd *pfd, void *user_data) host, sizeof host, port, sizeof port, NI_NUMERICSERV); if (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); client_init (c); @@ -2922,26 +2925,27 @@ irc_listen (struct addrinfo *gai_iter) soft_assert (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) != -1); - char real_host[NI_MAXHOST], real_port[NI_MAXSERV]; - real_host[0] = real_port[0] = '\0'; + char host[NI_MAXHOST], port[NI_MAXSERV]; + host[0] = port[0] = '\0'; 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); if (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)) - print_error ("bind to %s:%s failed: %s", - real_host, real_port, strerror (errno)); + print_error ("bind to %s failed: %s", address, strerror (errno)); else if (listen (fd, 16 /* arbitrary number */)) - print_error ("listen on %s:%s failed: %s", - real_host, real_port, strerror (errno)); + print_error ("listen on %s failed: %s", address, strerror (errno)); else { - print_status ("listening on %s:%s", real_host, real_port); + print_status ("listening on %s", address); + free (address); return fd; } + free (address); xclose (fd); return -1; } @@ -2954,8 +2958,10 @@ irc_listen_resolve (struct server_context *ctx, int err = getaddrinfo (host, port, gai_hints, &gai_result); if (err) { - print_error ("bind to %s:%s failed: %s: %s", - host, port, "getaddrinfo", gai_strerror (err)); + char *address = format_host_port_pair (host, port); + print_error ("bind to %s failed: %s: %s", + address, "getaddrinfo", gai_strerror (err)); + free (address); return; } diff --git a/zyklonb.c b/zyklonb.c index 806de8b..d4e66ca 100644 --- a/zyklonb.c +++ b/zyklonb.c @@ -355,7 +355,10 @@ irc_establish_connection (struct bot_context *ctx, real_host = buf; // 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)) break; -- cgit v1.2.3-70-g09d2