networkInterface.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2015, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package common
  20. import (
  21. "net"
  22. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  23. )
  24. // GetInterfaceIPAddresses takes an interface name, such as "eth0", and
  25. // returns the first non-link local IPv4 and IPv6 addresses associated with
  26. // it. Either of the IPv4 or IPv6 address may be nil. If neither type of
  27. // address is found, an error is returned.
  28. func GetInterfaceIPAddresses(interfaceName string) (net.IP, net.IP, error) {
  29. var IPv4Address, IPv6Address net.IP
  30. availableInterfaces, err := net.InterfaceByName(interfaceName)
  31. if err != nil {
  32. return nil, nil, errors.Trace(err)
  33. }
  34. addrs, err := availableInterfaces.Addrs()
  35. if err != nil {
  36. return nil, nil, errors.Trace(err)
  37. }
  38. for _, addr := range addrs {
  39. ipNet := addr.(*net.IPNet)
  40. if ipNet == nil {
  41. continue
  42. }
  43. if ipNet.IP.To4() != nil {
  44. if IPv4Address == nil && !ipNet.IP.IsLinkLocalUnicast() {
  45. IPv4Address = ipNet.IP
  46. }
  47. } else {
  48. if IPv6Address == nil && !ipNet.IP.IsLinkLocalUnicast() {
  49. IPv6Address = ipNet.IP
  50. }
  51. }
  52. if IPv4Address != nil && IPv6Address != nil {
  53. break
  54. }
  55. }
  56. if IPv4Address != nil || IPv6Address != nil {
  57. return IPv4Address, IPv6Address, nil
  58. }
  59. return nil, nil, errors.Tracef("could not find any IP address for interface %s", interfaceName)
  60. }
  61. // GetRoutableInterfaceIPAddresses returns GetInterfaceIPAddresses values for
  62. // the first non-loopback, non-point-to-point network interface on the host
  63. // that has an IP address.
  64. func GetRoutableInterfaceIPAddresses() (net.IP, net.IP, error) {
  65. interfaces, err := net.Interfaces()
  66. if err != nil {
  67. return nil, nil, errors.Trace(err)
  68. }
  69. for i, in := range interfaces {
  70. if (in.Flags&net.FlagUp == 0) ||
  71. (in.Flags&(net.FlagLoopback|net.FlagPointToPoint)) != 0 {
  72. continue
  73. }
  74. IPv4Address, IPv6Address, err := GetInterfaceIPAddresses(in.Name)
  75. if err == nil {
  76. return IPv4Address, IPv6Address, nil
  77. } else if i == len(interfaces)-1 {
  78. return nil, nil, errors.Trace(err)
  79. }
  80. }
  81. return nil, nil, errors.TraceNew("no candidate interfaces found")
  82. }