Coverage Report

Created: 2025-07-11 07:00

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