@@ -163,8 +163,7 @@ void FileSecurityDb::set_entry_local_ltk(
163
163
164
164
entry->flags .ltk_sent = true ;
165
165
166
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS_LTK, SEEK_SET);
167
- fwrite (<k, sizeof (ltk_t ), 1 , _db_file);
166
+ db_write (<k, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS_LTK);
168
167
}
169
168
170
169
void FileSecurityDb::set_entry_local_ediv_rand (
@@ -177,10 +176,8 @@ void FileSecurityDb::set_entry_local_ediv_rand(
177
176
return ;
178
177
}
179
178
180
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS_EDIV, SEEK_SET);
181
- fwrite (&ediv, sizeof (ediv_t ), 1 , _db_file);
182
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS_RAND, SEEK_SET);
183
- fwrite (&rand, sizeof (rand_t ), 1 , _db_file);
179
+ db_write (&ediv, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS_EDIV);
180
+ db_write (&rand, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS_RAND);
184
181
}
185
182
186
183
/* peer's keys */
@@ -198,8 +195,7 @@ void FileSecurityDb::set_entry_peer_ltk(
198
195
199
196
entry->flags .ltk_stored = true ;
200
197
201
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS_LTK, SEEK_SET);
202
- fwrite (<k, sizeof (ltk_t ), 1 , _db_file);
198
+ db_write (<k, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS_LTK);
203
199
}
204
200
205
201
void FileSecurityDb::set_entry_peer_ediv_rand (
@@ -212,10 +208,8 @@ void FileSecurityDb::set_entry_peer_ediv_rand(
212
208
return ;
213
209
}
214
210
215
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS_EDIV, SEEK_SET);
216
- fwrite (&ediv, sizeof (ediv_t ), 1 , _db_file);
217
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS_RAND, SEEK_SET);
218
- fwrite (&rand, sizeof (rand_t ), 1 , _db_file);
211
+ db_write (&ediv, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS_EDIV);
212
+ db_write (&rand, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS_RAND);
219
213
}
220
214
221
215
void FileSecurityDb::set_entry_peer_irk (
@@ -229,8 +223,7 @@ void FileSecurityDb::set_entry_peer_irk(
229
223
230
224
entry->flags .irk_stored = true ;
231
225
232
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY_IRK, SEEK_SET);
233
- fwrite (&irk, sizeof (irk_t ), 1 , _db_file);
226
+ db_write (&irk, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY_IRK);
234
227
}
235
228
236
229
void FileSecurityDb::set_entry_peer_bdaddr (
@@ -243,10 +236,8 @@ void FileSecurityDb::set_entry_peer_bdaddr(
243
236
return ;
244
237
}
245
238
246
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY_ADDRESS, SEEK_SET);
247
- fwrite (&peer_address, sizeof (address_t ), 1 , _db_file);
248
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY_ADDRESS_IS_PUBLIC, SEEK_SET);
249
- fwrite (&address_is_public, sizeof (bool ), 1 , _db_file);
239
+ db_write (&peer_address, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY_ADDRESS);
240
+ db_write (&address_is_public, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY_ADDRESS_IS_PUBLIC);
250
241
}
251
242
252
243
void FileSecurityDb::set_entry_peer_csrk (
@@ -260,9 +251,7 @@ void FileSecurityDb::set_entry_peer_csrk(
260
251
261
252
entry->flags .csrk_stored = true ;
262
253
263
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_SIGNING, SEEK_SET);
264
- /* only write in the csrk */
265
- fwrite (&csrk, sizeof (csrk_t ), 1 , _db_file);
254
+ db_write (&csrk, entry->file_offset + DB_STORE_OFFSET_PEER_SIGNING);
266
255
}
267
256
268
257
void FileSecurityDb::set_entry_peer_sign_counter (
@@ -290,14 +279,9 @@ void FileSecurityDb::restore() {
290
279
}
291
280
}
292
281
293
- fseek (_db_file, DB_OFFSET_LOCAL_IDENTITY, SEEK_SET);
294
- fread (&_local_identity, sizeof (_local_identity), 1 , _db_file);
295
-
296
- fseek (_db_file, DB_OFFSET_LOCAL_CSRK, SEEK_SET);
297
- fread (&_local_csrk, sizeof (_local_csrk), 1 , _db_file);
298
-
299
- fseek (_db_file, DB_OFFSET_LOCAL_SIGN_COUNT, SEEK_SET);
300
- fread (&_local_sign_counter, sizeof (_local_sign_counter), 1 , _db_file);
282
+ db_read (&_local_identity, DB_OFFSET_LOCAL_IDENTITY);
283
+ db_read (&_local_csrk, DB_OFFSET_LOCAL_CSRK);
284
+ db_read (&_local_sign_counter, DB_OFFSET_LOCAL_SIGN_COUNT);
301
285
302
286
fseek (_db_file, DB_OFFSET_ENTRIES, SEEK_SET);
303
287
/* we read the entries partially and fill the offsets ourselves*/
@@ -313,13 +297,11 @@ void FileSecurityDb::sync(entry_handle_t db_handle) {
313
297
return ;
314
298
}
315
299
316
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_SIGNING_COUNT, SEEK_SET);
317
- fwrite (&entry->peer_sign_counter , sizeof (sign_count_t ), 1 , _db_file);
300
+ db_write (&entry->peer_sign_counter , entry->file_offset + DB_STORE_OFFSET_PEER_SIGNING_COUNT);
318
301
}
319
302
320
303
void FileSecurityDb::set_restore (bool reload) {
321
- fseek (_db_file, DB_OFFSET_RESTORE, SEEK_SET);
322
- fwrite (&reload, sizeof (bool ), 1 , _db_file);
304
+ db_write (&reload, DB_OFFSET_RESTORE);
323
305
}
324
306
325
307
/* helper functions */
@@ -358,42 +340,50 @@ SecurityEntryIdentity_t* FileSecurityDb::read_in_entry_peer_identity(entry_handl
358
340
if (!entry) {
359
341
return NULL ;
360
342
}
361
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY, SEEK_SET);
362
- fread (&_buffer, sizeof (SecurityEntryIdentity_t), 1 , _db_file);
363
- return reinterpret_cast <SecurityEntryIdentity_t*>(_buffer);
343
+
344
+ SecurityEntryIdentity_t* identity = reinterpret_cast <SecurityEntryIdentity_t*>(_buffer);
345
+ db_read (identity, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY);
346
+
347
+ return identity;
364
348
};
365
349
366
350
SecurityEntryKeys_t* FileSecurityDb::read_in_entry_peer_keys (entry_handle_t db_entry) {
367
351
entry_t *entry = as_entry (db_entry);
368
352
if (!entry) {
369
353
return NULL ;
370
354
}
371
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS, SEEK_SET);
372
- fread (&_buffer, sizeof (SecurityEntryKeys_t), 1 , _db_file);
373
- return reinterpret_cast <SecurityEntryKeys_t*>(_buffer);
355
+
356
+ SecurityEntryKeys_t* keys = reinterpret_cast <SecurityEntryKeys_t*>(_buffer);
357
+ db_read (keys, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS);
358
+
359
+ return keys;
374
360
};
375
361
376
362
SecurityEntryKeys_t* FileSecurityDb::read_in_entry_local_keys (entry_handle_t db_entry) {
377
363
entry_t *entry = as_entry (db_entry);
378
364
if (!entry) {
379
365
return NULL ;
380
366
}
381
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS, SEEK_SET);
382
- fread (&_buffer, sizeof (SecurityEntryKeys_t), 1 , _db_file);
383
- return reinterpret_cast <SecurityEntryKeys_t*>(_buffer);
367
+
368
+ SecurityEntryKeys_t* keys = reinterpret_cast <SecurityEntryKeys_t*>(_buffer);
369
+ db_read (keys, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS);
370
+
371
+ return keys;
384
372
};
385
373
386
374
SecurityEntrySigning_t* FileSecurityDb::read_in_entry_peer_signing (entry_handle_t db_entry) {
387
375
entry_t *entry = as_entry (db_entry);
388
376
if (!entry) {
389
377
return NULL ;
390
378
}
391
- fseek (_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_SIGNING, SEEK_SET);
392
379
393
380
/* only read in the csrk */
394
- fread (&_buffer, sizeof (csrk_t ), 1 , _db_file);
395
- SecurityEntrySigning_t* signing = reinterpret_cast <SecurityEntrySigning_t*>(_buffer);
381
+ csrk_t * csrk = reinterpret_cast <csrk_t *>(_buffer);
382
+ db_read (csrk, entry->file_offset + DB_STORE_OFFSET_PEER_SIGNING);
383
+
384
+
396
385
/* use the counter held in memory */
386
+ SecurityEntrySigning_t* signing = reinterpret_cast <SecurityEntrySigning_t*>(_buffer);
397
387
signing->counter = entry->peer_sign_counter ;
398
388
399
389
return signing;
0 commit comments