2019-08-01 03:27:13 +03:00
|
|
|
module Vere.Ames (ames) where
|
2019-07-03 02:37:10 +03:00
|
|
|
|
2019-08-01 03:27:13 +03:00
|
|
|
import UrbitPrelude
|
|
|
|
|
|
|
|
import Arvo hiding (Fake)
|
2019-10-22 21:25:04 +03:00
|
|
|
import Config
|
2019-10-18 02:10:53 +03:00
|
|
|
import Control.Monad.Extra hiding (mapM_)
|
2019-08-01 03:27:13 +03:00
|
|
|
import Network.Socket hiding (recvFrom, sendTo)
|
|
|
|
import Network.Socket.ByteString
|
|
|
|
import Vere.Pier.Types
|
|
|
|
|
2019-10-09 01:01:16 +03:00
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import qualified Urbit.Ob as Ob
|
|
|
|
import qualified Urbit.Time as Time
|
2019-07-03 02:37:10 +03:00
|
|
|
|
2019-08-01 03:27:13 +03:00
|
|
|
-- Types -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
data AmesDrv = AmesDrv
|
2019-10-10 00:58:27 +03:00
|
|
|
{ aTurfs :: TVar (Maybe [Turf])
|
2019-10-08 23:04:21 +03:00
|
|
|
, aGalaxies :: IORef (M.Map Galaxy (Async (), TQueue ByteString))
|
2019-10-18 02:10:53 +03:00
|
|
|
, aSocket :: Maybe Socket
|
2019-10-08 23:04:21 +03:00
|
|
|
, aListener :: Async ()
|
|
|
|
, aSendingQueue :: TQueue (SockAddr, ByteString)
|
|
|
|
, aSendingThread :: Async ()
|
2019-08-01 03:27:13 +03:00
|
|
|
}
|
|
|
|
|
2019-10-18 02:10:53 +03:00
|
|
|
data NetworkMode = Fake | Localhost | Real | NoNetwork
|
2019-08-01 03:27:13 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
2019-07-03 02:37:10 +03:00
|
|
|
|
2019-08-01 03:27:13 +03:00
|
|
|
-- Utils -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
galaxyPort :: NetworkMode -> Galaxy -> PortNumber
|
2019-10-18 00:06:25 +03:00
|
|
|
galaxyPort Fake (Galaxy g) = fromIntegral g + 31337
|
|
|
|
galaxyPort Localhost (Galaxy g) = fromIntegral g + 13337
|
|
|
|
galaxyPort Real (Galaxy g) = fromIntegral g + 13337
|
2019-10-18 02:10:53 +03:00
|
|
|
galaxyPort NoNetwork _ = fromIntegral 0
|
2019-08-01 03:27:13 +03:00
|
|
|
|
|
|
|
listenPort :: NetworkMode -> Ship -> PortNumber
|
|
|
|
listenPort m s | s < 256 = galaxyPort m (fromIntegral s)
|
2019-10-08 23:04:21 +03:00
|
|
|
listenPort m _ = 0
|
2019-08-01 03:27:13 +03:00
|
|
|
|
|
|
|
localhost :: HostAddress
|
|
|
|
localhost = tupleToHostAddress (127,0,0,1)
|
|
|
|
|
2019-10-09 02:32:47 +03:00
|
|
|
inaddrAny :: HostAddress
|
|
|
|
inaddrAny = tupleToHostAddress (0,0,0,0)
|
|
|
|
|
2019-08-01 03:27:13 +03:00
|
|
|
okayFakeAddr :: AmesDest -> Bool
|
|
|
|
okayFakeAddr = \case
|
2019-12-10 05:45:19 +03:00
|
|
|
EachYes _ -> True
|
|
|
|
EachNo (Jammed (AAIpv4 (Ipv4 a) _)) -> a == localhost
|
|
|
|
EachNo (Jammed (AAVoid v)) -> absurd v
|
2019-08-01 03:27:13 +03:00
|
|
|
|
2019-10-18 00:06:25 +03:00
|
|
|
localhostSockAddr :: NetworkMode -> AmesDest -> SockAddr
|
|
|
|
localhostSockAddr mode = \case
|
2019-12-10 05:45:19 +03:00
|
|
|
EachYes g -> SockAddrInet (galaxyPort mode g) localhost
|
|
|
|
EachNo (Jammed (AAIpv4 _ p)) -> SockAddrInet (fromIntegral p) localhost
|
|
|
|
EachNo (Jammed (AAVoid v)) -> absurd v
|
2019-08-01 03:27:13 +03:00
|
|
|
|
2019-12-10 05:45:19 +03:00
|
|
|
bornEv :: KingId -> Ev
|
|
|
|
bornEv inst =
|
|
|
|
EvBlip $ BlipEvNewt $ NewtEvBorn (fromIntegral inst, ()) ()
|
2019-08-01 03:27:13 +03:00
|
|
|
|
2019-12-10 05:45:19 +03:00
|
|
|
hearEv :: PortNumber -> HostAddress -> ByteString -> Ev
|
|
|
|
hearEv p a bs =
|
2019-08-01 03:27:13 +03:00
|
|
|
EvBlip $ BlipEvAmes $ AmesEvHear () dest (MkBytes bs)
|
|
|
|
where
|
2019-12-10 05:45:19 +03:00
|
|
|
dest = EachNo $ Jammed $ AAIpv4 (Ipv4 a) (fromIntegral p)
|
2019-08-01 03:27:13 +03:00
|
|
|
|
|
|
|
_turfText :: Turf -> Text
|
|
|
|
_turfText = intercalate "." . reverse . fmap unCord . unTurf
|
|
|
|
|
2019-10-12 00:05:25 +03:00
|
|
|
renderGalaxy :: Galaxy -> Text
|
|
|
|
renderGalaxy = Ob.renderPatp . Ob.patp . fromIntegral . unGalaxy
|
2019-08-01 03:27:13 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
{-
|
|
|
|
inst -- Process instance number.
|
|
|
|
who -- Which ship are we?
|
|
|
|
enqueueEv -- Queue-event action.
|
|
|
|
mPort -- Explicit port override from command line arguments.
|
|
|
|
|
|
|
|
TODO Handle socket exceptions in waitPacket
|
|
|
|
|
|
|
|
4096 is a reasonable number for recvFrom. Packets of that size are
|
|
|
|
not possible on the internet.
|
|
|
|
|
2019-08-08 01:24:02 +03:00
|
|
|
TODO verify that the KingIds match on effects.
|
2019-08-01 03:27:13 +03:00
|
|
|
-}
|
2019-10-22 21:25:04 +03:00
|
|
|
ames :: forall e. (HasLogFunc e, HasNetworkConfig e)
|
2019-10-18 01:32:06 +03:00
|
|
|
=> KingId -> Ship -> Bool -> QueueEv
|
2019-10-12 00:05:25 +03:00
|
|
|
-> (Text -> RIO e ())
|
2019-10-09 01:01:16 +03:00
|
|
|
-> ([Ev], RAcquire e (EffCb e NewtEf))
|
2019-10-18 01:32:06 +03:00
|
|
|
ames inst who isFake enqueueEv stderr =
|
2019-08-01 03:27:13 +03:00
|
|
|
(initialEvents, runAmes)
|
|
|
|
where
|
|
|
|
initialEvents :: [Ev]
|
2019-12-10 05:45:19 +03:00
|
|
|
initialEvents = [bornEv inst]
|
2019-08-01 03:27:13 +03:00
|
|
|
|
2019-10-09 01:01:16 +03:00
|
|
|
runAmes :: RAcquire e (EffCb e NewtEf)
|
2019-08-01 03:27:13 +03:00
|
|
|
runAmes = do
|
2019-10-09 01:01:16 +03:00
|
|
|
drv <- mkRAcquire start stop
|
|
|
|
pure (handleEffect drv)
|
2019-08-01 03:27:13 +03:00
|
|
|
|
2019-10-09 01:01:16 +03:00
|
|
|
start :: RIO e AmesDrv
|
2019-08-01 03:27:13 +03:00
|
|
|
start = do
|
2019-10-10 00:58:27 +03:00
|
|
|
aTurfs <- newTVarIO Nothing
|
2019-10-09 01:01:16 +03:00
|
|
|
aGalaxies <- newIORef mempty
|
2019-10-09 01:56:32 +03:00
|
|
|
aSocket <- bindSock
|
|
|
|
aListener <- async (waitPacket aSocket)
|
2019-10-09 01:01:16 +03:00
|
|
|
aSendingQueue <- newTQueueIO
|
2019-10-09 01:56:32 +03:00
|
|
|
aSendingThread <- async (sendingThread aSendingQueue aSocket)
|
2019-10-09 01:01:16 +03:00
|
|
|
pure $ AmesDrv{..}
|
|
|
|
|
2019-10-18 00:06:25 +03:00
|
|
|
netMode :: RIO e NetworkMode
|
|
|
|
netMode = do
|
|
|
|
if isFake
|
|
|
|
then pure Fake
|
|
|
|
else getNetworkingType >>= \case
|
|
|
|
NetworkNormal -> pure Real
|
|
|
|
NetworkLocalhost -> pure Localhost
|
2019-10-18 02:10:53 +03:00
|
|
|
NetworkNone -> pure NoNetwork
|
2019-08-01 03:27:13 +03:00
|
|
|
|
2019-10-09 01:01:16 +03:00
|
|
|
stop :: AmesDrv -> RIO e ()
|
|
|
|
stop AmesDrv{..} = do
|
2019-10-12 00:05:25 +03:00
|
|
|
readIORef aGalaxies >>= mapM_ (cancel . fst)
|
2019-10-08 23:04:21 +03:00
|
|
|
|
|
|
|
cancel aSendingThread
|
2019-08-01 03:27:13 +03:00
|
|
|
cancel aListener
|
2019-10-18 02:10:53 +03:00
|
|
|
io $ maybeM (pure ()) (close') (pure aSocket)
|
|
|
|
-- io $ close' aSocket
|
2019-08-01 03:27:13 +03:00
|
|
|
|
2019-10-18 02:10:53 +03:00
|
|
|
bindSock :: RIO e (Maybe Socket)
|
2019-10-18 00:06:25 +03:00
|
|
|
bindSock = getBindAddr >>= doBindSocket
|
|
|
|
where
|
|
|
|
getBindAddr = netMode >>= \case
|
2019-10-18 02:10:53 +03:00
|
|
|
Fake -> pure $ Just localhost
|
|
|
|
Localhost -> pure $ Just localhost
|
|
|
|
Real -> pure $ Just inaddrAny
|
|
|
|
NoNetwork -> pure Nothing
|
|
|
|
|
|
|
|
doBindSocket :: Maybe HostAddress -> RIO e (Maybe Socket)
|
|
|
|
doBindSocket Nothing = pure Nothing
|
|
|
|
doBindSocket (Just bindAddr) = do
|
2019-10-18 00:06:25 +03:00
|
|
|
mode <- netMode
|
2019-10-18 01:32:06 +03:00
|
|
|
mPort <- getAmesPort
|
2019-10-18 00:06:25 +03:00
|
|
|
let ourPort = maybe (listenPort mode who) fromIntegral mPort
|
|
|
|
s <- io $ socket AF_INET Datagram defaultProtocol
|
2019-10-09 02:53:07 +03:00
|
|
|
|
2019-10-18 00:06:25 +03:00
|
|
|
logTrace $ displayShow ("(ames) Binding to port ", ourPort)
|
|
|
|
let addr = SockAddrInet ourPort bindAddr
|
|
|
|
() <- io $ bind s addr
|
|
|
|
|
2019-10-18 02:10:53 +03:00
|
|
|
pure $ Just s
|
2019-08-01 03:27:13 +03:00
|
|
|
|
2019-10-18 02:10:53 +03:00
|
|
|
waitPacket :: Maybe Socket -> RIO e ()
|
|
|
|
waitPacket Nothing = pure ()
|
|
|
|
waitPacket (Just s) = forever $ do
|
2019-10-09 01:01:16 +03:00
|
|
|
(bs, addr) <- io $ recvFrom s 4096
|
|
|
|
logTrace $ displayShow ("(ames) Received packet from ", addr)
|
2019-08-01 03:27:13 +03:00
|
|
|
case addr of
|
2019-12-10 05:45:19 +03:00
|
|
|
SockAddrInet p a -> atomically (enqueueEv $ hearEv p a bs)
|
2019-08-01 03:27:13 +03:00
|
|
|
_ -> pure ()
|
|
|
|
|
2019-10-09 01:01:16 +03:00
|
|
|
handleEffect :: AmesDrv -> NewtEf -> RIO e ()
|
2019-10-08 23:04:21 +03:00
|
|
|
handleEffect drv@AmesDrv{..} = \case
|
2019-08-01 03:27:13 +03:00
|
|
|
NewtEfTurf (_id, ()) turfs -> do
|
2019-10-10 00:58:27 +03:00
|
|
|
atomically $ writeTVar aTurfs (Just turfs)
|
2019-07-03 02:37:10 +03:00
|
|
|
|
2019-08-01 03:27:13 +03:00
|
|
|
NewtEfSend (_id, ()) dest (MkBytes bs) -> do
|
2019-10-10 00:58:27 +03:00
|
|
|
atomically (readTVar aTurfs) >>= \case
|
|
|
|
Nothing -> pure ()
|
2019-10-18 00:06:25 +03:00
|
|
|
Just turfs -> do
|
|
|
|
mode <- netMode
|
|
|
|
(sendPacket drv mode dest bs)
|
2019-10-08 23:04:21 +03:00
|
|
|
|
2019-10-09 01:01:16 +03:00
|
|
|
sendPacket :: AmesDrv -> NetworkMode -> AmesDest -> ByteString -> RIO e ()
|
2019-10-08 23:04:21 +03:00
|
|
|
|
2019-10-18 02:10:53 +03:00
|
|
|
sendPacket AmesDrv{..} NoNetwork dest bs = pure ()
|
|
|
|
|
2019-10-09 01:01:16 +03:00
|
|
|
sendPacket AmesDrv{..} Fake dest bs = do
|
2019-10-18 00:06:25 +03:00
|
|
|
when (okayFakeAddr dest) $ atomically $
|
|
|
|
writeTQueue aSendingQueue ((localhostSockAddr Fake dest), bs)
|
|
|
|
|
|
|
|
-- In localhost only mode, regardless of the actual destination, send it to
|
|
|
|
-- localhost.
|
|
|
|
sendPacket AmesDrv{..} Localhost dest bs = atomically $
|
|
|
|
writeTQueue aSendingQueue ((localhostSockAddr Localhost dest), bs)
|
2019-10-08 23:04:21 +03:00
|
|
|
|
2019-12-10 05:45:19 +03:00
|
|
|
sendPacket AmesDrv{..} Real (EachYes galaxy) bs = do
|
2019-10-08 23:04:21 +03:00
|
|
|
galaxies <- readIORef aGalaxies
|
|
|
|
queue <- case M.lookup galaxy galaxies of
|
|
|
|
Just (_, queue) -> pure queue
|
|
|
|
Nothing -> do
|
|
|
|
inQueue <- newTQueueIO
|
2019-10-09 01:01:16 +03:00
|
|
|
thread <- async $ galaxyResolver galaxy aTurfs inQueue aSendingQueue
|
2019-10-08 23:04:21 +03:00
|
|
|
modifyIORef (aGalaxies) (M.insert galaxy (thread, inQueue))
|
|
|
|
pure inQueue
|
|
|
|
|
|
|
|
atomically $ writeTQueue queue bs
|
|
|
|
|
2019-12-10 05:45:19 +03:00
|
|
|
sendPacket AmesDrv{..} Real (EachNo (Jammed (AAIpv4 a p))) bs = do
|
2019-10-09 02:18:52 +03:00
|
|
|
let addr = SockAddrInet (fromIntegral p) (unIpv4 a)
|
|
|
|
atomically $ writeTQueue aSendingQueue (addr, bs)
|
2019-10-09 01:01:16 +03:00
|
|
|
|
2019-12-10 05:45:19 +03:00
|
|
|
sendPacket AmesDrv{..} Real (EachNo (Jammed (AAVoid v))) bs = do
|
|
|
|
pure (absurd v)
|
|
|
|
|
2019-10-08 23:04:21 +03:00
|
|
|
-- An outbound queue of messages. We can only write to a socket from one
|
|
|
|
-- thread, so coalesce those writes here.
|
2019-10-18 02:10:53 +03:00
|
|
|
sendingThread :: TQueue (SockAddr, ByteString) -> Maybe Socket -> RIO e ()
|
|
|
|
sendingThread queue Nothing = pure ()
|
|
|
|
sendingThread queue (Just socket) = forever $
|
2019-10-12 00:05:25 +03:00
|
|
|
do
|
|
|
|
(dest, bs) <- atomically $ readTQueue queue
|
|
|
|
logTrace $ displayShow ("(ames) Sending packet to ", socket, dest)
|
|
|
|
sendAll bs dest
|
|
|
|
where
|
|
|
|
sendAll bs dest = do
|
|
|
|
bytesSent <- io $ sendTo socket bs dest
|
|
|
|
when (bytesSent /= BS.length bs) $ do
|
|
|
|
sendAll (drop bytesSent bs) dest
|
2019-10-08 23:04:21 +03:00
|
|
|
|
|
|
|
-- Asynchronous thread per galaxy which handles domain resolution, and can
|
|
|
|
-- block its own queue of ByteStrings to send.
|
|
|
|
--
|
|
|
|
-- Maybe perform the resolution asynchronously, injecting into the resolver
|
|
|
|
-- queue as a message.
|
|
|
|
--
|
|
|
|
-- TODO: Figure out how the real haskell time library works.
|
2019-10-10 00:58:27 +03:00
|
|
|
galaxyResolver :: Galaxy -> TVar (Maybe [Turf]) -> TQueue ByteString
|
2019-10-08 23:04:21 +03:00
|
|
|
-> TQueue (SockAddr, ByteString)
|
2019-10-09 01:01:16 +03:00
|
|
|
-> RIO e ()
|
2019-10-08 23:04:21 +03:00
|
|
|
galaxyResolver galaxy turfVar incoming outgoing =
|
2019-10-09 01:01:16 +03:00
|
|
|
loop Nothing Time.unixEpoch
|
2019-10-08 23:04:21 +03:00
|
|
|
where
|
2019-10-09 01:01:16 +03:00
|
|
|
loop :: Maybe SockAddr -> Time.Wen -> RIO e ()
|
2019-10-08 23:04:21 +03:00
|
|
|
loop lastGalaxyIP lastLookupTime = do
|
|
|
|
packet <- atomically $ readTQueue incoming
|
|
|
|
|
2019-10-10 00:58:27 +03:00
|
|
|
checkIP lastGalaxyIP lastLookupTime >>= \case
|
2019-10-08 23:04:21 +03:00
|
|
|
(Nothing, t) -> do
|
|
|
|
-- We've failed to lookup the IP. Drop the outbound packet
|
|
|
|
-- because we have no IP for our galaxy, including possible
|
|
|
|
-- previous IPs.
|
2019-10-09 01:01:16 +03:00
|
|
|
logDebug $ displayShow
|
|
|
|
("(ames) Dropping packet; no ip for galaxy ", galaxy)
|
2019-10-08 23:04:21 +03:00
|
|
|
loop Nothing t
|
|
|
|
(Just ip, t) -> do
|
|
|
|
queueSendToGalaxy ip packet
|
|
|
|
loop (Just ip) t
|
|
|
|
|
2019-10-09 01:01:16 +03:00
|
|
|
checkIP :: Maybe SockAddr -> Time.Wen
|
|
|
|
-> RIO e (Maybe SockAddr, Time.Wen)
|
2019-10-08 23:04:21 +03:00
|
|
|
checkIP lastIP lastLookupTime = do
|
2019-10-09 01:01:16 +03:00
|
|
|
current <- io $ Time.now
|
2019-10-08 23:04:21 +03:00
|
|
|
if (Time.gap current lastLookupTime ^. Time.secs) < 300
|
|
|
|
then pure (lastIP, lastLookupTime)
|
|
|
|
else do
|
2019-10-10 00:58:27 +03:00
|
|
|
toCheck <- fromMaybe [] <$> atomically (readTVar turfVar)
|
|
|
|
mybIp <- resolveFirstIP lastIP toCheck
|
2019-10-09 01:01:16 +03:00
|
|
|
timeAfterResolution <- io $ Time.now
|
2019-10-10 00:58:27 +03:00
|
|
|
pure (mybIp, timeAfterResolution)
|
2019-10-08 23:04:21 +03:00
|
|
|
|
2019-10-09 01:01:16 +03:00
|
|
|
resolveFirstIP :: Maybe SockAddr -> [Turf] -> RIO e (Maybe SockAddr)
|
2019-10-08 23:04:21 +03:00
|
|
|
resolveFirstIP prevIP [] = do
|
2019-10-12 00:05:25 +03:00
|
|
|
stderr $ "ames: czar at " ++ renderGalaxy galaxy ++ ": not found"
|
2019-10-09 01:01:16 +03:00
|
|
|
logDebug $ displayShow
|
|
|
|
("(ames) Failed to lookup IP for ", galaxy)
|
2019-10-08 23:04:21 +03:00
|
|
|
pure prevIP
|
|
|
|
|
|
|
|
resolveFirstIP prevIP (x:xs) = do
|
2019-10-10 00:58:27 +03:00
|
|
|
hostname <- buildDNS galaxy x
|
2019-10-09 01:01:16 +03:00
|
|
|
let portstr = show $ galaxyPort Real galaxy
|
|
|
|
listIPs <- io $ getAddrInfo Nothing (Just hostname) (Just portstr)
|
2019-10-08 23:04:21 +03:00
|
|
|
case listIPs of
|
|
|
|
[] -> resolveFirstIP prevIP xs
|
2019-10-09 01:01:16 +03:00
|
|
|
(y:ys) -> do
|
2019-10-12 00:05:25 +03:00
|
|
|
let sockaddr = Just $ addrAddress y
|
|
|
|
when (sockaddr /= prevIP) $
|
|
|
|
stderr $ "ames: czar " ++ renderGalaxy galaxy ++ ": ip " ++
|
|
|
|
(tshow $ addrAddress y)
|
2019-10-09 01:01:16 +03:00
|
|
|
logDebug $ displayShow
|
|
|
|
("(ames) Looked up ", hostname, portstr, y)
|
2019-10-12 00:05:25 +03:00
|
|
|
pure sockaddr
|
2019-10-08 23:04:21 +03:00
|
|
|
|
2019-10-10 00:58:27 +03:00
|
|
|
buildDNS :: Galaxy -> Turf -> RIO e String
|
|
|
|
buildDNS (Galaxy g) turf = do
|
|
|
|
let nameWithSig = Ob.renderPatp $ Ob.patp $ fromIntegral g
|
|
|
|
name <- case stripPrefix "~" nameWithSig of
|
2019-10-08 23:04:21 +03:00
|
|
|
Nothing -> error "Urbit.ob didn't produce string with ~"
|
2019-10-10 00:58:27 +03:00
|
|
|
Just x -> pure (unpack x)
|
|
|
|
pure $ name ++ "." ++ (unpack $ _turfText turf)
|
2019-10-08 23:04:21 +03:00
|
|
|
|
2019-10-09 01:01:16 +03:00
|
|
|
queueSendToGalaxy :: SockAddr -> ByteString -> RIO e ()
|
|
|
|
queueSendToGalaxy inet packet = do
|
2019-10-08 23:04:21 +03:00
|
|
|
atomically $ writeTQueue outgoing (inet, packet)
|
2019-10-18 00:06:25 +03:00
|
|
|
|