address.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #ifndef __ZMQ_ADDRESS_HPP_INCLUDED__
  25. #define __ZMQ_ADDRESS_HPP_INCLUDED__
  26. #include "fd.hpp"
  27. #include <string>
  28. #ifndef ZMQ_HAVE_WINDOWS
  29. #include <sys/socket.h>
  30. #else
  31. #include <ws2tcpip.h>
  32. #endif
  33. namespace zmq
  34. {
  35. class ctx_t;
  36. class tcp_address_t;
  37. class udp_address_t;
  38. class ws_address_t;
  39. #ifdef ZMQ_HAVE_WSS
  40. class wss_address_t;
  41. #endif
  42. #if defined ZMQ_HAVE_IPC
  43. class ipc_address_t;
  44. #endif
  45. #if defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_VXWORKS
  46. class tipc_address_t;
  47. #endif
  48. #if defined ZMQ_HAVE_VMCI
  49. class vmci_address_t;
  50. #endif
  51. namespace protocol_name
  52. {
  53. static const char inproc[] = "inproc";
  54. static const char tcp[] = "tcp";
  55. static const char udp[] = "udp";
  56. #ifdef ZMQ_HAVE_OPENPGM
  57. static const char pgm[] = "pgm";
  58. static const char epgm[] = "epgm";
  59. #endif
  60. #ifdef ZMQ_HAVE_NORM
  61. static const char norm[] = "norm";
  62. #endif
  63. #ifdef ZMQ_HAVE_WS
  64. static const char ws[] = "ws";
  65. #endif
  66. #ifdef ZMQ_HAVE_WSS
  67. static const char wss[] = "wss";
  68. #endif
  69. #if defined ZMQ_HAVE_IPC
  70. static const char ipc[] = "ipc";
  71. #endif
  72. #if defined ZMQ_HAVE_TIPC
  73. static const char tipc[] = "tipc";
  74. #endif
  75. #if defined ZMQ_HAVE_VMCI
  76. static const char vmci[] = "vmci";
  77. #endif
  78. }
  79. struct address_t
  80. {
  81. address_t (const std::string &protocol_,
  82. const std::string &address_,
  83. ctx_t *parent_);
  84. ~address_t ();
  85. const std::string protocol;
  86. const std::string address;
  87. ctx_t *const parent;
  88. // Protocol specific resolved address
  89. // All members must be pointers to allow for consistent initialization
  90. union
  91. {
  92. void *dummy;
  93. tcp_address_t *tcp_addr;
  94. udp_address_t *udp_addr;
  95. #ifdef ZMQ_HAVE_WS
  96. ws_address_t *ws_addr;
  97. #endif
  98. #ifdef ZMQ_HAVE_WSS
  99. wss_address_t *wss_addr;
  100. #endif
  101. #if defined ZMQ_HAVE_IPC
  102. ipc_address_t *ipc_addr;
  103. #endif
  104. #if defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_VXWORKS
  105. tipc_address_t *tipc_addr;
  106. #endif
  107. #if defined ZMQ_HAVE_VMCI
  108. vmci_address_t *vmci_addr;
  109. #endif
  110. } resolved;
  111. int to_string (std::string &addr_) const;
  112. };
  113. #if defined(ZMQ_HAVE_HPUX) || defined(ZMQ_HAVE_VXWORKS) \
  114. || defined(ZMQ_HAVE_WINDOWS)
  115. typedef int zmq_socklen_t;
  116. #else
  117. typedef socklen_t zmq_socklen_t;
  118. #endif
  119. enum socket_end_t
  120. {
  121. socket_end_local,
  122. socket_end_remote
  123. };
  124. zmq_socklen_t
  125. get_socket_address (fd_t fd_, socket_end_t socket_end_, sockaddr_storage *ss_);
  126. template <typename T>
  127. std::string get_socket_name (fd_t fd_, socket_end_t socket_end_)
  128. {
  129. struct sockaddr_storage ss;
  130. const zmq_socklen_t sl = get_socket_address (fd_, socket_end_, &ss);
  131. if (sl == 0) {
  132. return std::string ();
  133. }
  134. const T addr (reinterpret_cast<struct sockaddr *> (&ss), sl);
  135. std::string address_string;
  136. addr.to_string (address_string);
  137. return address_string;
  138. }
  139. }
  140. #endif