Guo-wei Shieh | 9faf154 | 2015-12-28 22:06:55 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "rtc_base/ifaddrs_converter.h" |
Guo-wei Shieh | 9faf154 | 2015-12-28 22:06:55 | [diff] [blame] | 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: { |
Taylor Brandstetter | 01cb5f2 | 2018-03-07 23:49:32 | [diff] [blame] | 25 | *ip = InterfaceAddress(IPAddress( |
| 26 | reinterpret_cast<sockaddr_in*>(interface->ifa_addr)->sin_addr)); |
Guo-wei Shieh | 9faf154 | 2015-12-28 22:06:55 | [diff] [blame] | 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 | } |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 43 | default: { |
| 44 | return false; |
| 45 | } |
Guo-wei Shieh | 9faf154 | 2015-12-28 22:06:55 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | bool 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 |
| 58 | IfAddrsConverter* CreateIfAddrsConverter() { |
| 59 | return new IfAddrsConverter(); |
| 60 | } |
| 61 | #endif |
| 62 | } // namespace rtc |