Lesson 13 of 15

Network Address Translation

Network Address Translation (NAT)

NAT allows multiple devices on a private network to share a single public IP address. A NAT router translates between private and public addresses.

How NAT Works

Private Network          NAT Router           Internet
                    ┌──────────────────┐
192.168.1.10:5000 ──>│ 203.0.113.1:40001│──> Server
192.168.1.11:5001 ──>│ 203.0.113.1:40002│──> Server
192.168.1.12:5000 ──>│ 203.0.113.1:40003│──> Server
                    └──────────────────┘

NAT Translation Table

The router maintains a translation table mapping internal (private IP:port) to external (public IP:port):

InternalExternalDestination
192.168.1.10:5000203.0.113.1:400018.8.8.8:53
192.168.1.11:5001203.0.113.1:400028.8.8.8:53

Types of NAT

  • Static NAT: One-to-one mapping (fixed)
  • Dynamic NAT: Pool of public IPs assigned on demand
  • PAT (Port Address Translation): Many-to-one using different ports

Your Task

Implement a NATRouter class that simulates PAT:

  • constructor(publicIP) — initialize with a public IP
  • translateOutbound(privateIP, privatePort, destIP, destPort) — creates a mapping, returns the external address
  • translateInbound(publicPort) — looks up the internal address from a public port
  • getTable() — returns all current mappings as an array
Node.js loading...
Loading...
Click "Run" to execute your code.