1 /* 2 * Copyright (C) 2019 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 <cstdint> 19 #include <ostream> 20 #include <set> 21 #include <string> 22 #include <vector> 23 24 #include "common/libs/fs/shared_fd.h" 25 26 namespace cuttlefish { 27 // Check network interface with given name exists, such as cvd-ebr. 28 bool NetworkInterfaceExists(const std::string& interface_name); 29 30 // Creates, or connects to if it already exists, a tap network interface. The 31 // user needs CAP_NET_ADMIN to create such interfaces or be the owner to connect 32 // to one. 33 SharedFD OpenTapInterface(const std::string& interface_name); 34 35 // Returns a list of TAP devices that have open file descriptors 36 std::set<std::string> TapInterfacesInUse(); 37 38 struct DnsmasqDhcp4Lease { 39 std::uint64_t expiry; 40 std::uint8_t mac_address[6]; 41 std::uint8_t ip_address[4]; 42 std::string hostname; 43 std::string client_id; 44 }; 45 46 // Parses a dnsmasq lease file 47 std::vector<DnsmasqDhcp4Lease> ParseDnsmasqLeases(SharedFD lease_file); 48 49 std::ostream& operator<<(std::ostream&, const DnsmasqDhcp4Lease&); 50 51 // Sends a DHCPRELEASE message over the socket; 52 bool ReleaseDhcp4(SharedFD tap, const std::uint8_t mac_address[6], 53 const std::uint8_t ip_address[4], 54 const std::uint8_t dhcp_server_ip[4]); 55 56 bool ReleaseDhcpLeases(const std::string& lease_path, SharedFD tap_fd, 57 const std::uint8_t dhcp_server_ip[4]); 58 59 void GenerateCorrespondingIpv6ForMac(const std::uint8_t mac[6], std::uint8_t out[16]); 60 void GenerateMobileMacForInstance(int index, std::uint8_t out[6]); 61 void GenerateEthMacForInstance(int index, std::uint8_t out[6]); 62 void GenerateWifiMacForInstance(int index, std::uint8_t out[6]); 63 64 std::string MacAddressToString(const std::uint8_t mac[6]); 65 std::string Ipv6ToString(const std::uint8_t ip[16]); 66 } 67