From 6073127ad0712bf925fd80982b68b072541824db Mon Sep 17 00:00:00 2001
From: erdgeist <>
Date: Wed, 26 Aug 2009 21:23:26 +0000
Subject: Add some subnet debugging features

---
 ot_http.c  | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 ot_mutex.h |  1 +
 ot_stats.c | 37 ++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+), 2 deletions(-)

diff --git a/ot_http.c b/ot_http.c
index 7334609..849e396 100644
--- a/ot_http.c
+++ b/ot_http.c
@@ -16,6 +16,7 @@
 #include "array.h"
 #include "iob.h"
 #include "ip6.h"
+#include "scan.h"
 
 /* Opentracker */
 #include "trackerlogic.h"
@@ -168,7 +169,7 @@ static const ot_keywords keywords_mode[] =
     { "busy", TASK_STATS_BUSY_NETWORKS }, { "torr", TASK_STATS_TORRENTS }, { "fscr", TASK_STATS_FULLSCRAPE },
     { "s24s", TASK_STATS_SLASH24S }, { "tpbs", TASK_STATS_TPB }, { "herr", TASK_STATS_HTTPERRORS }, { "completed", TASK_STATS_COMPLETED },
     { "top10", TASK_STATS_TOP10 }, { "renew", TASK_STATS_RENEW }, { "syncs", TASK_STATS_SYNCS }, { "version", TASK_STATS_VERSION },
-    { "everything", TASK_STATS_EVERYTHING }, { "statedump", TASK_FULLSCRAPE_TRACKERSTATE }, { NULL, -3 } };
+    { "everything", TASK_STATS_EVERYTHING }, { "statedump", TASK_FULLSCRAPE_TRACKERSTATE }, { "fulllog", TASK_STATS_FULLLOG }, { NULL, -3 } };
 static const ot_keywords keywords_format[] =
   { { "bin", TASK_FULLSCRAPE_TPB_BINARY }, { "ben", TASK_FULLSCRAPE }, { "url", TASK_FULLSCRAPE_TPB_URLENCODED },
     { "txt", TASK_FULLSCRAPE_TPB_ASCII }, { NULL, -3 } };
@@ -306,6 +307,9 @@ static ot_keywords keywords_announce[] = { { "port", 1 }, { "left", 2 }, { "even
 #ifdef WANT_IP_FROM_QUERY_STRING
 { "ip", 7 },
 #endif
+#ifdef WANT_FULLLOG_NETWORKS
+{ "lognet", 8 },
+#endif
 { NULL, -3 } };
 static ot_keywords keywords_announce_event[] = { { "completed", 1 }, { "stopped", 2 }, { NULL, -3 } };
 static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws, char *read_ptr ) {
@@ -411,6 +415,37 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
         OT_SETIP( &peer, tmp_buf1 );
       }
       break;
+#endif
+#ifdef WANT_FULLLOG_NETWORKS
+      case 8: /* matched "lognet" */
+      {
+        //if( accesslist_isblessed( cookie->ip, OT_PERMISSION_MAY_STAT ) ) {
+          char *tmp_buf = ws->reply;
+          ot_net net;
+          signed short parsed, bits;
+
+          len = scan_urlencoded_query( &read_ptr, tmp_buf, SCAN_SEARCHPATH_VALUE );
+          tmp_buf[len] = 0;
+          if( len <= 0 ) HTTPERROR_400_PARAM;
+          if( *tmp_buf == '-' ) {
+            loglist_reset( );
+            return ws->reply_size = sprintf( ws->reply, "Successfully removed.\n" );
+          }
+          parsed = scan_ip6( tmp_buf, net.address );
+          if( !parsed ) HTTPERROR_400_PARAM;
+          if( tmp_buf[parsed++] != '/' )
+            bits = 128;
+          else {
+            parsed = scan_short( tmp_buf + parsed, &bits );
+            if( !parsed ) HTTPERROR_400_PARAM; 
+            if( ip6_isv4mapped( net.address ) )
+              bits += 96;
+          }
+          net.bits = bits;
+          loglist_add_network( &net );
+          return ws->reply_size = sprintf( ws->reply, "Successfully added.\n" );
+        //}
+      }
 #endif
     }
   }
