Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some bugfixes and minor enhancements #36

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/net/dns/names.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,19 @@ def dn_comp(name,offset,compnames)
end

def valid?(name)
if name =~ /^([a-z0-9]([-a-z0-9]*[a-z0-9])?\.)+((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])$/i
return name
else
raise ArgumentError, "Invalid FQDN: #{name}"
raise ArgumentError, "Invalid FQDN: #{name}" if name.length < 1 or name.length > 255

return name if name == '.' # the root domain is the only valid domain that begins with a dot

parts = name.split('.', -1)
parts.delete_at(parts.length-1) if parts.last.empty? # the domain may end with a dot

parts.each do |part|
raise ArgumentError, "Invalid FQDN: #{name}" if
not part =~ /^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/i or part.length < 1 or part.length > 63
end

return name
end

end
Expand Down
13 changes: 7 additions & 6 deletions lib/net/dns/packet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,19 @@ class PacketError < Error

# Creates a new instance of <tt>Net::DNS::Packet</tt> class. Arguments are the
# canonical name of the resource, an optional type field and an optional
# class field. The record type and class can be omitted; they default
# to +A+ and +IN+.
# class field. If the arguments are omitted, no question is added to the new packet;
# type and class default to +A+ and +IN+ if a name is given.
#
# packet = Net::DNS::Packet.new
# packet = Net::DNS::Packet.new("www.example.com")
# packet = Net::DNS::Packet.new("example.com", Net::DNS::MX)
# packet = Net::DNS::Packet.new("example.com", Net::DNS::TXT, Net::DNS::CH)
#
# This class no longer instantiate object from binary data coming from
# network streams. Please use <tt>Net::DNS::Packet.parse</tt> instead.
def initialize(name, type = Net::DNS::A, cls = Net::DNS::IN)
@header = Net::DNS::Header.new(:qdCount => 1)
@question = [Net::DNS::Question.new(name, type, cls)]
def initialize(name = nil, type = Net::DNS::A, cls = Net::DNS::IN)
@header = Net::DNS::Header.new(:qdCount => name.nil? ? 0 : 1)
@question = name.nil? ? [] : [Net::DNS::Question.new(name, type, cls)]
@answer = []
@authority = []
@additional = []
Expand Down Expand Up @@ -542,7 +543,7 @@ def new_from_data(data, from = nil)
@additional << rrobj
@logger.debug rrobj.inspect
rescue NameError => e
warn "Net::DNS supported record type: #{e.message}"
warn "Net::DNS unsupported record type: #{e.message}"
end
end

Expand Down
16 changes: 13 additions & 3 deletions lib/net/dns/rr/soa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def build_pack
@soa_pack = pack_name(@mname)
@soa_pack += pack_name(@rname)
@soa_pack += [@serial,@refresh,@retry,@expire,@minimum].pack("N5")
@rdlength = @soa_pack.size
end

def get_data
Expand All @@ -32,7 +33,7 @@ def subclass_new_from_hash(args)
raise ArgumentError, "Missing field :#{key}" unless args.has_key? key
end
@mname = args[:mname] if valid? args[:mname]
@rname = args[:rname] if valid? args[:rname]
@rname = args[:rname] if rname? args[:rname]
@serial = args[:serial] if number? args[:serial]
@refresh = args[:refresh] if number? args[:refresh]
@retry = args[:retry] if number? args[:retry]
Expand All @@ -49,12 +50,21 @@ def number?(num)
end
end

def rname?(name)
begin
mailbox, domain = name.split(/(?<!\\)\./, 2)
return name if valid? domain
rescue
end
raise ArgumentError, "Invalid RNAME: #{name}"
end

def subclass_new_from_string(str)
mname,rname,serial,refresh,ret,expire,minimum = str.strip.split(" ")
@mname = mname if valid? mname
@rname = rname if valid? rname
@rname = rname if rname? rname
@serial,@refresh,@retry,@expire,@minimum = [serial,refresh,ret,expire,minimum].collect do |i|
i.to_i if valid? i.to_i
i.to_i if number? i.to_i
end
end

Expand Down