# Q3 · Longest-prefix matching

Subject: Computer Networks

Exam: M2

Reader: https://deep-learning-study-room.vercel.app/cn/#m2/mocks/mock-c/q3

Canonical: https://deep-learning-study-room.vercel.app/read/cn/m2/mocks/mock-c--q3/

Q1(a)+Q1(b) compulsory; choose any two of Q2/Q3/Q4 · 10 marks attempted

### Question. A router has routes 0.0.0.0/0 via A, 172.16.0.0/16 via B and 172.16.8.0/21 via C. Which route is used for destination 172.16.10.45? Show the range checks. [3 practice marks]

#### Answer

## Given

| Prefix | Next hop |
| --- | --- |
| 0.0.0.0/0 | A |
| 172.16.0.0/16 | B |
| 172.16.8.0/21 | C |

Destination=172.16.10.45.

## To find

Most specific matching next hop.

## Formula and why

Choose the largest matching prefix length; an entry that does not contain the destination is ineligible.

## Step-by-step solution

1. /0 matches all destinations.
2. /16 covers 172.16.0.0–172.16.255.255, so it matches.
3. /21 mask is 255.255.248.0; third-octet step=8. The block is 172.16.8.0–172.16.15.255, containing third octet 10.
4. Of /0, /16 and /21, /21 is longest.

**Final answer:** next hop C via 172.16.8.0/21.

#### Practical example

The longest match gives the more specific destination region.
