query_two_server.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/python3
  2. # Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  3. #
  4. # Permission to use, copy, modify, and/or distribute this software for any
  5. # purpose with or without fee is hereby granted, provided that the above
  6. # copyright notice and this permission notice appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. # PERFORMANCE OF THIS SOFTWARE.
  15. import sys; sys.path.append('lib')
  16. from optparse import OptionParser
  17. from read_query import *
  18. import handledns
  19. import compare_rrset
  20. def getopt():
  21. """
  22. get options from user.
  23. """
  24. usage = "usage: %prog -f <file> -s <svr1> [-p <port1>] -t <svr2> [-q <port2>] [-e] [-u] [--bufsize] [--edns]"
  25. parser = OptionParser(usage)
  26. parser.add_option("-f", "--file", dest="filename",
  27. help="specify the input data filename")
  28. parser.add_option("-s", "--svr1", dest="server1",
  29. help="specify the tested DNS server 1")
  30. parser.add_option("-p",
  31. help="specify the port of the tested DNS server 1, default is 53")
  32. parser.add_option("-t", "--svr2", dest="server2",
  33. help="specify the tested DNS server 2")
  34. parser.add_option("-q",
  35. help="specify the port of the tested DNS server 2, default is 53")
  36. parser.add_option("-e", "--dnssec", action="store_true",
  37. default=False, help="turn on dnssec")
  38. parser.add_option("", "--edns", action="store_true",
  39. default=False, help="turn on edns, if -e is set, --edns will lapse, it must be True")
  40. parser.add_option("-u", "--udp", action="store_true", default=False,
  41. help="if set, query by udp, otherwise by tcp, default is unset")
  42. parser.add_option("", "--bufsize", default=4096,
  43. help="if --edns is set, --bufsize specifies payload of edns0, default is 4096")
  44. (options, args) = parser.parse_args()
  45. if(options.filename == None or options.server1 == None or
  46. options.server2 == None):
  47. parser.print_help()
  48. sys.exit(1)
  49. return options
  50. def main():
  51. opts = getopt()
  52. qlist = []
  53. read_query(opts.filename, qlist)
  54. for q in qlist:
  55. # initial query
  56. query = {}
  57. if opts.dnssec:
  58. query['edns'] = 1
  59. query['dnssec'] = 1
  60. elif opts.edns:
  61. query['edns'] = 1
  62. query['dnssec'] = 0
  63. else:
  64. query['edns'] = 0
  65. query['dnssec'] = 0
  66. if opts.udp:
  67. query['protocol'] = 'udp'
  68. else:
  69. query['protocol'] = 'tcp'
  70. query['payload'] = opts.bufsize
  71. query['qname'] = q['question'][0]['qname']
  72. query['qtype'] = q['question'][0]['qtype']
  73. query['qclass'] = q['question'][0]['qclass']
  74. query['header'] = q['header']
  75. id = q['header']['id']
  76. # send the query to the 1st and 2nd server, and store the
  77. # response in res1 and res2
  78. res1 = handledns.send_req(query, opts.server1, opts.p)
  79. res2 = handledns.send_req(query, opts.server2, opts.q)
  80. if res1 != None and res2 != None:
  81. # compare res1 and res2, print the different part.
  82. res3 = compare_rrset.resp_casecmp(res1, res2, id)
  83. else:
  84. sys.stderr.write('Empty response.\n')
  85. if __name__ == "__main__":
  86. main()