packet.cc 14 KB

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