xfrout_client.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2010 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 <cstdlib>
  15. #include <cstring>
  16. #include <iostream>
  17. // for some IPC/network system calls in asio/detail/pipe_select_interrupter.hpp
  18. #include <unistd.h>
  19. #include <asio.hpp>
  20. #include <util/io/fd_share.h>
  21. #include <xfr/xfrout_client.h>
  22. using namespace std;
  23. using namespace isc::util::io;
  24. using asio::local::stream_protocol;
  25. namespace isc {
  26. namespace xfr {
  27. struct XfroutClientImpl {
  28. XfroutClientImpl(const string& file);
  29. const std::string file_path_;
  30. asio::io_service io_service_;
  31. // The socket used to communicate with the xfrout server.
  32. stream_protocol::socket socket_;
  33. };
  34. XfroutClientImpl::XfroutClientImpl(const string& file) :
  35. file_path_(file), socket_(io_service_)
  36. {}
  37. XfroutClient::XfroutClient(const string& file) :
  38. impl_(new XfroutClientImpl(file))
  39. {}
  40. XfroutClient::~XfroutClient() {
  41. delete impl_;
  42. }
  43. void
  44. XfroutClient::connect() {
  45. asio::error_code err;
  46. impl_->socket_.connect(stream_protocol::endpoint(impl_->file_path_), err);
  47. if (err) {
  48. isc_throw(XfroutError, "socket connect failed: " << err.message());
  49. }
  50. }
  51. void
  52. XfroutClient::disconnect() {
  53. asio::error_code err;
  54. impl_->socket_.close(err);
  55. if (err) {
  56. isc_throw(XfroutError, "close socket failed: " << err.message());
  57. }
  58. }
  59. int
  60. XfroutClient::sendXfroutRequestInfo(const int tcp_sock,
  61. const void* const msg_data,
  62. const uint16_t msg_len)
  63. {
  64. if (send_fd(impl_->socket_.native(), tcp_sock) < 0) {
  65. isc_throw(XfroutError,
  66. "Failed to send the socket file descriptor "
  67. "to xfrout module");
  68. }
  69. // TODO: this shouldn't be blocking send, even though it's unlikely to
  70. // block.
  71. // converting the 16-bit word to network byte order.
  72. const uint8_t lenbuf[2] = { msg_len >> 8, msg_len & 0xff };
  73. if (send(impl_->socket_.native(), lenbuf, sizeof(lenbuf), 0) !=
  74. sizeof(lenbuf)) {
  75. isc_throw(XfroutError,
  76. "failed to send XFR request length to xfrout module");
  77. }
  78. if (send(impl_->socket_.native(), msg_data, msg_len, 0) != msg_len) {
  79. isc_throw(XfroutError,
  80. "failed to send XFR request data to xfrout module");
  81. }
  82. return (0);
  83. }
  84. } // End for xfr
  85. } // End for isc