From afe966b64418c01e7ea09bb31c720365b0c4f395 Mon Sep 17 00:00:00 2001
From: erdgeist <>
Date: Sun, 22 Jul 2007 16:17:26 +0000
Subject: some subnet stats... still ugly and untested.

---
 opentracker.c  |  5 +++++
 trackerlogic.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 trackerlogic.h |  3 ++-
 3 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/opentracker.c b/opentracker.c
index b13dc11..ff1c1c6 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -274,6 +274,8 @@ static void httpresponse( const int64 s, char *data ) {
           mode = STATS_TCP;
         else if( !byte_diff(data,4,"udp4"))
           mode = STATS_UDP;
+        else if( !byte_diff(data,4,"s24s"))
+          mode = STATS_SLASH24S;
         else
           HTTPERROR_400_PARAM;
       }
@@ -304,6 +306,9 @@ static void httpresponse( const int64 s, char *data ) {
           /* Enough for http header + whole scrape string */
           if( !( reply_size = return_stats_for_tracker( SUCCESS_HTTP_HEADER_LENGTH + static_outbuf, mode ) ) ) HTTPERROR_500;
           break;
+        case STATS_SLASH24S:
+          if( !( reply_size = return_stats_for_slash24s( SUCCESS_HTTP_HEADER_LENGTH + static_outbuf, 25, 64 ) ) ) HTTPERROR_500;
+          break;
       }
     break;
 
diff --git a/trackerlogic.c b/trackerlogic.c
index 98fcef9..ee49eb7 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -634,6 +634,59 @@ size_t return_stats_for_tracker( char *reply, int mode ) {
   return r - reply;
 }
 
+size_t return_stats_for_slash24s( char *reply, size_t amount, ot_dword thresh ) {
+  ot_word *count = malloc( 0x1000000 * sizeof(ot_word) );
+  ot_dword slash24s[amount*2];  /* first dword amount, second dword subnet */
+  size_t i, j, k, l;
+  char     *r  = reply;
+
+  if( !count )
+    return 0;
+
+  byte_zero( count, 0x1000000 * sizeof(ot_word) );
+  byte_zero( slash24s, amount * 2 * sizeof(ot_dword) );
+
+  r += sprintf( r, "Stats for all /24s with more than %d announced torrents:\n\n", ((int)thresh) );
+
+  for( i=0; i<256; ++i ) {
+    ot_vector *torrents_list = &all_torrents[i];
+    for( j=0; j<torrents_list->size; ++j ) {
+      ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list;
+      for( k=0; k<OT_POOLS_COUNT; ++k ) {
+        ot_peer *peers =    peer_list->peers[k].data;
+        size_t   numpeers = peer_list->peers[k].size;
+        for( l=0; l<numpeers; ++l )
+          if( ++count[ (*(ot_dword*)(peers+l))>>8 ] == 65335 )
+            count[ (*(ot_dword*)(peers+l))>>8 ] = 65334;
+      }
+    }
+  }
+
+  for( i=0; i<0x1000000; ++i )
+    if( count[i] >= thresh ) {
+      /* This subnet seems to announce more torrents than the last in our list */
+      int insert_pos = amount - 1;
+      while( ( insert_pos >= 0 ) && ( count[i] > slash24s[ 2 * insert_pos ] ) )
+        --insert_pos;
+      ++insert_pos;
+      memmove( slash24s + 2 * ( insert_pos + 1 ), slash24s + 2 * ( insert_pos ), 2 * sizeof( ot_dword ) * ( amount - insert_pos - 1 ) );
+      slash24s[ 2 * insert_pos     ] = count[i];
+      slash24s[ 2 * insert_pos + 1 ] = i;
+      if( slash24s[ 2 * amount - 2 ] > thresh )
+        thresh = slash24s[ 2 * amount - 2 ];
+    }
+
+  free( count );
+
+  for( i=0; i < amount; ++i )
+    if( slash24s[ 2*i ] >= thresh ) {
+      unsigned long ip = slash24s[ 2*i +1 ];
+      r += sprintf( r, "% 10ld %d.%d.%d/24\n", (long)slash24s[ 2*i ], (int)(ip >> 16), (int)(255 & ( ip >> 8 )), (int)(ip & 255) );
+    }
+
+  return r - reply;
+}
+
 void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ) {
   int          exactmatch, i;
   ot_vector   *torrents_list = &all_torrents[*hash[0]];
diff --git a/trackerlogic.h b/trackerlogic.h
index eb4d4f6..3b2d883 100644
--- a/trackerlogic.h
+++ b/trackerlogic.h
@@ -84,7 +84,7 @@ typedef struct {
 int  init_logic( const char * const serverdir );
 void deinit_logic( void );
 
-enum { STATS_MRTG, STATS_TOP5, STATS_DMEM, STATS_TCP, STATS_UDP, SYNC_IN, SYNC_OUT };
+enum { STATS_MRTG, STATS_TOP5, STATS_DMEM, STATS_TCP, STATS_UDP, STATS_SLASH24S, SYNC_IN, SYNC_OUT };
 
 ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer, int from_changeset );
 int    add_changeset_to_tracker( ot_byte *data, size_t len );
@@ -93,6 +93,7 @@ size_t return_fullscrape_for_tracker( char **reply );
 size_t return_tcp_scrape_for_torrent( ot_hash *hash, char *reply );
 size_t return_udp_scrape_for_torrent( ot_hash *hash, char *reply );
 size_t return_stats_for_tracker( char *reply, int mode );
+size_t return_stats_for_slash24s( char *reply, size_t amount, ot_dword thresh );
 size_t return_memstat_for_tracker( char **reply );
 size_t return_changeset_for_tracker( char **reply );
 void   clean_all_torrents( void );
-- 
cgit v1.2.3