summaryrefslogtreecommitdiff
path: root/core/Messenger.c
diff options
context:
space:
mode:
authorChris Hall <followingthepath@gmail.com>2013-08-11 15:24:11 +1200
committerChris Hall <followingthepath@gmail.com>2013-08-12 21:37:38 +1200
commit4293c4b1e66e9547f88c86bd580b9a4c79ca7ace (patch)
tree035448a709db7cab74d9d40fcfec641694f375ec /core/Messenger.c
parent139d915482c82f2a4aa87b444008afffef728561 (diff)
Messenger refactor - redid work from pull request 79
Moves static state out of Messenger.c and into a Messenger struct Purely stylistic, no functional changes were made. This commit also changed all the callers of Messenger as they now have to pass an instance of the Messenger struct to messenger functions. Also removed some uses of the 'static' keyword at the beginning of function definitions when the function was already declared static, as these caused gcc to whine.
Diffstat (limited to 'core/Messenger.c')
-rw-r--r--core/Messenger.c516
1 files changed, 240 insertions, 276 deletions
diff --git a/core/Messenger.c b/core/Messenger.c
index 92c83b9b..a6189941 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -24,40 +24,8 @@
24#include "Messenger.h" 24#include "Messenger.h"
25#define MIN(a,b) (((a)<(b))?(a):(b)) 25#define MIN(a,b) (((a)<(b))?(a):(b))
26 26
27typedef struct { 27static void set_friend_status(Messenger *m, int friendnumber, uint8_t status);
28 uint8_t client_id[CLIENT_ID_SIZE]; 28static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length);
29 int crypt_connection_id;
30 uint64_t friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */
31 uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */
32 uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */
33 uint8_t name[MAX_NAME_LENGTH];
34 uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */
35 uint8_t *statusmessage;
36 uint16_t statusmessage_length;
37 uint8_t statusmessage_sent;
38 USERSTATUS userstatus;
39 uint8_t userstatus_sent;
40 uint16_t info_size; /* length of the info */
41 uint32_t message_id; /* a semi-unique id used in read receipts */
42 uint8_t receives_read_receipts; /* shall we send read receipts to this person? */
43} Friend;
44
45uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
46
47static uint8_t self_name[MAX_NAME_LENGTH];
48static uint16_t self_name_length;
49
50static uint8_t self_statusmessage[MAX_STATUSMESSAGE_LENGTH];
51static uint16_t self_statusmessage_length;
52
53static USERSTATUS self_userstatus;
54
55static Friend *friendlist;
56static uint32_t numfriends;
57
58
59static void set_friend_status(int friendnumber, uint8_t status);
60static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length);
61 29
62/* 1 if we are online 30/* 1 if we are online
63 0 if we are offline 31 0 if we are offline
@@ -65,24 +33,24 @@ static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *da
65 33
66/* set the size of the friend list to numfriends 34/* set the size of the friend list to numfriends
67 return -1 if realloc fails */ 35 return -1 if realloc fails */
68int realloc_friendlist(uint32_t num) { 36int realloc_friendlist(Messenger *m, uint32_t num) {
69 Friend *newfriendlist = realloc(friendlist, num*sizeof(Friend)); 37 Friend *newfriendlist = realloc(m->friendlist, num*sizeof(Friend));
70 if (newfriendlist == NULL) 38 if (newfriendlist == NULL)
71 return -1; 39 return -1;
72 memset(&newfriendlist[num-1], 0, sizeof(Friend)); 40 memset(&newfriendlist[num-1], 0, sizeof(Friend));
73 friendlist = newfriendlist; 41 m->friendlist = newfriendlist;
74 return 0; 42 return 0;
75} 43}
76 44
77/* return the friend id associated to that public key. 45/* return the friend id associated to that public key.
78 return -1 if no such friend */ 46 return -1 if no such friend */
79int getfriend_id(uint8_t *client_id) 47int getfriend_id(Messenger *m, uint8_t *client_id)
80{ 48{
81 uint32_t i; 49 uint32_t i;
82 50
83 for (i = 0; i < numfriends; ++i) { 51 for (i = 0; i < m->numfriends; ++i) {
84 if (friendlist[i].status > 0) 52 if (m->friendlist[i].status > 0)
85 if (memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) 53 if (memcmp(client_id, m->friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0)
86 return i; 54 return i;
87 } 55 }
88 56
@@ -93,13 +61,13 @@ int getfriend_id(uint8_t *client_id)
93 make sure that client_id is of size CLIENT_ID_SIZE. 61 make sure that client_id is of size CLIENT_ID_SIZE.
94 return 0 if success 62 return 0 if success
95 return -1 if failure. */ 63 return -1 if failure. */
96int getclient_id(int friend_id, uint8_t *client_id) 64int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
97{ 65{
98 if (friend_id >= numfriends || friend_id < 0) 66 if (friend_id >= m->numfriends || friend_id < 0)
99 return -1; 67 return -1;
100 68
101 if (friendlist[friend_id].status > 0) { 69 if (m->friendlist[friend_id].status > 0) {
102 memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE); 70 memcpy(client_id, m->friendlist[friend_id].client_id, CLIENT_ID_SIZE);
103 return 0; 71 return 0;
104 } 72 }
105 73
@@ -118,7 +86,7 @@ int getclient_id(int friend_id, uint8_t *client_id)
118 * return FAERR_ALREADYSENT if friend request already sent or already a friend 86 * return FAERR_ALREADYSENT if friend request already sent or already a friend
119 * return FAERR_UNKNOWN for unknown error 87 * return FAERR_UNKNOWN for unknown error
120 */ 88 */
121int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) 89int m_addfriend(Messenger *m, uint8_t *client_id, uint8_t *data, uint16_t length)
122{ 90{
123 if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES 91 if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES
124 - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES 92 - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
@@ -128,57 +96,57 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
128 return FAERR_NOMESSAGE; 96 return FAERR_NOMESSAGE;
129 if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) 97 if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
130 return FAERR_OWNKEY; 98 return FAERR_OWNKEY;
131 if (getfriend_id(client_id) != -1) 99 if (getfriend_id(m, client_id) != -1)
132 return FAERR_ALREADYSENT; 100 return FAERR_ALREADYSENT;
133 101
134 /* resize the friend list if necessary */ 102 /* resize the friend list if necessary */
135 realloc_friendlist(numfriends + 1); 103 realloc_friendlist(m, m->numfriends + 1);
136 104
137 uint32_t i; 105 uint32_t i;
138 for (i = 0; i <= numfriends; ++i) { 106 for (i = 0; i <= m->numfriends; ++i) {
139 if (friendlist[i].status == NOFRIEND) { 107 if (m->friendlist[i].status == NOFRIEND) {
140 DHT_addfriend(client_id); 108 DHT_addfriend(client_id);
141 friendlist[i].status = FRIEND_ADDED; 109 m->friendlist[i].status = FRIEND_ADDED;
142 friendlist[i].crypt_connection_id = -1; 110 m->friendlist[i].crypt_connection_id = -1;
143 friendlist[i].friend_request_id = -1; 111 m->friendlist[i].friend_request_id = -1;
144 memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); 112 memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
145 friendlist[i].statusmessage = calloc(1, 1); 113 m->friendlist[i].statusmessage = calloc(1, 1);
146 friendlist[i].statusmessage_length = 1; 114 m->friendlist[i].statusmessage_length = 1;
147 friendlist[i].userstatus = USERSTATUS_NONE; 115 m->friendlist[i].userstatus = USERSTATUS_NONE;
148 memcpy(friendlist[i].info, data, length); 116 memcpy(m->friendlist[i].info, data, length);
149 friendlist[i].info_size = length; 117 m->friendlist[i].info_size = length;
150 friendlist[i].message_id = 0; 118 m->friendlist[i].message_id = 0;
151 friendlist[i].receives_read_receipts = 1; /* default: YES */ 119 m->friendlist[i].receives_read_receipts = 1; /* default: YES */
152 120
153 ++numfriends; 121 ++ m->numfriends;
154 return i; 122 return i;
155 } 123 }
156 } 124 }
157 return FAERR_UNKNOWN; 125 return FAERR_UNKNOWN;
158} 126}
159 127
160int m_addfriend_norequest(uint8_t * client_id) 128int m_addfriend_norequest(Messenger *m, uint8_t * client_id)
161{ 129{
162 if (getfriend_id(client_id) != -1) 130 if (getfriend_id(m, client_id) != -1)
163 return -1; 131 return -1;
164 132
165 /* resize the friend list if necessary */ 133 /* resize the friend list if necessary */
166 realloc_friendlist(numfriends + 1); 134 realloc_friendlist(m, m->numfriends + 1);
167 135
168 uint32_t i; 136 uint32_t i;
169 for (i = 0; i <= numfriends; ++i) { 137 for (i = 0; i <= m->numfriends; ++i) {
170 if(friendlist[i].status == NOFRIEND) { 138 if(m->friendlist[i].status == NOFRIEND) {
171 DHT_addfriend(client_id); 139 DHT_addfriend(client_id);
172 friendlist[i].status = FRIEND_REQUESTED; 140 m->friendlist[i].status = FRIEND_REQUESTED;
173 friendlist[i].crypt_connection_id = -1; 141 m->friendlist[i].crypt_connection_id = -1;
174 friendlist[i].friend_request_id = -1; 142 m->friendlist[i].friend_request_id = -1;
175 memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); 143 memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
176 friendlist[i].statusmessage = calloc(1, 1); 144 m->friendlist[i].statusmessage = calloc(1, 1);
177 friendlist[i].statusmessage_length = 1; 145 m->friendlist[i].statusmessage_length = 1;
178 friendlist[i].userstatus = USERSTATUS_NONE; 146 m->friendlist[i].userstatus = USERSTATUS_NONE;
179 friendlist[i].message_id = 0; 147 m->friendlist[i].message_id = 0;
180 friendlist[i].receives_read_receipts = 1; /* default: YES */ 148 m->friendlist[i].receives_read_receipts = 1; /* default: YES */
181 ++numfriends; 149 ++ m->numfriends;
182 return i; 150 return i;
183 } 151 }
184 } 152 }
@@ -188,23 +156,23 @@ int m_addfriend_norequest(uint8_t * client_id)
188/* remove a friend 156/* remove a friend
189 return 0 if success 157 return 0 if success
190 return -1 if failure */ 158 return -1 if failure */
191int m_delfriend(int friendnumber) 159int m_delfriend(Messenger *m, int friendnumber)
192{ 160{
193 if (friendnumber >= numfriends || friendnumber < 0) 161 if (friendnumber >= m->numfriends || friendnumber < 0)
194 return -1; 162 return -1;
195 163
196 DHT_delfriend(friendlist[friendnumber].client_id); 164 DHT_delfriend(m->friendlist[friendnumber].client_id);
197 crypto_kill(friendlist[friendnumber].crypt_connection_id); 165 crypto_kill(m->friendlist[friendnumber].crypt_connection_id);
198 free(friendlist[friendnumber].statusmessage); 166 free(m->friendlist[friendnumber].statusmessage);
199 memset(&friendlist[friendnumber], 0, sizeof(Friend)); 167 memset(&(m->friendlist[friendnumber]), 0, sizeof(Friend));
200 uint32_t i; 168 uint32_t i;
201 169
202 for (i = numfriends; i != 0; --i) { 170 for (i = m->numfriends; i != 0; --i) {
203 if (friendlist[i-1].status != NOFRIEND) 171 if (m->friendlist[i-1].status != NOFRIEND)
204 break; 172 break;
205 } 173 }
206 numfriends = i; 174 m->numfriends = i;
207 realloc_friendlist(numfriends + 1); 175 realloc_friendlist(m, m->numfriends + 1);
208 176
209 return 0; 177 return 0;
210} 178}
@@ -214,31 +182,31 @@ int m_delfriend(int friendnumber)
214 return FRIEND_REQUESTED if the friend request was sent 182 return FRIEND_REQUESTED if the friend request was sent
215 return FRIEND_ADDED if the friend was added 183 return FRIEND_ADDED if the friend was added
216 return NOFRIEND if there is no friend with that number */ 184 return NOFRIEND if there is no friend with that number */
217int m_friendstatus(int friendnumber) 185int m_friendstatus(Messenger *m, int friendnumber)
218{ 186{
219 if (friendnumber < 0 || friendnumber >= numfriends) 187 if (friendnumber < 0 || friendnumber >= m->numfriends)
220 return NOFRIEND; 188 return NOFRIEND;
221 return friendlist[friendnumber].status; 189 return m->friendlist[friendnumber].status;
222} 190}
223 191
224/* send a text chat message to an online friend 192/* send a text chat message to an online friend
225 return the message id if packet was successfully put into the send queue 193 return the message id if packet was successfully put into the send queue
226 return 0 if it was not */ 194 return 0 if it was not */
227uint32_t m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) 195uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length)
228{ 196{
229 if (friendnumber < 0 || friendnumber >= numfriends) 197 if (friendnumber < 0 || friendnumber >= m->numfriends)
230 return 0; 198 return 0;
231 uint32_t msgid = ++friendlist[friendnumber].message_id; 199 uint32_t msgid = ++m->friendlist[friendnumber].message_id;
232 if (msgid == 0) 200 if (msgid == 0)
233 msgid = 1; /* otherwise, false error */ 201 msgid = 1; /* otherwise, false error */
234 if(m_sendmessage_withid(friendnumber, msgid, message, length)) { 202 if(m_sendmessage_withid(m, friendnumber, msgid, message, length)) {
235 return msgid; 203 return msgid;
236 } 204 }
237 205
238 return 0; 206 return 0;
239} 207}
240 208
241uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) 209uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length)
242{ 210{
243 if (length >= (MAX_DATA_SIZE - sizeof(theid))) 211 if (length >= (MAX_DATA_SIZE - sizeof(theid)))
244 return 0; 212 return 0;
@@ -246,34 +214,34 @@ uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message
246 theid = htonl(theid); 214 theid = htonl(theid);
247 memcpy(temp, &theid, sizeof(theid)); 215 memcpy(temp, &theid, sizeof(theid));
248 memcpy(temp + sizeof(theid), message, length); 216 memcpy(temp + sizeof(theid), message, length);
249 return write_cryptpacket_id(friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid)); 217 return write_cryptpacket_id(m, friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid));
250} 218}
251 219
252/* send an action to an online friend 220/* send an action to an online friend
253 return 1 if packet was successfully put into the send queue 221 return 1 if packet was successfully put into the send queue
254 return 0 if it was not */ 222 return 0 if it was not */
255int m_sendaction(int friendnumber, uint8_t *action, uint32_t length) 223int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length)
256{ 224{
257 return write_cryptpacket_id(friendnumber, PACKET_ID_ACTION, action, length); 225 return write_cryptpacket_id(m, friendnumber, PACKET_ID_ACTION, action, length);
258} 226}
259 227
260/* send a name packet to friendnumber 228/* send a name packet to friendnumber
261 length is the length with the NULL terminator*/ 229 length is the length with the NULL terminator*/
262static int m_sendname(int friendnumber, uint8_t * name, uint16_t length) 230static int m_sendname(Messenger *m, int friendnumber, uint8_t * name, uint16_t length)
263{ 231{
264 if(length > MAX_NAME_LENGTH || length == 0) 232 if(length > MAX_NAME_LENGTH || length == 0)
265 return 0; 233 return 0;
266 return write_cryptpacket_id(friendnumber, PACKET_ID_NICKNAME, name, length); 234 return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length);
267} 235}
268 236
269/* set the name of a friend 237/* set the name of a friend
270 return 0 if success 238 return 0 if success
271 return -1 if failure */ 239 return -1 if failure */
272static int setfriendname(int friendnumber, uint8_t * name) 240static int setfriendname(Messenger *m, int friendnumber, uint8_t * name)
273{ 241{
274 if (friendnumber >= numfriends || friendnumber < 0) 242 if (friendnumber >= m->numfriends || friendnumber < 0)
275 return -1; 243 return -1;
276 memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH); 244 memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
277 return 0; 245 return 0;
278} 246}
279 247
@@ -283,15 +251,15 @@ static int setfriendname(int friendnumber, uint8_t * name)
283 length is the length of name with the NULL terminator 251 length is the length of name with the NULL terminator
284 return 0 if success 252 return 0 if success
285 return -1 if failure */ 253 return -1 if failure */
286int setname(uint8_t * name, uint16_t length) 254int setname(Messenger *m, uint8_t * name, uint16_t length)
287{ 255{
288 if (length > MAX_NAME_LENGTH || length == 0) 256 if (length > MAX_NAME_LENGTH || length == 0)
289 return -1; 257 return -1;
290 memcpy(self_name, name, length); 258 memcpy(m->name, name, length);
291 self_name_length = length; 259 m->name_length = length;
292 uint32_t i; 260 uint32_t i;
293 for (i = 0; i < numfriends; ++i) 261 for (i = 0; i < m->numfriends; ++i)
294 friendlist[i].name_sent = 0; 262 m->friendlist[i].name_sent = 0;
295 return 0; 263 return 0;
296} 264}
297 265
@@ -299,10 +267,10 @@ int setname(uint8_t * name, uint16_t length)
299 put it in name 267 put it in name
300 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. 268 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
301 return the length of the name */ 269 return the length of the name */
302uint16_t getself_name(uint8_t *name) 270uint16_t getself_name(Messenger *m, uint8_t *name)
303{ 271{
304 memcpy(name, self_name, self_name_length); 272 memcpy(name, m->name, m->name_length);
305 return self_name_length; 273 return m->name_length;
306} 274}
307 275
308/* get name of friendnumber 276/* get name of friendnumber
@@ -310,293 +278,288 @@ uint16_t getself_name(uint8_t *name)
310 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. 278 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
311 return 0 if success 279 return 0 if success
312 return -1 if failure */ 280 return -1 if failure */
313int getname(int friendnumber, uint8_t * name) 281int getname(Messenger *m, int friendnumber, uint8_t * name)
314{ 282{
315 if (friendnumber >= numfriends || friendnumber < 0) 283 if (friendnumber >= m->numfriends || friendnumber < 0)
316 return -1; 284 return -1;
317 memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH); 285 memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH);
318 return 0; 286 return 0;
319} 287}
320 288
321int m_set_statusmessage(uint8_t *status, uint16_t length) 289int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length)
322{ 290{
323 if (length > MAX_STATUSMESSAGE_LENGTH) 291 if (length > MAX_STATUSMESSAGE_LENGTH)
324 return -1; 292 return -1;
325 memcpy(self_statusmessage, status, length); 293 memcpy(m->statusmessage, status, length);
326 self_statusmessage_length = length; 294 m->statusmessage_length = length;
327 295
328 uint32_t i; 296 uint32_t i;
329 for (i = 0; i < numfriends; ++i) 297 for (i = 0; i < m->numfriends; ++i)
330 friendlist[i].statusmessage_sent = 0; 298 m->friendlist[i].statusmessage_sent = 0;
331 return 0; 299 return 0;
332} 300}
333 301
334int m_set_userstatus(USERSTATUS status) 302int m_set_userstatus(Messenger *m, USERSTATUS status)
335{ 303{
336 if (status >= USERSTATUS_INVALID) { 304 if (status >= USERSTATUS_INVALID) {
337 return -1; 305 return -1;
338 } 306 }
339 self_userstatus = status; 307 m->userstatus = status;
340 uint32_t i; 308 uint32_t i;
341 for (i = 0; i < numfriends; ++i) 309 for (i = 0; i < m->numfriends; ++i)
342 friendlist[i].userstatus_sent = 0; 310 m->friendlist[i].userstatus_sent = 0;
343 return 0; 311 return 0;
344} 312}
345 313
346/* return the size of friendnumber's user status 314/* return the size of friendnumber's user status
347 guaranteed to be at most MAX_STATUSMESSAGE_LENGTH */ 315 guaranteed to be at most MAX_STATUSMESSAGE_LENGTH */
348int m_get_statusmessage_size(int friendnumber) 316int m_get_statusmessage_size(Messenger *m, int friendnumber)
349{ 317{
350 if (friendnumber >= numfriends || friendnumber < 0) 318 if (friendnumber >= m->numfriends || friendnumber < 0)
351 return -1; 319 return -1;
352 return friendlist[friendnumber].statusmessage_length; 320 return m->friendlist[friendnumber].statusmessage_length;
353} 321}
354 322
355/* copy the user status of friendnumber into buf, truncating if needed to maxlen 323/* copy the user status of friendnumber into buf, truncating if needed to maxlen
356 bytes, use m_get_statusmessage_size to find out how much you need to allocate */ 324 bytes, use m_get_statusmessage_size to find out how much you need to allocate */
357int m_copy_statusmessage(int friendnumber, uint8_t * buf, uint32_t maxlen) 325int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t * buf, uint32_t maxlen)
358{ 326{
359 if (friendnumber >= numfriends || friendnumber < 0) 327 if (friendnumber >= m->numfriends || friendnumber < 0)
360 return -1; 328 return -1;
361 memset(buf, 0, maxlen); 329 memset(buf, 0, maxlen);
362 memcpy(buf, friendlist[friendnumber].statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1); 330 memcpy(buf, m->friendlist[friendnumber].statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1);
363 return 0; 331 return 0;
364} 332}
365 333
366int m_copy_self_statusmessage(uint8_t * buf, uint32_t maxlen) 334int m_copy_self_statusmessage(Messenger *m, uint8_t * buf, uint32_t maxlen)
367{ 335{
368 memset(buf, 0, maxlen); 336 memset(buf, 0, maxlen);
369 memcpy(buf, self_statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1); 337 memcpy(buf, m->statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1);
370 return 0; 338 return 0;
371} 339}
372 340
373USERSTATUS m_get_userstatus(int friendnumber) 341USERSTATUS m_get_userstatus(Messenger *m, int friendnumber)
374{ 342{
375 if (friendnumber >= numfriends || friendnumber < 0) 343 if (friendnumber >= m->numfriends || friendnumber < 0)
376 return USERSTATUS_INVALID; 344 return USERSTATUS_INVALID;
377 USERSTATUS status = friendlist[friendnumber].userstatus; 345 USERSTATUS status = m->friendlist[friendnumber].userstatus;
378 if (status >= USERSTATUS_INVALID) { 346 if (status >= USERSTATUS_INVALID) {
379 status = USERSTATUS_NONE; 347 status = USERSTATUS_NONE;
380 } 348 }
381 return status; 349 return status;
382} 350}
383 351
384USERSTATUS m_get_self_userstatus(void) 352USERSTATUS m_get_self_userstatus(Messenger *m)
385{ 353{
386 return self_userstatus; 354 return m->userstatus;
387} 355}
388 356
389static int send_statusmessage(int friendnumber, uint8_t * status, uint16_t length) 357static int send_statusmessage(Messenger *m, int friendnumber, uint8_t * status, uint16_t length)
390{ 358{
391 return write_cryptpacket_id(friendnumber, PACKET_ID_STATUSMESSAGE, status, length); 359 return write_cryptpacket_id(m, friendnumber, PACKET_ID_STATUSMESSAGE, status, length);
392} 360}
393 361
394static int send_userstatus(int friendnumber, USERSTATUS status) 362static int send_userstatus(Messenger *m, int friendnumber, USERSTATUS status)
395{ 363{
396 uint8_t stat = status; 364 uint8_t stat = status;
397 return write_cryptpacket_id(friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat)); 365 return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat));
398} 366}
399 367
400static int set_friend_statusmessage(int friendnumber, uint8_t * status, uint16_t length) 368static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t * status, uint16_t length)
401{ 369{
402 if (friendnumber >= numfriends || friendnumber < 0) 370 if (friendnumber >= m->numfriends || friendnumber < 0)
403 return -1; 371 return -1;
404 uint8_t *newstatus = calloc(length, 1); 372 uint8_t *newstatus = calloc(length, 1);
405 memcpy(newstatus, status, length); 373 memcpy(newstatus, status, length);
406 free(friendlist[friendnumber].statusmessage); 374 free(m->friendlist[friendnumber].statusmessage);
407 friendlist[friendnumber].statusmessage = newstatus; 375 m->friendlist[friendnumber].statusmessage = newstatus;
408 friendlist[friendnumber].statusmessage_length = length; 376 m->friendlist[friendnumber].statusmessage_length = length;
409 return 0; 377 return 0;
410} 378}
411 379
412static void set_friend_userstatus(int friendnumber, USERSTATUS status) 380static void set_friend_userstatus(Messenger *m, int friendnumber, USERSTATUS status)
413{ 381{
414 friendlist[friendnumber].userstatus = status; 382 m->friendlist[friendnumber].userstatus = status;
415} 383}
416 384
417/* Sets whether we send read receipts for friendnumber. */ 385/* Sets whether we send read receipts for friendnumber. */
418void m_set_sends_receipts(int friendnumber, int yesno) 386void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno)
419{ 387{
420 if (yesno != 0 || yesno != 1) 388 if (yesno != 0 || yesno != 1)
421 return; 389 return;
422 if (friendnumber >= numfriends || friendnumber < 0) 390 if (friendnumber >= m->numfriends || friendnumber < 0)
423 return; 391 return;
424 friendlist[friendnumber].receives_read_receipts = yesno; 392 m->friendlist[friendnumber].receives_read_receipts = yesno;
425} 393}
426 394
427/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); 395/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
428static uint8_t friend_request_isset = 0; */ 396static uint8_t friend_request_isset = 0; */
429/* set the function that will be executed when a friend request is received. */ 397/* set the function that will be executed when a friend request is received. */
430void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) 398void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t))
431{ 399{
432 callback_friendrequest(function); 400 callback_friendrequest(function);
433} 401}
434 402
435static void (*friend_message)(int, uint8_t *, uint16_t);
436static uint8_t friend_message_isset = 0;
437
438/* set the function that will be executed when a message from a friend is received. */ 403/* set the function that will be executed when a message from a friend is received. */
439void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)) 404void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t))
440{ 405{
441 friend_message = function; 406 m->friend_message = function;
442 friend_message_isset = 1; 407 m->friend_message_isset = 1;
443} 408}
444 409
445static void (*friend_action)(int, uint8_t *, uint16_t); 410void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t))
446static uint8_t friend_action_isset = 0;
447void m_callback_action(void (*function)(int, uint8_t *, uint16_t))
448{ 411{
449 friend_action = function; 412 m->friend_action = function;
450 friend_action_isset = 1; 413 m->friend_action_isset = 1;
451} 414}
452 415
453static void (*friend_namechange)(int, uint8_t *, uint16_t); 416void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t))
454static uint8_t friend_namechange_isset = 0;
455void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t))
456{ 417{
457 friend_namechange = function; 418 m->friend_namechange = function;
458 friend_namechange_isset = 1; 419 m->friend_namechange_isset = 1;
459} 420}
460 421
461static void (*friend_statusmessagechange)(int, uint8_t *, uint16_t); 422void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t))
462static uint8_t friend_statusmessagechange_isset = 0;
463void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t))
464{ 423{
465 friend_statusmessagechange = function; 424 m->friend_statusmessagechange = function;
466 friend_statusmessagechange_isset = 1; 425 m->friend_statusmessagechange_isset = 1;
467} 426}
468 427
469static void (*friend_userstatuschange)(int, USERSTATUS); 428void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS))
470static uint8_t friend_userstatuschange_isset = 0;
471void m_callback_userstatus(void (*function)(int, USERSTATUS))
472{ 429{
473 friend_userstatuschange = function; 430 m->friend_userstatuschange = function;
474 friend_userstatuschange_isset = 1; 431 m->friend_userstatuschange_isset = 1;
475} 432}
476 433
477static void (*read_receipt)(int, uint32_t); 434void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t))
478static uint8_t read_receipt_isset = 0;
479void m_callback_read_receipt(void (*function)(int, uint32_t))
480{ 435{
481 read_receipt = function; 436 m->read_receipt = function;
482 read_receipt_isset = 1; 437 m->read_receipt_isset = 1;
483} 438}
484 439
485static void (*friend_connectionstatuschange)(int, uint8_t); 440void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t))
486static uint8_t friend_connectionstatuschange_isset = 0;
487void m_callback_connectionstatus(void (*function)(int, uint8_t))
488{ 441{
489 friend_connectionstatuschange = function; 442 m->friend_connectionstatuschange = function;
490 friend_connectionstatuschange_isset = 1; 443 m->friend_connectionstatuschange_isset = 1;
491} 444}
492 445
493static void check_friend_connectionstatus(int friendnumber, uint8_t status) 446static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_t status)
494{ 447{
495 if (!friend_connectionstatuschange_isset) 448 if (!m->friend_connectionstatuschange_isset)
496 return; 449 return;
497 if (status == NOFRIEND) 450 if (status == NOFRIEND)
498 return; 451 return;
499 const uint8_t was_connected = friendlist[friendnumber].status == FRIEND_ONLINE; 452 const uint8_t was_connected = m->friendlist[friendnumber].status == FRIEND_ONLINE;
500 const uint8_t is_connected = status == FRIEND_ONLINE; 453 const uint8_t is_connected = status == FRIEND_ONLINE;
501 if (is_connected != was_connected) 454 if (is_connected != was_connected)
502 friend_connectionstatuschange(friendnumber, is_connected); 455 m->friend_connectionstatuschange(m, friendnumber, is_connected);
503} 456}
504 457
505static void set_friend_status(int friendnumber, uint8_t status) 458void set_friend_status(Messenger *m, int friendnumber, uint8_t status)
506{ 459{
507 check_friend_connectionstatus(friendnumber, status); 460 check_friend_connectionstatus(m, friendnumber, status);
508 friendlist[friendnumber].status = status; 461 m->friendlist[friendnumber].status = status;
509} 462}
510 463
511static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) 464int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length)
512{ 465{
513 if (friendnumber < 0 || friendnumber >= numfriends) 466 if (friendnumber < 0 || friendnumber >= m->numfriends)
514 return 0; 467 return 0;
515 if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != FRIEND_ONLINE) 468 if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE)
516 return 0; 469 return 0;
517 uint8_t packet[length + 1]; 470 uint8_t packet[length + 1];
518 packet[0] = packet_id; 471 packet[0] = packet_id;
519 memcpy(packet + 1, data, length); 472 memcpy(packet + 1, data, length);
520 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, packet, length + 1); 473 return write_cryptpacket(m->friendlist[friendnumber].crypt_connection_id, packet, length + 1);
521} 474}
522 475
523#define PORT 33445 476#define PORT 33445
524/* run this at startup */ 477/* run this at startup */
525int initMessenger(void) 478Messenger * initMessenger(void)
526{ 479{
480 Messenger *m = calloc(1, sizeof(Messenger));
481 if( ! m )
482 return 0;
483
527 new_keys(); 484 new_keys();
528 m_set_statusmessage((uint8_t*)"Online", sizeof("Online")); 485 m_set_statusmessage(m, (uint8_t*)"Online", sizeof("Online"));
529 initNetCrypto(); 486 initNetCrypto();
530 IP ip; 487 IP ip;
531 ip.i = 0; 488 ip.i = 0;
532 489
533 if(init_networking(ip,PORT) == -1) 490 if(init_networking(ip,PORT) == -1)
534 return -1; 491 return 0;
535 492
536 DHT_init(); 493 DHT_init();
537 LosslessUDP_init(); 494 LosslessUDP_init();
538 friendreq_init(); 495 friendreq_init();
539 LANdiscovery_init(); 496 LANdiscovery_init();
540 497
541 return 0; 498 return m;
499}
500
501/* run this before closing shop */
502void cleanupMessenger(Messenger *m){
503 /* FIXME TODO it seems no one frees friendlist or all the elements status */
504 free(m);
542} 505}
543 506
544//TODO: make this function not suck. 507//TODO: make this function not suck.
545static void doFriends(void) 508void doFriends(Messenger *m)
546{ 509{
547 /* TODO: add incoming connections and some other stuff. */ 510 /* TODO: add incoming connections and some other stuff. */
548 uint32_t i; 511 uint32_t i;
549 int len; 512 int len;
550 uint8_t temp[MAX_DATA_SIZE]; 513 uint8_t temp[MAX_DATA_SIZE];
551 for (i = 0; i < numfriends; ++i) { 514 for (i = 0; i < m->numfriends; ++i) {
552 if (friendlist[i].status == FRIEND_ADDED) { 515 if (m->friendlist[i].status == FRIEND_ADDED) {
553 int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); 516 int fr = send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].info, m->friendlist[i].info_size);
554 if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */ 517 if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */
555 set_friend_status(i, FRIEND_REQUESTED); 518 set_friend_status(m, i, FRIEND_REQUESTED);
556 else if (fr > 0) 519 else if (fr > 0)
557 set_friend_status(i, FRIEND_REQUESTED); 520 set_friend_status(m, i, FRIEND_REQUESTED);
558 } 521 }
559 if (friendlist[i].status == FRIEND_REQUESTED || friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */ 522 if (m->friendlist[i].status == FRIEND_REQUESTED || m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */
560 if (friendlist[i].status == FRIEND_REQUESTED) { 523 if (m->friendlist[i].status == FRIEND_REQUESTED) {
561 if (friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/ 524 if (m->friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/
562 send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); 525 send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].info, m->friendlist[i].info_size);
563 friendlist[i].friend_request_id = unix_time(); 526 m->friendlist[i].friend_request_id = unix_time();
564 } 527 }
565 } 528 }
566 IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); 529 IP_Port friendip = DHT_getfriendip(m->friendlist[i].client_id);
567 switch (is_cryptoconnected(friendlist[i].crypt_connection_id)) { 530 switch (is_cryptoconnected(m->friendlist[i].crypt_connection_id)) {
568 case 0: 531 case 0:
569 if (friendip.ip.i > 1) 532 if (friendip.ip.i > 1)
570 friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); 533 m->friendlist[i].crypt_connection_id = crypto_connect(m->friendlist[i].client_id, friendip);
571 break; 534 break;
572 case 3: /* Connection is established */ 535 case 3: /* Connection is established */
573 set_friend_status(i, FRIEND_ONLINE); 536 set_friend_status(m, i, FRIEND_ONLINE);
574 friendlist[i].name_sent = 0; 537 m->friendlist[i].name_sent = 0;
575 friendlist[i].userstatus_sent = 0; 538 m->friendlist[i].userstatus_sent = 0;
576 friendlist[i].statusmessage_sent = 0; 539 m->friendlist[i].statusmessage_sent = 0;
577 break; 540 break;
578 case 4: 541 case 4:
579 crypto_kill(friendlist[i].crypt_connection_id); 542 crypto_kill(m->friendlist[i].crypt_connection_id);
580 friendlist[i].crypt_connection_id = -1; 543 m->friendlist[i].crypt_connection_id = -1;
581 break; 544 break;
582 default: 545 default:
583 break; 546 break;
584 } 547 }
585 } 548 }
586 while (friendlist[i].status == FRIEND_ONLINE) { /* friend is online */ 549 while (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online */
587 if (friendlist[i].name_sent == 0) { 550 if (m->friendlist[i].name_sent == 0) {
588 if (m_sendname(i, self_name, self_name_length)) 551 if (m_sendname(m, i, m->name, m->name_length))
589 friendlist[i].name_sent = 1; 552 m->friendlist[i].name_sent = 1;
590 } 553 }
591 if (friendlist[i].statusmessage_sent == 0) { 554 if (m->friendlist[i].statusmessage_sent == 0) {
592 if (send_statusmessage(i, self_statusmessage, self_statusmessage_length)) 555 if (send_statusmessage(m, i, m->statusmessage, m->statusmessage_length))
593 friendlist[i].statusmessage_sent = 1; 556 m->friendlist[i].statusmessage_sent = 1;
594 } 557 }
595 if (friendlist[i].userstatus_sent == 0) { 558 if (m->friendlist[i].userstatus_sent == 0) {
596 if (send_userstatus(i, self_userstatus)) 559 if (send_userstatus(m, i, m->userstatus))
597 friendlist[i].userstatus_sent = 1; 560 m->friendlist[i].userstatus_sent = 1;
598 } 561 }
599 len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); 562 len = read_cryptpacket(m->friendlist[i].crypt_connection_id, temp);
600 uint8_t packet_id = temp[0]; 563 uint8_t packet_id = temp[0];
601 uint8_t* data = temp + 1; 564 uint8_t* data = temp + 1;
602 int data_length = len - 1; 565 int data_length = len - 1;
@@ -605,10 +568,10 @@ static void doFriends(void)
605 case PACKET_ID_NICKNAME: { 568 case PACKET_ID_NICKNAME: {
606 if (data_length >= MAX_NAME_LENGTH || data_length == 0) 569 if (data_length >= MAX_NAME_LENGTH || data_length == 0)
607 break; 570 break;
608 if(friend_namechange_isset) 571 if(m->friend_namechange_isset)
609 friend_namechange(i, data, data_length); 572 m->friend_namechange(m, i, data, data_length);
610 memcpy(friendlist[i].name, data, data_length); 573 memcpy(m->friendlist[i].name, data, data_length);
611 friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ 574 m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */
612 break; 575 break;
613 } 576 }
614 case PACKET_ID_STATUSMESSAGE: { 577 case PACKET_ID_STATUSMESSAGE: {
@@ -616,9 +579,9 @@ static void doFriends(void)
616 break; 579 break;
617 uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1); 580 uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1);
618 memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); 581 memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
619 if (friend_statusmessagechange_isset) 582 if (m->friend_statusmessagechange_isset)
620 friend_statusmessagechange(i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); 583 m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
621 set_friend_statusmessage(i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); 584 set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
622 free(status); 585 free(status);
623 break; 586 break;
624 } 587 }
@@ -626,9 +589,9 @@ static void doFriends(void)
626 if (data_length != 1) 589 if (data_length != 1)
627 break; 590 break;
628 USERSTATUS status = data[0]; 591 USERSTATUS status = data[0];
629 if (friend_userstatuschange_isset) 592 if (m->friend_userstatuschange_isset)
630 friend_userstatuschange(i, status); 593 m->friend_userstatuschange(m, i, status);
631 set_friend_userstatus(i, status); 594 set_friend_userstatus(m, i, status);
632 break; 595 break;
633 } 596 }
634 case PACKET_ID_MESSAGE: { 597 case PACKET_ID_MESSAGE: {
@@ -636,16 +599,16 @@ static void doFriends(void)
636 uint8_t message_id_length = 4; 599 uint8_t message_id_length = 4;
637 uint8_t *message = data + message_id_length; 600 uint8_t *message = data + message_id_length;
638 uint16_t message_length = data_length - message_id_length; 601 uint16_t message_length = data_length - message_id_length;
639 if (friendlist[i].receives_read_receipts) { 602 if (m->friendlist[i].receives_read_receipts) {
640 write_cryptpacket_id(i, PACKET_ID_RECEIPT, message_id, message_id_length); 603 write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length);
641 } 604 }
642 if (friend_message_isset) 605 if (m->friend_message_isset)
643 (*friend_message)(i, message, message_length); 606 (*m->friend_message)(m, i, message, message_length);
644 break; 607 break;
645 } 608 }
646 case PACKET_ID_ACTION: { 609 case PACKET_ID_ACTION: {
647 if (friend_action_isset) 610 if (m->friend_action_isset)
648 (*friend_action)(i, data, data_length); 611 (*m->friend_action)(m, i, data, data_length);
649 break; 612 break;
650 } 613 }
651 case PACKET_ID_RECEIPT: { 614 case PACKET_ID_RECEIPT: {
@@ -654,16 +617,16 @@ static void doFriends(void)
654 break; 617 break;
655 memcpy(&msgid, data, sizeof(msgid)); 618 memcpy(&msgid, data, sizeof(msgid));
656 msgid = ntohl(msgid); 619 msgid = ntohl(msgid);
657 if (read_receipt_isset) 620 if (m->read_receipt_isset)
658 (*read_receipt)(i, msgid); 621 (*m->read_receipt)(m, i, msgid);
659 break; 622 break;
660 } 623 }
661 } 624 }
662 } else { 625 } else {
663 if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ 626 if (is_cryptoconnected(m->friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */
664 crypto_kill(friendlist[i].crypt_connection_id); 627 crypto_kill(m->friendlist[i].crypt_connection_id);
665 friendlist[i].crypt_connection_id = -1; 628 m->friendlist[i].crypt_connection_id = -1;
666 set_friend_status(i, FRIEND_CONFIRMED); 629 set_friend_status(m, i, FRIEND_CONFIRMED);
667 } 630 }
668 break; 631 break;
669 } 632 }
@@ -671,20 +634,20 @@ static void doFriends(void)
671 } 634 }
672} 635}
673 636
674static void doInbound(void) 637void doInbound(Messenger *m)
675{ 638{
676 uint8_t secret_nonce[crypto_box_NONCEBYTES]; 639 uint8_t secret_nonce[crypto_box_NONCEBYTES];
677 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 640 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
678 uint8_t session_key[crypto_box_PUBLICKEYBYTES]; 641 uint8_t session_key[crypto_box_PUBLICKEYBYTES];
679 int inconnection = crypto_inbound(public_key, secret_nonce, session_key); 642 int inconnection = crypto_inbound(public_key, secret_nonce, session_key);
680 if (inconnection != -1) { 643 if (inconnection != -1) {
681 int friend_id = getfriend_id(public_key); 644 int friend_id = getfriend_id(m, public_key);
682 if (friend_id != -1) { 645 if (friend_id != -1) {
683 crypto_kill(friendlist[friend_id].crypt_connection_id); 646 crypto_kill(m->friendlist[friend_id].crypt_connection_id);
684 friendlist[friend_id].crypt_connection_id = 647 m->friendlist[friend_id].crypt_connection_id =
685 accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); 648 accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key);
686 649
687 set_friend_status(friend_id, FRIEND_CONFIRMED); 650 set_friend_status(m, friend_id, FRIEND_CONFIRMED);
688 } 651 }
689 } 652 }
690} 653}
@@ -695,7 +658,7 @@ static void doInbound(void)
695static uint64_t last_LANdiscovery; 658static uint64_t last_LANdiscovery;
696 659
697/*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ 660/*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/
698static void LANdiscovery(void) 661void LANdiscovery(Messenger *m)
699{ 662{
700 if (last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { 663 if (last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
701 send_LANdiscovery(htons(PORT)); 664 send_LANdiscovery(htons(PORT));
@@ -705,27 +668,27 @@ static void LANdiscovery(void)
705 668
706 669
707/* the main loop that needs to be run at least 200 times per second. */ 670/* the main loop that needs to be run at least 200 times per second. */
708void doMessenger(void) 671void doMessenger(Messenger *m)
709{ 672{
710 networking_poll(); 673 networking_poll();
711 674
712 doDHT(); 675 doDHT();
713 doLossless_UDP(); 676 doLossless_UDP();
714 doNetCrypto(); 677 doNetCrypto();
715 doInbound(); 678 doInbound(m);
716 doFriends(); 679 doFriends(m);
717 LANdiscovery(); 680 LANdiscovery(m);
718} 681}
719 682
720/* returns the size of the messenger data (for saving) */ 683/* returns the size of the messenger data (for saving) */
721uint32_t Messenger_size(void) 684uint32_t Messenger_size(Messenger *m)
722{ 685{
723 return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES 686 return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
724 + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends; 687 + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * m->numfriends;
725} 688}
726 689
727/* save the messenger in data of size Messenger_size() */ 690/* save the messenger in data of size Messenger_size() */
728void Messenger_save(uint8_t *data) 691void Messenger_save(Messenger *m, uint8_t *data)
729{ 692{
730 save_keys(data); 693 save_keys(data);
731 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; 694 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
@@ -734,14 +697,14 @@ void Messenger_save(uint8_t *data)
734 data += sizeof(size); 697 data += sizeof(size);
735 DHT_save(data); 698 DHT_save(data);
736 data += size; 699 data += size;
737 size = sizeof(Friend) * numfriends; 700 size = sizeof(Friend) * m->numfriends;
738 memcpy(data, &size, sizeof(size)); 701 memcpy(data, &size, sizeof(size));
739 data += sizeof(size); 702 data += sizeof(size);
740 memcpy(data, friendlist, sizeof(Friend) * numfriends); 703 memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends);
741} 704}
742 705
743/* load the messenger from data of size length. */ 706/* load the messenger from data of size length. */
744int Messenger_load(uint8_t * data, uint32_t length) 707int Messenger_load(Messenger *m, uint8_t * data, uint32_t length)
745{ 708{
746 if (length == ~0) 709 if (length == ~0)
747 return -1; 710 return -1;
@@ -773,11 +736,12 @@ int Messenger_load(uint8_t * data, uint32_t length)
773 uint32_t i; 736 uint32_t i;
774 for (i = 0; i < num; ++i) { 737 for (i = 0; i < num; ++i) {
775 if(temp[i].status != 0) { 738 if(temp[i].status != 0) {
776 int fnum = m_addfriend_norequest(temp[i].client_id); 739 int fnum = m_addfriend_norequest(m, temp[i].client_id);
777 setfriendname(fnum, temp[i].name); 740 setfriendname(m, fnum, temp[i].name);
778 /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ 741 /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
779 } 742 }
780 } 743 }
781 free(temp); 744 free(temp);
782 return 0; 745 return 0;
783} 746}
747