~ubuntu-cloud-archive/ubuntu/precise/librabbitmq/precise-icehouse

« back to all changes in this revision

Viewing changes to librabbitmq/amqp_private.h

  • Committer: Package Import Robot
  • Author(s): Michael Fladischer
  • Date: 2013-11-04 20:15:55 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20131104201555-wdjlgotdjansj1j9
Tags: 0.4.1-1
* Imported Upstream version 0.4.1
* Add libssl-dev to Build-Depends.
* Update librabbitmq1.symbols.
* Switch buildsystem to cmake.
  + Add cmake_multiarch.patch to fix multiarch installation paths for shared
    library files in cmake.
  * Add cmake to Build-Depends.
  * Drop dh-autoreconf from Build-Depends.
* Format packaging files with wrap-and-sort.
* Update d/watch file to use github releases instead of tags.
* Bump Standards version to 3.9.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */
1
2
#ifndef librabbitmq_amqp_private_h
2
3
#define librabbitmq_amqp_private_h
3
4
 
5
6
 * ***** BEGIN LICENSE BLOCK *****
6
7
 * Version: MIT
7
8
 *
 
9
 * Portions created by Alan Antonuk are Copyright (c) 2012-2013
 
10
 * Alan Antonuk. All Rights Reserved.
 
11
 *
8
12
 * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
9
13
 * All Rights Reserved.
10
14
 *
41
45
#include "amqp_framing.h"
42
46
#include <string.h>
43
47
 
44
 
/* Error numbering: Because of differences in error numbering on
45
 
 * different platforms, we want to keep error numbers opaque for
46
 
 * client code.  Internally, we encode the category of an error
47
 
 * (i.e. where its number comes from) in the top bits of the number
48
 
 * (assuming that an int has at least 32 bits).
49
 
 */
50
 
#define ERROR_CATEGORY_MASK (1 << 29)
51
 
 
52
 
#define ERROR_CATEGORY_CLIENT (0 << 29) /* librabbitmq error codes */
53
 
#define ERROR_CATEGORY_OS (1 << 29) /* OS-specific error codes */
54
 
 
55
 
/* librabbitmq error codes */
56
 
#define ERROR_NO_MEMORY 1
57
 
#define ERROR_BAD_AMQP_DATA 2
58
 
#define ERROR_UNKNOWN_CLASS 3
59
 
#define ERROR_UNKNOWN_METHOD 4
60
 
#define ERROR_GETHOSTBYNAME_FAILED 5
61
 
#define ERROR_INCOMPATIBLE_AMQP_VERSION 6
62
 
#define ERROR_CONNECTION_CLOSED 7
63
 
#define ERROR_BAD_AMQP_URL 8
64
 
#define ERROR_MAX 8
 
48
#ifdef _WIN32
 
49
# include <Winsock2.h>
 
50
#else
 
51
# include <arpa/inet.h>
 
52
#endif
65
53
 
66
54
/* GCC attributes */
67
55
#if __GNUC__ > 2 | (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
68
56
#define AMQP_NORETURN \
69
57
  __attribute__ ((__noreturn__))
 
58
#define AMQP_UNUSED \
 
59
  __attribute__ ((__unused__))
70
60
#else
71
61
#define AMQP_NORETURN
 
62
#define AMQP_UNUSED
72
63
#endif
73
64
 
74
65
#if __GNUC__ >= 4
81
72
char *
82
73
amqp_os_error_string(int err);
83
74
 
84
 
#include "socket.h"
 
75
#ifdef WITH_SSL
 
76
char *
 
77
amqp_ssl_error_string(int err);
 
78
#endif
 
79
 
 
80
#include "amqp_socket.h"
 
81
#include "amqp_timer.h"
85
82
 
