net/interfaces: add IsExpensive and up state to State

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/277/head
Brad Fitzpatrick 4 years ago committed by Brad Fitzpatrick
parent a55c4217db
commit c0697e1feb

@ -174,6 +174,12 @@ func ForeachInterfaceAddress(fn func(Interface, net.IP)) error {
// For now it's pretty basic.
type State struct {
InterfaceIPs map[string][]net.IP
InterfaceUp map[string]bool
// IsExpensive is whether the current network interface is
// considered "expensive", which currently means LTE/etc
// instead of Wifi. This field is not populated by GetState.
IsExpensive bool
}
func (s *State) Equal(s2 *State) bool {
@ -188,14 +194,22 @@ func (s *State) RemoveTailscaleInterfaces() {
for name := range s.InterfaceIPs {
if strings.HasPrefix(name, "tailscale") { // TODO: use --tun flag value, etc; see TODO in method doc
delete(s.InterfaceIPs, name)
delete(s.InterfaceUp, name)
}
}
}
// GetState returns the state of all the current machine's network interfaces.
//
// It does not set the returned State.IsExpensive. The caller can populate that.
func GetState() (*State, error) {
s := &State{InterfaceIPs: make(map[string][]net.IP)}
s := &State{
InterfaceIPs: make(map[string][]net.IP),
InterfaceUp: make(map[string]bool),
}
if err := ForeachInterfaceAddress(func(ni Interface, ip net.IP) {
s.InterfaceIPs[ni.Name] = append(s.InterfaceIPs[ni.Name], ip)
s.InterfaceUp[ni.Name] = ni.IsUp()
}); err != nil {
return nil, err
}

@ -27,5 +27,23 @@ func TestIsTailscaleIP(t *testing.T) {
t.Errorf("F(%q) = %v; want %v", tt.ip, got, tt.want)
}
}
}
func TestGetState(t *testing.T) {
st, err := GetState()
if err != nil {
t.Fatal(err)
}
t.Logf("Got: %#v", st)
st2, err := GetState()
if err != nil {
t.Fatal(err)
}
if !st.Equal(st2) {
// let's assume nobody was changing the system network interfaces between
// the two GetState calls.
t.Fatal("two States back-to-back were not equal")
}
}

@ -644,6 +644,7 @@ func (e *userspaceEngine) LinkChange(isExpensive bool) {
e.logf("LinkChange: interfaces.GetState: %v", err)
return
}
cur.IsExpensive = isExpensive
needRebind := e.setLinkState(cur)
e.logf("LinkChange(isExpensive=%v); needsRebind=%v", isExpensive, needRebind)

Loading…
Cancel
Save