diff options
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | opentracker.c | 142 | ||||
| -rw-r--r-- | scan_urlencoded_query.c | 6 | ||||
| -rw-r--r-- | scan_urlencoded_query.h | 5 | ||||
| -rw-r--r-- | trackerlogic.c | 111 | ||||
| -rw-r--r-- | trackerlogic.h | 48 |
6 files changed, 219 insertions, 97 deletions
| @@ -1,6 +1,6 @@ | |||
| 1 | CC?=gcc | 1 | CC?=gcc |
| 2 | CFLAGS+=-I../libowfat -Wall -pipe -g -ggdb | 2 | CFLAGS+=-I../libowfat -Wall -pipe -O2 |
| 3 | LDFLAGS+=-L../libowfat/ -lowfat | 3 | LDFLAGS+=-L../libowfat/ -lowfat -s |
| 4 | 4 | ||
| 5 | SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c | 5 | SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c |
| 6 | 6 | ||
diff --git a/opentracker.c b/opentracker.c index bd8f5ca..6858f37 100644 --- a/opentracker.c +++ b/opentracker.c | |||
| @@ -106,13 +106,12 @@ const char* http_header(struct http_data* r,const char* h) | |||
| 106 | 106 | ||
| 107 | void httpresponse(struct http_data* h,int64 s) | 107 | void httpresponse(struct http_data* h,int64 s) |
| 108 | { | 108 | { |
| 109 | char *c, *d, *data, *reply = NULL; | 109 | char *c, *d, *data, *reply = NULL; |
| 110 | struct ot_peer peer; | 110 | ot_peer peer; |
| 111 | ot_torrent torrent; | 111 | ot_torrent *torrent; |
| 112 | ot_hash *hash = NULL; | 112 | ot_hash *hash = NULL; |
| 113 | unsigned long numwant; | 113 | int numwant, tmp, scanon; |
| 114 | int compact, scanon; | 114 | size_t reply_size = 0; |
| 115 | size_t reply_size = 0; | ||
| 116 | 115 | ||
| 117 | array_cat0(&h->r); | 116 | array_cat0(&h->r); |
| 118 | 117 | ||
| @@ -137,14 +136,57 @@ e400: | |||
| 137 | case 6: /* scrape ? */ | 136 | case 6: /* scrape ? */ |
| 138 | if (byte_diff(data,6,"scrape")) | 137 | if (byte_diff(data,6,"scrape")) |
| 139 | goto e404; | 138 | goto e404; |
| 139 | scanon = 1; | ||
| 140 | |||
| 141 | while( scanon ) { | ||
| 142 | switch( scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_PARAM ) ) { | ||
| 143 | case -2: /* terminator */ | ||
| 144 | scanon = 0; | ||
| 145 | break; | ||
| 146 | case -1: /* error */ | ||
| 147 | goto e404; | ||
| 148 | case 9: | ||
| 149 | if(byte_diff(data,9,"info_hash")) { | ||
| 150 | scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE ); | ||
| 151 | continue; | ||
| 152 | } | ||
| 153 | /* ignore this, when we have less than 20 bytes */ | ||
| 154 | switch( scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ) ) { | ||
| 155 | case -1: | ||
| 156 | goto e404; | ||
| 157 | case 20: | ||
| 158 | hash = (ot_hash*)data; /* Fall through intended */ | ||
| 159 | default: | ||
| 160 | continue; | ||
| 161 | } | ||
| 162 | default: | ||
| 163 | scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE ); | ||
| 164 | break; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | /* Scanned whole query string, wo */ | ||
| 169 | if( !hash ) { | ||
| 170 | httperror(h,"400 Invalid Request","This server only serves specific scrapes."); | ||
| 171 | goto bailout; | ||
| 172 | } | ||
| 173 | |||
| 174 | // Enough for whole scrape string | ||
| 175 | reply = malloc( 128 ); | ||
| 176 | if( reply ) | ||
| 177 | reply_size = return_scrape_for_torrent( hash, reply ); | ||
| 178 | if( !reply || ( reply_size < 0 ) ) { | ||
| 179 | if( reply ) free( reply ); | ||
| 180 | goto e500; | ||
| 181 | } | ||
| 140 | break; | 182 | break; |
| 141 | case 8: | 183 | case 8: |
| 142 | if( byte_diff(data,8,"announce")) | 184 | if( byte_diff(data,8,"announce")) |
| 143 | goto e404; | 185 | goto e404; |
| 186 | |||
| 144 | peer.ip = h->ip; | 187 | peer.ip = h->ip; |
| 145 | peer.port_flags = 6881 << 16; | 188 | peer.port_flags = 6881 << 16; |
| 146 | numwant = 50; | 189 | numwant = 50; |
| 147 | compact = 1; | ||
| 148 | scanon = 1; | 190 | scanon = 1; |
| 149 | 191 | ||
| 150 | while( scanon ) { | 192 | while( scanon ) { |
| @@ -155,19 +197,44 @@ e400: | |||
| 155 | case -1: /* error */ | 197 | case -1: /* error */ |
| 156 | goto e404; | 198 | goto e404; |
| 157 | case 4: | 199 | case 4: |
| 158 | if(!byte_diff(data,4,"port")) | 200 | if(!byte_diff(data,4,"port")) { |
| 159 | /* scan int */ c; | 201 | size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); |
| 160 | else if(!byte_diff(data,4,"left")) | 202 | if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) || (tmp > 65536) ) goto e404; |
| 161 | /* scan int */ c; | 203 | peer.port_flags = ( tmp << 16 ) | ( peer.port_flags & 0xffff ); |
| 162 | else | 204 | } else if(!byte_diff(data,4,"left")) { |
| 205 | size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); | ||
| 206 | if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) ) goto e404; | ||
| 207 | if( !tmp ) peer.port_flags |= PEER_FLAG_SEEDING; | ||
| 208 | } else | ||
| 163 | scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE ); | 209 | scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE ); |
| 164 | break; | 210 | break; |
| 211 | case 5: | ||
| 212 | if(byte_diff(data,5,"event")) | ||
| 213 | scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE ); | ||
| 214 | else switch( scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ) ) { | ||
| 215 | case -1: | ||
| 216 | goto e404; | ||
| 217 | case 7: | ||
| 218 | if(!byte_diff(data,7,"stopped")) peer.port_flags |= PEER_FLAG_STOPPED; | ||
| 219 | break; | ||
| 220 | case 9: | ||
| 221 | if(!byte_diff(data,9,"complete")) peer.port_flags |= PEER_FLAG_COMPLETED; | ||
| 222 | default: // Fall through intended | ||
| 223 | break; | ||
| 224 | } | ||
| 225 | break; | ||
| 165 | case 7: | 226 | case 7: |
| 166 | if(!byte_diff(data,7,"numwant")) | 227 | if(!byte_diff(data,7,"numwant")) { |
| 167 | /* scan int */ c; | 228 | size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); |
| 168 | else if(!byte_diff(data,7,"compact")) | 229 | if( ( len <= 0 ) || scan_fixed_int( data, len, &numwant ) ) goto e404; |
| 169 | /* scan flag */ c; | 230 | } else if(!byte_diff(data,7,"compact")) { |
| 170 | else | 231 | size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); |
| 232 | if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) ) goto e404; | ||
| 233 | if( !tmp ) { | ||
| 234 | httperror(h,"400 Invalid Request","This server only delivers compact results."); | ||
| 235 | goto bailout; | ||
| 236 | } | ||
| 237 | } else | ||
| 171 | scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE ); | 238 | scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE ); |
| 172 | break; | 239 | break; |
| 173 | case 9: | 240 | case 9: |
| @@ -180,9 +247,8 @@ e400: | |||
| 180 | case -1: | 247 | case -1: |
| 181 | goto e404; | 248 | goto e404; |
| 182 | case 20: | 249 | case 20: |
| 183 | hash = (ot_hash*)data; /* Fall through intended */ | 250 | hash = (ot_hash*)data; |
| 184 | printf("hash: %s\n",*hash); | 251 | default: // Fall through intended |
| 185 | default: | ||
| 186 | continue; | 252 | continue; |
| 187 | } | 253 | } |
| 188 | default: | 254 | default: |
| @@ -192,20 +258,25 @@ e400: | |||
| 192 | } | 258 | } |
| 193 | 259 | ||
| 194 | /* Scanned whole query string */ | 260 | /* Scanned whole query string */ |
| 195 | if( !hash || ( compact == 0 ) ) goto e404; | 261 | if( !hash ) goto e404; |
| 196 | 262 | ||
| 197 | torrent = add_peer_to_torrent( hash, &peer ); | 263 | if( peer.port_flags & PEER_FLAG_STOPPED ) { |
| 198 | if( !torrent ) { | 264 | remove_peer_from_torrent( hash, &peer ); |
| 265 | reply = strdup( "d15:warning message4:Okaye" ); reply_size = 26; | ||
| 266 | } else { | ||
| 267 | torrent = add_peer_to_torrent( hash, &peer ); | ||
| 268 | if( !torrent ) { | ||
| 199 | e500: | 269 | e500: |
| 200 | httperror(h,"500 Internal Server Error","A server error has occured. Please retry later."); | 270 | httperror(h,"500 Internal Server Error","A server error has occured. Please retry later."); |
| 201 | goto bailout; | 271 | goto bailout; |
| 202 | } | 272 | } |
| 203 | reply = malloc( numwant*6+24 ); | 273 | reply = malloc( numwant*6+64 ); // peerlist + seeder, peers and lametta |
| 204 | if( reply ) | 274 | if( reply ) |
| 205 | reply_size = return_peers_for_torrent( torrent, numwant, reply ); | 275 | reply_size = return_peers_for_torrent( torrent, numwant, reply ); |
| 206 | if( !reply || ( reply_size < 0 ) ) { | 276 | if( !reply || ( reply_size < 0 ) ) { |
| 207 | if( reply ) free( reply ); | 277 | if( reply ) free( reply ); |
| 208 | goto e500; | 278 | goto e500; |
| 279 | } | ||
| 209 | } | 280 | } |
| 210 | break; | 281 | break; |
| 211 | default: /* neither scrape nor announce */ | 282 | default: /* neither scrape nor announce */ |
| @@ -213,6 +284,7 @@ e404: | |||
| 213 | httperror(h,"404 Not Found","No such file or directory."); | 284 | httperror(h,"404 Not Found","No such file or directory."); |
| 214 | goto bailout; | 285 | goto bailout; |
| 215 | } | 286 | } |
| 287 | |||
| 216 | c=h->hdrbuf=(char*)malloc(500); | 288 | c=h->hdrbuf=(char*)malloc(500); |
| 217 | c+=fmt_str(c,"HTTP/1.1 200 OK\r\nContent-Type: text/plain"); | 289 | c+=fmt_str(c,"HTTP/1.1 200 OK\r\nContent-Type: text/plain"); |
| 218 | c+=fmt_str(c,"\r\nContent-Length: "); | 290 | c+=fmt_str(c,"\r\nContent-Length: "); |
| @@ -239,7 +311,6 @@ void graceful( int s ) { | |||
| 239 | int main() | 311 | int main() |
| 240 | { | 312 | { |
| 241 | int s=socket_tcp4(); | 313 | int s=socket_tcp4(); |
| 242 | uint32 scope_id; | ||
| 243 | unsigned long ip; | 314 | unsigned long ip; |
| 244 | uint16 port; | 315 | uint16 port; |
| 245 | 316 | ||
| @@ -284,7 +355,6 @@ int main() | |||
| 284 | io_close(n); | 355 | io_close(n); |
| 285 | } else | 356 | } else |
| 286 | io_close(n); | 357 | io_close(n); |
| 287 | buffer_putnlflush(buffer_2); | ||
| 288 | } | 358 | } |
| 289 | if (errno==EAGAIN) | 359 | if (errno==EAGAIN) |
| 290 | io_eagain(s); | 360 | io_eagain(s); |
diff --git a/scan_urlencoded_query.c b/scan_urlencoded_query.c index 3ac01cd..7bd5ee1 100644 --- a/scan_urlencoded_query.c +++ b/scan_urlencoded_query.c | |||
| @@ -49,3 +49,9 @@ size_t scan_urlencoded_query(char **string, char *deststring, int flags) { | |||
| 49 | *string = (char *)s; | 49 | *string = (char *)s; |
| 50 | return d - (unsigned char*)deststring; | 50 | return d - (unsigned char*)deststring; |
| 51 | } | 51 | } |
| 52 | |||
| 53 | size_t scan_fixed_int( char *data, size_t len, int *tmp ) { | ||
| 54 | *tmp = 0; | ||
| 55 | while( (len > 0) && (*data >= '0') && (*data <= '9') ) { --len; *tmp = 10**tmp + *data++-'0'; } | ||
| 56 | return len; | ||
| 57 | } | ||
diff --git a/scan_urlencoded_query.h b/scan_urlencoded_query.h index 1e59745..c87dbee 100644 --- a/scan_urlencoded_query.h +++ b/scan_urlencoded_query.h | |||
| @@ -12,4 +12,9 @@ | |||
| 12 | // or -1 for parse error | 12 | // or -1 for parse error |
| 13 | size_t scan_urlencoded_query(char **string, char *deststring, int flags); | 13 | size_t scan_urlencoded_query(char **string, char *deststring, int flags); |
| 14 | 14 | ||
| 15 | // data pointer to len chars of string | ||
| 16 | // len length of chars in data to parse | ||
| 17 | // number number to receive result | ||
| 18 | size_t scan_fixed_int( char *data, size_t len, int *number ); | ||
| 19 | |||
| 15 | #endif | 20 | #endif |
diff --git a/trackerlogic.c b/trackerlogic.c index 9b8c541..0207fad 100644 --- a/trackerlogic.c +++ b/trackerlogic.c | |||
| @@ -19,13 +19,14 @@ | |||
| 19 | // | 19 | // |
| 20 | int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); } | 20 | int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); } |
| 21 | int compare_ip_port( const void *peer1, const void *peer2 ) { | 21 | int compare_ip_port( const void *peer1, const void *peer2 ) { |
| 22 | if( ((ot_peer)peer1)->ip != ((ot_peer)peer2)->ip ) return ((ot_peer)peer1)->ip - ((ot_peer)peer2)->ip; | 22 | if( ((ot_peer*)peer1)->ip != ((ot_peer*)peer2)->ip ) return ((ot_peer*)peer1)->ip - ((ot_peer*)peer2)->ip; |
| 23 | return ((ot_peer)peer1)->port_flags - ((ot_peer)peer2)->port_flags; } | 23 | return ((ot_peer*)peer1)->port_flags - ((ot_peer*)peer2)->port_flags; } |
| 24 | 24 | ||
| 25 | void *binary_search( const void *key, const void *base, | 25 | static 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 | // |
| 54 | struct ot_vector all_torrents[256]; | 55 | static ot_vector all_torrents[256]; |
| 55 | 56 | ||
| 56 | void *vector_find_or_insert( ot_vector vector, void *key, size_t member_size, int(*compare_func)(const void*, const void*), int *exactmatch ) { | 57 | static 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 | ||
| 76 | int vector_remove_peer( ot_vector vector, ot_peer peer ) { | 77 | static 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 | ||
| 90 | void free_peerlist( ot_peerlist peer_list ) { | 91 | static 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 | ||
| 98 | int vector_remove_torrent( ot_vector vector, ot_hash *hash ) { | 99 | static 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 | ||
| 114 | void clean_peerlist( ot_peerlist peer_list ) { | 115 | // Returns 1, if torrent is gone, 0 otherwise |
| 116 | static 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 | ||
| 133 | ot_torrent add_peer_to_torrent( ot_hash *hash, ot_peer peer ) { | 136 | ot_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 | // |
| 189 | size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply ) { | 194 | size_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 | ||
| 223 | size_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 | |||
| 243 | void 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 | |||
| 260 | void cleanup_torrents( void ) { | ||
| 261 | |||
| 262 | } | ||
| 263 | |||
| 217 | int init_logic( char *directory ) { | 264 | int 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 ); |
diff --git a/trackerlogic.h b/trackerlogic.h index 3243eb7..44bd744 100644 --- a/trackerlogic.h +++ b/trackerlogic.h | |||
| @@ -32,40 +32,31 @@ typedef time_t ot_time; | |||
| 32 | #define OT_POOLS_TIMEOUT 300 | 32 | #define OT_POOLS_TIMEOUT 300 |
| 33 | #define NOW (time(NULL)/OT_POOLS_TIMEOUT) | 33 | #define NOW (time(NULL)/OT_POOLS_TIMEOUT) |
| 34 | 34 | ||
| 35 | typedef struct ot_vector { | 35 | typedef struct { |
| 36 | void *data; | 36 | void *data; |
| 37 | size_t size; | 37 | size_t size; |
| 38 | size_t space; | 38 | size_t space; |
| 39 | } *ot_vector; | 39 | } ot_vector; |
| 40 | 40 | ||
| 41 | typedef struct ot_peer { | 41 | typedef struct { |
| 42 | ot_ip ip; | 42 | ot_ip ip; |
| 43 | ot_dword port_flags; | 43 | ot_dword port_flags; |
| 44 | } *ot_peer; | 44 | } ot_peer; |
| 45 | static const ot_byte PEER_FLAG_SEEDING = 0x80; | 45 | static const ot_byte PEER_FLAG_SEEDING = 0x80; |
| 46 | static const ot_byte PEER_FLAG_COMPLETED = 0x40; | ||
| 47 | static const ot_byte PEER_FLAG_STOPPED = 0x20; | ||
| 46 | 48 | ||
| 47 | typedef struct ot_peerlist { | 49 | typedef struct { |
| 48 | ot_time base; | 50 | ot_time base; |
| 49 | unsigned long seed_count[ OT_POOLS_COUNT ]; | 51 | unsigned long seed_count[ OT_POOLS_COUNT ]; |
| 50 | struct ot_vector peers[ OT_POOLS_COUNT ]; | 52 | unsigned long downloaded; |
| 51 | } *ot_peerlist; | 53 | ot_vector peers[ OT_POOLS_COUNT ]; |
| 54 | } ot_peerlist; | ||
| 52 | 55 | ||
| 53 | typedef struct ot_torrent { | 56 | typedef struct { |
| 54 | ot_hash hash; | 57 | ot_hash hash; |
| 55 | ot_peerlist peer_list; | 58 | ot_peerlist *peer_list; |
| 56 | } *ot_torrent; | 59 | } ot_torrent; |
| 57 | |||
| 58 | void *map_file( char *file_name, size_t map_size ); | ||
| 59 | void unmap_file( char *file_name, void *map, size_t mapped_size, unsigned long real_size ); | ||
| 60 | |||
| 61 | // This behaves quite like bsearch but allows to find | ||
| 62 | // the insertion point for inserts after unsuccessful searches | ||
| 63 | // in this case exactmatch is 0 on exit | ||
| 64 | // | ||
| 65 | void *binary_search( const void *key, const void *base, | ||
| 66 | const unsigned long member_count, const unsigned long member_size, | ||
| 67 | int (*compar) (const void *, const void *), | ||
| 68 | int *exactmatch ); | ||
| 69 | 60 | ||
| 70 | // | 61 | // |
| 71 | // Exported functions | 62 | // Exported functions |
| @@ -74,7 +65,10 @@ void *binary_search( const void *key, const void *base, | |||
| 74 | int init_logic( char *chdir_directory ); | 65 | int init_logic( char *chdir_directory ); |
| 75 | void deinit_logic( ); | 66 | void deinit_logic( ); |
| 76 | 67 | ||
| 77 | ot_torrent add_peer_to_torrent( ot_hash *hash, ot_peer peer ); | 68 | ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ); |
| 78 | size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply ); | 69 | size_t return_peers_for_torrent( ot_torrent *torrent, unsigned long amount, char *reply ); |
| 70 | size_t return_scrape_for_torrent( ot_hash *hash, char *reply ); | ||
| 71 | void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ); | ||
| 72 | void cleanup_torrents( void ); | ||
| 79 | 73 | ||
| 80 | #endif | 74 | #endif |
