diff options
Diffstat (limited to 'Hosts.hs')
-rw-r--r-- | Hosts.hs | 21 |
1 files changed, 16 insertions, 5 deletions
@@ -7,6 +7,7 @@ | |||
7 | module Hosts | 7 | module Hosts |
8 | ( Hosts | 8 | ( Hosts |
9 | , assignName | 9 | , assignName |
10 | , assignName' | ||
10 | , assignNewName | 11 | , assignNewName |
11 | , inet_pton | 12 | , inet_pton |
12 | , inet_ntop | 13 | , inet_ntop |
@@ -145,7 +146,7 @@ scrubName f line = line' | |||
145 | in L.concat (a'++ws'') <> vs''' | 146 | in L.concat (a'++ws'') <> vs''' |
146 | 147 | ||
147 | assignName :: SockAddr -> L.ByteString -> Hosts -> Hosts | 148 | assignName :: SockAddr -> L.ByteString -> Hosts -> Hosts |
148 | assignName addr name hosts = assignName0 False addr name hosts | 149 | assignName addr name hosts = assignName' False addr name hosts |
149 | 150 | ||
150 | chaddr :: Int -> SockAddr -> Hosts -> Hosts | 151 | chaddr :: Int -> SockAddr -> Hosts -> Hosts |
151 | chaddr n addr hosts = | 152 | chaddr n addr hosts = |
@@ -177,14 +178,24 @@ scrubTrailingEmpties hosts = | |||
177 | $ [cnt,cnt-1..] | 178 | $ [cnt,cnt-1..] |
178 | cnt' = cnt - length es | 179 | cnt' = cnt - length es |
179 | 180 | ||
180 | assignName0 :: Bool -> SockAddr -> L.ByteString -> Hosts -> Hosts | 181 | cannonizeName :: L.ByteString -> L.ByteString -> L.ByteString |
181 | assignName0 iscannon addr name hosts = hosts' | 182 | cannonizeName name line = scrubName f line |
183 | where | ||
184 | f ws = [name," "] ++ pre ++ drop 2 rst | ||
185 | where | ||
186 | (pre,rst) = break (==name) ws | ||
187 | |||
188 | |||
189 | assignName' :: Bool -> SockAddr -> L.ByteString -> Hosts -> Hosts | ||
190 | assignName' iscannon addr name hosts = hosts' | ||
182 | where | 191 | where |
183 | ns = Map.lookup name (namenum hosts) | 192 | ns = Map.lookup name (namenum hosts) |
184 | a = Map.lookup addr (addrnum hosts) | 193 | a = Map.lookup addr (addrnum hosts) |
194 | canonize numline n = Map.adjust (cannonizeName name) n numline | ||
185 | hosts' = do | 195 | hosts' = do |
186 | if (== Just True) $ elem <$> a <*> ns | 196 | if (== Just True) $ elem <$> a <*> ns |
187 | then hosts -- address already has name, nothing to do | 197 | then if not iscannon then hosts -- address already has name, nothing to do |
198 | else hosts { numline = foldl' canonize (numline hosts) $ fromJust ns} | ||
188 | else | 199 | else |
189 | let hosts0 = -- remove name if it's present | 200 | let hosts0 = -- remove name if it's present |
190 | scrubTrailingEmpties $ maybe hosts (removeName hosts) ns | 201 | scrubTrailingEmpties $ maybe hosts (removeName hosts) ns |
@@ -219,7 +230,7 @@ assignName0 iscannon addr name hosts = hosts' | |||
219 | assignNewName :: SockAddr -> L.ByteString -> Hosts -> Hosts | 230 | assignNewName :: SockAddr -> L.ByteString -> Hosts -> Hosts |
220 | assignNewName addr name hosts = | 231 | assignNewName addr name hosts = |
221 | if hasName name hosts then hosts | 232 | if hasName name hosts then hosts |
222 | else assignName0 True addr name hosts | 233 | else assignName' True addr name hosts |
223 | 234 | ||
224 | appendName :: Bool -> L.ByteString -> Hosts -> Int -> Hosts | 235 | appendName :: Bool -> L.ByteString -> Hosts -> Int -> Hosts |
225 | appendName iscannon name hosts num = hosts | 236 | appendName iscannon name hosts num = hosts |