ncr_io.cc 13 KB

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