86
83
/*
87
84
 * Connection states: XXX FIX THIS
123
120
  void *data;
124
121
} amqp_link_t;
125
122
 
 
123
#define POOL_TABLE_SIZE 16
 
124
 
 
125
typedef struct amqp_pool_table_entry_t_ {
 
126
  struct amqp_pool_table_entry_t_ *next;
 
127
  amqp_pool_t pool;
 
128
  amqp_channel_t channel;
 
129
} amqp_pool_table_entry_t;
 
130
 
126
131
struct amqp_connection_state_t_ {
127
 
  amqp_pool_t frame_pool;
128
 
  amqp_pool_t decoding_pool;
 
132
  amqp_pool_table_entry_t *pool_table[POOL_TABLE_SIZE];
129
133
 
130
134
  amqp_connection_state_enum state;
131
135
 
132
136
  int channel_max;
133
137
  int frame_max;
134
138
  int heartbeat;
 
139
 
 
140
  /* buffer for holding frame headers.  Allows us to delay allocating
 
141
   * the raw frame buffer until the type, channel, and size are all known
 
142
   */
 
143
  char header_buffer[HEADER_SIZE + 1];
135
144
  amqp_bytes_t inbound_buffer;
136
145
 
137
146
  size_t inbound_offset;
139
148
 
140
149
  amqp_bytes_t outbound_buffer;
141
150
 
142
 
  int sockfd;
 
151
  amqp_socket_t *socket;
 
152
 
143
153
  amqp_bytes_t sock_inbound_buffer;
144
154
  size_t sock_inbound_offset;
145
155
  size_t sock_inbound_limit;
148
158
  amqp_link_t *last_queued_frame;
149
159
 
150
160
  amqp_rpc_reply_t most_recent_api_result;
 
161
 
 
162
  uint64_t next_recv_heartbeat;
 
163
  uint64_t next_send_heartbeat;
151
164
};
152
165
 
 
166
amqp_pool_t *amqp_get_or_create_channel_pool(amqp_connection_state_t connection, amqp_channel_t channel);
 
167
amqp_pool_t *amqp_get_channel_pool(amqp_connection_state_t state, amqp_channel_t channel);
 
168
 
 
169
static inline amqp_boolean_t amqp_heartbeat_enabled(amqp_connection_state_t state)
 
170
{
 
171
  return (state->heartbeat > 0);
 
172
}
 
173
 
 
174
static inline uint64_t amqp_calc_next_send_heartbeat(amqp_connection_state_t state, uint64_t cur)
 
175
{
 
176
  return cur + ((uint64_t)state->heartbeat * AMQP_NS_PER_S);
 
177
}
 
178
 
 
179
static inline uint64_t amqp_calc_next_recv_heartbeat(amqp_connection_state_t state, uint64_t cur)
 
180
{
 
181
  return cur + ((uint64_t)state->heartbeat * 2 * AMQP_NS_PER_S);
 
182
}
 
183
 
 
184
int amqp_try_recv(amqp_connection_state_t state, uint64_t current_time);
 
185
 
