ipc_address.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "precompiled.hpp"
  25. #include "ipc_address.hpp"
  26. #if defined ZMQ_HAVE_IPC
  27. #include "err.hpp"
  28. #include <string>
  29. #ifndef HAVE_STRNLEN
  30. static size_t strnlen (const char *s, size_t len)
  31. {
  32. for (size_t i = 0; i < len; i++) {
  33. if (s[i] == '\0')
  34. return i + 1;
  35. }
  36. return len;
  37. }
  38. #endif
  39. zmq::ipc_address_t::ipc_address_t ()
  40. {
  41. memset (&_address, 0, sizeof _address);
  42. }
  43. zmq::ipc_address_t::ipc_address_t (const sockaddr *sa_, socklen_t sa_len_) :
  44. _addrlen (sa_len_)
  45. {
  46. zmq_assert (sa_ && sa_len_ > 0);
  47. memset (&_address, 0, sizeof _address);
  48. if (sa_->sa_family == AF_UNIX)
  49. memcpy (&_address, sa_, sa_len_);
  50. }
  51. zmq::ipc_address_t::~ipc_address_t ()
  52. {
  53. }
  54. int zmq::ipc_address_t::resolve (const char *path_)
  55. {
  56. const size_t path_len = strlen (path_);
  57. if (path_len >= sizeof _address.sun_path) {
  58. errno = ENAMETOOLONG;
  59. return -1;
  60. }
  61. if (path_[0] == '@' && !path_[1]) {
  62. errno = EINVAL;
  63. return -1;
  64. }
  65. _address.sun_family = AF_UNIX;
  66. memcpy (_address.sun_path, path_, path_len + 1);
  67. /* Abstract sockets start with '\0' */
  68. if (path_[0] == '@')
  69. *_address.sun_path = '\0';
  70. _addrlen =
  71. static_cast<socklen_t> (offsetof (sockaddr_un, sun_path) + path_len);
  72. return 0;
  73. }
  74. int zmq::ipc_address_t::to_string (std::string &addr_) const
  75. {
  76. if (_address.sun_family != AF_UNIX) {
  77. addr_.clear ();
  78. return -1;
  79. }
  80. const char prefix[] = "ipc://";
  81. char buf[sizeof prefix + sizeof _address.sun_path];
  82. char *pos = buf;
  83. memcpy (pos, prefix, sizeof prefix - 1);
  84. pos += sizeof prefix - 1;
  85. const char *src_pos = _address.sun_path;
  86. if (!_address.sun_path[0] && _address.sun_path[1]) {
  87. *pos++ = '@';
  88. src_pos++;
  89. }
  90. // according to http://man7.org/linux/man-pages/man7/unix.7.html, NOTES
  91. // section, address.sun_path might not always be null-terminated; therefore,
  92. // we calculate the length based of addrlen
  93. const size_t src_len =
  94. strnlen (src_pos, _addrlen - offsetof (sockaddr_un, sun_path)
  95. - (src_pos - _address.sun_path));
  96. memcpy (pos, src_pos, src_len);
  97. addr_.assign (buf, pos - buf + src_len);
  98. return 0;
  99. }
  100. const sockaddr *zmq::ipc_address_t::addr () const
  101. {
  102. return reinterpret_cast<const sockaddr *> (&_address);
  103. }
  104. socklen_t zmq::ipc_address_t::addrlen () const
  105. {
  106. return _addrlen;
  107. }
  108. #endif