[M] Change in openvpn[master]: lladdr: Clean up code

"flichtenheld (Code Review)" <[email protected]>
Newsgroups gmane.network.openvpn.devel
Message-ID <08c49362b0466d16b761c9d365ff137b6e255216-EmailReplacePatchSet-HTML@gerrit.openvpn.net>
Attention is currently required from: flichtenheld, plaisthos.

Hello cron2, plaisthos, 

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1675?usp=email

to look at the new patch set (#4).


Change subject: lladdr: Clean up code
......................................................................

lladdr: Clean up code

cppcheck complained about the creation of a useless
variable on Windows. With the old code this was not
fixable in a good way. So rewrite the whole code to
hopefully be much more readable.

Change-Id: Ibe7b3c17176c97a1cbb4d9cc87735b05e62c4f43
Signed-off-by: Frank Lichtenheld <[email protected]>
---
M src/openvpn/lladdr.c
1 file changed, 39 insertions(+), 26 deletions(-)


  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/75/1675/4

diff --git a/src/openvpn/lladdr.c b/src/openvpn/lladdr.c
index d8bcad9..6d2afe2 100644
--- a/src/openvpn/lladdr.c
+++ b/src/openvpn/lladdr.c
@@ -13,42 +13,55 @@
 #include "lladdr.h"
 #include "proto.h"
 
+#ifdef TARGET_LINUX
+static int
+set_lladdr_linux(openvpn_net_ctx_t *ctx, const char *ifname, const char *lladdr)
+{
+    uint8_t addr[OPENVPN_ETH_ALEN];
+
+    sscanf(lladdr, MAC_FMT, MAC_SCAN_ARG(addr));
+    return (net_addr_ll_set(ctx, ifname, addr) == 0);
+}
+#else /* TARGET_LINUX */
+
+#if defined(TARGET_SOLARIS) || defined(TARGET_FREEBSD)
+#define IFCONFIG_LLADDR_CMD "ether"
+#elif defined(TARGET_OPENBSD) || defined(TARGET_DARWIN)
+#define IFCONFIG_LLADDR_CMD "lladdr"
+#endif
+static int
+set_lladdr_ifconfig(const char *ifname, const char *lladdr, const struct env_set *es)
+{
+#ifdef IFCONFIG_LLADDR_CMD
+    struct argv argv = argv_new();
+    argv_msg(M_INFO, &argv);
+    argv_printf(&argv, "%s %s %s %s", IFCONFIG_PATH, ifname, IFCONFIG_LLADDR_CMD, lladdr);
+    int r = openvpn_execve_check(&argv, es, M_WARN, "ERROR: Unable to set link layer address.");
+    argv_free(&argv);
+    return r;
+#else
+    msg(M_WARN,
+        "Sorry, but I don't know how to configure link layer addresses on this operating system.");
+    return -1;
+#endif
+}
+#endif /* TARGET_LINUX */
+
 int
 set_lladdr(openvpn_net_ctx_t *ctx, const char *ifname, const char *lladdr, const struct env_set *es)
 {
-    int r;
-
     if (!ifname || !lladdr)
     {
         return -1;
     }
 
 #if defined(TARGET_LINUX)
-    uint8_t addr[OPENVPN_ETH_ALEN];
+    int r = set_lladdr_linux(ctx, ifname, lladdr);
+#else
+    int r = set_lladdr_ifconfig(ifname, lladdr, es);
+#endif
 
-    sscanf(lladdr, MAC_FMT, MAC_SCAN_ARG(addr));
-    r = (net_addr_ll_set(ctx, ifname, addr) == 0);
-#else /* if defined(TARGET_LINUX) */
-    struct argv argv = argv_new();
-#if defined(TARGET_SOLARIS)
-    argv_printf(&argv, "%s %s ether %s", IFCONFIG_PATH, ifname, lladdr);
-#elif defined(TARGET_OPENBSD)
-    argv_printf(&argv, "%s %s lladdr %s", IFCONFIG_PATH, ifname, lladdr);
-#elif defined(TARGET_DARWIN)
-    argv_printf(&argv, "%s %s lladdr %s", IFCONFIG_PATH, ifname, lladdr);
-#elif defined(TARGET_FREEBSD)
-    argv_printf(&argv, "%s %s ether %s", IFCONFIG_PATH, ifname, lladdr);
-#else  /* if defined(TARGET_SOLARIS) */
-    msg(M_WARN,
-        "Sorry, but I don't know how to configure link layer addresses on this operating system.");
-    return -1;
-#endif /* if defined(TARGET_SOLARIS) */
-    argv_msg(M_INFO, &argv);
-    r = openvpn_execve_check(&argv, es, M_WARN, "ERROR: Unable to set link layer address.");
-    argv_free(&argv);
-#endif /* if defined(TARGET_LINUX) */
-
-    if (r)
+    if (r > 0)
     {
         msg(M_INFO, "TUN/TAP link layer address set to %s", lladdr);
     }

-- 
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1675?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ibe7b3c17176c97a1cbb4d9cc87735b05e62c4f43
Gerrit-Change-Number: 1675
Gerrit-PatchSet: 4
Gerrit-Owner: flichtenheld <[email protected]>
Gerrit-Reviewer: cron2 <[email protected]>
Gerrit-Reviewer: plaisthos <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]>

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.