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_DISTRIBUTOR_H
17 #define WRAPPER_DISTRIBUTOR_H
18 
19 #include "data_receiver.h"
20 #include "i_notify_callback.h"
21 #include "netsys_event_message.h"
22 #include <mutex>
23 
24 #define NET_SYMBOL_VISIBLE __attribute__ ((visibility("default")))
25 namespace OHOS {
26 namespace nmd {
27 class NET_SYMBOL_VISIBLE WrapperDistributor {
28 public:
29     WrapperDistributor(int32_t socket, const int32_t format, std::mutex& externMutex);
30     ~WrapperDistributor() = default;
31 
32     int32_t Start();
33     int32_t Stop();
34     int32_t
35         RegisterNetlinkCallbacks(std::shared_ptr<std::vector<sptr<NetsysNative::INotifyCallback>>> netlinkCallbacks);
36 
37 private:
38     void HandleDecodeSuccess(const std::shared_ptr<NetsysEventMessage> &message);
39     void HandleStateChanged(const std::shared_ptr<NetsysEventMessage> &message);
40     void HandleAddressChange(const std::shared_ptr<NetsysEventMessage> &message);
41     void HandleRouteChange(const std::shared_ptr<NetsysEventMessage> &message);
42     void HandleSubSysNet(const std::shared_ptr<NetsysEventMessage> &message);
43     void HandleSubSysQlog(const std::shared_ptr<NetsysEventMessage> &message);
44     void NotifyInterfaceAdd(const std::string &ifName);
45     void NotifyInterfaceRemove(const std::string &ifName);
46     void NotifyInterfaceChange(const std::string &ifName, bool isUp);
47     void NotifyInterfaceLinkStateChange(const std::string &ifName, bool isUp);
48     void NotifyQuotaLimitReache(const std::string &labelName, const std::string &ifName);
49     void NotifyInterfaceAddressUpdate(const std::string &addr, const std::string &ifName, int32_t flags,
50                                       int32_t scope);
51     void NotifyInterfaceAddressRemove(const std::string &addr, const std::string &ifName, int32_t flags,
52                                       int32_t scope);
53     void NotifyRouteChange(bool updated, const std::string &route, const std::string &gateway,
54                            const std::string &ifName);
55 
56     std::unique_ptr<DataReceiver> receiver_;
57     std::shared_ptr<std::vector<sptr<NetsysNative::INotifyCallback>>> netlinkCallbacks_;
58     std::mutex& netlinkCallbacksMutex_;
59 };
60 } // namespace nmd
61 } // namespace OHOS
62 
63 #endif // WRAPPER_DISTRIBUTOR_H
64