def default(self, obj): if isinstance(obj, ResourceRecord): return { "name": obj.name, "type": Type.to_string(obj.type_), "class": Class.to_string(obj.class_), "ttl": obj.ttl, "rdata": obj.rdata.data } return json.JSONEncoder.default(self, obj)
def from_bytes(cls, packet, offset): """Convert ResourceRecord from bytes.""" name, offset = Name.from_bytes(packet, offset) type_ = Type(struct.unpack_from("!H", packet, offset)[0]) class_ = Class(struct.unpack_from("!H", packet, offset + 2)[0]) ttl, rdlength = struct.unpack_from("!iH", packet, offset + 4) offset += 10 rdata = RecordData.create_from_bytes(type_, packet, offset, rdlength) offset += rdlength return cls(name, type_, class_, ttl, rdata), offset
def default(self, obj): if isinstance(obj, ResourceRecord): return { "name": obj.name, "type": Type.to_string(obj.type_), "class": Class.to_string(obj.class_), "ttl": obj.ttl, "rdata": obj.rdata.data, "time": obj.time } return json.JSONEncoder.default(self, obj)
def resource_from_json(dct): """ Convert JSON object to ResourceRecord Usage: records = json.loads(string, object_hook=resource_from_json) """ name = dct["name"] type_ = Type.from_string(dct["type"]) class_ = Class.from_string(dct["class"]) ttl = dct["ttl"] rdata = RecordData.create(type_, dct["rdata"]) return ResourceRecord(name, type_, class_, ttl, rdata)
def resource_from_json(dct): """ Convert JSON object to ResourceRecord Usage: records = json.loads(string, object_hook=resource_from_json) """ name = dct["name"] type_ = Type.from_string(dct["type"]) class_ = Class.from_string(dct["class"]) ttl = dct["ttl"] rdata = RecordData.create(type_, dct["rdata"]) t = dct["time"] return ResourceRecord(name, type_, class_, ttl, rdata, t)
def test_single_lookup(self): """Ask for a single existing hostname""" dnsID = randint(0, 65535) header = dns.message.Header(dnsID, 0, 1, 0, 0, 0) header.qr = 0 header.opcode = 0 header.aa = 0 header.tc = 0 header.rd = 1 header.ra = 0 header.z = 0 header.rcode = 0 qname = "www.funnygames.com" qtype = Type.ANY # request for all records qclass = Class.IN # ANY question = dns.message.Question(qname, qtype, qclass) request = dns.message.Message(header, [question], [], [], []) requestByte = request.to_bytes() self.client_socket.sendto(requestByte, (server, portnr)) print Type.to_string(question.qtype) + Class.to_string(question.qclass) responseData = self.client_socket.recv(512) response = dns.message.Message.from_bytes(responseData) self.assertNotEquals(response.answers, [])
def from_bytes(cls, packet, offset): """Convert Question from bytes.""" qname, offset = Name.from_bytes(packet, offset) qtype = Type(struct.unpack_from("!H", packet, offset)[0]) qclass = Class(struct.unpack_from("!H", packet, offset + 2)[0]) return cls(qname, qtype, qclass), offset + 4