UDPSocket#bind with 0 or nil

(Joel VanderWerf) #1

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.

···

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

(Daniel Brockman) #2

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).
The former binds to all interfaces, like bind("<any>", foo).
The latter only binds to the loopback interface.

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.)

···

--
Daniel Brockman <daniel@brockman.se>

    So really, we all have to ask ourselves:
    Am I waiting for RMS to do this? --TTN.

(Joel VanderWerf) #3

Daniel Brockman wrote:

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).
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.

···

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407