Changeset View
Changeset View
Standalone View
Standalone View
src/dht/database.cpp
Show First 20 Lines • Show All 121 Lines • ▼ Show 20 Line(s) | 118 | { | |||
---|---|---|---|---|---|
122 | while (dbl->count() > 0 && dbl->first().expired(now)) | 122 | while (dbl->count() > 0 && dbl->first().expired(now)) | ||
123 | { | 123 | { | ||
124 | dbl->pop_front(); | 124 | dbl->pop_front(); | ||
125 | } | 125 | } | ||
126 | ++itr; | 126 | ++itr; | ||
127 | } | 127 | } | ||
128 | } | 128 | } | ||
129 | 129 | | |||
130 | dht::Key Database::genToken(const net::Address & addr) | 130 | QByteArray Database::genToken(const net::Address & addr) | ||
131 | { | 131 | { | ||
132 | if (addr.ipVersion() == 4) | 132 | if (addr.ipVersion() == 4) | ||
133 | { | 133 | { | ||
134 | Uint8 tdata[14]; | 134 | Uint8 tdata[14]; | ||
135 | TimeStamp now = bt::CurrentTime(); | 135 | TimeStamp now = bt::CurrentTime(); | ||
136 | // generate a hash of the ip port and the current time | 136 | // generate a hash of the ip port and the current time | ||
137 | // should prevent anybody from crapping things up | 137 | // should prevent anybody from crapping things up | ||
138 | bt::WriteUint32(tdata, 0, addr.toIPv4Address()); | 138 | bt::WriteUint32(tdata, 0, addr.toIPv4Address()); | ||
139 | bt::WriteUint16(tdata, 4, addr.port()); | 139 | bt::WriteUint16(tdata, 4, addr.port()); | ||
140 | bt::WriteUint64(tdata, 6, now); | 140 | bt::WriteUint64(tdata, 6, now); | ||
141 | 141 | | |||
142 | dht::Key token = SHA1Hash::generate(tdata, 14); | 142 | QByteArray token = SHA1Hash::generate(tdata, 14).toByteArray(); | ||
143 | // keep track of the token, tokens will expire after a while | 143 | // keep track of the token, tokens will expire after a while | ||
144 | tokens.insert(token, now); | 144 | tokens.insert(token, now); | ||
145 | return token; | 145 | return token; | ||
146 | } | 146 | } | ||
147 | else | 147 | else | ||
148 | { | 148 | { | ||
149 | Uint8 tdata[26]; | 149 | Uint8 tdata[26]; | ||
150 | TimeStamp now = bt::CurrentTime(); | 150 | TimeStamp now = bt::CurrentTime(); | ||
151 | // generate a hash of the ip port and the current time | 151 | // generate a hash of the ip port and the current time | ||
152 | // should prevent anybody from crapping things up | 152 | // should prevent anybody from crapping things up | ||
153 | memcpy(tdata, addr.toIPv6Address().c, 16); | 153 | memcpy(tdata, addr.toIPv6Address().c, 16); | ||
154 | bt::WriteUint16(tdata, 16, addr.port()); | 154 | bt::WriteUint16(tdata, 16, addr.port()); | ||
155 | bt::WriteUint64(tdata, 18, now); | 155 | bt::WriteUint64(tdata, 18, now); | ||
156 | 156 | | |||
157 | dht::Key token = SHA1Hash::generate(tdata, 26); | 157 | QByteArray token = SHA1Hash::generate(tdata, 26).toByteArray(); | ||
158 | // keep track of the token, tokens will expire after a while | 158 | // keep track of the token, tokens will expire after a while | ||
159 | tokens.insert(token, now); | 159 | tokens.insert(token, now); | ||
160 | return token; | 160 | return token; | ||
161 | } | 161 | } | ||
162 | } | 162 | } | ||
163 | 163 | | |||
164 | bool Database::checkToken(const dht::Key & token, const net::Address & addr) | 164 | bool Database::checkToken(const QByteArray & token, const net::Address & addr) | ||
165 | { | 165 | { | ||
166 | // the token must be in the map | 166 | // the token must be in the map | ||
167 | if (!tokens.contains(token)) | 167 | if (!tokens.contains(token)) | ||
168 | return false; | 168 | return false; | ||
169 | 169 | | |||
170 | // in the map so now get the timestamp and regenerate the token | 170 | // in the map so now get the timestamp and regenerate the token | ||
171 | // using the IP and port of the sender | 171 | // using the IP and port of the sender | ||
172 | TimeStamp ts = tokens[token]; | 172 | TimeStamp ts = tokens[token]; | ||
173 | 173 | | |||
174 | if (addr.ipVersion() == 4) | 174 | if (addr.ipVersion() == 4) | ||
175 | { | 175 | { | ||
176 | Uint8 tdata[14]; | 176 | Uint8 tdata[14]; | ||
177 | bt::WriteUint32(tdata, 0, addr.toIPv4Address()); | 177 | bt::WriteUint32(tdata, 0, addr.toIPv4Address()); | ||
178 | bt::WriteUint16(tdata, 4, addr.port()); | 178 | bt::WriteUint16(tdata, 4, addr.port()); | ||
179 | bt::WriteUint64(tdata, 6, ts); | 179 | bt::WriteUint64(tdata, 6, ts); | ||
180 | dht::Key ct = SHA1Hash::generate(tdata, 14); | 180 | QByteArray ct = SHA1Hash::generate(tdata, 14).toByteArray(); | ||
181 | 181 | | |||
182 | // compare the generated token to the one received | 182 | // compare the generated token to the one received | ||
183 | if (token != ct) // not good, this peer didn't went through the proper channels | 183 | if (token != ct) // not good, this peer didn't went through the proper channels | ||
184 | { | 184 | { | ||
185 | Out(SYS_DHT | LOG_DEBUG) << "Invalid token" << endl; | 185 | Out(SYS_DHT | LOG_DEBUG) << "Invalid token" << endl; | ||
186 | return false; | 186 | return false; | ||
187 | } | 187 | } | ||
188 | } | 188 | } | ||
189 | else | 189 | else | ||
190 | { | 190 | { | ||
191 | Uint8 tdata[26]; | 191 | Uint8 tdata[26]; | ||
192 | 192 | | |||
193 | memcpy(tdata, addr.toIPv6Address().c, 16); | 193 | memcpy(tdata, addr.toIPv6Address().c, 16); | ||
194 | bt::WriteUint16(tdata, 16, addr.port()); | 194 | bt::WriteUint16(tdata, 16, addr.port()); | ||
195 | bt::WriteUint64(tdata, 18, ts); | 195 | bt::WriteUint64(tdata, 18, ts); | ||
196 | 196 | | |||
197 | dht::Key ct = SHA1Hash::generate(tdata, 26); | 197 | QByteArray ct = SHA1Hash::generate(tdata, 26).toByteArray(); | ||
198 | // compare the generated token to the one received | 198 | // compare the generated token to the one received | ||
199 | if (token != ct) // not good, this peer didn't went through the proper channels | 199 | if (token != ct) // not good, this peer didn't went through the proper channels | ||
200 | { | 200 | { | ||
201 | Out(SYS_DHT | LOG_DEBUG) << "Invalid token" << endl; | 201 | Out(SYS_DHT | LOG_DEBUG) << "Invalid token" << endl; | ||
202 | return false; | 202 | return false; | ||
203 | } | 203 | } | ||
204 | } | 204 | } | ||
205 | 205 | | |||
Show All 20 Lines |