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.c79
1 files changed, 42 insertions, 37 deletions
diff --git a/testing/DHT_test.c b/testing/DHT_test.c
index e194d06b..d14e1577 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]);
@@ -103,14 +114,14 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
103 printf("\n--------------------END-----------------------------\n\n\n"); 114 printf("\n--------------------END-----------------------------\n\n\n");
104} 115}
105 116
106//horrible function from one of my first C programs. 117//TODO: rewrite
107//only here because I was too lazy to write a proper one.
108unsigned char * hex_string_to_bin(char hex_string[]) 118unsigned char * hex_string_to_bin(char hex_string[])
109{ 119{
110 unsigned char * val = malloc(strlen(hex_string)); 120 size_t len = strlen(hex_string);
121 unsigned char * val = malloc(len);
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 < len)
114 { 125 {
115 sscanf(pos,"%2hhx",&val[i]); 126 sscanf(pos,"%2hhx",&val[i]);
116 pos+=2; 127 pos+=2;
@@ -130,8 +141,7 @@ int main(int argc, char *argv[])
130 new_keys(); 141 new_keys();
131 printf("OUR ID: "); 142 printf("OUR ID: ");
132 uint32_t i; 143 uint32_t i;
133 for(i = 0; i < 32; i++) 144 for(i = 0; i < 32; i++) {
134 {
135 if(self_public_key[i] < 16) 145 if(self_public_key[i] < 16)
136 printf("0"); 146 printf("0");
137 printf("%hhX",self_public_key[i]); 147 printf("%hhX",self_public_key[i]);
@@ -142,8 +152,8 @@ int main(int argc, char *argv[])
142 scanf("%s", temp_id); 152 scanf("%s", temp_id);
143 DHT_addfriend(hex_string_to_bin(temp_id)); 153 DHT_addfriend(hex_string_to_bin(temp_id));
144 154
145 //initialize networking 155 /* initialize networking */
146 //bind to ip 0.0.0.0:PORT 156 /* bind to ip 0.0.0.0:PORT */
147 IP ip; 157 IP ip;
148 ip.i = 0; 158 ip.i = 0;
149 init_networking(ip, PORT); 159 init_networking(ip, PORT);
@@ -152,10 +162,10 @@ int main(int argc, char *argv[])
152 perror("Initialization"); 162 perror("Initialization");
153 IP_Port bootstrap_ip_port; 163 IP_Port bootstrap_ip_port;
154 bootstrap_ip_port.port = htons(atoi(argv[2])); 164 bootstrap_ip_port.port = htons(atoi(argv[2]));
155 //bootstrap_ip_port.ip.c[0] = 127; 165 /* bootstrap_ip_port.ip.c[0] = 127;
156 //bootstrap_ip_port.ip.c[1] = 0; 166 * bootstrap_ip_port.ip.c[1] = 0;
157 //bootstrap_ip_port.ip.c[2] = 0; 167 * bootstrap_ip_port.ip.c[2] = 0;
158 //bootstrap_ip_port.ip.c[3] = 1; 168 * bootstrap_ip_port.ip.c[3] = 1; */
159 bootstrap_ip_port.ip.i = inet_addr(argv[1]); 169 bootstrap_ip_port.ip.i = inet_addr(argv[1]);
160 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); 170 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
161 171
@@ -163,20 +173,15 @@ int main(int argc, char *argv[])
163 uint8_t data[MAX_UDP_PACKET_SIZE]; 173 uint8_t data[MAX_UDP_PACKET_SIZE];
164 uint32_t length; 174 uint32_t length;
165 175
166 while(1) 176 while(1) {
167 {
168 177
169 doDHT(); 178 doDHT();
170 179
171 while(receivepacket(&ip_port, data, &length) != -1) 180 while(receivepacket(&ip_port, data, &length) != -1) {
172 { 181 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 182 //unhandled packet
176 printpacket(data, length, ip_port); 183 printpacket(data, length, ip_port);
177 } 184 } else {
178 else
179 {
180 printf("Received handled packet with length: %u\n", length); 185 printf("Received handled packet with length: %u\n", length);
181 } 186 }
182 } 187 }
@@ -187,4 +192,4 @@ int main(int argc, char *argv[])
187 192
188 shutdown_networking(); 193 shutdown_networking();
189 return 0; 194 return 0;
190} \ No newline at end of file 195}