I have a problem with a ruby application connecting to a data base. After some hours of poking around, my problem boiled down to Socket.getaddrinfo 'localhost', nil
returns unexpected addresses:
ruby -e "require \"socket\"; puts Socket.getaddrinfo('localhost', nil).inspect"
[["AF_INET", nil, "10.10.1.37", "10.10.1.37", 2, 2, 17],
["AF_INET", nil, "10.10.1.37", "10.10.1.37", 2, 1, 6],
["AF_INET", nil, "fe80:0:0:0:5e96:9dff:fe94:fce3%5", "fe80:0:0:0:5e96:9dff:fe94:fce3%5", 2, 2, 17],
["AF_INET", nil, "fe80:0:0:0:5e96:9dff:fe94:fce3%5", "fe80:0:0:0:5e96:9dff:fe94:fce3%5", 2, 1, 6]]
10.10.1.37 is the address of the network adapter.
I'm using rvm and this strange behavior just happens, when I use a jruby. If I use a regular ruby version, the result is what I expect ('127.0.0.1' for example).
According to wikipedia, localhost should refer to the local loop back interface. What could cause this behavior, it this a Java thingy?
JRuby versions I've tried are: 1.7.2, 1.7.4 and 1.7.9
The more expected output (when using a regular ruby-1.9.3) is:
ruby -e "require \"socket\"; puts Socket.getaddrinfo('localhost', nil).inspect"
[["AF_INET6", 0, "::1", "::1", 30, 2, 17],
["AF_INET6", 0, "::1", "::1", 30, 1, 6],
["AF_INET", 0, "127.0.0.1", "127.0.0.1", 2, 2, 17],
["AF_INET", 0, "127.0.0.1", "127.0.0.1", 2, 1, 6],
["AF_INET6", 0, "fe80::1%lo0", "fe80::1%lo0", 30, 2, 17],
["AF_INET6", 0, "fe80::1%lo0", "fe80::1%lo0", 30, 1, 6]]