packet.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 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. /**
  39. * @file
  40. * @brief Packet Definitions
  41. */
  42. #include <libgearman/common.h>
  43. #include <libgearman/allocator.hpp>
  44. #include <libgearman/universal.hpp>
  45. #include <libgearman/command.h>
  46. #include <libgearman/packet.hpp>
  47. #include <cerrno>
  48. #include <cstdlib>
  49. #include <cstring>
  50. #include <memory>
  51. /**
  52. * @addtogroup gearman_packet_static Static Packet Declarations
  53. * @ingroup gearman_packet
  54. * @{
  55. */
  56. #ifndef __INTEL_COMPILER
  57. #pragma GCC diagnostic ignored "-Wold-style-cast"
  58. #endif
  59. inline static gearman_return_t packet_create_arg(gearman_packet_st *packet,
  60. const void *arg, size_t arg_size)
  61. {
  62. if (packet->argc == gearman_command_info(packet->command)->argc and
  63. (not (gearman_command_info(packet->command)->data) || packet->data != NULL))
  64. {
  65. gearman_universal_set_error(*packet->universal, GEARMAN_TOO_MANY_ARGS, AT, "too many arguments for command (%s)",
  66. gearman_command_info(packet->command)->name);
  67. return GEARMAN_TOO_MANY_ARGS;
  68. }
  69. if (packet->argc == gearman_command_info(packet->command)->argc)
  70. {
  71. packet->data= arg;
  72. packet->data_size= arg_size;
  73. return GEARMAN_SUCCESS;
  74. }
  75. if (packet->args_size == 0 && packet->magic != GEARMAN_MAGIC_TEXT)
  76. {
  77. packet->args_size= GEARMAN_PACKET_HEADER_SIZE;
  78. }
  79. if ((packet->args_size + arg_size) < GEARMAN_ARGS_BUFFER_SIZE)
  80. {
  81. packet->args= packet->args_buffer;
  82. }
  83. else
  84. {
  85. if (packet->args == packet->args_buffer)
  86. {
  87. packet->args= NULL;
  88. }
  89. char *new_args= static_cast<char *>(realloc(packet->args, packet->args_size + arg_size +1));
  90. if (not new_args)
  91. {
  92. gearman_perror(*packet->universal, "packet realloc");
  93. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  94. }
  95. if (packet->args_size > 0)
  96. {
  97. memcpy(new_args, packet->args_buffer, packet->args_size);
  98. }
  99. packet->args= new_args;
  100. }
  101. memcpy(packet->args + packet->args_size, arg, arg_size);
  102. packet->args_size+= arg_size;
  103. packet->arg_size[packet->argc]= arg_size;
  104. packet->argc++;
  105. size_t offset;
  106. if (packet->magic == GEARMAN_MAGIC_TEXT)
  107. {
  108. offset= 0;
  109. }
  110. else
  111. {
  112. offset= GEARMAN_PACKET_HEADER_SIZE;
  113. }
  114. for (uint8_t x= 0; x < packet->argc; x++)
  115. {
  116. packet->arg[x]= packet->args + offset;
  117. offset+= packet->arg_size[x];
  118. }
  119. return GEARMAN_SUCCESS;
  120. }
  121. /** @} */
  122. /*
  123. * Public Definitions
  124. */
  125. gearman_packet_st *gearman_packet_create(gearman_universal_st &universal,
  126. gearman_packet_st *packet)
  127. {
  128. if (not packet)
  129. {
  130. packet= new (std::nothrow) gearman_packet_st;
  131. if (packet == NULL)
  132. {
  133. gearman_perror(universal, "gearman_packet_st new");
  134. errno= ENOMEM;
  135. return NULL;
  136. }
  137. packet->options.allocated= true;
  138. }
  139. else
  140. {
  141. packet->options.allocated= false;
  142. packet->options.complete= false;
  143. packet->options.free_data= false;
  144. }
  145. packet->magic= GEARMAN_MAGIC_TEXT;
  146. packet->command= GEARMAN_COMMAND_TEXT;
  147. packet->argc= 0;
  148. packet->args_size= 0;
  149. packet->data_size= 0;
  150. packet->universal= &universal;
  151. if (not (universal.options.dont_track_packets))
  152. {
  153. if (universal.packet_list != NULL)
  154. universal.packet_list->prev= packet;
  155. packet->next= universal.packet_list;
  156. packet->prev= NULL;
  157. universal.packet_list= packet;
  158. universal.packet_count++;
  159. }
  160. packet->args= NULL;
  161. packet->data= NULL;
  162. return packet;
  163. }
  164. gearman_return_t gearman_packet_create_arg(gearman_packet_st& self,
  165. const void *arg, size_t arg_size)
  166. {
  167. return packet_create_arg(&self, arg, arg_size);
  168. }
  169. gearman_return_t gearman_packet_create_args(gearman_universal_st& universal,
  170. gearman_packet_st& packet,
  171. enum gearman_magic_t magic,
  172. gearman_command_t command,
  173. const void *args[],
  174. const size_t args_size[],
  175. size_t args_count)
  176. {
  177. if (not gearman_packet_create(universal, &packet))
  178. {
  179. gearman_perror(universal, "failed in gearman_packet_create()");
  180. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  181. }
  182. packet.magic= magic;
  183. packet.command= command;
  184. if (gearman_command_info(packet.command)->data)
  185. {
  186. assert_msg(args_count -1 == gearman_command_info(packet.command)->argc,
  187. "Programmer error, number of arguments incorrect for protocol");
  188. }
  189. else
  190. {
  191. assert_msg(args_count == gearman_command_info(packet.command)->argc,
  192. "Programmer error, number of arguments incorrect for protocol");
  193. }
  194. for (size_t x= 0; x < args_count; x++)
  195. {
  196. gearman_return_t ret= packet_create_arg(&packet, args[x], args_size[x]);
  197. if (gearman_failed(ret))
  198. {
  199. gearman_packet_free(&packet);
  200. return ret;
  201. }
  202. }
  203. gearman_return_t ret= gearman_packet_pack_header(&packet);
  204. if (gearman_failed(ret))
  205. {
  206. gearman_packet_free(&packet);
  207. return ret;
  208. }
  209. return ret;
  210. }
  211. void gearman_packet_free(gearman_packet_st *packet)
  212. {
  213. if (packet->args != packet->args_buffer and packet->args)
  214. {
  215. // Created with realloc
  216. free(packet->args);
  217. packet->args= NULL;
  218. }
  219. assert_msg(packet->universal,
  220. "Packet that is being freed has not been allocated, most likely this is do to freeing a gearman_task_st or other object twice");
  221. if (packet->options.free_data && packet->data)
  222. {
  223. gearman_free((*packet->universal), const_cast<void *>(packet->data));
  224. packet->data= NULL;
  225. }
  226. if (not (packet->universal->options.dont_track_packets))
  227. {
  228. if (packet->universal->packet_list == packet)
  229. packet->universal->packet_list= packet->next;
  230. if (packet->prev)
  231. packet->prev->next= packet->next;
  232. if (packet->next)
  233. packet->next->prev= packet->prev;
  234. packet->universal->packet_count--;
  235. }
  236. if (packet->options.allocated)
  237. {
  238. delete packet;
  239. }
  240. else
  241. {
  242. memset(packet, 0, sizeof(gearman_packet_st));
  243. }
  244. }
  245. gearman_return_t gearman_packet_pack_header(gearman_packet_st *packet)
  246. {
  247. if (packet->magic == GEARMAN_MAGIC_TEXT)
  248. {
  249. packet->options.complete= true;
  250. return GEARMAN_SUCCESS;
  251. }
  252. if (packet->args_size == 0)
  253. {
  254. packet->args= packet->args_buffer;
  255. packet->args_size= GEARMAN_PACKET_HEADER_SIZE;
  256. }
  257. switch (packet->magic)
  258. {
  259. case GEARMAN_MAGIC_TEXT:
  260. break;
  261. case GEARMAN_MAGIC_REQUEST:
  262. memcpy(packet->args, "\0REQ", 4);
  263. break;
  264. case GEARMAN_MAGIC_RESPONSE:
  265. memcpy(packet->args, "\0RES", 4);
  266. break;
  267. default:
  268. gearman_error(*packet->universal, GEARMAN_INVALID_MAGIC, "invalid magic value");
  269. return GEARMAN_INVALID_MAGIC;
  270. }
  271. if (packet->command == GEARMAN_COMMAND_TEXT ||
  272. packet->command >= GEARMAN_COMMAND_MAX)
  273. {
  274. gearman_error(*packet->universal, GEARMAN_INVALID_COMMAND, "invalid command value");
  275. return GEARMAN_INVALID_COMMAND;
  276. }
  277. uint32_t tmp= packet->command;
  278. tmp= htonl(tmp);
  279. // Record the command
  280. memcpy(packet->args + 4, &tmp, 4);
  281. uint64_t length_64= packet->args_size + packet->data_size - GEARMAN_PACKET_HEADER_SIZE;
  282. // Check for overflow on 32bit(portable?).
  283. if (length_64 >= UINT32_MAX || length_64 < packet->data_size)
  284. {
  285. gearman_error(*packet->universal, GEARMAN_ARGUMENT_TOO_LARGE, "data size too too long");
  286. return GEARMAN_ARGUMENT_TOO_LARGE;
  287. }
  288. tmp= uint32_t(length_64);
  289. tmp= htonl(tmp);
  290. // Record the length of the packet
  291. memcpy(packet->args + 8, &tmp, 4);
  292. packet->options.complete= true;
  293. return GEARMAN_SUCCESS;
  294. }
  295. gearman_return_t gearman_packet_unpack_header(gearman_packet_st *packet)
  296. {
  297. uint32_t tmp;
  298. if (not memcmp(packet->args, "\0REQ", 4))
  299. {
  300. packet->magic= GEARMAN_MAGIC_REQUEST;
  301. }
  302. else if (not memcmp(packet->args, "\0RES", 4))
  303. {
  304. packet->magic= GEARMAN_MAGIC_RESPONSE;
  305. }
  306. else
  307. {
  308. gearman_error(*packet->universal, GEARMAN_INVALID_MAGIC, "invalid magic value");
  309. return GEARMAN_INVALID_MAGIC;
  310. }
  311. memcpy(&tmp, packet->args + 4, 4);
  312. packet->command= (gearman_command_t)ntohl(tmp);
  313. if (packet->command == GEARMAN_COMMAND_TEXT ||
  314. packet->command >= GEARMAN_COMMAND_MAX)
  315. {
  316. gearman_error(*packet->universal, GEARMAN_INVALID_COMMAND, "invalid command value");
  317. return GEARMAN_INVALID_COMMAND;
  318. }
  319. memcpy(&tmp, packet->args + 8, 4);
  320. packet->data_size= ntohl(tmp);
  321. return GEARMAN_SUCCESS;
  322. }
  323. size_t gearman_packet_pack(const gearman_packet_st &self,
  324. void *data, size_t data_size,
  325. gearman_return_t &ret)
  326. {
  327. if (not self.args_size)
  328. {
  329. ret= GEARMAN_SUCCESS;
  330. return 0;
  331. }
  332. if (self.args_size > data_size)
  333. {
  334. ret= GEARMAN_FLUSH_DATA;
  335. return 0;
  336. }
  337. memcpy(data, self.args, self.args_size);
  338. ret= GEARMAN_SUCCESS;
  339. return self.args_size;
  340. }
  341. size_t gearman_packet_unpack(gearman_packet_st& self,
  342. const void *data, size_t data_size,
  343. gearman_return_t &ret)
  344. {
  345. size_t used_size;
  346. size_t arg_size;
  347. if (self.args_size == 0)
  348. {
  349. if (data_size > 0 && ((uint8_t *)data)[0] != 0)
  350. {
  351. /* Try to parse a text-based command. */
  352. uint8_t *ptr= (uint8_t *)memchr(data, '\n', data_size);
  353. if (not ptr)
  354. {
  355. gearman_gerror(*self.universal, GEARMAN_IO_WAIT);
  356. ret= GEARMAN_IO_WAIT;
  357. return 0;
  358. }
  359. self.magic= GEARMAN_MAGIC_TEXT;
  360. self.command= GEARMAN_COMMAND_TEXT;
  361. used_size= (size_t)(ptr - ((uint8_t *)data)) + 1;
  362. *ptr= 0;
  363. if (used_size > 1 && *(ptr - 1) == '\r')
  364. *(ptr - 1)= 0;
  365. for (arg_size= used_size, ptr= (uint8_t *)data; ptr != NULL; data= ptr)
  366. {
  367. ptr= (uint8_t *)memchr(data, ' ', arg_size);
  368. if (ptr != NULL)
  369. {
  370. *ptr= 0;
  371. ptr++;
  372. while (*ptr == ' ')
  373. {
  374. ptr++;
  375. }
  376. arg_size-= (size_t)(ptr - ((uint8_t *)data));
  377. }
  378. ret= packet_create_arg(&self, data,
  379. ptr == NULL ? arg_size : size_t(ptr - ((uint8_t *)data)));
  380. if (gearman_failed(ret))
  381. {
  382. return used_size;
  383. }
  384. }
  385. return used_size;
  386. }
  387. else if (data_size < GEARMAN_PACKET_HEADER_SIZE)
  388. {
  389. gearman_gerror(*self.universal, GEARMAN_IO_WAIT);
  390. ret= GEARMAN_IO_WAIT;
  391. return 0;
  392. }
  393. self.args= self.args_buffer;
  394. self.args_size= GEARMAN_PACKET_HEADER_SIZE;
  395. memcpy(self.args, data, GEARMAN_PACKET_HEADER_SIZE);
  396. ret= gearman_packet_unpack_header(&self);
  397. if (gearman_failed(ret))
  398. {
  399. return 0;
  400. }
  401. used_size= GEARMAN_PACKET_HEADER_SIZE;
  402. }
  403. else
  404. {
  405. used_size= 0;
  406. }
  407. while (self.argc != gearman_command_info(self.command)->argc)
  408. {
  409. if (self.argc != (gearman_command_info(self.command)->argc - 1) or
  410. gearman_command_info(self.command)->data)
  411. {
  412. uint8_t *ptr= (uint8_t *)memchr((char *)data + used_size, 0, data_size - used_size);
  413. if (not ptr)
  414. {
  415. gearman_gerror(*self.universal, GEARMAN_IO_WAIT);
  416. ret= GEARMAN_IO_WAIT;
  417. return used_size;
  418. }
  419. arg_size= (size_t)(ptr - ((uint8_t *)data + used_size)) +1;
  420. ret= packet_create_arg(&self, (uint8_t *)data + used_size, arg_size);
  421. if (gearman_failed(ret))
  422. {
  423. return used_size;
  424. }
  425. self.data_size-= arg_size;
  426. used_size+= arg_size;
  427. }
  428. else
  429. {
  430. if ((data_size - used_size) < self.data_size)
  431. {
  432. gearman_gerror(*self.universal, GEARMAN_IO_WAIT);
  433. ret= GEARMAN_IO_WAIT;
  434. return used_size;
  435. }
  436. ret= packet_create_arg(&self, ((uint8_t *)data) + used_size, self.data_size);
  437. if (gearman_failed(ret))
  438. {
  439. return used_size;
  440. }
  441. used_size+= self.data_size;
  442. self.data_size= 0;
  443. }
  444. }
  445. ret= GEARMAN_SUCCESS;
  446. return used_size;
  447. }
  448. void gearman_packet_give_data(gearman_packet_st& self,
  449. const void *data, size_t data_size)
  450. {
  451. self.data= data;
  452. self.data_size= data_size;
  453. self.options.free_data= true;
  454. }
  455. void *gearman_packet_take_data(gearman_packet_st& self, size_t *data_size)
  456. {
  457. void *data= const_cast<void *>(self.data);
  458. *data_size= self.data_size;
  459. self.data= NULL;
  460. self.data_size= 0;
  461. self.options.free_data= false;
  462. return data;
  463. }