diff options
Diffstat (limited to 'src/Network/Address.hs')
-rw-r--r-- | src/Network/Address.hs | 34 |
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 | ||
89 | import Control.Applicative | 92 | import Control.Applicative |
@@ -109,7 +112,6 @@ import Data.IP hiding (fromSockAddr) | |||
109 | import Data.IP | 112 | import Data.IP |
110 | #endif | 113 | #endif |
111 | import Data.List as L | 114 | import Data.List as L |
112 | import Data.List.Split as L | ||
113 | import Data.Maybe (fromMaybe, catMaybes) | 115 | import Data.Maybe (fromMaybe, catMaybes) |
114 | import Data.Monoid | 116 | import Data.Monoid |
115 | import Data.Hashable | 117 | import Data.Hashable |
@@ -212,7 +214,8 @@ instance Serialize a => Serialize (NodeAddr a) where | |||
212 | -- | 214 | -- |
213 | instance IsString (NodeAddr IPv4) where | 215 | instance 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 | -- |
541 | instance IsString PeerAddr where | 544 | instance 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 | ||
965 | dropLastIf :: (a -> Bool) -> [a] -> [a] | ||
966 | dropLastIf pred [] = [] | ||
967 | dropLastIf 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 | |||
972 | linesBy :: (a -> Bool) -> [a] -> [[a]] | ||
973 | linesBy 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 | ||
971 | instance Pretty Version where | 989 | instance 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 _) | |||
1227 | data WantIP = Want_IP4 | Want_IP6 | Want_Both | 1245 | data WantIP = Want_IP4 | Want_IP6 | Want_Both |
1228 | deriving (Eq, Enum, Ord, Show) | 1246 | deriving (Eq, Enum, Ord, Show) |
1229 | 1247 | ||
1248 | localhost6 :: SockAddr | ||
1249 | localhost6 = SockAddrInet6 0 0 (0,0,0,1) 0 -- [::1]:0 | ||
1250 | |||
1251 | localhost4 :: SockAddr | ||
1252 | localhost4 = SockAddrInet 0 16777343 -- 127.0.0.1:0 | ||
1253 | |||