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.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/ProjectTox-Core/AUTHORS +0 -0
  3. data/ProjectTox-Core/ChangeLog +0 -0
  4. data/ProjectTox-Core/INSTALL +370 -0
  5. data/ProjectTox-Core/INSTALL.md +455 -56
  6. data/ProjectTox-Core/Makefile.am +35 -0
  7. data/ProjectTox-Core/NEWS +0 -0
  8. data/ProjectTox-Core/README +43 -0
  9. data/ProjectTox-Core/README.md +34 -44
  10. data/ProjectTox-Core/auto_tests/Makefile.inc +110 -0
  11. data/ProjectTox-Core/auto_tests/TCP_test.c +519 -0
  12. data/ProjectTox-Core/auto_tests/assoc_test.c +160 -0
  13. data/ProjectTox-Core/auto_tests/crypto_test.c +302 -0
  14. data/ProjectTox-Core/auto_tests/dht_test.c +362 -0
  15. data/ProjectTox-Core/auto_tests/encryptsave_test.c +104 -0
  16. data/ProjectTox-Core/auto_tests/friends_test.c +238 -0
  17. data/ProjectTox-Core/auto_tests/helpers.h +15 -0
  18. data/ProjectTox-Core/auto_tests/messenger_test.c +365 -0
  19. data/ProjectTox-Core/auto_tests/network_test.c +171 -0
  20. data/ProjectTox-Core/auto_tests/onion_test.c +363 -0
  21. data/ProjectTox-Core/auto_tests/skeleton_test.c +49 -0
  22. data/ProjectTox-Core/auto_tests/tox_test.c +454 -0
  23. data/ProjectTox-Core/auto_tests/toxav_basic_test.c +597 -0
  24. data/ProjectTox-Core/auto_tests/toxav_many_test.c +402 -0
  25. data/ProjectTox-Core/autogen.sh +6 -0
  26. data/ProjectTox-Core/build/Makefile.am +14 -0
  27. data/ProjectTox-Core/configure.ac +694 -0
  28. data/ProjectTox-Core/dist-build/android-arm.sh +3 -0
  29. data/ProjectTox-Core/dist-build/android-armv7.sh +3 -0
  30. data/ProjectTox-Core/dist-build/android-build.sh +59 -0
  31. data/ProjectTox-Core/dist-build/android-mips.sh +3 -0
  32. data/ProjectTox-Core/dist-build/android-x86.sh +3 -0
  33. data/ProjectTox-Core/docs/Group-Chats.md +71 -0
  34. data/ProjectTox-Core/docs/Hardening.txt +60 -0
  35. data/ProjectTox-Core/docs/Hardening_docs.txt +30 -0
  36. data/ProjectTox-Core/docs/Prevent_Tracking.txt +160 -0
  37. data/ProjectTox-Core/docs/TCP_Network.txt +154 -0
  38. data/ProjectTox-Core/docs/TODO +62 -0
  39. data/ProjectTox-Core/docs/Tox_middle_level_network_protocol.txt +120 -0
  40. data/ProjectTox-Core/docs/av_api.md +194 -0
  41. data/ProjectTox-Core/libtoxav.pc.in +11 -0
  42. data/ProjectTox-Core/libtoxcore.pc.in +11 -0
  43. data/ProjectTox-Core/m4/ax_have_epoll.m4 +104 -0
  44. data/ProjectTox-Core/m4/ax_pthread.m4 +317 -0
  45. data/ProjectTox-Core/m4/pkg.m4 +199 -0
  46. data/ProjectTox-Core/other/DHT_bootstrap.c +121 -58
  47. data/ProjectTox-Core/other/DHTnodes +3 -0
  48. data/ProjectTox-Core/other/Makefile.inc +20 -0
  49. data/ProjectTox-Core/other/bootstrap_node_packets.c +65 -0
  50. data/ProjectTox-Core/other/tox.png +0 -0
  51. data/ProjectTox-Core/testing/DHT_test.c +170 -98
  52. data/ProjectTox-Core/testing/Makefile.inc +112 -0
  53. data/ProjectTox-Core/testing/Messenger_test.c +133 -69
  54. data/ProjectTox-Core/testing/dns3_test.c +115 -0
  55. data/ProjectTox-Core/testing/misc_tools.c +59 -13
  56. data/ProjectTox-Core/testing/nTox.c +1127 -264
  57. data/ProjectTox-Core/testing/nTox.h +10 -19
  58. data/ProjectTox-Core/testing/tox_shell.c +159 -0
  59. data/ProjectTox-Core/testing/tox_sync.c +299 -0
  60. data/ProjectTox-Core/tools/README +11 -0
  61. data/ProjectTox-Core/tools/astylerc +11 -0
  62. data/ProjectTox-Core/tools/pre-commit +17 -0
  63. data/ProjectTox-Core/toxav/Makefile.inc +36 -0
  64. data/ProjectTox-Core/toxav/codec.c +357 -0
  65. data/ProjectTox-Core/toxav/codec.h +116 -0
  66. data/ProjectTox-Core/toxav/msi.c +1949 -0
  67. data/ProjectTox-Core/toxav/msi.h +267 -0
  68. data/ProjectTox-Core/toxav/rtp.c +600 -0
  69. data/ProjectTox-Core/toxav/rtp.h +196 -0
  70. data/ProjectTox-Core/toxav/toxav.c +1148 -0
  71. data/ProjectTox-Core/toxav/toxav.h +389 -0
  72. data/ProjectTox-Core/toxcore/DHT.c +2521 -0
  73. data/ProjectTox-Core/toxcore/DHT.h +412 -0
  74. data/ProjectTox-Core/toxcore/LAN_discovery.c +322 -0
  75. data/ProjectTox-Core/{core → toxcore}/LAN_discovery.h +17 -12
  76. data/ProjectTox-Core/toxcore/Makefile.inc +67 -0
  77. data/ProjectTox-Core/toxcore/Messenger.c +3006 -0
  78. data/ProjectTox-Core/toxcore/Messenger.h +818 -0
  79. data/ProjectTox-Core/toxcore/TCP_client.c +858 -0
  80. data/ProjectTox-Core/toxcore/TCP_client.h +156 -0
  81. data/ProjectTox-Core/toxcore/TCP_server.c +1332 -0
  82. data/ProjectTox-Core/toxcore/TCP_server.h +181 -0
  83. data/ProjectTox-Core/toxcore/assoc.c +1033 -0
  84. data/ProjectTox-Core/toxcore/assoc.h +104 -0
  85. data/ProjectTox-Core/toxcore/crypto_core.c +278 -0
  86. data/ProjectTox-Core/toxcore/crypto_core.h +151 -0
  87. data/ProjectTox-Core/toxcore/friend_requests.c +175 -0
  88. data/ProjectTox-Core/toxcore/friend_requests.h +83 -0
  89. data/ProjectTox-Core/toxcore/group_chats.c +837 -0
  90. data/ProjectTox-Core/toxcore/group_chats.h +199 -0
  91. data/ProjectTox-Core/toxcore/list.c +256 -0
  92. data/ProjectTox-Core/toxcore/list.h +85 -0
  93. data/ProjectTox-Core/toxcore/logger.c +153 -0
  94. data/ProjectTox-Core/toxcore/logger.h +84 -0
  95. data/ProjectTox-Core/toxcore/misc_tools.h +70 -0
  96. data/ProjectTox-Core/toxcore/net_crypto.c +2753 -0
  97. data/ProjectTox-Core/toxcore/net_crypto.h +410 -0
  98. data/ProjectTox-Core/toxcore/network.c +979 -0
  99. data/ProjectTox-Core/toxcore/network.h +367 -0
  100. data/ProjectTox-Core/toxcore/onion.c +540 -0
  101. data/ProjectTox-Core/toxcore/onion.h +150 -0
  102. data/ProjectTox-Core/toxcore/onion_announce.c +433 -0
  103. data/ProjectTox-Core/toxcore/onion_announce.h +139 -0
  104. data/ProjectTox-Core/toxcore/onion_client.c +1347 -0
  105. data/ProjectTox-Core/toxcore/onion_client.h +253 -0
  106. data/ProjectTox-Core/toxcore/ping.c +346 -0
  107. data/ProjectTox-Core/toxcore/ping.h +47 -0
  108. data/ProjectTox-Core/toxcore/ping_array.c +162 -0
  109. data/ProjectTox-Core/toxcore/ping_array.h +75 -0
  110. data/ProjectTox-Core/toxcore/tox.c +940 -0
  111. data/ProjectTox-Core/toxcore/tox.h +734 -0
  112. data/ProjectTox-Core/toxcore/util.c +193 -0
  113. data/ProjectTox-Core/toxcore/util.h +63 -0
  114. data/ProjectTox-Core/toxdns/Makefile.inc +29 -0
  115. data/ProjectTox-Core/toxdns/toxdns.c +238 -0
  116. data/ProjectTox-Core/toxdns/toxdns.h +88 -0
  117. data/ProjectTox-Core/toxencryptsave/Makefile.inc +45 -0
  118. data/ProjectTox-Core/toxencryptsave/toxencryptsave.c +179 -0
  119. data/ProjectTox-Core/toxencryptsave/toxencryptsave.h +74 -0
  120. data/interfaces/libtox.i +2 -6
  121. data/lib/ffi-tox/libtox.rb +406 -28
  122. metadata +124 -46
  123. data/ProjectTox-Core/CMakeLists.txt +0 -50
  124. data/ProjectTox-Core/cmake/FindLIBCONFIG.cmake +0 -15
  125. data/ProjectTox-Core/cmake/FindNaCl.cmake +0 -17
  126. data/ProjectTox-Core/cmake/FindSODIUM.cmake +0 -15
  127. data/ProjectTox-Core/core/CMakeLists.txt +0 -19
  128. data/ProjectTox-Core/core/DHT.c +0 -1104
  129. data/ProjectTox-Core/core/DHT.h +0 -111
  130. data/ProjectTox-Core/core/LAN_discovery.c +0 -79
  131. data/ProjectTox-Core/core/Lossless_UDP.c +0 -755
  132. data/ProjectTox-Core/core/Lossless_UDP.h +0 -106
  133. data/ProjectTox-Core/core/Messenger.c +0 -596
  134. data/ProjectTox-Core/core/Messenger.h +0 -165
  135. data/ProjectTox-Core/core/friend_requests.c +0 -131
  136. data/ProjectTox-Core/core/friend_requests.h +0 -51
  137. data/ProjectTox-Core/core/net_crypto.c +0 -575
  138. data/ProjectTox-Core/core/net_crypto.h +0 -134
  139. data/ProjectTox-Core/core/network.c +0 -205
  140. data/ProjectTox-Core/core/network.h +0 -134
  141. data/ProjectTox-Core/docs/commands.md +0 -25
  142. data/ProjectTox-Core/docs/start_guide.de.md +0 -40
  143. data/ProjectTox-Core/docs/start_guide.md +0 -38
  144. data/ProjectTox-Core/other/CMakeLists.txt +0 -9
  145. data/ProjectTox-Core/testing/CMakeLists.txt +0 -18
  146. data/ProjectTox-Core/testing/DHT_cryptosendfiletest.c +0 -228
  147. data/ProjectTox-Core/testing/DHT_sendfiletest.c +0 -176
  148. data/ProjectTox-Core/testing/Lossless_UDP_testclient.c +0 -214
  149. data/ProjectTox-Core/testing/Lossless_UDP_testserver.c +0 -201
  150. data/ProjectTox-Core/testing/misc_tools.h +0 -29
  151. data/ProjectTox-Core/testing/nTox_win32.c +0 -387
  152. data/ProjectTox-Core/testing/nTox_win32.h +0 -40
  153. data/ProjectTox-Core/testing/rect.py +0 -45
