Skip to content

Commit

Permalink
making getAddrInfo polymorphic
Browse files Browse the repository at this point in the history
  • Loading branch information
kazu-yamamoto committed Aug 31, 2024
1 parent 8c18f94 commit 152aea0
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
7 changes: 3 additions & 4 deletions Network/Socket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
-- > addrFlags = [AI_PASSIVE]
-- > , addrSocketType = Stream
-- > }
-- > NE.head <$> getAddrInfoNE (Just hints) mhost (Just port)
-- > NE.head <$> getAddrInfo (Just hints) mhost (Just port)
-- > open addr = E.bracketOnError (openSocket addr) close $ \sock -> do
-- > setSocketOption sock ReuseAddr 1
-- > withFdSocket sock setCloseOnExecIfNeeded
Expand Down Expand Up @@ -97,7 +97,7 @@
-- > where
-- > resolve = do
-- > let hints = defaultHints { addrSocketType = Stream }
-- > NE.head <$> getAddrInfoNE (Just hints) (Just host) (Just port)
-- > NE.head <$> getAddrInfo (Just hints) (Just host) (Just port)
-- > open addr = E.bracketOnError (openSocket addr) close $ \sock -> do
-- > connect sock $ addrAddress addr
-- > return sock
Expand All @@ -112,8 +112,7 @@ module Network.Socket (
withSocketsDo,

-- * Address information
getAddrInfo,
getAddrInfoNE,
GetAddrInfo (..),

-- ** Types
HostName,
Expand Down
17 changes: 15 additions & 2 deletions Network/Socket/Info.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,19 @@ defaultHints = AddrInfo {
, addrCanonName = Nothing
}

class GetAddrInfo t where
getAddrInfo
:: Maybe AddrInfo -- ^ preferred socket type or protocol
-> Maybe HostName -- ^ host name to look up
-> Maybe ServiceName -- ^ service name to look up
-> IO (t AddrInfo) -- ^ resolved addresses, with "best" first

instance GetAddrInfo [] where
getAddrInfo = getAddrInfoList

instance GetAddrInfo NE.NonEmpty where
getAddrInfo = getAddrInfoNE

-----------------------------------------------------------------------------
-- | Resolve a host or service name to one or more addresses.
-- The 'AddrInfo' values that this function returns contain 'SockAddr'
Expand Down Expand Up @@ -242,12 +255,12 @@ defaultHints = AddrInfo {
-- >>> addrAddress addr
-- 127.0.0.1:80

getAddrInfo
getAddrInfoList
:: Maybe AddrInfo -- ^ preferred socket type or protocol
-> Maybe HostName -- ^ host name to look up
-> Maybe ServiceName -- ^ service name to look up
-> IO [AddrInfo] -- ^ resolved addresses, with "best" first
getAddrInfo hints node service = alloc getaddrinfo
getAddrInfoList hints node service = alloc getaddrinfo
where
alloc body = withSocketsDo $ maybeWith withCString node $ \c_node ->
maybeWith withCString service $ \c_service ->
Expand Down
2 changes: 1 addition & 1 deletion examples/EchoClient.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ runTCPClient host port client = withSocketsDo $ do
where
resolve = do
let hints = defaultHints{addrSocketType = Stream}
NE.head <$> getAddrInfoNE (Just hints) (Just host) (Just port)
NE.head <$> getAddrInfo (Just hints) (Just host) (Just port)
open addr = E.bracketOnError (openSocket addr) close $ \sock -> do
connect sock $ addrAddress addr
return sock
2 changes: 1 addition & 1 deletion examples/EchoServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ runTCPServer mhost port server = withSocketsDo $ do
{ addrFlags = [AI_PASSIVE]
, addrSocketType = Stream
}
NE.head <$> getAddrInfoNE (Just hints) mhost (Just port)
NE.head <$> getAddrInfo (Just hints) mhost (Just port)
open addr = E.bracketOnError (openSocket addr) close $ \sock -> do
setSocketOption sock ReuseAddr 1
withFdSocket sock setCloseOnExecIfNeeded
Expand Down
2 changes: 1 addition & 1 deletion tests/Network/SocketSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ spec = do

it "does not cause segfault on macOS 10.8.2 due to AI_NUMERICSERV" $ do
let hints = defaultHints { addrFlags = [AI_NUMERICSERV] }
void $ getAddrInfo (Just hints) (Just "localhost") Nothing
void (getAddrInfo (Just hints) (Just "localhost") Nothing :: IO [AddrInfo])

#if defined(mingw32_HOST_OS)
let lpdevname = "loopback_0"
Expand Down
4 changes: 2 additions & 2 deletions tests/Network/Test/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ bracketWithReraise tid setup teardown thing =

resolveClient :: SocketType -> HostName -> PortNumber -> IO AddrInfo
resolveClient socketType host port =
NE.head <$> getAddrInfoNE (Just hints) (Just host) (Just $ show port)
NE.head <$> getAddrInfo (Just hints) (Just host) (Just $ show port)
where
hints = defaultHints {
addrSocketType = socketType
Expand All @@ -254,7 +254,7 @@ resolveClient socketType host port =

resolveServer :: SocketType -> HostName -> IO AddrInfo
resolveServer socketType host =
NE.head <$> getAddrInfoNE (Just hints) (Just host) Nothing
NE.head <$> getAddrInfo (Just hints) (Just host) Nothing
where
hints = defaultHints {
addrSocketType = socketType
Expand Down

0 comments on commit 152aea0

Please sign in to comment.