git-annex/Utility/SRV.hs

113 lines
2.6 KiB
Haskell
Raw Normal View History

2012-10-25 21:56:03 +00:00
{- SRV record lookup
-
- 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>
-
- 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,
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
#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
deriving (Show, Eq)
2012-10-26 17:03:08 +00:00
2012-10-25 21:56:03 +00:00
type HostPort = (HostName, PortID)
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
["_", 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
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
#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
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)
)
#else
2012-10-25 21:56:03 +00:00
lookupSRV = lookupSRVHost
#endif
#endif
2012-10-25 21:56:03 +00:00
2012-10-26 17:03:08 +00:00
lookupSRVHost :: SRV -> IO [HostPort]
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]
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-12-13 04:24:19 +00:00
_ -> Nothing
_ -> Nothing
2012-10-25 21:56:03 +00:00
orderHosts :: [(PriorityWeight, HostPort)] -> [HostPort]
orderHosts = map snd . sortBy (compare `on` fst)