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,165 +0,0 @@
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 key length is wrong.
54
- return -2 if user's own key
55
- return -3 if already a friend
56
- return -4 for other*/
57
- int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length);
58
-
59
-
60
- /* add a friend without sending a friendrequest.
61
- returns the friend number if success
62
- return -1 if failure. */
63
- int m_addfriend_norequest(uint8_t *client_id);
64
-
65
- /* return the friend id associated to that client id.
66
- return -1 if no such friend */
67
- int getfriend_id(uint8_t *client_id);
68
-
69
- /* copies the public key associated to that friend id into client_id buffer.
70
- make sure that client_id is of size CLIENT_ID_SIZE.
71
- return 0 if success
72
- return -1 if failure */
73
- int getclient_id(int friend_id, uint8_t *client_id);
74
-
75
- /* remove a friend */
76
- int m_delfriend(int friendnumber);
77
-
78
- /* return 4 if friend is online
79
- return 3 if friend is confirmed
80
- return 2 if the friend request was sent
81
- return 1 if the friend was added
82
- return 0 if there is no friend with that number */
83
- int m_friendstatus(int friendnumber);
84
-
85
- /* send a text chat message to an online friend
86
- returns 1 if packet was successfully put into the send queue
87
- return 0 if it was not */
88
- int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length);
89
-
90
- /* Set our nickname
91
- name must be a string of maximum MAX_NAME_LENGTH length.
92
- length must be at least 1 byte
93
- length is the length of name with the NULL terminator
94
- return 0 if success
95
- return -1 if failure */
96
- int setname(uint8_t *name, uint16_t length);
97
-
98
- /* get our nickname
99
- put it in name
100
- return the length of the name*/
101
- uint16_t getself_name(uint8_t *name);
102
-
103
- /* get name of friendnumber
104
- put it in name
105
- name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
106
- return 0 if success
107
- return -1 if failure */
108
- int getname(int friendnumber, uint8_t *name);
109
-
110
- /* set our user status
111
- you are responsible for freeing status after
112
- returns 0 on success, -1 on failure */
113
- int m_set_userstatus(uint8_t *status, uint16_t length);
114
-
115
- /* return the length of friendnumber's user status,
116
- including null
117
- pass it into malloc */
118
- int m_get_userstatus_size(int friendnumber);
119
-
120
- /* copy friendnumber's userstatus into buf, truncating if size is over maxlen
121
- get the size you need to allocate from m_get_userstatus_size */
122
- int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen);
123
-
124
- /* set the function that will be executed when a friend request is received.
125
- function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
126
- void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
127
-
128
- /* set the function that will be executed when a message from a friend is received.
129
- function format is: function(int friendnumber, uint8_t * message, uint32_t length) */
130
- void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t));
131
-
132
- /* set the callback for name changes
133
- function(int friendnumber, uint8_t *newname, uint16_t length)
134
- you are not responsible for freeing newname */
135
- void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t));
136
-
137
- /* set the callback for user status changes
138
- function(int friendnumber, uint8_t *newstatus, uint16_t length)
139
- you are not responsible for freeing newstatus */
140
- void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t));
141
-
142
- /* run this at startup
143
- returns 0 if no connection problems
144
- returns -1 if there are problems */
145
- int initMessenger();
146
-
147
- /* the main loop that needs to be run at least 200 times per second */
148
- void doMessenger();
149
-
150
- /* SAVING AND LOADING FUNCTIONS: */
151
-
152
- /* returns the size of the messenger data (for saving) */
153
- uint32_t Messenger_size();
154
-
155
- /* save the messenger in data (must be allocated memory of size Messenger_size()) */
156
- void Messenger_save(uint8_t *data);
157
-
158
- /* load the messenger from data of size length */
159
- int Messenger_load(uint8_t *data, uint32_t length);
160
-
161
- #ifdef __cplusplus
162
- }
163
- #endif
164
-
165
- #endif
@@ -1,131 +0,0 @@
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
- }
@@ -1,51 +0,0 @@
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
@@ -1,575 +0,0 @@
1
- /* net_crypto.c
2
- *
3
- * Functions for the core network crypto.
4
- * See also: docs/Crypto.txt
5
- *
6
- * NOTE: This code has to be perfect. We don't mess around with encryption.
7
- *
8
- * Copyright (C) 2013 Tox project All Rights Reserved.
9
- *
10
- * This file is part of Tox.
11
- *
12
- * Tox is free software: you can redistribute it and/or modify
13
- * it under the terms of the GNU General Public License as published by
14
- * the Free Software Foundation, either version 3 of the License, or
15
- * (at your option) any later version.
16
- *
17
- * Tox is distributed in the hope that it will be useful,
18
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
- * GNU General Public License for more details.
21
- *
22
- * You should have received a copy of the GNU General Public License
23
- * along with Tox. If not, see <http://www.gnu.org/licenses/>.
24
- *
25
- */
26
-
27
- #include "net_crypto.h"
28
-
29
- /* Our public and secret keys. */
30
- uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
31
- uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
32
-
33
- typedef struct {
34
- uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */
35
- uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */
36
- uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */
37
- uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* our public key for this session. */
38
- uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES]; /* our private key for this session. */
39
- uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* The public key of the peer. */
40
- uint8_t status; /* 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
41
- (we have received a handshake but no empty data packet), 3 if the connection is established.
42
- 4 if the connection is timed out. */
43
- uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */
44
-
45
- } Crypto_Connection;
46
-
47
- #define MAX_CRYPTO_CONNECTIONS 256
48
-
49
- static Crypto_Connection crypto_connections[MAX_CRYPTO_CONNECTIONS];
50
-
51
- #define MAX_INCOMING 64
52
-
53
- /* keeps track of the connection numbers for friends request so we can check later if they were sent */
54
- static int incoming_connections[MAX_INCOMING];
55
-
56
- /* encrypts plain of length length to encrypted of length + 16 using the
57
- public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce
58
- return -1 if there was a problem.
59
- return length of encrypted data if everything was fine. */
60
- int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
61
- uint8_t *plain, uint32_t length, uint8_t *encrypted)
62
- {
63
- if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0)
64
- return -1;
65
-
66
- uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES] = {0};
67
- uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES];
68
-
69
- memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); /* pad the message with 32 0 bytes. */
70
-
71
- crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key);
72
-
73
- /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero
74
- apparently memcmp should not be used so we do this instead:*/
75
- uint32_t i;
76
- uint32_t check = 0;
77
- for(i = 0; i < crypto_box_BOXZEROBYTES; ++i) {
78
- check |= temp_encrypted[i] ^ 0;
79
- }
80
- if(check != 0)
81
- return -1;
82
-
83
- /* unpad the encrypted message */
84
- memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES);
85
- return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES;
86
- }
87
-
88
- /* decrypts encrypted of length length to plain of length length - 16 using the
89
- public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce
90
- return -1 if there was a problem(decryption failed)
91
- return length of plain data if everything was fine. */
92
- int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
93
- uint8_t *encrypted, uint32_t length, uint8_t *plain)
94
- {
95
- if (length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES)
96
- return -1;
97
-
98
- uint8_t temp_plain[MAX_DATA_SIZE - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES];
99
- uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0};
100
-
101
- memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */
102
-
103
- if (crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES,
104
- nonce, public_key, secret_key) == -1)
105
- return -1;
106
-
107
- /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero
108
- apparently memcmp should not be used so we do this instead:*/
109
- uint32_t i;
110
- uint32_t check = 0;
111
- for(i = 0; i < crypto_box_ZEROBYTES; ++i) {
112
- check |= temp_plain[i] ^ 0;
113
- }
114
- if(check != 0)
115
- return -1;
116
-
117
- /* unpad the plain message */
118
- memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES);
119
- return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES;
120
- }
121
-
122
- /* increment the given nonce by 1 */
123
- void increment_nonce(uint8_t *nonce)
124
- {
125
- uint32_t i;
126
- for (i = 0; i < crypto_box_NONCEBYTES; ++i) {
127
- ++nonce[i];
128
- if(nonce[i] != 0)
129
- break;
130
- }
131
- }
132
-
133
- /* fill the given nonce with random bytes. */
134
- void random_nonce(uint8_t *nonce)
135
- {
136
- uint32_t i, temp;
137
- for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) {
138
- temp = random_int();
139
- memcpy(nonce + 4 * i, &temp, 4);
140
- }
141
- }
142
-
143
- /* return 0 if there is no received data in the buffer
144
- return -1 if the packet was discarded.
145
- return length of received data if successful */
146
- int read_cryptpacket(int crypt_connection_id, uint8_t *data)
147
- {
148
- if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
149
- return 0;
150
- if (crypto_connections[crypt_connection_id].status != 3)
151
- return 0;
152
- uint8_t temp_data[MAX_DATA_SIZE];
153
- int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data);
154
- if (length == 0)
155
- return 0;
156
- if (temp_data[0] != 3)
157
- return -1;
158
- int len = decrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key,
159
- crypto_connections[crypt_connection_id].sessionsecret_key,
160
- crypto_connections[crypt_connection_id].recv_nonce, temp_data + 1, length - 1, data);
161
- if (len != -1) {
162
- increment_nonce(crypto_connections[crypt_connection_id].recv_nonce);
163
- return len;
164
- }
165
- return -1;
166
- }
167
-
168
- /* return 0 if data could not be put in packet queue
169
- return 1 if data was put into the queue */
170
- int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length)
171
- {
172
- if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
173
- return 0;
174
- if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1)
175
- return 0;
176
- if (crypto_connections[crypt_connection_id].status != 3)
177
- return 0;
178
- uint8_t temp_data[MAX_DATA_SIZE];
179
- int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key,
180
- crypto_connections[crypt_connection_id].sessionsecret_key,
181
- crypto_connections[crypt_connection_id].sent_nonce, data, length, temp_data + 1);
182
- if (len == -1)
183
- return 0;
184
- temp_data[0] = 3;
185
- if (write_packet(crypto_connections[crypt_connection_id].number, temp_data, len + 1) == 0)
186
- return 0;
187
- increment_nonce(crypto_connections[crypt_connection_id].sent_nonce);
188
- return 1;
189
- }
190
-
191
- /* create a request to peer with public_key.
192
- packet must be an array of MAX_DATA_SIZE big.
193
- Data represents the data we send with the request with length being the length of the data.
194
- request_id is the id of the request (32 = friend request, 254 = ping request)
195
- returns -1 on failure
196
- returns the length of the created packet on success */
197
- int create_request(uint8_t *packet, uint8_t *public_key, uint8_t *data, uint32_t length, uint8_t request_id)
198
- {
199
- if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING)
200
- return -1;
201
- uint8_t nonce[crypto_box_NONCEBYTES];
202
- random_nonce(nonce);
203
- int len = encrypt_data(public_key, self_secret_key, nonce, data, length,
204
- 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet);
205
- if (len == -1)
206
- return -1;
207
- packet[0] = request_id;
208
- memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES);
209
- memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, self_public_key, crypto_box_PUBLICKEYBYTES);
210
- memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES);
211
-
212
- return len + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES;
213
- }
214
-
215
- /* puts the senders public key in the request in public_key, the data from the request
216
- in data if a friend or ping request was sent to us and returns the length of the data.
217
- packet is the request packet and length is its length
218
- return -1 if not valid request. */
219
- int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length)
220
- {
221
-
222
- if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
223
- length <= MAX_DATA_SIZE + ENCRYPTION_PADDING &&
224
- memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {
225
- memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES);
226
- uint8_t nonce[crypto_box_NONCEBYTES];
227
- memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES);
228
- int len1 = decrypt_data(public_key, self_secret_key, nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES,
229
- length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), data);
230
- if(len1 == -1)
231
- return -1;
232
- return len1;
233
- } else
234
- return -1;
235
- }
236
-
237
- /* Send a crypto handshake packet containing an encrypted secret nonce and session public key
238
- to peer with connection_id and public_key
239
- the packet is encrypted with a random nonce which is sent in plain text with the packet */
240
- int send_cryptohandshake(int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key)
241
- {
242
- uint8_t temp_data[MAX_DATA_SIZE];
243
- uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES];
244
- uint8_t nonce[crypto_box_NONCEBYTES];
245
-
246
- random_nonce(nonce);
247
- memcpy(temp, secret_nonce, crypto_box_NONCEBYTES);
248
- memcpy(temp + crypto_box_NONCEBYTES, session_key, crypto_box_PUBLICKEYBYTES);
249
-
250
- int len = encrypt_data(public_key, self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
251
- 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data);
252
- if (len == -1)
253
- return 0;
254
- temp_data[0] = 2;
255
- memcpy(temp_data + 1, self_public_key, crypto_box_PUBLICKEYBYTES);
256
- memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
257
- return write_packet(connection_id, temp_data, len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES);
258
- }
259
-
260
- /* Extract secret nonce, session public key and public_key from a packet(data) with length length
261
- return 1 if successful
262
- return 0 if failure */
263
- int handle_cryptohandshake(uint8_t *public_key, uint8_t *secret_nonce,
264
- uint8_t *session_key, uint8_t *data, uint16_t length)
265
- {
266
- int pad = (- crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES);
267
- if (length != 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES
268
- + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad) {
269
- return 0;
270
- }
271
- if (data[0] != 2)
272
- return 0;
273
- uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES];
274
-
275
- memcpy(public_key, data + 1, crypto_box_PUBLICKEYBYTES);
276
-
277
- int len = decrypt_data(public_key, self_secret_key, data + 1 + crypto_box_PUBLICKEYBYTES,
278
- data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
279
- crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad, temp);
280
-
281
- if (len != crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES)
282
- return 0;
283
-
284
- memcpy(secret_nonce, temp, crypto_box_NONCEBYTES);
285
- memcpy(session_key, temp + crypto_box_NONCEBYTES, crypto_box_PUBLICKEYBYTES);
286
- return 1;
287
- }
288
-
289
- /* get crypto connection id from public key of peer
290
- return -1 if there are no connections like we are looking for
291
- return id if it found it */
292
- int getcryptconnection_id(uint8_t *public_key)
293
- {
294
- uint32_t i;
295
- for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
296
- if (crypto_connections[i].status > 0)
297
- if (memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0)
298
- return i;
299
- }
300
- return -1;
301
- }
302
-
303
- /* Start a secure connection with other peer who has public_key and ip_port
304
- returns -1 if failure
305
- returns crypt_connection_id of the initialized connection if everything went well. */
306
- int crypto_connect(uint8_t *public_key, IP_Port ip_port)
307
- {
308
- uint32_t i;
309
- int id = getcryptconnection_id(public_key);
310
- if (id != -1) {
311
- IP_Port c_ip = connection_ip(crypto_connections[id].number);
312
- if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port)
313
- return -1;
314
- }
315
- for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
316
- if (crypto_connections[i].status == 0) {
317
- int id = new_connection(ip_port);
318
- if (id == -1)
319
- return -1;
320
- crypto_connections[i].number = id;
321
- crypto_connections[i].status = 1;
322
- random_nonce(crypto_connections[i].recv_nonce);
323
- memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
324
- crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key);
325
-
326
- if (send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce,
327
- crypto_connections[i].sessionpublic_key) == 1) {
328
- increment_nonce(crypto_connections[i].recv_nonce);
329
- return i;
330
- }
331
- return -1; /* this should never happen. */
332
- }
333
- }
334
- return -1;
335
- }
336
-
337
- /* handle an incoming connection
338
- return -1 if no crypto inbound connection
339
- return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection
340
- Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce
341
- and the session public key for the connection in session_key
342
- to accept it see: accept_crypto_inbound(...)
343
- to refuse it just call kill_connection(...) on the connection id */
344
- int crypto_inbound(uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key)
345
- {
346
- uint32_t i;
347
- for (i = 0; i < MAX_INCOMING; ++i) {
348
- if (incoming_connections[i] != -1) {
349
- if (is_connected(incoming_connections[i]) == 4 || is_connected(incoming_connections[i]) == 0) {
350
- kill_connection(incoming_connections[i]);
351
- incoming_connections[i] = -1;
352
- continue;
353
- }
354
- if (id_packet(incoming_connections[i]) == 2) {
355
- uint8_t temp_data[MAX_DATA_SIZE];
356
- uint16_t len = read_packet(incoming_connections[i], temp_data);
357
- if (handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) {
358
- int connection_id = incoming_connections[i];
359
- incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */
360
- return connection_id;
361
- }
362
- }
363
- }
364
- }
365
- return -1;
366
- }
367
-
368
- /* kill a crypto connection
369
- return 0 if killed successfully
370
- return 1 if there was a problem. */
371
- int crypto_kill(int crypt_connection_id)
372
- {
373
- if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
374
- return 1;
375
- if (crypto_connections[crypt_connection_id].status != 0) {
376
- crypto_connections[crypt_connection_id].status = 0;
377
- kill_connection(crypto_connections[crypt_connection_id].number);
378
- memset(&crypto_connections[crypt_connection_id], 0 ,sizeof(Crypto_Connection));
379
- crypto_connections[crypt_connection_id].number = ~0;
380
- return 0;
381
- }
382
- return 1;
383
- }
384
-
385
- /* accept an incoming connection using the parameters provided by crypto_inbound
386
- return -1 if not successful
387
- returns the crypt_connection_id if successful */
388
- int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key)
389
- {
390
- uint32_t i;
391
- if (connection_id == -1)
392
- return -1;
393
- /*
394
- if(getcryptconnection_id(public_key) != -1)
395
- {
396
- return -1;
397
- }*/
398
- for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
399
- if(crypto_connections[i].status == 0) {
400
- crypto_connections[i].number = connection_id;
401
- crypto_connections[i].status = 2;
402
- random_nonce(crypto_connections[i].recv_nonce);
403
- memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES);
404
- memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES);
405
- increment_nonce(crypto_connections[i].sent_nonce);
406
- memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
407
-
408
- crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key);
409
-
410
- if (send_cryptohandshake(connection_id, public_key, crypto_connections[i].recv_nonce,
411
- crypto_connections[i].sessionpublic_key) == 1) {
412
- increment_nonce(crypto_connections[i].recv_nonce);
413
- uint32_t zero = 0;
414
- crypto_connections[i].status = 3; /* connection status needs to be 3 for write_cryptpacket() to work */
415
- write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero));
416
- crypto_connections[i].status = 2; /* set it to its proper value right after. */
417
- return i;
418
- }
419
- return -1; /* this should never happen. */
420
- }
421
- }
422
- return -1;
423
- }
424
-
425
- /* return 0 if no connection, 1 we have sent a handshake, 2 if connection is not confirmed yet
426
- (we have received a handshake but no empty data packet), 3 if the connection is established.
427
- 4 if the connection is timed out and waiting to be killed */
428
- int is_cryptoconnected(int crypt_connection_id)
429
- {
430
- if (crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS)
431
- return crypto_connections[crypt_connection_id].status;
432
- return 0;
433
- }
434
-
435
- /* Generate our public and private keys
436
- Only call this function the first time the program starts. */
437
- void new_keys()
438
- {
439
- crypto_box_keypair(self_public_key,self_secret_key);
440
- }
441
-
442
- /* save the public and private keys to the keys array
443
- Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
444
- void save_keys(uint8_t *keys)
445
- {
446
- memcpy(keys, self_public_key, crypto_box_PUBLICKEYBYTES);
447
- memcpy(keys + crypto_box_PUBLICKEYBYTES, self_secret_key, crypto_box_SECRETKEYBYTES);
448
- }
449
-
450
- /* load the public and private keys from the keys array
451
- Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
452
- void load_keys(uint8_t *keys)
453
- {
454
- memcpy(self_public_key, keys, crypto_box_PUBLICKEYBYTES);
455
- memcpy(self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES);
456
- }
457
-
458
- /* TODO: optimize this
459
- adds an incoming connection to the incoming_connection list.
460
- returns 0 if successful
461
- returns 1 if failure */
462
- int new_incoming(int id)
463
- {
464
- uint32_t i;
465
- for (i = 0; i < MAX_INCOMING; ++i) {
466
- if (incoming_connections[i] == -1) {
467
- incoming_connections[i] = id;
468
- return 0;
469
- }
470
- }
471
- return 1;
472
- }
473
-
474
- /* TODO: optimize this
475
- handle all new incoming connections. */
476
- static void handle_incomings()
477
- {
478
- int income;
479
- while (1) {
480
- income = incoming_connection();
481
- if(income == -1 || new_incoming(income) )
482
- break;
483
- }
484
- }
485
-
486
- /* handle received packets for not yet established crypto connections. */
487
- static void receive_crypto()
488
- {
489
- uint32_t i;
490
- for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
491
- if (crypto_connections[i].status == 1) {
492
- uint8_t temp_data[MAX_DATA_SIZE];
493
- uint8_t secret_nonce[crypto_box_NONCEBYTES];
494
- uint8_t public_key[crypto_box_PUBLICKEYBYTES];
495
- uint8_t session_key[crypto_box_PUBLICKEYBYTES];
496
- uint16_t len;
497
- if (id_packet(crypto_connections[i].number) == 1)
498
- /* if the packet is a friend request drop it (because we are already friends) */
499
- len = read_packet(crypto_connections[i].number, temp_data);
500
- if (id_packet(crypto_connections[i].number) == 2) { /* handle handshake packet. */
501
- len = read_packet(crypto_connections[i].number, temp_data);
502
- if (handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) {
503
- if (memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) {
504
- memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES);
505
- memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES);
506
- increment_nonce(crypto_connections[i].sent_nonce);
507
- uint32_t zero = 0;
508
- crypto_connections[i].status = 3; /* connection status needs to be 3 for write_cryptpacket() to work */
509
- write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero));
510
- crypto_connections[i].status = 2; /* set it to its proper value right after. */
511
- }
512
- }
513
- } else if (id_packet(crypto_connections[i].number) != -1) // This should not happen kill the connection if it does
514
- crypto_kill(crypto_connections[i].number);
515
-
516
- }
517
- if (crypto_connections[i].status == 2) {
518
- if (id_packet(crypto_connections[i].number) == 3) {
519
- uint8_t temp_data[MAX_DATA_SIZE];
520
- uint8_t data[MAX_DATA_SIZE];
521
- int length = read_packet(crypto_connections[i].number, temp_data);
522
- int len = decrypt_data(crypto_connections[i].peersessionpublic_key,
523
- crypto_connections[i].sessionsecret_key,
524
- crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data);
525
- uint32_t zero = 0;
526
- if (len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) {
527
- increment_nonce(crypto_connections[i].recv_nonce);
528
- crypto_connections[i].status = 3;
529
-
530
- /* connection is accepted so we disable the auto kill by setting it to about 1 month from now. */
531
- kill_connection_in(crypto_connections[i].number, 3000000);
532
- } else
533
- crypto_kill(crypto_connections[i].number); // This should not happen kill the connection if it does
534
- } else if(id_packet(crypto_connections[i].number) != -1)
535
- /* This should not happen
536
- kill the connection if it does */
537
- crypto_kill(crypto_connections[i].number);
538
- }
539
- }
540
- }
541
-
542
- /* run this to (re)initialize net_crypto
543
- sets all the global connection variables to their default values. */
544
- void initNetCrypto()
545
- {
546
- memset(crypto_connections, 0 ,sizeof(crypto_connections));
547
- memset(incoming_connections, -1 ,sizeof(incoming_connections));
548
- uint32_t i;
549
- for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
550
- crypto_connections[i].number = ~0;
551
- }
552
-
553
- static void killTimedout()
554
- {
555
- uint32_t i;
556
- for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
557
- if (crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4)
558
- crypto_connections[i].status = 4;
559
- else if (is_connected(crypto_connections[i].number) == 4) {
560
- kill_connection(crypto_connections[i].number);
561
- crypto_connections[i].number = ~0;
562
- }
563
- }
564
- }
565
-
566
- /* main loop */
567
- void doNetCrypto()
568
- {
569
- /* TODO:check if friend requests were sent correctly
570
- handle new incoming connections
571
- handle friend requests */
572
- handle_incomings();
573
- receive_crypto();
574
- killTimedout();
575
- }