1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Catalyst.Net Ltd 2016
3 # Catalyst.Net's contribution was written by Douglas Bagnall
4 # <douglas.bagnall@catalyst.net.nz> and Garming Sam <garming@catalyst.net.nz>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # Based on the EchoServer example from python docs
23 from threading
import Timer
24 from samba
.dcerpc
import dns
25 import samba
.ndr
as ndr
34 print "\033[00;36m%s\033[00m" % msg
40 def answer_question(data
, question
):
42 r
.name
= question
.name
43 r
.rr_type
= dns
.DNS_QTYPE_CNAME
44 r
.rr_class
= dns
.DNS_QCLASS_IN
51 class DnsHandler(SocketServer
.BaseRequestHandler
):
52 def make_answer(self
, data
):
53 data
= ndr
.ndr_unpack(dns
.name_packet
, data
)
55 debug('answering this question:')
56 debug(data
.__ndr
_print
__())
58 answer
= answer_question(data
, data
.questions
[0])
59 if answer
is not None:
60 data
.answers
= [answer
] * 1
62 debug('the answer was: ')
63 debug(data
.__ndr
_print
__())
65 data
.operation |
= dns
.DNS_FLAG_REPLY
67 return ndr
.ndr_pack(data
)
69 def really_handle(self
, data
, socket
):
70 answer
= self
.make_answer(data
)
71 socket
.sendto(answer
, self
.client_address
)
74 data
, socket
= self
.request
75 debug("%s: %s wrote:" % (SERVER_ID
, self
.client_address
[0]))
78 m
= re
.match('^timeout\s+([\d.]+)$', data
.strip())
80 timeout
= float(m
.group(1))
81 debug("timing out at %s" % timeout
)
84 t
= Timer(timeout
, self
.really_handle
, [data
, socket
])
89 host
, port
, SERVER_ID
= sys
.argv
[1:]
90 server
= SocketServer
.UDPServer((host
, int(port
)), DnsHandler
)
91 server
.serve_forever()