forked from vanhoefm/modwifi-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientInfo.cpp
149 lines (112 loc) · 3.36 KB
/
ClientInfo.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <unistd.h>
#include "util.h"
#include "ClientInfo.h"
static const uint16_t SEQNUM_DELTA = 20;
static const struct timespec SEQNUM_TIMEOUT = {0, 500 * 1000};
ClientInfo::ClientInfo()
{
memset(mac, 0, sizeof(mac));
memset(&keys, 0, sizeof(keys));
}
ClientInfo::ClientInfo(const uint8_t mac[6])
{
memcpy(this->mac, mac, 6);
memset(&keys, 0, sizeof(keys));
memcpy(keys.stmac, mac, 6);
}
ClientInfo::ClientInfo(const uint8_t mac[6], const uint8_t bssid[6], const char *pw, const char *essid)
: ClientInfo(mac)
{
set_key_info(bssid, pw, essid);
}
/*static*/ int ClientInfo::test_new_seqnums()
{
ClientInfo client;
// Add new seqnums
if (!client.is_new_seqnum(10)) return -1;
if (!client.is_new_seqnum(110)) return -1;
if (!client.is_new_seqnum(500)) return -1;
// Now these should be old
if (client.is_new_seqnum(10)) return -2;
if (client.is_new_seqnum(5)) return -2;
if (client.is_new_seqnum(1)) return -2;
if (client.is_new_seqnum(110)) return -3;
if (client.is_new_seqnum(100)) return -3;
if (client.is_new_seqnum(500)) return -4;
if (client.is_new_seqnum(499)) return -4;
// And these should be new
if (!client.is_new_seqnum(11)) return -5;
if (!client.is_new_seqnum(13)) return -5;
if (!client.is_new_seqnum(120)) return -5;
if (!client.is_new_seqnum(505)) return -5;
if (!client.is_new_seqnum(535)) return -5;
if (!client.is_new_seqnum(120 - SEQNUM_DELTA - 10)) return -6;
if (!client.is_new_seqnum(505 - SEQNUM_DELTA - 10)) return -6;
// Test automatic expiration - these should be new again
sleep(1);
if (!client.is_new_seqnum(10)) return -7;
if (!client.is_new_seqnum(110)) return -7;
if (!client.is_new_seqnum(500)) return -7;
// Again test automatic expiration
sleep(1);
if (!client.is_new_seqnum(10)) return -8;
if (!client.is_new_seqnum(110)) return -8;
if (!client.is_new_seqnum(500)) return -8;
return 0;
}
bool ClientInfo::is_new_seqnum(uint16_t seqnum)
{
SeqnumInfo seqinfo(seqnum);
remove_old_seqnums();
// if we don't return, then (seqinfo <= it)
auto it = upper_inclusive(seqnums, seqinfo);
if (it == seqnums.end()) {
// no upper bound? Then it is new (we ignore conside overflow).
seqnums.insert(seqinfo);
//printf("Added %d\n", seqinfo.getnum());
return true;
}
// if this is true, then (it - SEQNUM_DELTA < seqinfo <= it)
if (it->getnum() - SEQNUM_DELTA < seqnum)
{
//printf("Updating time of %d\n", it->getnum());
it->updatetime();
return false;
}
// we have a new seqnum. Erase the one before it, if it exists
if (it != seqnums.begin() && (--it)->getnum() + SEQNUM_DELTA > seqnum) {
//printf("Erasing %d\n", it->getnum());
seqnums.erase(it);
}
// finally insert the new seqnum
seqnums.insert(seqinfo);
//printf("Added %d\n", seqinfo.getnum());
return true;
}
void ClientInfo::set_key_info(const uint8_t bssid[6], const char *passphrase, const char *essid)
{
memcpy(keys.bssid, bssid, 6);
strncpy(keys.psk, passphrase, sizeof(keys.psk));
strncpy(keys.essid, essid, sizeof(keys.essid));
}
void ClientInfo::remove_old_seqnums()
{
struct timespec now, oldest;
clock_gettime(CLOCK_MONOTONIC, &now);
timespec_diff(&now, &SEQNUM_TIMEOUT, &oldest);
auto it = seqnums.begin();
while (it != seqnums.end())
{
if (timespec_cmp(&it->gettsf(), &oldest) < 0) {
auto todel = it;
++it;
seqnums.erase(todel);
} else {
++it;
}
}
}
void ClientInfo::remove_all_seqnums()
{
seqnums.clear();
}