Add a verbose switch

This commit is contained in:
Quentin Dufour 2019-04-01 14:16:41 +02:00
parent 689b99cc25
commit 5a0e746e30
9 changed files with 32 additions and 17 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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;
};

View file

@ -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));

View file

@ -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) {

View file

@ -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);

View file

@ -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;

View file

@ -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");

View file

@ -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