1

I am trying to extend Resolv::DNS to be able to get SPF resources. For this, I've simply added the following:

class Resolv::DNS::Resource::IN::SPF < Resolv::DNS::Resource::IN::TXT
end

class Resolv::DNS::Resource::SPF < Resolv::DNS::Resource::TXT
end

When I now use SPFinstead of TXT for querying, I get an empty hash. So I tried digging further.. on resolv.rb line 516, the type class is provided as second argument when creating a Message object, which is located on line 1257. However, there stops my understanding of how things work.

Can anyone enlighten me or give me some guidance?

Cœur
  • 37,241
  • 25
  • 195
  • 267
pdu
  • 10,295
  • 4
  • 58
  • 95
  • 1
    can you give me an example of a domain that you are trying to get the SPF record for? Since SPF records are just TXT records, this might not work the way you are trying to do it. – Eugene Jan 12 '14 at 14:08
  • AFAIK, writing spf records as TXT is just for compatibility reasons, there is an actual SPF. Usually, you write both, but I want to catch the case where one just entered SPF and not TXT records. I've set up one on `notionlab.ch` to test this. `dig -t spf` returns only the spf one, and `dig -t txt` only the txt one. – pdu Jan 12 '14 at 15:30

1 Answers1

0

I found out by querying my domain that Resolv seems to auto generate classes for record types it does not recognize. What you can do is query for all records and then filter on the type code. SPF records have type code 99, so you end up with something like this:

require 'resolv'

resolver = Resolv::DNS.new
all_records = resolver.getresources('example.com', Resolv::DNS::Resource::IN::ANY)
spf_records = all_records.select { |r| r.class::TypeValue == 99 }
spf_records.each do |record|
  puts(record.data)
end

It's a bit ugly how you query the type code (Resolv creates a TypeValue constant on the generated classes, but adds no way to get this value from instances as far as I can see).

Community
  • 1
  • 1
Theo
  • 131,503
  • 21
  • 160
  • 205