Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 2015 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include "webrtc/base/ifaddrs_converter.h"
     12 
     13 namespace rtc {
     14 
     15 IfAddrsConverter::IfAddrsConverter() {}
     16 
     17 IfAddrsConverter::~IfAddrsConverter() {}
     18 
     19 bool IfAddrsConverter::ConvertIfAddrsToIPAddress(
     20     const struct ifaddrs* interface,
     21     InterfaceAddress* ip,
     22     IPAddress* mask) {
     23   switch (interface->ifa_addr->sa_family) {
     24     case AF_INET: {
     25       *ip = IPAddress(
     26           reinterpret_cast<sockaddr_in*>(interface->ifa_addr)->sin_addr);
     27       *mask = IPAddress(
     28           reinterpret_cast<sockaddr_in*>(interface->ifa_netmask)->sin_addr);
     29       return true;
     30     }
     31     case AF_INET6: {
     32       int ip_attributes = IPV6_ADDRESS_FLAG_NONE;
     33       if (!ConvertNativeAttributesToIPAttributes(interface, &ip_attributes)) {
     34         return false;
     35       }
     36       *ip = InterfaceAddress(
     37           reinterpret_cast<sockaddr_in6*>(interface->ifa_addr)->sin6_addr,
     38           ip_attributes);
     39       *mask = IPAddress(
     40           reinterpret_cast<sockaddr_in6*>(interface->ifa_netmask)->sin6_addr);
     41       return true;
     42     }
     43     default: { return false; }
     44   }
     45 }
     46 
     47 bool IfAddrsConverter::ConvertNativeAttributesToIPAttributes(
     48     const struct ifaddrs* interface,
     49     int* ip_attributes) {
     50   *ip_attributes = IPV6_ADDRESS_FLAG_NONE;
     51   return true;
     52 }
     53 
     54 #if !defined(WEBRTC_MAC)
     55 // For MAC and IOS, it's defined in macifaddrs_converter.cc
     56 IfAddrsConverter* CreateIfAddrsConverter() {
     57   return new IfAddrsConverter();
     58 }
     59 #endif
     60 }  // namespace rtc
     61