// SPDX-License-Identifier: BSD-2-Clause // Copyright CM4all GmbH // author: Max Kellermann #pragma once #include "SocketAddress.hxx" #include "StaticSocketAddress.hxx" #include #include inline constexpr struct msghdr MakeMsgHdr(std::span iov) noexcept { struct msghdr mh{}; mh.msg_iov = const_cast(iov.data()); mh.msg_iovlen = iov.size(); return mh; } /** * Construct a struct msghdr. The parameters are `const` because that * is needed for sending; but for receiving, these buffers must * actually be writable. */ inline constexpr struct msghdr MakeMsgHdr(SocketAddress name, std::span iov, std::span control) noexcept { auto mh = MakeMsgHdr(iov); mh.msg_name = const_cast(name.GetAddress()); mh.msg_namelen = name.GetSize(); mh.msg_control = const_cast(control.data()); mh.msg_controllen = control.size(); return mh; } inline constexpr struct msghdr MakeMsgHdr(StaticSocketAddress &name, std::span iov, std::span control) noexcept { auto mh = MakeMsgHdr(iov); mh.msg_name = name; mh.msg_namelen = name.GetCapacity(); mh.msg_control = const_cast(control.data()); mh.msg_controllen = control.size(); return mh; } inline constexpr struct msghdr MakeMsgHdr(struct sockaddr_storage &name, std::span iov, std::span control) noexcept { auto mh = MakeMsgHdr(iov); mh.msg_name = static_cast(static_cast(&name)); mh.msg_namelen = sizeof(name); mh.msg_control = const_cast(control.data()); mh.msg_controllen = control.size(); return mh; }