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