1import os 2''' 3 calc.py: Response packet logger 4 5 Copyright (c) 2009, Zdenek Vasicek (vasicek AT fit.vutbr.cz) 6 Marek Vavrusa (xvavru00 AT stud.fit.vutbr.cz) 7 8 This software is open source. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 * Redistributions of source code must retain the above copyright notice, 15 this list of conditions and the following disclaimer. 16 17 * Redistributions in binary form must reproduce the above copyright notice, 18 this list of conditions and the following disclaimer in the documentation 19 and/or other materials provided with the distribution. 20 21 * Neither the name of the organization nor the names of its 22 contributors may be used to endorse or promote products derived from this 23 software without specific prior written permission. 24 25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 29 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 POSSIBILITY OF SUCH DAMAGE. 36''' 37 38def dataHex(data, prefix=""): 39 """Converts binary string data to display representation form""" 40 res = "" 41 for i in range(0, (len(data)+15)/16): 42 res += "%s0x%02X | " % (prefix, i*16) 43 d = map(lambda x:ord(x), data[i*16:i*16+17]) 44 for ch in d: 45 res += "%02X " % ch 46 for i in range(0,17-len(d)): 47 res += " " 48 res += "| " 49 for ch in d: 50 if (ch < 32) or (ch > 127): 51 res += ". " 52 else: 53 res += "%c " % ch 54 res += "\n" 55 return res 56 57def logDnsMsg(qstate): 58 """Logs response""" 59 60 r = qstate.return_msg.rep 61 q = qstate.return_msg.qinfo 62 63 print "-"*100 64 print("Query: %s, type: %s (%d), class: %s (%d) " % ( 65 qstate.qinfo.qname_str, qstate.qinfo.qtype_str, qstate.qinfo.qtype, 66 qstate.qinfo.qclass_str, qstate.qinfo.qclass)) 67 print "-"*100 68 print "Return reply :: flags: %04X, QDcount: %d, Security:%d, TTL=%d" % (r.flags, r.qdcount, r.security, r.ttl) 69 print " qinfo :: qname: %s %s, qtype: %s, qclass: %s" % (str(q.qname_list), q.qname_str, q.qtype_str, q.qclass_str) 70 71 if (r): 72 print "Reply:" 73 for i in range(0, r.rrset_count): 74 rr = r.rrsets[i] 75 76 rk = rr.rk 77 print i,":",rk.dname_list, rk.dname_str, "flags: %04X" % rk.flags, 78 print "type:",rk.type_str,"(%d)" % ntohs(rk.type), "class:",rk.rrset_class_str,"(%d)" % ntohs(rk.rrset_class) 79 80 d = rr.entry.data 81 for j in range(0,d.count+d.rrsig_count): 82 print " ",j,":","TTL=",d.rr_ttl[j], 83 if (j >= d.count): print "rrsig", 84 print 85 print dataHex(d.rr_data[j]," ") 86 87 print "-"*100 88 89def init(id, cfg): 90 log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, cfg.port, mod_env['script'])) 91 return True 92 93def deinit(id): 94 log_info("pythonmod: deinit called, module id is %d" % id) 95 return True 96 97def inform_super(id, qstate, superqstate, qdata): 98 return True 99 100def operate(id, event, qstate, qdata): 101 log_info("pythonmod: operate called, id: %d, event:%s" % (id, strmodulevent(event))) 102 103 if (event == MODULE_EVENT_NEW) or (event == MODULE_EVENT_PASS): 104 #Pass on the new event to the iterator 105 qstate.ext_state[id] = MODULE_WAIT_MODULE 106 return True 107 108 if event == MODULE_EVENT_MODDONE: 109 #Iterator finished, show response (if any) 110 111 if (qstate.return_msg): 112 logDnsMsg(qstate) 113 114 qstate.ext_state[id] = MODULE_FINISHED 115 return True 116 117 qstate.ext_state[id] = MODULE_ERROR 118 return True 119 120