summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Schütz <schuetzm@gmx.net>2014-06-13 22:35:44 +0200
committerMarc Schütz <schuetzm@gmx.net>2014-06-13 22:35:44 +0200
commit714b2aeaee09ca04b73cfd05eb42c0cdd57e0f57 (patch)
tree72913c116d1d10e6607aa984a690f5e71688ef18
parent920a87be35e005fc8fe88599dede70cac56d7d6a (diff)
Const correctness in toxcore/group_chats.c
-rw-r--r--toxcore/Messenger.c4
-rw-r--r--toxcore/group_chats.c52
-rw-r--r--toxcore/group_chats.h26
3 files changed, 41 insertions, 41 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index b07b6012..37a7a252 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -959,7 +959,7 @@ static int get_chat_num(Messenger *m, Group_Chat *chat)
959 return -1; 959 return -1;
960} 960}
961 961
962static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata) 962static void group_message_function(Group_Chat *chat, int peer_number, const uint8_t *message, uint16_t length, void *userdata)
963{ 963{
964 Messenger *m = userdata; 964 Messenger *m = userdata;
965 int i = get_chat_num(m, chat); 965 int i = get_chat_num(m, chat);
@@ -975,7 +975,7 @@ static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *m
975 (*m->group_message)(m, i, peer_number, message_terminated, length, m->group_message_userdata); 975 (*m->group_message)(m, i, peer_number, message_terminated, length, m->group_message_userdata);
976} 976}
977 977
978static void group_action_function(Group_Chat *chat, int peer_number, uint8_t *action, uint16_t length, void *userdata) 978static void group_action_function(Group_Chat *chat, int peer_number, const uint8_t *action, uint16_t length, void *userdata)
979{ 979{
980 Messenger *m = userdata; 980 Messenger *m = userdata;
981 int i = get_chat_num(m, chat); 981 int i = get_chat_num(m, chat);
diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c
index 85928436..882b3f42 100644
--- a/toxcore/group_chats.c
+++ b/toxcore/group_chats.c
@@ -65,7 +65,7 @@ typedef struct {
65 * TODO: make this more efficient. 65 * TODO: make this more efficient.
66 */ 66 */
67 67
68static int peer_in_chat(Group_Chat *chat, uint8_t *client_id) 68static int peer_in_chat(const Group_Chat *chat, const uint8_t *client_id)
69{ 69{
70 uint32_t i; 70 uint32_t i;
71 71
@@ -82,7 +82,7 @@ static int peer_in_chat(Group_Chat *chat, uint8_t *client_id)
82 * return 1 if client_id1 is closer. 82 * return 1 if client_id1 is closer.
83 * return 2 if client_id2 is closer. 83 * return 2 if client_id2 is closer.
84 */ 84 */
85static int id_closest_groupchats(uint8_t *id, uint8_t *id1, uint8_t *id2) 85static int id_closest_groupchats(const uint8_t *id, const uint8_t *id1, const uint8_t *id2)
86{ 86{
87 size_t i; 87 size_t i;
88 uint8_t distance1, distance2; 88 uint8_t distance1, distance2;
@@ -110,7 +110,7 @@ static int id_closest_groupchats(uint8_t *id, uint8_t *id1, uint8_t *id2)
110 * Return -1 if the peer is in the closelist. 110 * Return -1 if the peer is in the closelist.
111 */ 111 */
112 112
113static int peer_okping(Group_Chat *chat, uint8_t *client_id) 113static int peer_okping(const Group_Chat *chat, const uint8_t *client_id)
114{ 114{
115 uint32_t i, j = 0; 115 uint32_t i, j = 0;
116 116
@@ -143,7 +143,7 @@ static int peer_okping(Group_Chat *chat, uint8_t *client_id)
143 * Return 0 if success. 143 * Return 0 if success.
144 * Return -1 if peer was not put in list/updated. 144 * Return -1 if peer was not put in list/updated.
145 */ 145 */
146static int add_closepeer(Group_Chat *chat, uint8_t *client_id, IP_Port ip_port) 146static int add_closepeer(Group_Chat *chat, const uint8_t *client_id, IP_Port ip_port)
147{ 147{
148 uint32_t i; 148 uint32_t i;
149 149
@@ -175,7 +175,7 @@ static int add_closepeer(Group_Chat *chat, uint8_t *client_id, IP_Port ip_port)
175 return -1; 175 return -1;
176} 176}
177 177
178static int send_groupchatpacket(Group_Chat *chat, IP_Port ip_port, uint8_t *public_key, uint8_t *data, uint32_t length, 178static int send_groupchatpacket(const Group_Chat *chat, IP_Port ip_port, const uint8_t *public_key, const uint8_t *data, uint32_t length,
179 uint8_t request_id) 179 uint8_t request_id)
180{ 180{
181 if (id_equal(chat->self_public_key, public_key)) 181 if (id_equal(chat->self_public_key, public_key))
@@ -200,7 +200,7 @@ static int send_groupchatpacket(Group_Chat *chat, IP_Port ip_port, uint8_t *publ
200 * 200 *
201 * return the number of peers the packet was sent to. 201 * return the number of peers the packet was sent to.
202 */ 202 */
203static uint8_t sendto_allpeers(Group_Chat *chat, uint8_t *data, uint16_t length, uint8_t request_id) 203static uint8_t sendto_allpeers(const Group_Chat *chat, const uint8_t *data, uint16_t length, uint8_t request_id)
204{ 204{
205 uint16_t sent = 0; 205 uint16_t sent = 0;
206 uint32_t i; 206 uint32_t i;
@@ -224,7 +224,7 @@ static uint8_t sendto_allpeers(Group_Chat *chat, uint8_t *data, uint16_t length,
224 * return peernum if success or peer already in chat. 224 * return peernum if success or peer already in chat.
225 * return -1 if error. 225 * return -1 if error.
226 */ 226 */
227static int addpeer(Group_Chat *chat, uint8_t *client_id) 227static int addpeer(Group_Chat *chat, const uint8_t *client_id)
228{ 228{
229 int peernum = peer_in_chat(chat, client_id); 229 int peernum = peer_in_chat(chat, client_id);
230 230
@@ -319,7 +319,7 @@ static int delpeer(Group_Chat *chat, int peernum)
319 * return length of name if success 319 * return length of name if success
320 * return -1 if failure 320 * return -1 if failure
321 */ 321 */
322int group_peername(Group_Chat *chat, int peernum, uint8_t *name) 322int group_peername(const Group_Chat *chat, int peernum, uint8_t *name)
323{ 323{
324 if ((uint32_t)peernum >= chat->numpeers) 324 if ((uint32_t)peernum >= chat->numpeers)
325 return -1; 325 return -1;
@@ -334,7 +334,7 @@ int group_peername(Group_Chat *chat, int peernum, uint8_t *name)
334 return chat->group[peernum].nick_len; 334 return chat->group[peernum].nick_len;
335} 335}
336 336
337static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t contents_len) 337static void setnick(Group_Chat *chat, int peernum, const uint8_t *contents, uint16_t contents_len)
338{ 338{
339 if (contents_len > MAX_NICK_BYTES || contents_len == 0) 339 if (contents_len > MAX_NICK_BYTES || contents_len == 0)
340 return; 340 return;
@@ -355,7 +355,7 @@ static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t c
355/* TODO: move this to global section */ 355/* TODO: move this to global section */
356#define GROUP_PING_TIMEOUT 5 356#define GROUP_PING_TIMEOUT 5
357 357
358static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum) 358static int send_getnodes(const Group_Chat *chat, IP_Port ip_port, int peernum)
359{ 359{
360 if ((uint32_t)peernum >= chat->numpeers) 360 if ((uint32_t)peernum >= chat->numpeers)
361 return -1; 361 return -1;
@@ -382,7 +382,7 @@ static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum)
382 CRYPTO_PACKET_GROUP_CHAT_GET_NODES); 382 CRYPTO_PACKET_GROUP_CHAT_GET_NODES);
383} 383}
384 384
385static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid) 385static int send_sendnodes(const Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid)
386{ 386{
387 if ((uint32_t)peernum >= chat->numpeers) 387 if ((uint32_t)peernum >= chat->numpeers)
388 return -1; 388 return -1;
@@ -404,7 +404,7 @@ static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64
404 sizeof(contents.pingid) + sizeof(groupchat_nodes) * j, CRYPTO_PACKET_GROUP_CHAT_SEND_NODES); 404 sizeof(contents.pingid) + sizeof(groupchat_nodes) * j, CRYPTO_PACKET_GROUP_CHAT_SEND_NODES);
405} 405}
406 406
407static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len) 407static int handle_getnodes(const Group_Chat *chat, IP_Port source, int peernum, const uint8_t *data, uint32_t len)
408{ 408{
409 if (len != sizeof(getnodes_data)) 409 if (len != sizeof(getnodes_data))
410 return 1; 410 return 1;
@@ -422,7 +422,7 @@ static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_
422 return 0; 422 return 0;
423} 423}
424 424
425static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len) 425static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, const uint8_t *data, uint32_t len)
426{ 426{
427 if ((uint32_t)peernum >= chat->numpeers) 427 if ((uint32_t)peernum >= chat->numpeers)
428 return 1; 428 return 1;
@@ -487,7 +487,7 @@ static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8
487#define GROUP_DATA_MIN_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + 1) 487#define GROUP_DATA_MIN_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + 1)
488static void send_names_new_peer(Group_Chat *chat); 488static void send_names_new_peer(Group_Chat *chat);
489 489
490static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len) 490static int handle_data(Group_Chat *chat, const uint8_t *data, uint32_t len)
491{ 491{
492 if (len < GROUP_DATA_MIN_SIZE) 492 if (len < GROUP_DATA_MIN_SIZE)
493 return 1; 493 return 1;
@@ -526,7 +526,7 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
526 chat->group[peernum].last_message_number = message_num; 526 chat->group[peernum].last_message_number = message_num;
527 527
528 int handled = 1; 528 int handled = 1;
529 uint8_t *contents = data + GROUP_DATA_MIN_SIZE; 529 const uint8_t *contents = data + GROUP_DATA_MIN_SIZE;
530 uint16_t contents_len = len - GROUP_DATA_MIN_SIZE; 530 uint16_t contents_len = len - GROUP_DATA_MIN_SIZE;
531 531
532 switch (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)]) { 532 switch (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)]) {
@@ -585,7 +585,7 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
585 return 1; 585 return 1;
586} 586}
587 587
588static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t message_id) 588static uint8_t send_data(Group_Chat *chat, const uint8_t *data, uint32_t len, uint8_t message_id)
589{ 589{
590 if (len + GROUP_DATA_MIN_SIZE > MAX_CRYPTO_REQUEST_SIZE) /*NOTE: not the real maximum len.*/ 590 if (len + GROUP_DATA_MIN_SIZE > MAX_CRYPTO_REQUEST_SIZE) /*NOTE: not the real maximum len.*/
591 return 1; 591 return 1;
@@ -614,7 +614,7 @@ static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t
614 * return 1 if error. 614 * return 1 if error.
615 */ 615 */
616 616
617int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, uint32_t length) 617int handle_groupchatpacket(Group_Chat *chat, IP_Port source, const uint8_t *packet, uint32_t length)
618{ 618{
619 if (length > MAX_CRYPTO_REQUEST_SIZE) 619 if (length > MAX_CRYPTO_REQUEST_SIZE)
620 return 1; 620 return 1;
@@ -652,12 +652,12 @@ int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, ui
652 return 1; 652 return 1;
653} 653}
654 654
655uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length) 655uint32_t group_sendmessage(Group_Chat *chat, const uint8_t *message, uint32_t length)
656{ 656{
657 return send_data(chat, message, length, GROUP_CHAT_CHAT_MESSAGE); //TODO: better return values? 657 return send_data(chat, message, length, GROUP_CHAT_CHAT_MESSAGE); //TODO: better return values?
658} 658}
659 659
660uint32_t group_sendaction(Group_Chat *chat, uint8_t *action, uint32_t length) 660uint32_t group_sendaction(Group_Chat *chat, const uint8_t *action, uint32_t length)
661{ 661{
662 return send_data(chat, action, length, GROUP_CHAT_ACTION); 662 return send_data(chat, action, length, GROUP_CHAT_ACTION);
663} 663}
@@ -686,20 +686,20 @@ int set_nick(Group_Chat *chat, const uint8_t *nick, uint16_t nick_len)
686 return 0; 686 return 0;
687} 687}
688 688
689uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id) 689uint32_t group_newpeer(Group_Chat *chat, const uint8_t *client_id)
690{ 690{
691 addpeer(chat, client_id); 691 addpeer(chat, client_id);
692 return send_data(chat, client_id, crypto_box_PUBLICKEYBYTES, GROUP_CHAT_NEW_PEER); //TODO: better return values? 692 return send_data(chat, client_id, crypto_box_PUBLICKEYBYTES, GROUP_CHAT_NEW_PEER); //TODO: better return values?
693} 693}
694 694
695void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), 695void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, const uint8_t *, uint16_t, void *),
696 void *userdata) 696 void *userdata)
697{ 697{
698 chat->group_message = function; 698 chat->group_message = function;
699 chat->group_message_userdata = userdata; 699 chat->group_message_userdata = userdata;
700} 700}
701 701
702void callback_groupaction(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), 702void callback_groupaction(Group_Chat *chat, void (*function)(Group_Chat *chat, int, const uint8_t *, uint16_t, void *),
703 void *userdata) 703 void *userdata)
704{ 704{
705 chat->group_action = function; 705 chat->group_action = function;
@@ -713,12 +713,12 @@ void callback_namelistchange(Group_Chat *chat, void (*function)(Group_Chat *chat
713 chat->group_namelistchange_userdata = userdata; 713 chat->group_namelistchange_userdata = userdata;
714} 714}
715 715
716uint32_t group_numpeers(Group_Chat *chat) 716uint32_t group_numpeers(const Group_Chat *chat)
717{ 717{
718 return chat->numpeers; 718 return chat->numpeers;
719} 719}
720 720
721uint32_t group_client_names(Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length) 721uint32_t group_client_names(const Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length)
722{ 722{
723 uint32_t i; 723 uint32_t i;
724 724
@@ -833,12 +833,12 @@ void kill_groupchat(Group_Chat *chat)
833 free(chat); 833 free(chat);
834} 834}
835 835
836void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id) 836void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, const uint8_t *client_id)
837{ 837{
838 send_getnodes(chat, ip_port, addpeer(chat, client_id)); 838 send_getnodes(chat, ip_port, addpeer(chat, client_id));
839} 839}
840 840
841void chat_bootstrap_nonlazy(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id) 841void chat_bootstrap_nonlazy(Group_Chat *chat, IP_Port ip_port, const uint8_t *client_id)
842{ 842{
843 send_getnodes(chat, ip_port, addpeer(chat, client_id)); 843 send_getnodes(chat, ip_port, addpeer(chat, client_id));
844 add_closepeer(chat, client_id, ip_port); 844 add_closepeer(chat, client_id, ip_port);
diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h
index 49b83c81..802d345b 100644
--- a/toxcore/group_chats.h
+++ b/toxcore/group_chats.h
@@ -62,9 +62,9 @@ typedef struct Group_Chat {
62 uint32_t numpeers; 62 uint32_t numpeers;
63 63
64 uint32_t message_number; 64 uint32_t message_number;
65 void (*group_message)(struct Group_Chat *m, int, uint8_t *, uint16_t, void *); 65 void (*group_message)(struct Group_Chat *m, int, const uint8_t *, uint16_t, void *);
66 void *group_message_userdata; 66 void *group_message_userdata;
67 void (*group_action)(struct Group_Chat *m, int, uint8_t *, uint16_t, void *); 67 void (*group_action)(struct Group_Chat *m, int, const uint8_t *, uint16_t, void *);
68 void *group_action_userdata; 68 void *group_action_userdata;
69 void (*peer_namelistchange)(struct Group_Chat *m, int peer, uint8_t change, void *); 69 void (*peer_namelistchange)(struct Group_Chat *m, int peer, uint8_t change, void *);
70 void *group_namelistchange_userdata; 70 void *group_namelistchange_userdata;
@@ -91,14 +91,14 @@ typedef struct Group_Chat {
91 * return length of name if success 91 * return length of name if success
92 * return -1 if failure 92 * return -1 if failure
93 */ 93 */
94int group_peername(Group_Chat *chat, int peernum, uint8_t *name); 94int group_peername(const Group_Chat *chat, int peernum, uint8_t *name);
95 95
96/* 96/*
97 * Set callback function for chat messages. 97 * Set callback function for chat messages.
98 * 98 *
99 * format of function is: function(Group_Chat *chat, peer number, message, message length, userdata) 99 * format of function is: function(Group_Chat *chat, peer number, message, message length, userdata)
100 */ 100 */
101void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), 101void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, const uint8_t *, uint16_t, void *),
102 void *userdata); 102 void *userdata);
103 103
104/* 104/*
@@ -106,7 +106,7 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat,
106 * 106 *
107 * format of function is: function(Group_Chat *chat, peer number, action, action length, userdata) 107 * format of function is: function(Group_Chat *chat, peer number, action, action length, userdata)
108 */ 108 */
109void callback_groupaction(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), 109void callback_groupaction(Group_Chat *chat, void (*function)(Group_Chat *chat, int, const uint8_t *, uint16_t, void *),
110 void *userdata); 110 void *userdata);
111 111
112/* 112/*
@@ -130,14 +130,14 @@ void callback_namelistchange(Group_Chat *chat, void (*function)(Group_Chat *chat
130 * 130 *
131 * returns the number of peers it has sent it to. 131 * returns the number of peers it has sent it to.
132 */ 132 */
133uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length); 133uint32_t group_sendmessage(Group_Chat *chat, const uint8_t *message, uint32_t length);
134 134
135/* 135/*
136 * Send an action to the group. 136 * Send an action to the group.
137 * 137 *
138 * returns the number of peers it has sent it to. 138 * returns the number of peers it has sent it to.
139 */ 139 */
140uint32_t group_sendaction(Group_Chat *chat, uint8_t *action, uint32_t length); 140uint32_t group_sendaction(Group_Chat *chat, const uint8_t *action, uint32_t length);
141 141
142/* 142/*
143 * Set our nick for this group. 143 * Set our nick for this group.
@@ -150,7 +150,7 @@ int set_nick(Group_Chat *chat, const uint8_t *nick, uint16_t nick_len);
150 * Tell everyone about a new peer (a person we are inviting for example.) 150 * Tell everyone about a new peer (a person we are inviting for example.)
151 * 151 *
152 */ 152 */
153uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id); 153uint32_t group_newpeer(Group_Chat *chat, const uint8_t *client_id);
154 154
155 155
156/* Create a new group chat. 156/* Create a new group chat.
@@ -164,7 +164,7 @@ Group_Chat *new_groupchat(Networking_Core *net);
164 164
165/* Return the number of peers in the group chat. 165/* Return the number of peers in the group chat.
166 */ 166 */
167uint32_t group_numpeers(Group_Chat *chat); 167uint32_t group_numpeers(const Group_Chat *chat);
168 168
169/* List all the peers in the group chat. 169/* List all the peers in the group chat.
170 * 170 *
@@ -172,7 +172,7 @@ uint32_t group_numpeers(Group_Chat *chat);
172 * 172 *
173 * returns the number of peers. 173 * returns the number of peers.
174 */ 174 */
175uint32_t group_client_names(Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length); 175uint32_t group_client_names(const Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length);
176 176
177/* Kill a group chat 177/* Kill a group chat
178 * 178 *
@@ -188,11 +188,11 @@ void do_groupchat(Group_Chat *chat);
188/* if we receive a group chat packet we call this function so it can be handled. 188/* if we receive a group chat packet we call this function so it can be handled.
189 return 0 if packet is handled correctly. 189 return 0 if packet is handled correctly.
190 return 1 if it didn't handle the packet or if the packet was shit. */ 190 return 1 if it didn't handle the packet or if the packet was shit. */
191int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, uint32_t length); 191int handle_groupchatpacket(Group_Chat *chat, IP_Port source, const uint8_t *packet, uint32_t length);
192 192
193 193
194void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id); 194void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, const uint8_t *client_id);
195void chat_bootstrap_nonlazy(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id); 195void chat_bootstrap_nonlazy(Group_Chat *chat, IP_Port ip_port, const uint8_t *client_id);
196 196
197 197
198#endif 198#endif