]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: net - Use getnameinfo() to convert addresses to string
authorAki Tuomi <aki.tuomi@open-xchange.com>
Mon, 4 Apr 2022 09:33:07 +0000 (12:33 +0300)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Thu, 14 Apr 2022 11:10:37 +0000 (11:10 +0000)
src/lib/net.c

index 557ce3f7b8e8a863adc372ee9aa4e76f7b0401ab..c318387cd6f5cb27b1ef45a6f846ba28bd9dd955 100644 (file)
@@ -923,11 +923,28 @@ int net_getunixcred(int fd, struct net_unix_cred *cred_r)
 
 const char *net_ip2addr(const struct ip_addr *ip)
 {
-       char *addr = t_malloc_no0(MAX_IP_LEN+1);
-
-       if (inet_ntop(ip->family, &ip->u.ip6, addr, MAX_IP_LEN) == NULL)
+       union sockaddr_union u;
+       socklen_t so_len;
+
+       if (ip->family == AF_INET) {
+               u.sin.sin_addr = ip->u.ip4;
+               u.sin.sin_family = ip->family;
+               so_len = sizeof(u.sin);
+       } else if (ip->family == AF_INET6) {
+               u.sin6.sin6_addr = ip->u.ip6;
+               u.sin6.sin6_family = ip->family;
+               u.sin6.sin6_scope_id = ip->scope_id;
+               so_len = sizeof(u.sin6);
+       } else /* not an IP */
                return "";
 
+       char *addr = t_malloc_no0(MAX_IP_LEN+1);
+       int ret;
+       if ((ret = getnameinfo(&u.sa, so_len, addr, MAX_IP_LEN+1, NULL, 0,
+                              NI_NUMERICHOST)) < 0) {
+               (void)net_handle_gai_error("getnameinfo", ret, TRUE);
+               return "";
+       }
        return addr;
 }