Code cleanups.

This commit is contained in:
irungentoo 2013-11-08 21:01:20 -05:00
parent e1b215cfa9
commit e30105af20
3 changed files with 20 additions and 13 deletions

View File

@ -289,7 +289,7 @@ static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum)
chat->group[peernum].last_pinged = unix_time(); chat->group[peernum].last_pinged = unix_time();
chat->group[peernum].pingid = contents.pingid; chat->group[peernum].pingid = contents.pingid;
return send_groupchatpacket(chat, ip_port, chat->group[peernum].client_id, (uint8_t *)&contents, sizeof(contents), 48); return send_groupchatpacket(chat, ip_port, chat->group[peernum].client_id, (uint8_t *)&contents, sizeof(contents), 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(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid)
@ -310,7 +310,7 @@ static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64
} }
return send_groupchatpacket(chat, ip_port, chat->group[peernum].client_id, (uint8_t *)&contents, return send_groupchatpacket(chat, ip_port, chat->group[peernum].client_id, (uint8_t *)&contents,
sizeof(contents.pingid) + sizeof(groupchat_nodes) * j, 49); 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(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len)
@ -414,21 +414,21 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
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)]) {
case 0: /* If message is ping */ case GROUP_CHAT_PING: /* If message is ping */
if (contents_len != 0) if (contents_len != 0)
return 1; return 1;
chat->group[peernum].last_recv_msgping = unix_time(); chat->group[peernum].last_recv_msgping = unix_time();
break; break;
case 16: /* If message is new peer */ case GROUP_CHAT_NEW_PEER: /* If message is new peer */
if (contents_len != crypto_box_PUBLICKEYBYTES) if (contents_len != crypto_box_PUBLICKEYBYTES)
return 1; return 1;
addpeer(chat, contents); addpeer(chat, contents);
break; break;
case 64: /* If message is chat message */ case GROUP_CHAT_CHAT_MESSAGE: /* If message is chat message */
if (chat->group_message != NULL) if (chat->group_message != NULL)
(*chat->group_message)(chat, peernum, contents, contents_len, chat->group_message_userdata); (*chat->group_message)(chat, peernum, contents, contents_len, chat->group_message_userdata);
@ -441,7 +441,7 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
} }
if (handled == 1) { if (handled == 1) {
sendto_allpeers(chat, data, len, 50); sendto_allpeers(chat, data, len, CRYPTO_PACKET_GROUP_CHAT_BROADCAST);
return 0; return 0;
} }
@ -468,7 +468,7 @@ static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t
memcpy(packet + GROUP_DATA_MIN_SIZE, data, len); memcpy(packet + GROUP_DATA_MIN_SIZE, data, len);
packet[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] = message_id; packet[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] = message_id;
return sendto_allpeers(chat, packet, len + GROUP_DATA_MIN_SIZE, 50); return sendto_allpeers(chat, packet, len + GROUP_DATA_MIN_SIZE, CRYPTO_PACKET_GROUP_CHAT_BROADCAST);
} }
/* /*
* Handle get nodes group packet. * Handle get nodes group packet.
@ -499,13 +499,13 @@ int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, ui
return 1; return 1;
switch (number) { switch (number) {
case 48: case CRYPTO_PACKET_GROUP_CHAT_GET_NODES:
return handle_getnodes(chat, source, peernum, data, len); return handle_getnodes(chat, source, peernum, data, len);
case 49: case CRYPTO_PACKET_GROUP_CHAT_SEND_NODES:
return handle_sendnodes(chat, source, peernum, data, len); return handle_sendnodes(chat, source, peernum, data, len);
case 50: case CRYPTO_PACKET_GROUP_CHAT_BROADCAST:
return handle_data(chat, data, len); return handle_data(chat, data, len);
default: default:
@ -517,13 +517,13 @@ int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, ui
uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length) uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length)
{ {
return send_data(chat, message, length, 64); //TODO: better return values? return send_data(chat, message, length, GROUP_CHAT_CHAT_MESSAGE); //TODO: better return values?
} }
uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id) uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id)
{ {
addpeer(chat, client_id); addpeer(chat, client_id);
return send_data(chat, client_id, crypto_box_PUBLICKEYBYTES, 16); //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, uint8_t *, uint16_t, void *),
@ -572,7 +572,7 @@ static void ping_close(Group_Chat *chat)
static void ping_group(Group_Chat *chat) static void ping_group(Group_Chat *chat)
{ {
if (is_timeout(chat->last_sent_ping, GROUP_PING_INTERVAL)) { if (is_timeout(chat->last_sent_ping, GROUP_PING_INTERVAL)) {
if (send_data(chat, 0, 0, 0) != 0) /* Ping */ if (send_data(chat, 0, 0, GROUP_CHAT_PING) != 0) /* Ping */
chat->last_sent_ping = unix_time(); chat->last_sent_ping = unix_time();
} }
} }

View File

@ -67,6 +67,10 @@ typedef struct Group_Chat {
} Group_Chat; } Group_Chat;
#define GROUP_CHAT_PING 0
#define GROUP_CHAT_NEW_PEER 16
#define GROUP_CHAT_CHAT_MESSAGE 64
/* Copy the name of peernum to name. /* Copy the name of peernum to name.
* name must be at least MAX_NICK_BYTES long. * name must be at least MAX_NICK_BYTES long.
* *

View File

@ -28,6 +28,9 @@
#define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */ #define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */
#define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */ #define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */
#define CRYPTO_PACKET_GROUP_CHAT_GET_NODES 48 /* Group chat get Nodes packet */
#define CRYPTO_PACKET_GROUP_CHAT_SEND_NODES 49 /* Group chat send Nodes packet */
#define CRYPTO_PACKET_GROUP_CHAT_BROADCAST 50 /* Group chat broadcast packet */
#define CRYPTO_HANDSHAKE_TIMEOUT (CONNECTION_TIMEOUT * 2) #define CRYPTO_HANDSHAKE_TIMEOUT (CONNECTION_TIMEOUT * 2)
#define CRYPTO_CONN_NO_CONNECTION 0 #define CRYPTO_CONN_NO_CONNECTION 0