]> git.saurik.com Git - apple/network_cmds.git/blob - unbound/pythonmod/ubmodule-msg.py
network_cmds-596.100.2.tar.gz
[apple/network_cmds.git] / unbound / pythonmod / ubmodule-msg.py
1 # -*- coding: utf-8 -*-
2 '''
3 ubmodule-msg.py: simple response packet logger
4
5 Authors: Zdenek Vasicek (vasicek AT fit.vutbr.cz)
6 Marek Vavrusa (xvavru00 AT stud.fit.vutbr.cz)
7
8 Copyright (c) 2008. All rights reserved.
9
10 This software is open source.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions
14 are met:
15
16 Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
18
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.
22
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.
34 '''
35 import os
36
37 def init(id, cfg):
38 log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, cfg.port, cfg.python_script))
39 return True
40
41 def deinit(id):
42 log_info("pythonmod: deinit called, module id is %d" % id)
43 return True
44
45 def inform_super(id, qstate, superqstate, qdata):
46 return True
47
48 def setTTL(qstate, ttl):
49 """Sets return_msg TTL and all the RRs TTL"""
50 if qstate.return_msg:
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):
56 d.rr_ttl[j] = ttl
57
58 def dataHex(data, prefix=""):
59 res = ""
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])
63 for ch in d:
64 res += "%02X " % ch
65 for i in range(0,17-len(d)):
66 res += " "
67 res += "| "
68 for ch in d:
69 if (ch < 32) or (ch > 127):
70 res += ". "
71 else:
72 res += "%c " % ch
73 res += "\n"
74 return res
75
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
81 prevkey = None
82 for i in range(0,qstate.return_msg.rep.rrset_count):
83 r = qstate.return_msg.rep.rrsets[i]
84 rk = r.rk
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)
87
88 d = r.entry.data
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]," ")
93
94
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
98
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
102 print
103
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
107
108 qstate.ext_state[id] = MODULE_FINISHED
109
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.")
113
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)
120
121 if not msg.set_return_msg(qstate):
122 qstate.ext_state[id] = MODULE_ERROR
123 return True
124
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)
127
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
132 # return False
133 #print "Store OK"
134
135 qstate.return_rcode = RCODE_NOERROR
136 return True
137
138 if event == MODULE_EVENT_NEW:
139 qstate.ext_state[id] = MODULE_WAIT_MODULE
140 return True
141
142 if event == MODULE_EVENT_MODDONE:
143 log_info("pythonmod: previous module done")
144 qstate.ext_state[id] = MODULE_FINISHED
145 return True
146
147 if event == MODULE_EVENT_PASS:
148 log_info("pythonmod: event_pass")
149 qstate.ext_state[id] = MODULE_WAIT_MODULE
150 return True
151
152 log_err("pythonmod: BAD event")
153 qstate.ext_state[id] = MODULE_ERROR
154 return True
155
156 log_info("pythonmod: script loaded.")