Fix packet handling bug

This commit is contained in:
Quentin 2019-08-12 16:24:20 +02:00
parent b985e0041a
commit cf027261c8
2 changed files with 21 additions and 11 deletions

View file

@ -11,7 +11,7 @@ int algo_naive_on_stream(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinf
struct algo_ctx* app_ctx = fdinfo->cat->app_ctx;
union abstract_packet* ap = (union abstract_packet*) &bp->ip;
// 1. Find destination
if (ctx->verbose > 1) fprintf(stderr, " [algo_naive] 1/2 Find destination\n");
sprintf(url, "udp:write:127.0.0.1:%d", ap->fmt.content.udp_encapsulated.port);
to_fdinfo = evt_core_get_from_url (ctx, url);
if (to_fdinfo == NULL) {
@ -20,7 +20,7 @@ int algo_naive_on_stream(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinf
return 1;
}
// 2. Move buffer
if (ctx->verbose > 1) fprintf(stderr, " [algo_naive] 2/2 Move buffer\n");
mv_buffer_rtow (&app_ctx->br, fdinfo, to_fdinfo);
main_on_udp_write(ctx, to_fdinfo);
@ -36,7 +36,7 @@ int algo_naive_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdi
to_fdinfo = cat->socklist->len > 0 ? g_array_index(cat->socklist, struct evt_core_fdinfo*, 0) : NULL;
if (to_fdinfo == NULL) {
fprintf(stderr, "No fd for cat %s in udp-read. Dropping packet :( \n", cat->name);
mv_buffer_wtof (&app_ctx->br, fdinfo);
mv_buffer_wtof(&app_ctx->br, fdinfo);
return 1;
}
//printf("Pass packet from %s to %s\n", fdinfo->url, url);

View file

@ -7,27 +7,34 @@ union abstract_packet* ap_next(union abstract_packet* ap) {
return NULL;
}
union abstract_packet* buffer_last_ptr(struct buffer_packet* bp) {
union abstract_packet* buffer_last_ap(struct buffer_packet* bp) {
union abstract_packet* ap = (union abstract_packet*) &bp->ip, *apn = NULL;
while ((apn = ap_next(ap)) != NULL) ap = apn;
return ap;
}
union abstract_packet* buffer_free_ptr(struct buffer_packet* bp) {
union abstract_packet* ap = buffer_last_ptr (bp);
union abstract_packet* buffer_free_ap(struct buffer_packet* bp) {
union abstract_packet* ap = buffer_last_ap (bp);
ap = (union abstract_packet*)(&ap->raw + ap->fmt.headers.size);
return ap;
}
size_t buffer_count_ap(struct buffer_packet* bp) {
size_t s = 1;
union abstract_packet* ap = (union abstract_packet*) &bp->ip;
while ((ap = ap_next(ap)) != NULL) s++;
return s;
}
size_t get_full_size(struct buffer_packet* bp) {
return &(buffer_free_ptr (bp))->raw - &bp->ip[0];
return &(buffer_free_ap (bp))->raw - &bp->ip[0];
}
void buffer_append_ap(struct buffer_packet* bp, union abstract_packet* ap) {
buffer_last_ptr(bp)->fmt.headers.flags |= FLAG_READ_NEXT;
memcpy(buffer_last_ptr(bp), ap, ap->fmt.headers.size);
buffer_last_ap(bp)->fmt.headers.flags |= FLAG_READ_NEXT;
memcpy(buffer_last_ap(bp), ap, ap->fmt.headers.size);
}
enum FD_STATE read_packet_from_tcp(struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) {
@ -53,7 +60,10 @@ enum FD_STATE read_packet_from_tcp(struct evt_core_fdinfo* fdinfo, struct buffer
}
bp->ap_count++;
if (bp->ap_count != get_full_size (bp)) return FDS_AGAIN;
if (bp->ap_count != buffer_count_ap (bp)) {
printf(" Expected %ld packets in buffer, received %d\n", get_full_size(bp), bp->ap_count);
return FDS_AGAIN;
}
bp->mode = BP_WRITING;
bp->awrite = 0;
@ -83,7 +93,7 @@ enum FD_STATE write_packet_to_tcp(struct evt_core_fdinfo* fdinfo, struct buffer_
enum FD_STATE write_packet_to_udp(struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp, struct udp_target* udp_t) {
ssize_t nwrite;
union abstract_packet* ap = (union abstract_packet*) (&bp->ip + bp->awrite);
union abstract_packet* end = buffer_free_ptr (bp);
union abstract_packet* end = buffer_free_ap(bp);
if (bp->mode != BP_WRITING) return FDS_ERR;
while (ap != end) {