tipc_address.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "tipc_address.hpp"
  26. #if defined ZMQ_HAVE_TIPC
  27. #include "err.hpp"
  28. #include <string>
  29. #include <sstream>
  30. zmq::tipc_address_t::tipc_address_t ()
  31. {
  32. memset (&address, 0, sizeof address);
  33. _random = false;
  34. }
  35. zmq::tipc_address_t::tipc_address_t (const sockaddr *sa_, socklen_t sa_len_)
  36. {
  37. zmq_assert (sa_ && sa_len_ > 0);
  38. memset (&address, 0, sizeof address);
  39. if (sa_->sa_family == AF_TIPC)
  40. memcpy (&address, sa_, sa_len_);
  41. _random = false;
  42. }
  43. void zmq::tipc_address_t::set_random ()
  44. {
  45. _random = true;
  46. }
  47. bool zmq::tipc_address_t::is_random () const
  48. {
  49. return _random;
  50. }
  51. bool zmq::tipc_address_t::is_service () const
  52. {
  53. if (address.addrtype == TIPC_ADDR_ID)
  54. return false;
  55. return true;
  56. }
  57. int zmq::tipc_address_t::resolve (const char *name_)
  58. {
  59. unsigned int type = 0;
  60. unsigned int lower = 0;
  61. unsigned int upper = 0;
  62. unsigned int ref = 0;
  63. unsigned int z = 1, c = 0, n = 0;
  64. char eof;
  65. const char *domain;
  66. int res;
  67. if (strncmp (name_, "<*>", 3) == 0) {
  68. set_random ();
  69. address.family = AF_TIPC;
  70. address.addrtype = TIPC_ADDR_ID;
  71. address.addr.id.node = 0;
  72. address.addr.id.ref = 0;
  73. address.scope = 0;
  74. return 0;
  75. }
  76. res = sscanf (name_, "{%u,%u,%u}", &type, &lower, &upper);
  77. /* Fetch optional domain suffix. */
  78. if ((domain = strchr (name_, '@'))) {
  79. if (sscanf (domain, "@%u.%u.%u%c", &z, &c, &n, &eof) != 3)
  80. return EINVAL;
  81. }
  82. if (res == 3) {
  83. if (type < TIPC_RESERVED_TYPES || upper < lower)
  84. return EINVAL;
  85. address.family = AF_TIPC;
  86. address.addrtype = TIPC_ADDR_NAMESEQ;
  87. address.addr.nameseq.type = type;
  88. address.addr.nameseq.lower = lower;
  89. address.addr.nameseq.upper = upper;
  90. address.scope = TIPC_ZONE_SCOPE;
  91. return 0;
  92. }
  93. if (res == 2 && type > TIPC_RESERVED_TYPES) {
  94. address.family = AF_TIPC;
  95. address.addrtype = TIPC_ADDR_NAME;
  96. address.addr.name.name.type = type;
  97. address.addr.name.name.instance = lower;
  98. address.addr.name.domain = tipc_addr (z, c, n);
  99. address.scope = 0;
  100. return 0;
  101. } else if (res == 0) {
  102. res = sscanf (name_, "<%u.%u.%u:%u>", &z, &c, &n, &ref);
  103. if (res == 4) {
  104. address.family = AF_TIPC;
  105. address.addrtype = TIPC_ADDR_ID;
  106. address.addr.id.node = tipc_addr (z, c, n);
  107. address.addr.id.ref = ref;
  108. address.scope = 0;
  109. return 0;
  110. }
  111. }
  112. return EINVAL;
  113. }
  114. int zmq::tipc_address_t::to_string (std::string &addr_) const
  115. {
  116. if (address.family != AF_TIPC) {
  117. addr_.clear ();
  118. return -1;
  119. }
  120. std::stringstream s;
  121. if (address.addrtype == TIPC_ADDR_NAMESEQ
  122. || address.addrtype == TIPC_ADDR_NAME) {
  123. s << "tipc://"
  124. << "{" << address.addr.nameseq.type;
  125. s << ", " << address.addr.nameseq.lower;
  126. s << ", " << address.addr.nameseq.upper << "}";
  127. addr_ = s.str ();
  128. } else if (address.addrtype == TIPC_ADDR_ID || is_random ()) {
  129. s << "tipc://"
  130. << "<" << tipc_zone (address.addr.id.node);
  131. s << "." << tipc_cluster (address.addr.id.node);
  132. s << "." << tipc_node (address.addr.id.node);
  133. s << ":" << address.addr.id.ref << ">";
  134. addr_ = s.str ();
  135. } else {
  136. addr_.clear ();
  137. return -1;
  138. }
  139. return 0;
  140. }
  141. const sockaddr *zmq::tipc_address_t::addr () const
  142. {
  143. return (sockaddr *) &address;
  144. }
  145. socklen_t zmq::tipc_address_t::addrlen () const
  146. {
  147. return static_cast<socklen_t> (sizeof address);
  148. }
  149. #endif