Home | History | Annotate | Download | only in netmgr
      1 /*
      2  * Copyright 2018, 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 "interface_state.h"
     20 #include "pollable.h"
     21 #include "result.h"
     22 
     23 const char* interfaceStateToStr(InterfaceState state);
     24 
     25 /** Monitor network interfaces and provide notifications of changes to those
     26  *  interfaces.
     27  */
     28 class Monitor : public Pollable {
     29 public:
     30     using OnInterfaceStateCallback = std::function<void (unsigned int index,
     31                                                          const char* name,
     32                                                          InterfaceState state)>;
     33     Monitor();
     34     ~Monitor();
     35 
     36     Result init();
     37 
     38     void setOnInterfaceState(OnInterfaceStateCallback callback);
     39 
     40     // Pollable interface
     41     void getPollData(std::vector<pollfd>* fds) const override;
     42     Timestamp getTimeout() const override;
     43     bool onReadAvailable(int fd, int* status) override;
     44     bool onClose(int fd, int* status) override;
     45     bool onTimeout(int* status) override;
     46 
     47 private:
     48     Result openSocket();
     49     void closeSocket();
     50     void handleNewLink(const struct nlmsghdr* hdr);
     51 
     52     int mSocketFd;
     53     OnInterfaceStateCallback mOnInterfaceStateCallback;
     54 };
     55 
     56