From 75a8232c86f6bc41df0dc93321543f10629525d5 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Wed, 17 Mar 2021 10:26:29 +0200 Subject: Use getAddrInfo instead of inet_addr --- src/Server.hs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Server.hs b/src/Server.hs index 14d7579..a61bd8e 100644 --- a/src/Server.hs +++ b/src/Server.hs @@ -9,21 +9,28 @@ import Data.Pool (createPool, destroyAllResources) import Database.MySQL.Base (ConnectInfo) import qualified Database.MySQL.Simple as MySQL import Network.Socket - ( Family(AF_INET, AF_UNIX) - , SockAddr(SockAddrInet, SockAddrUnix) + ( AddrInfoFlag(AI_NUMERICSERV) + , Family(AF_UNIX) + , SockAddr(SockAddrUnix) , Socket , SocketOption(ReuseAddr) , SocketType(Stream) + , addrAddress + , addrFamily + , addrFlags + , addrProtocol + , addrSocketType , bind , close + , defaultHints + , getAddrInfo , getSocketName - , inet_addr , listen , maxListenQueue , setSocketOption , socket ) -import Network.Wai.Handler.Warp (Port, defaultSettings, runSettingsSocket) +import Network.Wai.Handler.Warp (defaultSettings, runSettingsSocket) import System.IO (hPutStrLn, stderr) import System.IO.Error (isDoesNotExistError) import System.Posix.Files @@ -73,12 +80,15 @@ createSocket (Socket path) = do hPutStrLn stderr $ "Listening on UNIX socket `" ++ path ++ "'" return sock createSocket (Port port) = do - sock <- socket AF_INET Stream 0 + addr:_ <- getAddrInfo (Just hints) (Just "localhost") (Just svc) + sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr) setSocketOption sock ReuseAddr 1 - addr <- inet_addr "127.0.0.1" - bind sock $ SockAddrInet (fromIntegral port) addr + bind sock $ addrAddress addr hPutStrLn stderr $ "Listening on localhost:" ++ show port return sock + where + svc = show port + hints = defaultHints {addrFlags = [AI_NUMERICSERV], addrSocketType = Stream} closeSocket :: Socket -> IO () closeSocket sock = do -- cgit v1.2.3