-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathgateway.go
52 lines (41 loc) · 1.36 KB
/
gateway.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package gateway
import (
"fmt"
"net"
"runtime"
)
// ErrNoGateway is returned if a valid gateway entry was not
// found in the route table.
type ErrNoGateway struct{}
// ErrCantParse is returned if the route table is garbage.
type ErrCantParse struct{}
// ErrNotImplemented is returned if your operating system
// is not supported by this package. Please raise an issue
// to request support.
type ErrNotImplemented struct{}
// ErrInvalidRouteFileFormat is returned if the format
// of /proc/net/route is unexpected on Linux systems.
// Please raise an issue.
type ErrInvalidRouteFileFormat struct {
row string
}
func (*ErrNoGateway) Error() string {
return "no gateway found"
}
func (*ErrCantParse) Error() string {
return "can't parse route table"
}
func (*ErrNotImplemented) Error() string {
return "not implemented for OS: " + runtime.GOOS
}
func (e *ErrInvalidRouteFileFormat) Error() string {
return fmt.Sprintf("invalid row %q in route file: doesn't have 11 fields", e.row)
}
// DiscoverGateway is the OS independent function to get the default gateway
func DiscoverGateway() (ip net.IP, err error) {
return discoverGatewayOSSpecific()
}
// DiscoverInterface is the OS independent function to call to get the default network interface IP that uses the default gateway
func DiscoverInterface() (ip net.IP, err error) {
return discoverGatewayInterfaceOSSpecific()
}