packet.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /* Gearman server and library
  2. * Copyright (C) 2008 Brian Aker, Eric Day
  3. * All rights reserved.
  4. *
  5. * Use and distribution licensed under the BSD license. See
  6. * the COPYING file in the parent directory for full text.
  7. */
  8. /**
  9. * @file
  10. * @brief Server connection definitions
  11. */
  12. #include <libgearman-server/common.h>
  13. #define GEARMAN_CORE
  14. #include <libgearman/command.h>
  15. #include <libgearman-server/fifo.h>
  16. #include <assert.h>
  17. #include <cstring>
  18. #ifndef __INTEL_COMPILER
  19. #pragma GCC diagnostic ignored "-Wold-style-cast"
  20. #endif
  21. /*
  22. * Public definitions
  23. */
  24. gearman_server_packet_st *
  25. gearman_server_packet_create(gearman_server_thread_st *thread,
  26. bool from_thread)
  27. {
  28. gearman_server_packet_st *server_packet= NULL;
  29. if (from_thread && Server->flags.threaded)
  30. {
  31. if (thread->free_packet_count > 0)
  32. {
  33. server_packet= thread->free_packet_list;
  34. thread->free_packet_list= server_packet->next;
  35. thread->free_packet_count--;
  36. }
  37. }
  38. else
  39. {
  40. if (Server->free_packet_count > 0)
  41. {
  42. server_packet= Server->free_packet_list;
  43. Server->free_packet_list= server_packet->next;
  44. Server->free_packet_count--;
  45. }
  46. }
  47. if (server_packet == NULL)
  48. {
  49. server_packet= (gearman_server_packet_st *)malloc(sizeof(gearman_server_packet_st));
  50. if (server_packet == NULL)
  51. {
  52. gearmand_perror("malloc");
  53. return NULL;
  54. }
  55. }
  56. server_packet->next= NULL;
  57. return server_packet;
  58. }
  59. void gearman_server_packet_free(gearman_server_packet_st *packet,
  60. gearman_server_thread_st *thread,
  61. bool from_thread)
  62. {
  63. if (from_thread && Server->flags.threaded)
  64. {
  65. if (thread->free_packet_count < GEARMAN_MAX_FREE_SERVER_PACKET)
  66. {
  67. packet->next= thread->free_packet_list;
  68. thread->free_packet_list= packet;
  69. thread->free_packet_count++;
  70. }
  71. else
  72. {
  73. gearmand_crazy("free");
  74. free(packet);
  75. }
  76. }
  77. else
  78. {
  79. if (Server->free_packet_count < GEARMAN_MAX_FREE_SERVER_PACKET)
  80. {
  81. packet->next= Server->free_packet_list;
  82. Server->free_packet_list= packet;
  83. Server->free_packet_count++;
  84. }
  85. else
  86. {
  87. gearmand_crazy("free");
  88. free(packet);
  89. }
  90. }
  91. }
  92. gearmand_error_t gearman_server_io_packet_add(gearman_server_con_st *con,
  93. bool take_data,
  94. enum gearman_magic_t magic,
  95. gearman_command_t command,
  96. const void *arg, ...)
  97. {
  98. gearman_server_packet_st *server_packet;
  99. va_list ap;
  100. size_t arg_size;
  101. gearmand_error_t ret;
  102. server_packet= gearman_server_packet_create(con->thread, false);
  103. if (server_packet == NULL)
  104. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  105. gearmand_packet_init(&(server_packet->packet), magic, command);
  106. server_packet->packet.magic= magic;
  107. server_packet->packet.command= command;
  108. va_start(ap, arg);
  109. while (arg != NULL)
  110. {
  111. arg_size= va_arg(ap, size_t);
  112. ret= gearmand_packet_create(&(server_packet->packet), arg, arg_size);
  113. if (ret != GEARMAN_SUCCESS)
  114. {
  115. va_end(ap);
  116. gearmand_packet_free(&(server_packet->packet));
  117. gearman_server_packet_free(server_packet, con->thread, false);
  118. return ret;
  119. }
  120. arg = va_arg(ap, void *);
  121. }
  122. va_end(ap);
  123. ret= gearmand_packet_pack_header(&(server_packet->packet));
  124. if (ret != GEARMAN_SUCCESS)
  125. {
  126. gearmand_packet_free(&(server_packet->packet));
  127. gearman_server_packet_free(server_packet, con->thread, false);
  128. return ret;
  129. }
  130. if (take_data)
  131. {
  132. server_packet->packet.options.free_data= true;
  133. }
  134. (void) pthread_mutex_lock(&con->thread->lock);
  135. gearmand_server_con_fifo_add(con, server_packet);
  136. (void) pthread_mutex_unlock(&con->thread->lock);
  137. gearman_server_con_io_add(con);
  138. return GEARMAN_SUCCESS;
  139. }
  140. void gearman_server_io_packet_remove(gearman_server_con_st *con)
  141. {
  142. gearman_server_packet_st *server_packet= con->io_packet_list;
  143. gearmand_packet_free(&(server_packet->packet));
  144. (void) pthread_mutex_lock(&con->thread->lock);
  145. gearmand_server_con_fifo_free(con, server_packet);
  146. (void) pthread_mutex_unlock(&con->thread->lock);
  147. gearman_server_packet_free(server_packet, con->thread, true);
  148. }
  149. void gearman_server_proc_packet_add(gearman_server_con_st *con,
  150. gearman_server_packet_st *packet)
  151. {
  152. (void) pthread_mutex_lock(&con->thread->lock);
  153. gearmand_server_con_fifo_proc_add(con, packet);
  154. (void) pthread_mutex_unlock(&con->thread->lock);
  155. gearman_server_con_proc_add(con);
  156. }
  157. gearman_server_packet_st *
  158. gearman_server_proc_packet_remove(gearman_server_con_st *con)
  159. {
  160. gearman_server_packet_st *server_packet= con->proc_packet_list;
  161. if (server_packet == NULL)
  162. return NULL;
  163. (void) pthread_mutex_lock(&con->thread->lock);
  164. gearmand_server_con_fifo_proc_free(con, server_packet);
  165. (void) pthread_mutex_unlock(&con->thread->lock);
  166. return server_packet;
  167. }
  168. const char *gearmand_strcommand(gearmand_packet_st *packet)
  169. {
  170. assert(packet);
  171. return gearman_command_info(packet->command)->name;
  172. }
  173. inline static gearmand_error_t packet_create_arg(gearmand_packet_st *packet,
  174. const void *arg, size_t arg_size)
  175. {
  176. void *new_args;
  177. size_t offset;
  178. if (packet->argc == gearman_command_info(packet->command)->argc &&
  179. (not (gearman_command_info(packet->command)->data) ||
  180. packet->data != NULL))
  181. {
  182. gearmand_log_error("too many arguments for command(%s)", gearman_command_info(packet->command)->name);
  183. return GEARMAN_TOO_MANY_ARGS;
  184. }
  185. if (packet->argc == gearman_command_info(packet->command)->argc)
  186. {
  187. packet->data= static_cast<const char *>(arg);
  188. packet->data_size= arg_size;
  189. return GEARMAN_SUCCESS;
  190. }
  191. if (packet->args_size == 0 && packet->magic != GEARMAN_MAGIC_TEXT)
  192. packet->args_size= GEARMAN_PACKET_HEADER_SIZE;
  193. if ((packet->args_size + arg_size) < GEARMAN_ARGS_BUFFER_SIZE)
  194. {
  195. packet->args= packet->args_buffer;
  196. }
  197. else
  198. {
  199. if (packet->args == packet->args_buffer)
  200. packet->args= NULL;
  201. new_args= realloc(packet->args, packet->args_size + arg_size);
  202. if (new_args == NULL)
  203. {
  204. gearmand_perror("realloc");
  205. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  206. }
  207. if (packet->args_size > 0)
  208. memcpy(new_args, packet->args_buffer, packet->args_size);
  209. packet->args= static_cast<char *>(new_args);
  210. }
  211. memcpy(packet->args + packet->args_size, arg, arg_size);
  212. packet->args_size+= arg_size;
  213. packet->arg_size[packet->argc]= arg_size;
  214. packet->argc++;
  215. if (packet->magic == GEARMAN_MAGIC_TEXT)
  216. {
  217. offset= 0;
  218. }
  219. else
  220. {
  221. offset= GEARMAN_PACKET_HEADER_SIZE;
  222. }
  223. for (uint8_t x= 0; x < packet->argc; x++)
  224. {
  225. packet->arg[x]= packet->args + offset;
  226. offset+= packet->arg_size[x];
  227. }
  228. return GEARMAN_SUCCESS;
  229. }
  230. /** @} */
  231. /*
  232. * Public Definitions
  233. */
  234. void gearmand_packet_init(gearmand_packet_st *packet, enum gearman_magic_t magic, gearman_command_t command)
  235. {
  236. assert(packet);
  237. packet->options.complete= false;
  238. packet->options.free_data= false;
  239. packet->magic= magic;
  240. packet->command= command;
  241. packet->argc= 0;
  242. packet->args_size= 0;
  243. packet->data_size= 0;
  244. packet->args= NULL;
  245. packet->data= NULL;
  246. }
  247. gearmand_error_t gearmand_packet_create(gearmand_packet_st *packet,
  248. const void *arg, size_t arg_size)
  249. {
  250. return packet_create_arg(packet, arg, arg_size);
  251. }
  252. void gearmand_packet_free(gearmand_packet_st *packet)
  253. {
  254. if (packet->args != packet->args_buffer && packet->args != NULL)
  255. {
  256. gearmand_crazy("free");
  257. free(packet->args);
  258. packet->args= NULL;
  259. }
  260. if (packet->options.free_data && packet->data != NULL)
  261. {
  262. gearmand_crazy("free");
  263. free((void *)packet->data); //@todo fix the need for the casting.
  264. packet->data= NULL;
  265. }
  266. }
  267. gearmand_error_t gearmand_packet_pack_header(gearmand_packet_st *packet)
  268. {
  269. uint64_t length_64;
  270. uint32_t tmp;
  271. if (packet->magic == GEARMAN_MAGIC_TEXT)
  272. {
  273. packet->options.complete= true;
  274. return GEARMAN_SUCCESS;
  275. }
  276. if (packet->args_size == 0)
  277. {
  278. packet->args= packet->args_buffer;
  279. packet->args_size= GEARMAN_PACKET_HEADER_SIZE;
  280. }
  281. switch (packet->magic)
  282. {
  283. case GEARMAN_MAGIC_TEXT:
  284. break;
  285. case GEARMAN_MAGIC_REQUEST:
  286. memcpy(packet->args, "\0REQ", 4);
  287. break;
  288. case GEARMAN_MAGIC_RESPONSE:
  289. memcpy(packet->args, "\0RES", 4);
  290. break;
  291. default:
  292. gearmand_error("invalid magic value");
  293. return GEARMAN_INVALID_MAGIC;
  294. }
  295. if (packet->command == GEARMAN_COMMAND_TEXT ||
  296. packet->command >= GEARMAN_COMMAND_MAX)
  297. {
  298. gearmand_error("invalid command value");
  299. return GEARMAN_INVALID_COMMAND;
  300. }
  301. tmp= packet->command;
  302. tmp= htonl(tmp);
  303. memcpy(packet->args + 4, &tmp, 4);
  304. length_64= packet->args_size + packet->data_size - GEARMAN_PACKET_HEADER_SIZE;
  305. // Check for overflow on 32bit(portable?).
  306. if (length_64 >= UINT32_MAX || length_64 < packet->data_size)
  307. {
  308. gearmand_error("data size too too long");
  309. return GEARMAN_ARGUMENT_TOO_LARGE;
  310. }
  311. tmp= (uint32_t)length_64;
  312. tmp= htonl(tmp);
  313. memcpy(packet->args + 8, &tmp, 4);
  314. packet->options.complete= true;
  315. return GEARMAN_SUCCESS;
  316. }
  317. static gearmand_error_t gearmand_packet_unpack_header(gearmand_packet_st *packet)
  318. {
  319. uint32_t tmp;
  320. if (not memcmp(packet->args, "\0REQ", 4))
  321. {
  322. packet->magic= GEARMAN_MAGIC_REQUEST;
  323. }
  324. else if (not memcmp(packet->args, "\0RES", 4))
  325. {
  326. packet->magic= GEARMAN_MAGIC_RESPONSE;
  327. }
  328. else
  329. {
  330. gearmand_error("invalid magic value");
  331. return GEARMAN_INVALID_MAGIC;
  332. }
  333. memcpy(&tmp, packet->args + 4, 4);
  334. packet->command= static_cast<gearman_command_t>(ntohl(tmp));
  335. if (packet->command == GEARMAN_COMMAND_TEXT ||
  336. packet->command >= GEARMAN_COMMAND_MAX)
  337. {
  338. gearmand_error("invalid command value");
  339. return GEARMAN_INVALID_COMMAND;
  340. }
  341. memcpy(&tmp, packet->args + 8, 4);
  342. packet->data_size= ntohl(tmp);
  343. return GEARMAN_SUCCESS;
  344. }
  345. size_t gearmand_packet_pack(const gearmand_packet_st *packet,
  346. gearman_server_con_st *con __attribute__ ((unused)),
  347. void *data, size_t data_size,
  348. gearmand_error_t *ret_ptr)
  349. {
  350. if (packet->args_size == 0)
  351. {
  352. *ret_ptr= GEARMAN_SUCCESS;
  353. return 0;
  354. }
  355. if (packet->args_size > data_size)
  356. {
  357. *ret_ptr= GEARMAN_FLUSH_DATA;
  358. return 0;
  359. }
  360. memcpy(data, packet->args, packet->args_size);
  361. *ret_ptr= GEARMAN_SUCCESS;
  362. return packet->args_size;
  363. }
  364. size_t gearmand_packet_unpack(gearmand_packet_st *packet,
  365. gearman_server_con_st *con __attribute__ ((unused)),
  366. const void *data, size_t data_size,
  367. gearmand_error_t *ret_ptr)
  368. {
  369. uint8_t *ptr;
  370. size_t used_size;
  371. if (packet->args_size == 0)
  372. {
  373. if (data_size > 0 && ((uint8_t *)data)[0] != 0)
  374. {
  375. /* Try to parse a text-based command. */
  376. ptr= (uint8_t *)memchr(data, '\n', data_size);
  377. if (ptr == NULL)
  378. {
  379. *ret_ptr= GEARMAN_IO_WAIT;
  380. return 0;
  381. }
  382. packet->magic= GEARMAN_MAGIC_TEXT;
  383. packet->command= GEARMAN_COMMAND_TEXT;
  384. used_size= (size_t)(ptr - ((uint8_t *)data)) + 1;
  385. *ptr= 0;
  386. if (used_size > 1 && *(ptr - 1) == '\r')
  387. *(ptr - 1)= 0;
  388. size_t arg_size;
  389. for (arg_size= used_size, ptr= (uint8_t *)data; ptr != NULL; data= ptr)
  390. {
  391. ptr= (uint8_t *)memchr(data, ' ', arg_size);
  392. if (ptr != NULL)
  393. {
  394. *ptr= 0;
  395. ptr++;
  396. while (*ptr == ' ')
  397. ptr++;
  398. arg_size-= (size_t)(ptr - ((uint8_t *)data));
  399. }
  400. *ret_ptr= packet_create_arg(packet, data, ptr == NULL ? arg_size :
  401. (size_t)(ptr - ((uint8_t *)data)));
  402. if (*ret_ptr != GEARMAN_SUCCESS)
  403. return used_size;
  404. }
  405. return used_size;
  406. }
  407. else if (data_size < GEARMAN_PACKET_HEADER_SIZE)
  408. {
  409. *ret_ptr= GEARMAN_IO_WAIT;
  410. return 0;
  411. }
  412. packet->args= packet->args_buffer;
  413. packet->args_size= GEARMAN_PACKET_HEADER_SIZE;
  414. memcpy(packet->args, data, GEARMAN_PACKET_HEADER_SIZE);
  415. *ret_ptr= gearmand_packet_unpack_header(packet);
  416. if (gearmand_failed(*ret_ptr))
  417. {
  418. return 0;
  419. }
  420. used_size= GEARMAN_PACKET_HEADER_SIZE;
  421. }
  422. else
  423. {
  424. used_size= 0;
  425. }
  426. while (packet->argc != gearman_command_info(packet->command)->argc)
  427. {
  428. if (packet->argc != (gearman_command_info(packet->command)->argc - 1) ||
  429. gearman_command_info(packet->command)->data)
  430. {
  431. ptr= (uint8_t *)memchr(((uint8_t *)data) + used_size, 0, data_size - used_size);
  432. if (not ptr)
  433. {
  434. gearmand_log_crazy("Possible protocol error for %s, recieved only %u args", gearman_command_info(packet->command)->name, packet->argc);
  435. *ret_ptr= GEARMAN_IO_WAIT;
  436. return used_size;
  437. }
  438. size_t arg_size= (size_t)(ptr - (((uint8_t *)data) + used_size)) + 1;
  439. *ret_ptr= packet_create_arg(packet, ((uint8_t *)data) + used_size, arg_size);
  440. if (gearmand_failed(*ret_ptr))
  441. return used_size;
  442. packet->data_size-= arg_size;
  443. used_size+= arg_size;
  444. }
  445. else
  446. {
  447. if ((data_size - used_size) < packet->data_size)
  448. {
  449. *ret_ptr= GEARMAN_IO_WAIT;
  450. return used_size;
  451. }
  452. *ret_ptr= packet_create_arg(packet, ((uint8_t *)data) + used_size, packet->data_size);
  453. if (gearmand_failed(*ret_ptr))
  454. {
  455. return used_size;
  456. }
  457. used_size+= packet->data_size;
  458. packet->data_size= 0;
  459. }
  460. }
  461. *ret_ptr= GEARMAN_SUCCESS;
  462. return used_size;
  463. }