From 4709dd9f6dcf79f78db7deecf5049f2e863117d9 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sat, 7 Jul 2007 13:53:36 +0200 Subject: [PATCH] quit clients via webinterface --- connection.c | 38 +++++++++++++++++++++++++++++++++----- connection.h | 3 ++- torrent-stats.c | 3 ++- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/connection.c b/connection.c index 82b49a3..0d77cbb 100644 --- a/connection.c +++ b/connection.c @@ -219,7 +219,7 @@ int ctcs_accept_handler(int fd, void *privdata) return 0; } -int ctcs_httpd_handler(struct httpd_con *con, void *privdata) +int ctcs_httpd_show(struct httpd_con *con, void *privdata) { struct linebuffer *lbuf = create_linebuffer(16384); if (lbuf == NULL) { @@ -235,9 +235,9 @@ int ctcs_httpd_handler(struct httpd_con *con, void *privdata) if (list_empty(&torrent->client_list)) continue; - linebuffer_printf(lbuf, "\n\n", torrent->name); - linebuffer_printf(lbuf, ""); - linebuffer_printf(lbuf, "\n"); + linebuffer_printf(lbuf, "
%s
Client IP:PortChunks (have/total/avail)Download total(current)Upload total(current)Completed since
\n\n", torrent->name); + linebuffer_printf(lbuf, ""); + linebuffer_printf(lbuf, "\n"); struct client_con *tmp; list_for_each_entry(tmp, &torrent->client_list, list) { @@ -245,9 +245,11 @@ int ctcs_httpd_handler(struct httpd_con *con, void *privdata) get_sockaddr_buf(&tmp->addr), (double)tmp->chunk_have / (double)tmp->chunk_total * 100.0, tmp->chunk_have, tmp->chunk_total, tmp->chunk_avail); - linebuffer_printf(lbuf, "\n", + linebuffer_printf(lbuf, "", tmp->total_dn, tmp->bw_dn, tmp->total_up, tmp->bw_up, (tmp->completed != 0) ? ctime(&tmp->completed) : "-"); + + linebuffer_printf(lbuf, "\n", get_sockaddr_buf(&tmp->addr)); } linebuffer_printf(lbuf, "
%s
Client IP:PortChunks (have/total/avail)Download total(current)Upload total(current)Completed sinceQuit
%llu (%d)%llu (%d)%s
%llu (%d)%llu (%d)%sQuit
\n

\n"); } @@ -257,3 +259,29 @@ int ctcs_httpd_handler(struct httpd_con *con, void *privdata) linebuffer_free(lbuf); return 0; } + +int ctcs_httpd_quit(struct httpd_con *con, void *privdata) +{ + if (con->req_arg_cnt == 2 && strncmp(con->req_args[1], "client=", 7) == 0) { + struct sockaddr_in addr; + if (parse_sockaddr(con->req_args[1] +7, &addr) == 0) { + struct torrent_file *torrent; + list_for_each_entry(torrent, &torrent_list, list) { + struct client_con *search; + list_for_each_entry(search, &torrent->client_list, list) { + if (search->addr.sin_addr.s_addr != addr.sin_addr.s_addr) + continue; + + if (search->addr.sin_port != addr.sin_port) + continue; + + write(event_get_fd(search->event), "CTQUIT\n", 7); + } + } + } + } + + char *text = "HTTP/1.0 302 OK\r\nContent-Type: text/html\r\nConnection: close\r\nLocation: /\r\n\r\n"; + write(con->fd, text, strlen(text)); + return 0; +} diff --git a/connection.h b/connection.h index b114c29..fa18241 100644 --- a/connection.h +++ b/connection.h @@ -5,6 +5,7 @@ int ctcs_trigger_status(void *privdata); int ctcs_accept_handler(int fd, void *privdata); -int ctcs_httpd_handler(struct httpd_con *con, void *privdata); +int ctcs_httpd_show(struct httpd_con *con, void *privdata); +int ctcs_httpd_quit(struct httpd_con *con, void *privdata); #endif /* _CONNECTION_H_ */ diff --git a/torrent-stats.c b/torrent-stats.c index 9262b5a..b74c1d8 100644 --- a/torrent-stats.c +++ b/torrent-stats.c @@ -39,7 +39,8 @@ int main(int argc, char *argv[]) config_get_strings("global", "listen", listen_cb, ctcs_accept_handler); config_get_strings("global", "listen-http", listen_cb, httpd_accept_handler); - httpd_add_cb("/", 1, ctcs_httpd_handler, NULL); + httpd_add_cb("/quit", 0, ctcs_httpd_quit, NULL); + httpd_add_cb("/", 1, ctcs_httpd_show, NULL); struct timeval tv = { .tv_sec = 10, .tv_usec = 0 }; event_add_timeout(&tv, ctcs_trigger_status, NULL);