libcosmos
Linux C++ System Programming Library
Loading...
Searching...
No Matches
message_header.cxx
1// Cosmos
2#include <cosmos/error/RuntimeError.hxx>
3#include <cosmos/net/message_header.hxx>
4
5namespace cosmos {
6
7static_assert(sizeof(struct msghdr_const) == sizeof(struct msghdr),
8 "size mismatch between msghdr_const vs. struct msghdr in system headers");
9static_assert(sizeof(struct cmsghdr) == sizeof(ReceiveMessageHeader::ControlMessage),
10 "size mismatch between cmsghdr vs. struct ReceiveMessageHeader::ControlMessage in system headers");
11
13 m_header.msg_name = reinterpret_cast<const void*>(addr.basePtr());
14 m_header.msg_namelen = addr.size();
15}
16
17SendMessageHeader::ControlMessage::ControlMessage(const OptLevel level, int type, const size_t data_len) {
18 m_buffer.resize(CMSG_SPACE(data_len));
19 m_header = reinterpret_cast<cmsghdr*>(m_buffer.data());
20 m_header->cmsg_level = to_integral(level);
21 m_header->cmsg_type = type;
22 m_header->cmsg_len = CMSG_LEN(data_len);
23}
24
26 if (addr) {
27 setAddress(*addr);
28 } else {
30 }
31
33
34 if (control_msg) {
35 m_header.msg_control = control_msg->raw();
36 m_header.msg_controllen = control_msg->size();
37 } else {
38 m_header.msg_control = nullptr;
39 m_header.msg_controllen = 0;
40 }
41}
42
44 m_header.msg_name = reinterpret_cast<void*>(addr.basePtr());
45 m_header.msg_namelen = addr.maxSize();
46}
47
49 if (bytes == 0) {
50 m_control_buffer.clear();
51 return;
52 } else if (bytes < sizeof(cmsghdr)) {
53 cosmos_throw (RuntimeError("control buffer size smaller than control message header"));
54 }
55
56 m_control_buffer.resize(bytes);
57}
58
60 if (addr) {
61 setAddress(*addr);
62 } else {
64 }
65
67
68 if (!m_control_buffer.empty()) {
69 m_header.msg_control = m_control_buffer.data();
70 m_header.msg_controllen = m_control_buffer.size();
71 }
72}
73
74} // end ns
void prepareReceive(SocketAddress *addr)
Prepare a recvmsg() operation using the given optional source address storage.
void setAddress(SocketAddress &addr)
Fill in the source address storage fields of the struct msghdr for the given address object.
void setControlBufferSize(const size_t bytes)
Set the size of the buffer used for receiving ancillary messages.
Exception type for generic runtime errors.
struct cmsghdr * m_header
Pointer to the beginning of m_buffer for setting header data.
ControlMessage(const OptLevel level, int type, const size_t data_len)
Creates a new control message for the given level, type and size.
std::vector< uint8_t > m_buffer
The raw data the control message is composed of.
std::optional< ControlMessage > control_msg
Control message to send, if any.
void setAddress(const SocketAddress &addr)
Fill in the target address fields of the struct msghdr for the given address object.
WriteIOVector iovec
Memory regions to send.
void prepareSend(const SocketAddress *addr)
Prepare a sendmsg() operation using the given optional target address.
Base class for all types of socket addresses.
virtual size_t maxSize() const
Returns the maximum number of bytes the socket address can hold.
virtual size_t size() const =0
Returns the size of the socket address in bytes found at basePtr().
virtual sockaddr * basePtr()=0
Returns a mutable pointer to the sockaddr* base structure.
OptLevel
Representation of socket option levels.
Definition types.hxx:90