[netcf-devel] [PATCH] Make lookup-by-MAC case-insensitive
David Lutterkort
lutter at redhat.com
Wed Jul 29 10:11:54 UTC 2009
The /sys file system stores MAC's in all-lowercase; we therefore have to
translate MAC's to lowercase before looking up.
Fixes BZ 512955
* bootstrap: use c-ctype module from gnulib
* src/dutil-linux.c (aug_match_mac): use tolower'd MAC for lookup
* tests/test-initscripts.c: test correct lookup of mixed-case MACs
---
bootstrap | 1 +
src/dutil_linux.c | 14 +++++++++++---
tests/test-initscripts.c | 10 ++++++++++
3 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/bootstrap b/bootstrap
index 91225fb..f0653ef 100755
--- a/bootstrap
+++ b/bootstrap
@@ -58,6 +58,7 @@ gnulib_tool=$GNULIB_SRCDIR/gnulib-tool
<$gnulib_tool || exit
modules='
+c-ctype
read-file
safe-alloc
warnings
diff --git a/src/dutil_linux.c b/src/dutil_linux.c
index cd87d3b..1878719 100644
--- a/src/dutil_linux.c
+++ b/src/dutil_linux.c
@@ -31,6 +31,7 @@
#include <string.h>
#include <unistd.h>
#include <ctype.h>
+#include <c-ctype.h>
#include "safe-alloc.h"
#include "ref.h"
@@ -42,11 +43,17 @@
/* Returns a list of all interfaces with MAC address INTF */
int aug_match_mac(struct netcf *ncf, const char *mac, char ***matches) {
int r, nmatches;
- char *path;
+ char *path, *mac_lower;
struct augeas *aug = get_augeas(ncf);
+ mac_lower = strdup(mac);
+ ERR_COND_BAIL(mac_lower == NULL, ncf, ENOMEM);
+ for (char *s = mac_lower; *s != '\0'; s++)
+ *s = c_tolower(*s);
+
r = xasprintf(&path,
- "/files/sys/class/net/*[address/content = '%s']", mac);
+ "/files/sys/class/net/*[address/content = '%s']", mac_lower);
+ FREE(mac_lower);
ERR_COND_BAIL(r < 0, ncf, ENOMEM);
r = aug_match(aug, path, matches);
@@ -72,8 +79,9 @@ int aug_match_mac(struct netcf *ncf, const char *mac, char ***matches) {
return nmatches;
error:
+ FREE(mac_lower);
FREE(path);
- return r;
+ return -1;
}
/* Get the MAC address of the interface INTF */
diff --git a/tests/test-initscripts.c b/tests/test-initscripts.c
index 7f62c0b..253aef4 100644
--- a/tests/test-initscripts.c
+++ b/tests/test-initscripts.c
@@ -251,12 +251,14 @@ static void testLookupByName(CuTest *tc) {
static void testLookupByMAC(CuTest *tc) {
static const char *const good_mac = "aa:bb:cc:dd:ee:ff";
+ static const char *const good_mac_caps = "AA:bb:cc:DD:Ee:ff";
struct netcf_if *nif;
int r;
r = ncf_lookup_by_mac_string(ncf, "00:00:00:00:00:00", 1, &nif);
CuAssertIntEquals(tc, 0, r);
CuAssertPtrEquals(tc, NULL, nif);
+
r = ncf_lookup_by_mac_string(ncf, good_mac, 1, &nif);
CuAssertIntEquals(tc, 1, r);
CuAssertPtrNotNull(tc, nif);
@@ -264,6 +266,14 @@ static void testLookupByMAC(CuTest *tc) {
CuAssertStrEquals(tc, good_mac, ncf_if_mac_string(nif));
ncf_if_free(nif);
CuAssertIntEquals(tc, 1, ncf->ref);
+
+ r = ncf_lookup_by_mac_string(ncf, good_mac_caps, 1, &nif);
+ CuAssertIntEquals(tc, 1, r);
+ CuAssertPtrNotNull(tc, nif);
+ CuAssertStrEquals(tc, "br0", nif->name);
+ CuAssertStrEquals(tc, good_mac, ncf_if_mac_string(nif));
+ ncf_if_free(nif);
+ CuAssertIntEquals(tc, 1, ncf->ref);
}
static void testDefineUndefine(CuTest *tc) {
--
1.6.0.6
More information about the netcf-devel
mailing list