notify_out.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. # Copyright (C) 2010 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. import select
  16. import sys
  17. import random
  18. import socket
  19. import threading
  20. import time
  21. from isc.datasrc import sqlite3_ds
  22. import isc
  23. try:
  24. from pydnspp import *
  25. except ImportError as e:
  26. # C++ loadable module may not be installed;
  27. sys.stderr.write('[b10-xfrout] failed to import DNS or XFR module: %s\n' % str(e))
  28. ZONE_NEW_DATA_READY_CMD = 'zone_new_data_ready'
  29. _MAX_NOTIFY_NUM = 30
  30. _MAX_NOTIFY_TRY_NUM = 5
  31. _EVENT_NONE = 0
  32. _EVENT_READ = 1
  33. _EVENT_TIMEOUT = 2
  34. _NOTIFY_TIMEOUT = 1
  35. _IDLE_SLEEP_TIME = 0.5
  36. # define the rcode for parsing notify reply message
  37. _REPLY_OK = 0
  38. _BAD_QUERY_ID = 1
  39. _BAD_QUERY_NAME = 2
  40. _BAD_OPCODE = 3
  41. _BAD_QR = 4
  42. _BAD_REPLY_PACKET = 5
  43. SOCK_DATA = b'somedata'
  44. def addr_to_str(addr):
  45. return '%s#%s' % (addr[0], addr[1])
  46. class ZoneNotifyInfo:
  47. '''This class keeps track of notify-out information for one zone.'''
  48. def __init__(self, zone_name_, class_):
  49. '''notify_timeout_: absolute time for next notify reply. when the zone
  50. is preparing for sending notify message, notify_timeout_ is set to now,
  51. that means the first sending is triggered by the 'Timeout' mechanism.
  52. '''
  53. self._notify_current = None
  54. self._slave_index = 0
  55. self._sock = None
  56. self.notify_slaves = []
  57. self.zone_name = zone_name_
  58. self.zone_class = class_
  59. self.notify_msg_id = 0
  60. self.notify_timeout = 0
  61. self.notify_try_num = 0 #Notify times sending to one target.
  62. def set_next_notify_target(self):
  63. if self._slave_index < (len(self.notify_slaves) - 1):
  64. self._slave_index += 1
  65. self._notify_current = self.notify_slaves[self._slave_index]
  66. else:
  67. self._notify_current = None
  68. def prepare_notify_out(self):
  69. '''Create the socket and set notify timeout time to now'''
  70. self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #TODO support IPv6?
  71. self.notify_timeout = time.time()
  72. self.notify_try_num = 0
  73. self._slave_index = 0
  74. if len(self.notify_slaves) > 0:
  75. self._notify_current = self.notify_slaves[0]
  76. def finish_notify_out(self):
  77. if self._sock:
  78. self._sock.close()
  79. self._sock = None
  80. def get_socket(self):
  81. return self._sock
  82. def get_current_notify_target(self):
  83. return self._notify_current
  84. class NotifyOut:
  85. '''This class is used to handle notify logic for all zones(sending
  86. notify message to its slaves). notify service can be started by
  87. calling dispatcher(), and it can be stoped by calling shutdown()
  88. in another thread. '''
  89. def __init__(self, datasrc_file, log=None, verbose=True):
  90. self._notify_infos = {} # key is (zone_name, zone_class)
  91. self._waiting_zones = []
  92. self._notifying_zones = []
  93. self._log = log
  94. self._serving = False
  95. self._read_sock = None
  96. self.notify_num = 0 # the count of in progress notifies
  97. self._verbose = verbose
  98. self._lock = threading.Lock()
  99. self._db_file = datasrc_file
  100. self._init_notify_out(datasrc_file)
  101. def _init_notify_out(self, datasrc_file):
  102. '''Get all the zones name and its notify target's address
  103. TODO, currently the zones are got by going through the zone
  104. table in database. There should be a better way to get them
  105. and also the setting 'also_notify', and there should be one
  106. mechanism to cover the changed datasrc.'''
  107. self._db_file = datasrc_file
  108. for zone_name, zone_class in sqlite3_ds.get_zones_info(datasrc_file):
  109. zone_id = (zone_name, zone_class)
  110. self._notify_infos[zone_id] = ZoneNotifyInfo(zone_name, zone_class)
  111. slaves = self._get_notify_slaves_from_ns(zone_name)
  112. for item in slaves:
  113. self._notify_infos[zone_id].notify_slaves.append((item, 53))
  114. def send_notify(self, zone_name, zone_class='IN'):
  115. '''Send notify to one zone's slaves, this function is
  116. the only interface for class NotifyOut which can be called
  117. by other object.
  118. Internally, the function only set the zone's notify-reply
  119. timeout to now, then notify message will be sent out. '''
  120. if zone_name[len(zone_name) - 1] != '.':
  121. zone_name += '.'
  122. zone_id = (zone_name, zone_class)
  123. if zone_id not in self._notify_infos:
  124. return
  125. with self._lock:
  126. if (self.notify_num >= _MAX_NOTIFY_NUM) or (zone_id in self._notifying_zones):
  127. if zone_id not in self._waiting_zones:
  128. self._waiting_zones.append(zone_id)
  129. else:
  130. self._notify_infos[zone_id].prepare_notify_out()
  131. self.notify_num += 1
  132. self._notifying_zones.append(zone_id)
  133. def _dispatcher(self, started_event):
  134. while self._serving:
  135. # Let the master know we are alive already
  136. if started_event:
  137. started_event.set()
  138. replied_zones, not_replied_zones = self._wait_for_notify_reply()
  139. for name_ in replied_zones:
  140. self._zone_notify_handler(replied_zones[name_], _EVENT_READ)
  141. for name_ in not_replied_zones:
  142. if not_replied_zones[name_].notify_timeout <= time.time():
  143. self._zone_notify_handler(not_replied_zones[name_], _EVENT_TIMEOUT)
  144. def dispatcher(self, daemon=False):
  145. """Spawns a thread that will handle notify related events.
  146. If one zone get the notify reply before timeout, call the
  147. handle to process the reply. If one zone can't get the notify
  148. before timeout, call the handler to resend notify or notify
  149. next slave.
  150. The thread can be stopped by calling shutdown().
  151. Returns the thread object to anyone interested.
  152. """
  153. if self._serving:
  154. raise RuntimeError(
  155. 'Dispatcher already running, tried to start twice')
  156. # Prepare for launch
  157. self._serving = True
  158. self._read_sock, self._write_sock = socket.socketpair()
  159. started_event = threading.Event()
  160. # Start
  161. self._thread = threading.Thread(target=self._dispatcher,
  162. args=[started_event])
  163. if daemon:
  164. self._thread.daemon = daemon
  165. self._thread.start()
  166. # Wait for it to get started
  167. started_event.wait()
  168. # Return it to anyone listening
  169. return self._thread
  170. def shutdown(self):
  171. """Stop the dispatcher() thread. Blocks until the thread stopped."""
  172. if not self._serving:
  173. raise RuntimeError('Tried to stop while not running')
  174. # Ask it to stop
  175. self._serving = False
  176. self._write_sock.send(SOCK_DATA) # make self._read_sock be readable.
  177. # Wait for it
  178. self._thread.join()
  179. # Clean up
  180. self._write_sock = None
  181. self._read_sock = None
  182. self._thread = None
  183. def _get_rdata_data(self, rr):
  184. return rr[7].strip()
  185. def _get_notify_slaves_from_ns(self, zone_name):
  186. '''Get all NS records, then remove the primary master from ns rrset,
  187. then use the name in NS record rdata part to get the a/aaaa records
  188. in the same zone. the targets listed in a/aaaa record rdata are treated
  189. as the notify slaves.
  190. Note: this is the simplest way to get the address of slaves,
  191. but not correct, it can't handle the delegation slaves, or the CNAME
  192. and DNAME logic.
  193. TODO. the function should be provided by one library.'''
  194. ns_rrset = sqlite3_ds.get_zone_rrset(zone_name, zone_name, 'NS', self._db_file)
  195. soa_rrset = sqlite3_ds.get_zone_rrset(zone_name, zone_name, 'SOA', self._db_file)
  196. ns_rr_name = []
  197. for ns in ns_rrset:
  198. ns_rr_name.append(self._get_rdata_data(ns))
  199. if len(soa_rrset) > 0:
  200. sname = (soa_rrset[0][sqlite3_ds.RR_RDATA_INDEX].split(' '))[0].strip() #TODO, bad hardcode to get rdata part
  201. if sname in ns_rr_name:
  202. ns_rr_name.remove(sname)
  203. addr_list = []
  204. for rr_name in ns_rr_name:
  205. a_rrset = sqlite3_ds.get_zone_rrset(zone_name, rr_name, 'A', self._db_file)
  206. aaaa_rrset = sqlite3_ds.get_zone_rrset(zone_name, rr_name, 'AAAA', self._db_file)
  207. for rr in a_rrset:
  208. addr_list.append(self._get_rdata_data(rr))
  209. for rr in aaaa_rrset:
  210. addr_list.append(self._get_rdata_data(rr))
  211. return addr_list
  212. def _prepare_select_info(self):
  213. '''
  214. Prepare the information for select(), returned
  215. value is one tuple
  216. (block_timeout, valid_socks, notifying_zones)
  217. block_timeout: the timeout for select()
  218. valid_socks: sockets list for waiting ready reading.
  219. notifying_zones: the zones which have been triggered
  220. for notify.
  221. '''
  222. valid_socks = []
  223. notifying_zones = {}
  224. min_timeout = None
  225. for info in self._notify_infos:
  226. sock = self._notify_infos[info].get_socket()
  227. if sock:
  228. valid_socks.append(sock)
  229. notifying_zones[info] = self._notify_infos[info]
  230. tmp_timeout = self._notify_infos[info].notify_timeout
  231. if min_timeout is not None:
  232. if tmp_timeout < min_timeout:
  233. min_timeout = tmp_timeout
  234. else:
  235. min_timeout = tmp_timeout
  236. block_timeout = _IDLE_SLEEP_TIME
  237. if min_timeout is not None:
  238. block_timeout = min_timeout - time.time()
  239. if block_timeout < 0:
  240. block_timeout = 0
  241. return (block_timeout, valid_socks, notifying_zones)
  242. def _wait_for_notify_reply(self):
  243. '''
  244. Receive notify replies in specified time. returned value
  245. is one tuple:(replied_zones, not_replied_zones). ({}, {}) is
  246. returned if shutdown() was called.
  247. replied_zones: the zones which receive notify reply.
  248. not_replied_zones: the zones which haven't got notify reply.
  249. '''
  250. (block_timeout, valid_socks, notifying_zones) = \
  251. self._prepare_select_info()
  252. # This is None only during some tests
  253. if self._read_sock is not None:
  254. valid_socks.append(self._read_sock)
  255. try:
  256. r_fds, w, e = select.select(valid_socks, [], [], block_timeout)
  257. except select.error as err:
  258. if err.args[0] != EINTR:
  259. return {}, {}
  260. if (self._read_sock in r_fds) and \
  261. (self._read_sock.recv(len(SOCK_DATA)) == SOCK_DATA):
  262. return {}, {} # user has called shutdown()
  263. not_replied_zones = {}
  264. replied_zones = {}
  265. for info in notifying_zones:
  266. if notifying_zones[info].get_socket() in r_fds:
  267. replied_zones[info] = notifying_zones[info]
  268. else:
  269. not_replied_zones[info] = notifying_zones[info]
  270. return replied_zones, not_replied_zones
  271. def _zone_notify_handler(self, zone_notify_info, event_type):
  272. '''Notify handler for one zone. The first notify message is
  273. always triggered by the event "_EVENT_TIMEOUT" since when
  274. one zone prepares to notify its slaves, its notify_timeout
  275. is set to now, which is used to trigger sending notify
  276. message when dispatcher() scanning zones. '''
  277. tgt = zone_notify_info.get_current_notify_target()
  278. if event_type == _EVENT_READ:
  279. reply = self._get_notify_reply(zone_notify_info.get_socket(), tgt)
  280. if reply:
  281. if self._handle_notify_reply(zone_notify_info, reply):
  282. self._notify_next_target(zone_notify_info)
  283. elif event_type == _EVENT_TIMEOUT and zone_notify_info.notify_try_num > 0:
  284. self._log_msg('info', 'notify retry to %s' % addr_to_str(tgt))
  285. tgt = zone_notify_info.get_current_notify_target()
  286. if tgt:
  287. zone_notify_info.notify_try_num += 1
  288. if zone_notify_info.notify_try_num > _MAX_NOTIFY_TRY_NUM:
  289. self._log_msg('info', 'notify to %s: retried exceeded' % addr_to_str(tgt))
  290. self._notify_next_target(zone_notify_info)
  291. else:
  292. retry_timeout = _NOTIFY_TIMEOUT * pow(2, zone_notify_info.notify_try_num)
  293. # set exponential backoff according rfc1996 section 3.6
  294. zone_notify_info.notify_timeout = time.time() + retry_timeout
  295. self._send_notify_message_udp(zone_notify_info, tgt)
  296. def _notify_next_target(self, zone_notify_info):
  297. '''Notify next address for the same zone. If all the targets
  298. has been notified, notify the first zone in waiting list. '''
  299. zone_notify_info.notify_try_num = 0
  300. zone_notify_info.set_next_notify_target()
  301. tgt = zone_notify_info.get_current_notify_target()
  302. if not tgt:
  303. zone_notify_info.finish_notify_out()
  304. with self._lock:
  305. self.notify_num -= 1
  306. self._notifying_zones.remove((zone_notify_info.zone_name,
  307. zone_notify_info.zone_class))
  308. # trigger notify out for waiting zones
  309. if len(self._waiting_zones) > 0:
  310. zone_id = self._waiting_zones.pop(0)
  311. self._notify_infos[zone_id].prepare_notify_out()
  312. self.notify_num += 1
  313. def _send_notify_message_udp(self, zone_notify_info, addrinfo):
  314. msg, qid = self._create_notify_message(zone_notify_info.zone_name,
  315. zone_notify_info.zone_class)
  316. render = MessageRenderer()
  317. render.set_length_limit(512)
  318. msg.to_wire(render)
  319. zone_notify_info.notify_msg_id = qid
  320. sock = zone_notify_info.get_socket()
  321. try:
  322. sock.sendto(render.get_data(), 0, addrinfo)
  323. self._log_msg('info', 'sending notify to %s' % addr_to_str(addrinfo))
  324. except socket.error as err:
  325. self._log_msg('error', 'send notify to %s failed: %s' % (addr_to_str(addrinfo), str(err)))
  326. return False
  327. return True
  328. def _create_rrset_from_db_record(self, record, zone_class):
  329. '''Create one rrset from one record of datasource, if the schema of record is changed,
  330. This function should be updated first. TODO, the function is copied from xfrout, there
  331. should be library for creating one rrset. '''
  332. rrtype_ = RRType(record[sqlite3_ds.RR_TYPE_INDEX])
  333. rdata_ = Rdata(rrtype_, RRClass(zone_class), " ".join(record[sqlite3_ds.RR_RDATA_INDEX:]))
  334. rrset_ = RRset(Name(record[sqlite3_ds.RR_NAME_INDEX]), RRClass(zone_class), \
  335. rrtype_, RRTTL( int(record[sqlite3_ds.RR_TTL_INDEX])))
  336. rrset_.add_rdata(rdata_)
  337. return rrset_
  338. def _create_notify_message(self, zone_name, zone_class):
  339. msg = Message(Message.RENDER)
  340. qid = random.randint(0, 0xFFFF)
  341. msg.set_qid(qid)
  342. msg.set_opcode(Opcode.NOTIFY())
  343. msg.set_rcode(Rcode.NOERROR())
  344. msg.set_header_flag(MessageFlag.AA())
  345. question = Question(Name(zone_name), RRClass(zone_class), RRType('SOA'))
  346. msg.add_question(question)
  347. # Add soa record to answer section
  348. soa_record = sqlite3_ds.get_zone_rrset(zone_name, zone_name, 'SOA', self._db_file)
  349. rrset_soa = self._create_rrset_from_db_record(soa_record[0], zone_class)
  350. msg.add_rrset(Section.ANSWER(), rrset_soa)
  351. return msg, qid
  352. def _handle_notify_reply(self, zone_notify_info, msg_data):
  353. '''Parse the notify reply message.
  354. TODO, the error message should be refined properly.
  355. rcode will not checked here, If we get the response
  356. from the slave, it means the slaves has got the notify.'''
  357. msg = Message(Message.PARSE)
  358. try:
  359. errstr = 'notify reply error: '
  360. msg.from_wire(msg_data)
  361. if not msg.get_header_flag(MessageFlag.QR()):
  362. self._log_msg('error', errstr + 'bad flags')
  363. return _BAD_QR
  364. if msg.get_qid() != zone_notify_info.notify_msg_id:
  365. self._log_msg('error', errstr + 'bad query ID')
  366. return _BAD_QUERY_ID
  367. question = msg.get_question()[0]
  368. if question.get_name() != Name(zone_notify_info.zone_name):
  369. self._log_msg('error', errstr + 'bad query name')
  370. return _BAD_QUERY_NAME
  371. if msg.get_opcode() != Opcode.NOTIFY():
  372. self._log_msg('error', errstr + 'bad opcode')
  373. return _BAD_OPCODE
  374. except Exception as err:
  375. # We don't care what exception, just report it?
  376. self._log_msg('error', errstr + str(err))
  377. return _BAD_REPLY_PACKET
  378. return _REPLY_OK
  379. def _get_notify_reply(self, sock, tgt_addr):
  380. try:
  381. msg, addr = sock.recvfrom(512)
  382. except socket.error:
  383. self._log_msg('error', "notify to %s failed: can't read notify reply" % addr_to_str(tgt_addr))
  384. return None
  385. return msg
  386. def _log_msg(self, level, msg):
  387. if self._log:
  388. self._log.log_message(level, msg)