From 7538d2d9e91452b75a4b50d6d0ff93d6088d3730 Mon Sep 17 00:00:00 2001 From: Quentin Date: Tue, 24 Sep 2019 19:48:42 +0200 Subject: [PATCH] Reworked measlat --- src/meas_lat.c | 60 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/src/meas_lat.c b/src/meas_lat.c index 4555893..bfa6fce 100644 --- a/src/meas_lat.c +++ b/src/meas_lat.c @@ -10,12 +10,14 @@ #include "utils.h" #include "measure.h" #include "url.h" +#include "tor_os.h" +#include "tor_ctl.h" struct measlat_ctx { struct measure_conf mc; struct sockaddr_in addr; socklen_t addrlen; - int verbose, is_timer_started, is_from_needed; + int verbose, is_timer_started, is_from_needed, tor_flags; char *host, *port, *transport; }; @@ -72,9 +74,9 @@ int on_receive_measure_packet(struct evt_core_ctx* ctx, struct evt_core_fdinfo* struct measlat_ctx* mctx = fdinfo->cat->app_ctx; if (mctx->is_from_needed) nread = recvfrom(fdinfo->fd, mctx->mc.payload, mctx->mc.payload_size, MSG_TRUNC, (struct sockaddr*)&mctx->addr, &mctx->addrlen); - else nread = recv(fdinfo->fd, mctx->mc.payload, mctx->mc.payload_size, MSG_TRUNC); + else nread = recv(fdinfo->fd, mctx->mc.payload, mctx->mc.payload_size, 0); - if (nread == -1 && errno == EAGAIN) return 1; + if ((nread == -1 && errno == EAGAIN) || nread == 0) return 1; // @FIXME logic is wrong for TCP here but would lead (hopefully) to a crash // First we parse the packet and exit if needed @@ -153,11 +155,15 @@ int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { } struct measure_packet* head = measure_generate(&mctx->mc); + //printf("send(id=%ld,is_echo=%d)\n", head->counter, head->is_echo); struct evt_core_fdinfo* tgtinfo = evt_core_get_first_from_cat (ctx, "udp-read"); if (tgtinfo == NULL) tgtinfo = evt_core_get_first_from_cat (ctx, "tcp-read"); if (tgtinfo == NULL) { printf("No connection yet\n"); + struct evt_core_fdinfo* timer_fd = evt_core_get_first_from_cat (ctx, "timer"); + evt_core_rm_fd(ctx, timer_fd->fd); + mctx->is_timer_started = 0; return 1; } if (mctx->is_from_needed) s = sendto(tgtinfo->fd, mctx->mc.payload, mctx->mc.payload_size, 0, (struct sockaddr*)&mctx->addr, mctx->addrlen); @@ -227,7 +233,7 @@ void register_categories(struct evt_core_ctx* evts, struct measlat_ctx* mctx) { evt_core_add_cat(evts, &template); template.cb = on_receive_measure_packet; // intended but not elegant - template.err_cb = on_receive_measure_packet_err; // intended but not elegant + template.err_cb = NULL; // intended but not elegant template.name = "tcp-read"; template.flags = EPOLLIN | EPOLLET; evt_core_add_cat(evts, &template); @@ -309,8 +315,7 @@ void spawn_udp_server(struct evt_core_ctx* evts) { printf("--- UDP server is listening\n"); } -void spawn_tor_server(struct evt_core_ctx* evts) { - uint16_t ports[] = {7500}; +void spawn_tor_server(struct evt_core_ctx* evts, uint16_t *ports) { char buffer[1024]; int tcp_serv_sock, err; @@ -330,6 +335,23 @@ void spawn_tor_server(struct evt_core_ctx* evts) { printf("--- TCP server is listening\n"); } +void measlat_create_onion_services(struct tor_os_str* tos, struct tor_ctl* tctl, uint16_t* ports, int ports_count, enum TOR_ONION_FLAGS tof) { + tor_os_create (tos, "onion_services.pub", "onion_services.txt", ports_count); + tor_os_read (tos); + + int err = 0; + err = tor_ctl_connect (tctl, "127.0.0.1", "9051"); + if (err < 0) { + fprintf(stderr, "Unable to open Tor Socket\n"); + exit(EXIT_FAILURE); + } + err = tor_ctl_add_onion (tctl, tos, ports, tof); + if (err != 0) { + fprintf(stderr, "Unable to create Onion Services (error: %d)\n", err); + exit(EXIT_FAILURE); + } +} + int main(int argc, char** argv) { setvbuf(stdout, NULL, _IONBF, 0); printf("~ measlat ~\n"); @@ -339,7 +361,7 @@ int main(int argc, char** argv) { struct evt_core_ctx evts = {0}; // 1. Parse parameters - while ((opt = getopt(argc, argv, "vh:p:c:s:i:t:lr")) != -1) { + while ((opt = getopt(argc, argv, "vh:p:c:s:i:t:lrn")) != -1) { switch(opt) { case 'v': mctx.verbose++; @@ -365,6 +387,9 @@ int main(int argc, char** argv) { case 'r': mctx.mc.is_rtt = 1; break; + case 'n': + mctx.tor_flags |= TOR_ONION_FLAG_NON_ANONYMOUS; + break; case 'i': // interval - every ms mctx.mc.interval = atoi(optarg); break; @@ -379,14 +404,27 @@ int main(int argc, char** argv) { if (mctx.transport == NULL) mctx.transport = "udp"; if (mctx.host == NULL) mctx.host = "127.0.0.1"; if (mctx.port == NULL) mctx.port = strcmp(mctx.transport, "udp") == 0 ? "9000" : "7500"; - printf("[measlat_conf] host=%s, port=%s, listen=%d, transport=%s, count=%ld, size=%ld, interval=%ld\n", - mctx.host, mctx.port, mctx.mc.is_server, mctx.transport, mctx.mc.max_measure, mctx.mc.payload_size, mctx.mc.interval); + printf("[measlat_conf] host=%s, port=%s, listen=%d, transport=%s, count=%ld, size=%ld, interval=%ld, is_rtt=%d\n", + mctx.host, mctx.port, mctx.mc.is_server, mctx.transport, mctx.mc.max_measure, mctx.mc.payload_size, mctx.mc.interval, mctx.mc.is_rtt); // 3. Bind events register_categories(&evts, &mctx); - if (mctx.mc.is_server && strcmp(mctx.transport, "udp") == 0) { spawn_udp_server (&evts); mctx.is_from_needed = 1; } - else if (mctx.mc.is_server && strcmp(mctx.transport, "tor") == 0) spawn_tor_server(&evts); + struct tor_os_str tos = {0}; + struct tor_ctl tctl = {0}; + uint16_t ports[] = {7500}; + int ports_count = sizeof(ports) / sizeof(ports[0]); + + if (mctx.mc.is_server && strcmp(mctx.transport, "udp") == 0) { + spawn_udp_server (&evts); + mctx.is_from_needed = 1; + + } + else if (mctx.mc.is_server && strcmp(mctx.transport, "tor") == 0) { + spawn_tor_server(&evts, ports); + measlat_create_onion_services (&tos, &tctl, ports, ports_count, mctx.tor_flags); + printf("--- Onion services created\n"); + } else if (strcmp(mctx.transport, "udp") == 0) spawn_udp_client(&evts); else if (strcmp(mctx.transport, "tor") == 0) spawn_tor_client(&evts); else exit(EXIT_FAILURE);