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/Lossless_UDP.c | 174 ++++++++++++++++++++++++++-------------------------- 1 file changed, 88 insertions(+), 86 deletions(-) (limited to 'core/Lossless_UDP.c') diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index 43f61b5b..f9d20b2f 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c @@ -44,14 +44,12 @@ timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION /* initial send rate of data. */ #define DATA_SYNC_RATE 30 -typedef struct -{ +typedef struct { uint8_t data[MAX_DATA_SIZE]; uint16_t size; }Data; -typedef struct -{ +typedef struct { IP_Port ip_port; uint8_t status; /* 0 if connection is dead, 1 if attempting handshake, 2 if handshake is done (we start sending SYNC packets) @@ -97,13 +95,13 @@ static Connection connections[MAX_CONNECTIONS]; /* get connection id from IP_Port return -1 if there are no connections like we are looking for return id if it found it */ -int getconnection_id(IP_Port ip_port) -{ +int getconnection_id(IP_Port ip_port) { uint32_t i; - for(i = 0; i < MAX_CONNECTIONS; ++i) - if(connections[i].ip_port.ip.i == ip_port.ip.i && + for (i = 0; i < MAX_CONNECTIONS; ++i) { + if (connections[i].ip_port.ip.i == ip_port.ip.i && connections[i].ip_port.port == ip_port.port && connections[i].status > 0) - return i; + return i; + } return -1; } @@ -116,12 +114,12 @@ static uint32_t randtable[6][256]; uint32_t handshake_id(IP_Port source) { uint32_t id = 0, i; - for(i = 0; i < 6; ++i) { + for (i = 0; i < 6; ++i) { if(randtable[i][((uint8_t *)&source)[i]] == 0) randtable[i][((uint8_t *)&source)[i]] = random_int(); id ^= randtable[i][((uint8_t *)&source)[i]]; } - if(id == 0) /* id can't be zero */ + if (id == 0) /* id can't be zero */ id = 1; return id; } @@ -141,10 +139,10 @@ void change_handshake(IP_Port source) int new_connection(IP_Port ip_port) { int connect = getconnection_id(ip_port); - if(connect != -1) + if (connect != -1) return connect; uint32_t i; - for(i = 0; i < MAX_CONNECTIONS; ++i) { + for (i = 0; i < MAX_CONNECTIONS; ++i) { if(connections[i].status == 0) { memset(&connections[i], 0, sizeof(Connection)); connections[i].ip_port = ip_port; @@ -173,11 +171,11 @@ int new_connection(IP_Port ip_port) return -1 if it could not initialize the connection. */ int new_inconnection(IP_Port ip_port) { - if(getconnection_id(ip_port) != -1) + if (getconnection_id(ip_port) != -1) return -1; uint32_t i; - for(i = 0; i < MAX_CONNECTIONS; ++i) { - if(connections[i].status == 0) { + for (i = 0; i < MAX_CONNECTIONS; ++i) { + if (connections[i].status == 0) { memset(&connections[i], 0, sizeof(Connection)); connections[i].ip_port = ip_port; connections[i].status = 2; @@ -202,11 +200,12 @@ int new_inconnection(IP_Port ip_port) int incoming_connection() { uint32_t i; - for(i = 0; i < MAX_CONNECTIONS; ++i) - if(connections[i].inbound == 2) { + for (i = 0; i < MAX_CONNECTIONS; ++i) { + if (connections[i].inbound == 2) { connections[i].inbound = 1; return i; } + } return -1; } @@ -214,12 +213,13 @@ int incoming_connection() return 0 if killed successfully */ int kill_connection(int connection_id) { - if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) - if(connections[connection_id].status > 0) { - connections[connection_id].status = 0; - change_handshake(connections[connection_id].ip_port); - return 0; + if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) { + if (connections[connection_id].status > 0) { + connections[connection_id].status = 0; + change_handshake(connections[connection_id].ip_port); + return 0; } + } return -1; } @@ -228,11 +228,12 @@ int kill_connection(int connection_id) return 0 if it will kill it */ int kill_connection_in(int connection_id, uint32_t seconds) { - if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) - if(connections[connection_id].status > 0) { - connections[connection_id].killat = current_time() + 1000000UL*seconds; - return 0; + if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) { + if (connections[connection_id].status > 0) { + connections[connection_id].killat = current_time() + 1000000UL*seconds; + return 0; } + } return -1; } @@ -244,7 +245,7 @@ int kill_connection_in(int connection_id, uint32_t seconds) return 4 if timed out and waiting to be killed */ int is_connected(int connection_id) { - if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) + if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) return connections[connection_id].status; return 0; } @@ -252,7 +253,7 @@ int is_connected(int connection_id) /* returns the ip_port of the corresponding connection. */ IP_Port connection_ip(int connection_id) { - if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) + if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) return connections[connection_id].ip_port; IP_Port zero = {{{0}}, 0}; return zero; @@ -274,7 +275,7 @@ uint32_t recvqueue(int connection_id) return -1 if no packet in queue */ char id_packet(int connection_id) { - if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) + if (recvqueue(connection_id) != 0 && connections[connection_id].status != 0) return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; return -1; } @@ -283,7 +284,7 @@ char id_packet(int connection_id) return length of received packet if successful */ int read_packet(int connection_id, uint8_t * data) { - if(recvqueue(connection_id) != 0) { + if (recvqueue(connection_id) != 0) { uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM; uint16_t size = connections[connection_id].recvbuffer[index].size; memcpy(data, connections[connection_id].recvbuffer[index].data, size); @@ -298,11 +299,11 @@ int read_packet(int connection_id, uint8_t * data) return 1 if data was put into the queue */ int write_packet(int connection_id, uint8_t * data, uint32_t length) { - if(length > MAX_DATA_SIZE) + if (length > MAX_DATA_SIZE) return 0; - if(length == 0) + if (length == 0) return 0; - if(sendqueue(connection_id) < BUFFER_PACKET_NUM) { + if (sendqueue(connection_id) < BUFFER_PACKET_NUM) { uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM; memcpy(connections[connection_id].sendbuffer[index].data, data, length); connections[connection_id].sendbuffer[index].size = length; @@ -318,14 +319,15 @@ uint32_t missing_packets(int connection_id, uint32_t * requested) uint32_t number = 0; uint32_t i; uint32_t temp; - if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */ + if (recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */ return 0; - for(i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++ ) + for (i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++) { if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) { temp = htonl(i); memcpy(requested + number, &temp, 4); ++number; } + } if(number == 0) connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum; return number; @@ -393,14 +395,14 @@ int send_DATA(uint32_t connection_id) { int ret; uint32_t buffer[BUFFER_PACKET_NUM]; - if(connections[connection_id].num_req_paquets > 0) { + if (connections[connection_id].num_req_paquets > 0) { ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]); connections[connection_id].num_req_paquets--; memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4); memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4); return ret; } - if(connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) { + if (connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) { ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum); connections[connection_id].sent_packetnum++; return ret; @@ -415,7 +417,7 @@ int send_DATA(uint32_t connection_id) return 0 if handled correctly, 1 if packet is bad. */ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) { - if(length != (1 + 4 + 4)) + if (length != (1 + 4 + 4)) return 1; uint32_t temp; uint32_t handshake_id1, handshake_id2; @@ -425,13 +427,13 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) memcpy(&temp, packet + 5, 4); handshake_id2 = ntohl(temp); - if(handshake_id2 == 0) { + if (handshake_id2 == 0) { send_handshake(source, handshake_id(source), handshake_id1); return 0; } - if(is_connected(connection) != 1) + if (is_connected(connection) != 1) return 1; - if(handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */ + if (handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */ connections[connection].status = 2; /* NOTE: is this necessary? connections[connection].handshake_id2 = handshake_id1; */ @@ -448,10 +450,10 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) 0 if not. */ int SYNC_valid(uint32_t length) { - if(length < 4 + 4 + 2) + if (length < 4 + 4 + 2) return 0; - if(length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) || - ((length - 4 - 4 - 2) % 4) != 0) + if (length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) || + ((length - 4 - 4 - 2) % 4) != 0) return 0; return 1; } @@ -459,9 +461,9 @@ int SYNC_valid(uint32_t length) /* case 1: */ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) { - if(handshake_id(source) == recv_packetnum) { + if (handshake_id(source) == recv_packetnum) { int x = new_inconnection(source); - if(x != -1) { + if (x != -1) { connections[x].orecv_packetnum = recv_packetnum; connections[x].sent_packetnum = recv_packetnum; connections[x].sendbuff_packetnum = recv_packetnum; @@ -479,7 +481,7 @@ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnu /* case 2: */ int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) { - if(recv_packetnum == connections[connection_id].orecv_packetnum) { + if (recv_packetnum == connections[connection_id].orecv_packetnum) { /* && sent_packetnum == connections[connection_id].osent_packetnum) */ connections[connection_id].status = 3; connections[connection_id].recv_counter = counter; @@ -499,14 +501,14 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */ uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); - if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */ + if (comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */ connections[connection_id].orecv_packetnum = recv_packetnum; connections[connection_id].osent_packetnum = sent_packetnum; connections[connection_id].successful_sent = recv_packetnum; connections[connection_id].last_recvSYNC = current_time(); connections[connection_id].recv_counter = counter; ++connections[connection_id].send_counter; - for(i = 0; i < number; ++i) { + for (i = 0; i < number; ++i) { temp = ntohl(req_packets[i]); memcpy(connections[connection_id].req_packets + i, &temp, 4 * number); } @@ -516,10 +518,10 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui return 1; } -int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source) +int handle_SYNC(uint8_t *packet, uint32_t length, IP_Port source) { - if(!SYNC_valid(length)) + if (!SYNC_valid(length)) return 1; int connection = getconnection_id(source); uint8_t counter; @@ -533,39 +535,39 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source) recv_packetnum = ntohl(temp); memcpy(&temp,packet + 6, 4); sent_packetnum = ntohl(temp); - if(number != 0) + if (number != 0) memcpy(req_packets, packet + 10, 4 * number); - if(connection == -1) + if (connection == -1) return handle_SYNC1(source, recv_packetnum, sent_packetnum); - if(connections[connection].status == 2) + if (connections[connection].status == 2) return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum); - if(connections[connection].status == 3) + if (connections[connection].status == 3) return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number); return 0; } /* add a packet to the received buffer and set the recv_packetnum of the connection to its proper value. return 1 if data was too big, 0 if not. */ -int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) +int add_recv(int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) { - if(size > MAX_DATA_SIZE) + if (size > MAX_DATA_SIZE) return 1; uint32_t i; uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM; uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum; - for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { - if(i == data_num) { + for (i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { + if (i == data_num) { memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; connections[connection_id].last_recvdata = current_time(); - if(sent_packet < BUFFER_PACKET_NUM) + if (sent_packet < BUFFER_PACKET_NUM) connections[connection_id].osent_packetnum = data_num; break; } } - for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { - if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0) + for (i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { + if (connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0) connections[connection_id].recv_packetnum = i; else break; @@ -574,17 +576,17 @@ int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size return 0; } -int handle_data(uint8_t * packet, uint32_t length, IP_Port source) +int handle_data(uint8_t *packet, uint32_t length, IP_Port source) { int connection = getconnection_id(source); - if(connection == -1) + if (connection == -1) return 1; - if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */ + if (connections[connection].status != 3) /* Drop the data packet if connection is not connected. */ return 1; - if(length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) + if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) return 1; uint32_t temp; uint32_t number; @@ -597,9 +599,9 @@ int handle_data(uint8_t * packet, uint32_t length, IP_Port source) /* END of packet handling functions */ -int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) +int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source) { - switch (packet[0]) { + switch (packet[0]) { //TODO: check if no break statement is correct??? case 16: return handle_handshake(packet, length, source); @@ -622,19 +624,19 @@ void doNew() { uint32_t i; uint64_t temp_time = current_time(); - for(i = 0; i < MAX_CONNECTIONS; ++i) { - if(connections[i].status == 1) - if((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { + for (i = 0; i < MAX_CONNECTIONS; ++i) { + if (connections[i].status == 1) + if ((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0); connections[i].last_sent = temp_time; } /* kill all timed out connections */ - if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && + if ( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && connections[i].status != 4) /* kill_connection(i); */ connections[i].status = 4; - if(connections[i].status > 0 && connections[i].killat < temp_time) + if (connections[i].status > 0 && connections[i].killat < temp_time) kill_connection(i); } } @@ -643,9 +645,9 @@ void doSYNC() { uint32_t i; uint64_t temp_time = current_time(); - for(i = 0; i < MAX_CONNECTIONS; ++i) { - if(connections[i].status == 2 || connections[i].status == 3) - if((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { + for (i = 0; i < MAX_CONNECTIONS; ++i) { + if (connections[i].status == 2 || connections[i].status == 3) + if ((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { send_SYNC(i); connections[i].last_SYNC = temp_time; } @@ -657,10 +659,10 @@ void doData() uint32_t i; uint64_t j; uint64_t temp_time = current_time(); - for(i = 0; i < MAX_CONNECTIONS; ++i) - if(connections[i].status == 3 && sendqueue(i) != 0) - if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) { - for(j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate)) + for (i = 0; i < MAX_CONNECTIONS; ++i) + if (connections[i].status == 3 && sendqueue(i) != 0) + if ((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) { + for (j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate)) send_DATA(i); connections[i].last_sent = temp_time; } @@ -675,15 +677,15 @@ void adjustRates() { uint32_t i; uint64_t temp_time = current_time(); - for(i = 0; i < MAX_CONNECTIONS; ++i) { - if(connections[i].status == 1 || connections[i].status == 2) + for (i = 0; i < MAX_CONNECTIONS; ++i) { + if (connections[i].status == 1 || connections[i].status == 2) connections[i].SYNC_rate = MAX_SYNC_RATE; - if(connections[i].status == 3) { - if(sendqueue(i) != 0) { + if (connections[i].status == 3) { + if (sendqueue(i) != 0) { connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE; connections[i].SYNC_rate = MAX_SYNC_RATE; } - else if(connections[i].last_recvdata + 1000000UL > temp_time) + else if (connections[i].last_recvdata + 1000000UL > temp_time) connections[i].SYNC_rate = MAX_SYNC_RATE; else connections[i].SYNC_rate = SYNC_RATE; -- cgit v1.2.3