Follow

i feel like this is a dumb question but: how do routers work? how does a message to a remote server end up getting sent to a device behind a router? im guessing it is some IP field but i am not sure what

· · Web · 4 · 1 · 4

@dogstar@merveilles.town if u mean a consumer "router", then the answer is network address translation, or NATalie for short

router keeps track of all the connection sources and destinations and rewrites appropriately

@natalie @dogstar (and for incoming connections unless you're doing holepunching, which is another mess entirely, the "ip field" that's used by NAT to select the device to address-translate to, on the local network, is the port)

@natalie right, i mean the thing that people tend to have in an apartment

huh this looks hackier than i expected

@dogstar@merveilles.town yeah it's not elegant. at least things are a bit better with ipv6

long 

@dogstar there are various lengths of answer to this

the short answer is there are two routing modes (i will focus on IPv4 [+ CIDR], IPv6 is a whole other thing with more complexity). if you are sending a message to a computer on the same LAN as you, then you look up the computer's hardware address by sending out ARP requests to the LAN, and asking who has the IP address you want to send to. hopefully, the computer you are sending to replies, and tells you its hardware address. then, you can construct a layer-2 frame with your packet to that computer and send it on the LAN
if the computer is _not_ on the same LAN, you send it to your idea of the next closest computer to the target LAN, and then that computer forwards to the next closest, and so on, until hopefully it reaches the target LAN at which point it can get sent to the destination

long 

@haskal thank you! i appreciate the context, i was meaning outside of the lan but it is interesting to hear the internal case too

long 

@dogstar for you, the next closest computer to anything is your ISP, but for datacenters there may be multiple links to many neighboring servers, and it requires some graph algorithms to figure out what is the shortest path somewhere. computers on the internet use a protocol called BGP to exchange information about what segments they are connected to, and these are forwarded to other computers, adding on each computer it passes through, so that everyone ultimately receives a set of paths corresponding to the optimal packet routing. this goes in a routing table, and IP addresses are checked against the table using a few parameters, but mainly the longest prefix match of the bits of the IP address. so the target LAN of some IP address like a.b.c.d could be a.b.c.0/24, and the local BGP node at that location would advertise that it can route a.b.c.0/24 to its destination, and then a BGP node connected to that with access to more subnets might say it can route a.b.0.0/16, and so on

long 

@dogstar so when you send off a packet, you'll likely be sending it to some catch-all node that can route a wide range of addresses, which will send it to some node that can route more specific addresses, and so on, as you get closer to matching the whole IP address you get physically closer to the actual destination (hopefully)
BGP also has problems... it's unauthenticated, which allows servers to lie about what they can route and MITM lots of traffic, which has happened a few times in the past

@dogstar that's what I thought, but these days my only theory left standing is demonic conjuration

Sign in to participate in the conversation
Merveilles

Merveilles is a community project aimed at the establishment of new ways of speaking, seeing and organizing information — A culture that seeks augmentation through the arts of engineering and design. A warm welcome to any like-minded people who feel these ideals resonate with them.