-
Notifications
You must be signed in to change notification settings - Fork 0
/
arp_table.cpp
89 lines (83 loc) · 2.17 KB
/
arp_table.cpp
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
#include "arp_table.h"
#include "QMainWindow"
arp_table::arp_table()
{
table = NULL;
size = 0;
strcpy(ip,"");
strcpy(mac,"");
strcpy(type,"");
}
arp_table::~arp_table()
{
delete table;
}
bool arp_table::mac_to_string(BYTE _mac[], int _mac_len, char _smac[])
{
if (!_mac || !_mac_len || !_smac)
return false;
for (int _i = 0; _i < _mac_len; _i++)
{
if (_i == _mac_len - 1)
sprintf(_smac+(_i*3), "%02X", ((int)_mac[_i])&0xff);
else
sprintf(_smac+(_i*3), "%02X-", ((int)_mac[_i])&0xff);
}
return true;
}
void arp_table::get_arp_table()
{
//ôóíêöèÿ, âîçâðàùàþùàÿ arp-òàáëèöó
//table - óêàçàòåëü íà arp-òàáëèöó
//size - ðàçìåð
//true - òàáëèöà îòñîðòèðîâàííà â ïîðÿäêå âîçðàñòàíèÿ ip àäðåñîâ
GetIpNetTable(table, &size, true);
table = (PMIB_IPNETTABLE) new MIB_IPNETTABLE[size];
//ïðîäîëæèòü ðàáîòó, åñëè ôóíêöèÿ îòðàáîòàëà áåç îøèáîê
if (GetIpNetTable(table, &size, true) != NO_ERROR)
return;
_table = new Cell[table->dwNumEntries - 4];
_count = 0;
for (int i = 0; i < table->dwNumEntries - 4; i++)
{
mac_to_string(table->table[i].bPhysAddr, table->table[i].dwPhysAddrLen, mac);
switch (table->table[i].dwType)
{
case 1:
strcpy(type, "other");
break;
case 2:
strcpy(type, "Invalidated");
break;
case 3:
strcpy(type, "Dynamic");
break;
case 4:
strcpy(type, "Static");
break;
default:
strcpy(type,"");
}
//ñòðóêòóðà, ïðåäñòàâëÿåò ñîáîé ip àäðåñ (â ðàçëè÷íûõ òèïàõ äàííûõ)
struct in_addr adr;
adr.s_addr = table->table[i].dwAddr;
item = new Cell();
strcpy(item->ip, inet_ntoa(adr));
strcpy(item->mac, mac);
strcpy(item->type, type);
if (strcmp(item->mac,"")!=0)
{
*(_table + _count) = *item;
_count++;
}
delete item;
}
}
Cell* arp_table::arptable()
{
return _table;
}
int arp_table::length()
{
return _count;
}