From: Martti Rannanjärvi Date: Mon, 9 May 2016 12:24:28 +0000 (+0300) Subject: lib: net_get_ip_any[46]() to struct net_ip[46]_any X-Git-Tag: 2.3.0.rc1~3579 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=db623deb12c3566618faba5a35a44ceed83c3dc0;p=thirdparty%2Fdovecot%2Fcore.git lib: net_get_ip_any[46]() to struct net_ip[46]_any Also add struct net_ip[46]_loopback. --- diff --git a/src/director/director.c b/src/director/director.c index 11089b5cf7..e041d84904 100644 --- a/src/director/director.c +++ b/src/director/director.c @@ -26,14 +26,10 @@ bool director_debug; static bool director_is_self_ip_set(struct director *dir) { - struct ip_addr ip; - - net_get_ip_any4(&ip); - if (net_ip_compare(&dir->self_ip, &ip)) + if (net_ip_compare(&dir->self_ip, &net_ip4_any)) return FALSE; - net_get_ip_any6(&ip); - if (net_ip_compare(&dir->self_ip, &ip)) + if (net_ip_compare(&dir->self_ip, &net_ip6_any)) return FALSE; return TRUE; diff --git a/src/lib-http/test-http-server.c b/src/lib-http/test-http-server.c index 8721b0bee3..b134f094b6 100644 --- a/src/lib-http/test-http-server.c +++ b/src/lib-http/test-http-server.c @@ -115,7 +115,7 @@ int main(int argc, char *argv[]) if (argc < 2 || net_str2port(argv[1], &port) < 0) i_fatal("Port parameter missing"); if (argc < 3) - net_get_ip_any4(&my_ip); + my_ip = net_ip4_any; else if (net_addr2ip(argv[2], &my_ip) < 0) i_fatal("Invalid IP parameter"); diff --git a/src/lib/net.c b/src/lib/net.c index 33186791e5..06034eb4c4 100644 --- a/src/lib/net.c +++ b/src/lib/net.c @@ -375,17 +375,25 @@ int net_set_recv_buffer_size(int fd, size_t size) return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)); } -void net_get_ip_any4(struct ip_addr *ip) -{ - ip->family = AF_INET; - ip->u.ip4.s_addr = INADDR_ANY; -} +const struct ip_addr net_ip4_any = { + .family = AF_INET, + .u.ip4.s_addr = INADDR_ANY +}; -void net_get_ip_any6(struct ip_addr *ip) -{ - ip->family = AF_INET6; - ip->u.ip6 = in6addr_any; -} +const struct ip_addr net_ip6_any = { + .family = AF_INET6, + .u.ip6 = IN6ADDR_ANY_INIT +}; + +const struct ip_addr net_ip4_loopback = { + .family = AF_INET, + .u.ip4.s_addr = INADDR_LOOPBACK +}; + +const struct ip_addr net_ip6_loopback = { + .family = AF_INET6, + .u.ip6 = IN6ADDR_LOOPBACK_INIT +}; int net_listen(const struct ip_addr *my_ip, in_port_t *port, int backlog) { diff --git a/src/lib/net.h b/src/lib/net.h index a56a7a6305..3b0c9c7684 100644 --- a/src/lib/net.h +++ b/src/lib/net.h @@ -48,6 +48,14 @@ enum net_listen_flags { NET_LISTEN_FLAG_REUSEPORT = 0x01 }; +/* INADDR_ANY for IPv4 or IPv6. The IPv6 any address may + include IPv4 depending on the system (Linux yes, BSD no). */ +extern const struct ip_addr net_ip4_any; +extern const struct ip_addr net_ip6_any; + +extern const struct ip_addr net_ip4_loopback; +extern const struct ip_addr net_ip6_loopback; + /* Returns TRUE if IPs are the same */ bool net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2); /* Returns 0 if IPs are the same, -1 or 1 otherwise. */ @@ -84,11 +92,6 @@ int net_set_cork(int fd, bool cork) ATTR_NOWARN_UNUSED_RESULT; int net_set_send_buffer_size(int fd, size_t size); int net_set_recv_buffer_size(int fd, size_t size); -/* Set IP to contain INADDR_ANY for IPv4 or IPv6. The IPv6 any address may - include IPv4 depending on the system (Linux yes, BSD no). */ -void net_get_ip_any4(struct ip_addr *ip); -void net_get_ip_any6(struct ip_addr *ip); - /* Listen for connections on a socket */ int net_listen(const struct ip_addr *my_ip, in_port_t *port, int backlog); int net_listen_full(const struct ip_addr *my_ip, in_port_t *port, diff --git a/src/master/service.c b/src/master/service.c index d9f49d6bb7..1bae79f8c7 100644 --- a/src/master/service.c +++ b/src/master/service.c @@ -79,7 +79,7 @@ resolve_ip(const char *address, const struct ip_addr **ips_r, if (address == NULL || strcmp(address, "*") == 0) { /* IPv4 any */ ip_list = t_new(struct ip_addr, 1); - net_get_ip_any4(ip_list); + *ip_list = net_ip4_any; *ips_r = ip_list; *ips_count_r = 1; return 0; @@ -88,7 +88,7 @@ resolve_ip(const char *address, const struct ip_addr **ips_r, if (strcmp(address, "::") == 0 || strcmp(address, "[::]") == 0) { /* IPv6 any */ ip_list = t_new(struct ip_addr, 1); - net_get_ip_any6(ip_list); + *ip_list = net_ip6_any; *ips_r = ip_list; *ips_count_r = 1; return 0;