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 GUEST_GCE_NETWORK_NETWORK_INTERFACE_H_ 17 #define GUEST_GCE_NETWORK_NETWORK_INTERFACE_H_ 18 19 #include <string> 20 21 namespace cvd { 22 23 // Abstraction of network interfaces. 24 // This interface provides means to modify network interface parameters. 25 class NetworkInterface { 26 public: NetworkInterface(size_t if_index)27 explicit NetworkInterface(size_t if_index) 28 : if_index_(if_index) {} 29 30 NetworkInterface() = default; 31 ~NetworkInterface() = default; 32 33 // Get network interface index. Index()34 size_t Index() const { 35 return if_index_; 36 } 37 38 // Set name of the network interface. SetName(const std::string & new_name)39 NetworkInterface& SetName(const std::string& new_name) { 40 name_ = new_name; 41 return *this; 42 } 43 44 // Get name of the network interface. 45 // Returns name, if previously set. Name()46 const std::string& Name() const { 47 return name_; 48 } 49 PrefixLength()50 int PrefixLength() const { 51 return prefix_len_; 52 } 53 54 // Set operational state of the network interface (ie. whether interface is 55 // up). SetOperational(bool is_operational)56 NetworkInterface& SetOperational(bool is_operational) { 57 is_operational_ = is_operational; 58 return *this; 59 } 60 61 // Get operational state of the interface. Value of 'true' indicates interface 62 // should be 'up'. IsOperational()63 bool IsOperational() const { 64 return is_operational_; 65 } 66 67 // Set IPv4 address of the network interface. SetAddress(const std::string & address)68 NetworkInterface& SetAddress(const std::string& address) { 69 ip_address_ = address; 70 return *this; 71 } 72 73 // Get IPv4 address of the network interface. Address()74 const std::string& Address() const { 75 return ip_address_; 76 } 77 78 // Set IPv4 broadcast address of the network interface. SetBroadcastAddress(const std::string & address)79 NetworkInterface& SetBroadcastAddress(const std::string& address) { 80 bc_address_ = address; 81 return *this; 82 } 83 84 // Set IPv4 prefix length SetPrefixLength(int len)85 NetworkInterface& SetPrefixLength(int len) { 86 prefix_len_ = len; 87 return *this; 88 } 89 90 // Get IPv4 broadcast address of the network interface. BroadcastAddress()91 const std::string& BroadcastAddress() const { 92 return bc_address_; 93 } 94 95 private: 96 // Index of the network interface in the system table. 0 indicates new 97 // interface. 98 size_t if_index_ = 0; 99 // Name of the interface, e.g. "eth0". 100 std::string name_; 101 // Operational status, i.e. whether interface is up. 102 bool is_operational_ = false; 103 // IPv4 address of this interface. 104 std::string ip_address_; 105 // IPv4 broadcast address of this interface. 106 std::string bc_address_; 107 // IPv4 prefix (aka netmask. 0 means use the default) 108 int prefix_len_ = 24; 109 110 NetworkInterface(const NetworkInterface&); 111 NetworkInterface& operator= (const NetworkInterface&); 112 }; 113 114 } // namespace cvd 115 116 #endif // GUEST_GCE_NETWORK_NETWORK_INTERFACE_H_ 117