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.c53
1 files changed, 19 insertions, 34 deletions
diff --git a/testing/DHT_test.c b/testing/DHT_test.c
index 80019029..bb482595 100644
--- a/testing/DHT_test.c
+++ b/testing/DHT_test.c
@@ -47,17 +47,14 @@
47 47
48#define PORT 33445 48#define PORT 33445
49 49
50
51void print_clientlist() 50void print_clientlist()
52{ 51{
53 uint32_t i, j; 52 uint32_t i, j;
54 IP_Port p_ip; 53 IP_Port p_ip;
55 printf("___________________CLOSE________________________________\n"); 54 printf("___________________CLOSE________________________________\n");
56 for(i = 0; i < 4; i++) 55 for(i = 0; i < 4; i++) {
57 {
58 printf("ClientID: "); 56 printf("ClientID: ");
59 for(j = 0; j < 32; j++) 57 for(j = 0; j < 32; j++) {
60 {
61 printf("%c", close_clientlist[i].client_id[j]); 58 printf("%c", close_clientlist[i].client_id[j]);
62 } 59 }
63 p_ip = close_clientlist[i].ip_port; 60 p_ip = close_clientlist[i].ip_port;
@@ -75,12 +72,10 @@ void print_friendlist()
75 uint32_t i, j, k; 72 uint32_t i, j, k;
76 IP_Port p_ip; 73 IP_Port p_ip;
77 printf("_________________FRIENDS__________________________________\n"); 74 printf("_________________FRIENDS__________________________________\n");
78 for(k = 0; k < num_friends; k++) 75 for(k = 0; k < num_friends; k++) {
79 {
80 printf("FRIEND %u\n", k); 76 printf("FRIEND %u\n", k);
81 printf("ID: "); 77 printf("ID: ");
82 for(j = 0; j < 32; j++) 78 for(j = 0; j < 32; j++) {
83 {
84 printf("%c", friends_list[k].client_id[j]); 79 printf("%c", friends_list[k].client_id[j]);
85 } 80 }
86 p_ip = DHT_getfriendip(friends_list[k].client_id); 81 p_ip = DHT_getfriendip(friends_list[k].client_id);
@@ -88,11 +83,9 @@ void print_friendlist()
88 83
89 printf("\nCLIENTS IN LIST:\n\n"); 84 printf("\nCLIENTS IN LIST:\n\n");
90 85
91 for(i = 0; i < 4; i++) 86 for(i = 0; i < 4; i++) {
92 {
93 printf("ClientID: "); 87 printf("ClientID: ");
94 for(j = 0; j < 32; j++) 88 for(j = 0; j < 32; j++) {
95 {
96 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)
97 printf("0"); 90 printf("0");
98 printf("%hhX", friends_list[k].client_list[i].client_id[j]); 91 printf("%hhX", friends_list[k].client_list[i].client_id[j]);
@@ -113,8 +106,7 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
113 uint32_t i; 106 uint32_t i;
114 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); 107 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
115 printf("--------------------BEGIN-----------------------------\n"); 108 printf("--------------------BEGIN-----------------------------\n");
116 for(i = 0; i < length; i++) 109 for(i = 0; i < length; i++) {
117 {
118 if(data[i] < 16) 110 if(data[i] < 16)
119 printf("0"); 111 printf("0");
120 printf("%hhX",data[i]); 112 printf("%hhX",data[i]);
@@ -129,8 +121,7 @@ unsigned char * hex_string_to_bin(char hex_string[])
129 unsigned char * val = malloc(strlen(hex_string)); 121 unsigned char * val = malloc(strlen(hex_string));
130 char * pos = hex_string; 122 char * pos = hex_string;
131 int i=0; 123 int i=0;
132 while(i < strlen(hex_string)) 124 while(i < strlen(hex_string)) {
133 {
134 sscanf(pos,"%2hhx",&val[i]); 125 sscanf(pos,"%2hhx",&val[i]);
135 pos+=2; 126 pos+=2;
136 i++; 127 i++;
@@ -149,8 +140,7 @@ int main(int argc, char *argv[])
149 new_keys(); 140 new_keys();
150 printf("OUR ID: "); 141 printf("OUR ID: ");
151 uint32_t i; 142 uint32_t i;
152 for(i = 0; i < 32; i++) 143 for(i = 0; i < 32; i++) {
153 {
154 if(self_public_key[i] < 16) 144 if(self_public_key[i] < 16)
155 printf("0"); 145 printf("0");
156 printf("%hhX",self_public_key[i]); 146 printf("%hhX",self_public_key[i]);
@@ -161,8 +151,8 @@ int main(int argc, char *argv[])
161 scanf("%s", temp_id); 151 scanf("%s", temp_id);
162 DHT_addfriend(hex_string_to_bin(temp_id)); 152 DHT_addfriend(hex_string_to_bin(temp_id));
163 153
164 //initialize networking 154 /* initialize networking */
165 //bind to ip 0.0.0.0:PORT 155 /* bind to ip 0.0.0.0:PORT */
166 IP ip; 156 IP ip;
167 ip.i = 0; 157 ip.i = 0;
168 init_networking(ip, PORT); 158 init_networking(ip, PORT);
@@ -171,10 +161,10 @@ int main(int argc, char *argv[])
171 perror("Initialization"); 161 perror("Initialization");
172 IP_Port bootstrap_ip_port; 162 IP_Port bootstrap_ip_port;
173 bootstrap_ip_port.port = htons(atoi(argv[2])); 163 bootstrap_ip_port.port = htons(atoi(argv[2]));
174 //bootstrap_ip_port.ip.c[0] = 127; 164 /* bootstrap_ip_port.ip.c[0] = 127;
175 //bootstrap_ip_port.ip.c[1] = 0; 165 * bootstrap_ip_port.ip.c[1] = 0;
176 //bootstrap_ip_port.ip.c[2] = 0; 166 * bootstrap_ip_port.ip.c[2] = 0;
177 //bootstrap_ip_port.ip.c[3] = 1; 167 * bootstrap_ip_port.ip.c[3] = 1; */
178 bootstrap_ip_port.ip.i = inet_addr(argv[1]); 168 bootstrap_ip_port.ip.i = inet_addr(argv[1]);
179 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); 169 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
180 170
@@ -182,20 +172,15 @@ int main(int argc, char *argv[])
182 uint8_t data[MAX_UDP_PACKET_SIZE]; 172 uint8_t data[MAX_UDP_PACKET_SIZE];
183 uint32_t length; 173 uint32_t length;
184 174
185 while(1) 175 while(1) {
186 {
187 176
188 doDHT(); 177 doDHT();
189 178
190 while(receivepacket(&ip_port, data, &length) != -1) 179 while(receivepacket(&ip_port, data, &length) != -1) {
191 { 180 if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) {
192 if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port))
193 {
194 //unhandled packet 181 //unhandled packet
195 printpacket(data, length, ip_port); 182 printpacket(data, length, ip_port);
196 } 183 } else {
197 else
198 {
199 printf("Received handled packet with length: %u\n", length); 184 printf("Received handled packet with length: %u\n", length);
200 } 185 }
201 } 186 }