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/rr/classes.rb
Views: 11784
# -*- coding: binary -*-1module Net # :nodoc:2module DNS34class RR56#7# This is an auxiliary class to hadle RR class field in a DNS packet.8#9class Classes1011# An hash with the values of each RR class stored with the12# respective id number13Classes = {14'IN' => 1, # RFC 103515'CH' => 3, # RFC 103516'HS' => 4, # RFC 103517'NONE' => 254, # RFC 213618'ANY' => 255, # RFC 103519}2021# The default value when class is nil in Resource Records22@@default = Classes["IN"]2324# Be able to control the default class to assign when25# cls argument is +nil+. Default to +IN+26def self.default=(str)27if Classes.has_key? str28@@default = Classes[str]29else30raise ClassArgumentError, "Unknown class #{str}"31end32end3334# Checks whether +cls+ is a valid RR class.35def self.valid?(cls)36case cls37when String38return Classes.has_key?(cls)39when Integer40return Classes.invert.has_key?(cls)41else42raise ClassArgumentError, "Wrong class: #{cls.class}"43end44end4546# Returns the class in string format, as "IN" or "CH",47# given the numeric value48def self.to_str(cls)49case cls50when Integer51if Classes.invert.has_key? cls52return Classes.invert[cls]53else54raise ClassArgumentError, "Unknown class number #{cls}"55end56else57raise ClassArgumentError, "Wrong class: #{cls.class}"58end59end6061# Gives in output the keys from the +Classes+ hash62# in a format suited for regexps63def self.regexp64Classes.keys.join("|")65end6667# Creates a new object representing an RR class. Performs some68# checks on the argument validity too. Il +cls+ is +nil+, the69# default value is +ANY+ or the one set with Classes.default=70def initialize(cls)71case cls72when String73# type in the form "A" or "NS"74new_from_string(cls.upcase)75when Integer76# type in numeric form77new_from_num(cls)78when nil79# default type, control with Classes.default=80@str = Classes.invert[@@default]81@num = @@default82else83raise ClassArgumentError, "Wrong class: #{cls.class}"84end85end8687# Constructor for string data class,88# *PRIVATE* method89def new_from_string(cls)90case cls91when /^CLASS(\d+)$/92new_from_num(Regexp.last_match(1).to_i)93else94# String with name of class95if Classes.has_key? cls96@str = cls97@num = Classes[cls]98else99raise ClassArgumentError, "Unknown class #{cls}"100end101end102end103104# Constructor for numeric data class105# *PRIVATE* method106def new_from_num(cls)107raise ClassArgumentError, "Invalid class #{cls}" if cls < 0 || cls > 0xFFFF108if Classes.invert.has_key? cls109@num = cls110@str = Classes.invert[cls]111else112@num = cls113@str = "CLASS#{cls}"114end115end116117# Returns the class in number format118# (default for normal use)119def inspect120@num121end122123# Returns the class in string format,124# i.d. "IN" or "CH" or such a string.125def to_s126@str127end128129# Returns the class in numeric format,130# usable by the pack methods for data transfers131def to_i132@num.to_i133end134135136# Should be used only for testing purpouses137def to_str138@num.to_s139end140141private :new_from_num, :new_from_string142143end # class Classes144145end # class RR146end # module DNS147end # module Net148149class ClassArgumentError < ArgumentError # :nodoc:150end151152153