| 1 | /* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */ | ||
| 2 | #ifndef librabbitmq_amqp_private_h | ||
| 3 | #define librabbitmq_amqp_private_h | ||
| 4 | |||
| 5 | /* | ||
| 6 | * ***** BEGIN LICENSE BLOCK ***** | ||
| 7 | * Version: MIT | ||
| 8 | * | ||
| 9 | * Portions created by Alan Antonuk are Copyright (c) 2012-2014 | ||
| 10 | * Alan Antonuk. All Rights Reserved. | ||
| 11 | * | ||
| 12 | * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc. | ||
| 13 | * All Rights Reserved. | ||
| 14 | * | ||
| 15 | * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010 | ||
| 16 | * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved. | ||
| 17 | * | ||
| 18 | * Permission is hereby granted, free of charge, to any person | ||
| 19 | * obtaining a copy of this software and associated documentation | ||
| 20 | * files (the "Software"), to deal in the Software without | ||
| 21 | * restriction, including without limitation the rights to use, copy, | ||
| 22 | * modify, merge, publish, distribute, sublicense, and/or sell copies | ||
| 23 | * of the Software, and to permit persons to whom the Software is | ||
| 24 | * furnished to do so, subject to the following conditions: | ||
| 25 | * | ||
| 26 | * The above copyright notice and this permission notice shall be | ||
| 27 | * included in all copies or substantial portions of the Software. | ||
| 28 | * | ||
| 29 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 30 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 31 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| 32 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||
| 33 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
| 34 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| 35 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 36 | * SOFTWARE. | ||
| 37 | * ***** END LICENSE BLOCK ***** | ||
| 38 | */ | ||
| 39 | |||
| 40 | #ifdef HAVE_CONFIG_H | ||
| 41 | #include "config.h" | ||
| 42 | #endif | ||
| 43 | |||
| 44 | #define AMQ_COPYRIGHT "Copyright (c) 2007-2014 VMWare Inc, Tony Garnock-Jones," \ | ||
| 45 | " and Alan Antonuk." | ||
| 46 | |||
| 47 | #include "amqp.h" | ||
| 48 | #include "amqp_framing.h" | ||
| 49 | #include <string.h> | ||
| 50 | |||
| 51 | #ifdef _WIN32 | ||
| 52 | # ifndef WINVER | ||
| 53 | /* WINVER 0x0502 is WinXP SP2+, Windows Server 2003 SP1+ | ||
| 54 | * See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa383745(v=vs.85).aspx#macros_for_conditional_declarations */ | ||
| 55 | # define WINVER 0x0502 | ||
| 56 | # endif | ||
| 57 | # ifndef WIN32_LEAN_AND_MEAN | ||
| 58 | # define WIN32_LEAN_AND_MEAN | ||
| 59 | # endif | ||
| 60 | # include <Winsock2.h> | ||
| 61 | #else | ||
| 62 | # include <arpa/inet.h> | ||
| 63 | # include <sys/uio.h> | ||
| 64 | #endif | ||
| 65 | |||
| 66 | /* GCC attributes */ | ||
| 67 | #if __GNUC__ > 2 | (__GNUC__ == 2 && __GNUC_MINOR__ > 4) | ||
| 68 | #define AMQP_NORETURN \ | ||
| 69 | __attribute__ ((__noreturn__)) | ||
| 70 | #define AMQP_UNUSED \ | ||
| 71 | __attribute__ ((__unused__)) | ||
| 72 | #else | ||
| 73 | #define AMQP_NORETURN | ||
| 74 | #define AMQP_UNUSED | ||
| 75 | #endif | ||
| 76 | |||
| 77 | #if __GNUC__ >= 4 | ||
| 78 | #define AMQP_PRIVATE \ | ||
| 79 | __attribute__ ((visibility ("hidden"))) | ||
| 80 | #else | ||
| 81 | #define AMQP_PRIVATE | ||
| 82 | #endif | ||
| 83 | |||
| 84 | char * | ||
| 85 | amqp_os_error_string(int err); | ||
| 86 | |||
| 87 | #ifdef WITH_SSL | ||
| 88 | char * | ||
| 89 | amqp_ssl_error_string(int err); | ||
| 90 | #endif | ||
| 91 | |||
| 92 | #include "amqp_socket.h" | ||
| 93 | #include "amqp_time.h" | ||
| 94 | |||
| 95 | /* | ||
| 96 | * Connection states: XXX FIX THIS | ||
| 97 | * | ||
| 98 | * - CONNECTION_STATE_INITIAL: The initial state, when we cannot be | ||
| 99 | * sure if the next thing we will get is the first AMQP frame, or a | ||
| 100 | * protocol header from the server. | ||
| 101 | * | ||
| 102 | * - CONNECTION_STATE_IDLE: The normal state between | ||
| 103 | * frames. Connections may only be reconfigured, and the | ||
| 104 | * connection's pools recycled, when in this state. Whenever we're | ||
| 105 | * in this state, the inbound_buffer's bytes pointer must be NULL; | ||
| 106 | * any other state, and it must point to a block of memory allocated | ||
| 107 | * from the frame_pool. | ||
| 108 | * | ||
| 109 | * - CONNECTION_STATE_HEADER: Some bytes of an incoming frame have | ||
| 110 | * been seen, but not a complete frame header's worth. | ||
| 111 | * | ||
| 112 | * - CONNECTION_STATE_BODY: A complete frame header has been seen, but | ||
| 113 | * the frame is not yet complete. When it is completed, it will be | ||
| 114 | * returned, and the connection will return to IDLE state. | ||
| 115 | * | ||
| 116 | */ | ||
| 117 | typedef enum amqp_connection_state_enum_ { | ||
| 118 | CONNECTION_STATE_IDLE = 0, | ||
| 119 | CONNECTION_STATE_INITIAL, | ||
| 120 | CONNECTION_STATE_HEADER, | ||
| 121 | CONNECTION_STATE_BODY | ||
| 122 | } amqp_connection_state_enum; | ||
| 123 | |||
| 124 | typedef enum amqp_status_private_enum_ | ||
| 125 | { | ||
| 126 | /* 0x00xx -> AMQP_STATUS_*/ | ||
| 127 | /* 0x01xx -> AMQP_STATUS_TCP_* */ | ||
| 128 | /* 0x02xx -> AMQP_STATUS_SSL_* */ | ||
| 129 | AMQP_PRIVATE_STATUS_SOCKET_NEEDREAD = -0x1301, | ||
| 130 | AMQP_PRIVATE_STATUS_SOCKET_NEEDWRITE = -0x1302 | ||
| 131 | } amqp_status_private_enum; | ||
| 132 | |||
| 133 | /* 7 bytes up front, then payload, then 1 byte footer */ | ||
| 134 | #define HEADER_SIZE 7 | ||
| 135 | #define FOOTER_SIZE 1 | ||
| 136 | |||
| 137 | #define AMQP_PSEUDOFRAME_PROTOCOL_HEADER 'A' | ||
| 138 | |||
| 139 | typedef struct amqp_link_t_ { | ||
| 140 | struct amqp_link_t_ *next; | ||
| 141 | void *data; | ||
| 142 | } amqp_link_t; | ||
| 143 | |||
| 144 | #define POOL_TABLE_SIZE 16 | ||
| 145 | |||
| 146 | typedef struct amqp_pool_table_entry_t_ { | ||
| 147 | struct amqp_pool_table_entry_t_ *next; | ||
| 148 | amqp_pool_t pool; | ||
| 149 | amqp_channel_t channel; | ||
| 150 | } amqp_pool_table_entry_t; | ||
| 151 | |||
| 152 | struct amqp_connection_state_t_ { | ||
| 153 | amqp_pool_table_entry_t *pool_table[POOL_TABLE_SIZE]; | ||
| 154 | |||
| 155 | amqp_connection_state_enum state; | ||
| 156 | |||
| 157 | int channel_max; | ||
| 158 | int frame_max; | ||
| 159 | |||
| 160 | /* Heartbeat interval in seconds. If this is <= 0, then heartbeats are not | ||
| 161 | * enabled, and next_recv_heartbeat and next_send_heartbeat are set to | ||
| 162 | * infinite */ | ||
| 163 | int heartbeat; | ||
| 164 | amqp_time_t next_recv_heartbeat; | ||
| 165 | amqp_time_t next_send_heartbeat; | ||
| 166 | |||
| 167 | /* buffer for holding frame headers. Allows us to delay allocating | ||
| 168 | * the raw frame buffer until the type, channel, and size are all known | ||
| 169 | */ | ||
| 170 | char header_buffer[HEADER_SIZE + 1]; | ||
| 171 | amqp_bytes_t inbound_buffer; | ||
| 172 | |||
| 173 | size_t inbound_offset; | ||
| 174 | size_t target_size; | ||
| 175 | |||
| 176 | amqp_bytes_t outbound_buffer; | ||
| 177 | |||
| 178 | amqp_socket_t *socket; | ||
| 179 | |||
| 180 | amqp_bytes_t sock_inbound_buffer; | ||
| 181 | size_t sock_inbound_offset; | ||
| 182 | size_t sock_inbound_limit; | ||
| 183 | |||
| 184 | amqp_link_t *first_queued_frame; | ||
| 185 | amqp_link_t *last_queued_frame; | ||
| 186 | |||
| 187 | amqp_rpc_reply_t most_recent_api_result; | ||
| 188 | |||
| 189 | amqp_table_t server_properties; | ||
| 190 | amqp_table_t client_properties; | ||
| 191 | amqp_pool_t properties_pool; | ||
| 192 | }; | ||
| 193 | |||
| 194 | amqp_pool_t *amqp_get_or_create_channel_pool(amqp_connection_state_t connection, amqp_channel_t channel); | ||
| 195 | amqp_pool_t *amqp_get_channel_pool(amqp_connection_state_t state, amqp_channel_t channel); | ||
| 196 | |||
| 197 | |||
| 198 | 83 | static inline int amqp_heartbeat_send(amqp_connection_state_t state) { | |
| 199 | 83 | return state->heartbeat; | |
| 200 | } | ||
| 201 | |||
| 202 | 27 | static inline int amqp_heartbeat_recv(amqp_connection_state_t state) { | |
| 203 | 27 | return 2 * state->heartbeat; | |
| 204 | } | ||
| 205 | |||
| 206 | int amqp_try_recv(amqp_connection_state_t state); | ||
| 207 | |||
| 208 | 964 | static inline void *amqp_offset(void *data, size_t offset) | |
| 209 | { | ||
| 210 | 964 | return (char *)data + offset; | |
| 211 | } | ||
| 212 | |||
| 213 | /* This macro defines the encoding and decoding functions associated with a | ||
| 214 | simple type. */ | ||
| 215 | |||
| 216 | #define DECLARE_CODEC_BASE_TYPE(bits, htonx, ntohx) \ | ||
| 217 | \ | ||
| 218 | static inline void amqp_e##bits(void *data, size_t offset, \ | ||
| 219 | uint##bits##_t val) \ | ||
| 220 | { \ | ||
| 221 | /* The AMQP data might be unaligned. So we encode and then copy the \ | ||
| 222 | result into place. */ \ | ||
| 223 | uint##bits##_t res = htonx(val); \ | ||
| 224 | memcpy(amqp_offset(data, offset), &res, bits/8); \ | ||
| 225 | } \ | ||
| 226 | \ | ||
| 227 | static inline uint##bits##_t amqp_d##bits(void *data, size_t offset) \ | ||
| 228 | { \ | ||
| 229 | /* The AMQP data might be unaligned. So we copy the source value \ | ||
| 230 | into a variable and then decode it. */ \ | ||
| 231 | uint##bits##_t val; \ | ||
| 232 | memcpy(&val, amqp_offset(data, offset), bits/8); \ | ||
| 233 | return ntohx(val); \ | ||
| 234 | } \ | ||
| 235 | \ | ||
| 236 | static inline int amqp_encode_##bits(amqp_bytes_t encoded, size_t *offset, \ | ||
| 237 | uint##bits##_t input) \ | ||
| 238 | \ | ||
| 239 | { \ | ||
| 240 | size_t o = *offset; \ | ||
| 241 | if ((*offset = o + bits / 8) <= encoded.len) { \ | ||
| 242 | amqp_e##bits(encoded.bytes, o, input); \ | ||
| 243 | return 1; \ | ||
| 244 | } \ | ||
| 245 | else { \ | ||
| 246 | return 0; \ | ||
| 247 | } \ | ||
| 248 | } \ | ||
| 249 | \ | ||
| 250 | static inline int amqp_decode_##bits(amqp_bytes_t encoded, size_t *offset, \ | ||
| 251 | uint##bits##_t *output) \ | ||
| 252 | \ | ||
| 253 | { \ | ||
| 254 | size_t o = *offset; \ | ||
| 255 | if ((*offset = o + bits / 8) <= encoded.len) { \ | ||
| 256 | *output = amqp_d##bits(encoded.bytes, o); \ | ||
| 257 | return 1; \ | ||
| 258 | } \ | ||
| 259 | else { \ | ||
| 260 | return 0; \ | ||
| 261 | } \ | ||
| 262 | } | ||
| 263 | |||
| 264 | /* Determine byte order */ | ||
| 265 | #if defined(__GLIBC__) | ||
| 266 | # include <endian.h> | ||
| 267 | # if (__BYTE_ORDER == __LITTLE_ENDIAN) | ||
| 268 | # define AMQP_LITTLE_ENDIAN | ||
| 269 | # elif (__BYTE_ORDER == __BIG_ENDIAN) | ||
| 270 | # define AMQP_BIG_ENDIAN | ||
| 271 | # else | ||
| 272 | /* Don't define anything */ | ||
| 273 | # endif | ||
| 274 | #elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) || \ | ||
| 275 | defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__) | ||
| 276 | # define AMQP_BIG_ENDIAN | ||
| 277 | #elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) || \ | ||
| 278 | defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__) | ||
| 279 | # define AMQP_LITTLE_ENDIAN | ||
| 280 | #elif defined(__hppa__) || defined(__HPPA__) || defined(__hppa) || \ | ||
| 281 | defined(_POWER) || defined(__powerpc__) || defined(__ppc___) || \ | ||
| 282 | defined(_MIPSEB) || defined(__s390__) || \ | ||
| 283 | defined(__sparc) || defined(__sparc__) | ||
| 284 | # define AMQP_BIG_ENDIAN | ||
| 285 | #elif defined(__alpha__) || defined(__alpha) || defined(_M_ALPHA) || \ | ||
| 286 | defined(__amd64__) || defined(__x86_64__) || defined(_M_X64) || \ | ||
| 287 | defined(__ia64) || defined(__ia64__) || defined(_M_IA64) || \ | ||
| 288 | defined(__arm__) || defined(_M_ARM) || \ | ||
| 289 | defined(__i386__) || defined(_M_IX86) | ||
| 290 | # define AMQP_LITTLE_ENDIAN | ||
| 291 | #else | ||
| 292 | /* Don't define anything */ | ||
| 293 | #endif | ||
| 294 | |||
| 295 | #if defined(AMQP_LITTLE_ENDIAN) | ||
| 296 | |||
| 297 | #define DECLARE_XTOXLL(func) \ | ||
| 298 | static inline uint64_t func##ll(uint64_t val) \ | ||
| 299 | { \ | ||
| 300 | union { \ | ||
| 301 | uint64_t whole; \ | ||
| 302 | uint32_t halves[2]; \ | ||
| 303 | } u; \ | ||
| 304 | uint32_t t; \ | ||
| 305 | u.whole = val; \ | ||
| 306 | t = u.halves[0]; \ | ||
| 307 | u.halves[0] = func##l(u.halves[1]); \ | ||
| 308 | u.halves[1] = func##l(t); \ | ||
| 309 | return u.whole; \ | ||
| 310 | } | ||
| 311 | |||
| 312 | #elif defined(AMQP_BIG_ENDIAN) | ||
| 313 | |||
| 314 | #define DECLARE_XTOXLL(func) \ | ||
| 315 | static inline uint64_t func##ll(uint64_t val) \ | ||
| 316 | { \ | ||
| 317 | union { \ | ||
| 318 | uint64_t whole; \ | ||
| 319 | uint32_t halves[2]; \ | ||
| 320 | } u; \ | ||
| 321 | u.whole = val; \ | ||
| 322 | u.halves[0] = func##l(u.halves[0]); \ | ||
| 323 | u.halves[1] = func##l(u.halves[1]); \ | ||
| 324 | return u.whole; \ | ||
| 325 | } | ||
| 326 | |||
| 327 | #else | ||
| 328 | # error Endianness not known | ||
| 329 | #endif | ||
| 330 | |||
| 331 | #ifndef HAVE_HTONLL | ||
| 332 | 0 | DECLARE_XTOXLL(hton) | |
| 333 | 0 | DECLARE_XTOXLL(ntoh) | |
| 334 | #endif | ||
| 335 | |||
| 336 | 228 | DECLARE_CODEC_BASE_TYPE(8, (uint8_t), (uint8_t)) | |
| 337 | 170 | DECLARE_CODEC_BASE_TYPE(16, htons, ntohs) | |
| 338 | 228 | DECLARE_CODEC_BASE_TYPE(32, htonl, ntohl) | |
| 339 | 0 | DECLARE_CODEC_BASE_TYPE(64, htonll, ntohll) | |
| 340 | |||
| 341 | static inline int amqp_encode_bytes(amqp_bytes_t encoded, size_t *offset, | ||
| 342 | amqp_bytes_t input) | ||
| 343 | { | ||
| 344 | size_t o = *offset; | ||
| 345 | if ((*offset = o + input.len) <= encoded.len) { | ||
| 346 | memcpy(amqp_offset(encoded.bytes, o), input.bytes, input.len); | ||
| 347 | return 1; | ||
| 348 | } else { | ||
| 349 | return 0; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | static inline int amqp_decode_bytes(amqp_bytes_t encoded, size_t *offset, | ||
| 354 | amqp_bytes_t *output, size_t len) | ||
| 355 | { | ||
| 356 | size_t o = *offset; | ||
| 357 | if ((*offset = o + len) <= encoded.len) { | ||
| 358 | output->bytes = amqp_offset(encoded.bytes, o); | ||
| 359 | output->len = len; | ||
| 360 | return 1; | ||
| 361 | } else { | ||
| 362 | return 0; | ||
| 363 | } | ||
| 364 | } | ||
| 365 | |||
| 366 | AMQP_NORETURN | ||
| 367 | void | ||
| 368 | amqp_abort(const char *fmt, ...); | ||
| 369 | |||
| 370 | int amqp_bytes_equal(amqp_bytes_t r, amqp_bytes_t l); | ||
| 371 | |||
| 372 | int amqp_send_frame_inner(amqp_connection_state_t state, | ||
| 373 | const amqp_frame_t *frame, int flags); | ||
| 374 | #endif |