xref: /netbsd-src/external/bsd/unbound/dist/pythonmod/examples/calc.py (revision 76c7fc5f6b13ed0b1508e6b313e88e59977ed78e)
1# -*- coding: utf-8 -*-
2'''
3 calc.py: DNS-based calculator
4
5 Copyright (c) 2009, Zdenek Vasicek (vasicek AT fit.vutbr.cz)
6                     Marek Vavrusa  (xvavru00 AT stud.fit.vutbr.cz)
7
8 This software is open source.
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
12 are met:
13
14    * Redistributions of source code must retain the above copyright notice,
15      this list of conditions and the following disclaimer.
16
17    * Redistributions in binary form must reproduce the above copyright notice,
18      this list of conditions and the following disclaimer in the documentation
19      and/or other materials provided with the distribution.
20
21    * Neither the name of the organization nor the names of its
22      contributors may be used to endorse or promote products derived from this
23      software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
29 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 POSSIBILITY OF SUCH DAMAGE.
36'''
37
38#Try: dig @localhost 1*25._calc_.cz.
39
40def init(id, cfg): return True
41def deinit(id): return True
42def inform_super(id, qstate, superqstate, qdata): return True
43
44def operate(id, event, qstate, qdata):
45
46    if (event == MODULE_EVENT_NEW) or (event == MODULE_EVENT_PASS):
47
48        if qstate.qinfo.qname_str.endswith("._calc_.cz."):
49            try:
50                res = eval(''.join(qstate.qinfo.qname_list[0:-3]))
51            except:
52                res = "exception"
53
54            msg = DNSMessage(qstate.qinfo.qname_str, RR_TYPE_TXT, RR_CLASS_IN, PKT_QR | PKT_RA | PKT_AA) #, 300)
55            msg.answer.append("%s 300 IN TXT \"%s\"" % (qstate.qinfo.qname_str,res))
56            if not msg.set_return_msg(qstate):
57               qstate.ext_state[id] = MODULE_ERROR
58               return True
59
60            qstate.return_rcode = RCODE_NOERROR
61            qstate.ext_state[id] = MODULE_FINISHED
62            return True
63
64        else:
65            #Pass on the unknown query to the iterator
66            qstate.ext_state[id] = MODULE_WAIT_MODULE
67            return True
68
69    elif event == MODULE_EVENT_MODDONE:
70        #the iterator has finished
71        qstate.ext_state[id] = MODULE_FINISHED
72        return True
73
74    log_err("pythonmod: Unknown event")
75    qstate.ext_state[id] = MODULE_ERROR
76    return True
77
78