1 /*
2  * Copyright 2019, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "netlink_message.h"
18 
19 #include <netlink/genl/genl.h>
20 #include <netlink/msg.h>
21 
NetlinkMessage()22 NetlinkMessage::NetlinkMessage() {
23 }
24 
NetlinkMessage(NetlinkMessage && other)25 NetlinkMessage::NetlinkMessage(NetlinkMessage&& other) {
26     std::swap(mMessage, other.mMessage);
27 }
28 
~NetlinkMessage()29 NetlinkMessage::~NetlinkMessage() {
30     if (mMessage) {
31         nlmsg_free(mMessage);
32         mMessage = nullptr;
33     }
34 }
35 
operator =(NetlinkMessage && other)36 NetlinkMessage& NetlinkMessage::operator=(NetlinkMessage&& other) {
37     if (mMessage) {
38         nlmsg_free(mMessage);
39         mMessage = nullptr;
40     }
41     std::swap(mMessage, other.mMessage);
42     return *this;
43 }
44 
initGeneric(int family,uint8_t command,int version)45 bool NetlinkMessage::initGeneric(int family, uint8_t command, int version) {
46     if (mMessage) {
47         return false;
48     }
49 
50     mMessage = nlmsg_alloc();
51     if (!mMessage) {
52         return false;
53     }
54 
55     return genlmsg_put(mMessage, NL_AUTO_PORT, NL_AUTO_SEQ, family, 0,
56                        NLM_F_REQUEST, command, version) != nullptr;
57 }
58 
getSeqNum() const59 uint32_t NetlinkMessage::getSeqNum() const {
60     return nlmsg_hdr(mMessage)->nlmsg_seq;
61 }
62