amqp_socket.c

filter: Statementstmt 50.6% 311 / 614 Branchbran 33.6% 103 / 306 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 45.0% 414 / 920 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« amqp_time.c ↑ Summary amqp_connection.c »
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-2014
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 # define _CRT_SECURE_NO_WARNINGS
43 #endif
44  
45 #include "amqp_private.h"
46 #include "amqp_socket.h"
47 #include "amqp_table.h"
48 #include "amqp_time.h"
49  
50 #include <assert.h>
51 #include <limits.h>
52 #include <stdarg.h>
53 #include <stdint.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57  
58 #include <errno.h>
59  
60 #ifdef _WIN32
61 # ifndef WIN32_LEAN_AND_MEAN
62 # define WIN32_LEAN_AND_MEAN
63 # endif
64 # include <Winsock2.h>
65 # include <ws2tcpip.h>
66 #else
67 # include <sys/types.h> /* On older BSD this must come before net includes */
68 # include <netinet/in.h>
69 # include <netinet/tcp.h>
70 # ifdef HAVE_SELECT
71 # include <sys/select.h>
72 # endif
73 # include <sys/socket.h>
74 # include <netdb.h>
75 # include <sys/uio.h>
76 # include <fcntl.h>
77 # ifdef HAVE_POLL
78 # include <poll.h>
79 # endif
80 # include <unistd.h>
81 #endif
82  
83 static int amqp_id_in_reply_list( amqp_method_number_t expected, amqp_method_number_t *list );
84  
85 static int
86 30 amqp_os_socket_init(void)
87 {
88 #ifdef _WIN32
89 static called_wsastartup = 0;
90 if (!called_wsastartup) {
91 WSADATA data;
92 int res = WSAStartup(0x0202, &data);
93 if (res) {
94 return AMQP_STATUS_TCP_SOCKETLIB_INIT_ERROR;
95 }
96  
97 called_wsastartup = 1;
98 }
99 return AMQP_STATUS_OK;
100  
101 #else
102 30 return AMQP_STATUS_OK;
103 #endif
104 }
105  
106 static int
107 30 amqp_os_socket_socket(int domain, int type, int protocol)
108 {
109 #ifdef _WIN32
110 /*
111 This cast is to squash warnings on Win64, see:
112 http://stackoverflow.com/questions/1953639/is-it-safe-to-cast-socket-to-int-under-win64
113 */
114 return (int)socket(domain, type, protocol);
115 #else
116 int flags;
117  
118 30 int s = socket(domain, type, protocol);
119 30 ▶ if (s < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (s < 0) { 0 30 30
120 0 return s;
121 }
122  
123 /* Always enable CLOEXEC on the socket */
124 30 flags = fcntl(s, F_GETFD);
125 30 ▶ if (flags == -1
Branch: 1 branch
BranchTrueFalseTotal
if (flags == -1 30 0 30
126 30 ▶ || fcntl(s, F_SETFD, (long)(flags | FD_CLOEXEC)) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
|| fcntl(s, F_SETFD, (long)(flags | FD_CLOEXEC)) == -1) { 0 30 30
127 0 int e = errno;
128 0 close(s);
129 0 errno = e;
130 0 return -1;
131 }
132  
133 30 return s;
134  
135 #endif
136 }
137  
138 static int
139 30 amqp_os_socket_setsockopt(int sock, int level, int optname,
140 const void *optval, size_t optlen)
141 {
142 #ifdef _WIN32
143 /* the winsock setsockopt function has its 4th argument as a
144 const char * */
145 return setsockopt(sock, level, optname, (const char *)optval, (int)optlen);
146 #else
147 30 return setsockopt(sock, level, optname, optval, optlen);
148 #endif
149 }
150  
151 static int
152 30 amqp_os_socket_setsockblock(int sock, int block)
153 {
154  
155 #ifdef _WIN32
156 int nonblock = !block;
157 if (NO_ERROR != ioctlsocket(sock, FIONBIO, &nonblock)) {
158 return AMQP_STATUS_SOCKET_ERROR;
159 } else {
160 return AMQP_STATUS_OK;
161 }
162 #else
163 long arg;
164  
165 30 ▶ if ((arg = fcntl(sock, F_GETFL, NULL)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((arg = fcntl(sock, F_GETFL, NULL)) < 0) { 0 30 30
166 0 return AMQP_STATUS_SOCKET_ERROR;
167 }
168  
169 30 ▶ if (block) {
Branch: 1 branch
BranchTrueFalseTotal
if (block) { 0 30 30
170 0 arg &= (~O_NONBLOCK);
171 } else {
172 30 arg |= O_NONBLOCK;
173 }
174  
175 30 ▶ if (fcntl(sock, F_SETFL, arg) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fcntl(sock, F_SETFL, arg) < 0) { 0 30 30
176 0 return AMQP_STATUS_SOCKET_ERROR;
177 }
178  
179 30 return AMQP_STATUS_OK;
180 #endif
181 }
182  
183  
184 int
185 86 amqp_os_socket_error(void)
186 {
187 #ifdef _WIN32
188 return WSAGetLastError();
189 #else
190 86 return errno;
191 #endif
192 }
193  
194 int
195 30 amqp_os_socket_close(int sockfd)
196 {
197 #ifdef _WIN32
198 return closesocket(sockfd);
199 #else
200 30 return close(sockfd);
201 #endif
202 }
203  
204 ssize_t
205 86 amqp_socket_send(amqp_socket_t *self, const void *buf, size_t len, int flags)
206 {
207 86 ▶ assert(self);
Branch: 1 branch
BranchTrueFalseTotal
assert(self); 0 86 86
208 86 ▶ assert(self->klass->send);
Branch: 1 branch
BranchTrueFalseTotal
assert(self->klass->send); 0 86 86
209 86 return self->klass->send(self, buf, len, flags);
210 }
211  
212 ssize_t
213 112 amqp_socket_recv(amqp_socket_t *self, void *buf, size_t len, int flags)
214 {
215 112 ▶ assert(self);
Branch: 1 branch
BranchTrueFalseTotal
assert(self); 0 112 112
216 112 ▶ assert(self->klass->recv);
Branch: 1 branch
BranchTrueFalseTotal
assert(self->klass->recv); 0 112 112
217 112 return self->klass->recv(self, buf, len, flags);
218 }
219  
220 int
221 0 amqp_socket_open(amqp_socket_t *self, const char *host, int port)
222 {
223 0 ▶ assert(self);
Branch: 1 branch
BranchTrueFalseTotal
assert(self); 0 0 0
224 0 ▶ assert(self->klass->open);
Branch: 1 branch
BranchTrueFalseTotal
assert(self->klass->open); 0 0 0
225 0 return self->klass->open(self, host, port, NULL);
226 }
227  
228 int
229 30 amqp_socket_open_noblock(amqp_socket_t *self, const char *host, int port, struct timeval *timeout)
230 {
231 30 ▶ assert(self);
Branch: 1 branch
BranchTrueFalseTotal
assert(self); 0 30 30
232 30 ▶ assert(self->klass->open);
Branch: 1 branch
BranchTrueFalseTotal
assert(self->klass->open); 0 30 30
233 30 return self->klass->open(self, host, port, timeout);
234 }
235  
236 int
237 0 amqp_socket_close(amqp_socket_t *self)
238 {
239 0 ▶ assert(self);
Branch: 1 branch
BranchTrueFalseTotal
assert(self); 0 0 0
240 0 ▶ assert(self->klass->close);
Branch: 1 branch
BranchTrueFalseTotal
assert(self->klass->close); 0 0 0
241 0 return self->klass->close(self);
242 }
243  
244 void
245 85 amqp_socket_delete(amqp_socket_t *self)
246 {
247 85 ▶ if (self) {
Branch: 1 branch
BranchTrueFalseTotal
if (self) { 30 55 85
248 30 ▶ assert(self->klass->delete);
Branch: 1 branch
BranchTrueFalseTotal
assert(self->klass->delete); 0 30 30
249 30 self->klass->delete(self);
250 }
251 85 }
252  
253 int
254 67 amqp_socket_get_sockfd(amqp_socket_t *self)
255 {
256 67 ▶ assert(self);
Branch: 1 branch
BranchTrueFalseTotal
assert(self); 0 67 67
257 67 ▶ assert(self->klass->get_sockfd);
Branch: 1 branch
BranchTrueFalseTotal
assert(self->klass->get_sockfd); 0 67 67
258 67 return self->klass->get_sockfd(self);
259 }
260  
261 87 int amqp_poll(int fd, int event, amqp_time_t deadline) {
262 #ifdef HAVE_POLL
263 struct pollfd pfd;
264 int res;
265 int timeout_ms;
266  
267 /* Function should only ever be called with one of these two */
268 87 ▶ assert(event == AMQP_SF_POLLIN || event == AMQP_SF_POLLOUT);
Branch: 2 branches
BranchTrueFalseTotal
assert(event == AMQP_SF_POLLIN || event == AMQP_SF_POLLOUT); 57 30 87
assert(event == AMQP_SF_POLLIN || event == AMQP_SF_POLLOUT); 30 0 30
269  
270 87 start_poll:
271 87 pfd.fd = fd;
272 87 switch (event) {
273 57 case AMQP_SF_POLLIN:
274 57 pfd.events = POLLIN;
275 57 break;
276 30 case AMQP_SF_POLLOUT:
277 30 pfd.events = POLLOUT;
278 30 break;
279 }
280  
281 87 timeout_ms = amqp_time_ms_until(deadline);
282 87 ▶ if (-1 > timeout_ms) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 > timeout_ms) { 0 87 87
283 0 return timeout_ms;
284 }
285  
286 87 res = poll(&pfd, 1, timeout_ms);
287  
288 87 ▶ if (0 < res) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < res) { 86 1 87
289 /* TODO: optimize this a bit by returning the AMQP_STATUS_SOCKET_ERROR or
290 * equivalent when pdf.revent is POLLHUP or POLLERR, so an extra syscall
291 * doesn't need to be made. */
292 86 return AMQP_STATUS_OK;
293 1 ▶ } else if (0 == res) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (0 == res) { 1 0 1
294 1 return AMQP_STATUS_TIMEOUT;
295 } else {
296 0 ▶ switch (amqp_os_socket_error()) {
Branch: 1 branch
BranchTrueFalseTotal
switch (amqp_os_socket_error()) { 0 0 0
297 0 case EINTR:
298 0 goto start_poll;
299 0 default:
300 0 return AMQP_STATUS_SOCKET_ERROR;
301 }
302 }
303 return AMQP_STATUS_OK;
304 #elif defined(HAVE_SELECT)
305 fd_set fds;
306 int res;
307 struct timeval tv;
308 struct timeval *tvp;
309  
310 assert(event == AMQP_SF_POLLIN || event == AMQP_SF_POLLOUT);
311  
312 start_select:
313 FD_ZERO(&fds);
314 FD_SET(fd, &fds);
315  
316 res = amqp_time_tv_until(deadline, &tv, &tvp);
317 if (res != AMQP_STATUS_OK) {
318 return res;
319 }
320  
321 switch (event) {
322 case AMQP_SF_POLLIN:
323 res = select(fd + 1, &fds, NULL, NULL, tvp);
324 break;
325 case AMQP_SF_POLLOUT:
326 res = select(fd + 1, NULL, &fds, NULL, tvp);
327 }
328  
329 if (0 < res) {
330 return AMQP_STATUS_OK;
331 } else if (0 == res) {
332 return AMQP_STATUS_TIMEOUT;
333 } else {
334 switch (amqp_os_socket_error()) {
335 case EINTR:
336 goto start_select;
337 default:
338 return AMQP_STATUS_SOCKET_ERROR;
339 }
340 }
341 return AMQP_STATUS_OK;
342 #else
343 # error "poll() or select() is needed to compile rabbitmq-c"
344 #endif
345 }
346  
347 2 static ssize_t do_poll(amqp_connection_state_t state, ssize_t res,
348 amqp_time_t deadline) {
349 2 int fd = amqp_get_sockfd(state);
350 2 ▶ if (-1 == fd) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == fd) { 2 0 2
351 2 return AMQP_STATUS_SOCKET_CLOSED;
352 }
353 0 switch (res) {
354 0 case AMQP_PRIVATE_STATUS_SOCKET_NEEDREAD:
355 0 res = amqp_poll(fd, AMQP_SF_POLLIN, deadline);
356 0 break;
357 0 case AMQP_PRIVATE_STATUS_SOCKET_NEEDWRITE:
358 0 res = amqp_poll(fd, AMQP_SF_POLLOUT, deadline);
359 0 break;
360 }
361 0 return res;
362 }
363  
364 86 ssize_t amqp_try_send(amqp_connection_state_t state, const void *buf,
365 size_t len, amqp_time_t deadline, int flags) {
366 ssize_t res;
367 86 void* buf_left = (void*)buf;
368 /* Assume that len is not going to be larger than ssize_t can hold. */
369 86 ssize_t len_left = (size_t)len;
370  
371 0 start_send:
372 86 res = amqp_socket_send(state->socket, buf_left, len_left, flags);
373  
374 86 ▶ if (res > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (res > 0) { 84 2 86
375 84 len_left -= res;
376 84 buf_left = (char*)buf_left + res;
377 84 ▶ if (0 == len_left) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == len_left) { 84 0 84
378 84 return (ssize_t)len;
379 }
380 0 goto start_send;
381 }
382 2 res = do_poll(state, res, deadline);
383 2 ▶ if (AMQP_STATUS_OK == res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK == res) { 0 2 2
384 0 goto start_send;
385 }
386 2 ▶ if (AMQP_STATUS_TIMEOUT == res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_TIMEOUT == res) { 0 2 2
387 0 return (ssize_t)len - len_left;
388 }
389 2 return res;
390 }
391  
392 int
393 0 amqp_open_socket(char const *hostname,
394 int portnumber)
395 {
396 0 return amqp_open_socket_inner(hostname, portnumber,
397 amqp_time_infinite());
398 }
399  
400 29 int amqp_open_socket_noblock(char const *hostname,
401 int portnumber,
402 struct timeval *timeout)
403 {
404 amqp_time_t deadline;
405 29 int res = amqp_time_from_now(&deadline, timeout);
406 29 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 29 29
407 0 return res;
408 }
409 29 return amqp_open_socket_inner(hostname, portnumber, deadline);
410 }
411  
412 30 int amqp_open_socket_inner(char const *hostname,
413 int portnumber,
414 amqp_time_t deadline) {
415 struct addrinfo hint;
416 struct addrinfo *address_list;
417 struct addrinfo *addr;
418 char portnumber_string[33];
419 30 int sockfd = -1;
420 30 int last_error = AMQP_STATUS_OK;
421 30 int one = 1; /* for setsockopt */
422 int res;
423  
424 30 last_error = amqp_os_socket_init();
425 30 ▶ if (AMQP_STATUS_OK != last_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != last_error) { 0 30 30
426 0 return last_error;
427 }
428  
429 30 memset(&hint, 0, sizeof(hint));
430 30 hint.ai_family = PF_UNSPEC; /* PF_INET or PF_INET6 */
431 30 hint.ai_socktype = SOCK_STREAM;
432 30 hint.ai_protocol = IPPROTO_TCP;
433  
434 30 (void)sprintf(portnumber_string, "%d", portnumber);
435  
436 30 last_error = getaddrinfo(hostname, portnumber_string, &hint, &address_list);
437  
438 30 ▶ if (0 != last_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 != last_error) { 0 30 30
439 0 return AMQP_STATUS_HOSTNAME_RESOLUTION_FAILED;
440 }
441  
442 30 ▶ for (addr = address_list; addr; addr = addr->ai_next) {
Branch: 1 branch
BranchTrueFalseTotal
for (addr = address_list; addr; addr = addr->ai_next) { 30 0 30
443 30 ▶ if (-1 != sockfd) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 != sockfd) { 0 30 30
444 0 amqp_os_socket_close(sockfd);
445 }
446  
447 30 sockfd = amqp_os_socket_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
448  
449 30 ▶ if (-1 == sockfd) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == sockfd) { 0 30 30
450 0 last_error = AMQP_STATUS_SOCKET_ERROR;
451 0 continue;
452 }
453  
454 #ifdef SO_NOSIGPIPE
455 if (0 != amqp_os_socket_setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one))) {
456 last_error = AMQP_STATUS_SOCKET_ERROR;
457 continue;
458 }
459 #endif /* SO_NOSIGPIPE */
460  
461 30 ▶ if (0 != amqp_os_socket_setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one))) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 != amqp_os_socket_setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one))) { 0 30 30
462 0 last_error = AMQP_STATUS_SOCKET_ERROR;
463 0 continue;
464 }
465  
466 30 ▶ if (AMQP_STATUS_OK != amqp_os_socket_setsockblock(sockfd, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != amqp_os_socket_setsockblock(sockfd, 0)) { 0 30 30
467 0 last_error = AMQP_STATUS_SOCKET_ERROR;
468 0 continue;
469 }
470  
471 #ifdef _WIN32
472 res = connect(sockfd, addr->ai_addr, (int)addr->ai_addrlen);
473 #else
474 30 res = connect(sockfd, addr->ai_addr, addr->ai_addrlen);
475 #endif
476  
477 30 ▶ if (0 == res) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == res) { 0 30 30
478 0 last_error = AMQP_STATUS_OK;
479 0 break;
480 }
481  
482 #ifdef _WIN32
483 if (WSAEWOULDBLOCK == amqp_os_socket_error()) {
484 #else
485 30 ▶ if (EINPROGRESS == amqp_os_socket_error()) {
Branch: 1 branch
BranchTrueFalseTotal
if (EINPROGRESS == amqp_os_socket_error()) { 30 0 30
486 #endif
487 30 last_error = amqp_poll(sockfd, AMQP_SF_POLLOUT, deadline);
488 30 ▶ if (AMQP_STATUS_OK == last_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK == last_error) { 29 1 30
489 int result;
490 29 socklen_t result_len = sizeof(result);
491  
492 #ifdef _WIN32
493 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (char *)&result,
494 (int *)&result_len) < 0
495 #else
496 29 ▶ if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &result, &result_len) < 0
Branch: 1 branch
BranchTrueFalseTotal
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &result, &result_len) < 0 29 0 29
497 #endif
498 29 ▶ || result != 0) {
Branch: 1 branch
BranchTrueFalseTotal
|| result != 0) { 0 29 29
499 0 last_error = AMQP_STATUS_SOCKET_ERROR;
500 } else {
501 29 last_error = AMQP_STATUS_OK;
502 }
503 }
504  
505 30 ▶ if (last_error == AMQP_STATUS_OK || last_error == AMQP_STATUS_TIMEOUT ||
Branch: 3 branches
BranchTrueFalseTotal
if (last_error == AMQP_STATUS_OK || last_error == AMQP_STATUS_TIMEOUT || 1 29 30
if (last_error == AMQP_STATUS_OK || last_error == AMQP_STATUS_TIMEOUT || 0 1 1
if (last_error == AMQP_STATUS_OK || last_error == AMQP_STATUS_TIMEOUT || 0 0 0
506 last_error == AMQP_STATUS_TIMER_FAILURE) {
507 /* Exit for loop on timer errors or when connection established */
508 break;
509 }
510  
511 } else {
512 /* Error connecting */
513 0 last_error = AMQP_STATUS_SOCKET_ERROR;
514 0 break;
515 }
516 }
517  
518 30 freeaddrinfo(address_list);
519 30 ▶ if (last_error != AMQP_STATUS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (last_error != AMQP_STATUS_OK) { 1 29 30
520 1 ▶ if (-1 != sockfd) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 != sockfd) { 1 0 1
521 1 amqp_os_socket_close(sockfd);
522 }
523  
524 1 return last_error;
525 }
526  
527 29 return sockfd;
528 }
529  
530 28 int amqp_send_header(amqp_connection_state_t state)
531 {
532 ssize_t res;
533 static const uint8_t header[8] = { 'A', 'M', 'Q', 'P', 0,
534 AMQP_PROTOCOL_VERSION_MAJOR,
535 AMQP_PROTOCOL_VERSION_MINOR,
536 AMQP_PROTOCOL_VERSION_REVISION
537 };
538 28 res = amqp_try_send(state, header, sizeof(header), amqp_time_infinite(),
539 AMQP_SF_NONE);
540 28 ▶ if (sizeof(header) == res) {
Branch: 1 branch
BranchTrueFalseTotal
if (sizeof(header) == res) { 28 0 28
541 28 return AMQP_STATUS_OK;
542 }
543 0 return (int)res;
544 }
545  
546 56 static amqp_bytes_t sasl_method_name(amqp_sasl_method_enum method)
547 {
548 amqp_bytes_t res;
549  
550 56 switch (method) {
551 56 case AMQP_SASL_METHOD_PLAIN:
552 56 res.bytes = "PLAIN";
553 56 res.len = 5;
554 56 break;
555 0 case AMQP_SASL_METHOD_EXTERNAL:
556 0 res.bytes = "EXTERNAL";
557 0 res.len = 8;
558 0 break;
559  
560 0 default:
561 0 amqp_abort("Invalid SASL method: %d", (int) method);
562 }
563  
564 56 return res;
565 }
566  
567 28 static int bytes_equal(amqp_bytes_t l, amqp_bytes_t r)
568 {
569 28 ▶ if (l.len == r.len) {
Branch: 1 branch
BranchTrueFalseTotal
if (l.len == r.len) { 28 0 28
570 28 ▶ if (l.bytes && r.bytes) {
Branch: 2 branches
BranchTrueFalseTotal
if (l.bytes && r.bytes) { 28 0 28
if (l.bytes && r.bytes) { 28 0 28
571 28 ▶ if (0 == memcmp(l.bytes, r.bytes, l.len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == memcmp(l.bytes, r.bytes, l.len)) { 28 0 28
572 28 return 1;
573 }
574 }
575 }
576 0 return 0;
577 }
578  
579 28 int sasl_mechanism_in_list(amqp_bytes_t mechanisms,
580 amqp_sasl_method_enum method)
581 {
582 amqp_bytes_t mechanism;
583 amqp_bytes_t supported_mechanism;
584 uint8_t* start;
585 uint8_t* end;
586 uint8_t* current;
587  
588 28 ▶ assert(NULL != mechanisms.bytes);
Branch: 1 branch
BranchTrueFalseTotal
assert(NULL != mechanisms.bytes); 0 28 28
589  
590 28 mechanism = sasl_method_name(method);
591  
592 28 start = (uint8_t*)mechanisms.bytes;
593 28 current = start;
594 28 end = start + mechanisms.len;
595  
596 28 ▶ for ( ; current != end; start = current + 1) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ; current != end; start = current + 1) { 28 0 28
597 /* HACK: SASL states that we should be parsing this string as a UTF-8
598 * string, which we're plainly not doing here. At this point its not worth
599 * dragging an entire UTF-8 parser for this one case, and this should work
600 * most of the time */
601 28 current = memchr(start, ' ', end - start);
602 28 ▶ if (NULL == current) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == current) { 0 28 28
603 0 current = end;
604 }
605 28 supported_mechanism.bytes = start;
606 28 supported_mechanism.len = current - start;
607 28 ▶ if (bytes_equal(mechanism, supported_mechanism)) {
Branch: 1 branch
BranchTrueFalseTotal
if (bytes_equal(mechanism, supported_mechanism)) { 28 0 28
608 28 return 1;
609 }
610 }
611  
612 0 return 0;
613 }
614  
615 28 static amqp_bytes_t sasl_response(amqp_pool_t *pool,
616 amqp_sasl_method_enum method,
617 va_list args)
618 {
619 amqp_bytes_t response;
620  
621 28 switch (method) {
622 28 case AMQP_SASL_METHOD_PLAIN: {
623 28 char *username = va_arg(args, char *);
624 28 size_t username_len = strlen(username);
625 28 char *password = va_arg(args, char *);
626 28 size_t password_len = strlen(password);
627 char *response_buf;
628  
629 28 amqp_pool_alloc_bytes(pool, strlen(username) + strlen(password) + 2, &response);
630 28 ▶ if (response.bytes == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (response.bytes == NULL) 0 28 28
631 /* We never request a zero-length block, because of the +2
632 above, so a NULL here really is ENOMEM. */
633 {
634 0 return response;
635 }
636  
637 28 response_buf = response.bytes;
638 28 response_buf[0] = 0;
639 28 memcpy(response_buf + 1, username, username_len);
640 28 response_buf[username_len + 1] = 0;
641 28 memcpy(response_buf + username_len + 2, password, password_len);
642 28 break;
643 }
644 0 case AMQP_SASL_METHOD_EXTERNAL: {
645 0 char *identity = va_arg(args, char *);
646 0 size_t identity_len = strlen(identity);
647  
648 0 amqp_pool_alloc_bytes(pool, identity_len, &response);
649 0 ▶ if (response.bytes == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (response.bytes == NULL) { 0 0 0
650 0 return response;
651 }
652  
653 0 memcpy(response.bytes, identity, identity_len);
654 0 break;
655 }
656 0 default:
657 0 amqp_abort("Invalid SASL method: %d", (int) method);
658 }
659  
660 28 return response;
661 }
662  
663 0 amqp_boolean_t amqp_frames_enqueued(amqp_connection_state_t state)
664 {
665 0 return (state->first_queued_frame != NULL);
666 }
667  
668 /*
669 * Check to see if we have data in our buffer. If this returns 1, we
670 * will avoid an immediate blocking read in amqp_simple_wait_frame.
671 */
672 142 amqp_boolean_t amqp_data_in_buffer(amqp_connection_state_t state)
673 {
674 142 return (state->sock_inbound_offset < state->sock_inbound_limit);
675 }
676  
677 56 static int consume_one_frame(amqp_connection_state_t state, amqp_frame_t *decoded_frame)
678 {
679 int res;
680  
681 amqp_bytes_t buffer;
682 56 buffer.len = state->sock_inbound_limit - state->sock_inbound_offset;
683 56 buffer.bytes = ((char *) state->sock_inbound_buffer.bytes) + state->sock_inbound_offset;
684  
685 56 res = amqp_handle_input(state, buffer, decoded_frame);
686 56 ▶ if (res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (res < 0) { 0 56 56
687 0 return res;
688 }
689  
690 56 state->sock_inbound_offset += res;
691  
692 56 return AMQP_STATUS_OK;
693 }
694  
695  
696 56 static int recv_with_timeout(amqp_connection_state_t state, amqp_time_t timeout) {
697 ssize_t res;
698 int fd;
699  
700 112 start_recv:
701 112 res = amqp_socket_recv(state->socket, state->sock_inbound_buffer.bytes,
702 state->sock_inbound_buffer.len, 0);
703  
704 112 ▶ if (res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (res < 0) { 56 56 112
705 56 fd = amqp_get_sockfd(state);
706 56 ▶ if (-1 == fd) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == fd) { 0 56 56
707 0 return AMQP_STATUS_CONNECTION_CLOSED;
708 }
709 56 switch (res) {
710 0 default:
711 0 return (int)res;
712 56 case AMQP_PRIVATE_STATUS_SOCKET_NEEDREAD:
713 56 res = amqp_poll(fd, AMQP_SF_POLLIN, timeout);
714 56 break;
715 0 case AMQP_PRIVATE_STATUS_SOCKET_NEEDWRITE:
716 0 res = amqp_poll(fd, AMQP_SF_POLLOUT, timeout);
717 0 break;
718 }
719 56 ▶ if (AMQP_STATUS_OK == res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK == res) { 56 0 56
720 56 goto start_recv;
721 }
722 0 return (int)res;
723 }
724  
725 56 state->sock_inbound_limit = res;
726 56 state->sock_inbound_offset = 0;
727  
728 56 res = amqp_time_s_from_now(&state->next_recv_heartbeat,
729 amqp_heartbeat_recv(state));
730 56 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 56 56
731 0 return (int)res;
732 }
733 56 return AMQP_STATUS_OK;
734 }
735  
736 0 int amqp_try_recv(amqp_connection_state_t state) {
737 amqp_time_t timeout;
738  
739 0 ▶ while (amqp_data_in_buffer(state)) {
Branch: 1 branch
BranchTrueFalseTotal
while (amqp_data_in_buffer(state)) { 0 0 0
740 amqp_frame_t frame;
741 0 int res = consume_one_frame(state, &frame);
742  
743 0 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 0 0
744 0 return res;
745 }
746  
747 0 ▶ if (frame.frame_type != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (frame.frame_type != 0) { 0 0 0
748 amqp_pool_t *channel_pool;
749 amqp_frame_t *frame_copy;
750 amqp_link_t *link;
751  
752 0 channel_pool = amqp_get_or_create_channel_pool(state, frame.channel);
753 0 ▶ if (NULL == channel_pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == channel_pool) { 0 0 0
754 0 return AMQP_STATUS_NO_MEMORY;
755 }
756  
757 0 frame_copy = amqp_pool_alloc(channel_pool, sizeof(amqp_frame_t));
758 0 link = amqp_pool_alloc(channel_pool, sizeof(amqp_link_t));
759  
760 0 ▶ if (frame_copy == NULL || link == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (frame_copy == NULL || link == NULL) { 0 0 0
if (frame_copy == NULL || link == NULL) { 0 0 0
761 0 return AMQP_STATUS_NO_MEMORY;
762 }
763  
764 0 *frame_copy = frame;
765  
766 0 link->next = NULL;
767 0 link->data = frame_copy;
768  
769 0 ▶ if (state->last_queued_frame == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->last_queued_frame == NULL) { 0 0 0
770 0 state->first_queued_frame = link;
771 } else {
772 0 state->last_queued_frame->next = link;
773 }
774 0 state->last_queued_frame = link;
775 }
776 }
777 0 timeout = amqp_time_immediate();
778  
779 0 return recv_with_timeout(state, timeout);
780 }
781  
782 56 static int wait_frame_inner(amqp_connection_state_t state,
783 amqp_frame_t *decoded_frame,
784 struct timeval *timeout)
785 {
786 amqp_time_t deadline;
787 amqp_time_t timeout_deadline;
788 int res;
789  
790 56 res = amqp_time_from_now(&timeout_deadline, timeout);
791 56 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 56 0 56
792 0 return res;
793 }
794  
795 56 while (1) {
796 int res;
797  
798 112 ▶ while (amqp_data_in_buffer(state)) {
Branch: 1 branch
BranchTrueFalseTotal
while (amqp_data_in_buffer(state)) { 56 56 112
799 56 res = consume_one_frame(state, decoded_frame);
800  
801 56 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 56 56
802 0 return res;
803 }
804  
805 56 ▶ if (AMQP_FRAME_HEARTBEAT == decoded_frame->frame_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_FRAME_HEARTBEAT == decoded_frame->frame_type) { 0 56 56
806 0 amqp_maybe_release_buffers_on_channel(state, 0);
807 0 continue;
808 }
809  
810 56 ▶ if (decoded_frame->frame_type != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (decoded_frame->frame_type != 0) { 56 0 56
811 /* Complete frame was read. Return it. */
812 56 return AMQP_STATUS_OK;
813 }
814 }
815  
816 56 beginrecv:
817 56 res = amqp_time_has_past(state->next_send_heartbeat);
818 56 ▶ if (AMQP_STATUS_TIMER_FAILURE == res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_TIMER_FAILURE == res) { 0 56 56
819 0 return res;
820 56 ▶ } else if (AMQP_STATUS_TIMEOUT == res) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (AMQP_STATUS_TIMEOUT == res) { 0 56 56
821 amqp_frame_t heartbeat;
822 0 heartbeat.channel = 0;
823 0 heartbeat.frame_type = AMQP_FRAME_HEARTBEAT;
824  
825 0 res = amqp_send_frame(state, &heartbeat);
826 0 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 0 0
827 0 return res;
828 }
829 }
830 56 deadline = amqp_time_first(timeout_deadline,
831 amqp_time_first(state->next_recv_heartbeat,
832 state->next_send_heartbeat));
833  
834 /* TODO this needs to wait for a _frame_ and not anything written from the
835 * socket */
836 56 res = recv_with_timeout(state, deadline);
837  
838 56 ▶ if (AMQP_STATUS_TIMEOUT == res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_TIMEOUT == res) { 0 56 56
839 0 ▶ if (amqp_time_equal(deadline, state->next_recv_heartbeat)) {
Branch: 1 branch
BranchTrueFalseTotal
if (amqp_time_equal(deadline, state->next_recv_heartbeat)) { 0 0 0
840 0 amqp_socket_close(state->socket);
841 0 return AMQP_STATUS_HEARTBEAT_TIMEOUT;
842 0 ▶ } else if (amqp_time_equal(deadline, timeout_deadline)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (amqp_time_equal(deadline, timeout_deadline)) { 0 0 0
843 0 return AMQP_STATUS_TIMEOUT;
844 0 ▶ } else if (amqp_time_equal(deadline, state->next_send_heartbeat)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (amqp_time_equal(deadline, state->next_send_heartbeat)) { 0 0 0
845 /* send heartbeat happens before we do recv_with_timeout */
846 0 goto beginrecv;
847 } else {
848 0 amqp_abort("Internal error: unable to determine timeout reason");
849 }
850 56 ▶ } else if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (AMQP_STATUS_OK != res) { 0 56 56
851 0 return res;
852 }
853 }
854 }
855  
856 0 static amqp_link_t * amqp_create_link_for_frame(amqp_connection_state_t state, amqp_frame_t *frame)
857 {
858 amqp_link_t *link;
859 amqp_frame_t *frame_copy;
860  
861 0 amqp_pool_t *channel_pool = amqp_get_or_create_channel_pool(state, frame->channel);
862  
863 0 ▶ if (NULL == channel_pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == channel_pool) { 0 0 0
864 0 return NULL;
865 }
866  
867 0 link = amqp_pool_alloc(channel_pool, sizeof(amqp_link_t));
868 0 frame_copy = amqp_pool_alloc(channel_pool, sizeof(amqp_frame_t));
869  
870 0 ▶ if (NULL == link || NULL == frame_copy) {
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == link || NULL == frame_copy) { 0 0 0
if (NULL == link || NULL == frame_copy) { 0 0 0
871 0 return NULL;
872 }
873  
874 0 *frame_copy = *frame;
875 0 link->data = frame_copy;
876  
877 0 return link;
878 }
879  
880 0 int amqp_queue_frame(amqp_connection_state_t state, amqp_frame_t *frame)
881 {
882 0 amqp_link_t *link = amqp_create_link_for_frame(state, frame);
883 0 ▶ if (NULL == link) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == link) { 0 0 0
884 0 return AMQP_STATUS_NO_MEMORY;
885 }
886  
887 0 ▶ if (NULL == state->first_queued_frame) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == state->first_queued_frame) { 0 0 0
888 0 state->first_queued_frame = link;
889 } else {
890 0 state->last_queued_frame->next = link;
891 }
892  
893 0 link->next = NULL;
894 0 state->last_queued_frame = link;
895  
896 0 return AMQP_STATUS_OK;
897 }
898  
899 0 int amqp_put_back_frame(amqp_connection_state_t state, amqp_frame_t *frame)
900 {
901 0 amqp_link_t *link = amqp_create_link_for_frame(state, frame);
902 0 ▶ if (NULL == link) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == link) { 0 0 0
903 0 return AMQP_STATUS_NO_MEMORY;
904 }
905  
906 0 ▶ if (NULL == state->first_queued_frame) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == state->first_queued_frame) { 0 0 0
907 0 state->first_queued_frame = link;
908 0 state->last_queued_frame = link;
909 0 link->next = NULL;
910 } else {
911 0 link->next = state->first_queued_frame;
912 0 state->first_queued_frame = link;
913 }
914  
915 0 return AMQP_STATUS_OK;
916 }
917  
918 0 int amqp_simple_wait_frame_on_channel(amqp_connection_state_t state,
919 amqp_channel_t channel,
920 amqp_frame_t *decoded_frame)
921 {
922 amqp_frame_t *frame_ptr;
923 amqp_link_t *cur;
924 int res;
925  
926 0 ▶ for (cur = state->first_queued_frame; NULL != cur; cur = cur->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (cur = state->first_queued_frame; NULL != cur; cur = cur->next) { 0 0 0
927 0 frame_ptr = cur->data;
928  
929 0 ▶ if (channel == frame_ptr->channel) {
Branch: 1 branch
BranchTrueFalseTotal
if (channel == frame_ptr->channel) { 0 0 0
930 0 state->first_queued_frame = cur->next;
931 0 ▶ if (NULL == state->first_queued_frame) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == state->first_queued_frame) { 0 0 0
932 0 state->last_queued_frame = NULL;
933 }
934  
935 0 *decoded_frame = *frame_ptr;
936  
937 0 return AMQP_STATUS_OK;
938 }
939 }
940  
941 while (1) {
942 0 res = wait_frame_inner(state, decoded_frame, NULL);
943  
944 0 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 0 0
945 0 return res;
946 }
947  
948 0 ▶ if (channel == decoded_frame->channel) {
Branch: 1 branch
BranchTrueFalseTotal
if (channel == decoded_frame->channel) { 0 0 0
949 0 return AMQP_STATUS_OK;
950 } else {
951 0 res = amqp_queue_frame(state, decoded_frame);
952 0 ▶ if (res != AMQP_STATUS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (res != AMQP_STATUS_OK) { 0 0 0
953 0 return res;
954 }
955 }
956 }
957 }
958  
959 56 int amqp_simple_wait_frame(amqp_connection_state_t state,
960 amqp_frame_t *decoded_frame)
961 {
962 56 return amqp_simple_wait_frame_noblock(state, decoded_frame, NULL);
963 }
964  
965 56 int amqp_simple_wait_frame_noblock(amqp_connection_state_t state,
966 amqp_frame_t *decoded_frame,
967 struct timeval *timeout)
968 {
969 56 ▶ if (state->first_queued_frame != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->first_queued_frame != NULL) { 0 56 56
970 0 amqp_frame_t *f = (amqp_frame_t *) state->first_queued_frame->data;
971 0 state->first_queued_frame = state->first_queued_frame->next;
972 0 ▶ if (state->first_queued_frame == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->first_queued_frame == NULL) { 0 0 0
973 0 state->last_queued_frame = NULL;
974 }
975 0 *decoded_frame = *f;
976 0 return AMQP_STATUS_OK;
977 } else {
978 56 return wait_frame_inner(state, decoded_frame, timeout);
979 }
980 }
981  
982 56 static int amqp_simple_wait_method_list(amqp_connection_state_t state,
983 amqp_channel_t expected_channel,
984 amqp_method_number_t *expected_methods,
985 amqp_method_t *output) {
986 amqp_frame_t frame;
987 56 int res = amqp_simple_wait_frame(state, &frame);
988 56 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 56 56
989 0 return res;
990 }
991  
992 56 ▶ if (AMQP_FRAME_METHOD != frame.frame_type ||
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_FRAME_METHOD != frame.frame_type || 56 0 56
993 112 expected_channel != frame.channel ||
994 56 !amqp_id_in_reply_list(frame.payload.method.id, expected_methods)) {
995 0 return AMQP_STATUS_WRONG_METHOD;
996 }
997 56 *output = frame.payload.method;
998 56 return AMQP_STATUS_OK;
999 }
1000  
1001 28 int amqp_simple_wait_method(amqp_connection_state_t state,
1002 amqp_channel_t expected_channel,
1003 amqp_method_number_t expected_method,
1004 amqp_method_t *output)
1005 {
1006 28 amqp_method_number_t expected_methods[] = { 0, 0 };
1007 28 expected_methods[0] = expected_method;
1008 28 return amqp_simple_wait_method_list(state, expected_channel, expected_methods,
1009 output);
1010 }
1011  
1012 58 int amqp_send_method(amqp_connection_state_t state, amqp_channel_t channel,
1013 amqp_method_number_t id, void *decoded) {
1014 58 return amqp_send_method_inner(state, channel, id, decoded, AMQP_SF_NONE);
1015 }
1016  
1017 58 int amqp_send_method_inner(amqp_connection_state_t state,
1018 amqp_channel_t channel, amqp_method_number_t id,
1019 void *decoded, int flags) {
1020 amqp_frame_t frame;
1021  
1022 58 frame.frame_type = AMQP_FRAME_METHOD;
1023 58 frame.channel = channel;
1024 58 frame.payload.method.id = id;
1025 58 frame.payload.method.decoded = decoded;
1026 58 return amqp_send_frame_inner(state, &frame, flags);
1027 }
1028  
1029 56 static int amqp_id_in_reply_list( amqp_method_number_t expected, amqp_method_number_t *list )
1030 {
1031 84 ▶ while ( *list != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( *list != 0 ) { 84 0 84
1032 84 ▶ if ( *list == expected ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( *list == expected ) { 56 28 84
1033 56 return 1;
1034 }
1035 28 list++;
1036 }
1037 0 return 0;
1038 }
1039  
1040 2 amqp_rpc_reply_t amqp_simple_rpc(amqp_connection_state_t state,
1041 amqp_channel_t channel,
1042 amqp_method_number_t request_id,
1043 amqp_method_number_t *expected_reply_ids,
1044 void *decoded_request_method)
1045 {
1046 int status;
1047 amqp_rpc_reply_t result;
1048  
1049 2 memset(&result, 0, sizeof(result));
1050  
1051 2 status = amqp_send_method(state, channel, request_id, decoded_request_method);
1052 2 ▶ if (status < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status < 0) { 0 2 2
1053 2 result.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
1054 2 result.library_error = status;
1055 2 return result;
1056 }
1057  
1058 {
1059 amqp_frame_t frame;
1060  
1061 0 retry:
1062 0 status = wait_frame_inner(state, &frame, NULL);
1063 0 ▶ if (status < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status < 0) { 0 0 0
1064 0 result.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
1065 0 result.library_error = status;
1066 0 return result;
1067 }
1068  
1069 /*
1070 * We store the frame for later processing unless it's something
1071 * that directly affects us here, namely a method frame that is
1072 * either
1073 * - on the channel we want, and of the expected type, or
1074 * - on the channel we want, and a channel.close frame, or
1075 * - on channel zero, and a connection.close frame.
1076 */
1077 0 ▶ if (!((frame.frame_type == AMQP_FRAME_METHOD)
Branch: 1 branch
BranchTrueFalseTotal
if (!((frame.frame_type == AMQP_FRAME_METHOD) 0 0 0
1078 && (
1079 0 ▶ ((frame.channel == channel)
Branch: 1 branch
BranchTrueFalseTotal
((frame.channel == channel) 0 0 0
1080 0 ▶ && (amqp_id_in_reply_list(frame.payload.method.id, expected_reply_ids)
Branch: 1 branch
BranchTrueFalseTotal
&& (amqp_id_in_reply_list(frame.payload.method.id, expected_reply_ids) 0 0 0
1081 0 ▶ || (frame.payload.method.id == AMQP_CHANNEL_CLOSE_METHOD)))
Branch: 1 branch
BranchTrueFalseTotal
|| (frame.payload.method.id == AMQP_CHANNEL_CLOSE_METHOD))) 0 0 0
1082 0 ||
1083 0 ▶ ((frame.channel == 0)
Branch: 1 branch
BranchTrueFalseTotal
((frame.channel == 0) 0 0 0
1084 0 ▶ && (frame.payload.method.id == AMQP_CONNECTION_CLOSE_METHOD))
Branch: 1 branch
BranchTrueFalseTotal
&& (frame.payload.method.id == AMQP_CONNECTION_CLOSE_METHOD)) 0 0 0
1085 )
1086 )) {
1087 amqp_pool_t *channel_pool;
1088 amqp_frame_t *frame_copy;
1089 amqp_link_t *link;
1090  
1091 0 channel_pool = amqp_get_or_create_channel_pool(state, frame.channel);
1092 0 ▶ if (NULL == channel_pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == channel_pool) { 0 0 0
1093 0 result.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
1094 0 result.library_error = AMQP_STATUS_NO_MEMORY;
1095 0 return result;
1096 }
1097  
1098 0 frame_copy = amqp_pool_alloc(channel_pool, sizeof(amqp_frame_t));
1099 0 link = amqp_pool_alloc(channel_pool, sizeof(amqp_link_t));
1100  
1101 0 ▶ if (frame_copy == NULL || link == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (frame_copy == NULL || link == NULL) { 0 0 0
if (frame_copy == NULL || link == NULL) { 0 0 0
1102 0 result.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
1103 0 result.library_error = AMQP_STATUS_NO_MEMORY;
1104 0 return result;
1105 }
1106  
1107 0 *frame_copy = frame;
1108  
1109 0 link->next = NULL;
1110 0 link->data = frame_copy;
1111  
1112 0 ▶ if (state->last_queued_frame == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->last_queued_frame == NULL) { 0 0 0
1113 0 state->first_queued_frame = link;
1114 } else {
1115 0 state->last_queued_frame->next = link;
1116 }
1117 0 state->last_queued_frame = link;
1118  
1119 0 goto retry;
1120 }
1121  
1122 0 result.reply_type = (amqp_id_in_reply_list(frame.payload.method.id, expected_reply_ids))
1123 ? AMQP_RESPONSE_NORMAL
1124 0 ▶ : AMQP_RESPONSE_SERVER_EXCEPTION;
Branch: 1 branch
BranchTrueFalseTotal
: AMQP_RESPONSE_SERVER_EXCEPTION; 0 0 0
1125  
1126 0 result.reply = frame.payload.method;
1127 0 return result;
1128 }
1129 }
1130  
1131 0 void *amqp_simple_rpc_decoded(amqp_connection_state_t state,
1132 amqp_channel_t channel,
1133 amqp_method_number_t request_id,
1134 amqp_method_number_t reply_id,
1135 void *decoded_request_method)
1136 {
1137 amqp_method_number_t replies[2];
1138  
1139 0 replies[0] = reply_id;
1140 0 replies[1] = 0;
1141  
1142 0 state->most_recent_api_result = amqp_simple_rpc(state, channel,
1143 request_id, replies,
1144 decoded_request_method);
1145 0 ▶ if (state->most_recent_api_result.reply_type == AMQP_RESPONSE_NORMAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->most_recent_api_result.reply_type == AMQP_RESPONSE_NORMAL) { 0 0 0
1146 0 return state->most_recent_api_result.reply.decoded;
1147 } else {
1148 0 return NULL;
1149 }
1150 }
1151  
1152 0 amqp_rpc_reply_t amqp_get_rpc_reply(amqp_connection_state_t state)
1153 {
1154 0 return state->most_recent_api_result;
1155 }
1156  
1157 /*
1158 * Merge base and add tables. If the two tables contain an entry with the same
1159 * key, the entry from the add table takes precedence. For entries that are both
1160 * tables with the same key, the table is recursively merged.
1161 */
1162 28 int amqp_merge_capabilities(const amqp_table_t *base, const amqp_table_t *add,
1163 amqp_table_t *result, amqp_pool_t *pool) {
1164 int i;
1165 int res;
1166 amqp_pool_t temp_pool;
1167 amqp_table_t temp_result;
1168 28 ▶ assert(base != NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(base != NULL); 0 28 28
1169 28 ▶ assert(result != NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(result != NULL); 0 28 28
1170 28 ▶ assert(pool != NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(pool != NULL); 0 28 28
1171  
1172 28 ▶ if (NULL == add) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == add) { 0 28 28
1173 0 return amqp_table_clone(base, result, pool);
1174 }
1175  
1176 28 init_amqp_pool(&temp_pool, 4096);
1177 28 temp_result.num_entries = 0;
1178 28 temp_result.entries =
1179 28 amqp_pool_alloc(&temp_pool, sizeof(amqp_table_entry_t) *
1180 28 (base->num_entries + add->num_entries));
1181 28 ▶ if (NULL == temp_result.entries) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == temp_result.entries) { 0 28 28
1182 0 res = AMQP_STATUS_NO_MEMORY;
1183 0 goto error_out;
1184 }
1185 196 ▶ for (i = 0; i < base->num_entries; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < base->num_entries; ++i) { 168 28 196
1186 168 temp_result.entries[temp_result.num_entries] = base->entries[i];
1187 168 temp_result.num_entries++;
1188 }
1189 28 ▶ for (i = 0; i < add->num_entries; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < add->num_entries; ++i) { 0 28 28
1190 amqp_table_entry_t *e =
1191 0 amqp_table_get_entry_by_key(&temp_result, add->entries[i].key);
1192 0 ▶ if (NULL != e) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != e) { 0 0 0
1193 0 ▶ if (AMQP_FIELD_KIND_TABLE == add->entries[i].value.kind &&
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_FIELD_KIND_TABLE == add->entries[i].value.kind && 0 0 0
1194 0 ▶ AMQP_FIELD_KIND_TABLE == e->value.kind) {
Branch: 1 branch
BranchTrueFalseTotal
AMQP_FIELD_KIND_TABLE == e->value.kind) { 0 0 0
1195 amqp_table_entry_t *be =
1196 0 amqp_table_get_entry_by_key(base, add->entries[i].key);
1197  
1198 0 res = amqp_merge_capabilities(&be->value.value.table,
1199 0 &add->entries[i].value.value.table,
1200 &e->value.value.table, &temp_pool);
1201 0 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 0 0
1202 0 goto error_out;
1203 }
1204 } else {
1205 0 e->value = add->entries[i].value;
1206 }
1207 } else {
1208 0 temp_result.entries[temp_result.num_entries] = add->entries[i];
1209 0 temp_result.num_entries++;
1210 }
1211 }
1212 28 res = amqp_table_clone(&temp_result, result, pool);
1213 28 error_out:
1214 28 empty_amqp_pool(&temp_pool);
1215 28 return res;
1216 }
1217  
1218 28 static amqp_rpc_reply_t amqp_login_inner(amqp_connection_state_t state,
1219 char const *vhost,
1220 int channel_max,
1221 int frame_max,
1222 int heartbeat,
1223 const amqp_table_t *client_properties,
1224 amqp_sasl_method_enum sasl_method,
1225 va_list vl)
1226 {
1227 int res;
1228 amqp_method_t method;
1229 int server_frame_max;
1230 uint16_t server_channel_max;
1231 uint16_t server_heartbeat;
1232 amqp_rpc_reply_t result;
1233  
1234 28 res = amqp_send_header(state);
1235 28 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 28 28
1236 0 goto error_res;
1237 }
1238  
1239 28 res = amqp_simple_wait_method(state, 0, AMQP_CONNECTION_START_METHOD,
1240 &method);
1241 28 ▶ if (res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (res < 0) { 0 28 28
1242 0 goto error_res;
1243 }
1244  
1245 {
1246 28 amqp_connection_start_t *s = (amqp_connection_start_t *) method.decoded;
1247 28 ▶ if ((s->version_major != AMQP_PROTOCOL_VERSION_MAJOR)
Branch: 1 branch
BranchTrueFalseTotal
if ((s->version_major != AMQP_PROTOCOL_VERSION_MAJOR) 28 0 28
1248 28 ▶ || (s->version_minor != AMQP_PROTOCOL_VERSION_MINOR)) {
Branch: 1 branch
BranchTrueFalseTotal
|| (s->version_minor != AMQP_PROTOCOL_VERSION_MINOR)) { 0 28 28
1249 0 res = AMQP_STATUS_INCOMPATIBLE_AMQP_VERSION;
1250 0 goto error_res;
1251 }
1252  
1253 28 res = amqp_table_clone(&s->server_properties, &state->server_properties,
1254 &state->properties_pool);
1255  
1256 28 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 28 28
1257 0 goto error_res;
1258 }
1259  
1260 /* TODO: check that our chosen SASL mechanism is in the list of
1261 acceptable mechanisms. Or even let the application choose from
1262 the list! */
1263 28 ▶ if (!sasl_mechanism_in_list(s->mechanisms, sasl_method)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sasl_mechanism_in_list(s->mechanisms, sasl_method)) { 0 28 28
1264 0 res = AMQP_STATUS_BROKER_UNSUPPORTED_SASL_METHOD;
1265 0 goto error_res;
1266 }
1267 }
1268  
1269 {
1270 amqp_table_entry_t default_properties[6];
1271 amqp_table_t default_table;
1272 amqp_table_entry_t client_capabilities[1];
1273 amqp_table_t client_capabilities_table;
1274 amqp_connection_start_ok_t s;
1275 amqp_pool_t *channel_pool;
1276 amqp_bytes_t response_bytes;
1277  
1278 28 channel_pool = amqp_get_or_create_channel_pool(state, 0);
1279 28 ▶ if (NULL == channel_pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == channel_pool) { 0 28 28
1280 0 res = AMQP_STATUS_NO_MEMORY;
1281 0 goto error_res;
1282 }
1283  
1284 28 response_bytes = sasl_response(channel_pool,
1285 sasl_method, vl);
1286 28 ▶ if (response_bytes.bytes == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (response_bytes.bytes == NULL) { 0 28 28
1287 0 res = AMQP_STATUS_NO_MEMORY;
1288 0 goto error_res;
1289 }
1290  
1291 client_capabilities[0] =
1292 28 amqp_table_construct_bool_entry("authentication_failure_close", 1);
1293  
1294 28 client_capabilities_table.entries = client_capabilities;
1295 28 client_capabilities_table.num_entries =
1296 sizeof(client_capabilities) / sizeof(amqp_table_entry_t);
1297  
1298 default_properties[0] =
1299 28 amqp_table_construct_utf8_entry("product", "rabbitmq-c");
1300 default_properties[1] =
1301 28 amqp_table_construct_utf8_entry("version", AMQP_VERSION_STRING);
1302 default_properties[2] =
1303 28 amqp_table_construct_utf8_entry("platform", AMQ_PLATFORM);
1304 default_properties[3] =
1305 28 amqp_table_construct_utf8_entry("copyright", AMQ_COPYRIGHT);
1306 28 default_properties[4] = amqp_table_construct_utf8_entry(
1307 "information", "See https://github.com/alanxz/rabbitmq-c");
1308 28 default_properties[5] = amqp_table_construct_table_entry(
1309 "capabilities", &client_capabilities_table);
1310  
1311 28 default_table.entries = default_properties;
1312 28 default_table.num_entries =
1313 sizeof(default_properties) / sizeof(amqp_table_entry_t);
1314  
1315 28 res = amqp_merge_capabilities(&default_table, client_properties,
1316 &state->client_properties, channel_pool);
1317 28 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 28 28
1318 0 goto error_res;
1319 }
1320  
1321 28 s.client_properties = state->client_properties;
1322 28 s.mechanism = sasl_method_name(sasl_method);
1323 28 s.response = response_bytes;
1324 28 s.locale = amqp_cstring_bytes("en_US");
1325  
1326 28 res = amqp_send_method(state, 0, AMQP_CONNECTION_START_OK_METHOD, &s);
1327 28 ▶ if (res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (res < 0) { 0 28 28
1328 0 goto error_res;
1329 }
1330 }
1331  
1332 28 amqp_release_buffers(state);
1333  
1334 {
1335 28 amqp_method_number_t expected[] = { AMQP_CONNECTION_TUNE_METHOD,
1336 AMQP_CONNECTION_CLOSE_METHOD, 0 };
1337 28 res = amqp_simple_wait_method_list(state, 0, expected, &method);
1338 28 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 28 28
1339 0 goto error_res;
1340 }
1341 }
1342  
1343 28 ▶ if (AMQP_CONNECTION_CLOSE_METHOD == method.id) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_CONNECTION_CLOSE_METHOD == method.id) { 28 0 28
1344 28 result.reply_type = AMQP_RESPONSE_SERVER_EXCEPTION;
1345 28 result.reply = method;
1346 28 result.library_error = 0;
1347 28 goto out;
1348 }
1349  
1350 {
1351 0 amqp_connection_tune_t *s = (amqp_connection_tune_t *) method.decoded;
1352 0 server_channel_max = s->channel_max;
1353 0 server_frame_max = s->frame_max;
1354 0 server_heartbeat = s->heartbeat;
1355 }
1356  
1357 0 ▶ if (server_channel_max != 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if (server_channel_max != 0 && 0 0 0
1358 0 ▶ (server_channel_max < channel_max || channel_max == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
(server_channel_max < channel_max || channel_max == 0)) { 0 0 0
(server_channel_max < channel_max || channel_max == 0)) { 0 0 0
1359 0 channel_max = server_channel_max;
1360 0 ▶ } else if (server_channel_max == 0 && channel_max == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (server_channel_max == 0 && channel_max == 0) { 0 0 0
} else if (server_channel_max == 0 && channel_max == 0) { 0 0 0
1361 0 channel_max = UINT16_MAX;
1362 }
1363  
1364 0 ▶ if (server_frame_max != 0 && server_frame_max < frame_max) {
Branch: 2 branches
BranchTrueFalseTotal
if (server_frame_max != 0 && server_frame_max < frame_max) { 0 0 0
if (server_frame_max != 0 && server_frame_max < frame_max) { 0 0 0
1365 0 frame_max = server_frame_max;
1366 }
1367  
1368 0 ▶ if (server_heartbeat != 0 && server_heartbeat < heartbeat) {
Branch: 2 branches
BranchTrueFalseTotal
if (server_heartbeat != 0 && server_heartbeat < heartbeat) { 0 0 0
if (server_heartbeat != 0 && server_heartbeat < heartbeat) { 0 0 0
1369 0 heartbeat = server_heartbeat;
1370 }
1371  
1372 0 res = amqp_tune_connection(state, channel_max, frame_max, heartbeat);
1373 0 ▶ if (res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (res < 0) { 0 0 0
1374 0 goto error_res;
1375 }
1376  
1377 {
1378 amqp_connection_tune_ok_t s;
1379 0 s.frame_max = frame_max;
1380 0 s.channel_max = channel_max;
1381 0 s.heartbeat = heartbeat;
1382  
1383 0 res = amqp_send_method(state, 0, AMQP_CONNECTION_TUNE_OK_METHOD, &s);
1384 0 ▶ if (res < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (res < 0) { 0 0 0
1385 0 goto error_res;
1386 }
1387 }
1388  
1389 0 amqp_release_buffers(state);
1390  
1391 {
1392 0 amqp_method_number_t replies[] = { AMQP_CONNECTION_OPEN_OK_METHOD, 0 };
1393 amqp_connection_open_t s;
1394 0 s.virtual_host = amqp_cstring_bytes(vhost);
1395 0 s.capabilities.len = 0;
1396 0 s.capabilities.bytes = NULL;
1397 0 s.insist = 1;
1398  
1399 0 result = amqp_simple_rpc(state,
1400 0,
1401 AMQP_CONNECTION_OPEN_METHOD,
1402 (amqp_method_number_t *) &replies,
1403 &s);
1404 0 ▶ if (result.reply_type != AMQP_RESPONSE_NORMAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (result.reply_type != AMQP_RESPONSE_NORMAL) { 0 0 0
1405 0 goto out;
1406 }
1407 }
1408  
1409 0 result.reply_type = AMQP_RESPONSE_NORMAL;
1410 0 result.reply.id = 0;
1411 0 result.reply.decoded = NULL;
1412 0 result.library_error = 0;
1413 0 amqp_maybe_release_buffers(state);
1414  
1415 28 out:
1416 28 return result;
1417  
1418 0 error_res:
1419 0 result.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
1420 0 result.reply.id = 0;
1421 0 result.reply.decoded = NULL;
1422 0 result.library_error = res;
1423  
1424 0 goto out;
1425 }
1426  
1427 28 amqp_rpc_reply_t amqp_login(amqp_connection_state_t state,
1428 char const *vhost,
1429 int channel_max,
1430 int frame_max,
1431 int heartbeat,
1432 amqp_sasl_method_enum sasl_method,
1433 ...)
1434 {
1435 va_list vl;
1436 amqp_rpc_reply_t ret;
1437  
1438 28 va_start(vl, sasl_method);
1439  
1440 28 ret = amqp_login_inner(state, vhost, channel_max, frame_max, heartbeat,
1441 &amqp_empty_table, sasl_method, vl);
1442  
1443 28 va_end(vl);
1444  
1445 28 return ret;
1446 }
1447  
1448 0 amqp_rpc_reply_t amqp_login_with_properties(amqp_connection_state_t state,
1449 char const *vhost,
1450 int channel_max,
1451 int frame_max,
1452 int heartbeat,
1453 const amqp_table_t *client_properties,
1454 amqp_sasl_method_enum sasl_method,
1455 ...)
1456 {
1457 va_list vl;
1458 amqp_rpc_reply_t ret;
1459  
1460 0 va_start(vl, sasl_method);
1461  
1462 0 ret = amqp_login_inner(state, vhost, channel_max, frame_max, heartbeat,
1463 client_properties, sasl_method, vl);
1464  
1465 0 va_end(vl);
1466  
1467 0 return ret;
1468 }
« amqp_time.c ↑ Summary amqp_connection.c »