diff options
author | Daniel Gröber <dxld@darkboxed.org> | 2013-12-25 18:41:37 +0000 |
---|---|---|
committer | Daniel Gröber <dxld@darkboxed.org> | 2013-12-25 18:41:37 +0000 |
commit | 426e0a734acc4134636294624201ef15f15dc550 (patch) | |
tree | f381c1f0ea18b8bea9b7371fb168296961ab58ae /src | |
parent | 06f3f0ab66c872c36ef509e8b55477a7a0f481fc (diff) |
Add Serialize instance for IP
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/BitTorrent/Core/PeerAddr.hs | 14 |
1 files changed, 14 insertions, 0 deletions
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 | |||
132 | fromBEncode = ipFromBEncode | 132 | fromBEncode = ipFromBEncode |
133 | {-# INLINE fromBEncode #-} | 133 | {-# INLINE fromBEncode #-} |
134 | 134 | ||
135 | instance Serialize IP where | ||
136 | put (IPv4 ip) = put ip | ||
137 | put (IPv6 ip) = put ip | ||
138 | |||
139 | -- | When 'get'ing an IP it must be 'isolate'd to the appropriate number of | ||
140 | -- bytes since we have no other way of telling which address type we are | ||
141 | -- trying to parse | ||
142 | get = do | ||
143 | n <- remaining | ||
144 | case n of | ||
145 | 4 -> IPv4 <$> get | ||
146 | 16 -> IPv6 <$> get | ||
147 | _ -> fail "Wrong number of bytes remaining to parse IP" | ||
148 | |||
135 | instance Serialize IPv4 where | 149 | instance Serialize IPv4 where |
136 | put = putWord32host . toHostAddress | 150 | put = putWord32host . toHostAddress |
137 | get = fromHostAddress <$> getWord32host | 151 | get = fromHostAddress <$> getWord32host |