@@ -1,106 +0,0 @@
1
- /* Lossless_UDP.h
2
- *
3
- * An implementation of the Lossless_UDP protocol as seen in docs/Lossless_UDP.txt
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 LOSSLESS_UDP_H
25
- #define LOSSLESS_UDP_H
26
-
27
- #include "network.h"
28
-
29
- #ifdef __cplusplus
30
- extern "C" {
31
- #endif
32
-
33
- /* maximum length of the data in the data packets */
34
- #define MAX_DATA_SIZE 1024
35
-
36
- /* Functions */
37
-
38
- /* initialize a new connection to ip_port
39
- returns an integer corresponding to the connection id.
40
- return -1 if it could not initialize the connection.
41
- if there already was an existing connection to that ip_port return its number. */
42
- int new_connection(IP_Port ip_port);
43
-
44
- /* get connection id from IP_Port
45
- return -1 if there are no connections like we are looking for
46
- return id if it found it */
47
- int getconnection_id(IP_Port ip_port);
48
-
49
- /* returns an integer corresponding to the next connection in our imcoming connection list
50
- return -1 if there are no new incoming connections in the list. */
51
- int incoming_connection();
52
-
53
- /* return -1 if it could not kill the connection.
54
- return 0 if killed successfully */
55
- int kill_connection(int connection_id);
56
-
57
- /* kill connection in seconds seconds.
58
- return -1 if it can not kill the connection.
59
- return 0 if it will kill it */
60
- int kill_connection_in(int connection_id, uint32_t seconds);
61
-
62
- /* returns the ip_port of the corresponding connection.
63
- return 0 if there is no such connection. */
64
- IP_Port connection_ip(int connection_id);
65
-
66
- /* returns the id of the next packet in the queue
67
- return -1 if no packet in queue */
68
- char id_packet(int connection_id);
69
-
70
- /* return 0 if there is no received data in the buffer.
71
- return length of received packet if successful */
72
- int read_packet(int connection_id, uint8_t *data);
73
-
74
- /* return 0 if data could not be put in packet queue
75
- return 1 if data was put into the queue */
76
- int write_packet(int connection_id, uint8_t *data, uint32_t length);
77
-
78
- /* returns the number of packets in the queue waiting to be successfully sent. */
79
- uint32_t sendqueue(int connection_id);
80
-
81
- /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */
82
- uint32_t recvqueue(int connection_id);
83
-
84
- /* check if connection is connected
85
- return 0 no.
86
- return 1 if attempting handshake
87
- return 2 if handshake is done
88
- return 3 if fully connected
89
- return 4 if timed out and wating to be killed */
90
- int is_connected(int connection_id);
91
-
92
- /* Call this function a couple times per second
93
- It's the main loop. */
94
- void doLossless_UDP();
95
-
96
-
97
- /* if we receive a Lossless_UDP packet we call this function so it can be handled.
98
- return 0 if packet is handled correctly.
99
- return 1 if it didn't handle the packet or if the packet was shit. */
100
- int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
101
-
102
- #ifdef __cplusplus
103
- }
104
- #endif
105
-
106
- #endif
@@ -1,596 +0,0 @@
1
- /* Messenger.c
2
- *
3
- * An implementation of a simple text chat only messenger on the tox network core.
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 "Messenger.h"
25
- #define MIN(a,b) (((a)<(b))?(a):(b))
26
-
27
- typedef struct {
28
- uint8_t client_id[CLIENT_ID_SIZE];
29
- int crypt_connection_id;
30
- int friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */
31
- uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */
32
- uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */
33
- uint8_t name[MAX_NAME_LENGTH];
34
- uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */
35
- uint8_t *userstatus;
36
- uint16_t userstatus_length;
37
- uint8_t userstatus_sent;
38
- uint16_t info_size; /* length of the info */
39
- } Friend;
40
-
41
- uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
42
-
43
- static uint8_t self_name[MAX_NAME_LENGTH];
44
- static uint16_t self_name_length;
45
-
46
- static uint8_t *self_userstatus;
47
- static uint16_t self_userstatus_len;
48
-
49
- #define MAX_NUM_FRIENDS 256
50
-
51
- static Friend friendlist[MAX_NUM_FRIENDS];
52
-
53
- static uint32_t numfriends;
54
-
55
- /* 1 if we are online
56
- 0 if we are offline
57
- static uint8_t online; */
58
-
59
- /* return the friend id associated to that public key.
60
- return -1 if no such friend */
61
- int getfriend_id(uint8_t *client_id)
62
- {
63
- uint32_t i;
64
-
65
- for (i = 0; i < numfriends; ++i) {
66
- if (friendlist[i].status > 0)
67
- if (memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0)
68
- return i;
69
- }
70
-
71
- return -1;
72
- }
73
-
74
- /* Returns number of friends */
75
- int getnumfriends()
76
- {
77
- return numfriends;
78
- }
79
-
80
- /* copies the public key associated to that friend id into client_id buffer.
81
- make sure that client_id is of size CLIENT_ID_SIZE.
82
- return 0 if success
83
- return -1 if failure. */
84
- int getclient_id(int friend_id, uint8_t *client_id)
85
- {
86
- if (friend_id >= numfriends || friend_id < 0)
87
- return -1;
88
-
89
- if (friendlist[friend_id].status > 0) {
90
- memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE);
91
- return 0;
92
- }
93
-
94
- return -1;
95
- }
96
-
97
- /* add a friend
98
- set the data that will be sent along with friend request
99
- client_id is the client id of the friend
100
- data is the data and length is the length
101
- returns the friend number if success
102
- return -1 if key length is wrong.
103
- return -2 if user's own key
104
- return -3 if already a friend
105
- return -4 for other*/
106
- int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
107
- {
108
- if (length == 0 || length >=
109
- (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES))
110
- return -1;
111
- if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
112
- return -2;
113
- if (getfriend_id(client_id) != -1)
114
- return -3;
115
- uint32_t i;
116
- for (i = 0; i <= numfriends; ++i) { /*TODO: dynamic memory allocation, this will segfault if there are more than MAX_NUM_FRIENDS*/
117
- if(friendlist[i].status == 0) {
118
- DHT_addfriend(client_id);
119
- friendlist[i].status = 1;
120
- friendlist[i].crypt_connection_id = -1;
121
- friendlist[i].friend_request_id = -1;
122
- memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
123
- friendlist[i].userstatus = calloc(1, 1);
124
- friendlist[i].userstatus_length = 1;
125
- memcpy(friendlist[i].info, data, length);
126
- friendlist[i].info_size = length;
127
-
128
- ++numfriends;
129
- return i;
130
- }
131
- }
132
- return -4;
133
- }
134
-
135
- int m_addfriend_norequest(uint8_t * client_id)
136
- {
137
- if (getfriend_id(client_id) != -1)
138
- return -1;
139
- uint32_t i;
140
- for (i = 0; i <= numfriends; ++i) {/*TODO: dynamic memory allocation, this will segfault if there are more than MAX_NUM_FRIENDS*/
141
- if(friendlist[i].status == 0) {
142
- DHT_addfriend(client_id);
143
- friendlist[i].status = 2;
144
- friendlist[i].crypt_connection_id = -1;
145
- friendlist[i].friend_request_id = -1;
146
- memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
147
- friendlist[i].userstatus = calloc(1, 1);
148
- friendlist[i].userstatus_length = 1;
149
- numfriends++;
150
- return i;
151
- }
152
- }
153
- return -1;
154
- }
155
-
156
- /* remove a friend
157
- return 0 if success
158
- return -1 if failure */
159
- int m_delfriend(int friendnumber)
160
- {
161
- if (friendnumber >= numfriends || friendnumber < 0)
162
- return -1;
163
-
164
- DHT_delfriend(friendlist[friendnumber].client_id);
165
- crypto_kill(friendlist[friendnumber].crypt_connection_id);
166
- free(friendlist[friendnumber].userstatus);
167
- memset(&friendlist[friendnumber], 0, sizeof(Friend));
168
- uint32_t i;
169
-
170
- for (i = numfriends; i != 0; --i) {
171
- if (friendlist[i-1].status != 0)
172
- break;
173
- }
174
- numfriends = i;
175
-
176
- return 0;
177
- }
178
-
179
- /* return 4 if friend is online
180
- return 3 if friend is confirmed
181
- return 2 if the friend request was sent
182
- return 1 if the friend was added
183
- return 0 if there is no friend with that number */
184
- int m_friendstatus(int friendnumber)
185
- {
186
- if (friendnumber < 0 || friendnumber >= numfriends)
187
- return 0;
188
- return friendlist[friendnumber].status;
189
- }
190
-
191
- /* send a text chat message to an online friend
192
- return 1 if packet was successfully put into the send queue
193
- return 0 if it was not */
194
- int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length)
195
- {
196
- if (friendnumber < 0 || friendnumber >= numfriends)
197
- return 0;
198
- if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4)
199
- /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */
200
- return 0;
201
- uint8_t temp[MAX_DATA_SIZE];
202
- temp[0] = PACKET_ID_MESSAGE;
203
- memcpy(temp + 1, message, length);
204
- return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
205
- }
206
-
207
- /* send a name packet to friendnumber
208
- length is the length with the NULL terminator*/
209
- static int m_sendname(int friendnumber, uint8_t * name, uint16_t length)
210
- {
211
- if(length > MAX_NAME_LENGTH || length == 0)
212
- return 0;
213
- uint8_t temp[MAX_NAME_LENGTH + 1];
214
- memcpy(temp + 1, name, length);
215
- temp[0] = PACKET_ID_NICKNAME;
216
- return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
217
- }
218
-
219
- /* set the name of a friend
220
- return 0 if success
221
- return -1 if failure */
222
- static int setfriendname(int friendnumber, uint8_t * name)
223
- {
224
- if (friendnumber >= numfriends || friendnumber < 0)
225
- return -1;
226
- memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
227
- return 0;
228
- }
229
-
230
- /* Set our nickname
231
- name must be a string of maximum MAX_NAME_LENGTH length.
232
- length must be at least 1 byte
233
- length is the length of name with the NULL terminator
234
- return 0 if success
235
- return -1 if failure */
236
- int setname(uint8_t * name, uint16_t length)
237
- {
238
- if (length > MAX_NAME_LENGTH || length == 0)
239
- return -1;
240
- memcpy(self_name, name, length);
241
- self_name_length = length;
242
- uint32_t i;
243
- for (i = 0; i < numfriends; ++i)
244
- friendlist[i].name_sent = 0;
245
- return 0;
246
- }
247
-
248
- /* get our nickname
249
- put it in name
250
- name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
251
- return the length of the name */
252
- uint16_t getself_name(uint8_t *name)
253
- {
254
- memcpy(name, self_name, self_name_length);
255
- return self_name_length;
256
- }
257
-
258
- /* get name of friendnumber
259
- put it in name
260
- name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
261
- return 0 if success
262
- return -1 if failure */
263
- int getname(int friendnumber, uint8_t * name)
264
- {
265
- if (friendnumber >= numfriends || friendnumber < 0)
266
- return -1;
267
- memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH);
268
- return 0;
269
- }
270
-
271
- int m_set_userstatus(uint8_t *status, uint16_t length)
272
- {
273
- if (length > MAX_USERSTATUS_LENGTH)
274
- return -1;
275
- uint8_t *newstatus = calloc(length, 1);
276
- memcpy(newstatus, status, length);
277
- free(self_userstatus);
278
- self_userstatus = newstatus;
279
- self_userstatus_len = length;
280
-
281
- uint32_t i;
282
- for (i = 0; i < numfriends; ++i)
283
- friendlist[i].userstatus_sent = 0;
284
- return 0;
285
- }
286
-
287
- /* return the size of friendnumber's user status
288
- guaranteed to be at most MAX_USERSTATUS_LENGTH */
289
- int m_get_userstatus_size(int friendnumber)
290
- {
291
- if (friendnumber >= numfriends || friendnumber < 0)
292
- return -1;
293
- return friendlist[friendnumber].userstatus_length;
294
- }
295
-
296
- /* copy the user status of friendnumber into buf, truncating if needed to maxlen
297
- bytes, use m_get_userstatus_size to find out how much you need to allocate */
298
- int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen)
299
- {
300
- if (friendnumber >= numfriends || friendnumber < 0)
301
- return -1;
302
- memset(buf, 0, maxlen);
303
- memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1);
304
- return 0;
305
- }
306
-
307
- static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length)
308
- {
309
- uint8_t *thepacket = malloc(length + 1);
310
- memcpy(thepacket + 1, status, length);
311
- thepacket[0] = PACKET_ID_USERSTATUS;
312
- int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, length + 1);
313
- free(thepacket);
314
- return written;
315
- }
316
-
317
- static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length)
318
- {
319
- if (friendnumber >= numfriends || friendnumber < 0)
320
- return -1;
321
- uint8_t *newstatus = calloc(length, 1);
322
- memcpy(newstatus, status, length);
323
- free(friendlist[friendnumber].userstatus);
324
- friendlist[friendnumber].userstatus = newstatus;
325
- friendlist[friendnumber].userstatus_length = length;
326
- return 0;
327
- }
328
-
329
- /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
330
- static uint8_t friend_request_isset = 0; */
331
- /* set the function that will be executed when a friend request is received. */
332
- void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
333
- {
334
- callback_friendrequest(function);
335
- }
336
-
337
- static void (*friend_message)(int, uint8_t *, uint16_t);
338
- static uint8_t friend_message_isset = 0;
339
-
340
- /* set the function that will be executed when a message from a friend is received. */
341
- void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t))
342
- {
343
- friend_message = function;
344
- friend_message_isset = 1;
345
- }
346
-
347
- static void (*friend_namechange)(int, uint8_t *, uint16_t);
348
- static uint8_t friend_namechange_isset = 0;
349
- void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t))
350
- {
351
- friend_namechange = function;
352
- friend_namechange_isset = 1;
353
- }
354
-
355
- static void (*friend_statuschange)(int, uint8_t *, uint16_t);
356
- static uint8_t friend_statuschange_isset = 0;
357
- void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t))
358
- {
359
- friend_statuschange = function;
360
- friend_statuschange_isset = 1;
361
- }
362
-
363
- #define PORT 33445
364
- /* run this at startup */
365
- int initMessenger()
366
- {
367
- new_keys();
368
- m_set_userstatus((uint8_t*)"Online", sizeof("Online"));
369
- initNetCrypto();
370
- IP ip;
371
- ip.i = 0;
372
-
373
- if(init_networking(ip,PORT) == -1)
374
- return -1;
375
-
376
- return 0;
377
- }
378
-
379
- //TODO: make this function not suck.
380
- static void doFriends()
381
- {
382
- /* TODO: add incoming connections and some other stuff. */
383
- uint32_t i;
384
- int len;
385
- uint8_t temp[MAX_DATA_SIZE];
386
- for (i = 0; i < numfriends; ++i) {
387
- if (friendlist[i].status == 1) {
388
- int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size);
389
- if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */
390
- friendlist[i].status = 2;
391
- else if (fr > 0)
392
- friendlist[i].status = 2;
393
- }
394
- if (friendlist[i].status == 2 || friendlist[i].status == 3) { /* friend is not online */
395
- if (friendlist[i].status == 2) {
396
- if (friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/
397
- send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size);
398
- friendlist[i].friend_request_id = unix_time();
399
- }
400
- }
401
- IP_Port friendip = DHT_getfriendip(friendlist[i].client_id);
402
- switch (is_cryptoconnected(friendlist[i].crypt_connection_id)) {
403
- case 0:
404
- if (friendip.ip.i > 1)
405
- friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip);
406
- break;
407
- case 3: /* Connection is established */
408
- friendlist[i].status = 4;
409
- break;
410
- case 4:
411
- crypto_kill(friendlist[i].crypt_connection_id);
412
- friendlist[i].crypt_connection_id = -1;
413
- break;
414
- default:
415
- break;
416
- }
417
- }
418
- while (friendlist[i].status == 4) { /* friend is online */
419
- if (friendlist[i].name_sent == 0) {
420
- if (m_sendname(i, self_name, self_name_length))
421
- friendlist[i].name_sent = 1;
422
- }
423
- if (friendlist[i].userstatus_sent == 0) {
424
- if (send_userstatus(i, self_userstatus, self_userstatus_len))
425
- friendlist[i].userstatus_sent = 1;
426
- }
427
- len = read_cryptpacket(friendlist[i].crypt_connection_id, temp);
428
- if (len > 0) {
429
- switch (temp[0]) {
430
- case PACKET_ID_NICKNAME: {
431
- if (len >= MAX_NAME_LENGTH + 1 || len == 1)
432
- break;
433
- if(friend_namechange_isset)
434
- friend_namechange(i, temp + 1, len - 1);
435
- memcpy(friendlist[i].name, temp + 1, len - 1);
436
- friendlist[i].name[len - 2] = 0; /* make sure the NULL terminator is present. */
437
- break;
438
- }
439
- case PACKET_ID_USERSTATUS: {
440
- uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1);
441
- memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH));
442
- if (friend_statuschange_isset)
443
- friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
444
- set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
445
- free(status);
446
- break;
447
- }
448
- case PACKET_ID_MESSAGE: {
449
- if (friend_message_isset)
450
- (*friend_message)(i, temp + 1, len - 1);
451
- break;
452
- }
453
- }
454
- } else {
455
- if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */
456
- crypto_kill(friendlist[i].crypt_connection_id);
457
- friendlist[i].crypt_connection_id = -1;
458
- friendlist[i].status = 3;
459
- }
460
- break;
461
- }
462
- }
463
- }
464
- }
465
-
466
- static void doInbound()
467
- {
468
- uint8_t secret_nonce[crypto_box_NONCEBYTES];
469
- uint8_t public_key[crypto_box_PUBLICKEYBYTES];
470
- uint8_t session_key[crypto_box_PUBLICKEYBYTES];
471
- int inconnection = crypto_inbound(public_key, secret_nonce, session_key);
472
- if (inconnection != -1) {
473
- int friend_id = getfriend_id(public_key);
474
- if (friend_id != -1) {
475
- crypto_kill(friendlist[friend_id].crypt_connection_id);
476
- friendlist[friend_id].crypt_connection_id =
477
- accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key);
478
-
479
- friendlist[friend_id].status = 3;
480
- }
481
- }
482
- }
483
-
484
- /*Interval in seconds between LAN discovery packet sending*/
485
- #define LAN_DISCOVERY_INTERVAL 60
486
-
487
- static uint32_t last_LANdiscovery;
488
-
489
- /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/
490
- static void LANdiscovery()
491
- {
492
- if (last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
493
- send_LANdiscovery(htons(PORT));
494
- last_LANdiscovery = unix_time();
495
- }
496
- }
497
-
498
-
499
- /* the main loop that needs to be run at least 200 times per second. */
500
- void doMessenger()
501
- {
502
- IP_Port ip_port;
503
- uint8_t data[MAX_UDP_PACKET_SIZE];
504
- uint32_t length;
505
- while (receivepacket(&ip_port, data, &length) != -1) {
506
- #ifdef DEBUG
507
- /* if(rand() % 3 != 1) //simulate packet loss */
508
- /* { */
509
- if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) &&
510
- friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port))
511
- /* if packet is discarded */
512
- printf("Received unhandled packet with length: %u\n", length);
513
- else
514
- printf("Received handled packet with length: %u\n", length);
515
- /* } */
516
- printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id);
517
- #else
518
- DHT_handlepacket(data, length, ip_port);
519
- LosslessUDP_handlepacket(data, length, ip_port);
520
- friendreq_handlepacket(data, length, ip_port);
521
- LANdiscovery_handlepacket(data, length, ip_port);
522
- #endif
523
-
524
- }
525
- doDHT();
526
- doLossless_UDP();
527
- doNetCrypto();
528
- doInbound();
529
- doFriends();
530
- LANdiscovery();
531
- }
532
-
533
- /* returns the size of the messenger data (for saving) */
534
- uint32_t Messenger_size()
535
- {
536
- return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
537
- + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends;
538
- }
539
-
540
- /* save the messenger in data of size Messenger_size() */
541
- void Messenger_save(uint8_t *data)
542
- {
543
- save_keys(data);
544
- data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
545
- uint32_t size = DHT_size();
546
- memcpy(data, &size, sizeof(size));
547
- data += sizeof(size);
548
- DHT_save(data);
549
- data += size;
550
- size = sizeof(Friend) * numfriends;
551
- memcpy(data, &size, sizeof(size));
552
- data += sizeof(size);
553
- memcpy(data, friendlist, sizeof(Friend) * numfriends);
554
- }
555
-
556
- /* load the messenger from data of size length. */
557
- int Messenger_load(uint8_t * data, uint32_t length)
558
- {
559
- if (length == ~0)
560
- return -1;
561
- if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2)
562
- return -1;
563
- length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2;
564
- load_keys(data);
565
- data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
566
- uint32_t size;
567
- memcpy(&size, data, sizeof(size));
568
- data += sizeof(size);
569
-
570
- if (length < size)
571
- return -1;
572
- length -= size;
573
- if (DHT_load(data, size) == -1)
574
- return -1;
575
- data += size;
576
- memcpy(&size, data, sizeof(size));
577
- data += sizeof(size);
578
- if (length != size || length % sizeof(Friend) != 0)
579
- return -1;
580
-
581
- Friend * temp = malloc(size);
582
- memcpy(temp, data, size);
583
-
584
- uint16_t num = size / sizeof(Friend);
585
-
586
- uint32_t i;
587
- for (i = 0; i < num; ++i) {
588
- if(temp[i].status != 0) {
589
- int fnum = m_addfriend_norequest(temp[i].client_id);
590
- setfriendname(fnum, temp[i].name);
591
- /* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */
592
- }
593
- }
594
- free(temp);
595
- return 0;
596
- }