Bonjour and Socket::getaddrinfo

I was having a problem with DRb coming from Socket::getaddrinfo not
finding an address for the name returned by Socket::gethostname. I’m
running Mac OS 10.4 and Ruby 1.8. Replicating line 837 in drb.rb in
irb gave me this:

irb(main):001:0> require ‘socket’
=> true
irb(main):002:0> host = Socket::gethostname
=> “monkeydrome”
irb(main):003:0> Socket::getaddrinfo(host, nil,Socket::AF_UNSPEC,
irb(main):004:1* Socket::SOCK_STREAM,

irb(main):005:1* 0,
irb(main):006:1* Socket::AI_PASSIVE)
SocketError: getaddrinfo: No address associated with nodename
from (irb):3:in `getaddrinfo’
from (irb):3
from :0

And if I add my Bonjour share name to /etc/hosts (as “127.0.0.1
monkeydrome”), I get the correct response ([[“AF_INET”, 0, “localhost”,
“127.0.0.1”, 2, 1, 6]]). Is there a way to get the correct address
information without changing /etc/hosts?

Thanks,
Andrew O’Brien

[email protected] wrote:

irb(main):004:1* Socket::SOCK_STREAM,
“127.0.0.1”, 2, 1, 6]]). Is there a way to get the correct address
information without changing /etc/hosts?

If there is no IP associated with a particular network node, then no,
you
can’t. The relation between “localhost” and “127.0.0.1” is normally a
relationship created by an entry in /etc/hosts, nowhere else.

On Sep 27, 2006, at 3:05 PM, [email protected] wrote:

I was having a problem with DRb coming from Socket::getaddrinfo not
finding an address for the name returned by Socket::gethostname. I’m
running Mac OS 10.4 and Ruby 1.8. Replicating line 837 in drb.rb in
irb gave me this:

Strange. This worked just fine for me (Mac OS X 10.4.7, Ruby 1.8.5).

Here is what I get calling getaddrinfo without the extra params:

irb(main):008:0* info = Socket::getaddrinfo(host, nil)
=> [[“AF_INET6”, 0, “radar.local”, “fe80::203:93ff:fe96:4cb4%en0”,
30, 2, 17], [“AF_INET6”, 0, “radar.local”, “fe80::203:93ff:fe96:4cb4%
en0”, 30, 1, 6], [“AF_INET”, 0, “10.0.1.2”, “10.0.1.2”, 2, 2, 17],
[“AF_INET”, 0, “10.0.1.2”, “10.0.1.2”, 2, 1, 6]]
irb(main):009:0> info = Socket::getaddrinfo(‘localhost’, nil)
=> [[“AF_INET6”, 0, “localhost”, “::1”, 30, 2, 17], [“AF_INET6”, 0,
“localhost”, “::1”, 30, 1, 6], [“AF_INET”, 0, “localhost”,
“127.0.0.1”, 2, 2, 17], [“AF_INET”, 0, “localhost”, “127.0.0.1”, 2,
1, 6]]

Gary W.

Thanks for the quick reply, Paul.

I’m not sure I follow: my /etc/hosts had “127.0.0.1” and “localhost”
already specified. I had to add a line for “127.0.0.1” and
“monkeydrome”, which is the name I had in the Sharing Preference Pane
(and what Socket::gethostname returns).

My confusion was that I had given the computer a name and I would have
thought that that name would resolve to something, unless I’m
misunderstanding how OS X changes the name (which is more than likely
– that’s why I’m asking :)).

Thanks,
Andrew O’Brien

Hi Gary,

What was the value of your “host” variable in that line? “radar” or
“radar.local”?

I can get mine to work if I do Socket::getaddrinfo(“monkeydrome.local”,
nil), but not with just “monkeydrome” (which is how Socket::gethostname
returns it).

On Sep 27, 2006, at 4:00 PM, [email protected] wrote:

Hi Gary,

What was the value of your “host” variable in that line? “radar” or
“radar.local”?

It was ‘radar.local’. When I run hostname I get ‘radar.local’ also.
I’m assuming that you are just getting ‘monkeydrome’? That doesn’t
seem right to me. I think it should be returning ‘monkeydrome.local’.
Are you manually setting your hostname somehow and omitting the ‘.local’
domain?

In the System Preferences/Sharing, my computer name is listed as just
‘radar’.

Gary W.

[email protected] wrote:

Thanks for the quick reply, Paul.

I’m not sure I follow: my /etc/hosts had “127.0.0.1” and “localhost”
already specified. I had to add a line for “127.0.0.1” and
“monkeydrome”, which is the name I had in the Sharing Preference Pane
(and what Socket::gethostname returns).

I don’t recommend this. In many cases, assigning 127.0.0.1 to the system
hostname as well as localhost will cause complete confusion in your
local
network.

In any case, if you put a reasonable entry in /etc/hosts, like this:

192.168.0.1 monkeydrome.com monkeydrome

You should get the expected outcome.

My confusion was that I had given the computer a name and I would have
thought that that name would resolve to something,

It cannot resolve to an IP unless an IP has been assigned, either by
DHCP,
by way of /etc/hosts, or some third approach. Having a defined host name
won’t produce an address resolution if there is no assigned address.

I want to emphasize that “localhost” is the name of the loopback
pseudo-adaptor, it normally has the address 127.0.0.1, and you don’t
want
to assign this same address to the system. If you do, the system won’t
be
visible to your network.

A minimal /etc/hosts might look like this:

127.0.0.1 localhost.localdomain localhost
192.168.0.1 monkeydrome.com monkeydrome

There may be some differences on OS X, the above is typical in Linux.
Also,
obviously, the address you assign to the local system might not be such
a
generic choice as 192.168.0.1, although that is a reasonable choice if
you
don’t have a special reason to use another.

[email protected] wrote:

If I do “hostname -s monkeydrome.local”, everything works, so I’d say
that pretty much solves that.

Yes, as long as you realize this change won’t survive a reboot.
Typically,
the real hostname, the one that persists, is in /etc//hostname, and is
read
during boot-up. This information is based on a typical *nix environment,
it
may not fully apply to OS X.

[email protected] wrote:

I’m assuming that you are just getting ‘monkeydrome’?

Yep.

Are you manually setting your hostname somehow and omitting the ‘.local’
domain?

Hmm, don’t think so… but it is possible that I messed something up
when I first started learning. I’m guessing that’s the case since I
haven’t seen too many similar complaints on the net. I must have just
screwed up my environment somehow along the way. Good to know it’s not
a problem with the library or the OS.

If I do “hostname -s monkeydrome.local”, everything works, so I’d say
that pretty much solves that.

In the System Preferences/Sharing, my computer name is listed as just
‘radar’.

Yeah same here. Must just be a problem on my end.

Thanks for the help,
Andrew O’Brien

On Sep 27, 2006, at 4:40 PM, Paul L. wrote:

A minimal /etc/hosts might look like this:

127.0.0.1 localhost.localdomain localhost
192.168.0.1 monkeydrome.com monkeydrome

/etc/hosts on my Mac looks like:

127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost

This is the ‘out of the box’ configuration. I didn’t have to
add anything. Also my bonjour host name ‘radar.local’ is resolved
automatically with no need to play with /etc/hosts.

Gary W.

[email protected] wrote:

/ …

/etc/hosts on my Mac looks like:

127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost

This is the ‘out of the box’ configuration. I didn’t have to
add anything. Also my bonjour host name ‘radar.local’ is resolved
automatically with no need to play with /etc/hosts.

Do you mean it has an assigned address as well as being defined as
hostname?
I ask because this address is normally entered into /etc/hosts as well,
unless of course DHCP is creating the assignment (different address
every
time).

By the way, the “::1 localhost” syntax is quite alien from a *nix
perspective. Hard to say what it means.

Paul: I removed the line from /etc/hosts like you recommended –
something in my head was telling me that that wasn’t really a good idea
anyway.

Also, I have no /etc/hostname file. I think OS X has a different way
of assigning hostnames.

Also also, I see what you mean about hostname -s not persisting after
restarting.

Gary: That’s how my /etc/hosts looks too.

I ask because this address is normally entered into /etc/hosts as well,
unless of course DHCP is creating the assignment (different address every
time).

I suppose I should mention that my address is assigned by DHCP, so if I
understand correctly, it wouldn’t show up in that file. I’m assuming
that OS X keeps track of the association somewhere else (and I’m
further assuming that this is handled by Bonjour/Zeroconf?).

By the way, the “::1 localhost” syntax is quite alien from a *nix
perspective. Hard to say what it means.

Yeah, that confused me at first as well. I looked into it and it’s for
IPv6 compatibility.

On Sep 27, 2006, at 5:15 PM, Paul L. wrote:

Do you mean it has an assigned address as well as being defined as
hostname?
I ask because this address is normally entered into /etc/hosts as
well,
unless of course DHCP is creating the assignment (different address
every
time).

DHCP assignment from my firewall/router.

By the way, the “::1 localhost” syntax is quite alien from a *nix
perspective. Hard to say what it means.

::1 is the IPv6 loopback address. Mac OS X has full support for IPv6.

Gary W.

On Sep 27, 2006, at 4:45 PM, [email protected] wrote:

If I do “hostname -s monkeydrome.local”, everything works, so I’d say
that pretty much solves that.

If you feel so inclined, I’d take a look at any startup scripts you may
have added or modified or perhaps any shell startup scripts
(.profile, and
so on) that might get executed as root and include any calls to
hostname.

Gary W.