summaryrefslogtreecommitdiff
path: root/core/Messenger.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-27 21:08:06 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-27 21:08:06 -0400
commitdeb124f5393f8938709ac0bd5ab00082e5cb3723 (patch)
tree9d2878735b88ef91e637029188249f845735a9c9 /core/Messenger.c
parentb83359cd0594fae7789697b1d71a5aeb4c09a1fa (diff)
Fixed name related functions.
They now should give proper lengths and take length into account when sending.
Diffstat (limited to 'core/Messenger.c')
-rw-r--r--core/Messenger.c28
1 files changed, 18 insertions, 10 deletions
diff --git a/core/Messenger.c b/core/Messenger.c
index 86056df8..f77b4491 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -41,6 +41,8 @@ typedef struct {
41uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; 41uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
42 42
43static uint8_t self_name[MAX_NAME_LENGTH]; 43static uint8_t self_name[MAX_NAME_LENGTH];
44static uint16_t self_name_length;
45
44static uint8_t *self_userstatus; 46static uint8_t *self_userstatus;
45static uint16_t self_userstatus_len; 47static uint16_t self_userstatus_len;
46 48
@@ -191,13 +193,16 @@ int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length)
191 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1); 193 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
192} 194}
193 195
194/* send a name packet to friendnumber */ 196/* send a name packet to friendnumber
195static int m_sendname(int friendnumber, uint8_t * name) 197 length is the length with the NULL terminator*/
198static int m_sendname(int friendnumber, uint8_t * name, uint16_t length)
196{ 199{
200 if(length > MAX_NAME_LENGTH || length == 0)
201 return 0;
197 uint8_t temp[MAX_NAME_LENGTH + 1]; 202 uint8_t temp[MAX_NAME_LENGTH + 1];
198 memcpy(temp + 1, name, MAX_NAME_LENGTH); 203 memcpy(temp + 1, name, length);
199 temp[0] = PACKET_ID_NICKNAME; 204 temp[0] = PACKET_ID_NICKNAME;
200 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, MAX_NAME_LENGTH + 1); 205 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
201} 206}
202 207
203/* set the name of a friend 208/* set the name of a friend
@@ -213,13 +218,16 @@ static int setfriendname(int friendnumber, uint8_t * name)
213 218
214/* Set our nickname 219/* Set our nickname
215 name must be a string of maximum MAX_NAME_LENGTH length. 220 name must be a string of maximum MAX_NAME_LENGTH length.
221 length must be at least 1 byte
222 length is the length of name with the NULL terminator
216 return 0 if success 223 return 0 if success
217 return -1 if failure */ 224 return -1 if failure */
218int setname(uint8_t * name, uint16_t length) 225int setname(uint8_t * name, uint16_t length)
219{ 226{
220 if (length > MAX_NAME_LENGTH) 227 if (length > MAX_NAME_LENGTH || length == 0)
221 return -1; 228 return -1;
222 memcpy(self_name, name, length); 229 memcpy(self_name, name, length);
230 self_name_length = length;
223 uint32_t i; 231 uint32_t i;
224 for (i = 0; i < numfriends; ++i) 232 for (i = 0; i < numfriends; ++i)
225 friendlist[i].name_sent = 0; 233 friendlist[i].name_sent = 0;
@@ -388,7 +396,7 @@ static void doFriends()
388 } 396 }
389 while (friendlist[i].status == 4) { /* friend is online */ 397 while (friendlist[i].status == 4) { /* friend is online */
390 if (friendlist[i].name_sent == 0) { 398 if (friendlist[i].name_sent == 0) {
391 if (m_sendname(i, self_name)) 399 if (m_sendname(i, self_name, self_name_length))
392 friendlist[i].name_sent = 1; 400 friendlist[i].name_sent = 1;
393 } 401 }
394 if (friendlist[i].userstatus_sent == 0) { 402 if (friendlist[i].userstatus_sent == 0) {
@@ -399,12 +407,12 @@ static void doFriends()
399 if (len > 0) { 407 if (len > 0) {
400 switch (temp[0]) { 408 switch (temp[0]) {
401 case PACKET_ID_NICKNAME: { 409 case PACKET_ID_NICKNAME: {
402 if (len != MAX_NAME_LENGTH + 1) 410 if (len >= MAX_NAME_LENGTH + 1 || len == 1)
403 break; 411 break;
404 if(friend_namechange_isset) 412 if(friend_namechange_isset)
405 friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */ 413 friend_namechange(i, temp + 1, len - 1);
406 memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); 414 memcpy(friendlist[i].name, temp + 1, len - 1);
407 friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */ 415 friendlist[i].name[len - 2] = 0; /* make sure the NULL terminator is present. */
408 break; 416 break;
409 } 417 }
410 case PACKET_ID_USERSTATUS: { 418 case PACKET_ID_USERSTATUS: {