]> 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)
committerGitLab <gitlab@git.dovecot.net>
Tue, 15 Nov 2016 09:53:23 +0000 (11:53 +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 0031764bf9f1153d0c4a5bac6c63e8472fa7b4d2..9c3ac2ccaa9ebe75b278ecc7d1650a09767757dd 100644 (file)
@@ -981,6 +981,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 56a28d29f9fec91a65291681899e686d6c161527..de23431ed03cbc6fb1b5622d11244d0804f1c68e 100644 (file)
@@ -155,6 +155,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. */