Fix a first bug

This commit is contained in:
Quentin 2021-01-13 17:21:25 +01:00
parent 5b98780d79
commit 08f1632951

View file

@ -30,6 +30,10 @@ void free_ms(void* obj) {
free(obj); free(obj);
} }
int streq(char* s1, char* s2) {
return strcmp(s1, s2) == 0;
}
struct evt_core_fdinfo* register_timer(struct evt_core_ctx* evts, struct measlat_ctx* mctx, struct measure_state* ms, struct timespec* next_tick) { struct evt_core_fdinfo* register_timer(struct evt_core_ctx* evts, struct measlat_ctx* mctx, struct measure_state* ms, struct timespec* next_tick) {
struct timespec now; struct timespec now;
struct itimerspec timer_config; struct itimerspec timer_config;
@ -91,15 +95,14 @@ void measlat_stop(
struct evt_core_ctx* ctx, struct evt_core_ctx* ctx,
struct measlat_ctx* mctx, struct measlat_ctx* mctx,
struct measure_state* ms, struct measure_state* ms,
struct evt_core_fdinfo* net, int net_fd, int timer_fd) {
struct evt_core_fdinfo* timer) {
if (ms->mp_in->counter < mctx->mp.max_measure) return; if (ms->mp_in->counter < mctx->mp.max_measure) return;
if (ms->mp_out->counter < mctx->mp.max_measure) return; if (ms->mp_out->counter < mctx->mp.max_measure) return;
printf("All measurements done\n"); printf("All measurements done\n");
evt_core_rm_fd(ctx, timer->fd); evt_core_rm_fd(ctx, timer_fd);
if (mctx->role == MEASLAT_CLIENT) { if (mctx->role == MEASLAT_CLIENT) {
evt_core_rm_fd(ctx, net->fd); evt_core_rm_fd(ctx, net_fd);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
} }
@ -133,11 +136,11 @@ int on_receive_measure_packet(struct evt_core_ctx* ctx, struct evt_core_fdinfo*
if (nread > 0) ms->mp_nin += nread; if (nread > 0) ms->mp_nin += nread;
if (nread == -1 && errno == EAGAIN) { if (nread == -1 && errno == EAGAIN || nread == 0) {
return 1; return 1;
} }
if (strcmp("udp", mctx->transport) != 0 && ms->mp_nin < mctx->mp.payload_size) { if (!streq("udp", mctx->transport) && ms->mp_nin < mctx->mp.payload_size) {
printf("Packet has been fragmented\n"); printf("Packet has been fragmented, %ld/%ld\n",ms->mp_nin, mctx->mp.payload_size);
return 0; return 0;
} }
@ -154,7 +157,7 @@ int on_receive_measure_packet(struct evt_core_ctx* ctx, struct evt_core_fdinfo*
// 5. Check if our measurements are done // 5. Check if our measurements are done
if (ms->mp_in->counter >= mctx->mp.max_measure && ms->mp_out->counter >= mctx->mp.max_measure) if (ms->mp_in->counter >= mctx->mp.max_measure && ms->mp_out->counter >= mctx->mp.max_measure)
measlat_stop(ctx, mctx, ms, fdinfo, assoc_timer); measlat_stop(ctx, mctx, ms, fdinfo->fd, assoc_timer->fd);
return 0; return 0;
} }
@ -210,29 +213,17 @@ int on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
struct measure_packet* head = measure_generate(&mctx->mp, ms); struct measure_packet* head = measure_generate(&mctx->mp, ms);
//printf("send(id=%ld,is_echo=%d)\n", head->counter, head->is_echo); //printf("send(id=%ld,is_echo=%d)\n", head->counter, head->is_echo);
int target_fd;
sscanf(fdinfo->url, "timer:%d", &target_fd);
struct evt_core_fdinfo* tgtinfo = evt_core_get_from_fd(ctx, target_fd);
if (tgtinfo == NULL) {
fprintf(stderr, "No fd found to send, continue\n");
return 1;
}
s = mctx->connectionless ? s = mctx->connectionless ?
sendto(tgtinfo->fd, head, mctx->mp.payload_size, 0, (struct sockaddr*)&mctx->addr, mctx->addrlen) : sendto(ms->fd, head, mctx->mp.payload_size, 0, (struct sockaddr*)&mctx->addr, mctx->addrlen) :
send(tgtinfo->fd, head, mctx->mp.payload_size, 0); send(ms->fd, head, mctx->mp.payload_size, 0);
if (s < 0 || s != mctx->mp.payload_size) { if (s < 0 || s != mctx->mp.payload_size) {
perror("Send error"); perror("Send error");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (head->counter >= mctx->mp.max_measure) { if (ms->mp_in->counter >= mctx->mp.max_measure && ms->mp_out->counter >= mctx->mp.max_measure)
printf("All measurements sent for %d\n", tgtinfo->fd); measlat_stop(ctx, mctx, ms, ms->fd, fdinfo->fd);
evt_core_rm_fd(ctx, fdinfo->fd);
return 1;
}
return 0; return 0;
} }
@ -260,6 +251,7 @@ int on_socks5_success_measlat(struct evt_core_ctx* ctx, struct evt_core_fdinfo*
printf("--- Tor socket registered\n"); printf("--- Tor socket registered\n");
struct measure_state ms = {0}; struct measure_state ms = {0};
ms.fd = fdinfo_n.fd;
measure_state_init (&mctx->mp, &ms); measure_state_init (&mctx->mp, &ms);
register_timer (ctx, mctx, &ms, NULL); register_timer (ctx, mctx, &ms, NULL);
return 1; return 1;
@ -351,6 +343,7 @@ void spawn_udp_client(struct evt_core_ctx* evts) {
printf("--- UDP client registered\n"); printf("--- UDP client registered\n");
struct measure_state ms = {0}; struct measure_state ms = {0};
ms.fd = udp_sock;
measure_state_init (&mctx->mp, &ms); measure_state_init (&mctx->mp, &ms);
register_timer (evts, mctx, &ms, NULL); register_timer (evts, mctx, &ms, NULL);
} }
@ -399,6 +392,7 @@ void spawn_tcp_client(struct evt_core_ctx* evts) {
printf("--- TCP client registered\n"); printf("--- TCP client registered\n");
struct measure_state ms = {0}; struct measure_state ms = {0};
ms.fd = tcp_sock;
measure_state_init (&mctx->mp, &ms); measure_state_init (&mctx->mp, &ms);
register_timer (evts, mctx, &ms, NULL); register_timer (evts, mctx, &ms, NULL);
} }
@ -446,10 +440,6 @@ void spawn_onion_server(struct evt_core_ctx* evts, uint16_t *ports, int ports_co
measlat_create_onion_services (tos, tctl, ports, ports_count, mctx->tor_flags); measlat_create_onion_services (tos, tctl, ports, ports_count, mctx->tor_flags);
} }
int streq(char* s1, char* s2) {
return strcmp(s1, s2) == 0;
}
int main(int argc, char** argv) { int main(int argc, char** argv) {
setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0);
printf("~ measlat ~\n"); printf("~ measlat ~\n");