1 /*
2  * Copyright (c) 2022-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 WRAPPER_DECODER_H
17 #define WRAPPER_DECODER_H
18 
19 #include "netsys_event_message.h"
20 
21 #include <functional>
22 #include <linux/rtnetlink.h>
23 #include <netinet/icmp6.h>
24 
25 #define NET_SYMBOL_VISIBLE __attribute__ ((visibility("default")))
26 namespace OHOS {
27 namespace nmd {
28 class NET_SYMBOL_VISIBLE WrapperDecoder {
29 public:
30     WrapperDecoder(std::shared_ptr<NetsysEventMessage> message);
31     WrapperDecoder() = delete;
32     ~WrapperDecoder() = default;
33 
34     /**
35      * Decode Ascii event message
36      * @param buffer message buffer
37      * @param buffSize message buffer size
38      * @return true if decode success, otherwise false
39      */
40     bool DecodeAscii(const char *buffer, int32_t buffSize);
41 
42     /**
43      * Decode Binary event message
44      * @param buffer message buffer
45      * @param buffSize message buffer size
46      * @return true if decode success, otherwise false
47      */
48     bool DecodeBinary(const char *buffer, int32_t buffSize);
49 
50 private:
51     static constexpr int32_t SPLIT_SIZE = 2;
52     std::shared_ptr<NetsysEventMessage> message_ = nullptr;
53 
54     bool PushAsciiMessage(const std::vector<std::string> &recvmsg);
55     bool InterpreteInfoMsg(const nlmsghdr *hdrMsg);
56     bool InterpreteUlogMsg(const nlmsghdr *hdrMsg);
57     bool InterpreteAddressMsg(const nlmsghdr *hdrMsg);
58     bool InterpreteRtMsg(const nlmsghdr *hdrMsg);
59     bool InterpreteIFaceAddr(ifaddrmsg *ifAddr, char *addrStr, socklen_t sockLen, const std::string &msgType,
60                              char *ifName, rtattr *rta);
61     bool SaveAddressMsg(const std::string addrStr, const ifaddrmsg *addrMsg, const std::string flags,
62                         const ifa_cacheinfo *cacheInfo, const std::string ifname);
63     bool SaveRtMsg(std::string dst, const std::string gateWay, const std::string device, int32_t length,
64                    int32_t family);
65     rtmsg *CheckRtParam(const nlmsghdr *hdrMsg, uint8_t type);
66     void SaveOtherMsg(const std::string &info);
67 };
68 } // namespace nmd
69 } // namespace OHOS
70 
71 #endif // WRAPPER_DECODER_H
72