1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef INCLUDE_NETLINK_MSG_H
17 #define INCLUDE_NETLINK_MSG_H
18 
19 #include <arpa/inet.h>
20 #include <asm/types.h>
21 #include <cstdlib>
22 #include <cstring>
23 #include <iostream>
24 #include <linux/fib_rules.h>
25 #include <linux/inet_diag.h>
26 #include <linux/netlink.h>
27 #include <linux/rtnetlink.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 
33 namespace OHOS {
34 namespace nmd {
35 constexpr uint32_t NETLINK_MAX_LEN = 1024;
36 class NetlinkMsg {
37 public:
38     NetlinkMsg(uint16_t flags, size_t maxBufLen, int32_t pid);
39     ~NetlinkMsg();
40     /**
41      * Add route message to nlmsghdr
42      *
43      * @param action Action name
44      * @param msg Added message
45      */
46     void AddRoute(uint16_t action, struct rtmsg msg);
47 
48     /**
49      * Add rule message to nlmsghdr
50      *
51      * @param action Action name
52      * @param msg Added message
53      */
54     void AddRule(uint16_t action, struct fib_rule_hdr msg);
55 
56     /**
57      * Add address message to nlmsghdr
58      *
59      * @param action Action name
60      * @param msg Added message
61      */
62     void AddAddress(uint16_t action, struct ifaddrmsg msg);
63 
64     /**
65      * Add rtattr to nlmsghdr
66      *
67      * @param rtaType Rta type
68      * @param buf Rta data
69      * @param bufLen Rta data length
70      * @return Returns 0, add rtattr to nlmsghdr successfully, otherwise it will fail
71      */
72     int32_t AddAttr(uint16_t rtaType, void *data, size_t dataLen);
73 
74     /**
75      * Add 16 bit rtattr to nlmsghdr
76      *
77      * @param rtaType Rta type
78      * @param data Rta data
79      * @return Returns 0, add 16 bit rtattr to nlmsghdr successfully, otherwise it will fail
80      */
81     int32_t AddAttr16(uint16_t rtaType, uint16_t data);
82 
83     /**
84      * Add 32 bit rtattr to nlmsghdr for
85      *
86      * @param rtaType Rta type
87      * @param data Rta data
88      * @return Returns 0, add 32 bit rtattr to nlmsghdr successfully, otherwise it will fail
89      */
90     int32_t AddAttr32(uint16_t rtaType, uint32_t data);
91 
92     /**
93      * Get the netlink message
94      *
95      * @return Netlink message struct
96      */
97     struct nlmsghdr *GetNetLinkMessage();
98 
99 private:
100     std::unique_ptr<char[]> msghdrBuf_;
101     struct nlmsghdr *netlinkMessage_;
102     size_t maxBufLen_;
103 };
104 } // namespace nmd
105 } // namespace OHOS
106 #endif // !INCLUDE_NETLINK_MSG_H
107