diff options
Diffstat (limited to 'src/Network/Tox.hs')
-rw-r--r-- | src/Network/Tox.hs | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs index 7814046e..3860d309 100644 --- a/src/Network/Tox.hs +++ b/src/Network/Tox.hs | |||
@@ -21,6 +21,7 @@ import Control.Arrow | |||
21 | import Control.Concurrent (MVar) | 21 | import Control.Concurrent (MVar) |
22 | import Control.Concurrent.STM | 22 | import Control.Concurrent.STM |
23 | import Control.Monad | 23 | import Control.Monad |
24 | import Control.Monad.Fix | ||
24 | import qualified Crypto.Cipher.ChaChaPoly1305 as Symmetric | 25 | import qualified Crypto.Cipher.ChaChaPoly1305 as Symmetric |
25 | import qualified Crypto.Cipher.Salsa as Salsa | 26 | import qualified Crypto.Cipher.Salsa as Salsa |
26 | import qualified Crypto.Cipher.XSalsa as XSalsa | 27 | import qualified Crypto.Cipher.XSalsa as XSalsa |
@@ -94,6 +95,7 @@ import qualified Network.Tox.Onion.Handlers as Onion | |||
94 | import Network.Tox.Crypto.Transport (NetCrypto) | 95 | import Network.Tox.Crypto.Transport (NetCrypto) |
95 | import Text.XXD | 96 | import Text.XXD |
96 | import OnionRouter | 97 | import OnionRouter |
98 | import Data.Word64Map (fitsInInt) | ||
97 | 99 | ||
98 | newCrypto :: IO TransportCrypto | 100 | newCrypto :: IO TransportCrypto |
99 | newCrypto = do | 101 | newCrypto = do |
@@ -158,7 +160,7 @@ newClient :: (DRG g, Show addr, Show meth) => | |||
158 | -> (x -> MessageClass String meth DHT.TransactionId) | 160 | -> (x -> MessageClass String meth DHT.TransactionId) |
159 | -> (Maybe addr -> IO addr) | 161 | -> (Maybe addr -> IO addr) |
160 | -> (meth -> Maybe (MethodHandler String DHT.TransactionId addr x)) | 162 | -> (meth -> Maybe (MethodHandler String DHT.TransactionId addr x)) |
161 | -> (forall d. TransactionMethods d DHT.TransactionId x -> TransactionMethods d DHT.TransactionId x) | 163 | -> (forall d. TransactionMethods d DHT.TransactionId addr x -> TransactionMethods d DHT.TransactionId addr x) |
162 | -> (Client String meth DHT.TransactionId addr x -> Transport String addr x -> Transport String addr x) | 164 | -> (Client String meth DHT.TransactionId addr x -> Transport String addr x -> Transport String addr x) |
163 | -> IO (Client String meth DHT.TransactionId addr x) | 165 | -> IO (Client String meth DHT.TransactionId addr x) |
164 | newClient drg net classify selfAddr handlers modifytbl modifynet = do | 166 | newClient drg net classify selfAddr handlers modifytbl modifynet = do |
@@ -180,11 +182,12 @@ newClient drg net classify selfAddr handlers modifytbl modifynet = do | |||
180 | , lookupHandler = handlers -- var | 182 | , lookupHandler = handlers -- var |
181 | , tableMethods = modifytbl tbl | 183 | , tableMethods = modifytbl tbl |
182 | } | 184 | } |
185 | eprinter = printErrors stderr | ||
183 | mkclient (tbl,var) handlers = | 186 | mkclient (tbl,var) handlers = |
184 | let client = Client | 187 | let client = Client |
185 | { clientNet = addHandler (handleMessage client) $ modifynet client net | 188 | { clientNet = addHandler eprinter (handleMessage client) $ modifynet client net |
186 | , clientDispatcher = dispatch tbl var handlers -- (fmap (contramapAddr (\(ToxPath ni _) -> ni)) . handlers) | 189 | , clientDispatcher = dispatch tbl var handlers -- (fmap (contramapAddr (\(ToxPath ni _) -> ni)) . handlers) |
187 | , clientErrorReporter = (printErrors stderr) { reportTimeout = reportTimeout ignoreErrors } | 190 | , clientErrorReporter = eprinter { reportTimeout = reportTimeout ignoreErrors } |
188 | , clientPending = var | 191 | , clientPending = var |
189 | , clientAddress = selfAddr | 192 | , clientAddress = selfAddr |
190 | , clientResponseId = genNonce24 var | 193 | , clientResponseId = genNonce24 var |
@@ -199,18 +202,22 @@ data Tox = Tox | |||
199 | , toxRouting :: DHT.Routing | 202 | , toxRouting :: DHT.Routing |
200 | , toxTokens :: TVar SessionTokens | 203 | , toxTokens :: TVar SessionTokens |
201 | , toxAnnouncedKeys :: TVar Onion.AnnouncedKeys | 204 | , toxAnnouncedKeys :: TVar Onion.AnnouncedKeys |
205 | , toxOnionRoutes :: OnionRouter | ||
202 | } | 206 | } |
203 | 207 | ||
204 | addVerbosity :: Show addr => Transport err addr ByteString -> Transport err addr ByteString | 208 | isLocalHost (SockAddrInet _ host32) = (fromBE32 host32 == 0x7f000001) |
209 | isLocalHost _ = False | ||
210 | |||
211 | addVerbosity :: Transport err SockAddr ByteString -> Transport err SockAddr ByteString | ||
205 | addVerbosity tr = | 212 | addVerbosity tr = |
206 | tr { awaitMessage = \kont -> awaitMessage tr $ \m -> do | 213 | tr { awaitMessage = \kont -> awaitMessage tr $ \m -> do |
207 | forM_ m $ mapM_ $ \(msg,addr) -> do | 214 | forM_ m $ mapM_ $ \(msg,addr) -> do |
208 | when (not (B.null msg || elem (B.head msg) [0,1,2,4])) $ do | 215 | when (isLocalHost addr || not (B.null msg || elem (B.head msg) [0,1,2,4,0x81,0x82,0x8c,0x8d])) $ do |
209 | mapM_ (\x -> hPutStrLn stderr ( (show addr) ++ " --> " ++ x)) | 216 | mapM_ (\x -> hPutStrLn stderr ( (show addr) ++ " --> " ++ x)) |
210 | $ xxd 0 msg | 217 | $ xxd 0 msg |
211 | kont m | 218 | kont m |
212 | , sendMessage = \addr msg -> do | 219 | , sendMessage = \addr msg -> do |
213 | when (not (B.null msg || elem (B.head msg) [0,1,2,4])) $ do | 220 | when (isLocalHost addr || not (B.null msg || elem (B.head msg) [0,1,2,4,0x81,0x82,0x8c,0x8d])) $ do |
214 | mapM_ (\x -> hPutStrLn stderr ( (show addr) ++ " <-- " ++ x)) | 221 | mapM_ (\x -> hPutStrLn stderr ( (show addr) ++ " <-- " ++ x)) |
215 | $ xxd 0 msg | 222 | $ xxd 0 msg |
216 | sendMessage tr addr msg | 223 | sendMessage tr addr msg |
@@ -226,13 +233,15 @@ newTox keydb addr = do | |||
226 | crypto <- newCrypto | 233 | crypto <- newCrypto |
227 | drg <- drgNew | 234 | drg <- drgNew |
228 | let lookupClose _ = return Nothing | 235 | let lookupClose _ = return Nothing |
229 | routing <- DHT.newRouting addr crypto updateIP updateIP | ||
230 | |||
231 | (dhtcrypt,onioncrypt,cryptonet) <- toxTransport crypto (DHT.orouter routing) lookupClose udp | ||
232 | 236 | ||
237 | routing <- DHT.newRouting addr crypto updateIP updateIP | ||
238 | orouter <- newOnionRouter | ||
239 | (dhtcrypt,onioncrypt,cryptonet) <- toxTransport crypto orouter lookupClose udp | ||
233 | let dhtnet0 = layerTransport (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt | 240 | let dhtnet0 = layerTransport (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt |
234 | dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr routing) (DHT.handlers routing) id | 241 | dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr routing) (DHT.handlers routing) id |
235 | $ \client net -> onInbound (DHT.updateRouting client routing) net | 242 | $ \client net -> onInbound (DHT.updateRouting client routing orouter) net |
243 | |||
244 | orouter <- forkRouteBuilder orouter $ \nid ni -> maybe [] (\(_,ns,_)->ns) <$> DHT.getNodes dhtclient nid ni | ||
236 | 245 | ||
237 | toks <- do | 246 | toks <- do |
238 | nil <- nullSessionTokens | 247 | nil <- nullSessionTokens |
@@ -240,13 +249,14 @@ newTox keydb addr = do | |||
240 | oniondrg <- drgNew | 249 | oniondrg <- drgNew |
241 | let onionnet = layerTransport (Onion.decrypt crypto) (Onion.encrypt crypto) onioncrypt | 250 | let onionnet = layerTransport (Onion.decrypt crypto) (Onion.encrypt crypto) onioncrypt |
242 | onionclient <- newClient oniondrg onionnet Onion.classify | 251 | onionclient <- newClient oniondrg onionnet Onion.classify |
243 | (const $ return | 252 | (const $ atomically |
244 | $ either (const $ error "bad sockaddr") | 253 | $ flip Onion.OnionDestination Nothing |
245 | (flip Onion.OnionDestination Nothing) | 254 | . R.thisNode |
246 | $ nodeInfo zeroID addr) | 255 | <$> readTVar (DHT.routing4 routing)) |
247 | (Onion.handlers onionnet routing toks keydb) | 256 | (Onion.handlers onionnet routing toks keydb) |
248 | (hookQueries (DHT.orouter routing) DHT.transactionKey) | 257 | (hookQueries orouter DHT.transactionKey) |
249 | (const id) | 258 | (const id) |
259 | |||
250 | return Tox | 260 | return Tox |
251 | { toxDHT = dhtclient | 261 | { toxDHT = dhtclient |
252 | , toxOnion = onionclient | 262 | , toxOnion = onionclient |
@@ -254,8 +264,12 @@ newTox keydb addr = do | |||
254 | , toxRouting = routing | 264 | , toxRouting = routing |
255 | , toxTokens = toks | 265 | , toxTokens = toks |
256 | , toxAnnouncedKeys = keydb | 266 | , toxAnnouncedKeys = keydb |
267 | , toxOnionRoutes = orouter | ||
257 | } | 268 | } |
258 | 269 | ||
270 | onionTimeout :: Tox -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int) | ||
271 | onionTimeout Tox { toxOnionRoutes = or } (DHT.TransactionId n8 _) od = lookupTimeout or n8 od | ||
272 | |||
259 | forkTox :: Tox -> IO (IO ()) | 273 | forkTox :: Tox -> IO (IO ()) |
260 | forkTox tox = do | 274 | forkTox tox = do |
261 | _ <- forkListener "toxCrypto" (toxCrypto tox) | 275 | _ <- forkListener "toxCrypto" (toxCrypto tox) |