kazu-yamamoto / iproute

IP Routing Table in Haskell
http://www.mew.org/~kazu/proj/iproute/
BSD 3-Clause "New" or "Revised" License
47 stars 28 forks source link

Add parse from ByteString #33

Open tonal opened 8 years ago

tonal commented 8 years ago

Add parse from ByteString and Text

kazu-yamamoto commented 8 years ago

Are you talking about IP?

tonal commented 8 years ago

Yes. I parse very big apache log files and find each IP in RouteTable.

kazu-yamamoto commented 8 years ago

OK. Could you implement it by yourself and give me a pull request?

tonal commented 8 years ago

I work throuth 4Int list:

import Control.Applicative
import Data.Attoparsec.ByteString
import qualified Data.ByteString.Char8 as BC
import Data.ByteString.Internal (c2w)

ip2list :: BC.ByteString -> [Int]
ip2list ipstr =
  case parseOnly pIP2list ipstr of
    Left msg -> error msg
    Right ip -> ip

pIP2list :: Parser [Int]
pIP2list = toIP <$> dd <*> dd <*> dd <*> dig
  where
    toIP a b c d = [a, b, c, d]
    dig = AC.decimal
    dd = dig <* skip (== c2w '.')

And my ipFilter:

ipFiltr :: IPRTable IPv4 () -> LogLine -> Bool
ipFiltr bans
  = DM.isNothing . flip lookup bans
  . flip makeAddrRange 32 . toIPv4 . ip2list . getLogIP