diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2021-03-17 10:26:29 +0200 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2021-03-17 10:26:29 +0200 |
commit | 75a8232c86f6bc41df0dc93321543f10629525d5 (patch) | |
tree | ae7b9422912d36ac65eb12b5a574c2bdfb04c5b3 /src | |
parent | 3535557c1d89b1bc22806305bc7b6c1a350cf413 (diff) | |
download | juandelacosa-75a8232c86f6bc41df0dc93321543f10629525d5.tar.gz |
Use getAddrInfo instead of inet_addr
Diffstat (limited to 'src')
-rw-r--r-- | src/Server.hs | 24 |
1 files 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 |