#include #include #include #include #include #include #include "evt_core.h" #include "net_tools.h" #include "socks5.h" #include "utils.h" #include "measure.h" struct measlat_ctx { struct measure_conf mc; int verbose; char *host, *port, *transport; }; int on_udp_err(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { return 1; } int on_udp(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { ssize_t res; struct measlat_ctx* mctx = fdinfo->cat->app_ctx; res = read(fdinfo->fd, mctx->mc.payload, mctx->mc.payload_size); if (res == -1 && errno == EAGAIN) return 1; measure_parse (res, &mctx->mc); return 0; } int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { ssize_t s; uint64_t ticks = 0; struct measlat_ctx* mctx = fdinfo->cat->app_ctx; s = read(fdinfo->fd, &ticks, sizeof(uint64_t)); if (s == -1 && errno == EAGAIN) return 1; if (s != sizeof(uint64_t)) { perror("Read error"); exit(EXIT_FAILURE); } if (ticks != 1) { fprintf(stderr, "Has ticked %lu times, expected 1 time. This is a bug\n", ticks); } mctx->mc.counter++; struct measure_packet* head = (struct measure_packet*)mctx->mc.payload; head->counter = mctx->mc.counter; if (clock_gettime(CLOCK_MONOTONIC, &head->emit_time) == -1) { perror("clock_gettime error"); exit(EXIT_FAILURE); } 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"); return 1; } s = send(tgtinfo->fd, mctx->mc.payload, mctx->mc.payload_size, 0); if (s < 0) { perror("Send error"); //exit(EXIT_FAILURE); } return 0; } void register_timer(struct evt_core_ctx* evts) { struct timespec now; struct itimerspec timer_config; char url[1024]; struct evt_core_cat cat = {0}; struct evt_core_fdinfo fdinfo = {0}; fdinfo.cat = &cat; fdinfo.url = url; struct evt_core_cat* ucat = evt_core_get_from_cat (evts, "tcp-read"); if (ucat == NULL) { fprintf(stderr, "Category udp-read not found\n"); exit(EXIT_FAILURE); } struct measlat_ctx* mctx = ucat->app_ctx; if (clock_gettime(CLOCK_REALTIME, &now) == -1) { perror("clock_gettime"); exit(EXIT_FAILURE); } uint64_t micro_sec = mctx->mc.interval; timer_config.it_value.tv_sec = now.tv_sec + 1; timer_config.it_value.tv_nsec = now.tv_nsec; timer_config.it_interval.tv_sec = micro_sec / 1000; timer_config.it_interval.tv_nsec = micro_sec % 1000 * 1000000; fdinfo.fd = timerfd_create(CLOCK_REALTIME, 0); if (fdinfo.fd == -1) { perror("Unable to timerfd_create"); exit(EXIT_FAILURE); } if (timerfd_settime (fdinfo.fd, TFD_TIMER_ABSTIME, &timer_config, NULL) == -1) { perror("Unable to timerfd_time"); exit(EXIT_FAILURE); } fdinfo.cat->name = "timer"; sprintf(fdinfo.url, "timer:%ld:%ld", mctx->mc.interval, mctx->mc.max_measure); evt_core_add_fd (evts, &fdinfo); printf("--- Timer registered\n"); } int on_socks5_success_measlat(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { char url[1024]; struct evt_core_cat cat = {0}; struct evt_core_fdinfo fdinfo_n = {0}; struct socks5_ctx* s5ctx = fdinfo->other; fdinfo_n.cat = &cat; fdinfo_n.url = url; struct evt_core_cat* ucat = evt_core_get_from_cat (ctx, "tcp-read"); if (ucat == NULL) { fprintf(stderr, "Category udp-read not found\n"); exit(EXIT_FAILURE); } struct measlat_ctx* mctx = ucat->app_ctx; fdinfo_n.fd = dup(fdinfo->fd); fdinfo_n.cat->name = "tcp-read"; sprintf(fdinfo_n.url, "tcp:read:%s:%d", s5ctx->addr, s5ctx->port); evt_core_add_fd (ctx, &fdinfo_n); printf("--- Tor socket registered\n"); register_timer(ctx); return 1; } void spawn_tor_socket(struct evt_core_ctx* evts) { struct evt_core_cat* ucat = evt_core_get_from_cat (evts, "tcp-read"); if (ucat == NULL) { fprintf(stderr, "Category udp-read not found\n"); exit(EXIT_FAILURE); } struct measlat_ctx* mctx = ucat->app_ctx; socks5_create_dns_client (evts, "127.0.0.1", "9050", mctx->host, atoi(mctx->port)); } int on_socks5_failed_measlat(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { evt_core_rm_fd (ctx, fdinfo->fd); spawn_tor_socket(ctx); return 1; } 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, mctx->verbose); template.cb = on_timer; template.name = "timer"; template.flags = EPOLLIN | EPOLLET; evt_core_add_cat(evts, &template); template.cb = on_udp; // intended but not elegant template.err_cb = on_udp_err; // intended but not elegant template.name = "tcp-read"; template.flags = EPOLLIN | EPOLLET; evt_core_add_cat(evts, &template); template.cb = on_udp; template.err_cb = on_udp_err; template.name = "udp-read"; template.flags = EPOLLIN | EPOLLET; evt_core_add_cat(evts, &template); template.cb = on_socks5_success_measlat; template.err_cb = on_socks5_failed_measlat; template.name = "socks5-success"; template.flags = EPOLLET; evt_core_add_cat(evts, &template); template.cb = on_socks5_failed_measlat; template.err_cb = on_socks5_failed_measlat; template.name = "socks5-failed"; template.flags = EPOLLET; evt_core_add_cat(evts, &template); socks5_init(evts); printf("--- Categories registered\n"); } void spawn_udp_socket(struct evt_core_ctx* evts) { struct evt_core_cat* ucat = evt_core_get_from_cat (evts, "udp-read"); if (ucat == NULL) { fprintf(stderr, "Category udp-read not found\n"); exit(EXIT_FAILURE); } struct measlat_ctx* mctx = ucat->app_ctx; int udp_sock = create_udp_client (mctx->host, mctx->port); char url[1024]; struct evt_core_cat cat = {0}; struct evt_core_fdinfo fdinfo = {0}; fdinfo.cat = &cat; fdinfo.url = url; fdinfo.fd = udp_sock; fdinfo.cat->name = "udp-read"; fdinfo.other = &mctx->mc; fdinfo.free_other = NULL; sprintf(fdinfo.url, "udp:read:%s:%s", mctx->host, mctx->port); evt_core_add_fd (evts, &fdinfo); printf("--- UDP socket registered\n"); register_timer(evts); } int main(int argc, char** argv) { setvbuf(stdout, NULL, _IONBF, 0); printf("~ measlat ~\n"); int opt; struct measlat_ctx mctx = {0}; struct evt_core_ctx evts = {0}; // 1. Parse parameters 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; case 'p': // port mctx.port = optarg; break; case 't': // transport mctx.transport = optarg; break; case 'c': // count mctx.mc.max_measure = atoi(optarg); break; case 's': // size - payload in bytes mctx.mc.payload_size = atoi(optarg); break; case 'i': // interval - every ms mctx.mc.interval = atoi(optarg); break; default: goto usage; } } // 2. Check and fix parameters measure_prepare (&mctx.mc); if (mctx.transport == NULL) mctx.transport = "udp"; if (mctx.host == NULL || mctx.port == NULL) goto usage; printf("[measlat_conf] host=%s, port=%s, transport=%s, count=%ld, size=%ld, interval=%ld\n", mctx.host, mctx.port, mctx.transport, mctx.mc.max_measure, mctx.mc.payload_size, mctx.mc.interval); // 3. Bind events register_categories(&evts, &mctx); if (strcmp(mctx.transport, "udp") == 0) spawn_udp_socket(&evts); else if (strcmp(mctx.transport, "tor") == 0) spawn_tor_socket(&evts); // 4. Run main loop evt_core_loop(&evts); return 0; usage: fprintf(stderr, "Usage: %s -h -p [-t ] [-c ] [-i ] [-s ]\n", argv[0]); exit(EXIT_FAILURE); }