- mgr <- newManager def
- forever $ do
- l <- getLine
- case pdnsParse ver l of
- Left e -> putStrLn $ "ERROR\t" ++ e
- Right preq -> do
- case preq of
- PdnsRequestQ qname qtype id _ _ _ -> do
- ncres <- queryNmc mgr cfg qname id
- case ncres of
- Left e -> putStrLn $ "ERROR\t" ++ e
- Right dom -> putStrLn $ pdnsOut qtype dom
- PdnsRequestAXFR xfrreq ->
- putStrLn ("ERROR\tNo support for AXFR " ++ xfrreq)
- PdnsRequestPing -> putStrLn "OK"
-
--- for testing
-
-ask str = do
+ mgr <- newManager defaultManagerSettings
+
+ let
+ fetch = lookup
+ -- Save the name under current count, increment count for the next run
+ -- so the name is saved under the count that was put into the response.
+ stow name (count, cache) =
+ (if count >= 99 then 0 else count + 1
+ , insert count name
+ $ delete (if count >= 10 then count - 10 else count + 90) cache
+ )
+ io = liftIO
+
+ mainloop = forever $ do
+ l <- io getLine
+ gen <- io $ nmcAge
+ (count, cache) <- get
+ case pdnsParse ver l of
+ Left e -> io $ putStr $ pdnsReport e
+ Right preq -> do
+ case preq of
+ PdnsRequestQ qname qtype id _ _ _ -> do
+ io $ queryDom (queryOpNmc cfg mgr id) qname
+ >>= putStr . (pdnsOutQ ver count gen qname qtype)
+ {-
+ -- debug
+ io $ putStrLn $ "LOG\tRequest number " ++ (show count)
+ ++ " id: " ++ (show id)
+ ++ " qname: " ++ qname
+ ++ " qtype: " ++ (show qtype)
+ ++ " cache size: " ++ (show (size cache))
+ -- end debug
+ -}
+ put $ stow qname (count, cache)
+ PdnsRequestAXFR xrq zid -> do
+ {-
+ -- debug
+ io $ putStrLn $ "LOG\tAXFR request id=" ++ (show xrq)
+ ++ ", zone name: " ++ (show zid)
+ -- end debug
+ -}
+ let
+ czone = fetch xrq cache
+ zone = case zid of
+ Nothing -> czone
+ Just qname -> Just qname
+ -- if zid == czone then zid else Nothing -- paranoid
+ case zone of
+ Just qname ->
+ io $ queryDom (queryOpNmc cfg mgr xrq) qname
+ >>= putStr . (pdnsOutXfr ver count gen qname)
+ Nothing ->
+ io $ putStr $ pdnsReport $ "AXFR cannot determine zone: "
+ ++ (show xrq) ++ ", " ++ (show zid)
+ PdnsRequestPing -> io $ putStrLn "END"
+
+ runStateT mainloop (0, empty) >> return ()
+
+-- helper for command-line tools
+
+pdnsOut gen key qt dom =
+ case qt of
+ "AXFR" -> pdnsOutXfr 1 (-1) gen key dom
+ _ -> pdnsOutQ 1 (-1) gen key (rrType qt) dom
+
+-- run one query by key from Namecoin, print domain object and answer
+
+mainOne gen key qt = do