Commit 986a8671 authored by Michele Tartara's avatar Michele Tartara

Factor out resolveAddr function

This function can be useful to many parts of the code to convert the string
representation of an IP (v4 or v6) address into the proper data type.
Signed-off-by: default avatarMichele Tartara <mtartara@google.com>
Reviewed-by: default avatarHelga Velroyen <helgav@google.com>
parent 8f096849
......@@ -263,20 +263,6 @@ defaultBindAddr port Socket.AF_INET6 =
Socket.SockAddrInet6 (fromIntegral port) 0 Socket.iN6ADDR_ANY 0)
defaultBindAddr _ fam = Bad $ "Unsupported address family: " ++ show fam
-- | Default hints for the resolver
resolveAddrHints :: Maybe Socket.AddrInfo
resolveAddrHints =
Just Socket.defaultHints { Socket.addrFlags = [Socket.AI_NUMERICHOST,
Socket.AI_NUMERICSERV] }
-- | Resolves a numeric address.
resolveAddr :: Int -> String -> IO (Result (Socket.Family, Socket.SockAddr))
resolveAddr port str = do
resolved <- Socket.getAddrInfo resolveAddrHints (Just str) (Just (show port))
return $ case resolved of
[] -> Bad "Invalid results from lookup?"
best:_ -> Ok (Socket.addrFamily best, Socket.addrAddress best)
-- | Based on the options, compute the socket address to use for the
-- daemon.
parseAddress :: DaemonOptions -- ^ Command line options
......
......@@ -56,6 +56,7 @@ module Ganeti.Utils
, exitIfEmpty
, splitEithers
, recombineEithers
, resolveAddr
) where
import Data.Char (toUpper, isAlphaNum, isDigit, isSpace)
......@@ -64,6 +65,7 @@ import Data.List
import Control.Monad (foldM)
import Debug.Trace
import Network.Socket
import Ganeti.BasicTypes
import qualified Ganeti.Constants as C
......@@ -431,3 +433,16 @@ recombineEithers lefts rights trail =
recombiner (_, ls, rs) t = Bad $ "Inconsistent trail log: l=" ++
show ls ++ ", r=" ++ show rs ++ ",t=" ++
show t
-- | Default hints for the resolver
resolveAddrHints :: Maybe AddrInfo
resolveAddrHints =
Just defaultHints { addrFlags = [AI_NUMERICHOST, AI_NUMERICSERV] }
-- | Resolves a numeric address.
resolveAddr :: Int -> String -> IO (Result (Family, SockAddr))
resolveAddr port str = do
resolved <- getAddrInfo resolveAddrHints (Just str) (Just (show port))
return $ case resolved of
[] -> Bad "Invalid results from lookup?"
best:_ -> Ok (addrFamily best, addrAddress best)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment