]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/pythonmod/ubmodule-msg.py
1 # -*- coding: utf-8 -*-
3 ubmodule-msg.py: simple response packet logger
5 Authors: Zdenek Vasicek (vasicek AT fit.vutbr.cz)
6 Marek Vavrusa (xvavru00 AT stud.fit.vutbr.cz)
8 Copyright (c) 2008. All rights reserved.
10 This software is open source.
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions
16 Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
19 Redistributions in binary form must reproduce the above copyright notice,
20 this list of conditions and the following disclaimer in the documentation
21 and/or other materials provided with the distribution.
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 POSSIBILITY OF SUCH DAMAGE.
38 log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, cfg
.port
, cfg
.python_script
))
42 log_info("pythonmod: deinit called, module id is %d" % id)
45 def inform_super(id, qstate
, superqstate
, qdata
):
48 def setTTL(qstate
, ttl
):
49 """Sets return_msg TTL and all the RRs TTL"""
51 qstate
.return_msg
.rep
.ttl
= ttl
52 if (qstate
.return_msg
.rep
):
53 for i
in range(0,qstate
.return_msg
.rep
.rrset_count
):
54 d
= qstate
.return_msg
.rep
.rrsets
[i
].entry
.data
55 for j
in range(0,d
.count
+d
.rrsig_count
):
58 def dataHex(data
, prefix
=""):
60 for i
in range(0, (len(data
)+15)/16):
61 res
+= "%s0x%02X | " % (prefix
, i
*16)
62 d
= map(lambda x
:ord(x
), data
[i
*16:i
*16+17])
65 for i
in range(0,17-len(d
)):
69 if (ch
< 32) or (ch
> 127):
76 def printReturnMsg(qstate
):
77 print "Return MSG rep :: flags: %04X, QDcount: %d, Security:%d, TTL=%d" % (qstate
.return_msg
.rep
.flags
, qstate
.return_msg
.rep
.qdcount
,qstate
.return_msg
.rep
.security
, qstate
.return_msg
.rep
.ttl
)
78 print " qinfo :: qname:",qstate
.return_msg
.qinfo
.qname_list
, qstate
.return_msg
.qinfo
.qname_str
, "type:",qstate
.return_msg
.qinfo
.qtype_str
, "class:",qstate
.return_msg
.qinfo
.qclass_str
79 if (qstate
.return_msg
.rep
):
80 print "RRSets:",qstate
.return_msg
.rep
.rrset_count
82 for i
in range(0,qstate
.return_msg
.rep
.rrset_count
):
83 r
= qstate
.return_msg
.rep
.rrsets
[i
]
85 print i
,":",rk
.dname_list
, rk
.dname_str
, "flags: %04X" % rk
.flags
,
86 print "type:",rk
.type_str
,"(%d)" % ntohs(rk
.type), "class:",rk
.rrset_class_str
,"(%d)" % ntohs(rk
.rrset_class
)
89 print " RRDatas:",d
.count
+d
.rrsig_count
90 for j
in range(0,d
.count
+d
.rrsig_count
):
91 print " ",j
,":","TTL=",d
.rr_ttl
[j
],"RR data:"
92 print dataHex(d
.rr_data
[j
]," ")
95 def operate(id, event
, qstate
, qdata
):
96 log_info("pythonmod: operate called, id: %d, event:%s" % (id, strmodulevent(event
)))
97 #print "pythonmod: per query data", qdata
99 print "Query:", ''.join(map(lambda x
:chr(max(32,ord(x
))),qstate
.qinfo
.qname
)), qstate
.qinfo
.qname_list
, qstate
.qinfo
.qname_str
,
100 print "Type:",qstate
.qinfo
.qtype_str
,"(%d)" % qstate
.qinfo
.qtype
,
101 print "Class:",qstate
.qinfo
.qclass_str
,"(%d)" % qstate
.qinfo
.qclass
104 #if event == MODULE_EVENT_PASS: #pokud mame "validator python iterator"
105 if (event
== MODULE_EVENT_NEW
) and (qstate
.qinfo
.qname_str
.endswith(".seznam.cz.")): #pokud mame "python validator iterator"
106 print qstate
.qinfo
.qname_str
108 qstate
.ext_state
[id] = MODULE_FINISHED
110 msg
= DNSMessage(qstate
.qinfo
.qname_str
, RR_TYPE_A
, RR_CLASS_IN
, PKT_QR | PKT_RA | PKT_AA
) #, 300)
111 #msg.authority.append("xxx.seznam.cz. 10 IN A 192.168.1.1")
112 #msg.additional.append("yyy.seznam.cz. 10 IN A 1.1.1.2.")
114 if qstate
.qinfo
.qtype
== RR_TYPE_A
:
115 msg
.answer
.append("%s 10 IN A 192.168.1.1" % qstate
.qinfo
.qname_str
)
116 if (qstate
.qinfo
.qtype
== RR_TYPE_SRV
) or (qstate
.qinfo
.qtype
== RR_TYPE_ANY
):
117 msg
.answer
.append("%s 10 IN SRV 0 0 80 neinfo.example.com." % qstate
.qinfo
.qname_str
)
118 if (qstate
.qinfo
.qtype
== RR_TYPE_TXT
) or (qstate
.qinfo
.qtype
== RR_TYPE_ANY
):
119 msg
.answer
.append("%s 10 IN TXT path=/" % qstate
.qinfo
.qname_str
)
121 if not msg
.set_return_msg(qstate
):
122 qstate
.ext_state
[id] = MODULE_ERROR
125 #qstate.return_msg.rep.security = 2 #pokud nebude nasledovat validator, je zapotrebi nastavit security, aby nebyl paket zahozen v mesh_send_reply
126 printReturnMsg(qstate
)
128 #Authoritative result can't be stored in cache
129 #if (not storeQueryInCache(qstate, qstate.return_msg.qinfo, qstate.return_msg.rep, 0)):
130 # print "Can't store in cache"
131 # qstate.ext_state[id] = MODULE_ERROR
135 qstate
.return_rcode
= RCODE_NOERROR
138 if event
== MODULE_EVENT_NEW
:
139 qstate
.ext_state
[id] = MODULE_WAIT_MODULE
142 if event
== MODULE_EVENT_MODDONE
:
143 log_info("pythonmod: previous module done")
144 qstate
.ext_state
[id] = MODULE_FINISHED
147 if event
== MODULE_EVENT_PASS
:
148 log_info("pythonmod: event_pass")
149 qstate
.ext_state
[id] = MODULE_WAIT_MODULE
152 log_err("pythonmod: BAD event")
153 qstate
.ext_state
[id] = MODULE_ERROR
156 log_info("pythonmod: script loaded.")