| 1 |
|
| /* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */ |
| 2 |
|
| /* |
| 3 |
|
| * ***** BEGIN LICENSE BLOCK ***** |
| 4 |
|
| * Version: MIT |
| 5 |
|
| * |
| 6 |
|
| * Portions created by Alan Antonuk are Copyright (c) 2012-2013 |
| 7 |
|
| * Alan Antonuk. All Rights Reserved. |
| 8 |
|
| * |
| 9 |
|
| * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc. |
| 10 |
|
| * All Rights Reserved. |
| 11 |
|
| * |
| 12 |
|
| * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010 |
| 13 |
|
| * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved. |
| 14 |
|
| * |
| 15 |
|
| * Permission is hereby granted, free of charge, to any person |
| 16 |
|
| * obtaining a copy of this software and associated documentation |
| 17 |
|
| * files (the "Software"), to deal in the Software without |
| 18 |
|
| * restriction, including without limitation the rights to use, copy, |
| 19 |
|
| * modify, merge, publish, distribute, sublicense, and/or sell copies |
| 20 |
|
| * of the Software, and to permit persons to whom the Software is |
| 21 |
|
| * furnished to do so, subject to the following conditions: |
| 22 |
|
| * |
| 23 |
|
| * The above copyright notice and this permission notice shall be |
| 24 |
|
| * included in all copies or substantial portions of the Software. |
| 25 |
|
| * |
| 26 |
|
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 27 |
|
| * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 28 |
|
| * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 29 |
|
| * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 30 |
|
| * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 31 |
|
| * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 32 |
|
| * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 33 |
|
| * SOFTWARE. |
| 34 |
|
| * ***** END LICENSE BLOCK ***** |
| 35 |
|
| */ |
| 36 |
|
| |
| 37 |
|
| #ifdef HAVE_CONFIG_H |
| 38 |
|
| #include "config.h" |
| 39 |
|
| #endif |
| 40 |
|
| |
| 41 |
|
| #ifdef _MSC_VER |
| 42 |
|
| /* MSVC complains about sprintf being deprecated in favor of sprintf_s */ |
| 43 |
|
| # define _CRT_SECURE_NO_WARNINGS |
| 44 |
|
| /* MSVC complains about strdup being deprecated in favor of _strdup */ |
| 45 |
|
| # define _CRT_NONSTDC_NO_DEPRECATE |
| 46 |
|
| #endif |
| 47 |
|
| |
| 48 |
|
| #include "amqp_private.h" |
| 49 |
|
| #include "amqp_time.h" |
| 50 |
|
| #include <assert.h> |
| 51 |
|
| #include <stdarg.h> |
| 52 |
|
| #include <stdint.h> |
| 53 |
|
| #include <stdio.h> |
| 54 |
|
| #include <stdlib.h> |
| 55 |
|
| #include <string.h> |
| 56 |
|
| |
| 57 |
|
| #define ERROR_MASK (0x00FF) |
| 58 |
|
| #define ERROR_CATEGORY_MASK (0xFF00) |
| 59 |
|
| |
| 60 |
|
| enum error_category_enum_ { |
| 61 |
|
| EC_base = 0, |
| 62 |
|
| EC_tcp = 1, |
| 63 |
|
| EC_ssl = 2 |
| 64 |
|
| }; |
| 65 |
|
| |
| 66 |
|
| static const char *base_error_strings[] = { |
| 67 |
|
| "operation completed successfully", /* AMQP_STATUS_OK 0x0 */ |
| 68 |
|
| "could not allocate memory", /* AMQP_STATUS_NO_MEMORY -0x0001 */ |
| 69 |
|
| "invalid AMQP data", /* AMQP_STATUS_BAD_AQMP_DATA -0x0002 */ |
| 70 |
|
| "unknown AMQP class id", /* AMQP_STATUS_UNKNOWN_CLASS -0x0003 */ |
| 71 |
|
| "unknown AMQP method id", /* AMQP_STATUS_UNKNOWN_METHOD -0x0004 */ |
| 72 |
|
| "hostname lookup failed", /* AMQP_STATUS_HOSTNAME_RESOLUTION_FAILED -0x0005 */ |
| 73 |
|
| "incompatible AMQP version", /* AMQP_STATUS_INCOMPATIBLE_AMQP_VERSION -0x0006 */ |
| 74 |
|
| "connection closed unexpectedly", /* AMQP_STATUS_CONNECTION_CLOSED -0x0007 */ |
| 75 |
|
| "could not parse AMQP URL", /* AMQP_STATUS_BAD_AMQP_URL -0x0008 */ |
| 76 |
|
| "a socket error occurred", /* AMQP_STATUS_SOCKET_ERROR -0x0009 */ |
| 77 |
|
| "invalid parameter", /* AMQP_STATUS_INVALID_PARAMETER -0x000A */ |
| 78 |
|
| "table too large for buffer", /* AMQP_STATUS_TABLE_TOO_BIG -0x000B */ |
| 79 |
|
| "unexpected method received", /* AMQP_STATUS_WRONG_METHOD -0x000C */ |
| 80 |
|
| "request timed out", /* AMQP_STATUS_TIMEOUT -0x000D */ |
| 81 |
|
| "system timer has failed", /* AMQP_STATUS_TIMER_FAILED -0x000E */ |
| 82 |
|
| "heartbeat timeout, connection closed",/* AMQP_STATUS_HEARTBEAT_TIMEOUT -0x000F */ |
| 83 |
|
| "unexpected protocol state", /* AMQP_STATUS_UNEXPECTED STATE -0x0010 */ |
| 84 |
|
| "socket is closed", /* AMQP_STATUS_SOCKET_CLOSED -0x0011 */ |
| 85 |
|
| "socket already open", /* AMQP_STATUS_SOCKET_INUSE -0x0012 */ |
| 86 |
|
| "unsupported sasl method requested" /* AMQP_STATUS_BROKER_UNSUPPORTED_SASL_METHOD -0x0013 */ |
| 87 |
|
| }; |
| 88 |
|
| |
| 89 |
|
| static const char *tcp_error_strings[] = { |
| 90 |
|
| "a socket error occurred", /* AMQP_STATUS_TCP_ERROR -0x0100 */ |
| 91 |
|
| "socket library initialization failed" /* AMQP_STATUS_TCP_SOCKETLIB_INIT_ERROR -0x0101 */ |
| 92 |
|
| }; |
| 93 |
|
| |
| 94 |
|
| static const char *ssl_error_strings[] = { |
| 95 |
|
| "a SSL error occurred", /* AMQP_STATUS_SSL_ERROR -0x0200 */ |
| 96 |
|
| "SSL hostname verification failed", /* AMQP_STATUS_SSL_HOSTNAME_VERIFY_FAILED -0x0201 */ |
| 97 |
|
| "SSL peer cert verification failed", /* AMQP_STATUS_SSL_PEER_VERIFY_FAILED -0x0202 */ |
| 98 |
|
| "SSL handshake failed" /* AMQP_STATUS_SSL_CONNECTION_FAILED -0x0203 */ |
| 99 |
|
| }; |
| 100 |
|
| |
| 101 |
|
| static const char *unknown_error_string = "(unknown error)"; |
| 102 |
|
| |
| 103 |
34 |
| const char *amqp_error_string2(int code) |
| 104 |
|
| { |
| 105 |
|
| const char *error_string; |
| 106 |
34 |
| size_t category = (((-code) & ERROR_CATEGORY_MASK) >> 8); |
| 107 |
34 |
| size_t error = (-code) & ERROR_MASK; |
| 108 |
|
| |
| 109 |
34 |
| switch (category) { |
| 110 |
34 |
| case EC_base: |
| 111 |
34 |
▶ | if (error < (sizeof(base_error_strings) / sizeof(char *))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (error < (sizeof(base_error_strings) / sizeof(char *))) { |
34 |
0 |
34 |
|
| 112 |
34 |
| error_string = base_error_strings[error]; |
| 113 |
|
| } else { |
| 114 |
0 |
| error_string = unknown_error_string; |
| 115 |
|
| } |
| 116 |
34 |
| break; |
| 117 |
|
| |
| 118 |
0 |
| case EC_tcp: |
| 119 |
0 |
▶ | if (error < (sizeof(tcp_error_strings) / sizeof(char *))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (error < (sizeof(tcp_error_strings) / sizeof(char *))) { |
0 |
0 |
0 |
|
| 120 |
0 |
| error_string = tcp_error_strings[error]; |
| 121 |
|
| } else { |
| 122 |
0 |
| error_string = unknown_error_string; |
| 123 |
|
| } |
| 124 |
0 |
| break; |
| 125 |
|
| |
| 126 |
0 |
| case EC_ssl: |
| 127 |
0 |
▶ | if (error < (sizeof(ssl_error_strings) / sizeof(char *))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (error < (sizeof(ssl_error_strings) / sizeof(char *))) { |
0 |
0 |
0 |
|
| 128 |
0 |
| error_string = ssl_error_strings[error]; |
| 129 |
|
| } else { |
| 130 |
0 |
| error_string = unknown_error_string; |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
0 |
| break; |
| 134 |
|
| |
| 135 |
0 |
| default: |
| 136 |
0 |
| error_string = unknown_error_string; |
| 137 |
0 |
| break; |
| 138 |
|
| |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
34 |
| return error_string; |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
0 |
| char *amqp_error_string(int code) |
| 145 |
|
| { |
| 146 |
|
| /* Previously sometimes clients had to flip the sign on a return value from a |
| 147 |
|
| * function to get the correct error code. Now, all error codes are negative. |
| 148 |
|
| * To keep people's legacy code running correctly, we map all error codes to |
| 149 |
|
| * negative values. |
| 150 |
|
| * |
| 151 |
|
| * This is only done with this deprecated function. |
| 152 |
|
| */ |
| 153 |
0 |
▶ | if (code > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (code > 0) { |
0 |
0 |
0 |
|
| 154 |
0 |
| code = -code; |
| 155 |
|
| } |
| 156 |
0 |
| return strdup(amqp_error_string2(code)); |
| 157 |
|
| } |
| 158 |
|
| |
| 159 |
0 |
| void amqp_abort(const char *fmt, ...) |
| 160 |
|
| { |
| 161 |
|
| va_list ap; |
| 162 |
0 |
| va_start(ap, fmt); |
| 163 |
0 |
| vfprintf(stderr, fmt, ap); |
| 164 |
0 |
| va_end(ap); |
| 165 |
0 |
| fputc('\n', stderr); |
| 166 |
0 |
| abort(); |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
|
| const amqp_bytes_t amqp_empty_bytes = { 0, NULL }; |
| 170 |
|
| const amqp_table_t amqp_empty_table = { 0, NULL }; |
| 171 |
|
| const amqp_array_t amqp_empty_array = { 0, NULL }; |
| 172 |
|
| |
| 173 |
|
| #define RPC_REPLY(replytype)\ |
| 174 |
|
| (state->most_recent_api_result.reply_type == AMQP_RESPONSE_NORMAL\ |
| 175 |
|
| ? (replytype *) state->most_recent_api_result.reply.decoded\ |
| 176 |
|
| : NULL) |
| 177 |
|
| |
| 178 |
0 |
| int amqp_basic_publish(amqp_connection_state_t state, |
| 179 |
|
| amqp_channel_t channel, |
| 180 |
|
| amqp_bytes_t exchange, |
| 181 |
|
| amqp_bytes_t routing_key, |
| 182 |
|
| amqp_boolean_t mandatory, |
| 183 |
|
| amqp_boolean_t immediate, |
| 184 |
|
| amqp_basic_properties_t const *properties, |
| 185 |
|
| amqp_bytes_t body) |
| 186 |
|
| { |
| 187 |
|
| amqp_frame_t f; |
| 188 |
|
| size_t body_offset; |
| 189 |
0 |
| size_t usable_body_payload_size = state->frame_max - (HEADER_SIZE + FOOTER_SIZE); |
| 190 |
|
| int res; |
| 191 |
|
| |
| 192 |
|
| amqp_basic_publish_t m; |
| 193 |
|
| amqp_basic_properties_t default_properties; |
| 194 |
|
| |
| 195 |
0 |
| m.exchange = exchange; |
| 196 |
0 |
| m.routing_key = routing_key; |
| 197 |
0 |
| m.mandatory = mandatory; |
| 198 |
0 |
| m.immediate = immediate; |
| 199 |
0 |
| m.ticket = 0; |
| 200 |
|
| |
| 201 |
|
| /* TODO(alanxz): this heartbeat check is happening in the wrong place, it |
| 202 |
|
| * should really be done in amqp_try_send/writev */ |
| 203 |
0 |
| res = amqp_time_has_past(state->next_recv_heartbeat); |
| 204 |
0 |
▶ | if (AMQP_STATUS_TIMER_FAILURE == res) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (AMQP_STATUS_TIMER_FAILURE == res) { |
0 |
0 |
0 |
|
| 205 |
0 |
| return res; |
| 206 |
0 |
▶ | } else if (AMQP_STATUS_TIMEOUT == res) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (AMQP_STATUS_TIMEOUT == res) { |
0 |
0 |
0 |
|
| 207 |
0 |
| res = amqp_try_recv(state); |
| 208 |
0 |
▶ | if (AMQP_STATUS_TIMEOUT == res) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (AMQP_STATUS_TIMEOUT == res) { |
0 |
0 |
0 |
|
| 209 |
0 |
| return AMQP_STATUS_HEARTBEAT_TIMEOUT; |
| 210 |
0 |
▶ | } else if (AMQP_STATUS_OK != res) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (AMQP_STATUS_OK != res) { |
0 |
0 |
0 |
|
| 211 |
0 |
| return res; |
| 212 |
|
| } |
| 213 |
|
| } |
| 214 |
|
| |
| 215 |
0 |
| res = amqp_send_method_inner(state, channel, AMQP_BASIC_PUBLISH_METHOD, &m, |
| 216 |
|
| AMQP_SF_MORE); |
| 217 |
0 |
▶ | if (res < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (res < 0) { |
0 |
0 |
0 |
|
| 218 |
0 |
| return res; |
| 219 |
|
| } |
| 220 |
|
| |
| 221 |
0 |
▶ | if (properties == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (properties == NULL) { |
0 |
0 |
0 |
|
| 222 |
0 |
| memset(&default_properties, 0, sizeof(default_properties)); |
| 223 |
0 |
| properties = &default_properties; |
| 224 |
|
| } |
| 225 |
|
| |
| 226 |
0 |
| f.frame_type = AMQP_FRAME_HEADER; |
| 227 |
0 |
| f.channel = channel; |
| 228 |
0 |
| f.payload.properties.class_id = AMQP_BASIC_CLASS; |
| 229 |
0 |
| f.payload.properties.body_size = body.len; |
| 230 |
0 |
| f.payload.properties.decoded = (void *) properties; |
| 231 |
|
| |
| 232 |
0 |
| res = amqp_send_frame_inner(state, &f, AMQP_SF_MORE); |
| 233 |
0 |
▶ | if (res < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (res < 0) { |
0 |
0 |
0 |
|
| 234 |
0 |
| return res; |
| 235 |
|
| } |
| 236 |
|
| |
| 237 |
0 |
| body_offset = 0; |
| 238 |
0 |
▶ | while (body_offset < body.len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (body_offset < body.len) { |
0 |
0 |
0 |
|
| 239 |
0 |
| size_t remaining = body.len - body_offset; |
| 240 |
|
| int flagz; |
| 241 |
|
| |
| 242 |
0 |
▶ | if (remaining == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (remaining == 0) { |
0 |
0 |
0 |
|
| 243 |
0 |
| break; |
| 244 |
|
| } |
| 245 |
|
| |
| 246 |
0 |
| f.frame_type = AMQP_FRAME_BODY; |
| 247 |
0 |
| f.channel = channel; |
| 248 |
0 |
| f.payload.body_fragment.bytes = amqp_offset(body.bytes, body_offset); |
| 249 |
0 |
▶ | if (remaining >= usable_body_payload_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (remaining >= usable_body_payload_size) { |
0 |
0 |
0 |
|
| 250 |
0 |
| f.payload.body_fragment.len = usable_body_payload_size; |
| 251 |
0 |
| flagz = AMQP_SF_MORE; |
| 252 |
|
| } else { |
| 253 |
0 |
| f.payload.body_fragment.len = remaining; |
| 254 |
0 |
| flagz = AMQP_SF_NONE; |
| 255 |
|
| } |
| 256 |
|
| |
| 257 |
0 |
| body_offset += f.payload.body_fragment.len; |
| 258 |
0 |
| res = amqp_send_frame_inner(state, &f, flagz); |
| 259 |
0 |
▶ | if (res < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (res < 0) { |
0 |
0 |
0 |
|
| 260 |
0 |
| return res; |
| 261 |
|
| } |
| 262 |
|
| } |
| 263 |
|
| |
| 264 |
0 |
| return AMQP_STATUS_OK; |
| 265 |
|
| } |
| 266 |
|
| |
| 267 |
0 |
| amqp_rpc_reply_t amqp_channel_close(amqp_connection_state_t state, |
| 268 |
|
| amqp_channel_t channel, |
| 269 |
|
| int code) |
| 270 |
|
| { |
| 271 |
|
| char codestr[13]; |
| 272 |
0 |
| amqp_method_number_t replies[2] = { AMQP_CHANNEL_CLOSE_OK_METHOD, 0}; |
| 273 |
|
| amqp_channel_close_t req; |
| 274 |
|
| |
| 275 |
0 |
| req.reply_code = code; |
| 276 |
0 |
| req.reply_text.bytes = codestr; |
| 277 |
0 |
| req.reply_text.len = sprintf(codestr, "%d", code); |
| 278 |
0 |
| req.class_id = 0; |
| 279 |
0 |
| req.method_id = 0; |
| 280 |
|
| |
| 281 |
0 |
| return amqp_simple_rpc(state, channel, AMQP_CHANNEL_CLOSE_METHOD, |
| 282 |
|
| replies, &req); |
| 283 |
|
| } |
| 284 |
|
| |
| 285 |
32 |
| amqp_rpc_reply_t amqp_connection_close(amqp_connection_state_t state, |
| 286 |
|
| int code) |
| 287 |
|
| { |
| 288 |
|
| char codestr[13]; |
| 289 |
32 |
| amqp_method_number_t replies[2] = { AMQP_CONNECTION_CLOSE_OK_METHOD, 0}; |
| 290 |
|
| amqp_channel_close_t req; |
| 291 |
|
| |
| 292 |
32 |
| req.reply_code = code; |
| 293 |
32 |
| req.reply_text.bytes = codestr; |
| 294 |
32 |
| req.reply_text.len = sprintf(codestr, "%d", code); |
| 295 |
32 |
| req.class_id = 0; |
| 296 |
32 |
| req.method_id = 0; |
| 297 |
|
| |
| 298 |
32 |
| return amqp_simple_rpc(state, 0, AMQP_CONNECTION_CLOSE_METHOD, |
| 299 |
|
| replies, &req); |
| 300 |
|
| } |
| 301 |
|
| |
| 302 |
0 |
| int amqp_basic_ack(amqp_connection_state_t state, |
| 303 |
|
| amqp_channel_t channel, |
| 304 |
|
| uint64_t delivery_tag, |
| 305 |
|
| amqp_boolean_t multiple) |
| 306 |
|
| { |
| 307 |
|
| amqp_basic_ack_t m; |
| 308 |
0 |
| m.delivery_tag = delivery_tag; |
| 309 |
0 |
| m.multiple = multiple; |
| 310 |
0 |
| return amqp_send_method(state, channel, AMQP_BASIC_ACK_METHOD, &m); |
| 311 |
|
| } |
| 312 |
|
| |
| 313 |
0 |
| amqp_rpc_reply_t amqp_basic_get(amqp_connection_state_t state, |
| 314 |
|
| amqp_channel_t channel, |
| 315 |
|
| amqp_bytes_t queue, |
| 316 |
|
| amqp_boolean_t no_ack) |
| 317 |
|
| { |
| 318 |
0 |
| amqp_method_number_t replies[] = { AMQP_BASIC_GET_OK_METHOD, |
| 319 |
|
| AMQP_BASIC_GET_EMPTY_METHOD, |
| 320 |
|
| 0 |
| 321 |
|
| }; |
| 322 |
|
| amqp_basic_get_t req; |
| 323 |
0 |
| req.ticket = 0; |
| 324 |
0 |
| req.queue = queue; |
| 325 |
0 |
| req.no_ack = no_ack; |
| 326 |
|
| |
| 327 |
0 |
| state->most_recent_api_result = amqp_simple_rpc(state, channel, |
| 328 |
|
| AMQP_BASIC_GET_METHOD, |
| 329 |
|
| replies, &req); |
| 330 |
0 |
| return state->most_recent_api_result; |
| 331 |
|
| } |
| 332 |
|
| |
| 333 |
0 |
| int amqp_basic_reject(amqp_connection_state_t state, |
| 334 |
|
| amqp_channel_t channel, |
| 335 |
|
| uint64_t delivery_tag, |
| 336 |
|
| amqp_boolean_t requeue) |
| 337 |
|
| { |
| 338 |
|
| amqp_basic_reject_t req; |
| 339 |
0 |
| req.delivery_tag = delivery_tag; |
| 340 |
0 |
| req.requeue = requeue; |
| 341 |
0 |
| return amqp_send_method(state, channel, AMQP_BASIC_REJECT_METHOD, &req); |
| 342 |
|
| } |
| 343 |
|
| |
| 344 |
0 |
| int amqp_basic_nack(amqp_connection_state_t state, amqp_channel_t channel, |
| 345 |
|
| uint64_t delivery_tag, amqp_boolean_t multiple, |
| 346 |
|
| amqp_boolean_t requeue) |
| 347 |
|
| { |
| 348 |
|
| amqp_basic_nack_t req; |
| 349 |
0 |
| req.delivery_tag = delivery_tag; |
| 350 |
0 |
| req.multiple = multiple; |
| 351 |
0 |
| req.requeue = requeue; |
| 352 |
0 |
| return amqp_send_method(state, channel, AMQP_BASIC_NACK_METHOD, &req); |
| 353 |
|
| } |