[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 2/3] Move virMacAddrXXX functions to src/util/virmacaddr.[ch]



From: "Daniel P. Berrange" <berrange redhat com>

Move the virMacAddrXXX functions out of util.[ch] and into a
new dedicate file virmacaddr.[ch]
---
 src/Makefile.am             |    1 +
 src/conf/capabilities.h     |    2 +-
 src/conf/network_conf.h     |    2 +-
 src/libvirt_private.syms    |   11 ++-
 src/util/util.c             |   99 ---------------------------------
 src/util/util.h             |   13 ----
 src/util/virmacaddr.c       |  128 +++++++++++++++++++++++++++++++++++++++++++
 src/util/virmacaddr.h       |   41 ++++++++++++++
 src/util/virnetdev.c        |    1 +
 src/util/virnetdevmacvlan.c |    2 +-
 10 files changed, 181 insertions(+), 119 deletions(-)
 create mode 100644 src/util/virmacaddr.c
 create mode 100644 src/util/virmacaddr.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 22fd58e..4a5d9f7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -93,6 +93,7 @@ UTIL_SOURCES =							\
 		util/virhashcode.c util/virhashcode.h           \
 		util/virkeycode.c util/virkeycode.h		\
 		util/virkeymaps.h				\
+		util/virmacaddr.h util/virmacaddr.c		\
 		util/virnetdev.h util/virnetdev.c		\
 		util/virnetdevbandwidth.h util/virnetdevbandwidth.c \
 		util/virnetdevbridge.h util/virnetdevbridge.c	\
diff --git a/src/conf/capabilities.h b/src/conf/capabilities.h
index 7f35c17..38d07c4 100644
--- a/src/conf/capabilities.h
+++ b/src/conf/capabilities.h
@@ -25,9 +25,9 @@
 # define __VIR_CAPABILITIES_H
 
 # include "internal.h"
-# include "util.h"
 # include "buf.h"
 # include "cpu_conf.h"
+# include "virmacaddr.h"
 
 # include <libxml/xpath.h>
 
diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h
index 5cb396c..4339a69 100644
--- a/src/conf/network_conf.h
+++ b/src/conf/network_conf.h
@@ -35,7 +35,7 @@
 # include "virsocketaddr.h"
 # include "virnetdevbandwidth.h"
 # include "virnetdevvportprofile.h"
-# include "util.h"
+# include "virmacaddr.h"
 
 enum virNetworkForwardType {
     VIR_NETWORK_FORWARD_NONE   = 0,
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 4e50fa8..8a83f07 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1120,10 +1120,6 @@ virHexToBin;
 virIndexToDiskName;
 virIsDevMapperDevice;
 virKillProcess;
-virMacAddrCompare;
-virMacAddrFormat;
-virMacAddrGenerate;
-virMacAddrParse;
 virParseNumber;
 virParseVersionString;
 virPipeReadUntilEOF;
@@ -1183,6 +1179,13 @@ virKeycodeValueFromString;
 virKeycodeValueTranslate;
 
 
+# virmacaddr.h
+virMacAddrCompare;
+virMacAddrFormat;
+virMacAddrGenerate;
+virMacAddrParse;
+
+
 # virnetclient.h
 virNetClientHasPassFD;
 
diff --git a/src/util/util.c b/src/util/util.c
index 28b608d..baa0f12 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -77,7 +77,6 @@
 #include "command.h"
 #include "nonblocking.h"
 #include "passfd.h"
-#include "virrandom.h"
 
 #ifndef NSIG
 # define NSIG 32
@@ -1762,104 +1761,6 @@ virStrcpy(char *dest, const char *src, size_t destbytes)
     return virStrncpy(dest, src, strlen(src), destbytes);
 }
 
