iface.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2015 flannel authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // +build !windows
  15. package ip
  16. import (
  17. "errors"
  18. "net"
  19. "syscall"
  20. "github.com/vishvananda/netlink"
  21. )
  22. func getIfaceAddrs(iface *net.Interface) ([]netlink.Addr, error) {
  23. link := &netlink.Device{
  24. netlink.LinkAttrs{
  25. Index: iface.Index,
  26. },
  27. }
  28. return netlink.AddrList(link, syscall.AF_INET)
  29. }
  30. func GetIfaceIP4Addr(iface *net.Interface) (net.IP, error) {
  31. addrs, err := getIfaceAddrs(iface)
  32. if err != nil {
  33. return nil, err
  34. }
  35. // prefer non link-local addr
  36. var ll net.IP
  37. for _, addr := range addrs {
  38. if addr.IP.To4() == nil {
  39. continue
  40. }
  41. if addr.IP.IsGlobalUnicast() {
  42. return addr.IP, nil
  43. }
  44. if addr.IP.IsLinkLocalUnicast() {
  45. ll = addr.IP
  46. }
  47. }
  48. if ll != nil {
  49. // didn't find global but found link-local. it'll do.
  50. return ll, nil
  51. }
  52. return nil, errors.New("No IPv4 address found for given interface")
  53. }
  54. func GetIfaceIP4AddrMatch(iface *net.Interface, matchAddr net.IP) error {
  55. addrs, err := getIfaceAddrs(iface)
  56. if err != nil {
  57. return err
  58. }
  59. for _, addr := range addrs {
  60. // Attempt to parse the address in CIDR notation
  61. // and assert it is IPv4
  62. if addr.IP.To4() != nil {
  63. if addr.IP.To4().Equal(matchAddr) {
  64. return nil
  65. }
  66. }
  67. }
  68. return errors.New("No IPv4 address found for given interface")
  69. }
  70. func GetDefaultGatewayIface() (*net.Interface, error) {
  71. routes, err := netlink.RouteList(nil, syscall.AF_INET)
  72. if err != nil {
  73. return nil, err
  74. }
  75. for _, route := range routes {
  76. if route.Dst == nil || route.Dst.String() == "0.0.0.0/0" {
  77. if route.LinkIndex <= 0 {
  78. return nil, errors.New("Found default route but could not determine interface")
  79. }
  80. return net.InterfaceByIndex(route.LinkIndex)
  81. }
  82. }
  83. return nil, errors.New("Unable to find default route")
  84. }
  85. func GetInterfaceByIP(ip net.IP) (*net.Interface, error) {
  86. ifaces, err := net.Interfaces()
  87. if err != nil {
  88. return nil, err
  89. }
  90. for _, iface := range ifaces {
  91. err := GetIfaceIP4AddrMatch(&iface, ip)
  92. if err == nil {
  93. return &iface, nil
  94. }
  95. }
  96. return nil, errors.New("No interface with given IP found")
  97. }