diff options
author | joe <joe@jerkface.net> | 2017-01-02 00:34:47 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-01-02 00:37:07 -0500 |
commit | 5c2140807ceaa97a90c947437ecf5ef225f27c8d (patch) | |
tree | 650b6021af1b4c7a6c00ab4e5567424db959ece0 /src/Network/BitTorrent/DHT/Message.hs | |
parent | 80c860df8c29f99e0a82b454c1c0f0c6902764ef (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.hs | 13 |
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 #-} | ||
62 | module Network.BitTorrent.DHT.Message | 63 | module Network.BitTorrent.DHT.Message |
63 | ( -- * Envelopes | 64 | ( -- * Envelopes |
64 | Query (..) | 65 | Query (..) |
@@ -184,21 +185,27 @@ newtype NodeFound ip = NodeFound [NodeInfo ip] | |||
184 | nodes_key :: BKey | 185 | nodes_key :: BKey |
185 | nodes_key = "nodes" | 186 | nodes_key = "nodes" |
186 | 187 | ||
188 | -- Convert IPv4 address. Useful for using variadic IP type. | ||
189 | from4 :: forall s. Address s => NodeInfo IPv4 -> Either String (NodeInfo s) | ||
190 | from4 n = maybe (Left "Error converting IPv4") Right | ||
191 | $ traverse (fromAddr :: IPv4 -> Maybe s) n | ||
192 | |||
187 | binary :: Serialize a => BKey -> BE.Get [a] | 193 | binary :: Serialize a => BKey -> BE.Get [a] |
188 | binary k = field (req k) >>= either (fail . format) return . | 194 | binary 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 | ||
193 | instance (Typeable ip, Serialize ip) => BEncode (NodeFound ip) where | 199 | instance (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\" |
201 | instance (Serialize ip, Typeable ip) | 208 | instance (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 | ||