summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--OnionRouter.hs31
-rw-r--r--src/Network/Tox.hs14
-rw-r--r--src/Network/Tox/DHT/Handlers.hs20
-rw-r--r--src/Network/Tox/Transport.hs16
4 files changed, 57 insertions, 24 deletions
diff --git a/OnionRouter.hs b/OnionRouter.hs
new file mode 100644
index 00000000..15304221
--- /dev/null
+++ b/OnionRouter.hs
@@ -0,0 +1,31 @@
1module OnionRouter where
2
3import Crypto.Tox
4import Network.Kademlia
5import Network.Kademlia.Routing
6import Network.QueryResponse
7import Network.Tox.NodeId
8import Network.Tox.Onion.Transport
9
10import Network.Socket (SockAddr)
11import Control.Concurrent.STM
12
13newtype RouteId = RouteId Int
14 deriving Show
15
16data OnionRouter
17
18newOnionRouter :: IO OnionRouter
19newOnionRouter = return _todo
20
21lookupSender :: OnionRouter -> SockAddr -> Nonce8 -> IO (Maybe (NodeInfo, RouteId))
22lookupSender _ _ _ = return Nothing -- todo
23
24lookupRoute :: OnionRouter -> NodeInfo -> RouteId -> IO (Maybe OnionRoute)
25lookupRoute _ _ _ = return Nothing -- todo
26
27hookQueries :: OnionRouter -> (tid -> Nonce8) -> TransactionMethods d tid x -> TransactionMethods d tid x
28hookQueries _ n8 tmethods = tmethods -- todo
29
30hookBucketList :: OnionRouter -> RoutingTransition ni -> STM ()
31hookBucketList _ _ = return () -- todo
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index 3c5fc955..7814046e 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -93,6 +93,7 @@ import qualified Network.Tox.Onion.Transport as Onion
93import qualified Network.Tox.Onion.Handlers as Onion 93import qualified Network.Tox.Onion.Handlers as Onion
94import Network.Tox.Crypto.Transport (NetCrypto) 94import Network.Tox.Crypto.Transport (NetCrypto)
95import Text.XXD 95import Text.XXD
96import OnionRouter
96 97
97newCrypto :: IO TransportCrypto 98newCrypto :: IO TransportCrypto
98newCrypto = do 99newCrypto = do
@@ -157,9 +158,10 @@ newClient :: (DRG g, Show addr, Show meth) =>
157 -> (x -> MessageClass String meth DHT.TransactionId) 158 -> (x -> MessageClass String meth DHT.TransactionId)
158 -> (Maybe addr -> IO addr) 159 -> (Maybe addr -> IO addr)
159 -> (meth -> Maybe (MethodHandler String DHT.TransactionId addr x)) 160 -> (meth -> Maybe (MethodHandler String DHT.TransactionId addr x))
161 -> (forall d. TransactionMethods d DHT.TransactionId x -> TransactionMethods d DHT.TransactionId x)
160 -> (Client String meth DHT.TransactionId addr x -> Transport String addr x -> Transport String addr x) 162 -> (Client String meth DHT.TransactionId addr x -> Transport String addr x -> Transport String addr x)
161 -> IO (Client String meth DHT.TransactionId addr x) 163 -> IO (Client String meth DHT.TransactionId addr x)
162newClient drg net classify selfAddr handlers modifynet = do 164newClient drg net classify selfAddr handlers modifytbl modifynet = do
163 -- If we have 8-byte keys for IntMap, then use it for transaction lookups. 165 -- If we have 8-byte keys for IntMap, then use it for transaction lookups.
164 -- Otherwise, use ordinary Map. The details of which will be hidden by an 166 -- Otherwise, use ordinary Map. The details of which will be hidden by an
165 -- existential closure (see mkclient below). 167 -- existential closure (see mkclient below).
@@ -176,7 +178,7 @@ newClient drg net classify selfAddr handlers modifynet = do
176 let dispatch tbl var handlers = DispatchMethods 178 let dispatch tbl var handlers = DispatchMethods
177 { classifyInbound = classify 179 { classifyInbound = classify
178 , lookupHandler = handlers -- var 180 , lookupHandler = handlers -- var
179 , tableMethods = tbl 181 , tableMethods = modifytbl tbl
180 } 182 }
181 mkclient (tbl,var) handlers = 183 mkclient (tbl,var) handlers =
182 let client = Client 184 let client = Client
@@ -224,11 +226,12 @@ newTox keydb addr = do
224 crypto <- newCrypto 226 crypto <- newCrypto
225 drg <- drgNew 227 drg <- drgNew
226 let lookupClose _ = return Nothing 228 let lookupClose _ = return Nothing
227 (dhtcrypt,onioncrypt,cryptonet) <- toxTransport crypto lookupClose udp
228
229 routing <- DHT.newRouting addr crypto updateIP updateIP 229 routing <- DHT.newRouting addr crypto updateIP updateIP
230
231 (dhtcrypt,onioncrypt,cryptonet) <- toxTransport crypto (DHT.orouter routing) lookupClose udp
232
230 let dhtnet0 = layerTransport (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt 233 let dhtnet0 = layerTransport (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt
231 dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr routing) (DHT.handlers routing) 234 dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr routing) (DHT.handlers routing) id
232 $ \client net -> onInbound (DHT.updateRouting client routing) net 235 $ \client net -> onInbound (DHT.updateRouting client routing) net
233 236
234 toks <- do 237 toks <- do
@@ -242,6 +245,7 @@ newTox keydb addr = do
242 (flip Onion.OnionDestination Nothing) 245 (flip Onion.OnionDestination Nothing)
243 $ nodeInfo zeroID addr) 246 $ nodeInfo zeroID addr)
244 (Onion.handlers onionnet routing toks keydb) 247 (Onion.handlers onionnet routing toks keydb)
248 (hookQueries (DHT.orouter routing) DHT.transactionKey)
245 (const id) 249 (const id)
246 return Tox 250 return Tox
247 { toxDHT = dhtclient 251 { toxDHT = dhtclient
diff --git a/src/Network/Tox/DHT/Handlers.hs b/src/Network/Tox/DHT/Handlers.hs
index 4e43c4a7..c9adc860 100644
--- a/src/Network/Tox/DHT/Handlers.hs
+++ b/src/Network/Tox/DHT/Handlers.hs
@@ -14,6 +14,7 @@ import Network.Address (WantIP (..), ipFamily, testIdBit,fromSockA
14import qualified Network.Kademlia.Routing as R 14import qualified Network.Kademlia.Routing as R
15import Control.TriadCommittee 15import Control.TriadCommittee
16import System.Global6 16import System.Global6
17import OnionRouter
17 18
18import qualified Data.ByteArray as BA 19import qualified Data.ByteArray as BA
19import qualified Data.ByteString.Char8 as C8 20import qualified Data.ByteString.Char8 as C8
@@ -104,6 +105,7 @@ data Routing = Routing
104 , sched6 :: !( TVar (Int.PSQ POSIXTime) ) 105 , sched6 :: !( TVar (Int.PSQ POSIXTime) )
105 , routing6 :: !( TVar (R.BucketList NodeInfo) ) 106 , routing6 :: !( TVar (R.BucketList NodeInfo) )
106 , committee6 :: TriadCommittee NodeId SockAddr 107 , committee6 :: TriadCommittee NodeId SockAddr
108 , orouter :: OnionRouter
107 } 109 }
108 110
109newRouting :: SockAddr -> TransportCrypto 111newRouting :: SockAddr -> TransportCrypto
@@ -123,6 +125,7 @@ newRouting addr crypto update4 update6 = do
123 maybe (tentative_info { nodeIP = tentative_ip6 }) 125 maybe (tentative_info { nodeIP = tentative_ip6 })
124 (\ip6 -> tentative_info { nodeIP = IPv6 ip6 }) 126 (\ip6 -> tentative_info { nodeIP = IPv6 ip6 })
125 <$> global6 127 <$> global6
128 orouter <- newOnionRouter
126 atomically $ do 129 atomically $ do
127 let nobkts = R.defaultBucketCount :: Int 130 let nobkts = R.defaultBucketCount :: Int
128 tbl4 <- newTVar $ R.nullTable (comparing nodeId) (\s -> hashWithSalt s . nodeId) tentative_info4 nobkts 131 tbl4 <- newTVar $ R.nullTable (comparing nodeId) (\s -> hashWithSalt s . nodeId) tentative_info4 nobkts
@@ -131,7 +134,7 @@ newRouting addr crypto update4 update6 = do
131 committee6 <- newTriadCommittee (update6 tbl6) -- updateIPVote tbl6 addr6 134 committee6 <- newTriadCommittee (update6 tbl6) -- updateIPVote tbl6 addr6
132 sched4 <- newTVar Int.empty 135 sched4 <- newTVar Int.empty
133 sched6 <- newTVar Int.empty 136 sched6 <- newTVar Int.empty
134 return $ Routing tentative_info sched4 tbl4 committee4 sched6 tbl6 committee6 137 return $ Routing tentative_info sched4 tbl4 committee4 sched6 tbl6 committee6 orouter
135 138
136 139
137-- TODO: This should cover more cases 140-- TODO: This should cover more cases
@@ -241,24 +244,25 @@ updateRouting client routing naddr msg = do
241 hPutStrLn stderr $ "updateRouting "++show (typ,tid) 244 hPutStrLn stderr $ "updateRouting "++show (typ,tid)
242 -- TODO: check msg type 245 -- TODO: check msg type
243 case prefer4or6 naddr Nothing of 246 case prefer4or6 naddr Nothing of
244 Want_IP4 -> updateTable client naddr (routing4 routing) (committee4 routing) (sched4 routing) 247 Want_IP4 -> updateTable client naddr (orouter routing) (routing4 routing) (committee4 routing) (sched4 routing)
245 Want_IP6 -> updateTable client naddr (routing6 routing) (committee6 routing) (sched6 routing) 248 Want_IP6 -> updateTable client naddr (orouter routing) (routing6 routing) (committee6 routing) (sched6 routing)
246 249
247updateTable :: Client -> NodeInfo -> TVar (R.BucketList NodeInfo) -> TriadCommittee NodeId SockAddr -> TVar (Int.PSQ POSIXTime) -> IO () 250updateTable :: Client -> NodeInfo -> OnionRouter -> TVar (R.BucketList NodeInfo) -> TriadCommittee NodeId SockAddr -> TVar (Int.PSQ POSIXTime) -> IO ()
248updateTable client naddr tbl committee sched = do 251updateTable client naddr orouter tbl committee sched = do
249 self <- atomically $ R.thisNode <$> readTVar tbl 252 self <- atomically $ R.thisNode <$> readTVar tbl
250 when (nodeIP self /= nodeIP naddr) $ do 253 when (nodeIP self /= nodeIP naddr) $ do
251 -- TODO: IP address vote? 254 -- TODO: IP address vote?
252 insertNode (toxKademlia client committee tbl sched) naddr 255 insertNode (toxKademlia client committee orouter tbl sched) naddr
253 256
254toxKademlia :: Client -> TriadCommittee NodeId SockAddr -> TVar (R.BucketList NodeInfo) -> TVar (Int.PSQ POSIXTime) -> Kademlia NodeId NodeInfo 257toxKademlia :: Client -> TriadCommittee NodeId SockAddr -> OnionRouter -> TVar (R.BucketList NodeInfo) -> TVar (Int.PSQ POSIXTime) -> Kademlia NodeId NodeInfo
255toxKademlia client committee var sched 258toxKademlia client committee orouter var sched
256 = Kademlia quietInsertions 259 = Kademlia quietInsertions
257 toxSpace 260 toxSpace
258 (vanillaIO var $ ping client) 261 (vanillaIO var $ ping client)
259 { tblTransition = \tr -> do 262 { tblTransition = \tr -> do
260 io1 <- transitionCommittee committee tr 263 io1 <- transitionCommittee committee tr
261 io2 <- touchBucket toxSpace (15*60) var sched tr 264 io2 <- touchBucket toxSpace (15*60) var sched tr
265 hookBucketList orouter tr
262 return $ do 266 return $ do
263 io1 >> io2 267 io1 >> io2
264 {- 268 {-
diff --git a/src/Network/Tox/Transport.hs b/src/Network/Tox/Transport.hs
index d441dc0a..d4e1a754 100644
--- a/src/Network/Tox/Transport.hs
+++ b/src/Network/Tox/Transport.hs
@@ -13,28 +13,22 @@ import Crypto.Tox
13import Network.Tox.DHT.Transport 13import Network.Tox.DHT.Transport
14import Network.Tox.Onion.Transport 14import Network.Tox.Onion.Transport
15import Network.Tox.Crypto.Transport 15import Network.Tox.Crypto.Transport
16import OnionRouter
16 17
17import Network.Socket 18import Network.Socket
18 19
19type RouteId = () -- todo
20
21lookupSender :: SockAddr -> Nonce8 -> IO (Maybe (NodeInfo, RouteId))
22lookupSender _ _ = return Nothing -- todo
23
24lookupRoute :: NodeInfo -> RouteId -> IO (Maybe OnionRoute)
25lookupRoute _ _ = return Nothing -- todo
26
27toxTransport :: 20toxTransport ::
28 TransportCrypto 21 TransportCrypto
22 -> OnionRouter
29 -> (PublicKey -> IO (Maybe NodeInfo)) 23 -> (PublicKey -> IO (Maybe NodeInfo))
30 -> UDPTransport 24 -> UDPTransport
31 -> IO ( Transport String NodeInfo (DHTMessage Encrypted8) 25 -> IO ( Transport String NodeInfo (DHTMessage Encrypted8)
32 , Transport String (OnionDestination RouteId) (OnionMessage Encrypted) 26 , Transport String (OnionDestination RouteId) (OnionMessage Encrypted)
33 , Transport String SockAddr NetCrypto ) 27 , Transport String SockAddr NetCrypto )
34toxTransport crypto closeLookup udp = do 28toxTransport crypto orouter closeLookup udp = do
35 (dht,udp1) <- partitionTransport parseDHTAddr (Just . encodeDHTAddr) $ forwardOnions crypto udp 29 (dht,udp1) <- partitionTransport parseDHTAddr (Just . encodeDHTAddr) $ forwardOnions crypto udp
36 (onion,udp2) <- partitionTransportM (parseOnionAddr lookupSender) 30 (onion,udp2) <- partitionTransportM (parseOnionAddr $ lookupSender orouter)
37 (encodeOnionAddr lookupRoute) 31 (encodeOnionAddr $ lookupRoute orouter)
38 udp1 32 udp1
39 let netcrypto = layerTransport parseNetCrypto encodeNetCrypto udp2 33 let netcrypto = layerTransport parseNetCrypto encodeNetCrypto udp2
40 return ( forwardDHTRequests crypto closeLookup dht 34 return ( forwardDHTRequests crypto closeLookup dht