summaryrefslogtreecommitdiff
path: root/src/Network/Address.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/Address.hs')
-rw-r--r--src/Network/Address.hs34
1 files changed, 29 insertions, 5 deletions
diff --git a/src/Network/Address.hs b/src/Network/Address.hs
index 246463c0..e1cec34d 100644
--- a/src/Network/Address.hs
+++ b/src/Network/Address.hs
@@ -84,6 +84,9 @@ module Network.Address
84 , sockAddrPort 84 , sockAddrPort
85 , setPort 85 , setPort
86 , getBindAddress 86 , getBindAddress
87 , localhost4
88 , localhost6
89 , linesBy
87 ) where 90 ) where
88 91
89import Control.Applicative 92import Control.Applicative
@@ -109,7 +112,6 @@ import Data.IP hiding (fromSockAddr)
109import Data.IP 112import Data.IP
110#endif 113#endif
111import Data.List as L 114import Data.List as L
112import Data.List.Split as L
113import Data.Maybe (fromMaybe, catMaybes) 115import Data.Maybe (fromMaybe, catMaybes)
114import Data.Monoid 116import Data.Monoid
115import Data.Hashable 117import Data.Hashable
@@ -212,7 +214,8 @@ instance Serialize a => Serialize (NodeAddr a) where
212-- 214--
213instance IsString (NodeAddr IPv4) where 215instance IsString (NodeAddr IPv4) where
214 fromString str 216 fromString str
215 | [hostAddrStr, portStr] <- splitWhen (== ':') str 217 | (hostAddrStr, portStr0) <- L.break (== ':') str
218 , let portStr = L.drop 1 portStr0
216 , Just hostAddr <- readMaybe hostAddrStr 219 , Just hostAddr <- readMaybe hostAddrStr
217 , Just portNum <- toEnum <$> readMaybe portStr 220 , Just portNum <- toEnum <$> readMaybe portStr
218 = NodeAddr hostAddr portNum 221 = NodeAddr hostAddr portNum
@@ -540,7 +543,8 @@ instance Default PeerAddr where
540-- 543--
541instance IsString PeerAddr where 544instance IsString PeerAddr where
542 fromString str 545 fromString str
543 | [hostAddrStr, portStr] <- splitWhen (== ':') str 546 | (hostAddrStr, portStr0) <- L.break (== ':') str
547 , let portStr = L.drop 1 portStr0
544 , Just hostAddr <- readMaybe hostAddrStr 548 , Just hostAddr <- readMaybe hostAddrStr
545 , Just portNum <- toEnum <$> readMaybe portStr 549 , Just portNum <- toEnum <$> readMaybe portStr
546 = PeerAddr Nothing (IPv4 hostAddr) portNum 550 = PeerAddr Nothing (IPv4 hostAddr) portNum
@@ -958,6 +962,20 @@ instance Default Version where
958 def = Version [0] [] 962 def = Version [0] []
959 {-# INLINE def #-} 963 {-# INLINE def #-}
960 964
965dropLastIf :: (a -> Bool) -> [a] -> [a]
966dropLastIf pred [] = []
967dropLastIf pred (x:xs) = init' x xs
968 where init' y [] | pred y = []
969 init' y [] = [y]
970 init' y (z:zs) = y : init' z zs
971
972linesBy :: (a -> Bool) -> [a] -> [[a]]
973linesBy pred ys = dropLastIf L.null $ L.map dropDelim $ L.groupBy (\_ x -> not $ pred x) ys
974 where
975 dropDelim [] = []
976 dropDelim (x:xs) | pred x = xs
977 | otherwise = x:xs
978
961-- | For dot delimited version strings. 979-- | For dot delimited version strings.
962-- Example: @fromString \"0.1.0.2\" == Version [0, 1, 0, 2]@ 980-- Example: @fromString \"0.1.0.2\" == Version [0, 1, 0, 2]@
963-- 981--
@@ -966,7 +984,7 @@ instance IsString Version where
966 | Just nums <- chunkNums str = Version nums [] 984 | Just nums <- chunkNums str = Version nums []
967 | otherwise = error $ "fromString: invalid version string " ++ str 985 | otherwise = error $ "fromString: invalid version string " ++ str
968 where 986 where
969 chunkNums = sequence . L.map readMaybe . L.linesBy ('.' ==) 987 chunkNums = sequence . L.map readMaybe . linesBy ('.' ==)
970 988
971instance Pretty Version where 989instance Pretty Version where
972 pPrint = text . showVersion 990 pPrint = text . showVersion
@@ -1120,7 +1138,7 @@ fingerprint pid = either (const def) id $ runGet getCI (getPeerId pid)
1120 1138
1121 getMainlineVersion = do 1139 getMainlineVersion = do
1122 str <- BC.unpack <$> getByteString 7 1140 str <- BC.unpack <$> getByteString 7
1123 let mnums = L.filter (not . L.null) $ L.linesBy ('-' ==) str 1141 let mnums = L.filter (not . L.null) $ linesBy ('-' ==) str
1124 return $ Version (fromMaybe [] $ sequence $ L.map readMaybe mnums) [] 1142 return $ Version (fromMaybe [] $ sequence $ L.map readMaybe mnums) []
1125 1143
1126 getAzureusImpl = parseSoftware <$> getByteString 2 1144 getAzureusImpl = parseSoftware <$> getByteString 2
@@ -1227,3 +1245,9 @@ either4or6 a6@(SockAddrInet6 port _ addr _)
1227data WantIP = Want_IP4 | Want_IP6 | Want_Both 1245data WantIP = Want_IP4 | Want_IP6 | Want_Both
1228 deriving (Eq, Enum, Ord, Show) 1246 deriving (Eq, Enum, Ord, Show)
1229 1247
1248localhost6 :: SockAddr
1249localhost6 = SockAddrInet6 0 0 (0,0,0,1) 0 -- [::1]:0
1250
1251localhost4 :: SockAddr
1252localhost4 = SockAddrInet 0 16777343 -- 127.0.0.1:0
1253