xref: /openbmc/dbus-sensors/src/MctpAsioEndpoint.cpp (revision 6ef897399d8c2fb9f005b0c0d0ba3f9bd115965f)
1*6ef89739SEd Tanous #include "MctpAsioEndpoint.hpp"
2*6ef89739SEd Tanous 
3*6ef89739SEd Tanous #include <boost/asio/generic/datagram_protocol.hpp>
4*6ef89739SEd Tanous #include <phosphor-logging/lg2.hpp>
5*6ef89739SEd Tanous 
6*6ef89739SEd Tanous #include <bit>
7*6ef89739SEd Tanous #include <cstdint>
8*6ef89739SEd Tanous #include <optional>
9*6ef89739SEd Tanous 
10*6ef89739SEd Tanous // Becuase of issues with glibc not matching linux, we need to make sure these
11*6ef89739SEd Tanous // are included AFTER the system headers, which are implictly included by boost.
12*6ef89739SEd Tanous // These show up as errors like
13*6ef89739SEd Tanous // /usr/include/net/if.h:44:14: error: ‘IFF_UP’ conflicts with a previous
14*6ef89739SEd Tanous // declaration
15*6ef89739SEd Tanous // The bugs below are other projects working around similar issues
16*6ef89739SEd Tanous // https://bugzilla.redhat.com/show_bug.cgi?id=1300256
17*6ef89739SEd Tanous // https://github.com/systemd/systemd/commit/08ce521fb2546921f2642bef067d2cc02158b121
18*6ef89739SEd Tanous // https://github.com/systemd/systemd/issues/2864
19*6ef89739SEd Tanous // clang-format off
20*6ef89739SEd Tanous #include <linux/mctp.h>
21*6ef89739SEd Tanous #include <sys/socket.h>
22*6ef89739SEd Tanous // clang-format on
23*6ef89739SEd Tanous 
24*6ef89739SEd Tanous MctpAsioEndpoint::MctpAsioEndpoint(uint8_t eid, uint8_t msgType)
25*6ef89739SEd Tanous {
26*6ef89739SEd Tanous     endpoint.resize(sizeof(struct sockaddr_mctp));
27*6ef89739SEd Tanous     struct sockaddr_mctp* sock =
28*6ef89739SEd Tanous         std::bit_cast<struct sockaddr_mctp*>(endpoint.data());
29*6ef89739SEd Tanous     sock->smctp_addr.s_addr = eid;
30*6ef89739SEd Tanous     sock->smctp_family = AF_MCTP;
31*6ef89739SEd Tanous     sock->smctp_type = msgType;
32*6ef89739SEd Tanous     sock->smctp_tag = MCTP_TAG_OWNER;
33*6ef89739SEd Tanous     sock->smctp_network = MCTP_NET_ANY;
34*6ef89739SEd Tanous };
35*6ef89739SEd Tanous 
36*6ef89739SEd Tanous MctpAsioEndpoint::MctpAsioEndpoint(uint8_t msgType) :
37*6ef89739SEd Tanous     MctpAsioEndpoint{MCTP_ADDR_ANY, msgType}
38*6ef89739SEd Tanous {}
39*6ef89739SEd Tanous 
40*6ef89739SEd Tanous std::optional<uint8_t> MctpAsioEndpoint::eid() const
41*6ef89739SEd Tanous {
42*6ef89739SEd Tanous     const struct sockaddr_mctp* sock = getSockAddr();
43*6ef89739SEd Tanous     if (sock == nullptr)
44*6ef89739SEd Tanous     {
45*6ef89739SEd Tanous         return std::nullopt;
46*6ef89739SEd Tanous     }
47*6ef89739SEd Tanous     return sock->smctp_addr.s_addr;
48*6ef89739SEd Tanous }
49*6ef89739SEd Tanous 
50*6ef89739SEd Tanous std::optional<uint8_t> MctpAsioEndpoint::type() const
51*6ef89739SEd Tanous {
52*6ef89739SEd Tanous     const struct sockaddr_mctp* sock = getSockAddr();
53*6ef89739SEd Tanous     if (sock == nullptr)
54*6ef89739SEd Tanous     {
55*6ef89739SEd Tanous         return std::nullopt;
56*6ef89739SEd Tanous     }
57*6ef89739SEd Tanous     return sock->smctp_type;
58*6ef89739SEd Tanous }
59*6ef89739SEd Tanous 
60*6ef89739SEd Tanous const struct sockaddr_mctp* MctpAsioEndpoint::getSockAddr() const
61*6ef89739SEd Tanous {
62*6ef89739SEd Tanous     if (endpoint.size() < sizeof(struct sockaddr_mctp))
63*6ef89739SEd Tanous     {
64*6ef89739SEd Tanous         lg2::error("MctpRequester: Received endpoint is too small?");
65*6ef89739SEd Tanous         return nullptr;
66*6ef89739SEd Tanous     }
67*6ef89739SEd Tanous 
68*6ef89739SEd Tanous     return std::bit_cast<struct sockaddr_mctp*>(endpoint.data());
69*6ef89739SEd Tanous }
70