Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
Suppose a host machine A in LAN has address like this 192.168.0.123. It connects to the Internet through a default gateway 192.168.0.1.
When A sends data to an external machine B, how does the gateway handle the IP packet?
When B's response reaches the gateway, how does the gateway locate A as the intended receiver?
ADD 1
Seems communication between A and B can only be initiated by A. Because external machine doesn't know about A hidden in the LAN.
Suppose there's another machine C who sits in another LAN. A and C wants to communicate to each other. I guess the only way to do that is to have some public machine like B to act as a middle-man. Maybe that's why all chat programs like OICQ have to log onto a server first, such as B. And all chat messages are forwarded by that central server. And I think unless the A-B or B-C connection are persistent, the forwarding can only be implemented as pulling by A or C, rather than pushing by B, because only A and C can initiate request to B.
Well - a short answer - through something called Network Address Translation
A slightly longish answer -
So here's what happens # A - Route lookup to get Next Hop (default gw in this case). ARP resolution to find out MAC address of Default gateway) and then Network Address Translation # Gateway - before packet goes out into wilderness.
Route Lookup -
Every well configured machine should have a local routing table - which would normally contain two routes. One is default route and another is called the subnet route. There can be more than these two routes, but definitely these two routes should help. the 'subnet route' helps in reaching the nodes on the subnet. and the 'default route' helps to reach anyone else. So eg. in your case there'd be two routes (0.0.0.0/32 gw = 192.168.0.1) the 'subnet route need not have gateway - in fact will not have gw). In this case since B's address is not on the subnet default route and gw corresponding to it will be used.
ARP resolution
A broadcast ARP request message is sent by A to ask for Gateway's MAC address - which it wants to use before forwarding the packet. Since Gateway knows it's own IP and MAC, it replies with it's own MAC address.
NATing -
So when a browser (say) on Node A wants to connect to a machine on Internet Node B (on a web server). It will send a TCP packet with following information - DIP = IP of B/ SIP = IP of A/ DP = 80/SP = 10000 (randomly chosen by A)/Proto = TCP.
Now when this packet arrives on Node B. Node B can forward this packet as it is - if it was simply working as a gateway - without NAT functionality. Technically that packet can go out and reach B, but there's no way for B's reply to reach back to 'A'. 'cos 192.168.X.X are private IP addresses - those packets routers are not supposed to forward them. So packets going out of A should have an IP address that can be reached by Host B. Gateway usually has a 'global' IP address. This address is used in all outbound packets. So packets on return path could reach to Gateway and subsequently to 'A'. So the IP address part is taken care of. The port part needs to be dealt with as well. Technically B could simply re-use the Source Port in outgoing IP packets, but imagine another node (say C) using a same port going to same server, so gateway would be clueless about whom to send packets to - on return path. So typically Gateway uses different outgoing ports and keeps a mapping between Inside IP-Port and outside IP-Port (technically that means Gateway can only reliably forward packets for 64 K connections). Normally this is not a problem, but for a large number of nodes on LAN with persistent TCP connections, this could be a problem. So typically multiple IP addresses are used - effectively multiplying the connections supported.
Hope that was not too long!
This is called NAT: Network address translation. The router strips off the source address of outgoing packets and replaces it with its own public IP address then forwards the packet. Onthe way back the router receives the packet and replaces the destination address with the internal IP address.
Related
I'm currently learning about ARP and L2/L3 networking - would someone be able to help out? If I use an ethernet cable to connect my laptop (A) to another laptop (B), it could use ARP to find out B's MAC address based on B's IP address. Then, any ethernet frames could be sent and accepted by B.
But how does A know B's IP address in the first place? What if there is a switch (L2 device) in between A and B? Does this change the answer? What if there was a router in between (an L3 device)?
Usually, a switch/Router will assign an IP.
The question is unclear -
find out B's MAC address based on B's IP address.
B's MAC address is not based on B's IP.
A MAC is associated to the hardware (but somewhat assignable/cloneable) where as an IP is assigned. Either by DHCP or statically by an admin.
Effectively, you need a switch/router to make what you're defining work.
EDIT:
you can connect two computers direct, but you still need to give each a static IP address so they can find each other - this would likely not be ona network so the IP sub-network doesn't matter.
It still has very little to do with MAC.
If two PC's hand-shake, they'll see each others MAC.
It sends out a broadcast packet to the broadcast MAC address asking "who has the IP address x.x.x.x" which goes to all computers on that broadcast domain. If a computer sees that packet that owns the IP address "x.x.x.x" it will send a unicast reply back to the MAC address of the original requestor with its MAC address which will then be cached for further communication.
It doesn't know the IP unless you tell it. You say "the default gateway is 192.168.1.1" and it will start trying to talk to 192.168.1.1 etc.
If there is a router in between, all traffic to the remote device would go to the MAC of the router and your computer wouldn't be doing any ARP lookups other than that router.
Switches wouldn't matter for this, all they do is make the broadcast domain bigger. With a cable the broadcast domain is just you and the remote device. With a switch, it expands that to all devices connected to the same switch (or VLAN in the switch)
But how does A know B's IP address in the first place?
It doesn't know. At least there's no general method to find out.
The IP address of any resource needs to be supplied by "something else". You need to either provide the address yourself, resolve it from a (also provided) DNS name, read it from a file, ... Alternatively, you need to provide some kind of auto discovery between the nodes (broadcast, multicast, LLDP, ...) or provide a commonly known discovery service, registration server or similar.
What if there is a switch (L2 device) in between A and B?
That changes nothing but excludes link-level discovery (unless it's a "dumb" switch that simply forwards those frames).
What if there was a router in between (an L3 device)?
That eliminates broadcast and multicast from the picture. (Multicast could be routed but that is unlikely for discovery and I won't dive into that.)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
This questions is hypothetical, this situation would most likely never happen nor could any router handle it
Situation 1
Lets say for example you (somehow) managed to put 255 devices onto your network. Lets refer to the 255th device as D255 and, just for later reference, the router's IP address is 192.168.0.1. If I am correct D255's local IP address should be 192.168.1.255. So what if we buy a new device (D256) and connect it to my home internet along with the other 255 devices. What would D256's local IP address become? I thought about it and I assume that it would be 192.168.2.1. Am I correct?
Situation 2
After thinking about situation 1 I came up with another situation. What if you had 65025 (255 * 255 = 65025, if you see where I'm going with this.) devices connected to the your internet? The last device's (I will refer to as D65025) local IP adress should be 192.168.255.255 (That is assuming that situation1's solution is correct.). So what if I go out and buy a another device (D65026) what would it's local IP address be? It can't become 192.168.256.1 because the numbers can not exceed 255 and it can't become 192.169.1.1 because 192.168.x.x is the local IP reserve and 192.169.1.1 exceeds the local IP limit and (correct me if I'm wrong) would be a external IP address. So what would happen?
I appreciate your feedback!
A local network will typically be configured with a DHCP server to hand out IPv4 address leases along with name server addresses and a gateway address.
Residential and small commercial routers are usually configured to do Network Address Translation and have a DHCP server configured to hand out Private IPv4 addresses and the router's private IP as the gateway and nameserver. The size of the local subnet is determined by the router's configuration.
A typical configuration is 192.168.0.0/24 which provides 254 host addresses.
The DHCP server has configuration that specifies the address pool it can hand out leases for, how a DHCP server behaves if it is asked for a lease when all addresses in the pool are currently assigned depends on the configuration, but in most situations it will just not respond as there are "no free leases".
In this situation a host will likely select a link-local zero-configuration address from 169.254.0.0/16 and not have any Internet access or any other hosts on the network except those with Zero Conf addresses. It may retry DHCP at a later point.
The RFC1918 private address block 192.168.0.0/16 actually has capacity for 65534 uniquely addressed hosts. Ignoring the issues of having a layer 2 broadcast domain of this size, the same constraints apply: when there are no leases left to assign, none are assigned.
If these private addresses are to have Internet access they will be Network Address Translated to one or more public IP addresses (usually one) by the router. One public address is unlikely to be sufficient for a large number of private network hosts, constrained by the availability of local TCP / UDP port numbers.
I suggest you read up on IP networking to fill in basic knowledge.
If you are using DHCP, when you run out of addresses, it will kick one of the other devices off the network, probably the one which connected first. There are only so many devices which can connect to a network, but that number depends on the router configuration.
That's essentially my question. Isn't the network portion in a computer's IP address so that, when it is sent, other computers can look at that network portion and know where to send it back to? So why do routers have their own IP address?
The router needs to be a node on the same network as the computer using it. When your PC tries to communicate with a system on a different network, it consults it's routing table to figure out which router (there can be several) has the route to the destination. Without an IP on the router, there would be no way to send packets to the router, and thus no way to get out of your network.
I suppose the IP protocol could have been designed to use broadcasts to find the route out, but that would have caused issues with traffic congestion. Thankfully it wasn't designed like that.
I recently found that packets are encapsulated within ethernet frames. Packets use IP addresses, frames use MAC addresses.
Why aren't IP addresses used in ethernet frames for routing? I understand that when trying to access a basic website, the computer goes to a DNS to find the IP address relevant to the user-entered domain name. How do computers find the correct MAC address?
Really, how are MAC addresses used in routing internet traffic?
Thanks
IP packets aren't always encapsulated in Ethernet frames. There are other physical media such as ISDN, etc. When packets are routed, IP addresses are used to determine the next hop and the physical address is used to physically identify the interface serving as the next hop. Only the former (determining next-hop) is usually called routing.
To answer your second part, MAC addresses are discovered through ARP (Address Resolution Protocol) in IPv4 & ND6 (Neighbor Discovery) in IPv6.
Update:
The destination IP address in the IP header is the final destination. In the process of routing (at each hop), you get the next hop's IP address to (eventually) reach the final destination from the routing table (this could be a default gateway's IP address). To send the packet to the next hop, you need its MAC address. While hopping through intermediate links, the IP address in the IP header don't change - only the MAC addresses change.
Bit late but still here is my answer :) ...
To send data you need two address, the MAC address and the IP address.
Basically the sending host will ARP for a MAC address, this occurs when the local host doesn't know the MAC address of the host it has an IP address for or it will ARP for the default gateway MAC address (if it doesn't already know it) if the IP address in on a different subnet/ network. Once it obtains a MAC address the IP packet is encapsulated in a L2 frame and sent across the media. If the IP packet is meant for a host on a different subnet/ network, it will be sent to the default gateway, this router will de-encapsulate the L2 frame (remove and discard it) check the IP address and will forward it. For the router to do this it needs a MAC address to send it over the media, It will look up the next hop in it's routing table, encapsulate the IP packet with the same source and destination IP address that was sent from the original host into a new L2 frame. This time the MAC address for the source address will be that of the forwarding interface of the router, and the receiving interface of the next hop will be the destination MAC address. This will continue from hop to hop until it reaches the final host, each time the MAC addresses will change, but the original IP address will remain the same.
Here's the key point -- there can be more types of packets than INTERNET traffic. You could be using IPX, which is non-routable. How do clients identify each other? By the MAC address.
Routing != Addressing, which is really where the MAC comes into play.
In order to be routed, the OSI model adds a layer to allow for path discovery to the next gateway. This layer is responsible for routing, but knows nothing about the MAC address.
As a side note, at the hardware level, MAC addresses ARE used by switches, but not for routing. From How Stuff Works:
The switch gets the first packet of data from Node A. It reads the MAC
address and saves it to the lookup table for Segment A. The switch now
knows where to find Node A anytime a packet is addressed to it. This
process is called learning.
In this way, a switch can make sure that traffic is only outputted to the correct port. This isn't accomplishing routing so much as reducing network congestion. Only broadcasts and traffic destined specifically for that MAC address should be sent out the port.
Recently I have been thinking about the same and came upon this question. Here is my answer to this question. Actually MAC address is needed for correctly sending the packet to right destination. This is specially true when packet is needed to sent over a VLAN. There can be multiple switches/routes connected on that VLAN over multiple physical interfaces. However IP Routing is unaware of these physical interface. It only knows about the logical connectivity. For example, route 10.10.10.0/24 is reachable via VE/VIF0.10(logical VLAN interface) and/or nexthop neighbor is 20.20.20.1. There could be multiple interfaces under VLAN 10. Then to which interface packet is sent out? This is where ARP comes in the picture. ARP helps to discover the MAC address associated with the next-hop IP address. When switch/router learns the nexthop MAC. along with that it learns the physical interface also via which that MAC is reachable. Hence while routing packet, firstly MAC corresponding to the destination IP is searched and then the physical interface associated with that MAC is searched. Finally packet is sent out via that physical interface. The MAC corresponding to that destination IP is used as destination MAC. In absence of this, routed packets will always be flooded in the outgoing VLAN.
Hope this helps.
Thanks.
Answer: MAC addresses are not used in the process of routing of a packet.
segment -> transport layer (TCP ports)
packets -> network layer (IP addresses)
frame -> data link layer (MAC addresses)
bits -> physical layer (electric/optical signals)
Create your own packet/segment visit http://wirefloss.com/wireit/
There are 2 models (TCP/IP and ISO/OSI)
In detail:
Your app has some data. This is encapsulated by mentioned layers. Encapsulation means that a header with fields is added at each layer. If your data never leave the local network the MAC address will be the same. Once your data needs to be delivered outside your network the frame header is stripped by router and is replaced by router fields.
UPDATE 2021: Some people seems never heard of ISO OSI model and put this answer as incorrect.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 years ago.
Improve this question
(1) What is my and others real IP address on same lan? Can we still have a unique IP despite the fact that we share the same router/LAN? and how does the IPs are assigned?
Thanks!
If you use a router to access the internet you probably have a DHCP Server running on the router as well.
This gives each computer a unique IP address wit a lease of x-days.
From the net only your public (mostly dynamic) ip address is shown to others.
Inside you private local net you have private ip addresses like 192.168.x.x/class C.
Easy way to find out your ip addess on your local computer:
open a cmd shell and type "ipconfig /all".
Then you see your IP address and also the gateway (which is your router).
Within a single LAN (which I'm defining here as a section of the net with no network address translation (NAT)), your IP address should be unique.
That's so that traffic can get to the correct machine.
However, it's never that simple. There's a good chance that the IP address the world sees you at is nothing like your local IP address. Your local address is probably 10.x.x.x or 192.x.x.x (from memory), one of a group of private IP ranges. For example, my Ubuntu box here is 10.1.1.2, an address that probably half a million other people on the planet have.
But my packets don't hit the internet with that source address since they go through a NAT router on my desk as well as a large number of other NAT'ting devices in my ISP.
It's this NAT process that allows all those private IP ranges to be usable.
As to how you get your IP addreses, you can set up static addresses (very useful for servers that you don't want changing) but the usual approach for non-server machines is to use a DHCP (dynamic host configuration protocol) server which manages the IP addresses and hands them out as required (on booting your client machine usually).
Typically, the DHCP server wil lock an IP address to a specific Ethernet MAC address for a period of time (in our corporate environment, it's three weeks). That means that, barring a four week holiday with your machine turned off, you should always have the same address.
Keep in mind that you may have more than one network card with separate IP addreses. You may even have a single network card with multiple IP addresses. In that case, you can be said to have more than one real IP address although I'd probably still say that it's the one servicing your default route (the source address your packets go out on by default).
It's a very ... interesting ... field to work in :-)