From patchwork Mon Jun 4 23:04:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antonio Quartulli X-Patchwork-Id: 349 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director8.mail.ord1d.rsapps.net ([172.27.255.7]) by backend30.mail.ord1d.rsapps.net (Dovecot) with LMTP id u+OTIIVSFlsEIwAAIUCqbw for ; Tue, 05 Jun 2018 05:06:13 -0400 Received: from proxy11.mail.iad3a.rsapps.net ([172.27.255.7]) by director8.mail.ord1d.rsapps.net (Dovecot) with LMTP id u0FVGoVSFltZcwAAfY0hYg ; Tue, 05 Jun 2018 05:06:13 -0400 Received: from smtp4.gate.iad3a ([172.27.255.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy11.mail.iad3a.rsapps.net with LMTP id mAc0GIVSFlv1PwAAxCvdqw ; Tue, 05 Jun 2018 05:06:13 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp4.gate.iad3a.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dmarc=none (p=nil; dis=none) header.from=unstable.cc X-Suspicious-Flag: YES X-Classification-ID: b1702a26-689f-11e8-a5b4-5254003c557e-1-1 Received: from [216.105.38.7] ([216.105.38.7:47445] helo=lists.sourceforge.net) by smtp4.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.1.56364 r(Core:4.2.1.14)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id F7/AE-30858-482561B5; Tue, 05 Jun 2018 05:06:12 -0400 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1fQ7u9-0004AO-91; Tue, 05 Jun 2018 09:05:25 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1fQ7tw-00049u-0A for openvpn-devel@lists.sourceforge.net; Tue, 05 Jun 2018 09:05:12 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=References:In-Reply-To:Message-Id:Date:Subject:Cc: To:From:Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=zBCFtXVSuHoAkEMpe8HW0bDS7WD1/NGnemBxkLu7fqA=; b=lV0EkP3+fftUHMxNQUZJ2dTIw7 MKv9ApCbLl5LmKAZEI9NpIjpEpb22ARyWcPV+o1zG+zttvAi0YfVpXdCKfKmRS7Ro8f02r141dOPj CbslxLB/egqHuDG3oFXeXlTg2AzR/yeIQ8ioQkkAMup/3Cnyxhu6d11j5UDicwhFiKsI=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To :MIME-Version:Content-Type:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=zBCFtXVSuHoAkEMpe8HW0bDS7WD1/NGnemBxkLu7fqA=; b=XBkCBoIhEcIC393PuVYF+29djQ iGDglSFmT6+DsBfPhbxNhZ1EI+9JGwfl9Hpgzvjkle6SHxlXkp0HsNP+6ySllH3tYwcTK+oIW75gM nvd+BtR6c6RYGS+Q2SyxU98Qlndxbubzr8JWCz6QzBhsYnt5EplKIexruFPf/pnpHu1w=; Received: from s2.neomailbox.net ([5.148.176.60]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) id 1fQ7tq-006SQC-G2 for openvpn-devel@lists.sourceforge.net; Tue, 05 Jun 2018 09:05:11 +0000 From: Antonio Quartulli To: openvpn-devel@lists.sourceforge.net Date: Tue, 5 Jun 2018 17:04:18 +0800 Message-Id: <20180605090421.9746-3-a@unstable.cc> In-Reply-To: <20180605090421.9746-1-a@unstable.cc> References: <20180605090421.9746-1-a@unstable.cc> X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [5.148.176.60 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record X-Headers-End: 1fQ7tq-006SQC-G2 Subject: [Openvpn-devel] [PATCH 2/5] pool: restyle ipv4/ipv6 members to improve readability X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Antonio Quartulli MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox From: Antonio Quartulli (This is only code refactoring) IPv4 and IPv6 members are all part of the same flat hierarchy in the pool data structure, without a proper name convention. Create 2 sub-structures to properly saperate IPv4 from IPv6 relate members. This should make the structure more organized and also slightly improve code readability. Signed-off-by: Antonio Quartulli Acked-by: Gert Doering --- src/openvpn/pool.c | 72 +++++++++++++++++++++++----------------------- src/openvpn/pool.h | 16 +++++++---- 2 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/openvpn/pool.c b/src/openvpn/pool.c index da28bc06..26eb0ebd 100644 --- a/src/openvpn/pool.c +++ b/src/openvpn/pool.c @@ -66,7 +66,7 @@ ifconfig_pool_find(struct ifconfig_pool *pool, const char *common_name) int previous_usage = -1; int new_usage = -1; - for (i = 0; i < pool->size; ++i) + for (i = 0; i < pool->ipv4.size; ++i) { struct ifconfig_pool_entry *ipe = &pool->list[i]; if (!ipe->in_use) @@ -158,19 +158,19 @@ ifconfig_pool_init(int type, in_addr_t start, in_addr_t end, ASSERT(start <= end && end - start < IFCONFIG_POOL_MAX); ALLOC_OBJ_CLEAR(pool, struct ifconfig_pool); - pool->type = type; + pool->ipv4.type = type; pool->duplicate_cn = duplicate_cn; - switch (type) + switch (pool->ipv4.type) { case IFCONFIG_POOL_30NET: - pool->base = start & ~3; - pool->size = (((end | 3) + 1) - pool->base) >> 2; + pool->ipv4.base = start & ~3; + pool->ipv4.size = (((end | 3) + 1) - pool->ipv4.base) >> 2; break; case IFCONFIG_POOL_INDIV: - pool->base = start; - pool->size = end - start + 1; + pool->ipv4.base = start; + pool->ipv4.size = end - start + 1; break; default: @@ -178,30 +178,30 @@ ifconfig_pool_init(int type, in_addr_t start, in_addr_t end, } /* IPv6 pools are always "INDIV" type */ - pool->ipv6 = ipv6_pool; + pool->ipv6.enabled = ipv6_pool; - if (pool->ipv6) + if (pool->ipv6.enabled) { - pool->base_ipv6 = ipv6_base; - pool->size_ipv6 = ipv6_netbits>96 ? ( 1<<(128-ipv6_netbits) ) + pool->ipv6.base = ipv6_base; + pool->ipv6.size = ipv6_netbits > 96 ? (1 << (128 - ipv6_netbits)) : IFCONFIG_POOL_MAX; msg( D_IFCONFIG_POOL, "IFCONFIG POOL IPv6: (IPv4) size=%d, size_ipv6=%d, netbits=%d, base_ipv6=%s", - pool->size, pool->size_ipv6, ipv6_netbits, - print_in6_addr( pool->base_ipv6, 0, &gc )); + pool->ipv4.size, pool->ipv6.size, ipv6_netbits, + print_in6_addr(pool->ipv6.base, 0, &gc)); /* the current code is very simple and assumes that the IPv6 * pool is at least as big as the IPv4 pool, and we don't need * to do separate math etc. for IPv6 */ - ASSERT( pool->size < pool->size_ipv6 ); + ASSERT(pool->ipv4.size < pool->ipv6.size); } - ALLOC_ARRAY_CLEAR(pool->list, struct ifconfig_pool_entry, pool->size); + ALLOC_ARRAY_CLEAR(pool->list, struct ifconfig_pool_entry, pool->ipv4.size); msg(D_IFCONFIG_POOL, "IFCONFIG POOL: base=%s size=%d, ipv6=%d", - print_in_addr_t(pool->base, 0, &gc), - pool->size, pool->ipv6 ); + print_in_addr_t(pool->ipv4.base, 0, &gc), + pool->ipv4.size, pool->ipv6.enabled); gc_free(&gc); return pool; @@ -213,7 +213,7 @@ ifconfig_pool_free(struct ifconfig_pool *pool) if (pool) { int i; - for (i = 0; i < pool->size; ++i) + for (i = 0; i < pool->ipv4.size; ++i) { ifconfig_pool_entry_free(&pool->list[i], true); } @@ -239,11 +239,11 @@ ifconfig_pool_acquire(struct ifconfig_pool *pool, in_addr_t *local, in_addr_t *r ipe->common_name = string_alloc(common_name, NULL); } - switch (pool->type) + switch (pool->ipv4.type) { case IFCONFIG_POOL_30NET: { - in_addr_t b = pool->base + (i << 2); + in_addr_t b = pool->ipv4.base + (i << 2); *local = b + 1; *remote = b + 2; break; @@ -251,7 +251,7 @@ ifconfig_pool_acquire(struct ifconfig_pool *pool, in_addr_t *local, in_addr_t *r case IFCONFIG_POOL_INDIV: { - in_addr_t b = pool->base + i; + in_addr_t b = pool->ipv4.base + i; *local = 0; *remote = b; break; @@ -262,9 +262,9 @@ ifconfig_pool_acquire(struct ifconfig_pool *pool, in_addr_t *local, in_addr_t *r } /* IPv6 pools are always INDIV (--linear) */ - if (pool->ipv6 && remote_ipv6) + if (pool->ipv6.enabled && remote_ipv6) { - *remote_ipv6 = add_in6_addr( pool->base_ipv6, i ); + *remote_ipv6 = add_in6_addr(pool->ipv6.base, i); } } return i; @@ -274,7 +274,7 @@ bool ifconfig_pool_release(struct ifconfig_pool *pool, ifconfig_pool_handle hand, const bool hard) { bool ret = false; - if (pool && hand >= 0 && hand < pool->size) + if (pool && hand >= 0 && hand < pool->ipv4.size) { ifconfig_pool_entry_free(&pool->list[hand], hard); ret = true; @@ -291,17 +291,17 @@ ifconfig_pool_ip_base_to_handle(const struct ifconfig_pool *pool, const in_addr_ { ifconfig_pool_handle ret = -1; - switch (pool->type) + switch (pool->ipv4.type) { case IFCONFIG_POOL_30NET: { - ret = (addr - pool->base) >> 2; + ret = (addr - pool->ipv4.base) >> 2; break; } case IFCONFIG_POOL_INDIV: { - ret = (addr - pool->base); + ret = (addr - pool->ipv4.base); break; } @@ -309,7 +309,7 @@ ifconfig_pool_ip_base_to_handle(const struct ifconfig_pool *pool, const in_addr_ ASSERT(0); } - if (ret < 0 || ret >= pool->size) + if (ret < 0 || ret >= pool->ipv4.size) { ret = -1; } @@ -322,19 +322,19 @@ ifconfig_pool_handle_to_ip_base(const struct ifconfig_pool *pool, ifconfig_pool_ { in_addr_t ret = 0; - if (hand >= 0 && hand < pool->size) + if (hand >= 0 && hand < pool->ipv4.size) { - switch (pool->type) + switch (pool->ipv4.type) { case IFCONFIG_POOL_30NET: { - ret = pool->base + (hand << 2); + ret = pool->ipv4.base + (hand << 2); break; } case IFCONFIG_POOL_INDIV: { - ret = pool->base + hand; + ret = pool->ipv4.base + hand; break; } @@ -352,9 +352,9 @@ ifconfig_pool_handle_to_ipv6_base(const struct ifconfig_pool *pool, ifconfig_poo struct in6_addr ret = in6addr_any; /* IPv6 pools are always INDIV (--linear) */ - if (hand >= 0 && hand < pool->size_ipv6) + if (hand >= 0 && hand < pool->ipv6.size) { - ret = add_in6_addr( pool->base_ipv6, hand ); + ret = add_in6_addr( pool->ipv6.base, hand ); } return ret; } @@ -382,13 +382,13 @@ ifconfig_pool_list(const struct ifconfig_pool *pool, struct status_output *out) struct gc_arena gc = gc_new(); int i; - for (i = 0; i < pool->size; ++i) + for (i = 0; i < pool->ipv4.size; ++i) { const struct ifconfig_pool_entry *e = &pool->list[i]; if (e->common_name) { const in_addr_t ip = ifconfig_pool_handle_to_ip_base(pool, i); - if (pool->ipv6) + if (pool->ipv6.enabled) { struct in6_addr ip6 = ifconfig_pool_handle_to_ipv6_base(pool, i); status_printf(out, "%s,%s,%s", diff --git a/src/openvpn/pool.h b/src/openvpn/pool.h index 6de28ac5..544d4180 100644 --- a/src/openvpn/pool.h +++ b/src/openvpn/pool.h @@ -47,13 +47,17 @@ struct ifconfig_pool_entry struct ifconfig_pool { - in_addr_t base; - int size; - int type; bool duplicate_cn; - bool ipv6; - struct in6_addr base_ipv6; - unsigned int size_ipv6; + struct { + int type; + in_addr_t base; + int size; + } ipv4; + struct { + bool enabled; + struct in6_addr base; + unsigned int size; + } ipv6; struct ifconfig_pool_entry *list; };