7

I'm confused about the use and consequences of INADDR_ANY when binding a socket. Of course the INADDR_ANY listens to all the local interfaces. My question as about what consequences this does have.

I remember reading that binding to a specific IP address allows the kernel to handle the demultiplexing but can't find the reference any more.

Will the use of INADDR_ANY have consequences of this kind or will I simply just receive data from all my local ip's? What are the benifits and problems of using each kind of binding?

Other questions that discuss this:

bind with INADDR_ANY

Question about INADDR_ANY

EDIT: Found the reference. It's from Unix Network Programming (Stevens)

One advantage in binding a non-wildcard IP address is that the demultiplexing of a given destination IP address to a given server process is then done by the kernel.

What does this really mean?

Community
  • 1
  • 1
span
  • 5,405
  • 9
  • 57
  • 115
  • If you have a TCP server, running on a computer with four network interface cards, each card on a different network, and you want to listen on connections from all networks, would you rather create four sockets each bound to a separate network, or use one socket and bind to all using `INADDR_ANY`? – Some programmer dude Sep 17 '13 at 10:42
  • Well, I would like to bind to all of them if it doesn't have any performance or other consequences that might have a negative impact. The question tries to find out if there are any consequences of this kind. – span Sep 17 '13 at 10:43
  • As for the demultiplexing, it has to be done anyway, either by you using `select` et al, or automatically by the kernel (and if you use `select` or similar, it will be done by the kernel anyway). – Some programmer dude Sep 17 '13 at 10:44
  • Found the reference, not sure if it changes anything. – span Sep 17 '13 at 10:53
  • 1
    Performance isn't the issue here. If there is any difference, I would say it would be slower for you to manually multiplex the connection. The thing here is what requirements you have on the program: Should it only receive connections from a single network? In that case bind only to that network. Should it receive connections from all networks? Bind to `INADDR_ANY`. Should it receive connections from only *some* of the networks? Then it's up to you to decide what you think might be best, but performance is not going to be an issue. – Some programmer dude Sep 17 '13 at 10:55

2 Answers2

3

Binding to specific interfaces is something to use only in very special circumstances, when the application needs to "know" the local IP addresses and the immediate network layout. A routing daemon program is perhaps the best example.

Another, more pedestrian example: if you have a multi-homed machine (i.e. a machine with more than one connection to the Internet, possibly different ISPs) you can bind to a specific interface to make sure that the connection goes through a given connection. Binding separately to each network interface, the application could detect link down etc.

Implementations of protocols that need to "know" the local IP address (infamous examples: FTP, SIP, UPnP) tend to bind sockets to specific interfaces. (I consider them to be bad protocols, since they violate the isolation between transport and application layers).

Save for these cases, it is normally over-engineering to bind to specific interfaces, because addresses and interfaces may change, and the program must detect these conditions to update the respective sockets.

epx
  • 1,066
  • 9
  • 17
2

You are not going to be able to measure any performance difference between using a specific IP or all of them. You might wish to use a specific one based on the needs of your application... for example, if you know you should never have a (legitimate) connection from an external facing IP, you would not want to receive input from it, for security reasons.

mah
  • 39,056
  • 9
  • 76
  • 93