node.go 2.4 KB

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