1 /*
2  * Copyright (c) 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 NETSYS_NET_DIAG_DATA_H
17 #define NETSYS_NET_DIAG_DATA_H
18 
19 #include <iostream>
20 #include <list>
21 
22 #include "parcel.h"
23 
24 namespace OHOS {
25 namespace NetsysNative {
26 enum NetDiagForceType : uint8_t {
27     FORCE_TYPE_IPV4 = 0,
28     FORCE_TYPE_IPV6 = 1,
29 };
30 
31 enum NetDiagProtocolType : uint8_t {
32     PROTOCOL_TYPE_ALL,
33     PROTOCOL_TYPE_TCP,
34     PROTOCOL_TYPE_UDP,
35     PROTOCOL_TYPE_UNIX,
36     PROTOCOL_TYPE_RAW
37 };
38 
39 struct NetDiagPingOption final : public Parcelable {
40     NetDiagForceType forceType_ = FORCE_TYPE_IPV4; // Optional,default is FORCE_TYPE_IPV4
41     std::string destination_;                      // Required
42     std::string source_;                           // Optional
43     uint32_t interval_ = 0;                        // Optional(millisecond),default is 1000ms
44     uint16_t count_ = 0;                           // Optional,default is 25 counts
45     uint16_t dataSize_ = 0;                        // Optional,default is 56 bytes
46     uint16_t mark_ = 0;                            // Optional
47     uint16_t ttl_ = 0;                             // Optional
48     uint16_t timeOut_ = 0;                         // Optional(second),default is 3 seconds
49     uint16_t duration_ = 0;                        // Optional(second),default and maximum duration is 30 seconds
50     bool flood_ = false;                           // Optional
51 
52     bool Marshalling(Parcel &parcel) const override;
53     static bool Unmarshalling(Parcel &parcel, NetDiagPingOption &pingOption);
54 };
55 
56 struct PingIcmpResponseInfo final : public Parcelable {
57     uint16_t bytes_ = 0;
58     uint16_t icmpSeq_ = 0;
59     uint16_t ttl_ = 0;
60     uint32_t costTime_ = 0;
61     std::string from_;
62 
63     bool Marshalling(Parcel &parcel) const override;
64     static bool Unmarshalling(Parcel &parcel, PingIcmpResponseInfo &icmpSeq);
65 };
66 
67 struct NetDiagPingResult final : public Parcelable {
68     std::string host_;
69     std::string ipAddr_;
70     uint16_t dateSize_ = 0;
71     uint16_t payloadSize_ = 0;
72     uint16_t transCount_ = 0;
73     uint16_t recvCount_ = 0;
74     std::list<PingIcmpResponseInfo> icmpRespList_;
75 
76     bool Marshalling(Parcel &parcel) const override;
77     static bool Unmarshalling(Parcel &parcel, NetDiagPingResult &pingResult);
78 };
79 
80 struct NetDiagRouteTable final : public Parcelable {
81     std::string destination_;
82     std::string gateway_;
83     std::string mask_;
84     std::string iface_;
85     std::string flags_;
86     uint32_t metric_ = 0;
87     uint32_t ref_ = 0;
88     uint32_t use_ = 0;
89 
90     bool Marshalling(Parcel &parcel) const override;
91     static bool Unmarshalling(Parcel &parcel, NetDiagRouteTable &routeTable);
92 };
93 
94 struct NetDiagUnixSocketInfo final : public Parcelable {
95     uint16_t refCnt_ = 0;
96     uint32_t inode_ = 0;
97     std::string protocol_;
98     std::string flags_;
99     std::string type_;
100     std::string state_;
101     std::string path_;
102 
103     bool Marshalling(Parcel &parcel) const override;
104     static bool Unmarshalling(Parcel &parcel, NetDiagUnixSocketInfo &socketInfo);
105 };
106 
107 struct NeyDiagNetProtoSocketInfo final : public Parcelable {
108     std::string protocol_;
109     std::string localAddr_;
110     std::string foreignAddr_;
111     std::string state_;
112     std::string user_;
113     std::string programName_;
114     uint16_t recvQueue_ = 0;
115     uint16_t sendQueue_ = 0;
116     uint32_t inode_;
117 
118     bool Marshalling(Parcel &parcel) const override;
119     static bool Unmarshalling(Parcel &parcel, NeyDiagNetProtoSocketInfo &socketInfo);
120 };
121 
122 struct NetDiagSocketsInfo final : public Parcelable {
123     std::list<NetDiagUnixSocketInfo> unixSocketsInfo_;
124     std::list<NeyDiagNetProtoSocketInfo> netProtoSocketsInfo_;
125 
126     bool Marshalling(Parcel &parcel) const override;
127     static bool Unmarshalling(Parcel &parcel, NetDiagSocketsInfo &socketsInfo);
128 };
129 
130 struct NetDiagIfaceConfig final : public Parcelable {
131     std::string ifaceName_;
132     std::string linkEncap_;
133     std::string macAddr_;
134     std::string ipv4Addr_;
135     std::string ipv4Bcast_;
136     std::string ipv4Mask_;
137     std::list<std::pair<std::string, std::string>> ipv6Addrs_;
138     uint32_t mtu_ = 0;
139     uint32_t txQueueLen_ = 0;
140     uint32_t rxBytes_ = 0;
141     uint32_t txBytes_ = 0;
142     bool isUp_ = false;
143 
144     void Initialize();
145     bool Marshalling(Parcel &parcel) const override;
146     static bool Unmarshalling(Parcel &parcel, NetDiagIfaceConfig &ifaceConfig);
147 };
148 } // namespace NetsysNative
149 } // namespace OHOS
150 #endif // NETSYS_NET_DIAG_DATA_H