2013-05-08 09:48:25 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
extern "C" {
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include "r_types.h"
|
|
|
|
#include "stun.h"
|
|
|
|
#include "addrs.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2015-07-29 09:16:00 +03:00
|
|
|
#include "nsINetworkInterface.h"
|
2013-05-08 09:48:25 +04:00
|
|
|
#include "nsINetworkInterfaceListService.h"
|
|
|
|
#include "runnable_utils.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2014-03-29 11:18:25 +04:00
|
|
|
#include "nsMemory.h"
|
2013-05-08 09:48:25 +04:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2014-02-18 10:44:45 +04:00
|
|
|
#include "mozilla/SyncRunnable.h"
|
2013-05-08 09:48:25 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct NetworkInterface {
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
std::string name;
|
2013-08-28 11:27:06 +04:00
|
|
|
// See NR_INTERFACE_TYPE_* in nICEr/src/net/local_addrs.h
|
|
|
|
int type;
|
2013-05-08 09:48:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetInterfaces(std::vector<NetworkInterface>* aInterfaces)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aInterfaces);
|
|
|
|
|
|
|
|
// Obtain network interfaces from network manager.
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsINetworkInterfaceListService> listService =
|
|
|
|
do_GetService("@mozilla.org/network/interface-list-service;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
int32_t flags =
|
|
|
|
nsINetworkInterfaceListService::LIST_NOT_INCLUDE_SUPL_INTERFACES |
|
2014-03-19 16:39:00 +04:00
|
|
|
nsINetworkInterfaceListService::LIST_NOT_INCLUDE_MMS_INTERFACES |
|
|
|
|
nsINetworkInterfaceListService::LIST_NOT_INCLUDE_IMS_INTERFACES |
|
2015-07-24 05:41:15 +03:00
|
|
|
nsINetworkInterfaceListService::LIST_NOT_INCLUDE_DUN_INTERFACES |
|
|
|
|
nsINetworkInterfaceListService::LIST_NOT_INCLUDE_FOTA_INTERFACES;
|
2013-05-08 09:48:25 +04:00
|
|
|
nsCOMPtr<nsINetworkInterfaceList> networkList;
|
|
|
|
NS_ENSURE_SUCCESS(listService->GetDataInterfaceList(flags,
|
|
|
|
getter_AddRefs(networkList)),
|
|
|
|
NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// Translate nsINetworkInterfaceList to NetworkInterface.
|
|
|
|
int32_t listLength;
|
|
|
|
NS_ENSURE_SUCCESS(networkList->GetNumberOfInterface(&listLength),
|
|
|
|
NS_ERROR_FAILURE);
|
|
|
|
aInterfaces->clear();
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < listLength; i++) {
|
2015-07-29 09:16:00 +03:00
|
|
|
nsCOMPtr<nsINetworkInfo> info;
|
|
|
|
if (NS_FAILED(networkList->GetInterfaceInfo(i, getter_AddRefs(info)))) {
|
2013-05-08 09:48:25 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-29 11:18:25 +04:00
|
|
|
char16_t **ips = nullptr;
|
|
|
|
uint32_t *prefixs = nullptr;
|
|
|
|
uint32_t count = 0;
|
|
|
|
bool isAddressGot = false;
|
2013-05-08 09:48:25 +04:00
|
|
|
NetworkInterface interface;
|
|
|
|
memset(&(interface.addr), 0, sizeof(interface.addr));
|
|
|
|
interface.addr.sin_family = AF_INET;
|
2014-03-29 11:18:25 +04:00
|
|
|
|
2015-07-29 09:16:00 +03:00
|
|
|
if (NS_FAILED(info->GetAddresses(&ips, &prefixs, &count))) {
|
2013-05-08 09:48:25 +04:00
|
|
|
continue;
|
|
|
|
}
|
2014-03-29 11:18:25 +04:00
|
|
|
|
|
|
|
for (uint32_t j = 0; j < count; j++) {
|
|
|
|
nsAutoString ip;
|
|
|
|
|
|
|
|
ip.Assign(ips[j]);
|
|
|
|
if (inet_pton(AF_INET, NS_ConvertUTF16toUTF8(ip).get(),
|
|
|
|
&(interface.addr.sin_addr.s_addr)) == 1) {
|
|
|
|
isAddressGot = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 03:01:12 +03:00
|
|
|
free(prefixs);
|
2014-03-29 11:18:25 +04:00
|
|
|
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(count, ips);
|
|
|
|
|
|
|
|
if (!isAddressGot) {
|
2013-05-08 09:48:25 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-29 11:18:25 +04:00
|
|
|
nsAutoString ifaceName;
|
2015-07-29 09:16:00 +03:00
|
|
|
if (NS_FAILED(info->GetName(ifaceName))) {
|
2013-05-08 09:48:25 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
interface.name = NS_ConvertUTF16toUTF8(ifaceName).get();
|
|
|
|
|
2013-08-28 11:27:06 +04:00
|
|
|
int32_t type;
|
2015-07-29 09:16:00 +03:00
|
|
|
if (NS_FAILED(info->GetType(&type))) {
|
2013-08-28 11:27:06 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (type) {
|
2015-07-29 09:16:00 +03:00
|
|
|
case nsINetworkInfo::NETWORK_TYPE_WIFI:
|
2014-03-29 11:18:25 +04:00
|
|
|
interface.type = NR_INTERFACE_TYPE_WIFI;
|
|
|
|
break;
|
2015-07-29 09:16:00 +03:00
|
|
|
case nsINetworkInfo::NETWORK_TYPE_MOBILE:
|
2014-03-29 11:18:25 +04:00
|
|
|
interface.type = NR_INTERFACE_TYPE_MOBILE;
|
|
|
|
break;
|
2013-08-28 11:27:06 +04:00
|
|
|
}
|
|
|
|
|
2013-05-08 09:48:25 +04:00
|
|
|
aInterfaces->push_back(interface);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
int
|
2013-08-28 11:27:06 +04:00
|
|
|
nr_stun_get_addrs(nr_local_addr aAddrs[], int aMaxAddrs,
|
2015-04-22 23:02:50 +03:00
|
|
|
int aDropLoopback, int aDropLinkLocal, int* aCount)
|
2013-05-08 09:48:25 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
// Get network interface list.
|
|
|
|
std::vector<NetworkInterface> interfaces;
|
2014-02-18 10:44:45 +04:00
|
|
|
nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
|
|
|
|
mozilla::SyncRunnable::DispatchToThread(
|
|
|
|
mainThread.get(),
|
2015-05-05 23:21:37 +03:00
|
|
|
mozilla::WrapRunnableNMRet(&rv, &GetInterfaces, &interfaces),
|
2014-02-18 10:44:45 +04:00
|
|
|
false);
|
2013-05-08 09:48:25 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return R_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate to nr_transport_addr.
|
|
|
|
int32_t n = 0;
|
|
|
|
size_t num_interface = std::min(interfaces.size(), (size_t)aMaxAddrs);
|
|
|
|
for (size_t i = 0; i < num_interface; ++i) {
|
|
|
|
NetworkInterface &interface = interfaces[i];
|
|
|
|
if (nr_sockaddr_to_transport_addr((sockaddr*)&(interface.addr),
|
2013-08-28 11:27:06 +04:00
|
|
|
IPPROTO_UDP, 0, &(aAddrs[n].addr))) {
|
2013-05-08 09:48:25 +04:00
|
|
|
r_log(NR_LOG_STUN, LOG_WARNING, "Problem transforming address");
|
|
|
|
return R_FAILED;
|
|
|
|
}
|
2013-08-28 11:27:06 +04:00
|
|
|
strlcpy(aAddrs[n].addr.ifname, interface.name.c_str(),
|
|
|
|
sizeof(aAddrs[n].addr.ifname));
|
|
|
|
aAddrs[n].interface.type = interface.type;
|
|
|
|
aAddrs[n].interface.estimated_speed = 0;
|
2013-05-08 09:48:25 +04:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aCount = n;
|
2015-04-22 23:02:50 +03:00
|
|
|
r = nr_stun_remove_duplicate_addrs(aAddrs, aDropLoopback, aDropLinkLocal, aCount);
|
2013-05-08 09:48:25 +04:00
|
|
|
if (r != 0) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < *aCount; ++i) {
|
2013-08-28 11:27:06 +04:00
|
|
|
char typestr[100];
|
|
|
|
nr_local_addr_fmt_info_string(aAddrs + i, typestr, sizeof(typestr));
|
|
|
|
r_log(NR_LOG_STUN, LOG_DEBUG, "Address %d: %s on %s, type: %s\n",
|
|
|
|
i, aAddrs[i].addr.as_string, aAddrs[i].addr.ifname, typestr);
|
2013-05-08 09:48:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|