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