routes_linux.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2016 The etcd 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 linux
  15. // +build 386 amd64
  16. // TODO support native endian but without using "unsafe"
  17. package netutil
  18. import (
  19. "bytes"
  20. "encoding/binary"
  21. "fmt"
  22. "net"
  23. "syscall"
  24. )
  25. var errNoDefaultRoute = fmt.Errorf("could not find default route")
  26. func GetDefaultHost() (string, error) {
  27. rmsg, rerr := getDefaultRoute()
  28. if rerr != nil {
  29. return "", rerr
  30. }
  31. attrs, aerr := syscall.ParseNetlinkRouteAttr(rmsg)
  32. if aerr != nil {
  33. return "", aerr
  34. }
  35. oif := uint32(0)
  36. for _, attr := range attrs {
  37. if attr.Attr.Type == syscall.RTA_PREFSRC {
  38. return net.IP(attr.Value).String(), nil
  39. }
  40. if attr.Attr.Type == syscall.RTA_OIF {
  41. oif = binary.LittleEndian.Uint32(attr.Value)
  42. }
  43. }
  44. if oif == 0 {
  45. return "", errNoDefaultRoute
  46. }
  47. // prefsrc not detected, fall back to getting address from iface
  48. ifmsg, ierr := getIface(oif)
  49. if ierr != nil {
  50. return "", ierr
  51. }
  52. attrs, aerr = syscall.ParseNetlinkRouteAttr(ifmsg)
  53. if aerr != nil {
  54. return "", aerr
  55. }
  56. for _, attr := range attrs {
  57. if attr.Attr.Type == syscall.RTA_SRC {
  58. return net.IP(attr.Value).String(), nil
  59. }
  60. }
  61. return "", errNoDefaultRoute
  62. }
  63. func getDefaultRoute() (*syscall.NetlinkMessage, error) {
  64. dat, err := syscall.NetlinkRIB(syscall.RTM_GETROUTE, syscall.AF_UNSPEC)
  65. if err != nil {
  66. return nil, err
  67. }
  68. msgs, msgErr := syscall.ParseNetlinkMessage(dat)
  69. if msgErr != nil {
  70. return nil, msgErr
  71. }
  72. rtmsg := syscall.RtMsg{}
  73. for _, m := range msgs {
  74. if m.Header.Type != syscall.RTM_NEWROUTE {
  75. continue
  76. }
  77. buf := bytes.NewBuffer(m.Data[:syscall.SizeofRtMsg])
  78. if rerr := binary.Read(buf, binary.LittleEndian, &rtmsg); rerr != nil {
  79. continue
  80. }
  81. if rtmsg.Dst_len == 0 {
  82. // zero-length Dst_len implies default route
  83. return &m, nil
  84. }
  85. }
  86. return nil, errNoDefaultRoute
  87. }
  88. func getIface(idx uint32) (*syscall.NetlinkMessage, error) {
  89. dat, err := syscall.NetlinkRIB(syscall.RTM_GETADDR, syscall.AF_UNSPEC)
  90. if err != nil {
  91. return nil, err
  92. }
  93. msgs, msgErr := syscall.ParseNetlinkMessage(dat)
  94. if msgErr != nil {
  95. return nil, msgErr
  96. }
  97. ifaddrmsg := syscall.IfAddrmsg{}
  98. for _, m := range msgs {
  99. if m.Header.Type != syscall.RTM_NEWADDR {
  100. continue
  101. }
  102. buf := bytes.NewBuffer(m.Data[:syscall.SizeofIfAddrmsg])
  103. if rerr := binary.Read(buf, binary.LittleEndian, &ifaddrmsg); rerr != nil {
  104. continue
  105. }
  106. if ifaddrmsg.Index == idx {
  107. return &m, nil
  108. }
  109. }
  110. return nil, errNoDefaultRoute
  111. }