Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose NullSockAddr, add send-with-fds functions #562

Merged
merged 1 commit into from
Jun 28, 2023
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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