node_net.go 964 B

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