blob: 3e4e90dc444cd134f4a7b10fc56d06b2415294bd [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001// Copyright 2016 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.
4
5package uuid
6
7import (
8 "sync"
9)
10
11var (
12 nodeMu sync.Mutex
13 ifname string // name of interface being used
14 nodeID [6]byte // hardware for version 1 UUIDs
15 zeroID [6]byte // nodeID with only 0's
16)
17
18// NodeInterface returns the name of the interface from which the NodeID was
19// derived. The interface "user" is returned if the NodeID was set by
20// SetNodeID.
21func NodeInterface() string {
22 defer nodeMu.Unlock()
23 nodeMu.Lock()
24 return ifname
25}
26
27// SetNodeInterface selects the hardware address to be used for Version 1 UUIDs.
28// If name is "" then the first usable interface found will be used or a random
29// Node ID will be generated. If a named interface cannot be found then false
30// is returned.
31//
32// SetNodeInterface never fails when name is "".
33func SetNodeInterface(name string) bool {
34 defer nodeMu.Unlock()
35 nodeMu.Lock()
36 return setNodeInterface(name)
37}
38
39func setNodeInterface(name string) bool {
40 iname, addr := getHardwareInterface(name) // null implementation for js
41 if iname != "" && addr != nil {
42 ifname = iname
43 copy(nodeID[:], addr)
44 return true
45 }
46
47 // We found no interfaces with a valid hardware address. If name
48 // does not specify a specific interface generate a random Node ID
49 // (section 4.1.6)
50 if name == "" {
51 randomBits(nodeID[:])
52 return true
53 }
54 return false
55}
56
57// NodeID returns a slice of a copy of the current Node ID, setting the Node ID
58// if not already set.
59func NodeID() []byte {
60 defer nodeMu.Unlock()
61 nodeMu.Lock()
62 if nodeID == zeroID {
63 setNodeInterface("")
64 }
65 nid := nodeID
66 return nid[:]
67}
68
69// SetNodeID sets the Node ID to be used for Version 1 UUIDs. The first 6 bytes
70// of id are used. If id is less than 6 bytes then false is returned and the
71// Node ID is not set.
72func SetNodeID(id []byte) bool {
73 if len(id) < 6 {
74 return false
75 }
76 defer nodeMu.Unlock()
77 nodeMu.Lock()
78 copy(nodeID[:], id)
79 ifname = "user"
80 return true
81}
82
83// NodeID returns the 6 byte node id encoded in uuid. It returns nil if uuid is
84// not valid. The NodeID is only well defined for version 1 and 2 UUIDs.
85func (uuid UUID) NodeID() []byte {
86 var node [6]byte
87 copy(node[:], uuid[10:])
88 return node[:]
89}