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 #ifndef COMMON_LIBS_NET_NETWORK_INTERFACE_MANAGER_H_ 17 #define COMMON_LIBS_NET_NETWORK_INTERFACE_MANAGER_H_ 18 19 #include <memory> 20 #include <string> 21 22 #include "common/libs/net/netlink_client.h" 23 #include "common/libs/net/netlink_request.h" 24 #include "common/libs/net/network_interface.h" 25 26 namespace cuttlefish { 27 28 // Network interface manager class. 29 // - Provides access for existing network interfaces, 30 // - provides means to create new virtual interfaces. 31 // 32 // Example usage: 33 // 34 // std::unique_ptr<NetlinkClient> client(NetlinkClient::GetDefault()); 35 // NetworkInterfaceManager manager(client.get()); 36 // std::unique_ptr<NetworkInterface> iface(manager.Open("eth0", "em0")); 37 // 38 class NetworkInterfaceManager { 39 public: 40 // Open existing network interface. 41 // 42 // NOTE: this method does not fill in any NetworkInterface details yet. 43 std::unique_ptr<NetworkInterface> Open(const std::string& if_name, 44 const std::string& if_name_alt); 45 46 // Apply changes made to existing network interface. 47 // This method cannot be used to instantiate new network interfaces. 48 bool ApplyChanges(const NetworkInterface& interface); 49 50 // Creates new NetworkInterfaceManager. 51 static std::unique_ptr<NetworkInterfaceManager> New( 52 NetlinkClientFactory* factory); 53 54 private: 55 NetworkInterfaceManager(std::unique_ptr<NetlinkClient> nl_client); 56 57 std::unique_ptr<NetlinkClient> nl_client_; 58 59 NetworkInterfaceManager(const NetworkInterfaceManager&); 60 NetworkInterfaceManager& operator= (const NetworkInterfaceManager&); 61 }; 62 63 } // namespace cuttlefish 64 65 #endif // COMMON_LIBS_NET_NETWORK_INTERFACE_MANAGER_H_ 66