static int accesslist_addentry( ot_hash *infohash ) { int eger; void *insert = vector_find_or_insert( &accesslist, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &eger ); if( !insert ) return -1; memmove( insert, infohash, OT_HASH_COMPARE_SIZE ); return 0; }
static ssize_t http_handle_fullscrape( const int64 sock, struct ot_workstruct *ws ) { struct http_data* cookie = io_getcookie( sock ); int format = 0; tai6464 t; #ifdef WANT_MODEST_FULLSCRAPES { ot_scrape_log this_peer, *new_peer; int exactmatch; memcpy( this_peer.ip, cookie->ip, sizeof(ot_ip6)); this_peer.last_fullscrape = g_now_seconds; pthread_mutex_lock(&g_modest_fullscrape_mutex); new_peer = vector_find_or_insert( &g_modest_fullscrape_timeouts, &this_peer, sizeof(ot_scrape_log), sizeof(ot_ip6), &exactmatch ); if( !new_peer ) { pthread_mutex_unlock(&g_modest_fullscrape_mutex); HTTPERROR_500; } if( exactmatch && ( this_peer.last_fullscrape - new_peer->last_fullscrape ) < OT_MODEST_PEER_TIMEOUT ) { pthread_mutex_unlock(&g_modest_fullscrape_mutex); HTTPERROR_402_NOTMODEST; } memcpy( new_peer, &this_peer, sizeof(ot_scrape_log)); pthread_mutex_unlock(&g_modest_fullscrape_mutex); } #endif #ifdef WANT_COMPRESSION_GZIP ws->request[ws->request_size-1] = 0; if( strstr( ws->request, "gzip" ) ) { cookie->flag |= STRUCT_HTTP_FLAG_GZIP; format = TASK_FLAG_GZIP; stats_issue_event( EVENT_FULLSCRAPE_REQUEST_GZIP, 0, (uintptr_t)cookie->ip ); } else #endif stats_issue_event( EVENT_FULLSCRAPE_REQUEST, 0, (uintptr_t)cookie->ip ); #ifdef _DEBUG_HTTPERROR fprintf( stderr, "%s", ws->debugbuf ); #endif /* Pass this task to the worker thread */ cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK; /* Clients waiting for us should not easily timeout */ taia_uint( &t, 0 ); io_timeout( sock, t ); fullscrape_deliver( sock, TASK_FULLSCRAPE | format ); io_dontwantread( sock ); return ws->reply_size = -2; }
size_t add_peer_to_torrent_proxy( ot_hash hash, ot_peer *peer ) { int exactmatch; ot_torrent *torrent; ot_peer *peer_dest; ot_vector *torrents_list = mutex_bucket_lock_by_hash( hash ); torrent = vector_find_or_insert( torrents_list, (void*)hash, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch ); if( !torrent ) return -1; if( !exactmatch ) { /* Create a new torrent entry, then */ memcpy( torrent->hash, hash, sizeof(ot_hash) ); if( !( torrent->peer_list = malloc( sizeof (ot_peerlist) ) ) ) { vector_remove_torrent( torrents_list, torrent ); mutex_bucket_unlock_by_hash( hash, 0 ); return -1; } byte_zero( torrent->peer_list, sizeof( ot_peerlist ) ); } /* Check for peer in torrent */ peer_dest = vector_find_or_insert_peer( &(torrent->peer_list->peers), peer, &exactmatch ); if( !peer_dest ) { mutex_bucket_unlock_by_hash( hash, 0 ); return -1; } /* Tell peer that it's fresh */ OT_PEERTIME( peer ) = 0; /* If we hadn't had a match create peer there */ if( !exactmatch ) { torrent->peer_list->peer_count++; if( OT_PEERFLAG(peer) & PEER_FLAG_SEEDING ) torrent->peer_list->seed_count++; } memcpy( peer_dest, peer, sizeof(ot_peer) ); mutex_bucket_unlock_by_hash( hash, 0 ); return 0; }
static int persist_add_peer(ot_hash *hash, ot_peerlist *peer_list, ot_peer *peer) { int exactmatch, delta_torrentcount = 0; ot_torrent *torrent; ot_peer *peer_dest; /* eliminate compiler warnings */ (void)peer_list; ot_vector *torrents_list = mutex_bucket_lock_by_hash(*hash); if( !accesslist_hashisvalid( hash ) ) { mutex_bucket_unlock_by_hash( *hash, 0 ); return 0; } torrent = vector_find_or_insert( torrents_list, (void*)hash, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch ); if( !torrent ) { mutex_bucket_unlock_by_hash( *hash, 0 ); return 0; } if( !exactmatch ) { /* Create a new torrent entry, then */ memcpy( torrent->hash, hash, sizeof(ot_hash) ); if( !( torrent->peer_list = malloc( sizeof (ot_peerlist) ) ) ) { vector_remove_torrent( torrents_list, torrent ); mutex_bucket_unlock_by_hash( *hash, 0 ); return 0; } byte_zero( torrent->peer_list, sizeof( ot_peerlist ) ); delta_torrentcount = 1; } /* Ignore torrent base in odb file, just use current clock. */ torrent->peer_list->base = g_now_minutes; /* Check for peer in torrent */ peer_dest = vector_find_or_insert_peer( &(torrent->peer_list->peers), peer, &exactmatch ); if( !peer_dest ) { mutex_bucket_unlock_by_hash( *hash, delta_torrentcount ); return 0; } /* If we hadn't had a match, create peer there */ if( !exactmatch ) { torrent->peer_list->peer_count++; if( OT_PEERFLAG(peer) & PEER_FLAG_COMPLETED ) torrent->peer_list->down_count++; if( OT_PEERFLAG(peer) & PEER_FLAG_SEEDING ) torrent->peer_list->seed_count++; } else { LOG_ERR("Repeat peer in a same torrent\n"); assert(0); } memcpy( peer_dest, peer, sizeof(ot_peer) ); mutex_bucket_unlock_by_hash( *hash, delta_torrentcount ); return 0; }