From c0f667defe1efd74b98e283aad635eed5092475e Mon Sep 17 00:00:00 2001
From: erdgeist <>
Date: Wed, 13 Dec 2006 14:44:14 +0000
Subject: Reacts more appropriate, however EXC_BAD_ACCESS triggered

---
 Makefile       |  4 ++--
 opentracker.c  | 30 ++++++++++++++----------------
 trackerlogic.c | 26 ++++++++++++++------------
 3 files changed, 30 insertions(+), 30 deletions(-)

diff --git a/Makefile b/Makefile
index 21689e5..a85ab2b 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 CC?=gcc
-CFLAGS+=-I../libowfat -Wall -pipe -O2
-LDFLAGS+=-L../libowfat/ -lowfat -s
+CFLAGS+=-I../libowfat -Wall -pipe -g -ggdb
+LDFLAGS+=-L../libowfat/ -lowfat 
 
 SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c
 
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 {
   io_batch iob;
   char* hdrbuf;
   int hlen;
-  char ip[16];
+  unsigned long ip;
 };
 
 int header_complete(struct http_data* r)
@@ -141,7 +141,7 @@ e400:
     case 8: 
       if( byte_diff(data,8,"announce"))
         goto e404;
-      byte_copy( &peer.ip, 4, h->ip );
+      peer.ip = h->ip;
       peer.port_flags = 6881 << 16;
       numwant = 50;
       compact = 1;
@@ -193,14 +193,14 @@ e400:
 
       /* Scanned whole query string */
       if( !hash || ( compact == 0 ) ) goto e404;
-      printf("ALLFINE\n");
+
       torrent = add_peer_to_torrent( hash, &peer );
       if( !torrent ) {
 e500:
         httperror(h,"500 Internal Server Error","A server error has occured. Please retry later.");
         goto bailout;
       }
-      reply = malloc( numwant*6+10 );
+      reply = malloc( numwant*6+24 );
       if( reply )
         reply_size = return_peers_for_torrent( torrent, numwant, reply );
       if( !reply || ( reply_size < 0 ) ) {
@@ -214,13 +214,11 @@ e404:
       goto bailout;
     }
     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: ");
-    /* ANSWER SIZE*/
-    c+=fmt_ulonglong(c, 100 );
+    c+=fmt_ulonglong(c, reply_size );
     c+=fmt_str(c,"\r\nLast-Modified: ");
-    /* MODIFY DATE
-    c+=fmt_httpdate(c,s.st_mtime); */
+    c+=fmt_httpdate(c,time(0));
     c+=fmt_str(c,"\r\nConnection: close\r\n\r\n");
     iob_addbuf(&h->iob,h->hdrbuf,c - h->hdrbuf);
     if( reply && reply_size ) iob_addbuf(&h->iob,reply, reply_size );
@@ -240,13 +238,13 @@ void graceful( int s ) {
 
 int main()
 {
-    int s=socket_tcp6();
+    int s=socket_tcp4();
     uint32 scope_id;
-    char ip[16];
+    unsigned long ip;
     uint16 port;
 
-    if (socket_bind6_reuse(s,V6any,6969,0)==-1)
-        panic("socket_bind6_reuse");
+    if (socket_bind4_reuse(s,NULL,6969)==-1)
+        panic("socket_bind4_reuse");
 
     if (socket_listen(s,16)==-1)
         panic("socket_listen");
@@ -270,7 +268,7 @@ int main()
             if (i==s)    // ist es der serversocket?
             {
                 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))
                     {
@@ -280,7 +278,7 @@ int main()
                         if (h)
                         {
                             byte_zero(h,sizeof(struct http_data));
-                            byte_copy(h->ip,sizeof(ip),ip);
+                            h->ip=ip;
                             io_setcookie(n,h);
                         } else
                             io_close(n);
@@ -291,7 +289,7 @@ int main()
                 if (errno==EAGAIN)
                     io_eagain(s);
                 else
-                    carp("socket_accept6");
+                    carp("socket_accept4");
             }
             else
             {
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;
 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 ) {
-  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 );
 
-  if( exactmatch ) return match;
+  if( *exactmatch ) return match;
 
   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;
+
+    // Adjust pointer if it moved by realloc
+    match = match - (ot_byte*)vector->data + new_data;
+
     vector->data = new_data;
     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++;
   return match;
 }
 
 int vector_remove_peer( ot_vector vector, ot_peer peer ) {
   int exactmatch;
-  ot_peer end = ((ot_peer)vector->data) + vector->size;
   ot_peer match;
 
   if( !vector->size ) return 0;
@@ -80,7 +82,7 @@ int vector_remove_peer( ot_vector vector, ot_peer peer ) {
 
   if( !exactmatch ) return 0;
   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--;
   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 ) {
   char           *r = reply;
   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;
 
   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 ));
     int off = random() % (int)floor( step );
     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 ) {
-      off -= torrent->peer_list->peers[pool_index].size - pool_offset;
-      pool_offset = 0; pool_index++;
+    while( pool_offset >= torrent->peer_list->peers[pool_index].size ) {
+      pool_offset -= torrent->peer_list->peers[pool_index].size;
+      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++ = 'e';
-- 
cgit v1.2.3