]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/pythonmod/examples/calc.py
3230e37e3eea685b998ccde621682362252287f0
1 # -*- coding: utf-8 -*-
3 calc.py: DNS-based calculator
5 Copyright (c) 2009, Zdenek Vasicek (vasicek AT fit.vutbr.cz)
6 Marek Vavrusa (xvavru00 AT stud.fit.vutbr.cz)
8 This software is open source.
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
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.
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.
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.
38 #Try: dig @localhost 1*25._calc_.cz.
40 def init(id, cfg
): return True
41 def deinit(id): return True
42 def inform_super(id, qstate
, superqstate
, qdata
): return True
44 def operate(id, event
, qstate
, qdata
):
46 if (event
== MODULE_EVENT_NEW
) or (event
== MODULE_EVENT_PASS
):
48 if qstate
.qinfo
.qname_str
.endswith("._calc_.cz."):
50 res
= eval(''.join(qstate
.qinfo
.qname_list
[0:-3]))
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
60 qstate
.return_rcode
= RCODE_NOERROR
61 qstate
.ext_state
[id] = MODULE_FINISHED
65 #Pass on the unknown query to the iterator
66 qstate
.ext_state
[id] = MODULE_WAIT_MODULE
69 elif event
== MODULE_EVENT_MODDONE
:
70 #the iterator has finished
71 qstate
.ext_state
[id] = MODULE_FINISHED
74 log_err("pythonmod: Unknown event")
75 qstate
.ext_state
[id] = MODULE_ERROR