Skip to content

Commit 82c780b

Browse files
committed
change binary
1 parent dd39b5c commit 82c780b

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

gateway.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ func DiscoverGateway() (ip net.IP, err error) {
1818
}
1919

2020
// DiscoverInterface is the OS independent function to call to get the default network interface IP that uses the default gateway
21-
func DiscoverInterface() (ip net.IP, err error) {
21+
func DiscoverInterface() (ip *net.IPNet, err error) {
2222
return discoverGatewayInterfaceOSSpecific()
2323
}

gateway_linux.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build linux
12
// +build linux
23

34
package gateway
@@ -28,7 +29,7 @@ func discoverGatewayOSSpecific() (ip net.IP, err error) {
2829
return parseLinuxGatewayIP(bytes)
2930
}
3031

31-
func discoverGatewayInterfaceOSSpecific() (ip net.IP, err error) {
32+
func discoverGatewayInterfaceOSSpecific() (ip *net.IPNet, err error) {
3233
f, err := os.Open(file)
3334
if err != nil {
3435
return nil, fmt.Errorf("Can't access %s", file)

gateway_parsers.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func parseLinuxGatewayIP(output []byte) (net.IP, error) {
199199
return net.IP(ipd32), nil
200200
}
201201

202-
func parseLinuxInterfaceIP(output []byte) (net.IP, error) {
202+
func parseLinuxInterfaceIP(output []byte) (*net.IPNet, error) {
203203
parsedStruct, err := parseToLinuxRouteStruct(output)
204204
if err != nil {
205205
return nil, err
@@ -216,12 +216,11 @@ func parseLinuxInterfaceIP(output []byte) (net.IP, error) {
216216
}
217217

218218
// split when its 192.168.8.8/24
219-
ipString := strings.Split(addrs[0].String(), "/")[0]
220-
ip := net.ParseIP(ipString)
221-
if ip == nil {
222-
return nil, fmt.Errorf("invalid addr %s", ipString)
219+
_, ipNet, err := net.ParseCIDR(addrs[0].String())
220+
if err != nil {
221+
return nil, err
223222
}
224-
return ip, nil
223+
return ipNet, nil
225224
}
226225

227226
func parseDarwinRouteGet(output []byte) (net.IP, error) {

t_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package gateway
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func Test(t *testing.T) {
9+
discoverInterface, err := DiscoverInterface()
10+
if err != nil {
11+
t.Fatal(err)
12+
}
13+
fmt.Println(discoverInterface)
14+
}

0 commit comments

Comments
 (0)