diff options
author | joe <joe@jerkface.net> | 2017-10-02 18:27:12 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-10-02 18:27:12 -0400 |
commit | f7a3c6c057244c8ca14bb9c6ad4bdfb4629ac154 (patch) | |
tree | 833b7bd39883e56a16a230244fec04f06d8ac755 | |
parent | d408e6c3148106c6dbc8afe24a1488619adf34e1 (diff) |
Stubs for maintaining onion routes.
-rw-r--r-- | OnionRouter.hs | 31 | ||||
-rw-r--r-- | src/Network/Tox.hs | 14 | ||||
-rw-r--r-- | src/Network/Tox/DHT/Handlers.hs | 20 | ||||
-rw-r--r-- | src/Network/Tox/Transport.hs | 16 |
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 @@ | |||
1 | module OnionRouter where | ||
2 | |||
3 | import Crypto.Tox | ||
4 | import Network.Kademlia | ||
5 | import Network.Kademlia.Routing | ||
6 | import Network.QueryResponse | ||
7 | import Network.Tox.NodeId | ||
8 | import Network.Tox.Onion.Transport | ||
9 | |||
10 | import Network.Socket (SockAddr) | ||
11 | import Control.Concurrent.STM | ||
12 | |||
13 | newtype RouteId = RouteId Int | ||
14 | deriving Show | ||
15 | |||
16 | data OnionRouter | ||
17 | |||
18 | newOnionRouter :: IO OnionRouter | ||
19 | newOnionRouter = return _todo | ||
20 | |||
21 | lookupSender :: OnionRouter -> SockAddr -> Nonce8 -> IO (Maybe (NodeInfo, RouteId)) | ||
22 | lookupSender _ _ _ = return Nothing -- todo | ||
23 | |||
24 | lookupRoute :: OnionRouter -> NodeInfo -> RouteId -> IO (Maybe OnionRoute) | ||
25 | lookupRoute _ _ _ = return Nothing -- todo | ||
26 | |||
27 | hookQueries :: OnionRouter -> (tid -> Nonce8) -> TransactionMethods d tid x -> TransactionMethods d tid x | ||
28 | hookQueries _ n8 tmethods = tmethods -- todo | ||
29 | |||
30 | hookBucketList :: OnionRouter -> RoutingTransition ni -> STM () | ||
31 | hookBucketList _ _ = 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 | |||
93 | import qualified Network.Tox.Onion.Handlers as Onion | 93 | import qualified Network.Tox.Onion.Handlers as Onion |
94 | import Network.Tox.Crypto.Transport (NetCrypto) | 94 | import Network.Tox.Crypto.Transport (NetCrypto) |
95 | import Text.XXD | 95 | import Text.XXD |
96 | import OnionRouter | ||
96 | 97 | ||
97 | newCrypto :: IO TransportCrypto | 98 | newCrypto :: IO TransportCrypto |
98 | newCrypto = do | 99 | newCrypto = 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) |
162 | newClient drg net classify selfAddr handlers modifynet = do | 164 | newClient 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 | |||
14 | import qualified Network.Kademlia.Routing as R | 14 | import qualified Network.Kademlia.Routing as R |
15 | import Control.TriadCommittee | 15 | import Control.TriadCommittee |
16 | import System.Global6 | 16 | import System.Global6 |
17 | import OnionRouter | ||
17 | 18 | ||
18 | import qualified Data.ByteArray as BA | 19 | import qualified Data.ByteArray as BA |
19 | import qualified Data.ByteString.Char8 as C8 | 20 | import 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 | ||
109 | newRouting :: SockAddr -> TransportCrypto | 111 | newRouting :: 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 | ||
247 | updateTable :: Client -> NodeInfo -> TVar (R.BucketList NodeInfo) -> TriadCommittee NodeId SockAddr -> TVar (Int.PSQ POSIXTime) -> IO () | 250 | updateTable :: Client -> NodeInfo -> OnionRouter -> TVar (R.BucketList NodeInfo) -> TriadCommittee NodeId SockAddr -> TVar (Int.PSQ POSIXTime) -> IO () |
248 | updateTable client naddr tbl committee sched = do | 251 | updateTable 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 | ||
254 | toxKademlia :: Client -> TriadCommittee NodeId SockAddr -> TVar (R.BucketList NodeInfo) -> TVar (Int.PSQ POSIXTime) -> Kademlia NodeId NodeInfo | 257 | toxKademlia :: Client -> TriadCommittee NodeId SockAddr -> OnionRouter -> TVar (R.BucketList NodeInfo) -> TVar (Int.PSQ POSIXTime) -> Kademlia NodeId NodeInfo |
255 | toxKademlia client committee var sched | 258 | toxKademlia 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 | |||
13 | import Network.Tox.DHT.Transport | 13 | import Network.Tox.DHT.Transport |
14 | import Network.Tox.Onion.Transport | 14 | import Network.Tox.Onion.Transport |
15 | import Network.Tox.Crypto.Transport | 15 | import Network.Tox.Crypto.Transport |
16 | import OnionRouter | ||
16 | 17 | ||
17 | import Network.Socket | 18 | import Network.Socket |
18 | 19 | ||
19 | type RouteId = () -- todo | ||
20 | |||
21 | lookupSender :: SockAddr -> Nonce8 -> IO (Maybe (NodeInfo, RouteId)) | ||
22 | lookupSender _ _ = return Nothing -- todo | ||
23 | |||
24 | lookupRoute :: NodeInfo -> RouteId -> IO (Maybe OnionRoute) | ||
25 | lookupRoute _ _ = return Nothing -- todo | ||
26 | |||
27 | toxTransport :: | 20 | toxTransport :: |
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 ) |
34 | toxTransport crypto closeLookup udp = do | 28 | toxTransport 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 |