2012-10-25 21:56:03 +00:00
|
|
|
{- SRV record lookup
|
|
|
|
-
|
2012-10-28 23:14:30 +00:00
|
|
|
- Uses either the ADNS Haskell library, or the standalone Haskell DNS
|
|
|
|
- package, or the host command.
|
2012-10-25 21:56:03 +00:00
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-10-25 21:56:03 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
|
|
|
module Utility.SRV (
|
2012-10-26 17:03:08 +00:00
|
|
|
mkSRVTcp,
|
|
|
|
mkSRV,
|
2012-10-25 21:56:03 +00:00
|
|
|
lookupSRV,
|
2012-11-29 22:40:25 +00:00
|
|
|
lookupSRVHost,
|
2013-05-27 18:36:20 +00:00
|
|
|
HostPort,
|
2012-10-25 21:56:03 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Utility.Process
|
|
|
|
import Utility.Exception
|
|
|
|
import Utility.PartialPrelude
|
|
|
|
|
|
|
|
import Network
|
|
|
|
import Data.Function
|
|
|
|
import Data.List
|
|
|
|
import Control.Applicative
|
|
|
|
import Data.Maybe
|
|
|
|
|
|
|
|
#ifdef WITH_ADNS
|
|
|
|
import ADNS.Resolver
|
|
|
|
import Data.Either
|
2012-10-28 23:14:30 +00:00
|
|
|
#else
|
|
|
|
#ifdef WITH_DNS
|
|
|
|
import qualified Network.DNS.Lookup as DNS
|
|
|
|
import Network.DNS.Resolver
|
|
|
|
import qualified Data.ByteString.UTF8 as B8
|
|
|
|
#endif
|
2012-10-25 21:56:03 +00:00
|
|
|
#endif
|
|
|
|
|
2012-10-26 17:03:08 +00:00
|
|
|
newtype SRV = SRV String
|
2012-10-26 18:17:09 +00:00
|
|
|
deriving (Show, Eq)
|
2012-10-26 17:03:08 +00:00
|
|
|
|
2012-10-25 21:56:03 +00:00
|
|
|
type HostPort = (HostName, PortID)
|
|
|
|
|
2012-10-28 23:14:30 +00:00
|
|
|
type PriorityWeight = (Int, Int) -- sort by priority first, then weight
|
|
|
|
|
2012-10-26 17:03:08 +00:00
|
|
|
mkSRV :: String -> String -> HostName -> SRV
|
|
|
|
mkSRV transport protocol host = SRV $ concat
|
2012-10-26 18:17:09 +00:00
|
|
|
["_", protocol, "._", transport, ".", host]
|
2012-10-26 17:03:08 +00:00
|
|
|
|
|
|
|
mkSRVTcp :: String -> HostName -> SRV
|
|
|
|
mkSRVTcp = mkSRV "tcp"
|
|
|
|
|
2012-10-25 21:56:03 +00:00
|
|
|
{- Returns an ordered list, with highest priority hosts first.
|
|
|
|
-
|
|
|
|
- On error, returns an empty list. -}
|
2012-10-26 17:03:08 +00:00
|
|
|
lookupSRV :: SRV -> IO [HostPort]
|
2012-10-25 21:56:03 +00:00
|
|
|
#ifdef WITH_ADNS
|
2012-10-28 23:14:30 +00:00
|
|
|
lookupSRV (SRV srv) = initResolver [] $ \resolver -> do
|
2012-10-25 21:56:03 +00:00
|
|
|
r <- catchDefaultIO (Right []) $
|
|
|
|
resolveSRV resolver srv
|
|
|
|
return $ either (\_ -> []) id r
|
|
|
|
#else
|
2012-10-28 23:14:30 +00:00
|
|
|
#ifdef WITH_DNS
|
|
|
|
lookupSRV (SRV srv) = do
|
|
|
|
seed <- makeResolvSeed defaultResolvConf
|
|
|
|
r <- withResolver seed $ flip DNS.lookupSRV $ B8.fromString srv
|
2013-09-17 15:54:09 +00:00
|
|
|
return $
|
|
|
|
#if MIN_VERSION_dns(1,0,0)
|
|
|
|
either (const []) use r
|
|
|
|
#else
|
|
|
|
maybe [] use r
|
|
|
|
#endif
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
use = orderHosts . map tohosts
|
2012-12-13 04:24:19 +00:00
|
|
|
tohosts (priority, weight, port, hostname) =
|
|
|
|
( (priority, weight)
|
|
|
|
, (B8.toString hostname, PortNumber $ fromIntegral port)
|
|
|
|
)
|
2012-10-28 23:14:30 +00:00
|
|
|
#else
|
2012-10-25 21:56:03 +00:00
|
|
|
lookupSRV = lookupSRVHost
|
|
|
|
#endif
|
2012-10-28 23:14:30 +00:00
|
|
|
#endif
|
2012-10-25 21:56:03 +00:00
|
|
|
|
2012-10-26 17:03:08 +00:00
|
|
|
lookupSRVHost :: SRV -> IO [HostPort]
|
2012-10-30 23:28:46 +00:00
|
|
|
lookupSRVHost (SRV srv) = catchDefaultIO [] $
|
|
|
|
parseSrvHost <$> readProcessEnv "host" ["-t", "SRV", "--", srv]
|
|
|
|
-- clear environment, to avoid LANG affecting output
|
|
|
|
(Just [])
|
2012-10-25 21:56:03 +00:00
|
|
|
|
|
|
|
parseSrvHost :: String -> [HostPort]
|
2012-10-28 23:14:30 +00:00
|
|
|
parseSrvHost = orderHosts . catMaybes . map parse . lines
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
parse l = case words l of
|
|
|
|
[_, _, _, _, spriority, sweight, sport, hostname] -> do
|
|
|
|
let v =
|
|
|
|
( readish sport :: Maybe Int
|
|
|
|
, readish spriority :: Maybe Int
|
|
|
|
, readish sweight :: Maybe Int
|
|
|
|
)
|
|
|
|
case v of
|
|
|
|
(Just port, Just priority, Just weight) -> Just
|
|
|
|
( (priority, weight)
|
|
|
|
, (hostname, PortNumber $ fromIntegral port)
|
2012-10-28 23:14:30 +00:00
|
|
|
)
|
2012-12-13 04:24:19 +00:00
|
|
|
_ -> Nothing
|
|
|
|
_ -> Nothing
|
2012-10-25 21:56:03 +00:00
|
|
|
|
2012-10-28 23:14:30 +00:00
|
|
|
orderHosts :: [(PriorityWeight, HostPort)] -> [HostPort]
|
|
|
|
orderHosts = map snd . sortBy (compare `on` fst)
|