From 2e5d35966ec7d514466ccc91a62ffb3dcc3aeb9c Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Mon, 20 Nov 2006 16:42:16 +0000 Subject: [PATCH] * src/xml.c src/xs_internal.c src/xs_internal.h: cleanup for the dynamic device code to isolate as a separate function xenStoreDomainGetNetworkID() the access to the XenStore Daniel --- ChangeLog | 6 ++++++ src/xml.c | 40 ++++++++++---------------------------- src/xs_internal.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++ src/xs_internal.h | 3 +++ 4 files changed, 68 insertions(+), 30 deletions(-) diff --git a/ChangeLog b/ChangeLog index 404760c4ac..9486b9089b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Mon Nov 20 16:51:43 CET 2006 Daniel Veillard + + * src/xml.c src/xs_internal.c src/xs_internal.h: cleanup for the + dynamic device code to isolate as a separate function + xenStoreDomainGetNetworkID() the access to the XenStore + Thu Nov 16 19:36:12 EST 2006 Daniel Berrange * src/xm_internal.c: Add support for device_model parameter to diff --git a/src/xml.c b/src/xml.c index 06273b545b..841f500cb1 100644 --- a/src/xml.c +++ b/src/xml.c @@ -23,6 +23,7 @@ #include "hash.h" #include "sexpr.h" #include "xml.h" +#include "xs_internal.h" /* for xenStoreDomainGetNetworkID */ static void virXMLError(virErrorNumber error, const char *info, int value) @@ -1561,9 +1562,8 @@ virDomainXMLDevID(virDomainPtr domain, char *xmldesc, char *class, char *ref) xmlDocPtr xml = NULL; xmlNodePtr node, cur; xmlChar *attr = NULL; - char dir[80], path[128], **list = NULL, *mac = NULL; + char *xref; int ret = 0; - unsigned int num, i, len; xml = xmlReadDoc((const xmlChar *) xmldesc, "domain.xml", NULL, XML_PARSE_NOENT | XML_PARSE_NONET | @@ -1594,30 +1594,14 @@ virDomainXMLDevID(virDomainPtr domain, char *xmldesc, char *class, char *ref) if (attr == NULL) goto error; -/* - * TODO: this part need to be isolated as a high level routine in - * xs_internal.[ch] - */ - sprintf(dir, "/local/domain/0/backend/vif/%d", domain->handle); - list = xs_directory(domain->conn->xshandle, 0, dir, &num); - if (list == NULL) - goto error; - for (i = 0; i < num; i++) { - sprintf(path, "%s/%s/%s", dir, list[i], "mac"); - mac = xs_read(domain->conn->xshandle, 0, path, &len); - if (mac == NULL) - goto error; - if ((strlen((char*)attr) != len) || memcmp(attr, mac, len)) { - free(mac); - mac = NULL; - continue; - } - strcpy(ref, list[i]); - goto cleanup; - } -/* - * end of TODO block - */ + xref = xenStoreDomainGetNetworkID(domain->conn, domain->handle, + (char *) attr); + if (xref != NULL) { + strcpy(ref, xref); + free(xref); + goto cleanup; + } + goto error; } } @@ -1628,10 +1612,6 @@ cleanup: xmlFreeDoc(xml); if (attr != NULL) xmlFree(attr); - if (list != NULL) - free(list); - if (mac != NULL) - free(mac); return ret; } #endif /* !PROXY */ diff --git a/src/xs_internal.c b/src/xs_internal.c index 6ad145e489..0bd4dfaacd 100644 --- a/src/xs_internal.c +++ b/src/xs_internal.c @@ -784,3 +784,52 @@ xenStoreDomainGetOSTypeID(virConnectPtr conn, int id) { return (str); } #endif /* PROXY */ + +/* + * xenStoreDomainGetNetworkID: + * @conn: pointer to the connection. + * @id: the domain id + * @mac: the mac address + * + * Get the reference (i.e. the string number) for the device on that domain + * which uses the given mac address + * + * Returns the new string or NULL in case of error, the string must be + * freed by the caller. + */ +char * +xenStoreDomainGetNetworkID(virConnectPtr conn, int id, const char *mac) { + char dir[80], path[128], **list = NULL, *val = NULL; + unsigned int maclen, len, i, num; + char *ret = NULL; + + if (id < 0) + return(NULL); + if (conn->xshandle == NULL) + return (NULL); + if (mac == NULL) + return (NULL); + maclen = strlen(mac); + if (maclen <= 0) + return (NULL); + + sprintf(dir, "/local/domain/0/backend/vif/%d", id); + list = xs_directory(conn->xshandle, 0, dir, &num); + if (list == NULL) + return(NULL); + for (i = 0; i < num; i++) { + sprintf(path, "%s/%s/%s", dir, list[i], "mac"); + val = xs_read(conn->xshandle, 0, path, &len); + if (val == NULL) + break; + if ((maclen != len) || memcmp(val, mac, len)) { + free(val); + } else { + ret = list[i]; + free(val); + break; + } + } + free(list); + return(ret); +} diff --git a/src/xs_internal.h b/src/xs_internal.h index 06e0233880..fb6de75868 100644 --- a/src/xs_internal.h +++ b/src/xs_internal.h @@ -43,6 +43,9 @@ char * xenStoreDomainGetConsolePath(virConnectPtr conn, int domid); char * xenStoreDomainGetOSTypeID(virConnectPtr conn, int id); +char * xenStoreDomainGetNetworkID(virConnectPtr conn, + int id, + const char *mac); #ifdef __cplusplus }