summaryrefslogtreecommitdiff
path: root/toxcore/TCP_server.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/TCP_server.c')
-rw-r--r--toxcore/TCP_server.c55
1 files changed, 26 insertions, 29 deletions
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index 207a5107..7fe17434 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -177,7 +177,7 @@ static int kill_accepted(TCP_Server *tcp_server, int index);
177 * return index on success 177 * return index on success
178 * return -1 on failure 178 * return -1 on failure
179 */ 179 */
180static int add_accepted(TCP_Server *tcp_server, const TCP_Secure_Connection *con) 180static int add_accepted(TCP_Server *tcp_server, const Mono_Time *mono_time, const TCP_Secure_Connection *con)
181{ 181{
182 int index = get_TCP_connection_index(tcp_server, con->public_key); 182 int index = get_TCP_connection_index(tcp_server, con->public_key);
183 183
@@ -216,7 +216,7 @@ static int add_accepted(TCP_Server *tcp_server, const TCP_Secure_Connection *con
216 tcp_server->accepted_connection_array[index].status = TCP_STATUS_CONFIRMED; 216 tcp_server->accepted_connection_array[index].status = TCP_STATUS_CONFIRMED;
217 ++tcp_server->num_accepted_connections; 217 ++tcp_server->num_accepted_connections;
218 tcp_server->accepted_connection_array[index].identifier = ++tcp_server->counter; 218 tcp_server->accepted_connection_array[index].identifier = ++tcp_server->counter;
219 tcp_server->accepted_connection_array[index].last_pinged = unix_time(); 219 tcp_server->accepted_connection_array[index].last_pinged = mono_time_get(mono_time);
220 tcp_server->accepted_connection_array[index].ping_id = 0; 220 tcp_server->accepted_connection_array[index].ping_id = 0;
221 221
222 return index; 222 return index;
@@ -954,10 +954,11 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint
954} 954}
955 955
956 956
957static int confirm_TCP_connection(TCP_Server *tcp_server, TCP_Secure_Connection *con, const uint8_t *data, 957static int confirm_TCP_connection(TCP_Server *tcp_server, const Mono_Time *mono_time, TCP_Secure_Connection *con,
958 const uint8_t *data,
958 uint16_t length) 959 uint16_t length)
959{ 960{
960 int index = add_accepted(tcp_server, con); 961 int index = add_accepted(tcp_server, mono_time, con);
961 962
962 if (index == -1) { 963 if (index == -1) {
963 kill_TCP_secure_connection(con); 964 kill_TCP_secure_connection(con);
@@ -1161,7 +1162,7 @@ static int do_incoming(TCP_Server *tcp_server, uint32_t i)
1161 return -1; 1162 return -1;
1162} 1163}
1163 1164
1164static int do_unconfirmed(TCP_Server *tcp_server, uint32_t i) 1165static int do_unconfirmed(TCP_Server *tcp_server, const Mono_Time *mono_time, uint32_t i)
1165{ 1166{
1166 TCP_Secure_Connection *conn = &tcp_server->unconfirmed_connection_queue[i]; 1167 TCP_Secure_Connection *conn = &tcp_server->unconfirmed_connection_queue[i];
1167 1168
@@ -1182,7 +1183,7 @@ static int do_unconfirmed(TCP_Server *tcp_server, uint32_t i)
1182 return -1; 1183 return -1;
1183 } 1184 }
1184 1185
1185 return confirm_TCP_connection(tcp_server, conn, packet, len); 1186 return confirm_TCP_connection(tcp_server, mono_time, conn, packet, len);
1186} 1187}
1187 1188
1188static bool tcp_process_secure_packet(TCP_Server *tcp_server, uint32_t i) 1189static bool tcp_process_secure_packet(TCP_Server *tcp_server, uint32_t i)
@@ -1221,32 +1222,28 @@ static void do_confirmed_recv(TCP_Server *tcp_server, uint32_t i)
1221#ifndef TCP_SERVER_USE_EPOLL 1222#ifndef TCP_SERVER_USE_EPOLL
1222static void do_TCP_incoming(TCP_Server *tcp_server) 1223static void do_TCP_incoming(TCP_Server *tcp_server)
1223{ 1224{
1224 uint32_t i; 1225 for (uint32_t i = 0; i < MAX_INCOMING_CONNECTIONS; ++i) {
1225
1226 for (i = 0; i < MAX_INCOMING_CONNECTIONS; ++i) {
1227 do_incoming(tcp_server, i); 1226 do_incoming(tcp_server, i);
1228 } 1227 }
1229} 1228}
1230 1229
1231static void do_TCP_unconfirmed(TCP_Server *tcp_server) 1230static void do_TCP_unconfirmed(TCP_Server *tcp_server, const Mono_Time *mono_time)
1232{ 1231{
1233 uint32_t i; 1232 for (uint32_t i = 0; i < MAX_INCOMING_CONNECTIONS; ++i) {
1234 1233 do_unconfirmed(tcp_server, mono_time, i);
1235 for (i = 0; i < MAX_INCOMING_CONNECTIONS; ++i) {
1236 do_unconfirmed(tcp_server, i);
1237 } 1234 }
1238} 1235}
1239#endif 1236#endif
1240 1237
1241static void do_TCP_confirmed(TCP_Server *tcp_server) 1238static void do_TCP_confirmed(TCP_Server *tcp_server, const Mono_Time *mono_time)
1242{ 1239{
1243#ifdef TCP_SERVER_USE_EPOLL 1240#ifdef TCP_SERVER_USE_EPOLL
1244 1241
1245 if (tcp_server->last_run_pinged == unix_time()) { 1242 if (tcp_server->last_run_pinged == mono_time_get(mono_time)) {
1246 return; 1243 return;
1247 } 1244 }
1248 1245
1249 tcp_server->last_run_pinged = unix_time(); 1246 tcp_server->last_run_pinged = mono_time_get(mono_time);
1250#endif 1247#endif
1251 uint32_t i; 1248 uint32_t i;
1252 1249
@@ -1257,7 +1254,7 @@ static void do_TCP_confirmed(TCP_Server *tcp_server)
1257 continue; 1254 continue;
1258 } 1255 }
1259 1256
1260 if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) { 1257 if (mono_time_is_timeout(mono_time, conn->last_pinged, TCP_PING_FREQUENCY)) {
1261 uint8_t ping[1 + sizeof(uint64_t)]; 1258 uint8_t ping[1 + sizeof(uint64_t)];
1262 ping[0] = TCP_PACKET_PING; 1259 ping[0] = TCP_PACKET_PING;
1263 uint64_t ping_id = random_u64(); 1260 uint64_t ping_id = random_u64();
@@ -1270,17 +1267,17 @@ static void do_TCP_confirmed(TCP_Server *tcp_server)
1270 int ret = write_packet_TCP_secure_connection(conn, ping, sizeof(ping), 1); 1267 int ret = write_packet_TCP_secure_connection(conn, ping, sizeof(ping), 1);
1271 1268
1272 if (ret == 1) { 1269 if (ret == 1) {
1273 conn->last_pinged = unix_time(); 1270 conn->last_pinged = mono_time_get(mono_time);
1274 conn->ping_id = ping_id; 1271 conn->ping_id = ping_id;
1275 } else { 1272 } else {
1276 if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY + TCP_PING_TIMEOUT)) { 1273 if (mono_time_is_timeout(mono_time, conn->last_pinged, TCP_PING_FREQUENCY + TCP_PING_TIMEOUT)) {
1277 kill_accepted(tcp_server, i); 1274 kill_accepted(tcp_server, i);
1278 continue; 1275 continue;
1279 } 1276 }
1280 } 1277 }
1281 } 1278 }
1282 1279
1283 if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) { 1280 if (conn->ping_id && mono_time_is_timeout(mono_time, conn->last_pinged, TCP_PING_TIMEOUT)) {
1284 kill_accepted(tcp_server, i); 1281 kill_accepted(tcp_server, i);
1285 continue; 1282 continue;
1286 } 1283 }
@@ -1296,7 +1293,7 @@ static void do_TCP_confirmed(TCP_Server *tcp_server)
1296} 1293}
1297 1294
1298#ifdef TCP_SERVER_USE_EPOLL 1295#ifdef TCP_SERVER_USE_EPOLL
1299static bool tcp_epoll_process(TCP_Server *tcp_server) 1296static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time)
1300{ 1297{
1301#define MAX_EVENTS 16 1298#define MAX_EVENTS 16
1302 struct epoll_event events[MAX_EVENTS]; 1299 struct epoll_event events[MAX_EVENTS];
@@ -1387,7 +1384,7 @@ static bool tcp_epoll_process(TCP_Server *tcp_server)
1387 } 1384 }
1388 1385
1389 case TCP_SOCKET_UNCONFIRMED: { 1386 case TCP_SOCKET_UNCONFIRMED: {
1390 const int index_new = do_unconfirmed(tcp_server, index); 1387 const int index_new = do_unconfirmed(tcp_server, mono_time, index);
1391 1388
1392 if (index_new != -1) { 1389 if (index_new != -1) {
1393 events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP; 1390 events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP;
@@ -1413,27 +1410,27 @@ static bool tcp_epoll_process(TCP_Server *tcp_server)
1413 return nfds > 0; 1410 return nfds > 0;
1414} 1411}
1415 1412
1416static void do_TCP_epoll(TCP_Server *tcp_server) 1413static void do_TCP_epoll(TCP_Server *tcp_server, const Mono_Time *mono_time)
1417{ 1414{
1418 while (tcp_epoll_process(tcp_server)) { 1415 while (tcp_epoll_process(tcp_server, mono_time)) {
1419 // Keep processing packets until there are no more FDs ready for reading. 1416 // Keep processing packets until there are no more FDs ready for reading.
1420 continue; 1417 continue;
1421 } 1418 }
1422} 1419}
1423#endif 1420#endif
1424 1421
1425void do_TCP_server(TCP_Server *tcp_server) 1422void do_TCP_server(TCP_Server *tcp_server, Mono_Time *mono_time)
1426{ 1423{
1427#ifdef TCP_SERVER_USE_EPOLL 1424#ifdef TCP_SERVER_USE_EPOLL
1428 do_TCP_epoll(tcp_server); 1425 do_TCP_epoll(tcp_server, mono_time);
1429 1426
1430#else 1427#else
1431 do_TCP_accept_new(tcp_server); 1428 do_TCP_accept_new(tcp_server);
1432 do_TCP_incoming(tcp_server); 1429 do_TCP_incoming(tcp_server);
1433 do_TCP_unconfirmed(tcp_server); 1430 do_TCP_unconfirmed(tcp_server, mono_time);
1434#endif 1431#endif
1435 1432
1436 do_TCP_confirmed(tcp_server); 1433 do_TCP_confirmed(tcp_server, mono_time);
1437} 1434}
1438 1435
1439void kill_TCP_server(TCP_Server *tcp_server) 1436void kill_TCP_server(TCP_Server *tcp_server)