Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 944 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sockets - Using INADDR_ANY on client side

#1
I recently ran into [this blog post][1] which describes a TCP server client using libev. The sever uses `INADDR_ANY` to bind to an interface which is something I'm familiar with. However, I was surprised to see `INADDR_ANY` in the client code as well. The relevant code on the client code is as follows:

// Create client socket
if( (sd = socket(PF_INET, SOCK_STREAM, 0)) < 0 )
{
perror("socket error");
return -1;
}

bzero(&addr, sizeof(addr));

addr.sin_family = AF_INET;
addr.sin_port = htons(PORT_NO);
addr.sin_addr.s_addr = htonl(INADDR_ANY);

// Connect to server socket
if(connect(sd, (struct sockaddr *)&addr, sizeof addr) < 0)
{
perror("Connect error");
return -1;
}

Specifically I'm interesed in the line:

addr.sin_addr.s_addr = htonl(INADDR_ANY);

On the server side I understand that `INADDR_ANY` will bind the port to all available interfaces, but I'm not sure how this makes sense on the client side. In the end, the client will need to connect on a particular interface. Previously I have always specified the IP address or used `INADDR_LOOPBACK`.

[The Linux IP man page][2] doesn't talk about using `INADDR_ANY` on the client side. I did find [another Stack Overflow post here][3] which says that the OP should use `INADDR_ANY` on the client side, but gives no justification or explanation.

So what is this actually doing? Is it trying all interfaces until it finds one where the port is available for connection? What order does this happen in?

Thanks for your answers!


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#2
At client side, using INADDR_ANY is redundant, but I have seen some code with that, I guess it is for 'completeness'. You can specify the interface at client side if you want to force a specific interface, e.g. in multihomed machines.

Binding to a port at client side is uncommon as well. It is normally a better idea to let the system to find an available port, or else the program may fail because the port happens to be in use by a client or by a server.
Reply

#3
It seems like your question is not really about "client-side", but about `bind` vs `connect`.


`INADDR_ANY` can be sensibly used with `bind` on both client and server. Using it with `connect()` is pointless and should cause a connection failure.
Reply

#4
This is the answer as provided by nos in a comment. If nos comes back and posts it as an answer, I will mark nos' post as the answer and delete this one.

> INADDR_ANY is normally defined as 0. That is the IP address 0.0.0.0.
> RFC 1122 says that means "This host on this network". The linux IP
> stack seems to just route this to the loopback interface. (e.g. try
> ping 0.0.0.0 or even just ping 0). I'd say the author made a typo, and
> should have used INADDR_LOOPBACK.
Reply

#5
There is an old BSD convention that connecting to INADDR_ANY means you want to connect to the loopback network. The linux network code explicitly supports this (search for INADDR_ANY in [this file][1]). I have no idea what other OSes do or don't support it.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through