arp_linux.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package neighbors
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net"
  6. "os/exec"
  7. "goa.design/clue/log"
  8. )
  9. type (
  10. arp struct {
  11. cmd string
  12. arping ARPing
  13. }
  14. arpEntry struct {
  15. IPAddress string `json:"dst"`
  16. MACAddress string `json:"lladdr"`
  17. Interface string `json:"dev"`
  18. }
  19. )
  20. func NewARP(count uint) (ARP, error) {
  21. cmd, err := exec.LookPath("ip")
  22. if err != nil {
  23. return nil, err
  24. }
  25. arping, err := NewARPing(count)
  26. if err != nil {
  27. return nil, err
  28. }
  29. return &arp{cmd: cmd, arping: arping}, nil
  30. }
  31. func (a *arp) Present(ctx context.Context, ifs Interfaces, hws HardwareAddrStates) (present bool, err error) {
  32. as := make(map[string]bool, len(hws))
  33. for hw := range hws {
  34. as[hw] = false
  35. }
  36. cmd := exec.CommandContext(ctx, a.cmd, "-family", "inet", "-json", "neighbor", "show", "nud", "reachable")
  37. log.Debug(ctx, log.KV{K: "cmd", V: cmd})
  38. b, err := cmd.Output()
  39. if err != nil {
  40. return
  41. }
  42. var es []arpEntry
  43. err = json.Unmarshal(b, &es)
  44. for _, e := range es {
  45. if ifs[e.Interface] {
  46. var hwa net.HardwareAddr
  47. hwa, err = net.ParseMAC(e.MACAddress)
  48. if err != nil {
  49. return
  50. }
  51. hw := hwa.String()
  52. if _, ok := as[hw]; ok {
  53. ok, err = a.arping.Ping(ctx, e.Interface, hw, e.IPAddress)
  54. if err != nil {
  55. return
  56. }
  57. as[hw] = ok
  58. }
  59. }
  60. }
  61. for hw, ok := range as {
  62. hws[hw].Set(ok)
  63. if ok {
  64. present = true
  65. }
  66. }
  67. return
  68. }