-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.c
426 lines (371 loc) · 11.2 KB
/
db.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
int getTorrentId(sqlite3* db, char* hash){
sqlite3_stmt* queryTorrent;
int error;
error=sqlite3_prepare_v2(db,QUERY_TORRENT_ID,strlen(QUERY_TORRENT_ID),&queryTorrent,NULL);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
return DB_FAILED;
}
error=sqlite3_bind_text(queryTorrent,1,hash,strlen(hash),SQLITE_TRANSIENT);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(queryTorrent);
return DB_FAILED;
}
error=sqlite3_step(queryTorrent);
switch(error){
case SQLITE_DONE:
error=DB_NOSUCHHASH;
break;
case SQLITE_BUSY:
ERROR(printf("SQLite timeout elapsed while querying torrent ID\n"));
error=DB_FAILED;
break;
case SQLITE_ERROR:
ERROR(printf("SQLite encountered an error while querying torrent ID\n"));
error=DB_FAILED;
break;
case SQLITE_ROW:
error=sqlite3_column_int(queryTorrent,0);
break;
}
sqlite3_finalize(queryTorrent);
return error;
}
int queryTorrentParam(sqlite3* db, char* query, int torrent){
sqlite3_stmt* queryStatement;
int error;
error=sqlite3_prepare_v2(db,query,strlen(query),&queryStatement,NULL);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
return DB_FAILED;
}
error=sqlite3_bind_int(queryStatement,1,torrent);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(queryStatement);
return DB_FAILED;
}
error=sqlite3_step(queryStatement);
switch(error){
case SQLITE_DONE:
//this should never happen
error=DB_FAILED;
break;
case SQLITE_BUSY:
ERROR(printf("SQLite timeout elapsed while querying a torrent parameter\n"));
error=DB_FAILED;
break;
case SQLITE_ERROR:
ERROR(printf("SQLite encountered an error while querying a torrent Parameter\n"));
error=DB_FAILED;
break;
case SQLITE_ROW:
error=sqlite3_column_int(queryStatement,0);
break;
}
sqlite3_finalize(queryStatement);
return error;
}
int addPeer(sqlite3* db, ANNOUNCE_REQUEST* ar, int torrent){
sqlite3_stmt* addPeer;
int error;
struct timeval time={0,0};
if(gettimeofday(&time,NULL)){
ERROR(printf("Could not get current time\n"));
return E_GENERIC;
}
error=sqlite3_prepare_v2(db,ADD_TORRENT_PEER,strlen(ADD_TORRENT_PEER),&addPeer,NULL);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
return DB_FAILED;
}
//torrent
error=sqlite3_bind_int(addPeer,1,torrent);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(addPeer);
return DB_FAILED;
}
//id
error=sqlite3_bind_text(addPeer,2,ar->peer_id,strlen(ar->peer_id),SQLITE_TRANSIENT);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(addPeer);
return DB_FAILED;
}
//ip
error=sqlite3_bind_text(addPeer,3,ar->ip,strlen(ar->ip),SQLITE_TRANSIENT);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(addPeer);
return DB_FAILED;
}
//port
error=sqlite3_bind_int(addPeer,4,ar->port);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(addPeer);
return DB_FAILED;
}
//expires
error=sqlite3_bind_int(addPeer,5,time.tv_sec);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(addPeer);
return DB_FAILED;
}
//protocol
error=sqlite3_bind_int(addPeer,6,ar->protover);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(addPeer);
return DB_FAILED;
}
error=sqlite3_step(addPeer);
switch(error){
case SQLITE_DONE:
NOTICE_LO(printf("Added peer %s to db\n",ar->ip));
error=0;
break;
case SQLITE_BUSY:
ERROR(printf("SQLite encountered a timeout while adding a peer\n"));
error=DB_FAILED;
break;
case SQLITE_ERROR:
ERROR(printf("SQLite encountered an error while adding a peer\n"));
error=DB_FAILED;
break;
}
sqlite3_finalize(addPeer);
return error;
}
int prunePeers(sqlite3* db, int expireInterval){
sqlite3_stmt* killPeers;
struct timeval time={0,0};
int error;
if(gettimeofday(&time,NULL)){
ERROR(printf("Could not get current time (prune)\n"));
return E_GENERIC;
}
//prepare pruning
error=sqlite3_prepare_v2(db,QUERY_PRUNE_PEERS,strlen(QUERY_PRUNE_PEERS),&killPeers,NULL);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
return DB_FAILED;
}
error=sqlite3_bind_int(killPeers,1,time.tv_sec-expireInterval);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(killPeers);
return DB_FAILED;
}
error=sqlite3_step(killPeers);
switch(error){
case SQLITE_DONE:
NOTICE_LO(printf("Pruned %d peers from database\n",sqlite3_changes(db)));
error=0;
break;
case SQLITE_BUSY:
ERROR(printf("SQLite encountered a timeout while pruning the peers table\n"));
error=DB_FAILED;
break;
case SQLITE_ERROR:
ERROR(printf("SQLite encountered an error while pruning the peers table\n"));
error=DB_FAILED;
break;
default:
NOTICE_HI(printf("SQLite return code not accounted for (prune)\n"));
break;
}
sqlite3_finalize(killPeers);
return error;
}
int updatePeer(sqlite3* db, int torrentid, ANNOUNCE_REQUEST* ar){
sqlite3_stmt* queryPeer;
sqlite3_stmt* modifyPeer;
sqlite3_stmt* updateTorrent;
int peer_row=0;
struct timeval time={0,0};
int error;
//test if in db
error=sqlite3_prepare_v2(db,QUERY_PEER_BY_ID,strlen(QUERY_PEER_BY_ID),&queryPeer,NULL);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
return DB_FAILED;
}
error=sqlite3_bind_int(queryPeer,1,torrentid);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(queryPeer);
return DB_FAILED;
}
error=sqlite3_bind_text(queryPeer,2,ar->peer_id,strlen(ar->peer_id),SQLITE_TRANSIENT);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(queryPeer);
return DB_FAILED;
}
error=sqlite3_step(queryPeer);
if(error==SQLITE_ROW){
//DEBUG(printf("Using index from row %s\n",sqlite3_column_name(queryPeer,0)));
peer_row=sqlite3_column_int(queryPeer,0);
}
sqlite3_finalize(queryPeer);
if(error==SQLITE_DONE){
//peer not existing, add
DEBUG(printf("Peer not in database, calling add\n"));
error=addPeer(db,ar,torrentid);
if(error==0){//dem returncodes...
DEBUG(printf("Peer added successfully, recursing to update\n"));
return updatePeer(db,torrentid,ar);//yay recursion
}
else{
ERROR(printf("Could not update peer: failed to add\n"));
return DB_FAILED;
}
}
else if(error!=SQLITE_ROW){
ERROR(printf("Database error or timeout while updating peer\n"));
return DB_FAILED;
}
//update timer / status values
//FIXME if event is "stopped", we should probably remove the peer from the db
if(gettimeofday(&time,NULL)){
ERROR(printf("Could not get current time (update)\n"));
return E_GENERIC;
}
//prepare update statement
error=sqlite3_prepare_v2(db,UPDATE_PEER_DATA,strlen(UPDATE_PEER_DATA),&modifyPeer,NULL);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
return DB_FAILED;
}
error=sqlite3_bind_int(modifyPeer,1,ar->left);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(modifyPeer);
return DB_FAILED;
}
error=sqlite3_bind_int(modifyPeer,2,ar->event);//is this "completed" for seeds?
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(modifyPeer);
return DB_FAILED;
}
error=sqlite3_bind_int(modifyPeer,3,time.tv_sec);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(modifyPeer);
return DB_FAILED;
}
error=sqlite3_bind_int(modifyPeer,4,peer_row);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(modifyPeer);
return DB_FAILED;
}
error=sqlite3_step(modifyPeer);
switch(error){
case SQLITE_DONE:
DEBUG(printf("Updated peer with id %d\n",peer_row));
error=0;
break;
case SQLITE_BUSY:
ERROR(printf("SQLite encountered a timeout while updating a peer\n"));
error=DB_FAILED;
break;
case SQLITE_ERROR:
ERROR(printf("SQLite encountered an error while updating a peer\n"));
error=DB_FAILED;
break;
}
sqlite3_finalize(modifyPeer);//FIXME errhandling?
if(error!=0||ar->event!=COMPLETED){
return error;
}
error=sqlite3_prepare_v2(db,INCREASE_COMPLETION_COUNT,strlen(INCREASE_COMPLETION_COUNT),&updateTorrent,NULL);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
return DB_FAILED;
}
error=sqlite3_bind_int(updateTorrent,1,torrentid);
if(error!=SQLITE_OK){
ERROR(printf("Error while preparing statement [%d]\n",__LINE__));
sqlite3_finalize(updateTorrent);
return DB_FAILED;
}
error=sqlite3_step(updateTorrent);
switch(error){
case SQLITE_DONE:
DEBUG(printf("Increased torrent %d completion count\n",torrentid));
error=0;
break;
case SQLITE_BUSY:
ERROR(printf("SQLite encountered a timeout while updating a torrent\n"));
error=DB_FAILED;
break;
case SQLITE_ERROR:
ERROR(printf("SQLite encountered an error while updating a torrent\n"));
error=DB_FAILED;
break;
}
sqlite3_finalize(updateTorrent);//FIXME errhandling?
return error;
}
int createEmptyDatabase(char* filename){
sqlite3* dbHandle;
int error;
char* errmsg=NULL;
NOTICE_LO(printf("Initializing minimal database structure in file %s\n",filename));
error=sqlite3_open_v2(filename,&dbHandle,SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE,NULL);
if(error==SQLITE_OK){
DEBUG(printf("Acquired database handle\n"));
NOTICE_LO(printf("Creating torrent info table... "));
error=sqlite3_exec(dbHandle,CREATE_TORRENT_TABLE,NULL,NULL,&errmsg);
if(error!=SQLITE_OK){
ERROR(printf("\nSQLite reported failure (%s)\n",sqlite3_errmsg(dbHandle)));
if(errmsg){
ERROR(printf("Error message: %s\n",errmsg));
sqlite3_free(errmsg);
}
return DB_ERROR; //FIXME try to close db here before exiting
}
else{
NOTICE_LO(printf("OK\n"));
}
if(errmsg){
ERROR(printf("Error message: %s\n",errmsg));
sqlite3_free(errmsg);
}
NOTICE_LO(printf("Creating peer data table... "));
error=sqlite3_exec(dbHandle,CREATE_PEERS_TABLE,NULL,NULL,&errmsg);
if(error!=SQLITE_OK){
ERROR(printf("\nSQLite reported failure (%s)\n",sqlite3_errmsg(dbHandle)));
if(errmsg){
ERROR(printf("Error message: %s\n",errmsg));
sqlite3_free(errmsg);
}
return DB_ERROR; //FIXME try to close the database before exiting
}
else{
NOTICE_LO(printf("OK\n"));
}
if(errmsg){
ERROR(printf("Error message: %s\n",errmsg));
sqlite3_free(errmsg);
}
}
else{
ERROR(printf("Failed to open database: %s\n",sqlite3_errmsg(dbHandle)));
return DB_ERROR;
}
error=sqlite3_close(dbHandle);
if(error==SQLITE_OK){
DEBUG(printf("Successfully shut down SQLite instance\n"));
}
else{
ERROR(printf("Failed to shut down SQLite instance\n"));
}
return error==SQLITE_OK?0:DB_ERROR;
}