summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT/Message.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-01-02 00:34:47 -0500
committerjoe <joe@jerkface.net>2017-01-02 00:37:07 -0500
commit5c2140807ceaa97a90c947437ecf5ef225f27c8d (patch)
tree650b6021af1b4c7a6c00ab4e5567424db959ece0 /src/Network/BitTorrent/DHT/Message.hs
parent80c860df8c29f99e0a82b454c1c0f0c6902764ef (diff)
Parse IPv4 addresses even when using variadic IP type.
Diffstat (limited to 'src/Network/BitTorrent/DHT/Message.hs')
-rw-r--r--src/Network/BitTorrent/DHT/Message.hs13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/Network/BitTorrent/DHT/Message.hs b/src/Network/BitTorrent/DHT/Message.hs
index 145141ee..008145de 100644
--- a/src/Network/BitTorrent/DHT/Message.hs
+++ b/src/Network/BitTorrent/DHT/Message.hs
@@ -59,6 +59,7 @@
59{-# LANGUAGE FlexibleInstances #-} 59{-# LANGUAGE FlexibleInstances #-}
60{-# LANGUAGE MultiParamTypeClasses #-} 60{-# LANGUAGE MultiParamTypeClasses #-}
61{-# LANGUAGE UndecidableInstances #-} 61{-# LANGUAGE UndecidableInstances #-}
62{-# LANGUAGE ScopedTypeVariables #-}
62module Network.BitTorrent.DHT.Message 63module Network.BitTorrent.DHT.Message
63 ( -- * Envelopes 64 ( -- * Envelopes
64 Query (..) 65 Query (..)
@@ -184,21 +185,27 @@ newtype NodeFound ip = NodeFound [NodeInfo ip]
184nodes_key :: BKey 185nodes_key :: BKey
185nodes_key = "nodes" 186nodes_key = "nodes"
186 187
188-- Convert IPv4 address. Useful for using variadic IP type.
189from4 :: forall s. Address s => NodeInfo IPv4 -> Either String (NodeInfo s)
190from4 n = maybe (Left "Error converting IPv4") Right
191 $ traverse (fromAddr :: IPv4 -> Maybe s) n
192
187binary :: Serialize a => BKey -> BE.Get [a] 193binary :: Serialize a => BKey -> BE.Get [a]
188binary k = field (req k) >>= either (fail . format) return . 194binary k = field (req k) >>= either (fail . format) return .
189 runGet (many get) 195 runGet (many get)
190 where 196 where
191 format str = "fail to deserialize " ++ show k ++ " field: " ++ str 197 format str = "fail to deserialize " ++ show k ++ " field: " ++ str
192 198
193instance (Typeable ip, Serialize ip) => BEncode (NodeFound ip) where 199instance (Typeable ip, Address ip) => BEncode (NodeFound ip) where
194 toBEncode (NodeFound ns) = toDict $ 200 toBEncode (NodeFound ns) = toDict $
195 nodes_key .=! runPut (mapM_ put ns) 201 nodes_key .=! runPut (mapM_ put ns)
196 .: endDict 202 .: endDict
197 203
198 fromBEncode = fromDict $ NodeFound <$> binary nodes_key 204 -- TODO: handle IPv6 by reading the "nodes6" key (see bep 32)
205 fromBEncode bval = NodeFound <$> (traverse from4 =<< fromDict (binary nodes_key) bval)
199 206
200-- | \"q\" == \"find_node\" 207-- | \"q\" == \"find_node\"
201instance (Serialize ip, Typeable ip) 208instance (Address ip, Typeable ip)
202 => KRPC (Query FindNode) (Response (NodeFound ip)) where 209 => KRPC (Query FindNode) (Response (NodeFound ip)) where
203 method = "find_node" 210 method = "find_node"
204 211