Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include <winsock2.h>
8
#include <ws2ipdef.h>
9
#include <iphlpapi.h>
10
11
#include "mozilla/UniquePtr.h"
12
13
#include "NetworkInfoServiceImpl.h"
14
15
namespace mozilla {
16
namespace net {
17
18
nsresult DoListAddresses(AddrMapType& aAddrMap) {
19
UniquePtr<MIB_IPADDRTABLE> ipAddrTable;
20
DWORD size = sizeof(MIB_IPADDRTABLE);
21
22
ipAddrTable.reset((MIB_IPADDRTABLE*)malloc(size));
23
if (!ipAddrTable) {
24
return NS_ERROR_FAILURE;
25
}
26
27
DWORD retVal = GetIpAddrTable(ipAddrTable.get(), &size, 0);
28
if (retVal == ERROR_INSUFFICIENT_BUFFER) {
29
ipAddrTable.reset((MIB_IPADDRTABLE*)malloc(size));
30
if (!ipAddrTable) {
31
return NS_ERROR_FAILURE;
32
}
33
retVal = GetIpAddrTable(ipAddrTable.get(), &size, 0);
34
}
35
if (retVal != NO_ERROR) {
36
return NS_ERROR_FAILURE;
37
}
38
39
for (DWORD i = 0; i < ipAddrTable->dwNumEntries; i++) {
40
int index = ipAddrTable->table[i].dwIndex;
41
uint32_t addrVal = (uint32_t)ipAddrTable->table[i].dwAddr;
42
43
nsCString indexString;
44
indexString.AppendInt(index, 10);
45
46
nsCString addrString;
47
addrString.AppendPrintf("%d.%d.%d.%d", (addrVal >> 0) & 0xff,
48
(addrVal >> 8) & 0xff, (addrVal >> 16) & 0xff,
49
(addrVal >> 24) & 0xff);
50
51
aAddrMap.Put(indexString, addrString);
52
}
53
54
return NS_OK;
55
}
56
57
} // namespace net
58
} // namespace mozilla