Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/net/dns/names/names.rb
Views: 11655
# -*- coding: binary -*-1module Net # :nodoc:2module DNS34module Names # :nodoc: all56INT16SZ = 278# Expand a compressed name in a DNS Packet object. Please9# see RFC1025 for an explanation of how the compression10# in DNS packets works, how may it be useful and how should11# be handled.12#13# This method accept two parameters: a raw packet data and an14# offset, which indicates the point in the packet in which the15# parsing has arrived.16#17def dn_expand(packet,offset)18name = ""19packetlen = packet.size20while true21raise ExpandError, "offset is greater than packet length!" if packetlen < (offset+1)22len = packet.unpack("@#{offset} C")[0]2324if len == 025offset += 126break27elsif (len & 0xC0) == 0xC028raise ExpandError, "Packet ended before offset expand" if packetlen < (offset+INT16SZ)29ptr = packet.unpack("@#{offset} n")[0]30ptr &= 0x3FFF31name2 = dn_expand(packet,ptr)[0]32raise ExpandError, "Packet is malformed!" if name2 == nil33name += name234offset += INT16SZ35break36else37offset += 138raise ExpandError, "No expansion found" if packetlen < (offset+len)39elem = packet[offset..offset+len-1]40name += "#{elem}."41offset += len42end43end44return [name,offset] # name.chomp(".") if trailing dot has to be omitted45end4647def pack_name(name)48if name.size > 25549raise ArgumentError, "Name data cannot exceed 255 chars"50end51arr = name.split(".")52str = ""53arr.each do |elem|54if elem.size > 6355raise ArgumentError, "Label data cannot exceed 63 chars"56end57str += [elem.size,elem].pack("Ca*")58end59str += [0].pack("C")60str61end6263def names_array(name)64arr = name.split(".")65ar = []66string = ""67arr.size.times do |i|68x = i+169elem = arr[-x]70len = elem.size71string = ((string.reverse)+([len,elem].pack("Ca*")).reverse).reverse72ar.unshift(string)73end74return ar75end7677def dn_comp(name,offset,compnames)78names = {}79ptr = 080str = ""81arr = names_array(name)82arr.each do |entry|83if compnames.has_key?(entry)84ptr = 0xC000 | compnames[entry]85str += [ptr].pack("n")86offset += INT16SZ87break88else89len = entry.unpack("C")[0]90elem = entry[1..len]91str += [len,elem].pack("Ca*")92names.update({"#{entry}" => offset})93offset += len94end95end96return str,offset,names97end9899def valid?(name)100if 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])$/i101return name102else103raise ArgumentError, "Invalid FQDN: #{name}"104end105end106107end # module Names108end # module DNS109end # module Net110111class ExpandError < StandardError # :nodoc:112end113114115