DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5b81998bb7ab)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
/* -*- 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>
#include "nsINetworkManager.h"
#include "nsINetworkInterfaceListService.h"
#include "runnable_utils.h"
#include "nsCOMPtr.h"
#include "nsThreadUtils.h"
#include "nsServiceManagerUtils.h"

namespace {
struct NetworkInterface {
  struct sockaddr_in addr;
  std::string name;
};

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 |
    nsINetworkInterfaceListService::LIST_NOT_INCLUDE_MMS_INTERFACES;
  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();

  nsAutoString ip;
  nsAutoString ifaceName;
  for (int32_t i = 0; i < listLength; i++) {
    nsCOMPtr<nsINetworkInterface> iface;
    if (NS_FAILED(networkList->GetInterface(i, getter_AddRefs(iface)))) {
      continue;
    }

    NetworkInterface interface;
    memset(&(interface.addr), 0, sizeof(interface.addr));
    interface.addr.sin_family = AF_INET;
    if (NS_FAILED(iface->GetIp(ip))) {
      continue;
    }
    if (inet_pton(AF_INET, NS_ConvertUTF16toUTF8(ip).get(),
                  &(interface.addr.sin_addr.s_addr)) != 1) {
      continue;
    }

    if (NS_FAILED(iface->GetName(ifaceName))) {
      continue;
    }
    interface.name = NS_ConvertUTF16toUTF8(ifaceName).get();

    aInterfaces->push_back(interface);
  }
  return NS_OK;
}
} // anonymous namespace

int
nr_stun_get_addrs(nr_transport_addr aAddrs[], int aMaxAddrs,
                  int aDropLoopback, int* aCount)
{
  nsresult rv;
  int r;

  // Get network interface list.
  std::vector<NetworkInterface> interfaces;
  if (NS_FAILED(NS_DispatchToMainThread(
                    mozilla::WrapRunnableNMRet(&GetInterfaces, &interfaces, &rv),
                    NS_DISPATCH_SYNC))) {
    return R_FAILED;
  }

  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),
                                      sizeof(struct sockaddr_in),
                                      IPPROTO_UDP, 0, &(aAddrs[n]))) {
      r_log(NR_LOG_STUN, LOG_WARNING, "Problem transforming address");
      return R_FAILED;
    }
    strlcpy(aAddrs[n].ifname, interface.name.c_str(), sizeof(aAddrs[n].ifname));
    n++;
  }

  *aCount = n;
  r = nr_stun_remove_duplicate_addrs(aAddrs, aDropLoopback, aCount);
  if (r != 0) {
    return r;
  }

  for (int i = 0; i < *aCount; ++i) {
    r_log(NR_LOG_STUN, LOG_DEBUG, "Address %d: %s on %s", i,
          aAddrs[i].as_string, aAddrs[i].ifname);
  }

  return 0;
}