So I have the option to specify an address as well as a port when setting up as a UDP server.
According to the documentation, if no address is specified then the UDP server binds to 0.0.0.0:port. The documentation says the following: If address is not specified, the OS will try to listen on all addresses.
Why would I (or why would I not) specify a host to bind to? Is it for if you have multiple network cards and you only want to listen on one - so I would specify the ip of the network card I wish to listen on?
The option is for if you have multiple IP addresses on one machine, and you don't want to use all the addresses for binding. If you were to bind on 0.0.0.0 for port 80, you'd be using port 80 on all those addresses. By specifying an address you restrict binding to one address and port.
Related
Recently I found that I was able to bind Apache on 127.0.0.73 without 127.0.0.73 to be present.
Only 127.0.0.1 is present as normal.
I also spoke with a friend and he said that is "normal" on Linux and probably on Windows and not works on MacOS, but he has no idea why.
I can do following:
[nmmm#zenbook nmmm]$ curl 127.10.0.123
curl: (7) Failed to connect to 127.10.0.123 port 80: Connection refused
and it shows that whole A class network is available.
How this works?
I do not see anything special in ifconfig and ip, except lo interface has no broadcast. Is that the key point?
According to https://en.wikipedia.org/wiki/Localhost
IPv4 network standards reserve the entire address block 127.0.0.0/8 (more than 16 million addresses) for loopback purposes.2 That means any packet sent to any of those addresses is looped back. The address 127.0.0.1 is the standard address for IPv4 loopback traffic; the rest are not supported by all operating systems. However they can be used to set up multiple server applications on the host, all listening on the same port number. The IPv6 standard assigns only a single address for loopback: ::1.
Or from https://www.rfc-editor.org/rfc/rfc3330
127.0.0.0/8 - This block is assigned for use as the Internet host
loopback address. A datagram sent by a higher level protocol to an
address anywhere within this block should loop back inside the host.
Even though you can't see anything from ifconfig or ip, you still can ping all the addresses in that 127.0.0.0/8 block.
Which is the source IP address in tcp socket if bind is called on a multihomed client host? Client has two interfaces eth0(IP0) and eth1(IP1) and the client tcp socket is bound to IP0. After socket, bind, connect in client, it sends a packet to server.The destination IP isservIP. But servIP and IP0 are not in a same subnet(Maybe servIP and IP1 are). Which is the source IP in the packet sent to server? And what will getsockname return?
There are two separate issues here:
1) Which IP to bind on?
When calling bind() you have an option to specify and address to bind on or you can leave this decision to TCP/IP stack on your computer. You can pass a specific address in 'addr' parameter or leave it as INADDR_ANY. You can find more information how to do it in manual page of ip(7). If you call bind() providing the valid IP address and call to bind() succeeds, then datagrams sent using the binded socket will have their source address set to the value provided in call to bind().
2) How the packet is routed?
The way your packet is routed depend only on the destination address and not the source address. It can be that your source address will be the one from eth0 and it will go out through eht1. This is because the routing system in your OS is using destination based routing as opposed to source based routing. You can always see which adapter will be used by issuing "route" command in the console of your OS and comparing the output with the destination address
Here is my requirement.
2 TCP Client connection/Emulation from single eth port
I created two virtual interface.
ifconfig eth1:0 10.0.0.2
ifconfig eth1:1 10.0.0.3
Is is possible to create a TCP client code such that particular interface is used for TCP client establishment.
for example
./client_app eth1:0 - -- for client with ip 10.0.0.2
./client_app eth1:1 -- for client with ip 10.0.0.3
To enumerate all local interfaces and get the IP address associated to these interfaces use the system call getifaddrs().
Then use the bind() system call to bind the local side of a connection to a certain local interface's IP-address.
As far as I understand sockets are just datastructures just like files. Does that mean a differnt socket DS is created for different combinations IP, PORTNUMBER and TRANSPORT TYPE (TCP/UDP)?
I want to know if the below implementations are possible
--> Make a Socket listen on 2 different IPs but same port number so that when a client sends data to any of these IPs(but same port number), Server should be able to serve it. (Or can a port number be associated with 2 different IPs)
--> Make 2 process listen on same port number
Make a Socket listen on 2 different IPs but same port number so that when a client sends data to any of these IPs(but same port number), Server should be able to serve it. (Or can a port number be associated with 2 different IPs)
A socket cannot listen on 2+ explicit IP addresses. You can call bind() on a socket only once, and it does not allow you to specify more than 1 IP address at a time. However, you can bind() a socket to a wildcard IP address (INADDR_ANY for IPv4, IN6ADDR_ANY for IPv6), which will allow the socket to bind to all available local IP addresses at the same time. In that scenario, you can indeed send data to the same port on any IP address that the socket is bound to.
Make 2 process listen on same port number
This is only possible if each process binds to a different IP address and no wildcard IPs are used. If you had multiple processes listening on the same IP and Port, how would they know which process should process which data? They wouldn't, so the OS does not allow it in the first place.
Now, I have a situation where I alias a lot of ips on a single dev exposed by linux system. this dev is lets say ethX.
Now on ethX I add many ips from a subnet.
Problem:
It appears that the port range is shared for all ips on the ethx. which means:
if I use add an ip x.y.a.b on ethx and use port 5552
now i add an ip y.x.b.a on ethx and try to use 5552 -> this cannot be done
Can anyone confirm this?
addition is done using ip2 utils (ip addr add xxxx dev xxxx) etc.
A TCP connection is identified by the tuple (interface, source address, source port, dest address, dest port), so no, ports are not shared.
What do you mean by use? Do you mean bind()? You should be able to bind() to the same port on different addresses, though you may need to use SO_REUSEADDR, I don't remember the specifics.