ffi-tox 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ProjectTox-Core/AUTHORS +0 -0
- data/ProjectTox-Core/ChangeLog +0 -0
- data/ProjectTox-Core/INSTALL +370 -0
- data/ProjectTox-Core/INSTALL.md +455 -56
- data/ProjectTox-Core/Makefile.am +35 -0
- data/ProjectTox-Core/NEWS +0 -0
- data/ProjectTox-Core/README +43 -0
- data/ProjectTox-Core/README.md +34 -44
- data/ProjectTox-Core/auto_tests/Makefile.inc +110 -0
- data/ProjectTox-Core/auto_tests/TCP_test.c +519 -0
- data/ProjectTox-Core/auto_tests/assoc_test.c +160 -0
- data/ProjectTox-Core/auto_tests/crypto_test.c +302 -0
- data/ProjectTox-Core/auto_tests/dht_test.c +362 -0
- data/ProjectTox-Core/auto_tests/encryptsave_test.c +104 -0
- data/ProjectTox-Core/auto_tests/friends_test.c +238 -0
- data/ProjectTox-Core/auto_tests/helpers.h +15 -0
- data/ProjectTox-Core/auto_tests/messenger_test.c +365 -0
- data/ProjectTox-Core/auto_tests/network_test.c +171 -0
- data/ProjectTox-Core/auto_tests/onion_test.c +363 -0
- data/ProjectTox-Core/auto_tests/skeleton_test.c +49 -0
- data/ProjectTox-Core/auto_tests/tox_test.c +454 -0
- data/ProjectTox-Core/auto_tests/toxav_basic_test.c +597 -0
- data/ProjectTox-Core/auto_tests/toxav_many_test.c +402 -0
- data/ProjectTox-Core/autogen.sh +6 -0
- data/ProjectTox-Core/build/Makefile.am +14 -0
- data/ProjectTox-Core/configure.ac +694 -0
- data/ProjectTox-Core/dist-build/android-arm.sh +3 -0
- data/ProjectTox-Core/dist-build/android-armv7.sh +3 -0
- data/ProjectTox-Core/dist-build/android-build.sh +59 -0
- data/ProjectTox-Core/dist-build/android-mips.sh +3 -0
- data/ProjectTox-Core/dist-build/android-x86.sh +3 -0
- data/ProjectTox-Core/docs/Group-Chats.md +71 -0
- data/ProjectTox-Core/docs/Hardening.txt +60 -0
- data/ProjectTox-Core/docs/Hardening_docs.txt +30 -0
- data/ProjectTox-Core/docs/Prevent_Tracking.txt +160 -0
- data/ProjectTox-Core/docs/TCP_Network.txt +154 -0
- data/ProjectTox-Core/docs/TODO +62 -0
- data/ProjectTox-Core/docs/Tox_middle_level_network_protocol.txt +120 -0
- data/ProjectTox-Core/docs/av_api.md +194 -0
- data/ProjectTox-Core/libtoxav.pc.in +11 -0
- data/ProjectTox-Core/libtoxcore.pc.in +11 -0
- data/ProjectTox-Core/m4/ax_have_epoll.m4 +104 -0
- data/ProjectTox-Core/m4/ax_pthread.m4 +317 -0
- data/ProjectTox-Core/m4/pkg.m4 +199 -0
- data/ProjectTox-Core/other/DHT_bootstrap.c +121 -58
- data/ProjectTox-Core/other/DHTnodes +3 -0
- data/ProjectTox-Core/other/Makefile.inc +20 -0
- data/ProjectTox-Core/other/bootstrap_node_packets.c +65 -0
- data/ProjectTox-Core/other/tox.png +0 -0
- data/ProjectTox-Core/testing/DHT_test.c +170 -98
- data/ProjectTox-Core/testing/Makefile.inc +112 -0
- data/ProjectTox-Core/testing/Messenger_test.c +133 -69
- data/ProjectTox-Core/testing/dns3_test.c +115 -0
- data/ProjectTox-Core/testing/misc_tools.c +59 -13
- data/ProjectTox-Core/testing/nTox.c +1127 -264
- data/ProjectTox-Core/testing/nTox.h +10 -19
- data/ProjectTox-Core/testing/tox_shell.c +159 -0
- data/ProjectTox-Core/testing/tox_sync.c +299 -0
- data/ProjectTox-Core/tools/README +11 -0
- data/ProjectTox-Core/tools/astylerc +11 -0
- data/ProjectTox-Core/tools/pre-commit +17 -0
- data/ProjectTox-Core/toxav/Makefile.inc +36 -0
- data/ProjectTox-Core/toxav/codec.c +357 -0
- data/ProjectTox-Core/toxav/codec.h +116 -0
- data/ProjectTox-Core/toxav/msi.c +1949 -0
- data/ProjectTox-Core/toxav/msi.h +267 -0
- data/ProjectTox-Core/toxav/rtp.c +600 -0
- data/ProjectTox-Core/toxav/rtp.h +196 -0
- data/ProjectTox-Core/toxav/toxav.c +1148 -0
- data/ProjectTox-Core/toxav/toxav.h +389 -0
- data/ProjectTox-Core/toxcore/DHT.c +2521 -0
- data/ProjectTox-Core/toxcore/DHT.h +412 -0
- data/ProjectTox-Core/toxcore/LAN_discovery.c +322 -0
- data/ProjectTox-Core/{core → toxcore}/LAN_discovery.h +17 -12
- data/ProjectTox-Core/toxcore/Makefile.inc +67 -0
- data/ProjectTox-Core/toxcore/Messenger.c +3006 -0
- data/ProjectTox-Core/toxcore/Messenger.h +818 -0
- data/ProjectTox-Core/toxcore/TCP_client.c +858 -0
- data/ProjectTox-Core/toxcore/TCP_client.h +156 -0
- data/ProjectTox-Core/toxcore/TCP_server.c +1332 -0
- data/ProjectTox-Core/toxcore/TCP_server.h +181 -0
- data/ProjectTox-Core/toxcore/assoc.c +1033 -0
- data/ProjectTox-Core/toxcore/assoc.h +104 -0
- data/ProjectTox-Core/toxcore/crypto_core.c +278 -0
- data/ProjectTox-Core/toxcore/crypto_core.h +151 -0
- data/ProjectTox-Core/toxcore/friend_requests.c +175 -0
- data/ProjectTox-Core/toxcore/friend_requests.h +83 -0
- data/ProjectTox-Core/toxcore/group_chats.c +837 -0
- data/ProjectTox-Core/toxcore/group_chats.h +199 -0
- data/ProjectTox-Core/toxcore/list.c +256 -0
- data/ProjectTox-Core/toxcore/list.h +85 -0
- data/ProjectTox-Core/toxcore/logger.c +153 -0
- data/ProjectTox-Core/toxcore/logger.h +84 -0
- data/ProjectTox-Core/toxcore/misc_tools.h +70 -0
- data/ProjectTox-Core/toxcore/net_crypto.c +2753 -0
- data/ProjectTox-Core/toxcore/net_crypto.h +410 -0
- data/ProjectTox-Core/toxcore/network.c +979 -0
- data/ProjectTox-Core/toxcore/network.h +367 -0
- data/ProjectTox-Core/toxcore/onion.c +540 -0
- data/ProjectTox-Core/toxcore/onion.h +150 -0
- data/ProjectTox-Core/toxcore/onion_announce.c +433 -0
- data/ProjectTox-Core/toxcore/onion_announce.h +139 -0
- data/ProjectTox-Core/toxcore/onion_client.c +1347 -0
- data/ProjectTox-Core/toxcore/onion_client.h +253 -0
- data/ProjectTox-Core/toxcore/ping.c +346 -0
- data/ProjectTox-Core/toxcore/ping.h +47 -0
- data/ProjectTox-Core/toxcore/ping_array.c +162 -0
- data/ProjectTox-Core/toxcore/ping_array.h +75 -0
- data/ProjectTox-Core/toxcore/tox.c +940 -0
- data/ProjectTox-Core/toxcore/tox.h +734 -0
- data/ProjectTox-Core/toxcore/util.c +193 -0
- data/ProjectTox-Core/toxcore/util.h +63 -0
- data/ProjectTox-Core/toxdns/Makefile.inc +29 -0
- data/ProjectTox-Core/toxdns/toxdns.c +238 -0
- data/ProjectTox-Core/toxdns/toxdns.h +88 -0
- data/ProjectTox-Core/toxencryptsave/Makefile.inc +45 -0
- data/ProjectTox-Core/toxencryptsave/toxencryptsave.c +179 -0
- data/ProjectTox-Core/toxencryptsave/toxencryptsave.h +74 -0
- data/interfaces/libtox.i +2 -6
- data/lib/ffi-tox/libtox.rb +406 -28
- metadata +124 -46
- data/ProjectTox-Core/CMakeLists.txt +0 -50
- data/ProjectTox-Core/cmake/FindLIBCONFIG.cmake +0 -15
- data/ProjectTox-Core/cmake/FindNaCl.cmake +0 -17
- data/ProjectTox-Core/cmake/FindSODIUM.cmake +0 -15
- data/ProjectTox-Core/core/CMakeLists.txt +0 -19
- data/ProjectTox-Core/core/DHT.c +0 -1104
- data/ProjectTox-Core/core/DHT.h +0 -111
- data/ProjectTox-Core/core/LAN_discovery.c +0 -79
- data/ProjectTox-Core/core/Lossless_UDP.c +0 -755
- data/ProjectTox-Core/core/Lossless_UDP.h +0 -106
- data/ProjectTox-Core/core/Messenger.c +0 -596
- data/ProjectTox-Core/core/Messenger.h +0 -165
- data/ProjectTox-Core/core/friend_requests.c +0 -131
- data/ProjectTox-Core/core/friend_requests.h +0 -51
- data/ProjectTox-Core/core/net_crypto.c +0 -575
- data/ProjectTox-Core/core/net_crypto.h +0 -134
- data/ProjectTox-Core/core/network.c +0 -205
- data/ProjectTox-Core/core/network.h +0 -134
- data/ProjectTox-Core/docs/commands.md +0 -25
- data/ProjectTox-Core/docs/start_guide.de.md +0 -40
- data/ProjectTox-Core/docs/start_guide.md +0 -38
- data/ProjectTox-Core/other/CMakeLists.txt +0 -9
- data/ProjectTox-Core/testing/CMakeLists.txt +0 -18
- data/ProjectTox-Core/testing/DHT_cryptosendfiletest.c +0 -228
- data/ProjectTox-Core/testing/DHT_sendfiletest.c +0 -176
- data/ProjectTox-Core/testing/Lossless_UDP_testclient.c +0 -214
- data/ProjectTox-Core/testing/Lossless_UDP_testserver.c +0 -201
- data/ProjectTox-Core/testing/misc_tools.h +0 -29
- data/ProjectTox-Core/testing/nTox_win32.c +0 -387
- data/ProjectTox-Core/testing/nTox_win32.h +0 -40
- data/ProjectTox-Core/testing/rect.py +0 -45
@@ -0,0 +1,150 @@
|
|
1
|
+
/*
|
2
|
+
* onion.h -- Implementation of the onion part of docs/Prevent_Tracking.txt
|
3
|
+
*
|
4
|
+
* Copyright (C) 2013 Tox project All Rights Reserved.
|
5
|
+
*
|
6
|
+
* This file is part of Tox.
|
7
|
+
*
|
8
|
+
* Tox is free software: you can redistribute it and/or modify
|
9
|
+
* it under the terms of the GNU General Public License as published by
|
10
|
+
* the Free Software Foundation, either version 3 of the License, or
|
11
|
+
* (at your option) any later version.
|
12
|
+
*
|
13
|
+
* Tox is distributed in the hope that it will be useful,
|
14
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
+
* GNU General Public License for more details.
|
17
|
+
*
|
18
|
+
* You should have received a copy of the GNU General Public License
|
19
|
+
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
20
|
+
*
|
21
|
+
*/
|
22
|
+
|
23
|
+
#ifndef ONION_H
|
24
|
+
#define ONION_H
|
25
|
+
|
26
|
+
#include "DHT.h"
|
27
|
+
|
28
|
+
typedef struct {
|
29
|
+
DHT *dht;
|
30
|
+
Networking_Core *net;
|
31
|
+
uint8_t secret_symmetric_key[crypto_box_KEYBYTES];
|
32
|
+
uint64_t timestamp;
|
33
|
+
|
34
|
+
Shared_Keys shared_keys_1;
|
35
|
+
Shared_Keys shared_keys_2;
|
36
|
+
Shared_Keys shared_keys_3;
|
37
|
+
|
38
|
+
int (*recv_1_function)(void *, IP_Port, const uint8_t *, uint16_t);
|
39
|
+
void *callback_object;
|
40
|
+
} Onion;
|
41
|
+
|
42
|
+
#define ONION_MAX_PACKET_SIZE 1400
|
43
|
+
|
44
|
+
#define ONION_RETURN_1 (crypto_box_NONCEBYTES + SIZE_IPPORT + crypto_box_MACBYTES)
|
45
|
+
#define ONION_RETURN_2 (crypto_box_NONCEBYTES + SIZE_IPPORT + crypto_box_MACBYTES + ONION_RETURN_1)
|
46
|
+
#define ONION_RETURN_3 (crypto_box_NONCEBYTES + SIZE_IPPORT + crypto_box_MACBYTES + ONION_RETURN_2)
|
47
|
+
|
48
|
+
#define ONION_SEND_BASE (crypto_box_PUBLICKEYBYTES + SIZE_IPPORT + crypto_box_MACBYTES)
|
49
|
+
#define ONION_SEND_3 (crypto_box_NONCEBYTES + ONION_SEND_BASE + ONION_RETURN_2)
|
50
|
+
#define ONION_SEND_2 (crypto_box_NONCEBYTES + ONION_SEND_BASE*2 + ONION_RETURN_1)
|
51
|
+
#define ONION_SEND_1 (crypto_box_NONCEBYTES + ONION_SEND_BASE*3)
|
52
|
+
|
53
|
+
#define ONION_MAX_DATA_SIZE (ONION_MAX_PACKET_SIZE - (ONION_SEND_1 + 1))
|
54
|
+
#define ONION_RESPONSE_MAX_DATA_SIZE (ONION_MAX_PACKET_SIZE - (1 + ONION_RETURN_3))
|
55
|
+
|
56
|
+
typedef struct {
|
57
|
+
uint8_t shared_key1[crypto_box_BEFORENMBYTES];
|
58
|
+
uint8_t shared_key2[crypto_box_BEFORENMBYTES];
|
59
|
+
uint8_t shared_key3[crypto_box_BEFORENMBYTES];
|
60
|
+
|
61
|
+
uint8_t public_key1[crypto_box_PUBLICKEYBYTES];
|
62
|
+
uint8_t public_key2[crypto_box_PUBLICKEYBYTES];
|
63
|
+
uint8_t public_key3[crypto_box_PUBLICKEYBYTES];
|
64
|
+
|
65
|
+
IP_Port ip_port1;
|
66
|
+
IP_Port ip_port2;
|
67
|
+
IP_Port ip_port3;
|
68
|
+
|
69
|
+
uint32_t path_num;
|
70
|
+
} Onion_Path;
|
71
|
+
|
72
|
+
/* Create a new onion path.
|
73
|
+
*
|
74
|
+
* Create a new onion path out of nodes (nodes is a list of 3 nodes)
|
75
|
+
*
|
76
|
+
* new_path must be an empty memory location of atleast Onion_Path size.
|
77
|
+
*
|
78
|
+
* return -1 on failure.
|
79
|
+
* return 0 on success.
|
80
|
+
*/
|
81
|
+
int create_onion_path(const DHT *dht, Onion_Path *new_path, const Node_format *nodes);
|
82
|
+
|
83
|
+
/* Create a onion packet.
|
84
|
+
*
|
85
|
+
* Use Onion_Path path to create packet for data of length to dest.
|
86
|
+
* Maximum length of data is ONION_MAX_DATA_SIZE.
|
87
|
+
* packet should be at least ONION_MAX_PACKET_SIZE big.
|
88
|
+
*
|
89
|
+
* return -1 on failure.
|
90
|
+
* return length of created packet on success.
|
91
|
+
*/
|
92
|
+
int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest,
|
93
|
+
const uint8_t *data, uint32_t length);
|
94
|
+
|
95
|
+
|
96
|
+
/* Create a onion packet to be sent over tcp.
|
97
|
+
*
|
98
|
+
* Use Onion_Path path to create packet for data of length to dest.
|
99
|
+
* Maximum length of data is ONION_MAX_DATA_SIZE.
|
100
|
+
* packet should be at least ONION_MAX_PACKET_SIZE big.
|
101
|
+
*
|
102
|
+
* return -1 on failure.
|
103
|
+
* return length of created packet on success.
|
104
|
+
*/
|
105
|
+
int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest,
|
106
|
+
const uint8_t *data, uint32_t length);
|
107
|
+
|
108
|
+
/* Create and send a onion packet.
|
109
|
+
*
|
110
|
+
* Use Onion_Path path to send data of length to dest.
|
111
|
+
* Maximum length of data is ONION_MAX_DATA_SIZE.
|
112
|
+
*
|
113
|
+
* return -1 on failure.
|
114
|
+
* return 0 on success.
|
115
|
+
*/
|
116
|
+
int send_onion_packet(Networking_Core *net, const Onion_Path *path, IP_Port dest, const uint8_t *data, uint32_t length);
|
117
|
+
|
118
|
+
/* Create and send a onion response sent initially to dest with.
|
119
|
+
* Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE.
|
120
|
+
*
|
121
|
+
* return -1 on failure.
|
122
|
+
* return 0 on success.
|
123
|
+
*/
|
124
|
+
int send_onion_response(Networking_Core *net, IP_Port dest, const uint8_t *data, uint32_t length, const uint8_t *ret);
|
125
|
+
|
126
|
+
/* Function to handle/send received decrypted versions of the packet sent with send_onion_packet.
|
127
|
+
*
|
128
|
+
* return 0 on success.
|
129
|
+
* return 1 on failure.
|
130
|
+
*
|
131
|
+
* Used to handle these packets that are received in a non traditional way (by TCP for example).
|
132
|
+
*
|
133
|
+
* Source family must be set to something else than AF_INET6 or AF_INET so that the callback gets called
|
134
|
+
* when the response is received.
|
135
|
+
*/
|
136
|
+
int onion_send_1(const Onion *onion, const uint8_t *plain, uint32_t len, IP_Port source, const uint8_t *nonce);
|
137
|
+
|
138
|
+
/* Set the callback to be called when the dest ip_port doesn't have AF_INET6 or AF_INET as the family.
|
139
|
+
*
|
140
|
+
* Format: function(void *object, IP_Port dest, uint8_t *data, uint32_t length)
|
141
|
+
*/
|
142
|
+
void set_callback_handle_recv_1(Onion *onion, int (*function)(void *, IP_Port, const uint8_t *, uint16_t),
|
143
|
+
void *object);
|
144
|
+
|
145
|
+
Onion *new_onion(DHT *dht);
|
146
|
+
|
147
|
+
void kill_onion(Onion *onion);
|
148
|
+
|
149
|
+
|
150
|
+
#endif
|
@@ -0,0 +1,433 @@
|
|
1
|
+
/*
|
2
|
+
* onion_announce.c -- Implementation of the announce part of docs/Prevent_Tracking.txt
|
3
|
+
*
|
4
|
+
* Copyright (C) 2013 Tox project All Rights Reserved.
|
5
|
+
*
|
6
|
+
* This file is part of Tox.
|
7
|
+
*
|
8
|
+
* Tox is free software: you can redistribute it and/or modify
|
9
|
+
* it under the terms of the GNU General Public License as published by
|
10
|
+
* the Free Software Foundation, either version 3 of the License, or
|
11
|
+
* (at your option) any later version.
|
12
|
+
*
|
13
|
+
* Tox is distributed in the hope that it will be useful,
|
14
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
+
* GNU General Public License for more details.
|
17
|
+
*
|
18
|
+
* You should have received a copy of the GNU General Public License
|
19
|
+
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
20
|
+
*
|
21
|
+
*/
|
22
|
+
#ifdef HAVE_CONFIG_H
|
23
|
+
#include "config.h"
|
24
|
+
#endif
|
25
|
+
|
26
|
+
#include "onion_announce.h"
|
27
|
+
#include "LAN_discovery.h"
|
28
|
+
#include "util.h"
|
29
|
+
|
30
|
+
#define PING_ID_TIMEOUT 20
|
31
|
+
|
32
|
+
#define ANNOUNCE_REQUEST_SIZE_RECV (ONION_ANNOUNCE_REQUEST_SIZE + ONION_RETURN_3)
|
33
|
+
|
34
|
+
#define DATA_REQUEST_MIN_SIZE ONION_DATA_REQUEST_MIN_SIZE
|
35
|
+
#define DATA_REQUEST_MIN_SIZE_RECV (DATA_REQUEST_MIN_SIZE + ONION_RETURN_3)
|
36
|
+
|
37
|
+
/* Create an onion announce request packet in packet of max_packet_length (recommended size ONION_ANNOUNCE_REQUEST_SIZE).
|
38
|
+
*
|
39
|
+
* dest_client_id is the public key of the node the packet will be sent to.
|
40
|
+
* public_key and secret_key is the kepair which will be used to encrypt the request.
|
41
|
+
* ping_id is the ping id that will be sent in the request.
|
42
|
+
* client_id is the client id of the node we are searching for.
|
43
|
+
* data_public_key is the public key we want others to encrypt their data packets with.
|
44
|
+
* sendback_data is the data of ONION_ANNOUNCE_SENDBACK_DATA_LENGTH length that we expect to
|
45
|
+
* receive back in the response.
|
46
|
+
*
|
47
|
+
* return -1 on failure.
|
48
|
+
* return packet length on success.
|
49
|
+
*/
|
50
|
+
int create_announce_request(uint8_t *packet, uint16_t max_packet_length, const uint8_t *dest_client_id,
|
51
|
+
const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *ping_id, const uint8_t *client_id,
|
52
|
+
const uint8_t *data_public_key, uint64_t sendback_data)
|
53
|
+
{
|
54
|
+
if (max_packet_length < ONION_ANNOUNCE_REQUEST_SIZE)
|
55
|
+
return -1;
|
56
|
+
|
57
|
+
uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES +
|
58
|
+
ONION_ANNOUNCE_SENDBACK_DATA_LENGTH];
|
59
|
+
memcpy(plain, ping_id, ONION_PING_ID_SIZE);
|
60
|
+
memcpy(plain + ONION_PING_ID_SIZE, client_id, crypto_box_PUBLICKEYBYTES);
|
61
|
+
memcpy(plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES, data_public_key, crypto_box_PUBLICKEYBYTES);
|
62
|
+
memcpy(plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES, &sendback_data,
|
63
|
+
sizeof(sendback_data));
|
64
|
+
|
65
|
+
packet[0] = NET_PACKET_ANNOUNCE_REQUEST;
|
66
|
+
random_nonce(packet + 1);
|
67
|
+
|
68
|
+
int len = encrypt_data(dest_client_id, secret_key, packet + 1, plain, sizeof(plain),
|
69
|
+
packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES);
|
70
|
+
|
71
|
+
if ((uint32_t)len + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES != ONION_ANNOUNCE_REQUEST_SIZE)
|
72
|
+
return -1;
|
73
|
+
|
74
|
+
memcpy(packet + 1 + crypto_box_NONCEBYTES, public_key, crypto_box_PUBLICKEYBYTES);
|
75
|
+
|
76
|
+
return ONION_ANNOUNCE_REQUEST_SIZE;
|
77
|
+
}
|
78
|
+
|
79
|
+
/* Create an onion data request packet in packet of max_packet_length (recommended size ONION_MAX_PACKET_SIZE).
|
80
|
+
*
|
81
|
+
* public_key is the real public key of the node which we want to send the data of length length to.
|
82
|
+
* encrypt_public_key is the public key used to encrypt the data packet.
|
83
|
+
*
|
84
|
+
* nonce is the nonce to encrypt this packet with
|
85
|
+
*
|
86
|
+
* return -1 on failure.
|
87
|
+
* return 0 on success.
|
88
|
+
*/
|
89
|
+
int create_data_request(uint8_t *packet, uint16_t max_packet_length, const uint8_t *public_key,
|
90
|
+
const uint8_t *encrypt_public_key, const uint8_t *nonce, const uint8_t *data, uint16_t length)
|
91
|
+
{
|
92
|
+
if (DATA_REQUEST_MIN_SIZE + length > max_packet_length)
|
93
|
+
return -1;
|
94
|
+
|
95
|
+
if ((unsigned int)DATA_REQUEST_MIN_SIZE + length > ONION_MAX_DATA_SIZE)
|
96
|
+
return -1;
|
97
|
+
|
98
|
+
packet[0] = NET_PACKET_ONION_DATA_REQUEST;
|
99
|
+
memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES);
|
100
|
+
memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
|
101
|
+
|
102
|
+
uint8_t random_public_key[crypto_box_PUBLICKEYBYTES];
|
103
|
+
uint8_t random_secret_key[crypto_box_SECRETKEYBYTES];
|
104
|
+
crypto_box_keypair(random_public_key, random_secret_key);
|
105
|
+
|
106
|
+
memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, random_public_key, crypto_box_PUBLICKEYBYTES);
|
107
|
+
|
108
|
+
int len = encrypt_data(encrypt_public_key, random_secret_key, packet + 1 + crypto_box_PUBLICKEYBYTES, data, length,
|
109
|
+
packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES);
|
110
|
+
|
111
|
+
if (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + len != DATA_REQUEST_MIN_SIZE +
|
112
|
+
length)
|
113
|
+
return -1;
|
114
|
+
|
115
|
+
return DATA_REQUEST_MIN_SIZE + length;
|
116
|
+
}
|
117
|
+
|
118
|
+
/* Create and send an onion announce request packet.
|
119
|
+
*
|
120
|
+
* path is the path the request will take before it is sent to dest.
|
121
|
+
*
|
122
|
+
* public_key and secret_key is the kepair which will be used to encrypt the request.
|
123
|
+
* ping_id is the ping id that will be sent in the request.
|
124
|
+
* client_id is the client id of the node we are searching for.
|
125
|
+
* data_public_key is the public key we want others to encrypt their data packets with.
|
126
|
+
* sendback_data is the data of ONION_ANNOUNCE_SENDBACK_DATA_LENGTH length that we expect to
|
127
|
+
* receive back in the response.
|
128
|
+
*
|
129
|
+
* return -1 on failure.
|
130
|
+
* return 0 on success.
|
131
|
+
*/
|
132
|
+
int send_announce_request(Networking_Core *net, const Onion_Path *path, Node_format dest, const uint8_t *public_key,
|
133
|
+
const uint8_t *secret_key, const uint8_t *ping_id, const uint8_t *client_id, const uint8_t *data_public_key,
|
134
|
+
uint64_t sendback_data)
|
135
|
+
{
|
136
|
+
uint8_t request[ONION_ANNOUNCE_REQUEST_SIZE];
|
137
|
+
int len = create_announce_request(request, sizeof(request), dest.client_id, public_key, secret_key, ping_id, client_id,
|
138
|
+
data_public_key, sendback_data);
|
139
|
+
|
140
|
+
if (len != sizeof(request))
|
141
|
+
return -1;
|
142
|
+
|
143
|
+
uint8_t packet[ONION_MAX_PACKET_SIZE];
|
144
|
+
len = create_onion_packet(packet, sizeof(packet), path, dest.ip_port, request, sizeof(request));
|
145
|
+
|
146
|
+
if (len == -1)
|
147
|
+
return -1;
|
148
|
+
|
149
|
+
if (sendpacket(net, path->ip_port1, packet, len) != len)
|
150
|
+
return -1;
|
151
|
+
|
152
|
+
return 0;
|
153
|
+
}
|
154
|
+
|
155
|
+
/* Create and send an onion data request packet.
|
156
|
+
*
|
157
|
+
* path is the path the request will take before it is sent to dest.
|
158
|
+
* (if dest knows the person with the public_key they should
|
159
|
+
* send the packet to that person in the form of a response)
|
160
|
+
*
|
161
|
+
* public_key is the real public key of the node which we want to send the data of length length to.
|
162
|
+
* encrypt_public_key is the public key used to encrypt the data packet.
|
163
|
+
*
|
164
|
+
* nonce is the nonce to encrypt this packet with
|
165
|
+
*
|
166
|
+
* return -1 on failure.
|
167
|
+
* return 0 on success.
|
168
|
+
*/
|
169
|
+
int send_data_request(Networking_Core *net, const Onion_Path *path, IP_Port dest, const uint8_t *public_key,
|
170
|
+
const uint8_t *encrypt_public_key, const uint8_t *nonce, const uint8_t *data, uint16_t length)
|
171
|
+
{
|
172
|
+
uint8_t request[ONION_MAX_DATA_SIZE];
|
173
|
+
int len = create_data_request(request, sizeof(request), public_key, encrypt_public_key, nonce, data, length);
|
174
|
+
|
175
|
+
if (len == -1)
|
176
|
+
return -1;
|
177
|
+
|
178
|
+
uint8_t packet[ONION_MAX_PACKET_SIZE];
|
179
|
+
len = create_onion_packet(packet, sizeof(packet), path, dest, request, len);
|
180
|
+
|
181
|
+
if (sendpacket(net, path->ip_port1, packet, len) != len)
|
182
|
+
return -1;
|
183
|
+
|
184
|
+
return 0;
|
185
|
+
}
|
186
|
+
|
187
|
+
/* Generate a ping_id and put it in ping_id */
|
188
|
+
static void generate_ping_id(const Onion_Announce *onion_a, uint64_t time, const uint8_t *public_key,
|
189
|
+
IP_Port ret_ip_port, uint8_t *ping_id)
|
190
|
+
{
|
191
|
+
time /= PING_ID_TIMEOUT;
|
192
|
+
uint8_t data[crypto_box_KEYBYTES + sizeof(time) + crypto_box_PUBLICKEYBYTES + sizeof(ret_ip_port)];
|
193
|
+
memcpy(data, onion_a->secret_bytes, crypto_box_KEYBYTES);
|
194
|
+
memcpy(data + crypto_box_KEYBYTES, &time, sizeof(time));
|
195
|
+
memcpy(data + crypto_box_KEYBYTES + sizeof(time), public_key, crypto_box_PUBLICKEYBYTES);
|
196
|
+
memcpy(data + crypto_box_KEYBYTES + sizeof(time) + crypto_box_PUBLICKEYBYTES, &ret_ip_port, sizeof(ret_ip_port));
|
197
|
+
crypto_hash_sha256(ping_id, data, sizeof(data));
|
198
|
+
}
|
199
|
+
|
200
|
+
/* check if public key is in entries list
|
201
|
+
*
|
202
|
+
* return -1 if no
|
203
|
+
* return position in list if yes
|
204
|
+
*/
|
205
|
+
static int in_entries(const Onion_Announce *onion_a, const uint8_t *public_key)
|
206
|
+
{
|
207
|
+
uint32_t i;
|
208
|
+
|
209
|
+
for (i = 0; i < ONION_ANNOUNCE_MAX_ENTRIES; ++i) {
|
210
|
+
if (!is_timeout(onion_a->entries[i].time, ONION_ANNOUNCE_TIMEOUT)
|
211
|
+
&& memcmp(onion_a->entries[i].public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0)
|
212
|
+
return i;
|
213
|
+
}
|
214
|
+
|
215
|
+
return -1;
|
216
|
+
}
|
217
|
+
|
218
|
+
static uint8_t cmp_public_key[crypto_box_PUBLICKEYBYTES];
|
219
|
+
static int cmp_entry(const void *a, const void *b)
|
220
|
+
{
|
221
|
+
Onion_Announce_Entry entry1, entry2;
|
222
|
+
memcpy(&entry1, a, sizeof(Onion_Announce_Entry));
|
223
|
+
memcpy(&entry2, b, sizeof(Onion_Announce_Entry));
|
224
|
+
int t1 = is_timeout(entry1.time, ONION_ANNOUNCE_TIMEOUT);
|
225
|
+
int t2 = is_timeout(entry2.time, ONION_ANNOUNCE_TIMEOUT);
|
226
|
+
|
227
|
+
if (t1 && t2)
|
228
|
+
return 0;
|
229
|
+
|
230
|
+
if (t1)
|
231
|
+
return -1;
|
232
|
+
|
233
|
+
if (t2)
|
234
|
+
return 1;
|
235
|
+
|
236
|
+
int close = id_closest(cmp_public_key, entry1.public_key, entry2.public_key);
|
237
|
+
|
238
|
+
if (close == 1)
|
239
|
+
return 1;
|
240
|
+
|
241
|
+
if (close == 2)
|
242
|
+
return -1;
|
243
|
+
|
244
|
+
return 0;
|
245
|
+
}
|
246
|
+
|
247
|
+
/* add entry to entries list
|
248
|
+
*
|
249
|
+
* return -1 if failure
|
250
|
+
* return position if added
|
251
|
+
*/
|
252
|
+
static int add_to_entries(Onion_Announce *onion_a, IP_Port ret_ip_port, const uint8_t *public_key,
|
253
|
+
const uint8_t *data_public_key, const uint8_t *ret)
|
254
|
+
{
|
255
|
+
|
256
|
+
int pos = in_entries(onion_a, public_key);
|
257
|
+
|
258
|
+
uint32_t i;
|
259
|
+
|
260
|
+
if (pos == -1) {
|
261
|
+
for (i = 0; i < ONION_ANNOUNCE_MAX_ENTRIES; ++i) {
|
262
|
+
if (is_timeout(onion_a->entries[i].time, ONION_ANNOUNCE_TIMEOUT))
|
263
|
+
pos = i;
|
264
|
+
}
|
265
|
+
}
|
266
|
+
|
267
|
+
if (pos == -1) {
|
268
|
+
if (id_closest(onion_a->dht->self_public_key, public_key, onion_a->entries[0].public_key) == 1)
|
269
|
+
pos = 0;
|
270
|
+
}
|
271
|
+
|
272
|
+
if (pos == -1)
|
273
|
+
return -1;
|
274
|
+
|
275
|
+
memcpy(onion_a->entries[pos].public_key, public_key, crypto_box_PUBLICKEYBYTES);
|
276
|
+
onion_a->entries[pos].ret_ip_port = ret_ip_port;
|
277
|
+
memcpy(onion_a->entries[pos].ret, ret, ONION_RETURN_3);
|
278
|
+
memcpy(onion_a->entries[pos].data_public_key, data_public_key, crypto_box_PUBLICKEYBYTES);
|
279
|
+
onion_a->entries[pos].time = unix_time();
|
280
|
+
|
281
|
+
memcpy(cmp_public_key, onion_a->dht->self_public_key, crypto_box_PUBLICKEYBYTES);
|
282
|
+
qsort(onion_a->entries, ONION_ANNOUNCE_MAX_ENTRIES, sizeof(Onion_Announce_Entry), cmp_entry);
|
283
|
+
return in_entries(onion_a, public_key);
|
284
|
+
}
|
285
|
+
|
286
|
+
static int handle_announce_request(void *object, IP_Port source, const uint8_t *packet, uint32_t length)
|
287
|
+
{
|
288
|
+
Onion_Announce *onion_a = object;
|
289
|
+
|
290
|
+
if (length != ANNOUNCE_REQUEST_SIZE_RECV)
|
291
|
+
return 1;
|
292
|
+
|
293
|
+
const uint8_t *packet_public_key = packet + 1 + crypto_box_NONCEBYTES;
|
294
|
+
uint8_t shared_key[crypto_box_BEFORENMBYTES];
|
295
|
+
get_shared_key(&onion_a->shared_keys_recv, shared_key, onion_a->dht->self_secret_key, packet_public_key);
|
296
|
+
|
297
|
+
uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES +
|
298
|
+
ONION_ANNOUNCE_SENDBACK_DATA_LENGTH];
|
299
|
+
int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
|
300
|
+
ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH +
|
301
|
+
crypto_box_MACBYTES, plain);
|
302
|
+
|
303
|
+
if ((uint32_t)len != sizeof(plain))
|
304
|
+
return 1;
|
305
|
+
|
306
|
+
uint8_t ping_id1[ONION_PING_ID_SIZE];
|
307
|
+
generate_ping_id(onion_a, unix_time(), packet_public_key, source, ping_id1);
|
308
|
+
|
309
|
+
uint8_t ping_id2[ONION_PING_ID_SIZE];
|
310
|
+
generate_ping_id(onion_a, unix_time() + PING_ID_TIMEOUT, packet_public_key, source, ping_id2);
|
311
|
+
|
312
|
+
int index = -1;
|
313
|
+
|
314
|
+
uint8_t *data_public_key = plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES;
|
315
|
+
|
316
|
+
if (memcmp(ping_id1, plain, ONION_PING_ID_SIZE) == 0 || memcmp(ping_id2, plain, ONION_PING_ID_SIZE) == 0) {
|
317
|
+
index = add_to_entries(onion_a, source, packet_public_key, data_public_key,
|
318
|
+
packet + (ANNOUNCE_REQUEST_SIZE_RECV - ONION_RETURN_3));
|
319
|
+
} else {
|
320
|
+
index = in_entries(onion_a, plain + ONION_PING_ID_SIZE);
|
321
|
+
}
|
322
|
+
|
323
|
+
/*Respond with a announce response packet*/
|
324
|
+
Node_format nodes_list[MAX_SENT_NODES];
|
325
|
+
uint32_t num_nodes = get_close_nodes(onion_a->dht, plain + ONION_PING_ID_SIZE, nodes_list, 0, LAN_ip(source.ip) == 0,
|
326
|
+
1);
|
327
|
+
uint8_t nonce[crypto_box_NONCEBYTES];
|
328
|
+
random_nonce(nonce);
|
329
|
+
|
330
|
+
uint8_t pl[1 + ONION_PING_ID_SIZE + sizeof(nodes_list)];
|
331
|
+
|
332
|
+
if (index == -1) {
|
333
|
+
pl[0] = 0;
|
334
|
+
memcpy(pl + 1, ping_id2, ONION_PING_ID_SIZE);
|
335
|
+
} else {
|
336
|
+
if (memcmp(onion_a->entries[index].public_key, packet_public_key, crypto_box_PUBLICKEYBYTES) == 0) {
|
337
|
+
if (memcmp(onion_a->entries[index].data_public_key, data_public_key, crypto_box_PUBLICKEYBYTES) != 0) {
|
338
|
+
pl[0] = 0;
|
339
|
+
memcpy(pl + 1, ping_id2, ONION_PING_ID_SIZE);
|
340
|
+
} else {
|
341
|
+
pl[0] = 2;
|
342
|
+
memcpy(pl + 1, ping_id2, ONION_PING_ID_SIZE);
|
343
|
+
}
|
344
|
+
} else {
|
345
|
+
pl[0] = 1;
|
346
|
+
memcpy(pl + 1, onion_a->entries[index].data_public_key, crypto_box_PUBLICKEYBYTES);
|
347
|
+
}
|
348
|
+
}
|
349
|
+
|
350
|
+
int nodes_length = 0;
|
351
|
+
|
352
|
+
if (num_nodes != 0) {
|
353
|
+
nodes_length = pack_nodes(pl + 1 + ONION_PING_ID_SIZE, sizeof(nodes_list), nodes_list, num_nodes);
|
354
|
+
|
355
|
+
if (nodes_length <= 0)
|
356
|
+
return 1;
|
357
|
+
}
|
358
|
+
|
359
|
+
uint8_t data[ONION_ANNOUNCE_RESPONSE_MAX_SIZE];
|
360
|
+
len = encrypt_data_symmetric(shared_key, nonce, pl, 1 + ONION_PING_ID_SIZE + nodes_length,
|
361
|
+
data + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES);
|
362
|
+
|
363
|
+
if (len != 1 + ONION_PING_ID_SIZE + nodes_length + crypto_box_MACBYTES)
|
364
|
+
return 1;
|
365
|
+
|
366
|
+
data[0] = NET_PACKET_ANNOUNCE_RESPONSE;
|
367
|
+
memcpy(data + 1, plain + ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES,
|
368
|
+
ONION_ANNOUNCE_SENDBACK_DATA_LENGTH);
|
369
|
+
memcpy(data + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH, nonce, crypto_box_NONCEBYTES);
|
370
|
+
|
371
|
+
if (send_onion_response(onion_a->net, source, data,
|
372
|
+
1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES + len,
|
373
|
+
packet + (ANNOUNCE_REQUEST_SIZE_RECV - ONION_RETURN_3)) == -1)
|
374
|
+
return 1;
|
375
|
+
|
376
|
+
return 0;
|
377
|
+
}
|
378
|
+
|
379
|
+
static int handle_data_request(void *object, IP_Port source, const uint8_t *packet, uint32_t length)
|
380
|
+
{
|
381
|
+
Onion_Announce *onion_a = object;
|
382
|
+
|
383
|
+
if (length <= DATA_REQUEST_MIN_SIZE_RECV)
|
384
|
+
return 1;
|
385
|
+
|
386
|
+
if (length > ONION_MAX_PACKET_SIZE)
|
387
|
+
return 1;
|
388
|
+
|
389
|
+
int index = in_entries(onion_a, packet + 1);
|
390
|
+
|
391
|
+
if (index == -1)
|
392
|
+
return 1;
|
393
|
+
|
394
|
+
uint8_t data[length - (crypto_box_PUBLICKEYBYTES + ONION_RETURN_3)];
|
395
|
+
data[0] = NET_PACKET_ONION_DATA_RESPONSE;
|
396
|
+
memcpy(data + 1, packet + 1 + crypto_box_PUBLICKEYBYTES, length - (1 + crypto_box_PUBLICKEYBYTES + ONION_RETURN_3));
|
397
|
+
|
398
|
+
if (send_onion_response(onion_a->net, onion_a->entries[index].ret_ip_port, data, sizeof(data),
|
399
|
+
onion_a->entries[index].ret) == -1)
|
400
|
+
return 1;
|
401
|
+
|
402
|
+
return 0;
|
403
|
+
}
|
404
|
+
|
405
|
+
Onion_Announce *new_onion_announce(DHT *dht)
|
406
|
+
{
|
407
|
+
if (dht == NULL)
|
408
|
+
return NULL;
|
409
|
+
|
410
|
+
Onion_Announce *onion_a = calloc(1, sizeof(Onion_Announce));
|
411
|
+
|
412
|
+
if (onion_a == NULL)
|
413
|
+
return NULL;
|
414
|
+
|
415
|
+
onion_a->dht = dht;
|
416
|
+
onion_a->net = dht->net;
|
417
|
+
new_symmetric_key(onion_a->secret_bytes);
|
418
|
+
|
419
|
+
networking_registerhandler(onion_a->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_announce_request, onion_a);
|
420
|
+
networking_registerhandler(onion_a->net, NET_PACKET_ONION_DATA_REQUEST, &handle_data_request, onion_a);
|
421
|
+
|
422
|
+
return onion_a;
|
423
|
+
}
|
424
|
+
|
425
|
+
void kill_onion_announce(Onion_Announce *onion_a)
|
426
|
+
{
|
427
|
+
if (onion_a == NULL)
|
428
|
+
return;
|
429
|
+
|
430
|
+
networking_registerhandler(onion_a->net, NET_PACKET_ANNOUNCE_REQUEST, NULL, NULL);
|
431
|
+
networking_registerhandler(onion_a->net, NET_PACKET_ONION_DATA_REQUEST, NULL, NULL);
|
432
|
+
free(onion_a);
|
433
|
+
}
|