|
| 1 | +//go:build darwin |
| 2 | + |
| 3 | +package libbox |
| 4 | + |
| 5 | +import ( |
| 6 | + "net" |
| 7 | + "net/netip" |
| 8 | + "os" |
| 9 | + "slices" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/sagernet/sing-box/route" |
| 13 | + "github.com/sagernet/sing/common/buf" |
| 14 | + E "github.com/sagernet/sing/common/exceptions" |
| 15 | + |
| 16 | + xroute "golang.org/x/net/route" |
| 17 | + "golang.org/x/sys/unix" |
| 18 | +) |
| 19 | + |
| 20 | +func SubscribeNeighborTable(listener NeighborUpdateListener) (*NeighborSubscription, error) { |
| 21 | + entries, err := route.ReadNeighborEntries() |
| 22 | + if err != nil { |
| 23 | + return nil, E.Cause(err, "initial neighbor dump") |
| 24 | + } |
| 25 | + table := make(map[netip.Addr]net.HardwareAddr) |
| 26 | + for _, entry := range entries { |
| 27 | + table[entry.Address] = entry.MACAddress |
| 28 | + } |
| 29 | + listener.UpdateNeighborTable(tableToIterator(table)) |
| 30 | + routeSocket, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, 0) |
| 31 | + if err != nil { |
| 32 | + return nil, E.Cause(err, "open route socket") |
| 33 | + } |
| 34 | + err = unix.SetNonblock(routeSocket, true) |
| 35 | + if err != nil { |
| 36 | + unix.Close(routeSocket) |
| 37 | + return nil, E.Cause(err, "set route socket nonblock") |
| 38 | + } |
| 39 | + subscription := &NeighborSubscription{ |
| 40 | + done: make(chan struct{}), |
| 41 | + } |
| 42 | + go subscription.loop(listener, routeSocket, table) |
| 43 | + return subscription, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (s *NeighborSubscription) loop(listener NeighborUpdateListener, routeSocket int, table map[netip.Addr]net.HardwareAddr) { |
| 47 | + routeSocketFile := os.NewFile(uintptr(routeSocket), "route") |
| 48 | + defer routeSocketFile.Close() |
| 49 | + buffer := buf.NewPacket() |
| 50 | + defer buffer.Release() |
| 51 | + for { |
| 52 | + select { |
| 53 | + case <-s.done: |
| 54 | + return |
| 55 | + default: |
| 56 | + } |
| 57 | + tv := unix.NsecToTimeval(int64(3 * time.Second)) |
| 58 | + _ = unix.SetsockoptTimeval(routeSocket, unix.SOL_SOCKET, unix.SO_RCVTIMEO, &tv) |
| 59 | + n, err := routeSocketFile.Read(buffer.FreeBytes()) |
| 60 | + if err != nil { |
| 61 | + if nerr, ok := err.(net.Error); ok && nerr.Timeout() { |
| 62 | + continue |
| 63 | + } |
| 64 | + select { |
| 65 | + case <-s.done: |
| 66 | + return |
| 67 | + default: |
| 68 | + } |
| 69 | + continue |
| 70 | + } |
| 71 | + messages, err := xroute.ParseRIB(xroute.RIBTypeRoute, buffer.FreeBytes()[:n]) |
| 72 | + if err != nil { |
| 73 | + continue |
| 74 | + } |
| 75 | + changed := false |
| 76 | + for _, message := range messages { |
| 77 | + routeMessage, isRouteMessage := message.(*xroute.RouteMessage) |
| 78 | + if !isRouteMessage { |
| 79 | + continue |
| 80 | + } |
| 81 | + if routeMessage.Flags&unix.RTF_LLINFO == 0 { |
| 82 | + continue |
| 83 | + } |
| 84 | + address, mac, isDelete, ok := route.ParseRouteNeighborMessage(routeMessage) |
| 85 | + if !ok { |
| 86 | + continue |
| 87 | + } |
| 88 | + if isDelete { |
| 89 | + if _, exists := table[address]; exists { |
| 90 | + delete(table, address) |
| 91 | + changed = true |
| 92 | + } |
| 93 | + } else { |
| 94 | + existing, exists := table[address] |
| 95 | + if !exists || !slices.Equal(existing, mac) { |
| 96 | + table[address] = mac |
| 97 | + changed = true |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + if changed { |
| 102 | + listener.UpdateNeighborTable(tableToIterator(table)) |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func ReadBootpdLeases() NeighborEntryIterator { |
| 108 | + leaseIPToMAC, ipToHostname, macToHostname := route.ReloadLeaseFiles([]string{"/var/db/dhcpd_leases"}) |
| 109 | + entries := make([]*NeighborEntry, 0, len(leaseIPToMAC)) |
| 110 | + for address, mac := range leaseIPToMAC { |
| 111 | + entry := &NeighborEntry{ |
| 112 | + Address: address.String(), |
| 113 | + MacAddress: mac.String(), |
| 114 | + } |
| 115 | + hostname, found := ipToHostname[address] |
| 116 | + if !found { |
| 117 | + hostname = macToHostname[mac.String()] |
| 118 | + } |
| 119 | + entry.Hostname = hostname |
| 120 | + entries = append(entries, entry) |
| 121 | + } |
| 122 | + return &neighborEntryIterator{entries} |
| 123 | +} |
0 commit comments