Winsock bind address "INADDR_ANY" clarification - sockets

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.

Related

Port forwarding from public ip to local ip in other way

So... if i want my tcp server can be connected by other network(not just local network), i will need to setup port forwarding in my router setting page. But it's so annoying so i'm wondering is there other way to port forwarding from public IP to my local IP address.. maybe with code?

Can TCP socket bind to IPV6 address and connect to IPV4 peer?

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.

IP Address for a socket address

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.

Using public IP address in sockets

I am new to socket programming. My sockets work with localhost or local ip. But why can't I create a socket server with my public ip? When I try to do this in python, it complains "cannot assign requested address".
import socket
s=socket.socket()
host="address"
port=5000
s.bind((host, port))
s.listen(5)
while True:
c, addr=s.accept()
result=c.recv(1024)
print(result.decode('utf-8'))
c.send("""
test
""".encode('utf-8'))
c.close()
My IP I got from this website:http://whatismyip.host/
A service like http://whatismyip.host/ will show you the public address of your router, that is the machine that is connected to the internet. If you look up the IP of your desktop machine, it will probably show something like 192.168.0.10, with 192.168.0.1 being the address of your router that you enter in your desktop, as "gateway".
Your router resembles a firewall, separating your home network from the public internet.
In order to receive connections on your desktop machine, you would need to configure port forwarding on your router.
You will still have your python process listen on 192.168.0.10 (your local machine's IP). Or you could set it to 0.0.0.0 which is a shorthand for "all available interfaces".
Be aware that your public IP address is probably not stable.

What does it mean to bind() a socket to any address other than localhost?

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.