Const correctness in toxcore/group_chats.c

This commit is contained in:
Marc Schütz 2014-06-13 22:35:44 +02:00
parent 920a87be35
commit 714b2aeaee
3 changed files with 41 additions and 41 deletions

View File

@ -959,7 +959,7 @@ static int get_chat_num(Messenger *m, Group_Chat *chat)
return -1; return -1;
} }
static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata) static void group_message_function(Group_Chat *chat, int peer_number, const uint8_t *message, uint16_t length, void *userdata)
{ {
Messenger *m = userdata; Messenger *m = userdata;
int i = get_chat_num(m, chat); 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
(*m->group_message)(m, i, peer_number, message_terminated, length, m->group_message_userdata); (*m->group_message)(m, i, peer_number, message_terminated, length, m->group_message_userdata);
} }
static void group_action_function(Group_Chat *chat, int peer_number, uint8_t *action, uint16_t length, void *userdata) static void group_action_function(Group_Chat *chat, int peer_number, const uint8_t *action, uint16_t length, void *userdata)
{ {
Messenger *m = userdata; Messenger *m = userdata;
int i = get_chat_num(m, chat); int i = get_chat_num(m, chat);

View File

@ -65,7 +65,7 @@ typedef struct {
* TODO: make this more efficient. * TODO: make this more efficient.
*/ */
static int peer_in_chat(Group_Chat *chat, uint8_t *client_id) static int peer_in_chat(const Group_Chat *chat, const uint8_t *client_id)
{ {
uint32_t i; uint32_t i;
@ -82,7 +82,7 @@ static int peer_in_chat(Group_Chat *chat, uint8_t *client_id)
* return 1 if client_id1 is closer. * return 1 if client_id1 is closer.
* return 2 if client_id2 is closer. * return 2 if client_id2 is closer.
*/ */
static int id_closest_groupchats(uint8_t *id, uint8_t *id1, uint8_t *id2) static int id_closest_groupchats(const uint8_t *id, const uint8_t *id1, const uint8_t *id2)
{ {
size_t i; size_t i;
uint8_t distance1, distance2; uint8_t distance1, distance2;
@ -110,7 +110,7 @@ static int id_closest_groupchats(uint8_t *id, uint8_t *id1, uint8_t *id2)
* Return -1 if the peer is in the closelist. * Return -1 if the peer is in the closelist.
*/ */
static int peer_okping(Group_Chat *chat, uint8_t *client_id) static int peer_okping(const Group_Chat *chat, const uint8_t *client_id)
{ {
uint32_t i, j = 0; uint32_t i, j = 0;
@ -143,7 +143,7 @@ static int peer_okping(Group_Chat *chat, uint8_t *client_id)
* Return 0 if success. * Return 0 if success.
* Return -1 if peer was not put in list/updated. * Return -1 if peer was not put in list/updated.
*/ */
static int add_closepeer(Group_Chat *chat, uint8_t *client_id, IP_Port ip_port) static int add_closepeer(Group_Chat *chat, const uint8_t *client_id, IP_Port ip_port)
{ {
uint32_t i; uint32_t i;
@ -175,7 +175,7 @@ static int add_closepeer(Group_Chat *chat, uint8_t *client_id, IP_Port ip_port)
return -1; return -1;
} }
static int send_groupchatpacket(Group_Chat *chat, IP_Port ip_port, uint8_t *public_key, uint8_t *data, uint32_t length, static int send_groupchatpacket(const Group_Chat *chat, IP_Port ip_port, const uint8_t *public_key, const uint8_t *data, uint32_t length,
uint8_t request_id) uint8_t request_id)
{ {
if (id_equal(chat->self_public_key, public_key)) 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
* *
* return the number of peers the packet was sent to. * return the number of peers the packet was sent to.
*/ */
static uint8_t sendto_allpeers(Group_Chat *chat, uint8_t *data, uint16_t length, uint8_t request_id) static uint8_t sendto_allpeers(const Group_Chat *chat, const uint8_t *data, uint16_t length, uint8_t request_id)
{ {
uint16_t sent = 0; uint16_t sent = 0;
uint32_t i; uint32_t i;
@ -224,7 +224,7 @@ static uint8_t sendto_allpeers(Group_Chat *chat, uint8_t *data, uint16_t length,
* return peernum if success or peer already in chat. * return peernum if success or peer already in chat.
* return -1 if error. * return -1 if error.
*/ */
static int addpeer(Group_Chat *chat, uint8_t *client_id) static int addpeer(Group_Chat *chat, const uint8_t *client_id)
{ {
int peernum = peer_in_chat(chat, client_id); int peernum = peer_in_chat(chat, client_id);
@ -319,7 +319,7 @@ static int delpeer(Group_Chat *chat, int peernum)
* return length of name if success * return length of name if success
* return -1 if failure * return -1 if failure
*/ */
int group_peername(Group_Chat *chat, int peernum, uint8_t *name) int group_peername(const Group_Chat *chat, int peernum, uint8_t *name)
{ {
if ((uint32_t)peernum >= chat->numpeers) if ((uint32_t)peernum >= chat->numpeers)
return -1; return -1;
@ -334,7 +334,7 @@ int group_peername(Group_Chat *chat, int peernum, uint8_t *name)
return chat->group[peernum].nick_len; return chat->group[peernum].nick_len;
} }
static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t contents_len) static void setnick(Group_Chat *chat, int peernum, const uint8_t *contents, uint16_t contents_len)
{ {
if (contents_len > MAX_NICK_BYTES || contents_len == 0) if (contents_len > MAX_NICK_BYTES || contents_len == 0)
return; return;
@ -355,7 +355,7 @@ static void setnick(Group_Chat *chat, int peernum, uint8_t *contents, uint16_t c
/* TODO: move this to global section */ /* TODO: move this to global section */
#define GROUP_PING_TIMEOUT 5 #define GROUP_PING_TIMEOUT 5
static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum) static int send_getnodes(const Group_Chat *chat, IP_Port ip_port, int peernum)
{ {
if ((uint32_t)peernum >= chat->numpeers) if ((uint32_t)peernum >= chat->numpeers)
return -1; return -1;
@ -382,7 +382,7 @@ static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum)
CRYPTO_PACKET_GROUP_CHAT_GET_NODES); CRYPTO_PACKET_GROUP_CHAT_GET_NODES);
} }
static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid) static int send_sendnodes(const Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid)
{ {
if ((uint32_t)peernum >= chat->numpeers) if ((uint32_t)peernum >= chat->numpeers)
return -1; return -1;
@ -404,7 +404,7 @@ static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64
sizeof(contents.pingid) + sizeof(groupchat_nodes) * j, CRYPTO_PACKET_GROUP_CHAT_SEND_NODES); sizeof(contents.pingid) + sizeof(groupchat_nodes) * j, CRYPTO_PACKET_GROUP_CHAT_SEND_NODES);
} }
static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len) static int handle_getnodes(const Group_Chat *chat, IP_Port source, int peernum, const uint8_t *data, uint32_t len)
{ {
if (len != sizeof(getnodes_data)) if (len != sizeof(getnodes_data))
return 1; return 1;
@ -422,7 +422,7 @@ static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_
return 0; return 0;
} }
static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len) static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, const uint8_t *data, uint32_t len)
{ {
if ((uint32_t)peernum >= chat->numpeers) if ((uint32_t)peernum >= chat->numpeers)
return 1; return 1;
@ -487,7 +487,7 @@ static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8
#define GROUP_DATA_MIN_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + 1) #define GROUP_DATA_MIN_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + 1)
static void send_names_new_peer(Group_Chat *chat); static void send_names_new_peer(Group_Chat *chat);
static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len) static int handle_data(Group_Chat *chat, const uint8_t *data, uint32_t len)
{ {
if (len < GROUP_DATA_MIN_SIZE) if (len < GROUP_DATA_MIN_SIZE)
return 1; return 1;
@ -526,7 +526,7 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
chat->group[peernum].last_message_number = message_num; chat->group[peernum].last_message_number = message_num;
int handled = 1; int handled = 1;
uint8_t *contents = data + GROUP_DATA_MIN_SIZE; const uint8_t *contents = data + GROUP_DATA_MIN_SIZE;
uint16_t contents_len = len - GROUP_DATA_MIN_SIZE; uint16_t contents_len = len - GROUP_DATA_MIN_SIZE;
switch (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)]) { 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)
return 1; return 1;
} }
static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t message_id) static uint8_t send_data(Group_Chat *chat, const uint8_t *data, uint32_t len, uint8_t message_id)
{ {
if (len + GROUP_DATA_MIN_SIZE > MAX_CRYPTO_REQUEST_SIZE) /*NOTE: not the real maximum len.*/ if (len + GROUP_DATA_MIN_SIZE > MAX_CRYPTO_REQUEST_SIZE) /*NOTE: not the real maximum len.*/
return 1; return 1;
@ -614,7 +614,7 @@ static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t
* return 1 if error. * return 1 if error.
*/ */
int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, uint32_t length) int handle_groupchatpacket(Group_Chat *chat, IP_Port source, const uint8_t *packet, uint32_t length)
{ {
if (length > MAX_CRYPTO_REQUEST_SIZE) if (length > MAX_CRYPTO_REQUEST_SIZE)
return 1; return 1;
@ -652,12 +652,12 @@ int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, ui
return 1; return 1;
} }
uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length) uint32_t group_sendmessage(Group_Chat *chat, const uint8_t *message, uint32_t length)
{ {
return send_data(chat, message, length, GROUP_CHAT_CHAT_MESSAGE); //TODO: better return values? return send_data(chat, message, length, GROUP_CHAT_CHAT_MESSAGE); //TODO: better return values?
} }
uint32_t group_sendaction(Group_Chat *chat, uint8_t *action, uint32_t length) uint32_t group_sendaction(Group_Chat *chat, const uint8_t *action, uint32_t length)
{ {
return send_data(chat, action, length, GROUP_CHAT_ACTION); return send_data(chat, action, length, GROUP_CHAT_ACTION);
} }
@ -686,20 +686,20 @@ int set_nick(Group_Chat *chat, const uint8_t *nick, uint16_t nick_len)
return 0; return 0;
} }
uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id) uint32_t group_newpeer(Group_Chat *chat, const uint8_t *client_id)
{ {
addpeer(chat, client_id); addpeer(chat, client_id);
return send_data(chat, client_id, crypto_box_PUBLICKEYBYTES, GROUP_CHAT_NEW_PEER); //TODO: better return values? return send_data(chat, client_id, crypto_box_PUBLICKEYBYTES, GROUP_CHAT_NEW_PEER); //TODO: better return values?
} }
void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, const uint8_t *, uint16_t, void *),
void *userdata) void *userdata)
{ {
chat->group_message = function; chat->group_message = function;
chat->group_message_userdata = userdata; chat->group_message_userdata = userdata;
} }
void callback_groupaction(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), void callback_groupaction(Group_Chat *chat, void (*function)(Group_Chat *chat, int, const uint8_t *, uint16_t, void *),
void *userdata) void *userdata)
{ {
chat->group_action = function; chat->group_action = function;
@ -713,12 +713,12 @@ void callback_namelistchange(Group_Chat *chat, void (*function)(Group_Chat *chat
chat->group_namelistchange_userdata = userdata; chat->group_namelistchange_userdata = userdata;
} }
uint32_t group_numpeers(Group_Chat *chat) uint32_t group_numpeers(const Group_Chat *chat)
{ {
return chat->numpeers; return chat->numpeers;
} }
uint32_t group_client_names(Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length) uint32_t group_client_names(const Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length)
{ {
uint32_t i; uint32_t i;
@ -833,12 +833,12 @@ void kill_groupchat(Group_Chat *chat)
free(chat); free(chat);
} }
void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id) void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, const uint8_t *client_id)
{ {
send_getnodes(chat, ip_port, addpeer(chat, client_id)); send_getnodes(chat, ip_port, addpeer(chat, client_id));
} }
void chat_bootstrap_nonlazy(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id) void chat_bootstrap_nonlazy(Group_Chat *chat, IP_Port ip_port, const uint8_t *client_id)
{ {
send_getnodes(chat, ip_port, addpeer(chat, client_id)); send_getnodes(chat, ip_port, addpeer(chat, client_id));
add_closepeer(chat, client_id, ip_port); add_closepeer(chat, client_id, ip_port);

View File

@ -62,9 +62,9 @@ typedef struct Group_Chat {
uint32_t numpeers; uint32_t numpeers;
uint32_t message_number; uint32_t message_number;
void (*group_message)(struct Group_Chat *m, int, uint8_t *, uint16_t, void *); void (*group_message)(struct Group_Chat *m, int, const uint8_t *, uint16_t, void *);
void *group_message_userdata; void *group_message_userdata;
void (*group_action)(struct Group_Chat *m, int, uint8_t *, uint16_t, void *); void (*group_action)(struct Group_Chat *m, int, const uint8_t *, uint16_t, void *);
void *group_action_userdata; void *group_action_userdata;
void (*peer_namelistchange)(struct Group_Chat *m, int peer, uint8_t change, void *); void (*peer_namelistchange)(struct Group_Chat *m, int peer, uint8_t change, void *);
void *group_namelistchange_userdata; void *group_namelistchange_userdata;
@ -91,14 +91,14 @@ typedef struct Group_Chat {
* return length of name if success * return length of name if success
* return -1 if failure * return -1 if failure
*/ */
int group_peername(Group_Chat *chat, int peernum, uint8_t *name); int group_peername(const Group_Chat *chat, int peernum, uint8_t *name);
/* /*
* Set callback function for chat messages. * Set callback function for chat messages.
* *
* format of function is: function(Group_Chat *chat, peer number, message, message length, userdata) * format of function is: function(Group_Chat *chat, peer number, message, message length, userdata)
*/ */
void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, const uint8_t *, uint16_t, void *),
void *userdata); void *userdata);
/* /*
@ -106,7 +106,7 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat,
* *
* format of function is: function(Group_Chat *chat, peer number, action, action length, userdata) * format of function is: function(Group_Chat *chat, peer number, action, action length, userdata)
*/ */
void callback_groupaction(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), void callback_groupaction(Group_Chat *chat, void (*function)(Group_Chat *chat, int, const uint8_t *, uint16_t, void *),
void *userdata); void *userdata);
/* /*
@ -130,14 +130,14 @@ void callback_namelistchange(Group_Chat *chat, void (*function)(Group_Chat *chat
* *
* returns the number of peers it has sent it to. * returns the number of peers it has sent it to.
*/ */
uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length); uint32_t group_sendmessage(Group_Chat *chat, const uint8_t *message, uint32_t length);
/* /*
* Send an action to the group. * Send an action to the group.
* *
* returns the number of peers it has sent it to. * returns the number of peers it has sent it to.
*/ */
uint32_t group_sendaction(Group_Chat *chat, uint8_t *action, uint32_t length); uint32_t group_sendaction(Group_Chat *chat, const uint8_t *action, uint32_t length);
/* /*
* Set our nick for this group. * Set our nick for this group.
@ -150,7 +150,7 @@ int set_nick(Group_Chat *chat, const uint8_t *nick, uint16_t nick_len);
* Tell everyone about a new peer (a person we are inviting for example.) * Tell everyone about a new peer (a person we are inviting for example.)
* *
*/ */
uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id); uint32_t group_newpeer(Group_Chat *chat, const uint8_t *client_id);
/* Create a new group chat. /* Create a new group chat.
@ -164,7 +164,7 @@ Group_Chat *new_groupchat(Networking_Core *net);
/* Return the number of peers in the group chat. /* Return the number of peers in the group chat.
*/ */
uint32_t group_numpeers(Group_Chat *chat); uint32_t group_numpeers(const Group_Chat *chat);
/* List all the peers in the group chat. /* List all the peers in the group chat.
* *
@ -172,7 +172,7 @@ uint32_t group_numpeers(Group_Chat *chat);
* *
* returns the number of peers. * returns the number of peers.
*/ */
uint32_t group_client_names(Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length); uint32_t group_client_names(const Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[], uint16_t length);
/* Kill a group chat /* Kill a group chat
* *
@ -188,11 +188,11 @@ void do_groupchat(Group_Chat *chat);
/* if we receive a group chat packet we call this function so it can be handled. /* if we receive a group chat packet we call this function so it can be handled.
return 0 if packet is handled correctly. return 0 if packet is handled correctly.
return 1 if it didn't handle the packet or if the packet was shit. */ return 1 if it didn't handle the packet or if the packet was shit. */
int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, uint32_t length); int handle_groupchatpacket(Group_Chat *chat, IP_Port source, const uint8_t *packet, uint32_t length);
void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id); void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, const uint8_t *client_id);
void chat_bootstrap_nonlazy(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id); void chat_bootstrap_nonlazy(Group_Chat *chat, IP_Port ip_port, const uint8_t *client_id);
#endif #endif