ifaddrs_converter.cc revision 29488c23644721c10a45eba74c67602b0262e582
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#include "webrtc/base/logging.h"
14
15namespace rtc {
16
17IfAddrsConverter::IfAddrsConverter() {}
18
19IfAddrsConverter::~IfAddrsConverter() {}
20
21bool IfAddrsConverter::ConvertIfAddrsToIPAddress(
22    const struct ifaddrs* interface,
23    InterfaceAddress* ip,
24    IPAddress* mask) {
25  switch (interface->ifa_addr->sa_family) {
26    case AF_INET: {
27      *ip = IPAddress(
28          reinterpret_cast<sockaddr_in*>(interface->ifa_addr)->sin_addr);
29      *mask = IPAddress(
30          reinterpret_cast<sockaddr_in*>(interface->ifa_netmask)->sin_addr);
31      return true;
32    }
33    case AF_INET6: {
34      int ip_attributes = IPV6_ADDRESS_FLAG_NONE;
35      if (!ConvertNativeAttributesToIPAttributes(interface, &ip_attributes)) {
36        return false;
37      }
38      *ip = InterfaceAddress(
39          reinterpret_cast<sockaddr_in6*>(interface->ifa_addr)->sin6_addr,
40          ip_attributes);
41      *mask = IPAddress(
42          reinterpret_cast<sockaddr_in6*>(interface->ifa_netmask)->sin6_addr);
43      return true;
44    }
45    default: { return false; }
46  }
47}
48
49bool IfAddrsConverter::ConvertNativeAttributesToIPAttributes(
50    const struct ifaddrs* interface,
51    int* ip_attributes) {
52  *ip_attributes = IPV6_ADDRESS_FLAG_NONE;
53  return true;
54}
55
56#if !defined(WEBRTC_MAC)
57// For MAC and IOS, it's defined in macifaddrs_converter.cc
58IfAddrsConverter* CreateIfAddrsConverter() {
59  return new IfAddrsConverter();
60}
61#endif
62}  // namespace rtc
63