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)
|
|
|
|
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-09 01:56:32 +03:00
|
|
|
, aSocket :: Socket
|
2019-10-08 23:04:21 +03:00
|
|
|
, aWakeTimer :: Async ()
|
|
|
|
, aListener :: Async ()
|
|
|
|
, aSendingQueue :: TQueue (SockAddr, ByteString)
|
|
|
|
, aSendingThread :: Async ()
|
2019-08-01 03:27:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data NetworkMode = Fake | Real
|
|
|
|
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
|
|
|
|
galaxyPort Fake (Galaxy g) = fromIntegral g + 31337
|
|
|
|
galaxyPort Real (Galaxy g) = fromIntegral g + 13337
|
|
|
|
|
|
|
|
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
|
|
|
|
ADGala _ _ -> True
|
|
|
|
ADIpv4 _ p (Ipv4 a) -> a == localhost
|
|
|
|
|
2019-10-09 20:25:11 +03:00
|
|
|
fakeSockAddr :: AmesDest -> SockAddr
|
|
|
|
fakeSockAddr = \case
|
|
|
|
ADGala _ g -> SockAddrInet (galaxyPort Fake g) localhost
|
|
|
|
ADIpv4 _ p a -> SockAddrInet (fromIntegral p) localhost
|
2019-08-01 03:27:13 +03:00
|
|
|
|
2019-08-08 01:24:02 +03:00
|
|
|
barnEv :: KingId -> Ev
|
2019-08-01 03:27:13 +03:00
|
|
|
barnEv inst =
|
|
|
|
EvBlip $ BlipEvNewt $ NewtEvBarn (fromIntegral inst, ()) ()
|
|
|
|
|
|
|
|
wakeEv :: Ev
|
|
|
|
wakeEv =
|
|
|
|
EvBlip $ BlipEvAmes $ AmesEvWake () ()
|
|
|
|
|
|
|
|
hearEv :: Time.Wen -> PortNumber -> HostAddress -> ByteString -> Ev
|
|
|
|
hearEv w p a bs =
|
|
|
|
EvBlip $ BlipEvAmes $ AmesEvHear () dest (MkBytes bs)
|
|
|
|
where
|
|
|
|
dest = ADIpv4 w (fromIntegral p) (Ipv4 a)
|
|
|
|
|
|
|
|
_turfText :: Turf -> Text
|
|
|
|
_turfText = intercalate "." . reverse . fmap unCord . unTurf
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
{-
|
|
|
|
inst -- Process instance number.
|
|
|
|
who -- Which ship are we?
|
|
|
|
enqueueEv -- Queue-event action.
|
|
|
|
mPort -- Explicit port override from command line arguments.
|
|
|
|
|
|
|
|
We ignore the %turf arguments for now. We only have fake ships,
|
|
|
|
so we don't implement the DNS stuff yet.
|
|
|
|
|
|
|
|
TODO Handle socket exceptions in waitPacket
|
|
|
|
|
|
|
|
4096 is a reasonable number for recvFrom. Packets of that size are
|
|
|
|
not possible on the internet.
|
|
|
|
|
|
|
|
TODO log when `sendTo` sent fewer bytes than requested.
|
|
|
|
|
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-09 01:01:16 +03:00
|
|
|
ames :: forall e. HasLogFunc e
|
2019-10-09 02:18:52 +03:00
|
|
|
=> KingId -> Ship -> Bool -> Maybe Port -> QueueEv
|
2019-10-09 01:01:16 +03:00
|
|
|
-> ([Ev], RAcquire e (EffCb e NewtEf))
|
2019-10-09 02:18:52 +03:00
|
|
|
ames inst who isFake mPort enqueueEv =
|
2019-08-01 03:27:13 +03:00
|
|
|
(initialEvents, runAmes)
|
|
|
|
where
|
|
|
|
initialEvents :: [Ev]
|
|
|
|
initialEvents = [barnEv inst]
|
|
|
|
|
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
|
2019-10-09 01:01:16 +03:00
|
|
|
aWakeTimer <- async runTimer
|
2019-10-09 01:56:32 +03:00
|
|
|
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-08-01 03:27:13 +03:00
|
|
|
netMode :: NetworkMode
|
2019-10-09 02:18:52 +03:00
|
|
|
netMode = if isFake then Fake else Real
|
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-08 23:04:21 +03:00
|
|
|
galaxies <- readIORef aGalaxies
|
|
|
|
mapM_ (cancel . fst) galaxies
|
|
|
|
|
|
|
|
cancel aSendingThread
|
2019-08-01 03:27:13 +03:00
|
|
|
cancel aWakeTimer
|
|
|
|
cancel aListener
|
2019-10-09 01:56:32 +03:00
|
|
|
io $ close' aSocket
|
2019-08-01 03:27:13 +03:00
|
|
|
|
2019-10-09 01:01:16 +03:00
|
|
|
runTimer :: RIO e ()
|
2019-08-01 03:27:13 +03:00
|
|
|
runTimer = forever $ do
|
|
|
|
threadDelay (300 * 1000000) -- 300 seconds
|
|
|
|
atomically (enqueueEv wakeEv)
|
|
|
|
|
2019-10-09 01:56:32 +03:00
|
|
|
bindSock :: RIO e Socket
|
|
|
|
bindSock = do
|
2019-08-01 03:27:13 +03:00
|
|
|
let ourPort = maybe (listenPort netMode who) fromIntegral mPort
|
2019-10-09 01:01:16 +03:00
|
|
|
s <- io $ socket AF_INET Datagram defaultProtocol
|
2019-10-09 02:53:07 +03:00
|
|
|
|
2019-10-09 01:01:16 +03:00
|
|
|
logTrace $ displayShow ("(ames) Binding to port ", ourPort)
|
2019-10-09 02:32:47 +03:00
|
|
|
let addr = SockAddrInet ourPort $
|
|
|
|
if isFake then localhost else inaddrAny
|
|
|
|
() <- io $ bind s addr
|
2019-10-09 02:53:07 +03:00
|
|
|
|
2019-08-01 03:27:13 +03:00
|
|
|
pure s
|
|
|
|
|
2019-10-09 01:01:16 +03:00
|
|
|
waitPacket :: Socket -> RIO e ()
|
2019-08-01 03:27:13 +03:00
|
|
|
waitPacket s = forever $ do
|
2019-10-09 01:01:16 +03:00
|
|
|
(bs, addr) <- io $ recvFrom s 4096
|
|
|
|
logTrace $ displayShow ("(ames) Received packet from ", addr)
|
|
|
|
wen <- io $ Time.now
|
2019-08-01 03:27:13 +03:00
|
|
|
case addr of
|
|
|
|
SockAddrInet p a -> atomically (enqueueEv $ hearEv wen p a bs)
|
|
|
|
_ -> 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 ()
|
|
|
|
Just turfs -> (sendPacket drv netMode 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-09 01:01:16 +03:00
|
|
|
sendPacket AmesDrv{..} Fake dest bs = do
|
2019-10-08 23:04:21 +03:00
|
|
|
when (okayFakeAddr dest) $ do
|
2019-10-09 20:25:11 +03:00
|
|
|
atomically $ writeTQueue aSendingQueue ((fakeSockAddr dest), bs)
|
2019-10-08 23:04:21 +03:00
|
|
|
|
|
|
|
sendPacket AmesDrv{..} Real (ADGala wen galaxy) bs = do
|
|
|
|
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-10-09 02:18:52 +03:00
|
|
|
sendPacket AmesDrv{..} Real (ADIpv4 _ p a) bs = do
|
|
|
|
let addr = SockAddrInet (fromIntegral p) (unIpv4 a)
|
|
|
|
atomically $ writeTQueue aSendingQueue (addr, bs)
|
2019-10-09 01:01:16 +03:00
|
|
|
|
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-09 01:01:16 +03:00
|
|
|
sendingThread :: TQueue (SockAddr, ByteString) -> Socket -> RIO e ()
|
2019-10-08 23:04:21 +03:00
|
|
|
sendingThread queue socket = forever $ do
|
|
|
|
(dest, bs) <- atomically $ readTQueue queue
|
2019-10-09 01:01:16 +03:00
|
|
|
logTrace $ displayShow ("(ames) Sending packet to ", socket, dest)
|
|
|
|
bytesSent <- io $ sendTo socket bs dest
|
|
|
|
let len = BS.length bs
|
|
|
|
when (bytesSent /= len) $ do
|
|
|
|
logDebug $ displayShow
|
|
|
|
("(ames) Only sent ", bytesSent, " of ", (length bs))
|
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
|
|
|
|
-- print ("ames: czar at %s: not found (b)\n")
|
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
|
|
|
|
logDebug $ displayShow
|
|
|
|
("(ames) Looked up ", hostname, portstr, y)
|
|
|
|
pure $ Just $ addrAddress y
|
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)
|