ducky/devices

Paddy 2015-12-19 Parent:a700ede02f91

17:ad63b888f899 Go to Latest

ducky/devices/vendor/github.com/pborman/uuid/node.go

Fix JSON pointers in errors when creating devices. Our JSON pointers used to point at the root of the document, but the properties were actually in objects in an array keyed off of "devices", so we had to update the field property of our errors to match. While we were in there, we fixed the "insufficient" error for a Device's name to be "missing" when the name is an empty string. So far, the only way for a Device's name for it to be insufficient is _for it to be the empty string_, but if in the future we raise the minimum length of the Device name, there will be a distinction and I'd like the code to recognise it.

History
paddy@0 1 // Copyright 2011 Google Inc. All rights reserved.
paddy@0 2 // Use of this source code is governed by a BSD-style
paddy@0 3 // license that can be found in the LICENSE file.
paddy@0 4
paddy@0 5 package uuid
paddy@0 6
paddy@0 7 import "net"
paddy@0 8
paddy@0 9 var (
paddy@0 10 interfaces []net.Interface // cached list of interfaces
paddy@0 11 ifname string // name of interface being used
paddy@0 12 nodeID []byte // hardware for version 1 UUIDs
paddy@0 13 )
paddy@0 14
paddy@0 15 // NodeInterface returns the name of the interface from which the NodeID was
paddy@0 16 // derived. The interface "user" is returned if the NodeID was set by
paddy@0 17 // SetNodeID.
paddy@0 18 func NodeInterface() string {
paddy@0 19 return ifname
paddy@0 20 }
paddy@0 21
paddy@0 22 // SetNodeInterface selects the hardware address to be used for Version 1 UUIDs.
paddy@0 23 // If name is "" then the first usable interface found will be used or a random
paddy@0 24 // Node ID will be generated. If a named interface cannot be found then false
paddy@0 25 // is returned.
paddy@0 26 //
paddy@0 27 // SetNodeInterface never fails when name is "".
paddy@0 28 func SetNodeInterface(name string) bool {
paddy@0 29 if interfaces == nil {
paddy@0 30 var err error
paddy@0 31 interfaces, err = net.Interfaces()
paddy@0 32 if err != nil && name != "" {
paddy@0 33 return false
paddy@0 34 }
paddy@0 35 }
paddy@0 36
paddy@0 37 for _, ifs := range interfaces {
paddy@0 38 if len(ifs.HardwareAddr) >= 6 && (name == "" || name == ifs.Name) {
paddy@0 39 if setNodeID(ifs.HardwareAddr) {
paddy@0 40 ifname = ifs.Name
paddy@0 41 return true
paddy@0 42 }
paddy@0 43 }
paddy@0 44 }
paddy@0 45
paddy@0 46 // We found no interfaces with a valid hardware address. If name
paddy@0 47 // does not specify a specific interface generate a random Node ID
paddy@0 48 // (section 4.1.6)
paddy@0 49 if name == "" {
paddy@0 50 if nodeID == nil {
paddy@0 51 nodeID = make([]byte, 6)
paddy@0 52 }
paddy@0 53 randomBits(nodeID)
paddy@0 54 return true
paddy@0 55 }
paddy@0 56 return false
paddy@0 57 }
paddy@0 58
paddy@0 59 // NodeID returns a slice of a copy of the current Node ID, setting the Node ID
paddy@0 60 // if not already set.
paddy@0 61 func NodeID() []byte {
paddy@0 62 if nodeID == nil {
paddy@0 63 SetNodeInterface("")
paddy@0 64 }
paddy@0 65 nid := make([]byte, 6)
paddy@0 66 copy(nid, nodeID)
paddy@0 67 return nid
paddy@0 68 }
paddy@0 69
paddy@0 70 // SetNodeID sets the Node ID to be used for Version 1 UUIDs. The first 6 bytes
paddy@0 71 // of id are used. If id is less than 6 bytes then false is returned and the
paddy@0 72 // Node ID is not set.
paddy@0 73 func SetNodeID(id []byte) bool {
paddy@0 74 if setNodeID(id) {
paddy@0 75 ifname = "user"
paddy@0 76 return true
paddy@0 77 }
paddy@0 78 return false
paddy@0 79 }
paddy@0 80
paddy@0 81 func setNodeID(id []byte) bool {
paddy@0 82 if len(id) < 6 {
paddy@0 83 return false
paddy@0 84 }
paddy@0 85 if nodeID == nil {
paddy@0 86 nodeID = make([]byte, 6)
paddy@0 87 }
paddy@0 88 copy(nodeID, id)
paddy@0 89 return true
paddy@0 90 }
paddy@0 91
paddy@0 92 // NodeID returns the 6 byte node id encoded in uuid. It returns nil if uuid is
paddy@0 93 // not valid. The NodeID is only well defined for version 1 and 2 UUIDs.
paddy@0 94 func (uuid UUID) NodeID() []byte {
paddy@0 95 if len(uuid) != 16 {
paddy@0 96 return nil
paddy@0 97 }
paddy@0 98 node := make([]byte, 6)
paddy@0 99 copy(node, uuid[10:])
paddy@0 100 return node
paddy@0 101 }