summaryrefslogtreecommitdiff
path: root/src/Network/Tox/DHT/Transport.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/Tox/DHT/Transport.hs')
-rw-r--r--src/Network/Tox/DHT/Transport.hs88
1 files changed, 44 insertions, 44 deletions
diff --git a/src/Network/Tox/DHT/Transport.hs b/src/Network/Tox/DHT/Transport.hs
index 16af0e3f..5bd9caa1 100644
--- a/src/Network/Tox/DHT/Transport.hs
+++ b/src/Network/Tox/DHT/Transport.hs
@@ -50,30 +50,30 @@ type HandleHi a = Maybe (Either String (DHTMessage Encrypted8, NodeInfo)) -> IO
50 50
51 51
52data DHTMessage (f :: * -> *) 52data DHTMessage (f :: * -> *)
53 = DHTPing (Assym (f Ping)) 53 = DHTPing (Asymm (f Ping))
54 | DHTPong (Assym (f Pong)) 54 | DHTPong (Asymm (f Pong))
55 | DHTGetNodes (Assym (f GetNodes)) 55 | DHTGetNodes (Asymm (f GetNodes))
56 | DHTSendNodes (Assym (f SendNodes)) 56 | DHTSendNodes (Asymm (f SendNodes))
57 | DHTCookieRequest (Assym (f CookieRequest)) 57 | DHTCookieRequest (Asymm (f CookieRequest))
58 | DHTCookie Nonce24 (f Cookie) 58 | DHTCookie Nonce24 (f Cookie)
59 | DHTDHTRequest PublicKey (Assym (f DHTRequest)) 59 | DHTDHTRequest PublicKey (Asymm (f DHTRequest))
60 60
61deriving instance ( Show (f Cookie) 61deriving instance ( Show (f Cookie)
62 , Show (Assym (f Ping)) 62 , Show (Asymm (f Ping))
63 , Show (Assym (f Pong)) 63 , Show (Asymm (f Pong))
64 , Show (Assym (f GetNodes)) 64 , Show (Asymm (f GetNodes))
65 , Show (Assym (f SendNodes)) 65 , Show (Asymm (f SendNodes))
66 , Show (Assym (f CookieRequest)) 66 , Show (Asymm (f CookieRequest))
67 , Show (Assym (f DHTRequest)) 67 , Show (Asymm (f DHTRequest))
68 ) => Show (DHTMessage f) 68 ) => Show (DHTMessage f)
69 69
70mapMessage :: forall f b. (forall a. Nonce24 -> f a -> b) -> DHTMessage f -> b 70mapMessage :: forall f b. (forall a. Nonce24 -> f a -> b) -> DHTMessage f -> b
71mapMessage f (DHTPing a) = f (assymNonce a) (assymData a) 71mapMessage f (DHTPing a) = f (asymmNonce a) (asymmData a)
72mapMessage f (DHTPong a) = f (assymNonce a) (assymData a) 72mapMessage f (DHTPong a) = f (asymmNonce a) (asymmData a)
73mapMessage f (DHTGetNodes a) = f (assymNonce a) (assymData a) 73mapMessage f (DHTGetNodes a) = f (asymmNonce a) (asymmData a)
74mapMessage f (DHTSendNodes a) = f (assymNonce a) (assymData a) 74mapMessage f (DHTSendNodes a) = f (asymmNonce a) (asymmData a)
75mapMessage f (DHTCookieRequest a) = f (assymNonce a) (assymData a) 75mapMessage f (DHTCookieRequest a) = f (asymmNonce a) (asymmData a)
76mapMessage f (DHTDHTRequest _ a) = f (assymNonce a) (assymData a) 76mapMessage f (DHTDHTRequest _ a) = f (asymmNonce a) (asymmData a)
77mapMessage f (DHTCookie nonce fcookie) = f nonce fcookie 77mapMessage f (DHTCookie nonce fcookie) = f nonce fcookie
78 78
79 79
@@ -99,13 +99,13 @@ encodeDHTAddr :: (DHTMessage Encrypted8,NodeInfo) -> (ByteString, SockAddr)
99encodeDHTAddr (msg,ni) = (runPut $ putMessage msg, nodeAddr ni) 99encodeDHTAddr (msg,ni) = (runPut $ putMessage msg, nodeAddr ni)
100 100
101dhtMessageType :: DHTMessage Encrypted8 -> ( Word8, Put ) 101dhtMessageType :: DHTMessage Encrypted8 -> ( Word8, Put )
102dhtMessageType (DHTPing a) = (0x00, putAssym a) 102dhtMessageType (DHTPing a) = (0x00, putAsymm a)
103dhtMessageType (DHTPong a) = (0x01, putAssym a) 103dhtMessageType (DHTPong a) = (0x01, putAsymm a)
104dhtMessageType (DHTGetNodes a) = (0x02, putAssym a) 104dhtMessageType (DHTGetNodes a) = (0x02, putAsymm a)
105dhtMessageType (DHTSendNodes a) = (0x04, putAssym a) 105dhtMessageType (DHTSendNodes a) = (0x04, putAsymm a)
106dhtMessageType (DHTCookieRequest a) = (0x18, putAssym a) 106dhtMessageType (DHTCookieRequest a) = (0x18, putAsymm a)
107dhtMessageType (DHTCookie n x) = (0x19, put n >> put x) 107dhtMessageType (DHTCookie n x) = (0x19, put n >> put x)
108dhtMessageType (DHTDHTRequest k a) = (0x20, putPublicKey k >> putAssym a) 108dhtMessageType (DHTDHTRequest k a) = (0x20, putPublicKey k >> putAsymm a)
109 109
110putMessage :: DHTMessage Encrypted8 -> Put 110putMessage :: DHTMessage Encrypted8 -> Put
111putMessage msg = case dhtMessageType msg of (typ,p) -> put typ >> p 111putMessage msg = case dhtMessageType msg of (typ,p) -> put typ >> p
@@ -113,8 +113,8 @@ putMessage msg = case dhtMessageType msg of (typ,p) -> put typ >> p
113getCookie :: Get (Nonce24, Encrypted8 Cookie) 113getCookie :: Get (Nonce24, Encrypted8 Cookie)
114getCookie = get 114getCookie = get
115 115
116getDHTReqest :: Get (PublicKey, Assym (Encrypted8 DHTRequest)) 116getDHTReqest :: Get (PublicKey, Asymm (Encrypted8 DHTRequest))
117getDHTReqest = (,) <$> getPublicKey <*> getAssym 117getDHTReqest = (,) <$> getPublicKey <*> getAsymm
118 118
119-- ## DHT Request packets 119-- ## DHT Request packets
120-- 120--
@@ -125,19 +125,19 @@ getDHTReqest = (,) <$> getPublicKey <*> getAssym
125-- ... ... 125-- ... ...
126 126
127 127
128getDHT :: Sized a => Get (Assym (Encrypted8 a)) 128getDHT :: Sized a => Get (Asymm (Encrypted8 a))
129getDHT = getAssym 129getDHT = getAsymm
130 130
131 131
132-- Throws an error if called with a non-internet socket. 132-- Throws an error if called with a non-internet socket.
133direct :: Sized a => ByteString 133direct :: Sized a => ByteString
134 -> SockAddr 134 -> SockAddr
135 -> (Assym (Encrypted8 a) -> DHTMessage Encrypted8) 135 -> (Asymm (Encrypted8 a) -> DHTMessage Encrypted8)
136 -> Either String (DHTMessage Encrypted8, NodeInfo) 136 -> Either String (DHTMessage Encrypted8, NodeInfo)
137direct bs saddr f = fanGet bs getDHT f (asymNodeInfo saddr) 137direct bs saddr f = fanGet bs getDHT f (asymNodeInfo saddr)
138 138
139-- Throws an error if called with a non-internet socket. 139-- Throws an error if called with a non-internet socket.
140asymNodeInfo :: SockAddr -> Assym a -> NodeInfo 140asymNodeInfo :: SockAddr -> Asymm a -> NodeInfo
141asymNodeInfo saddr asym = either (error . mappend "asymNodeInfo: ") id $ nodeInfo (key2id $ senderKey asym) saddr 141asymNodeInfo saddr asym = either (error . mappend "asymNodeInfo: ") id $ nodeInfo (key2id $ senderKey asym) saddr
142 142
143 143
@@ -351,11 +351,11 @@ encrypt crypto msg ni = ( transcode (encryptMessage crypto (id2key $ nodeId ni))
351encryptMessage :: Serialize a => 351encryptMessage :: Serialize a =>
352 TransportCrypto -> 352 TransportCrypto ->
353 PublicKey -> 353 PublicKey ->
354 Nonce24 -> Either (Nonce8,a) (Assym (Nonce8,a)) -> Encrypted8 a 354 Nonce24 -> Either (Nonce8,a) (Asymm (Nonce8,a)) -> Encrypted8 a
355encryptMessage crypto destKey n (Right assym) = E8 $ ToxCrypto.encrypt secret plain 355encryptMessage crypto destKey n (Right asymm) = E8 $ ToxCrypto.encrypt secret plain
356 where 356 where
357 secret = computeSharedSecret (transportSecret crypto) destKey n 357 secret = computeSharedSecret (transportSecret crypto) destKey n
358 plain = encodePlain $ swap $ assymData assym 358 plain = encodePlain $ swap $ asymmData asymm
359encryptMessage crypto destKey n (Left plain) = _todo -- need cached public key. 359encryptMessage crypto destKey n (Left plain) = _todo -- need cached public key.
360 360
361decrypt :: TransportCrypto -> DHTMessage Encrypted8 -> NodeInfo -> Either String (DHTMessage ((,) Nonce8), NodeInfo) 361decrypt :: TransportCrypto -> DHTMessage Encrypted8 -> NodeInfo -> Either String (DHTMessage ((,) Nonce8), NodeInfo)
@@ -364,12 +364,12 @@ decrypt crypto msg ni = (, ni) <$> (sequenceMessage $ transcode (decryptMessage
364decryptMessage :: Serialize x => 364decryptMessage :: Serialize x =>
365 TransportCrypto 365 TransportCrypto
366 -> Nonce24 366 -> Nonce24
367 -> Either (Encrypted8 x) (Assym (Encrypted8 x)) 367 -> Either (Encrypted8 x) (Asymm (Encrypted8 x))
368 -> (Either String ∘ ((,) Nonce8)) x 368 -> (Either String ∘ ((,) Nonce8)) x
369decryptMessage crypto n (Right assymE) = plain8 $ ToxCrypto.decrypt secret e 369decryptMessage crypto n (Right asymmE) = plain8 $ ToxCrypto.decrypt secret e
370 where 370 where
371 secret = computeSharedSecret (transportSecret crypto) (senderKey assymE) n 371 secret = computeSharedSecret (transportSecret crypto) (senderKey asymmE) n
372 E8 e = assymData assymE 372 E8 e = asymmData asymmE
373 plain8 = Composed . fmap swap . (>>= decodePlain) 373 plain8 = Composed . fmap swap . (>>= decodePlain)
374decryptMessage crypto n (Left (E8 e)) = _todo -- need cached public key 374decryptMessage crypto n (Left (E8 e)) = _todo -- need cached public key
375 375
@@ -382,11 +382,11 @@ sequenceMessage (DHTCookieRequest asym) = fmap DHTCookieRequest $ sequenceA
382sequenceMessage (DHTCookie n dta) = fmap (DHTCookie n) $ uncomposed dta 382sequenceMessage (DHTCookie n dta) = fmap (DHTCookie n) $ uncomposed dta
383sequenceMessage (DHTDHTRequest pubkey asym) = fmap (DHTDHTRequest pubkey) $ sequenceA $ fmap uncomposed asym 383sequenceMessage (DHTDHTRequest pubkey asym) = fmap (DHTDHTRequest pubkey) $ sequenceA $ fmap uncomposed asym
384 384
385transcode :: forall f g. (forall a. Serialize a => Nonce24 -> Either (f a) (Assym (f a)) -> g a) -> DHTMessage f -> DHTMessage g 385transcode :: forall f g. (forall a. Serialize a => Nonce24 -> Either (f a) (Asymm (f a)) -> g a) -> DHTMessage f -> DHTMessage g
386transcode f (DHTPing asym) = DHTPing $ asym { assymData = f (assymNonce asym) (Right asym) } 386transcode f (DHTPing asym) = DHTPing $ asym { asymmData = f (asymmNonce asym) (Right asym) }
387transcode f (DHTPong asym) = DHTPong $ asym { assymData = f (assymNonce asym) (Right asym) } 387transcode f (DHTPong asym) = DHTPong $ asym { asymmData = f (asymmNonce asym) (Right asym) }
388transcode f (DHTGetNodes asym) = DHTGetNodes $ asym { assymData = f (assymNonce asym) (Right asym) } 388transcode f (DHTGetNodes asym) = DHTGetNodes $ asym { asymmData = f (asymmNonce asym) (Right asym) }
389transcode f (DHTSendNodes asym) = DHTSendNodes $ asym { assymData = f (assymNonce asym) (Right asym) } 389transcode f (DHTSendNodes asym) = DHTSendNodes $ asym { asymmData = f (asymmNonce asym) (Right asym) }
390transcode f (DHTCookieRequest asym) = DHTCookieRequest $ asym { assymData = f (assymNonce asym) (Right asym) } 390transcode f (DHTCookieRequest asym) = DHTCookieRequest $ asym { asymmData = f (asymmNonce asym) (Right asym) }
391transcode f (DHTCookie n dta) = DHTCookie n $ f n $ Left dta 391transcode f (DHTCookie n dta) = DHTCookie n $ f n $ Left dta
392transcode f (DHTDHTRequest pubkey asym) = DHTDHTRequest pubkey $ asym { assymData = f (assymNonce asym) (Right asym) } 392transcode f (DHTDHTRequest pubkey asym) = DHTDHTRequest pubkey $ asym { asymmData = f (asymmNonce asym) (Right asym) }