Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6355aa90 authored by Keith Mok's avatar Keith Mok
Browse files

libnl++: add new func to send raw data with len

Some netlink attrib does not ending with null char.
Currently req.add string will always sending ending null character.
Add a new func called addBuffer that does not send ending null
character.

Bug: 238794143
Test: build, manual test
Change-Id: If51380cab95b2ae725673301b429b9e9889c5b0a
parent acbc2ee4
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -80,7 +80,7 @@ bool setBitrate(std::string ifname, uint32_t bitrate) {

    {
        auto linkinfo = req.addNested(IFLA_LINKINFO);
        req.add(IFLA_INFO_KIND, "can");
        req.addBuffer(IFLA_INFO_KIND, "can");
        {
            auto infodata = req.addNested(IFLA_INFO_DATA);
            /* For CAN FD, it would require to add IFLA_CAN_DATA_BITTIMING
+1 −1
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ bool add(std::string dev, std::string type) {

    {
        auto linkinfo = req.addNested(IFLA_LINKINFO);
        req.add(IFLA_INFO_KIND, type);
        req.addBuffer(IFLA_INFO_KIND, type);
    }

    nl::Socket sock(NETLINK_ROUTE);
+1 −1
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ bool add(const std::string& eth, const std::string& vlan, uint16_t id) {

    {
        auto linkinfo = req.addNested(IFLA_LINKINFO);
        req.add(IFLA_INFO_KIND, "vlan");
        req.addBuffer(IFLA_INFO_KIND, "vlan");

        {
            auto linkinfo = req.addNested(IFLA_INFO_DATA);
+12 −5
Original line number Diff line number Diff line
@@ -106,18 +106,25 @@ class MessageFactory : private MessageFactoryBase {
     */
    template <class A>
    void add(nlattrtype_t type, const A& attr) {
        add(type, &attr, sizeof(attr));
        addInternal(type, &attr, sizeof(attr));
    }

    // It will always send the last null character, otherwise use addBuffer
    // variant instead
    template <>
    void add(nlattrtype_t type, const std::string& s) {
        add(type, s.c_str(), s.size() + 1);
        addInternal(type, s.c_str(), s.size() + 1);
    }

    void addBuffer(nlattrtype_t type, const std::string_view& s) {
        addInternal(type, s.data(), s.size());
    }

    /** Guard class to frame nested attributes. \see addNested(nlattrtype_t). */
    class [[nodiscard]] NestedGuard {
      public:
        NestedGuard(MessageFactory & req, nlattrtype_t type) : mReq(req), mAttr(req.add(type)) {}
        NestedGuard(MessageFactory& req, nlattrtype_t type)
            : mReq(req), mAttr(req.addInternal(type)) {}
        ~NestedGuard() { closeNested(&mReq.mMessage.header, mAttr); }

      private:
@@ -138,7 +145,7 @@ class MessageFactory : private MessageFactoryBase {
     *    MessageFactory<ifinfomsg> req(RTM_NEWLINK, NLM_F_REQUEST);
     *    {
     *        auto linkinfo = req.addNested(IFLA_LINKINFO);
     *        req.add(IFLA_INFO_KIND, "can");
     *        req.addBuffer(IFLA_INFO_KIND, "can");
     *        {
     *            auto infodata = req.addNested(IFLA_INFO_DATA);
     *            req.add(IFLA_CAN_BITTIMING, bitTimingStruct);
@@ -154,7 +161,7 @@ class MessageFactory : private MessageFactoryBase {
    Message mMessage = {};
    bool mIsGood = true;

    nlattr* add(nlattrtype_t type, const void* data = nullptr, size_t len = 0) {
    nlattr* addInternal(nlattrtype_t type, const void* data = nullptr, size_t len = 0) {
        if (!mIsGood) return nullptr;
        auto attr = MessageFactoryBase::add(&mMessage.header, sizeof(mMessage), type, data, len);
        if (attr == nullptr) mIsGood = false;