protocol.cc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. #include <libgearman-server/plugins/protocol/gear/protocol.h>
  48. #include "libgearman/command.h"
  49. static gearmand_error_t gearmand_packet_unpack_header(gearmand_packet_st *packet)
  50. {
  51. uint32_t tmp;
  52. if (memcmp(packet->args, "\0REQ", 4) == 0)
  53. {
  54. packet->magic= GEARMAN_MAGIC_REQUEST;
  55. }
  56. else if (memcmp(packet->args, "\0RES", 4) == 0)
  57. {
  58. packet->magic= GEARMAN_MAGIC_RESPONSE;
  59. }
  60. else
  61. {
  62. gearmand_warning("invalid magic value");
  63. return GEARMAND_INVALID_MAGIC;
  64. }
  65. memcpy(&tmp, packet->args + 4, 4);
  66. packet->command= static_cast<gearman_command_t>(ntohl(tmp));
  67. if (packet->command == GEARMAN_COMMAND_TEXT ||
  68. packet->command >= GEARMAN_COMMAND_MAX)
  69. {
  70. gearmand_error("invalid command value");
  71. return GEARMAND_INVALID_COMMAND;
  72. }
  73. memcpy(&tmp, packet->args + 8, 4);
  74. packet->data_size= ntohl(tmp);
  75. return GEARMAND_SUCCESS;
  76. }
  77. class Geartext : public gearmand::protocol::Context {
  78. public:
  79. ~Geartext()
  80. { }
  81. bool is_owner()
  82. {
  83. return false;
  84. }
  85. void notify(gearman_server_con_st *)
  86. {
  87. gearmand_info("Gear connection disconnected");
  88. }
  89. size_t unpack(gearmand_packet_st *packet,
  90. gearman_server_con_st *,
  91. const void *data, const size_t data_size,
  92. gearmand_error_t& ret_ptr)
  93. {
  94. size_t used_size;
  95. gearmand_info("Gear unpack");
  96. if (packet->args_size == 0)
  97. {
  98. if (data_size > 0 && ((uint8_t *)data)[0] != 0)
  99. {
  100. /* Try to parse a text-based command. */
  101. uint8_t* ptr= (uint8_t *)memchr(data, '\n', data_size);
  102. if (ptr == NULL)
  103. {
  104. ret_ptr= GEARMAND_IO_WAIT;
  105. return 0;
  106. }
  107. packet->magic= GEARMAN_MAGIC_TEXT;
  108. packet->command= GEARMAN_COMMAND_TEXT;
  109. used_size= size_t(ptr - ((uint8_t *)data)) +1;
  110. *ptr= 0;
  111. if (used_size > 1 && *(ptr - 1) == '\r')
  112. {
  113. *(ptr - 1)= 0;
  114. }
  115. size_t arg_size;
  116. for (arg_size= used_size, ptr= (uint8_t *)data; ptr != NULL; data= ptr)
  117. {
  118. ptr= (uint8_t *)memchr(data, ' ', arg_size);
  119. if (ptr != NULL)
  120. {
  121. *ptr= 0;
  122. ptr++;
  123. while (*ptr == ' ')
  124. {
  125. ptr++;
  126. }
  127. arg_size-= size_t(ptr - ((uint8_t *)data));
  128. }
  129. ret_ptr= gearmand_packet_create(packet, data, ptr == NULL ? arg_size :
  130. size_t(ptr - ((uint8_t *)data)));
  131. if (ret_ptr != GEARMAND_SUCCESS)
  132. {
  133. return used_size;
  134. }
  135. }
  136. return used_size;
  137. }
  138. else if (data_size < GEARMAND_PACKET_HEADER_SIZE)
  139. {
  140. ret_ptr= GEARMAND_IO_WAIT;
  141. return 0;
  142. }
  143. packet->args= packet->args_buffer;
  144. packet->args_size= GEARMAND_PACKET_HEADER_SIZE;
  145. memcpy(packet->args, data, GEARMAND_PACKET_HEADER_SIZE);
  146. if (gearmand_failed(ret_ptr= gearmand_packet_unpack_header(packet)))
  147. {
  148. return 0;
  149. }
  150. used_size= GEARMAND_PACKET_HEADER_SIZE;
  151. }
  152. else
  153. {
  154. used_size= 0;
  155. }
  156. while (packet->argc != gearman_command_info(packet->command)->argc)
  157. {
  158. if (packet->argc != (gearman_command_info(packet->command)->argc - 1) or
  159. gearman_command_info(packet->command)->data)
  160. {
  161. uint8_t* ptr= (uint8_t *)memchr(((uint8_t *)data) +used_size, 0,
  162. data_size -used_size);
  163. if (ptr == NULL)
  164. {
  165. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  166. "Possible protocol error for %s, received only %u args",
  167. gearman_command_info(packet->command)->name, packet->argc);
  168. ret_ptr= GEARMAND_IO_WAIT;
  169. return used_size;
  170. }
  171. size_t arg_size= size_t(ptr - (((uint8_t *)data) + used_size)) +1;
  172. if (gearmand_failed((ret_ptr= gearmand_packet_create(packet, ((uint8_t *)data) + used_size, arg_size))))
  173. {
  174. return used_size;
  175. }
  176. packet->data_size-= arg_size;
  177. used_size+= arg_size;
  178. }
  179. else
  180. {
  181. if ((data_size - used_size) < packet->data_size)
  182. {
  183. ret_ptr= GEARMAND_IO_WAIT;
  184. return used_size;
  185. }
  186. ret_ptr= gearmand_packet_create(packet, ((uint8_t *)data) + used_size, packet->data_size);
  187. if (gearmand_failed(ret_ptr))
  188. {
  189. return used_size;
  190. }
  191. used_size+= packet->data_size;
  192. packet->data_size= 0;
  193. }
  194. }
  195. if (packet->command == GEARMAN_COMMAND_ECHO_RES)
  196. {
  197. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  198. "GEAR length: %" PRIu64 " gearmand_command_t: %s echo: %.*s",
  199. uint64_t(packet->data_size),
  200. gearman_strcommand(packet->command),
  201. int(packet->data_size),
  202. packet->data);
  203. }
  204. else if (packet->command == GEARMAN_COMMAND_TEXT)
  205. {
  206. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  207. "GEAR length: %" PRIu64 " gearmand_command_t: %s text: %.*s",
  208. uint64_t(packet->data_size),
  209. gearman_strcommand(packet->command),
  210. int(packet->data_size),
  211. packet->data);
  212. }
  213. else
  214. {
  215. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  216. "GEAR length: %" PRIu64 " gearmand_command_t: %s",
  217. uint64_t(packet->data_size),
  218. gearman_strcommand(packet->command));
  219. }
  220. ret_ptr= GEARMAND_SUCCESS;
  221. return used_size;
  222. }
  223. size_t pack(const gearmand_packet_st *packet,
  224. gearman_server_con_st*,
  225. void *data, const size_t data_size,
  226. gearmand_error_t& ret_ptr)
  227. {
  228. if (packet->command == GEARMAN_COMMAND_ECHO_RES)
  229. {
  230. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  231. "GEAR length: %" PRIu64 " gearmand_command_t: %s echo: %.*",
  232. uint64_t(packet->data_size),
  233. gearman_strcommand(packet->command),
  234. int(packet->data_size),
  235. packet->data);
  236. }
  237. else
  238. {
  239. gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM,
  240. "GEAR length: %" PRIu64 " gearmand_command_t: %s",
  241. uint64_t(packet->data_size),
  242. gearman_strcommand(packet->command));
  243. }
  244. if (packet->args_size == 0)
  245. {
  246. ret_ptr= GEARMAND_SUCCESS;
  247. return 0;
  248. }
  249. if (packet->args_size > data_size)
  250. {
  251. ret_ptr= GEARMAND_FLUSH_DATA;
  252. return 0;
  253. }
  254. memcpy(data, packet->args, packet->args_size);
  255. ret_ptr= GEARMAND_SUCCESS;
  256. return packet->args_size;
  257. }
  258. private:
  259. };
  260. static Geartext gear_context;
  261. static gearmand_error_t _gear_con_add(gearman_server_con_st *connection)
  262. {
  263. gearmand_info("Gear connection made");
  264. connection->set_protocol(&gear_context);
  265. return GEARMAND_SUCCESS;
  266. }
  267. namespace gearmand {
  268. namespace protocol {
  269. Gear::Gear() :
  270. Plugin("Gear")
  271. {
  272. command_line_options().add_options()
  273. ("port,p", boost::program_options::value(&_port)->default_value(GEARMAN_DEFAULT_TCP_PORT_STRING),
  274. "Port the server should listen on.");
  275. }
  276. Gear::~Gear()
  277. {
  278. }
  279. gearmand_error_t Gear::start(gearmand_st *gearmand)
  280. {
  281. gearmand_error_t rc;
  282. if (_port.compare(GEARMAN_DEFAULT_TCP_PORT_STRING) == 0)
  283. {
  284. char* service;
  285. if ((service= getenv("GEARMAND_PORT")) and service[0])
  286. {
  287. _port.clear();
  288. _port.append(service);
  289. }
  290. }
  291. if (_port.empty())
  292. {
  293. const char* service= GEARMAN_DEFAULT_TCP_PORT_STRING;
  294. struct servent *gearman_servent;
  295. if ((gearman_servent= getservbyname(GEARMAN_DEFAULT_TCP_SERVICE, NULL)))
  296. {
  297. if (gearman_servent and gearman_servent->s_name)
  298. {
  299. service= gearman_servent->s_name;
  300. }
  301. }
  302. _port.clear();
  303. _port.append(service);
  304. }
  305. gearmand_log_info(GEARMAN_DEFAULT_LOG_PARAM, "Initializing Gear on port %s", _port.c_str());
  306. rc= gearmand_port_add(gearmand, _port.c_str(), _gear_con_add);
  307. return rc;
  308. }
  309. } // namespace protocol
  310. } // namespace gearmand