ffi-tox 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/ProjectTox-Core/CMakeLists.txt +28 -0
- data/ProjectTox-Core/COPYING +674 -0
- data/ProjectTox-Core/INSTALL.md +91 -0
- data/ProjectTox-Core/README.md +54 -0
- data/ProjectTox-Core/core/CMakeLists.txt +17 -0
- data/ProjectTox-Core/core/DHT.c +1104 -0
- data/ProjectTox-Core/core/DHT.h +111 -0
- data/ProjectTox-Core/core/LAN_discovery.c +79 -0
- data/ProjectTox-Core/core/LAN_discovery.h +50 -0
- data/ProjectTox-Core/core/Lossless_UDP.c +749 -0
- data/ProjectTox-Core/core/Lossless_UDP.h +106 -0
- data/ProjectTox-Core/core/Messenger.c +581 -0
- data/ProjectTox-Core/core/Messenger.h +157 -0
- data/ProjectTox-Core/core/friend_requests.c +131 -0
- data/ProjectTox-Core/core/friend_requests.h +51 -0
- data/ProjectTox-Core/core/net_crypto.c +564 -0
- data/ProjectTox-Core/core/net_crypto.h +134 -0
- data/ProjectTox-Core/core/network.c +188 -0
- data/ProjectTox-Core/core/network.h +134 -0
- data/ProjectTox-Core/other/CMakeLists.txt +9 -0
- data/ProjectTox-Core/other/DHT_bootstrap.c +139 -0
- data/ProjectTox-Core/testing/CMakeLists.txt +18 -0
- data/ProjectTox-Core/testing/DHT_cryptosendfiletest.c +228 -0
- data/ProjectTox-Core/testing/DHT_sendfiletest.c +176 -0
- data/ProjectTox-Core/testing/DHT_test.c +182 -0
- data/ProjectTox-Core/testing/Lossless_UDP_testclient.c +214 -0
- data/ProjectTox-Core/testing/Lossless_UDP_testserver.c +201 -0
- data/ProjectTox-Core/testing/Messenger_test.c +145 -0
- data/ProjectTox-Core/testing/misc_tools.c +40 -0
- data/ProjectTox-Core/testing/misc_tools.h +29 -0
- data/ProjectTox-Core/testing/nTox.c +381 -0
- data/ProjectTox-Core/testing/nTox.h +50 -0
- data/ProjectTox-Core/testing/nTox_win32.c +323 -0
- data/ProjectTox-Core/testing/nTox_win32.h +31 -0
- data/ProjectTox-Core/testing/rect.py +45 -0
- data/ext/ffi-tox/extconf.rb +16 -0
- data/interfaces/libtox.i +18 -0
- data/lib/ffi-tox/libtox.rb +37 -0
- data/lib/ffi-tox.rb +1 -0
- metadata +116 -0
@@ -0,0 +1,228 @@
|
|
1
|
+
/* DHT cryptosendfiletest
|
2
|
+
*
|
3
|
+
* This program sends or recieves a friend request.
|
4
|
+
*
|
5
|
+
* it also sends the encrypted data from a file to another client.
|
6
|
+
* Receives the file data that that client sends us.
|
7
|
+
*
|
8
|
+
* NOTE: this program simulates 33% packet loss.
|
9
|
+
*
|
10
|
+
* This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/DHT.c ../nacl/build/$HOSTNAME/lib/amd64/* DHT_cryptosendfiletest.c
|
11
|
+
*
|
12
|
+
*
|
13
|
+
* Command line arguments are the ip and port of a node (for bootstrapping).
|
14
|
+
*
|
15
|
+
* Saves all received data to: received.txt
|
16
|
+
*
|
17
|
+
* EX: ./test 127.0.0.1 33445 filename.txt
|
18
|
+
*
|
19
|
+
* Copyright (C) 2013 Tox project All Rights Reserved.
|
20
|
+
*
|
21
|
+
* This file is part of Tox.
|
22
|
+
*
|
23
|
+
* Tox is free software: you can redistribute it and/or modify
|
24
|
+
* it under the terms of the GNU General Public License as published by
|
25
|
+
* the Free Software Foundation, either version 3 of the License, or
|
26
|
+
* (at your option) any later version.
|
27
|
+
*
|
28
|
+
* Tox is distributed in the hope that it will be useful,
|
29
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
30
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
31
|
+
* GNU General Public License for more details.
|
32
|
+
*
|
33
|
+
* You should have received a copy of the GNU General Public License
|
34
|
+
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
35
|
+
*
|
36
|
+
*/
|
37
|
+
|
38
|
+
#include "../core/network.h"
|
39
|
+
#include "../core/DHT.h"
|
40
|
+
#include "../core/net_crypto.h"
|
41
|
+
#include "misc_tools.h"
|
42
|
+
|
43
|
+
#include <string.h>
|
44
|
+
|
45
|
+
/* Sleep function (x = milliseconds) */
|
46
|
+
#ifdef WIN32
|
47
|
+
|
48
|
+
#define c_sleep(x) Sleep(1*x)
|
49
|
+
|
50
|
+
#else
|
51
|
+
#include <unistd.h>
|
52
|
+
#include <arpa/inet.h>
|
53
|
+
#define c_sleep(x) usleep(1000*x)
|
54
|
+
|
55
|
+
#endif
|
56
|
+
|
57
|
+
#define PORT 33445
|
58
|
+
|
59
|
+
void printip(IP_Port ip_port)
|
60
|
+
{
|
61
|
+
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));
|
62
|
+
}
|
63
|
+
|
64
|
+
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
|
65
|
+
|
66
|
+
int main(int argc, char *argv[])
|
67
|
+
{
|
68
|
+
if (argc < 4) {
|
69
|
+
printf("usage %s ip port filename(of file to send)\n", argv[0]);
|
70
|
+
exit(0);
|
71
|
+
}
|
72
|
+
new_keys();
|
73
|
+
printf("OUR ID: ");
|
74
|
+
uint32_t i;
|
75
|
+
for(i = 0; i < 32; i++) {
|
76
|
+
if(self_public_key[i] < 16)
|
77
|
+
printf("0");
|
78
|
+
printf("%hhX",self_public_key[i]);
|
79
|
+
}
|
80
|
+
printf("\n");
|
81
|
+
|
82
|
+
memcpy(self_client_id, self_public_key, 32);
|
83
|
+
|
84
|
+
char temp_id[128];
|
85
|
+
printf("Enter the client_id of the friend to connect to (32 bytes HEX format):\n");
|
86
|
+
scanf("%s", temp_id);
|
87
|
+
|
88
|
+
uint8_t friend_id[32];
|
89
|
+
memcpy(friend_id, hex_string_to_bin(temp_id), 32);
|
90
|
+
|
91
|
+
/* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */
|
92
|
+
|
93
|
+
|
94
|
+
DHT_addfriend(friend_id);
|
95
|
+
IP_Port friend_ip;
|
96
|
+
int connection = -1;
|
97
|
+
int inconnection = -1;
|
98
|
+
|
99
|
+
uint8_t acceptedfriend_public_key[crypto_box_PUBLICKEYBYTES];
|
100
|
+
int friendrequest = -1;
|
101
|
+
uint8_t request_data[512];
|
102
|
+
|
103
|
+
/* initialize networking
|
104
|
+
* bind to ip 0.0.0.0:PORT */
|
105
|
+
IP ip;
|
106
|
+
ip.i = 0;
|
107
|
+
init_networking(ip, PORT);
|
108
|
+
initNetCrypto();
|
109
|
+
|
110
|
+
perror("Initialization");
|
111
|
+
IP_Port bootstrap_ip_port;
|
112
|
+
bootstrap_ip_port.port = htons(atoi(argv[2]));
|
113
|
+
bootstrap_ip_port.ip.i = inet_addr(argv[1]);
|
114
|
+
DHT_bootstrap(bootstrap_ip_port);
|
115
|
+
|
116
|
+
IP_Port ip_port;
|
117
|
+
uint8_t data[MAX_UDP_PACKET_SIZE];
|
118
|
+
uint32_t length;
|
119
|
+
|
120
|
+
uint8_t buffer1[128];
|
121
|
+
int read1 = 0;
|
122
|
+
uint8_t buffer2[128];
|
123
|
+
int read2 = 0;
|
124
|
+
FILE *file1 = fopen(argv[3], "rb");
|
125
|
+
if ( file1==NULL ){printf("Error opening file.\n");return 1;}
|
126
|
+
FILE *file2 = fopen("received.txt", "wb");
|
127
|
+
if ( file2==NULL ){return 1;}
|
128
|
+
read1 = fread(buffer1, 1, 128, file1);
|
129
|
+
|
130
|
+
while(1) {
|
131
|
+
while(receivepacket(&ip_port, data, &length) != -1) {
|
132
|
+
if(rand() % 3 != 1) { /* simulate packet loss */
|
133
|
+
if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) {
|
134
|
+
/* if packet is not recognized */
|
135
|
+
printf("Received unhandled packet with length: %u\n", length);
|
136
|
+
} else {
|
137
|
+
printf("Received handled packet with length: %u\n", length);
|
138
|
+
}
|
139
|
+
}
|
140
|
+
}
|
141
|
+
friend_ip = DHT_getfriendip(friend_id);
|
142
|
+
if(friend_ip.ip.i != 0) {
|
143
|
+
if(connection == -1 && friendrequest == -1) {
|
144
|
+
printf("Sending friend request to peer:");
|
145
|
+
printip(friend_ip);
|
146
|
+
friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12);
|
147
|
+
/* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */
|
148
|
+
/* connection = new_connection(friend_ip); */
|
149
|
+
}
|
150
|
+
if(check_friendrequest(friendrequest) == 1) {
|
151
|
+
printf("Started connecting to friend:");
|
152
|
+
connection = crypto_connect(friend_id, friend_ip);
|
153
|
+
}
|
154
|
+
}
|
155
|
+
if(inconnection == -1) {
|
156
|
+
uint8_t secret_nonce[crypto_box_NONCEBYTES];
|
157
|
+
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
158
|
+
uint8_t session_key[crypto_box_PUBLICKEYBYTES];
|
159
|
+
inconnection = crypto_inbound(public_key, secret_nonce, session_key);
|
160
|
+
inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key);
|
161
|
+
/* inconnection = incoming_connection(); */
|
162
|
+
if(inconnection != -1) {
|
163
|
+
printf("Someone connected to us:\n");
|
164
|
+
/* printip(connection_ip(inconnection)); */
|
165
|
+
}
|
166
|
+
}
|
167
|
+
if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) {
|
168
|
+
printf("RECIEVED FRIEND REQUEST: %s\n", request_data);
|
169
|
+
}
|
170
|
+
|
171
|
+
/* if someone connected to us write what he sends to a file
|
172
|
+
* also send him our file. */
|
173
|
+
if(inconnection != -1) {
|
174
|
+
if(write_cryptpacket(inconnection, buffer1, read1)) {
|
175
|
+
printf("Wrote data1.\n");
|
176
|
+
read1 = fread(buffer1, 1, 128, file1);
|
177
|
+
}
|
178
|
+
read2 = read_cryptpacket(inconnection, buffer2);
|
179
|
+
if(read2 != 0) {
|
180
|
+
printf("Received data1.\n");
|
181
|
+
if(!fwrite(buffer2, read2, 1, file2)) {
|
182
|
+
printf("file write error1\n");
|
183
|
+
}
|
184
|
+
if(read2 < 128) {
|
185
|
+
printf("Closed file1 %u\n", read2);
|
186
|
+
fclose(file2);
|
187
|
+
}
|
188
|
+
}
|
189
|
+
/* if buffer is empty and the connection timed out. */
|
190
|
+
else if(is_cryptoconnected(inconnection) == 4) {
|
191
|
+
crypto_kill(inconnection);
|
192
|
+
}
|
193
|
+
}
|
194
|
+
/* if we are connected to a friend send him data from the file.
|
195
|
+
* also put what he sends us in a file. */
|
196
|
+
if(is_cryptoconnected(connection) >= 3) {
|
197
|
+
if(write_cryptpacket(0, buffer1, read1)) {
|
198
|
+
printf("Wrote data2.\n");
|
199
|
+
read1 = fread(buffer1, 1, 128, file1);
|
200
|
+
}
|
201
|
+
read2 = read_cryptpacket(0, buffer2);
|
202
|
+
if(read2 != 0) {
|
203
|
+
printf("Received data2.\n");
|
204
|
+
if(!fwrite(buffer2, read2, 1, file2)) {
|
205
|
+
printf("file write error2\n");
|
206
|
+
}
|
207
|
+
if(read2 < 128) {
|
208
|
+
printf("Closed file2 %u\n", read2);
|
209
|
+
fclose(file2);
|
210
|
+
}
|
211
|
+
}
|
212
|
+
/* if buffer is empty and the connection timed out. */
|
213
|
+
else if(is_cryptoconnected(connection) == 4) {
|
214
|
+
crypto_kill(connection);
|
215
|
+
}
|
216
|
+
}
|
217
|
+
doDHT();
|
218
|
+
doLossless_UDP();
|
219
|
+
doNetCrypto();
|
220
|
+
/*print_clientlist();
|
221
|
+
*print_friendlist();
|
222
|
+
*c_sleep(300); */
|
223
|
+
c_sleep(1);
|
224
|
+
}
|
225
|
+
|
226
|
+
shutdown_networking();
|
227
|
+
return 0;
|
228
|
+
}
|
@@ -0,0 +1,176 @@
|
|
1
|
+
/* DHT sendfiletest
|
2
|
+
*
|
3
|
+
* Sends the data from a file to another client.
|
4
|
+
* Receives the file data that that client sends us.
|
5
|
+
*
|
6
|
+
* NOTE: this program simulates 33% packet loss.
|
7
|
+
*
|
8
|
+
* Compile with: gcc -O2 -Wall -o test ../core/DHT.c ../core/network.c ../core/Lossless_UDP.c DHT_sendfiletest.c
|
9
|
+
*
|
10
|
+
* Command line arguments are the ip and port of a node (for bootstrapping), the
|
11
|
+
* client_id (32 bytes) of the friend you want to send the data in filename to and
|
12
|
+
* the client_id this node will take.
|
13
|
+
*
|
14
|
+
* Saves all received data to: received.txt
|
15
|
+
*
|
16
|
+
* EX: ./test 127.0.0.1 33445 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef filename.txt ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeg
|
17
|
+
*
|
18
|
+
* Copyright (C) 2013 Tox project All Rights Reserved.
|
19
|
+
*
|
20
|
+
* This file is part of Tox.
|
21
|
+
*
|
22
|
+
* Tox is free software: you can redistribute it and/or modify
|
23
|
+
* it under the terms of the GNU General Public License as published by
|
24
|
+
* the Free Software Foundation, either version 3 of the License, or
|
25
|
+
* (at your option) any later version.
|
26
|
+
*
|
27
|
+
* Tox is distributed in the hope that it will be useful,
|
28
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
29
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
30
|
+
* GNU General Public License for more details.
|
31
|
+
*
|
32
|
+
* You should have received a copy of the GNU General Public License
|
33
|
+
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
34
|
+
*
|
35
|
+
*/
|
36
|
+
|
37
|
+
#include "../core/network.h"
|
38
|
+
#include "../core/DHT.h"
|
39
|
+
#include "../core/Lossless_UDP.h"
|
40
|
+
|
41
|
+
#include <string.h>
|
42
|
+
|
43
|
+
//Sleep function (x = milliseconds)
|
44
|
+
#ifdef WIN32
|
45
|
+
|
46
|
+
#define c_sleep(x) Sleep(1*x)
|
47
|
+
|
48
|
+
#else
|
49
|
+
#include <unistd.h>
|
50
|
+
#include <arpa/inet.h>
|
51
|
+
#define c_sleep(x) usleep(1000*x)
|
52
|
+
|
53
|
+
#endif
|
54
|
+
|
55
|
+
#define PORT 33445
|
56
|
+
|
57
|
+
void printip(IP_Port ip_port)
|
58
|
+
{
|
59
|
+
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
|
+
}
|
61
|
+
|
62
|
+
int main(int argc, char *argv[])
|
63
|
+
{
|
64
|
+
//memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
|
65
|
+
|
66
|
+
if (argc < 6) {
|
67
|
+
printf("usage %s ip port client_id(of friend to find ip_port of) filename(of file to send) client_id(ours)\n", argv[0]);
|
68
|
+
exit(0);
|
69
|
+
}
|
70
|
+
DHT_addfriend((uint8_t *)argv[3]);
|
71
|
+
IP_Port friend_ip;
|
72
|
+
int connection = -1;
|
73
|
+
int inconnection = -1;
|
74
|
+
|
75
|
+
//initialize networking
|
76
|
+
//bind to ip 0.0.0.0:PORT
|
77
|
+
IP ip;
|
78
|
+
ip.i = 0;
|
79
|
+
init_networking(ip, PORT);
|
80
|
+
|
81
|
+
memcpy(self_client_id, argv[5], 32);
|
82
|
+
|
83
|
+
|
84
|
+
perror("Initialization");
|
85
|
+
IP_Port bootstrap_ip_port;
|
86
|
+
bootstrap_ip_port.port = htons(atoi(argv[2]));
|
87
|
+
bootstrap_ip_port.ip.i = inet_addr(argv[1]);
|
88
|
+
DHT_bootstrap(bootstrap_ip_port);
|
89
|
+
|
90
|
+
IP_Port ip_port;
|
91
|
+
uint8_t data[MAX_UDP_PACKET_SIZE];
|
92
|
+
uint32_t length;
|
93
|
+
|
94
|
+
uint8_t buffer1[128];
|
95
|
+
int read1 = 0;
|
96
|
+
uint8_t buffer2[128];
|
97
|
+
int read2 = 0;
|
98
|
+
FILE *file1 = fopen(argv[4], "rb");
|
99
|
+
if (file1 == NULL) {
|
100
|
+
printf("Error opening file.\n");
|
101
|
+
return 1;
|
102
|
+
}
|
103
|
+
FILE *file2 = fopen("received.txt", "wb");
|
104
|
+
if (file2 == NULL)
|
105
|
+
return 1;
|
106
|
+
|
107
|
+
read1 = fread(buffer1, 1, 128, file1);
|
108
|
+
|
109
|
+
while (1) {
|
110
|
+
while (receivepacket(&ip_port, data, &length) != -1) {
|
111
|
+
if (rand() % 3 != 1) { /* simulate packet loss */
|
112
|
+
if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port))
|
113
|
+
printf("Received unhandled packet with length: %u\n", length); /* if packet is not recognized */
|
114
|
+
else
|
115
|
+
printf("Received handled packet with length: %u\n", length);
|
116
|
+
}
|
117
|
+
}
|
118
|
+
friend_ip = DHT_getfriendip((uint8_t *)argv[3]);
|
119
|
+
if (friend_ip.ip.i != 0) {
|
120
|
+
if (connection == -1) {
|
121
|
+
printf("Started connecting to friend:");
|
122
|
+
printip(friend_ip);
|
123
|
+
connection = new_connection(friend_ip);
|
124
|
+
}
|
125
|
+
}
|
126
|
+
if (inconnection == -1) {
|
127
|
+
inconnection = incoming_connection();
|
128
|
+
if (inconnection != -1) {
|
129
|
+
printf("Someone connected to us:");
|
130
|
+
printip(connection_ip(inconnection));
|
131
|
+
}
|
132
|
+
}
|
133
|
+
/* if someone connected to us write what he sends to a file */
|
134
|
+
/* also send him our file. */
|
135
|
+
if (inconnection != -1) {
|
136
|
+
if (write_packet(inconnection, buffer1, read1)) {
|
137
|
+
printf("Wrote data.\n");
|
138
|
+
read1 = fread(buffer1, 1, 128, file1);
|
139
|
+
}
|
140
|
+
read2 = read_packet(inconnection, buffer2);
|
141
|
+
if (read2 != 0) {
|
142
|
+
printf("Received data.\n");
|
143
|
+
if (!fwrite(buffer2, read2, 1, file2))
|
144
|
+
printf("file write error\n");
|
145
|
+
if (read2 < 128) {
|
146
|
+
fclose(file2);
|
147
|
+
}
|
148
|
+
}
|
149
|
+
}
|
150
|
+
/* if we are connected to a friend send him data from the file.
|
151
|
+
* also put what he sends us in a file. */
|
152
|
+
if (is_connected(connection) == 3) {
|
153
|
+
if (write_packet(0, buffer1, read1)) {
|
154
|
+
printf("Wrote data.\n");
|
155
|
+
read1 = fread(buffer1, 1, 128, file1);
|
156
|
+
}
|
157
|
+
read2 = read_packet(0, buffer2);
|
158
|
+
if (read2 != 0) {
|
159
|
+
printf("Received data.\n");
|
160
|
+
if(!fwrite(buffer2, read2, 1, file2))
|
161
|
+
printf("file write error\n");
|
162
|
+
if(read2 < 128)
|
163
|
+
fclose(file2);
|
164
|
+
}
|
165
|
+
}
|
166
|
+
doDHT();
|
167
|
+
doLossless_UDP();
|
168
|
+
/* print_clientlist();
|
169
|
+
* print_friendlist();
|
170
|
+
* c_sleep(300); */
|
171
|
+
c_sleep(1);
|
172
|
+
}
|
173
|
+
|
174
|
+
shutdown_networking();
|
175
|
+
return 0;
|
176
|
+
}
|
@@ -0,0 +1,182 @@
|
|
1
|
+
/* DHT test
|
2
|
+
* A file with a main that runs our DHT for testing.
|
3
|
+
*
|
4
|
+
* Compile with: gcc -O2 -Wall -D VANILLA_NACL -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} DHT_test.c
|
5
|
+
*
|
6
|
+
* Command line arguments are the ip, port and public key of a node.
|
7
|
+
* EX: ./test 127.0.0.1 33445 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
8
|
+
*
|
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
|
+
*
|
28
|
+
*/
|
29
|
+
|
30
|
+
//#include "../core/network.h"
|
31
|
+
#include "../core/DHT.c"
|
32
|
+
#include "../core/friend_requests.c"
|
33
|
+
#include "misc_tools.h"
|
34
|
+
|
35
|
+
#include <string.h>
|
36
|
+
|
37
|
+
//Sleep function (x = milliseconds)
|
38
|
+
#ifdef WIN32
|
39
|
+
|
40
|
+
#define c_sleep(x) Sleep(1*x)
|
41
|
+
|
42
|
+
#else
|
43
|
+
#include <unistd.h>
|
44
|
+
#include <arpa/inet.h>
|
45
|
+
#define c_sleep(x) usleep(1000*x)
|
46
|
+
|
47
|
+
#endif
|
48
|
+
|
49
|
+
#define PORT 33445
|
50
|
+
|
51
|
+
void print_clientlist()
|
52
|
+
{
|
53
|
+
uint32_t i, j;
|
54
|
+
IP_Port p_ip;
|
55
|
+
printf("___________________CLOSE________________________________\n");
|
56
|
+
for(i = 0; i < 4; i++) {
|
57
|
+
printf("ClientID: ");
|
58
|
+
for(j = 0; j < 32; j++) {
|
59
|
+
printf("%c", close_clientlist[i].client_id[j]);
|
60
|
+
}
|
61
|
+
p_ip = close_clientlist[i].ip_port;
|
62
|
+
printf("\nIP: %u.%u.%u.%u Port: %u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));
|
63
|
+
printf("\nTimestamp: %u", close_clientlist[i].timestamp);
|
64
|
+
printf("\nLast pinged: %u\n", close_clientlist[i].last_pinged);
|
65
|
+
p_ip = close_clientlist[i].ret_ip_port;
|
66
|
+
printf("OUR IP: %u.%u.%u.%u Port: %u\n",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));
|
67
|
+
printf("Timestamp: %u\n", close_clientlist[i].ret_timestamp);
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
void print_friendlist()
|
72
|
+
{
|
73
|
+
uint32_t i, j, k;
|
74
|
+
IP_Port p_ip;
|
75
|
+
printf("_________________FRIENDS__________________________________\n");
|
76
|
+
for(k = 0; k < num_friends; k++) {
|
77
|
+
printf("FRIEND %u\n", k);
|
78
|
+
printf("ID: ");
|
79
|
+
for(j = 0; j < 32; j++) {
|
80
|
+
printf("%c", friends_list[k].client_id[j]);
|
81
|
+
}
|
82
|
+
p_ip = DHT_getfriendip(friends_list[k].client_id);
|
83
|
+
printf("\nIP: %u.%u.%u.%u:%u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));
|
84
|
+
|
85
|
+
printf("\nCLIENTS IN LIST:\n\n");
|
86
|
+
|
87
|
+
for(i = 0; i < 4; i++) {
|
88
|
+
printf("ClientID: ");
|
89
|
+
for(j = 0; j < 32; j++) {
|
90
|
+
if(0 <= friends_list[k].client_list[i].client_id[j] && friends_list[k].client_list[i].client_id[j] < 16)
|
91
|
+
printf("0");
|
92
|
+
printf("%hhX", friends_list[k].client_list[i].client_id[j]);
|
93
|
+
}
|
94
|
+
p_ip = friends_list[k].client_list[i].ip_port;
|
95
|
+
printf("\nIP: %u.%u.%u.%u:%u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));
|
96
|
+
printf("\nTimestamp: %u", friends_list[k].client_list[i].timestamp);
|
97
|
+
printf("\nLast pinged: %u\n", friends_list[k].client_list[i].last_pinged);
|
98
|
+
p_ip = friends_list[k].client_list[i].ret_ip_port;
|
99
|
+
printf("ret IP: %u.%u.%u.%u:%u\n",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port));
|
100
|
+
printf("Timestamp: %u\n", friends_list[k].client_list[i].ret_timestamp);
|
101
|
+
}
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
|
106
|
+
{
|
107
|
+
uint32_t i;
|
108
|
+
printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
|
109
|
+
printf("--------------------BEGIN-----------------------------\n");
|
110
|
+
for(i = 0; i < length; i++) {
|
111
|
+
if(data[i] < 16)
|
112
|
+
printf("0");
|
113
|
+
printf("%hhX",data[i]);
|
114
|
+
}
|
115
|
+
printf("\n--------------------END-----------------------------\n\n\n");
|
116
|
+
}
|
117
|
+
|
118
|
+
int main(int argc, char *argv[])
|
119
|
+
{
|
120
|
+
//memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
|
121
|
+
|
122
|
+
if (argc < 4) {
|
123
|
+
printf("usage %s ip port public_key\n", argv[0]);
|
124
|
+
exit(0);
|
125
|
+
}
|
126
|
+
new_keys();
|
127
|
+
printf("OUR ID: ");
|
128
|
+
uint32_t i;
|
129
|
+
for(i = 0; i < 32; i++) {
|
130
|
+
if(self_public_key[i] < 16)
|
131
|
+
printf("0");
|
132
|
+
printf("%hhX",self_public_key[i]);
|
133
|
+
}
|
134
|
+
|
135
|
+
char temp_id[128];
|
136
|
+
printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n");
|
137
|
+
if(scanf("%s", temp_id) != 1)
|
138
|
+
exit(0);
|
139
|
+
|
140
|
+
DHT_addfriend(hex_string_to_bin(temp_id));
|
141
|
+
|
142
|
+
/* initialize networking */
|
143
|
+
/* bind to ip 0.0.0.0:PORT */
|
144
|
+
IP ip;
|
145
|
+
ip.i = 0;
|
146
|
+
init_networking(ip, PORT);
|
147
|
+
|
148
|
+
|
149
|
+
perror("Initialization");
|
150
|
+
IP_Port bootstrap_ip_port;
|
151
|
+
bootstrap_ip_port.port = htons(atoi(argv[2]));
|
152
|
+
/* bootstrap_ip_port.ip.c[0] = 127;
|
153
|
+
* bootstrap_ip_port.ip.c[1] = 0;
|
154
|
+
* bootstrap_ip_port.ip.c[2] = 0;
|
155
|
+
* bootstrap_ip_port.ip.c[3] = 1; */
|
156
|
+
bootstrap_ip_port.ip.i = inet_addr(argv[1]);
|
157
|
+
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
|
158
|
+
|
159
|
+
IP_Port ip_port;
|
160
|
+
uint8_t data[MAX_UDP_PACKET_SIZE];
|
161
|
+
uint32_t length;
|
162
|
+
|
163
|
+
while(1) {
|
164
|
+
|
165
|
+
doDHT();
|
166
|
+
|
167
|
+
while(receivepacket(&ip_port, data, &length) != -1) {
|
168
|
+
if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) {
|
169
|
+
//unhandled packet
|
170
|
+
printpacket(data, length, ip_port);
|
171
|
+
} else {
|
172
|
+
printf("Received handled packet with length: %u\n", length);
|
173
|
+
}
|
174
|
+
}
|
175
|
+
print_clientlist();
|
176
|
+
print_friendlist();
|
177
|
+
c_sleep(300);
|
178
|
+
}
|
179
|
+
|
180
|
+
shutdown_networking();
|
181
|
+
return 0;
|
182
|
+
}
|