iface_windows.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // +build windows
  2. // Copyright 2015 flannel authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package ip
  16. import (
  17. "errors"
  18. "fmt"
  19. "github.com/flannel-io/flannel/pkg/powershell"
  20. "net"
  21. )
  22. // GetInterfaceIP4Addr returns the IPv4 address for the given network interface
  23. func GetInterfaceIP4Addr(iface *net.Interface) (net.IP, error) {
  24. addrs, err := iface.Addrs()
  25. if err != nil {
  26. return nil, err
  27. }
  28. for _, addr := range addrs {
  29. var ip net.IP
  30. switch v := addr.(type) {
  31. case *net.IPAddr:
  32. ip = v.IP
  33. case *net.IPNet:
  34. ip = v.IP
  35. }
  36. if ip != nil && ip.To4() != nil {
  37. return ip, nil
  38. }
  39. }
  40. return nil, errors.New("no IPv4 address found for given interface")
  41. }
  42. // GetDefaultGatewayInterface returns the first network interface found with a default gateway set
  43. func GetDefaultGatewayInterface() (*net.Interface, error) {
  44. index, err := getDefaultGatewayInterfaceIndex()
  45. if err != nil {
  46. return nil, err
  47. }
  48. return net.InterfaceByIndex(index)
  49. }
  50. func getDefaultGatewayInterfaceIndex() (int, error) {
  51. powerShellJsonData := struct {
  52. IfIndex int `json:"ifIndex"`
  53. }{-1}
  54. err := powershell.RunCommandWithJsonResult("Get-NetRoute | Where { $_.DestinationPrefix -eq '0.0.0.0/0' } | Select-Object -Property ifIndex", &powerShellJsonData)
  55. if err != nil {
  56. return -1, err
  57. }
  58. if powerShellJsonData.IfIndex < 0 {
  59. return -1, errors.New("unable to find default gateway interface index")
  60. }
  61. return powerShellJsonData.IfIndex, nil
  62. }
  63. // GetInterfaceByIP tries to get the network interface with the given ip address
  64. func GetInterfaceByIP(search net.IP) (*net.Interface, error) {
  65. ifaces, err := net.Interfaces()
  66. if err != nil {
  67. return nil, err
  68. }
  69. for _, i := range ifaces {
  70. addrs, err := i.Addrs()
  71. if err != nil {
  72. return nil, err
  73. }
  74. for _, addr := range addrs {
  75. var ip net.IP
  76. switch v := addr.(type) {
  77. case *net.IPNet:
  78. ip = v.IP
  79. case *net.IPAddr:
  80. ip = v.IP
  81. }
  82. if ip != nil && ip.Equal(search) {
  83. return &i, nil
  84. }
  85. }
  86. }
  87. return nil, errors.New("no interface with given IP found")
  88. }
  89. // EnableForwardingForInterface enables forwarding for given interface.
  90. // The process must run with elevated rights. Otherwise the function will fail with an "Access Denied" error.
  91. func EnableForwardingForInterface(iface *net.Interface) error {
  92. return setForwardingForInterface(iface, true)
  93. }
  94. // DisableForwardingForInterface disables forwarding for given interface.
  95. // The process must run with elevated rights. Otherwise the function will fail with an "Access Denied" error.
  96. func DisableForwardingForInterface(iface *net.Interface) error {
  97. return setForwardingForInterface(iface, false)
  98. }
  99. func setForwardingForInterface(iface *net.Interface, forwarding bool) error {
  100. value := "Enabled"
  101. if !forwarding {
  102. value = "Disabled"
  103. }
  104. _, err := powershell.RunCommandf("Set-NetIPInterface -ifIndex %d -AddressFamily IPv4 -Forwarding %s", iface.Index, value)
  105. if err != nil {
  106. return err
  107. }
  108. return nil
  109. }
  110. func IsForwardingEnabledForInterface(iface *net.Interface) (bool, error) {
  111. powerShellJsonData := struct {
  112. Forwarding int `json:"Forwarding"`
  113. }{0}
  114. err := powershell.RunCommandWithJsonResult(fmt.Sprintf("Get-NetIPInterface -ifIndex %d -AddressFamily IPv4 | Select-Object -Property Forwarding", iface.Index), &powerShellJsonData)
  115. if err != nil {
  116. return false, err
  117. }
  118. return powerShellJsonData.Forwarding == 1, nil
  119. }