summaryrefslogtreecommitdiff
path: root/testing/DHT_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'testing/DHT_test.c')
-rw-r--r--testing/DHT_test.c72
1 files changed, 38 insertions, 34 deletions
diff --git a/testing/DHT_test.c b/testing/DHT_test.c
index e194d06b..bb482595 100644
--- a/testing/DHT_test.c
+++ b/testing/DHT_test.c
@@ -7,7 +7,26 @@
7 * EX: ./test 127.0.0.1 33445 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 7 * EX: ./test 127.0.0.1 33445 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
8 * 8 *
9 * The test will then ask you for the id (in hex format) of the friend you wish to add 9 * The test will then ask you for the id (in hex format) of the friend you wish to add
10 *
11 * Copyright (C) 2013 Tox project All Rights Reserved.
12 *
13 * This file is part of Tox.
14 *
15 * Tox is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * Tox is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
27 *
10 */ 28 */
29
11//#include "../core/network.h" 30//#include "../core/network.h"
12#include "../core/DHT.c" 31#include "../core/DHT.c"
13#include "../core/friend_requests.c" 32#include "../core/friend_requests.c"
@@ -28,17 +47,14 @@
28 47
29#define PORT 33445 48#define PORT 33445
30 49
31
32void print_clientlist() 50void print_clientlist()
33{ 51{
34 uint32_t i, j; 52 uint32_t i, j;
35 IP_Port p_ip; 53 IP_Port p_ip;
36 printf("___________________CLOSE________________________________\n"); 54 printf("___________________CLOSE________________________________\n");
37 for(i = 0; i < 4; i++) 55 for(i = 0; i < 4; i++) {
38 {
39 printf("ClientID: "); 56 printf("ClientID: ");
40 for(j = 0; j < 32; j++) 57 for(j = 0; j < 32; j++) {
41 {
42 printf("%c", close_clientlist[i].client_id[j]); 58 printf("%c", close_clientlist[i].client_id[j]);
43 } 59 }
44 p_ip = close_clientlist[i].ip_port; 60 p_ip = close_clientlist[i].ip_port;
@@ -56,12 +72,10 @@ void print_friendlist()
56 uint32_t i, j, k; 72 uint32_t i, j, k;
57 IP_Port p_ip; 73 IP_Port p_ip;
58 printf("_________________FRIENDS__________________________________\n"); 74 printf("_________________FRIENDS__________________________________\n");
59 for(k = 0; k < num_friends; k++) 75 for(k = 0; k < num_friends; k++) {
60 {
61 printf("FRIEND %u\n", k); 76 printf("FRIEND %u\n", k);
62 printf("ID: "); 77 printf("ID: ");
63 for(j = 0; j < 32; j++) 78 for(j = 0; j < 32; j++) {
64 {
65 printf("%c", friends_list[k].client_id[j]); 79 printf("%c", friends_list[k].client_id[j]);
66 } 80 }
67 p_ip = DHT_getfriendip(friends_list[k].client_id); 81 p_ip = DHT_getfriendip(friends_list[k].client_id);
@@ -69,11 +83,9 @@ void print_friendlist()
69 83
70 printf("\nCLIENTS IN LIST:\n\n"); 84 printf("\nCLIENTS IN LIST:\n\n");
71 85
72 for(i = 0; i < 4; i++) 86 for(i = 0; i < 4; i++) {
73 {
74 printf("ClientID: "); 87 printf("ClientID: ");
75 for(j = 0; j < 32; j++) 88 for(j = 0; j < 32; j++) {
76 {
77 if(0 <= friends_list[k].client_list[i].client_id[j] && friends_list[k].client_list[i].client_id[j] < 16) 89 if(0 <= friends_list[k].client_list[i].client_id[j] && friends_list[k].client_list[i].client_id[j] < 16)
78 printf("0"); 90 printf("0");
79 printf("%hhX", friends_list[k].client_list[i].client_id[j]); 91 printf("%hhX", friends_list[k].client_list[i].client_id[j]);
@@ -94,8 +106,7 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
94 uint32_t i; 106 uint32_t i;
95 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); 107 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
96 printf("--------------------BEGIN-----------------------------\n"); 108 printf("--------------------BEGIN-----------------------------\n");
97 for(i = 0; i < length; i++) 109 for(i = 0; i < length; i++) {
98 {
99 if(data[i] < 16) 110 if(data[i] < 16)
100 printf("0"); 111 printf("0");
101 printf("%hhX",data[i]); 112 printf("%hhX",data[i]);
@@ -110,8 +121,7 @@ unsigned char * hex_string_to_bin(char hex_string[])
110 unsigned char * val = malloc(strlen(hex_string)); 121 unsigned char * val = malloc(strlen(hex_string));
111 char * pos = hex_string; 122 char * pos = hex_string;
112 int i=0; 123 int i=0;
113 while(i < strlen(hex_string)) 124 while(i < strlen(hex_string)) {
114 {
115 sscanf(pos,"%2hhx",&val[i]); 125 sscanf(pos,"%2hhx",&val[i]);
116 pos+=2; 126 pos+=2;
117 i++; 127 i++;
@@ -130,8 +140,7 @@ int main(int argc, char *argv[])
130 new_keys(); 140 new_keys();
131 printf("OUR ID: "); 141 printf("OUR ID: ");
132 uint32_t i; 142 uint32_t i;
133 for(i = 0; i < 32; i++) 143 for(i = 0; i < 32; i++) {
134 {
135 if(self_public_key[i] < 16) 144 if(self_public_key[i] < 16)
136 printf("0"); 145 printf("0");
137 printf("%hhX",self_public_key[i]); 146 printf("%hhX",self_public_key[i]);
@@ -142,8 +151,8 @@ int main(int argc, char *argv[])
142 scanf("%s", temp_id); 151 scanf("%s", temp_id);
143 DHT_addfriend(hex_string_to_bin(temp_id)); 152 DHT_addfriend(hex_string_to_bin(temp_id));
144 153
145 //initialize networking 154 /* initialize networking */
146 //bind to ip 0.0.0.0:PORT 155 /* bind to ip 0.0.0.0:PORT */
147 IP ip; 156 IP ip;
148 ip.i = 0; 157 ip.i = 0;
149 init_networking(ip, PORT); 158 init_networking(ip, PORT);
@@ -152,10 +161,10 @@ int main(int argc, char *argv[])
152 perror("Initialization"); 161 perror("Initialization");
153 IP_Port bootstrap_ip_port; 162 IP_Port bootstrap_ip_port;
154 bootstrap_ip_port.port = htons(atoi(argv[2])); 163 bootstrap_ip_port.port = htons(atoi(argv[2]));
155 //bootstrap_ip_port.ip.c[0] = 127; 164 /* bootstrap_ip_port.ip.c[0] = 127;
156 //bootstrap_ip_port.ip.c[1] = 0; 165 * bootstrap_ip_port.ip.c[1] = 0;
157 //bootstrap_ip_port.ip.c[2] = 0; 166 * bootstrap_ip_port.ip.c[2] = 0;
158 //bootstrap_ip_port.ip.c[3] = 1; 167 * bootstrap_ip_port.ip.c[3] = 1; */
159 bootstrap_ip_port.ip.i = inet_addr(argv[1]); 168 bootstrap_ip_port.ip.i = inet_addr(argv[1]);
160 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); 169 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
161 170
@@ -163,20 +172,15 @@ int main(int argc, char *argv[])
163 uint8_t data[MAX_UDP_PACKET_SIZE]; 172 uint8_t data[MAX_UDP_PACKET_SIZE];
164 uint32_t length; 173 uint32_t length;
165 174
166 while(1) 175 while(1) {
167 {
168 176
169 doDHT(); 177 doDHT();
170 178
171 while(receivepacket(&ip_port, data, &length) != -1) 179 while(receivepacket(&ip_port, data, &length) != -1) {
172 { 180 if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) {
173 if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port))
174 {
175 //unhandled packet 181 //unhandled packet
176 printpacket(data, length, ip_port); 182 printpacket(data, length, ip_port);
177 } 183 } else {
178 else
179 {
180 printf("Received handled packet with length: %u\n", length); 184 printf("Received handled packet with length: %u\n", length);
181 } 185 }
182 } 186 }