admin mode

This commit is contained in:
Olaf Rempel 2006-12-03 22:13:20 +01:00
parent 5bdb9d85fb
commit 48f18df080
2 changed files with 37 additions and 12 deletions

View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <netinet/ip.h>
@ -20,7 +21,7 @@ struct connection {
int src_fd;
int dst_fd;
unsigned long login_time;
time_t login_time;
};
static LIST_HEAD(connection_list);
@ -34,6 +35,8 @@ static struct connection * create_connection()
}
con->src_fd = con->dst_fd = -1;
con->src_addr.sin_addr.s_addr = con->src_addr.sin_addr.s_addr = 0;
con->src_addr.sin_port = con->src_addr.sin_port = 0;
con->login_time = time(NULL);
return con;
@ -114,9 +117,39 @@ static int client_handler(int fd, void *privdata)
static int admin_handler(int fd, void *privdata)
{
struct connection *con = (struct connection *)privdata;
char buf[256];
int len = read(fd, buf, sizeof(buf));
if (len <= 0) {
list_del(&con->list);
destroy_connection(con);
return -1;
}
if (strncmp(buf, "list", 4) == 0) {
struct connection *entry;
list_for_each_entry(entry, &connection_list, list) {
len = snprintf(buf, sizeof(buf), "%d,", entry->src_fd);
len += get_sockaddr(&entry->src_addr, buf + len, sizeof(buf) - len);
len += snprintf(buf + len, sizeof(buf) - len, ",%d,", entry->dst_fd);
len += get_sockaddr(&entry->dst_addr, buf + len, sizeof(buf) - len);
len += snprintf(buf + len, sizeof(buf) - len, ",%ld\n", (time(NULL) - entry->login_time));
write(fd, buf, len);
}
} else if (strncmp(buf, "kill", 4) == 0) {
} else if (strncmp(buf, "quit", 4) == 0) {
list_del(&con->list);
destroy_connection(con);
return -1;
}
return 0;
}
int listen_handler(int fd, void *privdata)
{
int mode = (int)privdata;
@ -139,11 +172,9 @@ int listen_handler(int fd, void *privdata)
list_add_tail(&con->list, &connection_list);
if (mode == CON_ADMIN)
event_add_readfd(con->src_fd, client_handler, con);
else
event_add_readfd(con->src_fd, admin_handler, con);
else
event_add_readfd(con->src_fd, client_handler, con);
return 0;
}

View File

@ -164,7 +164,6 @@ int event_loop(void)
entry->flags &= ~FD_NEW;
if (entry->flags & FD_DELETE) {
log_print(LOG_DEBUG, "event_loop: delete FD: %d (%p/%p)", entry->fd, entry->callback, entry->privdata);
list_del(&entry->list);
free(entry);
@ -173,7 +172,6 @@ int event_loop(void)
readfds = &fdsets[0];
FD_ZERO(readfds);
}
log_print(LOG_DEBUG, "event_loop: read FD: %d (%p/%p)", entry->fd, entry->callback, entry->privdata);
FD_SET(entry->fd, readfds);
} else if ((entry->flags & FD_WRITE) != 0) {
@ -181,7 +179,6 @@ int event_loop(void)
writefds = &fdsets[1];
FD_ZERO(writefds);
}
log_print(LOG_DEBUG, "event_loop: write FD: %d (%p/%p)", entry->fd, entry->callback, entry->privdata);
FD_SET(entry->fd, writefds);
} else if ((entry->flags & FD_EXCEPT) != 0) {
@ -189,7 +186,6 @@ int event_loop(void)
exceptfds = &fdsets[2];
FD_ZERO(exceptfds);
}
log_print(LOG_DEBUG, "event_loop: except FD: %d (%p/%p)", entry->fd, entry->callback, entry->privdata);
FD_SET(entry->fd, exceptfds);
}
}
@ -218,8 +214,6 @@ int event_loop(void)
}
}
log_print(LOG_DEBUG, "");
int i = select(FD_SETSIZE, readfds, writefds, exceptfds, timeout_p);
if (i < 0) {
/* On error, -1 is returned, and errno is set