universal.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. #pragma once
  39. #include "libgearman/allocator.hpp"
  40. #include "libgearman/server_options.hpp"
  41. #include "libgearman/interface/packet.hpp"
  42. #include "libgearman/interface/error.hpp"
  43. #include "libgearman/vector.h"
  44. #include "libgearman/assert.hpp"
  45. #include "libgearman/ssl.h"
  46. #include <cstring>
  47. enum universal_options_t
  48. {
  49. GEARMAN_UNIVERSAL_NON_BLOCKING,
  50. GEARMAN_UNIVERSAL_DONT_TRACK_PACKETS,
  51. GEARMAN_UNIVERSAL_IDENTIFY,
  52. GEARMAN_UNIVERSAL_MAX
  53. };
  54. /**
  55. @todo this is only used by the server and should be made private.
  56. */
  57. typedef struct gearman_connection_st gearman_connection_st;
  58. typedef gearman_return_t (gearman_event_watch_fn)(gearman_connection_st *con,
  59. short events, void *context);
  60. struct gearman_universal_st : public error_st
  61. {
  62. struct Options {
  63. bool dont_track_packets;
  64. bool non_blocking;
  65. bool no_new_data;
  66. bool _ssl;
  67. struct gearman_vector_st *_ssl_ca_file;
  68. struct gearman_vector_st *_ssl_certificate;
  69. struct gearman_vector_st *_ssl_key;
  70. Options() :
  71. dont_track_packets{false},
  72. non_blocking{false},
  73. no_new_data{false},
  74. _ssl{false},
  75. _ssl_ca_file{NULL},
  76. _ssl_certificate{NULL},
  77. _ssl_key{NULL}
  78. { }
  79. } options;
  80. gearman_verbose_t verbose;
  81. uint32_t con_count;
  82. uint32_t packet_count;
  83. uint32_t pfds_size;
  84. uint32_t sending;
  85. int timeout; // Connection timeout.
  86. gearman_connection_st *con_list;
  87. gearman_server_options_st *server_options_list;
  88. gearman_packet_st *packet_list;
  89. struct pollfd *pfds;
  90. gearman_log_fn *log_fn;
  91. void *log_context;
  92. gearman_allocator_t allocator;
  93. struct gearman_vector_st *_identifier;
  94. struct gearman_vector_st *_namespace;
  95. SSL_CTX* _ctx_ssl;
  96. struct error_st _error;
  97. int wakeup_fd[2];
  98. bool ssl() const
  99. {
  100. return options._ssl;
  101. }
  102. void ssl(bool ssl_)
  103. {
  104. options._ssl= ssl_;
  105. }
  106. private:
  107. void close_wakeup();
  108. public:
  109. bool wakeup(bool);
  110. bool has_wakeup() const
  111. {
  112. return wakeup_fd[0] != INVALID_SOCKET;
  113. }
  114. bool is_non_blocking() const
  115. {
  116. return options.non_blocking;
  117. }
  118. void non_blocking(bool arg_)
  119. {
  120. options.non_blocking= arg_;
  121. }
  122. const char *error() const
  123. {
  124. if (_error.error() == nullptr)
  125. {
  126. if (_error.error_code() != GEARMAN_SUCCESS)
  127. {
  128. return gearman_strerror(_error.error_code());
  129. }
  130. return nullptr;
  131. }
  132. return _error.error();
  133. }
  134. gearman_return_t error_code() const
  135. {
  136. return _error.error_code();
  137. }
  138. gearman_return_t error_code(const gearman_return_t rc)
  139. {
  140. return _error.error_code(rc);
  141. }
  142. int last_errno() const
  143. {
  144. return _error.system_error();
  145. }
  146. void last_errno(const int last_errno_)
  147. {
  148. _error.system_error(last_errno_);
  149. }
  150. bool has_connections() const
  151. {
  152. return con_count;
  153. }
  154. void reset_error()
  155. {
  156. _error.clear();
  157. }
  158. gearman_return_t option(const universal_options_t& option_, bool value);
  159. gearman_universal_st(const universal_options_t *options_= nullptr) :
  160. verbose(GEARMAN_VERBOSE_NEVER),
  161. con_count(0),
  162. packet_count(0),
  163. pfds_size(0),
  164. sending(0),
  165. timeout{-1},
  166. con_list(nullptr),
  167. server_options_list(nullptr),
  168. packet_list(nullptr),
  169. pfds(nullptr),
  170. log_fn(nullptr),
  171. log_context(nullptr),
  172. allocator(gearman_default_allocator()),
  173. _identifier(nullptr),
  174. _namespace(nullptr),
  175. _ctx_ssl(nullptr)
  176. {
  177. wakeup_fd[0]= INVALID_SOCKET;
  178. wakeup_fd[1]= INVALID_SOCKET;
  179. if (options_)
  180. {
  181. while (*options_ != GEARMAN_UNIVERSAL_MAX)
  182. {
  183. /**
  184. @note Check for bad value, refactor gearman_add_options().
  185. */
  186. (void)option(*options_, true);
  187. options_++;
  188. }
  189. }
  190. }
  191. const char* ssl_ca_file() const
  192. {
  193. if (options._ssl_ca_file && options._ssl_ca_file->size())
  194. {
  195. return options._ssl_ca_file->c_str();
  196. }
  197. if (getenv("GEARMAND_CA_CERTIFICATE"))
  198. {
  199. return getenv("GEARMAND_CA_CERTIFICATE");
  200. }
  201. return GEARMAND_CA_CERTIFICATE;
  202. }
  203. void ssl_ca_file(const char* ssl_ca_file_)
  204. {
  205. gearman_string_free(options._ssl_ca_file);
  206. size_t ssl_ca_file_size_ = 0;
  207. if (ssl_ca_file_ && (ssl_ca_file_size_ = strlen(ssl_ca_file_)))
  208. {
  209. options._ssl_ca_file = gearman_string_create(NULL, ssl_ca_file_, ssl_ca_file_size_);
  210. }
  211. else
  212. {
  213. options._ssl_ca_file = NULL;
  214. }
  215. }
  216. const char* ssl_certificate() const
  217. {
  218. if (options._ssl_certificate && options._ssl_certificate->size())
  219. {
  220. return options._ssl_certificate->c_str();
  221. }
  222. if (getenv("GEARMAN_CLIENT_PEM"))
  223. {
  224. return getenv("GEARMAN_CLIENT_PEM");
  225. }
  226. return GEARMAN_CLIENT_PEM;
  227. }
  228. void ssl_certificate(const char *ssl_certificate_)
  229. {
  230. gearman_string_free(options._ssl_certificate);
  231. size_t ssl_certificate_size_ = 0;
  232. if (ssl_certificate_ && (ssl_certificate_size_ = strlen(ssl_certificate_)))
  233. {
  234. options._ssl_certificate = gearman_string_create(NULL, ssl_certificate_, ssl_certificate_size_);
  235. }
  236. else
  237. {
  238. options._ssl_certificate = NULL;
  239. }
  240. }
  241. const char* ssl_key() const
  242. {
  243. if (options._ssl_key && options._ssl_key->size())
  244. {
  245. return options._ssl_key->c_str();
  246. }
  247. if (getenv("GEARMAN_CLIENT_KEY"))
  248. {
  249. return getenv("GEARMAN_CLIENT_KEY");
  250. }
  251. return GEARMAN_CLIENT_KEY;
  252. }
  253. void ssl_key(const char *ssl_key_)
  254. {
  255. gearman_string_free(options._ssl_key);
  256. size_t ssl_key_size_ = 0;
  257. if (ssl_key_ && (ssl_key_size_ = strlen(ssl_key_)))
  258. {
  259. options._ssl_key = gearman_string_create(NULL, ssl_key_, ssl_key_size_);
  260. }
  261. else
  262. {
  263. options._ssl_key = NULL;
  264. }
  265. }
  266. private:
  267. bool init_ssl();
  268. public:
  269. SSL_CTX* ctx_ssl()
  270. {
  271. if (ssl())
  272. {
  273. if (!_ctx_ssl)
  274. {
  275. if (init_ssl() == false)
  276. {
  277. abort();
  278. }
  279. }
  280. assert(_ctx_ssl);
  281. return _ctx_ssl;
  282. }
  283. return nullptr;
  284. }
  285. ~gearman_universal_st();
  286. void identifier(const char *identifier_, const size_t identifier_size_);
  287. bool has_identifier() const
  288. {
  289. return _identifier;
  290. }
  291. void flush();
  292. void reset();
  293. };
  294. static inline bool gearman_universal_is_non_blocking(gearman_universal_st &self)
  295. {
  296. return self.is_non_blocking();
  297. }
  298. static inline const char *gearman_universal_error(const gearman_universal_st &self)
  299. {
  300. return self.error();
  301. }
  302. static inline gearman_return_t gearman_universal_error_code(const gearman_universal_st &self)
  303. {
  304. return self.error_code();
  305. }
  306. static inline int gearman_universal_errno(const gearman_universal_st &self)
  307. {
  308. return self.last_errno();
  309. }
  310. static inline void universal_reset_error(gearman_universal_st &self)
  311. {
  312. self.reset_error();
  313. }