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 DATA_RECEIVER_H
17 #define DATA_RECEIVER_H
18 
19 #include <netinet/icmp6.h>
20 
21 #include "netlink_define.h"
22 #include "netsys_event_message.h"
23 #include "wrapper_listener.h"
24 
25 #define NET_SYMBOL_VISIBLE __attribute__ ((visibility("default")))
26 namespace OHOS {
27 namespace nmd {
28 class WrapperListener;
29 class NET_SYMBOL_VISIBLE DataReceiver {
30 public:
31     using EventCallback = std::function<void(std::shared_ptr<NetsysEventMessage>)>;
32     DataReceiver(int32_t socketFd, int32_t format);
33     DataReceiver() = delete;
34     ~DataReceiver() = default;
35 
36     /**
37      * Register callback function to receive event message
38      * @param callback function pointer
39      */
40     void RegisterCallback(EventCallback callback);
41 
42     /**
43      * Start receive event message
44      * @return success or failed
45      */
46     int32_t Start();
47 
48     /**
49      * Stop receive event message
50      * @return success or failed
51      */
52     int32_t Stop();
53 
54 private:
55     void StartReceive(int32_t socket);
56     ssize_t ReceiveMessage(bool isRepair, uid_t &uid);
57     int32_t socket_;
58     int32_t format_;
59     std::unique_ptr<WrapperListener> listener_;
60     char buffer_[NetlinkDefine::BUFFER_SIZE] __attribute__((aligned(4))) = {0};
61     EventCallback callback_;
62 };
63 } // namespace nmd
64 } // namespace OHOS
65 
66 #endif // DATA_RECEIVER_H
67