diff options
author | Astonex <softukitu@gmail.com> | 2013-07-31 23:02:09 +0100 |
---|---|---|
committer | Astonex <softukitu@gmail.com> | 2013-07-31 23:02:09 +0100 |
commit | f05aa308701f33f3bf9df022a4b376deeedef235 (patch) | |
tree | c10b8a3cca6822400853bfd3bc2c5fd1cca73f83 /core/Messenger.c | |
parent | 8dfba27242ca23fca5de852541f2101568dbf7cb (diff) | |
parent | c558cb63f6db35bd51f2f2331e21df03105ee82a (diff) |
Merge remote-tracking branch 'ProjectTox/master'
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 d7770dfe..eb59b81a 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; |
@@ -165,7 +168,7 @@ int m_delfriend(int friendnumber) | |||
165 | uint32_t i; | 168 | uint32_t i; |
166 | 169 | ||
167 | for (i = numfriends; i != 0; --i) { | 170 | for (i = numfriends; i != 0; --i) { |
168 | if (friendlist[i].status != 0) | 171 | if (friendlist[i-1].status != 0) |
169 | break; | 172 | break; |
170 | } | 173 | } |
171 | numfriends = i; | 174 | numfriends = i; |
@@ -180,7 +183,7 @@ int m_delfriend(int friendnumber) | |||
180 | return 0 if there is no friend with that number */ | 183 | return 0 if there is no friend with that number */ |
181 | int m_friendstatus(int friendnumber) | 184 | int m_friendstatus(int friendnumber) |
182 | { | 185 | { |
183 | if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 186 | if (friendnumber < 0 || friendnumber >= numfriends) |
184 | return 0; | 187 | return 0; |
185 | return friendlist[friendnumber].status; | 188 | return friendlist[friendnumber].status; |
186 | } | 189 | } |
@@ -190,7 +193,7 @@ int m_friendstatus(int friendnumber) | |||
190 | return 0 if it was not */ | 193 | return 0 if it was not */ |
191 | int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) | 194 | int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) |
192 | { | 195 | { |
193 | if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 196 | if (friendnumber < 0 || friendnumber >= numfriends) |
194 | return 0; | 197 | return 0; |
195 | if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) | 198 | if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) |
196 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ | 199 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ |
@@ -242,6 +245,16 @@ int setname(uint8_t * name, uint16_t length) | |||
242 | return 0; | 245 | return 0; |
243 | } | 246 | } |
244 | 247 | ||
248 | /* get our nickname | ||
249 | put it in name | ||
250 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | ||
251 | return the length of the name */ | ||
252 | uint16_t getself_name(uint8_t *name) | ||
253 | { | ||
254 | memcpy(name, self_name, self_name_length); | ||
255 | return self_name_length; | ||
256 | } | ||
257 | |||
245 | /* get name of friendnumber | 258 | /* get name of friendnumber |
246 | put it in name | 259 | put it in name |
247 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | 260 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. |