diff --git a/ethertype-dump.c b/ethertype-dump.c index 95063d5..0b39571 100644 --- a/ethertype-dump.c +++ b/ethertype-dump.c @@ -80,7 +80,7 @@ int main(int argc, char **argv) { } for(;;) { - unsigned char frame[1522]; // 1522 is the largest a 802.3 frame can be + unsigned char frame[1518]; // 1518 is the largest a 802.3 frame can be without FCS (which we apparently don't get) if (fflush(stdout) == -1) { err(1, "fflush"); @@ -91,10 +91,12 @@ int main(int argc, char **argv) { if (frame_length == -1) { err(1, "recv"); } + size_t frame_data_length = (size_t)frame_length < sizeof(frame) ? (size_t)frame_length : sizeof(frame); + frame_length += 4; // Include the FCS in the true length - if (frame_length < 64) { + if (frame_data_length < 14) { errno = 0; - if (printf("Runt frame (%zdB)\n", frame_length) == -1) { + if (printf("Frame too short to contain header (%zdB)\n", frame_length) == -1) { err(1, "printf"); } continue; @@ -143,10 +145,14 @@ int main(int argc, char **argv) { } errno = 0; - if (frame_length > 1522) { + if ((size_t)frame_length > sizeof(frame) + 4) { // +4 for the FCS if (printf(" (overlong)\n") == -1) { err(1, "printf"); } + } else if(frame_length < 64) { + if (printf(" (runt)\n") == -1) { + err(1, "printf"); + } } else { if (printf("\n") == -1) { err(1, "printf");