1 // Copyright 2011 Google Inc. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 interfaces []net.Interface // cached list of interfaces
15 ifname string // name of interface being used
16 nodeID []byte // hardware for version 1 UUIDs
19 // NodeInterface returns the name of the interface from which the NodeID was
20 // derived. The interface "user" is returned if the NodeID was set by
22 func NodeInterface() string {
28 // SetNodeInterface selects the hardware address to be used for Version 1 UUIDs.
29 // If name is "" then the first usable interface found will be used or a random
30 // Node ID will be generated. If a named interface cannot be found then false
33 // SetNodeInterface never fails when name is "".
34 func SetNodeInterface(name string) bool {
37 return setNodeInterface(name)
40 func setNodeInterface(name string) bool {
41 if interfaces == nil {
43 interfaces, err = net.Interfaces()
44 if err != nil && name != "" {
49 for _, ifs := range interfaces {
50 if len(ifs.HardwareAddr) >= 6 && (name == "" || name == ifs.Name) {
51 if setNodeID(ifs.HardwareAddr) {
58 // We found no interfaces with a valid hardware address. If name
59 // does not specify a specific interface generate a random Node ID
63 nodeID = make([]byte, 6)
71 // NodeID returns a slice of a copy of the current Node ID, setting the Node ID
72 // if not already set.
73 func NodeID() []byte {
79 nid := make([]byte, 6)
84 // SetNodeID sets the Node ID to be used for Version 1 UUIDs. The first 6 bytes
85 // of id are used. If id is less than 6 bytes then false is returned and the
86 // Node ID is not set.
87 func SetNodeID(id []byte) bool {
97 func setNodeID(id []byte) bool {
102 nodeID = make([]byte, 6)
108 // NodeID returns the 6 byte node id encoded in uuid. It returns nil if uuid is
109 // not valid. The NodeID is only well defined for version 1 and 2 UUIDs.
110 func (uuid UUID) NodeID() []byte {
114 node := make([]byte, 6)
115 copy(node, uuid[10:])