1 /* 2 * Copyright (c) 2021 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 <cstdlib> 20 #include <cstring> 21 #include <iostream> 22 #include <unistd.h> 23 #include <arpa/inet.h> 24 #include <asm/types.h> 25 #include <sys/stat.h> 26 #include <sys/socket.h> 27 #include <sys/types.h> 28 #include <linux/fib_rules.h> 29 #include <linux/inet_diag.h> 30 #include <linux/netlink.h> 31 #include <linux/rtnetlink.h> 32 33 namespace OHOS { 34 namespace nmd { 35 static const uint32_t NETLINK_MAX_LEN = 1024; 36 37 class NetlinkMsg { 38 public: 39 NetlinkMsg(uint16_t flags, size_t maxBufLen, int pid); 40 ~NetlinkMsg(); 41 42 void AddRoute(unsigned short action, struct rtmsg msg); 43 void AddRule(unsigned short action, struct fib_rule_hdr msg); 44 void AddAddress(unsigned short action, struct ifaddrmsg msg); 45 int AddAttr(unsigned int rtaType, void *buf, size_t bufLen); 46 int AddAttr16(unsigned int rtaType, uint16_t data); 47 int AddAttr32(unsigned int rtaType, uint32_t data); 48 49 struct nlmsghdr *GetNetLinkMessage(); 50 private: 51 struct nlmsghdr *netlinkMessage; 52 size_t maxBufLen; 53 }; 54 } // namespace nmd 55 } // namespace OHOS 56 #endif // !INCLUDE_NETLINK_MSG_H__ 57