tor_multipath_voip/src/meas_lat.c

572 lines
17 KiB
C
Raw Normal View History

2019-11-27 15:08:44 +00:00
#include <stdlib.h>
#include <stdio.h>
2019-02-20 15:50:44 +00:00
#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>
2019-02-20 16:46:58 +00:00
#include <errno.h>
2021-01-08 17:30:22 +00:00
#include <arpa/inet.h>
#include "evt_core.h"
2019-02-14 17:08:20 +00:00
#include "net_tools.h"
2019-03-11 14:17:15 +00:00
#include "socks5.h"
2019-04-01 17:27:40 +00:00
#include "utils.h"
2019-09-23 14:32:59 +00:00
#include "measure.h"
2019-09-24 13:37:12 +00:00
#include "url.h"
2019-09-24 17:48:42 +00:00
#include "tor_os.h"
#include "tor_ctl.h"
2019-03-25 16:20:47 +00:00
struct measlat_ctx {
2019-09-24 13:37:12 +00:00
struct sockaddr_in addr;
socklen_t addrlen;
2021-01-08 17:30:22 +00:00
int verbose, connectionless, tor_flags;
2021-01-14 13:36:01 +00:00
char *host, *port, *transport, *tor_port;
2021-01-13 10:25:50 +00:00
enum { MEASLAT_CLIENT = 0, MEASLAT_SERVER = 1 } role;
struct measure_params mp;
2019-03-25 16:20:47 +00:00
};
2021-01-13 10:25:50 +00:00
void free_ms(void* obj) {
2021-01-26 11:13:03 +00:00
measure_state_free (obj);
2021-01-08 17:30:22 +00:00
free(obj);
}
2021-01-14 16:43:38 +00:00
struct measure_state ms_transi = {0};
2021-01-13 16:21:25 +00:00
int streq(char* s1, char* s2) {
return strcmp(s1, s2) == 0;
}
2021-01-13 10:25:50 +00:00
struct evt_core_fdinfo* register_timer(struct evt_core_ctx* evts, struct measlat_ctx* mctx, struct measure_state* ms, struct timespec* next_tick) {
2019-09-24 13:37:12 +00:00
struct timespec now;
struct itimerspec timer_config;
char url[1024];
struct evt_core_cat cat = {0};
struct evt_core_fdinfo fdinfo = {0};
2021-01-13 10:25:50 +00:00
struct measure_state* msheap = malloc(sizeof(struct measure_state));
if (ms == NULL) {
2021-01-08 17:30:22 +00:00
perror("unable to malloc struct timer_measure");
2019-09-24 13:37:12 +00:00
exit(EXIT_FAILURE);
}
2021-01-13 10:25:50 +00:00
memcpy(msheap, ms, sizeof(struct measure_state));
2019-10-16 15:56:51 +00:00
2021-01-08 17:30:22 +00:00
fdinfo.cat = &cat;
fdinfo.url = url;
2021-01-13 10:25:50 +00:00
fdinfo.other = msheap;
fdinfo.free_other = free_ms;
2019-09-24 13:37:12 +00:00
2020-01-31 23:00:45 +00:00
if (clock_gettime(CLOCK_MONOTONIC, &now) == -1) {
2019-09-24 13:37:12 +00:00
perror("clock_gettime");
exit(EXIT_FAILURE);
}
2021-01-13 10:25:50 +00:00
uint64_t micro_sec = mctx->mp.interval;
2019-09-24 13:37:12 +00:00
timer_config.it_value.tv_sec = next_tick == NULL ? now.tv_sec + 1 : next_tick->tv_sec;
timer_config.it_value.tv_nsec = next_tick == NULL ? now.tv_nsec : next_tick->tv_nsec;
timer_config.it_interval.tv_sec = micro_sec / 1000;
timer_config.it_interval.tv_nsec = micro_sec % 1000 * 1000000;
printf("timer_config: sec=%ld nsec=%ld \n", (uint64_t) timer_config.it_value.tv_sec, (uint64_t) timer_config.it_value.tv_nsec);
2020-01-31 23:00:45 +00:00
fdinfo.fd = timerfd_create(CLOCK_MONOTONIC, 0);
2019-09-24 13:37:12 +00:00
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";
2021-01-13 10:25:50 +00:00
sprintf(fdinfo.url, "timer:%d", msheap->fd);
2021-01-08 17:30:22 +00:00
struct evt_core_fdinfo *new_fdinfo = evt_core_add_fd (evts, &fdinfo);
2019-09-24 13:37:12 +00:00
printf("--- Timer registered\n");
2021-01-14 13:36:01 +00:00
printf("[states] measurement %d+%d started\n", msheap->fd, fdinfo.fd);
2021-01-08 17:30:22 +00:00
return new_fdinfo;
2019-09-24 13:37:12 +00:00
}
int on_receive_measure_packet_err(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
2021-01-14 13:43:28 +00:00
struct measlat_ctx* mctx = fdinfo->cat->app_ctx;
printf("An error occured with socket %d\n", fdinfo->fd);
2021-01-08 17:30:22 +00:00
char url[255];
sprintf(url, "timer:%d", fdinfo->fd);
struct evt_core_fdinfo* assoc_timer = evt_core_get_from_url (ctx, url);
2021-01-14 13:43:28 +00:00
if (assoc_timer != NULL) {
2021-01-26 15:35:36 +00:00
if (mctx->role == MEASLAT_CLIENT) measure_summary (&mctx->mp, assoc_timer->other);
2021-01-14 13:43:28 +00:00
evt_core_rm_fd (ctx, assoc_timer->fd);
printf("Deleted associated timer %s\n", url);
} else {
printf("No associated timer %s\n", url);
}
2021-01-08 17:30:22 +00:00
2021-01-26 11:53:32 +00:00
if (mctx->role == MEASLAT_CLIENT) exit(EXIT_FAILURE);
if (mctx->connectionless) return 1; // keep the NET FD
else return 0; // delete the NET fd
2019-03-05 15:21:25 +00:00
}
2021-01-13 10:25:50 +00:00
void measlat_stop(
struct evt_core_ctx* ctx,
struct measlat_ctx* mctx,
struct measure_state* ms,
2021-01-13 16:21:25 +00:00
int net_fd, int timer_fd) {
2021-01-13 10:25:50 +00:00
if (ms->mp_in->counter < mctx->mp.max_measure) return;
if (ms->mp_out->counter < mctx->mp.max_measure) return;
2021-01-14 13:36:01 +00:00
printf("[states] measurement %d+%d terminated\n", net_fd, timer_fd);
2021-01-26 15:35:36 +00:00
if (mctx->role == MEASLAT_CLIENT) measure_summary (&(mctx->mp), ms);
2021-01-13 16:21:25 +00:00
evt_core_rm_fd(ctx, timer_fd);
2021-01-14 11:23:58 +00:00
if (!(mctx->connectionless && mctx->role == MEASLAT_SERVER))
evt_core_rm_fd(ctx, net_fd);
if (mctx->role == MEASLAT_CLIENT)
2021-01-13 10:25:50 +00:00
exit(EXIT_SUCCESS);
}
2019-09-24 13:37:12 +00:00
int on_receive_measure_packet(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
2019-09-23 14:32:59 +00:00
struct measlat_ctx* mctx = fdinfo->cat->app_ctx;
2021-01-13 10:25:50 +00:00
struct measure_state* ms;
2020-01-31 23:00:45 +00:00
ssize_t nread;
2021-01-08 17:30:22 +00:00
char url[255];
2021-01-13 10:25:50 +00:00
// 1. Get our measurement state
2021-01-08 17:30:22 +00:00
sprintf(url, "timer:%d", fdinfo->fd);
struct evt_core_fdinfo* assoc_timer = evt_core_get_from_url (ctx, url);
2021-01-13 10:25:50 +00:00
if (assoc_timer) {
// Measurement state exists
ms = assoc_timer->other;
} else {
2021-01-14 16:43:38 +00:00
// Does not exist yet, we use a tmp stub.
ms = &ms_transi;
ms->fd = fdinfo->fd;
ms->mp_nin = 0;
2021-01-08 17:30:22 +00:00
}
2019-02-20 15:50:44 +00:00
2021-01-13 10:25:50 +00:00
// 2. Read data in our measurement object
nread = mctx->connectionless && mctx->role == MEASLAT_SERVER ?
2021-01-13 10:25:50 +00:00
recvfrom(fdinfo->fd, ms->mp_in, mctx->mp.payload_size, MSG_TRUNC, (struct sockaddr*)&mctx->addr, &mctx->addrlen) :
recv(fdinfo->fd, ((char*) ms->mp_in) + ms->mp_nin, mctx->mp.payload_size - ms->mp_nin, 0);
2021-01-08 17:30:22 +00:00
if (nread <= 0) return 1;
ms->mp_nin += nread;
if (ms->mp_nin < mctx->mp.payload_size) return 0;
2019-09-24 13:37:12 +00:00
2021-01-13 10:25:50 +00:00
// 3. Process data in our measurement object
2021-01-26 11:13:03 +00:00
measure_parse (&mctx->mp, ms, mctx->verbose);
2019-09-24 13:37:12 +00:00
2021-01-13 10:25:50 +00:00
// 4. Persist our measurement object if needed
// It includes starting a timer.
2021-01-14 16:43:38 +00:00
if (ms == &ms_transi) {
if (ms->mp_in->counter != 1) {
if (!(mctx->connectionless && mctx->role == MEASLAT_SERVER))
evt_core_rm_fd (ctx, fdinfo->fd);
return 1;
}
2021-01-13 10:25:50 +00:00
struct timespec next_tick = {0};
2021-01-14 16:43:38 +00:00
struct measure_state ms_new = {0};
measure_state_init(&mctx->mp, &ms_new);
ms_new.fd = fdinfo->fd;
ms_new.mp_nin = ms->mp_nin;
memcpy(ms_new.mp_in, ms->mp_in, mctx->mp.payload_size);
measure_next_tick(&mctx->mp, &ms_new, &next_tick);
assoc_timer = register_timer (ctx, mctx, &ms_new, &next_tick);
2020-02-05 09:52:21 +00:00
}
2021-01-13 10:25:50 +00:00
// 5. Check if our measurements are done
2021-01-13 16:39:17 +00:00
if (ms->mp_in->counter >= mctx->mp.max_measure && ms->mp_out->counter >= mctx->mp.max_measure) {
2021-01-13 16:21:25 +00:00
measlat_stop(ctx, mctx, ms, fdinfo->fd, assoc_timer->fd);
2021-01-13 16:39:17 +00:00
return 1;
}
2021-01-13 10:25:50 +00:00
2019-02-20 16:46:58 +00:00
return 0;
2019-02-14 17:08:20 +00:00
}
2019-09-24 13:37:12 +00:00
int on_tcp_co(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
int conn_sock1, conn_sock2;
struct sockaddr_in addr;
socklen_t in_len;
char url[1024], port[6];
struct evt_core_cat local_cat = {0};
struct evt_core_fdinfo to_fdinfo = {0};
to_fdinfo.cat = &local_cat;
to_fdinfo.url = url;
in_len = sizeof(addr);
conn_sock1 = accept(fdinfo->fd, (struct sockaddr*)&addr, &in_len);
if (conn_sock1 == -1 && errno == EAGAIN) return 1;
if (conn_sock1 == -1) goto co_error;
url_get_port(port, fdinfo->url);
to_fdinfo.fd = conn_sock1;
to_fdinfo.cat->name = "tcp-read";
sprintf(to_fdinfo.url, "tcp:read:127.0.0.1:%s", port);
evt_core_add_fd (ctx, &to_fdinfo);
2021-01-14 10:47:50 +00:00
uint16_t myPort = ntohs(addr.sin_port);
printf("client port is %u\n", myPort);
2019-09-24 13:37:12 +00:00
return 0;
co_error:
perror("Failed to handle new connection");
exit(EXIT_FAILURE);
}
2019-02-20 16:46:58 +00:00
int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
2019-02-20 15:50:44 +00:00
ssize_t s;
2019-04-02 09:38:02 +00:00
uint64_t ticks = 0;
2019-09-23 14:32:59 +00:00
struct measlat_ctx* mctx = fdinfo->cat->app_ctx;
2021-01-13 10:25:50 +00:00
struct measure_state* ms = fdinfo->other;
2019-02-20 15:50:44 +00:00
s = read(fdinfo->fd, &ticks, sizeof(uint64_t));
2019-03-27 16:31:46 +00:00
if (s == -1 && errno == EAGAIN) return 1;
2019-02-20 15:50:44 +00:00
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);
}
2019-05-28 13:47:31 +00:00
2021-01-13 10:25:50 +00:00
struct measure_packet* head = measure_generate(&mctx->mp, ms);
2019-09-24 17:48:42 +00:00
//printf("send(id=%ld,is_echo=%d)\n", head->counter, head->is_echo);
2019-05-28 13:47:31 +00:00
2021-01-14 11:23:58 +00:00
s = mctx->connectionless && mctx->role == MEASLAT_SERVER ?
2021-01-13 16:21:25 +00:00
sendto(ms->fd, head, mctx->mp.payload_size, 0, (struct sockaddr*)&mctx->addr, mctx->addrlen) :
send(ms->fd, head, mctx->mp.payload_size, 0);
2019-09-24 13:37:12 +00:00
2021-01-25 15:53:08 +00:00
// Too bad, we will drop this packet
if (s == -1 && errno == EAGAIN) return 1;
2021-01-13 10:25:50 +00:00
if (s < 0 || s != mctx->mp.payload_size) {
2019-02-20 15:50:44 +00:00
perror("Send error");
2020-01-31 23:00:45 +00:00
exit(EXIT_FAILURE);
2019-02-20 15:50:44 +00:00
}
2021-01-13 16:39:17 +00:00
if (ms->mp_in->counter >= mctx->mp.max_measure && ms->mp_out->counter >= mctx->mp.max_measure) {
2021-01-13 16:21:25 +00:00
measlat_stop(ctx, mctx, ms, ms->fd, fdinfo->fd);
2021-01-13 16:39:17 +00:00
return 1;
}
2021-01-08 17:30:22 +00:00
2019-09-24 13:37:12 +00:00
return 0;
2019-02-22 14:22:06 +00:00
}
2019-03-25 16:20:47 +00:00
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);
2021-01-08 17:30:22 +00:00
struct evt_core_fdinfo* reg_fdinfo = evt_core_add_fd (ctx, &fdinfo_n);
2019-03-25 16:20:47 +00:00
printf("--- Tor socket registered\n");
2021-01-13 10:25:50 +00:00
struct measure_state ms = {0};
measure_state_init (&mctx->mp, &ms);
2021-01-14 16:43:38 +00:00
ms.fd = fdinfo_n.fd;
2021-01-13 10:25:50 +00:00
register_timer (ctx, mctx, &ms, NULL);
2019-03-25 16:20:47 +00:00
return 1;
}
2019-09-24 13:37:12 +00:00
void spawn_tor_client(struct evt_core_ctx* evts) {
2019-03-25 16:20:47 +00:00
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;
2021-01-14 13:36:01 +00:00
socks5_create_dns_client (evts, "127.0.0.1", mctx->tor_port, mctx->host, atoi(mctx->port));
2019-09-24 13:37:12 +00:00
printf("--- Tor client SOCKS started\n");
2019-03-25 16:20:47 +00:00
}
int on_socks5_failed_measlat(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
evt_core_rm_fd (ctx, fdinfo->fd);
2019-09-24 13:37:12 +00:00
spawn_tor_client(ctx);
2019-03-25 16:20:47 +00:00
return 1;
}
void register_categories(struct evt_core_ctx* evts, struct measlat_ctx* mctx) {
struct evt_core_cat template = {0};
template.app_ctx = mctx;
2021-01-27 08:11:22 +00:00
evt_core_init(evts, mctx->verbose <= 0 ? 0 : mctx->verbose - 1);
2019-03-25 16:20:47 +00:00
template.cb = on_timer;
template.name = "timer";
template.flags = EPOLLIN | EPOLLET;
evt_core_add_cat(evts, &template);
2021-01-14 13:43:28 +00:00
template.cb = on_receive_measure_packet;
template.err_cb = on_receive_measure_packet_err;
2019-03-25 16:20:47 +00:00
template.name = "tcp-read";
template.flags = EPOLLIN | EPOLLET;
evt_core_add_cat(evts, &template);
2019-09-24 13:37:12 +00:00
template.cb = on_receive_measure_packet;
template.err_cb = on_receive_measure_packet_err;
2019-03-25 16:20:47 +00:00
template.name = "udp-read";
template.flags = EPOLLIN | EPOLLET;
evt_core_add_cat(evts, &template);
2019-09-24 13:37:12 +00:00
template.cb = on_tcp_co;
template.err_cb = NULL;
template.name = "tcp-co";
template.flags = EPOLLIN | EPOLLET;
evt_core_add_cat(evts, &template);
2019-03-25 16:20:47 +00:00
template.cb = on_socks5_success_measlat;
template.err_cb = on_socks5_failed_measlat;
template.name = "socks5-success";
template.flags = EPOLLET;
2019-04-02 14:54:50 +00:00
evt_core_add_cat(evts, &template);
2019-03-25 16:20:47 +00:00
template.cb = on_socks5_failed_measlat;
template.err_cb = on_socks5_failed_measlat;
template.name = "socks5-failed";
template.flags = EPOLLET;
2019-04-02 14:54:50 +00:00
evt_core_add_cat(evts, &template);
2019-03-25 16:20:47 +00:00
socks5_init(evts);
printf("--- Categories registered\n");
}
2019-09-24 13:37:12 +00:00
void spawn_udp_client(struct evt_core_ctx* evts) {
2019-03-25 16:20:47 +00:00
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";
sprintf(fdinfo.url, "udp:read:%s:%s", mctx->host, mctx->port);
2021-01-08 17:30:22 +00:00
struct evt_core_fdinfo* reg_fdinfo = evt_core_add_fd (evts, &fdinfo);
2019-09-24 13:37:12 +00:00
printf("--- UDP client registered\n");
2021-01-14 11:23:58 +00:00
struct sockaddr_in my_addr;
socklen_t my_addr_len = sizeof(my_addr);
getsockname(udp_sock, (struct sockaddr *) &my_addr, &my_addr_len);
uint16_t myPort = ntohs(my_addr.sin_port);
printf("client port is %u\n", myPort);
2021-01-13 10:25:50 +00:00
struct measure_state ms = {0};
measure_state_init (&mctx->mp, &ms);
2021-01-14 16:43:38 +00:00
ms.fd = udp_sock;
2021-01-14 11:23:58 +00:00
2021-01-13 10:25:50 +00:00
register_timer (evts, mctx, &ms, NULL);
2019-09-24 13:37:12 +00:00
}
void spawn_udp_server(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_server (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;
sprintf(url, "udp:rw:127.0.0.1:%s", mctx->port);
fdinfo.cat->name = "udp-read";
evt_core_add_fd(evts, &fdinfo);
printf("--- UDP server is listening\n");
}
2019-03-25 16:20:47 +00:00
2021-01-05 10:34:10 +00:00
void spawn_tcp_client(struct evt_core_ctx* evts) {
struct evt_core_cat* ucat = evt_core_get_from_cat (evts, "tcp-read");
if (ucat == NULL) {
fprintf(stderr, "Category tcp-read not found\n");
exit(EXIT_FAILURE);
}
struct measlat_ctx* mctx = ucat->app_ctx;
int tcp_sock = create_tcp_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 = tcp_sock;
fdinfo.cat->name = "tcp-read";
sprintf(fdinfo.url, "tcp:read:%s:%s", mctx->host, mctx->port);
2021-01-08 17:30:22 +00:00
struct evt_core_fdinfo* reg_fdinfo = evt_core_add_fd (evts, &fdinfo);
2021-01-05 10:34:10 +00:00
printf("--- TCP client registered\n");
2021-01-14 10:47:50 +00:00
struct sockaddr_in my_addr;
socklen_t my_addr_len = sizeof(my_addr);
getsockname(tcp_sock, (struct sockaddr *) &my_addr, &my_addr_len);
uint16_t myPort = ntohs(my_addr.sin_port);
printf("client port is %u\n", myPort);
2021-01-14 11:23:58 +00:00
struct measure_state ms = {0};
measure_state_init (&mctx->mp, &ms);
2021-01-14 16:43:38 +00:00
ms.fd = tcp_sock;
2021-01-14 11:23:58 +00:00
2021-01-13 10:25:50 +00:00
register_timer (evts, mctx, &ms, NULL);
2021-01-05 10:34:10 +00:00
}
2021-01-14 12:23:40 +00:00
void spawn_tcp_server(struct evt_core_ctx* evts, uint16_t port) {
2019-09-24 13:37:12 +00:00
char buffer[1024];
int tcp_serv_sock, err;
2021-01-14 12:23:40 +00:00
sprintf(buffer, "%d", port);
2019-09-24 13:37:12 +00:00
tcp_serv_sock = create_tcp_server ("0.0.0.0", buffer);
err = listen(tcp_serv_sock, SOMAXCONN);
struct evt_core_cat cat = {0};
struct evt_core_fdinfo fdinfo = {0};
fdinfo.cat = &cat;
fdinfo.url = buffer;
fdinfo.fd = tcp_serv_sock;
2021-01-14 12:23:40 +00:00
sprintf(buffer, "tcp:co:127.0.0.1:%d", port);
2019-09-24 13:37:12 +00:00
fdinfo.cat->name = "tcp-co";
evt_core_add_fd(evts, &fdinfo);
printf("--- TCP server is listening\n");
2019-03-25 16:20:47 +00:00
}
2021-01-14 12:23:40 +00:00
void measlat_create_onion_services(struct tor_os_str* tos, struct tor_ctl* tctl, uint16_t port, enum TOR_ONION_FLAGS tof) {
2020-02-01 22:33:45 +00:00
tor_os_create (tos, "onion_services.pub", "onion_services.txt", 1);
2019-09-24 17:48:42 +00:00
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);
}
2021-01-14 12:23:40 +00:00
err = tor_ctl_add_onion (tctl, tos, &port, 1, tof);
2019-09-24 17:48:42 +00:00
if (err != 0) {
fprintf(stderr, "Unable to create Onion Services (error: %d)\n", err);
exit(EXIT_FAILURE);
}
2021-01-08 17:30:22 +00:00
printf("--- Onion services created\n");
2019-09-24 17:48:42 +00:00
}
2021-01-14 12:23:40 +00:00
void spawn_onion_server(struct evt_core_ctx* evts, uint16_t port, struct tor_os_str* tos, struct tor_ctl* tctl, struct measlat_ctx* mctx) {
spawn_tcp_server(evts, port);
measlat_create_onion_services (tos, tctl, port, mctx->tor_flags);
2021-01-13 10:25:50 +00:00
}
2019-02-22 14:22:06 +00:00
int main(int argc, char** argv) {
2019-03-06 16:12:58 +00:00
setvbuf(stdout, NULL, _IONBF, 0);
2019-02-22 14:22:06 +00:00
printf("~ measlat ~\n");
2019-03-25 16:20:47 +00:00
int opt;
struct measlat_ctx mctx = {0};
2021-01-26 11:13:03 +00:00
mctx.mp.tag = "undefined";
2019-02-22 14:22:06 +00:00
struct evt_core_ctx evts = {0};
2021-01-13 10:25:50 +00:00
struct tor_os_str tos = {0};
struct tor_ctl tctl = {0};
2021-01-25 17:00:19 +00:00
tctl.os_endpoint = "127.0.0.1";
2021-01-26 11:13:03 +00:00
measure_params_init (&mctx.mp);
2019-02-22 14:22:06 +00:00
// 1. Parse parameters
2021-01-26 11:13:03 +00:00
while ((opt = getopt(argc, argv, "vq:h:p:c:s:i:t:lnm:")) != -1) {
2019-02-22 14:22:06 +00:00
switch(opt) {
2019-04-01 12:16:41 +00:00
case 'v':
mctx.verbose++;
break;
2019-03-11 14:17:15 +00:00
case 'h': // host
2019-03-25 16:20:47 +00:00
mctx.host = optarg;
2019-02-22 14:22:06 +00:00
break;
2019-03-11 14:17:15 +00:00
case 'p': // port
2019-03-25 16:20:47 +00:00
mctx.port = optarg;
2019-02-22 14:22:06 +00:00
break;
2021-01-14 13:36:01 +00:00
case 'q':
mctx.tor_port = optarg;
break;
2019-09-24 13:37:12 +00:00
case 'l':
2021-01-13 10:25:50 +00:00
mctx.role = MEASLAT_SERVER;
2019-09-24 13:37:12 +00:00
break;
2019-03-11 14:17:15 +00:00
case 't': // transport
2019-03-25 16:20:47 +00:00
mctx.transport = optarg;
2019-03-11 14:17:15 +00:00
break;
case 'c': // count
2021-01-13 10:25:50 +00:00
mctx.mp.max_measure = atoi(optarg);
2019-02-22 14:22:06 +00:00
break;
2019-03-11 14:17:15 +00:00
case 's': // size - payload in bytes
2021-01-26 11:13:03 +00:00
measure_params_setpl(&mctx.mp, atoi(optarg));
2019-02-22 14:22:06 +00:00
break;
2019-09-24 17:48:42 +00:00
case 'n':
mctx.tor_flags |= TOR_ONION_FLAG_NON_ANONYMOUS;
break;
2019-03-11 14:17:15 +00:00
case 'i': // interval - every ms
2021-01-13 10:25:50 +00:00
mctx.mp.interval = atoi(optarg);
2019-02-22 14:22:06 +00:00
break;
2021-01-26 11:13:03 +00:00
case 'm':
mctx.mp.tag = optarg;
break;
2019-02-22 14:22:06 +00:00
default:
goto usage;
}
}
// 2. Check and fix parameters
2021-01-14 16:43:38 +00:00
measure_state_init (&mctx.mp, &ms_transi);
2019-09-24 13:37:12 +00:00
mctx.addrlen = sizeof(mctx.addr);
2019-03-25 16:20:47 +00:00
if (mctx.transport == NULL) mctx.transport = "udp";
2021-01-08 17:30:22 +00:00
if (strcmp(mctx.transport, "udp") == 0) mctx.connectionless = 1;
2021-01-13 10:25:50 +00:00
if (mctx.host == NULL) mctx.host = "127.0.0.1";
if (mctx.port == NULL) mctx.port = mctx.connectionless ? "9000" : "7500";
2021-01-14 13:36:01 +00:00
if (mctx.tor_port == NULL) mctx.tor_port = "9050";
2019-02-22 14:22:06 +00:00
2021-01-14 13:36:01 +00:00
printf("[measlat_conf] host=%s, port=%s, listen=%d, transport=%s, count=%ld, size=%ld, interval=%ld, tor_port=%s\n",
mctx.host, mctx.port, mctx.role, mctx.transport, mctx.mp.max_measure, mctx.mp.payload_size, mctx.mp.interval, mctx.tor_port);
2019-09-24 13:37:12 +00:00
2021-01-13 10:25:50 +00:00
// 3. Create event structure
register_categories(&evts, &mctx);
2019-09-24 17:48:42 +00:00
2021-01-13 10:25:50 +00:00
// 4. Register services
if (mctx.role == MEASLAT_SERVER) {
if (streq(mctx.transport, "udp")) spawn_udp_server (&evts);
2021-01-14 12:23:40 +00:00
else if (streq(mctx.transport, "tcp")) spawn_tcp_server(&evts, atoi(mctx.port));
else if (streq(mctx.transport, "tor")) spawn_onion_server (&evts, atoi(mctx.port), &tos, &tctl, &mctx);
2021-01-13 10:25:50 +00:00
}
else if (mctx.role == MEASLAT_CLIENT) {
if (streq(mctx.transport, "udp")) spawn_udp_client(&evts);
else if (streq(mctx.transport, "tor")) spawn_tor_client(&evts);
else if (streq(mctx.transport, "tcp")) spawn_tcp_client(&evts);
2019-09-24 17:48:42 +00:00
}
2019-09-24 13:37:12 +00:00
else exit(EXIT_FAILURE);
2019-02-20 15:50:44 +00:00
2021-01-13 10:25:50 +00:00
// 5. Run main loop
2019-02-20 15:50:44 +00:00
evt_core_loop(&evts);
return 0;
2019-02-22 14:22:06 +00:00
usage:
2021-01-25 16:19:32 +00:00
fprintf(stderr, "Usage: %s [-h <host>] [-p <port>] [-l] [-r] [-t <udp|tcp|tor>] [-c <count>] [-i <ms>] [-s <bytes>]\n", argv[0]);
2019-02-22 14:22:06 +00:00
exit(EXIT_FAILURE);
}