Index: src/resolver.cr ================================================================== --- src/resolver.cr +++ src/resolver.cr @@ -29,49 +29,39 @@ def initialize(@query : Query, @id : UInt16, @settings : Settings, @block : Response | Error ->) @ns_index = 0 @attempt = 0 @used_ns = nil - @raw_data = Bytes.new(512) # Header - i = 0 - @raw_data[i] = (@id >> 8).to_u8; i += 1 - @raw_data[i] = (@id & 0xFF).to_u8; i += 1 + io = IO::Memory.new(512) + io.write_bytes(@id, IO::ByteFormat::BigEndian) # RD - @raw_data[i] = 1; i += 1 - i += 1 + io.write_bytes(0x100_u16, IO::ByteFormat::BigEndian) # QDCOUNT - i += 1 - @raw_data[i] = 1; i += 1 + io.write_bytes(1_u16, IO::ByteFormat::BigEndian) # ANCOUNT, NSCOUNT and ARCOUNT - i += 6 + 3.times { io.write_bytes(0_u16) } # Question # QNAME @query.domain.split('.').each do |component| - if component.bytesize > 63 || i + component.bytesize > 512 + if component.bytesize > 63 || io.size + component.bytesize > 512 raise ArgumentError.new("Domain component too long") end - raw_component = component.to_slice - @raw_data[i] = raw_component.bytesize.to_u8; i += 1 - @raw_data[i, raw_component.bytesize].copy_from(raw_component) - i += raw_component.bytesize + io << component end # QTYPE - qtype = @query.rr_type.to_i - @raw_data[i] = (qtype >> 8).to_u8; i += 1 - @raw_data[i] = (qtype & 0xFF).to_u8; i+= 1 - + io.write_bytes(@query.rr_type.to_u16, IO::ByteFormat::BigEndian) # QCLASS - qclass = @query.dns_class.to_i - @raw_data[i] = (qclass >> 8).to_u8; i += 1 - @raw_data[i] = (qclass & 0xFF).to_u8; i += 1 + io.write_bytes(@query.dns_class.to_u16, IO::ByteFormat::BigEndian) + + @raw_data = io.to_slice end end def initialize @settings = Settings.new