How sockets determine the requesting machine - sockets

how does a socket finds my private ip address as public ip are different than private and inside a public network there can be many private ip addresses with listening to a particular port

In order for a remote machine to send packets to your computer, the remote machine must know it's "public" (from the view of the remote machine) IP address.
Most routers these days have only a single IP address assigned to them from the upstream service and so do Network Address Translation (NAT) for those machines behind it.
Every IP packet outbound from your machine to an outside server has it's address changed to the public address of the router and the router keeps an internal record with the destination address & port along with the true source that sent it. Every inbound packet has the originating address & port checked against those records to find out what internal machine should receive it; the destination address is set appropriately and the packet forwarded.
Neither side can generally tell (or care) that NAT is happening.
Note, however, that you can't initiate a connection from the outside to an internal machine because there is no record indicating where those initial packets should be sent; additional static configuration would be necessary.

Related

Sending a UDP datagram to a shared IP address and port

Suppose that two computers use the same Wi-Fi to access the Internet. Each of these computers has the same program installed, which is bound to the same UDP port. I want to know, since both computers have the same external IP address and listen to the same port but on different machines, what will be the result if a UDP datagram is sent to this common external address and to a common port, then which machine will receive it and how to send it each machine its own personal datagram?
The router will not forward the packet to either computer, since it doesn't know which one it should forward to.
In fact, even if the program was only running on one computer, the router still wouldn't forward the packet. It has to see outbound traffic going from the computer to the outside world first, before it decides which external port to use for forwarding inbound traffic back to the computer. And the router might not decide to use the same port on the public IP that the computer used on the private IP.
This is why everyone hates NAT and likes IPv6.

distinguish client connections at server from different subnet

I have a socket server listening on specific port in one subnet. Client are present in different subnet.
Each client can make 2 or 3 connections on the same port.
From second parameter(struct sockaddr) of accept API, I can get the source IP address, but that address is translated by Gateway/Router. and I get same IP address for all client connections.
I need to segregate connections from each client but Checking IP address or Mac address give me same thing for all the connections irrespective of client1 and client2 have different IP address.
Any way to handle such thing?

Why we use the local IP address in identifying sockets?

When a server want to create a socket, it will use a combination of its IP address and some well-known port, let us say 80. So, when a packet arrived, both the server IP and port 80 will be used to decide whether the packet goes to that socket or not.
The question is why do we need to check the IP address of the server, since the packet (aka datagram) passed the network layer check and was certainly destined for this server. In other words, the network layer will not pass the packet to transport layer if the destination IP is not the server IP, so why do we use the IP address in the socket?
And if a host (a client or a server) created multiple sockets (network processes) using both its IP and some port numbers, is there any case where the IP could be different in these sockets?
Thanks in advance!
Why do we need to check the IP address of the server, since the packet (aka datagram) passed the network layer?
The Data Link Layer uses Media Access Control (MAC) addresses to direct packets. When a packet arrives at your computer operating system (OS), it arrived either because the MAC address matched the hardware address or it was a broadcast (ff:ff:ff:ff:ff:ff).
Once the packet is received, your OS determines if it is destined for an IP address assigned to the computer. At this point, the OS has several options:
If the IP address matches an assigned IP, deliver to any waiting applications or reject the packet and handle any needed Internet Control Message Protocol (ICMP) required.
Should the IP not match an assigned, your OS checks if IP routing is enabled. Then either rejects the packet issuing any required reply or forwards the packet to the destination IP in the routing table by creating a new packet targeting the MAC address of the destination router.
If a host (a client or a server) created multiple sockets (network processes) using both its IP and some port numbers, is there any case where the IP could be different in these sockets?
If your OS assigns more than one IP address to an interface, all of those IP addresses would be available to be used. You can open sockets using any available IP (usually INADDR_ANY or similar). In a listening context, your port will be available to every IP address assigned. In a transmitting context, your IP will be set depending on the outbound interface.

How Can I ping or create a scoket connection with my friend?

When we ping to any Public IP of a router it give a reply,
we dont get reply, if we ping a local computer with its private IP of a particular network,
So is there a way so I can make direct connection or socket with a particular PC in a network.
I have heard that, I have to make connection with public IP of a particular network and the port number will decide, on which PC in that network , my request will go.
But this method is not working..
If the computer is behind a router that uses Network Address Translation (NAT) - that is, the computer has a private IP that is not visible to the internet - then you cannot ping or establish a connection to the computer from the outside. This is because the router does not know which computer you want since all the computers behind the router share the same public IP address.
The way to establish a connection in this case is to have the computer which is behind the router establish the connection to the outside computer. For example, when you go to a web site, you can do it from a computer behind a router since your computer is initiating the connection. However, the web server you are going to must have a public IP.
When a computer behind the router initiates a connection, the router does select a port number to associate with that connection so packets from the outside for that connection will go to the right computer. However, this only works for traffic on that particular connection; the port number is not a general purpose mapping to that computer.
Two computers behind the same router can establish connections with each other using the private IPs as they don't have to go through the router to do it.
You can decide with your friend who is going to be the client (the one initiation the TCP connection) and who is the server (the one receiving the TCP connection) and which port should be used (let's say X). Let's say that your friend is the server. Then he/she has to configure his/her router to redirect all the traffic to the router to port X to his/her private IP to port X (this is what Warren mentioned as NAT).
If you use a port above 1024, the server (running on your friend's PC) doesn't need root/administrator privileges.
Your program (the client) would then connect to the public IP address of your friend. He/she can check his/her public IP with: What is my IP

UDP SOCKET how to answer to a specific computer in the same network?

Hello I am writing a server application with multiple clients.
For this i first authenticate the clients and the SOCKADDR_IN of the connected ones gets saved in an array.
Then i use the following to answer a specific client:
if(myArray[i].sin_addr.s_addr == current.sin_addr.s_s_addr)
This works because I am getting a LAN IP at the moment. Later when the server will not be in the same network and two clients from the same network connect I will get the same IP twice.
At least when i check the IP of mine and my brothers computer over one of the many sites that show the IP i see the same.
So how can i differentiate two computers in the same network from a remote server?
To do what you are asking, each client would have to explicitly send to your server the local IP it is using to send packets to your server, then your server can store that extra IP along with the IP it receives on inbound packets from that client. If the two IPs are different than you will know that the client is behind a router/NAT, and you will have to use both stored IPs as a pair to uniquely identify individual clients from the same network. Your communication protocol will need a small handshake to establish that information. For example:
C -> S: HELLO I want to talk to you
S: detects client's public IP:Port to send packets to
S -> C: WELCOME who are you
C: detects local IP that OS is using to route packets to/from the server
C -> S: IAMSAM here is my local IP
S: associates both public and private IPs with the client, allows further communication
If a client has the same public and private IPs then it is directly connected to the Internet.
If the public and private IPs are different than the client is behind a NAT:
If multiple clients are behind the same NAT then they will have the same public IP but different private IPs.
Clients on different NATs will have different public IPs but can have the same private IP.