summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormannol <eniz_vukovic@hotmail.com>2014-07-03 17:08:38 +0200
committermannol <eniz_vukovic@hotmail.com>2014-07-03 17:08:38 +0200
commitf8a2a865dc32bfa626cc150a509d30d897e1181b (patch)
tree5bb6b234149cdf2a79422815dc59a0c53cb3cb0e
parenta9a7f6a5952d0385904ec9df078ac065a3f5887f (diff)
parentc4f0650ae33e1669bfc994ef4f76c6c31e4d63c0 (diff)
Resolved conflicts when merging upstream
-rw-r--r--auto_tests/TCP_test.c6
-rw-r--r--auto_tests/tox_test.c10
-rw-r--r--docs/TODO4
-rw-r--r--testing/Messenger_test.c2
-rw-r--r--testing/nTox.c24
-rw-r--r--testing/tox_sync.c6
-rw-r--r--toxav/msi.c20
-rw-r--r--toxav/rtp.c22
-rw-r--r--toxav/toxav.c8
-rw-r--r--toxcore/DHT.c2
-rw-r--r--toxcore/DHT.h2
-rw-r--r--toxcore/Messenger.c194
-rw-r--r--toxcore/Messenger.h148
-rw-r--r--toxcore/TCP_client.c29
-rw-r--r--toxcore/TCP_client.h28
-rw-r--r--toxcore/TCP_server.c28
-rw-r--r--toxcore/TCP_server.h6
-rw-r--r--toxcore/net_crypto.c159
-rw-r--r--toxcore/net_crypto.h38
-rw-r--r--toxcore/network.c4
-rw-r--r--toxcore/onion.c10
-rw-r--r--toxcore/onion.h10
-rw-r--r--toxcore/onion_announce.c24
-rw-r--r--toxcore/onion_announce.h20
-rw-r--r--toxcore/onion_client.c31
-rw-r--r--toxcore/onion_client.h8
-rw-r--r--toxcore/tox.c184
-rw-r--r--toxcore/tox.h116
28 files changed, 601 insertions, 542 deletions
diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c
index ca5afd9a..8e75fae0 100644
--- a/auto_tests/TCP_test.c
+++ b/auto_tests/TCP_test.c
@@ -294,7 +294,7 @@ END_TEST
294static int response_callback_good; 294static int response_callback_good;
295static uint8_t response_callback_connection_id; 295static uint8_t response_callback_connection_id;
296static uint8_t response_callback_public_key[crypto_box_PUBLICKEYBYTES]; 296static uint8_t response_callback_public_key[crypto_box_PUBLICKEYBYTES];
297static int response_callback(void *object, uint8_t connection_id, uint8_t *public_key) 297static int response_callback(void *object, uint8_t connection_id, const uint8_t *public_key)
298{ 298{
299 if (set_tcp_connection_number(object - 2, connection_id, 7) != 0) 299 if (set_tcp_connection_number(object - 2, connection_id, 7) != 0)
300 return 1; 300 return 1;
@@ -321,7 +321,7 @@ static int status_callback(void *object, uint32_t number, uint8_t connection_id,
321 return 0; 321 return 0;
322} 322}
323static int data_callback_good; 323static int data_callback_good;
324static int data_callback(void *object, uint32_t number, uint8_t connection_id, uint8_t *data, uint16_t length) 324static int data_callback(void *object, uint32_t number, uint8_t connection_id, const uint8_t *data, uint16_t length)
325{ 325{
326 if (object != (void *)3) 326 if (object != (void *)3)
327 return 1; 327 return 1;
@@ -342,7 +342,7 @@ static int data_callback(void *object, uint32_t number, uint8_t connection_id, u
342 342
343static int oob_data_callback_good; 343static int oob_data_callback_good;
344static uint8_t oob_pubkey[crypto_box_PUBLICKEYBYTES]; 344static uint8_t oob_pubkey[crypto_box_PUBLICKEYBYTES];
345static int oob_data_callback(void *object, uint8_t *public_key, uint8_t *data, uint16_t length) 345static int oob_data_callback(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length)
346{ 346{
347 if (object != (void *)4) 347 if (object != (void *)4)
348 return 1; 348 return 1;
diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c
index f8da8560..1a3c5868 100644
--- a/auto_tests/tox_test.c
+++ b/auto_tests/tox_test.c
@@ -30,7 +30,7 @@ void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *dat
30} 30}
31uint32_t messages_received; 31uint32_t messages_received;
32 32
33void print_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) 33void print_message(Tox *m, int friendnumber, const uint8_t *string, uint16_t length, void *userdata)
34{ 34{
35 if (*((uint32_t *)userdata) != 974536) 35 if (*((uint32_t *)userdata) != 974536)
36 return; 36 return;
@@ -44,7 +44,7 @@ void print_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v
44 44
45uint32_t name_changes; 45uint32_t name_changes;
46 46
47void print_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) 47void print_nickchange(Tox *m, int friendnumber, const uint8_t *string, uint16_t length, void *userdata)
48{ 48{
49 if (*((uint32_t *)userdata) != 974536) 49 if (*((uint32_t *)userdata) != 974536)
50 return; 50 return;
@@ -69,7 +69,7 @@ void print_typingchange(Tox *m, int friendnumber, uint8_t typing, void *userdata
69uint8_t filenum; 69uint8_t filenum;
70uint32_t file_accepted; 70uint32_t file_accepted;
71uint64_t file_size; 71uint64_t file_size;
72void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, 72void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, const uint8_t *filename,
73 uint16_t filename_length, void *userdata) 73 uint16_t filename_length, void *userdata)
74{ 74{
75 if (*((uint32_t *)userdata) != 974536) 75 if (*((uint32_t *)userdata) != 974536)
@@ -85,7 +85,7 @@ void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t
85uint32_t file_sent; 85uint32_t file_sent;
86uint32_t sendf_ok; 86uint32_t sendf_ok;
87void file_print_control(Tox *m, int friendnumber, uint8_t send_recieve, uint8_t filenumber, uint8_t control_type, 87void file_print_control(Tox *m, int friendnumber, uint8_t send_recieve, uint8_t filenumber, uint8_t control_type,
88 uint8_t *data, uint16_t length, void *userdata) 88 const uint8_t *data, uint16_t length, void *userdata)
89{ 89{
90 if (*((uint32_t *)userdata) != 974536) 90 if (*((uint32_t *)userdata) != 974536)
91 return; 91 return;
@@ -100,7 +100,7 @@ void file_print_control(Tox *m, int friendnumber, uint8_t send_recieve, uint8_t
100 100
101uint64_t size_recv; 101uint64_t size_recv;
102uint8_t num; 102uint8_t num;
103void write_file(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) 103void write_file(Tox *m, int friendnumber, uint8_t filenumber, const uint8_t *data, uint16_t length, void *userdata)
104{ 104{
105 if (*((uint32_t *)userdata) != 974536) 105 if (*((uint32_t *)userdata) != 974536)
106 return; 106 return;
diff --git a/docs/TODO b/docs/TODO
index fc50d566..966a1b5f 100644
--- a/docs/TODO
+++ b/docs/TODO
@@ -24,8 +24,8 @@ Lossless UDP:
24 [DONE] Call initiation 24 [DONE] Call initiation
25 [DONE] Encryption 25 [DONE] Encryption
26 [IN PROGRESS] Auditing. 26 [IN PROGRESS] Auditing.
27 [NOT STARTED] Video packet splitting. 27 [NEEDS TESTING] Video packet splitting.
28 [NOT STARTED] Prevent audio skew. 28 [IN PROGRESS] Prevent audio skew (seems to be easily solvable client side.)
29 [IN PROGRESS] Group chats. 29 [IN PROGRESS] Group chats.
30 30
31Friend_requests.c: 31Friend_requests.c:
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c
index 7c1d64e7..905bcef4 100644
--- a/testing/Messenger_test.c
+++ b/testing/Messenger_test.c
@@ -56,7 +56,7 @@
56 56
57#endif 57#endif
58 58
59void print_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) 59void print_message(Messenger *m, int friendnumber, const uint8_t *string, uint16_t length, void *userdata)
60{ 60{
61 printf("Message with length %u received from %u: %s \n", length, friendnumber, string); 61 printf("Message with length %u received from %u: %s \n", length, friendnumber, string);
62 m_sendmessage(m, friendnumber, (uint8_t *)"Test1", 6); 62 m_sendmessage(m, friendnumber, (uint8_t *)"Test1", 6);
diff --git a/testing/nTox.c b/testing/nTox.c
index 050ec93d..971a2571 100644
--- a/testing/nTox.c
+++ b/testing/nTox.c
@@ -876,14 +876,16 @@ void print_request(Tox *m, const uint8_t *public_key, const uint8_t *data, uint1
876 do_refresh(); 876 do_refresh();
877} 877}
878 878
879void print_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) 879void print_message(Tox *m, int friendnumber, const uint8_t *string, uint16_t length, void *userdata)
880{ 880{
881 /* ensure null termination */ 881 /* ensure null termination */
882 string[length - 1] = 0; 882 uint8_t null_string[length + 1];
883 print_formatted_message(m, (char *)string, friendnumber, 0); 883 memcpy(null_string, string, length);
884 null_string[length] = 0;
885 print_formatted_message(m, (char *)null_string, friendnumber, 0);
884} 886}
885 887
886void print_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) 888void print_nickchange(Tox *m, int friendnumber, const uint8_t *string, uint16_t length, void *userdata)
887{ 889{
888 char name[TOX_MAX_NAME_LENGTH + 1]; 890 char name[TOX_MAX_NAME_LENGTH + 1];
889 891
@@ -899,7 +901,7 @@ void print_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length
899 } 901 }
900} 902}
901 903
902void print_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) 904void print_statuschange(Tox *m, int friendnumber, const uint8_t *string, uint16_t length, void *userdata)
903{ 905{
904 char name[TOX_MAX_NAME_LENGTH + 1]; 906 char name[TOX_MAX_NAME_LENGTH + 1];
905 907
@@ -999,7 +1001,7 @@ void print_help(char *prog_name)
999 puts(" -f keyfile [Optional] Specify a keyfile to read from and write to."); 1001 puts(" -f keyfile [Optional] Specify a keyfile to read from and write to.");
1000} 1002}
1001 1003
1002void print_invite(Tox *m, int friendnumber, uint8_t *group_public_key, void *userdata) 1004void print_invite(Tox *m, int friendnumber, const uint8_t *group_public_key, void *userdata)
1003{ 1005{
1004 char msg[256]; 1006 char msg[256];
1005 sprintf(msg, "[i] received group chat invite from: %u, auto accepting and joining. group number: %u", friendnumber, 1007 sprintf(msg, "[i] received group chat invite from: %u, auto accepting and joining. group number: %u", friendnumber,
@@ -1055,7 +1057,8 @@ void print_groupchatpeers(Tox *m, int groupnumber)
1055 new_lines_mark(msg, 1); 1057 new_lines_mark(msg, 1);
1056} 1058}
1057 1059
1058void print_groupmessage(Tox *m, int groupnumber, int peernumber, uint8_t *message, uint16_t length, void *userdata) 1060void print_groupmessage(Tox *m, int groupnumber, int peernumber, const uint8_t *message, uint16_t length,
1061 void *userdata)
1059{ 1062{
1060 char msg[256 + length]; 1063 char msg[256 + length];
1061 uint8_t name[TOX_MAX_NAME_LENGTH] = {0}; 1064 uint8_t name[TOX_MAX_NAME_LENGTH] = {0};
@@ -1116,7 +1119,7 @@ void print_groupnamelistchange(Tox *m, int groupnumber, int peernumber, uint8_t
1116 print_groupchatpeers(m, groupnumber); 1119 print_groupchatpeers(m, groupnumber);
1117 } 1120 }
1118} 1121}
1119void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, 1122void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, const uint8_t *filename,
1120 uint16_t filename_length, void *userdata) 1123 uint16_t filename_length, void *userdata)
1121{ 1124{
1122 char msg[512]; 1125 char msg[512];
@@ -1131,8 +1134,7 @@ void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t
1131} 1134}
1132 1135
1133void file_print_control(Tox *m, int friendnumber, uint8_t send_recieve, uint8_t filenumber, uint8_t control_type, 1136void file_print_control(Tox *m, int friendnumber, uint8_t send_recieve, uint8_t filenumber, uint8_t control_type,
1134 uint8_t *data, 1137 const uint8_t *data, uint16_t length, void *userdata)
1135 uint16_t length, void *userdata)
1136{ 1138{
1137 char msg[512] = {0}; 1139 char msg[512] = {0};
1138 1140
@@ -1146,7 +1148,7 @@ void file_print_control(Tox *m, int friendnumber, uint8_t send_recieve, uint8_t
1146 new_lines(msg); 1148 new_lines(msg);
1147} 1149}
1148 1150
1149void write_file(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) 1151void write_file(Tox *m, int friendnumber, uint8_t filenumber, const uint8_t *data, uint16_t length, void *userdata)
1150{ 1152{
1151 char filename[256]; 1153 char filename[256];
1152 sprintf(filename, "%u.%u.bin", friendnumber, filenumber); 1154 sprintf(filename, "%u.%u.bin", friendnumber, filenumber);
diff --git a/testing/tox_sync.c b/testing/tox_sync.c
index 68cad9f4..523f2c56 100644
--- a/testing/tox_sync.c
+++ b/testing/tox_sync.c
@@ -130,7 +130,7 @@ int not_sending()
130 130
131static char path[1024]; 131static char path[1024];
132 132
133void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, 133void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, const uint8_t *filename,
134 uint16_t filename_length, void *userdata) 134 uint16_t filename_length, void *userdata)
135{ 135{
136 char fullpath[1024]; 136 char fullpath[1024];
@@ -169,7 +169,7 @@ void file_request_accept(Tox *m, int friendnumber, uint8_t filenumber, uint64_t
169} 169}
170 170
171void file_print_control(Tox *m, int friendnumber, uint8_t recieve_send, uint8_t filenumber, uint8_t control_type, 171void file_print_control(Tox *m, int friendnumber, uint8_t recieve_send, uint8_t filenumber, uint8_t control_type,
172 uint8_t *data, 172 const uint8_t *data,
173 uint16_t length, void *userdata) 173 uint16_t length, void *userdata)
174{ 174{
175 if (recieve_send == 1 && (control_type == TOX_FILECONTROL_KILL || control_type == TOX_FILECONTROL_FINISHED)) { 175 if (recieve_send == 1 && (control_type == TOX_FILECONTROL_KILL || control_type == TOX_FILECONTROL_FINISHED)) {
@@ -185,7 +185,7 @@ void file_print_control(Tox *m, int friendnumber, uint8_t recieve_send, uint8_t
185 } 185 }
186} 186}
187 187
188void write_file(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) 188void write_file(Tox *m, int friendnumber, uint8_t filenumber, const uint8_t *data, uint16_t length, void *userdata)
189{ 189{
190 if (file_recv[filenumber].file != 0) 190 if (file_recv[filenumber].file != 0)
191 if (fwrite(data, length, 1, file_recv[filenumber].file) != 1) 191 if (fwrite(data, length, 1, file_recv[filenumber].file) != 1)
diff --git a/toxav/msi.c b/toxav/msi.c
index 9695029a..ee7a95f3 100644
--- a/toxav/msi.c
+++ b/toxav/msi.c
@@ -1002,18 +1002,18 @@ int send_error ( MSISession *session, MSICall *call, MSICallError errid, uint32_
1002 */ 1002 */
1003void add_peer( MSICall *call, int peer_id ) 1003void add_peer( MSICall *call, int peer_id )
1004{ 1004{
1005 uint32_t* peers = !call->peers ? peers = calloc(sizeof(uint32_t), 1) : 1005 uint32_t *peers = !call->peers ? peers = calloc(sizeof(uint32_t), 1) :
1006 realloc( call->peers, sizeof(uint32_t) * call->peer_count); 1006 realloc( call->peers, sizeof(uint32_t) * call->peer_count);
1007 1007
1008 if (!peers) { 1008 if (!peers) {
1009 LOGGER_WARNING("Allocation failed! Program might misbehave!"); 1009 LOGGER_WARNING("Allocation failed! Program might misbehave!");
1010 return; 1010 return;
1011 } 1011 }
1012 1012
1013 call->peer_count ++; 1013 call->peer_count ++;
1014 call->peers = peers; 1014 call->peers = peers;
1015 call->peers[call->peer_count - 1] = peer_id; 1015 call->peers[call->peer_count - 1] = peer_id;
1016 1016
1017 LOGGER_DEBUG("Added peer: %d", peer_id); 1017 LOGGER_DEBUG("Added peer: %d", peer_id);
1018} 1018}
1019 1019
@@ -1038,12 +1038,12 @@ MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
1038 1038
1039 for (; _call_idx < session->max_calls; _call_idx ++) { 1039 for (; _call_idx < session->max_calls; _call_idx ++) {
1040 if ( !session->calls[_call_idx] ) { 1040 if ( !session->calls[_call_idx] ) {
1041 1041
1042 if (!(session->calls[_call_idx] = calloc ( sizeof ( MSICall ), 1 ))) { 1042 if (!(session->calls[_call_idx] = calloc ( sizeof ( MSICall ), 1 ))) {
1043 LOGGER_WARNING("Allocation failed! Program might misbehave!"); 1043 LOGGER_WARNING("Allocation failed! Program might misbehave!");
1044 return NULL; 1044 return NULL;
1045 } 1045 }
1046 1046
1047 break; 1047 break;
1048 } 1048 }
1049 } 1049 }
@@ -1455,7 +1455,7 @@ int handle_recv_error ( MSISession *session, MSICall *call, MSIMessage *msg )
1455 * 1455 *
1456 * 1456 *
1457 */ 1457 */
1458void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16_t length, void *object ) 1458void msi_handle_packet ( Messenger *messenger, int source, const uint8_t *data, uint16_t length, void *object )
1459{ 1459{
1460 LOGGER_DEBUG("Got msi message"); 1460 LOGGER_DEBUG("Got msi message");
1461 /* Unused */ 1461 /* Unused */
@@ -1610,7 +1610,7 @@ MSISession *msi_init_session ( Messenger *messenger, int32_t max_calls )
1610 free(_retu); 1610 free(_retu);
1611 return NULL; 1611 return NULL;
1612 } 1612 }
1613 1613
1614 _retu->max_calls = max_calls; 1614 _retu->max_calls = max_calls;
1615 1615
1616 _retu->frequ = 10000; /* default value? */ 1616 _retu->frequ = 10000; /* default value? */
@@ -1663,8 +1663,6 @@ int msi_terminate_session ( MSISession *session )
1663 timer_terminate_session(session->timer_handler); 1663 timer_terminate_session(session->timer_handler);
1664 1664
1665 pthread_mutex_destroy(&session->mutex); 1665 pthread_mutex_destroy(&session->mutex);
1666
1667// timer_terminate_session();
1668 1666
1669 LOGGER_DEBUG("Terminated session: %p", session); 1667 LOGGER_DEBUG("Terminated session: %p", session);
1670 free ( session ); 1668 free ( session );
diff --git a/toxav/rtp.c b/toxav/rtp.c
index ca37bf47..9ba3b6a3 100644
--- a/toxav/rtp.c
+++ b/toxav/rtp.c
@@ -105,16 +105,16 @@ RTPHeader *extract_header ( const uint8_t *payload, int length )
105 } 105 }
106 106
107 RTPHeader *_retu = calloc(1, sizeof (RTPHeader)); 107 RTPHeader *_retu = calloc(1, sizeof (RTPHeader));
108 108
109 if ( !_retu ) { 109 if ( !_retu ) {
110 LOGGER_WARNING("Alloc failed! Program might misbehave!"); 110 LOGGER_WARNING("Alloc failed! Program might misbehave!");
111 return NULL; 111 return NULL;
112 } 112 }
113 113
114 bytes_to_U16(&_retu->sequnum, payload); 114 bytes_to_U16(&_retu->sequnum, payload);
115 115
116 const uint8_t *_it = payload + 2; 116 const uint8_t *_it = payload + 2;
117 117
118 _retu->flags = *_it; 118 _retu->flags = *_it;
119 ++_it; 119 ++_it;
120 120
@@ -178,7 +178,7 @@ RTPExtHeader *extract_ext_header ( const uint8_t *payload, uint16_t length )
178 const uint8_t *_it = payload; 178 const uint8_t *_it = payload;
179 179
180 RTPExtHeader *_retu = calloc(1, sizeof (RTPExtHeader)); 180 RTPExtHeader *_retu = calloc(1, sizeof (RTPExtHeader));
181 181
182 if ( !_retu ) { 182 if ( !_retu ) {
183 LOGGER_WARNING("Alloc failed! Program might misbehave!"); 183 LOGGER_WARNING("Alloc failed! Program might misbehave!");
184 return NULL; 184 return NULL;
@@ -290,7 +290,7 @@ uint8_t *add_ext_header ( RTPExtHeader *header, uint8_t *payload )
290RTPHeader *build_header ( RTPSession *session ) 290RTPHeader *build_header ( RTPSession *session )
291{ 291{
292 RTPHeader *_retu = calloc ( 1, sizeof (RTPHeader) ); 292 RTPHeader *_retu = calloc ( 1, sizeof (RTPHeader) );
293 293
294 if ( !_retu ) { 294 if ( !_retu ) {
295 LOGGER_WARNING("Alloc failed! Program might misbehave!"); 295 LOGGER_WARNING("Alloc failed! Program might misbehave!");
296 return NULL; 296 return NULL;
@@ -386,7 +386,7 @@ RTPMessage *msg_parse ( const uint8_t *data, int length )
386 * @retval -1 Error occurred. 386 * @retval -1 Error occurred.
387 * @retval 0 Success. 387 * @retval 0 Success.
388 */ 388 */
389int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length ) 389int rtp_handle_packet ( void *object, const uint8_t *data, uint32_t length )
390{ 390{
391 RTPSession *_session = object; 391 RTPSession *_session = object;
392 RTPMessage *_msg; 392 RTPMessage *_msg;
@@ -451,7 +451,7 @@ RTPMessage *rtp_new_message ( RTPSession *session, const uint8_t *data, uint32_t
451 451
452 uint8_t *_from_pos; 452 uint8_t *_from_pos;
453 RTPMessage *_retu = calloc(1, sizeof (RTPMessage)); 453 RTPMessage *_retu = calloc(1, sizeof (RTPMessage));
454 454
455 if ( !_retu ) { 455 if ( !_retu ) {
456 LOGGER_WARNING("Alloc failed! Program might misbehave!"); 456 LOGGER_WARNING("Alloc failed! Program might misbehave!");
457 return NULL; 457 return NULL;
@@ -465,7 +465,7 @@ RTPMessage *rtp_new_message ( RTPSession *session, const uint8_t *data, uint32_t
465 uint32_t _total_length = length + _retu->header->length + 1; 465 uint32_t _total_length = length + _retu->header->length + 1;
466 466
467 _retu->data[0] = session->prefix; 467 _retu->data[0] = session->prefix;
468 468
469 if ( _retu->ext_header ) { 469 if ( _retu->ext_header ) {
470 _total_length += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 ); 470 _total_length += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
471 471
@@ -617,10 +617,10 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat
617 } 617 }
618 618
619 619
620 /* Set sequ number */ 620 /* Set sequ number */
621 session->sequnum = session->sequnum >= MAX_SEQU_NUM ? 0 : session->sequnum + 1; 621 session->sequnum = session->sequnum >= MAX_SEQU_NUM ? 0 : session->sequnum + 1;
622 rtp_free_msg ( session, msg ); 622 rtp_free_msg ( session, msg );
623 623
624 return 0; 624 return 0;
625} 625}
626 626
@@ -665,7 +665,7 @@ void rtp_free_msg ( RTPSession *session, RTPMessage *msg )
665RTPSession *rtp_init_session ( int payload_type, Messenger *messenger, int friend_num ) 665RTPSession *rtp_init_session ( int payload_type, Messenger *messenger, int friend_num )
666{ 666{
667 RTPSession *_retu = calloc(1, sizeof(RTPSession)); 667 RTPSession *_retu = calloc(1, sizeof(RTPSession));
668 668
669 if ( !_retu ) { 669 if ( !_retu ) {
670 LOGGER_WARNING("Alloc failed! Program might misbehave!"); 670 LOGGER_WARNING("Alloc failed! Program might misbehave!");
671 return NULL; 671 return NULL;
diff --git a/toxav/toxav.c b/toxav/toxav.c
index 02c5a970..743d7fcf 100644
--- a/toxav/toxav.c
+++ b/toxav/toxav.c
@@ -447,7 +447,7 @@ inline__ int toxav_send_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallTy
447 /* number of pieces - 1*/ 447 /* number of pieces - 1*/
448 uint8_t numparts = (length - 1) / VIDEOFRAME_PIECE_SIZE; 448 uint8_t numparts = (length - 1) / VIDEOFRAME_PIECE_SIZE;
449 449
450 uint8_t load[3 + VIDEOFRAME_PIECE_SIZE]; 450 uint8_t load[2 + VIDEOFRAME_PIECE_SIZE];
451 load[0] = av->calls[call_index].frame_outid++; 451 load[0] = av->calls[call_index].frame_outid++;
452 load[1] = 0; 452 load[1] = 0;
453 453
@@ -569,11 +569,11 @@ inline__ int toxav_recv_video ( ToxAv *av, int32_t call_index, vpx_image_t **out
569 call->frame_limit = 0; 569 call->frame_limit = 0;
570 570
571 if (rc != VPX_CODEC_OK) { 571 if (rc != VPX_CODEC_OK) {
572 LOGGER_ERROR("Error decoding video: %s\n", vpx_codec_err_to_string(rc)); 572 LOGGER_ERROR("Error decoding video: %u %s\n", i, vpx_codec_err_to_string(rc));
573 return ErrorInternal;
574 } 573 }
575 } else { 574 } else {
576 /* old packet, dont read */ 575 /* old packet, dont read */
576 LOGGER_DEBUG("Old packet: %u\n", i);
577 continue; 577 continue;
578 } 578 }
579 579
@@ -583,12 +583,14 @@ inline__ int toxav_recv_video ( ToxAv *av, int32_t call_index, vpx_image_t **out
583 continue; 583 continue;
584 } 584 }
585 585
586 LOGGER_DEBUG("Video Packet: %u %u\n", packet[0], packet[1]);
586 memcpy(call->frame_buf + packet[1] * VIDEOFRAME_PIECE_SIZE, packet + VIDEOFRAME_HEADER_SIZE, 587 memcpy(call->frame_buf + packet[1] * VIDEOFRAME_PIECE_SIZE, packet + VIDEOFRAME_HEADER_SIZE,
587 recved_size - VIDEOFRAME_HEADER_SIZE); 588 recved_size - VIDEOFRAME_HEADER_SIZE);
588 uint32_t limit = packet[1] * VIDEOFRAME_PIECE_SIZE + recved_size - VIDEOFRAME_HEADER_SIZE; 589 uint32_t limit = packet[1] * VIDEOFRAME_PIECE_SIZE + recved_size - VIDEOFRAME_HEADER_SIZE;
589 590
590 if (limit > call->frame_limit) { 591 if (limit > call->frame_limit) {
591 call->frame_limit = limit; 592 call->frame_limit = limit;
593 LOGGER_DEBUG("Limit: %u\n", call->frame_limit);
592 } 594 }
593 } 595 }
594 596
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 133e9392..77d126c1 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -2212,7 +2212,7 @@ static int random_node_fromlist(Client_data *list, uint16_t list_size, Node_form
2212 * 2212 *
2213 * TODO: remove the LAN stuff from this. 2213 * TODO: remove the LAN stuff from this.
2214 */ 2214 */
2215uint16_t random_nodes_path(DHT *dht, Node_format *nodes, uint16_t max_num) 2215uint16_t random_nodes_path(const DHT *dht, Node_format *nodes, uint16_t max_num)
2216{ 2216{
2217 if (max_num == 0) 2217 if (max_num == 0)
2218 return 0; 2218 return 0;
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index c7fddb4f..ea94e4ca 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -306,7 +306,7 @@ uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num);
306 * 306 *
307 * NOTE:this is used to pick nodes for paths. 307 * NOTE:this is used to pick nodes for paths.
308 */ 308 */
309uint16_t random_nodes_path(DHT *dht, Node_format *nodes, uint16_t max_num); 309uint16_t random_nodes_path(const DHT *dht, Node_format *nodes, uint16_t max_num);
310 310
311/* Run this function at least a couple times per second (It's the main loop). */ 311/* Run this function at least a couple times per second (It's the main loop). */
312void do_DHT(DHT *dht); 312void do_DHT(DHT *dht);
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index e9a66272..4344fdcb 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -40,10 +40,11 @@
40 40
41 41
42static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status); 42static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status);
43static int write_cryptpacket_id(Messenger *m, int32_t friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); 43static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data,
44 uint32_t length);
44 45
45// friend_not_valid determines if the friendnumber passed is valid in the Messenger object 46// friend_not_valid determines if the friendnumber passed is valid in the Messenger object
46static uint8_t friend_not_valid(Messenger *m, int32_t friendnumber) 47static uint8_t friend_not_valid(const Messenger *m, int32_t friendnumber)
47{ 48{
48 return (unsigned int)friendnumber >= m->numfriends; 49 return (unsigned int)friendnumber >= m->numfriends;
49} 50}
@@ -109,7 +110,7 @@ int32_t getfriend_id(const Messenger *m, const uint8_t *client_id)
109 * return 0 if success. 110 * return 0 if success.
110 * return -1 if failure. 111 * return -1 if failure.
111 */ 112 */
112int getclient_id(Messenger *m, int32_t friendnumber, uint8_t *client_id) 113int getclient_id(const Messenger *m, int32_t friendnumber, uint8_t *client_id)
113{ 114{
114 if (friend_not_valid(m, friendnumber)) 115 if (friend_not_valid(m, friendnumber))
115 return -1; 116 return -1;
@@ -125,7 +126,7 @@ int getclient_id(Messenger *m, int32_t friendnumber, uint8_t *client_id)
125 * 126 *
126 * return a uint16_t that represents the checksum of address of length len. 127 * return a uint16_t that represents the checksum of address of length len.
127 */ 128 */
128static uint16_t address_checksum(uint8_t *address, uint32_t len) 129static uint16_t address_checksum(const uint8_t *address, uint32_t len)
129{ 130{
130 uint8_t checksum[2] = {0}; 131 uint8_t checksum[2] = {0};
131 uint16_t check; 132 uint16_t check;
@@ -142,7 +143,7 @@ static uint16_t address_checksum(uint8_t *address, uint32_t len)
142 * 143 *
143 * return FRIEND_ADDRESS_SIZE byte address to give to others. 144 * return FRIEND_ADDRESS_SIZE byte address to give to others.
144 */ 145 */
145void getaddress(Messenger *m, uint8_t *address) 146void getaddress(const Messenger *m, uint8_t *address)
146{ 147{
147 id_copy(address, m->net_crypto->self_public_key); 148 id_copy(address, m->net_crypto->self_public_key);
148 uint32_t nospam = get_nospam(&(m->fr)); 149 uint32_t nospam = get_nospam(&(m->fr));
@@ -152,7 +153,7 @@ void getaddress(Messenger *m, uint8_t *address)
152} 153}
153 154
154/* callback for recv TCP relay nodes. */ 155/* callback for recv TCP relay nodes. */
155static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, uint8_t *public_key) 156static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key)
156{ 157{
157 Messenger *m = object; 158 Messenger *m = object;
158 159
@@ -183,7 +184,7 @@ static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_por
183 * (the nospam for that friend was set to the new one). 184 * (the nospam for that friend was set to the new one).
184 * return FAERR_NOMEM if increasing the friend list size fails. 185 * return FAERR_NOMEM if increasing the friend list size fails.
185 */ 186 */
186int32_t m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) 187int32_t m_addfriend(Messenger *m, const uint8_t *address, const uint8_t *data, uint16_t length)
187{ 188{
188 if (length > MAX_FRIEND_REQUEST_DATA_SIZE) 189 if (length > MAX_FRIEND_REQUEST_DATA_SIZE)
189 return FAERR_TOOLONG; 190 return FAERR_TOOLONG;
@@ -346,7 +347,7 @@ int m_delfriend(Messenger *m, int32_t friendnumber)
346 return 0; 347 return 0;
347} 348}
348 349
349int m_get_friend_connectionstatus(Messenger *m, int32_t friendnumber) 350int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber)
350{ 351{
351 if (friend_not_valid(m, friendnumber)) 352 if (friend_not_valid(m, friendnumber))
352 return -1; 353 return -1;
@@ -354,7 +355,7 @@ int m_get_friend_connectionstatus(Messenger *m, int32_t friendnumber)
354 return m->friendlist[friendnumber].status == FRIEND_ONLINE; 355 return m->friendlist[friendnumber].status == FRIEND_ONLINE;
355} 356}
356 357
357int m_friend_exists(Messenger *m, int32_t friendnumber) 358int m_friend_exists(const Messenger *m, int32_t friendnumber)
358{ 359{
359 if (friend_not_valid(m, friendnumber)) 360 if (friend_not_valid(m, friendnumber))
360 return 0; 361 return 0;
@@ -402,7 +403,7 @@ uint32_t m_sendmessage_withid(Messenger *m, int32_t friendnumber, uint32_t theid
402 * return the message id if packet was successfully put into the send queue. 403 * return the message id if packet was successfully put into the send queue.
403 * return 0 if it was not. 404 * return 0 if it was not.
404 */ 405 */
405uint32_t m_sendaction(Messenger *m, int32_t friendnumber, uint8_t *action, uint32_t length) 406uint32_t m_sendaction(Messenger *m, int32_t friendnumber, const uint8_t *action, uint32_t length)
406{ 407{
407 if (friend_not_valid(m, friendnumber)) 408 if (friend_not_valid(m, friendnumber))
408 return 0; 409 return 0;
@@ -419,7 +420,8 @@ uint32_t m_sendaction(Messenger *m, int32_t friendnumber, uint8_t *action, uint3
419 return 0; 420 return 0;
420} 421}
421 422
422uint32_t m_sendaction_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length) 423uint32_t m_sendaction_withid(const Messenger *m, int32_t friendnumber, uint32_t theid, const uint8_t *action,
424 uint32_t length)
423{ 425{
424 if (length >= (MAX_CRYPTO_DATA_SIZE - sizeof(theid))) 426 if (length >= (MAX_CRYPTO_DATA_SIZE - sizeof(theid)))
425 return 0; 427 return 0;
@@ -434,7 +436,7 @@ uint32_t m_sendaction_withid(Messenger *m, int32_t friendnumber, uint32_t theid,
434/* Send a name packet to friendnumber. 436/* Send a name packet to friendnumber.
435 * length is the length with the NULL terminator. 437 * length is the length with the NULL terminator.
436 */ 438 */
437static int m_sendname(Messenger *m, int32_t friendnumber, uint8_t *name, uint16_t length) 439static int m_sendname(const Messenger *m, int32_t friendnumber, const uint8_t *name, uint16_t length)
438{ 440{
439 if (length > MAX_NAME_LENGTH || length == 0) 441 if (length > MAX_NAME_LENGTH || length == 0)
440 return 0; 442 return 0;
@@ -447,7 +449,7 @@ static int m_sendname(Messenger *m, int32_t friendnumber, uint8_t *name, uint16_
447 * return 0 if success. 449 * return 0 if success.
448 * return -1 if failure. 450 * return -1 if failure.
449 */ 451 */
450int setfriendname(Messenger *m, int32_t friendnumber, uint8_t *name, uint16_t length) 452int setfriendname(Messenger *m, int32_t friendnumber, const uint8_t *name, uint16_t length)
451{ 453{
452 if (friend_not_valid(m, friendnumber)) 454 if (friend_not_valid(m, friendnumber))
453 return -1; 455 return -1;
@@ -492,7 +494,7 @@ int setname(Messenger *m, const uint8_t *name, uint16_t length)
492 * 494 *
493 * return the length of the name. 495 * return the length of the name.
494 */ 496 */
495uint16_t getself_name(Messenger *m, uint8_t *name) 497uint16_t getself_name(const Messenger *m, uint8_t *name)
496{ 498{
497 if (name == NULL) { 499 if (name == NULL) {
498 return 0; 500 return 0;
@@ -509,7 +511,7 @@ uint16_t getself_name(Messenger *m, uint8_t *name)
509 * return length of name if success. 511 * return length of name if success.
510 * return -1 if failure. 512 * return -1 if failure.
511 */ 513 */
512int getname(Messenger *m, int32_t friendnumber, uint8_t *name) 514int getname(const Messenger *m, int32_t friendnumber, uint8_t *name)
513{ 515{
514 if (friend_not_valid(m, friendnumber)) 516 if (friend_not_valid(m, friendnumber))
515 return -1; 517 return -1;
@@ -518,7 +520,7 @@ int getname(Messenger *m, int32_t friendnumber, uint8_t *name)
518 return m->friendlist[friendnumber].name_length; 520 return m->friendlist[friendnumber].name_length;
519} 521}
520 522
521int m_get_name_size(Messenger *m, int32_t friendnumber) 523int m_get_name_size(const Messenger *m, int32_t friendnumber)
522{ 524{
523 if (friend_not_valid(m, friendnumber)) 525 if (friend_not_valid(m, friendnumber))
524 return -1; 526 return -1;
@@ -526,7 +528,7 @@ int m_get_name_size(Messenger *m, int32_t friendnumber)
526 return m->friendlist[friendnumber].name_length; 528 return m->friendlist[friendnumber].name_length;
527} 529}
528 530
529int m_get_self_name_size(Messenger *m) 531int m_get_self_name_size(const Messenger *m)
530{ 532{
531 return m->name_length; 533 return m->name_length;
532} 534}
@@ -565,7 +567,7 @@ int m_set_userstatus(Messenger *m, uint8_t status)
565/* return the size of friendnumber's user status. 567/* return the size of friendnumber's user status.
566 * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH. 568 * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH.
567 */ 569 */
568int m_get_statusmessage_size(Messenger *m, int32_t friendnumber) 570int m_get_statusmessage_size(const Messenger *m, int32_t friendnumber)
569{ 571{
570 if (friend_not_valid(m, friendnumber)) 572 if (friend_not_valid(m, friendnumber))
571 return -1; 573 return -1;
@@ -576,7 +578,7 @@ int m_get_statusmessage_size(Messenger *m, int32_t friendnumber)
576/* Copy the user status of friendnumber into buf, truncating if needed to maxlen 578/* Copy the user status of friendnumber into buf, truncating if needed to maxlen
577 * bytes, use m_get_statusmessage_size to find out how much you need to allocate. 579 * bytes, use m_get_statusmessage_size to find out how much you need to allocate.
578 */ 580 */
579int m_copy_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen) 581int m_copy_statusmessage(const Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen)
580{ 582{
581 if (friend_not_valid(m, friendnumber)) 583 if (friend_not_valid(m, friendnumber))
582 return -1; 584 return -1;
@@ -591,12 +593,12 @@ int m_copy_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *buf, uint3
591/* return the size of friendnumber's user status. 593/* return the size of friendnumber's user status.
592 * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH. 594 * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH.
593 */ 595 */
594int m_get_self_statusmessage_size(Messenger *m) 596int m_get_self_statusmessage_size(const Messenger *m)
595{ 597{
596 return m->statusmessage_length; 598 return m->statusmessage_length;
597} 599}
598 600
599int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen) 601int m_copy_self_statusmessage(const Messenger *m, uint8_t *buf, uint32_t maxlen)
600{ 602{
601 int msglen = MIN(maxlen, m->statusmessage_length); 603 int msglen = MIN(maxlen, m->statusmessage_length);
602 memcpy(buf, m->statusmessage, msglen); 604 memcpy(buf, m->statusmessage, msglen);
@@ -604,7 +606,7 @@ int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen)
604 return msglen; 606 return msglen;
605} 607}
606 608
607uint8_t m_get_userstatus(Messenger *m, int32_t friendnumber) 609uint8_t m_get_userstatus(const Messenger *m, int32_t friendnumber)
608{ 610{
609 if (friend_not_valid(m, friendnumber)) 611 if (friend_not_valid(m, friendnumber))
610 return USERSTATUS_INVALID; 612 return USERSTATUS_INVALID;
@@ -618,12 +620,12 @@ uint8_t m_get_userstatus(Messenger *m, int32_t friendnumber)
618 return status; 620 return status;
619} 621}
620 622
621uint8_t m_get_self_userstatus(Messenger *m) 623uint8_t m_get_self_userstatus(const Messenger *m)
622{ 624{
623 return m->userstatus; 625 return m->userstatus;
624} 626}
625 627
626uint64_t m_get_last_online(Messenger *m, int32_t friendnumber) 628uint64_t m_get_last_online(const Messenger *m, int32_t friendnumber)
627{ 629{
628 if (friend_not_valid(m, friendnumber)) 630 if (friend_not_valid(m, friendnumber))
629 return -1; 631 return -1;
@@ -647,7 +649,7 @@ int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing)
647 return 0; 649 return 0;
648} 650}
649 651
650uint8_t m_get_istyping(Messenger *m, int32_t friendnumber) 652uint8_t m_get_istyping(const Messenger *m, int32_t friendnumber)
651{ 653{
652 if (friend_not_valid(m, friendnumber)) 654 if (friend_not_valid(m, friendnumber))
653 return -1; 655 return -1;
@@ -655,23 +657,23 @@ uint8_t m_get_istyping(Messenger *m, int32_t friendnumber)
655 return m->friendlist[friendnumber].is_typing; 657 return m->friendlist[friendnumber].is_typing;
656} 658}
657 659
658static int send_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *status, uint16_t length) 660static int send_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length)
659{ 661{
660 return write_cryptpacket_id(m, friendnumber, PACKET_ID_STATUSMESSAGE, status, length); 662 return write_cryptpacket_id(m, friendnumber, PACKET_ID_STATUSMESSAGE, status, length);
661} 663}
662 664
663static int send_userstatus(Messenger *m, int32_t friendnumber, uint8_t status) 665static int send_userstatus(const Messenger *m, int32_t friendnumber, uint8_t status)
664{ 666{
665 return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &status, sizeof(status)); 667 return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &status, sizeof(status));
666} 668}
667 669
668static int send_user_istyping(Messenger *m, int32_t friendnumber, uint8_t is_typing) 670static int send_user_istyping(const Messenger *m, int32_t friendnumber, uint8_t is_typing)
669{ 671{
670 uint8_t typing = is_typing; 672 uint8_t typing = is_typing;
671 return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing)); 673 return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing));
672} 674}
673 675
674static int send_ping(Messenger *m, int32_t friendnumber) 676static int send_ping(const Messenger *m, int32_t friendnumber)
675{ 677{
676 int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_ALIVE, 0, 0); 678 int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_ALIVE, 0, 0);
677 679
@@ -681,7 +683,7 @@ static int send_ping(Messenger *m, int32_t friendnumber)
681 return ret; 683 return ret;
682} 684}
683 685
684static int send_relays(Messenger *m, int32_t friendnumber) 686static int send_relays(const Messenger *m, int32_t friendnumber)
685{ 687{
686 Node_format nodes[MAX_SHARED_RELAYS]; 688 Node_format nodes[MAX_SHARED_RELAYS];
687 uint8_t data[1024]; 689 uint8_t data[1024];
@@ -700,7 +702,7 @@ static int send_relays(Messenger *m, int32_t friendnumber)
700 702
701 703
702 704
703static int set_friend_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *status, uint16_t length) 705static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length)
704{ 706{
705 if (friend_not_valid(m, friendnumber)) 707 if (friend_not_valid(m, friendnumber))
706 return -1; 708 return -1;
@@ -713,12 +715,12 @@ static int set_friend_statusmessage(Messenger *m, int32_t friendnumber, uint8_t
713 return 0; 715 return 0;
714} 716}
715 717
716static void set_friend_userstatus(Messenger *m, int32_t friendnumber, uint8_t status) 718static void set_friend_userstatus(const Messenger *m, int32_t friendnumber, uint8_t status)
717{ 719{
718 m->friendlist[friendnumber].userstatus = status; 720 m->friendlist[friendnumber].userstatus = status;
719} 721}
720 722
721static void set_friend_typing(Messenger *m, int32_t friendnumber, uint8_t is_typing) 723static void set_friend_typing(const Messenger *m, int32_t friendnumber, uint8_t is_typing)
722{ 724{
723 m->friendlist[friendnumber].is_typing = is_typing; 725 m->friendlist[friendnumber].is_typing = is_typing;
724} 726}
@@ -745,28 +747,28 @@ void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, const
745} 747}
746 748
747/* Set the function that will be executed when a message from a friend is received. */ 749/* Set the function that will be executed when a message from a friend is received. */
748void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), 750void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int32_t, const uint8_t *, uint16_t, void *),
749 void *userdata) 751 void *userdata)
750{ 752{
751 m->friend_message = function; 753 m->friend_message = function;
752 m->friend_message_userdata = userdata; 754 m->friend_message_userdata = userdata;
753} 755}
754 756
755void m_callback_action(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), 757void m_callback_action(Messenger *m, void (*function)(Messenger *m, int32_t, const uint8_t *, uint16_t, void *),
756 void *userdata) 758 void *userdata)
757{ 759{
758 m->friend_action = function; 760 m->friend_action = function;
759 m->friend_action_userdata = userdata; 761 m->friend_action_userdata = userdata;
760} 762}
761 763
762void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), 764void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int32_t, const uint8_t *, uint16_t, void *),
763 void *userdata) 765 void *userdata)
764{ 766{
765 m->friend_namechange = function; 767 m->friend_namechange = function;
766 m->friend_namechange_userdata = userdata; 768 m->friend_namechange_userdata = userdata;
767} 769}
768 770
769void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), 771void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int32_t, const uint8_t *, uint16_t, void *),
770 void *userdata) 772 void *userdata)
771{ 773{
772 m->friend_statusmessagechange = function; 774 m->friend_statusmessagechange = function;
@@ -804,7 +806,7 @@ void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Mess
804 m->friend_connectionstatuschange_internal_userdata = userdata; 806 m->friend_connectionstatuschange_internal_userdata = userdata;
805} 807}
806 808
807static void break_files(Messenger *m, int32_t friendnumber); 809static void break_files(const Messenger *m, int32_t friendnumber);
808static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, uint8_t status) 810static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, uint8_t status)
809{ 811{
810 if (status == NOFRIEND) 812 if (status == NOFRIEND)
@@ -840,7 +842,8 @@ void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status)
840 m->friendlist[friendnumber].status = status; 842 m->friendlist[friendnumber].status = status;
841} 843}
842 844
843int write_cryptpacket_id(Messenger *m, int32_t friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) 845int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data,
846 uint32_t length)
844{ 847{
845 if (friend_not_valid(m, friendnumber)) 848 if (friend_not_valid(m, friendnumber))
846 return 0; 849 return 0;
@@ -862,7 +865,7 @@ int write_cryptpacket_id(Messenger *m, int32_t friendnumber, uint8_t packet_id,
862/* return 1 if the groupnumber is not valid. 865/* return 1 if the groupnumber is not valid.
863 * return 0 if the groupnumber is valid. 866 * return 0 if the groupnumber is valid.
864 */ 867 */
865static uint8_t groupnumber_not_valid(Messenger *m, int groupnumber) 868static uint8_t groupnumber_not_valid(const Messenger *m, int groupnumber)
866{ 869{
867 if ((unsigned int)groupnumber >= m->numchats) 870 if ((unsigned int)groupnumber >= m->numchats)
868 return 1; 871 return 1;
@@ -880,7 +883,7 @@ static uint8_t groupnumber_not_valid(Messenger *m, int groupnumber)
880/* returns valid ip port of connected friend on success 883/* returns valid ip port of connected friend on success
881 * returns zeroed out IP_Port on failure 884 * returns zeroed out IP_Port on failure
882 */ 885 */
883static IP_Port get_friend_ipport(Messenger *m, int32_t friendnumber) 886static IP_Port get_friend_ipport(const Messenger *m, int32_t friendnumber)
884{ 887{
885 IP_Port zero; 888 IP_Port zero;
886 memset(&zero, 0, sizeof(zero)); 889 memset(&zero, 0, sizeof(zero));
@@ -904,7 +907,7 @@ static IP_Port get_friend_ipport(Messenger *m, int32_t friendnumber)
904/* returns the group number of the chat with public key group_public_key. 907/* returns the group number of the chat with public key group_public_key.
905 * returns -1 on failure. 908 * returns -1 on failure.
906 */ 909 */
907static int group_num(Messenger *m, uint8_t *group_public_key) 910static int group_num(const Messenger *m, const uint8_t *group_public_key)
908{ 911{
909 uint32_t i; 912 uint32_t i;
910 913
@@ -921,7 +924,8 @@ static int group_num(Messenger *m, uint8_t *group_public_key)
921 * 924 *
922 * Function(Messenger *m, int32_t friendnumber, uint8_t *group_public_key, void *userdata) 925 * Function(Messenger *m, int32_t friendnumber, uint8_t *group_public_key, void *userdata)
923 */ 926 */
924void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, void *), void *userdata) 927void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int32_t, const uint8_t *, void *),
928 void *userdata)
925{ 929{
926 m->group_invite = function; 930 m->group_invite = function;
927 m->group_invite_userdata = userdata; 931 m->group_invite_userdata = userdata;
@@ -931,7 +935,7 @@ void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int32_
931 * 935 *
932 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) 936 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata)
933 */ 937 */
934void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *), 938void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, int, const uint8_t *, uint16_t, void *),
935 void *userdata) 939 void *userdata)
936{ 940{
937 m->group_message = function; 941 m->group_message = function;
@@ -942,7 +946,7 @@ void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int,
942 * 946 *
943 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) 947 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata)
944 */ 948 */
945void m_callback_group_action(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *), 949void m_callback_group_action(Messenger *m, void (*function)(Messenger *m, int, int, const uint8_t *, uint16_t, void *),
946 void *userdata) 950 void *userdata)
947{ 951{
948 m->group_action = function; 952 m->group_action = function;
@@ -961,7 +965,7 @@ void m_callback_group_namelistchange(Messenger *m, void (*function)(Messenger *m
961 m->group_namelistchange_userdata = userdata; 965 m->group_namelistchange_userdata = userdata;
962} 966}
963 967
964static int get_chat_num(Messenger *m, Group_Chat *chat) 968static int get_chat_num(const Messenger *m, const Group_Chat *chat)
965{ 969{
966 uint32_t i; 970 uint32_t i;
967 971
@@ -1114,7 +1118,7 @@ int del_groupchat(Messenger *m, int groupnumber)
1114 * return length of name if success 1118 * return length of name if success
1115 * return -1 if failure 1119 * return -1 if failure
1116 */ 1120 */
1117int m_group_peername(Messenger *m, int groupnumber, int peernumber, uint8_t *name) 1121int m_group_peername(const Messenger *m, int groupnumber, int peernumber, uint8_t *name)
1118{ 1122{
1119 if ((unsigned int)groupnumber >= m->numchats) 1123 if ((unsigned int)groupnumber >= m->numchats)
1120 return -1; 1124 return -1;
@@ -1141,7 +1145,7 @@ static void group_store_friendinvite(Messenger *m, int32_t friendnumber, int gro
1141/* return 1 if that friend was invited to the group 1145/* return 1 if that friend was invited to the group
1142 * return 0 if the friend was not or error. 1146 * return 0 if the friend was not or error.
1143 */ 1147 */
1144static uint8_t group_invited(Messenger *m, int32_t friendnumber, int groupnumber) 1148static uint8_t group_invited(const Messenger *m, int32_t friendnumber, int groupnumber)
1145{ 1149{
1146 1150
1147 uint32_t i; 1151 uint32_t i;
@@ -1189,7 +1193,7 @@ int invite_friend(Messenger *m, int32_t friendnumber, int groupnumber)
1189 * returns group number on success 1193 * returns group number on success
1190 * returns -1 on failure. 1194 * returns -1 on failure.
1191 */ 1195 */
1192int join_groupchat(Messenger *m, int32_t friendnumber, uint8_t *friend_group_public_key) 1196int join_groupchat(Messenger *m, int32_t friendnumber, const uint8_t *friend_group_public_key)
1193{ 1197{
1194 if (friend_not_valid(m, friendnumber)) 1198 if (friend_not_valid(m, friendnumber))
1195 return -1; 1199 return -1;
@@ -1222,7 +1226,7 @@ int join_groupchat(Messenger *m, int32_t friendnumber, uint8_t *friend_group_pub
1222 * return 0 on success 1226 * return 0 on success
1223 * return -1 on failure 1227 * return -1 on failure
1224 */ 1228 */
1225int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length) 1229int group_message_send(const Messenger *m, int groupnumber, const uint8_t *message, uint32_t length)
1226{ 1230{
1227 if (groupnumber_not_valid(m, groupnumber)) 1231 if (groupnumber_not_valid(m, groupnumber))
1228 return -1; 1232 return -1;
@@ -1237,7 +1241,7 @@ int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t
1237 * return 0 on success 1241 * return 0 on success
1238 * return -1 on failure 1242 * return -1 on failure
1239 */ 1243 */
1240int group_action_send(Messenger *m, int groupnumber, uint8_t *action, uint32_t length) 1244int group_action_send(const Messenger *m, int groupnumber, const uint8_t *action, uint32_t length)
1241{ 1245{
1242 if (groupnumber_not_valid(m, groupnumber)) 1246 if (groupnumber_not_valid(m, groupnumber))
1243 return -1; 1247 return -1;
@@ -1251,7 +1255,7 @@ int group_action_send(Messenger *m, int groupnumber, uint8_t *action, uint32_t l
1251/* Return the number of peers in the group chat on success. 1255/* Return the number of peers in the group chat on success.
1252 * return -1 on failure 1256 * return -1 on failure
1253 */ 1257 */
1254int group_number_peers(Messenger *m, int groupnumber) 1258int group_number_peers(const Messenger *m, int groupnumber)
1255{ 1259{
1256 if (groupnumber_not_valid(m, groupnumber)) 1260 if (groupnumber_not_valid(m, groupnumber))
1257 return -1; 1261 return -1;
@@ -1269,7 +1273,8 @@ int group_number_peers(Messenger *m, int groupnumber)
1269 * 1273 *
1270 * return -1 on failure. 1274 * return -1 on failure.
1271 */ 1275 */
1272int group_names(Messenger *m, int groupnumber, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length) 1276int group_names(const Messenger *m, int groupnumber, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[],
1277 uint16_t length)
1273{ 1278{
1274 if (groupnumber_not_valid(m, groupnumber)) 1279 if (groupnumber_not_valid(m, groupnumber))
1275 return -1; 1280 return -1;
@@ -1315,7 +1320,7 @@ static void do_allgroupchats(Messenger *m)
1315 * 1320 *
1316 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) 1321 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata)
1317 */ 1322 */
1318void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint64_t, uint8_t *, 1323void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint64_t, const uint8_t *,
1319 uint16_t, void *), void *userdata) 1324 uint16_t, void *), void *userdata)
1320{ 1325{
1321 m->file_sendrequest = function; 1326 m->file_sendrequest = function;
@@ -1327,8 +1332,8 @@ void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int3
1327 * Function(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) 1332 * Function(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
1328 * 1333 *
1329 */ 1334 */
1330void callback_file_control(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *, 1335void callback_file_control(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t, uint8_t,
1331 uint16_t, void *), void *userdata) 1336 const uint8_t *, uint16_t, void *), void *userdata)
1332{ 1337{
1333 m->file_filecontrol = function; 1338 m->file_filecontrol = function;
1334 m->file_filecontrol_userdata = userdata; 1339 m->file_filecontrol_userdata = userdata;
@@ -1339,7 +1344,7 @@ void callback_file_control(Messenger *m, void (*function)(Messenger *m, int32_t,
1339 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) 1344 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata)
1340 * 1345 *
1341 */ 1346 */
1342void callback_file_data(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t *, uint16_t length, 1347void callback_file_data(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, const uint8_t *, uint16_t length,
1343 void *), void *userdata) 1348 void *), void *userdata)
1344{ 1349{
1345 m->file_filedata = function; 1350 m->file_filedata = function;
@@ -1353,8 +1358,8 @@ void callback_file_data(Messenger *m, void (*function)(Messenger *m, int32_t, ui
1353 * return 1 on success 1358 * return 1 on success
1354 * return 0 on failure 1359 * return 0 on failure
1355 */ 1360 */
1356int file_sendrequest(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, 1361int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t filenumber, uint64_t filesize,
1357 uint16_t filename_length) 1362 const uint8_t *filename, uint16_t filename_length)
1358{ 1363{
1359 if (friend_not_valid(m, friendnumber)) 1364 if (friend_not_valid(m, friendnumber))
1360 return 0; 1365 return 0;
@@ -1376,7 +1381,8 @@ int file_sendrequest(Messenger *m, int32_t friendnumber, uint8_t filenumber, uin
1376 * return file number on success 1381 * return file number on success
1377 * return -1 on failure 1382 * return -1 on failure
1378 */ 1383 */
1379int new_filesender(Messenger *m, int32_t friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length) 1384int new_filesender(const Messenger *m, int32_t friendnumber, uint64_t filesize, const uint8_t *filename,
1385 uint16_t filename_length)
1380{ 1386{
1381 if (friend_not_valid(m, friendnumber)) 1387 if (friend_not_valid(m, friendnumber))
1382 return -1; 1388 return -1;
@@ -1406,8 +1412,8 @@ int new_filesender(Messenger *m, int32_t friendnumber, uint64_t filesize, uint8_
1406 * return 0 on success 1412 * return 0 on success
1407 * return -1 on failure 1413 * return -1 on failure
1408 */ 1414 */
1409int file_control(Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, 1415int file_control(const Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
1410 uint8_t *data, uint16_t length) 1416 const uint8_t *data, uint16_t length)
1411{ 1417{
1412 if (length > MAX_CRYPTO_DATA_SIZE - 3) 1418 if (length > MAX_CRYPTO_DATA_SIZE - 3)
1413 return -1; 1419 return -1;
@@ -1494,7 +1500,7 @@ int file_control(Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8
1494 * return 0 on success 1500 * return 0 on success
1495 * return -1 on failure 1501 * return -1 on failure
1496 */ 1502 */
1497int file_data(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length) 1503int file_data(const Messenger *m, int32_t friendnumber, uint8_t filenumber, const uint8_t *data, uint16_t length)
1498{ 1504{
1499 if (length > MAX_CRYPTO_DATA_SIZE - 1) 1505 if (length > MAX_CRYPTO_DATA_SIZE - 1)
1500 return -1; 1506 return -1;
@@ -1529,7 +1535,7 @@ int file_data(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t *d
1529 * return number of bytes remaining to be sent/received on success 1535 * return number of bytes remaining to be sent/received on success
1530 * return 0 on failure 1536 * return 0 on failure
1531 */ 1537 */
1532uint64_t file_dataremaining(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive) 1538uint64_t file_dataremaining(const Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive)
1533{ 1539{
1534 if (friend_not_valid(m, friendnumber)) 1540 if (friend_not_valid(m, friendnumber))
1535 return 0; 1541 return 0;
@@ -1552,7 +1558,7 @@ uint64_t file_dataremaining(Messenger *m, int32_t friendnumber, uint8_t filenumb
1552/* Run this when the friend disconnects. 1558/* Run this when the friend disconnects.
1553 * Sets all current file transfers to broken. 1559 * Sets all current file transfers to broken.
1554 */ 1560 */
1555static void break_files(Messenger *m, int32_t friendnumber) 1561static void break_files(const Messenger *m, int32_t friendnumber)
1556{ 1562{
1557 uint32_t i; 1563 uint32_t i;
1558 1564
@@ -1565,7 +1571,7 @@ static void break_files(Messenger *m, int32_t friendnumber)
1565 } 1571 }
1566} 1572}
1567 1573
1568static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receive_send, uint8_t filenumber, 1574static int handle_filecontrol(const Messenger *m, int32_t friendnumber, uint8_t receive_send, uint8_t filenumber,
1569 uint8_t message_id, uint8_t *data, 1575 uint8_t message_id, uint8_t *data,
1570 uint16_t length) 1576 uint16_t length)
1571{ 1577{
@@ -1655,7 +1661,7 @@ static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receiv
1655 * 1661 *
1656 * Function(Messenger *m, int friendnumber, uint8_t *data, uint16_t length, void *userdata) 1662 * Function(Messenger *m, int friendnumber, uint8_t *data, uint16_t length, void *userdata)
1657 */ 1663 */
1658void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), 1664void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int32_t, const uint8_t *, uint16_t, void *),
1659 void *userdata) 1665 void *userdata)
1660{ 1666{
1661 m->msi_packet = function; 1667 m->msi_packet = function;
@@ -1667,12 +1673,12 @@ void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int32_t,
1667 * return 1 on success 1673 * return 1 on success
1668 * return 0 on failure 1674 * return 0 on failure
1669 */ 1675 */
1670int m_msi_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t length) 1676int m_msi_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length)
1671{ 1677{
1672 return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length); 1678 return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length);
1673} 1679}
1674 1680
1675static int handle_custom_lossy_packet(void *object, int friend_num, uint8_t *packet, uint16_t length) 1681static int handle_custom_lossy_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length)
1676{ 1682{
1677 Messenger *m = object; 1683 Messenger *m = object;
1678 1684
@@ -1687,7 +1693,7 @@ static int handle_custom_lossy_packet(void *object, int friend_num, uint8_t *pac
1687} 1693}
1688 1694
1689int custom_lossy_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, 1695int custom_lossy_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte,
1690 int (*packet_handler_callback)(void *object, uint8_t *data, uint32_t len), void *object) 1696 int (*packet_handler_callback)(void *object, const uint8_t *data, uint32_t len), void *object)
1691{ 1697{
1692 if (friend_not_valid(m, friendnumber)) 1698 if (friend_not_valid(m, friendnumber))
1693 return -1; 1699 return -1;
@@ -1703,7 +1709,7 @@ int custom_lossy_packet_registerhandler(Messenger *m, int32_t friendnumber, uint
1703 return 0; 1709 return 0;
1704} 1710}
1705 1711
1706int send_custom_lossy_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint32_t length) 1712int send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length)
1707{ 1713{
1708 if (friend_not_valid(m, friendnumber)) 1714 if (friend_not_valid(m, friendnumber))
1709 return -1; 1715 return -1;
@@ -1717,7 +1723,7 @@ int send_custom_lossy_packet(Messenger *m, int32_t friendnumber, uint8_t *data,
1717 return send_lossy_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, data, length); 1723 return send_lossy_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, data, length);
1718} 1724}
1719 1725
1720static int handle_custom_lossless_packet(void *object, int friend_num, uint8_t *packet, uint16_t length) 1726static int handle_custom_lossless_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length)
1721{ 1727{
1722 Messenger *m = object; 1728 Messenger *m = object;
1723 1729
@@ -1738,7 +1744,7 @@ static int handle_custom_lossless_packet(void *object, int friend_num, uint8_t *
1738} 1744}
1739 1745
1740int custom_lossless_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, 1746int custom_lossless_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte,
1741 int (*packet_handler_callback)(void *object, uint8_t *data, uint32_t len), void *object) 1747 int (*packet_handler_callback)(void *object, const uint8_t *data, uint32_t len), void *object)
1742{ 1748{
1743 if (friend_not_valid(m, friendnumber)) 1749 if (friend_not_valid(m, friendnumber))
1744 return -1; 1750 return -1;
@@ -1755,7 +1761,7 @@ int custom_lossless_packet_registerhandler(Messenger *m, int32_t friendnumber, u
1755 return 0; 1761 return 0;
1756} 1762}
1757 1763
1758int send_custom_lossless_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint32_t length) 1764int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length)
1759{ 1765{
1760 if (friend_not_valid(m, friendnumber)) 1766 if (friend_not_valid(m, friendnumber))
1761 return -1; 1767 return -1;
@@ -1772,7 +1778,7 @@ int send_custom_lossless_packet(Messenger *m, int32_t friendnumber, uint8_t *dat
1772/* Function to filter out some friend requests*/ 1778/* Function to filter out some friend requests*/
1773static int friend_already_added(const uint8_t *client_id, void *data) 1779static int friend_already_added(const uint8_t *client_id, void *data)
1774{ 1780{
1775 Messenger *m = data; 1781 const Messenger *m = data;
1776 1782
1777 if (getfriend_id(m, client_id) == -1) 1783 if (getfriend_id(m, client_id) == -1)
1778 return 0; 1784 return 0;
@@ -2029,13 +2035,13 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len)
2029 } 2035 }
2030 2036
2031 case PACKET_ID_MESSAGE: { 2037 case PACKET_ID_MESSAGE: {
2032 uint8_t *message_id = data; 2038 const uint8_t *message_id = data;
2033 uint8_t message_id_length = 4; 2039 uint8_t message_id_length = 4;
2034 2040
2035 if (data_length <= message_id_length) 2041 if (data_length <= message_id_length)
2036 break; 2042 break;
2037 2043
2038 uint8_t *message = data + message_id_length; 2044 const uint8_t *message = data + message_id_length;
2039 uint16_t message_length = data_length - message_id_length; 2045 uint16_t message_length = data_length - message_id_length;
2040 2046
2041 /* Make sure the NULL terminator is present. */ 2047 /* Make sure the NULL terminator is present. */
@@ -2054,13 +2060,13 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len)
2054 } 2060 }
2055 2061
2056 case PACKET_ID_ACTION: { 2062 case PACKET_ID_ACTION: {
2057 uint8_t *message_id = data; 2063 const uint8_t *message_id = data;
2058 uint8_t message_id_length = 4; 2064 uint8_t message_id_length = 4;
2059 2065
2060 if (data_length <= message_id_length) 2066 if (data_length <= message_id_length)
2061 break; 2067 break;
2062 2068
2063 uint8_t *action = data + message_id_length; 2069 const uint8_t *action = data + message_id_length;
2064 uint16_t action_length = data_length - message_id_length; 2070 uint16_t action_length = data_length - message_id_length;
2065 2071
2066 /* Make sure the NULL terminator is present. */ 2072 /* Make sure the NULL terminator is present. */
@@ -2128,8 +2134,8 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len)
2128 2134
2129 uint8_t filenumber = data[0]; 2135 uint8_t filenumber = data[0];
2130 uint64_t filesize; 2136 uint64_t filesize;
2131 net_to_host(data + 1, sizeof(filesize));
2132 memcpy(&filesize, data + 1, sizeof(filesize)); 2137 memcpy(&filesize, data + 1, sizeof(filesize));
2138 net_to_host((uint8_t *) &filesize, sizeof(filesize));
2133 m->friendlist[i].file_receiving[filenumber].status = FILESTATUS_NOT_ACCEPTED; 2139 m->friendlist[i].file_receiving[filenumber].status = FILESTATUS_NOT_ACCEPTED;
2134 m->friendlist[i].file_receiving[filenumber].size = filesize; 2140 m->friendlist[i].file_receiving[filenumber].size = filesize;
2135 m->friendlist[i].file_receiving[filenumber].transferred = 0; 2141 m->friendlist[i].file_receiving[filenumber].transferred = 0;
@@ -2216,7 +2222,7 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len)
2216 return 0; 2222 return 0;
2217} 2223}
2218 2224
2219static int friend_new_connection(Messenger *m, int32_t friendnumber, uint8_t *real_public_key) 2225static int friend_new_connection(Messenger *m, int32_t friendnumber, const uint8_t *real_public_key)
2220{ 2226{
2221 if (friend_not_valid(m, friendnumber)) 2227 if (friend_not_valid(m, friendnumber))
2222 return -1; 2228 return -1;
@@ -2337,7 +2343,7 @@ void do_friends(Messenger *m)
2337#define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60UL 2343#define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60UL
2338static time_t lastdump = 0; 2344static time_t lastdump = 0;
2339static char IDString[CLIENT_ID_SIZE * 2 + 1]; 2345static char IDString[CLIENT_ID_SIZE * 2 + 1];
2340static char *ID2String(uint8_t *client_id) 2346static char *ID2String(const uint8_t *client_id)
2341{ 2347{
2342 uint32_t i; 2348 uint32_t i;
2343 2349
@@ -2349,7 +2355,7 @@ static char *ID2String(uint8_t *client_id)
2349} 2355}
2350#endif 2356#endif
2351 2357
2352/* Minimum messenger run interval in ms 2358/* Minimum messenger run interval in ms
2353 TODO: A/V */ 2359 TODO: A/V */
2354#define MIN_RUN_INTERVAL 50 2360#define MIN_RUN_INTERVAL 50
2355 2361
@@ -2530,12 +2536,12 @@ struct SAVED_FRIEND {
2530 uint64_t ping_lastrecv; 2536 uint64_t ping_lastrecv;
2531}; 2537};
2532 2538
2533static uint32_t saved_friendslist_size(Messenger *m) 2539static uint32_t saved_friendslist_size(const Messenger *m)
2534{ 2540{
2535 return count_friendlist(m) * sizeof(struct SAVED_FRIEND); 2541 return count_friendlist(m) * sizeof(struct SAVED_FRIEND);
2536} 2542}
2537 2543
2538static uint32_t friends_list_save(Messenger *m, uint8_t *data) 2544static uint32_t friends_list_save(const Messenger *m, uint8_t *data)
2539{ 2545{
2540 uint32_t i; 2546 uint32_t i;
2541 uint32_t num = 0; 2547 uint32_t num = 0;
@@ -2614,7 +2620,7 @@ static int friends_list_load(Messenger *m, const uint8_t *data, uint32_t length)
2614} 2620}
2615 2621
2616/* return size of the messenger data (for saving) */ 2622/* return size of the messenger data (for saving) */
2617uint32_t messenger_size(Messenger *m) 2623uint32_t messenger_size(const Messenger *m)
2618{ 2624{
2619 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; 2625 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
2620 return size32 * 2 // global cookie 2626 return size32 * 2 // global cookie
@@ -2639,7 +2645,7 @@ static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t typ
2639 2645
2640 2646
2641/* Save the messenger in data of size Messenger_size(). */ 2647/* Save the messenger in data of size Messenger_size(). */
2642void messenger_save(Messenger *m, uint8_t *data) 2648void messenger_save(const Messenger *m, uint8_t *data)
2643{ 2649{
2644 uint32_t len; 2650 uint32_t len;
2645 uint16_t type; 2651 uint16_t type;
@@ -2776,7 +2782,7 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3
2776} 2782}
2777 2783
2778/* Load the messenger from data of size length. */ 2784/* Load the messenger from data of size length. */
2779int messenger_load(Messenger *m, uint8_t *data, uint32_t length) 2785int messenger_load(Messenger *m, const uint8_t *data, uint32_t length)
2780{ 2786{
2781 uint32_t data32[2]; 2787 uint32_t data32[2];
2782 uint32_t cookie_len = sizeof(data32); 2788 uint32_t cookie_len = sizeof(data32);
@@ -2796,7 +2802,7 @@ int messenger_load(Messenger *m, uint8_t *data, uint32_t length)
2796/* Return the number of friends in the instance m. 2802/* Return the number of friends in the instance m.
2797 * You should use this to determine how much memory to allocate 2803 * You should use this to determine how much memory to allocate
2798 * for copy_friendlist. */ 2804 * for copy_friendlist. */
2799uint32_t count_friendlist(Messenger *m) 2805uint32_t count_friendlist(const Messenger *m)
2800{ 2806{
2801 uint32_t ret = 0; 2807 uint32_t ret = 0;
2802 uint32_t i; 2808 uint32_t i;
@@ -2811,7 +2817,7 @@ uint32_t count_friendlist(Messenger *m)
2811} 2817}
2812 2818
2813/* Return the number of online friends in the instance m. */ 2819/* Return the number of online friends in the instance m. */
2814uint32_t get_num_online_friends(Messenger *m) 2820uint32_t get_num_online_friends(const Messenger *m)
2815{ 2821{
2816 return m->numonline_friends; 2822 return m->numonline_friends;
2817} 2823}
@@ -2821,7 +2827,7 @@ uint32_t get_num_online_friends(Messenger *m)
2821 * Otherwise, returns the number of elements copied. 2827 * Otherwise, returns the number of elements copied.
2822 * If the array was too small, the contents 2828 * If the array was too small, the contents
2823 * of out_list will be truncated to list_size. */ 2829 * of out_list will be truncated to list_size. */
2824uint32_t copy_friendlist(Messenger *m, int32_t *out_list, uint32_t list_size) 2830uint32_t copy_friendlist(Messenger const *m, int32_t *out_list, uint32_t list_size)
2825{ 2831{
2826 if (!out_list) 2832 if (!out_list)
2827 return 0; 2833 return 0;
@@ -2853,7 +2859,7 @@ uint32_t copy_friendlist(Messenger *m, int32_t *out_list, uint32_t list_size)
2853 * retun 0 if success. 2859 * retun 0 if success.
2854 * return -1 if failure. 2860 * return -1 if failure.
2855 */ 2861 */
2856int get_friendlist(Messenger *m, int32_t **out_list, uint32_t *out_list_length) 2862int get_friendlist(const Messenger *m, int32_t **out_list, uint32_t *out_list_length)
2857{ 2863{
2858 uint32_t i; 2864 uint32_t i;
2859 2865
@@ -2883,7 +2889,7 @@ int get_friendlist(Messenger *m, int32_t **out_list, uint32_t *out_list_length)
2883/* Return the number of chats in the instance m. 2889/* Return the number of chats in the instance m.
2884 * You should use this to determine how much memory to allocate 2890 * You should use this to determine how much memory to allocate
2885 * for copy_chatlist. */ 2891 * for copy_chatlist. */
2886uint32_t count_chatlist(Messenger *m) 2892uint32_t count_chatlist(const Messenger *m)
2887{ 2893{
2888 uint32_t ret = 0; 2894 uint32_t ret = 0;
2889 uint32_t i; 2895 uint32_t i;
@@ -2902,7 +2908,7 @@ uint32_t count_chatlist(Messenger *m)
2902 * Otherwise, returns the number of elements copied. 2908 * Otherwise, returns the number of elements copied.
2903 * If the array was too small, the contents 2909 * If the array was too small, the contents
2904 * of out_list will be truncated to list_size. */ 2910 * of out_list will be truncated to list_size. */
2905uint32_t copy_chatlist(Messenger *m, int *out_list, uint32_t list_size) 2911uint32_t copy_chatlist(const Messenger *m, int *out_list, uint32_t list_size)
2906{ 2912{
2907 if (!out_list) 2913 if (!out_list)
2908 return 0; 2914 return 0;
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index e6ea59c1..ce0e5825 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -172,12 +172,12 @@ typedef struct {
172 uint16_t invited_groups_num; 172 uint16_t invited_groups_num;
173 173
174 struct { 174 struct {
175 int (*function)(void *object, uint8_t *data, uint32_t len); 175 int (*function)(void *object, const uint8_t *data, uint32_t len);
176 void *object; 176 void *object;
177 } lossy_packethandlers[PACKET_ID_LOSSY_RANGE_SIZE]; 177 } lossy_packethandlers[PACKET_ID_LOSSY_RANGE_SIZE];
178 178
179 struct { 179 struct {
180 int (*function)(void *object, uint8_t *data, uint32_t len); 180 int (*function)(void *object, const uint8_t *data, uint32_t len);
181 void *object; 181 void *object;
182 } lossless_packethandlers[PACKET_ID_LOSSLESS_RANGE_SIZE]; 182 } lossless_packethandlers[PACKET_ID_LOSSLESS_RANGE_SIZE];
183} Friend; 183} Friend;
@@ -212,13 +212,13 @@ typedef struct Messenger {
212 212
213 uint64_t last_LANdiscovery; 213 uint64_t last_LANdiscovery;
214 214
215 void (*friend_message)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *); 215 void (*friend_message)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *);
216 void *friend_message_userdata; 216 void *friend_message_userdata;
217 void (*friend_action)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *); 217 void (*friend_action)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *);
218 void *friend_action_userdata; 218 void *friend_action_userdata;
219 void (*friend_namechange)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *); 219 void (*friend_namechange)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *);
220 void *friend_namechange_userdata; 220 void *friend_namechange_userdata;
221 void (*friend_statusmessagechange)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *); 221 void (*friend_statusmessagechange)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *);
222 void *friend_statusmessagechange_userdata; 222 void *friend_statusmessagechange_userdata;
223 void (*friend_userstatuschange)(struct Messenger *m, int32_t, uint8_t, void *); 223 void (*friend_userstatuschange)(struct Messenger *m, int32_t, uint8_t, void *);
224 void *friend_userstatuschange_userdata; 224 void *friend_userstatuschange_userdata;
@@ -233,23 +233,23 @@ typedef struct Messenger {
233 void (*friend_connectionstatuschange_internal)(struct Messenger *m, int32_t, uint8_t, void *); 233 void (*friend_connectionstatuschange_internal)(struct Messenger *m, int32_t, uint8_t, void *);
234 void *friend_connectionstatuschange_internal_userdata; 234 void *friend_connectionstatuschange_internal_userdata;
235 235
236 void (*group_invite)(struct Messenger *m, int32_t, uint8_t *, void *); 236 void (*group_invite)(struct Messenger *m, int32_t, const uint8_t *, void *);
237 void *group_invite_userdata; 237 void *group_invite_userdata;
238 void (*group_message)(struct Messenger *m, int, int, uint8_t *, uint16_t, void *); 238 void (*group_message)(struct Messenger *m, int, int, const uint8_t *, uint16_t, void *);
239 void *group_message_userdata; 239 void *group_message_userdata;
240 void (*group_action)(struct Messenger *m, int, int, uint8_t *, uint16_t, void *); 240 void (*group_action)(struct Messenger *m, int, int, const uint8_t *, uint16_t, void *);
241 void *group_action_userdata; 241 void *group_action_userdata;
242 void (*group_namelistchange)(struct Messenger *m, int, int, uint8_t, void *); 242 void (*group_namelistchange)(struct Messenger *m, int, int, uint8_t, void *);
243 void *group_namelistchange_userdata; 243 void *group_namelistchange_userdata;
244 244
245 void (*file_sendrequest)(struct Messenger *m, int32_t, uint8_t, uint64_t, uint8_t *, uint16_t, void *); 245 void (*file_sendrequest)(struct Messenger *m, int32_t, uint8_t, uint64_t, const uint8_t *, uint16_t, void *);
246 void *file_sendrequest_userdata; 246 void *file_sendrequest_userdata;
247 void (*file_filecontrol)(struct Messenger *m, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *, uint16_t, void *); 247 void (*file_filecontrol)(struct Messenger *m, int32_t, uint8_t, uint8_t, uint8_t, const uint8_t *, uint16_t, void *);
248 void *file_filecontrol_userdata; 248 void *file_filecontrol_userdata;
249 void (*file_filedata)(struct Messenger *m, int32_t, uint8_t, uint8_t *, uint16_t length, void *); 249 void (*file_filedata)(struct Messenger *m, int32_t, uint8_t, const uint8_t *, uint16_t length, void *);
250 void *file_filedata_userdata; 250 void *file_filedata_userdata;
251 251
252 void (*msi_packet)(struct Messenger *m, int32_t, uint8_t *, uint16_t, void *); 252 void (*msi_packet)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *);
253 void *msi_packet_userdata; 253 void *msi_packet_userdata;
254 254
255} Messenger; 255} Messenger;
@@ -258,7 +258,7 @@ typedef struct Messenger {
258 * 258 *
259 * return FRIEND_ADDRESS_SIZE byte address to give to others. 259 * return FRIEND_ADDRESS_SIZE byte address to give to others.
260 */ 260 */
261void getaddress(Messenger *m, uint8_t *address); 261void getaddress(const Messenger *m, uint8_t *address);
262 262
263/* Add a friend. 263/* Add a friend.
264 * Set the data that will be sent along with friend request. 264 * Set the data that will be sent along with friend request.
@@ -276,7 +276,7 @@ void getaddress(Messenger *m, uint8_t *address);
276 * (the nospam for that friend was set to the new one). 276 * (the nospam for that friend was set to the new one).
277 * return -8 if increasing the friend list size fails. 277 * return -8 if increasing the friend list size fails.
278 */ 278 */
279int32_t m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length); 279int32_t m_addfriend(Messenger *m, const uint8_t *address, const uint8_t *data, uint16_t length);
280 280
281 281
282/* Add a friend without sending a friendrequest. 282/* Add a friend without sending a friendrequest.
@@ -296,7 +296,7 @@ int32_t getfriend_id(const Messenger *m, const uint8_t *client_id);
296 * return 0 if success 296 * return 0 if success
297 * return -1 if failure 297 * return -1 if failure
298 */ 298 */
299int getclient_id(Messenger *m, int32_t friendnumber, uint8_t *client_id); 299int getclient_id(const Messenger *m, int32_t friendnumber, uint8_t *client_id);
300 300
301/* Remove a friend. 301/* Remove a friend.
302 * 302 *
@@ -311,14 +311,14 @@ int m_delfriend(Messenger *m, int32_t friendnumber);
311 * return 0 if friend is not connected to us (Offline). 311 * return 0 if friend is not connected to us (Offline).
312 * return -1 on failure. 312 * return -1 on failure.
313 */ 313 */
314int m_get_friend_connectionstatus(Messenger *m, int32_t friendnumber); 314int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber);
315 315
316/* Checks if there exists a friend with given friendnumber. 316/* Checks if there exists a friend with given friendnumber.
317 * 317 *
318 * return 1 if friend exists. 318 * return 1 if friend exists.
319 * return 0 if friend doesn't exist. 319 * return 0 if friend doesn't exist.
320 */ 320 */
321int m_friend_exists(Messenger *m, int32_t friendnumber); 321int m_friend_exists(const Messenger *m, int32_t friendnumber);
322 322
323/* Send a text chat message to an online friend. 323/* Send a text chat message to an online friend.
324 * 324 *
@@ -344,8 +344,9 @@ uint32_t m_sendmessage_withid(Messenger *m, int32_t friendnumber, uint32_t theid
344 * m_sendaction_withid will send an action message with the id of your choosing, 344 * m_sendaction_withid will send an action message with the id of your choosing,
345 * however we can generate an id for you by calling plain m_sendaction. 345 * however we can generate an id for you by calling plain m_sendaction.
346 */ 346 */
347uint32_t m_sendaction(Messenger *m, int32_t friendnumber, uint8_t *action, uint32_t length); 347uint32_t m_sendaction(Messenger *m, int32_t friendnumber, const uint8_t *action, uint32_t length);
348uint32_t m_sendaction_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length); 348uint32_t m_sendaction_withid(const Messenger *m, int32_t friendnumber, uint32_t theid, const uint8_t *action,
349 uint32_t length);
349 350
350/* Set the name and name_length of a friend. 351/* Set the name and name_length of a friend.
351 * name must be a string of maximum MAX_NAME_LENGTH length. 352 * name must be a string of maximum MAX_NAME_LENGTH length.
@@ -355,7 +356,7 @@ uint32_t m_sendaction_withid(Messenger *m, int32_t friendnumber, uint32_t theid,
355 * return 0 if success. 356 * return 0 if success.
356 * return -1 if failure. 357 * return -1 if failure.
357 */ 358 */
358int setfriendname(Messenger *m, int32_t friendnumber, uint8_t *name, uint16_t length); 359int setfriendname(Messenger *m, int32_t friendnumber, const uint8_t *name, uint16_t length);
359 360
360/* Set our nickname. 361/* Set our nickname.
361 * name must be a string of maximum MAX_NAME_LENGTH length. 362 * name must be a string of maximum MAX_NAME_LENGTH length.
@@ -375,7 +376,7 @@ int setname(Messenger *m, const uint8_t *name, uint16_t length);
375 * return length of the name. 376 * return length of the name.
376 * return 0 on error. 377 * return 0 on error.
377 */ 378 */
378uint16_t getself_name(Messenger *m, uint8_t *name); 379uint16_t getself_name(const Messenger *m, uint8_t *name);
379 380
380/* Get name of friendnumber and put it in name. 381/* Get name of friendnumber and put it in name.
381 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. 382 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
@@ -383,13 +384,13 @@ uint16_t getself_name(Messenger *m, uint8_t *name);
383 * return length of name if success. 384 * return length of name if success.
384 * return -1 if failure. 385 * return -1 if failure.
385 */ 386 */
386int getname(Messenger *m, int32_t friendnumber, uint8_t *name); 387int getname(const Messenger *m, int32_t friendnumber, uint8_t *name);
387 388
388/* return the length of name, including null on success. 389/* return the length of name, including null on success.
389 * return -1 on failure. 390 * return -1 on failure.
390 */ 391 */
391int m_get_name_size(Messenger *m, int32_t friendnumber); 392int m_get_name_size(const Messenger *m, int32_t friendnumber);
392int m_get_self_name_size(Messenger *m); 393int m_get_self_name_size(const Messenger *m);
393 394
394/* Set our user status. 395/* Set our user status.
395 * You are responsible for freeing status after. 396 * You are responsible for freeing status after.
@@ -403,8 +404,8 @@ int m_set_userstatus(Messenger *m, uint8_t status);
403/* return the length of friendnumber's status message, including null on success. 404/* return the length of friendnumber's status message, including null on success.
404 * return -1 on failure. 405 * return -1 on failure.
405 */ 406 */
406int m_get_statusmessage_size(Messenger *m, int32_t friendnumber); 407int m_get_statusmessage_size(const Messenger *m, int32_t friendnumber);
407int m_get_self_statusmessage_size(Messenger *m); 408int m_get_self_statusmessage_size(const Messenger *m);
408 409
409/* Copy friendnumber's status message into buf, truncating if size is over maxlen. 410/* Copy friendnumber's status message into buf, truncating if size is over maxlen.
410 * Get the size you need to allocate from m_get_statusmessage_size. 411 * Get the size you need to allocate from m_get_statusmessage_size.
@@ -413,21 +414,21 @@ int m_get_self_statusmessage_size(Messenger *m);
413 * returns the length of the copied data on success 414 * returns the length of the copied data on success
414 * retruns -1 on failure. 415 * retruns -1 on failure.
415 */ 416 */
416int m_copy_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen); 417int m_copy_statusmessage(const Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen);
417int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen); 418int m_copy_self_statusmessage(const Messenger *m, uint8_t *buf, uint32_t maxlen);
418 419
419/* return one of USERSTATUS values. 420/* return one of USERSTATUS values.
420 * Values unknown to your application should be represented as USERSTATUS_NONE. 421 * Values unknown to your application should be represented as USERSTATUS_NONE.
421 * As above, the self variant will return our own USERSTATUS. 422 * As above, the self variant will return our own USERSTATUS.
422 * If friendnumber is invalid, this shall return USERSTATUS_INVALID. 423 * If friendnumber is invalid, this shall return USERSTATUS_INVALID.
423 */ 424 */
424uint8_t m_get_userstatus(Messenger *m, int32_t friendnumber); 425uint8_t m_get_userstatus(const Messenger *m, int32_t friendnumber);
425uint8_t m_get_self_userstatus(Messenger *m); 426uint8_t m_get_self_userstatus(const Messenger *m);
426 427
427/* returns timestamp of last time friendnumber was seen online, or 0 if never seen. 428/* returns timestamp of last time friendnumber was seen online, or 0 if never seen.
428 * returns -1 on error. 429 * returns -1 on error.
429 */ 430 */
430uint64_t m_get_last_online(Messenger *m, int32_t friendnumber); 431uint64_t m_get_last_online(const Messenger *m, int32_t friendnumber);
431 432
432/* Set our typing status for a friend. 433/* Set our typing status for a friend.
433 * You are responsible for turning it on or off. 434 * You are responsible for turning it on or off.
@@ -442,7 +443,7 @@ int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing);
442 * returns 0 if friend is not typing. 443 * returns 0 if friend is not typing.
443 * returns 1 if friend is typing. 444 * returns 1 if friend is typing.
444 */ 445 */
445uint8_t m_get_istyping(Messenger *m, int32_t friendnumber); 446uint8_t m_get_istyping(const Messenger *m, int32_t friendnumber);
446 447
447/* Sets whether we send read receipts for friendnumber. 448/* Sets whether we send read receipts for friendnumber.
448 * This function is not lazy, and it will fail if yesno is not (0 or 1). 449 * This function is not lazy, and it will fail if yesno is not (0 or 1).
@@ -458,20 +459,20 @@ void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, const
458/* Set the function that will be executed when a message from a friend is received. 459/* Set the function that will be executed when a message from a friend is received.
459 * Function format is: function(int32_t friendnumber, uint8_t * message, uint32_t length) 460 * Function format is: function(int32_t friendnumber, uint8_t * message, uint32_t length)
460 */ 461 */
461void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), 462void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int32_t, const uint8_t *, uint16_t, void *),
462 void *userdata); 463 void *userdata);
463 464
464/* Set the function that will be executed when an action from a friend is received. 465/* Set the function that will be executed when an action from a friend is received.
465 * Function format is: function(int32_t friendnumber, uint8_t * action, uint32_t length) 466 * Function format is: function(int32_t friendnumber, uint8_t * action, uint32_t length)
466 */ 467 */
467void m_callback_action(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), 468void m_callback_action(Messenger *m, void (*function)(Messenger *m, int32_t, const uint8_t *, uint16_t, void *),
468 void *userdata); 469 void *userdata);
469 470
470/* Set the callback for name changes. 471/* Set the callback for name changes.
471 * Function(int32_t friendnumber, uint8_t *newname, uint16_t length) 472 * Function(int32_t friendnumber, uint8_t *newname, uint16_t length)
472 * You are not responsible for freeing newname. 473 * You are not responsible for freeing newname.
473 */ 474 */
474void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), 475void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int32_t, const uint8_t *, uint16_t, void *),
475 void *userdata); 476 void *userdata);
476 477
477/* Set the callback for status message changes. 478/* Set the callback for status message changes.
@@ -479,7 +480,7 @@ void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int32_t,
479 * 480 *
480 * You are not responsible for freeing newstatus 481 * You are not responsible for freeing newstatus
481 */ 482 */
482void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), 483void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int32_t, const uint8_t *, uint16_t, void *),
483 void *userdata); 484 void *userdata);
484 485
485/* Set the callback for status type changes. 486/* Set the callback for status type changes.
@@ -526,20 +527,21 @@ void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Mess
526 * 527 *
527 * Function(Messenger *m, int32_t friendnumber, uint8_t *group_public_key, void *userdata) 528 * Function(Messenger *m, int32_t friendnumber, uint8_t *group_public_key, void *userdata)
528 */ 529 */
529void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, void *), void *userdata); 530void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int32_t, const uint8_t *, void *),
531 void *userdata);
530 532
531/* Set the callback for group messages. 533/* Set the callback for group messages.
532 * 534 *
533 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) 535 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata)
534 */ 536 */
535void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *), 537void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, int, const uint8_t *, uint16_t, void *),
536 void *userdata); 538 void *userdata);
537 539
538/* Set the callback for group actions. 540/* Set the callback for group actions.
539 * 541 *
540 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) 542 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata)
541 */ 543 */
542void m_callback_group_action(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *), 544void m_callback_group_action(Messenger *m, void (*function)(Messenger *m, int, int, const uint8_t *, uint16_t, void *),
543 void *userdata); 545 void *userdata);
544 546
545/* Set callback function for peer name list changes. 547/* Set callback function for peer name list changes.
@@ -570,7 +572,7 @@ int del_groupchat(Messenger *m, int groupnumber);
570 * return length of name if success 572 * return length of name if success
571 * return -1 if failure 573 * return -1 if failure
572 */ 574 */
573int m_group_peername(Messenger *m, int groupnumber, int peernumber, uint8_t *name); 575int m_group_peername(const Messenger *m, int groupnumber, int peernumber, uint8_t *name);
574 576
575/* invite friendnumber to groupnumber 577/* invite friendnumber to groupnumber
576 * return 0 on success 578 * return 0 on success
@@ -583,24 +585,24 @@ int invite_friend(Messenger *m, int32_t friendnumber, int groupnumber);
583 * returns group number on success 585 * returns group number on success
584 * returns -1 on failure. 586 * returns -1 on failure.
585 */ 587 */
586int join_groupchat(Messenger *m, int32_t friendnumber, uint8_t *friend_group_public_key); 588int join_groupchat(Messenger *m, int32_t friendnumber, const uint8_t *friend_group_public_key);
587 589
588/* send a group message 590/* send a group message
589 * return 0 on success 591 * return 0 on success
590 * return -1 on failure 592 * return -1 on failure
591 */ 593 */
592int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length); 594int group_message_send(const Messenger *m, int groupnumber, const uint8_t *message, uint32_t length);
593 595
594/* send a group action 596/* send a group action
595 * return 0 on success 597 * return 0 on success
596 * return -1 on failure 598 * return -1 on failure
597 */ 599 */
598int group_action_send(Messenger *m, int groupnumber, uint8_t *action, uint32_t length); 600int group_action_send(const Messenger *m, int groupnumber, const uint8_t *action, uint32_t length);
599 601
600/* Return the number of peers in the group chat on success. 602/* Return the number of peers in the group chat on success.
601 * return -1 on failure 603 * return -1 on failure
602 */ 604 */
603int group_number_peers(Messenger *m, int groupnumber); 605int group_number_peers(const Messenger *m, int groupnumber);
604 606
605/* List all the peers in the group chat. 607/* List all the peers in the group chat.
606 * 608 *
@@ -612,7 +614,8 @@ int group_number_peers(Messenger *m, int groupnumber);
612 * 614 *
613 * return -1 on failure. 615 * return -1 on failure.
614 */ 616 */
615int group_names(Messenger *m, int groupnumber, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length); 617int group_names(const Messenger *m, int groupnumber, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[],
618 uint16_t length);
616 619
617/****************FILE SENDING*****************/ 620/****************FILE SENDING*****************/
618 621
@@ -621,7 +624,7 @@ int group_names(Messenger *m, int groupnumber, uint8_t names[][MAX_NICK_BYTES],
621 * 624 *
622 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) 625 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata)
623 */ 626 */
624void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint64_t, uint8_t *, 627void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint64_t, const uint8_t *,
625 uint16_t, void *), void *userdata); 628 uint16_t, void *), void *userdata);
626 629
627/* Set the callback for file control requests. 630/* Set the callback for file control requests.
@@ -629,15 +632,15 @@ void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int3
629 * Function(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) 632 * Function(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
630 * 633 *
631 */ 634 */
632void callback_file_control(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *, 635void callback_file_control(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t, uint8_t,
633 uint16_t, void *), void *userdata); 636 const uint8_t *, uint16_t, void *), void *userdata);
634 637
635/* Set the callback for file data. 638/* Set the callback for file data.
636 * 639 *
637 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) 640 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata)
638 * 641 *
639 */ 642 */
640void callback_file_data(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t *, uint16_t length, 643void callback_file_data(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, const uint8_t *, uint16_t length,
641 void *), void *userdata); 644 void *), void *userdata);
642 645
643/* Send a file send request. 646/* Send a file send request.
@@ -645,15 +648,16 @@ void callback_file_data(Messenger *m, void (*function)(Messenger *m, int32_t, ui
645 * return 1 on success 648 * return 1 on success
646 * return 0 on failure 649 * return 0 on failure
647 */ 650 */
648int file_sendrequest(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, 651int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t filenumber, uint64_t filesize,
649 uint16_t filename_length); 652 const uint8_t *filename, uint16_t filename_length);
650 653
651/* Send a file send request. 654/* Send a file send request.
652 * Maximum filename length is 255 bytes. 655 * Maximum filename length is 255 bytes.
653 * return file number on success 656 * return file number on success
654 * return -1 on failure 657 * return -1 on failure
655 */ 658 */
656int new_filesender(Messenger *m, int32_t friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length); 659int new_filesender(const Messenger *m, int32_t friendnumber, uint64_t filesize, const uint8_t *filename,
660 uint16_t filename_length);
657 661
658/* Send a file control request. 662/* Send a file control request.
659 * send_receive is 0 if we want the control packet to target a sending file, 1 if it targets a receiving file. 663 * send_receive is 0 if we want the control packet to target a sending file, 1 if it targets a receiving file.
@@ -661,15 +665,15 @@ int new_filesender(Messenger *m, int32_t friendnumber, uint64_t filesize, uint8_
661 * return 1 on success 665 * return 1 on success
662 * return 0 on failure 666 * return 0 on failure
663 */ 667 */
664int file_control(Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, 668int file_control(const Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
665 uint8_t *data, uint16_t length); 669 const uint8_t *data, uint16_t length);
666 670
667/* Send file data. 671/* Send file data.
668 * 672 *
669 * return 1 on success 673 * return 1 on success
670 * return 0 on failure 674 * return 0 on failure
671 */ 675 */
672int file_data(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length); 676int file_data(const Messenger *m, int32_t friendnumber, uint8_t filenumber, const uint8_t *data, uint16_t length);
673 677
674/* Give the number of bytes left to be sent/received. 678/* Give the number of bytes left to be sent/received.
675 * 679 *
@@ -678,7 +682,7 @@ int file_data(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t *d
678 * return number of bytes remaining to be sent/received on success 682 * return number of bytes remaining to be sent/received on success
679 * return 0 on failure 683 * return 0 on failure
680 */ 684 */
681uint64_t file_dataremaining(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive); 685uint64_t file_dataremaining(const Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive);
682 686
683/*************** A/V related ******************/ 687/*************** A/V related ******************/
684 688
@@ -686,7 +690,7 @@ uint64_t file_dataremaining(Messenger *m, int32_t friendnumber, uint8_t filenumb
686 * 690 *
687 * Function(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t length, void *userdata) 691 * Function(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t length, void *userdata)
688 */ 692 */
689void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *), 693void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int32_t, const uint8_t *, uint16_t, void *),
690 void *userdata); 694 void *userdata);
691 695
692/* Send an msi packet. 696/* Send an msi packet.
@@ -694,7 +698,7 @@ void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int32_t,
694 * return 1 on success 698 * return 1 on success
695 * return 0 on failure 699 * return 0 on failure
696 */ 700 */
697int m_msi_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t length); 701int m_msi_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length);
698 702
699/**********************************************/ 703/**********************************************/
700 704
@@ -704,14 +708,14 @@ int m_msi_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t len
704 * return 0 on success. 708 * return 0 on success.
705 */ 709 */
706int custom_lossy_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, 710int custom_lossy_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte,
707 int (*packet_handler_callback)(void *object, uint8_t *data, uint32_t len), void *object); 711 int (*packet_handler_callback)(void *object, const uint8_t *data, uint32_t len), void *object);
708 712
709/* High level function to send custom lossy packets. 713/* High level function to send custom lossy packets.
710 * 714 *
711 * return -1 on failure. 715 * return -1 on failure.
712 * return 0 on success. 716 * return 0 on success.
713 */ 717 */
714int send_custom_lossy_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint32_t length); 718int send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length);
715 719
716 720
717/* Set handlers for custom lossless packets. 721/* Set handlers for custom lossless packets.
@@ -722,14 +726,14 @@ int send_custom_lossy_packet(Messenger *m, int32_t friendnumber, uint8_t *data,
722 * return 0 on success. 726 * return 0 on success.
723 */ 727 */
724int custom_lossless_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, 728int custom_lossless_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte,
725 int (*packet_handler_callback)(void *object, uint8_t *data, uint32_t len), void *object); 729 int (*packet_handler_callback)(void *object, const uint8_t *data, uint32_t len), void *object);
726 730
727/* High level function to send custom lossless packets. 731/* High level function to send custom lossless packets.
728 * 732 *
729 * return -1 on failure. 733 * return -1 on failure.
730 * return 0 on success. 734 * return 0 on success.
731 */ 735 */
732int send_custom_lossless_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint32_t length); 736int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length);
733 737
734/**********************************************/ 738/**********************************************/
735/* Run this at startup. 739/* Run this at startup.
@@ -756,28 +760,28 @@ uint32_t messenger_run_interval(Messenger *m);
756/* SAVING AND LOADING FUNCTIONS: */ 760/* SAVING AND LOADING FUNCTIONS: */
757 761
758/* return size of the messenger data (for saving). */ 762/* return size of the messenger data (for saving). */
759uint32_t messenger_size(Messenger *m); 763uint32_t messenger_size(const Messenger *m);
760 764
761/* Save the messenger in data (must be allocated memory of size Messenger_size()) */ 765/* Save the messenger in data (must be allocated memory of size Messenger_size()) */
762void messenger_save(Messenger *m, uint8_t *data); 766void messenger_save(const Messenger *m, uint8_t *data);
763 767
764/* Load the messenger from data of size length. */ 768/* Load the messenger from data of size length. */
765int messenger_load(Messenger *m, uint8_t *data, uint32_t length); 769int messenger_load(Messenger *m, const uint8_t *data, uint32_t length);
766 770
767/* Return the number of friends in the instance m. 771/* Return the number of friends in the instance m.
768 * You should use this to determine how much memory to allocate 772 * You should use this to determine how much memory to allocate
769 * for copy_friendlist. */ 773 * for copy_friendlist. */
770uint32_t count_friendlist(Messenger *m); 774uint32_t count_friendlist(const Messenger *m);
771 775
772/* Return the number of online friends in the instance m. */ 776/* Return the number of online friends in the instance m. */
773uint32_t get_num_online_friends(Messenger *m); 777uint32_t get_num_online_friends(const Messenger *m);
774 778
775/* Copy a list of valid friend IDs into the array out_list. 779/* Copy a list of valid friend IDs into the array out_list.
776 * If out_list is NULL, returns 0. 780 * If out_list is NULL, returns 0.
777 * Otherwise, returns the number of elements copied. 781 * Otherwise, returns the number of elements copied.
778 * If the array was too small, the contents 782 * If the array was too small, the contents
779 * of out_list will be truncated to list_size. */ 783 * of out_list will be truncated to list_size. */
780uint32_t copy_friendlist(Messenger *m, int32_t *out_list, uint32_t list_size); 784uint32_t copy_friendlist(const Messenger *m, int32_t *out_list, uint32_t list_size);
781 785
782/* Allocate and return a list of valid friend id's. List must be freed by the 786/* Allocate and return a list of valid friend id's. List must be freed by the
783 * caller. 787 * caller.
@@ -785,18 +789,18 @@ uint32_t copy_friendlist(Messenger *m, int32_t *out_list, uint32_t list_size);
785 * retun 0 if success. 789 * retun 0 if success.
786 * return -1 if failure. 790 * return -1 if failure.
787 */ 791 */
788int get_friendlist(Messenger *m, int **out_list, uint32_t *out_list_length); 792int get_friendlist(const Messenger *m, int **out_list, uint32_t *out_list_length);
789 793
790/* Return the number of chats in the instance m. 794/* Return the number of chats in the instance m.
791 * You should use this to determine how much memory to allocate 795 * You should use this to determine how much memory to allocate
792 * for copy_chatlist. */ 796 * for copy_chatlist. */
793uint32_t count_chatlist(Messenger *m); 797uint32_t count_chatlist(const Messenger *m);
794 798
795/* Copy a list of valid chat IDs into the array out_list. 799/* Copy a list of valid chat IDs into the array out_list.
796 * If out_list is NULL, returns 0. 800 * If out_list is NULL, returns 0.
797 * Otherwise, returns the number of elements copied. 801 * Otherwise, returns the number of elements copied.
798 * If the array was too small, the contents 802 * If the array was too small, the contents
799 * of out_list will be truncated to list_size. */ 803 * of out_list will be truncated to list_size. */
800uint32_t copy_chatlist(Messenger *m, int *out_list, uint32_t list_size); 804uint32_t copy_chatlist(const Messenger *m, int *out_list, uint32_t list_size);
801 805
802#endif 806#endif
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c
index e4845852..087188f7 100644
--- a/toxcore/TCP_client.c
+++ b/toxcore/TCP_client.c
@@ -65,7 +65,8 @@ static int connect_sock_to(sock_t sock, IP_Port ip_port)
65/* return 0 on success. 65/* return 0 on success.
66 * return -1 on failure. 66 * return -1 on failure.
67 */ 67 */
68static int generate_handshake(TCP_Client_Connection *TCP_conn, uint8_t *self_public_key, uint8_t *self_secret_key) 68static int generate_handshake(TCP_Client_Connection *TCP_conn, const uint8_t *self_public_key,
69 const uint8_t *self_secret_key)
69{ 70{
70 uint8_t plain[crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES]; 71 uint8_t plain[crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES];
71 crypto_box_keypair(plain, TCP_conn->temp_secret_key); 72 crypto_box_keypair(plain, TCP_conn->temp_secret_key);
@@ -90,7 +91,7 @@ static int generate_handshake(TCP_Client_Connection *TCP_conn, uint8_t *self_pub
90 * return 0 on success. 91 * return 0 on success.
91 * return -1 on failure. 92 * return -1 on failure.
92 */ 93 */
93static int handle_handshake(TCP_Client_Connection *TCP_conn, uint8_t *data) 94static int handle_handshake(TCP_Client_Connection *TCP_conn, const uint8_t *data)
94{ 95{
95 uint8_t plain[crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES]; 96 uint8_t plain[crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES];
96 int len = decrypt_data_symmetric(TCP_conn->shared_key, data, data + crypto_box_NONCEBYTES, 97 int len = decrypt_data_symmetric(TCP_conn->shared_key, data, data + crypto_box_NONCEBYTES,
@@ -137,7 +138,7 @@ static int send_pending_data(TCP_Client_Connection *con)
137 * return 0 if could not send packet. 138 * return 0 if could not send packet.
138 * return -1 on failure (connection must be killed). 139 * return -1 on failure (connection must be killed).
139 */ 140 */
140static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, uint8_t *data, uint16_t length) 141static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const uint8_t *data, uint16_t length)
141{ 142{
142 if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE) 143 if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE)
143 return -1; 144 return -1;
@@ -183,7 +184,7 @@ int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key)
183} 184}
184 185
185void routing_response_handler(TCP_Client_Connection *con, int (*response_callback)(void *object, uint8_t connection_id, 186void routing_response_handler(TCP_Client_Connection *con, int (*response_callback)(void *object, uint8_t connection_id,
186 uint8_t *public_key), void *object) 187 const uint8_t *public_key), void *object)
187{ 188{
188 con->response_callback = response_callback; 189 con->response_callback = response_callback;
189 con->response_callback_object = object; 190 con->response_callback_object = object;
@@ -200,7 +201,7 @@ void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(v
200 * return 0 if could not send packet. 201 * return 0 if could not send packet.
201 * return -1 on failure. 202 * return -1 on failure.
202 */ 203 */
203int send_data(TCP_Client_Connection *con, uint8_t con_id, uint8_t *data, uint16_t length) 204int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, uint16_t length)
204{ 205{
205 if (con_id >= NUM_CLIENT_CONNECTIONS) 206 if (con_id >= NUM_CLIENT_CONNECTIONS)
206 return -1; 207 return -1;
@@ -218,7 +219,7 @@ int send_data(TCP_Client_Connection *con, uint8_t con_id, uint8_t *data, uint16_
218 * return 0 if could not send packet. 219 * return 0 if could not send packet.
219 * return -1 on failure. 220 * return -1 on failure.
220 */ 221 */
221int send_oob_packet(TCP_Client_Connection *con, uint8_t *public_key, uint8_t *data, uint16_t length) 222int send_oob_packet(TCP_Client_Connection *con, const uint8_t *public_key, const uint8_t *data, uint16_t length)
222{ 223{
223 if (length == 0 || length > TCP_MAX_OOB_DATA_LENGTH) 224 if (length == 0 || length > TCP_MAX_OOB_DATA_LENGTH)
224 return -1; 225 return -1;
@@ -251,14 +252,14 @@ int set_tcp_connection_number(TCP_Client_Connection *con, uint8_t con_id, uint32
251} 252}
252 253
253void routing_data_handler(TCP_Client_Connection *con, int (*data_callback)(void *object, uint32_t number, 254void routing_data_handler(TCP_Client_Connection *con, int (*data_callback)(void *object, uint32_t number,
254 uint8_t connection_id, uint8_t *data, uint16_t length), void *object) 255 uint8_t connection_id, const uint8_t *data, uint16_t length), void *object)
255{ 256{
256 con->data_callback = data_callback; 257 con->data_callback = data_callback;
257 con->data_callback_object = object; 258 con->data_callback_object = object;
258} 259}
259 260
260void oob_data_handler(TCP_Client_Connection *con, int (*oob_data_callback)(void *object, uint8_t *public_key, 261void oob_data_handler(TCP_Client_Connection *con, int (*oob_data_callback)(void *object, const uint8_t *public_key,
261 uint8_t *data, uint16_t length), void *object) 262 const uint8_t *data, uint16_t length), void *object)
262{ 263{
263 con->oob_data_callback = oob_data_callback; 264 con->oob_data_callback = oob_data_callback;
264 con->oob_data_callback_object = object; 265 con->oob_data_callback_object = object;
@@ -318,7 +319,7 @@ int send_disconnect_request(TCP_Client_Connection *con, uint8_t con_id)
318 * return 0 if could not send packet. 319 * return 0 if could not send packet.
319 * return -1 on failure (connection must be killed). 320 * return -1 on failure (connection must be killed).
320 */ 321 */
321int send_onion_request(TCP_Client_Connection *con, uint8_t *data, uint16_t length) 322int send_onion_request(TCP_Client_Connection *con, const uint8_t *data, uint16_t length)
322{ 323{
323 uint8_t packet[1 + length]; 324 uint8_t packet[1 + length];
324 packet[0] = TCP_PACKET_ONION_REQUEST; 325 packet[0] = TCP_PACKET_ONION_REQUEST;
@@ -326,7 +327,7 @@ int send_onion_request(TCP_Client_Connection *con, uint8_t *data, uint16_t lengt
326 return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); 327 return write_packet_TCP_secure_connection(con, packet, sizeof(packet));
327} 328}
328 329
329void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, uint8_t *data, 330void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, const uint8_t *data,
330 uint16_t length), void *object) 331 uint16_t length), void *object)
331{ 332{
332 con->onion_callback = onion_callback; 333 con->onion_callback = onion_callback;
@@ -335,8 +336,8 @@ void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(vo
335 336
336/* Create new TCP connection to ip_port/public_key 337/* Create new TCP connection to ip_port/public_key
337 */ 338 */
338TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, uint8_t *public_key, uint8_t *self_public_key, 339TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public_key, const uint8_t *self_public_key,
339 uint8_t *self_secret_key) 340 const uint8_t *self_secret_key)
340{ 341{
341 if (networking_at_startup() != 0) { 342 if (networking_at_startup() != 0) {
342 return NULL; 343 return NULL;
@@ -388,7 +389,7 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, uint8_t *public_key,
388/* return 0 on success 389/* return 0 on success
389 * return -1 on failure 390 * return -1 on failure
390 */ 391 */
391static int handle_TCP_packet(TCP_Client_Connection *conn, uint8_t *data, uint16_t length) 392static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, uint16_t length)
392{ 393{
393 if (length <= 1) 394 if (length <= 1)
394 return -1; 395 return -1;
diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h
index afb95392..2622b4f7 100644
--- a/toxcore/TCP_client.h
+++ b/toxcore/TCP_client.h
@@ -64,23 +64,23 @@ typedef struct {
64 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 64 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
65 uint32_t number; 65 uint32_t number;
66 } connections[NUM_CLIENT_CONNECTIONS]; 66 } connections[NUM_CLIENT_CONNECTIONS];
67 int (*response_callback)(void *object, uint8_t connection_id, uint8_t *public_key); 67 int (*response_callback)(void *object, uint8_t connection_id, const uint8_t *public_key);
68 void *response_callback_object; 68 void *response_callback_object;
69 int (*status_callback)(void *object, uint32_t number, uint8_t connection_id, uint8_t status); 69 int (*status_callback)(void *object, uint32_t number, uint8_t connection_id, uint8_t status);
70 void *status_callback_object; 70 void *status_callback_object;
71 int (*data_callback)(void *object, uint32_t number, uint8_t connection_id, uint8_t *data, uint16_t length); 71 int (*data_callback)(void *object, uint32_t number, uint8_t connection_id, const uint8_t *data, uint16_t length);
72 void *data_callback_object; 72 void *data_callback_object;
73 int (*oob_data_callback)(void *object, uint8_t *public_key, uint8_t *data, uint16_t length); 73 int (*oob_data_callback)(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length);
74 void *oob_data_callback_object; 74 void *oob_data_callback_object;
75 75
76 int (*onion_callback)(void *object, uint8_t *data, uint16_t length); 76 int (*onion_callback)(void *object, const uint8_t *data, uint16_t length);
77 void *onion_callback_object; 77 void *onion_callback_object;
78} TCP_Client_Connection; 78} TCP_Client_Connection;
79 79
80/* Create new TCP connection to ip_port/public_key 80/* Create new TCP connection to ip_port/public_key
81 */ 81 */
82TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, uint8_t *public_key, uint8_t *self_public_key, 82TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public_key, const uint8_t *self_public_key,
83 uint8_t *self_secret_key); 83 const uint8_t *self_secret_key);
84 84
85/* Run the TCP connection 85/* Run the TCP connection
86 */ 86 */
@@ -94,8 +94,8 @@ void kill_TCP_connection(TCP_Client_Connection *TCP_connection);
94 * return 0 if could not send packet. 94 * return 0 if could not send packet.
95 * return -1 on failure (connection must be killed). 95 * return -1 on failure (connection must be killed).
96 */ 96 */
97int send_onion_request(TCP_Client_Connection *con, uint8_t *data, uint16_t length); 97int send_onion_request(TCP_Client_Connection *con, const uint8_t *data, uint16_t length);
98void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, uint8_t *data, 98void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, const uint8_t *data,
99 uint16_t length), void *object); 99 uint16_t length), void *object);
100 100
101/* return 1 on success. 101/* return 1 on success.
@@ -104,7 +104,7 @@ void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(vo
104 */ 104 */
105int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key); 105int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key);
106void routing_response_handler(TCP_Client_Connection *con, int (*response_callback)(void *object, uint8_t connection_id, 106void routing_response_handler(TCP_Client_Connection *con, int (*response_callback)(void *object, uint8_t connection_id,
107 uint8_t *public_key), void *object); 107 const uint8_t *public_key), void *object);
108void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(void *object, uint32_t number, 108void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(void *object, uint32_t number,
109 uint8_t connection_id, uint8_t status), void *object); 109 uint8_t connection_id, uint8_t status), void *object);
110 110
@@ -127,17 +127,17 @@ int set_tcp_connection_number(TCP_Client_Connection *con, uint8_t con_id, uint32
127 * return 0 if could not send packet. 127 * return 0 if could not send packet.
128 * return -1 on failure. 128 * return -1 on failure.
129 */ 129 */
130int send_data(TCP_Client_Connection *con, uint8_t con_id, uint8_t *data, uint16_t length); 130int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, uint16_t length);
131void routing_data_handler(TCP_Client_Connection *con, int (*data_callback)(void *object, uint32_t number, 131void routing_data_handler(TCP_Client_Connection *con, int (*data_callback)(void *object, uint32_t number,
132 uint8_t connection_id, uint8_t *data, uint16_t length), void *object); 132 uint8_t connection_id, const uint8_t *data, uint16_t length), void *object);
133 133
134/* return 1 on success. 134/* return 1 on success.
135 * return 0 if could not send packet. 135 * return 0 if could not send packet.
136 * return -1 on failure. 136 * return -1 on failure.
137 */ 137 */
138int send_oob_packet(TCP_Client_Connection *con, uint8_t *public_key, uint8_t *data, uint16_t length); 138int send_oob_packet(TCP_Client_Connection *con, const uint8_t *public_key, const uint8_t *data, uint16_t length);
139void oob_data_handler(TCP_Client_Connection *con, int (*oob_data_callback)(void *object, uint8_t *public_key, 139void oob_data_handler(TCP_Client_Connection *con, int (*oob_data_callback)(void *object, const uint8_t *public_key,
140 uint8_t *data, uint16_t length), void *object); 140 const uint8_t *data, uint16_t length), void *object);
141 141
142 142
143#endif 143#endif
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index 578784d0..7487ad4f 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -97,7 +97,7 @@ static int realloc_connection(TCP_Server *TCP_server, uint32_t num)
97/* return index corresponding to connection with peer on success 97/* return index corresponding to connection with peer on success
98 * return -1 on failure. 98 * return -1 on failure.
99 */ 99 */
100static int get_TCP_connection_index(TCP_Server *TCP_server, uint8_t *public_key) 100static int get_TCP_connection_index(const TCP_Server *TCP_server, const uint8_t *public_key)
101{ 101{
102 return bs_list_find(&TCP_server->accepted_key_list, public_key); 102 return bs_list_find(&TCP_server->accepted_key_list, public_key);
103} 103}
@@ -110,7 +110,7 @@ static int kill_accepted(TCP_Server *TCP_server, int index);
110 * return index on success 110 * return index on success
111 * return -1 on failure 111 * return -1 on failure
112 */ 112 */
113static int add_accepted(TCP_Server *TCP_server, TCP_Secure_Connection *con) 113static int add_accepted(TCP_Server *TCP_server, const TCP_Secure_Connection *con)
114{ 114{
115 int index = get_TCP_connection_index(TCP_server, con->public_key); 115 int index = get_TCP_connection_index(TCP_server, con->public_key);
116 116
@@ -249,7 +249,7 @@ int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length)
249 * return 0 if could not read any packet. 249 * return 0 if could not read any packet.
250 * return -1 on failure (connection must be killed). 250 * return -1 on failure (connection must be killed).
251 */ 251 */
252int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length, uint8_t *shared_key, 252int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length, const uint8_t *shared_key,
253 uint8_t *recv_nonce, uint8_t *data, uint16_t max_len) 253 uint8_t *recv_nonce, uint8_t *data, uint16_t max_len)
254{ 254{
255 if (*next_packet_length == 0) { 255 if (*next_packet_length == 0) {
@@ -318,7 +318,7 @@ static int send_pending_data(TCP_Secure_Connection *con)
318 * return 0 if could not send packet. 318 * return 0 if could not send packet.
319 * return -1 on failure (connection must be killed). 319 * return -1 on failure (connection must be killed).
320 */ 320 */
321static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, uint8_t *data, uint16_t length) 321static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length)
322{ 322{
323 if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE) 323 if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE)
324 return -1; 324 return -1;
@@ -389,7 +389,8 @@ static int kill_accepted(TCP_Server *TCP_server, int index)
389/* return 1 if everything went well. 389/* return 1 if everything went well.
390 * return -1 if the connection must be killed. 390 * return -1 if the connection must be killed.
391 */ 391 */
392static int handle_TCP_handshake(TCP_Secure_Connection *con, uint8_t *data, uint16_t length, uint8_t *self_secret_key) 392static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length,
393 const uint8_t *self_secret_key)
393{ 394{
394 if (length != TCP_CLIENT_HANDSHAKE_SIZE) 395 if (length != TCP_CLIENT_HANDSHAKE_SIZE)
395 return -1; 396 return -1;
@@ -435,7 +436,7 @@ static int handle_TCP_handshake(TCP_Secure_Connection *con, uint8_t *data, uint1
435 * return 0 if we didn't get it yet. 436 * return 0 if we didn't get it yet.
436 * return -1 if the connection must be killed. 437 * return -1 if the connection must be killed.
437 */ 438 */
438static int read_connection_handshake(TCP_Secure_Connection *con, uint8_t *self_secret_key) 439static int read_connection_handshake(TCP_Secure_Connection *con, const uint8_t *self_secret_key)
439{ 440{
440 uint8_t data[TCP_CLIENT_HANDSHAKE_SIZE]; 441 uint8_t data[TCP_CLIENT_HANDSHAKE_SIZE];
441 int len = 0; 442 int len = 0;
@@ -451,7 +452,7 @@ static int read_connection_handshake(TCP_Secure_Connection *con, uint8_t *self_s
451 * return 0 if could not send packet. 452 * return 0 if could not send packet.
452 * return -1 on failure (connection must be killed). 453 * return -1 on failure (connection must be killed).
453 */ 454 */
454static int send_routing_response(TCP_Secure_Connection *con, uint8_t rpid, uint8_t *public_key) 455static int send_routing_response(TCP_Secure_Connection *con, uint8_t rpid, const uint8_t *public_key)
455{ 456{
456 uint8_t data[1 + 1 + crypto_box_PUBLICKEYBYTES]; 457 uint8_t data[1 + 1 + crypto_box_PUBLICKEYBYTES];
457 data[0] = TCP_PACKET_ROUTING_RESPONSE; 458 data[0] = TCP_PACKET_ROUTING_RESPONSE;
@@ -484,7 +485,7 @@ static int send_disconnect_notification(TCP_Secure_Connection *con, uint8_t id)
484/* return 0 on success. 485/* return 0 on success.
485 * return -1 on failure (connection must be killed). 486 * return -1 on failure (connection must be killed).
486 */ 487 */
487static int handle_TCP_routing_req(TCP_Server *TCP_server, uint32_t con_id, uint8_t *public_key) 488static int handle_TCP_routing_req(TCP_Server *TCP_server, uint32_t con_id, const uint8_t *public_key)
488{ 489{
489 uint32_t i; 490 uint32_t i;
490 uint32_t index = ~0; 491 uint32_t index = ~0;
@@ -562,7 +563,7 @@ static int handle_TCP_routing_req(TCP_Server *TCP_server, uint32_t con_id, uint8
562/* return 0 on success. 563/* return 0 on success.
563 * return -1 on failure (connection must be killed). 564 * return -1 on failure (connection must be killed).
564 */ 565 */
565static int handle_TCP_oob_send(TCP_Server *TCP_server, uint32_t con_id, uint8_t *public_key, uint8_t *data, 566static int handle_TCP_oob_send(TCP_Server *TCP_server, uint32_t con_id, const uint8_t *public_key, const uint8_t *data,
566 uint16_t length) 567 uint16_t length)
567{ 568{
568 if (length == 0 || length > TCP_MAX_OOB_DATA_LENGTH) 569 if (length == 0 || length > TCP_MAX_OOB_DATA_LENGTH)
@@ -645,7 +646,7 @@ static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data,
645/* return 0 on success 646/* return 0 on success
646 * return -1 on failure 647 * return -1 on failure
647 */ 648 */
648static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, uint8_t *data, uint16_t length) 649static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint8_t *data, uint16_t length)
649{ 650{
650 if (length == 0) 651 if (length == 0)
651 return -1; 652 return -1;
@@ -764,7 +765,8 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, uint8_t *d
764} 765}
765 766
766 767
767static int confirm_TCP_connection(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t *data, uint16_t length) 768static int confirm_TCP_connection(TCP_Server *TCP_server, TCP_Secure_Connection *con, const uint8_t *data,
769 uint16_t length)
768{ 770{
769 int index = add_accepted(TCP_server, con); 771 int index = add_accepted(TCP_server, con);
770 772
@@ -838,8 +840,8 @@ static sock_t new_listening_TCP_socket(int family, uint16_t port)
838 return sock; 840 return sock;
839} 841}
840 842
841TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, uint16_t *ports, uint8_t *public_key, 843TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *public_key,
842 uint8_t *secret_key, Onion *onion) 844 const uint8_t *secret_key, Onion *onion)
843{ 845{
844 if (num_sockets == 0 || ports == NULL) 846 if (num_sockets == 0 || ports == NULL)
845 return NULL; 847 return NULL;
diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h
index 40984778..def0a978 100644
--- a/toxcore/TCP_server.h
+++ b/toxcore/TCP_server.h
@@ -133,8 +133,8 @@ typedef struct {
133 133
134/* Create new TCP server instance. 134/* Create new TCP server instance.
135 */ 135 */
136TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, uint16_t *ports, uint8_t *public_key, 136TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *public_key,
137 uint8_t *secret_key, Onion *onion); 137 const uint8_t *secret_key, Onion *onion);
138 138
139/* Run the TCP_server 139/* Run the TCP_server
140 */ 140 */
@@ -164,7 +164,7 @@ int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length);
164 * return 0 if could not read any packet. 164 * return 0 if could not read any packet.
165 * return -1 on failure (connection must be killed). 165 * return -1 on failure (connection must be killed).
166 */ 166 */
167int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length, uint8_t *shared_key, 167int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length, const uint8_t *shared_key,
168 uint8_t *recv_nonce, uint8_t *data, uint16_t max_len); 168 uint8_t *recv_nonce, uint8_t *data, uint16_t max_len);
169 169
170 170
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 8d598469..e0319f34 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -32,7 +32,7 @@
32#include "util.h" 32#include "util.h"
33#include "math.h" 33#include "math.h"
34 34
35static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id) 35static uint8_t crypt_connection_id_not_valid(const Net_Crypto *c, int crypt_connection_id)
36{ 36{
37 return (uint32_t)crypt_connection_id >= c->crypto_connections_length; 37 return (uint32_t)crypt_connection_id >= c->crypto_connections_length;
38} 38}
@@ -70,7 +70,7 @@ static int is_alive(uint8_t status)
70 * return -1 on failure. 70 * return -1 on failure.
71 * return COOKIE_REQUEST_LENGTH on success. 71 * return COOKIE_REQUEST_LENGTH on success.
72 */ 72 */
73static int create_cookie_request(Net_Crypto *c, uint8_t *packet, uint8_t *dht_public_key, uint64_t number, 73static int create_cookie_request(const Net_Crypto *c, uint8_t *packet, uint8_t *dht_public_key, uint64_t number,
74 uint8_t *shared_key) 74 uint8_t *shared_key)
75{ 75{
76 uint8_t plain[COOKIE_REQUEST_PLAIN_LENGTH]; 76 uint8_t plain[COOKIE_REQUEST_PLAIN_LENGTH];
@@ -100,7 +100,7 @@ static int create_cookie_request(Net_Crypto *c, uint8_t *packet, uint8_t *dht_pu
100 * return -1 on failure. 100 * return -1 on failure.
101 * return 0 on success. 101 * return 0 on success.
102 */ 102 */
103static int create_cookie(uint8_t *cookie, uint8_t *bytes, uint8_t *encryption_key) 103static int create_cookie(uint8_t *cookie, const uint8_t *bytes, const uint8_t *encryption_key)
104{ 104{
105 uint8_t contents[COOKIE_CONTENTS_LENGTH]; 105 uint8_t contents[COOKIE_CONTENTS_LENGTH];
106 uint64_t temp_time = unix_time(); 106 uint64_t temp_time = unix_time();
@@ -148,8 +148,8 @@ static int open_cookie(uint8_t *bytes, const uint8_t *cookie, const uint8_t *enc
148 * return -1 on failure. 148 * return -1 on failure.
149 * return COOKIE_RESPONSE_LENGTH on success. 149 * return COOKIE_RESPONSE_LENGTH on success.
150 */ 150 */
151static int create_cookie_response(Net_Crypto *c, uint8_t *packet, uint8_t *request_plain, uint8_t *shared_key, 151static int create_cookie_response(const Net_Crypto *c, uint8_t *packet, const uint8_t *request_plain,
152 uint8_t *dht_public_key) 152 const uint8_t *shared_key, const uint8_t *dht_public_key)
153{ 153{
154 uint8_t cookie_plain[COOKIE_DATA_LENGTH]; 154 uint8_t cookie_plain[COOKIE_DATA_LENGTH];
155 memcpy(cookie_plain, request_plain, crypto_box_PUBLICKEYBYTES); 155 memcpy(cookie_plain, request_plain, crypto_box_PUBLICKEYBYTES);
@@ -177,8 +177,8 @@ static int create_cookie_response(Net_Crypto *c, uint8_t *packet, uint8_t *reque
177 * return -1 on failure. 177 * return -1 on failure.
178 * return 0 on success. 178 * return 0 on success.
179 */ 179 */
180static int handle_cookie_request(Net_Crypto *c, uint8_t *request_plain, uint8_t *shared_key, uint8_t *dht_public_key, 180static int handle_cookie_request(const Net_Crypto *c, uint8_t *request_plain, uint8_t *shared_key,
181 const uint8_t *packet, uint16_t length) 181 uint8_t *dht_public_key, const uint8_t *packet, uint16_t length)
182{ 182{
183 if (length != COOKIE_REQUEST_LENGTH) 183 if (length != COOKIE_REQUEST_LENGTH)
184 return -1; 184 return -1;
@@ -220,8 +220,8 @@ static int udp_handle_cookie_request(void *object, IP_Port source, const uint8_t
220 220
221/* Handle the cookie request packet (for TCP) 221/* Handle the cookie request packet (for TCP)
222 */ 222 */
223static int tcp_handle_cookie_request(Net_Crypto *c, TCP_Client_Connection *TCP_con, uint8_t conn_id, uint8_t *packet, 223static int tcp_handle_cookie_request(const Net_Crypto *c, TCP_Client_Connection *TCP_con, uint8_t conn_id,
224 uint32_t length) 224 const uint8_t *packet, uint32_t length)
225{ 225{
226 uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH]; 226 uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH];
227 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 227 uint8_t shared_key[crypto_box_BEFORENMBYTES];
@@ -243,8 +243,8 @@ static int tcp_handle_cookie_request(Net_Crypto *c, TCP_Client_Connection *TCP_c
243 243
244/* Handle the cookie request packet (for TCP oob packets) 244/* Handle the cookie request packet (for TCP oob packets)
245 */ 245 */
246static int tcp_oob_handle_cookie_request(Net_Crypto *c, TCP_Client_Connection *TCP_con, uint8_t *dht_public_key, 246static int tcp_oob_handle_cookie_request(const Net_Crypto *c, TCP_Client_Connection *TCP_con,
247 uint8_t *packet, uint32_t length) 247 const uint8_t *dht_public_key, const uint8_t *packet, uint32_t length)
248{ 248{
249 uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH]; 249 uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH];
250 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 250 uint8_t shared_key[crypto_box_BEFORENMBYTES];
@@ -302,8 +302,8 @@ static int handle_cookie_response(uint8_t *cookie, uint64_t *number, const uint8
302 * return -1 on failure. 302 * return -1 on failure.
303 * return HANDSHAKE_PACKET_LENGTH on success. 303 * return HANDSHAKE_PACKET_LENGTH on success.
304 */ 304 */
305static int create_crypto_handshake(Net_Crypto *c, uint8_t *packet, uint8_t *cookie, uint8_t *nonce, uint8_t *session_pk, 305static int create_crypto_handshake(const Net_Crypto *c, uint8_t *packet, const uint8_t *cookie, const uint8_t *nonce,
306 uint8_t *peer_real_pk, uint8_t *peer_dht_pubkey) 306 const uint8_t *session_pk, const uint8_t *peer_real_pk, const uint8_t *peer_dht_pubkey)
307{ 307{
308 uint8_t plain[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_hash_sha512_BYTES + COOKIE_LENGTH]; 308 uint8_t plain[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_hash_sha512_BYTES + COOKIE_LENGTH];
309 memcpy(plain, nonce, crypto_box_NONCEBYTES); 309 memcpy(plain, nonce, crypto_box_NONCEBYTES);
@@ -348,8 +348,8 @@ static int create_crypto_handshake(Net_Crypto *c, uint8_t *packet, uint8_t *cook
348 * return -1 on failure. 348 * return -1 on failure.
349 * return 0 on success. 349 * return 0 on success.
350 */ 350 */
351static int handle_crypto_handshake(Net_Crypto *c, uint8_t *nonce, uint8_t *session_pk, uint8_t *peer_real_pk, 351static int handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t *session_pk, uint8_t *peer_real_pk,
352 uint8_t *dht_public_key, uint8_t *cookie, const uint8_t *packet, uint32_t length, uint8_t *expected_real_pk) 352 uint8_t *dht_public_key, uint8_t *cookie, const uint8_t *packet, uint32_t length, const uint8_t *expected_real_pk)
353{ 353{
354 if (length != HANDSHAKE_PACKET_LENGTH) 354 if (length != HANDSHAKE_PACKET_LENGTH)
355 return -1; 355 return -1;
@@ -386,7 +386,7 @@ static int handle_crypto_handshake(Net_Crypto *c, uint8_t *nonce, uint8_t *sessi
386} 386}
387 387
388 388
389static Crypto_Connection *get_crypto_connection(Net_Crypto *c, int crypt_connection_id) 389static Crypto_Connection *get_crypto_connection(const Net_Crypto *c, int crypt_connection_id)
390{ 390{
391 if (crypt_connection_id_not_valid(c, crypt_connection_id)) 391 if (crypt_connection_id_not_valid(c, crypt_connection_id))
392 return 0; 392 return 0;
@@ -400,7 +400,7 @@ static Crypto_Connection *get_crypto_connection(Net_Crypto *c, int crypt_connect
400 * return -1 on failure. 400 * return -1 on failure.
401 * return 0 on success. 401 * return 0 on success.
402 */ 402 */
403static int send_packet_to(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint16_t length) 403static int send_packet_to(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length)
404{ 404{
405//TODO TCP, etc... 405//TODO TCP, etc...
406 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 406 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
@@ -456,7 +456,7 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, uint8_t *data,
456/* Return number of packets in array 456/* Return number of packets in array
457 * Note that holes are counted too. 457 * Note that holes are counted too.
458 */ 458 */
459static uint32_t num_packets_array(Packets_Array *array) 459static uint32_t num_packets_array(const Packets_Array *array)
460{ 460{
461 return array->buffer_end - array->buffer_start; 461 return array->buffer_end - array->buffer_start;
462} 462}
@@ -466,7 +466,7 @@ static uint32_t num_packets_array(Packets_Array *array)
466 * return -1 on failure. 466 * return -1 on failure.
467 * return 0 on success. 467 * return 0 on success.
468 */ 468 */
469static int add_data_to_buffer(Packets_Array *array, uint32_t number, Packet_Data *data) 469static int add_data_to_buffer(Packets_Array *array, uint32_t number, const Packet_Data *data)
470{ 470{
471 if (number - array->buffer_start > CRYPTO_PACKET_BUFFER_SIZE) 471 if (number - array->buffer_start > CRYPTO_PACKET_BUFFER_SIZE)
472 return -1; 472 return -1;
@@ -496,7 +496,7 @@ static int add_data_to_buffer(Packets_Array *array, uint32_t number, Packet_Data
496 * return 0 if data at number is empty. 496 * return 0 if data at number is empty.
497 * return 1 if data pointer was put in data. 497 * return 1 if data pointer was put in data.
498 */ 498 */
499static int get_data_pointer(Packets_Array *array, Packet_Data **data, uint32_t number) 499static int get_data_pointer(const Packets_Array *array, Packet_Data **data, uint32_t number)
500{ 500{
501 uint32_t num_spots = array->buffer_end - array->buffer_start; 501 uint32_t num_spots = array->buffer_end - array->buffer_start;
502 502
@@ -517,7 +517,7 @@ static int get_data_pointer(Packets_Array *array, Packet_Data **data, uint32_t n
517 * return -1 on failure. 517 * return -1 on failure.
518 * return packet number on success. 518 * return packet number on success.
519 */ 519 */
520static int64_t add_data_end_of_buffer(Packets_Array *array, Packet_Data *data) 520static int64_t add_data_end_of_buffer(Packets_Array *array, const Packet_Data *data)
521{ 521{
522 if (num_packets_array(array) >= CRYPTO_PACKET_BUFFER_SIZE) 522 if (num_packets_array(array) >= CRYPTO_PACKET_BUFFER_SIZE)
523 return -1; 523 return -1;
@@ -607,7 +607,7 @@ static int set_buffer_end(Packets_Array *array, uint32_t number)
607 * return -1 on failure. 607 * return -1 on failure.
608 * return length of packet on success. 608 * return length of packet on success.
609 */ 609 */
610static int generate_request_packet(uint8_t *data, uint16_t length, Packets_Array *recv_array) 610static int generate_request_packet(uint8_t *data, uint16_t length, const Packets_Array *recv_array)
611{ 611{
612 if (length == 0) 612 if (length == 0)
613 return -1; 613 return -1;
@@ -656,7 +656,7 @@ static int generate_request_packet(uint8_t *data, uint16_t length, Packets_Array
656 * return -1 on failure. 656 * return -1 on failure.
657 * return number of requested packets on success. 657 * return number of requested packets on success.
658 */ 658 */
659static int handle_request_packet(Packets_Array *send_array, uint8_t *data, uint16_t length) 659static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, uint16_t length)
660{ 660{
661 if (length < 1) 661 if (length < 1)
662 return -1; 662 return -1;
@@ -718,7 +718,7 @@ static int handle_request_packet(Packets_Array *send_array, uint8_t *data, uint1
718 * return -1 on failure. 718 * return -1 on failure.
719 * return 0 on success. 719 * return 0 on success.
720 */ 720 */
721static int send_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint16_t length) 721static int send_data_packet(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length)
722{ 722{
723 if (length == 0 || length + (1 + sizeof(uint16_t) + crypto_box_MACBYTES) > MAX_CRYPTO_PACKET_SIZE) 723 if (length == 0 || length + (1 + sizeof(uint16_t) + crypto_box_MACBYTES) > MAX_CRYPTO_PACKET_SIZE)
724 return -1; 724 return -1;
@@ -750,8 +750,8 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *dat
750 * return -1 on failure. 750 * return -1 on failure.
751 * return 0 on success. 751 * return 0 on success.
752 */ 752 */
753static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint32_t buffer_start, uint32_t num, 753static int send_data_packet_helper(const Net_Crypto *c, int crypt_connection_id, uint32_t buffer_start, uint32_t num,
754 uint8_t *data, uint32_t length) 754 const uint8_t *data, uint32_t length)
755{ 755{
756 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) 756 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE)
757 return -1; 757 return -1;
@@ -771,7 +771,7 @@ static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint3
771/* return -1 if data could not be put in packet queue. 771/* return -1 if data could not be put in packet queue.
772 * return positive packet number if data was put into the queue. 772 * return positive packet number if data was put into the queue.
773 */ 773 */
774static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) 774static int64_t send_lossless_packet(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length)
775{ 775{
776 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) 776 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE)
777 return -1; 777 return -1;
@@ -781,8 +781,29 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, uint
781 if (conn == 0) 781 if (conn == 0)
782 return -1; 782 return -1;
783 783
784 uint64_t temp_time = current_time_monotonic();
785
786 /* If last packet send failed, try to send packet again.
787 If sending it fails we won't be able to send the new packet. */
788 if (conn->maximum_speed_reached) {
789 Packet_Data *dt = NULL;
790 uint32_t packet_num = conn->send_array.buffer_end - 1;
791 int ret = get_data_pointer(&conn->send_array, &dt, packet_num);
792
793 if (ret == 1) {
794 if (!dt->time) {
795 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data,
796 dt->length) != 0) {
797 return -1;
798 }
799
800 dt->time = temp_time;
801 }
802 }
803 }
804
784 Packet_Data dt; 805 Packet_Data dt;
785 dt.time = current_time_monotonic(); 806 dt.time = temp_time;
786 dt.length = length; 807 dt.length = length;
787 memcpy(dt.data, data, length); 808 memcpy(dt.data, data, length);
788 int64_t packet_num = add_data_end_of_buffer(&conn->send_array, &dt); 809 int64_t packet_num = add_data_end_of_buffer(&conn->send_array, &dt);
@@ -790,8 +811,15 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, uint
790 if (packet_num == -1) 811 if (packet_num == -1)
791 return -1; 812 return -1;
792 813
793 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, data, length) != 0) 814 conn->maximum_speed_reached = 0;
815
816 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, data, length) != 0) {
817 Packet_Data *dt1 = NULL;
818 get_data_pointer(&conn->send_array, &dt1, packet_num);
819 dt1->time = 0;
820 conn->maximum_speed_reached = 1;
794 fprintf(stderr, "send_data_packet failed\n"); 821 fprintf(stderr, "send_data_packet failed\n");
822 }
795 823
796 return packet_num; 824 return packet_num;
797} 825}
@@ -799,7 +827,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, uint
799/* Get the lowest 2 bytes from the nonce and convert 827/* Get the lowest 2 bytes from the nonce and convert
800 * them to host byte format before returning them. 828 * them to host byte format before returning them.
801 */ 829 */
802static uint16_t get_nonce_uint16(uint8_t *nonce) 830static uint16_t get_nonce_uint16(const uint8_t *nonce)
803{ 831{
804 uint16_t num; 832 uint16_t num;
805 memcpy(&num, nonce + (crypto_box_NONCEBYTES - sizeof(uint16_t)), sizeof(uint16_t)); 833 memcpy(&num, nonce + (crypto_box_NONCEBYTES - sizeof(uint16_t)), sizeof(uint16_t));
@@ -815,7 +843,7 @@ static uint16_t get_nonce_uint16(uint8_t *nonce)
815 * return -1 on failure. 843 * return -1 on failure.
816 * return length of data on success. 844 * return length of data on success.
817 */ 845 */
818static int handle_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *data, const uint8_t *packet, 846static int handle_data_packet(const Net_Crypto *c, int crypt_connection_id, uint8_t *data, const uint8_t *packet,
819 uint16_t length) 847 uint16_t length)
820{ 848{
821 if (length <= (1 + sizeof(uint16_t) + crypto_box_MACBYTES) || length > MAX_CRYPTO_PACKET_SIZE) 849 if (length <= (1 + sizeof(uint16_t) + crypto_box_MACBYTES) || length > MAX_CRYPTO_PACKET_SIZE)
@@ -852,7 +880,7 @@ static int handle_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *d
852 * return -1 on failure. 880 * return -1 on failure.
853 * return 0 on success. 881 * return 0 on success.
854 */ 882 */
855static int send_request_packet(Net_Crypto *c, int crypt_connection_id) 883static int send_request_packet(const Net_Crypto *c, int crypt_connection_id)
856{ 884{
857 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 885 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
858 886
@@ -874,7 +902,7 @@ static int send_request_packet(Net_Crypto *c, int crypt_connection_id)
874 * return -1 on failure. 902 * return -1 on failure.
875 * return number of packets sent on success. 903 * return number of packets sent on success.
876 */ 904 */
877static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16_t max_num) 905static int send_requested_packets(const Net_Crypto *c, int crypt_connection_id, uint16_t max_num)
878{ 906{
879 if (max_num == 0) 907 if (max_num == 0)
880 return -1; 908 return -1;
@@ -920,7 +948,7 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16
920 * return -1 on failure. 948 * return -1 on failure.
921 * return 0 on success. 949 * return 0 on success.
922 */ 950 */
923static int new_temp_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *packet, uint16_t length) 951static int new_temp_packet(const Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length)
924{ 952{
925 if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) 953 if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE)
926 return -1; 954 return -1;
@@ -951,7 +979,7 @@ static int new_temp_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *pack
951 * return -1 on failure. 979 * return -1 on failure.
952 * return 0 on success. 980 * return 0 on success.
953 */ 981 */
954static int clear_temp_packet(Net_Crypto *c, int crypt_connection_id) 982static int clear_temp_packet(const Net_Crypto *c, int crypt_connection_id)
955{ 983{
956 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 984 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
957 985
@@ -974,7 +1002,7 @@ static int clear_temp_packet(Net_Crypto *c, int crypt_connection_id)
974 * return -1 on failure. 1002 * return -1 on failure.
975 * return 0 on success. 1003 * return 0 on success.
976 */ 1004 */
977static int send_temp_packet(Net_Crypto *c, int crypt_connection_id) 1005static int send_temp_packet(const Net_Crypto *c, int crypt_connection_id)
978{ 1006{
979 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1007 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
980 1008
@@ -998,7 +1026,8 @@ static int send_temp_packet(Net_Crypto *c, int crypt_connection_id)
998 * return -1 on failure. 1026 * return -1 on failure.
999 * return 0 on success. 1027 * return 0 on success.
1000 */ 1028 */
1001static int create_send_handshake(Net_Crypto *c, int crypt_connection_id, uint8_t *cookie, uint8_t *dht_public_key) 1029static int create_send_handshake(const Net_Crypto *c, int crypt_connection_id, const uint8_t *cookie,
1030 const uint8_t *dht_public_key)
1002{ 1031{
1003 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1032 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1004 1033
@@ -1023,7 +1052,7 @@ static int create_send_handshake(Net_Crypto *c, int crypt_connection_id, uint8_t
1023 * return -1 on failure. 1052 * return -1 on failure.
1024 * return 0 on success. 1053 * return 0 on success.
1025 */ 1054 */
1026static int send_kill_packet(Net_Crypto *c, int crypt_connection_id) 1055static int send_kill_packet(const Net_Crypto *c, int crypt_connection_id)
1027{ 1056{
1028 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1057 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1029 1058
@@ -1040,7 +1069,8 @@ static int send_kill_packet(Net_Crypto *c, int crypt_connection_id)
1040 * return -1 on failure. 1069 * return -1 on failure.
1041 * return 0 on success. 1070 * return 0 on success.
1042 */ 1071 */
1043static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length) 1072static int handle_data_packet_helper(const Net_Crypto *c, int crypt_connection_id, const uint8_t *packet,
1073 uint16_t length)
1044{ 1074{
1045 if (length > MAX_CRYPTO_PACKET_SIZE || length <= CRYPTO_DATA_PACKET_MIN_SIZE) 1075 if (length > MAX_CRYPTO_PACKET_SIZE || length <= CRYPTO_DATA_PACKET_MIN_SIZE)
1046 return -1; 1076 return -1;
@@ -1289,7 +1319,7 @@ static int wipe_crypto_connection(Net_Crypto *c, int crypt_connection_id)
1289 * return -1 if there are no connections like we are looking for. 1319 * return -1 if there are no connections like we are looking for.
1290 * return id if it found it. 1320 * return id if it found it.
1291 */ 1321 */
1292static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) 1322static int getcryptconnection_id(const Net_Crypto *c, const uint8_t *public_key)
1293{ 1323{
1294 uint32_t i; 1324 uint32_t i;
1295 1325
@@ -1307,7 +1337,7 @@ static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key)
1307 * return -1 if there are no connections like we are looking for. 1337 * return -1 if there are no connections like we are looking for.
1308 * return id if it found it. 1338 * return id if it found it.
1309 */ 1339 */
1310static int getcryptconnection_id_dht_pubkey(Net_Crypto *c, uint8_t *dht_public_key) 1340static int getcryptconnection_id_dht_pubkey(const Net_Crypto *c, const uint8_t *dht_public_key)
1311{ 1341{
1312 uint32_t i; 1342 uint32_t i;
1313 1343
@@ -1465,7 +1495,7 @@ int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c)
1465 * return -1 on failure. 1495 * return -1 on failure.
1466 * return connection id on success. 1496 * return connection id on success.
1467 */ 1497 */
1468int new_crypto_connection(Net_Crypto *c, uint8_t *real_public_key) 1498int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key)
1469{ 1499{
1470 int crypt_connection_id = getcryptconnection_id(c, real_public_key); 1500 int crypt_connection_id = getcryptconnection_id(c, real_public_key);
1471 1501
@@ -1495,7 +1525,7 @@ int new_crypto_connection(Net_Crypto *c, uint8_t *real_public_key)
1495 * return -1 on failure. 1525 * return -1 on failure.
1496 * return 0 on success. 1526 * return 0 on success.
1497 */ 1527 */
1498static int disconnect_peer_tcp(Net_Crypto *c, int crypt_connection_id) 1528static int disconnect_peer_tcp(const Net_Crypto *c, int crypt_connection_id)
1499{ 1529{
1500 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1530 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1501 1531
@@ -1520,7 +1550,7 @@ static int disconnect_peer_tcp(Net_Crypto *c, int crypt_connection_id)
1520 * return -1 on failure. 1550 * return -1 on failure.
1521 * return 0 on success. 1551 * return 0 on success.
1522 */ 1552 */
1523static int connect_peer_tcp(Net_Crypto *c, int crypt_connection_id) 1553static int connect_peer_tcp(const Net_Crypto *c, int crypt_connection_id)
1524{ 1554{
1525 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1555 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1526 1556
@@ -1545,7 +1575,7 @@ static int connect_peer_tcp(Net_Crypto *c, int crypt_connection_id)
1545 * return 0 on failure (no key copied). 1575 * return 0 on failure (no key copied).
1546 * return timestamp on success (key copied). 1576 * return timestamp on success (key copied).
1547 */ 1577 */
1548uint64_t get_connection_dht_key(Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key) 1578uint64_t get_connection_dht_key(const Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key)
1549{ 1579{
1550 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1580 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1551 1581
@@ -1567,7 +1597,8 @@ uint64_t get_connection_dht_key(Net_Crypto *c, int crypt_connection_id, uint8_t
1567 * return -1 on failure. 1597 * return -1 on failure.
1568 * return 0 on success. 1598 * return 0 on success.
1569 */ 1599 */
1570int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key, uint64_t timestamp) 1600int set_connection_dht_public_key(const Net_Crypto *c, int crypt_connection_id, const uint8_t *dht_public_key,
1601 uint64_t timestamp)
1571{ 1602{
1572 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1603 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1573 1604
@@ -1628,7 +1659,7 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port)
1628 return -1; 1659 return -1;
1629} 1660}
1630 1661
1631static int tcp_response_callback(void *object, uint8_t connection_id, uint8_t *public_key) 1662static int tcp_response_callback(void *object, uint8_t connection_id, const uint8_t *public_key)
1632{ 1663{
1633 TCP_Client_Connection *TCP_con = object; 1664 TCP_Client_Connection *TCP_con = object;
1634 Net_Crypto *c = TCP_con->net_crypto_pointer; 1665 Net_Crypto *c = TCP_con->net_crypto_pointer;
@@ -1695,7 +1726,7 @@ static int tcp_status_callback(void *object, uint32_t number, uint8_t connection
1695 return 0; 1726 return 0;
1696} 1727}
1697 1728
1698static int tcp_data_callback(void *object, uint32_t number, uint8_t connection_id, uint8_t *data, uint16_t length) 1729static int tcp_data_callback(void *object, uint32_t number, uint8_t connection_id, const uint8_t *data, uint16_t length)
1699{ 1730{
1700 1731
1701 if (length == 0) 1732 if (length == 0)
@@ -1720,7 +1751,7 @@ static int tcp_data_callback(void *object, uint32_t number, uint8_t connection_i
1720 return 0; 1751 return 0;
1721} 1752}
1722 1753
1723static int tcp_oob_callback(void *object, uint8_t *public_key, uint8_t *data, uint16_t length) 1754static int tcp_oob_callback(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length)
1724{ 1755{
1725 if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) 1756 if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE)
1726 return -1; 1757 return -1;
@@ -1762,7 +1793,7 @@ static int tcp_oob_callback(void *object, uint8_t *public_key, uint8_t *data, ui
1762 * return -1 if it can't. 1793 * return -1 if it can't.
1763 * return 0 if it can. 1794 * return 0 if it can.
1764 */ 1795 */
1765static int tcp_connection_check(Net_Crypto *c, uint8_t *public_key) 1796static int tcp_connection_check(const Net_Crypto *c, const uint8_t *public_key)
1766{ 1797{
1767 uint32_t i; 1798 uint32_t i;
1768 1799
@@ -1797,7 +1828,7 @@ static int tcp_connection_check(Net_Crypto *c, uint8_t *public_key)
1797 * return 0 if it was added. 1828 * return 0 if it was added.
1798 * return -1 if it wasn't. 1829 * return -1 if it wasn't.
1799 */ 1830 */
1800int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, uint8_t *public_key) 1831int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, const uint8_t *public_key)
1801{ 1832{
1802 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1833 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1803 1834
@@ -1850,7 +1881,7 @@ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port,
1850 * return 0 if it was added. 1881 * return 0 if it was added.
1851 * return -1 if it wasn't. 1882 * return -1 if it wasn't.
1852 */ 1883 */
1853int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, uint8_t *public_key) 1884int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key)
1854{ 1885{
1855 if (ip_port.ip.family == TCP_INET) { 1886 if (ip_port.ip.family == TCP_INET) {
1856 ip_port.ip.family = AF_INET; 1887 ip_port.ip.family = AF_INET;
@@ -1882,7 +1913,7 @@ int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, uint8_t *public_key)
1882 * return number of relays copied to tcp_relays on success. 1913 * return number of relays copied to tcp_relays on success.
1883 * return 0 on failure. 1914 * return 0 on failure.
1884 */ 1915 */
1885unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, uint16_t num) 1916unsigned int copy_connected_tcp_relays(const Net_Crypto *c, Node_format *tcp_relays, uint16_t num)
1886{ 1917{
1887 if (num == 0) 1918 if (num == 0)
1888 return 0; 1919 return 0;
@@ -2046,8 +2077,8 @@ static void clear_disconnected_tcp(Net_Crypto *c)
2046 * return -1 on failure. 2077 * return -1 on failure.
2047 * return 0 on success. 2078 * return 0 on success.
2048 */ 2079 */
2049int connection_status_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_status_callback)(void *object, 2080int connection_status_handler(const Net_Crypto *c, int crypt_connection_id,
2050 int id, uint8_t status), void *object, int id) 2081 int (*connection_status_callback)(void *object, int id, uint8_t status), void *object, int id)
2051{ 2082{
2052 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 2083 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
2053 2084
@@ -2068,7 +2099,7 @@ int connection_status_handler(Net_Crypto *c, int crypt_connection_id, int (*conn
2068 * return -1 on failure. 2099 * return -1 on failure.
2069 * return 0 on success. 2100 * return 0 on success.
2070 */ 2101 */
2071int connection_data_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object, 2102int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object,
2072 int id, uint8_t *data, uint16_t length), void *object, int id) 2103 int id, uint8_t *data, uint16_t length), void *object, int id)
2073{ 2104{
2074 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 2105 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
@@ -2091,7 +2122,7 @@ int connection_data_handler(Net_Crypto *c, int crypt_connection_id, int (*connec
2091 * return 0 on success. 2122 * return 0 on success.
2092 */ 2123 */
2093int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, 2124int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id,
2094 int (*connection_lossy_data_callback)(void *object, int id, uint8_t *data, uint16_t length), void *object, int id) 2125 int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length), void *object, int id)
2095{ 2126{
2096 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 2127 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
2097 2128
@@ -2109,7 +2140,7 @@ int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id,
2109 * return -1 on failure. 2140 * return -1 on failure.
2110 * return connection id on success. 2141 * return connection id on success.
2111 */ 2142 */
2112static int crypto_id_ip_port(Net_Crypto *c, IP_Port ip_port) 2143static int crypto_id_ip_port(const Net_Crypto *c, IP_Port ip_port)
2113{ 2144{
2114 return bs_list_find(&c->ip_port_list, &ip_port); 2145 return bs_list_find(&c->ip_port_list, &ip_port);
2115} 2146}
@@ -2345,7 +2376,7 @@ static void send_crypto_packets(Net_Crypto *c)
2345/* returns the number of packet slots left in the sendbuffer. 2376/* returns the number of packet slots left in the sendbuffer.
2346 * return 0 if failure. 2377 * return 0 if failure.
2347 */ 2378 */
2348uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id) 2379uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connection_id)
2349{ 2380{
2350 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 2381 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
2351 2382
@@ -2362,7 +2393,7 @@ uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id)
2362 * 2393 *
2363 * The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range. 2394 * The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range.
2364 */ 2395 */
2365int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) 2396int64_t write_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length)
2366{ 2397{
2367 if (length == 0) 2398 if (length == 0)
2368 return -1; 2399 return -1;
@@ -2399,7 +2430,7 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data,
2399 * 2430 *
2400 * Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*) 2431 * Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*)
2401 */ 2432 */
2402int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) 2433int send_lossy_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length)
2403{ 2434{
2404 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) 2435 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE)
2405 return -1; 2436 return -1;
@@ -2441,7 +2472,7 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id)
2441 * 2472 *
2442 * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. 2473 * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't.
2443 */ 2474 */
2444unsigned int crypto_connection_status(Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected) 2475unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected)
2445{ 2476{
2446 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 2477 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
2447 2478
@@ -2464,7 +2495,7 @@ void new_keys(Net_Crypto *c)
2464/* Save the public and private keys to the keys array. 2495/* Save the public and private keys to the keys array.
2465 * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. 2496 * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES.
2466 */ 2497 */
2467void save_keys(Net_Crypto *c, uint8_t *keys) 2498void save_keys(const Net_Crypto *c, uint8_t *keys)
2468{ 2499{
2469 memcpy(keys, c->self_public_key, crypto_box_PUBLICKEYBYTES); 2500 memcpy(keys, c->self_public_key, crypto_box_PUBLICKEYBYTES);
2470 memcpy(keys + crypto_box_PUBLICKEYBYTES, c->self_secret_key, crypto_box_SECRETKEYBYTES); 2501 memcpy(keys + crypto_box_PUBLICKEYBYTES, c->self_secret_key, crypto_box_SECRETKEYBYTES);
@@ -2552,7 +2583,7 @@ static void kill_timedout(Net_Crypto *c)
2552 2583
2553/* return the optimal interval in ms for running do_net_crypto. 2584/* return the optimal interval in ms for running do_net_crypto.
2554 */ 2585 */
2555uint32_t crypto_run_interval(Net_Crypto *c) 2586uint32_t crypto_run_interval(const Net_Crypto *c)
2556{ 2587{
2557 return c->current_sleep_time; 2588 return c->current_sleep_time;
2558} 2589}
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index d30d5a63..4fed0000 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -129,7 +129,7 @@ typedef struct {
129 void *connection_data_callback_object; 129 void *connection_data_callback_object;
130 int connection_data_callback_id; 130 int connection_data_callback_id;
131 131
132 int (*connection_lossy_data_callback)(void *object, int id, uint8_t *data, uint16_t length); 132 int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length);
133 void *connection_lossy_data_callback_object; 133 void *connection_lossy_data_callback_object;
134 int connection_lossy_data_callback_id; 134 int connection_lossy_data_callback_id;
135 135
@@ -157,6 +157,8 @@ typedef struct {
157 Node_format tcp_relays[MAX_TCP_RELAYS_PEER]; 157 Node_format tcp_relays[MAX_TCP_RELAYS_PEER];
158 uint16_t num_tcp_relays; 158 uint16_t num_tcp_relays;
159 159
160 uint8_t maximum_speed_reached;
161
160 pthread_mutex_t mutex; 162 pthread_mutex_t mutex;
161} Crypto_Connection; 163} Crypto_Connection;
162 164
@@ -218,14 +220,14 @@ int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c);
218 * return -1 on failure. 220 * return -1 on failure.
219 * return connection id on success. 221 * return connection id on success.
220 */ 222 */
221int new_crypto_connection(Net_Crypto *c, uint8_t *real_public_key); 223int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key);
222 224
223/* Copy friends DHT public key into dht_key. 225/* Copy friends DHT public key into dht_key.
224 * 226 *
225 * return 0 on failure (no key copied). 227 * return 0 on failure (no key copied).
226 * return timestamp on success (key copied). 228 * return timestamp on success (key copied).
227 */ 229 */
228uint64_t get_connection_dht_key(Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key); 230uint64_t get_connection_dht_key(const Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key);
229 231
230/* Set the DHT public key of the crypto connection. 232/* Set the DHT public key of the crypto connection.
231 * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to 233 * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to
@@ -234,7 +236,8 @@ uint64_t get_connection_dht_key(Net_Crypto *c, int crypt_connection_id, uint8_t
234 * return -1 on failure. 236 * return -1 on failure.
235 * return 0 on success. 237 * return 0 on success.
236 */ 238 */
237int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key, uint64_t timestamp); 239int set_connection_dht_public_key(const Net_Crypto *c, int crypt_connection_id, const uint8_t *dht_public_key,
240 uint64_t timestamp);
238 241
239/* Set the direct ip of the crypto connection. 242/* Set the direct ip of the crypto connection.
240 * 243 *
@@ -253,8 +256,8 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port);
253 * return -1 on failure. 256 * return -1 on failure.
254 * return 0 on success. 257 * return 0 on success.
255 */ 258 */
256int connection_status_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_status_callback)(void *object, 259int connection_status_handler(const Net_Crypto *c, int crypt_connection_id,
257 int id, uint8_t status), void *object, int id); 260 int (*connection_status_callback)(void *object, int id, uint8_t status), void *object, int id);
258 261
259/* Set function to be called when connection with crypt_connection_id receives a lossless data packet of length. 262/* Set function to be called when connection with crypt_connection_id receives a lossless data packet of length.
260 * 263 *
@@ -264,7 +267,7 @@ int connection_status_handler(Net_Crypto *c, int crypt_connection_id, int (*conn
264 * return -1 on failure. 267 * return -1 on failure.
265 * return 0 on success. 268 * return 0 on success.
266 */ 269 */
267int connection_data_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object, 270int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object,
268 int id, uint8_t *data, uint16_t length), void *object, int id); 271 int id, uint8_t *data, uint16_t length), void *object, int id);
269 272
270 273
@@ -277,12 +280,13 @@ int connection_data_handler(Net_Crypto *c, int crypt_connection_id, int (*connec
277 * return 0 on success. 280 * return 0 on success.
278 */ 281 */
279int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, 282int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id,
280 int (*connection_lossy_data_callback)(void *object, int id, uint8_t *data, uint16_t length), void *object, int id); 283 int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length), void *object,
284 int id);
281 285
282/* returns the number of packet slots left in the sendbuffer. 286/* returns the number of packet slots left in the sendbuffer.
283 * return 0 if failure. 287 * return 0 if failure.
284 */ 288 */
285uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id); 289uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connection_id);
286 290
287/* Sends a lossless cryptopacket. 291/* Sends a lossless cryptopacket.
288 * 292 *
@@ -291,28 +295,28 @@ uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id)
291 * 295 *
292 * The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range. 296 * The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range.
293 */ 297 */
294int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length); 298int64_t write_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length);
295 299
296/* return -1 on failure. 300/* return -1 on failure.
297 * return 0 on success. 301 * return 0 on success.
298 * 302 *
299 * Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*) 303 * Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*)
300 */ 304 */
301int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length); 305int send_lossy_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length);
302 306
303/* Add a tcp relay, associating it to a crypt_connection_id. 307/* Add a tcp relay, associating it to a crypt_connection_id.
304 * 308 *
305 * return 0 if it was added. 309 * return 0 if it was added.
306 * return -1 if it wasn't. 310 * return -1 if it wasn't.
307 */ 311 */
308int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, uint8_t *public_key); 312int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, const uint8_t *public_key);
309 313
310/* Add a tcp relay to the array. 314/* Add a tcp relay to the array.
311 * 315 *
312 * return 0 if it was added. 316 * return 0 if it was added.
313 * return -1 if it wasn't. 317 * return -1 if it wasn't.
314 */ 318 */
315int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, uint8_t *public_key); 319int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key);
316 320
317/* Copy a maximum of num TCP relays we are connected to to tcp_relays. 321/* Copy a maximum of num TCP relays we are connected to to tcp_relays.
318 * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6. 322 * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6.
@@ -320,7 +324,7 @@ int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, uint8_t *public_key);
320 * return number of relays copied to tcp_relays on success. 324 * return number of relays copied to tcp_relays on success.
321 * return 0 on failure. 325 * return 0 on failure.
322 */ 326 */
323unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, uint16_t num); 327unsigned int copy_connected_tcp_relays(const Net_Crypto *c, Node_format *tcp_relays, uint16_t num);
324 328
325/* Kill a crypto connection. 329/* Kill a crypto connection.
326 * 330 *
@@ -334,7 +338,7 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id);
334 * 338 *
335 * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. 339 * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't.
336 */ 340 */
337unsigned int crypto_connection_status(Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected); 341unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected);
338 342
339 343
340/* Generate our public and private keys. 344/* Generate our public and private keys.
@@ -345,7 +349,7 @@ void new_keys(Net_Crypto *c);
345/* Save the public and private keys to the keys array. 349/* Save the public and private keys to the keys array.
346 * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. 350 * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES.
347 */ 351 */
348void save_keys(Net_Crypto *c, uint8_t *keys); 352void save_keys(const Net_Crypto *c, uint8_t *keys);
349 353
350/* Load the public and private keys from the keys array. 354/* Load the public and private keys from the keys array.
351 * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. 355 * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES.
@@ -359,7 +363,7 @@ Net_Crypto *new_net_crypto(DHT *dht);
359 363
360/* return the optimal interval in ms for running do_net_crypto. 364/* return the optimal interval in ms for running do_net_crypto.
361 */ 365 */
362uint32_t crypto_run_interval(Net_Crypto *c); 366uint32_t crypto_run_interval(const Net_Crypto *c);
363 367
364/* Main loop. */ 368/* Main loop. */
365void do_net_crypto(Net_Crypto *c); 369void do_net_crypto(Net_Crypto *c);
diff --git a/toxcore/network.c b/toxcore/network.c
index 05b2ce2e..0009a558 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -181,13 +181,13 @@ int set_socket_dualstack(sock_t sock)
181{ 181{
182 int ipv6only = 0; 182 int ipv6only = 0;
183 socklen_t optsize = sizeof(ipv6only); 183 socklen_t optsize = sizeof(ipv6only);
184 int res = getsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&ipv6only, &optsize); 184 int res = getsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&ipv6only, &optsize);
185 185
186 if ((res == 0) && (ipv6only == 0)) 186 if ((res == 0) && (ipv6only == 0))
187 return 1; 187 return 1;
188 188
189 ipv6only = 0; 189 ipv6only = 0;
190 return (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&ipv6only, sizeof(ipv6only)) == 0); 190 return (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&ipv6only, sizeof(ipv6only)) == 0);
191} 191}
192 192
193 193
diff --git a/toxcore/onion.c b/toxcore/onion.c
index ac654689..1915324f 100644
--- a/toxcore/onion.c
+++ b/toxcore/onion.c
@@ -54,7 +54,7 @@ static void change_symmetric_key(Onion *onion)
54 * return -1 on failure. 54 * return -1 on failure.
55 * return 0 on success. 55 * return 0 on success.
56 */ 56 */
57int create_onion_path(DHT *dht, Onion_Path *new_path, Node_format *nodes) 57int create_onion_path(const DHT *dht, Onion_Path *new_path, const Node_format *nodes)
58{ 58{
59 if (!new_path || !nodes) 59 if (!new_path || !nodes)
60 return -1; 60 return -1;
@@ -93,8 +93,8 @@ int create_onion_path(DHT *dht, Onion_Path *new_path, Node_format *nodes)
93 * return -1 on failure. 93 * return -1 on failure.
94 * return length of created packet on success. 94 * return length of created packet on success.
95 */ 95 */
96int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, Onion_Path *path, IP_Port dest, uint8_t *data, 96int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest,
97 uint32_t length) 97 const uint8_t *data, uint32_t length)
98{ 98{
99 if (1 + length + SEND_1 > max_packet_length || length == 0) 99 if (1 + length + SEND_1 > max_packet_length || length == 0)
100 return -1; 100 return -1;
@@ -149,7 +149,7 @@ int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, Onion_Path
149 * return -1 on failure. 149 * return -1 on failure.
150 * return 0 on success. 150 * return 0 on success.
151 */ 151 */
152int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *data, uint32_t length) 152int send_onion_packet(Networking_Core *net, const Onion_Path *path, IP_Port dest, const uint8_t *data, uint32_t length)
153{ 153{
154 uint8_t packet[ONION_MAX_PACKET_SIZE]; 154 uint8_t packet[ONION_MAX_PACKET_SIZE];
155 int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length); 155 int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length);
@@ -209,7 +209,7 @@ static int handle_send_initial(void *object, IP_Port source, const uint8_t *pack
209 return onion_send_1(onion, plain, len, source, packet + 1); 209 return onion_send_1(onion, plain, len, source, packet + 1);
210} 210}
211 211
212int onion_send_1(Onion *onion, uint8_t *plain, uint32_t len, IP_Port source, const uint8_t *nonce) 212int onion_send_1(const Onion *onion, const uint8_t *plain, uint32_t len, IP_Port source, const uint8_t *nonce)
213{ 213{
214 IP_Port send_to; 214 IP_Port send_to;
215 ipport_unpack(&send_to, plain); 215 ipport_unpack(&send_to, plain);
diff --git a/toxcore/onion.h b/toxcore/onion.h
index cd12fe2b..527f5c60 100644
--- a/toxcore/onion.h
+++ b/toxcore/onion.h
@@ -76,7 +76,7 @@ typedef struct {
76 * return -1 on failure. 76 * return -1 on failure.
77 * return 0 on success. 77 * return 0 on success.
78 */ 78 */
79int create_onion_path(DHT *dht, Onion_Path *new_path, Node_format *nodes); 79int create_onion_path(const DHT *dht, Onion_Path *new_path, const Node_format *nodes);
80 80
81/* Create a onion packet. 81/* Create a onion packet.
82 * 82 *
@@ -87,8 +87,8 @@ int create_onion_path(DHT *dht, Onion_Path *new_path, Node_format *nodes);
87 * return -1 on failure. 87 * return -1 on failure.
88 * return length of created packet on success. 88 * return length of created packet on success.
89 */ 89 */
90int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, Onion_Path *path, IP_Port dest, uint8_t *data, 90int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest,
91 uint32_t length); 91 const uint8_t *data, uint32_t length);
92 92
93/* Create and send a onion packet. 93/* Create and send a onion packet.
94 * 94 *
@@ -98,7 +98,7 @@ int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, Onion_Path
98 * return -1 on failure. 98 * return -1 on failure.
99 * return 0 on success. 99 * return 0 on success.
100 */ 100 */
101int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *data, uint32_t length); 101int send_onion_packet(Networking_Core *net, const Onion_Path *path, IP_Port dest, const uint8_t *data, uint32_t length);
102 102
103/* Create and send a onion response sent initially to dest with. 103/* Create and send a onion response sent initially to dest with.
104 * Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE. 104 * Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE.
@@ -118,7 +118,7 @@ int send_onion_response(Networking_Core *net, IP_Port dest, const uint8_t *data,
118 * Source family must be set to something else than AF_INET6 or AF_INET so that the callback gets called 118 * Source family must be set to something else than AF_INET6 or AF_INET so that the callback gets called
119 * when the response is received. 119 * when the response is received.
120 */ 120 */
121int onion_send_1(Onion *onion, uint8_t *plain, uint32_t len, IP_Port source, const uint8_t *nonce); 121int onion_send_1(const Onion *onion, const uint8_t *plain, uint32_t len, IP_Port source, const uint8_t *nonce);
122 122
123/* Set the callback to be called when the dest ip_port doesn't have AF_INET6 or AF_INET as the family. 123/* Set the callback to be called when the dest ip_port doesn't have AF_INET6 or AF_INET as the family.
124 * 124 *
diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c
index 658d4c11..dff05135 100644
--- a/toxcore/onion_announce.c
+++ b/toxcore/onion_announce.c
@@ -49,9 +49,9 @@
49 * return -1 on failure. 49 * return -1 on failure.
50 * return packet length on success. 50 * return packet length on success.
51 */ 51 */
52int create_announce_request(uint8_t *packet, uint16_t max_packet_length, Onion_Path *path, Node_format dest, 52int create_announce_request(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, Node_format dest,
53 uint8_t *public_key, uint8_t *secret_key, uint8_t *ping_id, uint8_t *client_id, uint8_t *data_public_key, 53 const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *ping_id, const uint8_t *client_id,
54 uint64_t sendback_data) 54 const uint8_t *data_public_key, uint64_t sendback_data)
55{ 55{
56 uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES + 56 uint8_t plain[ONION_PING_ID_SIZE + crypto_box_PUBLICKEYBYTES + crypto_box_PUBLICKEYBYTES +
57 ONION_ANNOUNCE_SENDBACK_DATA_LENGTH]; 57 ONION_ANNOUNCE_SENDBACK_DATA_LENGTH];
@@ -89,8 +89,9 @@ int create_announce_request(uint8_t *packet, uint16_t max_packet_length, Onion_P
89 * return -1 on failure. 89 * return -1 on failure.
90 * return 0 on success. 90 * return 0 on success.
91 */ 91 */
92int create_data_request(uint8_t *packet, uint16_t max_packet_length, Onion_Path *path, IP_Port dest, 92int create_data_request(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest,
93 uint8_t *public_key, uint8_t *encrypt_public_key, uint8_t *nonce, uint8_t *data, uint16_t length) 93 const uint8_t *public_key, const uint8_t *encrypt_public_key, const uint8_t *nonce, const uint8_t *data,
94 uint16_t length)
94{ 95{
95 if ((unsigned int)DATA_REQUEST_MIN_SIZE + length > ONION_MAX_DATA_SIZE) 96 if ((unsigned int)DATA_REQUEST_MIN_SIZE + length > ONION_MAX_DATA_SIZE)
96 return -1; 97 return -1;
@@ -129,8 +130,9 @@ int create_data_request(uint8_t *packet, uint16_t max_packet_length, Onion_Path
129 * return -1 on failure. 130 * return -1 on failure.
130 * return 0 on success. 131 * return 0 on success.
131 */ 132 */
132int send_announce_request(Networking_Core *net, Onion_Path *path, Node_format dest, uint8_t *public_key, 133int send_announce_request(Networking_Core *net, const Onion_Path *path, Node_format dest, const uint8_t *public_key,
133 uint8_t *secret_key, uint8_t *ping_id, uint8_t *client_id, uint8_t *data_public_key, uint64_t sendback_data) 134 const uint8_t *secret_key, const uint8_t *ping_id, const uint8_t *client_id, const uint8_t *data_public_key,
135 uint64_t sendback_data)
134{ 136{
135 uint8_t packet[ONION_MAX_PACKET_SIZE]; 137 uint8_t packet[ONION_MAX_PACKET_SIZE];
136 int len = create_announce_request(packet, sizeof(packet), path, dest, public_key, secret_key, ping_id, client_id, 138 int len = create_announce_request(packet, sizeof(packet), path, dest, public_key, secret_key, ping_id, client_id,
@@ -159,8 +161,8 @@ int send_announce_request(Networking_Core *net, Onion_Path *path, Node_format de
159 * return -1 on failure. 161 * return -1 on failure.
160 * return 0 on success. 162 * return 0 on success.
161 */ 163 */
162int send_data_request(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *public_key, 164int send_data_request(Networking_Core *net, const Onion_Path *path, IP_Port dest, const uint8_t *public_key,
163 uint8_t *encrypt_public_key, uint8_t *nonce, uint8_t *data, uint16_t length) 165 const uint8_t *encrypt_public_key, const uint8_t *nonce, const uint8_t *data, uint16_t length)
164{ 166{
165 uint8_t packet[ONION_MAX_PACKET_SIZE]; 167 uint8_t packet[ONION_MAX_PACKET_SIZE];
166 int len = create_data_request(packet, sizeof(packet), path, dest, public_key, encrypt_public_key, nonce, data, length); 168 int len = create_data_request(packet, sizeof(packet), path, dest, public_key, encrypt_public_key, nonce, data, length);
@@ -175,8 +177,8 @@ int send_data_request(Networking_Core *net, Onion_Path *path, IP_Port dest, uint
175} 177}
176 178
177/* Generate a ping_id and put it in ping_id */ 179/* Generate a ping_id and put it in ping_id */
178static void generate_ping_id(Onion_Announce *onion_a, uint64_t time, const uint8_t *public_key, IP_Port ret_ip_port, 180static void generate_ping_id(const Onion_Announce *onion_a, uint64_t time, const uint8_t *public_key,
179 uint8_t *ping_id) 181 IP_Port ret_ip_port, uint8_t *ping_id)
180{ 182{
181 time /= PING_ID_TIMEOUT; 183 time /= PING_ID_TIMEOUT;
182 uint8_t data[crypto_box_KEYBYTES + sizeof(time) + crypto_box_PUBLICKEYBYTES + sizeof(ret_ip_port)]; 184 uint8_t data[crypto_box_KEYBYTES + sizeof(time) + crypto_box_PUBLICKEYBYTES + sizeof(ret_ip_port)];
diff --git a/toxcore/onion_announce.h b/toxcore/onion_announce.h
index 42f63ccd..d31355e5 100644
--- a/toxcore/onion_announce.h
+++ b/toxcore/onion_announce.h
@@ -75,9 +75,9 @@ typedef struct {
75 * return -1 on failure. 75 * return -1 on failure.
76 * return packet length on success. 76 * return packet length on success.
77 */ 77 */
78int create_announce_request(uint8_t *packet, uint16_t max_packet_length, Onion_Path *path, Node_format dest, 78int create_announce_request(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, Node_format dest,
79 uint8_t *public_key, uint8_t *secret_key, uint8_t *ping_id, uint8_t *client_id, uint8_t *data_public_key, 79 const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *ping_id, const uint8_t *client_id,
80 uint64_t sendback_data); 80 const uint8_t *data_public_key, uint64_t sendback_data);
81 81
82/* Create an onion data request packet in packet of max_packet_length (recommended size ONION_MAX_PACKET_SIZE). 82/* Create an onion data request packet in packet of max_packet_length (recommended size ONION_MAX_PACKET_SIZE).
83 * 83 *
@@ -93,8 +93,9 @@ int create_announce_request(uint8_t *packet, uint16_t max_packet_length, Onion_P
93 * return -1 on failure. 93 * return -1 on failure.
94 * return 0 on success. 94 * return 0 on success.
95 */ 95 */
96int create_data_request(uint8_t *packet, uint16_t max_packet_length, Onion_Path *path, IP_Port dest, 96int create_data_request(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, IP_Port dest,
97 uint8_t *public_key, uint8_t *encrypt_public_key, uint8_t *nonce, uint8_t *data, uint16_t length); 97 const uint8_t *public_key, const uint8_t *encrypt_public_key, const uint8_t *nonce, const uint8_t *data,
98 uint16_t length);
98 99
99/* Create and send an onion announce request packet. 100/* Create and send an onion announce request packet.
100 * 101 *
@@ -110,8 +111,9 @@ int create_data_request(uint8_t *packet, uint16_t max_packet_length, Onion_Path
110 * return -1 on failure. 111 * return -1 on failure.
111 * return 0 on success. 112 * return 0 on success.
112 */ 113 */
113int send_announce_request(Networking_Core *net, Onion_Path *path, Node_format dest, uint8_t *public_key, 114int send_announce_request(Networking_Core *net, const Onion_Path *path, Node_format dest, const uint8_t *public_key,
114 uint8_t *secret_key, uint8_t *ping_id, uint8_t *client_id, uint8_t *data_public_key, uint64_t sendback_data); 115 const uint8_t *secret_key, const uint8_t *ping_id, const uint8_t *client_id, const uint8_t *data_public_key,
116 uint64_t sendback_data);
115 117
116/* Create and send an onion data request packet. 118/* Create and send an onion data request packet.
117 * 119 *
@@ -129,8 +131,8 @@ int send_announce_request(Networking_Core *net, Onion_Path *path, Node_format de
129 * return -1 on failure. 131 * return -1 on failure.
130 * return 0 on success. 132 * return 0 on success.
131 */ 133 */
132int send_data_request(Networking_Core *net, Onion_Path *path, IP_Port dest, uint8_t *public_key, 134int send_data_request(Networking_Core *net, const Onion_Path *path, IP_Port dest, const uint8_t *public_key,
133 uint8_t *encrypt_public_key, uint8_t *nonce, uint8_t *data, uint16_t length); 135 const uint8_t *encrypt_public_key, const uint8_t *nonce, const uint8_t *data, uint16_t length);
134 136
135 137
136Onion_Announce *new_onion_announce(DHT *dht); 138Onion_Announce *new_onion_announce(DHT *dht);
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index 40701e2d..7d0efc79 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -38,7 +38,7 @@
38 * return -1 if nodes are suitable for creating a new path. 38 * return -1 if nodes are suitable for creating a new path.
39 * return path number of already existing similar path if one already exists. 39 * return path number of already existing similar path if one already exists.
40 */ 40 */
41static int is_path_used(Onion_Client_Paths *onion_paths, Node_format *nodes) 41static int is_path_used(const Onion_Client_Paths *onion_paths, const Node_format *nodes)
42{ 42{
43 uint32_t i; 43 uint32_t i;
44 44
@@ -68,7 +68,7 @@ static int is_path_used(Onion_Client_Paths *onion_paths, Node_format *nodes)
68 * TODO: Make this function better, it currently probably is vulnerable to some attacks that 68 * TODO: Make this function better, it currently probably is vulnerable to some attacks that
69 * could de anonimize us. 69 * could de anonimize us.
70 */ 70 */
71static int random_path(DHT *dht, Onion_Client_Paths *onion_paths, uint32_t pathnum, Onion_Path *path) 71static int random_path(const DHT *dht, Onion_Client_Paths *onion_paths, uint32_t pathnum, Onion_Path *path)
72{ 72{
73 if (pathnum >= NUMBER_ONION_PATHS) 73 if (pathnum >= NUMBER_ONION_PATHS)
74 pathnum = rand() % NUMBER_ONION_PATHS; 74 pathnum = rand() % NUMBER_ONION_PATHS;
@@ -130,7 +130,7 @@ static uint32_t set_path_timeouts(Onion_Client *onion_c, uint32_t num, IP_Port s
130 * return -1 on failure. 130 * return -1 on failure.
131 * return 0 on success. 131 * return 0 on success.
132 */ 132 */
133static int send_onion_packet_tcp_udp(const Onion_Client *onion_c, IP_Port ip_port, uint8_t *data, uint32_t length) 133static int send_onion_packet_tcp_udp(const Onion_Client *onion_c, IP_Port ip_port, const uint8_t *data, uint32_t length)
134{ 134{
135 if (ip_port.ip.family == AF_INET || ip_port.ip.family == AF_INET6) { 135 if (ip_port.ip.family == AF_INET || ip_port.ip.family == AF_INET6) {
136 if ((uint32_t)sendpacket(onion_c->net, ip_port, data, length) != length) 136 if ((uint32_t)sendpacket(onion_c->net, ip_port, data, length) != length)
@@ -157,7 +157,8 @@ static int send_onion_packet_tcp_udp(const Onion_Client *onion_c, IP_Port ip_por
157 * return 0 on success 157 * return 0 on success
158 * 158 *
159 */ 159 */
160static int new_sendback(Onion_Client *onion_c, uint32_t num, uint8_t *public_key, IP_Port ip_port, uint64_t *sendback) 160static int new_sendback(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, IP_Port ip_port,
161 uint64_t *sendback)
161{ 162{
162 uint8_t data[sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port)]; 163 uint8_t data[sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port)];
163 memcpy(data, &num, sizeof(uint32_t)); 164 memcpy(data, &num, sizeof(uint32_t));
@@ -198,8 +199,8 @@ static uint32_t check_sendback(Onion_Client *onion_c, const uint8_t *sendback, u
198 return num; 199 return num;
199} 200}
200 201
201static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_Port dest, uint8_t *dest_pubkey, 202static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_Port dest, const uint8_t *dest_pubkey,
202 uint8_t *ping_id, uint32_t pathnum) 203 const uint8_t *ping_id, uint32_t pathnum)
203{ 204{
204 if (num > onion_c->num_friends) 205 if (num > onion_c->num_friends)
205 return -1; 206 return -1;
@@ -279,8 +280,8 @@ static int cmp_entry(const void *a, const void *b)
279 return 0; 280 return 0;
280} 281}
281 282
282static int client_add_to_list(Onion_Client *onion_c, uint32_t num, uint8_t *public_key, IP_Port ip_port, 283static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, IP_Port ip_port,
283 uint8_t is_stored, uint8_t *pingid_or_key, IP_Port source) 284 uint8_t is_stored, const uint8_t *pingid_or_key, IP_Port source)
284{ 285{
285 if (num > onion_c->num_friends) 286 if (num > onion_c->num_friends)
286 return -1; 287 return -1;
@@ -338,7 +339,7 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, uint8_t *publ
338 return 0; 339 return 0;
339} 340}
340 341
341static int good_to_ping(Last_Pinged *last_pinged, uint8_t *last_pinged_index, uint8_t *client_id) 342static int good_to_ping(Last_Pinged *last_pinged, uint8_t *last_pinged_index, const uint8_t *client_id)
342{ 343{
343 uint32_t i; 344 uint32_t i;
344 345
@@ -354,7 +355,7 @@ static int good_to_ping(Last_Pinged *last_pinged, uint8_t *last_pinged_index, ui
354 return 1; 355 return 1;
355} 356}
356 357
357static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, Node_format *nodes, uint16_t num_nodes, 358static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_format *nodes, uint16_t num_nodes,
358 IP_Port source) 359 IP_Port source)
359{ 360{
360 if (num > onion_c->num_friends) 361 if (num > onion_c->num_friends)
@@ -630,7 +631,7 @@ int send_onion_data(const Onion_Client *onion_c, int friend_num, const uint8_t *
630 * return the number of packets sent on success 631 * return the number of packets sent on success
631 * return -1 on failure. 632 * return -1 on failure.
632 */ 633 */
633static int send_dht_fakeid(Onion_Client *onion_c, int friend_num, uint8_t *data, uint32_t length) 634static int send_dht_fakeid(const Onion_Client *onion_c, int friend_num, const uint8_t *data, uint32_t length)
634{ 635{
635 if ((uint32_t)friend_num >= onion_c->num_friends) 636 if ((uint32_t)friend_num >= onion_c->num_friends)
636 return -1; 637 return -1;
@@ -693,7 +694,7 @@ static int handle_dht_fakeid(void *object, IP_Port source, const uint8_t *source
693 * return the number of packets sent on success 694 * return the number of packets sent on success
694 * return -1 on failure. 695 * return -1 on failure.
695 */ 696 */
696static int send_fakeid_announce(Onion_Client *onion_c, uint16_t friend_num, uint8_t onion_dht_both) 697static int send_fakeid_announce(const Onion_Client *onion_c, uint16_t friend_num, uint8_t onion_dht_both)
697{ 698{
698 if (friend_num >= onion_c->num_friends) 699 if (friend_num >= onion_c->num_friends)
699 return -1; 700 return -1;
@@ -851,7 +852,7 @@ int onion_delfriend(Onion_Client *onion_c, int friend_num)
851 * return 0 on success. 852 * return 0 on success.
852 */ 853 */
853int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_relay_node_callback)(void *object, 854int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_relay_node_callback)(void *object,
854 uint32_t number, IP_Port ip_port, uint8_t *public_key), void *object, uint32_t number) 855 uint32_t number, IP_Port ip_port, const uint8_t *public_key), void *object, uint32_t number)
855{ 856{
856 if ((uint32_t)friend_num >= onion_c->num_friends) 857 if ((uint32_t)friend_num >= onion_c->num_friends)
857 return -1; 858 return -1;
@@ -905,7 +906,7 @@ int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uin
905 * return 0 on failure (no key copied). 906 * return 0 on failure (no key copied).
906 * return timestamp on success (key copied). 907 * return timestamp on success (key copied).
907 */ 908 */
908uint64_t onion_getfriend_DHT_pubkey(Onion_Client *onion_c, int friend_num, uint8_t *dht_key) 909uint64_t onion_getfriend_DHT_pubkey(const Onion_Client *onion_c, int friend_num, uint8_t *dht_key)
909{ 910{
910 if ((uint32_t)friend_num >= onion_c->num_friends) 911 if ((uint32_t)friend_num >= onion_c->num_friends)
911 return 0; 912 return 0;
@@ -927,7 +928,7 @@ uint64_t onion_getfriend_DHT_pubkey(Onion_Client *onion_c, int friend_num, uint8
927 * return 1, ip if client_id refers to a friend and we found him 928 * return 1, ip if client_id refers to a friend and we found him
928 * 929 *
929 */ 930 */
930int onion_getfriendip(Onion_Client *onion_c, int friend_num, IP_Port *ip_port) 931int onion_getfriendip(const Onion_Client *onion_c, int friend_num, IP_Port *ip_port)
931{ 932{
932 uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; 933 uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES];
933 934
diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h
index a74783f6..bf891e7a 100644
--- a/toxcore/onion_client.h
+++ b/toxcore/onion_client.h
@@ -97,7 +97,7 @@ typedef struct {
97 Last_Pinged last_pinged[MAX_STORED_PINGED_NODES]; 97 Last_Pinged last_pinged[MAX_STORED_PINGED_NODES];
98 uint8_t last_pinged_index; 98 uint8_t last_pinged_index;
99 99
100 int (*tcp_relay_node_callback)(void *object, uint32_t number, IP_Port ip_port, uint8_t *public_key); 100 int (*tcp_relay_node_callback)(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key);
101 void *tcp_relay_node_callback_object; 101 void *tcp_relay_node_callback_object;
102 uint32_t tcp_relay_node_callback_number; 102 uint32_t tcp_relay_node_callback_number;
103} Onion_Friend; 103} Onion_Friend;
@@ -171,7 +171,7 @@ int onion_set_friend_online(Onion_Client *onion_c, int friend_num, uint8_t is_on
171 * return 1, ip if client_id refers to a friend and we found him 171 * return 1, ip if client_id refers to a friend and we found him
172 * 172 *
173 */ 173 */
174int onion_getfriendip(Onion_Client *onion_c, int friend_num, IP_Port *ip_port); 174int onion_getfriendip(const Onion_Client *onion_c, int friend_num, IP_Port *ip_port);
175 175
176/* Set the function for this friend that will be callbacked with object and number 176/* Set the function for this friend that will be callbacked with object and number
177 * when that friends gives us one of the TCP relays he is connected to. 177 * when that friends gives us one of the TCP relays he is connected to.
@@ -182,7 +182,7 @@ int onion_getfriendip(Onion_Client *onion_c, int friend_num, IP_Port *ip_port);
182 * return 0 on success. 182 * return 0 on success.
183 */ 183 */
184int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_relay_node_callback)(void *object, 184int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_relay_node_callback)(void *object,
185 uint32_t number, IP_Port ip_port, uint8_t *public_key), void *object, uint32_t number); 185 uint32_t number, IP_Port ip_port, const uint8_t *public_key), void *object, uint32_t number);
186 186
187/* Set a friends DHT public key. 187/* Set a friends DHT public key.
188 * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to 188 * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to
@@ -198,7 +198,7 @@ int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uin
198 * return 0 on failure (no key copied). 198 * return 0 on failure (no key copied).
199 * return timestamp on success (key copied). 199 * return timestamp on success (key copied).
200 */ 200 */
201uint64_t onion_getfriend_DHT_pubkey(Onion_Client *onion_c, int friend_num, uint8_t *dht_key); 201uint64_t onion_getfriend_DHT_pubkey(const Onion_Client *onion_c, int friend_num, uint8_t *dht_key);
202 202
203#define ONION_DATA_IN_RESPONSE_MIN_SIZE (crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES) 203#define ONION_DATA_IN_RESPONSE_MIN_SIZE (crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES)
204#define ONION_CLIENT_MAX_DATA_SIZE (MAX_DATA_REQUEST_SIZE - ONION_DATA_IN_RESPONSE_MIN_SIZE) 204#define ONION_CLIENT_MAX_DATA_SIZE (MAX_DATA_REQUEST_SIZE - ONION_DATA_IN_RESPONSE_MIN_SIZE)
diff --git a/toxcore/tox.c b/toxcore/tox.c
index 79e4c042..1b6597ba 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -38,9 +38,9 @@ typedef struct Messenger Tox;
38 * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] 38 * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
39 * 39 *
40 */ 40 */
41void tox_get_address(Tox *tox, uint8_t *address) 41void tox_get_address(const Tox *tox, uint8_t *address)
42{ 42{
43 Messenger *m = tox; 43 const Messenger *m = tox;
44 getaddress(m, address); 44 getaddress(m, address);
45} 45}
46 46
@@ -61,7 +61,7 @@ void tox_get_address(Tox *tox, uint8_t *address)
61 * (the nospam for that friend was set to the new one). 61 * (the nospam for that friend was set to the new one).
62 * return FAERR_NOMEM if increasing the friend list size fails. 62 * return FAERR_NOMEM if increasing the friend list size fails.
63 */ 63 */
64int32_t tox_add_friend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length) 64int32_t tox_add_friend(Tox *tox, const uint8_t *address, const uint8_t *data, uint16_t length)
65{ 65{
66 Messenger *m = tox; 66 Messenger *m = tox;
67 return m_addfriend(m, address, data, length); 67 return m_addfriend(m, address, data, length);
@@ -81,9 +81,9 @@ int32_t tox_add_friend_norequest(Tox *tox, const uint8_t *client_id)
81/* return the friend number associated to that client id. 81/* return the friend number associated to that client id.
82 * return -1 if no such friend. 82 * return -1 if no such friend.
83 */ 83 */
84int32_t tox_get_friend_number(Tox *tox, uint8_t *client_id) 84int32_t tox_get_friend_number(const Tox *tox, const uint8_t *client_id)
85{ 85{
86 Messenger *m = tox; 86 const Messenger *m = tox;
87 return getfriend_id(m, client_id); 87 return getfriend_id(m, client_id);
88} 88}
89 89
@@ -93,9 +93,9 @@ int32_t tox_get_friend_number(Tox *tox, uint8_t *client_id)
93 * return 0 if success. 93 * return 0 if success.
94 * return -1 if failure. 94 * return -1 if failure.
95 */ 95 */
96int tox_get_client_id(Tox *tox, int32_t friendnumber, uint8_t *client_id) 96int tox_get_client_id(const Tox *tox, int32_t friendnumber, uint8_t *client_id)
97{ 97{
98 Messenger *m = tox; 98 const Messenger *m = tox;
99 return getclient_id(m, friendnumber, client_id); 99 return getclient_id(m, friendnumber, client_id);
100} 100}
101 101
@@ -112,9 +112,9 @@ int tox_del_friend(Tox *tox, int32_t friendnumber)
112 * return 0 if friend is not connected to us (Offline). 112 * return 0 if friend is not connected to us (Offline).
113 * return -1 on failure. 113 * return -1 on failure.
114 */ 114 */
115int tox_get_friend_connection_status(Tox *tox, int32_t friendnumber) 115int tox_get_friend_connection_status(const Tox *tox, int32_t friendnumber)
116{ 116{
117 Messenger *m = tox; 117 const Messenger *m = tox;
118 return m_get_friend_connectionstatus(m, friendnumber); 118 return m_get_friend_connectionstatus(m, friendnumber);
119} 119}
120 120
@@ -123,9 +123,9 @@ int tox_get_friend_connection_status(Tox *tox, int32_t friendnumber)
123 * return 1 if friend exists. 123 * return 1 if friend exists.
124 * return 0 if friend doesn't exist. 124 * return 0 if friend doesn't exist.
125 */ 125 */
126int tox_friend_exists(Tox *tox, int32_t friendnumber) 126int tox_friend_exists(const Tox *tox, int32_t friendnumber)
127{ 127{
128 Messenger *m = tox; 128 const Messenger *m = tox;
129 return m_friend_exists(m, friendnumber); 129 return m_friend_exists(m, friendnumber);
130} 130}
131 131
@@ -144,7 +144,8 @@ uint32_t tox_send_message(Tox *tox, int32_t friendnumber, const uint8_t *message
144 return m_sendmessage(m, friendnumber, message, length); 144 return m_sendmessage(m, friendnumber, message, length);
145} 145}
146 146
147uint32_t tox_send_message_withid(Tox *tox, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length) 147uint32_t tox_send_message_withid(Tox *tox, int32_t friendnumber, uint32_t theid, const uint8_t *message,
148 uint32_t length)
148{ 149{
149 Messenger *m = tox; 150 Messenger *m = tox;
150 return m_sendmessage_withid(m, friendnumber, theid, message, length); 151 return m_sendmessage_withid(m, friendnumber, theid, message, length);
@@ -160,13 +161,13 @@ uint32_t tox_send_message_withid(Tox *tox, int32_t friendnumber, uint32_t theid,
160 * m_sendaction_withid will send an action message with the id of your choosing, 161 * m_sendaction_withid will send an action message with the id of your choosing,
161 * however we can generate an id for you by calling plain m_sendaction. 162 * however we can generate an id for you by calling plain m_sendaction.
162 */ 163 */
163uint32_t tox_send_action(Tox *tox, int32_t friendnumber, uint8_t *action, uint32_t length) 164uint32_t tox_send_action(Tox *tox, int32_t friendnumber, const uint8_t *action, uint32_t length)
164{ 165{
165 Messenger *m = tox; 166 Messenger *m = tox;
166 return m_sendaction(m, friendnumber, action, length); 167 return m_sendaction(m, friendnumber, action, length);
167} 168}
168 169
169uint32_t tox_send_action_withid(Tox *tox, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length) 170uint32_t tox_send_action_withid(Tox *tox, int32_t friendnumber, uint32_t theid, const uint8_t *action, uint32_t length)
170{ 171{
171 Messenger *m = tox; 172 Messenger *m = tox;
172 return m_sendaction_withid(m, friendnumber, theid, action, length); 173 return m_sendaction_withid(m, friendnumber, theid, action, length);
@@ -180,7 +181,7 @@ uint32_t tox_send_action_withid(Tox *tox, int32_t friendnumber, uint32_t theid,
180 * return 0 if success. 181 * return 0 if success.
181 * return -1 if failure. 182 * return -1 if failure.
182 */ 183 */
183int tox_set_name(Tox *tox, uint8_t *name, uint16_t length) 184int tox_set_name(Tox *tox, const uint8_t *name, uint16_t length)
184{ 185{
185 Messenger *m = tox; 186 Messenger *m = tox;
186 return setname(m, name, length); 187 return setname(m, name, length);
@@ -193,9 +194,9 @@ int tox_set_name(Tox *tox, uint8_t *name, uint16_t length)
193 * return length of the name. 194 * return length of the name.
194 * return 0 on error. 195 * return 0 on error.
195 */ 196 */
196uint16_t tox_get_self_name(Tox *tox, uint8_t *name) 197uint16_t tox_get_self_name(const Tox *tox, uint8_t *name)
197{ 198{
198 Messenger *m = tox; 199 const Messenger *m = tox;
199 return getself_name(m, name); 200 return getself_name(m, name);
200} 201}
201 202
@@ -205,24 +206,24 @@ uint16_t tox_get_self_name(Tox *tox, uint8_t *name)
205 * return length of name (with the NULL terminator) if success. 206 * return length of name (with the NULL terminator) if success.
206 * return -1 if failure. 207 * return -1 if failure.
207 */ 208 */
208int tox_get_name(Tox *tox, int32_t friendnumber, uint8_t *name) 209int tox_get_name(const Tox *tox, int32_t friendnumber, uint8_t *name)
209{ 210{
210 Messenger *m = tox; 211 const Messenger *m = tox;
211 return getname(m, friendnumber, name); 212 return getname(m, friendnumber, name);
212} 213}
213 214
214/* returns the length of name on success. 215/* returns the length of name on success.
215 * returns -1 on failure. 216 * returns -1 on failure.
216 */ 217 */
217int tox_get_name_size(Tox *tox, int32_t friendnumber) 218int tox_get_name_size(const Tox *tox, int32_t friendnumber)
218{ 219{
219 Messenger *m = tox; 220 const Messenger *m = tox;
220 return m_get_name_size(m, friendnumber); 221 return m_get_name_size(m, friendnumber);
221} 222}
222 223
223int tox_get_self_name_size(Tox *tox) 224int tox_get_self_name_size(const Tox *tox)
224{ 225{
225 Messenger *m = tox; 226 const Messenger *m = tox;
226 return m_get_self_name_size(m); 227 return m_get_self_name_size(m);
227} 228}
228 229
@@ -231,7 +232,7 @@ int tox_get_self_name_size(Tox *tox)
231 * 232 *
232 * return 0 on success, -1 on failure. 233 * return 0 on success, -1 on failure.
233 */ 234 */
234int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length) 235int tox_set_status_message(Tox *tox, const uint8_t *status, uint16_t length)
235{ 236{
236 Messenger *m = tox; 237 Messenger *m = tox;
237 return m_set_statusmessage(m, status, length); 238 return m_set_statusmessage(m, status, length);
@@ -246,15 +247,15 @@ int tox_set_user_status(Tox *tox, uint8_t status)
246/* returns the length of status message on success. 247/* returns the length of status message on success.
247 * returns -1 on failure. 248 * returns -1 on failure.
248 */ 249 */
249int tox_get_status_message_size(Tox *tox, int32_t friendnumber) 250int tox_get_status_message_size(const Tox *tox, int32_t friendnumber)
250{ 251{
251 Messenger *m = tox; 252 const Messenger *m = tox;
252 return m_get_statusmessage_size(m, friendnumber); 253 return m_get_statusmessage_size(m, friendnumber);
253} 254}
254 255
255int tox_get_self_status_message_size(Tox *tox) 256int tox_get_self_status_message_size(const Tox *tox)
256{ 257{
257 Messenger *m = tox; 258 const Messenger *m = tox;
258 return m_get_self_statusmessage_size(m); 259 return m_get_self_statusmessage_size(m);
259} 260}
260 261
@@ -262,15 +263,15 @@ int tox_get_self_status_message_size(Tox *tox)
262 * Get the size you need to allocate from m_get_statusmessage_size. 263 * Get the size you need to allocate from m_get_statusmessage_size.
263 * The self variant will copy our own status message. 264 * The self variant will copy our own status message.
264 */ 265 */
265int tox_get_status_message(Tox *tox, int32_t friendnumber, uint8_t *buf, uint32_t maxlen) 266int tox_get_status_message(const Tox *tox, int32_t friendnumber, uint8_t *buf, uint32_t maxlen)
266{ 267{
267 Messenger *m = tox; 268 const Messenger *m = tox;
268 return m_copy_statusmessage(m, friendnumber, buf, maxlen); 269 return m_copy_statusmessage(m, friendnumber, buf, maxlen);
269} 270}
270 271
271int tox_get_self_status_message(Tox *tox, uint8_t *buf, uint32_t maxlen) 272int tox_get_self_status_message(const Tox *tox, uint8_t *buf, uint32_t maxlen)
272{ 273{
273 Messenger *m = tox; 274 const Messenger *m = tox;
274 return m_copy_self_statusmessage(m, buf, maxlen); 275 return m_copy_self_statusmessage(m, buf, maxlen);
275} 276}
276 277
@@ -279,24 +280,24 @@ int tox_get_self_status_message(Tox *tox, uint8_t *buf, uint32_t maxlen)
279 * As above, the self variant will return our own USERSTATUS. 280 * As above, the self variant will return our own USERSTATUS.
280 * If friendnumber is invalid, this shall return USERSTATUS_INVALID. 281 * If friendnumber is invalid, this shall return USERSTATUS_INVALID.
281 */ 282 */
282uint8_t tox_get_user_status(Tox *tox, int32_t friendnumber) 283uint8_t tox_get_user_status(const Tox *tox, int32_t friendnumber)
283{ 284{
284 Messenger *m = tox; 285 const Messenger *m = tox;
285 return m_get_userstatus(m, friendnumber); 286 return m_get_userstatus(m, friendnumber);
286} 287}
287 288
288uint8_t tox_get_self_user_status(Tox *tox) 289uint8_t tox_get_self_user_status(const Tox *tox)
289{ 290{
290 Messenger *m = tox; 291 const Messenger *m = tox;
291 return m_get_self_userstatus(m); 292 return m_get_self_userstatus(m);
292} 293}
293 294
294/* returns timestamp of last time friendnumber was seen online, or 0 if never seen. 295/* returns timestamp of last time friendnumber was seen online, or 0 if never seen.
295 * returns -1 on error. 296 * returns -1 on error.
296 */ 297 */
297uint64_t tox_get_last_online(Tox *tox, int32_t friendnumber) 298uint64_t tox_get_last_online(const Tox *tox, int32_t friendnumber)
298{ 299{
299 Messenger *m = tox; 300 const Messenger *m = tox;
300 return m_get_last_online(m, friendnumber); 301 return m_get_last_online(m, friendnumber);
301} 302}
302 303
@@ -317,9 +318,9 @@ int tox_set_user_is_typing(Tox *tox, int32_t friendnumber, uint8_t is_typing)
317 * returns 0 if friend is not typing. 318 * returns 0 if friend is not typing.
318 * returns 1 if friend is typing. 319 * returns 1 if friend is typing.
319 */ 320 */
320uint8_t tox_get_is_typing(Tox *tox, int32_t friendnumber) 321uint8_t tox_get_is_typing(const Tox *tox, int32_t friendnumber)
321{ 322{
322 Messenger *m = tox; 323 const Messenger *m = tox;
323 return m_get_istyping(m, friendnumber); 324 return m_get_istyping(m, friendnumber);
324} 325}
325 326
@@ -335,16 +336,16 @@ void tox_set_sends_receipts(Tox *tox, int32_t friendnumber, int yesno)
335/* Return the number of friends in the instance m. 336/* Return the number of friends in the instance m.
336 * You should use this to determine how much memory to allocate 337 * You should use this to determine how much memory to allocate
337 * for copy_friendlist. */ 338 * for copy_friendlist. */
338uint32_t tox_count_friendlist(Tox *tox) 339uint32_t tox_count_friendlist(const Tox *tox)
339{ 340{
340 Messenger *m = tox; 341 const Messenger *m = tox;
341 return count_friendlist(m); 342 return count_friendlist(m);
342} 343}
343 344
344/* Return the number of online friends in the instance m. */ 345/* Return the number of online friends in the instance m. */
345uint32_t tox_get_num_online_friends(Tox *tox) 346uint32_t tox_get_num_online_friends(const Tox *tox)
346{ 347{
347 Messenger *m = tox; 348 const Messenger *m = tox;
348 return get_num_online_friends(m); 349 return get_num_online_friends(m);
349} 350}
350 351
@@ -353,9 +354,9 @@ uint32_t tox_get_num_online_friends(Tox *tox)
353 * Otherwise, returns the number of elements copied. 354 * Otherwise, returns the number of elements copied.
354 * If the array was too small, the contents 355 * If the array was too small, the contents
355 * of out_list will be truncated to list_size. */ 356 * of out_list will be truncated to list_size. */
356uint32_t tox_get_friendlist(Tox *tox, int32_t *out_list, uint32_t list_size) 357uint32_t tox_get_friendlist(const Tox *tox, int32_t *out_list, uint32_t list_size)
357{ 358{
358 Messenger *m = tox; 359 const Messenger *m = tox;
359 return copy_friendlist(m, out_list, list_size); 360 return copy_friendlist(m, out_list, list_size);
360} 361}
361 362
@@ -373,7 +374,7 @@ void tox_callback_friend_request(Tox *tox, void (*function)(Tox *tox, const uint
373/* Set the function that will be executed when a message from a friend is received. 374/* Set the function that will be executed when a message from a friend is received.
374 * Function format is: function(int32_t friendnumber, uint8_t * message, uint32_t length) 375 * Function format is: function(int32_t friendnumber, uint8_t * message, uint32_t length)
375 */ 376 */
376void tox_callback_friend_message(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, uint16_t, void *), 377void tox_callback_friend_message(Tox *tox, void (*function)(Messenger *tox, int32_t, const uint8_t *, uint16_t, void *),
377 void *userdata) 378 void *userdata)
378{ 379{
379 Messenger *m = tox; 380 Messenger *m = tox;
@@ -383,7 +384,7 @@ void tox_callback_friend_message(Tox *tox, void (*function)(Messenger *tox, int3
383/* Set the function that will be executed when an action from a friend is received. 384/* Set the function that will be executed when an action from a friend is received.
384 * function format is: function(int32_t friendnumber, uint8_t * action, uint32_t length) 385 * function format is: function(int32_t friendnumber, uint8_t * action, uint32_t length)
385 */ 386 */
386void tox_callback_friend_action(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, uint16_t, void *), 387void tox_callback_friend_action(Tox *tox, void (*function)(Messenger *tox, int32_t, const uint8_t *, uint16_t, void *),
387 void *userdata) 388 void *userdata)
388{ 389{
389 Messenger *m = tox; 390 Messenger *m = tox;
@@ -394,7 +395,7 @@ void tox_callback_friend_action(Tox *tox, void (*function)(Messenger *tox, int32
394 * function(int32_t friendnumber, uint8_t *newname, uint16_t length) 395 * function(int32_t friendnumber, uint8_t *newname, uint16_t length)
395 * You are not responsible for freeing newname. 396 * You are not responsible for freeing newname.
396 */ 397 */
397void tox_callback_name_change(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, uint16_t, void *), 398void tox_callback_name_change(Tox *tox, void (*function)(Messenger *tox, int32_t, const uint8_t *, uint16_t, void *),
398 void *userdata) 399 void *userdata)
399{ 400{
400 Messenger *m = tox; 401 Messenger *m = tox;
@@ -405,7 +406,7 @@ void tox_callback_name_change(Tox *tox, void (*function)(Messenger *tox, int32_t
405 * function(int32_t friendnumber, uint8_t *newstatus, uint16_t length) 406 * function(int32_t friendnumber, uint8_t *newstatus, uint16_t length)
406 * You are not responsible for freeing newstatus. 407 * You are not responsible for freeing newstatus.
407 */ 408 */
408void tox_callback_status_message(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, uint16_t, void *), 409void tox_callback_status_message(Tox *tox, void (*function)(Messenger *tox, int32_t, const uint8_t *, uint16_t, void *),
409 void *userdata) 410 void *userdata)
410{ 411{
411 Messenger *m = tox; 412 Messenger *m = tox;
@@ -468,9 +469,9 @@ void tox_callback_connection_status(Tox *tox, void (*function)(Messenger *tox, i
468 469
469/* Functions to get/set the nospam part of the id. 470/* Functions to get/set the nospam part of the id.
470 */ 471 */
471uint32_t tox_get_nospam(Tox *tox) 472uint32_t tox_get_nospam(const Tox *tox)
472{ 473{
473 Messenger *m = tox; 474 const Messenger *m = tox;
474 return get_nospam(&(m->fr)); 475 return get_nospam(&(m->fr));
475} 476}
476 477
@@ -486,7 +487,8 @@ void tox_set_nospam(Tox *tox, uint32_t nospam)
486 * 487 *
487 * Function(Tox *tox, int32_t friendnumber, uint8_t *group_public_key, void *userdata) 488 * Function(Tox *tox, int32_t friendnumber, uint8_t *group_public_key, void *userdata)
488 */ 489 */
489void tox_callback_group_invite(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t *, void *), void *userdata) 490void tox_callback_group_invite(Tox *tox, void (*function)(Messenger *tox, int32_t, const uint8_t *, void *),
491 void *userdata)
490{ 492{
491 Messenger *m = tox; 493 Messenger *m = tox;
492 m_callback_group_invite(m, function, userdata); 494 m_callback_group_invite(m, function, userdata);
@@ -496,7 +498,7 @@ void tox_callback_group_invite(Tox *tox, void (*function)(Messenger *tox, int32_
496 * 498 *
497 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) 499 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata)
498 */ 500 */
499void tox_callback_group_message(Tox *tox, void (*function)(Messenger *tox, int, int, uint8_t *, uint16_t, void *), 501void tox_callback_group_message(Tox *tox, void (*function)(Messenger *tox, int, int, const uint8_t *, uint16_t, void *),
500 void *userdata) 502 void *userdata)
501{ 503{
502 Messenger *m = tox; 504 Messenger *m = tox;
@@ -507,7 +509,7 @@ void tox_callback_group_message(Tox *tox, void (*function)(Messenger *tox, int,
507 * 509 *
508 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * action, uint16_t length, void *userdata) 510 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * action, uint16_t length, void *userdata)
509 */ 511 */
510void tox_callback_group_action(Tox *tox, void (*function)(Messenger *tox, int, int, uint8_t *, uint16_t, void *), 512void tox_callback_group_action(Tox *tox, void (*function)(Messenger *tox, int, int, const uint8_t *, uint16_t, void *),
511 void *userdata) 513 void *userdata)
512{ 514{
513 Messenger *m = tox; 515 Messenger *m = tox;
@@ -552,9 +554,9 @@ int tox_del_groupchat(Tox *tox, int groupnumber)
552 * return length of name if success 554 * return length of name if success
553 * return -1 if failure 555 * return -1 if failure
554 */ 556 */
555int tox_group_peername(Tox *tox, int groupnumber, int peernumber, uint8_t *name) 557int tox_group_peername(const Tox *tox, int groupnumber, int peernumber, uint8_t *name)
556{ 558{
557 Messenger *m = tox; 559 const Messenger *m = tox;
558 return m_group_peername(m, groupnumber, peernumber, name); 560 return m_group_peername(m, groupnumber, peernumber, name);
559} 561}
560/* invite friendnumber to groupnumber 562/* invite friendnumber to groupnumber
@@ -571,7 +573,7 @@ int tox_invite_friend(Tox *tox, int32_t friendnumber, int groupnumber)
571 * returns group number on success 573 * returns group number on success
572 * returns -1 on failure. 574 * returns -1 on failure.
573 */ 575 */
574int tox_join_groupchat(Tox *tox, int32_t friendnumber, uint8_t *friend_group_public_key) 576int tox_join_groupchat(Tox *tox, int32_t friendnumber, const uint8_t *friend_group_public_key)
575{ 577{
576 Messenger *m = tox; 578 Messenger *m = tox;
577 return join_groupchat(m, friendnumber, friend_group_public_key); 579 return join_groupchat(m, friendnumber, friend_group_public_key);
@@ -581,7 +583,7 @@ int tox_join_groupchat(Tox *tox, int32_t friendnumber, uint8_t *friend_group_pub
581 * return 0 on success 583 * return 0 on success
582 * return -1 on failure 584 * return -1 on failure
583 */ 585 */
584int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t length) 586int tox_group_message_send(Tox *tox, int groupnumber, const uint8_t *message, uint32_t length)
585{ 587{
586 Messenger *m = tox; 588 Messenger *m = tox;
587 return group_message_send(m, groupnumber, message, length); 589 return group_message_send(m, groupnumber, message, length);
@@ -591,7 +593,7 @@ int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t
591 * return 0 on success 593 * return 0 on success
592 * return -1 on failure 594 * return -1 on failure
593 */ 595 */
594int tox_group_action_send(Tox *tox, int groupnumber, uint8_t *action, uint32_t length) 596int tox_group_action_send(Tox *tox, int groupnumber, const uint8_t *action, uint32_t length)
595{ 597{
596 Messenger *m = tox; 598 Messenger *m = tox;
597 return group_action_send(m, groupnumber, action, length); 599 return group_action_send(m, groupnumber, action, length);
@@ -600,9 +602,9 @@ int tox_group_action_send(Tox *tox, int groupnumber, uint8_t *action, uint32_t l
600/* Return the number of peers in the group chat on success. 602/* Return the number of peers in the group chat on success.
601 * return -1 on failure 603 * return -1 on failure
602 */ 604 */
603int tox_group_number_peers(Tox *tox, int groupnumber) 605int tox_group_number_peers(const Tox *tox, int groupnumber)
604{ 606{
605 Messenger *m = tox; 607 const Messenger *m = tox;
606 return group_number_peers(m, groupnumber); 608 return group_number_peers(m, groupnumber);
607} 609}
608 610
@@ -616,19 +618,19 @@ int tox_group_number_peers(Tox *tox, int groupnumber)
616 * 618 *
617 * return -1 on failure. 619 * return -1 on failure.
618 */ 620 */
619int tox_group_get_names(Tox *tox, int groupnumber, uint8_t names[][TOX_MAX_NAME_LENGTH], uint16_t lengths[], 621int tox_group_get_names(const Tox *tox, int groupnumber, uint8_t names[][TOX_MAX_NAME_LENGTH], uint16_t lengths[],
620 uint16_t length) 622 uint16_t length)
621{ 623{
622 Messenger *m = tox; 624 const Messenger *m = tox;
623 return group_names(m, groupnumber, names, lengths, length); 625 return group_names(m, groupnumber, names, lengths, length);
624} 626}
625 627
626/* Return the number of chats in the instance m. 628/* Return the number of chats in the instance m.
627 * You should use this to determine how much memory to allocate 629 * You should use this to determine how much memory to allocate
628 * for copy_chatlist. */ 630 * for copy_chatlist. */
629uint32_t tox_count_chatlist(Tox *tox) 631uint32_t tox_count_chatlist(const Tox *tox)
630{ 632{
631 Messenger *m = tox; 633 const Messenger *m = tox;
632 return count_chatlist(m); 634 return count_chatlist(m);
633} 635}
634 636
@@ -637,9 +639,9 @@ uint32_t tox_count_chatlist(Tox *tox)
637 * Otherwise, returns the number of elements copied. 639 * Otherwise, returns the number of elements copied.
638 * If the array was too small, the contents 640 * If the array was too small, the contents
639 * of out_list will be truncated to list_size. */ 641 * of out_list will be truncated to list_size. */
640uint32_t tox_get_chatlist(Tox *tox, int *out_list, uint32_t list_size) 642uint32_t tox_get_chatlist(const Tox *tox, int *out_list, uint32_t list_size)
641{ 643{
642 Messenger *m = tox; 644 const Messenger *m = tox;
643 return copy_chatlist(m, out_list, list_size); 645 return copy_chatlist(m, out_list, list_size);
644} 646}
645 647
@@ -651,9 +653,8 @@ uint32_t tox_get_chatlist(Tox *tox, int *out_list, uint32_t list_size)
651 * 653 *
652 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) 654 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata)
653 */ 655 */
654void tox_callback_file_send_request(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, uint64_t, uint8_t *, 656void tox_callback_file_send_request(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, uint64_t,
655 uint16_t, 657 const uint8_t *, uint16_t, void *), void *userdata)
656 void *), void *userdata)
657{ 658{
658 Messenger *m = tox; 659 Messenger *m = tox;
659 callback_file_sendrequest(m, function, userdata); 660 callback_file_sendrequest(m, function, userdata);
@@ -663,8 +664,8 @@ void tox_callback_file_send_request(Tox *tox, void (*function)(Messenger *tox, i
663 * Function(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) 664 * Function(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
664 * 665 *
665 */ 666 */
666void tox_callback_file_control(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *, 667void tox_callback_file_control(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, uint8_t, uint8_t,
667 uint16_t, void *), void *userdata) 668 const uint8_t *, uint16_t, void *), void *userdata)
668{ 669{
669 Messenger *m = tox; 670 Messenger *m = tox;
670 callback_file_control(m, function, userdata); 671 callback_file_control(m, function, userdata);
@@ -674,9 +675,8 @@ void tox_callback_file_control(Tox *tox, void (*function)(Messenger *tox, int32_
674 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) 675 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata)
675 * 676 *
676 */ 677 */
677void tox_callback_file_data(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, uint8_t *, uint16_t length, 678void tox_callback_file_data(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, const uint8_t *,
678 void *), 679 uint16_t length, void *), void *userdata)
679 void *userdata)
680 680
681{ 681{
682 Messenger *m = tox; 682 Messenger *m = tox;
@@ -687,7 +687,8 @@ void tox_callback_file_data(Tox *tox, void (*function)(Messenger *tox, int32_t,
687 * return file number on success 687 * return file number on success
688 * return -1 on failure 688 * return -1 on failure
689 */ 689 */
690int tox_new_file_sender(Tox *tox, int32_t friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length) 690int tox_new_file_sender(Tox *tox, int32_t friendnumber, uint64_t filesize, const uint8_t *filename,
691 uint16_t filename_length)
691{ 692{
692 Messenger *m = tox; 693 Messenger *m = tox;
693 return new_filesender(m, friendnumber, filesize, filename, filename_length); 694 return new_filesender(m, friendnumber, filesize, filename, filename_length);
@@ -699,7 +700,7 @@ int tox_new_file_sender(Tox *tox, int32_t friendnumber, uint64_t filesize, uint8
699 * return -1 on failure 700 * return -1 on failure
700 */ 701 */
701int tox_file_send_control(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, 702int tox_file_send_control(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
702 uint8_t *data, uint16_t length) 703 const uint8_t *data, uint16_t length)
703{ 704{
704 Messenger *m = tox; 705 Messenger *m = tox;
705 return file_control(m, friendnumber, send_receive, filenumber, message_id, data, length); 706 return file_control(m, friendnumber, send_receive, filenumber, message_id, data, length);
@@ -709,7 +710,7 @@ int tox_file_send_control(Tox *tox, int32_t friendnumber, uint8_t send_receive,
709 * return 0 on success 710 * return 0 on success
710 * return -1 on failure 711 * return -1 on failure
711 */ 712 */
712int tox_file_send_data(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length) 713int tox_file_send_data(Tox *tox, int32_t friendnumber, uint8_t filenumber, const uint8_t *data, uint16_t length)
713{ 714{
714 Messenger *m = tox; 715 Messenger *m = tox;
715 return file_data(m, friendnumber, filenumber, data, length); 716 return file_data(m, friendnumber, filenumber, data, length);
@@ -720,7 +721,7 @@ int tox_file_send_data(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8
720 * return size on success 721 * return size on success
721 * return -1 on failure (currently will never return -1) 722 * return -1 on failure (currently will never return -1)
722 */ 723 */
723int tox_file_data_size(Tox *tox, int32_t friendnumber) 724int tox_file_data_size(const Tox *tox, int32_t friendnumber)
724{ 725{
725 return MAX_CRYPTO_DATA_SIZE - 2; 726 return MAX_CRYPTO_DATA_SIZE - 2;
726} 727}
@@ -732,16 +733,17 @@ int tox_file_data_size(Tox *tox, int32_t friendnumber)
732 * return number of bytes remaining to be sent/received on success 733 * return number of bytes remaining to be sent/received on success
733 * return 0 on failure 734 * return 0 on failure
734 */ 735 */
735uint64_t tox_file_data_remaining(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive) 736uint64_t tox_file_data_remaining(const Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive)
736{ 737{
737 Messenger *m = tox; 738 const Messenger *m = tox;
738 return file_dataremaining(m, friendnumber, filenumber, send_receive); 739 return file_dataremaining(m, friendnumber, filenumber, send_receive);
739} 740}
740 741
741/***************END OF FILE SENDING FUNCTIONS******************/ 742/***************END OF FILE SENDING FUNCTIONS******************/
742 743
743/* TODO: expose this properly. */ 744/* TODO: expose this properly. */
744static int tox_add_tcp_relay(Tox *tox, const char *address, uint8_t ipv6enabled, uint16_t port, uint8_t *public_key) 745static int tox_add_tcp_relay(Tox *tox, const char *address, uint8_t ipv6enabled, uint16_t port,
746 const uint8_t *public_key)
745{ 747{
746 Messenger *m = tox; 748 Messenger *m = tox;
747 IP_Port ip_port_v64; 749 IP_Port ip_port_v64;
@@ -766,7 +768,7 @@ static int tox_add_tcp_relay(Tox *tox, const char *address, uint8_t ipv6enabled,
766} 768}
767 769
768int tox_bootstrap_from_address(Tox *tox, const char *address, 770int tox_bootstrap_from_address(Tox *tox, const char *address,
769 uint8_t ipv6enabled, uint16_t port, uint8_t *public_key) 771 uint8_t ipv6enabled, uint16_t port, const uint8_t *public_key)
770{ 772{
771 Messenger *m = tox; 773 Messenger *m = tox;
772 tox_add_tcp_relay(tox, address, ipv6enabled, port, public_key); 774 tox_add_tcp_relay(tox, address, ipv6enabled, port, public_key);
@@ -776,9 +778,9 @@ int tox_bootstrap_from_address(Tox *tox, const char *address,
776/* return 0 if we are not connected to the DHT. 778/* return 0 if we are not connected to the DHT.
777 * return 1 if we are. 779 * return 1 if we are.
778 */ 780 */
779int tox_isconnected(Tox *tox) 781int tox_isconnected(const Tox *tox)
780{ 782{
781 Messenger *m = tox; 783 const Messenger *m = tox;
782 return DHT_isconnected(m->dht); 784 return DHT_isconnected(m->dht);
783} 785}
784 786
@@ -823,21 +825,21 @@ void tox_do(Tox *tox)
823/* SAVING AND LOADING FUNCTIONS: */ 825/* SAVING AND LOADING FUNCTIONS: */
824 826
825/* return size of the messenger data (for saving). */ 827/* return size of the messenger data (for saving). */
826uint32_t tox_size(Tox *tox) 828uint32_t tox_size(const Tox *tox)
827{ 829{
828 Messenger *m = tox; 830 const Messenger *m = tox;
829 return messenger_size(m); 831 return messenger_size(m);
830} 832}
831 833
832/* Save the messenger in data (must be allocated memory of size Messenger_size()). */ 834/* Save the messenger in data (must be allocated memory of size Messenger_size()). */
833void tox_save(Tox *tox, uint8_t *data) 835void tox_save(const Tox *tox, uint8_t *data)
834{ 836{
835 Messenger *m = tox; 837 const Messenger *m = tox;
836 messenger_save(m, data); 838 messenger_save(m, data);
837} 839}
838 840
839/* Load the messenger from data of size length. */ 841/* Load the messenger from data of size length. */
840int tox_load(Tox *tox, uint8_t *data, uint32_t length) 842int tox_load(Tox *tox, const uint8_t *data, uint32_t length)
841{ 843{
842 Messenger *m = tox; 844 Messenger *m = tox;
843 return messenger_load(m, data, length); 845 return messenger_load(m, data, length);
diff --git a/toxcore/tox.h b/toxcore/tox.h
index afa502f5..5418b03e 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -82,7 +82,7 @@ typedef struct Tox Tox;
82/* return TOX_FRIEND_ADDRESS_SIZE byte address to give to others. 82/* return TOX_FRIEND_ADDRESS_SIZE byte address to give to others.
83 * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] 83 * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
84 */ 84 */
85void tox_get_address(Tox *tox, uint8_t *address); 85void tox_get_address(const Tox *tox, uint8_t *address);
86 86
87/* Add a friend. 87/* Add a friend.
88 * Set the data that will be sent along with friend request. 88 * Set the data that will be sent along with friend request.
@@ -100,7 +100,7 @@ void tox_get_address(Tox *tox, uint8_t *address);
100 * (the nospam for that friend was set to the new one). 100 * (the nospam for that friend was set to the new one).
101 * return TOX_FAERR_NOMEM if increasing the friend list size fails. 101 * return TOX_FAERR_NOMEM if increasing the friend list size fails.
102 */ 102 */
103int32_t tox_add_friend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length); 103int32_t tox_add_friend(Tox *tox, const uint8_t *address, const uint8_t *data, uint16_t length);
104 104
105 105
106/* Add a friend without sending a friendrequest. 106/* Add a friend without sending a friendrequest.
@@ -111,14 +111,14 @@ int32_t tox_add_friend_norequest(Tox *tox, const uint8_t *client_id);
111 111
112/* return the friend number associated to that client id. 112/* return the friend number associated to that client id.
113 return -1 if no such friend */ 113 return -1 if no such friend */
114int32_t tox_get_friend_number(Tox *tox, uint8_t *client_id); 114int32_t tox_get_friend_number(const Tox *tox, const uint8_t *client_id);
115 115
116/* Copies the public key associated to that friend id into client_id buffer. 116/* Copies the public key associated to that friend id into client_id buffer.
117 * Make sure that client_id is of size CLIENT_ID_SIZE. 117 * Make sure that client_id is of size CLIENT_ID_SIZE.
118 * return 0 if success. 118 * return 0 if success.
119 * return -1 if failure. 119 * return -1 if failure.
120 */ 120 */
121int tox_get_client_id(Tox *tox, int32_t friendnumber, uint8_t *client_id); 121int tox_get_client_id(const Tox *tox, int32_t friendnumber, uint8_t *client_id);
122 122
123/* Remove a friend. 123/* Remove a friend.
124 * 124 *
@@ -133,14 +133,14 @@ int tox_del_friend(Tox *tox, int32_t friendnumber);
133 * return 0 if friend is not connected to us (Offline). 133 * return 0 if friend is not connected to us (Offline).
134 * return -1 on failure. 134 * return -1 on failure.
135 */ 135 */
136int tox_get_friend_connection_status(Tox *tox, int32_t friendnumber); 136int tox_get_friend_connection_status(const Tox *tox, int32_t friendnumber);
137 137
138/* Checks if there exists a friend with given friendnumber. 138/* Checks if there exists a friend with given friendnumber.
139 * 139 *
140 * return 1 if friend exists. 140 * return 1 if friend exists.
141 * return 0 if friend doesn't exist. 141 * return 0 if friend doesn't exist.
142 */ 142 */
143int tox_friend_exists(Tox *tox, int32_t friendnumber); 143int tox_friend_exists(const Tox *tox, int32_t friendnumber);
144 144
145/* Send a text chat message to an online friend. 145/* Send a text chat message to an online friend.
146 * 146 *
@@ -157,7 +157,8 @@ int tox_friend_exists(Tox *tox, int32_t friendnumber);
157 * however we can generate an id for you by calling plain m_sendmessage. 157 * however we can generate an id for you by calling plain m_sendmessage.
158 */ 158 */
159uint32_t tox_send_message(Tox *tox, int32_t friendnumber, const uint8_t *message, uint32_t length); 159uint32_t tox_send_message(Tox *tox, int32_t friendnumber, const uint8_t *message, uint32_t length);
160uint32_t tox_send_message_withid(Tox *tox, int32_t friendnumber, uint32_t theid, uint8_t *message, uint32_t length); 160uint32_t tox_send_message_withid(Tox *tox, int32_t friendnumber, uint32_t theid, const uint8_t *message,
161 uint32_t length);
161 162
162/* Send an action to an online friend. 163/* Send an action to an online friend.
163 * 164 *
@@ -173,8 +174,8 @@ uint32_t tox_send_message_withid(Tox *tox, int32_t friendnumber, uint32_t theid,
173 * m_sendaction_withid will send an action message with the id of your choosing, 174 * m_sendaction_withid will send an action message with the id of your choosing,
174 * however we can generate an id for you by calling plain m_sendaction. 175 * however we can generate an id for you by calling plain m_sendaction.
175 */ 176 */
176uint32_t tox_send_action(Tox *tox, int32_t friendnumber, uint8_t *action, uint32_t length); 177uint32_t tox_send_action(Tox *tox, int32_t friendnumber, const uint8_t *action, uint32_t length);
177uint32_t tox_send_action_withid(Tox *tox, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length); 178uint32_t tox_send_action_withid(Tox *tox, int32_t friendnumber, uint32_t theid, const uint8_t *action, uint32_t length);
178 179
179/* Set our nickname. 180/* Set our nickname.
180 * name must be a string of maximum MAX_NAME_LENGTH length. 181 * name must be a string of maximum MAX_NAME_LENGTH length.
@@ -184,7 +185,7 @@ uint32_t tox_send_action_withid(Tox *tox, int32_t friendnumber, uint32_t theid,
184 * return 0 if success. 185 * return 0 if success.
185 * return -1 if failure. 186 * return -1 if failure.
186 */ 187 */
187int tox_set_name(Tox *tox, uint8_t *name, uint16_t length); 188int tox_set_name(Tox *tox, const uint8_t *name, uint16_t length);
188 189
189/* 190/*
190 * Get your nickname. 191 * Get your nickname.
@@ -194,7 +195,7 @@ int tox_set_name(Tox *tox, uint8_t *name, uint16_t length);
194 * return length of name. 195 * return length of name.
195 * return 0 on error. 196 * return 0 on error.
196 */ 197 */
197uint16_t tox_get_self_name(Tox *tox, uint8_t *name); 198uint16_t tox_get_self_name(const Tox *tox, uint8_t *name);
198 199
199/* Get name of friendnumber and put it in name. 200/* Get name of friendnumber and put it in name.
200 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. 201 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
@@ -202,13 +203,13 @@ uint16_t tox_get_self_name(Tox *tox, uint8_t *name);
202 * return length of name if success. 203 * return length of name if success.
203 * return -1 if failure. 204 * return -1 if failure.
204 */ 205 */
205int tox_get_name(Tox *tox, int32_t friendnumber, uint8_t *name); 206int tox_get_name(const Tox *tox, int32_t friendnumber, uint8_t *name);
206 207
207/* returns the length of name on success. 208/* returns the length of name on success.
208 * returns -1 on failure. 209 * returns -1 on failure.
209 */ 210 */
210int tox_get_name_size(Tox *tox, int32_t friendnumber); 211int tox_get_name_size(const Tox *tox, int32_t friendnumber);
211int tox_get_self_name_size(Tox *tox); 212int tox_get_self_name_size(const Tox *tox);
212 213
213/* Set our user status. 214/* Set our user status.
214 * 215 *
@@ -218,14 +219,14 @@ int tox_get_self_name_size(Tox *tox);
218 * returns 0 on success. 219 * returns 0 on success.
219 * returns -1 on failure. 220 * returns -1 on failure.
220 */ 221 */
221int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length); 222int tox_set_status_message(Tox *tox, const uint8_t *status, uint16_t length);
222int tox_set_user_status(Tox *tox, uint8_t userstatus); 223int tox_set_user_status(Tox *tox, uint8_t userstatus);
223 224
224/* returns the length of status message on success. 225/* returns the length of status message on success.
225 * returns -1 on failure. 226 * returns -1 on failure.
226 */ 227 */
227int tox_get_status_message_size(Tox *tox, int32_t friendnumber); 228int tox_get_status_message_size(const Tox *tox, int32_t friendnumber);
228int tox_get_self_status_message_size(Tox *tox); 229int tox_get_self_status_message_size(const Tox *tox);
229 230
230/* Copy friendnumber's status message into buf, truncating if size is over maxlen. 231/* Copy friendnumber's status message into buf, truncating if size is over maxlen.
231 * Get the size you need to allocate from m_get_statusmessage_size. 232 * Get the size you need to allocate from m_get_statusmessage_size.
@@ -234,22 +235,22 @@ int tox_get_self_status_message_size(Tox *tox);
234 * returns the length of the copied data on success 235 * returns the length of the copied data on success
235 * retruns -1 on failure. 236 * retruns -1 on failure.
236 */ 237 */
237int tox_get_status_message(Tox *tox, int32_t friendnumber, uint8_t *buf, uint32_t maxlen); 238int tox_get_status_message(const Tox *tox, int32_t friendnumber, uint8_t *buf, uint32_t maxlen);
238int tox_get_self_status_message(Tox *tox, uint8_t *buf, uint32_t maxlen); 239int tox_get_self_status_message(const Tox *tox, uint8_t *buf, uint32_t maxlen);
239 240
240/* return one of TOX_USERSTATUS values. 241/* return one of TOX_USERSTATUS values.
241 * Values unknown to your application should be represented as TOX_USERSTATUS_NONE. 242 * Values unknown to your application should be represented as TOX_USERSTATUS_NONE.
242 * As above, the self variant will return our own TOX_USERSTATUS. 243 * As above, the self variant will return our own TOX_USERSTATUS.
243 * If friendnumber is invalid, this shall return TOX_USERSTATUS_INVALID. 244 * If friendnumber is invalid, this shall return TOX_USERSTATUS_INVALID.
244 */ 245 */
245uint8_t tox_get_user_status(Tox *tox, int32_t friendnumber); 246uint8_t tox_get_user_status(const Tox *tox, int32_t friendnumber);
246uint8_t tox_get_self_user_status(Tox *tox); 247uint8_t tox_get_self_user_status(const Tox *tox);
247 248
248 249
249/* returns timestamp of last time friendnumber was seen online, or 0 if never seen. 250/* returns timestamp of last time friendnumber was seen online, or 0 if never seen.
250 * returns -1 on error. 251 * returns -1 on error.
251 */ 252 */
252uint64_t tox_get_last_online(Tox *tox, int32_t friendnumber); 253uint64_t tox_get_last_online(const Tox *tox, int32_t friendnumber);
253 254
254/* Set our typing status for a friend. 255/* Set our typing status for a friend.
255 * You are responsible for turning it on or off. 256 * You are responsible for turning it on or off.
@@ -264,7 +265,7 @@ int tox_set_user_is_typing(Tox *tox, int32_t friendnumber, uint8_t is_typing);
264 * returns 0 if friend is not typing. 265 * returns 0 if friend is not typing.
265 * returns 1 if friend is typing. 266 * returns 1 if friend is typing.
266 */ 267 */
267uint8_t tox_get_is_typing(Tox *tox, int32_t friendnumber); 268uint8_t tox_get_is_typing(const Tox *tox, int32_t friendnumber);
268 269
269/* Sets whether we send read receipts for friendnumber. 270/* Sets whether we send read receipts for friendnumber.
270 * This function is not lazy, and it will fail if yesno is not (0 or 1). 271 * This function is not lazy, and it will fail if yesno is not (0 or 1).
@@ -274,17 +275,17 @@ void tox_set_sends_receipts(Tox *tox, int32_t friendnumber, int yesno);
274/* Return the number of friends in the instance m. 275/* Return the number of friends in the instance m.
275 * You should use this to determine how much memory to allocate 276 * You should use this to determine how much memory to allocate
276 * for copy_friendlist. */ 277 * for copy_friendlist. */
277uint32_t tox_count_friendlist(Tox *tox); 278uint32_t tox_count_friendlist(const Tox *tox);
278 279
279/* Return the number of online friends in the instance m. */ 280/* Return the number of online friends in the instance m. */
280uint32_t tox_get_num_online_friends(Tox *tox); 281uint32_t tox_get_num_online_friends(const Tox *tox);
281 282
282/* Copy a list of valid friend IDs into the array out_list. 283/* Copy a list of valid friend IDs into the array out_list.
283 * If out_list is NULL, returns 0. 284 * If out_list is NULL, returns 0.
284 * Otherwise, returns the number of elements copied. 285 * Otherwise, returns the number of elements copied.
285 * If the array was too small, the contents 286 * If the array was too small, the contents
286 * of out_list will be truncated to list_size. */ 287 * of out_list will be truncated to list_size. */
287uint32_t tox_get_friendlist(Tox *tox, int32_t *out_list, uint32_t list_size); 288uint32_t tox_get_friendlist(const Tox *tox, int32_t *out_list, uint32_t list_size);
288 289
289/* Set the function that will be executed when a friend request is received. 290/* Set the function that will be executed when a friend request is received.
290 * Function format is function(Tox *tox, uint8_t * public_key, uint8_t * data, uint16_t length, void *userdata) 291 * Function format is function(Tox *tox, uint8_t * public_key, uint8_t * data, uint16_t length, void *userdata)
@@ -295,27 +296,27 @@ void tox_callback_friend_request(Tox *tox, void (*function)(Tox *tox, const uint
295/* Set the function that will be executed when a message from a friend is received. 296/* Set the function that will be executed when a message from a friend is received.
296 * Function format is: function(Tox *tox, int32_t friendnumber, uint8_t * message, uint32_t length, void *userdata) 297 * Function format is: function(Tox *tox, int32_t friendnumber, uint8_t * message, uint32_t length, void *userdata)
297 */ 298 */
298void tox_callback_friend_message(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, uint16_t, void *), 299void tox_callback_friend_message(Tox *tox, void (*function)(Tox *tox, int32_t, const uint8_t *, uint16_t, void *),
299 void *userdata); 300 void *userdata);
300 301
301/* Set the function that will be executed when an action from a friend is received. 302/* Set the function that will be executed when an action from a friend is received.
302 * Function format is: function(Tox *tox, int32_t friendnumber, uint8_t * action, uint32_t length, void *userdata) 303 * Function format is: function(Tox *tox, int32_t friendnumber, uint8_t * action, uint32_t length, void *userdata)
303 */ 304 */
304void tox_callback_friend_action(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, uint16_t, void *), 305void tox_callback_friend_action(Tox *tox, void (*function)(Tox *tox, int32_t, const uint8_t *, uint16_t, void *),
305 void *userdata); 306 void *userdata);
306 307
307/* Set the callback for name changes. 308/* Set the callback for name changes.
308 * function(Tox *tox, int32_t friendnumber, uint8_t *newname, uint16_t length, void *userdata) 309 * function(Tox *tox, int32_t friendnumber, uint8_t *newname, uint16_t length, void *userdata)
309 * You are not responsible for freeing newname 310 * You are not responsible for freeing newname
310 */ 311 */
311void tox_callback_name_change(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, uint16_t, void *), 312void tox_callback_name_change(Tox *tox, void (*function)(Tox *tox, int32_t, const uint8_t *, uint16_t, void *),
312 void *userdata); 313 void *userdata);
313 314
314/* Set the callback for status message changes. 315/* Set the callback for status message changes.
315 * function(Tox *tox, int32_t friendnumber, uint8_t *newstatus, uint16_t length, void *userdata) 316 * function(Tox *tox, int32_t friendnumber, uint8_t *newstatus, uint16_t length, void *userdata)
316 * You are not responsible for freeing newstatus. 317 * You are not responsible for freeing newstatus.
317 */ 318 */
318void tox_callback_status_message(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, uint16_t, void *), 319void tox_callback_status_message(Tox *tox, void (*function)(Tox *tox, int32_t, const uint8_t *, uint16_t, void *),
319 void *userdata); 320 void *userdata);
320 321
321/* Set the callback for status type changes. 322/* Set the callback for status type changes.
@@ -357,7 +358,7 @@ void tox_callback_connection_status(Tox *tox, void (*function)(Tox *tox, int32_t
357 358
358/* Functions to get/set the nospam part of the id. 359/* Functions to get/set the nospam part of the id.
359 */ 360 */
360uint32_t tox_get_nospam(Tox *tox); 361uint32_t tox_get_nospam(const Tox *tox);
361void tox_set_nospam(Tox *tox, uint32_t nospam); 362void tox_set_nospam(Tox *tox, uint32_t nospam);
362 363
363 364
@@ -367,20 +368,20 @@ void tox_set_nospam(Tox *tox, uint32_t nospam);
367 * 368 *
368 * Function(Tox *tox, int friendnumber, uint8_t *group_public_key, void *userdata) 369 * Function(Tox *tox, int friendnumber, uint8_t *group_public_key, void *userdata)
369 */ 370 */
370void tox_callback_group_invite(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t *, void *), void *userdata); 371void tox_callback_group_invite(Tox *tox, void (*function)(Tox *tox, int32_t, const uint8_t *, void *), void *userdata);
371 372
372/* Set the callback for group messages. 373/* Set the callback for group messages.
373 * 374 *
374 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) 375 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata)
375 */ 376 */
376void tox_callback_group_message(Tox *tox, void (*function)(Tox *tox, int, int, uint8_t *, uint16_t, void *), 377void tox_callback_group_message(Tox *tox, void (*function)(Tox *tox, int, int, const uint8_t *, uint16_t, void *),
377 void *userdata); 378 void *userdata);
378 379
379/* Set the callback for group actions. 380/* Set the callback for group actions.
380 * 381 *
381 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * action, uint16_t length, void *userdata) 382 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * action, uint16_t length, void *userdata)
382 */ 383 */
383void tox_callback_group_action(Tox *tox, void (*function)(Tox *tox, int, int, uint8_t *, uint16_t, void *), 384void tox_callback_group_action(Tox *tox, void (*function)(Tox *tox, int, int, const uint8_t *, uint16_t, void *),
384 void *userdata); 385 void *userdata);
385 386
386/* Set callback function for peer name list changes. 387/* Set callback function for peer name list changes.
@@ -417,7 +418,7 @@ int tox_del_groupchat(Tox *tox, int groupnumber);
417 * return length of name if success 418 * return length of name if success
418 * return -1 if failure 419 * return -1 if failure
419 */ 420 */
420int tox_group_peername(Tox *tox, int groupnumber, int peernumber, uint8_t *name); 421int tox_group_peername(const Tox *tox, int groupnumber, int peernumber, uint8_t *name);
421 422
422/* invite friendnumber to groupnumber 423/* invite friendnumber to groupnumber
423 * return 0 on success 424 * return 0 on success
@@ -430,24 +431,24 @@ int tox_invite_friend(Tox *tox, int32_t friendnumber, int groupnumber);
430 * returns group number on success 431 * returns group number on success
431 * returns -1 on failure. 432 * returns -1 on failure.
432 */ 433 */
433int tox_join_groupchat(Tox *tox, int32_t friendnumber, uint8_t *friend_group_public_key); 434int tox_join_groupchat(Tox *tox, int32_t friendnumber, const uint8_t *friend_group_public_key);
434 435
435/* send a group message 436/* send a group message
436 * return 0 on success 437 * return 0 on success
437 * return -1 on failure 438 * return -1 on failure
438 */ 439 */
439int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t length); 440int tox_group_message_send(Tox *tox, int groupnumber, const uint8_t *message, uint32_t length);
440 441
441/* send a group action 442/* send a group action
442 * return 0 on success 443 * return 0 on success
443 * return -1 on failure 444 * return -1 on failure
444 */ 445 */
445int tox_group_action_send(Tox *tox, int groupnumber, uint8_t *action, uint32_t length); 446int tox_group_action_send(Tox *tox, int groupnumber, const uint8_t *action, uint32_t length);
446 447
447/* Return the number of peers in the group chat on success. 448/* Return the number of peers in the group chat on success.
448 * return -1 on failure 449 * return -1 on failure
449 */ 450 */
450int tox_group_number_peers(Tox *tox, int groupnumber); 451int tox_group_number_peers(const Tox *tox, int groupnumber);
451 452
452/* List all the peers in the group chat. 453/* List all the peers in the group chat.
453 * 454 *
@@ -459,20 +460,20 @@ int tox_group_number_peers(Tox *tox, int groupnumber);
459 * 460 *
460 * return -1 on failure. 461 * return -1 on failure.
461 */ 462 */
462int tox_group_get_names(Tox *tox, int groupnumber, uint8_t names[][TOX_MAX_NAME_LENGTH], uint16_t lengths[], 463int tox_group_get_names(const Tox *tox, int groupnumber, uint8_t names[][TOX_MAX_NAME_LENGTH], uint16_t lengths[],
463 uint16_t length); 464 uint16_t length);
464 465
465/* Return the number of chats in the instance m. 466/* Return the number of chats in the instance m.
466 * You should use this to determine how much memory to allocate 467 * You should use this to determine how much memory to allocate
467 * for copy_chatlist. */ 468 * for copy_chatlist. */
468uint32_t tox_count_chatlist(Tox *tox); 469uint32_t tox_count_chatlist(const Tox *tox);
469 470
470/* Copy a list of valid chat IDs into the array out_list. 471/* Copy a list of valid chat IDs into the array out_list.
471 * If out_list is NULL, returns 0. 472 * If out_list is NULL, returns 0.
472 * Otherwise, returns the number of elements copied. 473 * Otherwise, returns the number of elements copied.
473 * If the array was too small, the contents 474 * If the array was too small, the contents
474 * of out_list will be truncated to list_size. */ 475 * of out_list will be truncated to list_size. */
475uint32_t tox_get_chatlist(Tox *tox, int *out_list, uint32_t list_size); 476uint32_t tox_get_chatlist(const Tox *tox, int *out_list, uint32_t list_size);
476 477
477 478
478/****************FILE SENDING FUNCTIONS*****************/ 479/****************FILE SENDING FUNCTIONS*****************/
@@ -523,8 +524,8 @@ enum {
523 * 524 *
524 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) 525 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata)
525 */ 526 */
526void tox_callback_file_send_request(Tox *tox, void (*function)(Tox *m, int32_t, uint8_t, uint64_t, uint8_t *, uint16_t, 527void tox_callback_file_send_request(Tox *tox, void (*function)(Tox *m, int32_t, uint8_t, uint64_t, const uint8_t *,
527 void *), void *userdata); 528 uint16_t, void *), void *userdata);
528 529
529/* Set the callback for file control requests. 530/* Set the callback for file control requests.
530 * 531 *
@@ -534,7 +535,7 @@ void tox_callback_file_send_request(Tox *tox, void (*function)(Tox *m, int32_t,
534 * Function(Tox *tox, int32_t friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata) 535 * Function(Tox *tox, int32_t friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
535 * 536 *
536 */ 537 */
537void tox_callback_file_control(Tox *tox, void (*function)(Tox *m, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *, 538void tox_callback_file_control(Tox *tox, void (*function)(Tox *m, int32_t, uint8_t, uint8_t, uint8_t, const uint8_t *,
538 uint16_t, void *), void *userdata); 539 uint16_t, void *), void *userdata);
539 540
540/* Set the callback for file data. 541/* Set the callback for file data.
@@ -542,8 +543,8 @@ void tox_callback_file_control(Tox *tox, void (*function)(Tox *m, int32_t, uint8
542 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) 543 * Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata)
543 * 544 *
544 */ 545 */
545void tox_callback_file_data(Tox *tox, void (*function)(Tox *m, int32_t, uint8_t, uint8_t *, uint16_t length, void *), 546void tox_callback_file_data(Tox *tox, void (*function)(Tox *m, int32_t, uint8_t, const uint8_t *, uint16_t length,
546 void *userdata); 547 void *), void *userdata);
547 548
548 549
549/* Send a file send request. 550/* Send a file send request.
@@ -551,7 +552,8 @@ void tox_callback_file_data(Tox *tox, void (*function)(Tox *m, int32_t, uint8_t,
551 * return file number on success 552 * return file number on success
552 * return -1 on failure 553 * return -1 on failure
553 */ 554 */
554int tox_new_file_sender(Tox *tox, int32_t friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length); 555int tox_new_file_sender(Tox *tox, int32_t friendnumber, uint64_t filesize, const uint8_t *filename,
556 uint16_t filename_length);
555 557
556/* Send a file control request. 558/* Send a file control request.
557 * 559 *
@@ -562,21 +564,21 @@ int tox_new_file_sender(Tox *tox, int32_t friendnumber, uint64_t filesize, uint8
562 * return -1 on failure 564 * return -1 on failure
563 */ 565 */
564int tox_file_send_control(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id, 566int tox_file_send_control(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
565 uint8_t *data, uint16_t length); 567 const uint8_t *data, uint16_t length);
566 568
567/* Send file data. 569/* Send file data.
568 * 570 *
569 * return 0 on success 571 * return 0 on success
570 * return -1 on failure 572 * return -1 on failure
571 */ 573 */
572int tox_file_send_data(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length); 574int tox_file_send_data(Tox *tox, int32_t friendnumber, uint8_t filenumber, const uint8_t *data, uint16_t length);
573 575
574/* Returns the recommended/maximum size of the filedata you send with tox_file_send_data() 576/* Returns the recommended/maximum size of the filedata you send with tox_file_send_data()
575 * 577 *
576 * return size on success 578 * return size on success
577 * return -1 on failure (currently will never return -1) 579 * return -1 on failure (currently will never return -1)
578 */ 580 */
579int tox_file_data_size(Tox *tox, int32_t friendnumber); 581int tox_file_data_size(const Tox *tox, int32_t friendnumber);
580 582
581/* Give the number of bytes left to be sent/received. 583/* Give the number of bytes left to be sent/received.
582 * 584 *
@@ -585,7 +587,7 @@ int tox_file_data_size(Tox *tox, int32_t friendnumber);
585 * return number of bytes remaining to be sent/received on success 587 * return number of bytes remaining to be sent/received on success
586 * return 0 on failure 588 * return 0 on failure
587 */ 589 */
588uint64_t tox_file_data_remaining(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive); 590uint64_t tox_file_data_remaining(const Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive);
589 591
590/***************END OF FILE SENDING FUNCTIONS******************/ 592/***************END OF FILE SENDING FUNCTIONS******************/
591 593
@@ -606,12 +608,12 @@ uint64_t tox_file_data_remaining(Tox *tox, int32_t friendnumber, uint8_t filenum
606 * returns 0 otherwise 608 * returns 0 otherwise
607 */ 609 */
608int tox_bootstrap_from_address(Tox *tox, const char *address, uint8_t ipv6enabled, 610int tox_bootstrap_from_address(Tox *tox, const char *address, uint8_t ipv6enabled,
609 uint16_t port, uint8_t *public_key); 611 uint16_t port, const uint8_t *public_key);
610 612
611/* return 0 if we are not connected to the DHT. 613/* return 0 if we are not connected to the DHT.
612 * return 1 if we are. 614 * return 1 if we are.
613 */ 615 */
614int tox_isconnected(Tox *tox); 616int tox_isconnected(const Tox *tox);
615 617
616/* 618/*
617 * Run this function at startup. 619 * Run this function at startup.
@@ -645,17 +647,17 @@ void tox_do(Tox *tox);
645/* SAVING AND LOADING FUNCTIONS: */ 647/* SAVING AND LOADING FUNCTIONS: */
646 648
647/* return size of messenger data (for saving). */ 649/* return size of messenger data (for saving). */
648uint32_t tox_size(Tox *tox); 650uint32_t tox_size(const Tox *tox);
649 651
650/* Save the messenger in data (must be allocated memory of size Messenger_size()). */ 652/* Save the messenger in data (must be allocated memory of size Messenger_size()). */
651void tox_save(Tox *tox, uint8_t *data); 653void tox_save(const Tox *tox, uint8_t *data);
652 654
653/* Load the messenger from data of size length. 655/* Load the messenger from data of size length.
654 * 656 *
655 * returns 0 on success 657 * returns 0 on success
656 * returns -1 on failure 658 * returns -1 on failure
657 */ 659 */
658int tox_load(Tox *tox, uint8_t *data, uint32_t length); 660int tox_load(Tox *tox, const uint8_t *data, uint32_t length);
659 661
660#ifdef __cplusplus 662#ifdef __cplusplus
661} 663}