networking · beginner · ~10 min · safe pentest lab
Read a fixed-offset big-endian field with bounds checks.
Read the opcode out of an ARP frame to tell a request from a reply — the first thing an ARP-spoofing detector classifies.
Implement int arp_opcode(const uint8_t *frame, size_t n). The grader passes a static ARP frame buffer; no frames are sent.
frame: the ARP frame bytes (the harness supplies a fixed fixture).n: number of bytes available in frame.The opcode is a big-endian 16-bit value at byte offset 6, i.e. (frame[6] << 8) | frame[7].
Return 1 for an ARP request (opcode 1), 2 for an ARP reply (opcode 2). Return -1 for NULL frame, n < 8, or any other opcode value.
{0,1,8,0,6,4,0,1} -> 1 (request)
{0,1,8,0,6,4,0,2} -> 2 (reply)
{0,1,8,0,6,4,0,9} -> -1 (unknown opcode)
n = 7 -> -1 (too short)
NULL -> -1
-1.n < 8 (not enough bytes to reach the opcode) returns -1.ARP request vs reply is the first thing an ARP-spoofing detector classifies. The opcode is two bytes at a fixed offset.
frame, the ARP frame bytes; n, the number of bytes available.
1 for request, 2 for reply, -1 for NULL, n<8, or any other opcode.
Opcode is a big-endian u16 at offset 6; check n>=8 before reading.
#include <stdint.h>
#include <stddef.h>
int arp_opcode(const uint8_t *frame, size_t n) {
/* TODO */
(void)frame; (void)n;
return -1;
}
Reading the wrong offset. Forgetting the n>=8 bound.
Unknown opcode. Short buffer. NULL.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.