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