Home | History | Annotate | Line # | Download | only in ans2
      1 """
      2 Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      3 
      4 SPDX-License-Identifier: MPL-2.0
      5 
      6 This Source Code Form is subject to the terms of the Mozilla Public
      7 License, v. 2.0.  If a copy of the MPL was not distributed with this
      8 file, you can obtain one at https://mozilla.org/MPL/2.0/.
      9 
     10 See the COPYRIGHT file distributed with this work for additional
     11 information regarding copyright ownership.
     12 """
     13 
     14 from typing import AsyncGenerator
     15 
     16 import dns.flags
     17 
     18 from isctest.asyncserver import (
     19     AsyncDnsServer,
     20     BytesResponseSend,
     21     QueryContext,
     22     ResponseHandler,
     23 )
     24 
     25 
     26 class TruncatedWithLastByteDroppedHandler(ResponseHandler):
     27     """
     28     Return a TC=1 response with the final byte removed to make
     29     dns_message_parse() return ISC_R_UNEXPECTEDEND.
     30     """
     31 
     32     async def get_responses(
     33         self, qctx: QueryContext
     34     ) -> AsyncGenerator[BytesResponseSend, None]:
     35         tc_response = qctx.query
     36         tc_response.flags |= dns.flags.QR
     37         tc_response.flags |= dns.flags.TC
     38         tc_response.flags |= dns.flags.RA
     39         yield BytesResponseSend(tc_response.to_wire()[:-1])
     40 
     41 
     42 def main() -> None:
     43     server = AsyncDnsServer(keyring=None)
     44     server.install_response_handler(TruncatedWithLastByteDroppedHandler())
     45     server.run()
     46 
     47 
     48 if __name__ == "__main__":
     49     main()
     50