Skip to content

Commit

Permalink
Merge PR #562
Browse files Browse the repository at this point in the history
  • Loading branch information
kazu-yamamoto committed Jun 28, 2023
2 parents e4444fb + 12aaeec commit e234c86
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,10 @@
## Version 3.1.5.0

* [#541](https://github.com/haskell/network/issues/541)
* Export `Network.Socket.Internal.NullSockAddr`
* Add `Network.Socket.ByteString.sendManyWithFds`
* Add `Network.Socket.ByteString.Lazy.sendWithFds`

## Version 3.1.4.0

* Install and use afunix_compat.h header.
Expand Down
1 change: 1 addition & 0 deletions Network/Socket/ByteString.hs
Expand Up @@ -29,6 +29,7 @@ module Network.Socket.ByteString
-- $vectored
, sendMany
, sendManyTo
, sendManyWithFds

-- * Receive data from a socket
, recv
Expand Down
17 changes: 17 additions & 0 deletions Network/Socket/ByteString/IO.hsc
Expand Up @@ -24,6 +24,7 @@ module Network.Socket.ByteString.IO
-- $vectored
, sendMany
, sendManyTo
, sendManyWithFds

-- * Receive data from a socket
, recv
Expand Down Expand Up @@ -53,6 +54,7 @@ import Data.ByteString.Internal (create, ByteString(..))
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Marshal.Utils (with)
import Network.Socket.Internal
import System.Posix.Types (Fd(..))

import Network.Socket.Flag

Expand Down Expand Up @@ -217,6 +219,21 @@ sendManyTo s cs addr = do
peek send_ptr
#endif

-- | Send data and file descriptors over a UNIX-domain socket in
-- a single system call. This function does not work on Windows.
sendManyWithFds :: Socket -- ^ Socket
-> [ByteString] -- ^ Data to send
-> [Fd] -- ^ File descriptors
-> IO ()
sendManyWithFds s bss fds =
void $
withBufSizs bss $ \bufsizs ->
sendBufMsg s addr bufsizs cmsgs flags
where
addr = NullSockAddr
cmsgs = encodeCmsg <$> fds
flags = mempty

-- ----------------------------------------------------------------------------
-- Receiving

Expand Down
11 changes: 11 additions & 0 deletions Network/Socket/ByteString/Lazy.hs
Expand Up @@ -23,6 +23,7 @@ module Network.Socket.ByteString.Lazy (
-- * Send data to a socket
send
, sendAll
, sendWithFds
-- * Receive data from a socket
, getContents
, recv
Expand All @@ -33,6 +34,7 @@ import Network.Socket (ShutdownCmd (..), shutdown)
import Prelude hiding (getContents)
import System.IO.Unsafe (unsafeInterleaveIO)
import System.IO.Error (catchIOError)
import System.Posix.Types (Fd(..))

#if defined(mingw32_HOST_OS)
import Network.Socket.ByteString.Lazy.Windows (send, sendAll)
Expand All @@ -41,10 +43,19 @@ import Network.Socket.ByteString.Lazy.Posix (send, sendAll)
#endif

import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
import qualified Network.Socket.ByteString as N
import Network.Socket.Imports
import Network.Socket.Types

-- | Send data and file descriptors over a UNIX-domain socket in
-- a single system call. This function does not work on Windows.
sendWithFds :: Socket -- ^ Socket
-> ByteString -- ^ Data to send
-> [Fd] -- ^ File descriptors
-> IO ()
sendWithFds s lbs fds = N.sendManyWithFds s (L.toChunks lbs) fds

-- -----------------------------------------------------------------------------
-- Receiving
-- | Receive data from the socket. The socket must be in a connected
Expand Down
3 changes: 3 additions & 0 deletions Network/Socket/Internal.hs
Expand Up @@ -48,6 +48,9 @@ module Network.Socket.Internal
-- * Initialization
, withSocketsDo

-- * Null socket address type
, NullSockAddr (..)

-- * Low-level helpers
, zeroMemory
) where
Expand Down
14 changes: 14 additions & 0 deletions Network/Socket/Types.hsc
Expand Up @@ -67,6 +67,9 @@ module Network.Socket.Types (
, pokeSockAddr
, withSockAddr

-- * Null socket address type
, NullSockAddr(..)

-- * Unsorted
, ProtocolNumber
, defaultProtocol
Expand Down Expand Up @@ -1021,6 +1024,17 @@ withNewSocketAddress f = allocaBytes sockaddrStorageLen $ \ptr -> do
zeroMemory ptr $ fromIntegral sockaddrStorageLen
f ptr sockaddrStorageLen

------------------------------------------------------------------------

-- | A null 'SocketAddress' for situations where a socket address
-- parameter is optional.
data NullSockAddr = NullSockAddr

instance SocketAddress NullSockAddr where
sizeOfSocketAddress _ = 0
peekSocketAddress _ = return NullSockAddr
pokeSocketAddress _ _ = return ()

------------------------------------------------------------------------
-- Socket addresses

Expand Down
7 changes: 0 additions & 7 deletions Network/Socket/Unix.hsc
Expand Up @@ -133,13 +133,6 @@ getPeerEid _ = return (0, 0)
isUnixDomainSocketAvailable :: Bool
isUnixDomainSocketAvailable = True

data NullSockAddr = NullSockAddr

instance SocketAddress NullSockAddr where
sizeOfSocketAddress _ = 0
peekSocketAddress _ = return NullSockAddr
pokeSocketAddress _ _ = return ()

-- | Send a file descriptor over a UNIX-domain socket.
-- This function does not work on Windows.
sendFd :: Socket -> CInt -> IO ()
Expand Down
2 changes: 1 addition & 1 deletion network.cabal
@@ -1,6 +1,6 @@
cabal-version: 1.18
name: network
version: 3.1.4.0
version: 3.1.5.0
license: BSD3
license-file: LICENSE
maintainer: Kazu Yamamoto, Evan Borden
Expand Down

0 comments on commit e234c86

Please sign in to comment.