-
Notifications
You must be signed in to change notification settings - Fork 0
/
trackerhash.c
100 lines (92 loc) · 2.55 KB
/
trackerhash.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "trackerhash.h"
#include "torrent.h"
#include "common.h"
tracker_node *tracker_table[TRACKER_TABLE_SIZE];
void tracker_init_table() {
memset(tracker_table, 0, TRACKER_TABLE_SIZE*(sizeof(*tracker_table)));
}
int tracker_hash(unsigned char *h) {
return (int) *h;
}
int tracker_add_hash(char *host, unsigned char *fh) {
tracker_node *n = tracker_table[tracker_hash(fh)];
for(; n != NULL; n=n->next) {
if(memcmp((void*)fh, (void*)n->file_hash, SHA1_SIZE) == 0) {
break; // the file has already been hashed.
}
}
if (n == NULL) { // not in table
n = malloc(sizeof(tracker_node));
n->next = tracker_table[tracker_hash(fh)];
n->p = NULL;
memcpy((void*)n->file_hash, (void*)fh, SHA1_SIZE);
tracker_table[tracker_hash(fh)] = n;
}
peer *p = n->p;
for(; p != NULL; p=p->next) {
if(strcmp(p->host, host) == 0) {
break;
}
}
if (p == NULL) { // new peer
p = malloc(sizeof(peer));
p->npeers = (n->p) ? n->p->npeers + 1 : 1;
p->next = n->p;
strcpy(p->host, host);
n->p = p;
}
return 0;
}
peer *tracker_find_hash(unsigned char *fh) {
tracker_node *n = tracker_table[tracker_hash(fh)];
for(; n != NULL; n=n->next) {
if(memcmp((void*)fh, (void*)n->file_hash, SHA1_SIZE) == 0) {
break; // the file has already been hashed.
}
}
if (n == NULL) {
printf("file not found!\n");
return NULL;
}
return n->p;
}
void tracker_dump_table() {
int i;
tracker_node *n;
peer *p;
for(i=0; i<TRACKER_TABLE_SIZE; i++) {
if(tracker_table[i] == NULL) {
continue;
}
for(n=tracker_table[i]; n != NULL; n = n->next) {
printf("%02x: ", i); print_hash(n->file_hash);
printf("%d peer(s):\n", ((n->p) ? n->p->npeers : 0));
for(p=n->p; p != NULL; p=p->next) {
printf("\t%s\n", p->host);
}
}
}
}
void tracker_free_table() {
int i;
tracker_node *n, *n_next;
peer *p, *p_next;
for(i=0; i<TRACKER_TABLE_SIZE; i++) {
if(tracker_table[i] == NULL) {
continue;
}
for(n=tracker_table[i]; n != NULL; n=n_next) {
n_next=n->next;
for(p=n->p; p != NULL; p=p_next) {
p_next=p->next;
free(p);
}
free(n);
}
tracker_table[i]=NULL;
}
}