summaryrefslogtreecommitdiff
path: root/wifi/netlinkinterceptor/aidl/default/InterceptorRelay.h
blob: 0178c90e1e69b133306a10e7496e2453c1cbe20b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <libnl++/Socket.h>

#include <mutex>
#include <thread>

namespace android::nlinterceptor {

class InterceptorRelay {
   public:
    /**
     * Wrapper around the netlink socket and thread which relays messages.
     *
     * \param nlFamily - netlink family to use for the netlink socket.
     * \param clientNlPid - pid of the client netlink socket.
     * \param clientName - name of the client to be used for debugging.
     */
    InterceptorRelay(uint32_t nlFamily, uint32_t clientNlPid,
                     const std::string& clientName);

    /**
     * Stops the relay thread if running and destroys itself.
     */
    ~InterceptorRelay();

    /**
     * Returns the PID of the internal Netlink socket.
     *
     * \return value of PID,
     */
    uint32_t getPid();

    /**
     * Spawns relay thread.
     */
    bool start();

    /**
     * Subscribes the internal socket to a single Netlink multicast group.
     *
     * \param nlGroup - Netlink group to subscribe to.
     * \returns - true for success, false for failure.
     */
    bool subscribeGroup(uint32_t nlGroup);

    /**
     * Unsubscribes the internal socket from a single Netlink multicast group.
     *
     * \param nlGroup - Netlink group to unsubscribe from.
     * \returns - true for success, false for failure.
     */
    bool unsubscribeGroup(uint32_t nlGroup);

   private:
    std::string mClientName;  ///< Name of client (Wificond, for example).
    std::optional<nl::Socket> mNlSocket;
    const uint32_t mClientNlPid = 0;  ///< pid of client NL socket.

    /**
     * If set to true, the relay thread should be running. Setting this to false
     * stops the relay thread.
     */
    std::atomic_bool mRunning = false;

    /**
     * Reads incoming Netlink messages destined for mNlSocket. If from the
     * kernel, the message is relayed to the client specified in the
     * constructor. Otherwise, the message is relayed to the kernel. This will
     * run as long as mRunning is set to true.
     */
    void relayMessages();

    std::thread mRelayThread;
};

}  // namespace android::nlinterceptor