io_address.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <config.h>
  15. #include <unistd.h> // for some IPC/network system calls
  16. #include <stdint.h>
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. #include <asio.hpp>
  20. #include <exceptions/exceptions.h>
  21. #include <asiolink/io_address.h>
  22. #include <asiolink/io_error.h>
  23. #include <boost/static_assert.hpp>
  24. using namespace asio;
  25. using asio::ip::udp;
  26. using asio::ip::tcp;
  27. using namespace std;
  28. namespace isc {
  29. namespace asiolink {
  30. // XXX: we cannot simply construct the address in the initialization list,
  31. // because we'd like to throw our own exception on failure.
  32. IOAddress::IOAddress(const string& address_str) {
  33. asio::error_code err;
  34. asio_address_ = ip::address::from_string(address_str, err);
  35. if (err) {
  36. isc_throw(IOError, "Failed to convert string to address '"
  37. << address_str << "': " << err.message());
  38. }
  39. }
  40. IOAddress::IOAddress(const ip::address& asio_address) :
  41. asio_address_(asio_address)
  42. {}
  43. IOAddress::IOAddress(uint32_t v4address):
  44. asio_address_(asio::ip::address_v4(v4address)) {
  45. }
  46. string
  47. IOAddress::toText() const {
  48. return (asio_address_.to_string());
  49. }
  50. IOAddress
  51. IOAddress::from_bytes(short family, const uint8_t* data) {
  52. if (data == NULL) {
  53. isc_throw(BadValue, "NULL pointer received.");
  54. } else
  55. if ( (family != AF_INET) && (family != AF_INET6) ) {
  56. isc_throw(BadValue, "Invalid family type. Only AF_INET and AF_INET6"
  57. << "are supported");
  58. }
  59. BOOST_STATIC_ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
  60. char addr_str[INET6_ADDRSTRLEN];
  61. inet_ntop(family, data, addr_str, INET6_ADDRSTRLEN);
  62. return IOAddress(string(addr_str));
  63. }
  64. short
  65. IOAddress::getFamily() const {
  66. if (asio_address_.is_v4()) {
  67. return (AF_INET);
  68. } else {
  69. return (AF_INET6);
  70. }
  71. }
  72. const asio::ip::address&
  73. IOAddress::getAddress() const {
  74. return asio_address_;
  75. }
  76. IOAddress::operator uint32_t() const {
  77. if (getAddress().is_v4()) {
  78. return (getAddress().to_v4().to_ulong());
  79. } else {
  80. isc_throw(BadValue, "Can't convert " << toText()
  81. << " address to IPv4.");
  82. }
  83. }
  84. } // namespace asiolink
  85. } // namespace isc