forked from rabbitmq/rabbitmq-c
-
Notifications
You must be signed in to change notification settings - Fork 675
/
amqp_socket.c
1470 lines (1230 loc) · 40.5 KB
/
amqp_socket.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
// SPDX-License-Identifier: mit
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "amqp_private.h"
#include "amqp_socket.h"
#include "amqp_table.h"
#include "amqp_time.h"
#include <assert.h>
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#if ((defined(_WIN32)) || (defined(__MINGW32__)) || (defined(__MINGW64__)))
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/types.h>
/* On older BSD types.h must come before net includes */
#include <netinet/in.h>
#include <netinet/tcp.h>
#ifdef HAVE_SELECT
#include <sys/select.h>
#endif
#include <fcntl.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/uio.h>
#ifdef HAVE_POLL
#include <poll.h>
#endif
#include <unistd.h>
#endif
static int amqp_id_in_reply_list(amqp_method_number_t expected,
amqp_method_number_t *list);
static int amqp_os_socket_init(void) {
#ifdef _WIN32
static int called_wsastartup = 0;
if (!called_wsastartup) {
WSADATA data;
int res = WSAStartup(0x0202, &data);
if (res) {
return AMQP_STATUS_TCP_SOCKETLIB_INIT_ERROR;
}
called_wsastartup = 1;
}
return AMQP_STATUS_OK;
#else
return AMQP_STATUS_OK;
#endif
}
int amqp_os_socket_error(void) {
#ifdef _WIN32
return WSAGetLastError();
#else
return errno;
#endif
}
int amqp_os_socket_close(int sockfd) {
#ifdef _WIN32
return closesocket(sockfd);
#else
return close(sockfd);
#endif
}
ssize_t amqp_socket_send(amqp_socket_t *self, const void *buf, size_t len,
int flags) {
assert(self);
assert(self->klass->send);
return self->klass->send(self, buf, len, flags);
}
ssize_t amqp_socket_recv(amqp_socket_t *self, void *buf, size_t len,
int flags) {
assert(self);
assert(self->klass->recv);
return self->klass->recv(self, buf, len, flags);
}
int amqp_socket_open(amqp_socket_t *self, const char *host, int port) {
assert(self);
assert(self->klass->open);
return self->klass->open(self, host, port, NULL);
}
int amqp_socket_open_noblock(amqp_socket_t *self, const char *host, int port,
const struct timeval *timeout) {
assert(self);
assert(self->klass->open);
return self->klass->open(self, host, port, timeout);
}
int amqp_socket_close(amqp_socket_t *self, amqp_socket_close_enum force) {
assert(self);
assert(self->klass->close);
return self->klass->close(self, force);
}
void amqp_socket_delete(amqp_socket_t *self) {
if (self) {
assert(self->klass->delete);
self->klass->delete (self);
}
}
int amqp_socket_get_sockfd(amqp_socket_t *self) {
assert(self);
assert(self->klass->get_sockfd);
return self->klass->get_sockfd(self);
}
int amqp_poll(int fd, int event, amqp_time_t deadline) {
#ifdef HAVE_POLL
struct pollfd pfd;
int res;
int timeout_ms;
/* Function should only ever be called with one of these two */
assert(event == AMQP_SF_POLLIN || event == AMQP_SF_POLLOUT);
start_poll:
pfd.fd = fd;
switch (event) {
case AMQP_SF_POLLIN:
pfd.events = POLLIN;
break;
case AMQP_SF_POLLOUT:
pfd.events = POLLOUT;
break;
}
timeout_ms = amqp_time_ms_until(deadline);
if (-1 > timeout_ms) {
return timeout_ms;
}
res = poll(&pfd, 1, timeout_ms);
if (0 < res) {
/* TODO: optimize this a bit by returning the AMQP_STATUS_SOCKET_ERROR or
* equivalent when pdf.revent is POLLHUP or POLLERR, so an extra syscall
* doesn't need to be made. */
return AMQP_STATUS_OK;
} else if (0 == res) {
return AMQP_STATUS_TIMEOUT;
} else {
switch (amqp_os_socket_error()) {
case EINTR:
goto start_poll;
default:
return AMQP_STATUS_SOCKET_ERROR;
}
}
return AMQP_STATUS_OK;
#elif defined(HAVE_SELECT)
fd_set fds;
fd_set exceptfds;
fd_set *exceptfdsp;
int res;
struct timeval tv;
struct timeval *tvp;
assert((0 != (event & AMQP_SF_POLLIN)) || (0 != (event & AMQP_SF_POLLOUT)));
#ifndef _WIN32
/* On Win32 connect() failure is indicated through the exceptfds, it does not
* make any sense to allow POLLERR on any other platform or condition */
assert(0 == (event & AMQP_SF_POLLERR));
#endif
start_select:
FD_ZERO(&fds);
FD_SET(fd, &fds);
if (event & AMQP_SF_POLLERR) {
FD_ZERO(&exceptfds);
FD_SET(fd, &exceptfds);
exceptfdsp = &exceptfds;
} else {
exceptfdsp = NULL;
}
res = amqp_time_tv_until(deadline, &tv, &tvp);
if (res != AMQP_STATUS_OK) {
return res;
}
if (event & AMQP_SF_POLLIN) {
res = select(fd + 1, &fds, NULL, exceptfdsp, tvp);
} else if (event & AMQP_SF_POLLOUT) {
res = select(fd + 1, NULL, &fds, exceptfdsp, tvp);
}
if (0 < res) {
return AMQP_STATUS_OK;
} else if (0 == res) {
return AMQP_STATUS_TIMEOUT;
} else {
switch (amqp_os_socket_error()) {
case EINTR:
goto start_select;
default:
return AMQP_STATUS_SOCKET_ERROR;
}
}
#else
#error "poll() or select() is needed to compile rabbitmq-c"
#endif
}
static ssize_t do_poll(amqp_connection_state_t state, ssize_t res,
amqp_time_t deadline) {
int fd = amqp_get_sockfd(state);
if (-1 == fd) {
return AMQP_STATUS_SOCKET_CLOSED;
}
switch (res) {
case AMQP_PRIVATE_STATUS_SOCKET_NEEDREAD:
res = amqp_poll(fd, AMQP_SF_POLLIN, deadline);
break;
case AMQP_PRIVATE_STATUS_SOCKET_NEEDWRITE:
res = amqp_poll(fd, AMQP_SF_POLLOUT, deadline);
break;
}
return res;
}
ssize_t amqp_try_send(amqp_connection_state_t state, const void *buf,
size_t len, amqp_time_t deadline, int flags) {
ssize_t res;
void *buf_left = (void *)buf;
/* Assume that len is not going to be larger than ssize_t can hold. */
ssize_t len_left = (size_t)len;
start_send:
res = amqp_socket_send(state->socket, buf_left, len_left, flags);
if (res > 0) {
len_left -= res;
buf_left = (char *)buf_left + res;
if (0 == len_left) {
return (ssize_t)len;
}
goto start_send;
}
res = do_poll(state, res, deadline);
if (AMQP_STATUS_OK == res) {
goto start_send;
}
if (AMQP_STATUS_TIMEOUT == res) {
return (ssize_t)len - len_left;
}
return res;
}
int amqp_open_socket(char const *hostname, int portnumber) {
return amqp_open_socket_inner(hostname, portnumber, amqp_time_infinite());
}
int amqp_open_socket_noblock(char const *hostname, int portnumber,
const struct timeval *timeout) {
amqp_time_t deadline;
int res = amqp_time_from_now(&deadline, timeout);
if (AMQP_STATUS_OK != res) {
return res;
}
return amqp_open_socket_inner(hostname, portnumber, deadline);
}
#ifdef _WIN32
static int connect_socket(struct addrinfo *addr, amqp_time_t deadline) {
int one = 1;
SOCKET sockfd;
int last_error;
/*
* This cast is to squash warnings on Win64, see:
* http://stackoverflow.com/questions/1953639/is-it-safe-to-cast-socket-to-int-under-win64
*/
sockfd = (int)socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (INVALID_SOCKET == sockfd) {
return AMQP_STATUS_SOCKET_ERROR;
}
/* Set the socket to be non-blocking */
if (SOCKET_ERROR == ioctlsocket(sockfd, FIONBIO, &one)) {
last_error = AMQP_STATUS_SOCKET_ERROR;
goto err;
}
/* Disable nagle */
if (SOCKET_ERROR == setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
(const char *)&one, sizeof(one))) {
last_error = AMQP_STATUS_SOCKET_ERROR;
goto err;
}
/* Enable TCP keepalives */
if (SOCKET_ERROR == setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE,
(const char *)&one, sizeof(one))) {
last_error = AMQP_STATUS_SOCKET_ERROR;
goto err;
}
if (SOCKET_ERROR != connect(sockfd, addr->ai_addr, (int)addr->ai_addrlen)) {
return (int)sockfd;
}
if (WSAEWOULDBLOCK != WSAGetLastError()) {
last_error = AMQP_STATUS_SOCKET_ERROR;
goto err;
}
last_error =
amqp_poll((int)sockfd, AMQP_SF_POLLOUT | AMQP_SF_POLLERR, deadline);
if (AMQP_STATUS_OK != last_error) {
goto err;
}
{
int result;
int result_len = sizeof(result);
if (SOCKET_ERROR == getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
(char *)&result, &result_len) ||
result != 0) {
last_error = AMQP_STATUS_SOCKET_ERROR;
goto err;
}
}
return (int)sockfd;
err:
closesocket(sockfd);
return last_error;
}
#else
static int connect_socket(struct addrinfo *addr, amqp_time_t deadline) {
int one = 1;
int sockfd;
int flags;
int last_error;
sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (-1 == sockfd) {
return AMQP_STATUS_SOCKET_ERROR;
}
/* Enable CLOEXEC on socket */
flags = fcntl(sockfd, F_GETFD);
if (flags == -1 || fcntl(sockfd, F_SETFD, (long)(flags | FD_CLOEXEC)) == -1) {
last_error = AMQP_STATUS_SOCKET_ERROR;
goto err;
}
/* Set the socket as non-blocking */
flags = fcntl(sockfd, F_GETFL);
if (flags == -1 || fcntl(sockfd, F_SETFL, (long)(flags | O_NONBLOCK)) == -1) {
last_error = AMQP_STATUS_SOCKET_ERROR;
goto err;
}
#ifdef SO_NOSIGPIPE
/* Turn off SIGPIPE on platforms that support it, BSD, MacOSX */
if (0 != setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one))) {
last_error = AMQP_STATUS_SOCKET_ERROR;
goto err;
}
#endif /* SO_NOSIGPIPE */
/* Disable nagle */
if (0 != setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one))) {
last_error = AMQP_STATUS_SOCKET_ERROR;
goto err;
}
/* Enable TCP keepalives */
if (0 != setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one))) {
last_error = AMQP_STATUS_SOCKET_ERROR;
goto err;
}
if (0 == connect(sockfd, addr->ai_addr, addr->ai_addrlen)) {
return sockfd;
}
if (EINPROGRESS != errno) {
last_error = AMQP_STATUS_SOCKET_ERROR;
goto err;
}
last_error = amqp_poll(sockfd, AMQP_SF_POLLOUT, deadline);
if (AMQP_STATUS_OK != last_error) {
goto err;
}
{
int result;
socklen_t result_len = sizeof(result);
if (-1 == getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &result, &result_len) ||
result != 0) {
last_error = AMQP_STATUS_SOCKET_ERROR;
goto err;
}
}
return sockfd;
err:
close(sockfd);
return last_error;
}
#endif
int amqp_open_socket_inner(char const *hostname, int portnumber,
amqp_time_t deadline) {
struct addrinfo hint;
struct addrinfo *address_list;
struct addrinfo *addr;
char portnumber_string[33];
int sockfd = -1;
int last_error;
last_error = amqp_os_socket_init();
if (AMQP_STATUS_OK != last_error) {
return last_error;
}
memset(&hint, 0, sizeof(hint));
hint.ai_family = PF_UNSPEC; /* PF_INET or PF_INET6 */
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = IPPROTO_TCP;
(void)sprintf(portnumber_string, "%d", portnumber);
last_error = getaddrinfo(hostname, portnumber_string, &hint, &address_list);
if (0 != last_error) {
return AMQP_STATUS_HOSTNAME_RESOLUTION_FAILED;
}
for (addr = address_list; addr; addr = addr->ai_next) {
sockfd = connect_socket(addr, deadline);
if (sockfd >= 0) {
last_error = AMQP_STATUS_OK;
break;
} else if (sockfd == AMQP_STATUS_TIMEOUT) {
last_error = sockfd;
break;
}
}
freeaddrinfo(address_list);
if (last_error != AMQP_STATUS_OK || sockfd == -1) {
return last_error;
}
return sockfd;
}
static int send_header_inner(amqp_connection_state_t state,
amqp_time_t deadline) {
ssize_t res;
static const uint8_t header[8] = {'A',
'M',
'Q',
'P',
0,
AMQP_PROTOCOL_VERSION_MAJOR,
AMQP_PROTOCOL_VERSION_MINOR,
AMQP_PROTOCOL_VERSION_REVISION};
res = amqp_try_send(state, header, sizeof(header), deadline, AMQP_SF_NONE);
if (sizeof(header) == res) {
return AMQP_STATUS_OK;
}
return (int)res;
}
int amqp_send_header(amqp_connection_state_t state) {
return send_header_inner(state, amqp_time_infinite());
}
static amqp_bytes_t sasl_method_name(amqp_sasl_method_enum method) {
amqp_bytes_t res;
switch (method) {
case AMQP_SASL_METHOD_PLAIN:
res = amqp_literal_bytes("PLAIN");
break;
case AMQP_SASL_METHOD_EXTERNAL:
res = amqp_literal_bytes("EXTERNAL");
break;
default:
amqp_abort("Invalid SASL method: %d", (int)method);
}
return res;
}
static int bytes_equal(amqp_bytes_t l, amqp_bytes_t r) {
if (l.len == r.len) {
if (l.bytes && r.bytes) {
if (0 == memcmp(l.bytes, r.bytes, l.len)) {
return 1;
}
}
}
return 0;
}
int sasl_mechanism_in_list(amqp_bytes_t mechanisms,
amqp_sasl_method_enum method) {
amqp_bytes_t mechanism;
amqp_bytes_t supported_mechanism;
uint8_t *start;
uint8_t *end;
uint8_t *current;
assert(NULL != mechanisms.bytes);
mechanism = sasl_method_name(method);
start = (uint8_t *)mechanisms.bytes;
current = start;
end = start + mechanisms.len;
for (; current != end; start = current + 1) {
/* HACK: SASL states that we should be parsing this string as a UTF-8
* string, which we're plainly not doing here. At this point its not worth
* dragging an entire UTF-8 parser for this one case, and this should work
* most of the time */
current = memchr(start, ' ', end - start);
if (NULL == current) {
current = end;
}
supported_mechanism.bytes = start;
supported_mechanism.len = current - start;
if (bytes_equal(mechanism, supported_mechanism)) {
return 1;
}
}
return 0;
}
static amqp_bytes_t sasl_response(amqp_pool_t *pool,
amqp_sasl_method_enum method, va_list args) {
amqp_bytes_t response;
switch (method) {
case AMQP_SASL_METHOD_PLAIN: {
char *username = va_arg(args, char *);
size_t username_len = strlen(username);
char *password = va_arg(args, char *);
size_t password_len = strlen(password);
char *response_buf;
amqp_pool_alloc_bytes(pool, strlen(username) + strlen(password) + 2,
&response);
if (response.bytes == NULL)
/* We never request a zero-length block, because of the +2
above, so a NULL here really is ENOMEM. */
{
return response;
}
response_buf = response.bytes;
response_buf[0] = 0;
memcpy(response_buf + 1, username, username_len);
response_buf[username_len + 1] = 0;
memcpy(response_buf + username_len + 2, password, password_len);
break;
}
case AMQP_SASL_METHOD_EXTERNAL: {
char *identity = va_arg(args, char *);
size_t identity_len = strlen(identity);
amqp_pool_alloc_bytes(pool, identity_len, &response);
if (response.bytes == NULL) {
return response;
}
memcpy(response.bytes, identity, identity_len);
break;
}
default:
amqp_abort("Invalid SASL method: %d", (int)method);
}
return response;
}
amqp_boolean_t amqp_frames_enqueued(amqp_connection_state_t state) {
return (state->first_queued_frame != NULL);
}
/*
* Check to see if we have data in our buffer. If this returns 1, we
* will avoid an immediate blocking read in amqp_simple_wait_frame.
*/
amqp_boolean_t amqp_data_in_buffer(amqp_connection_state_t state) {
return (state->sock_inbound_offset < state->sock_inbound_limit);
}
static int consume_one_frame(amqp_connection_state_t state,
amqp_frame_t *decoded_frame) {
int res;
amqp_bytes_t buffer;
buffer.len = state->sock_inbound_limit - state->sock_inbound_offset;
buffer.bytes =
((char *)state->sock_inbound_buffer.bytes) + state->sock_inbound_offset;
res = amqp_handle_input(state, buffer, decoded_frame);
if (res < 0) {
return res;
}
state->sock_inbound_offset += res;
return AMQP_STATUS_OK;
}
static int recv_with_timeout(amqp_connection_state_t state,
amqp_time_t timeout) {
ssize_t res;
int fd;
start_recv:
res = amqp_socket_recv(state->socket, state->sock_inbound_buffer.bytes,
state->sock_inbound_buffer.len, 0);
if (res < 0) {
fd = amqp_get_sockfd(state);
if (-1 == fd) {
return AMQP_STATUS_CONNECTION_CLOSED;
}
switch (res) {
default:
return (int)res;
case AMQP_PRIVATE_STATUS_SOCKET_NEEDREAD:
res = amqp_poll(fd, AMQP_SF_POLLIN, timeout);
break;
case AMQP_PRIVATE_STATUS_SOCKET_NEEDWRITE:
res = amqp_poll(fd, AMQP_SF_POLLOUT, timeout);
break;
}
if (AMQP_STATUS_OK == res) {
goto start_recv;
}
return (int)res;
}
state->sock_inbound_limit = res;
state->sock_inbound_offset = 0;
res = amqp_time_s_from_now(&state->next_recv_heartbeat,
amqp_heartbeat_recv(state));
if (AMQP_STATUS_OK != res) {
return (int)res;
}
return AMQP_STATUS_OK;
}
int amqp_try_recv(amqp_connection_state_t state) {
amqp_time_t timeout;
int res;
while (amqp_data_in_buffer(state)) {
amqp_frame_t frame;
res = consume_one_frame(state, &frame);
if (AMQP_STATUS_OK != res) {
return res;
}
if (frame.frame_type != 0) {
amqp_pool_t *channel_pool;
amqp_frame_t *frame_copy;
amqp_link_t *link;
channel_pool = amqp_get_or_create_channel_pool(state, frame.channel);
if (NULL == channel_pool) {
return AMQP_STATUS_NO_MEMORY;
}
frame_copy = amqp_pool_alloc(channel_pool, sizeof(amqp_frame_t));
link = amqp_pool_alloc(channel_pool, sizeof(amqp_link_t));
if (frame_copy == NULL || link == NULL) {
return AMQP_STATUS_NO_MEMORY;
}
*frame_copy = frame;
link->next = NULL;
link->data = frame_copy;
if (state->last_queued_frame == NULL) {
state->first_queued_frame = link;
} else {
state->last_queued_frame->next = link;
}
state->last_queued_frame = link;
}
}
res = amqp_time_from_now(&timeout, &(struct timeval){0});
if (AMQP_STATUS_OK != res) {
return res;
}
return recv_with_timeout(state, timeout);
}
static int wait_frame_inner(amqp_connection_state_t state,
amqp_frame_t *decoded_frame,
amqp_time_t timeout_deadline) {
amqp_time_t deadline;
int res;
for (;;) {
while (amqp_data_in_buffer(state)) {
res = consume_one_frame(state, decoded_frame);
if (AMQP_STATUS_OK != res) {
return res;
}
if (AMQP_FRAME_HEARTBEAT == decoded_frame->frame_type) {
amqp_maybe_release_buffers_on_channel(state, 0);
continue;
}
if (decoded_frame->frame_type != 0) {
/* Complete frame was read. Return it. */
return AMQP_STATUS_OK;
}
}
beginrecv:
res = amqp_time_has_past(state->next_send_heartbeat);
if (AMQP_STATUS_TIMER_FAILURE == res) {
return res;
} else if (AMQP_STATUS_TIMEOUT == res) {
amqp_frame_t heartbeat;
heartbeat.channel = 0;
heartbeat.frame_type = AMQP_FRAME_HEARTBEAT;
res = amqp_send_frame(state, &heartbeat);
if (AMQP_STATUS_OK != res) {
return res;
}
}
deadline = amqp_time_first(timeout_deadline,
amqp_time_first(state->next_recv_heartbeat,
state->next_send_heartbeat));
/* TODO this needs to wait for a _frame_ and not anything written from the
* socket */
res = recv_with_timeout(state, deadline);
if (AMQP_STATUS_TIMEOUT == res) {
if (amqp_time_equal(deadline, state->next_recv_heartbeat)) {
amqp_socket_close(state->socket, AMQP_SC_FORCE);
return AMQP_STATUS_HEARTBEAT_TIMEOUT;
} else if (amqp_time_equal(deadline, timeout_deadline)) {
return AMQP_STATUS_TIMEOUT;
} else if (amqp_time_equal(deadline, state->next_send_heartbeat)) {
/* send heartbeat happens before we do recv_with_timeout */
goto beginrecv;
} else {
amqp_abort("Internal error: unable to determine timeout reason");
}
} else if (AMQP_STATUS_OK != res) {
return res;
}
}
}
static amqp_link_t *amqp_create_link_for_frame(amqp_connection_state_t state,
amqp_frame_t *frame) {
amqp_link_t *link;
amqp_frame_t *frame_copy;
amqp_pool_t *channel_pool =
amqp_get_or_create_channel_pool(state, frame->channel);
if (NULL == channel_pool) {
return NULL;
}
link = amqp_pool_alloc(channel_pool, sizeof(amqp_link_t));
frame_copy = amqp_pool_alloc(channel_pool, sizeof(amqp_frame_t));
if (NULL == link || NULL == frame_copy) {
return NULL;
}
*frame_copy = *frame;
link->data = frame_copy;
return link;
}
int amqp_queue_frame(amqp_connection_state_t state, amqp_frame_t *frame) {
amqp_link_t *link = amqp_create_link_for_frame(state, frame);
if (NULL == link) {
return AMQP_STATUS_NO_MEMORY;
}
if (NULL == state->first_queued_frame) {
state->first_queued_frame = link;
} else {
state->last_queued_frame->next = link;
}
link->next = NULL;
state->last_queued_frame = link;
return AMQP_STATUS_OK;
}
int amqp_put_back_frame(amqp_connection_state_t state, amqp_frame_t *frame) {
amqp_link_t *link = amqp_create_link_for_frame(state, frame);
if (NULL == link) {
return AMQP_STATUS_NO_MEMORY;
}
if (NULL == state->first_queued_frame) {
state->first_queued_frame = link;
state->last_queued_frame = link;
link->next = NULL;
} else {
link->next = state->first_queued_frame;
state->first_queued_frame = link;
}
return AMQP_STATUS_OK;
}
int amqp_simple_wait_frame_on_channel(amqp_connection_state_t state,
amqp_channel_t channel,
amqp_frame_t *decoded_frame) {
amqp_frame_t *frame_ptr;
amqp_link_t *cur;
int res;
for (cur = state->first_queued_frame; NULL != cur; cur = cur->next) {
frame_ptr = cur->data;
if (channel == frame_ptr->channel) {
state->first_queued_frame = cur->next;
if (NULL == state->first_queued_frame) {
state->last_queued_frame = NULL;
}
*decoded_frame = *frame_ptr;
return AMQP_STATUS_OK;
}
}
for (;;) {
res = wait_frame_inner(state, decoded_frame, amqp_time_infinite());
if (AMQP_STATUS_OK != res) {
return res;
}
if (channel == decoded_frame->channel) {
return AMQP_STATUS_OK;
} else {
res = amqp_queue_frame(state, decoded_frame);
if (res != AMQP_STATUS_OK) {
return res;
}
}
}
}
int amqp_simple_wait_frame(amqp_connection_state_t state,
amqp_frame_t *decoded_frame) {
return amqp_simple_wait_frame_noblock(state, decoded_frame, NULL);
}
int amqp_simple_wait_frame_noblock(amqp_connection_state_t state,
amqp_frame_t *decoded_frame,
const struct timeval *timeout) {
amqp_time_t deadline;
int res = amqp_time_from_now(&deadline, timeout);
if (AMQP_STATUS_OK != res) {
return res;
}
if (state->first_queued_frame != NULL) {
amqp_frame_t *f = (amqp_frame_t *)state->first_queued_frame->data;
state->first_queued_frame = state->first_queued_frame->next;
if (state->first_queued_frame == NULL) {
state->last_queued_frame = NULL;
}
*decoded_frame = *f;
return AMQP_STATUS_OK;
} else {
return wait_frame_inner(state, decoded_frame, deadline);
}
}
static int amqp_simple_wait_method_list(amqp_connection_state_t state,
amqp_channel_t expected_channel,
amqp_method_number_t *expected_methods,
amqp_time_t deadline,
amqp_method_t *output) {
amqp_frame_t frame;
struct timeval tv;
struct timeval *tvp;
int res = amqp_time_tv_until(deadline, &tv, &tvp);
if (res != AMQP_STATUS_OK) {
return res;
}
res = amqp_simple_wait_frame_noblock(state, &frame, tvp);
if (AMQP_STATUS_OK != res) {
return res;
}
if (AMQP_FRAME_METHOD != frame.frame_type ||
expected_channel != frame.channel ||
!amqp_id_in_reply_list(frame.payload.method.id, expected_methods)) {
return AMQP_STATUS_WRONG_METHOD;
}
*output = frame.payload.method;
return AMQP_STATUS_OK;
}
static int simple_wait_method_inner(amqp_connection_state_t state,
amqp_channel_t expected_channel,
amqp_method_number_t expected_method,
amqp_time_t deadline,
amqp_method_t *output) {
amqp_method_number_t expected_methods[2];
expected_methods[0] = expected_method;
expected_methods[1] = 0;
return amqp_simple_wait_method_list(state, expected_channel, expected_methods,
deadline, output);
}
int amqp_simple_wait_method(amqp_connection_state_t state,
amqp_channel_t expected_channel,
amqp_method_number_t expected_method,
amqp_method_t *output) {
return simple_wait_method_inner(state, expected_channel, expected_method,
amqp_time_infinite(), output);
}
int amqp_send_method(amqp_connection_state_t state, amqp_channel_t channel,
amqp_method_number_t id, void *decoded) {
return amqp_send_method_inner(state, channel, id, decoded, AMQP_SF_NONE,
amqp_time_infinite());
}
int amqp_send_method_inner(amqp_connection_state_t state,
amqp_channel_t channel, amqp_method_number_t id,
void *decoded, int flags, amqp_time_t deadline) {
amqp_frame_t frame;
frame.frame_type = AMQP_FRAME_METHOD;
frame.channel = channel;
frame.payload.method.id = id;
frame.payload.method.decoded = decoded;
return amqp_send_frame_inner(state, &frame, flags, deadline);
}
static int amqp_id_in_reply_list(amqp_method_number_t expected,