-/* Compare two MAC addresses, ignoring differences in case,
- * as well as leading zeros.
- */
-int
-virMacAddrCompare (const char *p, const char *q)
-{
-    unsigned char c, d;
-    do {
-        while (*p == '0' && c_isxdigit (p[1]))
-            ++p;
-        while (*q == '0' && c_isxdigit (q[1]))
-            ++q;
-        c = c_tolower (*p);
-        d = c_tolower (*q);
-
-        if (c == 0 || d == 0)
-            break;
-
-        ++p;
-        ++q;
-    } while (c == d);
-
-    if (UCHAR_MAX <= INT_MAX)
-        return c - d;
-
-    /* On machines where 'char' and 'int' are types of the same size, the
-       difference of two 'unsigned char' values - including the sign bit -
-       doesn't fit in an 'int'.  */
-    return (c > d ? 1 : c < d ? -1 : 0);
-}
-
-/**
- * virMacAddrParse:
- * @str: string representation of MAC address, e.g., "0:1E:FC:E:3a:CB"
- * @addr: 6-byte MAC address
- *
- * Parse a MAC address
- *
- * Return 0 upon success, or -1 in case of error.
- */
-int
-virMacAddrParse(const char* str, unsigned char *addr)
-{
-    int i;
-
-    errno = 0;
-    for (i = 0; i < VIR_MAC_BUFLEN; i++) {
-        char *end_ptr;
-        unsigned long result;
-
-        /* This is solely to avoid accepting the leading
-         * space or "+" that strtoul would otherwise accept.
-         */
-        if (!c_isxdigit(*str))
-            break;
-
-        result = strtoul(str, &end_ptr, 16);
-
-        if ((end_ptr - str) < 1 || 2 < (end_ptr - str) ||
-            (errno != 0) ||
-            (0xFF < result))
-            break;
-
-        addr[i] = (unsigned char) result;
-
-        if ((i == 5) && (*end_ptr == '\0'))
-            return 0;
-        if (*end_ptr != ':')
-            break;
-
-        str = end_ptr + 1;
-    }
-
-    return -1;
-}
-
-void virMacAddrFormat(const unsigned char *addr,
-                      char *str)
-{
-    snprintf(str, VIR_MAC_STRING_BUFLEN,
-             "%02X:%02X:%02X:%02X:%02X:%02X",
-             addr[0], addr[1], addr[2],
-             addr[3], addr[4], addr[5]);
-    str[VIR_MAC_STRING_BUFLEN-1] = '\0';
-}
-
-void virMacAddrGenerate(const unsigned char *prefix,
-                        unsigned char *addr)
-{
-    addr[0] = prefix[0];
-    addr[1] = prefix[1];
-    addr[2] = prefix[2];
-    addr[3] = virRandomBits(8);
-    addr[4] = virRandomBits(8);
-    addr[5] = virRandomBits(8);
-}
-
-
 int virEnumFromString(const char *const*types,
                       unsigned int ntypes,
                       const char *type)
