@ -120,11 +120,9 @@ type userspaceEngine struct {
mu sync . Mutex // guards following; see lock order comment below
mu sync . Mutex // guards following; see lock order comment below
closing bool // Close was called (even if we're still closing)
closing bool // Close was called (even if we're still closing)
statusCallback StatusCallback
statusCallback StatusCallback
linkChangeCallback func ( major bool , newState * interfaces . State )
peerSequence [ ] wgkey . Key
peerSequence [ ] wgkey . Key
endpoints [ ] string
endpoints [ ] string
pingers map [ wgkey . Key ] * pinger // legacy pingers for pre-discovery peers
pingers map [ wgkey . Key ] * pinger // legacy pingers for pre-discovery peers
linkState * interfaces . State
pendOpen map [ flowtrack . Tuple ] * pendingOpenFlow // see pendopen.go
pendOpen map [ flowtrack . Tuple ] * pendingOpenFlow // see pendopen.go
networkMapCallbacks map [ * someHandle ] NetworkMapCallback
networkMapCallbacks map [ * someHandle ] NetworkMapCallback
@ -248,12 +246,11 @@ func newUserspaceEngine(logf logger.Logf, rawTUNDev tun.Device, conf Config) (_
e . linkMonOwned = true
e . linkMonOwned = true
}
}
e . linkState , _ = e . linkMon . InterfaceState ( )
logf ( "link state: %+v" , e . linkMon . InterfaceState ( ) )
logf ( "link state: %+v" , e . linkState )
unregisterMonWatch := e . linkMon . RegisterChangeCallback ( func ( ) {
unregisterMonWatch := e . linkMon . RegisterChangeCallback ( func ( changed bool , st * interfaces . State ) {
e . LinkChange ( false )
tshttpproxy . InvalidateCache ( )
tshttpproxy . InvalidateCache ( )
e . linkChange ( false , st )
} )
} )
closePool . addFunc ( unregisterMonWatch )
closePool . addFunc ( unregisterMonWatch )
e . linkMonUnregister = unregisterMonWatch
e . linkMonUnregister = unregisterMonWatch
@ -279,7 +276,7 @@ func newUserspaceEngine(logf logger.Logf, rawTUNDev tun.Device, conf Config) (_
return nil , fmt . Errorf ( "wgengine: %v" , err )
return nil , fmt . Errorf ( "wgengine: %v" , err )
}
}
closePool . add ( e . magicConn )
closePool . add ( e . magicConn )
e . magicConn . SetNetworkUp ( e . link State. AnyInterfaceUp ( ) )
e . magicConn . SetNetworkUp ( e . link Mon. Interface State( ) . AnyInterfaceUp ( ) )
// Respond to all pings only in fake mode.
// Respond to all pings only in fake mode.
if conf . Fake {
if conf . Fake {
@ -1250,30 +1247,15 @@ func (e *userspaceEngine) Wait() {
<- e . waitCh
<- e . waitCh
}
}
func ( e * userspaceEngine ) setLinkState ( st * interfaces . State ) ( changed bool , cb func ( major bool , newState * interfaces . State ) ) {
func ( e * userspaceEngine ) GetLinkMonitor ( ) * monitor . Mon {
if st == nil {
return e . linkMon
return false , nil
}
e . mu . Lock ( )
defer e . mu . Unlock ( )
changed = e . linkState == nil || ! st . Equal ( e . linkState )
e . linkState = st
return changed , e . linkChangeCallback
}
}
func ( e * userspaceEngine ) LinkChange ( isExpensive bool ) {
func ( e * userspaceEngine ) linkChange ( changed bool , cur * interfaces . State ) {
cur , err := e . linkMon . InterfaceState ( )
if err != nil {
e . logf ( "LinkChange: interfaces.GetState: %v" , err )
return
}
cur . IsExpensive = isExpensive
needRebind , linkChangeCallback := e . setLinkState ( cur )
up := cur . AnyInterfaceUp ( )
up := cur . AnyInterfaceUp ( )
if ! up {
if ! up {
e . logf ( "LinkChange: all links down; pausing: %v" , cur )
e . logf ( "LinkChange: all links down; pausing: %v" , cur )
} else if needRebin d {
} else if changed {
e . logf ( "LinkChange: major, rebinding. New state: %v" , cur )
e . logf ( "LinkChange: major, rebinding. New state: %v" , cur )
} else {
} else {
e . logf ( "[v1] LinkChange: minor" )
e . logf ( "[v1] LinkChange: minor" )
@ -1282,23 +1264,11 @@ func (e *userspaceEngine) LinkChange(isExpensive bool) {
e . magicConn . SetNetworkUp ( up )
e . magicConn . SetNetworkUp ( up )
why := "link-change-minor"
why := "link-change-minor"
if needRebin d {
if change d {
why = "link-change-major"
why = "link-change-major"
e . magicConn . Rebind ( )
e . magicConn . Rebind ( )
}
}
e . magicConn . ReSTUN ( why )
e . magicConn . ReSTUN ( why )
if linkChangeCallback != nil {
go linkChangeCallback ( needRebind , cur )
}
}
func ( e * userspaceEngine ) SetLinkChangeCallback ( cb func ( major bool , newState * interfaces . State ) ) {
e . mu . Lock ( )
defer e . mu . Unlock ( )
e . linkChangeCallback = cb
if e . linkState != nil {
go cb ( false , e . linkState )
}
}
}
func ( e * userspaceEngine ) AddNetworkMapCallback ( cb NetworkMapCallback ) func ( ) {
func ( e * userspaceEngine ) AddNetworkMapCallback ( cb NetworkMapCallback ) func ( ) {