UUID buffer lengths [was Re: Virtual networking]

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

 



On Tue, 2007-01-23 at 11:20 +0100, Karel Zak wrote:

> +    char uuid[37];
> 
>   Magic number? :-)
> 
>   #define UUID_STRLEN	36
> 
>    char uuid[UUID_STRLEN+1];

	Good point. Here's a proposed API addition to put the buffer lengths as
macros in libvirt.h.

	Anyone got objections to that?

Cheers,
Mark.

Index: libvirt/include/libvirt/libvirt.h.in
===================================================================
--- libvirt.orig/include/libvirt/libvirt.h.in
+++ libvirt/include/libvirt/libvirt.h.in
@@ -187,6 +187,24 @@ struct _virNodeInfo {
 
 typedef virNodeInfo *virNodeInfoPtr;
 
+/**
+ * VIR_UUID_STRING_BUFLEN:
+ *
+ * This macro provides the length of the buffer required
+ * for virDomainGetUUID()
+ */
+
+#define VIR_UUID_BUFLEN (16)
+
+/**
+ * VIR_UUID_STRING_BUFLEN:
+ *
+ * This macro provides the length of the buffer required
+ * for virDomainGetUUIDString()
+ */
+
+#define VIR_UUID_STRING_BUFLEN (36+1)
+
 /* library versionning */
 
 /**
Index: libvirt/proxy/libvirt_proxy.c
===================================================================
--- libvirt.orig/proxy/libvirt_proxy.c
+++ libvirt/proxy/libvirt_proxy.c
@@ -462,7 +462,7 @@ retry2:
 	    break;
 	case VIR_PROXY_LOOKUP_ID: {
 	    char *name = NULL;
-	    unsigned char uuid[16];
+	    unsigned char uuid[VIR_UUID_BUFLEN];
 	    int len;
 
 	    if (req->len != sizeof(virProxyPacket))
@@ -476,9 +476,9 @@ retry2:
 		    len = 1000;
 		    name[1000] = 0;
 		}
-	        req->len += 16 + len + 1;
-		memcpy(&request.extra.str[0], uuid, 16);
-		strcpy(&request.extra.str[16], name);
+	        req->len += VIR_UUID_BUFLEN + len + 1;
+		memcpy(&request.extra.str[0], uuid, VIR_UUID_BUFLEN);
+		strcpy(&request.extra.str[VIR_UUID_BUFLEN], name);
 	    }
 	    if (name)
 	        free(name);
@@ -489,9 +489,9 @@ retry2:
 	    char **tmp;
 	    int ident, len;
 	    char *name = NULL;
-	    unsigned char uuid[16];
+	    unsigned char uuid[VIR_UUID_BUFLEN];
 
-	    if (req->len != sizeof(virProxyPacket) + 16)
+	    if (req->len != sizeof(virProxyPacket) + VIR_UUID_BUFLEN)
 	        goto comm_error;
 
 	    /*
@@ -504,7 +504,7 @@ retry2:
 	    if (names != NULL) {
 	       while (*tmp != NULL) {
 		  ident = xenDaemonDomainLookupByName_ids(conn, *tmp, &uuid[0]);
-		  if (!memcmp(uuid, &request.extra.str[0], 16)) {
+		  if (!memcmp(uuid, &request.extra.str[0], VIR_UUID_BUFLEN)) {
 		     name = *tmp;
 		     break;
 		  }
@@ -530,7 +530,7 @@ retry2:
 	}
 	case VIR_PROXY_LOOKUP_NAME: {
 	    int ident;
-	    unsigned char uuid[16];
+	    unsigned char uuid[VIR_UUID_BUFLEN];
 
 	    if (req->len > sizeof(virProxyPacket) + 1000)
 	        goto comm_error;
@@ -542,8 +542,8 @@ retry2:
                 req->data.arg = -1;
 		req->len = sizeof(virProxyPacket);
 	    } else {
-	        req->len = sizeof(virProxyPacket) + 16;
-		memcpy(&request.extra.str[0], uuid, 16);
+	        req->len = sizeof(virProxyPacket) + VIR_UUID_BUFLEN;
+		memcpy(&request.extra.str[0], uuid, VIR_UUID_BUFLEN);
 		req->data.arg = ident;
 	    }
 	    break;
Index: libvirt/src/hash.c
===================================================================
--- libvirt.orig/src/hash.c
+++ libvirt/src/hash.c
@@ -759,7 +759,7 @@ virGetDomain(virConnectPtr conn, const c
     ret->conn = conn;
     ret->id = -1;
     if (uuid != NULL)
-        memcpy(&(ret->uuid[0]), uuid, 16);
+        memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);
 
     if (virHashAddEntry(conn->domains, name, ret) < 0) {
         virHashError(conn, VIR_ERR_INTERNAL_ERROR,
Index: libvirt/src/internal.h
===================================================================
--- libvirt.orig/src/internal.h
+++ libvirt/src/internal.h
@@ -145,15 +145,15 @@ enum {
 * Internal structure associated to a domain
 */
 struct _virDomain {
-    unsigned int magic;     /* specific value to check */
-    int uses;               /* reference count */
-    virConnectPtr conn;     /* pointer back to the connection */
-    char *name;             /* the domain external name */
-    char *path;             /* the domain internal path */
-    int id;                 /* the domain ID */
-    int flags;              /* extra flags */
-    unsigned char uuid[16]; /* the domain unique identifier */
-    char *xml;              /* the XML description for defined domains */
+    unsigned int magic;                  /* specific value to check */
+    int uses;                            /* reference count */
+    virConnectPtr conn;                  /* pointer back to the connection */
+    char *name;                          /* the domain external name */
+    char *path;                          /* the domain internal path */
+    int id;                              /* the domain ID */
+    int flags;                           /* extra flags */
+    unsigned char uuid[VIR_UUID_BUFLEN]; /* the domain unique identifier */
+    char *xml;                           /* the XML description for defined domains */
 };
 
 /*
Index: libvirt/src/libvirt.c
===================================================================
--- libvirt.orig/src/libvirt.c
+++ libvirt/src/libvirt.c
@@ -645,8 +645,8 @@ virDomainLookupByUUID(virConnectPtr conn
 virDomainPtr
 virDomainLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
 {
-    int raw[16], i;
-    unsigned char uuid[16];
+    int raw[VIR_UUID_BUFLEN], i;
+    unsigned char uuid[VIR_UUID_BUFLEN];
     int ret;
 
     if (!VIR_IS_CONNECT(conn)) {
@@ -672,11 +672,11 @@ virDomainLookupByUUIDString(virConnectPt
                  raw + 8, raw + 9, raw + 10, raw + 11,
                  raw + 12, raw + 13, raw + 14, raw + 15);
     
-    if (ret!=16) {
+    if (ret!=VIR_UUID_BUFLEN) {
 	virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
 	return (NULL);
     }
-    for (i = 0; i < 16; i++)
+    for (i = 0; i < VIR_UUID_BUFLEN; i++)
         uuid[i] = raw[i] & 0xFF;
     
     return virDomainLookupByUUID(conn, &uuid[0]);
@@ -1205,7 +1205,7 @@ virDomainGetName(virDomainPtr domain)
 /**
  * virDomainGetUUID:
  * @domain: a domain object
- * @uuid: pointer to a 16 bytes array
+ * @uuid: pointer to a VIR_UUID_BUFLEN bytes array
  *
  * Get the UUID for a domain
  *
@@ -1224,7 +1224,7 @@ virDomainGetUUID(virDomainPtr domain, un
     }
 
     if (domain->id == 0) {
-        memset(uuid, 0, 16);
+        memset(uuid, 0, VIR_UUID_BUFLEN);
     } else {
         if ((domain->uuid[0] == 0) && (domain->uuid[1] == 0) &&
             (domain->uuid[2] == 0) && (domain->uuid[3] == 0) &&
@@ -1236,7 +1236,7 @@ virDomainGetUUID(virDomainPtr domain, un
             (domain->uuid[14] == 0) && (domain->uuid[15] == 0))
             xenDaemonDomainLookupByName_ids(domain->conn, domain->name,
                                 &domain->uuid[0]);
-        memcpy(uuid, &domain->uuid[0], 16);
+        memcpy(uuid, &domain->uuid[0], VIR_UUID_BUFLEN);
     }
     return (0);
 }
@@ -1244,7 +1244,7 @@ virDomainGetUUID(virDomainPtr domain, un
 /**
  * virDomainGetUUIDString:
  * @domain: a domain object
- * @buf: pointer to a 37 bytes array
+ * @buf: pointer to a VIR_UUID_STRING_BUFLEN bytes array
  *
  * Get the UUID for a domain as string. For more information about 
  * UUID see RFC4122.
@@ -1254,7 +1254,7 @@ virDomainGetUUID(virDomainPtr domain, un
 int
 virDomainGetUUIDString(virDomainPtr domain, char *buf)
 {
-    unsigned char uuid[16];
+    unsigned char uuid[VIR_UUID_BUFLEN];
 
     if (!VIR_IS_DOMAIN(domain)) {
         virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
@@ -1268,7 +1268,7 @@ virDomainGetUUIDString(virDomainPtr doma
     if (virDomainGetUUID(domain, &uuid[0]))
 	return (-1);
 
-    snprintf(buf, 37, 
+    snprintf(buf, VIR_UUID_STRING_BUFLEN,
 	"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
                       uuid[0], uuid[1], uuid[2], uuid[3],
                       uuid[4], uuid[5], uuid[6], uuid[7],
Index: libvirt/src/proxy_internal.c
===================================================================
--- libvirt.orig/src/proxy_internal.c
+++ libvirt/src/proxy_internal.c
@@ -761,7 +761,7 @@ xenProxyLookupByID(virConnectPtr conn, i
 {
     virProxyPacket req;
     virProxyFullPacket ans;
-    unsigned char uuid[16];
+    unsigned char uuid[VIR_UUID_BUFLEN];
     const char *name;
     int ret;
     virDomainPtr res;
@@ -786,8 +786,8 @@ xenProxyLookupByID(virConnectPtr conn, i
     if (ans.data.arg == -1) {
 	return(NULL);
     }
-    memcpy(uuid, &ans.extra.str[0], 16);
-    name = &ans.extra.str[16];
+    memcpy(uuid, &ans.extra.str[0], VIR_UUID_BUFLEN);
+    name = &ans.extra.str[VIR_UUID_BUFLEN];
     res = virGetDomain(conn, name, uuid);
 
     if (res == NULL)
@@ -825,7 +825,7 @@ xenProxyLookupByUUID(virConnectPtr conn,
     }
     memset(&req, 0, sizeof(virProxyPacket));
     req.command = VIR_PROXY_LOOKUP_UUID;
-    req.len = sizeof(virProxyPacket) + 16;
+    req.len = sizeof(virProxyPacket) + VIR_UUID_BUFLEN;
     ret = xenProxyCommand(conn, (virProxyPacketPtr) &req, &req, 0);
     if (ret < 0) {
         xenProxyClose(conn);
Index: libvirt/src/test.c
===================================================================
--- libvirt.orig/src/test.c
+++ libvirt/src/test.c
@@ -139,7 +139,7 @@ typedef struct _testDom {
     int active;
     int id;
     char name[20];
-    unsigned char uuid[16];
+    unsigned char uuid[VIR_UUID_BUFLEN];
     virDomainKernel kernel;
     virDomainInfo info;
     unsigned int maxVCPUs;
@@ -247,7 +247,7 @@ static int testLoadDomain(virConnectPtr 
     xmlXPathContextPtr ctxt = NULL;
     xmlXPathObjectPtr obj = NULL;
     char *name = NULL;
-    unsigned char rawuuid[16];
+    unsigned char rawuuid[VIR_UUID_BUFLEN];
     char *dst_uuid;
     testCon *con;
     struct timeval tv;
@@ -397,7 +397,7 @@ static int testLoadDomain(virConnectPtr 
     if (memory > maxMem)
         memory = maxMem;
 
-    memmove(con->domains[handle].uuid, rawuuid, 16);
+    memmove(con->domains[handle].uuid, rawuuid, VIR_UUID_BUFLEN);
     con->domains[handle].info.maxMem = maxMem;
     con->domains[handle].info.memory = memory;
     con->domains[handle].info.state = domid < 0 ? VIR_DOMAIN_SHUTOFF : VIR_DOMAIN_RUNNING;
@@ -487,7 +487,7 @@ static int testOpenDefault(virConnectPtr
     node->connections[connid].domains[0].onCrash = VIR_DOMAIN_RESTART;
     node->connections[connid].domains[0].onPoweroff = VIR_DOMAIN_DESTROY;
     strcpy(node->connections[connid].domains[0].name, "test");
-    for (u = 0 ; u < 16 ; u++) {
+    for (u = 0 ; u < VIR_UUID_BUFLEN ; u++) {
         node->connections[connid].domains[0].uuid[u] = (u * 75)%255;
     }
     node->connections[connid].domains[0].info.maxMem = 8192 * 1024;
@@ -901,7 +901,7 @@ virDomainPtr testLookupDomainByUUID(virC
     int i, idx = -1;
     for (i = 0 ; i < MAX_DOMAINS ; i++) {
         if (con->domains[i].active &&
-            memcmp(uuid, con->domains[i].uuid, 16) == 0) {
+            memcmp(uuid, con->domains[i].uuid, VIR_UUID_BUFLEN) == 0) {
             idx = i;
             break;
         }
Index: libvirt/src/virsh.c
===================================================================
--- libvirt.orig/src/virsh.c
+++ libvirt/src/virsh.c
@@ -1030,7 +1030,7 @@ cmdDominfo(vshControl * ctl, vshCmd * cm
     virDomainPtr dom;
     int ret = TRUE;
     unsigned int id;
-    char *str, uuid[37];
+    char *str, uuid[VIR_UUID_STRING_BUFLEN];
 
     if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
         return FALSE;
@@ -1535,7 +1535,7 @@ static int
 cmdDomuuid(vshControl * ctl, vshCmd * cmd)
 {
     virDomainPtr dom;
-    char uuid[37];
+    char uuid[VIR_UUID_STRING_BUFLEN];
 
     if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
         return FALSE;
Index: libvirt/src/xend_internal.c
===================================================================
--- libvirt.orig/src/xend_internal.c
+++ libvirt/src/xend_internal.c
@@ -1100,7 +1100,7 @@ xenDaemonDomainLookupByName_ids(virConne
     int ret = -1;
 
     if (uuid != NULL)
-        memset(uuid, 0, 16);
+        memset(uuid, 0, VIR_UUID_BUFLEN);
     root = sexpr_get(xend, "/xend/domain/%s?detail=1", domname);
     if (root == NULL)
         goto error;
@@ -1152,7 +1152,7 @@ xenDaemonDomainLookupByID(virConnectPtr 
     char *dst_uuid;
     struct sexpr *root;
 
-    memset(uuid, 0, 16);
+    memset(uuid, 0, VIR_UUID_BUFLEN);
 
     root = sexpr_get(xend, "/xend/domain/%d?detail=1", id);
     if (root == NULL)
@@ -1939,7 +1939,7 @@ sexpr_to_domain(virConnectPtr conn, stru
 {
     virDomainPtr ret = NULL;
     char *dst_uuid = NULL;
-    char uuid[16];
+    char uuid[VIR_UUID_BUFLEN];
     const char *name;
     const char *tmp;
 
@@ -2728,7 +2728,7 @@ error:
 static virDomainPtr
 xenDaemonLookupByID(virConnectPtr conn, int id) {
     char *name = NULL;
-    unsigned char uuid[16];
+    unsigned char uuid[VIR_UUID_BUFLEN];
     virDomainPtr ret;
 
     if (xenDaemonDomainLookupByID(conn, id, &name, uuid) < 0) {
@@ -2762,7 +2762,7 @@ xenDaemonLookupByID(virConnectPtr conn, 
 int
 xenDaemonDomainSetVcpus(virDomainPtr domain, unsigned int vcpus)
 {
-    char buf[16];
+    char buf[VIR_UUID_BUFLEN];
 
     if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)
      || (vcpus < 1)) {
@@ -2793,7 +2793,7 @@ int
 xenDaemonDomainPinVcpu(virDomainPtr domain, unsigned int vcpu,
                      unsigned char *cpumap, int maplen)
 {
-    char buf[16], mapstr[sizeof(cpumap_t) * 64] = "[";
+    char buf[VIR_UUID_BUFLEN], mapstr[sizeof(cpumap_t) * 64] = "[";
     int i, j;
 
     if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)
@@ -2929,7 +2929,7 @@ xenDaemonLookupByUUID(virConnectPtr conn
     char *name = NULL;
     char **names;
     char **tmp;
-    unsigned char ident[16];
+    unsigned char ident[VIR_UUID_BUFLEN];
     int id = -1;
 
     names = xenDaemonListDomainsOld(conn);
@@ -2942,7 +2942,7 @@ xenDaemonLookupByUUID(virConnectPtr conn
     while (*tmp != NULL) {
         id = xenDaemonDomainLookupByName_ids(conn, *tmp, &ident[0]);
         if (id >= 0) {
-            if (!memcmp(uuid, ident, 16)) {
+            if (!memcmp(uuid, ident, VIR_UUID_BUFLEN)) {
                 name = strdup(*tmp);
                 break;
             }
Index: libvirt/src/xm_internal.c
===================================================================
--- libvirt.orig/src/xm_internal.c
+++ libvirt/src/xm_internal.c
@@ -209,7 +209,7 @@ static int xenXMConfigGetUUID(virConfPtr
    have one in its config */
 static void xenXMConfigGenerateUUID(unsigned char *uuid) {
     int i;
-    for (i = 0 ; i < 16 ; i++) {
+    for (i = 0 ; i < VIR_UUID_BUFLEN ; i++) {
         uuid[i] = (unsigned char)(1 + (int) (256.0 * (rand() / (RAND_MAX + 1.0))));
     }
 }
@@ -217,7 +217,7 @@ static void xenXMConfigGenerateUUID(unsi
 /* Ensure that a config object has a valid UUID in it,
    if it doesn't then (re-)generate one */
 static int xenXMConfigEnsureIdentity(virConfPtr conf, const char *filename) {
-    unsigned char uuid[16];
+    unsigned char uuid[VIR_UUID_BUFLEN];
     const char *name;
 
     /* Had better have a name...*/
@@ -242,7 +242,7 @@ static int xenXMConfigEnsureIdentity(vir
     /* If there is no uuid...*/
     if (xenXMConfigGetUUID(conf, "uuid", uuid) < 0) {
         virConfValuePtr value;
-        char uuidstr[37];
+        char uuidstr[VIR_UUID_STRING_BUFLEN];
 
         value = malloc(sizeof(virConfValue));
         if (!value) {
@@ -251,7 +251,7 @@ static int xenXMConfigEnsureIdentity(vir
 
         /* ... then generate one */
         xenXMConfigGenerateUUID(uuid);
-        snprintf(uuidstr, 37,
+        snprintf(uuidstr, VIR_UUID_STRING_BUFLEN,
                  "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x</uuid>\n",
                  uuid[0], uuid[1], uuid[2], uuid[3],
                  uuid[4], uuid[5], uuid[6], uuid[7],
@@ -565,7 +565,7 @@ char *xenXMDomainFormatXML(virConnectPtr
     virBufferPtr buf;
     char *xml;
     const char *name;
-    unsigned char uuid[16];
+    unsigned char uuid[VIR_UUID_BUFLEN];
     const char *str;
     int hvm = 0;
     long val;
@@ -1168,7 +1168,7 @@ virDomainPtr xenXMDomainLookupByName(vir
     const char *filename;
     xenXMConfCachePtr entry;
     virDomainPtr ret;
-    unsigned char uuid[16];
+    unsigned char uuid[VIR_UUID_BUFLEN];
     if (!VIR_IS_CONNECT(conn)) {
         xenXMError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
         return (NULL);
@@ -1209,7 +1209,7 @@ virDomainPtr xenXMDomainLookupByName(vir
  * Hash table iterator to search for a domain based on UUID
  */
 static int xenXMDomainSearchForUUID(const void *payload, const char *name ATTRIBUTE_UNUSED, const void *data) {
-    unsigned char uuid[16];
+    unsigned char uuid[VIR_UUID_BUFLEN];
     const unsigned char *wantuuid = (const unsigned char *)data;
     const xenXMConfCachePtr entry = (const xenXMConfCachePtr)payload;
 
@@ -1217,7 +1217,7 @@ static int xenXMDomainSearchForUUID(cons
         return (0);
     }
 
-    if (!memcmp(uuid, wantuuid, 16))
+    if (!memcmp(uuid, wantuuid, VIR_UUID_BUFLEN))
         return (1);
 
     return (0);
@@ -1271,7 +1271,7 @@ int xenXMDomainCreate(virDomainPtr domai
     char *xml;
     char *sexpr;
     int ret;
-    unsigned char uuid[16];
+    unsigned char uuid[VIR_UUID_BUFLEN];
 
     if ((domain == NULL) || (domain->conn == NULL) || (domain->name == NULL)) {
         xenXMError((domain ? domain->conn : NULL), VIR_ERR_INVALID_ARG,
@@ -2046,7 +2046,7 @@ virConfPtr xenXMParseXMLToConfig(virConn
 virDomainPtr xenXMDomainDefineXML(virConnectPtr conn, const char *xml) {
     virDomainPtr ret;
     char filename[PATH_MAX];
-    unsigned char uuid[16];
+    unsigned char uuid[VIR_UUID_BUFLEN];
     virConfPtr conf = NULL;
     xenXMConfCachePtr entry = NULL;
     virConfValuePtr value;
Index: libvirt/src/xml.c
===================================================================
--- libvirt.orig/src/xml.c
+++ libvirt/src/xml.c
@@ -525,7 +525,7 @@ char *
 virDomainGetXMLDesc(virDomainPtr domain, int flags)
 {
     char *ret = NULL;
-    unsigned char uuid[16];
+    unsigned char uuid[VIR_UUID_BUFLEN];
     virBuffer buf;
     virDomainInfo info;
 
@@ -1533,7 +1533,7 @@ virDomainParseXMLDesc(const char *xmldes
 
 
 unsigned char *virParseUUID(char **ptr, const char *uuid) {
-    int rawuuid[16];
+    int rawuuid[VIR_UUID_BUFLEN];
     const char *cur;
     unsigned char *dst_uuid = NULL;
     int i;
@@ -1546,7 +1546,7 @@ unsigned char *virParseUUID(char **ptr, 
      * pairs as long as there is 32 of them in the end.
      */
     cur = uuid;
-    for (i = 0;i < 16;) {
+    for (i = 0;i < VIR_UUID_BUFLEN;) {
         rawuuid[i] = 0;
         if (*cur == 0)
 	    goto error;
@@ -1581,7 +1581,7 @@ unsigned char *virParseUUID(char **ptr, 
     dst_uuid = (unsigned char *) *ptr;
     *ptr += 16;
 
-    for (i = 0; i < 16; i++)
+    for (i = 0; i < VIR_UUID_BUFLEN; i++)
         dst_uuid[i] = rawuuid[i] & 0xFF;
 
 error:



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]