amqp_consumer.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
  2. // SPDX-License-Identifier: mit
  3. #include "amqp_private.h"
  4. #include "amqp_socket.h"
  5. #include "rabbitmq-c/amqp.h"
  6. #include <stdlib.h>
  7. #include <string.h>
  8. static int amqp_basic_properties_clone(amqp_basic_properties_t *original,
  9. amqp_basic_properties_t *clone,
  10. amqp_pool_t *pool) {
  11. memset(clone, 0, sizeof(*clone));
  12. clone->_flags = original->_flags;
  13. #define CLONE_BYTES_POOL(original, clone, pool) \
  14. if (0 == original.len) { \
  15. clone = amqp_empty_bytes; \
  16. } else { \
  17. amqp_pool_alloc_bytes(pool, original.len, &clone); \
  18. if (NULL == clone.bytes) { \
  19. return AMQP_STATUS_NO_MEMORY; \
  20. } \
  21. memcpy(clone.bytes, original.bytes, clone.len); \
  22. }
  23. if (clone->_flags & AMQP_BASIC_CONTENT_TYPE_FLAG) {
  24. CLONE_BYTES_POOL(original->content_type, clone->content_type, pool)
  25. }
  26. if (clone->_flags & AMQP_BASIC_CONTENT_ENCODING_FLAG) {
  27. CLONE_BYTES_POOL(original->content_encoding, clone->content_encoding, pool)
  28. }
  29. if (clone->_flags & AMQP_BASIC_HEADERS_FLAG) {
  30. int res = amqp_table_clone(&original->headers, &clone->headers, pool);
  31. if (AMQP_STATUS_OK != res) {
  32. return res;
  33. }
  34. }
  35. if (clone->_flags & AMQP_BASIC_DELIVERY_MODE_FLAG) {
  36. clone->delivery_mode = original->delivery_mode;
  37. }
  38. if (clone->_flags & AMQP_BASIC_PRIORITY_FLAG) {
  39. clone->priority = original->priority;
  40. }
  41. if (clone->_flags & AMQP_BASIC_CORRELATION_ID_FLAG) {
  42. CLONE_BYTES_POOL(original->correlation_id, clone->correlation_id, pool)
  43. }
  44. if (clone->_flags & AMQP_BASIC_REPLY_TO_FLAG) {
  45. CLONE_BYTES_POOL(original->reply_to, clone->reply_to, pool)
  46. }
  47. if (clone->_flags & AMQP_BASIC_EXPIRATION_FLAG) {
  48. CLONE_BYTES_POOL(original->expiration, clone->expiration, pool)
  49. }
  50. if (clone->_flags & AMQP_BASIC_MESSAGE_ID_FLAG) {
  51. CLONE_BYTES_POOL(original->message_id, clone->message_id, pool)
  52. }
  53. if (clone->_flags & AMQP_BASIC_TIMESTAMP_FLAG) {
  54. clone->timestamp = original->timestamp;
  55. }
  56. if (clone->_flags & AMQP_BASIC_TYPE_FLAG) {
  57. CLONE_BYTES_POOL(original->type, clone->type, pool)
  58. }
  59. if (clone->_flags & AMQP_BASIC_USER_ID_FLAG) {
  60. CLONE_BYTES_POOL(original->user_id, clone->user_id, pool)
  61. }
  62. if (clone->_flags & AMQP_BASIC_APP_ID_FLAG) {
  63. CLONE_BYTES_POOL(original->app_id, clone->app_id, pool)
  64. }
  65. if (clone->_flags & AMQP_BASIC_CLUSTER_ID_FLAG) {
  66. CLONE_BYTES_POOL(original->cluster_id, clone->cluster_id, pool)
  67. }
  68. return AMQP_STATUS_OK;
  69. #undef CLONE_BYTES_POOL
  70. }
  71. void amqp_destroy_message(amqp_message_t *message) {
  72. empty_amqp_pool(&message->pool);
  73. amqp_bytes_free(message->body);
  74. }
  75. void amqp_destroy_envelope(amqp_envelope_t *envelope) {
  76. amqp_destroy_message(&envelope->message);
  77. amqp_bytes_free(envelope->routing_key);
  78. amqp_bytes_free(envelope->exchange);
  79. amqp_bytes_free(envelope->consumer_tag);
  80. }
  81. static int amqp_bytes_malloc_dup_failed(amqp_bytes_t bytes) {
  82. if (bytes.len != 0 && bytes.bytes == NULL) {
  83. return 1;
  84. }
  85. return 0;
  86. }
  87. amqp_rpc_reply_t amqp_consume_message(amqp_connection_state_t state,
  88. amqp_envelope_t *envelope,
  89. const struct timeval *timeout,
  90. AMQP_UNUSED int flags) {
  91. int res;
  92. amqp_frame_t frame;
  93. amqp_basic_deliver_t *delivery_method;
  94. amqp_rpc_reply_t ret;
  95. memset(&ret, 0, sizeof(ret));
  96. memset(envelope, 0, sizeof(*envelope));
  97. res = amqp_simple_wait_frame_noblock(state, &frame, timeout);
  98. if (AMQP_STATUS_OK != res) {
  99. ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
  100. ret.library_error = res;
  101. goto error_out1;
  102. }
  103. if (AMQP_FRAME_METHOD != frame.frame_type ||
  104. AMQP_BASIC_DELIVER_METHOD != frame.payload.method.id) {
  105. amqp_put_back_frame(state, &frame);
  106. ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
  107. ret.library_error = AMQP_STATUS_UNEXPECTED_STATE;
  108. goto error_out1;
  109. }
  110. delivery_method = frame.payload.method.decoded;
  111. envelope->channel = frame.channel;
  112. envelope->consumer_tag = amqp_bytes_malloc_dup(delivery_method->consumer_tag);
  113. envelope->delivery_tag = delivery_method->delivery_tag;
  114. envelope->redelivered = delivery_method->redelivered;
  115. envelope->exchange = amqp_bytes_malloc_dup(delivery_method->exchange);
  116. envelope->routing_key = amqp_bytes_malloc_dup(delivery_method->routing_key);
  117. if (amqp_bytes_malloc_dup_failed(envelope->consumer_tag) ||
  118. amqp_bytes_malloc_dup_failed(envelope->exchange) ||
  119. amqp_bytes_malloc_dup_failed(envelope->routing_key)) {
  120. ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
  121. ret.library_error = AMQP_STATUS_NO_MEMORY;
  122. goto error_out2;
  123. }
  124. ret = amqp_read_message(state, envelope->channel, &envelope->message, 0);
  125. if (AMQP_RESPONSE_NORMAL != ret.reply_type) {
  126. goto error_out2;
  127. }
  128. ret.reply_type = AMQP_RESPONSE_NORMAL;
  129. return ret;
  130. error_out2:
  131. amqp_bytes_free(envelope->routing_key);
  132. amqp_bytes_free(envelope->exchange);
  133. amqp_bytes_free(envelope->consumer_tag);
  134. error_out1:
  135. return ret;
  136. }
  137. amqp_rpc_reply_t amqp_read_message(amqp_connection_state_t state,
  138. amqp_channel_t channel,
  139. amqp_message_t *message,
  140. AMQP_UNUSED int flags) {
  141. amqp_frame_t frame;
  142. amqp_rpc_reply_t ret;
  143. size_t body_read;
  144. char *body_read_ptr;
  145. int res;
  146. memset(&ret, 0, sizeof(ret));
  147. memset(message, 0, sizeof(*message));
  148. res = amqp_simple_wait_frame_on_channel(state, channel, &frame);
  149. if (AMQP_STATUS_OK != res) {
  150. ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
  151. ret.library_error = res;
  152. goto error_out1;
  153. }
  154. if (AMQP_FRAME_HEADER != frame.frame_type) {
  155. if (AMQP_FRAME_METHOD == frame.frame_type &&
  156. (AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id ||
  157. AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) {
  158. ret.reply_type = AMQP_RESPONSE_SERVER_EXCEPTION;
  159. ret.reply = frame.payload.method;
  160. } else {
  161. ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
  162. ret.library_error = AMQP_STATUS_UNEXPECTED_STATE;
  163. amqp_put_back_frame(state, &frame);
  164. }
  165. goto error_out1;
  166. }
  167. init_amqp_pool(&message->pool, 4096);
  168. res = amqp_basic_properties_clone(frame.payload.properties.decoded,
  169. &message->properties, &message->pool);
  170. if (AMQP_STATUS_OK != res) {
  171. ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
  172. ret.library_error = res;
  173. goto error_out3;
  174. }
  175. if (0 == frame.payload.properties.body_size) {
  176. message->body = amqp_empty_bytes;
  177. } else {
  178. if (SIZE_MAX < frame.payload.properties.body_size) {
  179. ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
  180. ret.library_error = AMQP_STATUS_NO_MEMORY;
  181. goto error_out1;
  182. }
  183. message->body =
  184. amqp_bytes_malloc((size_t)frame.payload.properties.body_size);
  185. if (NULL == message->body.bytes) {
  186. ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
  187. ret.library_error = AMQP_STATUS_NO_MEMORY;
  188. goto error_out1;
  189. }
  190. }
  191. body_read = 0;
  192. body_read_ptr = message->body.bytes;
  193. while (body_read < message->body.len) {
  194. res = amqp_simple_wait_frame_on_channel(state, channel, &frame);
  195. if (AMQP_STATUS_OK != res) {
  196. ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
  197. ret.library_error = res;
  198. goto error_out2;
  199. }
  200. if (AMQP_FRAME_BODY != frame.frame_type) {
  201. if (AMQP_FRAME_METHOD == frame.frame_type &&
  202. (AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id ||
  203. AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) {
  204. ret.reply_type = AMQP_RESPONSE_SERVER_EXCEPTION;
  205. ret.reply = frame.payload.method;
  206. } else {
  207. ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
  208. ret.library_error = AMQP_STATUS_BAD_AMQP_DATA;
  209. }
  210. goto error_out2;
  211. }
  212. if (body_read + frame.payload.body_fragment.len > message->body.len) {
  213. ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
  214. ret.library_error = AMQP_STATUS_BAD_AMQP_DATA;
  215. goto error_out2;
  216. }
  217. memcpy(body_read_ptr, frame.payload.body_fragment.bytes,
  218. frame.payload.body_fragment.len);
  219. body_read += frame.payload.body_fragment.len;
  220. body_read_ptr += frame.payload.body_fragment.len;
  221. }
  222. ret.reply_type = AMQP_RESPONSE_NORMAL;
  223. return ret;
  224. error_out2:
  225. amqp_bytes_free(message->body);
  226. error_out3:
  227. empty_amqp_pool(&message->pool);
  228. error_out1:
  229. return ret;
  230. }