From 8fdcc452d485b34241214c4ec9bb7e4078b0c8da Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 15 Oct 2019 18:38:51 +0200 Subject: [PATCH] WIP redundancy --- src/algo_lightning.c | 56 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/algo_lightning.c b/src/algo_lightning.c index 6fb618e..1592dfb 100644 --- a/src/algo_lightning.c +++ b/src/algo_lightning.c @@ -113,6 +113,44 @@ void algo_lightning_init(struct evt_core_ctx* ctx, struct algo_ctx* app_ctx, str printf("explain = %s\n", lightc->explain ? "yes" : "no"); } +void algo_lightning_pad(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) { + struct algo_ctx* app_ctx = fdinfo->cat->app_ctx; + struct light_ctx* lightc = app_ctx->misc; + uint64_t ref = lightc->pkt_sent_id; + + // 0. Store current buffer to application + fprintf(stderr, " [algo_lightning] Store buffer with pointer %p\n", (void*) ref); + 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) + fprintf(stderr, " [algo_lightning] Clean queue\n"); + if (ref > lightc->total_links && get_app_buffer (&app_ctx->br, (void *)(ref - lightc->total_links))) { + mv_buffer_atof (&app_ctx->br, (void *)(ref - lightc->total_links)); + } + + // 2. Append abstract packets stored in our buffers + uint64_t add_ref = ref; + while(1) { + fprintf(stderr, " [algo_lightning] Enter loop\n"); + 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) { + fprintf(stderr, "Invalid buffer payload!\n"); + exit(EXIT_FAILURE); + } + + fprintf(stderr, " [algo_lightning] Start loop, currently %ld bytes, would be %ld\n", buffer_full_size (bp), buffer_full_size (bp) + ap->fmt.headers.size); + + if (buffer_full_size (bp) + ap->fmt.headers.size > TOR_CELL_SIZE - lightc->monit_pkt_size) break; + + buffer_append_ap (bp, ap); + /*if (ctx->verbose > 1)*/ fprintf(stderr, " [algo_lightning] Pad packet (now %ld bytes)\n", buffer_full_size (bp)); + } +} + void monitoring(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) { struct algo_ctx* app_ctx = fdinfo->cat->app_ctx; struct light_ctx* lightc = app_ctx->misc; @@ -313,7 +351,10 @@ int algo_lightning_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* struct light_ctx* lightc = app_ctx->misc; union abstract_packet* ap = (union abstract_packet*) &bp->ip; - // Last step, send packet + // Pad packet + algo_lightning_pad (ctx, fdinfo, bp); + + // Compute stats struct stat_entry stats[MAX_LINKS] = {0}; algo_lightning_update_stats(lightc, stats); @@ -325,6 +366,7 @@ int algo_lightning_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* printf("\n"); } + // Init vars struct timespec now, sel_link_last, temp_time; set_now(&now); uint64_t now_timestamp = timespec_get_unit(&now, MILISEC); @@ -353,8 +395,16 @@ int algo_lightning_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* timespec_diff (&now, &sel_link_last, &temp_time); uint64_t elapsed = timespec_get_unit(&temp_time, MILISEC); if (lightc->is_measlat) { - struct measure_packet *mp = (void*)&ap->fmt.content.udp_encapsulated.payload; - mp->flag = 1; + union abstract_packet* cur = ap; + while (cur != NULL) { + if (ap->fmt.headers.cmd != CMD_UDP_ENCAPSULATED) { + cur = ap_next(cur); + continue; + } + struct measure_packet *mp = (void*)&cur->fmt.content.udp_encapsulated.payload; + mp->flag = 1; + cur = ap_next(cur); + } } if (elapsed >= lightc->sleep_duration) { send_message (ctx, bp);