connection.cc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2013 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 Connection Definitions
  41. */
  42. #include "gear_config.h"
  43. #include "libgearman/common.h"
  44. #include "libgearman/assert.hpp"
  45. #include "libgearman/interface/push.hpp"
  46. #include <cerrno>
  47. #include <cstdio>
  48. #include <cstdlib>
  49. #include <cstring>
  50. #include <memory>
  51. #include <unistd.h>
  52. #if HAVE_NETINET_TCP_H
  53. # include <netinet/tcp.h> /* for TCP_NODELAY */
  54. #endif
  55. #ifdef HAVE_FCNTL_H
  56. # include <fcntl.h>
  57. #endif
  58. #ifndef SOCK_CLOEXEC
  59. # define SOCK_CLOEXEC 0
  60. #endif
  61. #ifndef SOCK_NONBLOCK
  62. # define SOCK_NONBLOCK 0
  63. #endif
  64. #ifndef FD_CLOEXEC
  65. # define FD_CLOEXEC 0
  66. #endif
  67. #ifndef MSG_DONTWAIT
  68. # define MSG_DONTWAIT 0
  69. #endif
  70. static gearman_return_t gearman_connection_set_option(gearman_connection_st *connection,
  71. gearman_connection_options_t options,
  72. bool value)
  73. {
  74. switch (options)
  75. {
  76. case GEARMAN_CON_READY:
  77. connection->options.ready= value;
  78. break;
  79. case GEARMAN_CON_PACKET_IN_USE:
  80. connection->options.packet_in_use= value;
  81. break;
  82. case GEARMAN_CON_IGNORE_LOST_CONNECTION:
  83. break;
  84. case GEARMAN_CON_CLOSE_AFTER_FLUSH:
  85. break;
  86. case GEARMAN_CON_EXTERNAL_FD:
  87. case GEARMAN_CON_MAX:
  88. default:
  89. return GEARMAN_INVALID_COMMAND;
  90. }
  91. return GEARMAN_SUCCESS;
  92. }
  93. /**
  94. * @addtogroup gearman_connection_static Static Connection Declarations
  95. * @ingroup gearman_connection
  96. * @{
  97. */
  98. gearman_connection_st::gearman_connection_st(gearman_universal_st &universal_arg,
  99. gearman_connection_options_t *options_args) :
  100. state(GEARMAN_CON_UNIVERSAL_ADDRINFO),
  101. send_state(GEARMAN_CON_SEND_STATE_NONE),
  102. recv_state(GEARMAN_CON_RECV_UNIVERSAL_NONE),
  103. events(0),
  104. revents(0),
  105. fd(-1),
  106. _ssl(NULL),
  107. cached_errno(0),
  108. created_id(0),
  109. created_id_next(0),
  110. send_buffer_size(0),
  111. send_data_size(0),
  112. send_data_offset(0),
  113. recv_buffer_size(0),
  114. recv_data_size(0),
  115. recv_data_offset(0),
  116. universal(universal_arg),
  117. next(NULL),
  118. prev(NULL),
  119. context(NULL),
  120. _addrinfo(NULL),
  121. addrinfo_next(NULL),
  122. send_buffer_ptr(NULL),
  123. _recv_packet(NULL)
  124. {
  125. if (options_args)
  126. {
  127. while (*options_args != GEARMAN_CON_MAX)
  128. {
  129. gearman_connection_set_option(this, *options_args, true);
  130. options_args++;
  131. }
  132. }
  133. if (universal.con_list)
  134. {
  135. universal.con_list->prev= this;
  136. }
  137. next= universal.con_list;
  138. universal.con_list= this;
  139. universal.con_count++;
  140. send_buffer_ptr= send_buffer;
  141. recv_buffer_ptr= recv_buffer;
  142. _host[0]= 0;
  143. _service[0]= 0;
  144. }
  145. gearman_connection_st *gearman_connection_create(gearman_universal_st &universal,
  146. gearman_connection_options_t *options)
  147. {
  148. gearman_connection_st *connection= new (std::nothrow) gearman_connection_st(universal, options);
  149. if (connection == NULL)
  150. {
  151. gearman_perror(universal, "Failed at new() gearman_connection_st new");
  152. }
  153. return connection;
  154. }
  155. gearman_connection_st *gearman_connection_create_args(gearman_universal_st& universal,
  156. const char *host, const char* service_)
  157. {
  158. gearman_connection_st *connection= gearman_connection_create(universal);
  159. if (connection == NULL)
  160. {
  161. return NULL;
  162. }
  163. connection->set_host(host, service_);
  164. if (gearman_failed(connection->lookup()))
  165. {
  166. gearman_gerror(universal, GEARMAN_GETADDRINFO);
  167. delete connection;
  168. return NULL;
  169. }
  170. return connection;
  171. }
  172. gearman_connection_st *gearman_connection_create_args(gearman_universal_st& universal,
  173. const char *host, in_port_t port)
  174. {
  175. gearman_connection_st *connection= gearman_connection_create(universal, NULL);
  176. if (connection)
  177. {
  178. connection->set_host(host, port);
  179. if (gearman_failed(connection->lookup()))
  180. {
  181. gearman_gerror(universal, GEARMAN_GETADDRINFO);
  182. delete connection;
  183. return NULL;
  184. }
  185. }
  186. return connection;
  187. }
  188. gearman_connection_st *gearman_connection_copy(gearman_universal_st& universal,
  189. const gearman_connection_st& from)
  190. {
  191. gearman_connection_st *connection= gearman_connection_create(universal);
  192. if (connection)
  193. {
  194. connection->options.ready= from.options.ready;
  195. // @todo Is this right?
  196. connection->options.packet_in_use= from.options.packet_in_use;
  197. strcpy(connection->_host, from._host);
  198. strcpy(connection->_service, from._service);
  199. }
  200. return connection;
  201. }
  202. gearman_connection_st::~gearman_connection_st()
  203. {
  204. close_socket();
  205. reset_addrinfo();
  206. { // Remove from universal list
  207. if (universal.con_list == this)
  208. {
  209. universal.con_list= next;
  210. }
  211. if (prev)
  212. {
  213. prev->next= next;
  214. }
  215. if (next)
  216. {
  217. next->prev= prev;
  218. }
  219. universal.con_count--;
  220. }
  221. free_private_packet();
  222. }
  223. void gearman_connection_st::free_private_packet()
  224. {
  225. if (options.packet_in_use)
  226. {
  227. gearman_packet_free(&_packet);
  228. options.packet_in_use= false;
  229. }
  230. }
  231. /** @} */
  232. /*
  233. * Public Definitions
  234. */
  235. void gearman_connection_st::set_host(const char *host_, const in_port_t& port_)
  236. {
  237. if (port_ < 1)
  238. {
  239. set_host(host_, GEARMAN_DEFAULT_TCP_PORT_STRING);
  240. }
  241. else
  242. {
  243. snprintf(_service, sizeof(_service), "%hu", uint16_t(port_));
  244. _service[GEARMAN_NI_MAXSERV - 1]= 0;
  245. set_host(host_, _service);
  246. }
  247. }
  248. void gearman_connection_st::set_host(const char *host_, const char* service_)
  249. {
  250. reset_addrinfo();
  251. if (host_ and host_[0])
  252. { }
  253. else
  254. {
  255. host_= GEARMAN_DEFAULT_TCP_HOST;
  256. }
  257. strncpy(_host, host_, GEARMAN_NI_MAXHOST);
  258. _host[GEARMAN_NI_MAXHOST - 1]= 0;
  259. if (service_ != _service)
  260. {
  261. if (service_)
  262. {
  263. size_t string_len= strlen(service_);
  264. if (string_len == 0)
  265. {
  266. service_= GEARMAN_DEFAULT_TCP_PORT_STRING;
  267. }
  268. }
  269. else
  270. {
  271. service_= GEARMAN_DEFAULT_TCP_PORT_STRING;
  272. }
  273. strncpy(_service, service_, GEARMAN_NI_MAXSERV);
  274. _service[GEARMAN_NI_MAXSERV - 1]= 0;
  275. }
  276. }
  277. /*
  278. Do not call error within this function.
  279. */
  280. void gearman_connection_st::close_socket()
  281. {
  282. if (_ssl)
  283. {
  284. CyaSSL_free(_ssl);
  285. _ssl= NULL;
  286. }
  287. if (fd == INVALID_SOCKET)
  288. {
  289. return;
  290. }
  291. /* in case of death shutdown to avoid blocking at close_socket() */
  292. if (shutdown(fd, SHUT_RDWR) == SOCKET_ERROR && get_socket_errno() != ENOTCONN)
  293. { }
  294. else
  295. {
  296. if (closesocket(fd) == SOCKET_ERROR)
  297. { }
  298. }
  299. state= GEARMAN_CON_UNIVERSAL_ADDRINFO;
  300. fd= INVALID_SOCKET;
  301. events= 0;
  302. revents= 0;
  303. send_state= GEARMAN_CON_SEND_STATE_NONE;
  304. send_buffer_ptr= send_buffer;
  305. send_buffer_size= 0;
  306. send_data_size= 0;
  307. send_data_offset= 0;
  308. recv_state= GEARMAN_CON_RECV_UNIVERSAL_NONE;
  309. free_recv_packet();
  310. recv_buffer_ptr= recv_buffer;
  311. recv_buffer_size= 0;
  312. options.server_options_sent= false;
  313. // created_id_next is incremented for every outbound packet (except status).
  314. // created_id is incremented for every response packet received, and also when
  315. // no packets are received due to an error. There are lots of such error paths
  316. // and it seems simpler to just reset these both to zero when a connection is
  317. // 'closed'.
  318. created_id= 0;
  319. created_id_next= 0;
  320. }
  321. void gearman_connection_st::free_recv_packet()
  322. {
  323. if (_recv_packet)
  324. {
  325. gearman_packet_free(recv_packet());
  326. _recv_packet= NULL;
  327. }
  328. }
  329. void gearman_connection_st::reset_addrinfo()
  330. {
  331. if (_addrinfo)
  332. {
  333. freeaddrinfo(_addrinfo);
  334. _addrinfo= NULL;
  335. }
  336. addrinfo_next= NULL;
  337. }
  338. gearman_return_t gearman_connection_st::send_identifier(void)
  339. {
  340. if (universal._identifier)
  341. {
  342. options.identifier_sent= false;
  343. const void* id= (void*)universal._identifier->value();
  344. size_t id_size= universal._identifier->size();
  345. gearman_packet_st packet;
  346. gearman_return_t ret= gearman_packet_create_args(universal, packet, GEARMAN_MAGIC_REQUEST, GEARMAN_COMMAND_SET_CLIENT_ID, (const void**)&id, &id_size, 1);
  347. if (gearman_success(ret))
  348. {
  349. PUSH_BLOCKING(universal);
  350. options.identifier_sent= true;
  351. gearman_return_t local_ret= send_packet(packet, true);
  352. if (gearman_failed(local_ret))
  353. {
  354. options.identifier_sent= false;
  355. ret= local_ret;
  356. }
  357. else
  358. {
  359. options.identifier_sent= true;
  360. }
  361. }
  362. gearman_packet_free(&packet);
  363. return ret;
  364. }
  365. return GEARMAN_SUCCESS;
  366. }
  367. /*
  368. * The send_packet() method does not only send the passed-in packet_arg. If there are any server options
  369. * established, and they haven't yet been sent over, then these options are sent over first.
  370. * Only if that succeeds is the packet_arg sent.
  371. * The reason for this is server options are only set once by the client/worker. In the older code, this
  372. * resulted in them being sent over exactly once. If the connection was dropped and rebuilt, then the options
  373. * were not sent over again, rendering them moot. This way, we're guaranteed that the options are always sent
  374. * at least once to a connected server.
  375. */
  376. gearman_return_t gearman_connection_st::send_packet(const gearman_packet_st& packet_arg, const bool flush_buffer)
  377. {
  378. if (options.identifier_sent == false)
  379. {
  380. gearman_return_t ret= send_identifier();
  381. if (gearman_failed(ret))
  382. {
  383. return ret;
  384. }
  385. options.identifier_sent= true;
  386. }
  387. if (options.server_options_sent == false)
  388. {
  389. for (gearman_server_options_st* head= universal.server_options_list;
  390. head;
  391. head= head->next)
  392. {
  393. gearman_packet_st message;
  394. const void *args[]= { (const void*)head->value() };
  395. size_t args_size[]= { head->size() };
  396. gearman_return_t ret= gearman_packet_create_args(universal, message, GEARMAN_MAGIC_REQUEST,
  397. GEARMAN_COMMAND_OPTION_REQ, args, args_size, 1);
  398. if (gearman_failed(ret))
  399. {
  400. gearman_packet_free(&message);
  401. gearman_error(universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "gearman_packet_create_args()");
  402. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  403. }
  404. PUSH_BLOCKING(universal);
  405. OptionCheck check(universal);
  406. ret= _send_packet(message, true);
  407. if (gearman_failed(ret))
  408. {
  409. gearman_packet_free(&message);
  410. gearman_error(universal, ret, "Failed to send server-options packet");
  411. return ret;
  412. }
  413. options.packet_in_use= true;
  414. gearman_packet_st *packet_ptr= receiving(_packet, ret, true);
  415. if (packet_ptr == NULL)
  416. {
  417. gearman_packet_free(&message);
  418. options.packet_in_use= false;
  419. gearman_error(universal, ret, "Failed in receiving()");
  420. return ret;
  421. }
  422. if (gearman_failed(ret) ||
  423. gearman_failed(ret= check.success(this)))
  424. {
  425. gearman_packet_free(&message);
  426. free_private_packet();
  427. reset_recv_packet();
  428. gearman_error(universal, ret, "receiving()");
  429. return ret;
  430. }
  431. free_private_packet();
  432. reset_recv_packet();
  433. gearman_packet_free(&message);
  434. }
  435. options.server_options_sent= true;
  436. }
  437. return _send_packet(packet_arg, flush_buffer);
  438. }
  439. /*
  440. * This is the real implementation that actually sends a packet. Read the comments for send_packet() for why
  441. * that is. Note that this is a private method. External callers should only call send_packet().
  442. */
  443. gearman_return_t gearman_connection_st::_send_packet(const gearman_packet_st& packet_arg, const bool flush_buffer)
  444. {
  445. switch (send_state)
  446. {
  447. case GEARMAN_CON_SEND_STATE_NONE:
  448. assert_msg(packet_arg.universal, "send_packet() was to execute against a packet with no universal");
  449. universal_reset_error(*(packet_arg.universal));
  450. if (packet_arg.options.complete == false)
  451. {
  452. return gearman_error(universal, GEARMAN_INVALID_PACKET, "packet not complete");
  453. }
  454. /* Pack first part of packet, which is everything but the payload. */
  455. while (1)
  456. {
  457. { // Scoping to shut compiler up about switch/case jump
  458. gearman_return_t rc;
  459. size_t send_size= gearman_packet_pack(packet_arg,
  460. send_buffer +send_buffer_size,
  461. GEARMAN_SEND_BUFFER_SIZE -send_buffer_size, rc);
  462. if (gearman_success(rc))
  463. {
  464. send_buffer_size+= send_size;
  465. break;
  466. }
  467. else if (rc == GEARMAN_IGNORE_PACKET)
  468. {
  469. return GEARMAN_SUCCESS;
  470. }
  471. else if (rc != GEARMAN_FLUSH_DATA)
  472. {
  473. return rc;
  474. }
  475. }
  476. /* We were asked to flush when the buffer is already flushed! */
  477. if (send_buffer_size == 0)
  478. {
  479. return gearman_universal_set_error(universal, GEARMAN_SEND_BUFFER_TOO_SMALL, GEARMAN_AT,
  480. "send buffer too small (%u)", GEARMAN_SEND_BUFFER_SIZE);
  481. }
  482. /* Flush buffer now if first part of packet won't fit in. */
  483. send_state= GEARMAN_CON_SEND_UNIVERSAL_PRE_FLUSH;
  484. case GEARMAN_CON_SEND_UNIVERSAL_PRE_FLUSH:
  485. {
  486. gearman_return_t ret= flush();
  487. if (gearman_failed(ret))
  488. {
  489. return ret;
  490. }
  491. }
  492. }
  493. /* Return here if we have no data to send. */
  494. if (packet_arg.data_size == 0)
  495. {
  496. break;
  497. }
  498. /* If there is any room in the buffer, copy in data. */
  499. if (packet_arg.data and (GEARMAN_SEND_BUFFER_SIZE - send_buffer_size) > 0)
  500. {
  501. send_data_offset= GEARMAN_SEND_BUFFER_SIZE - send_buffer_size;
  502. if (send_data_offset > packet_arg.data_size)
  503. {
  504. send_data_offset= packet_arg.data_size;
  505. }
  506. memcpy(send_buffer + send_buffer_size, packet_arg.data, send_data_offset);
  507. send_buffer_size+= send_data_offset;
  508. /* Return if all data fit in the send buffer. */
  509. if (send_data_offset == packet_arg.data_size)
  510. {
  511. send_data_offset= 0;
  512. break;
  513. }
  514. }
  515. /* Flush buffer now so we can start writing directly from data buffer. */
  516. send_state= GEARMAN_CON_SEND_UNIVERSAL_FORCE_FLUSH;
  517. case GEARMAN_CON_SEND_UNIVERSAL_FORCE_FLUSH:
  518. {
  519. gearman_return_t ret= flush();
  520. if (gearman_failed(ret))
  521. {
  522. return ret;
  523. }
  524. }
  525. send_data_size= packet_arg.data_size;
  526. /* If this is NULL, then gearman_connection_send_data function will be used. */
  527. if (packet_arg.data == NULL)
  528. {
  529. send_state= GEARMAN_CON_SEND_UNIVERSAL_FLUSH_DATA;
  530. return GEARMAN_SUCCESS;
  531. }
  532. /* Copy into the buffer if it fits, otherwise flush from packet buffer. */
  533. send_buffer_size= packet_arg.data_size - send_data_offset;
  534. if (send_buffer_size < GEARMAN_SEND_BUFFER_SIZE)
  535. {
  536. memcpy(send_buffer,
  537. (const char*)(packet_arg.data) +send_data_offset,
  538. send_buffer_size);
  539. send_data_size= 0;
  540. send_data_offset= 0;
  541. break;
  542. }
  543. send_buffer_ptr= (const char*)(size_t(packet_arg.data) +send_data_offset);
  544. send_state= GEARMAN_CON_SEND_UNIVERSAL_FLUSH_DATA;
  545. case GEARMAN_CON_SEND_UNIVERSAL_FLUSH:
  546. case GEARMAN_CON_SEND_UNIVERSAL_FLUSH_DATA:
  547. return flush();
  548. }
  549. if (flush_buffer)
  550. {
  551. send_state= GEARMAN_CON_SEND_UNIVERSAL_FLUSH;
  552. return flush();
  553. }
  554. send_state= GEARMAN_CON_SEND_STATE_NONE;
  555. return GEARMAN_SUCCESS;
  556. }
  557. size_t gearman_connection_st::send_and_flush(const void *data, size_t data_size, gearman_return_t *ret_ptr)
  558. {
  559. if (send_state != GEARMAN_CON_SEND_UNIVERSAL_FLUSH_DATA)
  560. {
  561. return gearman_error(universal, GEARMAN_NOT_FLUSHING, "not flushing");
  562. }
  563. if (data_size > (send_data_size - send_data_offset))
  564. {
  565. return gearman_error(universal, GEARMAN_DATA_TOO_LARGE, "data too large");
  566. }
  567. send_buffer_ptr= (const char*)data;
  568. send_buffer_size= data_size;
  569. *ret_ptr= flush();
  570. return data_size -send_buffer_size;
  571. }
  572. gearman_return_t gearman_connection_st::lookup()
  573. {
  574. reset_addrinfo();
  575. struct addrinfo ai;
  576. memset(&ai, 0, sizeof(struct addrinfo));
  577. ai.ai_socktype= SOCK_STREAM;
  578. ai.ai_protocol= IPPROTO_TCP;
  579. assert_msg(_addrinfo == NULL, "Programmer error, reset_addrinfo() is either broke, or was not called.");
  580. int ret;
  581. if ((ret= getaddrinfo(_host, _service, &ai, &(_addrinfo))))
  582. {
  583. reset_addrinfo();
  584. return gearman_universal_set_error(universal, GEARMAN_GETADDRINFO, GEARMAN_AT, "getaddrinfo:%s", gai_strerror(ret));
  585. }
  586. addrinfo_next= _addrinfo;
  587. assert(addrinfo_next);
  588. state= GEARMAN_CON_UNIVERSAL_CONNECT;
  589. return GEARMAN_SUCCESS;
  590. }
  591. gearman_return_t gearman_connection_st::enable_ssl()
  592. {
  593. #if defined(HAVE_CYASSL) && HAVE_CYASSL
  594. _ssl= CyaSSL_new(universal.ctx_ssl());
  595. if (_ssl == NULL)
  596. {
  597. close_socket();
  598. return gearman_error(universal, GEARMAN_COULD_NOT_CONNECT, "CyaSSL_new() failed to return a valid object");
  599. }
  600. if (CyaSSL_set_fd(_ssl, fd) != SSL_SUCCESS)
  601. {
  602. close_socket();
  603. char errorString[80];
  604. return gearman_error(universal, GEARMAN_COULD_NOT_CONNECT, CyaSSL_ERR_error_string(CyaSSL_get_error(_ssl, 0), errorString));
  605. }
  606. #if 0
  607. if (CyaSSL_connect(_ssl) != SSL_SUCCESS)
  608. {
  609. close_socket();
  610. char errorString[80];
  611. return gearman_error(universal, GEARMAN_COULD_NOT_CONNECT, CyaSSL_ERR_error_string(CyaSSL_get_error(_ssl, 0), errorString));
  612. }
  613. #endif
  614. #endif
  615. return GEARMAN_SUCCESS;
  616. }
  617. gearman_return_t gearman_connection_st::flush()
  618. {
  619. while (1)
  620. {
  621. switch (state)
  622. {
  623. case GEARMAN_CON_UNIVERSAL_ADDRINFO:
  624. {
  625. gearman_return_t ret= lookup();
  626. if (gearman_failed(ret))
  627. {
  628. return ret;
  629. }
  630. }
  631. case GEARMAN_CON_UNIVERSAL_CONNECT:
  632. if (fd != INVALID_SOCKET)
  633. {
  634. close_socket();
  635. }
  636. if (addrinfo_next == NULL)
  637. {
  638. state= GEARMAN_CON_UNIVERSAL_ADDRINFO;
  639. return gearman_universal_set_error(universal, GEARMAN_COULD_NOT_CONNECT, GEARMAN_AT, "%s:%s", _host, _service);
  640. }
  641. // rewrite tye if HAVE_SOCK_CLOEXEC
  642. {
  643. int type= addrinfo_next->ai_socktype;
  644. if (SOCK_CLOEXEC)
  645. {
  646. type|= SOCK_CLOEXEC;
  647. }
  648. if (SOCK_NONBLOCK)
  649. {
  650. type|= SOCK_NONBLOCK;
  651. }
  652. fd= socket(addrinfo_next->ai_family, type, addrinfo_next->ai_protocol);
  653. }
  654. if (fd == INVALID_SOCKET)
  655. {
  656. state= GEARMAN_CON_UNIVERSAL_ADDRINFO;
  657. return gearman_perror(universal, "socket");
  658. }
  659. {
  660. gearman_return_t gret= set_socket_options();
  661. if (gearman_failed(gret))
  662. {
  663. close_socket();
  664. return gret;
  665. }
  666. }
  667. while (1)
  668. {
  669. if (connect(fd, addrinfo_next->ai_addr, addrinfo_next->ai_addrlen) == 0)
  670. {
  671. state= GEARMAN_CON_UNIVERSAL_CONNECTED;
  672. addrinfo_next= NULL;
  673. break;
  674. }
  675. switch (errno)
  676. {
  677. // Treat as an async connect
  678. case EINTR:
  679. case EINPROGRESS:
  680. state= GEARMAN_CON_UNIVERSAL_CONNECTING;
  681. break;
  682. case ECONNREFUSED:
  683. case ENETUNREACH:
  684. case ETIMEDOUT:
  685. addrinfo_next= addrinfo_next->ai_next;
  686. // We will treat this as an error but retry the address
  687. case EAGAIN:
  688. state= GEARMAN_CON_UNIVERSAL_CONNECT;
  689. close_socket();
  690. break;
  691. default:
  692. gearman_perror(universal, "connect");
  693. close_socket();
  694. return GEARMAN_COULD_NOT_CONNECT;
  695. }
  696. break;
  697. }
  698. if (state != GEARMAN_CON_UNIVERSAL_CONNECTING)
  699. {
  700. break;
  701. }
  702. case GEARMAN_CON_UNIVERSAL_CONNECTING:
  703. while (1)
  704. {
  705. if (revents & (POLLERR | POLLHUP | POLLNVAL))
  706. {
  707. state= GEARMAN_CON_UNIVERSAL_CONNECT;
  708. addrinfo_next= addrinfo_next->ai_next;
  709. break;
  710. }
  711. else if (revents & POLLOUT)
  712. {
  713. state= GEARMAN_CON_UNIVERSAL_CONNECTED;
  714. gearman_return_t ssl_ret;
  715. if ((ssl_ret= enable_ssl()) != GEARMAN_SUCCESS)
  716. {
  717. return ssl_ret;
  718. }
  719. break;
  720. }
  721. set_events(POLLOUT);
  722. if (universal.is_non_blocking())
  723. {
  724. state= GEARMAN_CON_UNIVERSAL_CONNECTING;
  725. return gearman_gerror(universal, GEARMAN_IO_WAIT);
  726. }
  727. gearman_return_t gret= gearman_wait(universal);
  728. if (gearman_failed(gret))
  729. {
  730. return gret;
  731. }
  732. }
  733. if (state != GEARMAN_CON_UNIVERSAL_CONNECTED)
  734. {
  735. break;
  736. }
  737. case GEARMAN_CON_UNIVERSAL_CONNECTED:
  738. while (send_buffer_size != 0)
  739. {
  740. ssize_t write_size;
  741. #if defined(HAVE_CYASSL) && HAVE_CYASSL
  742. write_size= 0;
  743. if (_ssl)
  744. {
  745. write_size= CyaSSL_send(_ssl, send_buffer_ptr, send_buffer_size, MSG_NOSIGNAL);
  746. if (write_size < 0)
  747. {
  748. int err;
  749. switch ((err= CyaSSL_get_error(_ssl, 0)))
  750. {
  751. case SSL_ERROR_WANT_WRITE:
  752. case SSL_ERROR_WANT_READ:
  753. errno= EWOULDBLOCK;
  754. break;
  755. default:
  756. {
  757. char errorString[80];
  758. CyaSSL_ERR_error_string(err, errorString);
  759. close_socket();
  760. return gearman_universal_set_error(universal, GEARMAN_LOST_CONNECTION, GEARMAN_AT, "SSL failure(%s)", errorString);
  761. }
  762. }
  763. }
  764. }
  765. #else
  766. write_size= ::send(fd, send_buffer_ptr, send_buffer_size, MSG_NOSIGNAL);
  767. #endif
  768. if (write_size == 0) // Zero value on send()
  769. { }
  770. else if (write_size == -1)
  771. {
  772. switch (errno)
  773. {
  774. #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
  775. case EWOULDBLOCK:
  776. #endif
  777. case EAGAIN:
  778. {
  779. set_events(POLLOUT);
  780. if (gearman_universal_is_non_blocking(universal))
  781. {
  782. return gearman_gerror(universal, GEARMAN_IO_WAIT);
  783. }
  784. gearman_return_t gret= gearman_wait(universal);
  785. if (gearman_failed(gret))
  786. {
  787. return gret;
  788. }
  789. continue;
  790. }
  791. case EPIPE:
  792. case ECONNRESET:
  793. case EHOSTDOWN:
  794. {
  795. gearman_return_t ret= gearman_perror(universal, "lost connection to server during send");
  796. close_socket();
  797. return ret;
  798. }
  799. default:
  800. break;
  801. }
  802. gearman_return_t ret= gearman_perror(universal, "send");
  803. close_socket();
  804. return ret;
  805. }
  806. send_buffer_size-= size_t(write_size);
  807. if (send_state == GEARMAN_CON_SEND_UNIVERSAL_FLUSH_DATA)
  808. {
  809. send_data_offset+= size_t(write_size);
  810. if (send_data_offset == send_data_size)
  811. {
  812. send_data_size= 0;
  813. send_data_offset= 0;
  814. break;
  815. }
  816. if (send_buffer_size == 0)
  817. {
  818. return GEARMAN_SUCCESS;
  819. }
  820. }
  821. else if (send_buffer_size == 0)
  822. {
  823. break;
  824. }
  825. send_buffer_ptr+= write_size;
  826. }
  827. send_state= GEARMAN_CON_SEND_STATE_NONE;
  828. send_buffer_ptr= send_buffer;
  829. return GEARMAN_SUCCESS;
  830. }
  831. }
  832. }
  833. gearman_packet_st *gearman_connection_st::receiving(gearman_packet_st& packet_arg,
  834. gearman_return_t& ret,
  835. const bool recv_data)
  836. {
  837. switch (recv_state)
  838. {
  839. case GEARMAN_CON_RECV_UNIVERSAL_NONE:
  840. if (state != GEARMAN_CON_UNIVERSAL_CONNECTED)
  841. {
  842. ret= gearman_error(universal, GEARMAN_NOT_CONNECTED, "not connected");
  843. return NULL;
  844. }
  845. _recv_packet= gearman_packet_create(universal, &packet_arg);
  846. if (_recv_packet == NULL)
  847. {
  848. ret= gearman_error(universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "gearman_packet_create()");
  849. return NULL;
  850. }
  851. recv_state= GEARMAN_CON_RECV_UNIVERSAL_READ;
  852. case GEARMAN_CON_RECV_UNIVERSAL_READ:
  853. while (1)
  854. {
  855. // If we have data, see if it is a complete packet
  856. if (recv_buffer_size > 0)
  857. {
  858. size_t recv_size= gearman_packet_unpack(*(recv_packet()),
  859. recv_buffer_ptr,
  860. recv_buffer_size, ret);
  861. recv_buffer_ptr+= recv_size;
  862. recv_buffer_size-= recv_size;
  863. if (gearman_success(ret))
  864. {
  865. break;
  866. }
  867. else if (ret != GEARMAN_IO_WAIT)
  868. {
  869. close_socket();
  870. return NULL;
  871. }
  872. }
  873. /* Shift buffer contents if needed. */
  874. if (recv_buffer_size > 0)
  875. {
  876. memmove(recv_buffer, recv_buffer_ptr, recv_buffer_size);
  877. }
  878. recv_buffer_ptr= recv_buffer;
  879. size_t recv_size= recv_socket(recv_buffer +recv_buffer_size, GEARMAN_RECV_BUFFER_SIZE -recv_buffer_size, ret);
  880. if (gearman_failed(ret))
  881. {
  882. return NULL;
  883. }
  884. recv_buffer_size+= recv_size;
  885. }
  886. if (packet_arg.data_size == 0)
  887. {
  888. recv_state= GEARMAN_CON_RECV_UNIVERSAL_NONE;
  889. break;
  890. }
  891. recv_data_size= packet_arg.data_size;
  892. if (recv_data == false )
  893. {
  894. recv_state= GEARMAN_CON_RECV_STATE_READ_DATA;
  895. break;
  896. }
  897. assert(packet_arg.universal);
  898. packet_arg.data= gearman_malloc((*packet_arg.universal), packet_arg.data_size);
  899. if (packet_arg.data == NULL)
  900. {
  901. ret= gearman_error(universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "gearman_malloc((*packet_arg.universal), packet_arg.data_size)");
  902. close_socket();
  903. return NULL;
  904. }
  905. packet_arg.options.free_data= true;
  906. recv_state= GEARMAN_CON_RECV_STATE_READ_DATA;
  907. case GEARMAN_CON_RECV_STATE_READ_DATA:
  908. while (recv_data_size)
  909. {
  910. (void)receive_data(static_cast<uint8_t *>(const_cast<void *>(packet_arg.data)) +
  911. recv_data_offset,
  912. packet_arg.data_size -recv_data_offset, ret);
  913. if (gearman_failed(ret))
  914. {
  915. return NULL;
  916. }
  917. }
  918. recv_state= GEARMAN_CON_RECV_UNIVERSAL_NONE;
  919. break;
  920. }
  921. gearman_packet_st *tmp_packet_arg= recv_packet();
  922. reset_recv_packet();
  923. return tmp_packet_arg;
  924. }
  925. size_t gearman_connection_st::receive_data(void *data, size_t data_size, gearman_return_t& ret)
  926. {
  927. size_t recv_size= 0;
  928. if (recv_data_size == 0)
  929. {
  930. ret= GEARMAN_SUCCESS;
  931. return 0;
  932. }
  933. if ((recv_data_size - recv_data_offset) < data_size)
  934. {
  935. data_size= recv_data_size - recv_data_offset;
  936. }
  937. if (recv_buffer_size > 0)
  938. {
  939. if (recv_buffer_size < data_size)
  940. {
  941. recv_size= recv_buffer_size;
  942. }
  943. else
  944. {
  945. recv_size= data_size;
  946. }
  947. memcpy(data, recv_buffer_ptr, recv_size);
  948. recv_buffer_ptr+= recv_size;
  949. recv_buffer_size-= recv_size;
  950. }
  951. if (data_size != recv_size)
  952. {
  953. recv_size+= recv_socket(static_cast<uint8_t *>(const_cast<void *>(data)) + recv_size, data_size - recv_size, ret);
  954. recv_data_offset+= recv_size;
  955. }
  956. else
  957. {
  958. recv_data_offset+= recv_size;
  959. ret= GEARMAN_SUCCESS;
  960. }
  961. if (recv_data_size == recv_data_offset)
  962. {
  963. recv_data_size= 0;
  964. recv_data_offset= 0;
  965. recv_state= GEARMAN_CON_RECV_UNIVERSAL_NONE;
  966. }
  967. return recv_size;
  968. }
  969. size_t gearman_connection_st::recv_socket(void *data, size_t data_size, gearman_return_t& ret)
  970. {
  971. ssize_t read_size;
  972. while (1)
  973. {
  974. #if defined(HAVE_CYASSL) && HAVE_CYASSL
  975. if (_ssl)
  976. {
  977. read_size= CyaSSL_recv(_ssl, data, data_size, MSG_DONTWAIT);
  978. if (read_size < 0)
  979. {
  980. int sendErr= CyaSSL_get_error(_ssl, 0);
  981. if (sendErr != SSL_ERROR_WANT_READ)
  982. {
  983. char errorString[80];
  984. int err = CyaSSL_get_error(_ssl, 0);
  985. CyaSSL_ERR_error_string(err, errorString);
  986. close_socket();
  987. ret= gearman_universal_set_error(universal, GEARMAN_LOST_CONNECTION, GEARMAN_AT,
  988. "SSL failure(%s)", errorString);
  989. }
  990. errno= EAGAIN;
  991. }
  992. }
  993. #else
  994. read_size= ::recv(fd, data, data_size, MSG_NOSIGNAL);
  995. #endif
  996. if (read_size == 0)
  997. {
  998. ret= gearman_error(universal, GEARMAN_LOST_CONNECTION, "lost connection to server (EOF)");
  999. close_socket();
  1000. return 0;
  1001. }
  1002. else if (read_size == -1)
  1003. {
  1004. if (errno == EAGAIN)
  1005. {
  1006. set_events(POLLIN);
  1007. if (universal.is_non_blocking())
  1008. {
  1009. ret= gearman_gerror(universal, GEARMAN_IO_WAIT);
  1010. return 0;
  1011. }
  1012. ret= gearman_wait(universal);
  1013. if (gearman_failed(ret))
  1014. {
  1015. if (ret == GEARMAN_SHUTDOWN)
  1016. {
  1017. close_socket();
  1018. }
  1019. return 0;
  1020. }
  1021. continue;
  1022. }
  1023. else if (errno == EINTR)
  1024. {
  1025. continue;
  1026. }
  1027. else if (errno == EPIPE || errno == ECONNRESET || errno == EHOSTDOWN)
  1028. {
  1029. ret= gearman_perror(universal, "lost connection to server during read");
  1030. }
  1031. else
  1032. {
  1033. ret= gearman_perror(universal, "recv");
  1034. }
  1035. close_socket();
  1036. return 0;
  1037. }
  1038. break;
  1039. }
  1040. ret= GEARMAN_SUCCESS;
  1041. return size_t(read_size);
  1042. }
  1043. void gearman_connection_st::set_events(short arg)
  1044. {
  1045. if ((events | arg) == events)
  1046. {
  1047. return;
  1048. }
  1049. events|= arg;
  1050. }
  1051. void gearman_connection_st::set_revents(short arg)
  1052. {
  1053. if (arg)
  1054. {
  1055. options.ready= true;
  1056. }
  1057. revents= arg;
  1058. events&= short(~arg);
  1059. }
  1060. /*
  1061. * Static Definitions
  1062. */
  1063. gearman_return_t gearman_connection_st::set_socket_options()
  1064. {
  1065. if (SOCK_CLOEXEC == 0)
  1066. {
  1067. if (FD_CLOEXEC)
  1068. {
  1069. int flags;
  1070. do
  1071. {
  1072. flags= fcntl(fd, F_GETFD, 0);
  1073. } while (flags == -1 and (errno == EINTR or errno == EAGAIN));
  1074. if (flags != -1)
  1075. {
  1076. int rval;
  1077. do
  1078. {
  1079. rval= fcntl (fd, F_SETFD, flags | FD_CLOEXEC);
  1080. } while (rval == -1 && (errno == EINTR or errno == EAGAIN));
  1081. // we currently ignore the case where rval is -1
  1082. }
  1083. }
  1084. }
  1085. {
  1086. int ret= 1;
  1087. ret= setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &ret,
  1088. socklen_t(sizeof(int)));
  1089. if (ret == -1 && errno != EOPNOTSUPP)
  1090. {
  1091. gearman_perror(universal, "setsockopt(TCP_NODELAY)");
  1092. return GEARMAN_ERRNO;
  1093. }
  1094. }
  1095. {
  1096. struct linger linger;
  1097. linger.l_onoff= 1;
  1098. linger.l_linger= GEARMAN_DEFAULT_SOCKET_TIMEOUT;
  1099. int ret= setsockopt(fd, SOL_SOCKET, SO_LINGER, &linger,
  1100. socklen_t(sizeof(struct linger)));
  1101. if (ret == -1)
  1102. {
  1103. gearman_perror(universal, "setsockopt(SO_LINGER)");
  1104. return GEARMAN_ERRNO;
  1105. }
  1106. }
  1107. #if 0
  1108. if (0)
  1109. {
  1110. struct timeval waittime;
  1111. waittime.tv_sec= GEARMAN_DEFAULT_SOCKET_TIMEOUT;
  1112. waittime.tv_usec= 0;
  1113. int ret= setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &waittime,
  1114. socklen_t(sizeof(struct timeval)));
  1115. if (ret == -1 && errno != ENOPROTOOPT)
  1116. {
  1117. gearman_perror(universal, "setsockopt(SO_SNDTIMEO)");
  1118. return GEARMAN_ERRNO;
  1119. }
  1120. ret= setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &waittime,
  1121. socklen_t(sizeof(struct timeval)));
  1122. if (ret == -1 && errno != ENOPROTOOPT)
  1123. {
  1124. gearman_perror(universal, "setsockopt(SO_RCVTIMEO)");
  1125. return GEARMAN_ERRNO;
  1126. }
  1127. }
  1128. #endif
  1129. {
  1130. int optval= 1;
  1131. int ret= setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval));
  1132. if (ret == -1 && errno != ENOPROTOOPT)
  1133. {
  1134. gearman_perror(universal, "setsockopt(SO_KEEPALIVE)");
  1135. return GEARMAN_ERRNO;
  1136. }
  1137. }
  1138. {
  1139. int ret= GEARMAN_DEFAULT_SOCKET_SEND_SIZE;
  1140. ret= setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &ret, socklen_t(sizeof(int)));
  1141. if (ret == -1)
  1142. {
  1143. gearman_perror(universal, "setsockopt(SO_SNDBUF)");
  1144. return GEARMAN_ERRNO;
  1145. }
  1146. }
  1147. #if defined(SO_NOSIGPIPE)
  1148. if (SO_NOSIGPIPE)
  1149. {
  1150. int ret= 1;
  1151. ret= setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, static_cast<void *>(&ret), sizeof(int));
  1152. // This is not considered a fatal error
  1153. if (ret == -1)
  1154. {
  1155. gearman_perror(universal, "setsockopt(SO_NOSIGPIPE)");
  1156. }
  1157. }
  1158. #endif
  1159. {
  1160. int ret= GEARMAN_DEFAULT_SOCKET_RECV_SIZE;
  1161. ret= setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &ret, socklen_t(sizeof(int)));
  1162. if (ret == -1)
  1163. {
  1164. gearman_perror(universal, "setsockopt(SO_RCVBUF)");
  1165. return GEARMAN_ERRNO;
  1166. }
  1167. }
  1168. // If SOCK_NONBLOCK doesn't work, just enable non_block via fcntl
  1169. if (SOCK_NONBLOCK == 0)
  1170. {
  1171. int flags;
  1172. do
  1173. {
  1174. flags= fcntl(fd, F_GETFL, 0);
  1175. } while (flags == -1 and (errno == EINTR or errno == EAGAIN));
  1176. if (flags == -1)
  1177. {
  1178. gearman_perror(universal, "fcntl(F_GETFL)");
  1179. return GEARMAN_ERRNO;
  1180. }
  1181. else if ((flags & O_NONBLOCK) == 0)
  1182. {
  1183. int retval;
  1184. do
  1185. {
  1186. retval= fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  1187. } while (retval == -1 and (errno == EINTR or errno == EAGAIN));
  1188. if (retval == -1)
  1189. {
  1190. gearman_perror(universal, "fcntl(F_SETFL)");
  1191. return GEARMAN_ERRNO;
  1192. }
  1193. }
  1194. }
  1195. return GEARMAN_SUCCESS;
  1196. }