2020-02-12 20:53:55 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & 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 monitor provides facilities for monitoring network
|
2020-07-06 23:36:57 +00:00
|
|
|
// interface and route changes. It primarily exists to know when
|
|
|
|
// portable devices move between different networks.
|
2020-02-12 20:53:55 +00:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
2020-02-17 17:00:38 +00:00
|
|
|
"sync"
|
2020-02-12 20:53:55 +00:00
|
|
|
"time"
|
|
|
|
|
2020-02-15 03:23:16 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-02-12 20:53:55 +00:00
|
|
|
)
|
|
|
|
|
2020-02-17 17:00:38 +00:00
|
|
|
// message represents a message returned from an osMon.
|
2020-07-06 23:36:57 +00:00
|
|
|
type message interface {
|
|
|
|
// Ignore is whether we should ignore this message.
|
|
|
|
ignore() bool
|
|
|
|
}
|
2020-02-12 20:53:55 +00:00
|
|
|
|
2020-07-07 17:44:54 +00:00
|
|
|
// unspecifiedMessage is a minimal message implementation that should not
|
|
|
|
// be ignored. In general, OS-specific implementations should use better
|
|
|
|
// types and avoid this if they can.
|
|
|
|
type unspecifiedMessage struct{}
|
|
|
|
|
|
|
|
func (unspecifiedMessage) ignore() bool { return false }
|
|
|
|
|
2020-02-17 17:00:38 +00:00
|
|
|
// osMon is the interface that each operating system-specific
|
|
|
|
// implementation of the link monitor must implement.
|
|
|
|
type osMon interface {
|
2020-02-12 20:53:55 +00:00
|
|
|
Close() error
|
2020-02-17 17:00:38 +00:00
|
|
|
|
|
|
|
// Receive returns a new network interface change message. It
|
|
|
|
// should block until there's either something to return, or
|
|
|
|
// until the osMon is closed. After a Close, the returned
|
|
|
|
// error is ignored.
|
|
|
|
Receive() (message, error)
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeFunc is a callback function that's called when
|
|
|
|
// an interface status changes.
|
|
|
|
type ChangeFunc func()
|
|
|
|
|
|
|
|
// Mon represents a monitoring instance.
|
|
|
|
type Mon struct {
|
|
|
|
logf logger.Logf
|
|
|
|
cb ChangeFunc
|
2020-02-17 17:00:38 +00:00
|
|
|
om osMon // nil means not supported on this platform
|
2020-02-12 20:53:55 +00:00
|
|
|
change chan struct{}
|
|
|
|
stop chan struct{}
|
2020-02-17 17:00:38 +00:00
|
|
|
|
|
|
|
onceStart sync.Once
|
|
|
|
started bool
|
|
|
|
goroutines sync.WaitGroup
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New instantiates and starts a monitoring instance. Change notifications
|
|
|
|
// are propagated to the callback function.
|
2020-02-17 17:00:38 +00:00
|
|
|
// The returned monitor is inactive until it's started by the Start method.
|
2020-02-12 20:53:55 +00:00
|
|
|
func New(logf logger.Logf, callback ChangeFunc) (*Mon, error) {
|
2020-07-06 23:36:57 +00:00
|
|
|
logf = logger.WithPrefix(logf, "monitor: ")
|
|
|
|
om, err := newOSMon(logf)
|
2020-02-12 20:53:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-17 17:00:38 +00:00
|
|
|
return &Mon{
|
2020-02-12 20:53:55 +00:00
|
|
|
logf: logf,
|
|
|
|
cb: callback,
|
2020-02-17 17:00:38 +00:00
|
|
|
om: om,
|
2020-02-12 20:53:55 +00:00
|
|
|
change: make(chan struct{}, 1),
|
|
|
|
stop: make(chan struct{}),
|
2020-02-17 17:00:38 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the monitor.
|
|
|
|
// A monitor can only be started & closed once.
|
|
|
|
func (m *Mon) Start() {
|
|
|
|
m.onceStart.Do(func() {
|
|
|
|
if m.om == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.started = true
|
|
|
|
m.goroutines.Add(2)
|
|
|
|
go m.pump()
|
|
|
|
go m.debounce()
|
|
|
|
})
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 17:00:38 +00:00
|
|
|
// Close closes the monitor.
|
|
|
|
// It may only be called once.
|
2020-02-12 20:53:55 +00:00
|
|
|
func (m *Mon) Close() error {
|
|
|
|
close(m.stop)
|
2020-02-17 17:00:38 +00:00
|
|
|
var err error
|
|
|
|
if m.om != nil {
|
|
|
|
err = m.om.Close()
|
|
|
|
}
|
|
|
|
// If it was previously started, wait for those goroutines to finish.
|
|
|
|
m.onceStart.Do(func() {})
|
|
|
|
if m.started {
|
|
|
|
m.goroutines.Wait()
|
|
|
|
}
|
|
|
|
return err
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pump continuously retrieves messages from the connection, notifying
|
|
|
|
// the change channel of changes, and stopping when a stop is issued.
|
|
|
|
func (m *Mon) pump() {
|
2020-02-17 17:00:38 +00:00
|
|
|
defer m.goroutines.Done()
|
2020-02-12 20:53:55 +00:00
|
|
|
for {
|
2020-07-06 23:36:57 +00:00
|
|
|
msg, err := m.om.Receive()
|
2020-02-12 20:53:55 +00:00
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case <-m.stop:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
// Keep retrying while we're not closed.
|
2020-07-06 23:36:57 +00:00
|
|
|
m.logf("error receiving from connection: %v", err)
|
2020-02-12 20:53:55 +00:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2020-07-06 23:36:57 +00:00
|
|
|
if msg.ignore() {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-12 20:53:55 +00:00
|
|
|
select {
|
|
|
|
case m.change <- struct{}{}:
|
2020-03-10 18:02:30 +00:00
|
|
|
case <-m.stop:
|
|
|
|
return
|
2020-02-12 20:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// debounce calls the callback function with a delay between events
|
|
|
|
// and exits when a stop is issued.
|
|
|
|
func (m *Mon) debounce() {
|
2020-02-17 17:00:38 +00:00
|
|
|
defer m.goroutines.Done()
|
2020-02-12 20:53:55 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.stop:
|
|
|
|
return
|
|
|
|
case <-m.change:
|
|
|
|
}
|
|
|
|
|
|
|
|
m.cb()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-m.stop:
|
|
|
|
return
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|