summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2020-12-04 06:20:54 +0100
committerDirk Engling <erdgeist@erdgeist.org>2020-12-04 06:20:54 +0100
commit0730917115f2f2df04ef7d9c075fcef907d29708 (patch)
tree23f7aeff154c54be824ae1578cc8bf8de3ec745f
parentc674cde5690bf2cf2a7749c42255b197d835eea8 (diff)
Add walker that closes unused files after 20 minutes
-rw-r--r--receiver.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/receiver.c b/receiver.c
index 36b649c..0fe24a2 100644
--- a/receiver.c
+++ b/receiver.c
@@ -47,6 +47,9 @@ static const unsigned char privkey[] =
47 47
48static const unsigned char pp[] = "IJUHZGFDXTZKHJKHGFDHZLUÖDRTFGHHJGHH"; 48static const unsigned char pp[] = "IJUHZGFDXTZKHJKHGFDHZLUÖDRTFGHHJGHH";
49 49
50// Close files after 20 minutes
51#define ACCESS_TIMEOUT (20*60)
52
50/* 53/*
51 1 byte type: 0 allocate session, 1 log to session, rest: discard 54 1 byte type: 0 allocate session, 1 log to session, rest: discard
52 8 bytes session id 55 8 bytes session id
@@ -74,7 +77,7 @@ static size_t g_session_count = 0;
74 77
75int session_compare(const void *a, const void *b) { return memcmp(a, b, 8); } 78int session_compare(const void *a, const void *b) { return memcmp(a, b, 8); }
76 79
77enum { SIDOFFS = 20, KEYOFFS = 37, }; 80enum { SIDOFFS = 20, KEYOFFS = 37 };
78 81
79static uint8_t hex2nyble(char c) 82static uint8_t hex2nyble(char c)
80{ 83{
@@ -238,6 +241,17 @@ static void log_to_session(const uint8_t *packet, size_t len) {
238 mbedtls_gcm_free(&ctx); 241 mbedtls_gcm_free(&ctx);
239} 242}
240 243
244void close_files() {
245 time_t jetzt = now();
246 for (int i=0; i<g_session_count; ++i)
247 if (g_sessions[i].fd >= 0 &&
248 g_sessions[i].last_access != 0 &&
249 jetzt - g_sessions[i].last_access > ACCESS_TIMEOUT) {
250 close(g_sessions[i].fd);
251 g_sessions[i].fd = -1;
252 }
253}
254
241int main() { 255int main() {
242 mbedtls_ctr_drbg_context ctr_drbg; 256 mbedtls_ctr_drbg_context ctr_drbg;
243 mbedtls_entropy_context entropy; 257 mbedtls_entropy_context entropy;
@@ -270,6 +284,7 @@ int main() {
270 284
271 import_sessions("."); 285 import_sessions(".");
272 286
287 time_t last_close_check = now();
273 while(1) { 288 while(1) {
274 uint8_t packet[16*1024]; 289 uint8_t packet[16*1024];
275 uint8_t rsa_plain_text[AES_KEY_LENGTH]; 290 uint8_t rsa_plain_text[AES_KEY_LENGTH];
@@ -294,5 +309,10 @@ int main() {
294 default: 309 default:
295 break; 310 break;
296 } 311 }
312 time_t jetzt = now();
313 if (jetzt > last_close_check + 60) {
314 close_files();
315 last_close_check = jetzt;
316 }
297 } 317 }
298} 318}