#11 only read from tun if any active user is not sending

This commit is contained in:
Erik Ekman 2007-02-11 11:51:30 +00:00
parent 23ad29522b
commit 08ecccc7fe
3 changed files with 30 additions and 5 deletions

View file

@ -80,10 +80,15 @@ tunnel_tun(int tun_fd, int dns_fd)
outlen = sizeof(out); outlen = sizeof(out);
compress2((uint8_t*)out, &outlen, (uint8_t*)in, read, 9); 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 { typedef enum {
@ -281,7 +286,8 @@ tunnel(int tun_fd, int dns_fd)
} }
FD_ZERO(&fds); 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(tun_fd, &fds);
FD_SET(dns_fd, &fds); FD_SET(dns_fd, &fds);

View file

@ -79,12 +79,30 @@ find_user_by_ip(uint32_t ip)
return ret; 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 int
find_available_user() find_available_user()
{ {
int ret = -1; int ret = -1;
int i; int i;
for (i = 0; i < USERS; i++) { for (i = 0; i < USERS; i++) {
/* Not used at all or not used in one minute */ /* Not used at all or not used in one minute */
if (!users[i].active || users[i].last_pkt + 60 < time(NULL)) { if (!users[i].active || users[i].last_pkt + 60 < time(NULL)) {

View file

@ -37,6 +37,7 @@ extern struct user users[USERS];
void init_users(in_addr_t); void init_users(in_addr_t);
int users_waiting_on_reply(); int users_waiting_on_reply();
int find_user_by_ip(uint32_t); int find_user_by_ip(uint32_t);
int all_users_waiting_to_send();
int find_available_user(); int find_available_user();
#endif #endif