iface.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2015 CoreOS, Inc.
  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. package ip
  15. import (
  16. "errors"
  17. "net"
  18. "syscall"
  19. "github.com/coreos/flannel/Godeps/_workspace/src/github.com/vishvananda/netlink"
  20. )
  21. func GetIfaceIP4Addr(iface *net.Interface) (net.IP, error) {
  22. addrs, err := iface.Addrs()
  23. if err != nil {
  24. return nil, err
  25. }
  26. // prefer non link-local addr
  27. var ll net.IP
  28. for _, addr := range addrs {
  29. // Attempt to parse the address in CIDR notation
  30. // and assert it is IPv4
  31. ip, _, err := net.ParseCIDR(addr.String())
  32. if err != nil || ip.To4() == nil {
  33. continue
  34. }
  35. if ip.IsGlobalUnicast() {
  36. return ip, nil
  37. }
  38. if ip.IsLinkLocalUnicast() {
  39. ll = ip
  40. }
  41. }
  42. if ll != nil {
  43. // didn't find global but found link-local. it'll do.
  44. return ll, nil
  45. }
  46. return nil, errors.New("No IPv4 address found for given interface")
  47. }
  48. func GetIfaceIP4AddrMatch(iface *net.Interface, matchAddr net.IP) error {
  49. addrs, err := iface.Addrs()
  50. if err != nil {
  51. return err
  52. }
  53. for _, addr := range addrs {
  54. // Attempt to parse the address in CIDR notation
  55. // and assert it is IPv4
  56. ip, _, err := net.ParseCIDR(addr.String())
  57. if err == nil && ip.To4() != nil {
  58. if ip.To4().Equal(matchAddr) {
  59. return nil
  60. }
  61. }
  62. }
  63. return errors.New("No IPv4 address found for given interface")
  64. }
  65. func GetDefaultGatewayIface() (*net.Interface, error) {
  66. routes, err := netlink.RouteList(nil, syscall.AF_INET)
  67. if err != nil {
  68. return nil, err
  69. }
  70. for _, route := range routes {
  71. if route.Dst == nil || route.Dst.String() == "0.0.0.0/0" {
  72. if route.LinkIndex <= 0 {
  73. return nil, errors.New("Found default route but could not determine interface")
  74. }
  75. return net.InterfaceByIndex(route.LinkIndex)
  76. }
  77. }
  78. return nil, errors.New("Unable to find default route")
  79. }
  80. func GetInterfaceByIP(ip net.IP) (*net.Interface, error) {
  81. ifaces, err := net.Interfaces()
  82. if err != nil {
  83. return nil, err
  84. }
  85. for _, iface := range ifaces {
  86. err := GetIfaceIP4AddrMatch(&iface, ip)
  87. if err == nil {
  88. return &iface, nil
  89. }
  90. }
  91. return nil, errors.New("No interface with given IP found")
  92. }