28 #include <sys/types.h>
29 #include <sys/socket.h>
32 ssize_t
G::Msg::send(
int fd ,
const void * buffer ,
size_t size ,
int flags ,
35 return sendto( fd , buffer , size , flags ,
nullptr , 0 , fd_to_send ) ;
38 ssize_t
G::Msg::sendto(
int fd ,
const void * buffer ,
size_t size ,
int flags ,
39 const sockaddr * address_p , socklen_t address_n ,
int fd_to_send )
41 static struct ::msghdr msg_zero ;
42 struct ::msghdr msg = msg_zero ;
44 msg.msg_name =
const_cast<sockaddr*
>(address_p) ;
45 msg.msg_namelen = address_n ;
47 static struct ::iovec io_zero ;
48 struct ::iovec io = io_zero ;
49 io.iov_base =
const_cast<void*
>(buffer) ;
54 char control_buffer[CMSG_SPACE(
sizeof(
int))] ;
55 if( fd_to_send == -1 )
57 msg.msg_control = nullptr ;
58 msg.msg_controllen = 0U ;
62 std::memset( control_buffer , 0 ,
sizeof(control_buffer) ) ;
63 msg.msg_control = control_buffer ;
64 msg.msg_controllen =
sizeof(control_buffer) ;
66 struct ::cmsghdr * cmsg = CMSG_FIRSTHDR( &msg ) ;
67 cmsg->cmsg_len = CMSG_LEN(
sizeof(
int) ) ;
68 cmsg->cmsg_level = SOL_SOCKET ;
69 cmsg->cmsg_type = SCM_RIGHTS ;
70 std::memcpy( CMSG_DATA(cmsg) , &fd_to_send ,
sizeof(
int) ) ;
73 return ::sendmsg( fd , &msg , flags | MSG_NOSIGNAL ) ;
76 ssize_t
G::Msg::recv(
int fd ,
void * buffer ,
size_t size ,
int flags ,
79 return recvfrom( fd , buffer , size , flags ,
nullptr ,
nullptr , fd_received_p ) ;
83 sockaddr * address_p , socklen_t * address_np ,
int * fd_received_p )
85 static struct ::msghdr msg_zero ;
86 struct ::msghdr msg = msg_zero ;
88 msg.msg_name = address_p ;
89 msg.msg_namelen = address_np ==
nullptr ? socklen_t(0) : *address_np ;
92 io.iov_base = buffer ;
97 char control_buffer[CMSG_SPACE(
sizeof(
int))] ;
98 msg.msg_control = control_buffer ;
99 msg.msg_controllen =
sizeof(control_buffer) ;
101 ssize_t rc = ::recvmsg( fd , &msg , flags ) ;
102 int e = G::Process::errno_() ;
103 if( rc >= 0 && msg.msg_controllen > 0U && fd_received_p !=
nullptr )
105 struct cmsghdr * cmsg = CMSG_FIRSTHDR( &msg ) ;
106 if( cmsg !=
nullptr && cmsg->cmsg_type == SCM_RIGHTS )
108 std::memcpy( fd_received_p , CMSG_DATA(cmsg) ,
sizeof(
int) ) ;
111 if( rc >= 0 && address_np !=
nullptr )
113 *address_np = msg.msg_namelen ;
An empty structure that is used to indicate a signal-safe, reentrant implementation.
static ssize_t send(int, const void *, size_t, int, int fd_to_send=-1)
A send() replacement using sendmsg().
static ssize_t sendto(int, const void *, size_t, int, const sockaddr *, socklen_t, int fd_to_send=-1)
A sendto() replacement using sendmsg().
static ssize_t recvfrom(int, void *, size_t, int, sockaddr *, socklen_t *, int *fd_received_p=nullptr)
A recvfrom() replacement using recvmsg().
static ssize_t recv(int, void *, size_t, int, int *fd_received_p=nullptr)
A recv() replacement using recvmsg().
static bool fatal(int error)
Returns true if the error value indicates a permanent problem with the socket.