2012-09-08 19:21:34 +00:00
|
|
|
{- git-annex assistant pairing network code
|
2012-09-11 01:55:59 +00:00
|
|
|
-
|
|
|
|
- All network traffic is sent over multicast UDP. For reliability,
|
|
|
|
- each message is repeated until acknowledged. This is done using a
|
|
|
|
- thread, that gets stopped before the next message is sent.
|
2012-09-08 19:21:34 +00:00
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-09-08 19:21:34 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-09-08 19:21:34 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.Pairing.Network where
|
|
|
|
|
2012-09-11 01:55:59 +00:00
|
|
|
import Assistant.Common
|
2012-09-08 19:21:34 +00:00
|
|
|
import Assistant.Pairing
|
2012-09-11 01:55:59 +00:00
|
|
|
import Assistant.DaemonStatus
|
2012-09-08 19:21:34 +00:00
|
|
|
import Utility.ThreadScheduler
|
2012-09-11 01:55:59 +00:00
|
|
|
import Utility.Verifiable
|
2012-09-08 19:21:34 +00:00
|
|
|
|
|
|
|
import Network.Multicast
|
|
|
|
import Network.Info
|
|
|
|
import Network.Socket
|
2016-09-05 18:39:44 +00:00
|
|
|
import qualified Network.Socket.ByteString as B
|
|
|
|
import qualified Data.ByteString.UTF8 as BU8
|
2012-09-08 19:21:34 +00:00
|
|
|
import qualified Data.Map as M
|
2012-09-11 01:55:59 +00:00
|
|
|
import Control.Concurrent
|
2012-09-08 19:21:34 +00:00
|
|
|
|
|
|
|
{- This is an arbitrary port in the dynamic port range, that could
|
|
|
|
- conceivably be used for some other broadcast messages.
|
2023-03-14 02:39:16 +00:00
|
|
|
- If so, hope they ignore the garbage from us; we'll certainly
|
2012-09-08 19:21:34 +00:00
|
|
|
- ignore garbage from them. Wild wild west. -}
|
|
|
|
pairingPort :: PortNumber
|
|
|
|
pairingPort = 55556
|
|
|
|
|
2012-10-22 19:50:30 +00:00
|
|
|
{- Goal: Reach all hosts on the same network segment.
|
|
|
|
- Method: Use same address that avahi uses. Other broadcast addresses seem
|
|
|
|
- to not be let through some routers. -}
|
2015-04-19 04:38:29 +00:00
|
|
|
multicastAddress :: AddrClass -> HostName
|
|
|
|
multicastAddress IPv4AddrClass = "224.0.0.251"
|
|
|
|
multicastAddress IPv6AddrClass = "ff02::fb"
|
2012-09-08 19:21:34 +00:00
|
|
|
|
2012-09-11 01:55:59 +00:00
|
|
|
{- Multicasts a message repeatedly on all interfaces, with a 2 second
|
|
|
|
- delay between each transmission. The message is repeated forever
|
|
|
|
- unless a number of repeats is specified.
|
2012-09-08 19:21:34 +00:00
|
|
|
-
|
|
|
|
- The remoteHostAddress is set to the interface's IP address.
|
|
|
|
-
|
|
|
|
- Note that new sockets are opened each time. This is hardly efficient,
|
|
|
|
- but it allows new network interfaces to be used as they come up.
|
|
|
|
- On the other hand, the expensive DNS lookups are cached.
|
|
|
|
-}
|
2012-09-11 16:58:00 +00:00
|
|
|
multicastPairMsg :: Maybe Int -> Secret -> PairData -> PairStage -> IO ()
|
|
|
|
multicastPairMsg repeats secret pairdata stage = go M.empty repeats
|
2012-10-30 21:14:26 +00:00
|
|
|
where
|
|
|
|
go _ (Just 0) = noop
|
|
|
|
go cache n = do
|
|
|
|
addrs <- activeNetworkAddresses
|
|
|
|
let cache' = updatecache cache addrs
|
|
|
|
mapM_ (sendinterface cache') addrs
|
|
|
|
threadDelaySeconds (Seconds 2)
|
|
|
|
go cache' $ pred <$> n
|
|
|
|
{- The multicast library currently chokes on ipv6 addresses. -}
|
|
|
|
sendinterface _ (IPv6Addr _) = noop
|
2013-02-14 18:10:36 +00:00
|
|
|
sendinterface cache i = void $ tryIO $
|
2012-10-30 21:14:26 +00:00
|
|
|
withSocketsDo $ bracket setup cleanup use
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
2015-04-19 04:38:29 +00:00
|
|
|
setup = multicastSender (multicastAddress IPv4AddrClass) pairingPort
|
2016-09-05 18:39:44 +00:00
|
|
|
cleanup (sock, _) = close sock -- FIXME does not work
|
2012-10-31 06:34:03 +00:00
|
|
|
use (sock, addr) = do
|
|
|
|
setInterface sock (showAddr i)
|
2016-09-05 18:39:44 +00:00
|
|
|
maybe noop
|
|
|
|
(\s -> void $ B.sendTo sock (BU8.fromString s) addr)
|
2012-10-31 06:34:03 +00:00
|
|
|
(M.lookup i cache)
|
2012-10-30 21:14:26 +00:00
|
|
|
updatecache cache [] = cache
|
|
|
|
updatecache cache (i:is)
|
|
|
|
| M.member i cache = updatecache cache is
|
|
|
|
| otherwise = updatecache (M.insert i (show $ mkmsg i) cache) is
|
|
|
|
mkmsg addr = PairMsg $
|
|
|
|
mkVerifiable (stage, pairdata, addr) secret
|
2012-09-08 19:21:34 +00:00
|
|
|
|
2012-10-30 21:14:26 +00:00
|
|
|
startSending :: PairingInProgress -> PairStage -> (PairStage -> IO ()) -> Assistant ()
|
|
|
|
startSending pip stage sender = do
|
|
|
|
a <- asIO start
|
|
|
|
void $ liftIO $ forkIO a
|
|
|
|
where
|
|
|
|
start = do
|
|
|
|
tid <- liftIO myThreadId
|
|
|
|
let pip' = pip { inProgressPairStage = stage, inProgressThreadId = Just tid }
|
|
|
|
oldpip <- modifyDaemonStatus $
|
|
|
|
\s -> (s { pairingInProgress = Just pip' }, pairingInProgress s)
|
|
|
|
maybe noop stopold oldpip
|
|
|
|
liftIO $ sender stage
|
|
|
|
stopold = maybe noop (liftIO . killThread) . inProgressThreadId
|
2012-09-11 01:55:59 +00:00
|
|
|
|
2012-10-30 21:14:26 +00:00
|
|
|
stopSending :: PairingInProgress -> Assistant ()
|
|
|
|
stopSending pip = do
|
|
|
|
maybe noop (liftIO . killThread) $ inProgressThreadId pip
|
|
|
|
modifyDaemonStatus_ $ \s -> s { pairingInProgress = Nothing }
|
2012-09-08 19:21:34 +00:00
|
|
|
|
|
|
|
class ToSomeAddr a where
|
|
|
|
toSomeAddr :: a -> SomeAddr
|
|
|
|
|
|
|
|
instance ToSomeAddr IPv4 where
|
|
|
|
toSomeAddr (IPv4 a) = IPv4Addr a
|
|
|
|
|
|
|
|
instance ToSomeAddr IPv6 where
|
|
|
|
toSomeAddr (IPv6 o1 o2 o3 o4) = IPv6Addr (o1, o2, o3, o4)
|
|
|
|
|
|
|
|
showAddr :: SomeAddr -> HostName
|
|
|
|
showAddr (IPv4Addr a) = show $ IPv4 a
|
|
|
|
showAddr (IPv6Addr (o1, o2, o3, o4)) = show $ IPv6 o1 o2 o3 o4
|
|
|
|
|
|
|
|
activeNetworkAddresses :: IO [SomeAddr]
|
|
|
|
activeNetworkAddresses = filter (not . all (`elem` "0.:") . showAddr)
|
2012-09-13 04:57:52 +00:00
|
|
|
. concatMap (\ni -> [toSomeAddr $ ipv4 ni, toSomeAddr $ ipv6 ni])
|
2012-09-08 19:21:34 +00:00
|
|
|
<$> getNetworkInterfaces
|
2012-09-11 19:06:29 +00:00
|
|
|
|
|
|
|
{- A human-visible description of the repository being paired with.
|
|
|
|
- Note that the repository's description is not shown to the user, because
|
|
|
|
- it could be something like "my repo", which is confusing when pairing
|
|
|
|
- with someone else's repo. However, this has the same format as the
|
2021-04-08 17:08:01 +00:00
|
|
|
- default description of a repo. -}
|
2012-09-11 19:06:29 +00:00
|
|
|
pairRepo :: PairMsg -> String
|
|
|
|
pairRepo msg = concat
|
|
|
|
[ remoteUserName d
|
|
|
|
, "@"
|
|
|
|
, fromMaybe (showAddr $ pairMsgAddr msg) (remoteHostName d)
|
|
|
|
, ":"
|
|
|
|
, remoteDirectory d
|
|
|
|
]
|
2012-10-30 21:14:26 +00:00
|
|
|
where
|
|
|
|
d = pairMsgData msg
|