From: Razvan Cojocaru <[email protected]> ifconfig_pool_acquire() duplicated the address calculation of ifconfig_pool_handle_to_ip_base() and ifconfig_pool_handle_to_ipv6_base(). Use those instead, and add in6_addr_low32() for the three open-coded conversions of the last 32 bits of an IPv6 address. Its uint32_t casts also avoid shifting an int into the sign bit.
Change-Id: I0f8ea4fa572f84e6dcc9b0806c23977c64ea645d Signed-off-by: Razvan Cojocaru <[email protected]> Acked-by: Antonio Quartulli <[email protected]> Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2126 --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master. Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2126 This mail reflects revision 1 of this Change. Acked-by according to Gerrit (reflected above): Antonio Quartulli <[email protected]> diff --git a/src/openvpn/pool.c b/src/openvpn/pool.c index fff0e3f..7978f93 100644 --- a/src/openvpn/pool.c +++ b/src/openvpn/pool.c @@ -35,6 +35,20 @@ #include "memdbg.h" +static in_addr_t ifconfig_pool_handle_to_ip_base(const struct ifconfig_pool *pool, + ifconfig_pool_handle hand); + +static struct in6_addr ifconfig_pool_handle_to_ipv6_base(const struct ifconfig_pool *pool, + ifconfig_pool_handle hand); + +/* Return the last 32 bits of an IPv6 address in host byte order */ +static inline uint32_t +in6_addr_low32(const struct in6_addr *a) +{ + return ((uint32_t)a->s6_addr[12] << 24) | ((uint32_t)a->s6_addr[13] << 16) + | ((uint32_t)a->s6_addr[14] << 8) | a->s6_addr[15]; +} + static void ifconfig_pool_entry_free(struct ifconfig_pool_entry *ipe, bool hard) { @@ -192,8 +206,7 @@ * 4 bytes, therefore we can just extract that and use it as base in * integer form */ - uint32_t base = (ipv6_base.s6_addr[12] << 24) | (ipv6_base.s6_addr[13] << 16) - | (ipv6_base.s6_addr[14] << 8) | ipv6_base.s6_addr[15]; + uint32_t base = in6_addr_low32(&ipv6_base); /* some bits of the last 4 bytes may still be part of the network * portion of the address, therefore we need to set them to 0 */ @@ -314,33 +327,22 @@ if (pool->ipv4.enabled && local && remote) { - switch (pool->ipv4.type) + const in_addr_t b = ifconfig_pool_handle_to_ip_base(pool, i); + if (pool->ipv4.type == IFCONFIG_POOL_30NET) { - case IFCONFIG_POOL_30NET: - { - in_addr_t b = pool->ipv4.base + (i << 2); - *local = b + 1; - *remote = b + 2; - break; - } - - case IFCONFIG_POOL_INDIV: - { - in_addr_t b = pool->ipv4.base + i; - *local = 0; - *remote = b; - break; - } - - default: - ASSERT(0); + *local = b + 1; + *remote = b + 2; + } + else + { + *local = 0; + *remote = b; } } - /* IPv6 pools are always INDIV (--linear) */ if (pool->ipv6.enabled && remote_ipv6) { - *remote_ipv6 = add_in6_addr(pool->ipv6.base, i); + *remote_ipv6 = ifconfig_pool_handle_to_ipv6_base(pool, i); } } return i; @@ -415,11 +417,8 @@ } } - base = (pool->ipv6.base.s6_addr[12] << 24) | (pool->ipv6.base.s6_addr[13] << 16) - | (pool->ipv6.base.s6_addr[14] << 8) | pool->ipv6.base.s6_addr[15]; - - addr = (in_addr->s6_addr[12] << 24) | (in_addr->s6_addr[13] << 16) | (in_addr->s6_addr[14] << 8) - | in_addr->s6_addr[15]; + base = in6_addr_low32(&pool->ipv6.base); + addr = in6_addr_low32(in_addr); ret = addr - base; if (ret < 0 || ret >= pool->size) _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
