From 241aca98bdc8106221ee7d7dbcea9f2fa17f24bc Mon Sep 17 00:00:00 2001 From: Konstantin Kowalski Date: Fri, 26 Jul 2013 23:07:25 -0400 Subject: A *lot* of style changes. --- core/Messenger.c | 168 ++++++++++++++++++++++++------------------------------- 1 file changed, 74 insertions(+), 94 deletions(-) (limited to 'core/Messenger.c') diff --git a/core/Messenger.c b/core/Messenger.c index 69d33172..fd95869f 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -24,8 +24,7 @@ #include "Messenger.h" #define MIN(a,b) (((a)<(b))?(a):(b)) -typedef struct -{ +typedef struct { uint8_t client_id[CLIENT_ID_SIZE]; int crypt_connection_id; int friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */ @@ -37,7 +36,7 @@ typedef struct uint16_t userstatus_length; uint8_t userstatus_sent; uint16_t info_size; /* length of the info */ -}Friend; +} Friend; uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; @@ -57,14 +56,15 @@ static uint32_t numfriends; /* return the friend id associated to that public key. return -1 if no such friend */ -int getfriend_id(uint8_t * client_id) +int getfriend_id(uint8_t *client_id) { uint32_t i; - for(i = 0; i < numfriends; ++i) - if(friendlist[i].status > 0) - if(memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) + for (i = 0; i < numfriends; ++i) { + if (friendlist[i].status > 0) + if (memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) return i; + } return -1; } @@ -73,12 +73,12 @@ int getfriend_id(uint8_t * client_id) make sure that client_id is of size CLIENT_ID_SIZE. return 0 if success return -1 if failure. */ -int getclient_id(int friend_id, uint8_t * client_id) +int getclient_id(int friend_id, uint8_t *client_id) { - if(friend_id >= numfriends || friend_id < 0) + if (friend_id >= numfriends || friend_id < 0) return -1; - if(friendlist[friend_id].status > 0) { + if (friendlist[friend_id].status > 0) { memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE); return 0; } @@ -92,20 +92,18 @@ int getclient_id(int friend_id, uint8_t * client_id) data is the data and length is the length returns the friend number if success return -1 if failure. */ -int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) +int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) { - if(length == 0 || length >= + if (length == 0 || length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) return -1; - if(memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) + if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) return -1; - if(getfriend_id(client_id) != -1) + if (getfriend_id(client_id) != -1) return -1; uint32_t i; - for(i = 0; i <= numfriends; ++i) - { - if(friendlist[i].status == 0) - { + for (i = 0; i <= numfriends; ++i) { + if(friendlist[i].status == 0) { DHT_addfriend(client_id); friendlist[i].status = 1; friendlist[i].crypt_connection_id = -1; @@ -125,13 +123,11 @@ int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) int m_addfriend_norequest(uint8_t * client_id) { - if(getfriend_id(client_id) != -1) + if (getfriend_id(client_id) != -1) return -1; uint32_t i; - for(i = 0; i <= numfriends; ++i) - { - if(friendlist[i].status == 0) - { + for (i = 0; i <= numfriends; ++i) { + if(friendlist[i].status == 0) { DHT_addfriend(client_id); friendlist[i].status = 2; friendlist[i].crypt_connection_id = -1; @@ -151,7 +147,7 @@ int m_addfriend_norequest(uint8_t * client_id) return -1 if failure */ int m_delfriend(int friendnumber) { - if(friendnumber >= numfriends || friendnumber < 0) + if (friendnumber >= numfriends || friendnumber < 0) return -1; DHT_delfriend(friendlist[friendnumber].client_id); @@ -159,9 +155,10 @@ int m_delfriend(int friendnumber) free(friendlist[friendnumber].userstatus); memset(&friendlist[friendnumber], 0, sizeof(Friend)); uint32_t i; - for(i = numfriends; i != 0; --i) - if(friendlist[i].status != 0) + for (i = numfriends; i != 0; --i) { + if (friendlist[i].status != 0) break; + } numfriends = i; return 0; } @@ -173,7 +170,7 @@ int m_delfriend(int friendnumber) return 0 if there is no friend with that number */ int m_friendstatus(int friendnumber) { - if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) + if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) return 0; return friendlist[friendnumber].status; } @@ -181,11 +178,11 @@ int m_friendstatus(int friendnumber) /* send a text chat message to an online friend return 1 if packet was successfully put into the send queue return 0 if it was not */ -int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) +int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) { - if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) + if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) return 0; - if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) + if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ return 0; uint8_t temp[MAX_DATA_SIZE]; @@ -208,7 +205,7 @@ static int m_sendname(int friendnumber, uint8_t * name) return -1 if failure */ static int setfriendname(int friendnumber, uint8_t * name) { - if(friendnumber >= numfriends || friendnumber < 0) + if (friendnumber >= numfriends || friendnumber < 0) return -1; memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH); return 0; @@ -220,11 +217,11 @@ static int setfriendname(int friendnumber, uint8_t * name) return -1 if failure */ int setname(uint8_t * name, uint16_t length) { - if(length > MAX_NAME_LENGTH) + if (length > MAX_NAME_LENGTH) return -1; memcpy(self_name, name, length); uint32_t i; - for(i = 0; i < numfriends; ++i) + for (i = 0; i < numfriends; ++i) friendlist[i].name_sent = 0; return 0; } @@ -236,7 +233,7 @@ int setname(uint8_t * name, uint16_t length) return -1 if failure */ int getname(int friendnumber, uint8_t * name) { - if(friendnumber >= numfriends || friendnumber < 0) + if (friendnumber >= numfriends || friendnumber < 0) return -1; memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH); return 0; @@ -244,7 +241,7 @@ int getname(int friendnumber, uint8_t * name) int m_set_userstatus(uint8_t *status, uint16_t length) { - if(length > MAX_USERSTATUS_LENGTH) + if (length > MAX_USERSTATUS_LENGTH) return -1; uint8_t *newstatus = calloc(length, 1); memcpy(newstatus, status, length); @@ -253,7 +250,7 @@ int m_set_userstatus(uint8_t *status, uint16_t length) self_userstatus_len = length; uint32_t i; - for(i = 0; i < numfriends; ++i) + for (i = 0; i < numfriends; ++i) friendlist[i].userstatus_sent = 0; return 0; } @@ -262,7 +259,7 @@ int m_set_userstatus(uint8_t *status, uint16_t length) guaranteed to be at most MAX_USERSTATUS_LENGTH */ int m_get_userstatus_size(int friendnumber) { - if(friendnumber >= numfriends || friendnumber < 0) + if (friendnumber >= numfriends || friendnumber < 0) return -1; return friendlist[friendnumber].userstatus_length; } @@ -271,7 +268,7 @@ int m_get_userstatus_size(int friendnumber) bytes, use m_get_userstatus_size to find out how much you need to allocate */ int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) { - if(friendnumber >= numfriends || friendnumber < 0) + if (friendnumber >= numfriends || friendnumber < 0) return -1; memset(buf, 0, maxlen); memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1); @@ -290,7 +287,7 @@ static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length) static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length) { - if(friendnumber >= numfriends || friendnumber < 0) + if (friendnumber >= numfriends || friendnumber < 0) return -1; uint8_t *newstatus = calloc(length, 1); memcpy(newstatus, status, length); @@ -350,35 +347,29 @@ int initMessenger() return 0; } +//TODO: make this function not suck. static void doFriends() {/* TODO: add incoming connections and some other stuff. */ uint32_t i; int len; uint8_t temp[MAX_DATA_SIZE]; - for(i = 0; i < numfriends; ++i) - { - if(friendlist[i].status == 1) - { + for (i = 0; i < numfriends; ++i) { + if (friendlist[i].status == 1) { int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); - if(fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case - of packet loss */ + if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */ friendlist[i].status = 2; - else - if(fr > 0) + else if (fr > 0) friendlist[i].status = 2; } - if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */ - { - if(friendlist[i].status == 2) - { - if(friendlist[i].friend_request_id + 10 < unix_time()) /*I know this is hackish but it should work.*/ - { + if (friendlist[i].status == 2 || friendlist[i].status == 3) { /* friend is not online */ + if (friendlist[i].status == 2) { + if (friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/ send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); friendlist[i].friend_request_id = unix_time(); } } IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); - switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) { + switch (is_cryptoconnected(friendlist[i].crypt_connection_id)) { case 0: if (friendip.ip.i > 1) friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); @@ -394,24 +385,23 @@ static void doFriends() break; } } - while(friendlist[i].status == 4) /* friend is online */ - { - if(friendlist[i].name_sent == 0) - if(m_sendname(i, self_name)) + while (friendlist[i].status == 4) { /* friend is online */ + if (friendlist[i].name_sent == 0) { + if (m_sendname(i, self_name)) friendlist[i].name_sent = 1; - if(friendlist[i].userstatus_sent == 0) - if(send_userstatus(i, self_userstatus, self_userstatus_len)) + } + if (friendlist[i].userstatus_sent == 0) { + if (send_userstatus(i, self_userstatus, self_userstatus_len)) friendlist[i].userstatus_sent = 1; + } len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); - if(len > 0) - { - switch(temp[0]) { + if (len > 0) { + switch (temp[0]) { case PACKET_ID_NICKNAME: { - if (len != MAX_NAME_LENGTH + 1) break; + if (len != MAX_NAME_LENGTH + 1) + break; if(friend_namechange_isset) - { friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */ - } memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */ break; @@ -419,25 +409,21 @@ static void doFriends() case PACKET_ID_USERSTATUS: { uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1); memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH)); - if(friend_statuschange_isset) - { - friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); - } + if (friend_statuschange_isset) + friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); free(status); break; } case PACKET_ID_MESSAGE: { - if(friend_message_isset) + if (friend_message_isset) (*friend_message)(i, temp + 1, len - 1); break; } } } - else - { - if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) /* if the connection timed out, kill it */ - { + else { + if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ crypto_kill(friendlist[i].crypt_connection_id); friendlist[i].crypt_connection_id = -1; friendlist[i].status = 3; @@ -454,11 +440,9 @@ static void doInbound() uint8_t public_key[crypto_box_PUBLICKEYBYTES]; uint8_t session_key[crypto_box_PUBLICKEYBYTES]; int inconnection = crypto_inbound(public_key, secret_nonce, session_key); - if(inconnection != -1) - { + if (inconnection != -1) { int friend_id = getfriend_id(public_key); - if(friend_id != -1) - { + if (friend_id != -1) { crypto_kill(friendlist[friend_id].crypt_connection_id); friendlist[friend_id].crypt_connection_id = accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); @@ -476,8 +460,7 @@ static uint32_t last_LANdiscovery; /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ static void LANdiscovery() { - if(last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) - { + if (last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { send_LANdiscovery(htons(PORT)); last_LANdiscovery = unix_time(); } @@ -490,12 +473,11 @@ void doMessenger() IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; - while(receivepacket(&ip_port, data, &length) != -1) - { + while (receivepacket(&ip_port, data, &length) != -1) { #ifdef DEBUG /* if(rand() % 3 != 1) //simulate packet loss */ /* { */ - if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && + if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port)) /* if packet is discarded */ printf("Received unhandled packet with length: %u\n", length); @@ -527,7 +509,7 @@ uint32_t Messenger_size() } /* save the messenger in data of size Messenger_size() */ -void Messenger_save(uint8_t * data) +void Messenger_save(uint8_t *data) { save_keys(data); data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; @@ -545,9 +527,9 @@ void Messenger_save(uint8_t * data) /* load the messenger from data of size length. */ int Messenger_load(uint8_t * data, uint32_t length) { - if(length == ~0) + if (length == ~0) return -1; - if(length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) + if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) return -1; length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2; load_keys(data); @@ -556,15 +538,15 @@ int Messenger_load(uint8_t * data, uint32_t length) memcpy(&size, data, sizeof(size)); data += sizeof(size); - if(length < size) + if (length < size) return -1; length -= size; - if(DHT_load(data, size) == -1) + if (DHT_load(data, size) == -1) return -1; data += size; memcpy(&size, data, sizeof(size)); data += sizeof(size); - if(length != size || length % sizeof(Friend) != 0) + if (length != size || length % sizeof(Friend) != 0) return -1; Friend * temp = malloc(size); @@ -573,10 +555,8 @@ int Messenger_load(uint8_t * data, uint32_t length) uint16_t num = size / sizeof(Friend); uint32_t i; - for(i = 0; i < num; ++i) - { - if(temp[i].status != 0) - { + for (i = 0; i < num; ++i) { + if(temp[i].status != 0) { int fnum = m_addfriend_norequest(temp[i].client_id); setfriendname(fnum, temp[i].name); /* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */ -- cgit v1.2.3