@@ -437,6 +472,31 @@ ssize_t http_handle_request( const int64 sock, struct ot_workstruct *ws ) {
   ssize_t reply_off, len;
   char   *read_ptr = ws->request, *write_ptr;
 
+#ifdef WANT_FULLLOG_NETWORKS
+  struct http_data *cookie = io_getcookie( sock );
+  if( loglist_check_address( cookie->ip ) ) {
+    ot_log *log = malloc( sizeof( ot_log ) );
+    printf( "Hello World\n" );
+    if( log ) {
+      log->size = ws->request_size;
+      log->data = malloc( ws->request_size );
+      log->next = 0;
+      log->time = g_now_seconds;
+      memcpy( log->ip, cookie->ip, sizeof(ot_ip6));
+      if( log->data ) {
+        memcpy( log->data, ws->request, ws->request_size );
+        if( !g_logchain_first )
+          g_logchain_first = g_logchain_last = log;
+        else {
+          g_logchain_last->next = log;
+          g_logchain_last = log;  
+        }        
+      } else
+        free( log );
+    }
+  }
+#endif
+
 #ifdef _DEBUG_HTTPERROR
   reply_off = ws->request_size;
   if( ws->request_size >= G_DEBUGBUF_SIZE )
@@ -444,7 +504,7 @@ ssize_t http_handle_request( const int64 sock, struct ot_workstruct *ws ) {
   memcpy( ws->debugbuf, ws->request, reply_off );
   ws->debugbuf[ reply_off ] = 0;
 #endif
-
+  
   /* Tell subroutines where to put reply data */
   ws->reply = ws->outbuf + SUCCESS_HTTP_HEADER_LENGTH;
 
diff --git a/ot_mutex.h b/ot_mutex.h
index 183fe2b..47d9abf 100644
--- a/ot_mutex.h
+++ b/ot_mutex.h
@@ -39,6 +39,7 @@ typedef enum {
   TASK_STATS_SLASH24S              = 0x0103,
   TASK_STATS_TOP10                 = 0x0104,
   TASK_STATS_EVERYTHING            = 0x0105,
+  TASK_STATS_FULLLOG               = 0x0106,
 
   TASK_FULLSCRAPE                  = 0x0200, /* Default mode */
   TASK_FULLSCRAPE_TPB_BINARY       = 0x0201,
diff --git a/ot_stats.c b/ot_stats.c
index 8b36323..46fc961 100644
--- a/ot_stats.c
+++ b/ot_stats.c
@@ -26,6 +26,7 @@
 #include "ot_mutex.h"
 #include "ot_iovec.h"
 #include "ot_stats.h"
+#include "ot_accesslist.h"
 
 #ifndef NO_FULLSCRAPE_LOGGING
 #define LOG_TO_STDERR( ... ) fprintf( stderr, __VA_ARGS__ )
@@ -477,6 +478,38 @@ static size_t stats_return_completed_mrtg( char * reply ) {
                  );
 }
 
+#ifdef WANT_FULLLOG_NETWORKS
+static void stats_return_fulllog( int *iovec_entries, struct iovec **iovector, char *r ) {
+  ot_log *loglist = g_logchain_first, *llnext;
+  char * re = r + OT_STATS_TMPSIZE;
+
+  g_logchain_first = g_logchain_last = 0;
+  
+  while( loglist ) {
+    if( r + ( loglist->size + 64 ) >= re ) {
+      r = iovec_fix_increase_or_free( iovec_entries, iovector, r, 32 * OT_STATS_TMPSIZE );
+      if( !r ) return;
+      re = r + 32 * OT_STATS_TMPSIZE;
+    }
+    r += sprintf( r, "%08ld: ", loglist->time );
+    r += fmt_ip6c( r, loglist->ip );
+    *r++ = '\n';
+    memcpy( r, loglist->data, loglist->size );
+    r += loglist->size;
+    *r++ = '\n';
+    *r++ = '*';
+    *r++ = '\n';
+    *r++ = '\n';
+
+    llnext = loglist->next;
+    free( loglist->data );
+    free( loglist );
+    loglist = llnext;
+  }
+  iovec_fixlast( iovec_entries, iovector, r );
+}
+#endif
+
 static size_t stats_return_everything( char * reply ) {
   torrent_stats stats = {0,0,0};
   int i;
@@ -570,6 +603,10 @@ static void stats_make( int *iovec_entries, struct iovec **iovector, ot_tasktype
     case TASK_STATS_SLASH24S:    r += stats_slash24s_txt( r, 128 );         break;
     case TASK_STATS_TOP10:       r += stats_top10_txt( r );                 break;
     case TASK_STATS_EVERYTHING:  r += stats_return_everything( r );         break;
+#ifdef WANT_FULLLOG_NETWORKS
+    case TASK_STATS_FULLLOG:      stats_return_fulllog( iovec_entries, iovector, r );
+                                                                            return;
+#endif
     default:
       iovec_free(iovec_entries, iovector);
       return;
-- 
cgit v1.2.3