async def get_response_data(request_logger, resolve, request_data): # This may raise an exception, which is handled at a higher level. # We can't [and I suspect shouldn't try to] return an error to the # client, since we're not able to extract the QID, so the client won't # be able to match it with an outgoing request query = parse(request_data) try: return pack(await proxy(request_logger, resolve, query)) except Exception: request_logger.exception('Failed to proxy %s', query) return pack(error(query, ERRORS.SERVFAIL))
async def get_response(query_data): query = parse(query_data) response_record = ResourceRecord( name=query.qd[0].name, qtype=TYPES.A, qclass=1, ttl=0, rdata=ipaddress.IPv4Address('123.100.123.1').packed, ) response = Message( qid=query.qid, qr=RESPONSE, opcode=0, aa=0, tc=0, rd=0, ra=1, z=0, rcode=0, qd=query.qd, an=(response_record, ), ns=(), ar=(), ) received_request.set() await continue_request.wait() return pack(response)
async def get_response(query_data): query = parse(query_data) response_records = tuple( ResourceRecord( name=query.qd[0].name, qtype=TYPES.A, qclass=1, ttl=0, rdata=ipaddress.IPv4Address('123.100.123.' + str(i)).packed, ) for i in range(0, num_records)) response = Message( qid=query.qid, qr=RESPONSE, opcode=0, aa=0, tc=0, rd=0, ra=1, z=0, rcode=0, qd=query.qd, an=response_records, ns=(), ar=(), ) return pack(response)
async def get_response(query_data): query = parse(query_data) response = Message( qid=query.qid, qr=RESPONSE, opcode=0, aa=0, tc=0, rd=0, ra=1, z=0, rcode=rcode, qd=query.qd, an=(), ns=(), ar=(), ) return pack(response)
async def test_sending_lots_of_good_messages_not_affect_later_queries( self): resolve, clear_cache = get_resolver(53) self.add_async_cleanup(clear_cache) start = DnsProxy(rules=((r'(^.*$)', r'\1'), )) server_task = await start() self.add_async_cleanup(await_cancel, server_task) response = await resolve('www.google.com', TYPES.A) self.assertEqual(type(response[0]), IPv4AddressExpiresAt) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) for i in range(0, 100000): name = b'doesnotexist' + str(i).encode('ascii') + b'.charemza.name' question_record = QuestionRecord(name, TYPES.A, qclass=1) question = Message( qid=i % 65535, qr=QUESTION, opcode=0, aa=0, tc=0, rd=0, ra=1, z=0, rcode=0, qd=(question_record, ), an=(), ns=(), ar=(), ) sock.sendto(pack(question), ('127.0.0.1', 53)) sock.close() response = await resolve('www.google.com', TYPES.A) self.assertEqual(type(response[0]), IPv4AddressExpiresAt)
async def test_sending_pointer_loop_not_affect_later_queries_c(self): resolve, clear_cache = get_resolver(53) self.add_async_cleanup(clear_cache) start = DnsProxy(rules=((r'(^.*$)', r'\1'), )) server_task = await start() self.add_async_cleanup(await_cancel, server_task) response = await resolve('www.google.com', TYPES.A) self.assertEqual(type(response[0]), IPv4AddressExpiresAt) name = b'mydomain.com' question_record = QuestionRecord(name, TYPES.A, qclass=1) record_1 = ResourceRecord( name=name, qtype=TYPES.A, qclass=1, ttl=0, rdata=ipaddress.IPv4Address('123.100.124.1').packed, ) response = Message( qid=1, qr=RESPONSE, opcode=0, aa=0, tc=0, rd=0, ra=1, z=0, rcode=0, qd=(question_record, ), an=(record_1, ), ns=(), ar=(), ) data = pack(response) packed_name = b''.join( component for label in name.split(b'.') for component in (bytes([len(label)]), label)) + b'\0' occurance_1 = data.index(packed_name) occurance_1_end = occurance_1 + len(packed_name) occurance_2 = occurance_1_end + data[occurance_1_end:].index( packed_name) occurance_2_end = occurance_2 + len(packed_name) data_compressed = \ data[:occurance_2] + \ struct.pack('!H', (192 * 256) + occurance_2 + 4) + \ struct.pack('!H', (192 * 256) + occurance_2) + \ struct.pack('!H', (192 * 256) + occurance_2 + 2) + \ data[occurance_2_end:] sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(data_compressed, ('127.0.0.1', 53)) sock.close() tasks = [ asyncio.create_task(resolve('www.google.com', TYPES.A)) for _ in range(0, 100000) ] responses = await asyncio.gather(*tasks) for response in responses: self.assertEqual(type(response[0]), IPv4AddressExpiresAt)