From fbf9890a6bcd4e6212b5947f908bc34f233b279d Mon Sep 17 00:00:00 2001 From: Joe Crayne Date: Sun, 9 Sep 2018 02:32:20 -0400 Subject: Moved resolving duty to Connection manager. --- Connection.hs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'Connection.hs') diff --git a/Connection.hs b/Connection.hs index a7e5d4cc..9a4077f5 100644 --- a/Connection.hs +++ b/Connection.hs @@ -5,8 +5,11 @@ module Connection where import Control.Applicative import Control.Arrow import Control.Concurrent.STM +import Data.Bits +import Data.Word import qualified Data.Map as Map ;import Data.Map (Map) +import Network.Socket (SockAddr(..)) import PingMachine @@ -34,6 +37,43 @@ data Connection status = Connection } deriving Functor +-- | A 'PeerAddress' identifies an active session. For inactive sessions, multiple +-- values may be feasible. + +-- We use a 'SockAddr' as it is convenient for TCP and UDP connections. But if +-- that is not your use case, see 'uniqueAsKey'. +newtype PeerAddress = PeerAddress { peerAddress :: SockAddr } + deriving (Eq,Ord,Show) + +-- | A 24-byte word. +data Uniq24 = Uniq24 !Word64 !Word64 !Word64 + deriving (Eq,Ord,Show) + +-- | Coerce a 'Uniq24' to a useable 'PeerAddress'. Note that this stores the +-- special value 0 into the port number of the underlying 'SockAddr' and thus +-- should be compatible for mixing together with TCP/UDP peers. +uniqueAsKey :: Uniq24 -> PeerAddress +uniqueAsKey (Uniq24 x y z) = PeerAddress $ SockAddrInet6 (fromIntegral 0) a bcde f + where + a = fromIntegral (x `shiftR` 32) + b = fromIntegral x + c = fromIntegral (y `shiftR` 32) + d = fromIntegral y + e = fromIntegral (z `shiftR` 32) + f = fromIntegral z + bcde = (b,c,d,e) + +-- | Inverse of 'uniqueAsKey' +keyAsUnique :: PeerAddress -> Maybe Uniq24 +keyAsUnique (PeerAddress (SockAddrInet6 0 a bcde f)) = Just $ Uniq24 x y z + where + (b,c,d,e) = bcde + x = (fromIntegral a `shiftL` 32) .|. fromIntegral b + y = (fromIntegral c `shiftL` 32) .|. fromIntegral d + z = (fromIntegral e `shiftL` 32) .|. fromIntegral f +keyAsUniq _ = Nothing + + -- | This is an interface to make or query status information about connections -- of a specific kind. -- @@ -58,6 +98,12 @@ data Manager status k = Manager , showProgress :: status -> String -- | Show a connection key as a string. , showKey :: k -> String + -- | Obtain an address from a human-friendly name. For TCP/UDP + -- connections, this might be a forward-resolving DNS query. + , resolvePeer :: k -> IO [PeerAddress] + -- | This is the reverse of 'resolvePeer'. For TCP/UDP connections, this + -- might be a reverse-resolve DNS query. + , reverseAddress :: PeerAddress -> IO [k] } -- | Present status information (visible in a UI) for a connection. -- cgit v1.2.3