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/a.rb
Views: 11784
# -*- coding: binary -*-1##2#3# Net::DNS::RR::A4#5# $id$6#7##89require 'ipaddr'1011module Net # :nodoc:12module DNS1314class RR1516# =Name17#18# Net::DNS::RR::A DNS A resource record19#20# =Synopsis21#22# require "net/dns/rr"23#24# =Description25#26# Net::DNS::RR::A is the class to handle resource records of type A, the27# most common in a DNS query. Its resource data is an IPv4 (i.e. 32 bit28# long) address, hold in the instance variable +address+.29# a = Net::DNS::RR::A.new("localhost.movie.edu. 360 IN A 127.0.0.1")30#31# a = Net::DNS::RR::A.new(:name => "localhost.movie.edu.",32# :ttl => 360,33# :cls => Net::DNS::IN,34# :type => Net::DNS::A,35# :address => "127.0.0.1")36#37# When computing binary data to transmit the RR, the RDATA section is an38# Internet address expressed as four decimal numbers separated by dots39# without any embedded spaces (e.g.,"10.2.0.52" or "192.0.5.6").40#41class A < RR42attr_reader :address4344# Assign to the RR::A object a new IPv4 address, which can be in the45# form of a string or an IPAddr object46#47# a.address = "192.168.0.1"48# a.address = IPAddr.new("10.0.0.1")49#50def address=(addr)51@address = check_address addr52build_pack53end # address=5455private5657def check_address(addr)58address = ""59case addr60when String61address = IPAddr.new addr62when Integer # Address in numeric form63tempAddr = [(addr>>24),(addr>>16)&0xFF,(addr>>8)&0xFF,addr&0xFF]64tempAddr = tempAddr.collect {|x| x.to_s}.join(".")65address = IPAddr.new tempAddr66when IPAddr67address = addr68else69raise RRArgumentError, "Unknown address type: #{addr}"70end71raise RRArgumentError, "Must specify an IPv4 address" unless address.ipv4?72address73rescue ArgumentError74raise RRArgumentError, "Invalid address #{addr}"75end7677def build_pack78@address_pack = @address.hton79@rdlength = @address_pack.size80end8182def set_type83@type = Net::DNS::RR::Types.new("A")84end8586def get_data87@address_pack88end8990def get_inspect91"#@address"92end9394def subclass_new_from_hash(args)95if args.has_key? :address96@address = check_address args[:address]97elsif args.has_key? :rdata98@address = check_address args[:rdata]99else100# Address field is mandatory101raise RRArgumentError, ":address field is mandatory but missing"102end103end104105def subclass_new_from_string(str)106@address = check_address(str)107end108109def subclass_new_from_binary(data,offset)110a,b,c,d = data.unpack("@#{offset} CCCC")111@address = IPAddr.new "#{a}.#{b}.#{c}.#{d}"112return offset + 4113end114115end # class A116117end # class RR118end # module DNS119end # module Net120121122