protocol.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. /**
  38. * @file
  39. * @brief Gear Protocol Definitions
  40. */
  41. #include "gear_config.h"
  42. #include <libgearman-server/common.h>
  43. #include <libgearman/strcommand.h>
  44. #include <libgearman-server/packet.h>
  45. #include <cstdio>
  46. #include <cstdlib>
  47. #if defined(HAVE_CYASSL) && HAVE_CYASSL
  48. # include <cyassl/ssl.h>
  49. #endif
  50. #include <libgearman-server/plugins/protocol/gear/protocol.h>
  51. #include "libgearman/command.h"
  52. #define CERT_PEM "/home/brian/cyassl/certs/server-cert.pem"
  53. #define CERT_KEY_PEM "/home/brian/cyassl/certs/server-key.pem"
  54. static gearmand_error_t gearmand_packet_unpack_header(gearmand_packet_st *packet)
  55. {
  56. uint32_t tmp;
  57. if (memcmp(packet->args, "\0REQ", 4) == 0)
  58. {
  59. packet->magic= GEARMAN_MAGIC_REQUEST;
  60. }
  61. else if (memcmp(packet->args, "\0RES", 4) == 0)
  62. {
  63. packet->magic= GEARMAN_MAGIC_RESPONSE;
  64. }
  65. else
  66. {
  67. gearmand_warning("invalid magic value");
  68. return GEARMAND_INVALID_MAGIC;
  69. }
  70. memcpy(&tmp, packet->args + 4, 4);
  71. packet->command= static_cast<gearman_command_t>(ntohl(tmp));
  72. if (packet->command == GEARMAN_COMMAND_TEXT ||
  73. packet->command >= GEARMAN_COMMAND_MAX)
  74. {
  75. gearmand_error("invalid command value");
  76. return GEARMAND_INVALID_COMMAND;
  77. }
  78. memcpy(&tmp, packet->args + 8, 4);
  79. packet->data_size= ntohl(tmp);
  80. return GEARMAND_SUCCESS;
  81. }
  82. class Geartext : public gearmand::protocol::Context {
  83. public:
  84. ~Geartext()
  85. { }
  86. bool is_owner()
  87. {
  88. return false;
  89. }
  90. void notify(gearman_server_con_st *)
  91. {
  92. gearmand_info("Gear connection disconnected");
  93. }
  94. size_t unpack(gearmand_packet_st *packet,
  95. gearman_server_con_st *,
  96. const void *data, const size_t data_size,
  97. gearmand_error_t& ret_ptr)
  98. {
  99. size_t used_size;
  100. gearmand_info("Gear unpack");
  101. if (packet->args_size == 0)
  102. {
  103. if (data_size > 0 && ((uint8_t *)data)[0] != 0)
  104. {
  105. /* Try to parse a text-based command. */
  106. uint8_t* ptr= (uint8_t *)memchr(data, '\n', data_size);
  107. if (ptr == NULL)
  108. {
  109. ret_ptr= GEARMAND_IO_WAIT;
  110. return 0;
  111. }
  112. packet->magic= GEARMAN_MAGIC_TEXT;
  113. packet->command= GEARMAN_COMMAND_TEXT;
  114. used_size= size_t(ptr - ((uint8_t *)data)) +1;
  115. *ptr= 0;
  116. if (used_size > 1 && *(ptr - 1) == '\r')
  117. {
  118. *(ptr - 1)= 0;
  119. }
  120. size_t arg_size;
  121. for (arg_size= used_size, ptr= (uint8_t *)data; ptr != NULL; data= ptr)
  122. {
  123. ptr= (uint8_t *)memchr(data, ' ', arg_size);
  124. if (ptr != NULL)
  125. {
  126. *ptr= 0;
  127. ptr++;
  128. while (*ptr == ' ')
  129. {
  130. ptr++;
  131. }
  132. arg_size-= size_t(ptr - ((uint8_t *)data));
  133. }
  134. ret_ptr= gearmand_packet_create(packet, data, ptr == NULL ? arg_size :
  135. size_t(ptr - ((uint8_t *)data)));
  136. if (ret_ptr != GEARMAND_SUCCESS)
  137. {
  138. return used_size;
  139. }
  140. }
  141. return used_size;
  142. }
  143. else if (data_size < GEARMAND_PACKET_HEADER_SIZE)
  144. {
  145. ret_ptr= GEARMAND_IO_WAIT;
  146. return 0;
  147. }
  148. packet->args= packet->args_buffer;
  149. packet->args_size= GEARMAND_PACKET_HEADER_SIZE;
  150. memcpy(packet->args, data, GEARMAND_PACKET_HEADER_SIZE);
  151. if (gearmand_failed(ret_ptr= gearmand_packet_unpack_header(packet)))
  152. {
  153. return 0;
  154. }
  155. used_size= GEARMAND_PACKET_HEADER_SIZE;
  156. }
  157. else
  158. {
  159. used_size= 0;
  160. }
  161. while (packet->argc != gearman_command_info(packet->command)->argc)
  162. {
  163. if (packet->argc != (gearman_command_info(packet->command)->argc - 1) or
  164. gearman_command_info(packet->command)->data)
  165. {
  166. uint8_t* ptr= (uint8_t *)memchr(((uint8_t *)data) +used_size, 0,
  167. data_size -used_size);
  168. if (ptr == NULL)
  169. {
  170. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  171. "Possible protocol error for %s, received only %u args",
  172. gearman_command_info(packet->command)->name, packet->argc);
  173. ret_ptr= GEARMAND_IO_WAIT;
  174. return used_size;
  175. }
  176. size_t arg_size= size_t(ptr - (((uint8_t *)data) + used_size)) +1;
  177. if (gearmand_failed((ret_ptr= gearmand_packet_create(packet, ((uint8_t *)data) + used_size, arg_size))))
  178. {
  179. return used_size;
  180. }
  181. packet->data_size-= arg_size;
  182. used_size+= arg_size;
  183. }
  184. else
  185. {
  186. if ((data_size - used_size) < packet->data_size)
  187. {
  188. ret_ptr= GEARMAND_IO_WAIT;
  189. return used_size;
  190. }
  191. ret_ptr= gearmand_packet_create(packet, ((uint8_t *)data) + used_size, packet->data_size);
  192. if (gearmand_failed(ret_ptr))
  193. {
  194. return used_size;
  195. }
  196. used_size+= packet->data_size;
  197. packet->data_size= 0;
  198. }
  199. }
  200. if (packet->command == GEARMAN_COMMAND_ECHO_RES)
  201. {
  202. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  203. "GEAR length: %" PRIu64 " gearmand_command_t: %s echo: %.*s",
  204. uint64_t(packet->data_size),
  205. gearman_strcommand(packet->command),
  206. int(packet->data_size),
  207. packet->data);
  208. }
  209. else if (packet->command == GEARMAN_COMMAND_TEXT)
  210. {
  211. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  212. "GEAR length: %" PRIu64 " gearmand_command_t: %s text: %.*s",
  213. uint64_t(packet->data_size),
  214. gearman_strcommand(packet->command),
  215. int(packet->data_size),
  216. packet->data);
  217. }
  218. else
  219. {
  220. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  221. "GEAR length: %" PRIu64 " gearmand_command_t: %s",
  222. uint64_t(packet->data_size),
  223. gearman_strcommand(packet->command));
  224. }
  225. ret_ptr= GEARMAND_SUCCESS;
  226. return used_size;
  227. }
  228. size_t pack(const gearmand_packet_st *packet,
  229. gearman_server_con_st*,
  230. void *data, const size_t data_size,
  231. gearmand_error_t& ret_ptr)
  232. {
  233. if (packet->command == GEARMAN_COMMAND_ECHO_RES)
  234. {
  235. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  236. "GEAR length: %" PRIu64 " gearmand_command_t: %s echo: %.*",
  237. uint64_t(packet->data_size),
  238. gearman_strcommand(packet->command),
  239. int(packet->data_size),
  240. packet->data);
  241. }
  242. else
  243. {
  244. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  245. "GEAR length: %" PRIu64 " gearmand_command_t: %s",
  246. uint64_t(packet->data_size),
  247. gearman_strcommand(packet->command));
  248. }
  249. if (packet->args_size == 0)
  250. {
  251. ret_ptr= GEARMAND_SUCCESS;
  252. return 0;
  253. }
  254. if (packet->args_size > data_size)
  255. {
  256. ret_ptr= GEARMAND_FLUSH_DATA;
  257. return 0;
  258. }
  259. memcpy(data, packet->args, packet->args_size);
  260. ret_ptr= GEARMAND_SUCCESS;
  261. return packet->args_size;
  262. }
  263. private:
  264. };
  265. static Geartext gear_context;
  266. static gearmand_error_t _gear_con_add(gearman_server_con_st *connection)
  267. {
  268. #if defined(HAVE_CYASSL) && HAVE_CYASSL
  269. assert(Gearmand()->ctx_ssl());
  270. if ((connection->_ssl= CyaSSL_new(Gearmand()->ctx_ssl())) == NULL)
  271. {
  272. return gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "CyaSSL_new() failed");
  273. }
  274. CyaSSL_set_fd(connection->_ssl, connection->con.fd);
  275. bool connecting= true;
  276. while (connecting)
  277. {
  278. if (CyaSSL_accept(connection->_ssl) == SSL_SUCCESS)
  279. {
  280. connecting= false;
  281. break;
  282. }
  283. if (CyaSSL_get_error(connection->_ssl, 0) != SSL_ERROR_WANT_READ)
  284. {
  285. int cyassl_error= CyaSSL_get_error(connection->_ssl, 0);
  286. char cyassl_error_buffer[1024]= { 0 };
  287. CyaSSL_ERR_error_string(cyassl_error, cyassl_error_buffer);
  288. return gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, "%s(%d)", cyassl_error_buffer, cyassl_error);
  289. }
  290. }
  291. gearmand_log_info(GEARMAN_DEFAULT_LOG_PARAM, "GearSSL connection made: %d", connection->con.fd);
  292. #endif
  293. connection->set_protocol(&gear_context);
  294. return GEARMAND_SUCCESS;
  295. }
  296. namespace gearmand {
  297. namespace protocol {
  298. Gear::Gear() :
  299. Plugin("Gear")
  300. {
  301. command_line_options().add_options()
  302. ("port,p", boost::program_options::value(&_port)->default_value(GEARMAN_DEFAULT_TCP_PORT_STRING),
  303. "Port the server should listen on.");
  304. }
  305. Gear::~Gear()
  306. {
  307. }
  308. gearmand_error_t Gear::start(gearmand_st *gearmand)
  309. {
  310. gearmand_error_t rc;
  311. if (_port.compare(GEARMAN_DEFAULT_TCP_PORT_STRING) == 0)
  312. {
  313. char* service;
  314. if ((service= getenv("GEARMAND_PORT")) and service[0])
  315. {
  316. _port.clear();
  317. _port.append(service);
  318. }
  319. }
  320. if (_port.empty())
  321. {
  322. const char* service= GEARMAN_DEFAULT_TCP_PORT_STRING;
  323. struct servent *gearman_servent;
  324. if ((gearman_servent= getservbyname(GEARMAN_DEFAULT_TCP_SERVICE, NULL)))
  325. {
  326. if (gearman_servent and gearman_servent->s_name)
  327. {
  328. service= gearman_servent->s_name;
  329. }
  330. }
  331. _port.clear();
  332. _port.append(service);
  333. }
  334. gearmand_log_info(GEARMAN_DEFAULT_LOG_PARAM, "Initializing Gear on port %s", _port.c_str());
  335. #if defined(HAVE_CYASSL) && HAVE_CYASSL
  336. {
  337. if (access(CERT_PEM, R_OK) == -1)
  338. {
  339. assert("access()" == NULL);
  340. }
  341. if (CyaSSL_CTX_use_certificate_file(gearmand->ctx_ssl(), CERT_PEM, SSL_FILETYPE_PEM) != SSL_SUCCESS)
  342. {
  343. gearmand_fatal("CyaSSL_CTX_use_certificate_file() cannot obtain certificate");
  344. }
  345. if (access(CERT_KEY_PEM, R_OK) == -1)
  346. {
  347. gearmand_fatal("access(CERT_KEY_PEM, R_OK) == -1");
  348. }
  349. if (CyaSSL_CTX_use_PrivateKey_file(gearmand->ctx_ssl(), CERT_KEY_PEM, SSL_FILETYPE_PEM) != SSL_SUCCESS)
  350. {
  351. gearmand_fatal("CyaSSL_CTX_use_PrivateKey_file() cannot obtain certificate");
  352. }
  353. assert(gearmand->ctx_ssl());
  354. }
  355. #endif
  356. rc= gearmand_port_add(gearmand, _port.c_str(), _gear_con_add);
  357. return rc;
  358. }
  359. } // namespace protocol
  360. } // namespace gearmand