From 426e0a734acc4134636294624201ef15f15dc550 Mon Sep 17 00:00:00 2001 From: Daniel Gröber Date: Wed, 25 Dec 2013 18:41:37 +0000 Subject: Add Serialize instance for IP --- src/Network/BitTorrent/Core/PeerAddr.hs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/Network/BitTorrent/Core') diff --git a/src/Network/BitTorrent/Core/PeerAddr.hs b/src/Network/BitTorrent/Core/PeerAddr.hs index 86b88491..b735f8f2 100644 --- a/src/Network/BitTorrent/Core/PeerAddr.hs +++ b/src/Network/BitTorrent/Core/PeerAddr.hs @@ -132,6 +132,20 @@ instance BEncode IPv6 where fromBEncode = ipFromBEncode {-# INLINE fromBEncode #-} +instance Serialize IP where + put (IPv4 ip) = put ip + put (IPv6 ip) = put ip + + -- | When 'get'ing an IP it must be 'isolate'd to the appropriate number of + -- bytes since we have no other way of telling which address type we are + -- trying to parse + get = do + n <- remaining + case n of + 4 -> IPv4 <$> get + 16 -> IPv6 <$> get + _ -> fail "Wrong number of bytes remaining to parse IP" + instance Serialize IPv4 where put = putWord32host . toHostAddress get = fromHostAddress <$> getWord32host -- cgit v1.2.3