If I know the public IP address of a network, I can talk to the router for that network.. But how would I specify to the router which device in the network I want to start talking to? What I'm asking is pretty much this: How does a router know which device to send a received packet to, if the sending device hasn't setup a connection with the receiving device..

I understand that in the IP header of a packet resides the destination address, that is the public IP address. And when this packet is sent, it arrives at different networking devices on its journey, asking each if they contain the destination address, and if not the packet hops to another device until it finds the destination address. Once it reaches the device with the public IP address, it is forwarded to the router. Say this packet was meant for on a device behind that router and say a connection hasn't been established prior to sending this packet, how does the router know to send the packet to How and at which point is this specified?


1 Answer 1


During the construction of a packet, your computer must first resolve any hostname used (eg, google.com) using the Domain Name System. Your computer will then have the destination IP address and you know know where to send the packet to, but do not know the route.

In order to determine this information, Every router maintains routing tables and one or more protocols are then used in order to determine the path of the packet. Typically, for small offices and homes, the protocol used is RIPv2. For the most simple setups, there is only one route - the default route which will route traffic through the default gateway of your firewall/router. At this point, you then reach an ISP router/cable modem. These will typically forward you via similar methods to a large router which use the BGP protocol. This is a much more complicated routing method in which large tables of routes are maintained which direct large public subnets assigned by companies like ARIN to designated routers. In the BGP protocol, these routes are updated by peers and expire and are refreshed periodically. For routing in between small and large scales (eg, within a large office or campus or an ISP, alternate protocols such as EIGRP or OSPF may be used

When the packet leaves your router, the source address is changed to be your router's public IP address. Thus when the packet reaches it's destination, the receiving server simply responds back to your router's address. It is then up to your router to track and maintain stateful connections - that is which source IPaddress:port and destination IPaddress:port combinations were communicating with each other. The creation, lifespan and teardown of these connections are maintained by your router based on it's firewall rules, NAT policies and other firewall settings.

On a lower level, network devices maintain ARP tables of which IP addresses should be sent to which MAC address connected to the device which maintains the table. So for example, with a home router, it knows to send the packet to the MAC address of your workstation or laptop and for all other traffic, it knows to send the packet to your cable modem or ISP router. To look up an address that has never before been seen, an ARP probe is broadcast and used to populate the table. This only tells each device it's next hop and ARP has no visibility to the next device (typically). (Eg, your workstation, will not know the MAC address of your Cable Modem/ISP Router. Only your router/WAP would.

  • Okay so what if I wanted to telnet to a device in a different network that my router or machine has never encountered before, how would that connection be initiated? My packets know how to get to the router providing I know the public IP, but say I wanted to telnet to in a different network, how would the router at the dest know that my packets are meant for, considering that 1) my device or router has never encountered the receiving device or their router and 2) multiple devices in the receiving network have an open telnet port..
    – l30n1d45
    Commented Mar 24, 2015 at 6:11
  • In this case, is a reserved subnet for private networks so this will never be a public IP address. In that case, only RIPv2 would (typically) be used. Under this protocol, you would need to create a static route with the other router as the gateway address for the or subnet (whichever you are using). Likewise, the other router would need a matching static route for the return traffic for your subnet and your router as the gateway address. Commented Mar 24, 2015 at 6:17
  • If you do not create a static route, this traffic will be unroutable and lost in the ether. Commented Mar 24, 2015 at 6:17
  • I know that they addresses are reserved, but my question is when my data reaches their router via the public ip address, how does the router know that I want to telnet to in its network?
    – l30n1d45
    Commented Mar 24, 2015 at 6:20
  • Ah, right. That's the NAT policy/firewall rule bit. You have to define those explicitly. Typically, you would do Port Address Translation (PAT; a NAT subtype) which is an explicitly defined policy to forward traffic for the public IP on port 23 (the telnet port) to the address. Commented Mar 24, 2015 at 6:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.