diff --git a/src/algo_rr.c b/src/algo_rr.c index 610f690..248a5af 100644 --- a/src/algo_rr.c +++ b/src/algo_rr.c @@ -205,6 +205,8 @@ int algo_rr_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo struct evt_core_fdinfo *to_fdinfo = NULL; uint16_t min_pkt; char url[255]; + size_t health_packet_size = sizeof(bp->ip.ap.fmt.headers) - sizeof(bp->ip.ap.fmt.content.health); + size_t max_size = sizeof(struct internet_packet) - health_packet_size; if (ctx->verbose > 1) fprintf(stderr, " [algo/rr] Read a UDP packet on URL %s\n", fdinfo->url); @@ -230,13 +232,24 @@ int algo_rr_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo bp->ip.ap.fmt.content.clear.id = rr->sent_content_id; min_pkt = rr->sent_content_id; rr->sent_content_id++; + if (bp->ip.ap.fmt.headers.size > max_size) { + fprintf(stderr, "Packet is too big to be relayed. Oops...\n"); + exit(EXIT_FAILURE); + } // 4. Append redundancy if needed if (app_ctx->ap.redundant_data == 1) { - min_pkt = rr->prev_packet.ap.fmt.content.clear.id; - append_buffer(&bp->ip.ap, bp->ap_count, &rr->prev_packet.ap); // We append previous packet + size_t current_size = get_full_size (bp); + size_t final_size = current_size + rr->prev_packet.ap.fmt.headers.size; + if (final_size <= max_size) { + min_pkt = rr->prev_packet.ap.fmt.content.clear.id; + append_buffer(&bp->ip.ap, bp->ap_count, &rr->prev_packet.ap); // We append previous packet + bp->ap_count++; + } else if (ctx->verbose) { + fprintf(stderr, " [algo/rr] Can't append redundancy (current=%ld, after=%ld, max=%ld)\n", current_size, final_size, max_size); + } + append_buffer(&rr->prev_packet.ap, 0, &bp->ip.ap); // We store current packet for next time - bp->ap_count++; } // 5. Append health packet diff --git a/src/packet.h b/src/packet.h index 1aae2d2..6f161e2 100644 --- a/src/packet.h +++ b/src/packet.h @@ -80,6 +80,8 @@ struct udp_target { int ref_count; }; +size_t get_full_size(struct buffer_packet* bp); + enum FD_STATE read_packet_from_tcp(int fd, struct buffer_packet* bp); enum FD_STATE write_packet_to_tcp(int fd, struct buffer_packet* bp); enum FD_STATE write_packet_to_udp(int fd, struct buffer_packet* bp, struct udp_target* udp_t);