summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
Diffstat (limited to 'testing')
-rw-r--r--testing/DHT_cryptosendfiletest.c119
-rw-r--r--testing/DHT_sendfiletest.c60
-rw-r--r--testing/DHT_test.c53
-rw-r--r--testing/Lossless_UDP_testclient.c72
-rw-r--r--testing/Lossless_UDP_testserver.c48
-rw-r--r--testing/Messenger_test.c19
-rw-r--r--testing/nTox.c13
-rw-r--r--testing/nTox.h1
8 files changed, 147 insertions, 238 deletions
diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c
index 6aaac677..b06ddea5 100644
--- a/testing/DHT_cryptosendfiletest.c
+++ b/testing/DHT_cryptosendfiletest.c
@@ -41,7 +41,7 @@
41 41
42#include <string.h> 42#include <string.h>
43 43
44//Sleep function (x = milliseconds) 44/* Sleep function (x = milliseconds) */
45#ifdef WIN32 45#ifdef WIN32
46 46
47#define c_sleep(x) Sleep(1*x) 47#define c_sleep(x) Sleep(1*x)
@@ -60,16 +60,14 @@ void printip(IP_Port ip_port)
60 printf("\nIP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port)); 60 printf("\nIP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port));
61} 61}
62 62
63 63/* horrible function from one of my first C programs.
64//horrible function from one of my first C programs. 64 *only here because I was too lazy to write a proper one. */
65//only here because I was too lazy to write a proper one.
66unsigned char * hex_string_to_bin(char hex_string[]) 65unsigned char * hex_string_to_bin(char hex_string[])
67{ 66{
68 unsigned char * val = malloc(strlen(hex_string)); 67 unsigned char * val = malloc(strlen(hex_string));
69 char * pos = hex_string; 68 char * pos = hex_string;
70 int i=0; 69 int i=0;
71 while(i < strlen(hex_string)) 70 while(i < strlen(hex_string)) {
72 {
73 sscanf(pos,"%2hhx",&val[i]); 71 sscanf(pos,"%2hhx",&val[i]);
74 pos+=2; 72 pos+=2;
75 i++; 73 i++;
@@ -88,8 +86,7 @@ int main(int argc, char *argv[])
88 new_keys(); 86 new_keys();
89 printf("OUR ID: "); 87 printf("OUR ID: ");
90 uint32_t i; 88 uint32_t i;
91 for(i = 0; i < 32; i++) 89 for(i = 0; i < 32; i++) {
92 {
93 if(self_public_key[i] < 16) 90 if(self_public_key[i] < 16)
94 printf("0"); 91 printf("0");
95 printf("%hhX",self_public_key[i]); 92 printf("%hhX",self_public_key[i]);
@@ -105,8 +102,7 @@ int main(int argc, char *argv[])
105 uint8_t friend_id[32]; 102 uint8_t friend_id[32];
106 memcpy(friend_id, hex_string_to_bin(temp_id), 32); 103 memcpy(friend_id, hex_string_to_bin(temp_id), 32);
107 104
108 105 /* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */
109 //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
110 106
111 107
112 DHT_addfriend(friend_id); 108 DHT_addfriend(friend_id);
@@ -118,15 +114,13 @@ int main(int argc, char *argv[])
118 int friendrequest = -1; 114 int friendrequest = -1;
119 uint8_t request_data[512]; 115 uint8_t request_data[512];
120 116
121 //initialize networking 117 /* initialize networking
122 //bind to ip 0.0.0.0:PORT 118 * bind to ip 0.0.0.0:PORT */
123 IP ip; 119 IP ip;
124 ip.i = 0; 120 ip.i = 0;
125 init_networking(ip, PORT); 121 init_networking(ip, PORT);
126 initNetCrypto(); 122 initNetCrypto();
127 123
128
129
130 perror("Initialization"); 124 perror("Initialization");
131 IP_Port bootstrap_ip_port; 125 IP_Port bootstrap_ip_port;
132 bootstrap_ip_port.port = htons(atoi(argv[2])); 126 bootstrap_ip_port.port = htons(atoi(argv[2]));
@@ -147,122 +141,99 @@ int main(int argc, char *argv[])
147 if ( file2==NULL ){return 1;} 141 if ( file2==NULL ){return 1;}
148 read1 = fread(buffer1, 1, 128, file1); 142 read1 = fread(buffer1, 1, 128, file1);
149 143
150 while(1) 144 while(1) {
151 { 145 while(receivepacket(&ip_port, data, &length) != -1) {
152 146 if(rand() % 3 != 1) { /* simulate packet loss */
153 while(receivepacket(&ip_port, data, &length) != -1) 147 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) {
154 { 148 /* if packet is not recognized */
155 if(rand() % 3 != 1)//simulate packet loss
156 {
157 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
158 {
159 //if packet is not recognized
160 printf("Received unhandled packet with length: %u\n", length); 149 printf("Received unhandled packet with length: %u\n", length);
161 } 150 } else {
162 else
163 {
164 printf("Received handled packet with length: %u\n", length); 151 printf("Received handled packet with length: %u\n", length);
165 } 152 }
166 } 153 }
167 } 154 }
168 friend_ip = DHT_getfriendip(friend_id); 155 friend_ip = DHT_getfriendip(friend_id);
169 if(friend_ip.ip.i != 0) 156 if(friend_ip.ip.i != 0) {
170 { 157 if(connection == -1 && friendrequest == -1) {
171 if(connection == -1 && friendrequest == -1)
172 {
173 printf("Sending friend request to peer:"); 158 printf("Sending friend request to peer:");
174 printip(friend_ip); 159 printip(friend_ip);
175 friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12); 160 friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12);
176 //connection = crypto_connect((uint8_t *)friend_id, friend_ip); 161 /* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */
177 //connection = new_connection(friend_ip); 162 /* connection = new_connection(friend_ip); */
178 } 163 }
179 if(check_friendrequest(friendrequest) == 1) 164 if(check_friendrequest(friendrequest) == 1) {
180 {
181 printf("Started connecting to friend:"); 165 printf("Started connecting to friend:");
182 connection = crypto_connect(friend_id, friend_ip); 166 connection = crypto_connect(friend_id, friend_ip);
183 } 167 }
184 } 168 }
185 if(inconnection == -1) 169 if(inconnection == -1) {
186 {
187 uint8_t secret_nonce[crypto_box_NONCEBYTES]; 170 uint8_t secret_nonce[crypto_box_NONCEBYTES];
188 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 171 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
189 uint8_t session_key[crypto_box_PUBLICKEYBYTES]; 172 uint8_t session_key[crypto_box_PUBLICKEYBYTES];
190 inconnection = crypto_inbound(public_key, secret_nonce, session_key); 173 inconnection = crypto_inbound(public_key, secret_nonce, session_key);
191 inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key); 174 inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key);
192 //inconnection = incoming_connection(); 175 /* inconnection = incoming_connection(); */
193 if(inconnection != -1) 176 if(inconnection != -1) {
194 {
195 printf("Someone connected to us:\n"); 177 printf("Someone connected to us:\n");
196 // printip(connection_ip(inconnection)); 178 /* printip(connection_ip(inconnection)); */
197 } 179 }
198 } 180 }
199 if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) 181 if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) {
200 {
201 printf("RECIEVED FRIEND REQUEST: %s\n", request_data); 182 printf("RECIEVED FRIEND REQUEST: %s\n", request_data);
202 } 183 }
203 184
204 //if someone connected to us write what he sends to a file 185 /* if someone connected to us write what he sends to a file
205 //also send him our file. 186 * also send him our file. */
206 if(inconnection != -1) 187 if(inconnection != -1) {
207 { 188 if(write_cryptpacket(inconnection, buffer1, read1)) {
208 if(write_cryptpacket(inconnection, buffer1, read1))
209 {
210 printf("Wrote data1.\n"); 189 printf("Wrote data1.\n");
211 read1 = fread(buffer1, 1, 128, file1); 190 read1 = fread(buffer1, 1, 128, file1);
212 } 191 }
213 read2 = read_cryptpacket(inconnection, buffer2); 192 read2 = read_cryptpacket(inconnection, buffer2);
214 if(read2 != 0) 193 if(read2 != 0) {
215 {
216 printf("Received data1.\n"); 194 printf("Received data1.\n");
217 if(!fwrite(buffer2, read2, 1, file2)) 195 if(!fwrite(buffer2, read2, 1, file2)) {
218 {
219 printf("file write error1\n"); 196 printf("file write error1\n");
220 } 197 }
221 if(read2 < 128) 198 if(read2 < 128) {
222 {
223 printf("Closed file1 %u\n", read2); 199 printf("Closed file1 %u\n", read2);
224 fclose(file2); 200 fclose(file2);
225 } 201 }
226 } 202 }
227 else if(is_cryptoconnected(inconnection) == 4)//if buffer is empty and the connection timed out. 203 /* if buffer is empty and the connection timed out. */
228 { 204 else if(is_cryptoconnected(inconnection) == 4) {
229 crypto_kill(inconnection); 205 crypto_kill(inconnection);
230 } 206 }
231 } 207 }
232 //if we are connected to a friend send him data from the file. 208 /* if we are connected to a friend send him data from the file.
233 //also put what he sends us in a file. 209 * also put what he sends us in a file. */
234 if(is_cryptoconnected(connection) >= 3) 210 if(is_cryptoconnected(connection) >= 3) {
235 { 211 if(write_cryptpacket(0, buffer1, read1)) {
236 if(write_cryptpacket(0, buffer1, read1))
237 {
238 printf("Wrote data2.\n"); 212 printf("Wrote data2.\n");
239 read1 = fread(buffer1, 1, 128, file1); 213 read1 = fread(buffer1, 1, 128, file1);
240 } 214 }
241 read2 = read_cryptpacket(0, buffer2); 215 read2 = read_cryptpacket(0, buffer2);
242 if(read2 != 0) 216 if(read2 != 0) {
243 {
244 printf("Received data2.\n"); 217 printf("Received data2.\n");
245 if(!fwrite(buffer2, read2, 1, file2)) 218 if(!fwrite(buffer2, read2, 1, file2)) {
246 {
247 printf("file write error2\n"); 219 printf("file write error2\n");
248 } 220 }
249 if(read2 < 128) 221 if(read2 < 128) {
250 {
251 printf("Closed file2 %u\n", read2); 222 printf("Closed file2 %u\n", read2);
252 fclose(file2); 223 fclose(file2);
253 } 224 }
254 } 225 }
255 else if(is_cryptoconnected(connection) == 4)//if buffer is empty and the connection timed out. 226 /* if buffer is empty and the connection timed out. */
256 { 227 else if(is_cryptoconnected(connection) == 4) {
257 crypto_kill(connection); 228 crypto_kill(connection);
258 } 229 }
259 } 230 }
260 doDHT(); 231 doDHT();
261 doLossless_UDP(); 232 doLossless_UDP();
262 doNetCrypto(); 233 doNetCrypto();
263 //print_clientlist(); 234 /*print_clientlist();
264 //print_friendlist(); 235 *print_friendlist();
265 //c_sleep(300); 236 *c_sleep(300); */
266 c_sleep(1); 237 c_sleep(1);
267 } 238 }
268 239
diff --git a/testing/DHT_sendfiletest.c b/testing/DHT_sendfiletest.c
index c9865843..5b5d6c9f 100644
--- a/testing/DHT_sendfiletest.c
+++ b/testing/DHT_sendfiletest.c
@@ -33,6 +33,7 @@
33 * along with Tox. If not, see <http://www.gnu.org/licenses/>. 33 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
34 * 34 *
35 */ 35 */
36
36#include "../core/network.h" 37#include "../core/network.h"
37#include "../core/DHT.h" 38#include "../core/DHT.h"
38#include "../core/Lossless_UDP.h" 39#include "../core/Lossless_UDP.h"
@@ -100,68 +101,53 @@ int main(int argc, char *argv[])
100 if ( file2==NULL ){return 1;} 101 if ( file2==NULL ){return 1;}
101 read1 = fread(buffer1, 1, 128, file1); 102 read1 = fread(buffer1, 1, 128, file1);
102 103
103 while(1) 104 while(1) {
104 {
105 105
106 while(receivepacket(&ip_port, data, &length) != -1) 106 while(receivepacket(&ip_port, data, &length) != -1) {
107 { 107 if(rand() % 3 != 1) { /* simulate packet loss */
108 if(rand() % 3 != 1)//simulate packet loss 108 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) {
109 { 109 /* if packet is not recognized */
110 if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
111 {
112 //if packet is not recognized
113 printf("Received unhandled packet with length: %u\n", length); 110 printf("Received unhandled packet with length: %u\n", length);
114 } 111 } else {
115 else
116 {
117 printf("Received handled packet with length: %u\n", length); 112 printf("Received handled packet with length: %u\n", length);
118 } 113 }
119 } 114 }
120 } 115 }
121 friend_ip = DHT_getfriendip((uint8_t *)argv[3]); 116 friend_ip = DHT_getfriendip((uint8_t *)argv[3]);
122 if(friend_ip.ip.i != 0) 117 if(friend_ip.ip.i != 0) {
123 { 118 if(connection == -1) {
124 if(connection == -1)
125 {
126 printf("Started connecting to friend:"); 119 printf("Started connecting to friend:");
127 printip(friend_ip); 120 printip(friend_ip);
128 connection = new_connection(friend_ip); 121 connection = new_connection(friend_ip);
129 } 122 }
130 } 123 }
131 if(inconnection == -1) 124 if(inconnection == -1) {
132 {
133 inconnection = incoming_connection(); 125 inconnection = incoming_connection();
134 if(inconnection != -1) 126 if(inconnection != -1) {
135 {
136 printf("Someone connected to us:"); 127 printf("Someone connected to us:");
137 printip(connection_ip(inconnection)); 128 printip(connection_ip(inconnection));
138 } 129 }
139 } 130 }
140 //if someone connected to us write what he sends to a file 131 /* if someone connected to us write what he sends to a file */
141 //also send him our file. 132 /* also send him our file. */
142 if(inconnection != -1) 133 if(inconnection != -1) {
143 { 134 if(write_packet(inconnection, buffer1, read1)) {
144 if(write_packet(inconnection, buffer1, read1))
145 {
146 printf("Wrote data.\n"); 135 printf("Wrote data.\n");
147 read1 = fread(buffer1, 1, 128, file1); 136 read1 = fread(buffer1, 1, 128, file1);
148 } 137 }
149 read2 = read_packet(inconnection, buffer2); 138 read2 = read_packet(inconnection, buffer2);
150 if(read2 != 0) 139 if(read2 != 0) {
151 {
152 printf("Received data.\n"); 140 printf("Received data.\n");
153 if(!fwrite(buffer2, read2, 1, file2)) 141 if(!fwrite(buffer2, read2, 1, file2)) {
154 {
155 printf("file write error\n"); 142 printf("file write error\n");
156 } 143 }
157 if(read2 < 128) 144 if(read2 < 128) {
158 {
159 fclose(file2); 145 fclose(file2);
160 } 146 }
161 } 147 }
162 } 148 }
163 //if we are connected to a friend send him data from the file. 149 /* if we are connected to a friend send him data from the file.
164 //also put what he sends us in a file. 150 * also put what he sends us in a file. */
165 if(is_connected(connection) == 3) 151 if(is_connected(connection) == 3)
166 { 152 {
167 if(write_packet(0, buffer1, read1)) 153 if(write_packet(0, buffer1, read1))
@@ -185,9 +171,9 @@ int main(int argc, char *argv[])
185 } 171 }
186 doDHT(); 172 doDHT();
187 doLossless_UDP(); 173 doLossless_UDP();
188 //print_clientlist(); 174 /* print_clientlist();
189 //print_friendlist(); 175 * print_friendlist();
190 //c_sleep(300); 176 * c_sleep(300); */
191 c_sleep(1); 177 c_sleep(1);
192 } 178 }
193 179
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 }
diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c
index 07ab319b..8f23528c 100644
--- a/testing/Lossless_UDP_testclient.c
+++ b/testing/Lossless_UDP_testclient.c
@@ -42,7 +42,6 @@
42 42
43#endif 43#endif
44 44
45
46#define PORT 33446 45#define PORT 33446
47 46
48void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) 47void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
@@ -50,8 +49,7 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
50 uint32_t i; 49 uint32_t i;
51 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); 50 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
52 printf("--------------------BEGIN-----------------------------\n"); 51 printf("--------------------BEGIN-----------------------------\n");
53 for(i = 0; i < length; i++) 52 for(i = 0; i < length; i++) {
54 {
55 if(data[i] < 16) 53 if(data[i] < 16)
56 printf("0"); 54 printf("0");
57 printf("%hhX",data[i]); 55 printf("%hhX",data[i]);
@@ -117,39 +115,34 @@ void printconnection(int connection_id)
117 115
118} 116}
119*/ 117*/
120//recieve packets and send them to the packethandler 118
121//run doLossless_UDP(); 119/*( recieve packets and send them to the packethandler */
120/*run doLossless_UDP(); */
122void Lossless_UDP() 121void Lossless_UDP()
123{ 122{
124 IP_Port ip_port; 123 IP_Port ip_port;
125 uint8_t data[MAX_UDP_PACKET_SIZE]; 124 uint8_t data[MAX_UDP_PACKET_SIZE];
126 uint32_t length; 125 uint32_t length;
127 while(receivepacket(&ip_port, data, &length) != -1) 126 while(receivepacket(&ip_port, data, &length) != -1) {
128 {
129 printf("packet with length: %u\n", length); 127 printf("packet with length: %u\n", length);
130 //if(rand() % 3 != 1)//add packet loss 128 /* if(rand() % 3 != 1)//add packet loss
131 // { 129 { */
132 if(LosslessUDP_handlepacket(data, length, ip_port)) 130 if(LosslessUDP_handlepacket(data, length, ip_port)) {
133 {
134 printpacket(data, length, ip_port); 131 printpacket(data, length, ip_port);
135 } 132 } else {
136 else
137 {
138 //printconnection(0); 133 //printconnection(0);
139 printf("Received handled packet with length: %u\n", length); 134 printf("Received handled packet with length: %u\n", length);
140 } 135 }
141 // } 136 /* } */
142 } 137 }
143 138
144 doLossless_UDP(); 139 doLossless_UDP();
145 140
146} 141}
147 142
148
149int main(int argc, char *argv[]) 143int main(int argc, char *argv[])
150{ 144{
151 if (argc < 4) 145 if (argc < 4) {
152 {
153 printf("usage: %s ip port filename\n", argv[0]); 146 printf("usage: %s ip port filename\n", argv[0]);
154 exit(0); 147 exit(0);
155 } 148 }
@@ -161,8 +154,8 @@ int main(int argc, char *argv[])
161 if ( file==NULL ){return 1;} 154 if ( file==NULL ){return 1;}
162 155
163 156
164 //initialize networking 157 /* initialize networking */
165 //bind to ip 0.0.0.0:PORT 158 /* bind to ip 0.0.0.0:PORT */
166 IP ip; 159 IP ip;
167 ip.i = 0; 160 ip.i = 0;
168 init_networking(ip, PORT); 161 init_networking(ip, PORT);
@@ -173,17 +166,14 @@ int main(int argc, char *argv[])
173 printip(serverip); 166 printip(serverip);
174 int connection = new_connection(serverip); 167 int connection = new_connection(serverip);
175 uint64_t timer = current_time(); 168 uint64_t timer = current_time();
176 while(1) 169 while(1) {
177 { 170 /* printconnection(connection); */
178 // printconnection(connection);
179 Lossless_UDP(); 171 Lossless_UDP();
180 if(is_connected(connection) == 3) 172 if(is_connected(connection) == 3) {
181 {
182 printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer)); 173 printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer));
183 break; 174 break;
184 } 175 }
185 if(is_connected(connection) == 0) 176 if(is_connected(connection) == 0) {
186 {
187 printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer)); 177 printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer));
188 return 1; 178 return 1;
189 } 179 }
@@ -192,38 +182,32 @@ int main(int argc, char *argv[])
192 timer = current_time(); 182 timer = current_time();
193 183
194 184
195 //read first part of file 185 /*read first part of file */
196 read = fread(buffer, 1, 512, file); 186 read = fread(buffer, 1, 512, file);
197 187
198 while(1) 188 while(1) {
199 { 189 /* printconnection(connection); */
200 //printconnection(connection);
201 Lossless_UDP(); 190 Lossless_UDP();
202 if(is_connected(connection) == 3) 191 if(is_connected(connection) == 3) {
203 {
204 192
205 if(write_packet(connection, buffer, read)) 193 if(write_packet(connection, buffer, read)) {
206 { 194 /* printf("Wrote data.\n"); */
207 //printf("Wrote data.\n");
208 read = fread(buffer, 1, 512, file); 195 read = fread(buffer, 1, 512, file);
209 196
210 } 197 }
211 //printf("%u\n", sendqueue(connection)); 198 /* printf("%u\n", sendqueue(connection)); */
212 if(sendqueue(connection) == 0) 199 if(sendqueue(connection) == 0) {
213 { 200 if(read == 0) {
214 if(read == 0)
215 {
216 printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer)); 201 printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer));
217 break; 202 break;
218 } 203 }
219 } 204 }
220 } 205 }
221 else 206 else {
222 {
223 printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer)); 207 printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
224 return 0; 208 return 0;
225 } 209 }
226 //c_sleep(1); 210 /* c_sleep(1); */
227 } 211 }
228 212
229 return 0; 213 return 0;
diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c
index 0ed214e9..a8097048 100644
--- a/testing/Lossless_UDP_testserver.c
+++ b/testing/Lossless_UDP_testserver.c
@@ -43,7 +43,6 @@
43 43
44#endif 44#endif
45 45
46
47#define PORT 33445 46#define PORT 33445
48 47
49void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) 48void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
@@ -51,14 +50,14 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
51 uint32_t i; 50 uint32_t i;
52 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); 51 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
53 printf("--------------------BEGIN-----------------------------\n"); 52 printf("--------------------BEGIN-----------------------------\n");
54 for(i = 0; i < length; i++) 53 for(i = 0; i < length; i++) {
55 {
56 if(data[i] < 16) 54 if(data[i] < 16)
57 printf("0"); 55 printf("0");
58 printf("%hhX",data[i]); 56 printf("%hhX",data[i]);
59 } 57 }
60 printf("\n--------------------END-----------------------------\n\n\n"); 58 printf("\n--------------------END-----------------------------\n\n\n");
61} 59}
60
62/* 61/*
63void printpackets(Data test) 62void printpackets(Data test)
64{ 63{
@@ -113,23 +112,20 @@ void printconnection(int connection_id)
113 112
114} 113}
115*/ 114*/
116//recieve packets and send them to the packethandler 115
117//run doLossless_UDP(); 116/* recieve packets and send them to the packethandler
117 * run doLossless_UDP(); */
118void Lossless_UDP() 118void Lossless_UDP()
119{ 119{
120 IP_Port ip_port; 120 IP_Port ip_port;
121 uint8_t data[MAX_UDP_PACKET_SIZE]; 121 uint8_t data[MAX_UDP_PACKET_SIZE];
122 uint32_t length; 122 uint32_t length;
123 while(receivepacket(&ip_port, data, &length) != -1) 123 while(receivepacket(&ip_port, data, &length) != -1) {
124 {
125 //if(rand() % 3 != 1)//add packet loss 124 //if(rand() % 3 != 1)//add packet loss
126 //{ 125 //{
127 if(LosslessUDP_handlepacket(data, length, ip_port)) 126 if(LosslessUDP_handlepacket(data, length, ip_port)) {
128 {
129 printpacket(data, length, ip_port); 127 printpacket(data, length, ip_port);
130 } 128 } else {
131 else
132 {
133 //printconnection(0); 129 //printconnection(0);
134 printf("Received handled packet with length: %u\n", length); 130 printf("Received handled packet with length: %u\n", length);
135 } 131 }
@@ -137,14 +133,12 @@ void Lossless_UDP()
137 } 133 }
138 134
139 doLossless_UDP(); 135 doLossless_UDP();
140
141} 136}
142 137
143 138
144int main(int argc, char *argv[]) 139int main(int argc, char *argv[])
145{ 140{
146 if (argc < 2) 141 if (argc < 2) {
147 {
148 printf("usage: %s filename\n", argv[0]); 142 printf("usage: %s filename\n", argv[0]);
149 exit(0); 143 exit(0);
150 } 144 }
@@ -167,14 +161,11 @@ int main(int argc, char *argv[])
167 uint64_t timer = current_time(); 161 uint64_t timer = current_time();
168 162
169 163
170 while(1) 164 while(1) {
171 {
172 Lossless_UDP(); 165 Lossless_UDP();
173 connection = incoming_connection(); 166 connection = incoming_connection();
174 if(connection != -1) 167 if(connection != -1) {
175 { 168 if(is_connected(connection) == 2) {
176 if(is_connected(connection) == 2)
177 {
178 printf("Recieved the connection.\n"); 169 printf("Recieved the connection.\n");
179 170
180 } 171 }
@@ -185,25 +176,20 @@ int main(int argc, char *argv[])
185 176
186 timer = current_time(); 177 timer = current_time();
187 178
188 while(1) 179 while(1) {
189 {
190 //printconnection(0); 180 //printconnection(0);
191 Lossless_UDP(); 181 Lossless_UDP();
192 if(is_connected(connection) >= 2) 182 if(is_connected(connection) >= 2) {
193 {
194 kill_connection_in(connection, 3000000); 183 kill_connection_in(connection, 3000000);
195 read = read_packet(connection, buffer); 184 read = read_packet(connection, buffer);
196 if(read != 0) 185 if(read != 0) {
197 {
198 // printf("Recieved data.\n"); 186 // printf("Recieved data.\n");
199 if(!fwrite(buffer, read, 1, file)) 187 if(!fwrite(buffer, read, 1, file)) {
200 {
201 printf("file write error\n"); 188 printf("file write error\n");
202 } 189 }
203 } 190 }
204 } 191 }
205 if(is_connected(connection) == 4) 192 if(is_connected(connection) == 4) {
206 {
207 printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer)); 193 printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
208 fclose(file); 194 fclose(file);
209 return 1; 195 return 1;
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c
index e5da16e9..19ab4cc3 100644
--- a/testing/Messenger_test.c
+++ b/testing/Messenger_test.c
@@ -104,15 +104,12 @@ int main(int argc, char *argv[])
104 exit(0); 104 exit(0);
105 } 105 }
106 initMessenger(); 106 initMessenger();
107 if(argc > 3) 107 if(argc > 3) {
108 {
109 IP_Port bootstrap_ip_port; 108 IP_Port bootstrap_ip_port;
110 bootstrap_ip_port.port = htons(atoi(argv[2])); 109 bootstrap_ip_port.port = htons(atoi(argv[2]));
111 bootstrap_ip_port.ip.i = inet_addr(argv[1]); 110 bootstrap_ip_port.ip.i = inet_addr(argv[1]);
112 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); 111 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
113 } 112 } else {
114 else
115 {
116 FILE *file = fopen(argv[1], "rb"); 113 FILE *file = fopen(argv[1], "rb");
117 if ( file==NULL ){return 1;} 114 if ( file==NULL ){return 1;}
118 int read; 115 int read;
@@ -127,8 +124,7 @@ int main(int argc, char *argv[])
127 124
128 printf("OUR ID: "); 125 printf("OUR ID: ");
129 uint32_t i; 126 uint32_t i;
130 for(i = 0; i < 32; i++) 127 for(i = 0; i < 32; i++) {
131 {
132 if(self_public_key[i] < 16) 128 if(self_public_key[i] < 16)
133 printf("0"); 129 printf("0");
134 printf("%hhX",self_public_key[i]); 130 printf("%hhX",self_public_key[i]);
@@ -138,16 +134,14 @@ int main(int argc, char *argv[])
138 134
139 char temp_id[128]; 135 char temp_id[128];
140 printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); 136 printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n");
141 if(scanf("%s", temp_id) != 1) 137 if(scanf("%s", temp_id) != 1) {
142 {
143 return 1; 138 return 1;
144 } 139 }
145 int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); 140 int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
146 141
147 perror("Initialization"); 142 perror("Initialization");
148 143
149 while(1) 144 while(1) {
150 {
151 uint8_t name[128]; 145 uint8_t name[128];
152 getname(num, name); 146 getname(num, name);
153 printf("%s\n", name); 147 printf("%s\n", name);
@@ -162,6 +156,5 @@ int main(int argc, char *argv[])
162 fwrite(buffer, 1, Messenger_size(), file); 156 fwrite(buffer, 1, Messenger_size(), file);
163 free(buffer); 157 free(buffer);
164 fclose(file); 158 fclose(file);
165 } 159 }
166
167} 160}
diff --git a/testing/nTox.c b/testing/nTox.c
index 87a87007..caa7d45c 100644
--- a/testing/nTox.c
+++ b/testing/nTox.c
@@ -51,8 +51,7 @@ unsigned char * hex_string_to_bin(char hex_string[])
51 unsigned char * val = malloc(strlen(hex_string)); 51 unsigned char * val = malloc(strlen(hex_string));
52 char * pos = hex_string; 52 char * pos = hex_string;
53 int i=0; 53 int i=0;
54 while(i < strlen(hex_string)) 54 while(i < strlen(hex_string)) {
55 {
56 sscanf(pos,"%2hhx",&val[i]); 55 sscanf(pos,"%2hhx",&val[i]);
57 pos+=2; 56 pos+=2;
58 i++; 57 i++;
@@ -190,6 +189,7 @@ void do_refresh()
190 clrtoeol(); 189 clrtoeol();
191 refresh(); 190 refresh();
192} 191}
192
193void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) 193void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
194{ 194{
195 new_lines("[i] received friend request"); 195 new_lines("[i] received friend request");
@@ -205,6 +205,7 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
205 new_lines(numchar); 205 new_lines(numchar);
206 } 206 }
207} 207}
208
208void print_message(int friendnumber, uint8_t * string, uint16_t length) 209void print_message(int friendnumber, uint8_t * string, uint16_t length)
209{ 210{
210 char name[MAX_NAME_LENGTH]; 211 char name[MAX_NAME_LENGTH];
@@ -220,6 +221,7 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length)
220 sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // someone please fix this 221 sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // someone please fix this
221 new_lines(msg); 222 new_lines(msg);
222} 223}
224
223void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) { 225void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
224 char name[MAX_NAME_LENGTH]; 226 char name[MAX_NAME_LENGTH];
225 getname(friendnumber, (uint8_t*)name); 227 getname(friendnumber, (uint8_t*)name);
@@ -227,6 +229,7 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
227 sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); 229 sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string);
228 new_lines(msg); 230 new_lines(msg);
229} 231}
232
230void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { 233void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
231 char name[MAX_NAME_LENGTH]; 234 char name[MAX_NAME_LENGTH];
232 getname(friendnumber, (uint8_t*)name); 235 getname(friendnumber, (uint8_t*)name);
@@ -234,6 +237,7 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
234 sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); 237 sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string);
235 new_lines(msg); 238 new_lines(msg);
236} 239}
240
237void load_key(){ 241void load_key(){
238 FILE *data_file = NULL; 242 FILE *data_file = NULL;
239 if ((data_file = fopen("data","r"))) { 243 if ((data_file = fopen("data","r"))) {
@@ -260,6 +264,7 @@ void load_key(){
260 } 264 }
261 fclose(data_file); 265 fclose(data_file);
262} 266}
267
263int main(int argc, char *argv[]) 268int main(int argc, char *argv[])
264{ 269{
265 if (argc < 4) { 270 if (argc < 4) {
@@ -317,9 +322,7 @@ int main(int argc, char *argv[])
317 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); 322 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
318 nodelay(stdscr, TRUE); 323 nodelay(stdscr, TRUE);
319 while(true) { 324 while(true) {
320 325 if (on == 0 && DHT_isconnected()) {
321 if (on == 0 && DHT_isconnected())
322 {
323 new_lines("[i] connected to DHT\n[i] define username with /n"); 326 new_lines("[i] connected to DHT\n[i] define username with /n");
324 on = 1; 327 on = 1;
325 } 328 }
diff --git a/testing/nTox.h b/testing/nTox.h
index 5a86830f..9b69d959 100644
--- a/testing/nTox.h
+++ b/testing/nTox.h
@@ -20,6 +20,7 @@
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>. 20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 * 21 *
22 */ 22 */
23
23#ifndef NTOX_H 24#ifndef NTOX_H
24#define NTOX_H 25#define NTOX_H
25 26