Home | History | Annotate | Download | only in server
      1 /*
      2  * Copyright (C) 2017 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 WAKEUP_CONTROLLER_H
     18 #define WAKEUP_CONTROLLER_H
     19 
     20 #include <functional>
     21 
     22 #include <netdutils/Status.h>
     23 
     24 #include "IptablesRestoreController.h"
     25 #include "NFLogListener.h"
     26 
     27 namespace android {
     28 namespace net {
     29 
     30 class WakeupController {
     31   public:
     32     using ReportFn = std::function<void(const std::string&, uid_t, gid_t, uint64_t)>;
     33 
     34     // iptables chain where wakeup packets are matched
     35     static const char LOCAL_MANGLE_INPUT[];
     36 
     37     WakeupController(ReportFn report, IptablesRestoreInterface* iptables)
     38         : mReport(report), mIptables(iptables) {}
     39 
     40     ~WakeupController();
     41 
     42     // Subscribe this controller to a NFLOG events arriving at |listener|.
     43     netdutils::Status init(NFLogListenerInterface* listener);
     44 
     45     // Install iptables rules to match packets arriving on |ifName|
     46     // which match |mark|/|mask|. Metadata from matching packets will
     47     // be delivered along with the arbitrary string |prefix| to
     48     // INetdEventListener::onWakeupEvent.
     49     netdutils::Status addInterface(const std::string& ifName, const std::string& prefix,
     50                                    uint32_t mark, uint32_t mask);
     51 
     52     // Remove iptables rules previously installed by addInterface().
     53     // |ifName|, |prefix|, |mark| and |mask| must match precisely.
     54     netdutils::Status delInterface(const std::string& ifName, const std::string& prefix,
     55                                    uint32_t mark, uint32_t mask);
     56 
     57   private:
     58     netdutils::Status execIptables(const std::string& action, const std::string& ifName,
     59                                    const std::string& prefix, uint32_t mark, uint32_t mask);
     60 
     61     ReportFn const mReport;
     62     IptablesRestoreInterface* const mIptables;
     63     NFLogListenerInterface* mListener;
     64 };
     65 
     66 }  // namespace net
     67 }  // namespace android
     68 
     69 #endif /* WAKEUP_CONTROLLER_H */
     70