diff options
author | erdgeist <> | 2006-12-13 14:44:14 +0000 |
---|---|---|
committer | erdgeist <> | 2006-12-13 14:44:14 +0000 |
commit | c0f667defe1efd74b98e283aad635eed5092475e (patch) | |
tree | e8fa4249b69703571b8a2b2ad9c9e5f22d22f741 | |
parent | d7c26dc71bd3cc04824ad979d31c1386dd5b1fa3 (diff) |
Reacts more appropriate, however EXC_BAD_ACCESS triggered
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | opentracker.c | 30 | ||||
-rw-r--r-- | trackerlogic.c | 26 |
3 files changed, 30 insertions, 30 deletions
@@ -1,6 +1,6 @@ | |||
1 | CC?=gcc | 1 | CC?=gcc |
2 | CFLAGS+=-I../libowfat -Wall -pipe -O2 | 2 | CFLAGS+=-I../libowfat -Wall -pipe -g -ggdb |
3 | LDFLAGS+=-L../libowfat/ -lowfat -s | 3 | LDFLAGS+=-L../libowfat/ -lowfat |
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 9c76f7f..bd8f5ca 100644 --- a/opentracker.c +++ b/opentracker.c | |||
@@ -36,7 +36,7 @@ struct http_data { | |||
36 | io_batch iob; | 36 | io_batch iob; |
37 | char* hdrbuf; | 37 | char* hdrbuf; |
38 | int hlen; | 38 | int hlen; |
39 | char ip[16]; | 39 | unsigned long ip; |
40 | }; | 40 | }; |
41 | 41 | ||
42 | int header_complete(struct http_data* r) | 42 | int header_complete(struct http_data* r) |
@@ -141,7 +141,7 @@ e400: | |||
141 | case 8: | 141 | case 8: |
142 | if( byte_diff(data,8,"announce")) | 142 | if( byte_diff(data,8,"announce")) |
143 | goto e404; | 143 | goto e404; |
144 | byte_copy( &peer.ip, 4, h->ip ); | 144 | peer.ip = h->ip; |
145 | peer.port_flags = 6881 << 16; | 145 | peer.port_flags = 6881 << 16; |
146 | numwant = 50; | 146 | numwant = 50; |
147 | compact = 1; | 147 | compact = 1; |
@@ -193,14 +193,14 @@ e400: | |||
193 | 193 | ||
194 | /* Scanned whole query string */ | 194 | /* Scanned whole query string */ |
195 | if( !hash || ( compact == 0 ) ) goto e404; | 195 | if( !hash || ( compact == 0 ) ) goto e404; |
196 | printf("ALLFINE\n"); | 196 | |
197 | torrent = add_peer_to_torrent( hash, &peer ); | 197 | torrent = add_peer_to_torrent( hash, &peer ); |
198 | if( !torrent ) { | 198 | if( !torrent ) { |
199 | e500: | 199 | e500: |
200 | httperror(h,"500 Internal Server Error","A server error has occured. Please retry later."); | 200 | httperror(h,"500 Internal Server Error","A server error has occured. Please retry later."); |
201 | goto bailout; | 201 | goto bailout; |
202 | } | 202 | } |
203 | reply = malloc( numwant*6+10 ); | 203 | reply = malloc( numwant*6+24 ); |
204 | if( reply ) | 204 | if( reply ) |
205 | reply_size = return_peers_for_torrent( torrent, numwant, reply ); | 205 | reply_size = return_peers_for_torrent( torrent, numwant, reply ); |
206 | if( !reply || ( reply_size < 0 ) ) { | 206 | if( !reply || ( reply_size < 0 ) ) { |
@@ -214,13 +214,11 @@ e404: | |||
214 | goto bailout; | 214 | goto bailout; |
215 | } | 215 | } |
216 | c=h->hdrbuf=(char*)malloc(500); | 216 | c=h->hdrbuf=(char*)malloc(500); |
217 | c+=fmt_str(c,"HTTP/1.1 Coming Up\r\nContent-Type: text/plain"); | 217 | c+=fmt_str(c,"HTTP/1.1 200 OK\r\nContent-Type: text/plain"); |
218 | c+=fmt_str(c,"\r\nContent-Length: "); | 218 | c+=fmt_str(c,"\r\nContent-Length: "); |
219 | /* ANSWER SIZE*/ | 219 | c+=fmt_ulonglong(c, reply_size ); |
220 | c+=fmt_ulonglong(c, 100 ); | ||
221 | c+=fmt_str(c,"\r\nLast-Modified: "); | 220 | c+=fmt_str(c,"\r\nLast-Modified: "); |
222 | /* MODIFY DATE | 221 | c+=fmt_httpdate(c,time(0)); |
223 | c+=fmt_httpdate(c,s.st_mtime); */ | ||
224 | c+=fmt_str(c,"\r\nConnection: close\r\n\r\n"); | 222 | c+=fmt_str(c,"\r\nConnection: close\r\n\r\n"); |
225 | iob_addbuf(&h->iob,h->hdrbuf,c - h->hdrbuf); | 223 | iob_addbuf(&h->iob,h->hdrbuf,c - h->hdrbuf); |
226 | if( reply && reply_size ) iob_addbuf(&h->iob,reply, reply_size ); | 224 | if( reply && reply_size ) iob_addbuf(&h->iob,reply, reply_size ); |
@@ -240,13 +238,13 @@ void graceful( int s ) { | |||
240 | 238 | ||
241 | int main() | 239 | int main() |
242 | { | 240 | { |
243 | int s=socket_tcp6(); | 241 | int s=socket_tcp4(); |
244 | uint32 scope_id; | 242 | uint32 scope_id; |
245 | char ip[16]; | 243 | unsigned long ip; |
246 | uint16 port; | 244 | uint16 port; |
247 | 245 | ||
248 | if (socket_bind6_reuse(s,V6any,6969,0)==-1) | 246 | if (socket_bind4_reuse(s,NULL,6969)==-1) |
249 | panic("socket_bind6_reuse"); | 247 | panic("socket_bind4_reuse"); |
250 | 248 | ||
251 | if (socket_listen(s,16)==-1) | 249 | if (socket_listen(s,16)==-1) |
252 | panic("socket_listen"); | 250 | panic("socket_listen"); |
@@ -270,7 +268,7 @@ int main() | |||
270 | if (i==s) // ist es der serversocket? | 268 | if (i==s) // ist es der serversocket? |
271 | { | 269 | { |
272 | int n; | 270 | int n; |
273 | while ((n=socket_accept6(s,ip,&port,&scope_id))!=-1) | 271 | while ((n=socket_accept4(s,(void*)&ip,&port))!=-1) |
274 | { | 272 | { |
275 | if (io_fd(n)) | 273 | if (io_fd(n)) |
276 | { | 274 | { |
@@ -280,7 +278,7 @@ int main() | |||
280 | if (h) | 278 | if (h) |
281 | { | 279 | { |
282 | byte_zero(h,sizeof(struct http_data)); | 280 | byte_zero(h,sizeof(struct http_data)); |
283 | byte_copy(h->ip,sizeof(ip),ip); | 281 | h->ip=ip; |
284 | io_setcookie(n,h); | 282 | io_setcookie(n,h); |
285 | } else | 283 | } else |
286 | io_close(n); | 284 | io_close(n); |
@@ -291,7 +289,7 @@ int main() | |||
291 | if (errno==EAGAIN) | 289 | if (errno==EAGAIN) |
292 | io_eagain(s); | 290 | io_eagain(s); |
293 | else | 291 | else |
294 | carp("socket_accept6"); | 292 | carp("socket_accept4"); |
295 | } | 293 | } |
296 | else | 294 | else |
297 | { | 295 | { |
diff --git a/trackerlogic.c b/trackerlogic.c index 5d83abb..9b8c541 100644 --- a/trackerlogic.c +++ b/trackerlogic.c | |||
@@ -54,25 +54,27 @@ char ths[1+2*20];char*to_hex(ot_byte*s){char*m="0123456789ABCDEF";char*e=ths+40; | |||
54 | struct ot_vector all_torrents[256]; | 54 | struct ot_vector all_torrents[256]; |
55 | 55 | ||
56 | void *vector_find_or_insert( ot_vector vector, void *key, size_t member_size, int(*compare_func)(const void*, const void*), int *exactmatch ) { | 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 | ot_byte *end = ((ot_byte*)vector->data) + member_size * vector->size; | ||
58 | ot_byte *match = BINARY_FIND( key, vector->data, vector->size, member_size, compare_func, exactmatch ); | 57 | ot_byte *match = BINARY_FIND( key, vector->data, vector->size, member_size, compare_func, exactmatch ); |
59 | 58 | ||
60 | if( exactmatch ) return match; | 59 | if( *exactmatch ) return match; |
61 | 60 | ||
62 | if( vector->size + 1 >= vector->space ) { | 61 | if( vector->size + 1 >= vector->space ) { |
63 | void *new_data = realloc( vector->data, vector->space ? 2 * vector->space : 1024 ); | 62 | ot_byte *new_data = realloc( vector->data, vector->space ? 2 * vector->space : 1024 ); |
64 | if( !new_data ) return NULL; | 63 | if( !new_data ) return NULL; |
64 | |||
65 | // Adjust pointer if it moved by realloc | ||
66 | match = match - (ot_byte*)vector->data + new_data; | ||
67 | |||
65 | vector->data = new_data; | 68 | vector->data = new_data; |
66 | vector->space = vector->space ? vector->space * 2 : 1024; | 69 | vector->space = vector->space ? vector->space * 2 : 1024; |
67 | } | 70 | } |
68 | MEMMOVE( match + member_size, match, end - match ); | 71 | MEMMOVE( match + member_size, match, ((ot_byte*)vector->data) + member_size * vector->size - match ); |
69 | vector->size++; | 72 | vector->size++; |
70 | return match; | 73 | return match; |
71 | } | 74 | } |
72 | 75 | ||
73 | int vector_remove_peer( ot_vector vector, ot_peer peer ) { | 76 | int vector_remove_peer( ot_vector vector, ot_peer peer ) { |
74 | int exactmatch; | 77 | int exactmatch; |
75 | ot_peer end = ((ot_peer)vector->data) + vector->size; | ||
76 | ot_peer match; | 78 | ot_peer match; |
77 | 79 | ||
78 | if( !vector->size ) return 0; | 80 | if( !vector->size ) return 0; |
@@ -80,7 +82,7 @@ int vector_remove_peer( ot_vector vector, ot_peer peer ) { | |||
80 | 82 | ||
81 | if( !exactmatch ) return 0; | 83 | if( !exactmatch ) return 0; |
82 | exactmatch = match->port_flags & PEER_FLAG_SEEDING ? 2 : 1; | 84 | exactmatch = match->port_flags & PEER_FLAG_SEEDING ? 2 : 1; |
83 | MEMMOVE( match, match + 1, end - match - 1 ); | 85 | MEMMOVE( match, match + 1, ((ot_peer)vector->data) + vector->size - match - 1 ); |
84 | vector->size--; | 86 | vector->size--; |
85 | return exactmatch; | 87 | return exactmatch; |
86 | } | 88 | } |
@@ -187,7 +189,7 @@ ot_torrent add_peer_to_torrent( ot_hash *hash, ot_peer peer ) { | |||
187 | size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply ) { | 189 | size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply ) { |
188 | char *r = reply; | 190 | char *r = reply; |
189 | unsigned long peer_count, index; | 191 | unsigned long peer_count, index; |
190 | unsigned long pool_offset = 0, pool_index = 0; | 192 | signed long pool_offset = -1, pool_index = 0; |
191 | signed long wert = -1; | 193 | signed long wert = -1; |
192 | 194 | ||
193 | for( peer_count=index=0; index<OT_POOLS_COUNT; ++index) peer_count += torrent->peer_list->peers[index].size; | 195 | for( peer_count=index=0; index<OT_POOLS_COUNT; ++index) peer_count += torrent->peer_list->peers[index].size; |
@@ -198,14 +200,14 @@ size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char | |||
198 | double step = 1.8*((double)( peer_count - wert - 1 ))/((double)( amount - index )); | 200 | double step = 1.8*((double)( peer_count - wert - 1 ))/((double)( amount - index )); |
199 | int off = random() % (int)floor( step ); | 201 | int off = random() % (int)floor( step ); |
200 | off = 1 + ( off % ( peer_count - wert - 1 )); | 202 | off = 1 + ( off % ( peer_count - wert - 1 )); |
201 | wert += off; | 203 | wert += off; pool_offset += off; |
202 | 204 | ||
203 | while( pool_offset + off > torrent->peer_list->peers[pool_index].size ) { | 205 | while( pool_offset >= torrent->peer_list->peers[pool_index].size ) { |
204 | off -= torrent->peer_list->peers[pool_index].size - pool_offset; | 206 | pool_offset -= torrent->peer_list->peers[pool_index].size; |
205 | pool_offset = 0; pool_index++; | 207 | pool_index++; |
206 | } | 208 | } |
207 | 209 | ||
208 | MEMMOVE( r, ((ot_peer*)torrent->peer_list->peers[pool_index].data)[pool_offset], 6 ); | 210 | MEMMOVE( r, ((ot_peer*)torrent->peer_list->peers[pool_index].data) + pool_offset, 6 ); |
209 | r += 6; | 211 | r += 6; |
210 | } | 212 | } |
211 | *r++ = 'e'; | 213 | *r++ = 'e'; |