Skip to content

Commit

Permalink
vpn/link: ignore some casefail on darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
rkonfj committed Jul 11, 2024
1 parent aec3419 commit 6079a9f
Showing 1 changed file with 65 additions and 43 deletions.
108 changes: 65 additions & 43 deletions vpn/link/route_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,7 @@ func RouteSubscribe(ctx context.Context, ch chan<- RouteUpdate) error {
return fmt.Errorf("syscall socket: %w", err)
}
go func() {
buf := make([]byte, os.Getpagesize())
err := func() error {
for {
_, err := syscall.Read(fd, buf)
if err != nil {
return fmt.Errorf("syscall read: %w", err)
}
msg := buf[:176]
msg[0] = 176
msgs, err := route.ParseRIB(route.RIBTypeRoute, msg)
if err != nil {
return fmt.Errorf("route parseRIB: %w", err)
}
for _, msg := range msgs {
addrs := msg.(*route.RouteMessage).Addrs
if len(addrs) < 3 {
continue
}

var dst, via, mask []byte

if ip, ok := addrs[0].(*route.Inet4Addr); ok {
dst = ip.IP[:]
via = addrs[1].(*route.Inet4Addr).IP[:]
mask = addrs[2].(*route.Inet4Addr).IP[:]
} else if ip, ok := addrs[0].(*route.Inet6Addr); ok {
dst = ip.IP[:]
via = addrs[1].(*route.Inet6Addr).IP[:]
mask = addrs[2].(*route.Inet6Addr).IP[:]
} else {
continue
}
ch <- RouteUpdate{
Type: uint16(buf[3]),
Dst: &net.IPNet{
IP: dst,
Mask: mask[:],
},
Via: via,
}
}
}
}()
err := runReadLoop(fd, ch)
if err != nil {
slog.Error("SyscallReadExited", "err", err)
}
Expand All @@ -71,3 +29,67 @@ func RouteSubscribe(ctx context.Context, ch chan<- RouteUpdate) error {
}()
return nil
}

func runReadLoop(fd int, ch chan<- RouteUpdate) error {
buf := make([]byte, os.Getpagesize())
for {
_, err := syscall.Read(fd, buf)
if err != nil {
return fmt.Errorf("syscall read: %w", err)
}
msg := buf[:176]
msg[0] = 176
msgs, err := route.ParseRIB(route.RIBTypeRoute, msg)
if err != nil {
return fmt.Errorf("route parseRIB: %w", err)
}
for _, msg := range msgs {
m, ok := msg.(*route.RouteMessage)
if !ok {
continue
}
addrs := m.Addrs
if len(addrs) < 3 {
continue
}

var dst, via, mask []byte

if ip, ok := addrs[0].(*route.Inet4Addr); ok {
dst = ip.IP[:]
addr, ok := addrs[1].(*route.Inet4Addr)
if !ok {
continue
}
via = addr.IP[:]
addr, ok = addrs[2].(*route.Inet4Addr)
if !ok {
continue
}
mask = addr.IP[:]
} else if ip, ok := addrs[0].(*route.Inet6Addr); ok {
dst = ip.IP[:]
addr, ok := addrs[1].(*route.Inet6Addr)
if !ok {
continue
}
via = addr.IP[:]
addr, ok = addrs[2].(*route.Inet6Addr)
if !ok {
continue
}
mask = addr.IP[:]
} else {
continue
}
ch <- RouteUpdate{
Type: uint16(buf[3]),
Dst: &net.IPNet{
IP: dst,
Mask: mask[:],
},
Via: via,
}
}
}
}

0 comments on commit 6079a9f

Please sign in to comment.