-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathot_persist.c
493 lines (411 loc) · 12.3 KB
/
ot_persist.c
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* This file add persistence of peers and torrents in memory on the fly.
* Author: FengGu <[email protected]>
*
* This software was written by Dirk Engling <[email protected]>
It is considered beerware. Prost. Skol. Cheers or whatever.
$id$ */
/* System */
#include <stdlib.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <ctype.h>
#include <inttypes.h>
#include <assert.h>
#ifdef WANT_SYSLOGS
#include <syslog.h>
#endif
/* Libowfat */
#include "byte.h"
#include "io.h"
#include "ip4.h"
#include "ip6.h"
/* Opentracker */
#include "trackerlogic.h"
#include "ot_mutex.h"
#include "ot_accesslist.h"
#include "ot_persist.h"
#ifdef WANT_PERSISTENCE
#ifdef _DEBUG
#define _DEBUG_PERSIST
#endif /* _DEBUG */
#ifdef _DEBUG_PERSIST
#define LOG_ERR( ... ) fprintf( stderr, __VA_ARGS__ )
#else
#define LOG_ERR( ... )
#endif /* _DEBUG_PERSIST */
#define PERSIST_SLEEP_INTERVAL 1000000 /* macroseconds */
/* Just for file corruption checking */
#define OT_DUMP_TORRENT 0xfe
#define OT_DUMP_EOF 0xff
#define OT_DUMP_IDENTI "OPENTRACKER"
#define OT_DUMP_IDENTI_LEN (sizeof(OT_DUMP_IDENTI) - 1)
#define OT_DUMP_VERSION "0001"
#define OT_DUMP_VERSION_LEN (sizeof(OT_DUMP_VERSION) - 1)
#define OT_DUMP_IDENTI_VERSION (OT_DUMP_IDENTI OT_DUMP_VERSION)
#define OT_DUMP_IDENTI_VERSION_LEN (sizeof(OT_DUMP_IDENTI_VERSION) - 1)
typedef struct dump_saveparam {
time_t seconds;
int changes;
} dump_saveparam_t;
char * g_persistfile;
int g_persistmode = PMODE_NULL;
static int dump_dirty;
static int dump_lastsave;
static size_t saveparam_len;
static dump_saveparam_t *saveparams;
static int persist_add_peer(ot_hash *hash, ot_peerlist *peer_list, ot_peer *peer) {
int exactmatch, delta_torrentcount = 0;
ot_torrent *torrent;
ot_peer *peer_dest;
/* eliminate compiler warnings */
(void)peer_list;
ot_vector *torrents_list = mutex_bucket_lock_by_hash(*hash);
if( !accesslist_hashisvalid( hash ) ) {
mutex_bucket_unlock_by_hash( *hash, 0 );
return 0;
}
torrent = vector_find_or_insert( torrents_list, (void*)hash, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch );
if( !torrent ) {
mutex_bucket_unlock_by_hash( *hash, 0 );
return 0;
}
if( !exactmatch ) {
/* Create a new torrent entry, then */
memcpy( torrent->hash, hash, sizeof(ot_hash) );
if( !( torrent->peer_list = malloc( sizeof (ot_peerlist) ) ) ) {
vector_remove_torrent( torrents_list, torrent );
mutex_bucket_unlock_by_hash( *hash, 0 );
return 0;
}
byte_zero( torrent->peer_list, sizeof( ot_peerlist ) );
delta_torrentcount = 1;
}
/* Ignore torrent base in odb file, just use current clock. */
torrent->peer_list->base = g_now_minutes;
/* Check for peer in torrent */
peer_dest = vector_find_or_insert_peer( &(torrent->peer_list->peers), peer, &exactmatch );
if( !peer_dest ) {
mutex_bucket_unlock_by_hash( *hash, delta_torrentcount );
return 0;
}
/* If we hadn't had a match, create peer there */
if( !exactmatch ) {
torrent->peer_list->peer_count++;
if( OT_PEERFLAG(peer) & PEER_FLAG_COMPLETED )
torrent->peer_list->down_count++;
if( OT_PEERFLAG(peer) & PEER_FLAG_SEEDING )
torrent->peer_list->seed_count++;
} else {
LOG_ERR("Repeat peer in a same torrent\n");
assert(0);
}
memcpy( peer_dest, peer, sizeof(ot_peer) );
mutex_bucket_unlock_by_hash( *hash, delta_torrentcount );
return 0;
}
static int persist_load_peers(FILE *fp, ot_hash *hash, ot_peerlist *peer_list) {
unsigned int count;
unsigned int i;
ot_peer peer;
#ifdef _DEBUG_PERSIST
struct in_addr myaddr;
char str[40];
#endif /* _DEBUG_PERSIST */
if (fread(&count, sizeof(unsigned int), 1, fp) == 0) goto rerr;
LOG_ERR("Peer count: %d\n", count);
if (count == 0) return 0;
for (i = 0; i < count; ++i) {
if (fread(&peer, sizeof(ot_peer), 1, fp) != 1) goto rerr;
#ifdef _DEBUG_PERSIST
/* ot_peer's ip and port is big endian. */
myaddr.s_addr = *(unsigned int *)&peer;
if (!inet_ntop(AF_INET, &myaddr, str, sizeof(str))) {
LOG_ERR("inet_ntop failed");
assert(0);
}
LOG_ERR("%s:%d\n", str, ntohs(*(unsigned short *)((uint8_t*)(&peer) + (OT_IP_SIZE))));
#endif /* _DEBUG_PERSIST */
if (persist_add_peer(hash, peer_list, &peer) < 0) {
LOG_ERR("persist_add_peer failed\n");
return -1;
}
}
return 0;
rerr:
LOG_ERR("%s\n", strerror(errno));
return -1;
}
static int persist_load_torrent(FILE *fp) {
ot_hash hash;
ot_peerlist peer_list;
#ifdef _DEBUG_PERSIST
char log_buf[512];
#endif /* _DEBUG_PERSIST */
/* load torrent hash */
if (fread(&hash, sizeof(ot_hash), 1, fp) != 1) goto rerr;
#ifdef _DEBUG_PERSIST
if (urlencode((const char *)&hash, sizeof(ot_hash), log_buf, 512) <= 0) {
LOG_ERR("urlencode failed\n");
assert(0);
}
LOG_ERR("%s\n", log_buf);
#endif /* _DEBUG_PERSIST */
/*
* load peer_list data:
*
* struct ot_peerlist {
* ot_time base;
* size_t seed_count;
* size_t peer_count;
* size_t down_count;
* ot_vector peers;
* }
*
*/
if (fread(&peer_list.base, sizeof(ot_time), 1, fp) != 1) goto rerr;
if (fread(&peer_list.seed_count, sizeof(size_t), 1, fp) != 1) goto rerr;
if (fread(&peer_list.peer_count, sizeof(size_t), 1, fp) != 1) goto rerr;
if (fread(&peer_list.down_count, sizeof(size_t), 1, fp) != 1) goto rerr;
if (persist_load_peers(fp, &hash, &peer_list) < 0) goto rerr;
return 0;
rerr:
LOG_ERR("%s\n", strerror(errno));
return -1;
}
int persist_load_file() {
FILE *fp;
uint8_t buf[1024];
int version;
#ifdef _DEBUG_PERSIST
int torrent_cnt = 0;
#endif /* _DEBUG_PERSIST */
if (!g_persistfile) {
g_persistfile = strdup("opentracker.odb");
}
fp = fopen(g_persistfile, "r");
if (!fp) {
LOG_ERR("%s\n", strerror(errno));
return 0;
}
if (fread(buf, OT_DUMP_IDENTI_VERSION_LEN, 1, fp) == 0) {
LOG_ERR("%s\n", strerror(errno));
goto rerr;
}
if (memcmp(buf, OT_DUMP_IDENTI, OT_DUMP_IDENTI_LEN) != 0) {
LOG_ERR("%s\n", strerror(errno));
goto rerr;
}
version = atoi((const char *)buf + OT_DUMP_IDENTI_LEN);
if (version != 1) {
LOG_ERR("Can't handle ODB format version %d\n", version);
goto rerr;
}
for ( ; ; ) {
if (fread(buf, 1, 1, fp) == 0) goto rerr;
if (buf[0] != OT_DUMP_TORRENT && buf[0] != OT_DUMP_EOF) {
LOG_ERR("ODB file corrupted\n");
goto rerr;
}
if (buf[0] == OT_DUMP_EOF) {
break;
}
if (persist_load_torrent(fp) < 0) goto rerr;
#ifdef _DEBUG_PERSIST
++torrent_cnt;
#endif /* _DEBUG_PERSIST */
}
#ifdef _DEBUG_PERSIST
LOG_ERR("Load ODB file success: torrent count: %d\n", torrent_cnt);
#endif /* _DEBUG_PERSIST */
fclose(fp);
return 0;
rerr:
fclose(fp);
return -1;
}
void persist_change(struct ot_workstruct *ws) {
(void)ws; /* In "dump" mode, don't use this param */
switch (g_persistmode) {
case PMODE_NULL:
return;
case PMODE_DUMP:
++dump_dirty;
break;
default:
assert(0);
break;
}
return;
}
void persist_append_save_param(time_t seconds, int changes) {
saveparams = realloc(saveparams, sizeof(dump_saveparam_t) * (saveparam_len + 1));
if (!saveparams) {
LOG_ERR("out of memory\n");
exit(123);
}
saveparams[saveparam_len].seconds = seconds;
saveparams[saveparam_len].changes = changes;
++saveparam_len;
}
static int persist_dump_peers(ot_peerlist *peer_list, FILE *fp ) {
unsigned int bucket, num_buckets = 1;
ot_vector *bucket_list = &peer_list->peers;
unsigned int count = 0;
if( OT_PEERLIST_HASBUCKETS(peer_list) ) {
num_buckets = bucket_list->size;
bucket_list = (ot_vector *)bucket_list->data;
}
/* write peers count */
for (bucket = 0; bucket < num_buckets; ++bucket) {
count += bucket_list[bucket].size;
}
if (fwrite(&count, sizeof(unsigned int), 1, fp) == 0) goto werr;
for (bucket = 0; bucket < num_buckets; ++bucket) {
ot_peer *peers = (ot_peer*)bucket_list[bucket].data;
size_t peer_count = bucket_list[bucket].size;
while( peer_count-- ) {
if (fwrite(peers++, sizeof(ot_peer), 1, fp) == 0) goto werr;
}
}
return 0;
werr:
LOG_ERR("%s: persist dump peers failed\n", __FUNCTION__);
return -1;
}
static int persist_dump_torrent(ot_torrent* torrent, FILE *fp ) {
uint8_t c;
ot_peerlist *peer_list = torrent->peer_list;
ot_hash *hash = &torrent->hash;
/* Write TORRENT opcode */
c = OT_DUMP_TORRENT;
if (fwrite(&c, 1, 1, fp) != 1) goto werr;
/* Write torrent hash */
if (fwrite(hash, sizeof(ot_hash), 1, fp) == 0) goto werr;
/*
* write peer_list data:
*
* struct ot_peerlist {
* ot_time base;
* size_t seed_count;
* size_t peer_count;
* size_t down_count;
* ot_vector peers;
* }
*
*/
if (fwrite(&peer_list->base, sizeof(ot_time), 1, fp) == 0) goto werr;
if (fwrite(&peer_list->seed_count, sizeof(size_t), 1, fp) == 0) goto werr;
if (fwrite(&peer_list->peer_count, sizeof(size_t), 1, fp) == 0) goto werr;
if (fwrite(&peer_list->down_count, sizeof(size_t), 1, fp) == 0) goto werr;
if (persist_dump_peers(peer_list, fp) < 0) goto werr;
return 0;
werr:
LOG_ERR("%s: persist dump torrent failed\n", __FUNCTION__);
return -1;
}
static int persist_dump_make() {
int bucket;
size_t j;
uint8_t c;
FILE *fp;
char tmpfile[256];
snprintf(tmpfile, 256, "temp-%u.odb", (unsigned int)g_now_seconds);
LOG_ERR("Start write odb file:%s\n", tmpfile);
fp = fopen(tmpfile, "w");
if (!fp) {
LOG_ERR("%s: fopen odb file:%s failed: %s\n", __FUNCTION__, tmpfile, strerror(errno));
return -1;
}
/* write identifier and version */
if (fwrite(OT_DUMP_IDENTI_VERSION, OT_DUMP_IDENTI_VERSION_LEN, 1, fp) == 0) goto werr;
/* Dump torrents and peers */
for(bucket=0; bucket < OT_BUCKET_COUNT; ++bucket ) {
ot_vector *torrents_list = mutex_bucket_lock( bucket );
ot_torrent *torrents = (ot_torrent*)(torrents_list->data);
for( j=0; j < torrents_list->size; ++j )
if( persist_dump_torrent( torrents + j, fp ) < 0 ) {
mutex_bucket_unlock( bucket, 0 );
goto werr;
}
mutex_bucket_unlock( bucket, 0 );
}
/* EOF opcode */
c = OT_DUMP_EOF;
if (fwrite(&c, 1, 1, fp) != 1) goto werr;
/* Make sure data will not remain on the OS's output buffers. */
fflush(fp);
fsync(fileno(fp));
fclose(fp);
/* Use RENAME to make sure the dump file is changed atomically
* only if the generate dump file is ok. */
if (!g_persistfile) {
g_persistfile = strdup("opentracker.odb");
}
if (rename(tmpfile, g_persistfile) < 0) {
unlink(tmpfile);
return -1;
}
dump_dirty = 0;
dump_lastsave = g_now_seconds;
return 0;
werr:
LOG_ERR("%s: persist dump odb file:%s failed: %s\n", __FUNCTION__, tmpfile, strerror(errno));
fclose(fp);
unlink(tmpfile);
return -1;
}
int persist_set_mode(char *value) {
while( isspace(*value) ) ++value;
if (!strcmp(value, "null")) {
g_persistmode = PMODE_NULL;
} else if (!strcmp(value, "dump")) {
g_persistmode = PMODE_DUMP;
} else {
return -1;
}
return 0;
}
static void * persist_worker( void * args ) {
size_t i = 0;
(void)args;
while (1) {
if (g_persistmode == PMODE_DUMP) {
for (i = 0; i < saveparam_len; ++i) {
dump_saveparam_t *sp = saveparams + i;
if (dump_dirty >= sp->changes && g_now_seconds - dump_lastsave > sp->seconds) {
persist_dump_make();
break;
}
}
if( !g_opentracker_running ) return NULL;
usleep(PERSIST_SLEEP_INTERVAL);
} else {
return NULL;
}
}
}
void persist_make() {
if (g_persistmode == PMODE_DUMP && dump_dirty != 0) {
persist_dump_make();
}
}
static pthread_t thread_id;
void persist_init( ) {
if (g_persistmode != PMODE_NULL)
pthread_create( &thread_id, NULL, persist_worker, NULL );
}
void persist_deinit( ) {
if (g_persistmode != PMODE_NULL)
pthread_cancel( thread_id );
if (saveparams) free(saveparams);
if (g_persistfile) free(g_persistfile);
}
#endif
const char *g_version_persist_c = "$Source: ot_persist.c Added by FengGu <[email protected]>,v $: $Revision: 0.01 $\n";