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