summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/Lossless_UDP.c51
1 files changed, 48 insertions, 3 deletions
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c
index fd17c0c0..eb1314d1 100644
--- a/core/Lossless_UDP.c
+++ b/core/Lossless_UDP.c
@@ -84,11 +84,13 @@ typedef struct {
84 uint8_t timeout; /* connection timeout in seconds. */ 84 uint8_t timeout; /* connection timeout in seconds. */
85} Connection; 85} Connection;
86 86
87#define MAX_CONNECTIONS 256
88 87
89static Connection connections[MAX_CONNECTIONS]; 88static Connection * connections;
90 89
91/* static uint32_t numconnections; */ 90static uint32_t connections_length; /* Length of connections array */
91static uint32_t connections_number; /* Number of connections in connections array */
92
93#define MAX_CONNECTIONS connections_length
92 94
93/* Functions */ 95/* Functions */
94 96
@@ -142,6 +144,17 @@ int new_connection(IP_Port ip_port)
142 int connect = getconnection_id(ip_port); 144 int connect = getconnection_id(ip_port);
143 if (connect != -1) 145 if (connect != -1)
144 return connect; 146 return connect;
147
148 if(connections_number == connections_length) {
149 Connection * temp;
150 temp = realloc(connections, sizeof(Connection) * (connections_length + 1));
151 if(temp == NULL)
152 return -1;
153 memset(&temp[connections_length], 0, sizeof(Connection));
154 ++connections_length;
155 connections = temp;
156 }
157
145 uint32_t i; 158 uint32_t i;
146 for (i = 0; i < MAX_CONNECTIONS; ++i) { 159 for (i = 0; i < MAX_CONNECTIONS; ++i) {
147 if(connections[i].status == 0) { 160 if(connections[i].status == 0) {
@@ -161,6 +174,7 @@ int new_connection(IP_Port ip_port)
161 connections[i].send_counter = 0; 174 connections[i].send_counter = 0;
162 /* add randomness to timeout to prevent connections getting stuck in a loop. */ 175 /* add randomness to timeout to prevent connections getting stuck in a loop. */
163 connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; 176 connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT;
177 ++connections_number;
164 return i; 178 return i;
165 } 179 }
166 } 180 }
@@ -174,6 +188,17 @@ int new_inconnection(IP_Port ip_port)
174{ 188{
175 if (getconnection_id(ip_port) != -1) 189 if (getconnection_id(ip_port) != -1)
176 return -1; 190 return -1;
191
192 if(connections_number == connections_length) {
193 Connection * temp;
194 temp = realloc(connections, sizeof(Connection) * (connections_length + 1));
195 if(temp == NULL)
196 return -1;
197 memset(&temp[connections_length], 0, sizeof(Connection));
198 ++connections_length;
199 connections = temp;
200 }
201
177 uint32_t i; 202 uint32_t i;
178 for (i = 0; i < MAX_CONNECTIONS; ++i) { 203 for (i = 0; i < MAX_CONNECTIONS; ++i) {
179 if (connections[i].status == 0) { 204 if (connections[i].status == 0) {
@@ -190,6 +215,7 @@ int new_inconnection(IP_Port ip_port)
190 /* if this connection isn't handled within the timeout kill it. */ 215 /* if this connection isn't handled within the timeout kill it. */
191 connections[i].killat = current_time() + 1000000UL*connections[i].timeout; 216 connections[i].killat = current_time() + 1000000UL*connections[i].timeout;
192 connections[i].send_counter = 127; 217 connections[i].send_counter = 127;
218 ++connections_number;
193 return i; 219 return i;
194 } 220 }
195 } 221 }
@@ -209,6 +235,23 @@ int incoming_connection()
209 } 235 }
210 return -1; 236 return -1;
211} 237}
238/*Try to free some memory from the connections array.*/
239static void free_connections()
240{
241 uint32_t i;
242 for(i = connections_length; i != 0; --i)
243 if (connections[i - 1].status != 0)
244 break;
245
246 if(connections_length == i)
247 return;
248 Connection * temp;
249 temp = realloc(connections, sizeof(Connection) * i);
250 if(temp == NULL && i != 0)
251 return;
252 connections = temp;
253 connections_length = i;
254}
212 255
213/* return -1 if it could not kill the connection. 256/* return -1 if it could not kill the connection.
214 return 0 if killed successfully */ 257 return 0 if killed successfully */
@@ -218,6 +261,8 @@ int kill_connection(int connection_id)
218 if (connections[connection_id].status > 0) { 261 if (connections[connection_id].status > 0) {
219 connections[connection_id].status = 0; 262 connections[connection_id].status = 0;
220 change_handshake(connections[connection_id].ip_port); 263 change_handshake(connections[connection_id].ip_port);
264 --connections_number;
265 free_connections();
221 return 0; 266 return 0;
222 } 267 }
223 } 268 }