node.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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.
  4. package uuid
  5. import (
  6. guuid "github.com/google/uuid"
  7. )
  8. // NodeInterface returns the name of the interface from which the NodeID was
  9. // derived. The interface "user" is returned if the NodeID was set by
  10. // SetNodeID.
  11. func NodeInterface() string {
  12. return guuid.NodeInterface()
  13. }
  14. // SetNodeInterface selects the hardware address to be used for Version 1 UUIDs.
  15. // If name is "" then the first usable interface found will be used or a random
  16. // Node ID will be generated. If a named interface cannot be found then false
  17. // is returned.
  18. //
  19. // SetNodeInterface never fails when name is "".
  20. func SetNodeInterface(name string) bool {
  21. return guuid.SetNodeInterface(name)
  22. }
  23. // NodeID returns a slice of a copy of the current Node ID, setting the Node ID
  24. // if not already set.
  25. func NodeID() []byte {
  26. return guuid.NodeID()
  27. }
  28. // SetNodeID sets the Node ID to be used for Version 1 UUIDs. The first 6 bytes
  29. // of id are used. If id is less than 6 bytes then false is returned and the
  30. // Node ID is not set.
  31. func SetNodeID(id []byte) bool {
  32. return guuid.SetNodeID(id)
  33. }
  34. // NodeID returns the 6 byte node id encoded in uuid. It returns nil if uuid is
  35. // not valid. The NodeID is only well defined for version 1 and 2 UUIDs.
  36. func (uuid UUID) NodeID() []byte {
  37. if len(uuid) != 16 {
  38. return nil
  39. }
  40. node := make([]byte, 6)
  41. copy(node, uuid[10:])
  42. return node
  43. }