route: fix address parsing of messages on Darwin

sizeofSockaddrInet is 16, but first byte of sockaddr specifies actual
size of sockaddr.

Although, 16 works for most cases, it fails for Netmasks addresses. On
Darwin only the significant bits of the netmask are in the msg.

Take this route message as an example

```
// rt_msg_hdr
88 00 05 01 00 00 00 00
41 08 00 00 07 00 00 00
92 7b 00 00 01 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00

// metrics
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

// SOCKADDRS - DST  (100.113.0.0)
10 02 00 00 64 71 00 00
00 00 00 00 00 00 00 00

// GW  utun4319
14 12 21 00 01 08 00 00
75 74 75 6e 34 33 31 39
00 00 00 00

// NETMASK 255.255.0.0
06 02 00 00 ff ff

// NULL
00 00
```

i.e. ipv4
```
06 02 00 00 ff ff
```

The above byte sequence is for a sockaddr that is 6 bytes long
representing an ipv4 for address that is 255.255.0.0.

i.e. ipv6 netmask
```
0e 1e 00 00 00 00 00 00 ff ff ff ff ff ff 00 00
```

The above is `/48` netmask that should also be parsed using `b[0]` of the
sockaddr that contains the length.

Confirmed by using `route monitor`.

sources:
https://github.com/apple/darwin-xnu/blob/main/bsd/net/route.h
https://github.com/apple/darwin-xnu/blob/main/bsd/sys/socket.h#L603

Fixes golang/go#44740

Change-Id: I8153130d02d0a5e547fbf60a85762d3889e1d08c
GitHub-Last-Rev: f7b9253061ab7daea8c04833f30bb60699c205f1
GitHub-Pull-Request: golang/net#220
Reviewed-on: https://go-review.googlesource.com/c/net/+/609577
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
2 files changed
tree: 9376bbc485a7ced46e1b05734302ee59c7be962d
  1. bpf/
  2. context/
  3. dict/
  4. dns/
  5. html/
  6. http/
  7. http2/
  8. icmp/
  9. idna/
  10. internal/
  11. ipv4/
  12. ipv6/
  13. lif/
  14. nettest/
  15. netutil/
  16. proxy/
  17. publicsuffix/
  18. quic/
  19. route/
  20. trace/
  21. webdav/
  22. websocket/
  23. xsrftoken/
  24. .gitattributes
  25. .gitignore
  26. codereview.cfg
  27. CONTRIBUTING.md
  28. go.mod
  29. go.sum
  30. LICENSE
  31. PATENTS
  32. README.md
README.md

Go Networking

Go Reference

This repository holds supplementary Go networking libraries.

Download/Install

The easiest way to install is to run go get -u golang.org/x/net. You can also manually git clone the repository to $GOPATH/src/golang.org/x/net.

Report Issues / Send Patches

This repository uses Gerrit for code changes. To learn how to submit changes to this repository, see https://golang.org/doc/contribute.html. The main issue tracker for the net repository is located at https://github.com/golang/go/issues. Prefix your issue with “x/net:” in the subject line, so it is easy to find.