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,157 @@
|
|
1
|
+
/* Messenger.h
|
2
|
+
*
|
3
|
+
* An implementation of a simple text chat only messenger on the tox network core.
|
4
|
+
*
|
5
|
+
* NOTE: All the text in the messages must be encoded using UTF-8
|
6
|
+
*
|
7
|
+
* Copyright (C) 2013 Tox project All Rights Reserved.
|
8
|
+
*
|
9
|
+
* This file is part of Tox.
|
10
|
+
*
|
11
|
+
* Tox is free software: you can redistribute it and/or modify
|
12
|
+
* it under the terms of the GNU General Public License as published by
|
13
|
+
* the Free Software Foundation, either version 3 of the License, or
|
14
|
+
* (at your option) any later version.
|
15
|
+
*
|
16
|
+
* Tox is distributed in the hope that it will be useful,
|
17
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19
|
+
* GNU General Public License for more details.
|
20
|
+
*
|
21
|
+
* You should have received a copy of the GNU General Public License
|
22
|
+
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
23
|
+
*
|
24
|
+
*/
|
25
|
+
|
26
|
+
#ifndef MESSENGER_H
|
27
|
+
#define MESSENGER_H
|
28
|
+
|
29
|
+
#include "net_crypto.h"
|
30
|
+
#include "DHT.h"
|
31
|
+
#include "friend_requests.h"
|
32
|
+
#include "LAN_discovery.h"
|
33
|
+
|
34
|
+
#ifdef __cplusplus
|
35
|
+
extern "C" {
|
36
|
+
#endif
|
37
|
+
|
38
|
+
#define MAX_NAME_LENGTH 128
|
39
|
+
#define MAX_USERSTATUS_LENGTH 128
|
40
|
+
|
41
|
+
#define PACKET_ID_NICKNAME 48
|
42
|
+
#define PACKET_ID_USERSTATUS 49
|
43
|
+
#define PACKET_ID_MESSAGE 64
|
44
|
+
|
45
|
+
/* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased
|
46
|
+
to an absurdly large number later */
|
47
|
+
|
48
|
+
/* add a friend
|
49
|
+
set the data that will be sent along with friend request
|
50
|
+
client_id is the client id of the friend
|
51
|
+
data is the data and length is the length
|
52
|
+
returns the friend number if success
|
53
|
+
return -1 if failure. */
|
54
|
+
int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length);
|
55
|
+
|
56
|
+
|
57
|
+
/* add a friend without sending a friendrequest.
|
58
|
+
returns the friend number if success
|
59
|
+
return -1 if failure. */
|
60
|
+
int m_addfriend_norequest(uint8_t *client_id);
|
61
|
+
|
62
|
+
/* return the friend id associated to that client id.
|
63
|
+
return -1 if no such friend */
|
64
|
+
int getfriend_id(uint8_t *client_id);
|
65
|
+
|
66
|
+
/* copies the public key associated to that friend id into client_id buffer.
|
67
|
+
make sure that client_id is of size CLIENT_ID_SIZE.
|
68
|
+
return 0 if success
|
69
|
+
return -1 if failure */
|
70
|
+
int getclient_id(int friend_id, uint8_t *client_id);
|
71
|
+
|
72
|
+
/* remove a friend */
|
73
|
+
int m_delfriend(int friendnumber);
|
74
|
+
|
75
|
+
/* return 4 if friend is online
|
76
|
+
return 3 if friend is confirmed
|
77
|
+
return 2 if the friend request was sent
|
78
|
+
return 1 if the friend was added
|
79
|
+
return 0 if there is no friend with that number */
|
80
|
+
int m_friendstatus(int friendnumber);
|
81
|
+
|
82
|
+
/* send a text chat message to an online friend
|
83
|
+
returns 1 if packet was successfully put into the send queue
|
84
|
+
return 0 if it was not */
|
85
|
+
int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length);
|
86
|
+
|
87
|
+
/* Set our nickname
|
88
|
+
name must be a string of maximum MAX_NAME_LENGTH length.
|
89
|
+
length must be at least 1 byte
|
90
|
+
length is the length of name with the NULL terminator
|
91
|
+
return 0 if success
|
92
|
+
return -1 if failure */
|
93
|
+
int setname(uint8_t *name, uint16_t length);
|
94
|
+
|
95
|
+
/* get name of friendnumber
|
96
|
+
put it in name
|
97
|
+
name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
|
98
|
+
return 0 if success
|
99
|
+
return -1 if failure */
|
100
|
+
int getname(int friendnumber, uint8_t *name);
|
101
|
+
|
102
|
+
/* set our user status
|
103
|
+
you are responsible for freeing status after
|
104
|
+
returns 0 on success, -1 on failure */
|
105
|
+
int m_set_userstatus(uint8_t *status, uint16_t length);
|
106
|
+
|
107
|
+
/* return the length of friendnumber's user status,
|
108
|
+
including null
|
109
|
+
pass it into malloc */
|
110
|
+
int m_get_userstatus_size(int friendnumber);
|
111
|
+
|
112
|
+
/* copy friendnumber's userstatus into buf, truncating if size is over maxlen
|
113
|
+
get the size you need to allocate from m_get_userstatus_size */
|
114
|
+
int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen);
|
115
|
+
|
116
|
+
/* set the function that will be executed when a friend request is received.
|
117
|
+
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
|
118
|
+
void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
|
119
|
+
|
120
|
+
/* set the function that will be executed when a message from a friend is received.
|
121
|
+
function format is: function(int friendnumber, uint8_t * message, uint32_t length) */
|
122
|
+
void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t));
|
123
|
+
|
124
|
+
/* set the callback for name changes
|
125
|
+
function(int friendnumber, uint8_t *newname, uint16_t length)
|
126
|
+
you are not responsible for freeing newname */
|
127
|
+
void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t));
|
128
|
+
|
129
|
+
/* set the callback for user status changes
|
130
|
+
function(int friendnumber, uint8_t *newstatus, uint16_t length)
|
131
|
+
you are not responsible for freeing newstatus */
|
132
|
+
void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t));
|
133
|
+
|
134
|
+
/* run this at startup
|
135
|
+
returns 0 if no connection problems
|
136
|
+
returns -1 if there are problems */
|
137
|
+
int initMessenger();
|
138
|
+
|
139
|
+
/* the main loop that needs to be run at least 200 times per second */
|
140
|
+
void doMessenger();
|
141
|
+
|
142
|
+
/* SAVING AND LOADING FUNCTIONS: */
|
143
|
+
|
144
|
+
/* returns the size of the messenger data (for saving) */
|
145
|
+
uint32_t Messenger_size();
|
146
|
+
|
147
|
+
/* save the messenger in data (must be allocated memory of size Messenger_size()) */
|
148
|
+
void Messenger_save(uint8_t *data);
|
149
|
+
|
150
|
+
/* load the messenger from data of size length */
|
151
|
+
int Messenger_load(uint8_t *data, uint32_t length);
|
152
|
+
|
153
|
+
#ifdef __cplusplus
|
154
|
+
}
|
155
|
+
#endif
|
156
|
+
|
157
|
+
#endif
|
@@ -0,0 +1,131 @@
|
|
1
|
+
/* friend_requests.c
|
2
|
+
*
|
3
|
+
* Handle friend requests.
|
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 "friend_requests.h"
|
25
|
+
|
26
|
+
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
|
27
|
+
|
28
|
+
/* Try to send a friendrequest to peer with public_key
|
29
|
+
data is the data in the request and length is the length.
|
30
|
+
return -1 if failure.
|
31
|
+
return 0 if it sent the friend request directly to the friend.
|
32
|
+
return the number of peers it was routed through if it did not send it directly.*/
|
33
|
+
int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length)
|
34
|
+
{
|
35
|
+
uint8_t packet[MAX_DATA_SIZE];
|
36
|
+
int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */
|
37
|
+
|
38
|
+
if (len == -1)
|
39
|
+
return -1;
|
40
|
+
|
41
|
+
IP_Port ip_port = DHT_getfriendip(public_key);
|
42
|
+
|
43
|
+
if (ip_port.ip.i == 1)
|
44
|
+
return -1;
|
45
|
+
|
46
|
+
if (ip_port.ip.i != 0) {
|
47
|
+
if (sendpacket(ip_port, packet, len) != -1)
|
48
|
+
return 0;
|
49
|
+
return -1;
|
50
|
+
}
|
51
|
+
|
52
|
+
int num = route_tofriend(public_key, packet, len);
|
53
|
+
|
54
|
+
if (num == 0)
|
55
|
+
return -1;
|
56
|
+
|
57
|
+
return num;
|
58
|
+
}
|
59
|
+
|
60
|
+
static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t);
|
61
|
+
static uint8_t handle_friendrequest_isset = 0;
|
62
|
+
|
63
|
+
/* set the function that will be executed when a friend request is received. */
|
64
|
+
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
|
65
|
+
{
|
66
|
+
handle_friendrequest = function;
|
67
|
+
handle_friendrequest_isset = 1;
|
68
|
+
}
|
69
|
+
|
70
|
+
|
71
|
+
/*NOTE: the following is just a temporary fix for the multiple friend requests recieved at the same time problem
|
72
|
+
TODO: Make this better (This will most likely tie in with the way we will handle spam.)*/
|
73
|
+
|
74
|
+
#define MAX_RECIEVED_STORED 32
|
75
|
+
|
76
|
+
static uint8_t recieved_requests[MAX_RECIEVED_STORED][crypto_box_PUBLICKEYBYTES];
|
77
|
+
static uint16_t recieved_requests_index;
|
78
|
+
|
79
|
+
/*Add to list of recieved friend requests*/
|
80
|
+
static void addto_recievedlist(uint8_t * client_id)
|
81
|
+
{
|
82
|
+
if (recieved_requests_index >= MAX_RECIEVED_STORED)
|
83
|
+
recieved_requests_index = 0;
|
84
|
+
|
85
|
+
memcpy(recieved_requests[recieved_requests_index], client_id, crypto_box_PUBLICKEYBYTES);
|
86
|
+
++recieved_requests_index;
|
87
|
+
}
|
88
|
+
|
89
|
+
/* Check if a friend request was already recieved
|
90
|
+
return 0 if not, 1 if we did */
|
91
|
+
static int request_recieved(uint8_t * client_id)
|
92
|
+
{
|
93
|
+
uint32_t i;
|
94
|
+
|
95
|
+
for (i = 0; i < MAX_RECIEVED_STORED; ++i) {
|
96
|
+
if (memcmp(recieved_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0)
|
97
|
+
return 1;
|
98
|
+
}
|
99
|
+
|
100
|
+
return 0;
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
|
105
|
+
{
|
106
|
+
if (packet[0] == 32) {
|
107
|
+
if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
|
108
|
+
length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
|
109
|
+
return 1;
|
110
|
+
if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us.
|
111
|
+
if (handle_friendrequest_isset == 0)
|
112
|
+
return 1;
|
113
|
+
|
114
|
+
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
115
|
+
uint8_t data[MAX_DATA_SIZE];
|
116
|
+
int len = handle_request(public_key, data, packet, length);
|
117
|
+
|
118
|
+
if (len == -1)
|
119
|
+
return 1;
|
120
|
+
if (request_recieved(public_key))
|
121
|
+
return 1;
|
122
|
+
|
123
|
+
addto_recievedlist(public_key);
|
124
|
+
(*handle_friendrequest)(public_key, data, len);
|
125
|
+
} else { /* if request is not for us, try routing it. */
|
126
|
+
if(route_packet(packet + 1, packet, length) == length)
|
127
|
+
return 0;
|
128
|
+
}
|
129
|
+
}
|
130
|
+
return 1;
|
131
|
+
}
|
@@ -0,0 +1,51 @@
|
|
1
|
+
/* friend_requests.h
|
2
|
+
*
|
3
|
+
* Handle friend requests.
|
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 FRIEND_REQUESTS_H
|
25
|
+
#define FRIEND_REQUESTS_H
|
26
|
+
|
27
|
+
#include "DHT.h"
|
28
|
+
#include "net_crypto.h"
|
29
|
+
|
30
|
+
#ifdef __cplusplus
|
31
|
+
extern "C" {
|
32
|
+
#endif
|
33
|
+
|
34
|
+
/* Try to send a friendrequest to peer with public_key
|
35
|
+
data is the data in the request and length is the length. */
|
36
|
+
int send_friendrequest(uint8_t *public_key, uint8_t *data, uint32_t length);
|
37
|
+
|
38
|
+
/* set the function that will be executed when a friend request for us is received.
|
39
|
+
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
|
40
|
+
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
|
41
|
+
|
42
|
+
/* if we receive a packet we call this function so it can be handled.
|
43
|
+
return 0 if packet is handled correctly.
|
44
|
+
return 1 if it didn't handle the packet or if the packet was shit. */
|
45
|
+
int friendreq_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
|
46
|
+
|
47
|
+
#ifdef __cplusplus
|
48
|
+
}
|
49
|
+
#endif
|
50
|
+
|
51
|
+
#endif
|