logstash-plugins / logstash-filter-dns

Apache License 2.0
7 stars 28 forks source link

getaddress() and getname() don't return strings #11

Closed docwhat closed 9 years ago

docwhat commented 9 years ago

I was looking through the code and ran across these two statements:

dns.rb Lines 114-115:

# in JRuby 1.7.11 outputs as US-ASCII
address = @resolv.getaddress(raw).force_encoding(Encoding::UTF_8)

and dns.rb Lines 173-174:

# in JRuby 1.7.11 outputs as US-ASCII
hostname = @resolv.getname(raw).force_encoding(Encoding::UTF_8)

Both try to run .force_encoding() on the results of getaddress() and getname() but they return objects that don't have a .force_encoding() method on them:

$ jpry -r resolv
Pry running on 1.9.3-p551 with RubyGems 2.4.8 and .
[1] (pry) main: 0> r = Resolv::DNS.new
[2] (pry) main: 0> r.getaddress('google.com').force_encoding(Encoding::UTF_8)
NoMethodError: undefined method `force_encoding' for #<Resolv::IPv4 74.125.29.139>
from (pry):2:in `evaluate_ruby'
[3] (pry) main: 0> r.getname('74.125.29.113').force_encoding(Encoding::UTF_8)
NoMethodError: undefined method `force_encoding' for #<Resolv::DNS::Name: qg-in-f113.1e100.net.>
from (pry):3:in `evaluate_ruby'

Should there be a .to_s before the .force_encoding()?

jordansissel commented 9 years ago

Thanks for pointing me here!

The code we use invokes Resolv.new not Resolv::DNS.new as your example has.

I can confirm that Resolv::DNS.new.getaddress("google.com") returns an object of type Resolv::IPv4, however, this filter uses Resolv.new and Resolv#getaddress (both instance and class methods) return strings, not objects.

Does this clarify things?

jordansissel commented 9 years ago

I agree it's really weird that Resolv#getaddress and Resolv::DNS#getaddress return different object types :\

docwhat commented 9 years ago

Hmm. I'm wondering where I got the second form from? Maybe I was setting the resolvers? I swear I duplicated the code in pry and got an object.

This isn't one of those things that "sometimes" returns a string?

Sorry to waste your time.

jordansissel commented 9 years ago

@docwhat wasn't a waste of time. I appreciate the extra eyes reviewing the code! :)