amqp_connection.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
  2. // SPDX-License-Identifier: mit
  3. #ifdef HAVE_CONFIG_H
  4. #include "config.h"
  5. #endif
  6. #ifdef _MSC_VER
  7. #define _CRT_SECURE_NO_WARNINGS
  8. #endif
  9. #include "amqp_private.h"
  10. #include "amqp_time.h"
  11. #include "rabbitmq-c/tcp_socket.h"
  12. #include <errno.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #ifndef AMQP_INITIAL_FRAME_POOL_PAGE_SIZE
  18. #define AMQP_INITIAL_FRAME_POOL_PAGE_SIZE 65536
  19. #endif
  20. #ifndef AMQP_INITIAL_INBOUND_SOCK_BUFFER_SIZE
  21. #define AMQP_INITIAL_INBOUND_SOCK_BUFFER_SIZE 131072
  22. #endif
  23. #ifndef AMQP_DEFAULT_LOGIN_TIMEOUT_SEC
  24. #define AMQP_DEFAULT_LOGIN_TIMEOUT_SEC 12
  25. #endif
  26. #define ENFORCE_STATE(statevec, statenum) \
  27. { \
  28. amqp_connection_state_t _check_state = (statevec); \
  29. amqp_connection_state_enum _wanted_state = (statenum); \
  30. if (_check_state->state != _wanted_state) \
  31. amqp_abort( \
  32. "Programming error: invalid AMQP connection state: expected %d, " \
  33. "got %d", \
  34. _wanted_state, _check_state->state); \
  35. }
  36. amqp_connection_state_t amqp_new_connection(void) {
  37. int res;
  38. amqp_connection_state_t state = (amqp_connection_state_t)calloc(
  39. 1, sizeof(struct amqp_connection_state_t_));
  40. if (state == NULL) {
  41. return NULL;
  42. }
  43. res = amqp_tune_connection(state, 0, AMQP_INITIAL_FRAME_POOL_PAGE_SIZE, 0);
  44. if (0 != res) {
  45. goto out_nomem;
  46. }
  47. state->inbound_buffer.bytes = state->header_buffer;
  48. state->inbound_buffer.len = sizeof(state->header_buffer);
  49. state->state = CONNECTION_STATE_INITIAL;
  50. /* the server protocol version response is 8 bytes, which conveniently
  51. is also the minimum frame size */
  52. state->target_size = 8;
  53. state->sock_inbound_buffer.len = AMQP_INITIAL_INBOUND_SOCK_BUFFER_SIZE;
  54. state->sock_inbound_buffer.bytes =
  55. malloc(AMQP_INITIAL_INBOUND_SOCK_BUFFER_SIZE);
  56. if (state->sock_inbound_buffer.bytes == NULL) {
  57. goto out_nomem;
  58. }
  59. init_amqp_pool(&state->properties_pool, 512);
  60. /* Use address of the internal_handshake_timeout object by default. */
  61. state->internal_handshake_timeout.tv_sec = AMQP_DEFAULT_LOGIN_TIMEOUT_SEC;
  62. state->internal_handshake_timeout.tv_usec = 0;
  63. state->handshake_timeout = &state->internal_handshake_timeout;
  64. return state;
  65. out_nomem:
  66. free(state->sock_inbound_buffer.bytes);
  67. free(state);
  68. return NULL;
  69. }
  70. int amqp_get_sockfd(amqp_connection_state_t state) {
  71. return state->socket ? amqp_socket_get_sockfd(state->socket) : -1;
  72. }
  73. void amqp_set_sockfd(amqp_connection_state_t state, int sockfd) {
  74. amqp_socket_t *socket = amqp_tcp_socket_new(state);
  75. if (!socket) {
  76. amqp_abort("%s", strerror(errno));
  77. }
  78. amqp_tcp_socket_set_sockfd(socket, sockfd);
  79. }
  80. void amqp_set_socket(amqp_connection_state_t state, amqp_socket_t *socket) {
  81. amqp_socket_delete(state->socket);
  82. state->socket = socket;
  83. }
  84. amqp_socket_t *amqp_get_socket(amqp_connection_state_t state) {
  85. return state->socket;
  86. }
  87. int amqp_tune_connection(amqp_connection_state_t state, int channel_max,
  88. int frame_max, int heartbeat) {
  89. void *newbuf;
  90. int res;
  91. ENFORCE_STATE(state, CONNECTION_STATE_IDLE);
  92. state->channel_max = channel_max;
  93. state->frame_max = frame_max;
  94. state->heartbeat = heartbeat;
  95. if (0 > state->heartbeat) {
  96. state->heartbeat = 0;
  97. }
  98. res = amqp_time_s_from_now(&state->next_send_heartbeat,
  99. amqp_heartbeat_send(state));
  100. if (AMQP_STATUS_OK != res) {
  101. return res;
  102. }
  103. res = amqp_time_s_from_now(&state->next_recv_heartbeat,
  104. amqp_heartbeat_recv(state));
  105. if (AMQP_STATUS_OK != res) {
  106. return res;
  107. }
  108. state->outbound_buffer.len = frame_max;
  109. newbuf = realloc(state->outbound_buffer.bytes, frame_max);
  110. if (newbuf == NULL) {
  111. return AMQP_STATUS_NO_MEMORY;
  112. }
  113. state->outbound_buffer.bytes = newbuf;
  114. return AMQP_STATUS_OK;
  115. }
  116. int amqp_get_channel_max(amqp_connection_state_t state) {
  117. return state->channel_max;
  118. }
  119. int amqp_get_frame_max(amqp_connection_state_t state) {
  120. return state->frame_max;
  121. }
  122. int amqp_get_heartbeat(amqp_connection_state_t state) {
  123. return state->heartbeat;
  124. }
  125. int amqp_destroy_connection(amqp_connection_state_t state) {
  126. int status = AMQP_STATUS_OK;
  127. if (state) {
  128. int i;
  129. for (i = 0; i < POOL_TABLE_SIZE; ++i) {
  130. amqp_pool_table_entry_t *entry = state->pool_table[i];
  131. while (NULL != entry) {
  132. amqp_pool_table_entry_t *todelete = entry;
  133. empty_amqp_pool(&entry->pool);
  134. entry = entry->next;
  135. free(todelete);
  136. }
  137. }
  138. free(state->outbound_buffer.bytes);
  139. free(state->sock_inbound_buffer.bytes);
  140. amqp_socket_delete(state->socket);
  141. empty_amqp_pool(&state->properties_pool);
  142. free(state);
  143. }
  144. return status;
  145. }
  146. static void return_to_idle(amqp_connection_state_t state) {
  147. state->inbound_buffer.len = sizeof(state->header_buffer);
  148. state->inbound_buffer.bytes = state->header_buffer;
  149. state->inbound_offset = 0;
  150. state->target_size = HEADER_SIZE;
  151. state->state = CONNECTION_STATE_IDLE;
  152. }
  153. static size_t consume_data(amqp_connection_state_t state,
  154. amqp_bytes_t *received_data) {
  155. /* how much data is available and will fit? */
  156. size_t bytes_consumed = state->target_size - state->inbound_offset;
  157. if (received_data->len < bytes_consumed) {
  158. bytes_consumed = received_data->len;
  159. }
  160. memcpy(amqp_offset(state->inbound_buffer.bytes, state->inbound_offset),
  161. received_data->bytes, bytes_consumed);
  162. state->inbound_offset += bytes_consumed;
  163. received_data->bytes = amqp_offset(received_data->bytes, bytes_consumed);
  164. received_data->len -= bytes_consumed;
  165. return bytes_consumed;
  166. }
  167. int amqp_handle_input(amqp_connection_state_t state, amqp_bytes_t received_data,
  168. amqp_frame_t *decoded_frame) {
  169. size_t bytes_consumed;
  170. void *raw_frame;
  171. /* Returning frame_type of zero indicates either insufficient input,
  172. or a complete, ignored frame was read. */
  173. decoded_frame->frame_type = 0;
  174. if (received_data.len == 0) {
  175. return AMQP_STATUS_OK;
  176. }
  177. if (state->state == CONNECTION_STATE_IDLE) {
  178. state->state = CONNECTION_STATE_HEADER;
  179. }
  180. bytes_consumed = consume_data(state, &received_data);
  181. /* do we have target_size data yet? if not, return with the
  182. expectation that more will arrive */
  183. if (state->inbound_offset < state->target_size) {
  184. return (int)bytes_consumed;
  185. }
  186. raw_frame = state->inbound_buffer.bytes;
  187. switch (state->state) {
  188. case CONNECTION_STATE_INITIAL:
  189. /* check for a protocol header from the server */
  190. if (memcmp(raw_frame, "AMQP", 4) == 0) {
  191. decoded_frame->frame_type = AMQP_PSEUDOFRAME_PROTOCOL_HEADER;
  192. decoded_frame->channel = 0;
  193. decoded_frame->payload.protocol_header.transport_high =
  194. amqp_d8(amqp_offset(raw_frame, 4));
  195. decoded_frame->payload.protocol_header.transport_low =
  196. amqp_d8(amqp_offset(raw_frame, 5));
  197. decoded_frame->payload.protocol_header.protocol_version_major =
  198. amqp_d8(amqp_offset(raw_frame, 6));
  199. decoded_frame->payload.protocol_header.protocol_version_minor =
  200. amqp_d8(amqp_offset(raw_frame, 7));
  201. return_to_idle(state);
  202. return (int)bytes_consumed;
  203. }
  204. /* it's not a protocol header; fall through to process it as a
  205. regular frame header */
  206. case CONNECTION_STATE_HEADER: {
  207. amqp_channel_t channel;
  208. amqp_pool_t *channel_pool;
  209. uint32_t frame_size;
  210. channel = amqp_d16(amqp_offset(raw_frame, 1));
  211. /* frame length is 3 bytes in */
  212. frame_size = amqp_d32(amqp_offset(raw_frame, 3));
  213. /* To prevent the target_size calculation below from overflowing, check
  214. * that the stated frame_size is smaller than a signed 32-bit. Given
  215. * the library only allows configuring frame_max as an int32_t, and
  216. * frame_size is uint32_t, the math below is safe from overflow. */
  217. if (frame_size >= INT32_MAX) {
  218. return AMQP_STATUS_BAD_AMQP_DATA;
  219. }
  220. state->target_size = frame_size + HEADER_SIZE + FOOTER_SIZE;
  221. if ((size_t)state->frame_max < state->target_size) {
  222. return AMQP_STATUS_BAD_AMQP_DATA;
  223. }
  224. channel_pool = amqp_get_or_create_channel_pool(state, channel);
  225. if (NULL == channel_pool) {
  226. return AMQP_STATUS_NO_MEMORY;
  227. }
  228. amqp_pool_alloc_bytes(channel_pool, state->target_size,
  229. &state->inbound_buffer);
  230. if (NULL == state->inbound_buffer.bytes) {
  231. return AMQP_STATUS_NO_MEMORY;
  232. }
  233. memcpy(state->inbound_buffer.bytes, state->header_buffer, HEADER_SIZE);
  234. raw_frame = state->inbound_buffer.bytes;
  235. state->state = CONNECTION_STATE_BODY;
  236. bytes_consumed += consume_data(state, &received_data);
  237. /* do we have target_size data yet? if not, return with the
  238. expectation that more will arrive */
  239. if (state->inbound_offset < state->target_size) {
  240. return (int)bytes_consumed;
  241. }
  242. }
  243. /* fall through to process body */
  244. case CONNECTION_STATE_BODY: {
  245. amqp_bytes_t encoded;
  246. int res;
  247. amqp_pool_t *channel_pool;
  248. /* Check frame end marker (footer) */
  249. if (amqp_d8(amqp_offset(raw_frame, state->target_size - 1)) !=
  250. AMQP_FRAME_END) {
  251. return AMQP_STATUS_BAD_AMQP_DATA;
  252. }
  253. decoded_frame->frame_type = amqp_d8(amqp_offset(raw_frame, 0));
  254. decoded_frame->channel = amqp_d16(amqp_offset(raw_frame, 1));
  255. channel_pool =
  256. amqp_get_or_create_channel_pool(state, decoded_frame->channel);
  257. if (NULL == channel_pool) {
  258. return AMQP_STATUS_NO_MEMORY;
  259. }
  260. switch (decoded_frame->frame_type) {
  261. case AMQP_FRAME_METHOD:
  262. decoded_frame->payload.method.id =
  263. amqp_d32(amqp_offset(raw_frame, HEADER_SIZE));
  264. encoded.bytes = amqp_offset(raw_frame, HEADER_SIZE + 4);
  265. encoded.len = state->target_size - HEADER_SIZE - 4 - FOOTER_SIZE;
  266. res = amqp_decode_method(decoded_frame->payload.method.id,
  267. channel_pool, encoded,
  268. &decoded_frame->payload.method.decoded);
  269. if (res < 0) {
  270. return res;
  271. }
  272. break;
  273. case AMQP_FRAME_HEADER:
  274. decoded_frame->payload.properties.class_id =
  275. amqp_d16(amqp_offset(raw_frame, HEADER_SIZE));
  276. /* unused 2-byte weight field goes here */
  277. decoded_frame->payload.properties.body_size =
  278. amqp_d64(amqp_offset(raw_frame, HEADER_SIZE + 4));
  279. encoded.bytes = amqp_offset(raw_frame, HEADER_SIZE + 12);
  280. encoded.len = state->target_size - HEADER_SIZE - 12 - FOOTER_SIZE;
  281. decoded_frame->payload.properties.raw = encoded;
  282. res = amqp_decode_properties(
  283. decoded_frame->payload.properties.class_id, channel_pool, encoded,
  284. &decoded_frame->payload.properties.decoded);
  285. if (res < 0) {
  286. return res;
  287. }
  288. break;
  289. case AMQP_FRAME_BODY:
  290. decoded_frame->payload.body_fragment.len =
  291. state->target_size - HEADER_SIZE - FOOTER_SIZE;
  292. decoded_frame->payload.body_fragment.bytes =
  293. amqp_offset(raw_frame, HEADER_SIZE);
  294. break;
  295. case AMQP_FRAME_HEARTBEAT:
  296. break;
  297. default:
  298. /* Ignore the frame */
  299. decoded_frame->frame_type = 0;
  300. break;
  301. }
  302. return_to_idle(state);
  303. return (int)bytes_consumed;
  304. }
  305. default:
  306. amqp_abort("Internal error: invalid amqp_connection_state_t->state %d",
  307. state->state);
  308. }
  309. }
  310. amqp_boolean_t amqp_release_buffers_ok(amqp_connection_state_t state) {
  311. return (state->state == CONNECTION_STATE_IDLE);
  312. }
  313. void amqp_release_buffers(amqp_connection_state_t state) {
  314. int i;
  315. ENFORCE_STATE(state, CONNECTION_STATE_IDLE);
  316. for (i = 0; i < POOL_TABLE_SIZE; ++i) {
  317. amqp_pool_table_entry_t *entry = state->pool_table[i];
  318. for (; NULL != entry; entry = entry->next) {
  319. amqp_maybe_release_buffers_on_channel(state, entry->channel);
  320. }
  321. }
  322. }
  323. void amqp_maybe_release_buffers(amqp_connection_state_t state) {
  324. if (amqp_release_buffers_ok(state)) {
  325. amqp_release_buffers(state);
  326. }
  327. }
  328. void amqp_maybe_release_buffers_on_channel(amqp_connection_state_t state,
  329. amqp_channel_t channel) {
  330. amqp_link_t *queued_link;
  331. amqp_pool_t *pool;
  332. if (CONNECTION_STATE_IDLE != state->state) {
  333. return;
  334. }
  335. queued_link = state->first_queued_frame;
  336. while (NULL != queued_link) {
  337. amqp_frame_t *frame = queued_link->data;
  338. if (channel == frame->channel) {
  339. return;
  340. }
  341. queued_link = queued_link->next;
  342. }
  343. pool = amqp_get_channel_pool(state, channel);
  344. if (pool != NULL) {
  345. recycle_amqp_pool(pool);
  346. }
  347. }
  348. static int amqp_frame_to_bytes(const amqp_frame_t *frame, amqp_bytes_t buffer,
  349. amqp_bytes_t *encoded) {
  350. void *out_frame = buffer.bytes;
  351. size_t out_frame_len;
  352. int res;
  353. amqp_e8(frame->frame_type, amqp_offset(out_frame, 0));
  354. amqp_e16(frame->channel, amqp_offset(out_frame, 1));
  355. switch (frame->frame_type) {
  356. case AMQP_FRAME_BODY: {
  357. const amqp_bytes_t *body = &frame->payload.body_fragment;
  358. memcpy(amqp_offset(out_frame, HEADER_SIZE), body->bytes, body->len);
  359. out_frame_len = body->len;
  360. break;
  361. }
  362. case AMQP_FRAME_METHOD: {
  363. amqp_bytes_t method_encoded;
  364. amqp_e32(frame->payload.method.id, amqp_offset(out_frame, HEADER_SIZE));
  365. method_encoded.bytes = amqp_offset(out_frame, HEADER_SIZE + 4);
  366. method_encoded.len = buffer.len - HEADER_SIZE - 4 - FOOTER_SIZE;
  367. res = amqp_encode_method(frame->payload.method.id,
  368. frame->payload.method.decoded, method_encoded);
  369. if (res < 0) {
  370. return res;
  371. }
  372. out_frame_len = res + 4;
  373. break;
  374. }
  375. case AMQP_FRAME_HEADER: {
  376. amqp_bytes_t properties_encoded;
  377. amqp_e16(frame->payload.properties.class_id,
  378. amqp_offset(out_frame, HEADER_SIZE));
  379. amqp_e16(0, amqp_offset(out_frame, HEADER_SIZE + 2)); /* "weight" */
  380. amqp_e64(frame->payload.properties.body_size,
  381. amqp_offset(out_frame, HEADER_SIZE + 4));
  382. properties_encoded.bytes = amqp_offset(out_frame, HEADER_SIZE + 12);
  383. properties_encoded.len = buffer.len - HEADER_SIZE - 12 - FOOTER_SIZE;
  384. res = amqp_encode_properties(frame->payload.properties.class_id,
  385. frame->payload.properties.decoded,
  386. properties_encoded);
  387. if (res < 0) {
  388. return res;
  389. }
  390. out_frame_len = res + 12;
  391. break;
  392. }
  393. case AMQP_FRAME_HEARTBEAT:
  394. out_frame_len = 0;
  395. break;
  396. default:
  397. return AMQP_STATUS_INVALID_PARAMETER;
  398. }
  399. amqp_e32((uint32_t)out_frame_len, amqp_offset(out_frame, 3));
  400. amqp_e8(AMQP_FRAME_END, amqp_offset(out_frame, HEADER_SIZE + out_frame_len));
  401. encoded->bytes = out_frame;
  402. encoded->len = out_frame_len + HEADER_SIZE + FOOTER_SIZE;
  403. return AMQP_STATUS_OK;
  404. }
  405. int amqp_send_frame(amqp_connection_state_t state, const amqp_frame_t *frame) {
  406. return amqp_send_frame_inner(state, frame, AMQP_SF_NONE,
  407. amqp_time_infinite());
  408. }
  409. int amqp_send_frame_inner(amqp_connection_state_t state,
  410. const amqp_frame_t *frame, int flags,
  411. amqp_time_t deadline) {
  412. int res;
  413. ssize_t sent;
  414. amqp_bytes_t encoded;
  415. amqp_time_t next_timeout;
  416. /* TODO: if the AMQP_SF_MORE socket optimization can be shown to work
  417. * correctly, then this could be un-done so that body-frames are sent as 3
  418. * send calls, getting rid of the copy of the body content, some testing
  419. * would need to be done to see if this would actually a win for performance.
  420. * */
  421. res = amqp_frame_to_bytes(frame, state->outbound_buffer, &encoded);
  422. if (AMQP_STATUS_OK != res) {
  423. return res;
  424. }
  425. start_send:
  426. next_timeout = amqp_time_first(deadline, state->next_recv_heartbeat);
  427. sent = amqp_try_send(state, encoded.bytes, encoded.len, next_timeout, flags);
  428. if (0 > sent) {
  429. return (int)sent;
  430. }
  431. /* A partial send has occurred, because of a heartbeat timeout (so try recv
  432. * something) or common timeout (so return AMQP_STATUS_TIMEOUT) */
  433. if ((ssize_t)encoded.len != sent) {
  434. if (amqp_time_equal(next_timeout, deadline)) {
  435. /* timeout of method was received, so return from method*/
  436. return AMQP_STATUS_TIMEOUT;
  437. }
  438. res = amqp_try_recv(state);
  439. if (AMQP_STATUS_TIMEOUT == res) {
  440. return AMQP_STATUS_HEARTBEAT_TIMEOUT;
  441. } else if (AMQP_STATUS_OK != res) {
  442. return res;
  443. }
  444. encoded.bytes = (uint8_t *)encoded.bytes + sent;
  445. encoded.len -= sent;
  446. goto start_send;
  447. }
  448. res = amqp_time_s_from_now(&state->next_send_heartbeat,
  449. amqp_heartbeat_send(state));
  450. return res;
  451. }
  452. amqp_table_t *amqp_get_server_properties(amqp_connection_state_t state) {
  453. return &state->server_properties;
  454. }
  455. amqp_table_t *amqp_get_client_properties(amqp_connection_state_t state) {
  456. return &state->client_properties;
  457. }