diff --git a/src/util/util.h b/src/util/util.h
index 3b31182..96491e5 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -157,8 +157,6 @@ int virStrToDouble(char const *s,
 
 int virHexToBin(unsigned char c);
 
-int virMacAddrCompare (const char *mac1, const char *mac2);
-
 void virSkipSpaces(const char **str) ATTRIBUTE_NONNULL(1);
 void virSkipSpacesAndBackslash(const char **str) ATTRIBUTE_NONNULL(1);
 void virTrimSpaces(char *str, char **endp) ATTRIBUTE_NONNULL(1);
@@ -178,17 +176,6 @@ char *virStrcpy(char *dest, const char *src, size_t destbytes)
     ATTRIBUTE_RETURN_CHECK;
 # define virStrcpyStatic(dest, src) virStrcpy((dest), (src), sizeof(dest))
 
-# define VIR_MAC_BUFLEN 6
-# define VIR_MAC_PREFIX_BUFLEN 3
-# define VIR_MAC_STRING_BUFLEN VIR_MAC_BUFLEN * 3
-
-int virMacAddrParse(const char* str,
-                    unsigned char *addr) ATTRIBUTE_RETURN_CHECK;
-void virMacAddrFormat(const unsigned char *addr,
-                      char *str);
-void virMacAddrGenerate(const unsigned char *prefix,
-                        unsigned char *addr);
-
 int virDiskNameToIndex(const char* str);
 char *virIndexToDiskName(int idx, const char *prefix);
 
diff --git a/src/util/virmacaddr.c b/src/util/virmacaddr.c
new file mode 100644
index 0000000..2a11e78
--- /dev/null
+++ b/src/util/virmacaddr.c
@@ -0,0 +1,128 @@
+/*
+ * virmacaddr.c: MAC address handling
+ *
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Authors:
+ *     Daniel P. Berrange <berrange redhat com>
+ */
+
+#include <config.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "c-ctype.h"
+#include "virmacaddr.h"
+#include "virrandom.h"
+
+/* Compare two MAC addresses, ignoring differences in case,
+ * as well as leading zeros.
+ */
+int
+virMacAddrCompare (const char *p, const char *q)
+{
+    unsigned char c, d;
+    do {
+        while (*p == '0' && c_isxdigit (p[1]))
+            ++p;
+        while (*q == '0' && c_isxdigit (q[1]))
+            ++q;
+        c = c_tolower (*p);
+        d = c_tolower (*q);
+
+        if (c == 0 || d == 0)
+            break;
+
+        ++p;
+        ++q;
+    } while (c == d);
+
+    if (UCHAR_MAX <= INT_MAX)
+        return c - d;
+
+    /* On machines where 'char' and 'int' are types of the same size, the
+       difference of two 'unsigned char' values - including the sign bit -
+       doesn't fit in an 'int'.  */
+    return (c > d ? 1 : c < d ? -1 : 0);
+}
+
+/**
+ * virMacAddrParse:
+ * @str: string representation of MAC address, e.g., "0:1E:FC:E:3a:CB"
+ * @addr: 6-byte MAC address
+ *
+ * Parse a MAC address
+ *
+ * Return 0 upon success, or -1 in case of error.
+ */
+int
+virMacAddrParse(const char* str, unsigned char *addr)
+{
+    int i;
+
+    errno = 0;
+    for (i = 0; i < VIR_MAC_BUFLEN; i++) {
+        char *end_ptr;
+        unsigned long result;
+
+        /* This is solely to avoid accepting the leading
+         * space or "+" that strtoul would otherwise accept.
+         */
+        if (!c_isxdigit(*str))
+            break;
+
+        result = strtoul(str, &end_ptr, 16);
+
+        if ((end_ptr - str) < 1 || 2 < (end_ptr - str) ||
+            (errno != 0) ||
+            (0xFF < result))
+            break;
+
+        addr[i] = (unsigned char) result;
+
+        if ((i == 5) && (*end_ptr == '\0'))
+            return 0;
+        if (*end_ptr != ':')
+            break;
+
+        str = end_ptr + 1;
+    }
+
+    return -1;
+}
+
+void virMacAddrFormat(const unsigned char *addr,
+                      char *str)
+{
+    snprintf(str, VIR_MAC_STRING_BUFLEN,
+             "%02X:%02X:%02X:%02X:%02X:%02X",
+             addr[0], addr[1], addr[2],
+             addr[3], addr[4], addr[5]);
+    str[VIR_MAC_STRING_BUFLEN-1] = '\0';
+}
+
+void virMacAddrGenerate(const unsigned char *prefix,
+                        unsigned char *addr)
+{
+    addr[0] = prefix[0];
+    addr[1] = prefix[1];
+    addr[2] = prefix[2];
+    addr[3] = virRandomBits(8);
+    addr[4] = virRandomBits(8);
+    addr[5] = virRandomBits(8);
+}
diff --git a/src/util/virmacaddr.h b/src/util/virmacaddr.h
new file mode 100644
index 0000000..c14b5a0
--- /dev/null
+++ b/src/util/virmacaddr.h
@@ -0,0 +1,41 @@
+/*
+ * virmacaddr.h: MAC address handling
+ *
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Authors:
+ *     Daniel P. Berrange <berrange redhat com>
+ */
+
+#ifndef __VIR_MACADDR_H__
+# define __VIR_MACADDR_H__
+
+# include "internal.h"
+
+# define VIR_MAC_BUFLEN 6
+# define VIR_MAC_PREFIX_BUFLEN 3
+# define VIR_MAC_STRING_BUFLEN VIR_MAC_BUFLEN * 3
+
+int virMacAddrCompare(const char *mac1, const char *mac2);
+void virMacAddrFormat(const unsigned char *addr,
+                      char *str);
+void virMacAddrGenerate(const unsigned char *prefix,
+                        unsigned char *addr);
+int virMacAddrParse(const char* str,
+                    unsigned char *addr) ATTRIBUTE_RETURN_CHECK;
+
+#endif /* __VIR_MACADDR_H__ */
diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index eafb47b..9d76d47 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -23,6 +23,7 @@
 #include <config.h>
 
 #include "virnetdev.h"
+#include "virmacaddr.h"
 #include "virfile.h"
 #include "virterror_internal.h"
 #include "command.h"
diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c
index 5e55b72..1d64b73 100644
--- a/src/util/virnetdevmacvlan.c
+++ b/src/util/virnetdevmacvlan.c
@@ -27,8 +27,8 @@
 
 #include <config.h>
 
-
 #include "virnetdevmacvlan.h"
+#include "virmacaddr.h"
 #include "util.h"
 #include "virterror_internal.h"
 
-- 
1.7.7.6


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]