Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* Watchdog scheme for gracefulClose.
[#620](https://github.com/haskell/network/pull/620)
* Exporting recvBufNoWait.
* Support for getHostName
[#621](https://github.com/haskell/network/pull/621)

## Version 3.2.8.0

Expand Down
13 changes: 11 additions & 2 deletions Network/Socket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ module Network.Socket (
getAddrInfo,

-- ** Types
HostName,
ServiceName,
AddrInfo (..),
defaultHints,
Expand Down Expand Up @@ -340,6 +339,10 @@ module Network.Socket (
getNameInfo,
NameInfoFlag (..),

-- * Host name information
getHostName,
HostName,

-- * Low level

-- ** socket operations
Expand Down Expand Up @@ -409,7 +412,8 @@ module Network.Socket (

-- * Multicast Group
MulticastGroup (..),
) where
)
where

import Network.Socket.Buffer hiding (
recvBufFrom,
Expand All @@ -423,6 +427,11 @@ import Network.Socket.Flag
import Network.Socket.Handle
import Network.Socket.If
import Network.Socket.Info
#if !defined(mingw32_HOST_OS)
import Network.Socket.Posix.HostName(getHostName)
#else
import Network.Socket.Win32.HostName(getHostName)
#endif
import Network.Socket.Internal
import Network.Socket.Name hiding (getPeerName, getSocketName)
import Network.Socket.Options
Expand Down
25 changes: 25 additions & 0 deletions Network/Socket/Posix/HostName.hsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Network.Socket.Posix.HostName (
getHostName,
)
where

#include "HsNet.h"

import Foreign.C.Error
import Foreign.C.String
import Foreign.C.Types
import Foreign.Marshal.Array
import Network.Socket.Info (HostName)

foreign import ccall unsafe "gethostname"
gethostname :: CString -> CSize -> IO CInt

-- | Get name of current host
--
-- Since: 3.3.0.0
getHostName :: IO HostName
getHostName = allocaArray0 size $ \cstr -> do
throwErrnoIfMinus1_ "getHostName" $ gethostname cstr (fromIntegral size)
peekCString cstr
where
size = 256
39 changes: 39 additions & 0 deletions Network/Socket/Win32/HostName.hsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{-# LANGUAGE CPP #-}

#include "HsNet.h"
##include "HsNetDef.h"

module Network.Socket.Win32.HostName (
getHostName,
)
where

import Foreign.C.Types
import Foreign.Marshal.Utils
import Foreign.Ptr
import Foreign.Storable
import Network.Socket.Info (HostName)
import System.Win32.Types

foreign import CALLCONV unsafe "windows.h GetComputerNameExW"
getComputerNameEx :: COMPUTER_NAME_FORMAT -> LPTSTR -> LPDWORD -> IO BOOL

type COMPUTER_NAME_FORMAT = CInt

computerNamePhysicalDnsHostname :: COMPUTER_NAME_FORMAT
computerNamePhysicalDnsHostname = 5

-- | Get name of current host
--
-- Since: 3.3.0.0
getHostName :: IO HostName
getHostName = with 0 $ \p_charcount -> do
-- On the first run, determine the character count and ignore any error we get
_ <- getComputerNameEx computerNamePhysicalDnsHostname nullPtr p_charcount
charcount <- peek p_charcount

-- The second time around, use the correct character count to retrieve the data
withTString (replicate (fromIntegral charcount) ' ') $ \name -> do
failIfFalse_ "GetComputerNameExW" $
getComputerNameEx computerNamePhysicalDnsHostname name p_charcount
peekTString name
2 changes: 2 additions & 0 deletions network.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ library
Network.Socket.ByteString.Lazy.Posix
Network.Socket.Posix.Cmsg
Network.Socket.Posix.CmsgHdr
Network.Socket.Posix.HostName
Network.Socket.Posix.IOVec
Network.Socket.Posix.MsgHdr

Expand All @@ -160,6 +161,7 @@ library
Network.Socket.ByteString.Lazy.Windows
Network.Socket.Win32.Cmsg
Network.Socket.Win32.CmsgHdr
Network.Socket.Win32.HostName
Network.Socket.Win32.WSABuf
Network.Socket.Win32.MsgHdr

Expand Down
4 changes: 4 additions & 0 deletions tests/Network/SocketSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@
it "holds for CmsgId" $ forAll cmsgidGen $
\x -> (read . show $ x) == (x :: CmsgId)

describe "hostname" $ do
it "is not empty" $ do
hostname <- getHostName
hostname `shouldSatisfy` (not . null)

-- Type-specific generators with strong bias towards pattern synonyms

Expand All @@ -434,7 +438,7 @@
cmsgidGen = biasedGen (\g -> CmsgId <$> g <*> g) cmsgidPatterns arbitrary

genFds :: Gen [Fd]
genFds = listOf (Fd <$> arbitrary)

Check warning on line 441 in tests/Network/SocketSpec.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.14)

Defined but not used: ‘genFds’

Check warning on line 441 in tests/Network/SocketSpec.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.14)

Defined but not used: ‘genFds’

Check warning on line 441 in tests/Network/SocketSpec.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.12)

Defined but not used: ‘genFds’

Check warning on line 441 in tests/Network/SocketSpec.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.12)

Defined but not used: ‘genFds’

Check warning on line 441 in tests/Network/SocketSpec.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.4)

Defined but not used: ‘genFds’

Check warning on line 441 in tests/Network/SocketSpec.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.4)

Defined but not used: ‘genFds’

Check warning on line 441 in tests/Network/SocketSpec.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.10)

Defined but not used: ‘genFds’

Check warning on line 441 in tests/Network/SocketSpec.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.10)

Defined but not used: ‘genFds’

Check warning on line 441 in tests/Network/SocketSpec.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.6)

Defined but not used: ‘genFds’

Check warning on line 441 in tests/Network/SocketSpec.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.6)

Defined but not used: ‘genFds’

Check warning on line 441 in tests/Network/SocketSpec.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.8)

Defined but not used: ‘genFds’

Check warning on line 441 in tests/Network/SocketSpec.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 9.8)

Defined but not used: ‘genFds’

-- pruned lists of pattern synonym values for each type to generate values from

Expand Down
Loading