endpoint.ipp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // local/detail/impl/endpoint.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Derived from a public domain implementation written by Daniel Casimiro.
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
  12. #define BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
  18. #include <cstring>
  19. #include <boost/asio/detail/socket_ops.hpp>
  20. #include <boost/asio/detail/throw_error.hpp>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/local/detail/endpoint.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace local {
  27. namespace detail {
  28. endpoint::endpoint()
  29. {
  30. init("", 0);
  31. }
  32. endpoint::endpoint(const char* path_name)
  33. {
  34. using namespace std; // For strlen.
  35. init(path_name, strlen(path_name));
  36. }
  37. endpoint::endpoint(const std::string& path_name)
  38. {
  39. init(path_name.data(), path_name.length());
  40. }
  41. #if defined(BOOST_ASIO_HAS_STRING_VIEW)
  42. endpoint::endpoint(string_view path_name)
  43. {
  44. init(path_name.data(), path_name.length());
  45. }
  46. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  47. void endpoint::resize(std::size_t new_size)
  48. {
  49. if (new_size > sizeof(boost::asio::detail::sockaddr_un_type))
  50. {
  51. boost::system::error_code ec(boost::asio::error::invalid_argument);
  52. boost::asio::detail::throw_error(ec);
  53. }
  54. else if (new_size == 0)
  55. {
  56. path_length_ = 0;
  57. }
  58. else
  59. {
  60. path_length_ = new_size
  61. - offsetof(boost::asio::detail::sockaddr_un_type, sun_path);
  62. // The path returned by the operating system may be NUL-terminated.
  63. if (path_length_ > 0 && data_.local.sun_path[path_length_ - 1] == 0)
  64. --path_length_;
  65. }
  66. }
  67. std::string endpoint::path() const
  68. {
  69. return std::string(data_.local.sun_path, path_length_);
  70. }
  71. void endpoint::path(const char* p)
  72. {
  73. using namespace std; // For strlen.
  74. init(p, strlen(p));
  75. }
  76. void endpoint::path(const std::string& p)
  77. {
  78. init(p.data(), p.length());
  79. }
  80. bool operator==(const endpoint& e1, const endpoint& e2)
  81. {
  82. return e1.path() == e2.path();
  83. }
  84. bool operator<(const endpoint& e1, const endpoint& e2)
  85. {
  86. return e1.path() < e2.path();
  87. }
  88. void endpoint::init(const char* path_name, std::size_t path_length)
  89. {
  90. if (path_length > sizeof(data_.local.sun_path) - 1)
  91. {
  92. // The buffer is not large enough to store this address.
  93. boost::system::error_code ec(boost::asio::error::name_too_long);
  94. boost::asio::detail::throw_error(ec);
  95. }
  96. using namespace std; // For memcpy.
  97. data_.local = boost::asio::detail::sockaddr_un_type();
  98. data_.local.sun_family = AF_UNIX;
  99. if (path_length > 0)
  100. memcpy(data_.local.sun_path, path_name, path_length);
  101. path_length_ = path_length;
  102. // NUL-terminate normal path names. Names that start with a NUL are in the
  103. // UNIX domain protocol's "abstract namespace" and are not NUL-terminated.
  104. if (path_length > 0 && data_.local.sun_path[0] == 0)
  105. data_.local.sun_path[path_length] = 0;
  106. }
  107. } // namespace detail
  108. } // namespace local
  109. } // namespace asio
  110. } // namespace boost
  111. #include <boost/asio/detail/pop_options.hpp>
  112. #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
  113. #endif // BOOST_ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP