Home | History | Annotate | Download | only in ipv6proxy
      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 
     17 #include "address.h"
     18 
     19 #include <arpa/inet.h>
     20 #include <errno.h>
     21 #include <linux/if_ether.h>
     22 #include <linux/if_packet.h>
     23 #include <netdb.h>
     24 #include <net/if.h>
     25 #include <string.h>
     26 #include <sys/ioctl.h>
     27 
     28 #include "socket.h"
     29 
     30 std::string addrToStr(const struct in6_addr& addr) {
     31     char buf[INET6_ADDRSTRLEN];
     32     if (inet_ntop(AF_INET6, &addr, buf, sizeof(buf)) == nullptr) {
     33         return "[unknown]";
     34     }
     35     return buf;
     36 }
     37 
     38 Address::Address() {
     39     mStorage.reserve(sizeof(struct sockaddr_storage));
     40 }
     41 
     42 Address::Address(const struct sockaddr_nl& address)
     43         : mStorage(sizeof(address)) {
     44     memcpy(mStorage.data(), &address, sizeof(address));
     45 }
     46 Address::Address(const struct sockaddr_in6& address)
     47         : mStorage(sizeof(address)) {
     48     memcpy(mStorage.data(), &address, sizeof(address));
     49 }
     50 
     51 Address::Address(struct in6_addr address)
     52         : mStorage(sizeof(struct sockaddr_in6)) {
     53     auto sockaddr = reinterpret_cast<struct sockaddr_in6*>(mStorage.data());
     54     sockaddr->sin6_family = AF_INET6;
     55     sockaddr->sin6_addr = address;
     56 }
     57 
     58 void Address::reset() {
     59     mStorage.resize(sizeof(struct sockaddr_storage));
     60 }
     61 
     62 Result Address::resolveInet(const std::string& address) {
     63     struct addrinfo hints;
     64     memset(&hints, 0, sizeof(hints));
     65     hints.ai_family = AF_INET6;
     66     hints.ai_socktype = SOCK_RAW;
     67     struct addrinfo* addrinfo;
     68     int res = ::getaddrinfo(address.c_str(), nullptr, &hints, &addrinfo);
     69     if (res != 0) {
     70         return Result::error(gai_strerror(res));
     71     }
     72     mStorage.resize(addrinfo->ai_addrlen);
     73     memcpy(mStorage.data(), addrinfo->ai_addr, mStorage.size());
     74     freeaddrinfo(addrinfo);
     75 
     76     return Result::success();
     77 }
     78 
     79 Result Address::resolveEth(const std::string& interfaceName) {
     80     mStorage.resize(sizeof(struct sockaddr_ll));
     81     memset(mStorage.data(), 0, mStorage.size());
     82     auto addr = reinterpret_cast<struct sockaddr_ll*>(mStorage.data());
     83     addr->sll_family = AF_PACKET;
     84     addr->sll_protocol = htons(ETH_P_IPV6);
     85 
     86     unsigned int index = if_nametoindex(interfaceName.c_str());
     87     if (index == 0) {
     88         return Result::error(strerror(errno));
     89     }
     90     addr->sll_ifindex = index;
     91 
     92     struct ifreq request;
     93     memset(&request, 0, sizeof(request));
     94     strncpy(request.ifr_name, interfaceName.c_str(), sizeof(request.ifr_name));
     95     request.ifr_name[sizeof(request.ifr_name) - 1] = '\0';
     96 
     97     Socket socket;
     98     Result res = socket.open(AF_INET, SOCK_DGRAM, IPPROTO_IP);
     99     if (!res) {
    100         return res;
    101     }
    102     int status = ::ioctl(socket.get(), SIOCGIFHWADDR, &request);
    103     if (status != 0) {
    104         return Result::error(strerror(errno));
    105     }
    106     memcpy(addr->sll_addr, request.ifr_addr.sa_data, 6);
    107 
    108     return Result::success();
    109 }
    110