summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/Messenger.c16
-rw-r--r--toxcore/Messenger.h13
-rw-r--r--toxcore/network.c38
-rw-r--r--toxcore/network.h12
-rw-r--r--toxcore/tox.c133
-rw-r--r--toxcore/tox.h25
-rw-r--r--toxcore/util.c9
7 files changed, 184 insertions, 62 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index c8d69494..54ee6cce 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -26,6 +26,7 @@
26#endif 26#endif
27 27
28#include "Messenger.h" 28#include "Messenger.h"
29#include "network.h"
29#include "util.h" 30#include "util.h"
30 31
31#define MIN(a,b) (((a)<(b))?(a):(b)) 32#define MIN(a,b) (((a)<(b))?(a):(b))
@@ -1388,6 +1389,21 @@ void doMessenger(Messenger *m)
1388#endif 1389#endif
1389} 1390}
1390 1391
1392/*
1393 * Waits for something to happen on the socket for up to milliseconds milliseconds
1394 * *** Function MUSTN'T poll. ***
1395 * The function mustn't modify anything at all, so it can be called completely
1396 * asynchronously without any worry.
1397 *
1398 * returns 0 if the timeout was reached
1399 * returns 1 if there is socket activity (i.e. tox_do() should be called)
1400 *
1401 */
1402int waitMessenger(Messenger *m, uint16_t milliseconds)
1403{
1404 return networking_wait(m->net, milliseconds);
1405};
1406
1391/* return size of the messenger data (for saving) */ 1407/* return size of the messenger data (for saving) */
1392uint32_t Messenger_size_old(Messenger *m) 1408uint32_t Messenger_size_old(Messenger *m)
1393{ 1409{
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 0ff14de4..27e2c502 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -455,6 +455,18 @@ void cleanupMessenger(Messenger *M);
455/* The main loop that needs to be run at least 20 times per second. */ 455/* The main loop that needs to be run at least 20 times per second. */
456void doMessenger(Messenger *m); 456void doMessenger(Messenger *m);
457 457
458/*
459 * Waits for something to happen on the socket for up to milliseconds milliseconds
460 * *** Function MUSTN'T poll. ***
461 * The function mustn't modify anything at all, so it can be called completely
462 * asynchronously without any worry.
463 *
464 * returns 0 if the timeout was reached
465 * returns 1 if there is socket activity (i.e. tox_do() should be called)
466 *
467 */
468int waitMessenger(Messenger *m, uint16_t milliseconds);
469
458/* SAVING AND LOADING FUNCTIONS: */ 470/* SAVING AND LOADING FUNCTIONS: */
459 471
460/* return size of the messenger data (for saving). */ 472/* return size of the messenger data (for saving). */
@@ -487,3 +499,4 @@ uint32_t copy_friendlist(Messenger *m, int *out_list, uint32_t list_size);
487int get_friendlist(Messenger *m, int **out_list, uint32_t *out_list_length); 499int get_friendlist(Messenger *m, int **out_list, uint32_t *out_list_length);
488 500
489#endif 501#endif
502
diff --git a/toxcore/network.c b/toxcore/network.c
index 619248d8..5d308507 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -299,6 +299,44 @@ void networking_poll(Networking_Core *net)
299 } 299 }
300} 300}
301 301
302/*
303 * Waits for something to happen on the socket for up to milliseconds milliseconds
304 * *** Function MUSTN'T poll. ***
305 * The function mustn't modify anything at all, so it can be called completely
306 * asynchronously without any worry.
307 *
308 * returns 0 if the timeout was reached
309 * returns 1 if there is socket activity (i.e. tox_do() should be called)
310 *
311 */
312int networking_wait(Networking_Core *net, uint16_t milliseconds)
313{
314 /* WIN32: supported since Win2K, but might need some adjustements */
315 /* UNIX: this should work for any remotely Unix'ish system */
316 int nfds = 1 + net->sock;
317
318 /* the FD_ZERO calls might be superfluous */
319 fd_set readfds;
320 FD_ZERO(&readfds);
321 FD_SET(net->sock, &readfds);
322
323 fd_set writefds;
324 FD_ZERO(&writefds);
325 FD_SET(net->sock, &writefds);
326
327 fd_set exceptfds;
328 FD_ZERO(&exceptfds);
329 FD_SET(net->sock, &exceptfds);
330
331 struct timeval timeout;
332 timeout.tv_sec = 0;
333 timeout.tv_usec = milliseconds * 1000;
334
335 /* returns -1 on error, 0 on timeout, the socket on activity */
336 int res = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
337
338 return res > 0 ? 1 : 0;
339};
302 340
303uint8_t at_startup_ran = 0; 341uint8_t at_startup_ran = 0;
304static int at_startup(void) 342static int at_startup(void)
diff --git a/toxcore/network.h b/toxcore/network.h
index 0d2b5786..a7ae75ab 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -264,6 +264,18 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl
264/* Call this several times a second. */ 264/* Call this several times a second. */
265void networking_poll(Networking_Core *net); 265void networking_poll(Networking_Core *net);
266 266
267/*
268 * Waits for something to happen on the socket for up to milliseconds milliseconds
269 * *** Function MUSTN'T poll. ***
270 * The function mustn't modify anything at all, so it can be called completely
271 * asynchronously without any worry.
272 *
273 * returns 0 if the timeout was reached
274 * returns 1 if there is socket activity (i.e. tox_do() should be called)
275 *
276 */
277int networking_wait(Networking_Core *net, uint16_t milliseconds);
278
267/* Initialize networking. 279/* Initialize networking.
268 * bind to ip and port. 280 * bind to ip and port.
269 * ip must be in network order EX: 127.0.0.1 = (7F000001). 281 * ip must be in network order EX: 127.0.0.1 = (7F000001).
diff --git a/toxcore/tox.c b/toxcore/tox.c
index 1d7118be..080909d3 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -26,12 +26,18 @@
26#endif 26#endif
27 27
28#include "Messenger.h" 28#include "Messenger.h"
29
30#define __TOX_DEFINED__
31typedef struct Messenger Tox;
32
33#include "tox.h"
34
29/* 35/*
30 * returns a FRIEND_ADDRESS_SIZE byte address to give to others. 36 * returns a FRIEND_ADDRESS_SIZE byte address to give to others.
31 * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] 37 * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
32 * 38 *
33 */ 39 */
34void tox_getaddress(void *tox, uint8_t *address) 40void tox_getaddress(Tox *tox, uint8_t *address)
35{ 41{
36 Messenger *m = tox; 42 Messenger *m = tox;
37 getaddress(m, address); 43 getaddress(m, address);
@@ -54,7 +60,7 @@ void tox_getaddress(void *tox, uint8_t *address)
54 * (the nospam for that friend was set to the new one). 60 * (the nospam for that friend was set to the new one).
55 * return FAERR_NOMEM if increasing the friend list size fails. 61 * return FAERR_NOMEM if increasing the friend list size fails.
56 */ 62 */
57int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length) 63int tox_addfriend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length)
58{ 64{
59 Messenger *m = tox; 65 Messenger *m = tox;
60 return m_addfriend(m, address, data, length); 66 return m_addfriend(m, address, data, length);
@@ -65,7 +71,7 @@ int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length)
65 * return the friend number if success. 71 * return the friend number if success.
66 * return -1 if failure. 72 * return -1 if failure.
67 */ 73 */
68int tox_addfriend_norequest(void *tox, uint8_t *client_id) 74int tox_addfriend_norequest(Tox *tox, uint8_t *client_id)
69{ 75{
70 Messenger *m = tox; 76 Messenger *m = tox;
71 return m_addfriend_norequest(m, client_id); 77 return m_addfriend_norequest(m, client_id);
@@ -74,7 +80,7 @@ int tox_addfriend_norequest(void *tox, uint8_t *client_id)
74/* return the friend id associated to that client id. 80/* return the friend id associated to that client id.
75 * return -1 if no such friend. 81 * return -1 if no such friend.
76 */ 82 */
77int tox_getfriend_id(void *tox, uint8_t *client_id) 83int tox_getfriend_id(Tox *tox, uint8_t *client_id)
78{ 84{
79 Messenger *m = tox; 85 Messenger *m = tox;
80 return getfriend_id(m, client_id); 86 return getfriend_id(m, client_id);
@@ -86,14 +92,14 @@ int tox_getfriend_id(void *tox, uint8_t *client_id)
86 * return 0 if success. 92 * return 0 if success.
87 * return -1 if failure. 93 * return -1 if failure.
88 */ 94 */
89int tox_getclient_id(void *tox, int friend_id, uint8_t *client_id) 95int tox_getclient_id(Tox *tox, int friend_id, uint8_t *client_id)
90{ 96{
91 Messenger *m = tox; 97 Messenger *m = tox;
92 return getclient_id(m, friend_id, client_id); 98 return getclient_id(m, friend_id, client_id);
93} 99}
94 100
95/* Remove a friend. */ 101/* Remove a friend. */
96int tox_delfriend(void *tox, int friendnumber) 102int tox_delfriend(Tox *tox, int friendnumber)
97{ 103{
98 Messenger *m = tox; 104 Messenger *m = tox;
99 return m_delfriend(m, friendnumber); 105 return m_delfriend(m, friendnumber);
@@ -105,7 +111,7 @@ int tox_delfriend(void *tox, int friendnumber)
105 * return 0 if friend is not connected to us (Offline). 111 * return 0 if friend is not connected to us (Offline).
106 * return -1 on failure. 112 * return -1 on failure.
107 */ 113 */
108int tox_get_friend_connectionstatus(void *tox, int friendnumber) 114int tox_get_friend_connectionstatus(Tox *tox, int friendnumber)
109{ 115{
110 Messenger *m = tox; 116 Messenger *m = tox;
111 return m_get_friend_connectionstatus(m, friendnumber); 117 return m_get_friend_connectionstatus(m, friendnumber);
@@ -116,7 +122,7 @@ int tox_get_friend_connectionstatus(void *tox, int friendnumber)
116 * return 1 if friend exists. 122 * return 1 if friend exists.
117 * return 0 if friend doesn't exist. 123 * return 0 if friend doesn't exist.
118 */ 124 */
119int tox_friend_exists(void *tox, int friendnumber) 125int tox_friend_exists(Tox *tox, int friendnumber)
120{ 126{
121 Messenger *m = tox; 127 Messenger *m = tox;
122 return m_friend_exists(m, friendnumber); 128 return m_friend_exists(m, friendnumber);
@@ -131,13 +137,13 @@ int tox_friend_exists(void *tox, int friendnumber)
131 * m_sendmessage_withid will send a message with the id of your choosing, 137 * m_sendmessage_withid will send a message with the id of your choosing,
132 * however we can generate an id for you by calling plain m_sendmessage. 138 * however we can generate an id for you by calling plain m_sendmessage.
133 */ 139 */
134uint32_t tox_sendmessage(void *tox, int friendnumber, uint8_t *message, uint32_t length) 140uint32_t tox_sendmessage(Tox *tox, int friendnumber, uint8_t *message, uint32_t length)
135{ 141{
136 Messenger *m = tox; 142 Messenger *m = tox;
137 return m_sendmessage(m, friendnumber, message, length); 143 return m_sendmessage(m, friendnumber, message, length);
138} 144}
139 145
140uint32_t tox_sendmessage_withid(void *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) 146uint32_t tox_sendmessage_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length)
141{ 147{
142 Messenger *m = tox; 148 Messenger *m = tox;
143 return m_sendmessage_withid(m, friendnumber, theid, message, length); 149 return m_sendmessage_withid(m, friendnumber, theid, message, length);
@@ -147,7 +153,7 @@ uint32_t tox_sendmessage_withid(void *tox, int friendnumber, uint32_t theid, uin
147 * return 1 if packet was successfully put into the send queue. 153 * return 1 if packet was successfully put into the send queue.
148 * return 0 if it was not. 154 * return 0 if it was not.
149 */ 155 */
150int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length) 156int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length)
151{ 157{
152 Messenger *m = tox; 158 Messenger *m = tox;
153 return m_sendaction(m, friendnumber, action, length); 159 return m_sendaction(m, friendnumber, action, length);
@@ -161,7 +167,7 @@ int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length
161 * return 0 if success. 167 * return 0 if success.
162 * return -1 if failure. 168 * return -1 if failure.
163 */ 169 */
164int tox_setfriendname(void *tox, int friendnumber, uint8_t *name, uint16_t length) 170int tox_setfriendname(Tox *tox, int friendnumber, uint8_t *name, uint16_t length)
165{ 171{
166 Messenger *m = tox; 172 Messenger *m = tox;
167 return setfriendname(m, friendnumber, name, length); 173 return setfriendname(m, friendnumber, name, length);
@@ -175,7 +181,7 @@ int tox_setfriendname(void *tox, int friendnumber, uint8_t *name, uint16_t lengt
175 * return 0 if success. 181 * return 0 if success.
176 * return -1 if failure. 182 * return -1 if failure.
177 */ 183 */
178int tox_setname(void *tox, uint8_t *name, uint16_t length) 184int tox_setname(Tox *tox, uint8_t *name, uint16_t length)
179{ 185{
180 Messenger *m = tox; 186 Messenger *m = tox;
181 return setname(m, name, length); 187 return setname(m, name, length);
@@ -189,7 +195,7 @@ int tox_setname(void *tox, uint8_t *name, uint16_t length)
189 * return length of the name. 195 * return length of the name.
190 * return 0 on error. 196 * return 0 on error.
191 */ 197 */
192uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen) 198uint16_t tox_getselfname(Tox *tox, uint8_t *name, uint16_t nlen)
193{ 199{
194 Messenger *m = tox; 200 Messenger *m = tox;
195 return getself_name(m, name, nlen); 201 return getself_name(m, name, nlen);
@@ -201,7 +207,7 @@ uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen)
201 * return length of name (with the NULL terminator) if success. 207 * return length of name (with the NULL terminator) if success.
202 * return -1 if failure. 208 * return -1 if failure.
203 */ 209 */
204int tox_getname(void *tox, int friendnumber, uint8_t *name) 210int tox_getname(Tox *tox, int friendnumber, uint8_t *name)
205{ 211{
206 Messenger *m = tox; 212 Messenger *m = tox;
207 return getname(m, friendnumber, name); 213 return getname(m, friendnumber, name);
@@ -212,22 +218,22 @@ int tox_getname(void *tox, int friendnumber, uint8_t *name)
212 * 218 *
213 * return 0 on success, -1 on failure. 219 * return 0 on success, -1 on failure.
214 */ 220 */
215int tox_set_statusmessage(void *tox, uint8_t *status, uint16_t length) 221int tox_set_statusmessage(Tox *tox, uint8_t *status, uint16_t length)
216{ 222{
217 Messenger *m = tox; 223 Messenger *m = tox;
218 return m_set_statusmessage(m, status, length); 224 return m_set_statusmessage(m, status, length);
219} 225}
220 226
221int tox_set_userstatus(void *tox, USERSTATUS status) 227int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status)
222{ 228{
223 Messenger *m = tox; 229 Messenger *m = tox;
224 return m_set_userstatus(m, status); 230 return m_set_userstatus(m, (USERSTATUS)status);
225} 231}
226 232
227/* return the length of friendnumber's status message, including null. 233/* return the length of friendnumber's status message, including null.
228 * Pass it into malloc. 234 * Pass it into malloc.
229 */ 235 */
230int tox_get_statusmessage_size(void *tox, int friendnumber) 236int tox_get_statusmessage_size(Tox *tox, int friendnumber)
231{ 237{
232 Messenger *m = tox; 238 Messenger *m = tox;
233 return m_get_statusmessage_size(m, friendnumber); 239 return m_get_statusmessage_size(m, friendnumber);
@@ -237,13 +243,13 @@ int tox_get_statusmessage_size(void *tox, int friendnumber)
237 * Get the size you need to allocate from m_get_statusmessage_size. 243 * Get the size you need to allocate from m_get_statusmessage_size.
238 * The self variant will copy our own status message. 244 * The self variant will copy our own status message.
239 */ 245 */
240int tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen) 246int tox_copy_statusmessage(Tox *tox, int friendnumber, uint8_t *buf, uint32_t maxlen)
241{ 247{
242 Messenger *m = tox; 248 Messenger *m = tox;
243 return m_copy_statusmessage(m, friendnumber, buf, maxlen); 249 return m_copy_statusmessage(m, friendnumber, buf, maxlen);
244} 250}
245 251
246int tox_copy_self_statusmessage(void *tox, uint8_t *buf, uint32_t maxlen) 252int tox_copy_self_statusmessage(Tox *tox, uint8_t *buf, uint32_t maxlen)
247{ 253{
248 Messenger *m = tox; 254 Messenger *m = tox;
249 return m_copy_self_statusmessage(m, buf, maxlen); 255 return m_copy_self_statusmessage(m, buf, maxlen);
@@ -254,23 +260,23 @@ int tox_copy_self_statusmessage(void *tox, uint8_t *buf, uint32_t maxlen)
254 * As above, the self variant will return our own USERSTATUS. 260 * As above, the self variant will return our own USERSTATUS.
255 * If friendnumber is invalid, this shall return USERSTATUS_INVALID. 261 * If friendnumber is invalid, this shall return USERSTATUS_INVALID.
256 */ 262 */
257USERSTATUS tox_get_userstatus(void *tox, int friendnumber) 263TOX_USERSTATUS tox_get_userstatus(Tox *tox, int friendnumber)
258{ 264{
259 Messenger *m = tox; 265 Messenger *m = tox;
260 return m_get_userstatus(m, friendnumber); 266 return (TOX_USERSTATUS)m_get_userstatus(m, friendnumber);
261} 267}
262 268
263USERSTATUS tox_get_selfuserstatus(void *tox) 269TOX_USERSTATUS tox_get_selfuserstatus(Tox *tox)
264{ 270{
265 Messenger *m = tox; 271 Messenger *m = tox;
266 return m_get_self_userstatus(m); 272 return (TOX_USERSTATUS)m_get_self_userstatus(m);
267} 273}
268 274
269 275
270/* Sets whether we send read receipts for friendnumber. 276/* Sets whether we send read receipts for friendnumber.
271 * This function is not lazy, and it will fail if yesno is not (0 or 1). 277 * This function is not lazy, and it will fail if yesno is not (0 or 1).
272 */ 278 */
273void tox_set_sends_receipts(void *tox, int friendnumber, int yesno) 279void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno)
274{ 280{
275 Messenger *m = tox; 281 Messenger *m = tox;
276 m_set_sends_receipts(m, friendnumber, yesno); 282 m_set_sends_receipts(m, friendnumber, yesno);
@@ -279,7 +285,7 @@ void tox_set_sends_receipts(void *tox, int friendnumber, int yesno)
279/* Return the number of friends in the instance m. 285/* Return the number of friends in the instance m.
280 * You should use this to determine how much memory to allocate 286 * You should use this to determine how much memory to allocate
281 * for copy_friendlist. */ 287 * for copy_friendlist. */
282uint32_t tox_count_friendlist(void *tox) 288uint32_t tox_count_friendlist(Tox *tox)
283{ 289{
284 Messenger *m = tox; 290 Messenger *m = tox;
285 return count_friendlist(m); 291 return count_friendlist(m);
@@ -290,7 +296,7 @@ uint32_t tox_count_friendlist(void *tox)
290 * Otherwise, returns the number of elements copied. 296 * Otherwise, returns the number of elements copied.
291 * If the array was too small, the contents 297 * If the array was too small, the contents
292 * of out_list will be truncated to list_size. */ 298 * of out_list will be truncated to list_size. */
293uint32_t tox_copy_friendlist(void *tox, int *out_list, uint32_t list_size) 299uint32_t tox_copy_friendlist(Tox *tox, int *out_list, uint32_t list_size)
294{ 300{
295 Messenger *m = tox; 301 Messenger *m = tox;
296 return copy_friendlist(m, out_list, list_size); 302 return copy_friendlist(m, out_list, list_size);
@@ -299,7 +305,7 @@ uint32_t tox_copy_friendlist(void *tox, int *out_list, uint32_t list_size)
299/* Set the function that will be executed when a friend request is received. 305/* Set the function that will be executed when a friend request is received.
300 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) 306 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length)
301 */ 307 */
302void tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) 308void tox_callback_friendrequest(Tox *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata)
303{ 309{
304 Messenger *m = tox; 310 Messenger *m = tox;
305 m_callback_friendrequest(m, function, userdata); 311 m_callback_friendrequest(m, function, userdata);
@@ -309,7 +315,7 @@ void tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t *
309/* Set the function that will be executed when a message from a friend is received. 315/* Set the function that will be executed when a message from a friend is received.
310 * Function format is: function(int friendnumber, uint8_t * message, uint32_t length) 316 * Function format is: function(int friendnumber, uint8_t * message, uint32_t length)
311 */ 317 */
312void tox_callback_friendmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), 318void tox_callback_friendmessage(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *),
313 void *userdata) 319 void *userdata)
314{ 320{
315 Messenger *m = tox; 321 Messenger *m = tox;
@@ -319,7 +325,7 @@ void tox_callback_friendmessage(void *tox, void (*function)(Messenger *tox, int,
319/* Set the function that will be executed when an action from a friend is received. 325/* Set the function that will be executed when an action from a friend is received.
320 * function format is: function(int friendnumber, uint8_t * action, uint32_t length) 326 * function format is: function(int friendnumber, uint8_t * action, uint32_t length)
321 */ 327 */
322void tox_callback_action(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), void *userdata) 328void tox_callback_action(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), void *userdata)
323{ 329{
324 Messenger *m = tox; 330 Messenger *m = tox;
325 m_callback_action(m, function, userdata); 331 m_callback_action(m, function, userdata);
@@ -329,7 +335,7 @@ void tox_callback_action(void *tox, void (*function)(Messenger *tox, int, uint8_
329 * function(int friendnumber, uint8_t *newname, uint16_t length) 335 * function(int friendnumber, uint8_t *newname, uint16_t length)
330 * You are not responsible for freeing newname. 336 * You are not responsible for freeing newname.
331 */ 337 */
332void tox_callback_namechange(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), 338void tox_callback_namechange(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *),
333 void *userdata) 339 void *userdata)
334{ 340{
335 Messenger *m = tox; 341 Messenger *m = tox;
@@ -340,7 +346,7 @@ void tox_callback_namechange(void *tox, void (*function)(Messenger *tox, int, ui
340 * function(int friendnumber, uint8_t *newstatus, uint16_t length) 346 * function(int friendnumber, uint8_t *newstatus, uint16_t length)
341 * You are not responsible for freeing newstatus. 347 * You are not responsible for freeing newstatus.
342 */ 348 */
343void tox_callback_statusmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), 349void tox_callback_statusmessage(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *),
344 void *userdata) 350 void *userdata)
345{ 351{
346 Messenger *m = tox; 352 Messenger *m = tox;
@@ -350,9 +356,11 @@ void tox_callback_statusmessage(void *tox, void (*function)(Messenger *tox, int,
350/* Set the callback for status type changes. 356/* Set the callback for status type changes.
351 * function(int friendnumber, USERSTATUS kind) 357 * function(int friendnumber, USERSTATUS kind)
352 */ 358 */
353void tox_callback_userstatus(void *tox, void (*function)(Messenger *tox, int, USERSTATUS, void *), void *userdata) 359void tox_callback_userstatus(Tox *tox, void (*_function)(Tox *tox, int, TOX_USERSTATUS, void *), void *userdata)
354{ 360{
355 Messenger *m = tox; 361 Messenger *m = tox;
362 typedef void (*function_type)(Messenger *, int, USERSTATUS, void *);
363 function_type function = (function_type)_function;
356 m_callback_userstatus(m, function, userdata); 364 m_callback_userstatus(m, function, userdata);
357} 365}
358 366
@@ -365,7 +373,7 @@ void tox_callback_userstatus(void *tox, void (*function)(Messenger *tox, int, US
365 * Since core doesn't track ids for you, receipt may not correspond to any message. 373 * Since core doesn't track ids for you, receipt may not correspond to any message.
366 * in that case, you should discard it. 374 * in that case, you should discard it.
367 */ 375 */
368void tox_callback_read_receipt(void *tox, void (*function)(Messenger *tox, int, uint32_t, void *), void *userdata) 376void tox_callback_read_receipt(Tox *tox, void (*function)(Messenger *tox, int, uint32_t, void *), void *userdata)
369{ 377{
370 Messenger *m = tox; 378 Messenger *m = tox;
371 m_callback_read_receipt(m, function, userdata); 379 m_callback_read_receipt(m, function, userdata);
@@ -382,7 +390,7 @@ void tox_callback_read_receipt(void *tox, void (*function)(Messenger *tox, int,
382 * being previously online" part. It's assumed that when adding friends, 390 * being previously online" part. It's assumed that when adding friends,
383 * their connection status is offline. 391 * their connection status is offline.
384 */ 392 */
385void tox_callback_connectionstatus(void *tox, void (*function)(Messenger *tox, int, uint8_t, void *), void *userdata) 393void tox_callback_connectionstatus(Tox *tox, void (*function)(Messenger *tox, int, uint8_t, void *), void *userdata)
386{ 394{
387 Messenger *m = tox; 395 Messenger *m = tox;
388 m_callback_connectionstatus(m, function, userdata); 396 m_callback_connectionstatus(m, function, userdata);
@@ -394,7 +402,7 @@ void tox_callback_connectionstatus(void *tox, void (*function)(Messenger *tox, i
394 * 402 *
395 * Function(Tox *tox, int friendnumber, uint8_t *group_public_key, void *userdata) 403 * Function(Tox *tox, int friendnumber, uint8_t *group_public_key, void *userdata)
396 */ 404 */
397void tox_callback_group_invite(void *tox, void (*function)(Messenger *tox, int, uint8_t *, void *), void *userdata) 405void tox_callback_group_invite(Tox *tox, void (*function)(Messenger *tox, int, uint8_t *, void *), void *userdata)
398{ 406{
399 Messenger *m = tox; 407 Messenger *m = tox;
400 m_callback_group_invite(m, function, userdata); 408 m_callback_group_invite(m, function, userdata);
@@ -403,7 +411,7 @@ void tox_callback_group_invite(void *tox, void (*function)(Messenger *tox, int,
403 * 411 *
404 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) 412 * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata)
405 */ 413 */
406void tox_callback_group_message(void *tox, void (*function)(Messenger *tox, int, int, uint8_t *, uint16_t, void *), 414void tox_callback_group_message(Tox *tox, void (*function)(Messenger *tox, int, int, uint8_t *, uint16_t, void *),
407 void *userdata) 415 void *userdata)
408{ 416{
409 Messenger *m = tox; 417 Messenger *m = tox;
@@ -414,7 +422,7 @@ void tox_callback_group_message(void *tox, void (*function)(Messenger *tox, int,
414 * return group number on success. 422 * return group number on success.
415 * return -1 on failure. 423 * return -1 on failure.
416 */ 424 */
417int tox_add_groupchat(void *tox) 425int tox_add_groupchat(Tox *tox)
418{ 426{
419 Messenger *m = tox; 427 Messenger *m = tox;
420 return add_groupchat(m); 428 return add_groupchat(m);
@@ -424,7 +432,7 @@ int tox_add_groupchat(void *tox)
424 * return 0 on success. 432 * return 0 on success.
425 * return -1 if failure. 433 * return -1 if failure.
426 */ 434 */
427int tox_del_groupchat(void *tox, int groupnumber) 435int tox_del_groupchat(Tox *tox, int groupnumber)
428{ 436{
429 Messenger *m = tox; 437 Messenger *m = tox;
430 return del_groupchat(m, groupnumber); 438 return del_groupchat(m, groupnumber);
@@ -436,7 +444,7 @@ int tox_del_groupchat(void *tox, int groupnumber)
436 * return length of name if success 444 * return length of name if success
437 * return -1 if failure 445 * return -1 if failure
438 */ 446 */
439int tox_group_peername(void *tox, int groupnumber, int peernumber, uint8_t *name) 447int tox_group_peername(Tox *tox, int groupnumber, int peernumber, uint8_t *name)
440{ 448{
441 Messenger *m = tox; 449 Messenger *m = tox;
442 return m_group_peername(m, groupnumber, peernumber, name); 450 return m_group_peername(m, groupnumber, peernumber, name);
@@ -445,7 +453,7 @@ int tox_group_peername(void *tox, int groupnumber, int peernumber, uint8_t *name
445 * return 0 on success 453 * return 0 on success
446 * return -1 on failure 454 * return -1 on failure
447 */ 455 */
448int tox_invite_friend(void *tox, int friendnumber, int groupnumber) 456int tox_invite_friend(Tox *tox, int friendnumber, int groupnumber)
449{ 457{
450 Messenger *m = tox; 458 Messenger *m = tox;
451 return invite_friend(m, friendnumber, groupnumber); 459 return invite_friend(m, friendnumber, groupnumber);
@@ -455,7 +463,7 @@ int tox_invite_friend(void *tox, int friendnumber, int groupnumber)
455 * returns group number on success 463 * returns group number on success
456 * returns -1 on failure. 464 * returns -1 on failure.
457 */ 465 */
458int tox_join_groupchat(void *tox, int friendnumber, uint8_t *friend_group_public_key) 466int tox_join_groupchat(Tox *tox, int friendnumber, uint8_t *friend_group_public_key)
459{ 467{
460 Messenger *m = tox; 468 Messenger *m = tox;
461 return join_groupchat(m, friendnumber, friend_group_public_key); 469 return join_groupchat(m, friendnumber, friend_group_public_key);
@@ -465,7 +473,7 @@ int tox_join_groupchat(void *tox, int friendnumber, uint8_t *friend_group_public
465 * return 0 on success 473 * return 0 on success
466 * return -1 on failure 474 * return -1 on failure
467 */ 475 */
468int tox_group_message_send(void *tox, int groupnumber, uint8_t *message, uint32_t length) 476int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t length)
469{ 477{
470 Messenger *m = tox; 478 Messenger *m = tox;
471 return group_message_send(m, groupnumber, message, length); 479 return group_message_send(m, groupnumber, message, length);
@@ -478,12 +486,15 @@ int tox_group_message_send(void *tox, int groupnumber, uint8_t *message, uint32_
478/* Use these functions to bootstrap the client. 486/* Use these functions to bootstrap the client.
479 * Sends a get nodes request to the given node with ip port and public_key. 487 * Sends a get nodes request to the given node with ip port and public_key.
480 */ 488 */
481void tox_bootstrap_from_ip(void *tox, IP_Port ip_port, uint8_t *public_key) 489void tox_bootstrap_from_ip(Tox *tox, tox_IP_Port _ip_port, uint8_t *public_key)
482{ 490{
483 Messenger *m = tox; 491 Messenger *m = tox;
492 IP_Port ip_port;
493 memcpy(&ip_port, &_ip_port, sizeof(IP_Port));
484 DHT_bootstrap(m->dht, ip_port, public_key); 494 DHT_bootstrap(m->dht, ip_port, public_key);
485} 495}
486int tox_bootstrap_from_address(void *tox, const char *address, 496
497int tox_bootstrap_from_address(Tox *tox, const char *address,
487 uint8_t ipv6enabled, uint16_t port, uint8_t *public_key) 498 uint8_t ipv6enabled, uint16_t port, uint8_t *public_key)
488{ 499{
489 Messenger *m = tox; 500 Messenger *m = tox;
@@ -493,7 +504,7 @@ int tox_bootstrap_from_address(void *tox, const char *address,
493/* return 0 if we are not connected to the DHT. 504/* return 0 if we are not connected to the DHT.
494 * return 1 if we are. 505 * return 1 if we are.
495 */ 506 */
496int tox_isconnected(void *tox) 507int tox_isconnected(Tox *tox)
497{ 508{
498 Messenger *m = tox; 509 Messenger *m = tox;
499 return DHT_isconnected(m->dht); 510 return DHT_isconnected(m->dht);
@@ -504,7 +515,7 @@ int tox_isconnected(void *tox)
504 * return allocated instance of tox on success. 515 * return allocated instance of tox on success.
505 * return 0 if there are problems. 516 * return 0 if there are problems.
506 */ 517 */
507void *tox_new(uint8_t ipv6enabled) 518Tox *tox_new(uint8_t ipv6enabled)
508{ 519{
509 return initMessenger(ipv6enabled); 520 return initMessenger(ipv6enabled);
510} 521}
@@ -512,37 +523,53 @@ void *tox_new(uint8_t ipv6enabled)
512/* Run this before closing shop. 523/* Run this before closing shop.
513 * Free all datastructures. 524 * Free all datastructures.
514 */ 525 */
515void tox_kill(void *tox) 526void tox_kill(Tox *tox)
516{ 527{
517 Messenger *m = tox; 528 Messenger *m = tox;
518 cleanupMessenger(m); 529 cleanupMessenger(m);
519} 530}
520 531
521/* The main loop that needs to be run at least 20 times per second. */ 532/* The main loop that needs to be run at least 20 times per second. */
522void tox_do(void *tox) 533void tox_do(Tox *tox)
523{ 534{
524 Messenger *m = tox; 535 Messenger *m = tox;
525 doMessenger(m); 536 doMessenger(m);
526} 537}
527 538
539/*
540 * Waits for something to happen on the socket for up to milliseconds milliseconds
541 * *** Function MUSTN'T poll. ***
542 * The function mustn't modify anything at all, so it can be called completely
543 * asynchronously without any worry.
544 *
545 * returns 0 if the timeout was reached
546 * returns 1 if there is socket activity (i.e. tox_do() should be called)
547 *
548 */
549int tox_wait(Tox *tox, uint16_t milliseconds)
550{
551 Messenger *m = tox;
552 waitMessenger(m, milliseconds);
553}
554
528/* SAVING AND LOADING FUNCTIONS: */ 555/* SAVING AND LOADING FUNCTIONS: */
529 556
530/* return size of the messenger data (for saving). */ 557/* return size of the messenger data (for saving). */
531uint32_t tox_size(void *tox) 558uint32_t tox_size(Tox *tox)
532{ 559{
533 Messenger *m = tox; 560 Messenger *m = tox;
534 return Messenger_size(m); 561 return Messenger_size(m);
535} 562}
536 563
537/* Save the messenger in data (must be allocated memory of size Messenger_size()). */ 564/* Save the messenger in data (must be allocated memory of size Messenger_size()). */
538void tox_save(void *tox, uint8_t *data) 565void tox_save(Tox *tox, uint8_t *data)
539{ 566{
540 Messenger *m = tox; 567 Messenger *m = tox;
541 Messenger_save(m, data); 568 Messenger_save(m, data);
542} 569}
543 570
544/* Load the messenger from data of size length. */ 571/* Load the messenger from data of size length. */
545int tox_load(void *tox, uint8_t *data, uint32_t length) 572int tox_load(Tox *tox, uint8_t *data, uint32_t length)
546{ 573{
547 Messenger *m = tox; 574 Messenger *m = tox;
548 return Messenger_load(m, data, length); 575 return Messenger_load(m, data, length);
diff --git a/toxcore/tox.h b/toxcore/tox.h
index eabbb1af..e497e6f5 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -140,7 +140,10 @@ typedef enum {
140} 140}
141TOX_USERSTATUS; 141TOX_USERSTATUS;
142 142
143typedef void Tox; 143#ifndef __TOX_DEFINED__
144#define __TOX_DEFINED__
145typedef struct Tox Tox;
146#endif
144 147
145/* return FRIEND_ADDRESS_SIZE byte address to give to others. 148/* return FRIEND_ADDRESS_SIZE byte address to give to others.
146 * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] 149 * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
@@ -229,7 +232,7 @@ int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length)
229 * return 0 if success. 232 * return 0 if success.
230 * return -1 if failure. 233 * return -1 if failure.
231 */ 234 */
232int tox_setfriendname(void *tox, int friendnumber, uint8_t *name, uint16_t length); 235int tox_setfriendname(Tox *tox, int friendnumber, uint8_t *name, uint16_t length);
233 236
234/* Set our nickname. 237/* Set our nickname.
235 * name must be a string of maximum MAX_NAME_LENGTH length. 238 * name must be a string of maximum MAX_NAME_LENGTH length.
@@ -300,14 +303,14 @@ void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno);
300/* Return the number of friends in the instance m. 303/* Return the number of friends in the instance m.
301 * You should use this to determine how much memory to allocate 304 * You should use this to determine how much memory to allocate
302 * for copy_friendlist. */ 305 * for copy_friendlist. */
303uint32_t tox_count_friendlist(void *tox); 306uint32_t tox_count_friendlist(Tox *tox);
304 307
305/* Copy a list of valid friend IDs into the array out_list. 308/* Copy a list of valid friend IDs into the array out_list.
306 * If out_list is NULL, returns 0. 309 * If out_list is NULL, returns 0.
307 * Otherwise, returns the number of elements copied. 310 * Otherwise, returns the number of elements copied.
308 * If the array was too small, the contents 311 * If the array was too small, the contents
309 * of out_list will be truncated to list_size. */ 312 * of out_list will be truncated to list_size. */
310uint32_t tox_copy_friendlist(void *tox, int *out_list, uint32_t list_size); 313uint32_t tox_copy_friendlist(Tox *tox, int *out_list, uint32_t list_size);
311 314
312/* Set the function that will be executed when a friend request is received. 315/* Set the function that will be executed when a friend request is received.
313 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) 316 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length)
@@ -436,6 +439,7 @@ int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t
436 * to setup connections 439 * to setup connections
437 */ 440 */
438void tox_bootstrap_from_ip(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); 441void tox_bootstrap_from_ip(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key);
442
439/* Resolves address into an IP address. If successful, sends a "get nodes" 443/* Resolves address into an IP address. If successful, sends a "get nodes"
440 * request to the given node with ip, port and public_key to setup connections 444 * request to the given node with ip, port and public_key to setup connections
441 * 445 *
@@ -477,6 +481,18 @@ void tox_kill(Tox *tox);
477/* The main loop that needs to be run at least 20 times per second. */ 481/* The main loop that needs to be run at least 20 times per second. */
478void tox_do(Tox *tox); 482void tox_do(Tox *tox);
479 483
484/*
485 * Waits for something to happen on the socket for up to milliseconds milliseconds.
486 * *** Function MUSTN'T poll. ***
487 * The function mustn't modify anything at all, so it can be called completely
488 * asynchronously without any worry.
489 *
490 * returns 0 if the timeout was reached
491 * returns 1 if there is socket activity (i.e. tox_do() should be called)
492 *
493 */
494int tox_wait(Tox *tox, uint16_t milliseconds);
495
480/* SAVING AND LOADING FUNCTIONS: */ 496/* SAVING AND LOADING FUNCTIONS: */
481 497
482/* return size of messenger data (for saving). */ 498/* return size of messenger data (for saving). */
@@ -494,3 +510,4 @@ int tox_load(Tox *tox, uint8_t *data, uint32_t length);
494#endif 510#endif
495 511
496#endif 512#endif
513
diff --git a/toxcore/util.c b/toxcore/util.c
index acd47daf..e307bad6 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -118,7 +118,7 @@ void loginit(uint16_t port)
118 118
119 if (logbufferpredata) { 119 if (logbufferpredata) {
120 if (logfile) 120 if (logfile)
121 fprintf(logfile, logbufferpredata); 121 fprintf(logfile, "%s", logbufferpredata);
122 122
123 free(logbufferpredata); 123 free(logbufferpredata);
124 logbufferpredata = NULL; 124 logbufferpredata = NULL;
@@ -128,8 +128,7 @@ void loginit(uint16_t port)
128void loglog(char *text) 128void loglog(char *text)
129{ 129{
130 if (logfile) { 130 if (logfile) {
131 fprintf(logfile, "%4u ", (uint32_t)(now() - starttime)); 131 fprintf(logfile, "%4u %s", (uint32_t)(now() - starttime), text);
132 fprintf(logfile, text);
133 fflush(logfile); 132 fflush(logfile);
134 133
135 return; 134 return;
@@ -158,8 +157,7 @@ void loglog(char *text)
158 logbufferprelen = lennew; 157 logbufferprelen = lennew;
159 } 158 }
160 159
161 size_t written; 160 int written = sprintf(logbufferprehead, "%4u %s", (uint32_t)(now() - starttime), text);
162 sprintf(logbufferprehead, "%4u %s%n", (uint32_t)(now() - starttime), text, &written);
163 logbufferprehead += written; 161 logbufferprehead += written;
164} 162}
165 163
@@ -171,3 +169,4 @@ void logexit()
171 } 169 }
172}; 170};
173#endif 171#endif
172