amqp_socket.c

filter: Statementstmt 21.6% 45 / 208 Branchbran 10.4% 10 / 96 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 18.0% 55 / 304 SCAR 0
CC
0
Cov
0%
CRAP 0
« amqp_framing.c ↑ Summary amqp_table.c »
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include <errno.h>
6 #include <stdarg.h>
7 #include <fcntl.h>
8 #include <poll.h>
9  
10 #include "amqp.h"
11 #include "amqp_framing.h"
12 #include "amqp_private.h"
13  
14 #include <sys/types.h>
15 #include <sys/uio.h>
16 #include <unistd.h>
17 #include <sys/socket.h>
18 #include <netdb.h>
19 #include <netinet/in.h>
20  
21 #include <assert.h>
22  
23 18 int amqp_open_socket(char const *hostname,
24 int portnumber, struct timeval *timeout)
25 {
26 18 int result = -1;
27 int sockfd;
28 int flags;
29 struct sockaddr_in addr;
30 struct hostent *he;
31  
32 18 he = gethostbyname(hostname);
33 18 ▶ if (he == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (he == NULL) { 17 1 18
34 17 return -ENOENT;
35 }
36  
37 1 addr.sin_family = AF_INET;
38 1 addr.sin_port = htons(portnumber);
39 1 addr.sin_addr.s_addr = * (uint32_t *) he->h_addr_list[0];
40  
41 1 sockfd = socket(PF_INET, SOCK_STREAM, 0);
42 2 if(((flags = fcntl(sockfd, F_GETFL, 0)) == -1) ||
43 1 (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1)) {
44 0 result = -errno;
45 0 close(sockfd);
46 0 return -1;
47 }
48  
49 1 ▶ if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 1 0 1
50 1 result = -errno;
51 1 ▶ if(errno == EINPROGRESS) {
Branch: 1 branch
BranchTrueFalseTotal
if(errno == EINPROGRESS) { 1 0 1
52 int aerrno, prv;
53 1 socklen_t aerrno_len = sizeof(aerrno);
54 struct pollfd pfd;
55  
56 1 pfd.fd = sockfd;
57 1 pfd.events = POLLOUT;
58 1 ▶ prv = poll(&pfd, 1, timeout?(timeout->tv_sec*1000 + timeout->tv_usec/1000):-1);
Branch: 1 branch
BranchTrueFalseTotal
prv = poll(&pfd, 1, timeout?(timeout->tv_sec*1000 + timeout->tv_usec/1000):-1); 1 0 1
59 1 ▶ if(prv == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if(prv == 1) { 0 1 1
60 0 ▶ if(getsockopt(sockfd,SOL_SOCKET,SO_ERROR, &aerrno, &aerrno_len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(getsockopt(sockfd,SOL_SOCKET,SO_ERROR, &aerrno, &aerrno_len) == 0) { 0 0 0
61 0 ▶ if(aerrno == 0) goto good;
Branch: 1 branch
BranchTrueFalseTotal
if(aerrno == 0) goto good; 0 0 0
62 }
63 else
64 0 goto good;
65 0 result = -aerrno;
66 }
67 1 else result = -ETIMEDOUT;
68 }
69 1 close(sockfd);
70 1 return result;
71 }
72 0 good:
73 0 if(((flags = fcntl(sockfd, F_GETFL, 0)) == -1) ||
74 0 (fcntl(sockfd, F_SETFL, flags & ~O_NONBLOCK) == -1)) {
75 0 result = -errno;
76 0 close(sockfd);
77 0 return result;
78 }
79 0 ▶ if(timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if(timeout) { 0 0 0
80 0 setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, timeout, sizeof(*timeout));
81 0 setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, timeout, sizeof(*timeout));
82 }
83 0 return sockfd;
84 }
85  
86 0 static char *header() {
87 static char header[8];
88 0 header[0] = 'A';
89 0 header[1] = 'M';
90 0 header[2] = 'Q';
91 0 header[3] = 'P';
92 0 header[4] = 1;
93 0 header[5] = 1;
94 0 header[6] = AMQP_PROTOCOL_VERSION_MAJOR;
95 0 header[7] = AMQP_PROTOCOL_VERSION_MINOR;
96 0 return header;
97 }
98  
99 0 int amqp_send_header(amqp_connection_state_t state) {
100 0 return write(state->sockfd, header(), 8);
101 }
102  
103 0 int amqp_send_header_to(amqp_connection_state_t state,
104 amqp_output_fn_t fn,
105 void *context)
106 {
107 0 return fn(context, header(), 8);
108 }
109  
110 0 static amqp_bytes_t sasl_method_name(amqp_sasl_method_enum method) {
111 0 ▶ switch (method) {
Branch: 1 branch
BranchTrueFalseTotal
switch (method) { 0 0 0
112 0 case AMQP_SASL_METHOD_PLAIN: return (amqp_bytes_t) {.len = 5, .bytes = "PLAIN"};
113 0 default:
114 0 amqp_assert(0, "Invalid SASL method: %d", (int) method);
115 }
116 abort(); /* unreachable */
117 }
118  
119 0 static amqp_bytes_t sasl_response(amqp_pool_t *pool,
120 amqp_sasl_method_enum method,
121 va_list args)
122 {
123 amqp_bytes_t response;
124  
125 0 ▶ switch (method) {
Branch: 1 branch
BranchTrueFalseTotal
switch (method) { 0 0 0
126 0 case AMQP_SASL_METHOD_PLAIN: {
127 0 char *username = va_arg(args, char *);
128 0 size_t username_len = strlen(username);
129 0 char *password = va_arg(args, char *);
130 0 size_t password_len = strlen(password);
131 0 amqp_pool_alloc_bytes(pool, strlen(username) + strlen(password) + 2, &response);
132 0 *BUF_AT(response, 0) = 0;
133 0 memcpy(((char *) response.bytes) + 1, username, username_len);
134 0 *BUF_AT(response, username_len + 1) = 0;
135 0 memcpy(((char *) response.bytes) + username_len + 2, password, password_len);
136 0 break;
137 }
138 0 default:
139 0 amqp_assert(0, "Invalid SASL method: %d", (int) method);
140 }
141  
142 0 return response;
143 }
144  
145 0 amqp_boolean_t amqp_frames_enqueued(amqp_connection_state_t state) {
146 0 return (state->first_queued_frame != NULL);
147 }
148  
149 /*
150 * Check to see if we have data in our buffer. If this returns 1, we
151 * will avoid an immediate blocking read in amqp_simple_wait_frame.
152 */
153 11 amqp_boolean_t amqp_data_in_buffer(amqp_connection_state_t state) {
154 11 return (state->sock_inbound_offset < state->sock_inbound_limit);
155 }
156  
157 11 static int wait_frame_inner(amqp_connection_state_t state,
158 amqp_frame_t *decoded_frame)
159 {
160 0 while (1) {
161 int result;
162  
163 11 ▶ while (amqp_data_in_buffer(state)) {
Branch: 1 branch
BranchTrueFalseTotal
while (amqp_data_in_buffer(state)) { 0 11 11
164 amqp_bytes_t buffer;
165 0 buffer.len = state->sock_inbound_limit - state->sock_inbound_offset;
166 0 buffer.bytes = ((char *) state->sock_inbound_buffer.bytes) + state->sock_inbound_offset;
167 0 ▶ AMQP_CHECK_RESULT((result = amqp_handle_input(state, buffer, decoded_frame)));
Branch: 1 branch
BranchTrueFalseTotal
AMQP_CHECK_RESULT((result = amqp_handle_input(state, buffer, decoded_frame))); 0 0 0
168 0 state->sock_inbound_offset += result;
169  
170 0 ▶ if (decoded_frame->frame_type != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (decoded_frame->frame_type != 0) { 0 0 0
171 /* Complete frame was read. Return it. */
172 0 return 1;
173 }
174  
175 /* Incomplete or ignored frame. Keep processing input. */
176 0 ▶ assert(result != 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(result != 0); 0 0 0
177 }
178  
179 11 result = read(state->sockfd,
180 state->sock_inbound_buffer.bytes,
181 state->sock_inbound_buffer.len);
182 11 ▶ if (result < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (result < 0) { 11 0 11
183 11 return -errno;
184 }
185 0 ▶ if (result == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (result == 0) { 0 0 0
186 /* EOF. */
187 0 return 0;
188 }
189  
190 0 state->sock_inbound_limit = result;
191 0 state->sock_inbound_offset = 0;
192 }
193 }
194  
195 11 int amqp_simple_wait_frame(amqp_connection_state_t state,
196 amqp_frame_t *decoded_frame)
197 {
198 11 ▶ if (state->first_queued_frame != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->first_queued_frame != NULL) { 0 11 11
199 0 amqp_frame_t *f = (amqp_frame_t *) state->first_queued_frame->data;
200 0 state->first_queued_frame = state->first_queued_frame->next;
201 0 ▶ if (state->first_queued_frame == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->first_queued_frame == NULL) { 0 0 0
202 0 state->last_queued_frame = NULL;
203 }
204 0 *decoded_frame = *f;
205 0 return 1;
206 } else {
207 11 return wait_frame_inner(state, decoded_frame);
208 }
209 }
210  
211 0 int amqp_simple_wait_method(amqp_connection_state_t state,
212 amqp_channel_t expected_channel,
213 amqp_method_number_t expected_method,
214 amqp_method_t *output)
215 {
216 amqp_frame_t frame;
217  
218 0 ▶ AMQP_CHECK_EOF_RESULT(amqp_simple_wait_frame(state, &frame));
Branch: 1 branch
BranchTrueFalseTotal
AMQP_CHECK_EOF_RESULT(amqp_simple_wait_frame(state, &frame)); 0 0 0
219 0 ▶ amqp_assert(frame.channel == expected_channel,
Branch: 1 branch
BranchTrueFalseTotal
amqp_assert(frame.channel == expected_channel, 0 0 0
220 "Expected 0x%08X method frame on channel %d, got frame on channel %d",
221 expected_method,
222 expected_channel,
223 frame.channel);
224 0 ▶ amqp_assert(frame.frame_type == AMQP_FRAME_METHOD,
Branch: 1 branch
BranchTrueFalseTotal
amqp_assert(frame.frame_type == AMQP_FRAME_METHOD, 0 0 0
225 "Expected 0x%08X method frame on channel %d, got frame type %d",
226 expected_method,
227 expected_channel,
228 frame.frame_type);
229 0 ▶ amqp_assert(frame.payload.method.id == expected_method,
Branch: 1 branch
BranchTrueFalseTotal
amqp_assert(frame.payload.method.id == expected_method, 0 0 0
230 "Expected method ID 0x%08X on channel %d, got ID 0x%08X",
231 expected_method,
232 expected_channel,
233 frame.payload.method.id);
234 0 *output = frame.payload.method;
235 0 return 1;
236 }
237  
238 91 int amqp_send_method(amqp_connection_state_t state,
239 amqp_channel_t channel,
240 amqp_method_number_t id,
241 void *decoded)
242 {
243 amqp_frame_t frame;
244  
245 91 frame.frame_type = AMQP_FRAME_METHOD;
246 91 frame.channel = channel;
247 91 frame.payload.method.id = id;
248 91 frame.payload.method.decoded = decoded;
249 91 return amqp_send_frame(state, &frame);
250 }
251  
252 0 static int amqp_id_in_reply_list( amqp_method_number_t expected, amqp_method_number_t *list )
253 {
254 0 ▶ while ( *list != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( *list != 0 ) { 0 0 0
255 0 ▶ if ( *list == expected ) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if ( *list == expected ) return 1; 0 0 0
256 0 list++;
257 }
258 0 return 0;
259 }
260  
261 76 amqp_rpc_reply_t amqp_simple_rpc(amqp_connection_state_t state,
262 amqp_channel_t channel,
263 amqp_method_number_t request_id,
264 amqp_method_number_t *expected_reply_ids,
265 void *decoded_request_method)
266 {
267 int status;
268 amqp_rpc_reply_t result;
269  
270 76 memset(&result, 0, sizeof(result));
271  
272 76 status = amqp_send_method(state, channel, request_id, decoded_request_method);
273 76 ▶ if (status < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status < 0) { 0 76 76
274 76 result.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
275 76 result.library_errno = -status;
276 76 return result;
277 }
278  
279 {
280 amqp_frame_t frame;
281  
282 0 retry:
283 0 status = wait_frame_inner(state, &frame);
284 0 ▶ if (status <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (status <= 0) { 0 0 0
285 0 result.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
286 0 result.library_errno = -status;
287 0 return result;
288 }
289  
290 /*
291 * We store the frame for later processing unless it's something
292 * that directly affects us here, namely a method frame that is
293 * either
294 * - on the channel we want, and of the expected type, or
295 * - on the channel we want, and a channel.close frame, or
296 * - on channel zero, and a connection.close frame.
297 */
298 0 ▶ if (!( (frame.frame_type == AMQP_FRAME_METHOD) &&
Branch: 1 branch
BranchTrueFalseTotal
if (!( (frame.frame_type == AMQP_FRAME_METHOD) && 0 0 0
299 0 ( ((frame.channel == channel) &&
300 0 ((amqp_id_in_reply_list(frame.payload.method.id, expected_reply_ids)) ||
301 0 ▶ (frame.payload.method.id == AMQP_CHANNEL_CLOSE_METHOD)))
Branch: 1 branch
BranchTrueFalseTotal
(frame.payload.method.id == AMQP_CHANNEL_CLOSE_METHOD))) 0 0 0
302 0 ||
303 0 ▶ ((frame.channel == 0) &&
Branch: 1 branch
BranchTrueFalseTotal
((frame.channel == 0) && 0 0 0
304 0 ▶ (frame.payload.method.id == AMQP_CONNECTION_CLOSE_METHOD)) ) ))
Branch: 1 branch
BranchTrueFalseTotal
(frame.payload.method.id == AMQP_CONNECTION_CLOSE_METHOD)) ) )) 0 0 0
305 {
306 0 amqp_frame_t *frame_copy = amqp_pool_alloc(&state->decoding_pool, sizeof(amqp_frame_t));
307 0 amqp_link_t *link = amqp_pool_alloc(&state->decoding_pool, sizeof(amqp_link_t));
308  
309 0 *frame_copy = frame;
310  
311 0 link->next = NULL;
312 0 link->data = frame_copy;
313  
314 0 ▶ if (state->last_queued_frame == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->last_queued_frame == NULL) { 0 0 0
315 0 state->first_queued_frame = link;
316 } else {
317 0 state->last_queued_frame->next = link;
318 }
319 0 state->last_queued_frame = link;
320  
321 0 goto retry;
322 }
323  
324 0 result.reply_type = (amqp_id_in_reply_list(frame.payload.method.id, expected_reply_ids))
325 ? AMQP_RESPONSE_NORMAL
326 0 ▶ : AMQP_RESPONSE_SERVER_EXCEPTION;
Branch: 1 branch
BranchTrueFalseTotal
: AMQP_RESPONSE_SERVER_EXCEPTION; 0 0 0
327  
328 0 result.reply = frame.payload.method;
329 0 return result;
330 }
331 }
332  
333 0 static int amqp_login_inner(amqp_connection_state_t state,
334 int channel_max,
335 int frame_max,
336 int heartbeat,
337 amqp_sasl_method_enum sasl_method,
338 va_list vl)
339 {
340 struct timeval hb;
341 amqp_method_t method;
342 uint32_t server_frame_max;
343 uint16_t server_channel_max;
344 uint16_t server_heartbeat;
345  
346 0 ▶ if(heartbeat != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(heartbeat != 0) { 0 0 0
347 0 hb.tv_sec = 2*heartbeat; hb.tv_usec = 0;
348 0 setsockopt(state->sockfd, SOL_SOCKET, SO_RCVTIMEO, &hb, sizeof(hb));
349 0 setsockopt(state->sockfd, SOL_SOCKET, SO_SNDTIMEO, &hb, sizeof(hb));
350 }
351  
352 0 amqp_send_header(state);
353  
354 0 ▶ AMQP_CHECK_EOF_RESULT(amqp_simple_wait_method(state, 0, AMQP_CONNECTION_START_METHOD, &method));
Branch: 1 branch
BranchTrueFalseTotal
AMQP_CHECK_EOF_RESULT(amqp_simple_wait_method(state, 0, AMQP_CONNECTION_START_METHOD, &method)); 0 0 0
355 {
356 0 amqp_connection_start_t *s = (amqp_connection_start_t *) method.decoded;
357 0 ▶ if ((s->version_major != AMQP_PROTOCOL_VERSION_MAJOR) ||
Branch: 1 branch
BranchTrueFalseTotal
if ((s->version_major != AMQP_PROTOCOL_VERSION_MAJOR) || 0 0 0
358 0 ▶ (s->version_minor != AMQP_PROTOCOL_VERSION_MINOR)) {
Branch: 1 branch
BranchTrueFalseTotal
(s->version_minor != AMQP_PROTOCOL_VERSION_MINOR)) { 0 0 0
359 0 return -EPROTOTYPE;
360 }
361  
362 /* TODO: check that our chosen SASL mechanism is in the list of
363 acceptable mechanisms. Or even let the application choose from
364 the list! */
365 }
366  
367 {
368 0 amqp_bytes_t response_bytes = sasl_response(&state->decoding_pool, sasl_method, vl);
369 0 amqp_connection_start_ok_t s =
370 (amqp_connection_start_ok_t) {
371 .client_properties = {.num_entries = 0, .entries = NULL},
372 0 .mechanism = sasl_method_name(sasl_method),
373 .response = response_bytes,
374 .locale = {.len = 5, .bytes = "en_US"}
375 };
376 0 ▶ AMQP_CHECK_RESULT(amqp_send_method(state, 0, AMQP_CONNECTION_START_OK_METHOD, &s));
Branch: 1 branch
BranchTrueFalseTotal
AMQP_CHECK_RESULT(amqp_send_method(state, 0, AMQP_CONNECTION_START_OK_METHOD, &s)); 0 0 0
377 }
378  
379 0 amqp_release_buffers(state);
380  
381 0 ▶ AMQP_CHECK_EOF_RESULT(amqp_simple_wait_method(state, 0, AMQP_CONNECTION_TUNE_METHOD, &method));
Branch: 1 branch
BranchTrueFalseTotal
AMQP_CHECK_EOF_RESULT(amqp_simple_wait_method(state, 0, AMQP_CONNECTION_TUNE_METHOD, &method)); 0 0 0
382 {
383 0 amqp_connection_tune_t *s = (amqp_connection_tune_t *) method.decoded;
384 0 server_channel_max = s->channel_max;
385 0 server_frame_max = s->frame_max;
386 0 server_heartbeat = s->heartbeat;
387 }
388  
389 0 ▶ if (server_channel_max != 0 && server_channel_max < channel_max) {
Branch: 2 branches
BranchTrueFalseTotal
if (server_channel_max != 0 && server_channel_max < channel_max) { 0 0 0
if (server_channel_max != 0 && server_channel_max < channel_max) { 0 0 0
390 0 channel_max = server_channel_max;
391 }
392  
393 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
394 0 frame_max = server_frame_max;
395 }
396  
397 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
398 0 heartbeat = server_heartbeat;
399 }
400 0 ▶ if(heartbeat != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(heartbeat != 0) { 0 0 0
401 0 hb.tv_sec = 2*heartbeat; hb.tv_usec = 0;
402 0 setsockopt(state->sockfd, SOL_SOCKET, SO_RCVTIMEO, &hb, sizeof(hb));
403 0 setsockopt(state->sockfd, SOL_SOCKET, SO_SNDTIMEO, &hb, sizeof(hb));
404 }
405  
406 0 ▶ AMQP_CHECK_RESULT(amqp_tune_connection(state, channel_max, frame_max, heartbeat));
Branch: 1 branch
BranchTrueFalseTotal
AMQP_CHECK_RESULT(amqp_tune_connection(state, channel_max, frame_max, heartbeat)); 0 0 0
407  
408 {
409 0 amqp_connection_tune_ok_t s =
410 (amqp_connection_tune_ok_t) {
411 .channel_max = channel_max,
412 .frame_max = frame_max,
413 .heartbeat = heartbeat
414 };
415 0 ▶ AMQP_CHECK_RESULT(amqp_send_method(state, 0, AMQP_CONNECTION_TUNE_OK_METHOD, &s));
Branch: 1 branch
BranchTrueFalseTotal
AMQP_CHECK_RESULT(amqp_send_method(state, 0, AMQP_CONNECTION_TUNE_OK_METHOD, &s)); 0 0 0
416 }
417  
418 0 amqp_release_buffers(state);
419  
420 0 return 1;
421 }
422  
423 0 amqp_rpc_reply_t amqp_login(amqp_connection_state_t state,
424 char const *vhost,
425 int channel_max,
426 int frame_max,
427 int heartbeat,
428 amqp_sasl_method_enum sasl_method,
429 ...)
430 {
431 va_list vl;
432 amqp_rpc_reply_t result;
433  
434 0 va_start(vl, sasl_method);
435  
436 0 amqp_login_inner(state, channel_max, frame_max, heartbeat, sasl_method, vl);
437  
438 {
439 0 amqp_connection_open_t s =
440 (amqp_connection_open_t) {
441 0 .virtual_host = amqp_cstring_bytes(vhost),
442 .capabilities = {.len = 0, .bytes = NULL},
443 .insist = 1
444 };
445 0 amqp_method_number_t replies[] = { AMQP_CONNECTION_OPEN_OK_METHOD, 0 };
446 0 result = amqp_simple_rpc(state,
447 0,
448 AMQP_CONNECTION_OPEN_METHOD,
449 (amqp_method_number_t *) &replies,
450 &s);
451 0 ▶ if (result.reply_type != AMQP_RESPONSE_NORMAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (result.reply_type != AMQP_RESPONSE_NORMAL) { 0 0 0
452 0 return result;
453 }
454 }
455 0 amqp_maybe_release_buffers(state);
456  
457 0 va_end(vl);
458  
459 0 result.reply_type = AMQP_RESPONSE_NORMAL;
460 0 result.reply.id = 0;
461 0 result.reply.decoded = NULL;
462 0 result.library_errno = 0;
463 0 return result;
464 }
« amqp_framing.c ↑ Summary amqp_table.c »