# Problem with Socket.getaddrinfo inside controller or model

**URL:** <https://www.ruby-forum.com/t/problem-with-socket-getaddrinfo-inside-controller-or-model/160430>\
**Category:** Rails\
**Created:** [February 16, 2009, 10:55am UTC](https://www.ruby-forum.com/t/problem-with-socket-getaddrinfo-inside-controller-or-model/160430 "2009-02-16T10:55:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![iwakura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8797f3/32.png) [@iwakura](https://www.ruby-forum.com/u/iwakura)\
**Post date:** [February 16, 2009, 10:55am UTC](https://www.ruby-forum.com/t/problem-with-socket-getaddrinfo-inside-controller-or-model/160430/1 "2009-02-16T10:55:16Z")

</div>

I am using Rails 2.2.2,  
trying to get hostname by IP with code like

require ‘socket’  
Socket.getaddrinfo(‘221.186.184.68’, nil, “AF\_INET”)

In the rails console it works well and gives me:

[[“AF\_INET”, 0, “[carbon.ruby-lang.org](http://carbon.ruby-lang.org)”, “221.186.184.68”, 2, 2, 17],  
[“AF\_INET”, 0, “[carbon.ruby-lang.org](http://carbon.ruby-lang.org)”, “221.186.184.68”, 2, 1, 6]]

But, inside model or controller output different:

[[“AF\_INET”, 0, “221.186.184.68”, “221.186.184.68”, 2, 2, 17],  
[“AF\_INET”, 0, “221.186.184.68”, “221.186.184.68”, 2, 1, 6]]

Please help to fix and get hostname by ip inside model.

---

<div class="post-metadata">

**Author:** ![iwakura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8797f3/32.png) [@iwakura](https://www.ruby-forum.com/u/iwakura)\
**Post date:** [March 17, 2009, 12:17am UTC](https://www.ruby-forum.com/t/problem-with-socket-getaddrinfo-inside-controller-or-model/160430/2 "2009-03-17T00:17:47Z")

</div>

Hi,

There is a method in mongrel gem, mongrel.rb, Mongrel::HttpServer#run  
which has a line:

BasicSocket.do\_not\_reverse\_lookup=true

and it seems this changes the global state of the Socket class. So,  
basically I did this:

BasicSocket.do\_not\_reverse\_lookup=false  
Socket.getaddrinfo(some\_ip, nil, “AF\_INET”)  
BasicSocket.do\_not\_reverse\_lookup=true # In case nothing goes wrong  
with mongrel 🙂

Quit annoying stuff, I found it almost accidently, doubt it is  
somewhere documented.

On Feb 16, 11:55 am, Taro I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
