tor_multipath_voip/src/measure.c

111 lines
3.6 KiB
C
Raw Normal View History

2019-09-23 14:32:59 +00:00
#include "measure.h"
2021-01-13 10:25:50 +00:00
void measure_params_init(struct measure_params* mp) {
if (mp->interval <= 0) mp->interval = 1000;
if (mp->max_measure <= 0) mp->max_measure = 3;
if (mp->payload_size < sizeof(struct measure_packet)) mp->payload_size = sizeof(struct measure_packet);
}
void measure_state_init(struct measure_params* mp, struct measure_state* ms) {
if ((ms->mp_out = malloc(sizeof(char) * mp->payload_size)) == NULL) {
perror("payload malloc failed");
exit(EXIT_FAILURE);
}
memset(ms->mp_out, 0, mp->payload_size);
if ((ms->mp_in = malloc(sizeof(char) * mp->payload_size)) == NULL) {
perror("payload malloc failed");
exit(EXIT_FAILURE);
}
memset(ms->mp_in, 0, mp->payload_size);
char *my_msg = "Tu n'es pas tout a fait la misere,\nCar les levres les plus pauvres te denoncent\nPar un sourire.";
size_t msg_len = strlen(my_msg);
size_t cursor_msg = 0;
char* pl = (char*) ms->mp_out;
for (size_t i = sizeof(struct measure_packet); i < mp->payload_size; i++) {
pl[i] = my_msg[cursor_msg];
cursor_msg = (cursor_msg + 1) % msg_len;
}
}
void measure_parse(struct measure_params* mp, struct measure_state* ms) {
2019-09-23 14:32:59 +00:00
struct timespec curr;
uint64_t micro_sec;
2021-01-13 10:25:50 +00:00
if (ms->mp_nin != mp->payload_size) {
fprintf(stderr, "read size: %ld, expected: %ld\n", ms->mp_nin, mp->payload_size);
2020-01-31 23:19:04 +00:00
int i;
fprintf(stderr, "received buffer:\n");
2021-01-13 10:25:50 +00:00
for (i = 0; i < mp->payload_size; i++) {
2020-01-31 23:19:04 +00:00
if (i > 0) fprintf(stderr, ":");
2021-01-13 10:25:50 +00:00
fprintf(stderr, "%02x", (unsigned char) ((char*)ms->mp_in)[i]);
2020-01-31 23:19:04 +00:00
}
fprintf(stderr, "\n");
fprintf(stderr, "local buffer (reference):\n");
2021-01-13 10:25:50 +00:00
for (i = 0; i < mp->payload_size; i++) {
2020-01-31 23:19:04 +00:00
if (i > 0) fprintf(stderr, ":");
2021-01-13 10:25:50 +00:00
fprintf(stderr, "%02X", (unsigned char) ((char*)ms->mp_out)[i]);
2020-01-31 23:19:04 +00:00
}
fprintf(stderr, "\n");
2019-10-17 09:00:18 +00:00
perror("read error, payload has wrong size");
2019-09-23 14:32:59 +00:00
}
2020-01-31 21:49:54 +00:00
if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1){
2019-09-23 14:32:59 +00:00
perror("clock_gettime error");
exit(EXIT_FAILURE);
}
2021-01-13 10:25:50 +00:00
micro_sec = elapsed_micros (&ms->mp_in->emit_time, &curr);
uint8_t is_slow = ms->mp_in->flag >> 7;
uint8_t is_vanilla = (ms->mp_in->flag & 0x40) >> 6;
uint8_t link_id = ms->mp_in->flag & 0x3f;
2019-10-18 15:29:49 +00:00
printf(
2021-01-14 11:23:58 +00:00
"[%s] src=%d, id=%lu, owd=%luµs, flag=%d, link=%d, vanilla=%d\n",
2020-01-23 09:48:36 +00:00
current_human_datetime(),
2021-01-13 10:25:50 +00:00
ms->fd,
2021-01-14 11:23:58 +00:00
ms->mp_in->counter,
2019-10-18 15:29:49 +00:00
micro_sec,
is_slow,
link_id,
is_vanilla);
2019-09-23 14:32:59 +00:00
}
2021-01-13 10:25:50 +00:00
struct measure_packet* measure_generate(struct measure_params* mp, struct measure_state* ms) {
ms->mp_out->counter++;
ms->mp_out->flag = 0;
if (clock_gettime(CLOCK_MONOTONIC, &ms->mp_out->emit_time) == -1) {
2019-09-24 13:37:12 +00:00
perror("clock_gettime error");
exit(EXIT_FAILURE);
}
2021-01-13 10:25:50 +00:00
return ms->mp_out;
2019-09-24 13:37:12 +00:00
}
2021-01-13 10:25:50 +00:00
void measure_next_tick(struct measure_params *mp, struct measure_state* ms, struct timespec *next) {
//struct measure_packet *head = (struct measure_packet*) ms->payload_rcv;
struct timespec now = {0}, *sent_at = &ms->mp_in->emit_time;
2019-09-24 13:37:12 +00:00
2020-01-31 21:49:54 +00:00
if (clock_gettime(CLOCK_MONOTONIC, &now) == -1) {
2019-09-24 13:37:12 +00:00
perror("clock_gettime error");
exit(EXIT_FAILURE);
}
memcpy(next, sent_at, sizeof(struct timespec));
while(!timespec_gt (next, &now)) {
2021-01-13 10:25:50 +00:00
next->tv_nsec += mp->interval * 1000000L;
2019-09-24 13:37:12 +00:00
if (next->tv_nsec > ONE_SEC) {
next->tv_sec += next->tv_nsec / ONE_SEC;
next->tv_nsec = next->tv_nsec % ONE_SEC;
}
2021-01-13 10:25:50 +00:00
ms->mp_out->counter++;
2019-09-24 13:37:12 +00:00
}
2021-01-13 10:25:50 +00:00
ms->mp_out->counter--;
printf("interval: %ld\n", mp->interval);
2019-09-24 13:37:12 +00:00
printf("sent_at: sec=%ld nsec=%ld \n", (uint64_t) sent_at->tv_sec, (uint64_t) sent_at->tv_nsec);
printf("now: sec=%ld nsec=%ld \n", (uint64_t) now.tv_sec, (uint64_t) now.tv_nsec);
printf("next: sec=%ld nsec=%ld \n", (uint64_t) next->tv_sec, (uint64_t) next->tv_nsec);
}