diff options
Diffstat (limited to 'src/Network/Tox/DHT/Transport.hs')
-rw-r--r-- | src/Network/Tox/DHT/Transport.hs | 88 |
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 | ||
52 | data DHTMessage (f :: * -> *) | 52 | data 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 | ||
61 | deriving instance ( Show (f Cookie) | 61 | deriving 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 | ||
70 | mapMessage :: forall f b. (forall a. Nonce24 -> f a -> b) -> DHTMessage f -> b | 70 | mapMessage :: forall f b. (forall a. Nonce24 -> f a -> b) -> DHTMessage f -> b |
71 | mapMessage f (DHTPing a) = f (assymNonce a) (assymData a) | 71 | mapMessage f (DHTPing a) = f (asymmNonce a) (asymmData a) |
72 | mapMessage f (DHTPong a) = f (assymNonce a) (assymData a) | 72 | mapMessage f (DHTPong a) = f (asymmNonce a) (asymmData a) |
73 | mapMessage f (DHTGetNodes a) = f (assymNonce a) (assymData a) | 73 | mapMessage f (DHTGetNodes a) = f (asymmNonce a) (asymmData a) |
74 | mapMessage f (DHTSendNodes a) = f (assymNonce a) (assymData a) | 74 | mapMessage f (DHTSendNodes a) = f (asymmNonce a) (asymmData a) |
75 | mapMessage f (DHTCookieRequest a) = f (assymNonce a) (assymData a) | 75 | mapMessage f (DHTCookieRequest a) = f (asymmNonce a) (asymmData a) |
76 | mapMessage f (DHTDHTRequest _ a) = f (assymNonce a) (assymData a) | 76 | mapMessage f (DHTDHTRequest _ a) = f (asymmNonce a) (asymmData a) |
77 | mapMessage f (DHTCookie nonce fcookie) = f nonce fcookie | 77 | mapMessage f (DHTCookie nonce fcookie) = f nonce fcookie |
78 | 78 | ||
79 | 79 | ||
@@ -99,13 +99,13 @@ encodeDHTAddr :: (DHTMessage Encrypted8,NodeInfo) -> (ByteString, SockAddr) | |||
99 | encodeDHTAddr (msg,ni) = (runPut $ putMessage msg, nodeAddr ni) | 99 | encodeDHTAddr (msg,ni) = (runPut $ putMessage msg, nodeAddr ni) |
100 | 100 | ||
101 | dhtMessageType :: DHTMessage Encrypted8 -> ( Word8, Put ) | 101 | dhtMessageType :: DHTMessage Encrypted8 -> ( Word8, Put ) |
102 | dhtMessageType (DHTPing a) = (0x00, putAssym a) | 102 | dhtMessageType (DHTPing a) = (0x00, putAsymm a) |
103 | dhtMessageType (DHTPong a) = (0x01, putAssym a) | 103 | dhtMessageType (DHTPong a) = (0x01, putAsymm a) |
104 | dhtMessageType (DHTGetNodes a) = (0x02, putAssym a) | 104 | dhtMessageType (DHTGetNodes a) = (0x02, putAsymm a) |
105 | dhtMessageType (DHTSendNodes a) = (0x04, putAssym a) | 105 | dhtMessageType (DHTSendNodes a) = (0x04, putAsymm a) |
106 | dhtMessageType (DHTCookieRequest a) = (0x18, putAssym a) | 106 | dhtMessageType (DHTCookieRequest a) = (0x18, putAsymm a) |
107 | dhtMessageType (DHTCookie n x) = (0x19, put n >> put x) | 107 | dhtMessageType (DHTCookie n x) = (0x19, put n >> put x) |
108 | dhtMessageType (DHTDHTRequest k a) = (0x20, putPublicKey k >> putAssym a) | 108 | dhtMessageType (DHTDHTRequest k a) = (0x20, putPublicKey k >> putAsymm a) |
109 | 109 | ||
110 | putMessage :: DHTMessage Encrypted8 -> Put | 110 | putMessage :: DHTMessage Encrypted8 -> Put |
111 | putMessage msg = case dhtMessageType msg of (typ,p) -> put typ >> p | 111 | putMessage 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 | |||
113 | getCookie :: Get (Nonce24, Encrypted8 Cookie) | 113 | getCookie :: Get (Nonce24, Encrypted8 Cookie) |
114 | getCookie = get | 114 | getCookie = get |
115 | 115 | ||
116 | getDHTReqest :: Get (PublicKey, Assym (Encrypted8 DHTRequest)) | 116 | getDHTReqest :: Get (PublicKey, Asymm (Encrypted8 DHTRequest)) |
117 | getDHTReqest = (,) <$> getPublicKey <*> getAssym | 117 | getDHTReqest = (,) <$> 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 | ||
128 | getDHT :: Sized a => Get (Assym (Encrypted8 a)) | 128 | getDHT :: Sized a => Get (Asymm (Encrypted8 a)) |
129 | getDHT = getAssym | 129 | getDHT = 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. |
133 | direct :: Sized a => ByteString | 133 | direct :: 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) |
137 | direct bs saddr f = fanGet bs getDHT f (asymNodeInfo saddr) | 137 | direct 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. |
140 | asymNodeInfo :: SockAddr -> Assym a -> NodeInfo | 140 | asymNodeInfo :: SockAddr -> Asymm a -> NodeInfo |
141 | asymNodeInfo saddr asym = either (error . mappend "asymNodeInfo: ") id $ nodeInfo (key2id $ senderKey asym) saddr | 141 | asymNodeInfo 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)) | |||
351 | encryptMessage :: Serialize a => | 351 | encryptMessage :: 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 |
355 | encryptMessage crypto destKey n (Right assym) = E8 $ ToxCrypto.encrypt secret plain | 355 | encryptMessage 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 |
359 | encryptMessage crypto destKey n (Left plain) = _todo -- need cached public key. | 359 | encryptMessage crypto destKey n (Left plain) = _todo -- need cached public key. |
360 | 360 | ||
361 | decrypt :: TransportCrypto -> DHTMessage Encrypted8 -> NodeInfo -> Either String (DHTMessage ((,) Nonce8), NodeInfo) | 361 | decrypt :: TransportCrypto -> DHTMessage Encrypted8 -> NodeInfo -> Either String (DHTMessage ((,) Nonce8), NodeInfo) |
@@ -364,12 +364,12 @@ decrypt crypto msg ni = (, ni) <$> (sequenceMessage $ transcode (decryptMessage | |||
364 | decryptMessage :: Serialize x => | 364 | decryptMessage :: 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 |
369 | decryptMessage crypto n (Right assymE) = plain8 $ ToxCrypto.decrypt secret e | 369 | decryptMessage 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) |
374 | decryptMessage crypto n (Left (E8 e)) = _todo -- need cached public key | 374 | decryptMessage crypto n (Left (E8 e)) = _todo -- need cached public key |
375 | 375 | ||
@@ -382,11 +382,11 @@ sequenceMessage (DHTCookieRequest asym) = fmap DHTCookieRequest $ sequenceA | |||
382 | sequenceMessage (DHTCookie n dta) = fmap (DHTCookie n) $ uncomposed dta | 382 | sequenceMessage (DHTCookie n dta) = fmap (DHTCookie n) $ uncomposed dta |
383 | sequenceMessage (DHTDHTRequest pubkey asym) = fmap (DHTDHTRequest pubkey) $ sequenceA $ fmap uncomposed asym | 383 | sequenceMessage (DHTDHTRequest pubkey asym) = fmap (DHTDHTRequest pubkey) $ sequenceA $ fmap uncomposed asym |
384 | 384 | ||
385 | transcode :: forall f g. (forall a. Serialize a => Nonce24 -> Either (f a) (Assym (f a)) -> g a) -> DHTMessage f -> DHTMessage g | 385 | transcode :: forall f g. (forall a. Serialize a => Nonce24 -> Either (f a) (Asymm (f a)) -> g a) -> DHTMessage f -> DHTMessage g |
386 | transcode f (DHTPing asym) = DHTPing $ asym { assymData = f (assymNonce asym) (Right asym) } | 386 | transcode f (DHTPing asym) = DHTPing $ asym { asymmData = f (asymmNonce asym) (Right asym) } |
387 | transcode f (DHTPong asym) = DHTPong $ asym { assymData = f (assymNonce asym) (Right asym) } | 387 | transcode f (DHTPong asym) = DHTPong $ asym { asymmData = f (asymmNonce asym) (Right asym) } |
388 | transcode f (DHTGetNodes asym) = DHTGetNodes $ asym { assymData = f (assymNonce asym) (Right asym) } | 388 | transcode f (DHTGetNodes asym) = DHTGetNodes $ asym { asymmData = f (asymmNonce asym) (Right asym) } |
389 | transcode f (DHTSendNodes asym) = DHTSendNodes $ asym { assymData = f (assymNonce asym) (Right asym) } | 389 | transcode f (DHTSendNodes asym) = DHTSendNodes $ asym { asymmData = f (asymmNonce asym) (Right asym) } |
390 | transcode f (DHTCookieRequest asym) = DHTCookieRequest $ asym { assymData = f (assymNonce asym) (Right asym) } | 390 | transcode f (DHTCookieRequest asym) = DHTCookieRequest $ asym { asymmData = f (asymmNonce asym) (Right asym) } |
391 | transcode f (DHTCookie n dta) = DHTCookie n $ f n $ Left dta | 391 | transcode f (DHTCookie n dta) = DHTCookie n $ f n $ Left dta |
392 | transcode f (DHTDHTRequest pubkey asym) = DHTDHTRequest pubkey $ asym { assymData = f (assymNonce asym) (Right asym) } | 392 | transcode f (DHTDHTRequest pubkey asym) = DHTDHTRequest pubkey $ asym { asymmData = f (asymmNonce asym) (Right asym) } |