-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefaultroute.go
48 lines (38 loc) · 1.33 KB
/
defaultroute.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
// Copyright (c) Tailscale Inc & AUTHORS
// Copyright (c) Erik Junsved
// SPDX-License-Identifier: BSD-3-Clause
// Package defaultroute contains helpers for looking up system network interfaces.
package defaultroute
import "net"
// DefaultRouteDetails are the details about a default route returned
// by DefaultRoute.
type DefaultRouteDetails struct {
// InterfaceName is the interface name. It must always be populated.
// It's like "eth0" (Linux), "Ethernet 2" (Windows), "en0" (macOS).
InterfaceName string
// InterfaceDesc is populated on Windows at least. It's a
// longer description, like "Red Hat VirtIO Ethernet Adapter".
InterfaceDesc string
// InterfaceIndex is like net.Interface.Index.
// Zero means not populated.
InterfaceIndex int
// TODO(bradfitz): break this out into v4-vs-v6 once that need arises.
}
// DefaultRouteInterface is like DefaultRoute but only returns the
// interface name.
func DefaultRouteInterface() (*net.Interface, error) {
dr, err := DefaultRoute()
if err != nil {
return nil, err
}
interf, err := net.InterfaceByName(dr.InterfaceName)
if err != nil {
return nil, err
}
return interf, nil
}
// DefaultRoute returns details of the network interface that owns
// the default route, not including any tailscale interfaces.
func DefaultRoute() (DefaultRouteDetails, error) {
return defaultRoute()
}