node_net.go 949 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2017 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. // +build !js
  5. package uuid
  6. import "net"
  7. var interfaces []net.Interface // cached list of interfaces
  8. // getHardwareInterface returns the name and hardware address of interface name.
  9. // If name is "" then the name and hardware address of one of the system's
  10. // interfaces is returned. If no interfaces are found (name does not exist or
  11. // there are no interfaces) then "", nil is returned.
  12. //
  13. // Only addresses of at least 6 bytes are returned.
  14. func getHardwareInterface(name string) (string, []byte) {
  15. if interfaces == nil {
  16. var err error
  17. interfaces, err = net.Interfaces()
  18. if err != nil {
  19. return "", nil
  20. }
  21. }
  22. for _, ifs := range interfaces {
  23. if len(ifs.HardwareAddr) >= 6 && (name == "" || name == ifs.Name) {
  24. return ifs.Name, ifs.HardwareAddr
  25. }
  26. }
  27. return "", nil
  28. }