Reacts more appropriate, however EXC_BAD_ACCESS triggered

dynamic-accesslists
erdgeist 18 years ago
parent d7c26dc71b
commit c0f667defe

@ -1,6 +1,6 @@
CC?=gcc CC?=gcc
CFLAGS+=-I../libowfat -Wall -pipe -O2 CFLAGS+=-I../libowfat -Wall -pipe -g -ggdb
LDFLAGS+=-L../libowfat/ -lowfat -s LDFLAGS+=-L../libowfat/ -lowfat
SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c

@ -36,7 +36,7 @@ struct http_data {
io_batch iob; io_batch iob;
char* hdrbuf; char* hdrbuf;
int hlen; int hlen;
char ip[16]; unsigned long ip;
}; };
int header_complete(struct http_data* r) int header_complete(struct http_data* r)
@ -141,7 +141,7 @@ e400:
case 8: case 8:
if( byte_diff(data,8,"announce")) if( byte_diff(data,8,"announce"))
goto e404; goto e404;
byte_copy( &peer.ip, 4, h->ip ); peer.ip = h->ip;
peer.port_flags = 6881 << 16; peer.port_flags = 6881 << 16;
numwant = 50; numwant = 50;
compact = 1; compact = 1;
@ -193,14 +193,14 @@ e400:
/* Scanned whole query string */ /* Scanned whole query string */
if( !hash || ( compact == 0 ) ) goto e404; if( !hash || ( compact == 0 ) ) goto e404;
printf("ALLFINE\n");
torrent = add_peer_to_torrent( hash, &peer ); torrent = add_peer_to_torrent( hash, &peer );
if( !torrent ) { if( !torrent ) {
e500: e500:
httperror(h,"500 Internal Server Error","A server error has occured. Please retry later."); httperror(h,"500 Internal Server Error","A server error has occured. Please retry later.");
goto bailout; goto bailout;
} }
reply = malloc( numwant*6+10 ); reply = malloc( numwant*6+24 );
if( reply ) if( reply )
reply_size = return_peers_for_torrent( torrent, numwant, reply ); reply_size = return_peers_for_torrent( torrent, numwant, reply );
if( !reply || ( reply_size < 0 ) ) { if( !reply || ( reply_size < 0 ) ) {
@ -214,13 +214,11 @@ e404:
goto bailout; goto bailout;
} }
c=h->hdrbuf=(char*)malloc(500); c=h->hdrbuf=(char*)malloc(500);
c+=fmt_str(c,"HTTP/1.1 Coming Up\r\nContent-Type: text/plain"); c+=fmt_str(c,"HTTP/1.1 200 OK\r\nContent-Type: text/plain");
c+=fmt_str(c,"\r\nContent-Length: "); c+=fmt_str(c,"\r\nContent-Length: ");
/* ANSWER SIZE*/ c+=fmt_ulonglong(c, reply_size );
c+=fmt_ulonglong(c, 100 );
c+=fmt_str(c,"\r\nLast-Modified: "); c+=fmt_str(c,"\r\nLast-Modified: ");
/* MODIFY DATE c+=fmt_httpdate(c,time(0));
c+=fmt_httpdate(c,s.st_mtime); */
c+=fmt_str(c,"\r\nConnection: close\r\n\r\n"); c+=fmt_str(c,"\r\nConnection: close\r\n\r\n");
iob_addbuf(&h->iob,h->hdrbuf,c - h->hdrbuf); iob_addbuf(&h->iob,h->hdrbuf,c - h->hdrbuf);
if( reply && reply_size ) iob_addbuf(&h->iob,reply, reply_size ); if( reply && reply_size ) iob_addbuf(&h->iob,reply, reply_size );
@ -240,13 +238,13 @@ void graceful( int s ) {
int main() int main()
{ {
int s=socket_tcp6(); int s=socket_tcp4();
uint32 scope_id; uint32 scope_id;
char ip[16]; unsigned long ip;
uint16 port; uint16 port;
if (socket_bind6_reuse(s,V6any,6969,0)==-1) if (socket_bind4_reuse(s,NULL,6969)==-1)
panic("socket_bind6_reuse"); panic("socket_bind4_reuse");
if (socket_listen(s,16)==-1) if (socket_listen(s,16)==-1)
panic("socket_listen"); panic("socket_listen");
@ -270,7 +268,7 @@ int main()
if (i==s) // ist es der serversocket? if (i==s) // ist es der serversocket?
{ {
int n; int n;
while ((n=socket_accept6(s,ip,&port,&scope_id))!=-1) while ((n=socket_accept4(s,(void*)&ip,&port))!=-1)
{ {
if (io_fd(n)) if (io_fd(n))
{ {
@ -280,7 +278,7 @@ int main()
if (h) if (h)
{ {
byte_zero(h,sizeof(struct http_data)); byte_zero(h,sizeof(struct http_data));
byte_copy(h->ip,sizeof(ip),ip); h->ip=ip;
io_setcookie(n,h); io_setcookie(n,h);
} else } else
io_close(n); io_close(n);
@ -291,7 +289,7 @@ int main()
if (errno==EAGAIN) if (errno==EAGAIN)
io_eagain(s); io_eagain(s);
else else
carp("socket_accept6"); carp("socket_accept4");
} }
else else
{ {

@ -54,25 +54,27 @@ char ths[1+2*20];char*to_hex(ot_byte*s){char*m="0123456789ABCDEF";char*e=ths+40;
struct ot_vector all_torrents[256]; struct ot_vector all_torrents[256];
void *vector_find_or_insert( ot_vector vector, void *key, size_t member_size, int(*compare_func)(const void*, const void*), int *exactmatch ) { void *vector_find_or_insert( ot_vector vector, void *key, size_t member_size, int(*compare_func)(const void*, const void*), int *exactmatch ) {
ot_byte *end = ((ot_byte*)vector->data) + member_size * vector->size;
ot_byte *match = BINARY_FIND( key, vector->data, vector->size, member_size, compare_func, exactmatch ); ot_byte *match = BINARY_FIND( key, vector->data, vector->size, member_size, compare_func, exactmatch );
if( exactmatch ) return match; if( *exactmatch ) return match;
if( vector->size + 1 >= vector->space ) { if( vector->size + 1 >= vector->space ) {
void *new_data = realloc( vector->data, vector->space ? 2 * vector->space : 1024 ); ot_byte *new_data = realloc( vector->data, vector->space ? 2 * vector->space : 1024 );
if( !new_data ) return NULL; if( !new_data ) return NULL;
// Adjust pointer if it moved by realloc
match = match - (ot_byte*)vector->data + new_data;
vector->data = new_data; vector->data = new_data;
vector->space = vector->space ? vector->space * 2 : 1024; vector->space = vector->space ? vector->space * 2 : 1024;
} }
MEMMOVE( match + member_size, match, end - match ); MEMMOVE( match + member_size, match, ((ot_byte*)vector->data) + member_size * vector->size - match );
vector->size++; vector->size++;
return match; return match;
} }
int vector_remove_peer( ot_vector vector, ot_peer peer ) { int vector_remove_peer( ot_vector vector, ot_peer peer ) {
int exactmatch; int exactmatch;
ot_peer end = ((ot_peer)vector->data) + vector->size;
ot_peer match; ot_peer match;
if( !vector->size ) return 0; if( !vector->size ) return 0;
@ -80,7 +82,7 @@ int vector_remove_peer( ot_vector vector, ot_peer peer ) {
if( !exactmatch ) return 0; if( !exactmatch ) return 0;
exactmatch = match->port_flags & PEER_FLAG_SEEDING ? 2 : 1; exactmatch = match->port_flags & PEER_FLAG_SEEDING ? 2 : 1;
MEMMOVE( match, match + 1, end - match - 1 ); MEMMOVE( match, match + 1, ((ot_peer)vector->data) + vector->size - match - 1 );
vector->size--; vector->size--;
return exactmatch; return exactmatch;
} }
@ -187,7 +189,7 @@ ot_torrent add_peer_to_torrent( ot_hash *hash, ot_peer peer ) {
size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply ) { size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply ) {
char *r = reply; char *r = reply;
unsigned long peer_count, index; unsigned long peer_count, index;
unsigned long pool_offset = 0, pool_index = 0; signed long pool_offset = -1, pool_index = 0;
signed long wert = -1; signed long wert = -1;
for( peer_count=index=0; index<OT_POOLS_COUNT; ++index) peer_count += torrent->peer_list->peers[index].size; 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
double step = 1.8*((double)( peer_count - wert - 1 ))/((double)( amount - index )); double step = 1.8*((double)( peer_count - wert - 1 ))/((double)( amount - index ));
int off = random() % (int)floor( step ); int off = random() % (int)floor( step );
off = 1 + ( off % ( peer_count - wert - 1 )); off = 1 + ( off % ( peer_count - wert - 1 ));
wert += off; wert += off; pool_offset += off;
while( pool_offset + off > torrent->peer_list->peers[pool_index].size ) { while( pool_offset >= torrent->peer_list->peers[pool_index].size ) {
off -= torrent->peer_list->peers[pool_index].size - pool_offset; pool_offset -= torrent->peer_list->peers[pool_index].size;
pool_offset = 0; pool_index++; pool_index++;
} }
MEMMOVE( r, ((ot_peer*)torrent->peer_list->peers[pool_index].data)[pool_offset], 6 ); MEMMOVE( r, ((ot_peer*)torrent->peer_list->peers[pool_index].data) + pool_offset, 6 );
r += 6; r += 6;
} }
*r++ = 'e'; *r++ = 'e';

Loading…
Cancel
Save