summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-11-19 22:59:49 -0500
committerjoe <joe@jerkface.net>2017-11-19 22:59:49 -0500
commiteaaf761dfb6af3673d9f064a791afadbbdc60e29 (patch)
treea8b927e0dc497f91298c2d43e85bba9dff48e0b9
parent0e10db03f6d8f60b4afa58c39765ac2d78e2f859 (diff)
Conduit stubs to convert between Tox and XMPP messages.
-rw-r--r--Presence/XMPPServer.hs5
-rw-r--r--ToxToXMPP.hs11
-rw-r--r--examples/dhtd.hs38
-rw-r--r--src/Network/Tox.hs68
4 files changed, 93 insertions, 29 deletions
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs
index c81cb9ce..6d6d3bd7 100644
--- a/Presence/XMPPServer.hs
+++ b/Presence/XMPPServer.hs
@@ -9,6 +9,7 @@ module XMPPServer
9 , XMPPServerParameters(..) 9 , XMPPServerParameters(..)
10 , XMPPServer 10 , XMPPServer
11 , xmppConnections 11 , xmppConnections
12 , xmppEventChannel
12 , StanzaWrap(..) 13 , StanzaWrap(..)
13 , Stanza(..) 14 , Stanza(..)
14 , StanzaType(..) 15 , StanzaType(..)
@@ -27,6 +28,7 @@ module XMPPServer
27 , makeMessage 28 , makeMessage
28 , JabberShow(..) 29 , JabberShow(..)
29 , Server 30 , Server
31 , flushPassThrough
30 ) where 32 ) where
31 33
32import ConnectionKey 34import ConnectionKey
@@ -1804,6 +1806,9 @@ xmppConnections sv = tcpManager (grokPeer sv) (Just . Text.pack) resolvPeer (_xm
1804 resolvPeer :: Text -> IO (Maybe ConnectionKey) 1806 resolvPeer :: Text -> IO (Maybe ConnectionKey)
1805 resolvPeer str = fmap PeerKey <$> listToMaybe <$> resolvePeer str 1807 resolvPeer str = fmap PeerKey <$> listToMaybe <$> resolvePeer str
1806 1808
1809xmppEventChannel :: XMPPServer -> TChan ((ConnectionKey, SockAddr), ConnectionEvent Event)
1810xmppEventChannel sv = serverEvent $ _xmpp_sv sv
1811
1807xmppServer :: ( MonadResource m 1812xmppServer :: ( MonadResource m
1808 , MonadIO m 1813 , MonadIO m
1809 ) => XMPPServerParameters -> m XMPPServer 1814 ) => XMPPServerParameters -> m XMPPServer
diff --git a/ToxToXMPP.hs b/ToxToXMPP.hs
new file mode 100644
index 00000000..b018e47b
--- /dev/null
+++ b/ToxToXMPP.hs
@@ -0,0 +1,11 @@
1module ToxToXMPP where
2
3import Data.Conduit as C
4import Data.XML.Types as XML
5import Network.Tox.Crypto.Transport as Tox
6
7xmppToTox :: Conduit XML.Event IO Tox.CryptoMessage
8xmppToTox = _todo
9
10toxToXmpp :: Conduit Tox.CryptoMessage IO XML.Event
11toxToXmpp = _todo
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index e9b365cb..527af7e7 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -27,6 +27,7 @@ import Control.Monad.Trans.Control
27import Control.Monad.Trans.Resource (runResourceT) 27import Control.Monad.Trans.Resource (runResourceT)
28import Data.Bool 28import Data.Bool
29import Data.Char 29import Data.Char
30import Data.Conduit as C
30import Data.Function 31import Data.Function
31import Data.Hashable 32import Data.Hashable
32import Data.List 33import Data.List
@@ -35,6 +36,7 @@ import qualified Data.Map.Strict as Map
35import Data.Maybe 36import Data.Maybe
36import qualified Data.Set as Set 37import qualified Data.Set as Set
37import Data.Time.Clock 38import Data.Time.Clock
39import qualified Data.XML.Types as XML
38import GHC.Conc (threadStatus,ThreadStatus(..)) 40import GHC.Conc (threadStatus,ThreadStatus(..))
39import GHC.Stats 41import GHC.Stats
40import Network.Socket 42import Network.Socket
@@ -85,6 +87,7 @@ import qualified Network.Tox.DHT.Transport as Tox
85import qualified Network.Tox.DHT.Handlers as Tox 87import qualified Network.Tox.DHT.Handlers as Tox
86import qualified Network.Tox.Onion.Transport as Tox 88import qualified Network.Tox.Onion.Transport as Tox
87import qualified Network.Tox.Onion.Handlers as Tox 89import qualified Network.Tox.Onion.Handlers as Tox
90import qualified Network.Tox.Crypto.Transport as Tox (CryptoMessage)
88import qualified Network.Tox.Crypto.Handlers as Tox 91import qualified Network.Tox.Crypto.Handlers as Tox
89import Data.Typeable 92import Data.Typeable
90import Roster 93import Roster
@@ -95,6 +98,8 @@ import ConsoleWriter
95import Presence 98import Presence
96import XMPPServer 99import XMPPServer
97import Connection 100import Connection
101import ToxToXMPP
102import qualified Server (ConnectionEvent(..))
98 103
99 104
100showReport :: [(String,String)] -> String 105showReport :: [(String,String)] -> String
@@ -982,6 +987,22 @@ noArgPing :: (x -> IO (Maybe r)) -> [String] -> x -> IO (Maybe r)
982noArgPing f [] x = f x 987noArgPing f [] x = f x
983noArgPing _ _ _ = return Nothing 988noArgPing _ _ _ = return Nothing
984 989
990announceToxConnection :: TChan ((ConnectionKey,SockAddr), Server.ConnectionEvent XML.Event)
991 -> SockAddr
992 -> SockAddr
993 -> STM Bool
994 -> C.Source IO Tox.CryptoMessage
995 -> C.Sink (Flush Tox.CryptoMessage) IO ()
996 -> IO ()
997announceToxConnection echan laddr saddr pingflag tsrc tsnk
998 = atomically $ writeTChan echan
999 ( (PeerKey saddr, laddr )
1000 , Server.Connection pingflag xsrc xsnk )
1001 where
1002 xsrc = tsrc =$= toxToXmpp
1003 xsnk = flushPassThrough xmppToTox =$= tsnk
1004
1005
985main :: IO () 1006main :: IO ()
986main = runResourceT $ liftBaseWith $ \resT -> do 1007main = runResourceT $ liftBaseWith $ \resT -> do
987 args <- getArgs 1008 args <- getArgs
@@ -1007,7 +1028,12 @@ main = runResourceT $ liftBaseWith $ \resT -> do
1007 -- We now have a server object but it's not ready to use until 1028 -- We now have a server object but it's not ready to use until
1008 -- we put it into the 'server' field of our /state/ record. 1029 -- we put it into the 'server' field of our /state/ record.
1009 1030
1010 conns <- xmppConnections sv 1031 conns <- xmppConnections sv
1032
1033 atomically $ do
1034 putTMVar serverVar (sv,conns) -- Okay, now it's ready. :)
1035 -- FIXME: This is error prone.
1036
1011 1037
1012 (quitBt,btdhts,btips,baddrs) <- case portbt opts of 1038 (quitBt,btdhts,btips,baddrs) <- case portbt opts of
1013 "" -> return (return (), Map.empty,return [],[]) 1039 "" -> return (return (), Map.empty,return [],[])
@@ -1106,7 +1132,11 @@ main = runResourceT $ liftBaseWith $ \resT -> do
1106 toxport -> do 1132 toxport -> do
1107 addrTox <- getBindAddress toxport (ip6tox opts) 1133 addrTox <- getBindAddress toxport (ip6tox opts)
1108 hPutStrLn stderr $ "Supplied key: " ++ show (fmap (Tox.key2id . toPublic) (dhtkey opts)) 1134 hPutStrLn stderr $ "Supplied key: " ++ show (fmap (Tox.key2id . toPublic) (dhtkey opts))
1109 tox <- Tox.newTox keysdb addrTox (Just netCryptoSessionsState) (dhtkey opts) 1135 tox <- Tox.newTox keysdb
1136 addrTox
1137 (Just netCryptoSessionsState)
1138 (dhtkey opts)
1139 (announceToxConnection (xmppEventChannel sv) addrTox)
1110 (quitTox, toxStrap4, toxStrap6) <- Tox.forkTox tox 1140 (quitTox, toxStrap4, toxStrap6) <- Tox.forkTox tox
1111 1141
1112 toxSearches <- atomically $ newTVar Map.empty 1142 toxSearches <- atomically $ newTVar Map.empty
@@ -1329,10 +1359,6 @@ main = runResourceT $ liftBaseWith $ \resT -> do
1329 bootstrap btSaved fallbackNodes 1359 bootstrap btSaved fallbackNodes
1330 return () 1360 return ()
1331 1361
1332 atomically $ do
1333 putTMVar serverVar (sv,conns) -- Okay, now it's ready. :)
1334 -- FIXME: This is error prone.
1335
1336 forkIO $ do 1362 forkIO $ do
1337 myThreadId >>= flip labelThread "XMPP.stanzas" 1363 myThreadId >>= flip labelThread "XMPP.stanzas"
1338 let console = cwPresenceChan <$> consoleWriter state 1364 let console = cwPresenceChan <$> consoleWriter state
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index 37802e3c..5b30a7e6 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -46,6 +46,7 @@ import qualified Data.ByteString.Base16 as Base16
46import qualified Data.ByteString.Char8 as C8 46import qualified Data.ByteString.Char8 as C8
47import Data.ByteString.Lazy (toStrict) 47import Data.ByteString.Lazy (toStrict)
48import Data.Char 48import Data.Char
49import Data.Conduit (Source,Sink,Flush(..))
49import Data.Data 50import Data.Data
50import Data.Functor.Contravariant 51import Data.Functor.Contravariant
51import Data.Hashable 52import Data.Hashable
@@ -85,11 +86,12 @@ import Control.TriadCommittee
85import Network.BitTorrent.DHT.Token as Token 86import Network.BitTorrent.DHT.Token as Token
86import GHC.TypeLits 87import GHC.TypeLits
87 88
89import Connection
88import Crypto.Tox 90import Crypto.Tox
89import Data.Word64Map (fitsInInt) 91import Data.Word64Map (fitsInInt)
90import qualified Data.Word64Map (empty) 92import qualified Data.Word64Map (empty)
91import Network.Kademlia.Bootstrap (forkPollForRefresh, bootstrap) 93import Network.Kademlia.Bootstrap (forkPollForRefresh, bootstrap)
92import Network.Tox.Crypto.Transport (NetCrypto) 94import Network.Tox.Crypto.Transport (NetCrypto, CryptoMessage)
93import Network.Tox.Crypto.Handlers (cryptoNetHandler, newSessionsState, defaultUnRecHook, defaultCryptoDataHooks, NetCryptoSessions(..)) 95import Network.Tox.Crypto.Handlers (cryptoNetHandler, newSessionsState, defaultUnRecHook, defaultCryptoDataHooks, NetCryptoSessions(..))
94import qualified Network.Tox.DHT.Handlers as DHT 96import qualified Network.Tox.DHT.Handlers as DHT
95import qualified Network.Tox.DHT.Transport as DHT 97import qualified Network.Tox.DHT.Transport as DHT
@@ -221,18 +223,22 @@ newClient drg net classify selfAddr handlers modifytbl modifynet = do
221 in client 223 in client
222 return $ either mkclient mkclient tblvar handlers 224 return $ either mkclient mkclient tblvar handlers
223 225
226data ConnectionKey -- TODO
227data ConnectionStatus -- TODO
228
224data Tox = Tox 229data Tox = Tox
225 { toxDHT :: DHT.Client 230 { toxDHT :: DHT.Client
226 , toxOnion :: Onion.Client RouteId 231 , toxOnion :: Onion.Client RouteId
227 , toxToRoute :: Transport String Onion.AnnouncedRendezvous (PublicKey,Onion.OnionData) 232 , toxToRoute :: Transport String Onion.AnnouncedRendezvous (PublicKey,Onion.OnionData)
228 , toxCrypto :: Transport String SockAddr NetCrypto 233 , toxCrypto :: Transport String SockAddr NetCrypto
229 , toxCryptoSessions :: NetCryptoSessions 234 , toxCryptoSessions :: NetCryptoSessions
230 , toxCryptoKeys :: TransportCrypto 235 , toxCryptoKeys :: TransportCrypto
231 , toxRouting :: DHT.Routing 236 , toxRouting :: DHT.Routing
232 , toxTokens :: TVar SessionTokens 237 , toxTokens :: TVar SessionTokens
233 , toxAnnouncedKeys :: TVar Onion.AnnouncedKeys 238 , toxAnnouncedKeys :: TVar Onion.AnnouncedKeys
234 , toxOnionRoutes :: OnionRouter 239 , toxOnionRoutes :: OnionRouter
235 , toxRoster :: Roster 240 , toxRoster :: Roster
241 , toxManager :: Connection.Manager ConnectionStatus ConnectionKey
236 } 242 }
237 243
238getContactInfo :: Tox -> IO DHT.DHTPublicKey 244getContactInfo :: Tox -> IO DHT.DHTPublicKey
@@ -289,8 +295,17 @@ getOnionAlias crypto dhtself remoteNode = atomically $ do
289 return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing 295 return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing
290 296
291 297
292newTox :: TVar Onion.AnnouncedKeys -> SockAddr -> Maybe NetCryptoSessions -> Maybe SecretKey -> IO Tox 298newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for.
293newTox keydb addr mbSessionsState suppliedDHTKey = do 299 -> SockAddr -- ^ Bind-address to listen on.
300 -> Maybe NetCryptoSessions -- ^ State of all one-on-one Tox links.
301 -> Maybe SecretKey -- ^ Optional DHT secret key to use.
302 -> ( SockAddr -- ^ Address of remote peer.
303 -> STM Bool -- ^ True if connection requires a ping.
304 -> Source IO CryptoMessage -- ^ Inbound packets.
305 -> Sink (Flush CryptoMessage) IO () -- ^ Outbound packets.
306 -> IO () ) -- ^ Action to invoke on new connections.
307 -> IO Tox
308newTox keydb addr mbSessionsState suppliedDHTKey announceConnection = do
294 udp <- {- addVerbosity <$> -} udpTransport addr 309 udp <- {- addVerbosity <$> -} udpTransport addr
295 (crypto0,sessionsState) <- case mbSessionsState of 310 (crypto0,sessionsState) <- case mbSessionsState of
296 Nothing -> do 311 Nothing -> do
@@ -338,17 +353,24 @@ newTox keydb addr mbSessionsState suppliedDHTKey = do
338 353
339 roster <- newRoster 354 roster <- newRoster
340 return Tox 355 return Tox
341 { toxDHT = dhtclient 356 { toxDHT = dhtclient
342 , toxOnion = onionclient 357 , toxOnion = onionclient
343 , toxToRoute = onInbound (updateRoster roster) dtacrypt 358 , toxToRoute = onInbound (updateRoster roster) dtacrypt
344 , toxCrypto = addHandler (hPutStrLn stderr) (cryptoNetHandler sessionsState) cryptonet 359 , toxCrypto = addHandler (hPutStrLn stderr) (cryptoNetHandler sessionsState) cryptonet
345 , toxCryptoSessions = sessionsState 360 , toxCryptoSessions = sessionsState
346 , toxCryptoKeys = crypto 361 , toxCryptoKeys = crypto
347 , toxRouting = mkrouting dhtclient 362 , toxRouting = mkrouting dhtclient
348 , toxTokens = toks 363 , toxTokens = toks
349 , toxAnnouncedKeys = keydb 364 , toxAnnouncedKeys = keydb
350 , toxOnionRoutes = orouter 365 , toxOnionRoutes = orouter
351 , toxRoster = roster 366 , toxRoster = roster
367 , toxManager = Connection.Manager
368 { setPolicy = _todo -- k -> Policy -> IO ()
369 , connections = _todo -- STM (Map k (Connection status))
370 , stringToKey = _todo -- String -> Maybe k
371 , showProgress = _todo -- status -> String
372 , showKey = _todo -- k -> String
373 }
352 } 374 }
353 375
354onionTimeout :: Tox -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int) 376onionTimeout :: Tox -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int)