UDPSocket#bind with 0 or nil

J

Joel VanderWerf

Anyone happen know what the difference between bind(0) and bind(nil) is?
I thought they both meant the same as "<any>", but I think I observed
different behavior in one case.
 
D

Daniel Brockman

Joel,
Anyone happen know what the difference between bind(0) and
bind(nil) is?

I assume you mean bind(0, foo) vs. bind(nil, foo).
I thought they both meant the same as "<any>", but I think
I observed different behavior in one case.

The arguments to UDPSocket#bind are passed to getaddrinfo,
and the socket is bound to the first returned interface.

Quoting the man page for getaddrinfo(3),

int getaddrinfo(const char *node, const char *service,
const struct addrinfo *hints,
struct addrinfo **res);

[...]

If node is NULL, the network address in each socket
structure is initialized according to the AI_PASSIVE
flag, which is set in the ai_flags member of the hints
parameter.

In our case, this flag and all other flags are always unset.
(See `udp_bind' in ext/socket/socket.c:1406.)

The network address in each socket structure will be left
unspecified if AI_PASSIVE flag is set. This is used by
server applications, which intend to accept client
connections on any network address. THE NETWORK ADDRESS
WILL BE SET TO THE LOOPBACK INTERFACE ADDRESS if the
AI_PASSIVE flag is not set. This is used by client
applications, which intend to connect to a server running
on the same network host.

(Emphasis mine.)
 
J

Joel VanderWerf

Daniel said:
Joel,




I assume you mean bind(0, foo) vs. bind(nil, foo).
The former binds to all interfaces, like bind("<any>", foo).
The latter only binds to the loopback interface.
...snipped useful explanation...

Thanks, that clears it up and explains what I was seeing.

It's just confusing to me that the ruby nil value is used differently
from the Fixnum 0 in the front-end to a system call.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top