xref: /netbsd-src/external/bsd/unbound/dist/pythonmod/examples/log.py (revision 91f7d55fb697b5e0475da4718fa34c3a3ebeac85)
13b6c3722Schristosimport os
23b6c3722Schristos'''
33b6c3722Schristos calc.py: Response packet logger
43b6c3722Schristos
53b6c3722Schristos Copyright (c) 2009, Zdenek Vasicek (vasicek AT fit.vutbr.cz)
63b6c3722Schristos                     Marek Vavrusa  (xvavru00 AT stud.fit.vutbr.cz)
73b6c3722Schristos
83b6c3722Schristos This software is open source.
93b6c3722Schristos
103b6c3722Schristos Redistribution and use in source and binary forms, with or without
113b6c3722Schristos modification, are permitted provided that the following conditions
123b6c3722Schristos are met:
133b6c3722Schristos
143b6c3722Schristos    * Redistributions of source code must retain the above copyright notice,
153b6c3722Schristos      this list of conditions and the following disclaimer.
163b6c3722Schristos
173b6c3722Schristos    * Redistributions in binary form must reproduce the above copyright notice,
183b6c3722Schristos      this list of conditions and the following disclaimer in the documentation
193b6c3722Schristos      and/or other materials provided with the distribution.
203b6c3722Schristos
213b6c3722Schristos    * Neither the name of the organization nor the names of its
223b6c3722Schristos      contributors may be used to endorse or promote products derived from this
233b6c3722Schristos      software without specific prior written permission.
243b6c3722Schristos
253b6c3722Schristos THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
263b6c3722Schristos "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
273b6c3722Schristos TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
283b6c3722Schristos PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
293b6c3722Schristos LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
303b6c3722Schristos CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
313b6c3722Schristos SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
323b6c3722Schristos INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
333b6c3722Schristos CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
343b6c3722Schristos ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
353b6c3722Schristos POSSIBILITY OF SUCH DAMAGE.
363b6c3722Schristos'''
373b6c3722Schristos
383b6c3722Schristosdef dataHex(data, prefix=""):
393b6c3722Schristos    """Converts binary string data to display representation form"""
403b6c3722Schristos    res = ""
413b6c3722Schristos    for i in range(0, (len(data)+15)/16):
423b6c3722Schristos        res += "%s0x%02X | " % (prefix, i*16)
433b6c3722Schristos        d = map(lambda x:ord(x), data[i*16:i*16+17])
443b6c3722Schristos        for ch in d:
453b6c3722Schristos            res += "%02X " % ch
463b6c3722Schristos        for i in range(0,17-len(d)):
473b6c3722Schristos            res += "   "
483b6c3722Schristos        res += "| "
493b6c3722Schristos        for ch in d:
503b6c3722Schristos            if (ch < 32) or (ch > 127):
513b6c3722Schristos                res += ". "
523b6c3722Schristos            else:
533b6c3722Schristos                res += "%c " % ch
543b6c3722Schristos        res += "\n"
553b6c3722Schristos    return res
563b6c3722Schristos
573b6c3722Schristosdef logDnsMsg(qstate):
583b6c3722Schristos    """Logs response"""
593b6c3722Schristos
603b6c3722Schristos    r  = qstate.return_msg.rep
613b6c3722Schristos    q  = qstate.return_msg.qinfo
623b6c3722Schristos
633b6c3722Schristos    print "-"*100
643b6c3722Schristos    print("Query: %s, type: %s (%d), class: %s (%d) " % (
653b6c3722Schristos            qstate.qinfo.qname_str, qstate.qinfo.qtype_str, qstate.qinfo.qtype,
663b6c3722Schristos            qstate.qinfo.qclass_str, qstate.qinfo.qclass))
673b6c3722Schristos    print "-"*100
683b6c3722Schristos    print "Return    reply :: flags: %04X, QDcount: %d, Security:%d, TTL=%d" % (r.flags, r.qdcount, r.security, r.ttl)
693b6c3722Schristos    print "          qinfo :: qname: %s %s, qtype: %s, qclass: %s" % (str(q.qname_list), q.qname_str, q.qtype_str, q.qclass_str)
703b6c3722Schristos
713b6c3722Schristos    if (r):
723b6c3722Schristos        print "Reply:"
733b6c3722Schristos        for i in range(0, r.rrset_count):
743b6c3722Schristos            rr = r.rrsets[i]
753b6c3722Schristos
763b6c3722Schristos            rk = rr.rk
773b6c3722Schristos            print i,":",rk.dname_list, rk.dname_str, "flags: %04X" % rk.flags,
783b6c3722Schristos            print "type:",rk.type_str,"(%d)" % ntohs(rk.type), "class:",rk.rrset_class_str,"(%d)" % ntohs(rk.rrset_class)
793b6c3722Schristos
803b6c3722Schristos            d = rr.entry.data
813b6c3722Schristos            for j in range(0,d.count+d.rrsig_count):
823b6c3722Schristos                print "  ",j,":","TTL=",d.rr_ttl[j],
833b6c3722Schristos                if (j >= d.count): print "rrsig",
843b6c3722Schristos                print
853b6c3722Schristos                print dataHex(d.rr_data[j],"       ")
863b6c3722Schristos
873b6c3722Schristos    print "-"*100
883b6c3722Schristos
893b6c3722Schristosdef init(id, cfg):
90*91f7d55fSchristos   log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, cfg.port, mod_env['script']))
913b6c3722Schristos   return True
923b6c3722Schristos
933b6c3722Schristosdef deinit(id):
943b6c3722Schristos   log_info("pythonmod: deinit called, module id is %d" % id)
953b6c3722Schristos   return True
963b6c3722Schristos
973b6c3722Schristosdef inform_super(id, qstate, superqstate, qdata):
983b6c3722Schristos   return True
993b6c3722Schristos
1003b6c3722Schristosdef operate(id, event, qstate, qdata):
1013b6c3722Schristos   log_info("pythonmod: operate called, id: %d, event:%s" % (id, strmodulevent(event)))
1023b6c3722Schristos
1033b6c3722Schristos   if (event == MODULE_EVENT_NEW) or (event == MODULE_EVENT_PASS):
1043b6c3722Schristos      #Pass on the new event to the iterator
1053b6c3722Schristos      qstate.ext_state[id] = MODULE_WAIT_MODULE
1063b6c3722Schristos      return True
1073b6c3722Schristos
1083b6c3722Schristos   if event == MODULE_EVENT_MODDONE:
1093b6c3722Schristos      #Iterator finished, show response (if any)
1103b6c3722Schristos
1113b6c3722Schristos      if (qstate.return_msg):
1123b6c3722Schristos          logDnsMsg(qstate)
1133b6c3722Schristos
1143b6c3722Schristos      qstate.ext_state[id] = MODULE_FINISHED
1153b6c3722Schristos      return True
1163b6c3722Schristos
1173b6c3722Schristos   qstate.ext_state[id] = MODULE_ERROR
1183b6c3722Schristos   return True
1193b6c3722Schristos
120