153
186
static inline void *amqp_offset(void *data, size_t offset)
154
187
{
155
188
  return (char *)data + offset;
158
191
/* This macro defines the encoding and decoding functions associated with a
159
192
   simple type. */
160
193
 
161
 
#define DECLARE_CODEC_BASE_TYPE(bits, htonx, ntohx)                         \
162
 
                                                                            \
163
 
static inline void amqp_e##bits(void *data, size_t offset,                  \
164
 
                                uint##bits##_t val)                         \
165
 
{                                                                           \
166
 
  /* The AMQP data might be unaligned. So we encode and then copy the       \
167
 
     result into place. */                                                  \
168
 
  uint##bits##_t res = htonx(val);                                          \
169
 
  memcpy(amqp_offset(data, offset), &res, bits/8);                          \
170
 
}                                                                           \
171
 
                                                                            \
172
 
static inline uint##bits##_t amqp_d##bits(void *data, size_t offset)        \
173
 
{                                                                           \
174
 
  /* The AMQP data might be unaligned.  So we copy the source value         \
175
 
     into a variable and then decode it. */                                 \
176
 
  uint##bits##_t val;                                                       \
177
 
  memcpy(&val, amqp_offset(data, offset), bits/8);                          \
178
 
  return ntohx(val);                                                        \
179
 
}                                                                           \
180
 
                                                                            \
181
 
static inline int amqp_encode_##bits(amqp_bytes_t encoded, size_t *offset,  \
182
 
                                     uint##bits##_t input)                  \
183
 
                                                                            \
184
 
{                                                                           \
185
 
  size_t o = *offset;                                                       \
186
 
  if ((*offset = o + bits / 8) <= encoded.len) {                            \
187
 
    amqp_e##bits(encoded.bytes, o, input);                                  \
188
 
    return 1;                                                               \
189
 
  }                                                                         \
190
 
  else {                                                                    \
191
 
    return 0;                                                               \
192
 
  }                                                                         \
193
 
}                                                                           \
194
 
                                                                            \
195
 
static inline int amqp_decode_##bits(amqp_bytes_t encoded, size_t *offset,  \
196
 
                                     uint##bits##_t *output)                \
197
 
                                                                            \
198
 
{                                                                           \
199
 
  size_t o = *offset;                                                       \
200
 
  if ((*offset = o + bits / 8) <= encoded.len) {                            \
201
 
    *output = amqp_d##bits(encoded.bytes, o);                               \
202
 
    return 1;                                                               \
203
 
  }                                                                         \
204
 
  else {                                                                    \
205
 
    return 0;                                                               \
206
 
  }                                                                         \
207
 
}
 
194
#define DECLARE_CODEC_BASE_TYPE(bits, htonx, ntohx)                           \
 
195
                                                                              \
 
196
  static inline void amqp_e##bits(void *data, size_t offset,                  \
 
197
                                  uint##bits##_t val)                         \
 
198
  {                                                                           \
 
199
    /* The AMQP data might be unaligned. So we encode and then copy the       \
 
200
             result into place. */                                            \
 
201
    uint##bits##_t res = htonx(val);                                          \
 
202
    memcpy(amqp_offset(data, offset), &res, bits/8);                          \
 
203
  }                                                                           \
 
204
                                                                              \
 
205
  static inline uint##bits##_t amqp_d##bits(void *data, size_t offset)        \
 
206
  {                                                                           \
 
207
    /* The AMQP data might be unaligned.  So we copy the source value         \
 
208
             into a variable and then decode it. */                           \
 
209
    uint##bits##_t val;                                                       \
 
210
    memcpy(&val, amqp_offset(data, offset), bits/8);                          \
 
211
    return ntohx(val);                                                        \
 
212
  }                                                                           \
 
213
                                                                              \
 
214
  static inline int amqp_encode_##bits(amqp_bytes_t encoded, size_t *offset,  \
 
215
                                       uint##bits##_t input)                  \
 
216
                                                                              \
 
217
  {                                                                           \
 
218
    size_t o = *offset;                                                       \
 
219
    if ((*offset = o + bits / 8) <= encoded.len) {                            \
 
220
      amqp_e##bits(encoded.bytes, o, input);                                  \
 
221
      return 1;                                                               \
 
222
    }                                                                         \
 
223
    else {                                                                    \
 
224
      return 0;                                                               \
 
225
    }                                                                         \
 
226
  }                                                                           \
 
227
                                                                              \
 
228
  static inline int amqp_decode_##bits(amqp_bytes_t encoded, size_t *offset,  \
 
229
                                       uint##bits##_t *output)                \
 
230
                                                                              \
 
231
  {                                                                           \
 
232
    size_t o = *offset;                                                       \
 
233
    if ((*offset = o + bits / 8) <= encoded.len) {                            \
 
234
      *output = amqp_d##bits(encoded.bytes, o);                               \
 
235
      return 1;                                                               \
 
236
    }                                                                         \
 
237
    else {                                                                    \
 
238
      return 0;                                                               \
 
239
    }                                                                         \
 
240
  }
208
241
 
209
242
/* Determine byte order */
210
243
#if defined(__GLIBC__)
214
247
# elif (__BYTE_ORDER == __BIG_ENDIAN)
215
248
#  define AMQP_BIG_ENDIAN
216
249
# else
217
 
   /* Don't define anything */
 
250
/* Don't define anything */
218
251
# endif
219
252
#elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) ||                   \
220
253
      defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
234
267
      defined(__i386__) || defined(_M_IX86)
235
268
# define AMQP_LITTLE_ENDIAN
236
269
#else
237
 
  /* Don't define anything */
 
270
/* Don't define anything */
238
271
#endif
239
272
 
240
273
#if defined(AMQP_LITTLE_ENDIAN)
241
274
 
242
 
#define DECLARE_XTOXLL(func)                      \
243
 
static inline uint64_t func##ll(uint64_t val)     \
244
 
{                                                 \
245
 
  union {                                         \
246
 
    uint64_t whole;                               \
247
 
    uint32_t halves[2];                           \
248
 
  } u;                                            \
249
 
  uint32_t t;                                     \
250
 
  u.whole = val;                                  \
251
 
  t = u.halves[0];                                \
252
 
  u.halves[0] = func##l(u.halves[1]);             \
253
 
  u.halves[1] = func##l(t);                       \
254
 
  return u.whole;                                 \
255
 
}
 
275
#define DECLARE_XTOXLL(func)                        \
 
276
  static inline uint64_t func##ll(uint64_t val)     \
 
277
  {                                                 \
 
278
    union {                                         \
 
279
      uint64_t whole;                               \
 
280
      uint32_t halves[2];                           \
 
281
    } u;                                            \
 
282
    uint32_t t;                                     \
 
283
    u.whole = val;                                  \
 
284
    t = u.halves[0];                                \
 
285
    u.halves[0] = func##l(u.halves[1]);             \
 
286
    u.halves[1] = func##l(t);                       \
 
287
    return u.whole;                                 \
 
288
  }
256
289
 
257
290
#elif defined(AMQP_BIG_ENDIAN)
258
291
 
259
 
#define DECLARE_XTOXLL(func)                      \
260
 
static inline uint64_t func##ll(uint64_t val)     \
261
 
{                                                 \
262
 
  union {                                         \
263
 
    uint64_t whole;                               \
264
 
    uint32_t halves[2];                           \
265
 
  } u;                                            \
266
 
  u.whole = val;                                  \
267
 
  u.halves[0] = func##l(u.halves[0]);             \
268
 
  u.halves[1] = func##l(u.halves[1]);             \
269
 
  return u.whole;                                 \
270
 
}
 
292
#define DECLARE_XTOXLL(func)                        \
 
293
  static inline uint64_t func##ll(uint64_t val)     \
 
294
  {                                                 \
 
295
    union {                                         \
 
296
      uint64_t whole;                               \
 
297
      uint32_t halves[2];                           \
 
298
    } u;                                            \
 
299
    u.whole = val;                                  \
 
300
    u.halves[0] = func##l(u.halves[0]);             \
 
301
    u.halves[1] = func##l(u.halves[1]);             \
 
302
    return u.whole;                                 \
 
303
  }
271
304
 
272
305
#else
273
306
# error Endianness not known
284
317
DECLARE_CODEC_BASE_TYPE(64, htonll, ntohll)
285
318
 
286
319
static inline int amqp_encode_bytes(amqp_bytes_t encoded, size_t *offset,
287
 
                                    amqp_bytes_t input)
 
320
                                    amqp_bytes_t input)
288
321
{
289
322
  size_t o = *offset;
290
323
  if ((*offset = o + input.len) <= encoded.len) {
291
324
    memcpy(amqp_offset(encoded.bytes, o), input.bytes, input.len);
292
325
    return 1;
293
 
  }
294
 
  else {
 
326
  } else {
295
327
    return 0;
296
328
  }
297
329
}
298
330
 
299
331
static inline int amqp_decode_bytes(amqp_bytes_t encoded, size_t *offset,
300
 
                                    amqp_bytes_t *output, size_t len)
 
332
                                    amqp_bytes_t *output, size_t len)
301
333
{
302
334
  size_t o = *offset;
303
335
  if ((*offset = o + len) <= encoded.len) {
304
336
    output->bytes = amqp_offset(encoded.bytes, o);
305
337
    output->len = len;
306
338
    return 1;
307
 
  }
308
 
  else {
 
339
  } else {
309
340
    return 0;
310
341
  }
311
342
}