int kad_status( char *buf, int size ) { char hexbuf[SHA1_HEX_LENGTH+1]; struct storage *strg = storage; struct search *srch = searches; int numsearches_active = 0; int numsearches_done = 0; int numstorage = 0; int numstorage_peers = 0; int numvalues = 0; int written = 0; /* count searches */ while( srch != NULL ) { if( srch->done ) { numsearches_done++; } else { numsearches_active++; } srch = srch->next; } /* count storage and peers */ while( strg != NULL ) { numstorage_peers += strg->numpeers; numstorage++; strg = strg->next; } numvalues = values_count(); bprintf( "Version: %s\n", kadnode_version_str ); bprintf( "DHT id: %s\n", str_id( myid, hexbuf ) ); bprintf( "DHT bound to: %s:%s / %s\n", (gconf->af == AF_INET) ? "0.0.0.0" : "::", gconf->dht_port, (gconf->dht_ifname == NULL) ? "<any device>" : gconf->dht_ifname ); bprintf( "DHT Nodes: %d (%d good) (%s)\n", kad_count_nodes( 0 ), kad_count_nodes( 1 ), (gconf->af == AF_INET) ? "IPv4" : "IPv6" ); bprintf( "DHT Storage: %d (max %d), %d peers (max %d per storage)\n", numstorage, DHT_MAX_HASHES, numstorage_peers, DHT_MAX_PEERS ); bprintf( "DHT Searches: %d active, %d completed (max %d)\n", numsearches_active, numsearches_done, DHT_MAX_SEARCHES ); bprintf( "DHT Blacklist: %d (max %d)\n", (next_blacklisted % DHT_MAX_BLACKLISTED), DHT_MAX_BLACKLISTED ); bprintf( "DHT Values to announce: %d\n", numvalues ); return written; }
void handle_mcast( int rc, int sock_recv ) { char addrbuf[FULL_ADDSTRLEN+1]; char buf[512]; IP c_addr; socklen_t addrlen; int rc_recv; if( g_mcast_time <= time_now_sec() ) { if( kad_count_nodes( 0 ) == 0 ) { /* Join multicast group if possible */ if( g_mcast_registered == 0 && multicast_set_groups( sock_recv, &g_lpd_addr, gconf->dht_ifname, 1 ) == 0 ) { log_info( "LPD: No peers known. Joined multicast group." ); g_mcast_registered = 1; } if( g_mcast_registered == 1 ) { log_info( "LPD: Send multicast message to find nodes." ); /* Create message */ snprintf( buf, sizeof(buf), msg_fmt, str_addr( &g_lpd_addr, addrbuf ), atoi( gconf->dht_port ), g_infohash ); mcast_send_packets( buf, gconf->dht_ifname ); } } /* Cap number of received packets to 10 per minute */ g_packet_limit = 5 * PACKET_LIMIT_MAX; /* Try again in ~5 minutes */ g_mcast_time = time_add_min( 5 ); } if( rc <= 0 ) { return; } /* Reveice multicast ping */ addrlen = sizeof(IP); rc_recv = recvfrom( sock_recv, buf, sizeof(buf), 0, (struct sockaddr*) &c_addr, (socklen_t*) &addrlen ); if( rc_recv < 0 ) { log_warn( "LPD: Cannot receive multicast message: %s", strerror( errno ) ); return; } if( g_packet_limit < 0 ) { /* Too much traffic - leave multicast group for now */ if( g_mcast_registered == 1 && multicast_set_groups( sock_recv, &g_lpd_addr, gconf->dht_ifname, 0 ) == 0 ) { log_warn( "LPD: Too much traffic. Left multicast group." ); g_mcast_registered = 0; } return; } else { g_packet_limit--; } if( rc_recv >= sizeof(buf) ) { return; } else { buf[rc_recv] = '\0'; } int port = parse_packet( buf ); if( port > 0 ) { port_set( &c_addr, port ); log_debug( "LPD: Ping lonely peer at %s", str_addr( &c_addr, addrbuf ) ); kad_ping( &c_addr ); } else { log_debug( "LPD: Received invalid packet on multicast group." ); } }