Skip to content

Commit

Permalink
Only disestablish relay on last HostInfo delete, and ensure relay con…
Browse files Browse the repository at this point in the history
…nection is 'Estabslished' on successful handshake receipt
  • Loading branch information
brad-defined committed Nov 13, 2024
1 parent 1b61b5e commit 86dfbb1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
3 changes: 3 additions & 0 deletions handshake_ix.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ func ixHandshakeStage1(f *Interface, addr netip.AddrPort, via *ViaSender, packet
return
}
hostinfo.relayState.InsertRelayTo(via.relayHI.vpnAddrs[0])
// I successfully received a handshake. Just in case I marked this tunnel as 'Disestablished', ensure
// it's correctly marked as working.
hostinfo.relayState.UpdateRelayForByIdxState(via.remoteIdx, Established)
f.SendVia(via.relayHI, via.relay, msg, make([]byte, 12), make([]byte, mtu), false)
f.l.WithField("vpnAddrs", vpnAddrs).WithField("relay", via.relayHI.vpnAddrs[0]).
WithField("certName", certName).
Expand Down
17 changes: 14 additions & 3 deletions hostmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ func (rs *RelayState) UpdateRelayForByIpState(vpnIp netip.Addr, state int) {
}
}

func (rs *RelayState) UpdateRelayForByIdxState(idx uint32, state int) {
rs.Lock()
defer rs.Unlock()
if r, ok := rs.relayForByIdx[idx]; ok {
newRelay := *r
newRelay.State = state
rs.relayForByAddr[newRelay.PeerAddr] = &newRelay
rs.relayForByIdx[newRelay.LocalIndex] = &newRelay
}
}

func (rs *RelayState) CopyAllRelayFor() []*Relay {
rs.RLock()
defer rs.RUnlock()
Expand Down Expand Up @@ -378,8 +389,8 @@ func (hm *HostMap) unlockedDeleteHostInfo(hostinfo *HostInfo) {

func (hm *HostMap) unlockedInnerDeleteHostInfo(hostinfo *HostInfo, addr netip.Addr) {
primary, ok := hm.Hosts[addr]
isPrimary := primary == hostinfo
if ok && isPrimary {
isLastHostinfo := hostinfo.next == nil && hostinfo.prev == nil
if ok && primary == hostinfo {
// The vpn addr pointer points to the same hostinfo as the local index id, we can remove it
delete(hm.Hosts, addr)
if len(hm.Hosts) == 0 {
Expand Down Expand Up @@ -428,7 +439,7 @@ func (hm *HostMap) unlockedInnerDeleteHostInfo(hostinfo *HostInfo, addr netip.Ad
Debug("Hostmap hostInfo deleted")
}

if isPrimary {
if isLastHostinfo {
// I have lost connectivity to my peers. My relay tunnel is likely broken. Mark the next
// hops as 'Requested' so that new relay tunnels are created in the future.
hm.unlockedDisestablishVpnAddrRelayFor(hostinfo)
Expand Down

0 comments on commit 86dfbb1

Please sign in to comment.