1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <libnl++/Buffer.h>
20 #include <libnl++/Message.h>
21 
22 #include <linux/genetlink.h>
23 
24 #include <optional>
25 
26 namespace android::nl::generic {
27 
28 /**
29  * Tracker of Netlink family ID registrations.
30  */
31 class FamilyTracker {
32   public:
33     /**
34      * Try parsing NL80211 message.
35      *
36      * Proper parsing of NL80211 nessages requires prior parsing of control message for Generic
37      * Netlink protocol.
38      *
39      * \param msg Message to parse
40      * \returns Parsed NL80211 message or std::nullopt if it wasn't one
41      */
42     std::optional<Message<genlmsghdr>> parseNl80211(Buffer<nlmsghdr> msg);
43 
44   private:
45     /* For efficiency, we use a single hardcoded family ID. However, if we supported multiple family
46      * types, this should probably be a map.
47      */
48     std::optional<uint16_t> mNl80211FamilyId;
49 
50     /**
51      * Track Generic protocol messages.
52      *
53      * This method is looking for family registration messages.
54      *
55      * \param msg Message to track
56      * \returns True, if the message was a control message (regardless of carrying
57      *          family info or not)
58      */
59     bool track(const Buffer<nlmsghdr>& msg);
60 };
61 
62 }  // namespace android::nl::generic
63