summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <>2009-08-29 13:55:59 +0000
committererdgeist <>2009-08-29 13:55:59 +0000
commit478884660fa428f8fa1581f4e81d2d2aa27a3cb4 (patch)
tree2f2081a44f5f49e750224a089d918e9926d7e40b
parent682bd069d28faf4145b0f01009080a06e2ccd45d (diff)
Enable limiting fullscrapes to only every 5 minutes per IP
-rw-r--r--ot_http.c32
-rw-r--r--ot_stats.h1
-rw-r--r--trackerlogic.h4
3 files changed, 35 insertions, 2 deletions
diff --git a/ot_http.c b/ot_http.c
index 849e396..f116c1f 100644
--- a/ot_http.c
+++ b/ot_http.c
@@ -10,6 +10,7 @@
10#include <stdio.h> 10#include <stdio.h>
11#include <string.h> 11#include <string.h>
12#include <unistd.h> 12#include <unistd.h>
13#include <pthread.h>
13 14
14/* Libowfat */ 15/* Libowfat */
15#include "byte.h" 16#include "byte.h"
@@ -79,11 +80,12 @@ static void http_senddata( const int64 sock, struct ot_workstruct *ws ) {
79#define HTTPERROR_400_PARAM return http_issue_error( sock, ws, CODE_HTTPERROR_400_PARAM ) 80#define HTTPERROR_400_PARAM return http_issue_error( sock, ws, CODE_HTTPERROR_400_PARAM )
80#define HTTPERROR_400_COMPACT return http_issue_error( sock, ws, CODE_HTTPERROR_400_COMPACT ) 81#define HTTPERROR_400_COMPACT return http_issue_error( sock, ws, CODE_HTTPERROR_400_COMPACT )
81#define HTTPERROR_400_DOUBLEHASH return http_issue_error( sock, ws, CODE_HTTPERROR_400_PARAM ) 82#define HTTPERROR_400_DOUBLEHASH return http_issue_error( sock, ws, CODE_HTTPERROR_400_PARAM )
83#define HTTPERROR_402_NOTMODEST return http_issue_error( sock, ws, CODE_HTTPERROR_402_NOTMODEST )
82#define HTTPERROR_403_IP return http_issue_error( sock, ws, CODE_HTTPERROR_403_IP ) 84#define HTTPERROR_403_IP return http_issue_error( sock, ws, CODE_HTTPERROR_403_IP )
83#define HTTPERROR_404 return http_issue_error( sock, ws, CODE_HTTPERROR_404 ) 85#define HTTPERROR_404 return http_issue_error( sock, ws, CODE_HTTPERROR_404 )
84#define HTTPERROR_500 return http_issue_error( sock, ws, CODE_HTTPERROR_500 ) 86#define HTTPERROR_500 return http_issue_error( sock, ws, CODE_HTTPERROR_500 )
85ssize_t http_issue_error( const int64 sock, struct ot_workstruct *ws, int code ) { 87ssize_t http_issue_error( const int64 sock, struct ot_workstruct *ws, int code ) {
86 char *error_code[] = { "302 Found", "400 Invalid Request", "400 Invalid Request", "400 Invalid Request", 88 char *error_code[] = { "302 Found", "400 Invalid Request", "400 Invalid Request", "400 Invalid Request", "402 Payment Required",
87 "403 Access Denied", "404 Not Found", "500 Internal Server Error" }; 89 "403 Access Denied", "404 Not Found", "500 Internal Server Error" };
88 char *title = error_code[code]; 90 char *title = error_code[code];
89 91
@@ -234,12 +236,39 @@ static const ot_keywords keywords_format[] =
234 return ws->reply_size; 236 return ws->reply_size;
235} 237}
236 238
239#ifdef WANT_MODEST_FULLSCRAPES
240static pthread_mutex_t g_modest_fullscrape_mutex = PTHREAD_MUTEX_INITIALIZER;
241static ot_vector g_modest_fullscrape_timeouts;
242typedef struct { ot_ip6 ip; ot_time last_fullscrape; } ot_scrape_log;
243#endif
244
237#ifdef WANT_FULLSCRAPE 245#ifdef WANT_FULLSCRAPE
238static ssize_t http_handle_fullscrape( const int64 sock, struct ot_workstruct *ws ) { 246static ssize_t http_handle_fullscrape( const int64 sock, struct ot_workstruct *ws ) {
239 struct http_data* cookie = io_getcookie( sock ); 247 struct http_data* cookie = io_getcookie( sock );
240 int format = 0; 248 int format = 0;
241 tai6464 t; 249 tai6464 t;
242 250
251#ifdef WANT_MODEST_FULLSCRAPES
252 {
253 ot_scrape_log this_peer, *new_peer;
254 int exactmatch;
255 memcpy( this_peer.ip, cookie->ip, sizeof(ot_ip6));
256 this_peer.last_fullscrape = g_now_seconds;
257 pthread_mutex_lock(&g_modest_fullscrape_mutex);
258 new_peer = vector_find_or_insert( &g_modest_fullscrape_timeouts, &this_peer, sizeof(ot_scrape_log), sizeof(ot_ip6), &exactmatch );
259 if( !new_peer ) {
260 pthread_mutex_unlock(&g_modest_fullscrape_mutex);
261 HTTPERROR_500;
262 }
263 if( exactmatch && ( this_peer.last_fullscrape - new_peer->last_fullscrape ) < OT_MODEST_PEER_TIMEOUT ) {
264 pthread_mutex_unlock(&g_modest_fullscrape_mutex);
265 HTTPERROR_402_NOTMODEST;
266 }
267 memcpy( new_peer, &this_peer, sizeof(ot_scrape_log));
268 pthread_mutex_unlock(&g_modest_fullscrape_mutex);
269 }
270#endif
271
243#ifdef WANT_COMPRESSION_GZIP 272#ifdef WANT_COMPRESSION_GZIP
244 ws->request[ws->request_size-1] = 0; 273 ws->request[ws->request_size-1] = 0;
245 if( strstr( ws->request, "gzip" ) ) { 274 if( strstr( ws->request, "gzip" ) ) {
@@ -476,7 +505,6 @@ ssize_t http_handle_request( const int64 sock, struct ot_workstruct *ws ) {
476 struct http_data *cookie = io_getcookie( sock ); 505 struct http_data *cookie = io_getcookie( sock );
477 if( loglist_check_address( cookie->ip ) ) { 506 if( loglist_check_address( cookie->ip ) ) {
478 ot_log *log = malloc( sizeof( ot_log ) ); 507 ot_log *log = malloc( sizeof( ot_log ) );
479 printf( "Hello World\n" );
480 if( log ) { 508 if( log ) {
481 log->size = ws->request_size; 509 log->size = ws->request_size;
482 log->data = malloc( ws->request_size ); 510 log->data = malloc( ws->request_size );
diff --git a/ot_stats.h b/ot_stats.h
index d356aaf..ca6b57b 100644
--- a/ot_stats.h
+++ b/ot_stats.h
@@ -27,6 +27,7 @@ enum {
27 CODE_HTTPERROR_400, 27 CODE_HTTPERROR_400,
28 CODE_HTTPERROR_400_PARAM, 28 CODE_HTTPERROR_400_PARAM,
29 CODE_HTTPERROR_400_COMPACT, 29 CODE_HTTPERROR_400_COMPACT,
30 CODE_HTTPERROR_402_NOTMODEST,
30 CODE_HTTPERROR_403_IP, 31 CODE_HTTPERROR_403_IP,
31 CODE_HTTPERROR_404, 32 CODE_HTTPERROR_404,
32 CODE_HTTPERROR_500, 33 CODE_HTTPERROR_500,
diff --git a/trackerlogic.h b/trackerlogic.h
index 7d9c2e1..5a40055 100644
--- a/trackerlogic.h
+++ b/trackerlogic.h
@@ -36,6 +36,10 @@ typedef struct { ot_ip6 address; int bits; }
36 36
37#define OT_CLIENT_REQUEST_INTERVAL_RANDOM ( OT_CLIENT_REQUEST_INTERVAL - OT_CLIENT_REQUEST_VARIATION/2 + (int)( random( ) % OT_CLIENT_REQUEST_VARIATION ) ) 37#define OT_CLIENT_REQUEST_INTERVAL_RANDOM ( OT_CLIENT_REQUEST_INTERVAL - OT_CLIENT_REQUEST_VARIATION/2 + (int)( random( ) % OT_CLIENT_REQUEST_VARIATION ) )
38 38
39/* If WANT_MODEST_FULLSCRAPES is on, ip addresses may not
40 fullscrape more frequently than this amount in seconds */
41#define OT_MODEST_PEER_TIMEOUT (60*5)
42
39/* If peers come back before 10 minutes, don't live sync them */ 43/* If peers come back before 10 minutes, don't live sync them */
40#define OT_CLIENT_SYNC_RENEW_BOUNDARY 10 44#define OT_CLIENT_SYNC_RENEW_BOUNDARY 10
41 45