1 /* 2 * Copyright 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 #pragma once 17 18 #include "result.h" 19 20 #include <linux/if_ether.h> 21 #include <netinet/in.h> 22 23 #include <string> 24 25 // A class representing a network interface. The class provides useful 26 // functionality to configure and query the network interface. 27 class Interface { 28 public: 29 Interface(); 30 ~Interface(); 31 Result init(const char* interfaceName); 32 33 // Returns the interface index indicated by the system getIndex()34 unsigned int getIndex() const { return mIndex; } 35 // Get the MAC address of the interface getMacAddress()36 const uint8_t (&getMacAddress() const)[ETH_ALEN] { return mMacAddress; } 37 // Get the name of the interface getName()38 const std::string& getName() const { return mInterfaceName; } 39 40 Result bringUp(); 41 Result bringDown(); 42 Result setMtu(uint16_t mtu); 43 Result setAddress(in_addr_t address, in_addr_t subnetMask); 44 45 private: 46 struct ifreq createRequest() const; 47 Result populateIndex(); 48 Result populateMacAddress(); 49 Result setInterfaceUp(bool shouldBeUp); 50 51 std::string mInterfaceName; 52 int mSocketFd; 53 unsigned int mIndex; 54 uint8_t mMacAddress[ETH_ALEN]; 55 }; 56 57