Fix a ring comparison bug

This commit is contained in:
Quentin 2019-03-19 20:48:07 +01:00
parent 5a6685e7c2
commit f312b5fe18
3 changed files with 9 additions and 10 deletions

View file

@ -79,7 +79,7 @@ int set_timeout(struct evt_core_ctx* evts, uint64_t milli_sec, struct waited_pkt
fdinfo.cat = &cat;
fdinfo.url = url;
printf("Will add a timeout of %ld ms\n", milli_sec);
//printf("Will add a timeout of %ld ms\n", milli_sec);
if (clock_gettime(CLOCK_REALTIME, &now) == -1) {
perror("clock_gettime");
exit(EXIT_FAILURE);
@ -124,6 +124,7 @@ void rr_pkt_register(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo,
// 2. If packet arrived too late, we discard it
if (ring_gt(rr->recv_id, bp->ip.ap.str.id - 1)) {
// Packet has already been delivered or dropped, we free the buffer
fprintf(stderr, "Packet %d arrived too late (current: %d)\n", bp->ip.ap.str.id, rr->recv_id);
mv_buffer_wtor (app_ctx, fdinfo, bp);
return;
}
@ -131,20 +132,20 @@ void rr_pkt_register(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo,
// 3. If packet arrived too early, we register a timer
if (ring_lt(rr->recv_id, bp->ip.ap.str.id - 1)) {
int64_t timeout = rr->mjit - (int64_t) bp->ip.ap.str.deltat;
printf("%ld - %ld = %ld\n", rr->mjit, (int64_t) bp->ip.ap.str.deltat, timeout);
//printf("%ld - %ld = %ld\n", rr->mjit, (int64_t) bp->ip.ap.str.deltat, timeout);
if (timeout <= 0) timeout = 0;
int idx_waited = (bp->ip.ap.str.id - 1) % PACKET_BUFFER_SIZE;
rr->wait[idx_waited].on = 1;
rr->wait[idx_waited].id = bp->ip.ap.str.id;
rr->wait[idx_waited].link_num = bp->ip.ap.str.prevlink;
rr->wait[idx_waited].timer_fd = set_timeout(ctx, timeout, &rr->wait[idx_waited]);
g_hash_table_remove(app_ctx->used_buffer, &fdinfo->fd); // We remove the packet from the reading buffer
}
// 4. We queue the packet
int idx_real = bp->ip.ap.str.id % PACKET_BUFFER_SIZE;
rr->real[idx_real].bp = bp;
rr->real[idx_real].link_fd = fdinfo->fd;
g_hash_table_remove(app_ctx->used_buffer, &fdinfo->fd); // We remove the packet from the reading buffer
// 5. We make sure that the remote link is set to up
char buffer[16];
@ -307,7 +308,7 @@ int rr_on_udp_read(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
rr->current_link = (rr->current_link + 1) % 10;
if (!(rr->my_links & (1 << rr->current_link))) continue;
sprintf(url, "tcp:write:127.0.0.1:%d", 7500 + rr->current_link); //@FIXME Hardcoded
printf("-- Trying %s\n", url);
//printf("-- Trying %s\n", url);
to_fdinfo = evt_core_get_from_url (ctx, url);
if (to_fdinfo != NULL) {
printf("Selected url %s for pkt %d to be sent on Tor\n", url, bp->ip.ap.str.id);
@ -366,13 +367,11 @@ int rr_on_timer(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
struct rr_ctx* rr = app_ctx->misc;
printf("Timer has been triggered\n");
struct waited_pkt* pkt = fdinfo->other;
pkt->on = 0;
evt_core_rm_fd(ctx, fdinfo->fd);
rr->remote_links &= 0xffff ^ 1 << pkt->link_num;
//rr->remote_links &= 0xffff ^ 1 << pkt->link_num;
rr->recv_id = pkt->id;
rr_pkt_unroll (ctx, app_ctx);

View file

@ -37,7 +37,7 @@ union abstract_packet {
struct {
uint16_t size;
uint16_t port;
uint8_t id;
uint16_t id;
uint8_t bitfield;
uint8_t prevlink;
uint16_t deltat;

View file

@ -64,7 +64,7 @@ int ring_buffer_used_space(struct ring_buffer* rb) {
// Why we are using modulo, plus and modulo again:
// https://stackoverflow.com/a/1907585
int ring_ge(uint16_t v1, uint16_t v2) {
uint32_t vv1 = (uint32_t) v1, vv2 = (uint32_t) v2;
int64_t vv1 = (int64_t) v1, vv2 = (int64_t) v2;
return (((vv1 - vv2) % UINT16_MAX) + UINT16_MAX) % UINT16_MAX <= UINT16_MAX / 2;
}