1b1b8bc3fSopenharmony_ci/* 2b1b8bc3fSopenharmony_ci * Copyright (C) 2021-2023 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_SOCKET_H 17b1b8bc3fSopenharmony_ci#define INCLUDE_NETLINK_SOCKET_H 18b1b8bc3fSopenharmony_ci 19b1b8bc3fSopenharmony_ci#include <functional> 20b1b8bc3fSopenharmony_ci#include <linux/netlink.h> 21b1b8bc3fSopenharmony_ci#include <linux/rtnetlink.h> 22b1b8bc3fSopenharmony_ci#include <memory> 23b1b8bc3fSopenharmony_ci#include <netinet/in.h> 24b1b8bc3fSopenharmony_ci#include <sys/epoll.h> 25b1b8bc3fSopenharmony_ci 26b1b8bc3fSopenharmony_cinamespace OHOS { 27b1b8bc3fSopenharmony_cinamespace nmd { 28b1b8bc3fSopenharmony_ciconstexpr uint32_t NETLINKMESSAGE_MAX_LEN = 1024; 29b1b8bc3fSopenharmony_ciconstexpr uint32_t KERNEL_BUFFER_SIZE = 8192; 30b1b8bc3fSopenharmony_ciconstexpr uint32_t LOCAL_PRIORITY = 32767; 31b1b8bc3fSopenharmony_ci/** 32b1b8bc3fSopenharmony_ci * Send netklink message to kernel 33b1b8bc3fSopenharmony_ci * 34b1b8bc3fSopenharmony_ci * @param msg nlmsghdr struct 35b1b8bc3fSopenharmony_ci * @param table If clear route,this is table number, otherwise it will is 0 36b1b8bc3fSopenharmony_ci * @return Returns 0, send netklink message to kernel successfully, otherwise it will fail 37b1b8bc3fSopenharmony_ci */ 38b1b8bc3fSopenharmony_ciint32_t SendNetlinkMsgToKernel(nlmsghdr *msg, uint32_t table = 0); 39b1b8bc3fSopenharmony_ci 40b1b8bc3fSopenharmony_ci/** 41b1b8bc3fSopenharmony_ci * Clear route or rule configure 42b1b8bc3fSopenharmony_ci * 43b1b8bc3fSopenharmony_ci * @param clearThing Decide to clear route or rule. Must be one of RTM_GETRULE/RTM_GETROUTE 44b1b8bc3fSopenharmony_ci * @param table If clear route,this is table number, otherwise it will is 0 45b1b8bc3fSopenharmony_ci * @return Returns 0, clear route or rule configure successfully, otherwise it will fail 46b1b8bc3fSopenharmony_ci */ 47b1b8bc3fSopenharmony_ciint32_t ClearRouteInfo(uint16_t clearThing, uint32_t table); 48b1b8bc3fSopenharmony_ci 49b1b8bc3fSopenharmony_ci/** 50b1b8bc3fSopenharmony_ci * Get info from kernel 51b1b8bc3fSopenharmony_ci * 52b1b8bc3fSopenharmony_ci * @param sock Sock for read 53b1b8bc3fSopenharmony_ci * @param clearThing Type for kernel nlmsg_type 54b1b8bc3fSopenharmony_ci * @param table Route property for RTA_TABLE 55b1b8bc3fSopenharmony_ci * @return Returns 0, get info from kernel successfully, otherwise it will fail 56b1b8bc3fSopenharmony_ci */ 57b1b8bc3fSopenharmony_ciint32_t GetInfoFromKernel(int32_t sock, uint16_t clearThing, uint32_t table); 58b1b8bc3fSopenharmony_ci 59b1b8bc3fSopenharmony_ci/** 60b1b8bc3fSopenharmony_ci * Deal info from kernel 61b1b8bc3fSopenharmony_ci * 62b1b8bc3fSopenharmony_ci * @param nlmsgHeader nlmsghdr 63b1b8bc3fSopenharmony_ci * @param clearThing Type for kernel nlmsg_type 64b1b8bc3fSopenharmony_ci * @param table Route property for RTA_TABLE 65b1b8bc3fSopenharmony_ci * @return Returns 0, deal info from kernel successfully, otherwise it will fail 66b1b8bc3fSopenharmony_ci */ 67b1b8bc3fSopenharmony_civoid DealInfoFromKernel(nlmsghdr *nlmsgHeader, uint16_t clearThing, uint32_t table); 68b1b8bc3fSopenharmony_ci 69b1b8bc3fSopenharmony_ci/** 70b1b8bc3fSopenharmony_ci * Get route property 71b1b8bc3fSopenharmony_ci * 72b1b8bc3fSopenharmony_ci * @param nlmsgHeader nlmsghdr 73b1b8bc3fSopenharmony_ci * @param property Property for route 74b1b8bc3fSopenharmony_ci * @return Returns 0, get route property successfully, otherwise it will fail 75b1b8bc3fSopenharmony_ci */ 76b1b8bc3fSopenharmony_ciint32_t GetRouteProperty(const nlmsghdr *nlmsgHeader, int32_t property); 77b1b8bc3fSopenharmony_ci} // namespace nmd 78b1b8bc3fSopenharmony_ci} // namespace OHOS 79b1b8bc3fSopenharmony_ci#endif // !INCLUDE_NETLINK_SOCKET_H 80