diff --git a/core/DHT.c b/core/DHT.c index 1d13aa73..bcaaf6d8 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -319,6 +319,28 @@ static int replace_bad( Client_data * list, return 1; } +/*Sort the list. It will be sorted from furthest to closest. + TODO: this is innefficient and needs to be optimized.*/ +static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id) +{ + if(length == 0) + return; + uint32_t i, count; + while(1) { + count = 0; + for(i = 0; i < (length - 1); ++i) { + if(id_closest(comp_client_id, list[i].client_id, list[i + 1].client_id) == 1) { + Client_data temp = list[i + 1]; + list[i + 1] = list[i]; + list[i] = temp; + ++count; + } + } + if(count == 0) + return; + } +} + /* replace the first good node that is further to the comp_client_id than that of the client_id in the list */ static int replace_good( Client_data * list, @@ -329,6 +351,7 @@ static int replace_good( Client_data * list, { uint32_t i; uint64_t temp_time = unix_time(); + sort_list(list, length, comp_client_id); for(i = 0; i < length; ++i) if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) { diff --git a/testing/rect.py b/testing/rect.py deleted file mode 100644 index 05f0abc4..00000000 --- a/testing/rect.py +++ /dev/null @@ -1,44 +0,0 @@ -#basic python UDP script -#for testing only -import socket -import random - -UDP_IP = "127.0.0.1" -UDP_PORT = 5004 - -sock = socket.socket(socket.AF_INET, # Internet - socket.SOCK_DGRAM) # UDP -sock.bind((UDP_IP, UDP_PORT)) - -#our client_id -client_id = str(''.join(random.choice("abcdefghijklmnopqrstuvwxyz") for x in range(32))) - -print client_id -a = 1; -#send ping request to our DHT on localhost. -sock.sendto("0012345678".decode("hex") + client_id, ('127.0.0.1', 33445)) - -#print all packets received and respond to ping requests properly -while True: - data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes - print "received message:", data.encode('hex'), " From:", addr - #if we receive a ping request. - print data[0].encode('hex') - if data[0] == "00".decode('hex'): - print "Sending ping resp" - sock.sendto("01".decode('hex') + data[1:5] + client_id, addr) - - #if we receive a get_nodes request. - if data[0] == "02".decode('hex'): - print "Sending getn resp" - #send send nodes packet with a couple 127.0.0.1 ips and ports. - #127.0.0.1:5000, 127.0.0.1:5001, 127.0.0.1:5002 - sock.sendto("03".decode('hex') + data[1:5] + client_id + ("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH" + "7F00000113880000".decode('hex') + "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH" + "7F00000113890000".decode('hex') + "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH" + "7F000001138A0000".decode('hex')), addr) - - if data[0] == "10".decode('hex'): - print "Sending handshake resp" - sock.sendto("10".decode('hex') + data[1:5] + client_id[:4], addr) - if data[0] == "11".decode('hex'): - print "Sending SYNC resp" - a+=1 - sock.sendto("11".decode('hex') + chr(a) + data[1:9], addr) diff --git a/testing/toxic/dhtstatus.c b/testing/toxic/dhtstatus.c index 66268900..e026a173 100644 --- a/testing/toxic/dhtstatus.c +++ b/testing/toxic/dhtstatus.c @@ -43,13 +43,13 @@ static void dhtstatus_onDraw(ToxWindow *self) uint32_t i, j; ipbuf ipbuf; wprintw(self->window,"\n%llu ______________________ CLOSE LIST ________________________ ___ IP ADDR ___ _PRT_ LST PNG ____ SELF ____ _PRT_ LST\n\n", now); - for(i = 0; i < CLIENT_ID_SIZE; i++) { + for(i = 0; i < 32; i++) { /*Number of nodes in closelist*/ Client_data * client = close_clientlist + i; if (i == num_selected) wattron(self->window, COLOR_PAIR(3)); wprintw(self->window,"[%02i] ", i); uint16_t port = ntohs(client->ip_port.port); if(port) { - for(j = 0; j < 32; j++) + for(j = 0; j < CLIENT_ID_SIZE; j++) wprintw(self->window, "%02hhx", client->client_id[j]); printip(ipbuf, client->ip_port.ip);