summaryrefslogtreecommitdiff
path: root/trackerlogic.c
diff options
context:
space:
mode:
authorerdgeist <>2006-12-14 02:44:50 +0000
committererdgeist <>2006-12-14 02:44:50 +0000
commit688a4edd9e489de26a9b53ea79f513e6461c7533 (patch)
tree25cb8c040bf69fbb350a602950528f5f15addd61 /trackerlogic.c
parentc0f667defe1efd74b98e283aad635eed5092475e (diff)
Now actually seems to work for the most parts
Added scraping Added graceful disconnect
Diffstat (limited to 'trackerlogic.c')
-rw-r--r--trackerlogic.c111
1 files changed, 79 insertions, 32 deletions
diff --git a/trackerlogic.c b/trackerlogic.c
index 9b8c541..0207fad 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -19,13 +19,14 @@
19// 19//
20int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); } 20int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); }
21int compare_ip_port( const void *peer1, const void *peer2 ) { 21int compare_ip_port( const void *peer1, const void *peer2 ) {
22if( ((ot_peer)peer1)->ip != ((ot_peer)peer2)->ip ) return ((ot_peer)peer1)->ip - ((ot_peer)peer2)->ip; 22if( ((ot_peer*)peer1)->ip != ((ot_peer*)peer2)->ip ) return ((ot_peer*)peer1)->ip - ((ot_peer*)peer2)->ip;
23return ((ot_peer)peer1)->port_flags - ((ot_peer)peer2)->port_flags; } 23return ((ot_peer*)peer1)->port_flags - ((ot_peer*)peer2)->port_flags; }
24 24
25void *binary_search( const void *key, const void *base, 25static void *binary_search( const void *key, const void *base,
26 unsigned long member_count, const unsigned long member_size, 26 unsigned long member_count, const unsigned long member_size,
27 int (*compar) (const void *, const void *), 27 int (*compar) (const void *, const void *),
28 int *exactmatch ) { 28 int *exactmatch )
29{
29 ot_byte *lookat = ((ot_byte*)base) + member_size * (member_count >> 1); 30 ot_byte *lookat = ((ot_byte*)base) + member_size * (member_count >> 1);
30 *exactmatch = 1; 31 *exactmatch = 1;
31 32
@@ -51,9 +52,9 @@ char ths[1+2*20];char*to_hex(ot_byte*s){char*m="0123456789ABCDEF";char*e=ths+40;
51 52
52// GLOBAL VARIABLES 53// GLOBAL VARIABLES
53// 54//
54struct ot_vector all_torrents[256]; 55static ot_vector all_torrents[256];
55 56
56void *vector_find_or_insert( ot_vector vector, void *key, size_t member_size, int(*compare_func)(const void*, const void*), int *exactmatch ) { 57static void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_size, int(*compare_func)(const void*, const void*), int *exactmatch ) {
57 ot_byte *match = BINARY_FIND( key, vector->data, vector->size, member_size, compare_func, exactmatch ); 58 ot_byte *match = BINARY_FIND( key, vector->data, vector->size, member_size, compare_func, exactmatch );
58 59
59 if( *exactmatch ) return match; 60 if( *exactmatch ) return match;
@@ -72,22 +73,22 @@ void *vector_find_or_insert( ot_vector vector, void *key, size_t member_size, in
72 vector->size++; 73 vector->size++;
73 return match; 74 return match;
74} 75}
75 76
76int vector_remove_peer( ot_vector vector, ot_peer peer ) { 77static int vector_remove_peer( ot_vector *vector, ot_peer *peer ) {
77 int exactmatch; 78 int exactmatch;
78 ot_peer match; 79 ot_peer *match;
79 80
80 if( !vector->size ) return 0; 81 if( !vector->size ) return 0;
81 match = BINARY_FIND( peer, vector->data, vector->size, sizeof( struct ot_peer ), compare_ip_port, &exactmatch ); 82 match = BINARY_FIND( peer, vector->data, vector->size, sizeof( ot_peer ), compare_ip_port, &exactmatch );
82 83
83 if( !exactmatch ) return 0; 84 if( !exactmatch ) return 0;
84 exactmatch = match->port_flags & PEER_FLAG_SEEDING ? 2 : 1; 85 exactmatch = match->port_flags & PEER_FLAG_SEEDING ? 2 : 1;
85 MEMMOVE( match, match + 1, ((ot_peer)vector->data) + vector->size - match - 1 ); 86 MEMMOVE( match, match + 1, ((ot_peer*)vector->data) + vector->size - match - 1 );
86 vector->size--; 87 vector->size--;
87 return exactmatch; 88 return exactmatch;
88} 89}
89 90
90void free_peerlist( ot_peerlist peer_list ) { 91static void free_peerlist( ot_peerlist *peer_list ) {
91 int i; 92 int i;
92 for( i=0; i<OT_POOLS_COUNT; ++i ) 93 for( i=0; i<OT_POOLS_COUNT; ++i )
93 if( peer_list->peers[i].data ) 94 if( peer_list->peers[i].data )
@@ -95,10 +96,10 @@ void free_peerlist( ot_peerlist peer_list ) {
95 free( peer_list ); 96 free( peer_list );
96} 97}
97 98
98int vector_remove_torrent( ot_vector vector, ot_hash *hash ) { 99static int vector_remove_torrent( ot_vector *vector, ot_hash *hash ) {
99 int exactmatch; 100 int exactmatch;
100 ot_torrent end = ((ot_torrent)vector->data) + vector->size; 101 ot_torrent *end = ((ot_torrent*)vector->data) + vector->size;
101 ot_torrent match = BINARY_FIND( hash, vector->data, vector->size, sizeof( struct ot_torrent ), compare_hash, &exactmatch ); 102 ot_torrent *match = BINARY_FIND( hash, vector->data, vector->size, sizeof( ot_torrent ), compare_hash, &exactmatch );
102 103
103 if( !exactmatch ) return -1; 104 if( !exactmatch ) return -1;
104 free_peerlist( match->peer_list ); 105 free_peerlist( match->peer_list );
@@ -111,11 +112,12 @@ int vector_remove_torrent( ot_vector vector, ot_hash *hash ) {
111 return 0; 112 return 0;
112} 113}
113 114
114void clean_peerlist( ot_peerlist peer_list ) { 115// Returns 1, if torrent is gone, 0 otherwise
116static int clean_peerlist( ot_peerlist *peer_list ) {
115 long timedout = NOW-peer_list->base; 117 long timedout = NOW-peer_list->base;
116 int i; 118 int i;
117 119
118 if( !timedout ) return; 120 if( !timedout ) return 0;
119 if( timedout > OT_POOLS_COUNT ) timedout = OT_POOLS_COUNT; 121 if( timedout > OT_POOLS_COUNT ) timedout = OT_POOLS_COUNT;
120 122
121 for( i=OT_POOLS_COUNT-timedout; i<OT_POOLS_COUNT; ++i ) 123 for( i=OT_POOLS_COUNT-timedout; i<OT_POOLS_COUNT; ++i )
@@ -128,39 +130,40 @@ void clean_peerlist( ot_peerlist peer_list ) {
128 byte_zero( peer_list->seed_count, sizeof( unsigned long ) * timedout ); 130 byte_zero( peer_list->seed_count, sizeof( unsigned long ) * timedout );
129 131
130 peer_list->base = NOW; 132 peer_list->base = NOW;
133 return timedout == OT_POOLS_COUNT;
131} 134}
132 135
133ot_torrent add_peer_to_torrent( ot_hash *hash, ot_peer peer ) { 136ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ) {
134 int exactmatch; 137 int exactmatch;
135 ot_torrent torrent; 138 ot_torrent *torrent;
136 ot_peer peer_dest; 139 ot_peer *peer_dest;
137 ot_vector torrents_list = all_torrents + *hash[0], peer_pool; 140 ot_vector *torrents_list = &all_torrents[*hash[0]], *peer_pool;
138 141
139 torrent = vector_find_or_insert( torrents_list, (void*)hash, sizeof( struct ot_torrent ), compare_hash, &exactmatch ); 142 torrent = vector_find_or_insert( torrents_list, (void*)hash, sizeof( ot_torrent ), compare_hash, &exactmatch );
140 if( !torrent ) return NULL; 143 if( !torrent ) return NULL;
141 144
142 if( !exactmatch ) { 145 if( !exactmatch ) {
143 // Create a new torrent entry, then 146 // Create a new torrent entry, then
144 torrent->peer_list = malloc( sizeof (struct ot_peerlist) ); 147 torrent->peer_list = malloc( sizeof (ot_peerlist) );
145 if( !torrent->peer_list ) { 148 if( !torrent->peer_list ) {
146 vector_remove_torrent( torrents_list, hash ); 149 vector_remove_torrent( torrents_list, hash );
147 return NULL; 150 return NULL;
148 } 151 }
149 MEMMOVE( &torrent->hash, hash, sizeof( ot_hash ) ); 152 MEMMOVE( &torrent->hash, hash, sizeof( ot_hash ) );
150 153
151 byte_zero( torrent->peer_list, sizeof( struct ot_peerlist )); 154 byte_zero( torrent->peer_list, sizeof( ot_peerlist ));
152 torrent->peer_list->base = NOW; 155 torrent->peer_list->base = NOW;
153 } else 156 } else
154 clean_peerlist( torrent->peer_list ); 157 clean_peerlist( torrent->peer_list );
155 158
156 peer_pool = &torrent->peer_list->peers[0]; 159 peer_pool = &torrent->peer_list->peers[0];
157 peer_dest = vector_find_or_insert( peer_pool, (void*)peer, sizeof( struct ot_peer ), compare_ip_port, &exactmatch ); 160 peer_dest = vector_find_or_insert( peer_pool, (void*)peer, sizeof( ot_peer ), compare_ip_port, &exactmatch );
158 161
159 // If we hadn't had a match in current pool, create peer there and 162 // If we hadn't had a match in current pool, create peer there and
160 // remove it from all older pools 163 // remove it from all older pools
161 if( !exactmatch ) { 164 if( !exactmatch ) {
162 int i; 165 int i;
163 MEMMOVE( peer_dest, peer, sizeof( struct ot_peer ) ); 166 MEMMOVE( peer_dest, peer, sizeof( ot_peer ) );
164 if( peer->port_flags & PEER_FLAG_SEEDING ) 167 if( peer->port_flags & PEER_FLAG_SEEDING )
165 torrent->peer_list->seed_count[0]++; 168 torrent->peer_list->seed_count[0]++;
166 for( i=1; i<OT_POOLS_COUNT; ++i ) { 169 for( i=1; i<OT_POOLS_COUNT; ++i ) {
@@ -176,6 +179,8 @@ ot_torrent add_peer_to_torrent( ot_hash *hash, ot_peer peer ) {
176 if( !(peer_dest->port_flags & PEER_FLAG_SEEDING ) && (peer->port_flags & PEER_FLAG_SEEDING ) ) 179 if( !(peer_dest->port_flags & PEER_FLAG_SEEDING ) && (peer->port_flags & PEER_FLAG_SEEDING ) )
177 torrent->peer_list->seed_count[0]++; 180 torrent->peer_list->seed_count[0]++;
178 } 181 }
182 if( peer->port_flags & PEER_FLAG_COMPLETED )
183 torrent->peer_list->downloaded++;
179 184
180 return torrent; 185 return torrent;
181} 186}
@@ -186,7 +191,7 @@ ot_torrent add_peer_to_torrent( ot_hash *hash, ot_peer peer ) {
186// * RANDOM may return huge values 191// * RANDOM may return huge values
187// * does not yet check not to return self 192// * does not yet check not to return self
188// 193//
189size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply ) { 194size_t return_peers_for_torrent( ot_torrent *torrent, unsigned long amount, char *reply ) {
190 char *r = reply; 195 char *r = reply;
191 unsigned long peer_count, index; 196 unsigned long peer_count, index;
192 signed long pool_offset = -1, pool_index = 0; 197 signed long pool_offset = -1, pool_index = 0;
@@ -214,6 +219,48 @@ size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char
214 return r - reply; 219 return r - reply;
215} 220}
216 221
222// Fetches scrape info for a specific torrent
223size_t return_scrape_for_torrent( ot_hash *hash, char *reply ) {
224 char *r = reply;
225 int exactmatch, peers = 0, seeds = 0, i;
226 ot_vector *torrents_list = &all_torrents[*hash[0]];
227 ot_torrent *torrent = BINARY_FIND( hash, torrents_list->data, torrents_list->size, sizeof( ot_torrent ), compare_hash, &exactmatch );
228
229 if( !exactmatch ) return 0;
230 clean_peerlist( torrent->peer_list );
231
232 for( i=0; i<OT_POOLS_COUNT; ++i ) {
233 peers += torrent->peer_list->peers[i].size;
234 seeds += torrent->peer_list->seed_count[i];
235 }
236
237 MEMMOVE( r, "d5:filesd20:", 12 ); MEMMOVE( r+12, hash, 20 );
238 r += FORMAT_FORMAT_STRING( r+32, "d8:completei%de10:downloadedi%lde10:incompletei%deeee", seeds, torrent->peer_list->downloaded, peers-seeds ) + 32;
239
240 return r - reply;
241}
242
243void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ) {
244 int exactmatch, i;
245 ot_vector *torrents_list = &all_torrents[*hash[0]];
246 ot_torrent *torrent = BINARY_FIND( hash, torrents_list->data, torrents_list->size, sizeof( ot_torrent ), compare_hash, &exactmatch );
247
248 if( !exactmatch ) return;
249
250 for( i=0; i<OT_POOLS_COUNT; ++i )
251 switch( vector_remove_peer( &torrent->peer_list->peers[i], peer ) ) {
252 case 0: continue;
253 case 2: torrent->peer_list->seed_count[i]--;
254 case 1: default: return;
255 }
256
257 clean_peerlist( torrent->peer_list );
258}
259
260void cleanup_torrents( void ) {
261
262}
263
217int init_logic( char *directory ) { 264int init_logic( char *directory ) {
218 glob_t globber; 265 glob_t globber;
219 int i; 266 int i;
@@ -257,7 +304,7 @@ void deinit_logic( ) {
257 // Free all torrents... 304 // Free all torrents...
258 for(i=0; i<256; ++i ) { 305 for(i=0; i<256; ++i ) {
259 if( all_torrents[i].size ) { 306 if( all_torrents[i].size ) {
260 ot_torrent torrents_list = (ot_torrent)all_torrents[i].data; 307 ot_torrent *torrents_list = (ot_torrent*)all_torrents[i].data;
261 for( j=0; j<all_torrents[i].size; ++j ) 308 for( j=0; j<all_torrents[i].size; ++j )
262 free_peerlist( torrents_list[j].peer_list ); 309 free_peerlist( torrents_list[j].peer_list );
263 free( all_torrents[i].data ); 310 free( all_torrents[i].data );