X-Git-Url: http://www.average.org/gitweb/?p=pdns-pipe-nmc.git;a=blobdiff_plain;f=NmcJson.hs;h=69e693ff000e7ea5aafb986e5c45f017a1c60d05;hp=c92e363ce4b6a6f5d92e10f25806c8e58e45adf3;hb=f2dbb98dde41abe69d0bc9c3ee70eda0eeb12670;hpb=3677a8d0e1164eb4e7ba481a5e0d025fb1193005 diff --git a/NmcJson.hs b/NmcJson.hs index c92e363..69e693f 100644 --- a/NmcJson.hs +++ b/NmcJson.hs @@ -7,6 +7,9 @@ module NmcJson ( NmcRes(..) ) where import Data.ByteString.Lazy (ByteString) +import Data.Text as T (unpack) +import Data.List.Split +import Data.Char import Data.Map as M (Map, lookup) import Control.Applicative ((<$>), (<*>), empty) import Data.Aeson @@ -65,6 +68,19 @@ data NmcDom = NmcDom { domService :: Maybe [[String]] -- [NmcRRService] } deriving (Show, Eq) instance FromJSON NmcDom where + -- Wherever we expect a domain object, there may be a string + -- containing IPv4 address. Interpret it as such. + -- Question: shall we try to recognize IPv6 addresses too? + parseJSON (String s) = + return $ if isIPv4 s' + then emptyNmcDom { domIp = Just [s'] } + else emptyNmcDom + where + s' = T.unpack s + isIPv4 x = all isNibble $ splitOn "." x + isNibble x = + if all isDigit x then (read x :: Int) < 256 + else False parseJSON (Object o) = NmcDom <$> o .:? "service" <*> o .:? "ip"