summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Tox.hs41
-rw-r--r--src/Network/QueryResponse.hs8
2 files changed, 42 insertions, 7 deletions
diff --git a/Tox.hs b/Tox.hs
index 513f50c1..e1a1bf8a 100644
--- a/Tox.hs
+++ b/Tox.hs
@@ -10,6 +10,7 @@
10module Tox where 10module Tox where
11 11
12import Control.Arrow 12import Control.Arrow
13import Control.Concurrent.STM
13import qualified Crypto.Cipher.Salsa as Salsa 14import qualified Crypto.Cipher.Salsa as Salsa
14import qualified Crypto.Cipher.XSalsa as XSalsa 15import qualified Crypto.Cipher.XSalsa as XSalsa
15import Crypto.ECC.Class 16import Crypto.ECC.Class
@@ -28,6 +29,7 @@ import qualified Data.ByteString.Char8 as C8
28import Data.ByteString.Lazy (toStrict) 29import Data.ByteString.Lazy (toStrict)
29import Data.Data 30import Data.Data
30import Data.IP 31import Data.IP
32import Data.Maybe
31import Data.Monoid 33import Data.Monoid
32import qualified Data.Serialize as S 34import qualified Data.Serialize as S
33import Data.Typeable 35import Data.Typeable
@@ -158,6 +160,12 @@ id2key recipient = case publicKey recipient of
158 -- This should never happen because a NodeId is 32 bytes. 160 -- This should never happen because a NodeId is 32 bytes.
159 CryptoFailed e -> error ("Unexpected pattern fail: "++show e) 161 CryptoFailed e -> error ("Unexpected pattern fail: "++show e)
160 162
163key2id :: PublicKey -> NodeId
164key2id pk = case S.decode (BA.convert pk) of
165 Left _ -> error "key2id"
166 Right nid -> nid
167
168
161zeros32 :: Bytes 169zeros32 :: Bytes
162zeros32 = BA.replicate 32 0 170zeros32 = BA.replicate 32 0
163 171
@@ -232,21 +240,40 @@ encodePacket :: SecretKey -> SecretsCache -> Message ByteString -> NodeInfo -> (
232encodePacket sk cache msg ni = ( S.runPut . putMessage $ encryptMessage sk cache (nodeId ni) msg 240encodePacket sk cache msg ni = ( S.runPut . putMessage $ encryptMessage sk cache (nodeId ni) msg
233 , nodeAddr ni ) 241 , nodeAddr ni )
234 242
243newClient :: SockAddr -> IO (Client String Method TransactionId NodeInfo (Message ByteString) ())
235newClient addr = do 244newClient addr = do
236 udp <- udpTransport addr 245 udp <- udpTransport addr
237 secret <- generateSecretKey 246 secret <- generateSecretKey
247 let pubkey = key2id $ toPublic secret
238 cache <- newEmptyCache 248 cache <- newEmptyCache
239 drg <- getSystemDRG 249 drg <- getSystemDRG
240 let net = layerTransport (parsePacket secret cache) (encodePacket secret cache) udp 250 let me = NodeInfo pubkey (fromMaybe (toEnum 0) $ fromSockAddr addr)
241 intmapT = transactionMethods (contramapT intKey intMapMethods) gen 251 (fromMaybe 0 $ sockAddrPort addr)
242 mapT = transactionMethods (contramapT nonceKey mapMethods) gen 252 tox <- atomically $ newTVar (me,())
243 dispatch = DispatchMethods 253 let net = layerTransport (parsePacket secret cache)
254 (encodePacket secret cache)
255 udp
256 dispatch tbl = DispatchMethods
244 { classifyInbound = classify 257 { classifyInbound = classify
245 , lookupHandler = handlers 258 , lookupHandler = handlers
246 , tableMethods = intmapT 259 , tableMethods = tbl
247 } 260 }
248 261 client tbl var = Client
249 return net 262 { clientNet = net
263 , clientDispatcher = dispatch tbl
264 , clientErrorReporter = ignoreErrors -- TODO
265 , clientPending = var
266 , clientContext = atomically (readTVar tox)
267 }
268 if fitsInInt (Proxy :: Proxy Word64)
269 then do
270 let intmapT = transactionMethods (contramapT intKey intMapMethods) gen
271 intmap_var <- atomically $ newTVar (drg, mempty)
272 return (client intmapT intmap_var)
273 else do
274 let mapT = transactionMethods (contramapT nonceKey mapMethods) gen
275 map_var <- atomically $ newTVar (drg, mempty)
276 return (client mapT map_var)
250 277
251last8 :: ByteString -> Nonce8 278last8 :: ByteString -> Nonce8
252last8 bs 279last8 bs
diff --git a/src/Network/QueryResponse.hs b/src/Network/QueryResponse.hs
index 4fc9438c..221c2284 100644
--- a/src/Network/QueryResponse.hs
+++ b/src/Network/QueryResponse.hs
@@ -306,6 +306,14 @@ data ErrorReporter addr x meth tid err = ErrorReporter
306 , reportTimeout :: meth -> tid -> addr -> IO () 306 , reportTimeout :: meth -> tid -> addr -> IO ()
307 } 307 }
308 308
309ignoreErrors :: ErrorReporter addr x meth tid err
310ignoreErrors = ErrorReporter
311 { reportParseError = \_ -> return ()
312 , reportMissingHandler = \_ _ _ -> return ()
313 , reportUnknown = \_ _ _ -> return ()
314 , reportTimeout = \_ _ _ -> return ()
315 }
316
309-- Change the /err/ type for an 'ErrorReporter'. 317-- Change the /err/ type for an 'ErrorReporter'.
310contramapE f (ErrorReporter pe mh unk tim) 318contramapE f (ErrorReporter pe mh unk tim)
311 = ErrorReporter (\e -> pe (f e)) 319 = ErrorReporter (\e -> pe (f e))