summaryrefslogtreecommitdiff
path: root/src/Network/Address.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-29 03:13:26 -0400
committerjoe <joe@jerkface.net>2017-07-29 03:13:26 -0400
commit0ef0bfedc65cc67cbe8ad66ab9ae2fb9ae20b7f3 (patch)
tree0b0653612e649bde9900b4a16cb3332c6f13e34c /src/Network/Address.hs
parentf876da224f503542394b3d7614fcc161106ebbb4 (diff)
Refactoring for tox/mainline code-sharing.
Diffstat (limited to 'src/Network/Address.hs')
-rw-r--r--src/Network/Address.hs37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/Network/Address.hs b/src/Network/Address.hs
index 8715a82d..cc06ac0d 100644
--- a/src/Network/Address.hs
+++ b/src/Network/Address.hs
@@ -35,6 +35,11 @@ module Network.Address
35 , IPv4 35 , IPv4
36 , IPv6 36 , IPv6
37 , IP (..) 37 , IP (..)
38 , un4map
39 , WantIP (..)
40 , ipFamily
41 , is4mapped
42 , either4or6
38 43
39 -- * PeerId 44 -- * PeerId
40 -- $peer-id 45 -- $peer-id
@@ -1180,3 +1185,35 @@ getBindAddress listenPortString enabled6 = do
1180 else SockAddrInet (parsePort listenPortString) iNADDR_ANY 1185 else SockAddrInet (parsePort listenPortString) iNADDR_ANY
1181 where parsePort s = fromMaybe 0 $ readMaybe s 1186 where parsePort s = fromMaybe 0 $ readMaybe s
1182 return listenAddr 1187 return listenAddr
1188
1189-- | True if the argument is an IPv4-mapped address with prefix ::FFFF:0:0/96
1190-- as defined in RFC 4291.
1191is4mapped :: IPv6 -> Bool
1192is4mapped ip
1193 | [0,0,0,0,0,0xffff,_,_] <- fromIPv6 ip
1194 = True
1195 | otherwise = False
1196
1197un4map :: IPv6 -> Maybe IPv4
1198un4map ip
1199 | [0,0,0,0,0,0xffff,x,y] <- fromIPv6 ip
1200 = Just $ toIPv4
1201 $ L.map (.&. 0xFF)
1202 [x `shiftR` 8, x, y `shiftR` 8, y ]
1203 | otherwise = Nothing
1204
1205ipFamily :: IP -> WantIP
1206ipFamily ip = case ip of
1207 IPv4 _ -> Want_IP4
1208 IPv6 a | is4mapped a -> Want_IP4
1209 | otherwise -> Want_IP6
1210
1211either4or6 :: SockAddr -> Either SockAddr SockAddr
1212either4or6 a4@(SockAddrInet port addr) = Left a4
1213either4or6 a6@(SockAddrInet6 port _ addr _)
1214 | Just ip4 <- (fromSockAddr a6 >>= un4map) = Left (setPort port $ toSockAddr ip4)
1215 | otherwise = Right a6
1216
1217data WantIP = Want_IP4 | Want_IP6 | Want_Both
1218 deriving (Eq, Enum, Ord, Show)
1219