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 #pragma once 17 18 #include "result.h" 19 20 #include <linux/netlink.h> 21 #include <sys/types.h> 22 #include <sys/socket.h> 23 24 #include <string> 25 #include <vector> 26 27 // Convert an IPv6 address struct to a string for debugging purposes 28 std::string addrToStr(const struct in6_addr& addr); 29 30 // Represents any kind of address in a struct sockaddr. 31 class Address { 32 public: 33 Address(); 34 explicit Address(const struct sockaddr_nl& address); 35 explicit Address(const struct sockaddr_in6& address); 36 explicit Address(struct in6_addr address); 37 38 template<typename T> get()39 const T* get() const { 40 return reinterpret_cast<const T*>(mStorage.data()); 41 } 42 43 template<typename T> get()44 T* get() { 45 return reinterpret_cast<T*>(mStorage.data()); 46 } 47 size()48 ssize_t size() const { return mStorage.size(); } 49 50 // Reset the address to be the max size possible for an address 51 void reset(); 52 53 // Resolve |address| into an IPv6 address. |address| may be either a domain 54 // name or just a string containing a numeric address. 55 Result resolveInet(const std::string& address); 56 // Resolve |interfaceName| into a link layer address. This can be used to 57 // create a struct sockaddr_nl that can be used to listen on the given 58 // interface at the link layer. 59 Result resolveEth(const std::string& interfaceName); 60 private: 61 std::vector<char> mStorage; 62 }; 63 64