# HG changeset patch
# User Ian Campbell <ian.campbell@xxxxxxxxxx>
# Date 1317807819 -3600
# Node ID b85fcaaf73a9cfb5e5f448015c56630aa8daba3d
# Parent 6a40e32738e7026769c2932fa53b8aefd34e6a77
libxl: add and use parse_mac helper function
rather than open coding a bunch it a bunch of times.
Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx>
diff -r 6a40e32738e7 -r b85fcaaf73a9 tools/libxl/libxl_internal.c
--- a/tools/libxl/libxl_internal.c Wed Oct 05 10:43:10 2011 +0100
+++ b/tools/libxl/libxl_internal.c Wed Oct 05 10:43:39 2011 +0100
@@ -277,6 +277,23 @@ int libxl__file_reference_unmap(libxl_fi
return ERROR_FAIL;
}
+_hidden int libxl__parse_mac(const char *s, libxl_mac mac)
+{
+ const char *tok;
+ char *endptr;
+ int i;
+
+ for (i = 0, tok = s; *tok && (i < 6); ++i, tok += 3) {
+ mac[i] = strtol(tok, &endptr, 16);
+ if (endptr != (tok + 2) || (*endptr != '\0' && *endptr != ':') )
+ return ERROR_INVAL;
+ }
+ if ( i != 6 )
+ return ERROR_INVAL;
+
+ return 0;
+}
+
int libxl__fd_set_cloexec(int fd)
{
int flags = 0;
diff -r 6a40e32738e7 -r b85fcaaf73a9 tools/libxl/libxl_internal.h
--- a/tools/libxl/libxl_internal.h Wed Oct 05 10:43:10 2011 +0100
+++ b/tools/libxl/libxl_internal.h Wed Oct 05 10:43:39 2011 +0100
@@ -426,6 +426,8 @@ _hidden int libxl__fd_set_cloexec(int fd
_hidden int libxl__e820_alloc(libxl__gc *gc, uint32_t domid,
libxl_domain_config *d_config);
+_hidden int libxl__parse_mac(const char *s, libxl_mac mac);
+
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
diff -r 6a40e32738e7 -r b85fcaaf73a9 tools/libxl/libxl_utils.c
--- a/tools/libxl/libxl_utils.c Wed Oct 05 10:43:10 2011 +0100
+++ b/tools/libxl/libxl_utils.c Wed Oct 05 10:43:39 2011 +0100
@@ -452,22 +452,19 @@ int libxl_mac_to_device_nic(libxl_ctx *c
const char *mac, libxl_device_nic *nic)
{
libxl_nicinfo *nics;
- unsigned int nb, i;
+ unsigned int nb, rc, i;
int found;
- uint8_t mac_n[6];
+ libxl_mac mac_n;
uint8_t *a, *b;
- const char *tok;
- char *endptr;
+
+ rc = libxl__parse_mac(mac, mac_n);
+ if (rc)
+ return rc;
nics = libxl_list_nics(ctx, domid, &nb);
if (!nics)
return ERROR_FAIL;
- for (i = 0, tok = mac; *tok && (i < 6); ++i, tok += 3) {
- mac_n[i] = strtol(tok, &endptr, 16);
- if (endptr != (tok + 2))
- return ERROR_INVAL;
- }
memset(nic, 0, sizeof (libxl_device_nic));
found = 0;
for (i = 0; i < nb; ++i) {
@@ -494,9 +491,8 @@ int libxl_devid_to_device_nic(libxl_ctx
const char *devid, libxl_device_nic *nic)
{
libxl__gc gc = LIBXL_INIT_GC(ctx);
- char *tok, *val;
+ char *val;
char *dompath, *nic_path_fe, *nic_path_be;
- unsigned int i;
int rc = ERROR_FAIL;
memset(nic, 0, sizeof (libxl_device_nic));
@@ -515,10 +511,10 @@ int libxl_devid_to_device_nic(libxl_ctx
nic->devid = strtoul(devid, NULL, 10);
val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac",
nic_path_fe));
- for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
- ++i, tok = strtok(NULL, ":")) {
- nic->mac[i] = strtoul(tok, NULL, 16);
- }
+ rc = libxl__parse_mac(val, nic->mac);
+ if (rc)
+ goto out;
+
nic->script = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/script",
nic_path_be), NULL);
rc = 0;
out:
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel
|