1//
2// Copyright (C) 2015 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#ifndef APMANAGER_FIREWALL_MANAGER_H_
18#define APMANAGER_FIREWALL_MANAGER_H_
19
20#include <set>
21#include <string>
22
23#include <base/macros.h>
24#include <base/memory/weak_ptr.h>
25
26#include "apmanager/firewall_proxy_interface.h"
27
28// Class for managing required firewall rules for apmanager.
29namespace apmanager {
30
31class ControlInterface;
32
33class FirewallManager final {
34 public:
35  FirewallManager();
36  ~FirewallManager();
37
38  void Init(ControlInterface* control_interface);
39
40  // Request/release DHCP port access for the specified interface.
41  void RequestDHCPPortAccess(const std::string& interface);
42  void ReleaseDHCPPortAccess(const std::string& interface);
43
44 private:
45  // Invoked when remote firewall service appeared/vanished.
46  void OnFirewallServiceAppeared();
47  void OnFirewallServiceVanished();
48
49  // This is called when a new instance of firewall proxy is detected. Since
50  // the new instance doesn't have any knowledge of previous port access
51  // requests, re-issue those requests to the proxy to get in sync.
52  void RequestAllPortsAccess();
53
54  std::unique_ptr<FirewallProxyInterface> firewall_proxy_;
55
56  // List of interfaces with DHCP port access.
57  std::set<std::string> dhcp_access_interfaces_;
58
59  base::WeakPtrFactory<FirewallManager> weak_factory_{this};
60  DISALLOW_COPY_AND_ASSIGN(FirewallManager);
61};
62
63}  // namespace apmanager
64
65#endif  // APMANAGER_FIREWALL_MANAGER_H_
66