diff options
Diffstat (limited to 'toxcore/group_chats.c')
-rw-r--r-- | toxcore/group_chats.c | 52 |
1 files changed, 26 insertions, 26 deletions
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 | ||
68 | static int peer_in_chat(Group_Chat *chat, uint8_t *client_id) | 68 | static 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 | */ |
85 | static int id_closest_groupchats(uint8_t *id, uint8_t *id1, uint8_t *id2) | 85 | static 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 | ||
113 | static int peer_okping(Group_Chat *chat, uint8_t *client_id) | 113 | static 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 | */ |
146 | static int add_closepeer(Group_Chat *chat, uint8_t *client_id, IP_Port ip_port) | 146 | static 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 | ||
178 | static int send_groupchatpacket(Group_Chat *chat, IP_Port ip_port, uint8_t *public_key, uint8_t *data, uint32_t length, | 178 | static 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 | */ |
203 | static uint8_t sendto_allpeers(Group_Chat *chat, uint8_t *data, uint16_t length, uint8_t request_id) | 203 | static 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 | */ |
227 | static int addpeer(Group_Chat *chat, uint8_t *client_id) | 227 | static 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 | */ |
322 | int group_peername(Group_Chat *chat, int peernum, uint8_t *name) | 322 | int 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 | ||
337 | static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t contents_len) | 337 | static 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 | ||
358 | static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum) | 358 | static 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 | ||
385 | static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid) | 385 | static 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 | ||
407 | static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len) | 407 | static 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 | ||
425 | static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len) | 425 | static 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) |
488 | static void send_names_new_peer(Group_Chat *chat); | 488 | static void send_names_new_peer(Group_Chat *chat); |
489 | 489 | ||
490 | static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len) | 490 | static 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 | ||
588 | static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t message_id) | 588 | static 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 | ||
617 | int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, uint32_t length) | 617 | int 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 | ||
655 | uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length) | 655 | uint32_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 | ||
660 | uint32_t group_sendaction(Group_Chat *chat, uint8_t *action, uint32_t length) | 660 | uint32_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 | ||
689 | uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id) | 689 | uint32_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 | ||
695 | void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), | 695 | void 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 | ||
702 | void callback_groupaction(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), | 702 | void 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 | ||
716 | uint32_t group_numpeers(Group_Chat *chat) | 716 | uint32_t group_numpeers(const Group_Chat *chat) |
717 | { | 717 | { |
718 | return chat->numpeers; | 718 | return chat->numpeers; |
719 | } | 719 | } |
720 | 720 | ||
721 | uint32_t group_client_names(Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length) | 721 | uint32_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 | ||
836 | void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id) | 836 | void 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 | ||
841 | void chat_bootstrap_nonlazy(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id) | 841 | void 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); |