[netcf-devel] [PATCH 1/2] Add interface lookup routine normalize_if
Jonas Eriksson
jonas.j.eriksson at ericsson.com
Wed Aug 5 07:28:52 UTC 2009
dutil.c: Added normalize_if, which implements the interface lookup
logic when we have a name and a mac and want to make sure that it
corresponds to the correct interface.
---
src/dutil.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
src/dutil.h | 11 +++++++++++
2 files changed, 64 insertions(+), 0 deletions(-)
diff --git a/src/dutil.c b/src/dutil.c
index 28832b3..5f6bbe3 100644
--- a/src/dutil.c
+++ b/src/dutil.c
@@ -41,6 +41,7 @@
#include "list.h"
#include "netcf.h"
#include "dutil.h"
+#include "dutil_linux.h"
#include <libxml/parser.h>
#include <libxml/relaxng.h>
@@ -401,6 +402,58 @@ void run1(struct netcf *ncf, const char *prog, const char *arg) {
run_program(ncf, argv);
}
+char *normalize_if(struct netcf *ncf, const char *name,
+ const char *mac) {
+ char *nname = NULL;
+ int nmatches = 0;
+ char **matches = NULL;
+
+ /* Check that we have any information */
+ ERR_COND_BAIL(name == NULL && mac == NULL, ncf, EOTHER);
+
+ if (mac != NULL) {
+ /* Find all interfaces with the given mac address */
+ nmatches = aug_match_mac(ncf, mac, &matches);
+ ERR_BAIL(ncf);
+
+ /* Check for a matching name */
+ if (name != NULL) {
+ for (int i = 0; i < nmatches; i++) {
+ if (STREQ(name, matches[i])) {
+ nname = strdup(name);
+ goto done;
+ }
+ }
+ }
+
+ /* Check for a name that is sane */
+ for (int i = 0; i < nmatches; i++) {
+ /* Check that the interface begins with eth and does not
+ * contain a . ( => VLAN if) */
+ if (strstr(matches[i], "eth") == matches[i] &&
+ strchr(matches[i], '.') == NULL) {
+ nname = strdup(matches[i]);
+ goto done;
+ }
+ }
+
+ /* No match and no name => fail */
+ ERR_THROW(name == NULL, ncf, EOTHER,
+ "no toplevel interface has MAC address '%s'", name);
+ }
+
+ nname = strdup(name);
+
+ done:
+ free_matches(nmatches, &matches);
+ return nname;
+
+ error:
+ free_matches(nmatches, &matches);
+ FREE(nname);
+ return NULL;
+}
+
/*
* Local variables:
* indent-tabs-mode: nil
diff --git a/src/dutil.h b/src/dutil.h
index 6dc05e2..f66ee9e 100644
--- a/src/dutil.h
+++ b/src/dutil.h
@@ -100,6 +100,17 @@ int dutil_put_aug(struct netcf *ncf, const char *aug_xml, char **ncf_xml);
/* Run the program PROG with the single argument ARG */
void run1(struct netcf *ncf, const char *prog, const char *arg);
+/* Returns the most matching interface in this order:
+ * 1. The interface with the same name and mac
+ * 2. The ethernet interface with the same mac
+ * 3. The name supplied as NAME
+ * The result must be FREEd
+ * Both NAME and MAC are allowed to be NULL, but if the function is unable to
+ * find a interface name, NULL is returned and EOTHER is raised.
+ */
+char *normalize_if(struct netcf *ncf, const char *name,
+ const char *mac);
+
#endif
/*
--
1.6.2
More information about the netcf-devel
mailing list