From 5a0e746e30b2bf5b39579e472c35142e180ea271 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 1 Apr 2019 14:16:41 +0200 Subject: [PATCH] Add a verbose switch --- src/donar.c | 5 ++++- src/donar_client.c | 2 +- src/donar_init.h | 2 +- src/donar_server.c | 2 +- src/evt_core.c | 20 ++++++++++++++------ src/evt_core.h | 3 ++- src/meas_lat.c | 9 ++++++--- src/tor_echo.c | 2 +- src/udp_echo.c | 4 ++-- 9 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/donar.c b/src/donar.c index 85905f9..041a318 100644 --- a/src/donar.c +++ b/src/donar.c @@ -15,8 +15,11 @@ int main(int argc, char** argv) { struct donar_params dp; donar_init_params (&dp); - while ((dp.opt = getopt(argc, argv, "cse:r:o:a:bh")) != -1) { + while ((dp.opt = getopt(argc, argv, "vcse:r:o:a:bh")) != -1) { switch(dp.opt) { + case 'v': + dp.verbose++; + break; case 's': dp.is_server = 1; break; diff --git a/src/donar_client.c b/src/donar_client.c index ee4e994..99f9e85 100644 --- a/src/donar_client.c +++ b/src/donar_client.c @@ -76,7 +76,7 @@ void donar_client(struct donar_client_ctx* ctx, struct donar_params* dp) { .is_healing = dp->is_healing }; - evt_core_init (&(ctx->evts)); + evt_core_init (&(ctx->evts), dp->verbose); init_algo(&ctx->evts, &algo, dp->algo, &ap); socks5_init (&ctx->evts); init_socks5_sinks(ctx); diff --git a/src/donar_init.h b/src/donar_init.h index 72595c5..6d83551 100644 --- a/src/donar_init.h +++ b/src/donar_init.h @@ -7,7 +7,7 @@ #include "packet.h" struct donar_params { - int opt, is_server, is_client, is_waiting_bootstrap, is_healing, errored; + int opt, is_server, is_client, is_waiting_bootstrap, is_healing, errored, verbose; char *port, *onion_file, *algo; GPtrArray *remote_ports, *exposed_ports; }; diff --git a/src/donar_server.c b/src/donar_server.c index 310f2b5..48e309f 100644 --- a/src/donar_server.c +++ b/src/donar_server.c @@ -58,7 +58,7 @@ void donar_server(struct donar_server_ctx* ctx, struct donar_params* dp) { .is_healing = dp->is_healing }; - evt_core_init (&(ctx->evts)); + evt_core_init (&(ctx->evts), dp->verbose); init_algo(&ctx->evts, &algo, dp->algo, &ap); evt_core_add_cat (&(ctx->evts), &(algo.on_tcp_co)); evt_core_add_cat (&(ctx->evts), &(algo.on_udp_read)); diff --git a/src/evt_core.c b/src/evt_core.c index 1e7b728..498679e 100644 --- a/src/evt_core.c +++ b/src/evt_core.c @@ -20,13 +20,13 @@ void free_cat(void* vcat) { free(cat); } -void evt_core_init(struct evt_core_ctx* ctx) { +void evt_core_init(struct evt_core_ctx* ctx, uint8_t verbose) { ctx->epollfd = epoll_create1(0); if (ctx->epollfd == -1) { perror("Failed to create epoll file descriptor epoll_create1"); exit(EXIT_FAILURE); } - + ctx->verbose = verbose; ctx->catlist = g_hash_table_new_full(g_str_hash, g_str_equal,NULL, free_cat); ctx->socklist = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, free_fdinfo); ctx->urltofd = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, NULL); @@ -65,7 +65,7 @@ void evt_core_add_cat(struct evt_core_ctx* ctx, struct evt_core_cat* cat) { } void evt_core_mv_fd(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct evt_core_cat* to_cat) { - printf("Moving fd=%d from cat=%s to cat=%s\n",fdinfo->fd, fdinfo->cat->name, to_cat->name); + if (ctx->verbose) printf("Moving fd=%d from cat=%s to cat=%s\n",fdinfo->fd, fdinfo->cat->name, to_cat->name); // 1. Update old category for (int i = 0; i < fdinfo->cat->socklist->len; i++) { @@ -130,7 +130,7 @@ struct evt_core_fdinfo* evt_core_add_fd(struct evt_core_ctx* ctx, struct evt_cor // 5. Add file descriptor to epoll add_fd_to_epoll(ctx->epollfd, user_data->fd, cat->flags); - printf("Added fd=%d with url=%s in cat=%s\n", fdinfo->fd, fdinfo->url, fdinfo->cat->name); + if (ctx->verbose) printf("Added fd=%d with url=%s in cat=%s\n", fdinfo->fd, fdinfo->url, fdinfo->cat->name); // 6. Ensure that events arrived before epoll registering are handled fdinfo->cat->cb(ctx, fdinfo); @@ -145,7 +145,7 @@ struct evt_core_cat* evt_core_rm_fd(struct evt_core_ctx* ctx, int fd) { struct evt_core_fdinfo* fdinfo = g_hash_table_lookup (ctx->socklist, &fd); if (fdinfo == NULL) return NULL; cat = fdinfo->cat; - printf("Closing fd=%d from cat=%s\n",fdinfo->fd, fdinfo->cat->name); + if (ctx->verbose) printf("Closing fd=%d from cat=%s\n",fdinfo->fd, fdinfo->cat->name); // 2. Update category for (int i = 0; i < cat->socklist->len; i++) { @@ -176,6 +176,8 @@ void evt_core_loop(struct evt_core_ctx* ctx) { struct epoll_event current_event, events[EVT_CORE_MAX_EVENTS]; struct evt_core_fdinfo* fdinfo; struct evt_core_cat* cat; + clock_t start, end; + double elapsed_in_cb; printf("--- Start main loop\n"); int num_fd, n = 0; @@ -231,12 +233,18 @@ void evt_core_loop(struct evt_core_ctx* ctx) { fprintf(stderr, "Ignoring file descriptor %d as it is not registered. This is a bug.\n", events[n].data.fd); continue; } + if (ctx->verbose) start = clock(); while(fdinfo->cat->cb(ctx, fdinfo) == 0); - + if (ctx->verbose) { + end = clock(); + elapsed_in_cb = ((double) (end - start)) / CLOCKS_PER_SEC; + printf("%s cb took %f seconds to execute.\n", fdinfo->url, elapsed_in_cb); + } } } evt_core_free(ctx); + return; } struct evt_core_fdinfo* evt_core_get_from_fd(struct evt_core_ctx* ctx, int fd) { diff --git a/src/evt_core.h b/src/evt_core.h index 0b5b2ac..4f3c39b 100644 --- a/src/evt_core.h +++ b/src/evt_core.h @@ -30,6 +30,7 @@ struct evt_core_cat { struct evt_core_ctx { int epollfd; + uint8_t verbose; GHashTable* catlist; // name -> category GHashTable* socklist; // fd -> category GHashTable* urltofd; // url -> fd, like "tcp:127.0.0.1:7500" @@ -43,7 +44,7 @@ struct evt_core_fdinfo { evt_core_free_app_ctx free_other; }; -void evt_core_init(struct evt_core_ctx* ctx); +void evt_core_init(struct evt_core_ctx* ctx, uint8_t verbose); void evt_core_add_cat(struct evt_core_ctx* ctx, struct evt_core_cat* cat); void evt_core_mv_fd(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct evt_core_cat* to_cat); void evt_core_mv_fd2(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, char* to_cat); diff --git a/src/meas_lat.c b/src/meas_lat.c index 679cab4..972be87 100644 --- a/src/meas_lat.c +++ b/src/meas_lat.c @@ -9,7 +9,7 @@ #include "socks5.h" struct measlat_ctx { - int count, size, interval; + int count, size, interval, verbose; char *host, *port, *transport; }; @@ -213,7 +213,7 @@ int on_socks5_failed_measlat(struct evt_core_ctx* ctx, struct evt_core_fdinfo* f void register_categories(struct evt_core_ctx* evts, struct measlat_ctx* mctx) { struct evt_core_cat template = {0}; template.app_ctx = mctx; - evt_core_init(evts); + evt_core_init(evts, mctx->verbose); template.cb = on_timer; template.name = "timer"; @@ -282,8 +282,11 @@ int main(int argc, char** argv) { struct evt_core_ctx evts = {0}; // 1. Parse parameters - while ((opt = getopt(argc, argv, "h:p:c:s:i:t:")) != -1) { + while ((opt = getopt(argc, argv, "vh:p:c:s:i:t:")) != -1) { switch(opt) { + case 'v': + mctx.verbose++; + break; case 'h': // host mctx.host = optarg; break; diff --git a/src/tor_echo.c b/src/tor_echo.c index 2436b4c..a612a60 100644 --- a/src/tor_echo.c +++ b/src/tor_echo.c @@ -110,7 +110,7 @@ int main(int argc, char** argv) { .flags = EPOLLIN | EPOLLOUT | EPOLLET | EPOLLRDHUP, .socklist = NULL }; - evt_core_init(&evts); + evt_core_init(&evts, 0); evt_core_add_cat(&evts, &tcp_co); evt_core_add_cat(&evts, &tcp_all); printf("--- Categories created\n"); diff --git a/src/udp_echo.c b/src/udp_echo.c index 3fd6256..71d2caf 100644 --- a/src/udp_echo.c +++ b/src/udp_echo.c @@ -41,7 +41,7 @@ int main(int argc, char** argv) { while ((opt = getopt(argc, argv, "p:v")) != -1) { switch(opt) { case 'v': - verbose = 1; + verbose++; break; case 'p': port = optarg; @@ -61,7 +61,7 @@ int main(int argc, char** argv) { .flags = EPOLLIN | EPOLLET, .socklist = NULL }; - evt_core_init(&evts); + evt_core_init(&evts, verbose); evt_core_add_cat(&evts, &udp_read); // 3. Register UDP socket