]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Add net_ipport2str
authorAki Tuomi <aki.tuomi@dovecot.fi>
Wed, 9 Nov 2016 13:17:01 +0000 (15:17 +0200)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 15 Nov 2016 20:24:29 +0000 (22:24 +0200)
Converts struct ip_addr + port into either
127.0.0.1:6000 or [::1]:6000.

src/lib/net.c
src/lib/net.h

index 73c2d5ce5c2685c2b314c48a6149331d3aed612e..387c8df87ff4742a2a857a0e08671c0f39011cbf 100644 (file)
@@ -1046,6 +1046,18 @@ int net_str2hostport(const char *str, in_port_t default_port,
        return 0;
 }
 
+int net_ipport2str(const struct ip_addr *ip, in_port_t port, const char **str_r)
+{
+       if (!IPADDR_IS_V4(ip) && !IPADDR_IS_V6(ip)) return -1;
+
+       *str_r = t_strdup_printf("%s%s%s:%u",
+                                IPADDR_IS_V6(ip) ? "[" : "",
+                                net_ip2addr(ip),
+                                IPADDR_IS_V6(ip) ? "]" : "",
+                                port);
+       return 0;
+}
+
 int net_ipv6_mapped_ipv4_convert(const struct ip_addr *src,
                                 struct ip_addr *dest)
 {
index 172021f2f04a75ad20c6e33b5564798fc6d14f8c..243a503c4101d88fba9feabc4e8ad40163950497 100644 (file)
@@ -165,6 +165,9 @@ int net_str2port_zero(const char *str, in_port_t *port_r);
    through. */
 int net_str2hostport(const char *str, in_port_t default_port,
                     const char **host_r, in_port_t *port_r);
+/* Converts ip and port to ipv4:port or [ipv6]:port. Returns -1 if
+   ip is not valid IPv4 or IPv6 address. */
+int net_ipport2str(const struct ip_addr *ip, in_port_t port, const char **str_r);
 
 /* Convert IPv6 mapped IPv4 address to an actual IPv4 address. Returns 0 if
    successful, -1 if the source address isn't IPv6 mapped IPv4 address. */