1*3b6c3722Schristos''' 2*3b6c3722Schristos resmod.py: This example shows how to modify the response from iterator 3*3b6c3722Schristos 4*3b6c3722Schristos Copyright (c) 2009, Zdenek Vasicek (vasicek AT fit.vutbr.cz) 5*3b6c3722Schristos Marek Vavrusa (xvavru00 AT stud.fit.vutbr.cz) 6*3b6c3722Schristos 7*3b6c3722Schristos This software is open source. 8*3b6c3722Schristos 9*3b6c3722Schristos Redistribution and use in source and binary forms, with or without 10*3b6c3722Schristos modification, are permitted provided that the following conditions 11*3b6c3722Schristos are met: 12*3b6c3722Schristos 13*3b6c3722Schristos * Redistributions of source code must retain the above copyright notice, 14*3b6c3722Schristos this list of conditions and the following disclaimer. 15*3b6c3722Schristos 16*3b6c3722Schristos * Redistributions in binary form must reproduce the above copyright notice, 17*3b6c3722Schristos this list of conditions and the following disclaimer in the documentation 18*3b6c3722Schristos and/or other materials provided with the distribution. 19*3b6c3722Schristos 20*3b6c3722Schristos * Neither the name of the organization nor the names of its 21*3b6c3722Schristos contributors may be used to endorse or promote products derived from this 22*3b6c3722Schristos software without specific prior written permission. 23*3b6c3722Schristos 24*3b6c3722Schristos THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25*3b6c3722Schristos "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 26*3b6c3722Schristos TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 27*3b6c3722Schristos PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 28*3b6c3722Schristos LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29*3b6c3722Schristos CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30*3b6c3722Schristos SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31*3b6c3722Schristos INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32*3b6c3722Schristos CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33*3b6c3722Schristos ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34*3b6c3722Schristos POSSIBILITY OF SUCH DAMAGE. 35*3b6c3722Schristos''' 36*3b6c3722Schristos 37*3b6c3722Schristosdef init(id, cfg): return True 38*3b6c3722Schristos 39*3b6c3722Schristosdef deinit(id): return True 40*3b6c3722Schristos 41*3b6c3722Schristosdef inform_super(id, qstate, superqstate, qdata): return True 42*3b6c3722Schristos 43*3b6c3722Schristosdef setTTL(qstate, ttl): 44*3b6c3722Schristos """Updates return_msg TTL and the TTL of all the RRs""" 45*3b6c3722Schristos if qstate.return_msg: 46*3b6c3722Schristos qstate.return_msg.rep.ttl = ttl 47*3b6c3722Schristos if (qstate.return_msg.rep): 48*3b6c3722Schristos for i in range(0,qstate.return_msg.rep.rrset_count): 49*3b6c3722Schristos d = qstate.return_msg.rep.rrsets[i].entry.data 50*3b6c3722Schristos for j in range(0,d.count+d.rrsig_count): 51*3b6c3722Schristos d.rr_ttl[j] = ttl 52*3b6c3722Schristos 53*3b6c3722Schristosdef operate(id, event, qstate, qdata): 54*3b6c3722Schristos if (event == MODULE_EVENT_NEW) or (event == MODULE_EVENT_PASS): 55*3b6c3722Schristos #pass the query to validator 56*3b6c3722Schristos qstate.ext_state[id] = MODULE_WAIT_MODULE 57*3b6c3722Schristos return True 58*3b6c3722Schristos 59*3b6c3722Schristos if event == MODULE_EVENT_MODDONE: 60*3b6c3722Schristos log_info("pythonmod: iterator module done") 61*3b6c3722Schristos 62*3b6c3722Schristos if not qstate.return_msg: 63*3b6c3722Schristos qstate.ext_state[id] = MODULE_FINISHED 64*3b6c3722Schristos return True 65*3b6c3722Schristos 66*3b6c3722Schristos #modify the response 67*3b6c3722Schristos 68*3b6c3722Schristos qdn = qstate.qinfo.qname_str 69*3b6c3722Schristos if qdn.endswith(".nic.cz."): 70*3b6c3722Schristos #invalidate response in cache added by iterator 71*3b6c3722Schristos #invalidateQueryInCache(qstate, qstate.return_msg.qinfo) 72*3b6c3722Schristos 73*3b6c3722Schristos #modify TTL to 10 secs and store response in cache 74*3b6c3722Schristos #setTTL(qstate, 5) 75*3b6c3722Schristos #if not storeQueryInCache(qstate, qstate.return_msg.qinfo, qstate.return_msg.rep, 0): 76*3b6c3722Schristos # qstate.ext_state[id] = MODULE_ERROR 77*3b6c3722Schristos # return False 78*3b6c3722Schristos 79*3b6c3722Schristos #modify TTL of response, which will be send to a) validator and then b) client 80*3b6c3722Schristos setTTL(qstate, 10) 81*3b6c3722Schristos qstate.return_rcode = RCODE_NOERROR 82*3b6c3722Schristos 83*3b6c3722Schristos qstate.ext_state[id] = MODULE_FINISHED 84*3b6c3722Schristos return True 85*3b6c3722Schristos 86*3b6c3722Schristos log_err("pythonmod: bad event") 87*3b6c3722Schristos qstate.ext_state[id] = MODULE_ERROR 88*3b6c3722Schristos return True 89