ncr_io.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // Copyright (C) 2013-2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or 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 ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <dhcp_ddns/dhcp_ddns_log.h>
  15. #include <dhcp_ddns/ncr_io.h>
  16. #include <asio.hpp>
  17. #include <boost/algorithm/string/predicate.hpp>
  18. namespace isc {
  19. namespace dhcp_ddns {
  20. NameChangeProtocol stringToNcrProtocol(const std::string& protocol_str) {
  21. if (boost::iequals(protocol_str, "UDP")) {
  22. return (NCR_UDP);
  23. }
  24. if (boost::iequals(protocol_str, "TCP")) {
  25. return (NCR_TCP);
  26. }
  27. isc_throw(BadValue, "Invalid NameChangeRequest protocol:" << protocol_str);
  28. }
  29. std::string ncrProtocolToString(NameChangeProtocol protocol) {
  30. switch (protocol) {
  31. case NCR_UDP:
  32. return ("UDP");
  33. case NCR_TCP:
  34. return ("TCP");
  35. default:
  36. break;
  37. }
  38. std::ostringstream stream;
  39. stream << "UNKNOWN(" << protocol << ")";
  40. return (stream.str());
  41. }
  42. //************************** NameChangeListener ***************************
  43. NameChangeListener::NameChangeListener(RequestReceiveHandler&
  44. recv_handler)
  45. : listening_(false), io_pending_(false), recv_handler_(recv_handler) {
  46. };
  47. void
  48. NameChangeListener::startListening(isc::asiolink::IOService& io_service) {
  49. if (amListening()) {
  50. // This amounts to a programmatic error.
  51. isc_throw(NcrListenerError, "NameChangeListener is already listening");
  52. }
  53. // Call implementation dependent open.
  54. try {
  55. open(io_service);
  56. } catch (const isc::Exception& ex) {
  57. stopListening();
  58. isc_throw(NcrListenerOpenError, "Open failed:" << ex.what());
  59. }
  60. // Set our status to listening.
  61. setListening(true);
  62. // Start the first asynchronous receive.
  63. try {
  64. receiveNext();
  65. } catch (const isc::Exception& ex) {
  66. stopListening();
  67. isc_throw(NcrListenerReceiveError, "doReceive failed:" << ex.what());
  68. }
  69. }
  70. void
  71. NameChangeListener::receiveNext() {
  72. io_pending_ = true;
  73. doReceive();
  74. }
  75. void
  76. NameChangeListener::stopListening() {
  77. try {
  78. // Call implementation dependent close.
  79. close();
  80. } catch (const isc::Exception &ex) {
  81. // Swallow exceptions. If we have some sort of error we'll log
  82. // it but we won't propagate the throw.
  83. LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_NCR_LISTEN_CLOSE_ERROR)
  84. .arg(ex.what());
  85. }
  86. // Set it false, no matter what. This allows us to at least try to
  87. // re-open via startListening().
  88. setListening(false);
  89. }
  90. void
  91. NameChangeListener::invokeRecvHandler(const Result result,
  92. NameChangeRequestPtr& ncr) {
  93. // Call the registered application layer handler.
  94. // Surround the invocation with a try-catch. The invoked handler is
  95. // not supposed to throw, but in the event it does we will at least
  96. // report it.
  97. try {
  98. io_pending_ = false;
  99. recv_handler_(result, ncr);
  100. } catch (const std::exception& ex) {
  101. LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_UNCAUGHT_NCR_RECV_HANDLER_ERROR)
  102. .arg(ex.what());
  103. }
  104. // Start the next IO layer asynchronous receive.
  105. // In the event the handler above intervened and decided to stop listening
  106. // we need to check that first.
  107. if (amListening()) {
  108. try {
  109. receiveNext();
  110. } catch (const isc::Exception& ex) {
  111. // It is possible though unlikely, for doReceive to fail without
  112. // scheduling the read. While, unlikely, it does mean the callback
  113. // will not get called with a failure. A throw here would surface
  114. // at the IOService::run (or run variant) invocation. So we will
  115. // close the window by invoking the application handler with
  116. // a failed result, and let the application layer sort it out.
  117. LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_NCR_RECV_NEXT_ERROR)
  118. .arg(ex.what());
  119. // Call the registered application layer handler.
  120. // Surround the invocation with a try-catch. The invoked handler is
  121. // not supposed to throw, but in the event it does we will at least
  122. // report it.
  123. NameChangeRequestPtr empty;
  124. try {
  125. io_pending_ = false;
  126. recv_handler_(ERROR, empty);
  127. } catch (const std::exception& ex) {
  128. LOG_ERROR(dhcp_ddns_logger,
  129. DHCP_DDNS_UNCAUGHT_NCR_RECV_HANDLER_ERROR)
  130. .arg(ex.what());
  131. }
  132. }
  133. }
  134. }
  135. //************************* NameChangeSender ******************************
  136. NameChangeSender::NameChangeSender(RequestSendHandler& send_handler,
  137. size_t send_queue_max)
  138. : sending_(false), send_handler_(send_handler),
  139. send_queue_max_(send_queue_max), io_service_(NULL) {
  140. // Queue size must be big enough to hold at least 1 entry.
  141. setQueueMaxSize(send_queue_max);
  142. }
  143. void
  144. NameChangeSender::startSending(isc::asiolink::IOService& io_service) {
  145. if (amSending()) {
  146. // This amounts to a programmatic error.
  147. isc_throw(NcrSenderError, "NameChangeSender is already sending");
  148. }
  149. // Clear send marker.
  150. ncr_to_send_.reset();
  151. // Call implementation dependent open.
  152. try {
  153. // Remember io service we're given.
  154. io_service_ = &io_service;
  155. open(io_service);
  156. } catch (const isc::Exception& ex) {
  157. stopSending();
  158. isc_throw(NcrSenderOpenError, "Open failed: " << ex.what());
  159. }
  160. // Set our status to sending.
  161. setSending(true);
  162. // If there's any queued already.. we'll start sending.
  163. sendNext();
  164. }
  165. void
  166. NameChangeSender::stopSending() {
  167. // Set it send indicator to false, no matter what. This allows us to at
  168. // least try to re-open via startSending(). Also, setting it false now,
  169. // allows us to break sendNext() chain in invokeSendHandler.
  170. setSending(false);
  171. // If there is an outstanding IO to complete, attempt to process it.
  172. if (ioReady() && io_service_ != NULL) {
  173. try {
  174. runReadyIO();
  175. } catch (const std::exception& ex) {
  176. // Swallow exceptions. If we have some sort of error we'll log
  177. // it but we won't propagate the throw.
  178. LOG_ERROR(dhcp_ddns_logger,
  179. DHCP_DDNS_NCR_FLUSH_IO_ERROR).arg(ex.what());
  180. }
  181. }
  182. try {
  183. // Call implementation dependent close.
  184. close();
  185. } catch (const isc::Exception &ex) {
  186. // Swallow exceptions. If we have some sort of error we'll log
  187. // it but we won't propagate the throw.
  188. LOG_ERROR(dhcp_ddns_logger,
  189. DHCP_DDNS_NCR_SEND_CLOSE_ERROR).arg(ex.what());
  190. }
  191. io_service_ = NULL;
  192. }
  193. void
  194. NameChangeSender::sendRequest(NameChangeRequestPtr& ncr) {
  195. if (!amSending()) {
  196. isc_throw(NcrSenderError, "sender is not ready to send");
  197. }
  198. if (!ncr) {
  199. isc_throw(NcrSenderError, "request to send is empty");
  200. }
  201. if (send_queue_.size() >= send_queue_max_) {
  202. isc_throw(NcrSenderQueueFull, "send queue has reached maximum capacity:"
  203. << send_queue_max_ );
  204. }
  205. // Put it on the queue.
  206. send_queue_.push_back(ncr);
  207. // Call sendNext to schedule the next one to go.
  208. sendNext();
  209. }
  210. void
  211. NameChangeSender::sendNext() {
  212. if (ncr_to_send_) {
  213. // @todo Not sure if there is any risk of getting stuck here but
  214. // an interval timer to defend would be good.
  215. // In reality, the derivation should ensure they timeout themselves
  216. return;
  217. }
  218. // If queue isn't empty, then get one from the front. Note we leave
  219. // it on the front of the queue until we successfully send it.
  220. if (!send_queue_.empty()) {
  221. ncr_to_send_ = send_queue_.front();
  222. // @todo start defense timer
  223. // If a send were to hang and we timed it out, then timeout
  224. // handler need to cycle thru open/close ?
  225. // Call implementation dependent send.
  226. doSend(ncr_to_send_);
  227. }
  228. }
  229. void
  230. NameChangeSender::invokeSendHandler(const NameChangeSender::Result result) {
  231. // @todo reset defense timer
  232. if (result == SUCCESS) {
  233. // It shipped so pull it off the queue.
  234. send_queue_.pop_front();
  235. }
  236. // Invoke the completion handler passing in the result and a pointer
  237. // the request involved.
  238. // Surround the invocation with a try-catch. The invoked handler is
  239. // not supposed to throw, but in the event it does we will at least
  240. // report it.
  241. try {
  242. send_handler_(result, ncr_to_send_);
  243. } catch (const std::exception& ex) {
  244. LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_UNCAUGHT_NCR_SEND_HANDLER_ERROR)
  245. .arg(ex.what());
  246. }
  247. // Clear the pending ncr pointer.
  248. ncr_to_send_.reset();
  249. // Set up the next send
  250. try {
  251. if (amSending()) {
  252. sendNext();
  253. }
  254. } catch (const isc::Exception& ex) {
  255. // It is possible though unlikely, for sendNext to fail without
  256. // scheduling the send. While, unlikely, it does mean the callback
  257. // will not get called with a failure. A throw here would surface
  258. // at the IOService::run (or run variant) invocation. So we will
  259. // close the window by invoking the application handler with
  260. // a failed result, and let the application layer sort it out.
  261. LOG_ERROR(dhcp_ddns_logger, DHCP_DDNS_NCR_SEND_NEXT_ERROR)
  262. .arg(ex.what());
  263. // Invoke the completion handler passing in failed result.
  264. // Surround the invocation with a try-catch. The invoked handler is
  265. // not supposed to throw, but in the event it does we will at least
  266. // report it.
  267. try {
  268. send_handler_(ERROR, ncr_to_send_);
  269. } catch (const std::exception& ex) {
  270. LOG_ERROR(dhcp_ddns_logger,
  271. DHCP_DDNS_UNCAUGHT_NCR_SEND_HANDLER_ERROR).arg(ex.what());
  272. }
  273. }
  274. }
  275. void
  276. NameChangeSender::skipNext() {
  277. if (!send_queue_.empty()) {
  278. // Discards the request at the front of the queue.
  279. send_queue_.pop_front();
  280. }
  281. }
  282. void
  283. NameChangeSender::clearSendQueue() {
  284. if (amSending()) {
  285. isc_throw(NcrSenderError, "Cannot clear queue while sending");
  286. }
  287. send_queue_.clear();
  288. }
  289. void
  290. NameChangeSender::setQueueMaxSize(const size_t new_max) {
  291. if (new_max == 0) {
  292. isc_throw(NcrSenderError, "NameChangeSender:"
  293. " queue size must be greater than zero");
  294. }
  295. send_queue_max_ = new_max;
  296. }
  297. const NameChangeRequestPtr&
  298. NameChangeSender::peekAt(const size_t index) const {
  299. if (index >= getQueueSize()) {
  300. isc_throw(NcrSenderError,
  301. "NameChangeSender::peekAt peek beyond end of queue attempted"
  302. << " index: " << index << " queue size: " << getQueueSize());
  303. }
  304. return (send_queue_.at(index));
  305. }
  306. void
  307. NameChangeSender::assumeQueue(NameChangeSender& source_sender) {
  308. if (source_sender.amSending()) {
  309. isc_throw(NcrSenderError, "Cannot assume queue:"
  310. " source sender is actively sending");
  311. }
  312. if (amSending()) {
  313. isc_throw(NcrSenderError, "Cannot assume queue:"
  314. " target sender is actively sending");
  315. }
  316. if (getQueueMaxSize() < source_sender.getQueueSize()) {
  317. isc_throw(NcrSenderError, "Cannot assume queue:"
  318. " source queue count exceeds target queue max");
  319. }
  320. if (!send_queue_.empty()) {
  321. isc_throw(NcrSenderError, "Cannot assume queue:"
  322. " target queue is not empty");
  323. }
  324. send_queue_.swap(source_sender.getSendQueue());
  325. }
  326. int
  327. NameChangeSender::getSelectFd() {
  328. isc_throw(NotImplemented, "NameChangeSender::getSelectFd is not supported");
  329. }
  330. void
  331. NameChangeSender::runReadyIO() {
  332. if (!io_service_) {
  333. isc_throw(NcrSenderError, "NameChangeSender::runReadyIO"
  334. " sender io service is null");
  335. }
  336. // We shouldn't be here if IO isn't ready to execute.
  337. // By running poll we're gauranteed not to hang.
  338. /// @todo Trac# 3325 requests that asiolink::IOService provide a
  339. /// wrapper for poll().
  340. io_service_->get_io_service().poll_one();
  341. }
  342. } // namespace isc::dhcp_ddns
  343. } // namespace isc