50879
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*
|
|
23 |
*/
|
|
24 |
|
|
25 |
#include "precompiled.hpp"
|
|
26 |
#include "iphlp_interface.hpp"
|
|
27 |
#include "runtime/os.hpp"
|
|
28 |
|
|
29 |
// IPHLP API
|
|
30 |
typedef DWORD(WINAPI *GetIfTable2_Fn)(PMIB_IF_TABLE2*);
|
|
31 |
typedef DWORD(WINAPI *FreeMibTable_Fn)(PVOID);
|
|
32 |
|
|
33 |
// IPHLP statics
|
|
34 |
GetIfTable2_Fn IphlpDll::_GetIfTable2 = NULL;
|
|
35 |
FreeMibTable_Fn IphlpDll::_FreeMibTable = NULL;
|
|
36 |
|
|
37 |
LONG IphlpDll::_critical_section = 0;
|
|
38 |
LONG IphlpDll::_initialized = 0;
|
|
39 |
LONG IphlpDll::_iphlp_reference_count = 0;
|
|
40 |
HMODULE IphlpDll::_hModule = NULL;
|
|
41 |
|
|
42 |
void IphlpDll::initialize(void) {
|
|
43 |
_hModule = os::win32::load_Windows_dll("iphlpapi.dll", NULL, 0);
|
|
44 |
|
|
45 |
if (NULL == _hModule) {
|
|
46 |
return;
|
|
47 |
}
|
|
48 |
|
|
49 |
// The 'A' at the end means the ANSI (not the UNICODE) vesions of the methods
|
|
50 |
_GetIfTable2 = (GetIfTable2_Fn)::GetProcAddress(_hModule, "GetIfTable2");
|
|
51 |
_FreeMibTable = (FreeMibTable_Fn)::GetProcAddress(_hModule, "FreeMibTable");
|
|
52 |
|
|
53 |
// interlock is used for fencing
|
|
54 |
InterlockedExchange(&_initialized, 1);
|
|
55 |
}
|
|
56 |
|
|
57 |
bool IphlpDll::IphlpDetach(void) {
|
|
58 |
LONG prev_ref_count = InterlockedExchangeAdd(&_iphlp_reference_count, -1);
|
|
59 |
BOOL ret = false;
|
|
60 |
|
|
61 |
if (1 == prev_ref_count) {
|
|
62 |
if (_initialized && _hModule != NULL) {
|
|
63 |
ret = FreeLibrary(_hModule);
|
|
64 |
if (ret) {
|
|
65 |
_hModule = NULL;
|
|
66 |
_GetIfTable2 = NULL;
|
|
67 |
_FreeMibTable = NULL;
|
|
68 |
InterlockedExchange(&_initialized, 0);
|
|
69 |
}
|
|
70 |
}
|
|
71 |
}
|
|
72 |
return ret != 0;
|
|
73 |
}
|
|
74 |
|
|
75 |
bool IphlpDll::IphlpAttach(void) {
|
|
76 |
InterlockedExchangeAdd(&_iphlp_reference_count, 1);
|
|
77 |
|
|
78 |
if (1 == _initialized) {
|
|
79 |
return true;
|
|
80 |
}
|
|
81 |
|
|
82 |
while (InterlockedCompareExchange(&_critical_section, 1, 0) == 1);
|
|
83 |
|
|
84 |
if (0 == _initialized) {
|
|
85 |
initialize();
|
|
86 |
}
|
|
87 |
|
|
88 |
while (InterlockedCompareExchange(&_critical_section, 0, 1) == 0);
|
|
89 |
|
|
90 |
return (_GetIfTable2 != NULL && _FreeMibTable != NULL);
|
|
91 |
}
|
|
92 |
|
|
93 |
DWORD IphlpDll::GetIfTable2(PMIB_IF_TABLE2* Table) {
|
|
94 |
assert(_initialized && _GetIfTable2 != NULL,
|
|
95 |
"IphlpAttach() not yet called");
|
|
96 |
|
|
97 |
return _GetIfTable2(Table);
|
|
98 |
}
|
|
99 |
|
|
100 |
DWORD IphlpDll::FreeMibTable(PVOID Memory) {
|
|
101 |
assert(_initialized && _FreeMibTable != NULL,
|
|
102 |
"IphlpAttach() not yet called");
|
|
103 |
|
|
104 |
return _FreeMibTable(Memory);
|
|
105 |
}
|