So I know that a socket address is the combination of an IP address and a port number, but which IP address is used for the socket address? Is it the private or the public IP address, or can it be both but you have to choose one?
A connected socket can have any IP which is local on the machine - but only one at a time. A socket which is not connected (i.e. listening socket in case of TCP) can instead also be bound the catch-all IP (0.0.0.0 for IPv4, :: for IPv6) and thus receive data on all local IP.
Running socket.gethostbyname(socket.gethostname()) will give you the IP address it is using.
Related
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?
Can I bind a TCP socket to a local IPV6 address, then connect it to peer IPV4 address? It seems impossible since an ip packet cannot include ipv4 (destination) address and ipv6 (source) address at the same time.
Can I bind a TCP socket to a local IPV6 address, then connect it to peer IPV4 address?
No, the IP protocol must match for the connection to succeed. The connect call will simply fail, without even sending out any packets.
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.
I don't understand what it means to bind a socket to any address other than 127.0.0.1 (or ::1, etc.).
Am I not -- by definition -- binding the socket to a port on my own machine.. which is localhost?
What sense does it make to bind or listen to another machine or IP address's port?
Conceptually, it just doesn't make sense to me!
(This has proven surprisingly hard to Google... possibly because I'm not Googling the right terms.)
Binding of a socket is done to address and port in order to receive data on this socket (most cases) or to use this address/port as the source of the data when sending data (for example used with data connections in FTP server).
Usually there are several interfaces on a specific machine, i.e. the pseudo-interface loopback where the machine can reach itself, ethernet, WLAN, VPN... . Each of these interfaces can have multiple IP addresses assigned. For example, loopback usually has 127.0.0.1 and with IPv6 also ::1, but you can assign others too. Ethernet or WLAN have the IP addresses on the local network, i.e. 172.16.0.34 or whatever.
If you bind a socket for receiving data to a specific address you can only receive data sent to this specific IP address. For example, if you bind to 127.0.0.1 you will be able to receive data from your own system but not from some other system on the local network, because they cannot send data to your 127.0.0.1: for one any data to 127.0.0.1 will be sent to their own 127.0.0.1 and second your 127.0.0.1 is an address on your internal loopback interface which is not reachable from outside.
You can also bind a socket to a catch-all address like 0.0.0.0 (Ipv4) and :: (Ipv6). In this case it is not bound to a specific IP address but will be able to receive data send to any IP address of the machine.
When i use the INADDR_ANY to specify the IP address to bind to socket, which later listens on a port, like so:
sockaddr_in sockAddr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(80);
sockAddr.sin_addr.S_un.S_addr = INADDR_ANY; // use default
Will this socket structure allow external, internal, and loopback IP addresses to connect to to me?
I don't want to limit IP addresses that can communicate with me by putting my computer external address(ie: 122.215.214.3) in place of INADDR_ANY because then internal IP's can't connect.
I'm wondering if INADDR_ANY will bind with all 3 of my computer's IP addresses(external,internal,loopback).
When binding a listening socket, INADDR_ANY allows inbound connections on any local IPv4 address that directly belongs to the machine that the listening socket is running on, which includes loopback addresses. However, you cannot bind to an external IP address that is outside of the machine, such as the public IP of a network router. The router would have to be configured to forward incoming connections from the public IP to a private LAN IP that is assigned to the listening machine that it can bind on.