tor_multipath_voip/src/algo_thunder.c

194 lines
6.5 KiB
C
Raw Normal View History

2019-08-09 15:01:28 +00:00
#include <sys/timerfd.h>
#include "algo_utils.h"
#include "utils.h"
#include "url.h"
#include "proxy.h"
#include "timer.h"
2019-08-27 12:31:27 +00:00
// A Tor cell size is 512 bytes but handle only 498 bytes of data
#define TOR_CELL_SIZE 498
2019-08-13 15:07:52 +00:00
struct thunder_ctx {
uint16_t recv_id;
uint16_t emit_id;
2019-08-26 10:08:31 +00:00
uint8_t selected_link;
uint8_t total_links;
uint8_t delta_t_per_link[64];
2019-08-27 15:28:14 +00:00
uint64_t received_pkts_on_link[64];
2019-08-26 15:35:23 +00:00
uint8_t blacklisted[64];
2019-08-27 08:10:17 +00:00
size_t monit_pkt_size;
struct timespec prev_link_time, prev_packet_time;
2019-08-13 15:07:52 +00:00
};
2019-08-27 08:10:17 +00:00
uint64_t compute_delta(struct timespec* prev_time, uint64_t max) {
struct timespec curr;
int secs, nsecs;
uint64_t mili_sec;
2019-08-27 12:31:27 +00:00
// 1. We compute the time difference
2019-08-27 08:10:17 +00:00
if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1){
perror("clock_gettime error");
exit(EXIT_FAILURE);
}
secs = curr.tv_sec - prev_time->tv_sec;
nsecs = curr.tv_nsec - prev_time->tv_nsec;
*prev_time = curr;
mili_sec = secs * 1000 + nsecs / 1000000;
if (mili_sec > max) mili_sec = max;
return mili_sec;
}
2019-08-09 15:01:28 +00:00
void prepare(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) {
2019-08-13 15:07:52 +00:00
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
struct thunder_ctx* thunderc = app_ctx->misc;
2019-08-27 12:31:27 +00:00
thunderc->emit_id++;
2019-08-13 15:07:52 +00:00
union abstract_packet metadata = {
.fmt.headers.cmd = CMD_UDP_METADATA_THUNDER,
.fmt.headers.size = sizeof(metadata.fmt.headers) + sizeof(metadata.fmt.content.udp_metadata_thunder),
.fmt.headers.flags = 0,
.fmt.content.udp_metadata_thunder.id = thunderc->emit_id,
};
2019-08-26 10:08:31 +00:00
buffer_append_ap (bp, &metadata);
2019-08-09 15:01:28 +00:00
}
void pad(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) {
2019-08-27 08:10:17 +00:00
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
struct thunder_ctx* thunderc = app_ctx->misc;
uint64_t ref = 0l + thunderc->emit_id;
2019-08-09 15:01:28 +00:00
2019-08-27 15:28:14 +00:00
dup_buffer_toa (&app_ctx->br, bp, (void *)ref);
// 1. Clean old buffers (we keep only thunderc->total_links buffer, keeping more would be useless)
if (ref > thunderc->total_links && get_app_buffer (&app_ctx->br, (void *)(ref - thunderc->total_links))) {
mv_buffer_atof (&app_ctx->br, (void *)(ref - thunderc->total_links));
2019-08-27 08:10:17 +00:00
}
2019-08-27 12:31:27 +00:00
// 2. Append abstract packets stored in our buffers
uint64_t add_ref = ref;
while(1) {
if (add_ref < 1) break;
add_ref--;
struct buffer_packet *bp_iter = get_app_buffer (&app_ctx->br, (void *)add_ref);
if (bp_iter == NULL) break;
union abstract_packet *ap = buffer_first_ap (bp_iter);
if (ap->fmt.headers.cmd != CMD_UDP_ENCAPSULATED || ap->fmt.headers.flags & FLAG_READ_NEXT) {
fprintf(stderr, "Invalid buffer!\n");
exit(EXIT_FAILURE);
}
if (buffer_full_size (bp) + ap->fmt.headers.size > TOR_CELL_SIZE - thunderc->monit_pkt_size) break;
buffer_append_ap (bp, ap);
2019-08-27 08:10:17 +00:00
}
2019-08-09 15:01:28 +00:00
}
int schedule(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) {
2019-08-26 10:08:31 +00:00
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
struct thunder_ctx* thunderc = app_ctx->misc;
struct evt_core_fdinfo *to_fdinfo = NULL;
struct evt_core_cat* cat = evt_core_get_from_cat (ctx, "tcp-write");
2019-08-26 15:35:23 +00:00
do {
// 1. We choose the link
thunderc->selected_link = (thunderc->selected_link + 1) % cat->socklist->len;
// 2. We create the packet template
2019-08-26 10:08:31 +00:00
union abstract_packet links = {
.fmt.headers.cmd = CMD_LINK_MONITORING_THUNDER,
2019-08-27 08:10:17 +00:00
.fmt.headers.size = thunderc->monit_pkt_size,
2019-08-26 10:08:31 +00:00
.fmt.headers.flags = 0,
2019-08-26 15:35:23 +00:00
.fmt.content.link_monitoring_thunder.links_status = {}
2019-08-26 10:08:31 +00:00
};
to_fdinfo = g_array_index(cat->socklist, struct evt_core_fdinfo*, thunderc->selected_link);
2019-08-26 15:35:23 +00:00
// 3. We append the template to the buffer
2019-08-26 10:08:31 +00:00
struct buffer_packet* bp_dup = dup_buffer_tow (&app_ctx->br, bp, to_fdinfo);
2019-08-26 15:35:23 +00:00
union abstract_packet *new_ap = buffer_append_ap (bp_dup, &links);
// 4. We compute the time difference
2019-08-27 08:10:17 +00:00
uint64_t mili_sec = compute_delta (&thunderc->prev_link_time, 200);
2019-08-26 15:35:23 +00:00
// 5. We create the array
struct link_info *li = &new_ap->fmt.content.link_monitoring_thunder.links_status;
for (int i = 0; i < thunderc->total_links; i++) {
thunderc->delta_t_per_link[i] += mili_sec;
li[i].delta_t = thunderc->delta_t_per_link[i];
}
li[thunderc->selected_link].delta_t = 0;
2019-08-26 10:08:31 +00:00
main_on_tcp_write(ctx, to_fdinfo);
2019-08-26 15:35:23 +00:00
} while (thunderc->blacklisted[thunderc->selected_link]);
2019-08-26 10:08:31 +00:00
if (ctx->verbose > 1) fprintf(stderr, " [algo_thunder] Packets sent\n");
// Release the buffer
mv_buffer_rtof (&app_ctx->br, fdinfo);
2019-08-09 15:01:28 +00:00
return 0;
}
void algo_thunder_init(struct evt_core_ctx* ctx, struct algo_ctx* app_ctx, struct algo_params* ap) {
2019-08-13 15:07:52 +00:00
app_ctx->misc = malloc(sizeof(struct thunder_ctx));
if (app_ctx->misc == NULL) {
perror("malloc failed in algo thunder init");
exit(EXIT_FAILURE);
}
memset(app_ctx->misc, 0, sizeof(struct thunder_ctx));
2019-08-26 10:08:31 +00:00
struct thunder_ctx* thunderc = app_ctx->misc;
2019-08-26 15:35:23 +00:00
thunderc->selected_link = UINT8_MAX - 1;
2019-08-27 08:10:17 +00:00
union abstract_packet links = {};
thunderc->monit_pkt_size = sizeof(links.fmt.headers) + sizeof(links.fmt.content.link_monitoring_thunder) + sizeof(struct link_info) * (thunderc->total_links - 1);
2019-08-09 15:01:28 +00:00
}
2019-08-27 15:28:14 +00:00
void classify(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) {
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
struct thunder_ctx* thunderc = app_ctx->misc;
union abstract_packet* ap = buffer_first_ap (bp);
while (ap != NULL && ap->fmt.headers.cmd != CMD_LINK_MONITORING_THUNDER) ap = ap_next(ap);
if (ap == NULL) {
fprintf(stderr, "Unable to find our packet\n");
exit(EXIT_FAILURE);
}
int link_id = url_get_port_int(fdinfo->url) - 7500;
thunderc->received_pkts_on_link[link_id]++;
2019-08-26 15:48:22 +00:00
2019-08-27 15:28:14 +00:00
struct link_info *li = &ap->fmt.content.link_monitoring_thunder.links_status;
for (int i = 0; i < thunderc->total_links; i++) {
uint64_t expected = i <= link_id ? thunderc->received_pkts_on_link[link_id] : thunderc->received_pkts_on_link[link_id] - 1;
if (thunderc->received_pkts_on_link[i] >= expected) continue; // Nothing to do, all packets have been received
set_timeout ()
}
2019-08-26 15:48:22 +00:00
}
void unpad() {
2019-08-09 15:01:28 +00:00
2019-08-26 15:48:22 +00:00
}
void adapt() {
}
int algo_thunder_on_stream(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) {
2019-08-27 15:28:14 +00:00
classify(ctx, fdinfo, bp);
2019-08-26 15:48:22 +00:00
unpad();
adapt();
2019-08-09 15:01:28 +00:00
return 0;
}
int algo_thunder_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) {
prepare(ctx, fdinfo, bp);
pad(ctx, fdinfo, bp);
return schedule(ctx, fdinfo, bp);
}
int algo_thunder_on_err(struct evt_core_ctx *ctx, struct evt_core_fdinfo *fdinfo) {
if (strstr(fdinfo->cat->name, "udp") != NULL) return 1;
return 0;
}