From 08ecccc7fedd49072a885b7c809e6d6eb319ffa5 Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Sun, 11 Feb 2007 11:51:30 +0000 Subject: [PATCH] #11 only read from tun if any active user is not sending --- src/iodined.c | 14 ++++++++++---- src/user.c | 20 +++++++++++++++++++- src/user.h | 1 + 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/iodined.c b/src/iodined.c index 0e98072..fb32cba 100644 --- a/src/iodined.c +++ b/src/iodined.c @@ -80,10 +80,15 @@ tunnel_tun(int tun_fd, int dns_fd) outlen = sizeof(out); compress2((uint8_t*)out, &outlen, (uint8_t*)in, read, 9); - memcpy(users[userid].outpacket.data, out, outlen); - users[userid].outpacket.len = outlen; - return outlen; + /* if another packet is queued, throw away this one. TODO build queue */ + if (users[userid].outpacket.len == 0) { + memcpy(users[userid].outpacket.data, out, outlen); + users[userid].outpacket.len = outlen; + return outlen; + } else { + return 0; + } } typedef enum { @@ -281,7 +286,8 @@ tunnel(int tun_fd, int dns_fd) } FD_ZERO(&fds); - //if(outpacket.len == 0) TODO fix this + /* TODO : use some kind of packet queue */ + if(!all_users_waiting_to_send()) FD_SET(tun_fd, &fds); FD_SET(dns_fd, &fds); diff --git a/src/user.c b/src/user.c index a85e280..6571f30 100644 --- a/src/user.c +++ b/src/user.c @@ -79,12 +79,30 @@ find_user_by_ip(uint32_t ip) return ret; } +int +all_users_waiting_to_send() +{ + time_t now; + int ret; + int i; + + ret = 1; + now = time(NULL); + for (i = 0; i < USERS; i++) { + if (users[i].active && users[i].last_pkt + 60 > now && + users[i].outpacket.len == 0) { + ret = 0; + break; + } + } + return ret; +} + int find_available_user() { int ret = -1; int i; - for (i = 0; i < USERS; i++) { /* Not used at all or not used in one minute */ if (!users[i].active || users[i].last_pkt + 60 < time(NULL)) { diff --git a/src/user.h b/src/user.h index f9bbfb1..8e2021d 100644 --- a/src/user.h +++ b/src/user.h @@ -37,6 +37,7 @@ 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(); #endif