From 9c74eb09a7cbd3e9feedab5e4aefd324fdce7550 Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Thu, 7 Aug 2008 13:22:30 +0000 Subject: [PATCH] Improved latency for traffic initiated from server --- CHANGELOG | 6 ++++++ src/iodined.c | 53 +++++++++++++++++++++++++++------------------------ src/user.c | 17 ----------------- src/user.h | 1 - tests/user.c | 24 ----------------------- 5 files changed, 34 insertions(+), 67 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 15623dc..3162268 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,12 @@ iodine - http://code.kryo.se/iodine CHANGES: +2008-xx-xx: 0.x.x + - Delayed sending responses in server to improve latency. Pings from + server to client are now always fast instead of cycling from + fast to 1000 ms. + - The server now replies to all received queries. + 2008-08-06: 0.4.2 "Opened Zone" - Applied a few small patches from Maxim Bourmistrov and Gregor Herrmann - Applied a patch for not creating and configuring the tun interface, diff --git a/src/iodined.c b/src/iodined.c index d87ada0..326feec 100644 --- a/src/iodined.c +++ b/src/iodined.c @@ -107,6 +107,13 @@ tunnel_tun(int tun_fd, int dns_fd) if (users[userid].outpacket.len == 0) { memcpy(users[userid].outpacket.data, out, outlen); users[userid].outpacket.len = outlen; + if (users[userid].q.id != 0) { + /* If delayed response is kept, send reply immediately */ + write_dns(dns_fd, &(users[userid].q), users[userid].outpacket.data, users[userid].outpacket.len); + users[userid].outpacket.len = 0; + users[userid].q.id = 0; + return 0; + } return outlen; } else { return 0; @@ -252,6 +259,12 @@ handle_null_request(int tun_fd, int dns_fd, struct query *q) write_dns(dns_fd, q, "BADIP", 5); return; /* illegal id */ } + if (users[userid].q.id != 0) { + /* If delayed response is kept, send empty reply before overwriting */ + write_dns(dns_fd, &(users[userid].q), users[userid].outpacket.data, users[userid].outpacket.len); + users[userid].outpacket.len = 0; + users[userid].q.id = 0; + } memcpy(&(users[userid].q), q, sizeof(struct query)); users[userid].last_pkt = time(NULL); } else if(in[0] == 'Z' || in[0] == 'z') { @@ -285,6 +298,12 @@ handle_null_request(int tun_fd, int dns_fd, struct query *q) users[userid].encoder); users[userid].last_pkt = time(NULL); + if (users[userid].q.id != 0) { + /* If delayed response is kept, send empty reply before overwriting */ + write_dns(dns_fd, &(users[userid].q), users[userid].outpacket.data, users[userid].outpacket.len); + users[userid].outpacket.len = 0; + users[userid].q.id = 0; + } memcpy(&(users[userid].q), q, sizeof(struct query)); memcpy(users[userid].inpacket.data + users[userid].inpacket.offset, unpacked, read); users[userid].inpacket.len += read; @@ -353,16 +372,10 @@ tunnel(int tun_fd, int dns_fd) struct timeval tv; fd_set fds; int i; - int j; while (running) { - if (users_waiting_on_reply()) { - tv.tv_sec = 0; - tv.tv_usec = 5000; - } else { - tv.tv_sec = 1; - tv.tv_usec = 0; - } + tv.tv_sec = 1; + tv.tv_usec = 0; FD_ZERO(&fds); /* TODO : use some kind of packet queue */ @@ -379,24 +392,14 @@ tunnel(int tun_fd, int dns_fd) return 1; } - if (i==0) { - for (j = 0; j < USERS; j++) { - if (users[j].q.id != 0) { - write_dns(dns_fd, &(users[j].q), users[j].outpacket.data, users[j].outpacket.len); - users[j].outpacket.len = 0; - users[j].q.id = 0; - } - } - } else { - if(FD_ISSET(tun_fd, &fds)) { - tunnel_tun(tun_fd, dns_fd); - continue; - } - if(FD_ISSET(dns_fd, &fds)) { - tunnel_dns(tun_fd, dns_fd); - continue; - } + if(FD_ISSET(tun_fd, &fds)) { + tunnel_tun(tun_fd, dns_fd); + continue; } + if(FD_ISSET(dns_fd, &fds)) { + tunnel_dns(tun_fd, dns_fd); + continue; + } } return 0; diff --git a/src/user.c b/src/user.c index 6cbddfb..0df78be 100644 --- a/src/user.c +++ b/src/user.c @@ -53,23 +53,6 @@ init_users(in_addr_t my_ip) } } -int -users_waiting_on_reply() -{ - int ret; - int i; - - ret = 0; - for (i = 0; i < USERS; i++) { - if (users[i].active && users[i].last_pkt + 60 > time(NULL) && - users[i].q.id != 0) { - ret++; - } - } - - return ret; -} - int find_user_by_ip(uint32_t ip) { diff --git a/src/user.h b/src/user.h index b333bcb..7578be5 100644 --- a/src/user.h +++ b/src/user.h @@ -35,7 +35,6 @@ struct user { extern struct user users[USERS]; void init_users(in_addr_t); -int users_waiting_on_reply(); int find_user_by_ip(uint32_t); int all_users_waiting_to_send(); int find_available_user(); diff --git a/tests/user.c b/tests/user.c index 7c3f87a..f5ef2a8 100644 --- a/tests/user.c +++ b/tests/user.c @@ -46,29 +46,6 @@ START_TEST(test_init_users) } END_TEST -START_TEST(test_users_waiting) -{ - in_addr_t ip; - - ip = inet_addr("127.0.0.1"); - init_users(ip); - - fail_unless(users_waiting_on_reply() == 0); - - users[3].active = 1; - - fail_unless(users_waiting_on_reply() == 0); - - users[3].last_pkt = time(NULL); - - fail_unless(users_waiting_on_reply() == 0); - - users[3].q.id = 1; - - fail_unless(users_waiting_on_reply() == 1); -} -END_TEST - START_TEST(test_find_user_by_ip) { in_addr_t ip; @@ -153,7 +130,6 @@ test_user_create_tests() tc = tcase_create("User"); tcase_add_test(tc, test_init_users); - tcase_add_test(tc, test_users_waiting); tcase_add_test(tc, test_find_user_by_ip); tcase_add_test(tc, test_all_users_waiting_to_send); tcase_add_test(tc, test_find_available_user);