ffi-tox 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,214 @@
|
|
1
|
+
/* Lossless_UDP testclient
|
2
|
+
* A program that connects and sends a file using our lossless UDP algorithm.
|
3
|
+
* NOTE: this program simulates a 33% packet loss.
|
4
|
+
*
|
5
|
+
* Best used in combination with Lossless_UDP_testserver
|
6
|
+
*
|
7
|
+
* Compile with: gcc -O2 -Wall -o testclient ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testclient.c
|
8
|
+
*
|
9
|
+
* Command line arguments are the ip and port to connect and send the file to.
|
10
|
+
* EX: ./testclient 127.0.0.1 33445 filename.txt
|
11
|
+
*
|
12
|
+
* Copyright (C) 2013 Tox project All Rights Reserved.
|
13
|
+
*
|
14
|
+
* This file is part of Tox.
|
15
|
+
*
|
16
|
+
* Tox is free software: you can redistribute it and/or modify
|
17
|
+
* it under the terms of the GNU General Public License as published by
|
18
|
+
* the Free Software Foundation, either version 3 of the License, or
|
19
|
+
* (at your option) any later version.
|
20
|
+
*
|
21
|
+
* Tox is distributed in the hope that it will be useful,
|
22
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
23
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
24
|
+
* GNU General Public License for more details.
|
25
|
+
*
|
26
|
+
* You should have received a copy of the GNU General Public License
|
27
|
+
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
28
|
+
*
|
29
|
+
*/
|
30
|
+
|
31
|
+
#include "../core/network.h"
|
32
|
+
#include "../core/Lossless_UDP.h"
|
33
|
+
|
34
|
+
#ifdef WIN32
|
35
|
+
|
36
|
+
#define c_sleep(x) Sleep(1*x)
|
37
|
+
|
38
|
+
#else
|
39
|
+
#include <unistd.h>
|
40
|
+
#include <arpa/inet.h>
|
41
|
+
#define c_sleep(x) usleep(1000*x)
|
42
|
+
|
43
|
+
#endif
|
44
|
+
|
45
|
+
#define PORT 33446
|
46
|
+
|
47
|
+
void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port)
|
48
|
+
{
|
49
|
+
uint32_t i;
|
50
|
+
printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
|
51
|
+
printf("--------------------BEGIN-----------------------------\n");
|
52
|
+
for (i = 0; i < length; i++) {
|
53
|
+
if (data[i] < 16)
|
54
|
+
printf("0");
|
55
|
+
printf("%hhX",data[i]);
|
56
|
+
}
|
57
|
+
printf("\n--------------------END-----------------------------\n\n\n");
|
58
|
+
}
|
59
|
+
|
60
|
+
void printip(IP_Port ip_port)
|
61
|
+
{
|
62
|
+
printf("\nIP: %u.%u.%u.%u Port: %u", ip_port.ip.c[0], ip_port.ip.c[1], ip_port.ip.c[2], ip_port.ip.c[3], ntohs(ip_port.port));
|
63
|
+
}
|
64
|
+
/*
|
65
|
+
void printpackets(Data test)
|
66
|
+
{
|
67
|
+
int i;
|
68
|
+
if(test.size == 0)
|
69
|
+
return;
|
70
|
+
printf("SIZE: %u\n", test.size);
|
71
|
+
for(i =0; i < test.size; i++)
|
72
|
+
{
|
73
|
+
printf("%hhX", test.data[i]);
|
74
|
+
}
|
75
|
+
printf("\n");
|
76
|
+
}
|
77
|
+
|
78
|
+
void printconnection(int connection_id)
|
79
|
+
{
|
80
|
+
printf("--------------------BEGIN---------------------\n");
|
81
|
+
IP_Port ip_port = connections[connection_id].ip_port;
|
82
|
+
printf("IP: %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));
|
83
|
+
printf("status: %u, inbound: %u, SYNC_rate: %u\n", connections[connection_id].status,
|
84
|
+
connections[connection_id].inbound, connections[connection_id].SYNC_rate);
|
85
|
+
printf("data rate: %u, last sync: %llu, last sent: %llu, last recv: %llu \n", connections[connection_id].data_rate,
|
86
|
+
connections[connection_id].last_SYNC, connections[connection_id].last_sent, connections[connection_id].last_recv);
|
87
|
+
int i;
|
88
|
+
for(i =0; i < MAX_QUEUE_NUM; i++)
|
89
|
+
{
|
90
|
+
printf(" %u ",i);
|
91
|
+
printpackets(connections[connection_id].sendbuffer[i]);
|
92
|
+
}
|
93
|
+
for(i =0; i < MAX_QUEUE_NUM; i++)
|
94
|
+
{
|
95
|
+
printf(" %u ",i);
|
96
|
+
printpackets(connections[connection_id].recvbuffer[i]);
|
97
|
+
}
|
98
|
+
Data sendbuffer[MAX_QUEUE_NUM];
|
99
|
+
Data recvbuffer[MAX_QUEUE_NUM];
|
100
|
+
printf("recv_num: %u, orecv_num: %u, sent_packetnum %u, osent_packetnum: %u, successful_sent: %u, successful_read: %u\n",
|
101
|
+
connections[connection_id].recv_packetnum,
|
102
|
+
connections[connection_id].orecv_packetnum, connections[connection_id].sent_packetnum, connections[connection_id].osent_packetnum,
|
103
|
+
connections[connection_id].successful_sent,
|
104
|
+
connections[connection_id].successful_read);
|
105
|
+
|
106
|
+
printf("req packets: \n");
|
107
|
+
for(i = 0; i < BUFFER_PACKET_NUM; i++)
|
108
|
+
{
|
109
|
+
printf(" %u ", connections[connection_id].req_packets[i]);
|
110
|
+
}
|
111
|
+
printf("\nNumber: %u recv_counter: %u, send_counter: %u\n", connections[connection_id].num_req_paquets,
|
112
|
+
connections[connection_id].recv_counter, connections[connection_id].send_counter);
|
113
|
+
|
114
|
+
printf("--------------------END---------------------\n");
|
115
|
+
|
116
|
+
}
|
117
|
+
*/
|
118
|
+
|
119
|
+
/*( recieve packets and send them to the packethandler */
|
120
|
+
/*run doLossless_UDP(); */
|
121
|
+
void Lossless_UDP()
|
122
|
+
{
|
123
|
+
IP_Port ip_port;
|
124
|
+
uint8_t data[MAX_UDP_PACKET_SIZE];
|
125
|
+
uint32_t length;
|
126
|
+
while (receivepacket(&ip_port, data, &length) != -1) {
|
127
|
+
printf("packet with length: %u\n", length);
|
128
|
+
/* if(rand() % 3 != 1)//add packet loss
|
129
|
+
{ */
|
130
|
+
if (LosslessUDP_handlepacket(data, length, ip_port))
|
131
|
+
printpacket(data, length, ip_port);
|
132
|
+
else
|
133
|
+
printf("Received handled packet with length: %u\n", length); //printconnection(0);
|
134
|
+
|
135
|
+
/* } */
|
136
|
+
}
|
137
|
+
|
138
|
+
doLossless_UDP();
|
139
|
+
|
140
|
+
}
|
141
|
+
|
142
|
+
int main(int argc, char *argv[])
|
143
|
+
{
|
144
|
+
if (argc < 4) {
|
145
|
+
printf("usage: %s ip port filename\n", argv[0]);
|
146
|
+
exit(0);
|
147
|
+
}
|
148
|
+
|
149
|
+
uint8_t buffer[512];
|
150
|
+
int read;
|
151
|
+
|
152
|
+
FILE *file = fopen(argv[3], "rb");
|
153
|
+
if (file == NULL)
|
154
|
+
return 1;
|
155
|
+
|
156
|
+
|
157
|
+
/* initialize networking */
|
158
|
+
/* bind to ip 0.0.0.0:PORT */
|
159
|
+
IP ip;
|
160
|
+
ip.i = 0;
|
161
|
+
init_networking(ip, PORT);
|
162
|
+
perror("Initialization");
|
163
|
+
IP_Port serverip;
|
164
|
+
serverip.ip.i = inet_addr(argv[1]);
|
165
|
+
serverip.port = htons(atoi(argv[2]));
|
166
|
+
printip(serverip);
|
167
|
+
int connection = new_connection(serverip);
|
168
|
+
uint64_t timer = current_time();
|
169
|
+
while (1) {
|
170
|
+
/* printconnection(connection); */
|
171
|
+
Lossless_UDP();
|
172
|
+
if (is_connected(connection) == 3) {
|
173
|
+
printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer));
|
174
|
+
break;
|
175
|
+
}
|
176
|
+
if (is_connected(connection) == 0) {
|
177
|
+
printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer));
|
178
|
+
return 1;
|
179
|
+
}
|
180
|
+
c_sleep(1);
|
181
|
+
}
|
182
|
+
timer = current_time();
|
183
|
+
|
184
|
+
|
185
|
+
/*read first part of file */
|
186
|
+
read = fread(buffer, 1, 512, file);
|
187
|
+
|
188
|
+
while (1) {
|
189
|
+
/* printconnection(connection); */
|
190
|
+
Lossless_UDP();
|
191
|
+
if (is_connected(connection) == 3) {
|
192
|
+
|
193
|
+
if (write_packet(connection, buffer, read)) {
|
194
|
+
/* printf("Wrote data.\n"); */
|
195
|
+
read = fread(buffer, 1, 512, file);
|
196
|
+
|
197
|
+
}
|
198
|
+
/* printf("%u\n", sendqueue(connection)); */
|
199
|
+
if (sendqueue(connection) == 0) {
|
200
|
+
if (read == 0) {
|
201
|
+
printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer));
|
202
|
+
break;
|
203
|
+
}
|
204
|
+
}
|
205
|
+
}
|
206
|
+
else {
|
207
|
+
printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
|
208
|
+
return 0;
|
209
|
+
}
|
210
|
+
/* c_sleep(1); */
|
211
|
+
}
|
212
|
+
|
213
|
+
return 0;
|
214
|
+
}
|
@@ -0,0 +1,201 @@
|
|
1
|
+
/* Lossless_UDP testserver
|
2
|
+
* A program that waits for a lossless UDP connection and then saves all the data recieved to a file.
|
3
|
+
* NOTE: this program simulates a 33% packet loss.
|
4
|
+
*
|
5
|
+
* Best used in combination with Lossless_UDP_testclient
|
6
|
+
*
|
7
|
+
* Compile with: gcc -O2 -Wall -o testserver ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testserver.c
|
8
|
+
*
|
9
|
+
* Command line argument is the name of the file to save what we recieve to.
|
10
|
+
* EX: ./testserver filename1.txt
|
11
|
+
*
|
12
|
+
* Copyright (C) 2013 Tox project All Rights Reserved.
|
13
|
+
*
|
14
|
+
* This file is part of Tox.
|
15
|
+
*
|
16
|
+
* Tox is free software: you can redistribute it and/or modify
|
17
|
+
* it under the terms of the GNU General Public License as published by
|
18
|
+
* the Free Software Foundation, either version 3 of the License, or
|
19
|
+
* (at your option) any later version.
|
20
|
+
*
|
21
|
+
* Tox is distributed in the hope that it will be useful,
|
22
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
23
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
24
|
+
* GNU General Public License for more details.
|
25
|
+
*
|
26
|
+
* You should have received a copy of the GNU General Public License
|
27
|
+
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
28
|
+
*
|
29
|
+
*/
|
30
|
+
|
31
|
+
#include "../core/network.h"
|
32
|
+
#include "../core/Lossless_UDP.h"
|
33
|
+
|
34
|
+
//Sleep function (x = milliseconds)
|
35
|
+
#ifdef WIN32
|
36
|
+
|
37
|
+
#define c_sleep(x) Sleep(1*x)
|
38
|
+
|
39
|
+
#else
|
40
|
+
#include <unistd.h>
|
41
|
+
#include <arpa/inet.h>
|
42
|
+
#define c_sleep(x) usleep(1000*x)
|
43
|
+
|
44
|
+
#endif
|
45
|
+
|
46
|
+
#define PORT 33445
|
47
|
+
|
48
|
+
void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port)
|
49
|
+
{
|
50
|
+
uint32_t i;
|
51
|
+
printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
|
52
|
+
printf("--------------------BEGIN-----------------------------\n");
|
53
|
+
for (i = 0; i < length; i++) {
|
54
|
+
if(data[i] < 16)
|
55
|
+
printf("0");
|
56
|
+
printf("%hhX",data[i]);
|
57
|
+
}
|
58
|
+
printf("\n--------------------END-----------------------------\n\n\n");
|
59
|
+
}
|
60
|
+
|
61
|
+
/*
|
62
|
+
void printpackets(Data test)
|
63
|
+
{
|
64
|
+
int i;
|
65
|
+
if(test.size == 0)
|
66
|
+
return;
|
67
|
+
printf("SIZE: %u\n", test.size);
|
68
|
+
for(i =0; i < test.size; i++)
|
69
|
+
{
|
70
|
+
printf("%hhX", test.data[i]);
|
71
|
+
}
|
72
|
+
printf("\n");
|
73
|
+
}
|
74
|
+
|
75
|
+
void printconnection(int connection_id)
|
76
|
+
{
|
77
|
+
printf("--------------------BEGIN---------------------\n");
|
78
|
+
IP_Port ip_port = connections[connection_id].ip_port;
|
79
|
+
printf("IP: %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));
|
80
|
+
printf("status: %u, inbound: %u, SYNC_rate: %u\n", connections[connection_id].status,
|
81
|
+
connections[connection_id].inbound, connections[connection_id].SYNC_rate);
|
82
|
+
printf("data rate: %u, last sync: %llu, last sent: %llu, last recv: %llu \n", connections[connection_id].data_rate,
|
83
|
+
connections[connection_id].last_SYNC, connections[connection_id].last_sent, connections[connection_id].last_recv);
|
84
|
+
int i;
|
85
|
+
for(i =0; i < MAX_QUEUE_NUM; i++)
|
86
|
+
{
|
87
|
+
printf(" %u ",i);
|
88
|
+
printpackets(connections[connection_id].sendbuffer[i]);
|
89
|
+
}
|
90
|
+
for(i =0; i < MAX_QUEUE_NUM; i++)
|
91
|
+
{
|
92
|
+
printf(" %u ",i);
|
93
|
+
printpackets(connections[connection_id].recvbuffer[i]);
|
94
|
+
}
|
95
|
+
Data sendbuffer[MAX_QUEUE_NUM];
|
96
|
+
Data recvbuffer[MAX_QUEUE_NUM];
|
97
|
+
printf("recv_num: %u, orecv_num: %u, sent_packetnum %u, osent_packetnum: %u, successful_sent: %u, successful_read: %u\n",
|
98
|
+
connections[connection_id].recv_packetnum,
|
99
|
+
connections[connection_id].orecv_packetnum, connections[connection_id].sent_packetnum, connections[connection_id].osent_packetnum,
|
100
|
+
connections[connection_id].successful_sent,
|
101
|
+
connections[connection_id].successful_read);
|
102
|
+
|
103
|
+
printf("req packets: \n");
|
104
|
+
for(i = 0; i < BUFFER_PACKET_NUM; i++)
|
105
|
+
{
|
106
|
+
printf(" %u ", connections[connection_id].req_packets[i]);
|
107
|
+
}
|
108
|
+
printf("\nNumber: %u recv_counter: %u, send_counter: %u\n", connections[connection_id].num_req_paquets,
|
109
|
+
connections[connection_id].recv_counter, connections[connection_id].send_counter);
|
110
|
+
|
111
|
+
printf("--------------------END---------------------\n");
|
112
|
+
|
113
|
+
}
|
114
|
+
*/
|
115
|
+
|
116
|
+
/* recieve packets and send them to the packethandler
|
117
|
+
* run doLossless_UDP(); */
|
118
|
+
void Lossless_UDP()
|
119
|
+
{
|
120
|
+
IP_Port ip_port;
|
121
|
+
uint8_t data[MAX_UDP_PACKET_SIZE];
|
122
|
+
uint32_t length;
|
123
|
+
while (receivepacket(&ip_port, data, &length) != -1) {
|
124
|
+
//if(rand() % 3 != 1)//add packet loss
|
125
|
+
//{
|
126
|
+
if (LosslessUDP_handlepacket(data, length, ip_port)) {
|
127
|
+
printpacket(data, length, ip_port);
|
128
|
+
} else {
|
129
|
+
//printconnection(0);
|
130
|
+
printf("Received handled packet with length: %u\n", length);
|
131
|
+
}
|
132
|
+
//}
|
133
|
+
}
|
134
|
+
|
135
|
+
doLossless_UDP();
|
136
|
+
}
|
137
|
+
|
138
|
+
|
139
|
+
int main(int argc, char *argv[])
|
140
|
+
{
|
141
|
+
if (argc < 2) {
|
142
|
+
printf("usage: %s filename\n", argv[0]);
|
143
|
+
exit(0);
|
144
|
+
}
|
145
|
+
|
146
|
+
uint8_t buffer[512];
|
147
|
+
int read;
|
148
|
+
|
149
|
+
FILE *file = fopen(argv[1], "wb");
|
150
|
+
if (file == NULL)
|
151
|
+
return 1;
|
152
|
+
|
153
|
+
|
154
|
+
//initialize networking
|
155
|
+
//bind to ip 0.0.0.0:PORT
|
156
|
+
IP ip;
|
157
|
+
ip.i = 0;
|
158
|
+
init_networking(ip, PORT);
|
159
|
+
perror("Initialization");
|
160
|
+
|
161
|
+
int connection;
|
162
|
+
uint64_t timer = current_time();
|
163
|
+
|
164
|
+
|
165
|
+
while (1) {
|
166
|
+
Lossless_UDP();
|
167
|
+
connection = incoming_connection();
|
168
|
+
if(connection != -1) {
|
169
|
+
if(is_connected(connection) == 2) {
|
170
|
+
printf("Recieved the connection.\n");
|
171
|
+
|
172
|
+
}
|
173
|
+
break;
|
174
|
+
}
|
175
|
+
c_sleep(1);
|
176
|
+
}
|
177
|
+
|
178
|
+
timer = current_time();
|
179
|
+
|
180
|
+
while (1) {
|
181
|
+
//printconnection(0);
|
182
|
+
Lossless_UDP();
|
183
|
+
if (is_connected(connection) >= 2) {
|
184
|
+
kill_connection_in(connection, 3000000);
|
185
|
+
read = read_packet(connection, buffer);
|
186
|
+
if (read != 0) {
|
187
|
+
// printf("Recieved data.\n");
|
188
|
+
if (!fwrite(buffer, read, 1, file))
|
189
|
+
printf("file write error\n");
|
190
|
+
}
|
191
|
+
}
|
192
|
+
if(is_connected(connection) == 4) {
|
193
|
+
printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
|
194
|
+
fclose(file);
|
195
|
+
return 1;
|
196
|
+
}
|
197
|
+
c_sleep(1);
|
198
|
+
}
|
199
|
+
|
200
|
+
return 0;
|
201
|
+
}
|
@@ -0,0 +1,145 @@
|
|
1
|
+
/* Messenger test
|
2
|
+
*
|
3
|
+
* This program adds a friend and accepts all friend requests with the proper message.
|
4
|
+
*
|
5
|
+
* It tries sending a message to the added friend.
|
6
|
+
*
|
7
|
+
* If it recieves a message from a friend it replies back.
|
8
|
+
*
|
9
|
+
*
|
10
|
+
* This is how I compile it: gcc -O2 -Wall -D VANILLA_NACL -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} Messenger_test.c
|
11
|
+
*
|
12
|
+
*
|
13
|
+
* Command line arguments are the ip, port and public_key of a node (for bootstrapping).
|
14
|
+
*
|
15
|
+
* EX: ./test 127.0.0.1 33445 CDCFD319CE3460824B33BE58FD86B8941C9585181D8FBD7C79C5721D7C2E9F7C
|
16
|
+
*
|
17
|
+
* Or the argument can be the path to the save file.
|
18
|
+
*
|
19
|
+
* EX: ./test Save.bak
|
20
|
+
*
|
21
|
+
* Copyright (C) 2013 Tox project All Rights Reserved.
|
22
|
+
*
|
23
|
+
* This file is part of Tox.
|
24
|
+
*
|
25
|
+
* Tox is free software: you can redistribute it and/or modify
|
26
|
+
* it under the terms of the GNU General Public License as published by
|
27
|
+
* the Free Software Foundation, either version 3 of the License, or
|
28
|
+
* (at your option) any later version.
|
29
|
+
*
|
30
|
+
* Tox is distributed in the hope that it will be useful,
|
31
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
32
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
33
|
+
* GNU General Public License for more details.
|
34
|
+
*
|
35
|
+
* You should have received a copy of the GNU General Public License
|
36
|
+
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
37
|
+
*
|
38
|
+
*/
|
39
|
+
|
40
|
+
#include "../core/Messenger.h"
|
41
|
+
#include "misc_tools.h"
|
42
|
+
|
43
|
+
#ifdef WIN32
|
44
|
+
|
45
|
+
#define c_sleep(x) Sleep(1*x)
|
46
|
+
|
47
|
+
#else
|
48
|
+
#include <unistd.h>
|
49
|
+
#include <arpa/inet.h>
|
50
|
+
#define c_sleep(x) usleep(1000*x)
|
51
|
+
|
52
|
+
#endif
|
53
|
+
|
54
|
+
void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
|
55
|
+
{
|
56
|
+
printf("Friend request recieved from: \n");
|
57
|
+
printf("ClientID: ");
|
58
|
+
uint32_t j;
|
59
|
+
for(j = 0; j < 32; j++)
|
60
|
+
{
|
61
|
+
if(public_key[j] < 16)
|
62
|
+
printf("0");
|
63
|
+
printf("%hhX", public_key[j]);
|
64
|
+
}
|
65
|
+
printf("\nOf length: %u with data: %s \n", length, data);
|
66
|
+
|
67
|
+
if(length != sizeof("Install Gentoo"))
|
68
|
+
{
|
69
|
+
return;
|
70
|
+
}
|
71
|
+
if(memcmp(data , "Install Gentoo", sizeof("Install Gentoo")) == 0 )
|
72
|
+
//if the request contained the message of peace the person is obviously a friend so we add him.
|
73
|
+
{
|
74
|
+
printf("Friend request accepted.\n");
|
75
|
+
m_addfriend_norequest(public_key);
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
void print_message(int friendnumber, uint8_t * string, uint16_t length)
|
80
|
+
{
|
81
|
+
printf("Message with length %u recieved from %u: %s \n", length, friendnumber, string);
|
82
|
+
m_sendmessage(friendnumber, (uint8_t*)"Test1", 6);
|
83
|
+
}
|
84
|
+
|
85
|
+
int main(int argc, char *argv[])
|
86
|
+
{
|
87
|
+
if (argc < 4 && argc != 2) {
|
88
|
+
printf("usage %s ip port public_key (of the DHT bootstrap node)\n or\n %s Save.bak\n", argv[0], argv[0]);
|
89
|
+
exit(0);
|
90
|
+
}
|
91
|
+
initMessenger();
|
92
|
+
if(argc > 3) {
|
93
|
+
IP_Port bootstrap_ip_port;
|
94
|
+
bootstrap_ip_port.port = htons(atoi(argv[2]));
|
95
|
+
bootstrap_ip_port.ip.i = inet_addr(argv[1]);
|
96
|
+
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
|
97
|
+
} else {
|
98
|
+
FILE *file = fopen(argv[1], "rb");
|
99
|
+
if ( file==NULL ){return 1;}
|
100
|
+
int read;
|
101
|
+
uint8_t buffer[128000];
|
102
|
+
read = fread(buffer, 1, 128000, file);
|
103
|
+
printf("Messenger loaded: %i\n", Messenger_load(buffer, read));
|
104
|
+
fclose(file);
|
105
|
+
|
106
|
+
}
|
107
|
+
m_callback_friendrequest(print_request);
|
108
|
+
m_callback_friendmessage(print_message);
|
109
|
+
|
110
|
+
printf("OUR ID: ");
|
111
|
+
uint32_t i;
|
112
|
+
for(i = 0; i < 32; i++) {
|
113
|
+
if(self_public_key[i] < 16)
|
114
|
+
printf("0");
|
115
|
+
printf("%hhX",self_public_key[i]);
|
116
|
+
}
|
117
|
+
|
118
|
+
setname((uint8_t *)"Anon", 5);
|
119
|
+
|
120
|
+
char temp_id[128];
|
121
|
+
printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n");
|
122
|
+
if(scanf("%s", temp_id) != 1) {
|
123
|
+
return 1;
|
124
|
+
}
|
125
|
+
int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
|
126
|
+
|
127
|
+
perror("Initialization");
|
128
|
+
|
129
|
+
while(1) {
|
130
|
+
uint8_t name[128];
|
131
|
+
getname(num, name);
|
132
|
+
printf("%s\n", name);
|
133
|
+
|
134
|
+
m_sendmessage(num, (uint8_t*)"Test", 5);
|
135
|
+
doMessenger();
|
136
|
+
c_sleep(30);
|
137
|
+
FILE *file = fopen("Save.bak", "wb");
|
138
|
+
if ( file==NULL ){return 1;}
|
139
|
+
uint8_t * buffer = malloc(Messenger_size());
|
140
|
+
Messenger_save(buffer);
|
141
|
+
fwrite(buffer, 1, Messenger_size(), file);
|
142
|
+
free(buffer);
|
143
|
+
fclose(file);
|
144
|
+
}
|
145
|
+
}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
/* misc_tools.c
|
2
|
+
*
|
3
|
+
* Miscellaneous functions and data structures for doing random things.
|
4
|
+
*
|
5
|
+
* Copyright (C) 2013 Tox project All Rights Reserved.
|
6
|
+
*
|
7
|
+
* This file is part of Tox.
|
8
|
+
*
|
9
|
+
* Tox is free software: you can redistribute it and/or modify
|
10
|
+
* it under the terms of the GNU General Public License as published by
|
11
|
+
* the Free Software Foundation, either version 3 of the License, or
|
12
|
+
* (at your option) any later version.
|
13
|
+
*
|
14
|
+
* Tox is distributed in the hope that it will be useful,
|
15
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
+
* GNU General Public License for more details.
|
18
|
+
*
|
19
|
+
* You should have received a copy of the GNU General Public License
|
20
|
+
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
21
|
+
*
|
22
|
+
*/
|
23
|
+
|
24
|
+
#include "misc_tools.h"
|
25
|
+
|
26
|
+
#include <string.h>
|
27
|
+
#include <stdlib.h>
|
28
|
+
#include <stdio.h> /* for sscanf */
|
29
|
+
|
30
|
+
/* TODO: rewrite */
|
31
|
+
unsigned char * hex_string_to_bin(char hex_string[])
|
32
|
+
{
|
33
|
+
size_t len = strlen(hex_string);
|
34
|
+
unsigned char *val = malloc(len);
|
35
|
+
char *pos = hex_string;
|
36
|
+
int i;
|
37
|
+
for(i = 0; i < len; ++i, pos+=2)
|
38
|
+
sscanf(pos,"%2hhx",&val[i]);
|
39
|
+
return val;
|
40
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
/* misc_tools.h
|
2
|
+
*
|
3
|
+
* Miscellaneous functions and data structures for doing random things.
|
4
|
+
*
|
5
|
+
* Copyright (C) 2013 Tox project All Rights Reserved.
|
6
|
+
*
|
7
|
+
* This file is part of Tox.
|
8
|
+
*
|
9
|
+
* Tox is free software: you can redistribute it and/or modify
|
10
|
+
* it under the terms of the GNU General Public License as published by
|
11
|
+
* the Free Software Foundation, either version 3 of the License, or
|
12
|
+
* (at your option) any later version.
|
13
|
+
*
|
14
|
+
* Tox is distributed in the hope that it will be useful,
|
15
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
+
* GNU General Public License for more details.
|
18
|
+
*
|
19
|
+
* You should have received a copy of the GNU General Public License
|
20
|
+
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
21
|
+
*
|
22
|
+
*/
|
23
|
+
|
24
|
+
#ifndef MISC_TOOLS_H
|
25
|
+
#define MISC_TOOLS_H
|
26
|
+
|
27
|
+
unsigned char * hex_string_to_bin(char hex_string[]);
|
28
|
+
|
29
|
+
#endif // MISC_TOOLS_H
|