A NAT table

A NAT router rewrites outgoing packets and uses its table to let replies back in.

What to build

A program that follows a list of packets through a NAT router.

Requirements

  • out IP PORT RIP RPORT: an inside device sends to the remote RIP:RPORT. Give the inside IP:PORT a public port (first 40000, then 40001, and so on); reuse it if the same IP:PORT was seen before. Print out IP:PORT -> PUBLIC:PUBPORT.
  • in RIP RPORT PUBPORT: a packet from the remote RIP:RPORT arrives for a public port. If that public port exists and this mapping has already sent to RIP:RPORT, print in to IP:PORT; otherwise print in dropped.

Input

Line 1: the public IP of the router. Line 2: the number of packets n. Then n lines, each an out or in packet.

Output

One line per packet.

Constraints

1 ≤ n ≤ 100.

Example

Input:

203.0.113.5
5
out 192.168.1.10 5000 93.184.216.34 80
out 192.168.1.11 5000 93.184.216.34 80
out 192.168.1.10 5000 93.184.216.34 80
in 93.184.216.34 80 40001
in 93.184.216.34 80 40009

Output:

out 192.168.1.10:5000 -> 203.0.113.5:40000
out 192.168.1.11:5000 -> 203.0.113.5:40001
out 192.168.1.10:5000 -> 203.0.113.5:40000
in to 192.168.1.11:5000
in dropped

Why it works

The table remembers which inside device and port each public port belongs to. Checking that the remote address was contacted first is how NAT drops unsolicited packets that merely guess a public port.

Examples

Example 1 · Input
203.0.113.5
5
out 192.168.1.10 5000 93.184.216.34 80
out 192.168.1.11 5000 93.184.216.34 80
out 192.168.1.10 5000 93.184.216.34 80
in 93.184.216.34 80 40001
in 93.184.216.34 80 40009
Expected output
out 192.168.1.10:5000 -> 203.0.113.5:40000
out 192.168.1.11:5000 -> 203.0.113.5:40001
out 192.168.1.10:5000 -> 203.0.113.5:40000
in to 192.168.1.11:5000
in dropped
Example 2 · Input
198.51.100.7
2
in 1.2.3.4 80 40000
out 10.0.0.2 1234 8.8.8.8 53
Expected output
in dropped
out 10.0.0.2:1234 -> 198.51.100.7:40000

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Use a dict from (private ip, private port) to the public port, and another from public port to (private ip, private port, set of remote addresses).

Hint 2

Public ports start at 40000 and go up by one for each new inside address and port pair.

Hint 3

A reply is accepted only when the remote address and port are one this mapping has sent to; otherwise print dropped.

Solution

Show one correct solution (try it yourself first)
public = input().strip()
n = int(input())
mapping = {}
reverse = {}
next_port = 40000
for _ in range(n):
    p = input().split()
    if p[0] == "out":
        key = (p[1], p[2])
        if key not in mapping:
            mapping[key] = next_port
            reverse[next_port] = (key, set())
            next_port += 1
        reverse[mapping[key]][1].add((p[3], p[4]))
        print("out " + p[1] + ":" + p[2] + " -> " + public + ":" + str(mapping[key]))
    else:
        port = int(p[3])
        if port in reverse and (p[1], p[2]) in reverse[port][1]:
            key = reverse[port][0]
            print("in to " + key[0] + ":" + key[1])
        else:
            print("in dropped")

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: DHCP and NAT: getting an address and sharing one →