diff options
Diffstat (limited to 'core/Messenger.c')
-rw-r--r-- | core/Messenger.c | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index 4c76dd30..872d7407 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -99,18 +99,21 @@ int getclient_id(int friend_id, uint8_t *client_id) | |||
99 | client_id is the client id of the friend | 99 | client_id is the client id of the friend |
100 | data is the data and length is the length | 100 | data is the data and length is the length |
101 | returns the friend number if success | 101 | returns the friend number if success |
102 | return -1 if failure. */ | 102 | return -1 if key length is wrong. |
103 | return -2 if user's own key | ||
104 | return -3 if already a friend | ||
105 | return -4 for other*/ | ||
103 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | 106 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) |
104 | { | 107 | { |
105 | if (length == 0 || length >= | 108 | if (length == 0 || length >= |
106 | (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) | 109 | (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) |
107 | return -1; | 110 | return -1; |
108 | if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 111 | if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) |
109 | return -1; | 112 | return -2; |
110 | if (getfriend_id(client_id) != -1) | 113 | if (getfriend_id(client_id) != -1) |
111 | return -1; | 114 | return -3; |
112 | uint32_t i; | 115 | uint32_t i; |
113 | for (i = 0; i <= numfriends; ++i) { | 116 | for (i = 0; i <= numfriends; ++i) { /*TODO: dynamic memory allocation, this will segfault if there are more than MAX_NUM_FRIENDS*/ |
114 | if(friendlist[i].status == 0) { | 117 | if(friendlist[i].status == 0) { |
115 | DHT_addfriend(client_id); | 118 | DHT_addfriend(client_id); |
116 | friendlist[i].status = 1; | 119 | friendlist[i].status = 1; |
@@ -126,7 +129,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | |||
126 | return i; | 129 | return i; |
127 | } | 130 | } |
128 | } | 131 | } |
129 | return -1; | 132 | return -4; |
130 | } | 133 | } |
131 | 134 | ||
132 | int m_addfriend_norequest(uint8_t * client_id) | 135 | int m_addfriend_norequest(uint8_t * client_id) |
@@ -134,7 +137,7 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
134 | if (getfriend_id(client_id) != -1) | 137 | if (getfriend_id(client_id) != -1) |
135 | return -1; | 138 | return -1; |
136 | uint32_t i; | 139 | uint32_t i; |
137 | for (i = 0; i <= numfriends; ++i) { | 140 | for (i = 0; i <= numfriends; ++i) {/*TODO: dynamic memory allocation, this will segfault if there are more than MAX_NUM_FRIENDS*/ |
138 | if(friendlist[i].status == 0) { | 141 | if(friendlist[i].status == 0) { |
139 | DHT_addfriend(client_id); | 142 | DHT_addfriend(client_id); |
140 | friendlist[i].status = 2; | 143 | friendlist[i].status = 2; |
@@ -164,7 +167,7 @@ int m_delfriend(int friendnumber) | |||
164 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); | 167 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); |
165 | uint32_t i; | 168 | uint32_t i; |
166 | for (i = numfriends; i != 0; --i) { | 169 | for (i = numfriends; i != 0; --i) { |
167 | if (friendlist[i].status != 0) | 170 | if (friendlist[i-1].status != 0) |
168 | break; | 171 | break; |
169 | } | 172 | } |
170 | numfriends = i; | 173 | numfriends = i; |
@@ -178,7 +181,7 @@ int m_delfriend(int friendnumber) | |||
178 | return 0 if there is no friend with that number */ | 181 | return 0 if there is no friend with that number */ |
179 | int m_friendstatus(int friendnumber) | 182 | int m_friendstatus(int friendnumber) |
180 | { | 183 | { |
181 | if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 184 | if (friendnumber < 0 || friendnumber >= numfriends) |
182 | return 0; | 185 | return 0; |
183 | return friendlist[friendnumber].status; | 186 | return friendlist[friendnumber].status; |
184 | } | 187 | } |
@@ -188,7 +191,7 @@ int m_friendstatus(int friendnumber) | |||
188 | return 0 if it was not */ | 191 | return 0 if it was not */ |
189 | int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) | 192 | int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) |
190 | { | 193 | { |
191 | if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 194 | if (friendnumber < 0 || friendnumber >= numfriends) |
192 | return 0; | 195 | return 0; |
193 | if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) | 196 | if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) |
194 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ | 197 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ |
@@ -240,6 +243,16 @@ int setname(uint8_t * name, uint16_t length) | |||
240 | return 0; | 243 | return 0; |
241 | } | 244 | } |
242 | 245 | ||
246 | /* get our nickname | ||
247 | put it in name | ||
248 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | ||
249 | return the length of the name */ | ||
250 | uint16_t getself_name(uint8_t *name) | ||
251 | { | ||
252 | memcpy(name, self_name, self_name_length); | ||
253 | return self_name_length; | ||
254 | } | ||
255 | |||
243 | /* get name of friendnumber | 256 | /* get name of friendnumber |
244 | put it in name | 257 | put it in name |
245 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | 258 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. |