-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
route: fix RTM_GET netmask parsing on Darwin
On Darwin, the AF_FAMILY byte of a sockaddr for a netmask or genmask can be ignored if unreasonable. In such cases, it is the family of the DST address that should instead be used. Additionally, fixing faulty test data. 192.168.86.0 is a Class C network address, that should have a subnet mask of 255.255.255.0. What's more is the data can also be flag as incorrect considering structure padding rules alone. Further more, you can validate that `route get` will never actually return a netmask for a host query, even though it should be 255.255.255.255. You can run the following to check: route -n get -host 127.0.0.1 You will note the reply has no mention of netmask. Depends on CL 646556 - https://go.dev/cl/646556 Fixes golang/go#71578. Change-Id: Id95669b649a416a380d26c5cdba0e3d1c4bc1ffb GitHub-Last-Rev: 20064b2 GitHub-Pull-Request: #232 Reviewed-on: https://go-review.googlesource.com/c/net/+/647176 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Commit-Queue: Ian Lance Taylor <[email protected]> Reviewed-by: Damien Neil <[email protected]>
- Loading branch information
1 parent
df97a48
commit cd9d661
Showing
3 changed files
with
84 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2025 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package route_test | ||
|
||
import ( | ||
"fmt" | ||
"net/netip" | ||
"os" | ||
"syscall" | ||
|
||
"golang.org/x/net/route" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// This example demonstrates how to parse a response to RTM_GET request. | ||
func ExampleParseRIB() { | ||
fd, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC) | ||
if err != nil { | ||
return | ||
} | ||
defer unix.Close(fd) | ||
|
||
// Create a RouteMessage with RTM_GET type | ||
rtm := &route.RouteMessage{ | ||
Version: syscall.RTM_VERSION, | ||
Type: unix.RTM_GET, | ||
ID: uintptr(os.Getpid()), | ||
Seq: 0, | ||
Addrs: []route.Addr{ | ||
&route.Inet4Addr{IP: [4]byte{127, 0, 0, 0}}, | ||
}, | ||
} | ||
|
||
// Marshal the message into bytes | ||
msgBytes, err := rtm.Marshal() | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Send the message over the routing socket | ||
_, err = unix.Write(fd, msgBytes) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Read the response from the routing socket | ||
var buf [2 << 10]byte | ||
n, err := unix.Read(fd, buf[:]) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Parse the response messages | ||
msgs, err := route.ParseRIB(route.RIBTypeRoute, buf[:n]) | ||
if err != nil { | ||
return | ||
} | ||
routeMsg, ok := msgs[0].(*route.RouteMessage) | ||
if !ok { | ||
return | ||
} | ||
netmask, ok := routeMsg.Addrs[2].(*route.Inet4Addr) | ||
if !ok { | ||
return | ||
} | ||
fmt.Println(netip.AddrFrom4(netmask.IP)) | ||
// Output: 255.0.0.0 | ||
} |