Skip to contentSkip to conversation

Support alternative formats for MAC addresses

PATCH New Balazs Attila-Mihaly (Cd-MaN) 1 participant 1 message Dec 4, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Support alternative formats for MAC addresses

The attached patch modifies the parser function for the mac addresses (ie. -net nic,macaddr=XXXXXX) in the following ways:

- the accepted separators now include "-", because on Windows it is common to write a MAC addresses separated by dashes (ie. 54-32-00-12-34-56)
- it supports specifying the lower part of the MAC address (ie. the last three bytes) as an integer number (so that you can say -net nic,macaddr=0x1234, which would result in the mac address of 54-32-00-00-12-34). This simplifies the situations where you want to connect together multiple Qemu virtual machines and you want an easy way to ensure that they network cards don't have colliding MAC addresses (simpler than having to retype the whole address)

Any comments are welcome.

      __________________________________________________________

Sent from Yahoo! - the World's favourite mail http://uk.mail.yahoo.com

Index: vl.c
===================================================================
RCS file: /sources/qemu/qemu/vl.c,v
retrieving revision 1.376

vl.c +25 -11
diff -u -r1.376 vl.c
@@ -3452,18 +3452,32 @@
 static int parse_macaddr(uint8_t *macaddr, const char *p)
 {
     int i;
-    for(i = 0; i < 6; i++) {
-        macaddr[i] = strtol(p, (char **)&p, 16);
-        if (i == 5) {
-            if (*p != '\0')
-                return -1;
-        } else {
-            if (*p != ':')
-                return -1;
-            p++;
-        }
+    char *last_char;
+    long int offset;
+    
+    errno = 0;
+    offset = strtol(p, &last_char, 0);    
+    if ((0 == errno) && ('\0' == *last_char) && (offset >= 0) && (offset <= 0xFFFFFF)) {
+    	macaddr[3] = (offset & 0xFF0000) >> 16;
+    	macaddr[4] = (offset & 0xFF00) >> 8;
+    	macaddr[5] = offset & 0xFF;
+    	return 0;
+    } else {
+		  for(i = 0; i < 6; i++) {
+		      macaddr[i] = strtol(p, (char **)&p, 16);
+		      if (i == 5) {
+		          if (*p != '\0')
+		              return -1;
+		      } else {
+		          if (*p != ':' && *p != '-')
+		              return -1;
+		          p++;
+		      }
+		  }
+		  return 0;    
     }
-    return 0;
+    
+    return -1;
 }
 
 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)