For one of my courses I am writing a Java Applet that simulates network activity. I have a host machine which keeps an ARP table of MAC to IP address, and then I have the switch keeps track of what MAC is attached to what physical port. (interface)

My question is, how should I simulate the router. From my understanding, the host will perform a computation on the outgoing IP to determine if that IP is on the network, assuming it is not, the host sends the packet to the default gateway (by looking up the MAC address of the IP address of the default gateway), in this case the router. Now the router will have at least two interfaces on two different networks.

So, my theory is that the router will look at the destination IP address and pipe the packet down the right interface that has that network on it. But then, do routers have "default gateways" where if none of those interfaces are on the destination network, can the router just pass it off to some other router?

2 Answers
2

Not exactly. Routers have routing tables. They take the destination address of a a packet and find the route to that destination. The algorithm they use is, basically, "longest match" (or "most specific match").

To implement the equivalent of a default gateway, you place a default route, that is, a route with a destination of 0.0.0.0/0. That will match every packet (since every packet is in that network), but it will always be the worst match.

The first route is the public LAN. The third route is the private LAN. Notice the two "Null0" routes to stop traffic to unused private IP addresses from going out the default route to my ISP. The second to last route is the default route.

Note that most devices that we typically think of as end systems are really routers anyway. Typical PC and server operating systems have router capabilities too, so they internally use routing tables, metrics, and longest match too.