]> xenbits.xensource.com Git - xen.git/commitdiff
golang/xenlight: define Mac builtin type
authorNick Rosbrook <rosbrookn@ainfosec.com>
Mon, 16 Dec 2019 18:08:03 +0000 (18:08 +0000)
committerGeorge Dunlap <george.dunlap@citrix.com>
Mon, 16 Dec 2019 18:08:03 +0000 (18:08 +0000)
Define Mac as [6]byte and implement fromC, toC, and String functions.

Signed-off-by: Nick Rosbrook <rosbrookn@ainfosec.com>
Reviewed-by: George Dunlap <george.dunlap@citrix.com>
tools/golang/xenlight/xenlight.go

index 72afc3cf145e9bf393ac29129656af19b8aca50d..17d146771e7865cabcb7f012dbdbd866cb7919d6 100644 (file)
@@ -181,6 +181,39 @@ func (d *Defbool) toC() (C.libxl_defbool, error) {
        return c, nil
 }
 
+// Mac represents a libxl_mac, or simply a MAC address.
+type Mac [6]byte
+
+// String formats a Mac address to string representation.
+func (mac Mac) String() string {
+       s := "%02x:%02x:%02x:%02x:%02x:%02x"
+       opts := make([]interface{}, 6)
+
+       for i, v := range mac {
+               opts[i] = v
+       }
+
+       return fmt.Sprintf(s, opts...)
+}
+
+func (mac *Mac) fromC(cmac *C.libxl_mac) error {
+       for i := range *mac {
+               mac[i] = byte(cmac[i])
+       }
+
+       return nil
+}
+
+func (mac Mac) toC() (C.libxl_mac, error) {
+       var cmac C.libxl_mac
+
+       for i, v := range mac {
+               cmac[i] = C.uint8_t(v)
+       }
+
+       return cmac, nil
+}
+
 type Context struct {
        ctx    *C.libxl_ctx
        logger *C.xentoollog_logger_stdiostream