networking · beginner · ~15 min

Does the datagram fit?

Check a single datagram against the MTU.

Challenge

Check whether a payload fits in a single UDP datagram without fragmentation.

Task

Implement int datagram_fits(int payload, int mtu) that returns 1 if payload <= mtu (fits in one datagram), otherwise 0.

Input

  • payload: the payload size in bytes.
  • mtu: the maximum datagram size.

Output

Returns 1 if the payload fits, else 0.

Example

datagram_fits(1000, 1500)   ->   1
datagram_fits(1500, 1500)   ->   1
datagram_fits(1501, 1500)   ->   0

Edge cases

  • A payload exactly equal to mtu fits (boundary is inclusive).

Input format

Two integers: payload and mtu.

Output format

1 if payload <= mtu, else 0.

Constraints

The boundary payload == mtu counts as fitting.

Starter code

int datagram_fits(int payload, int mtu) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.