MCPcopy Create free account
hub / github.com/BeneficialCode/WinArk / EnumConnections

Method EnumConnections

WinSysCore/ActiveConnectionTracker.cpp:9–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7
8
9int WinSys::ActiveConnectionTracker::EnumConnections() {
10 DWORD size = 1 << 16;
11 auto orgSize = size;
12 DWORD error = NO_ERROR;
13 void* buffer = nullptr;
14
15 bool first = _connections.empty();
16 _newConnections.clear();
17 _closedConnections.clear();
18 if (first) {
19 _connectionMap.reserve(512);
20 _newConnections.reserve(16);
21 _closedConnections.reserve(16);
22 }
23
24 auto map = _connectionMap;
25 std::vector<std::shared_ptr<Connection>> local;
26 local.reserve(256);
27 if ((_trackedConnections & ConnectionType::Tcp) == ConnectionType::Tcp) {
28 do
29 {
30 buffer = ::VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
31 if (!buffer)
32 return -1;
33 error = ::GetExtendedTcpTable(buffer, &size, FALSE, AF_INET, TCP_TABLE_OWNER_MODULE_ALL, 0);
34 if (error != NO_ERROR)
35 ::VirtualFree(buffer, 0, MEM_RELEASE);
36 } while (error == ERROR_INSUFFICIENT_BUFFER);
37
38 if (error == NOERROR) {
39 auto table = (PMIB_TCPTABLE_OWNER_MODULE)buffer;
40 AddTcp4Connections(table, map, local, first);
41 ::VirtualFree(buffer,0,MEM_RELEASE);
42 }
43 }
44 if ((_trackedConnections & ConnectionType::TcpV6) == ConnectionType::TcpV6) {
45 size = orgSize;
46 do
47 {
48 buffer = ::VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
49 if (!buffer)
50 return -1;
51 error = ::GetExtendedTcpTable(buffer, &size, FALSE, AF_INET6, TCP_TABLE_OWNER_MODULE_ALL, 0);
52 if (error != NO_ERROR)
53 ::VirtualFree(buffer, 0, MEM_RELEASE);
54 } while (error == ERROR_INSUFFICIENT_BUFFER);
55 if (error == NOERROR) {
56 auto table = (PMIB_TCP6TABLE_OWNER_MODULE)buffer;
57 AddTcp6Connections(table, map, local, first);
58 ::VirtualFree(buffer, 0, MEM_RELEASE);
59 }
60 }
61 if ((_trackedConnections & ConnectionType::Udp) == ConnectionType::Udp) {
62 size = orgSize;
63 do
64 {
65 buffer = ::VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
66 if (!buffer)

Callers 1

DoRefreshMethod · 0.80

Calls 5

emptyMethod · 0.80
clearMethod · 0.80
reserveMethod · 0.80
push_backMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected