Re: [PATCH] comparing MAC address is changed to case insensitive

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

 



Hi Rich.

Thank you for your advice.

On 2008/04/11 20:42, Richard W.M. Jones wrote:
> You also have to note that 1:2:3:4:5:6 is the same as
> 01:02:03:04:05:06.  Comparing MAC addresses is hard :-)

I remake the patch.
This patch compares two MAC addresses ignoring leading zeros.

Signed-off-by: Hiroyuki Kaguchi <fj7025cf@xxxxxxxxxxxxxxxxx>
diff -r 6462dfd3e606 -r 6ec83c2a5c36 virtinst/Guest.py
--- a/virtinst/Guest.py	Tue Apr 08 15:35:49 2008 -0400
+++ b/virtinst/Guest.py	Mon Apr 21 15:44:24 2008 +0900
@@ -368,13 +368,10 @@
                 continue
             ctx = doc.xpathNewContext()
             try:
-                try:
-                    count += ctx.xpathEval("count(/domain/devices/interface/mac[@address='%s'])"
-                                           % self.macaddr.upper())
-                    count += ctx.xpathEval("count(/domain/devices/interface/mac[@address='%s'])"
-                                           % self.macaddr.lower())
-                except:
-                    continue
+                for mac in ctx.xpathEval("/domain/devices/interface/mac"):
+                    macaddr = mac.xpathEval("attribute::address")[0].content
+                    if util.compareMAC(self.macaddr, macaddr) == 0:
+                        count += 1
             finally:
                 if ctx is not None:
                     ctx.xpathFreeContext()
diff -r 6462dfd3e606 -r 6ec83c2a5c36 virtinst/util.py
--- a/virtinst/util.py	Tue Apr 08 15:35:49 2008 -0400
+++ b/virtinst/util.py	Mon Apr 21 15:44:24 2008 +0900
@@ -222,3 +222,22 @@
     str = str.replace("<", "&lt;")
     str = str.replace(">", "&gt;")
     return str
+
+def compareMAC(p, q):
+    """Compare two MAC addresses"""
+    pa = p.split(":")
+    qa = q.split(":")
+
+    if len(pa) != len(qa):
+        if p > q:
+            return 1
+        else:
+            return -1
+
+    for i in xrange(len(pa)):
+        n = int(pa[i], 0x10) - int(qa[i], 0x10)
+        if n > 0:
+            return 1
+        elif n < 0:
+            return -1
+    return 0
_______________________________________________
et-mgmt-tools mailing list
et-mgmt-tools@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/et-mgmt-tools

[Index of Archives]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux