Skip to content

Commit

Permalink
suppress localhost from breaking multicast discovery w/o prefs
Browse files Browse the repository at this point in the history
  • Loading branch information
dpwiz committed Nov 27, 2023
1 parent cbd6efa commit 12a701b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Simplex/RemoteControl/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ connectRCHost drg pairing@RCHostPairing {caKey, caCert, idPrivKey, knownHost} ct
where
findCtrlAddress :: ExceptT RCErrorType IO (NonEmpty RCCtrlAddress, RCCtrlAddress)
findCtrlAddress = do
(matched_, found') <- getLocalAddress rcAddrPrefs_
(found', matched_) <- getLocalAddress rcAddrPrefs_
found <- maybe (throwError RCENoLocalAddress) pure $ L.nonEmpty found'
let selected = fromMaybe (L.head found) matched_
pure (found, selected)
Expand Down
13 changes: 11 additions & 2 deletions src/Simplex/RemoteControl/Discovery.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ pattern ANY_ADDR_V4 = "0.0.0.0"
pattern DISCOVERY_PORT :: (IsString a, Eq a) => a
pattern DISCOVERY_PORT = "5227"

getLocalAddress :: MonadIO m => Maybe RCCtrlAddress -> m (Maybe RCCtrlAddress, [RCCtrlAddress])
getLocalAddress :: MonadIO m => Maybe RCCtrlAddress -> m ([RCCtrlAddress], Maybe RCCtrlAddress)
getLocalAddress preferred_ = do
found <- mapMaybe toCtrlAddr <$> liftIO getNetworkInterfaces
pure (byAddr found <|> byIface found, found)
let selected = byAddr found <|> byIface found
let localhost = L.find (\RCCtrlAddress {address = a} -> a == THIPv4 (127, 0, 0, 1)) found
let prioritized = bringUp selected $ pushDown localhost found
pure (prioritized, selected)
where
toCtrlAddr NetworkInterface {name, ipv4 = IPv4 ha} = case N.hostAddressToTuple ha of
(0, 0, 0, 0) -> Nothing -- "no" address
Expand All @@ -61,6 +64,12 @@ getLocalAddress preferred_ = do
byIface found = do
RCCtrlAddress {interface = p} <- preferred_
L.find (\RCCtrlAddress {interface = a} -> a == p) found
bringUp x_ xs = case x_ of
Nothing -> xs
Just x -> x : L.delete x xs
pushDown x_ xs = case x_ of
Nothing -> xs
Just x -> L.delete x xs <> [x]

getLocalAddressMulticast :: MonadIO m => TMVar Int -> m (Maybe TransportHost)
getLocalAddressMulticast subscribers = liftIO $ do
Expand Down
2 changes: 1 addition & 1 deletion src/Simplex/RemoteControl/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ data RCCtrlAddress = RCCtrlAddress
{ address :: TransportHost, -- allows any interface when found exactly
interface :: Text
}
deriving (Show)
deriving (Show, Eq)

-- | Long-term part of host (mobile) connection to controller (desktop)
data RCCtrlPairing = RCCtrlPairing
Expand Down

0 comments on commit 12a701b

Please sign in to comment.