Debian Codegraph @

c/src/core/framing.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22
23 #include "framing.h"
24
25 #include "engine-internal.h"
26 #include "util.h"
27
28 #include <assert.h>
29
30 static inline void pn_do_tx_trace(pn_logger_t *logger, uint16_t ch, pn_bytes_t frame)
31 {
32 if (PN_SHOULD_LOG(logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_FRAME) ) {
33 if (frame.size==0) {
34 pn_logger_logf(logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_FRAME, "%u -> (EMPTY FRAME)", ch);
35 } else {
36 pni_logger_log_msg_frame(logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_FRAME, frame, "%u -> ", ch);
37 }
38 }
39 }
40
41 static inline void pn_do_rx_trace(pn_logger_t *logger, uint16_t ch, pn_bytes_t frame)
42 {
43 if (PN_SHOULD_LOG(logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_FRAME) ) {
44 if (frame.size==0) {
45 pn_logger_logf(logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_FRAME, "%u <- (EMPTY FRAME)", ch);
46 } else {
47 pni_logger_log_msg_frame(logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_FRAME, frame, "%u <- ", ch);
48 }
49 }
50 }
51
52 static inline void pn_do_raw_trace(pn_logger_t *logger, pn_buffer_t *output, size_t size)
53 {
54 PN_LOG_RAW(logger, PN_SUBSYSTEM_IO, PN_LEVEL_RAW, output, size);
55 }
56
57 ssize_t pn_read_frame(pn_frame_t *frame, const char *bytes, size_t available, uint32_t max, pn_logger_t *logger)
58 {
59 if (available < AMQP_HEADER_SIZE) return 0;
60 uint32_t size = pni_read32(&bytes[0]);
61 if (max && size > max) return PN_ERR;
62 if (available < size) return 0;
63 unsigned int doff = 4 * (uint8_t)bytes[4];
64 if (doff < AMQP_HEADER_SIZE || doff > size) return PN_ERR;
65
66 frame->frame_payload0 = (pn_bytes_t){.size=size-doff, .start=bytes+doff};
67 frame->frame_payload1 = (pn_bytes_t){.size=0,.start=NULL};
68 frame->extended = (pn_bytes_t){.size=doff-AMQP_HEADER_SIZE, .start=bytes+AMQP_HEADER_SIZE};
69 frame->type = bytes[5];
70 frame->channel = pni_read16(&bytes[6]);
71
72 pn_do_rx_trace(logger, frame->channel, frame->frame_payload0);
73
74 return size;
75 }
76
77 size_t pn_write_frame(pn_buffer_t* buffer, pn_frame_t frame, pn_logger_t *logger)
78 {
79 size_t size = AMQP_HEADER_SIZE + frame.extended.size + frame.frame_payload0.size + frame.frame_payload1.size;
80 if (size <= pn_buffer_available(buffer))
81 {
82 // Prepare header
83 char bytes[8];
84 pni_write32(&bytes[0], size);
85 int doff = (frame.extended.size + AMQP_HEADER_SIZE - 1)/4 + 1;
86 bytes[4] = doff;
87 bytes[5] = frame.type;
88 pni_write16(&bytes[6], frame.channel);
89
90 // Write header then rest of frame
91 pn_buffer_append(buffer, bytes, 8);
92 pn_buffer_append(buffer, frame.extended.start, frame.extended.size);
93
94 // Don't mess with the buffer unless we are logging frame traces to avoid
95 // shuffling the buffer unnecessarily.
96 if (PN_SHOULD_LOG(logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_FRAME) ) {
97 // Get current buffer pointer so we can trace dump performative and payload together
98 pn_bytes_t smem = pn_buffer_bytes(buffer);
99 pn_buffer_append(buffer, frame.frame_payload0.start, frame.frame_payload0.size);
100 pn_buffer_append(buffer, frame.frame_payload1.start, frame.frame_payload1.size);
101 pn_bytes_t emem = pn_buffer_bytes(buffer);
102
103 // The buffer can't have moved
104 assert(smem.start==emem.start);
105 pn_bytes_t frame_payload = {.size=emem.size-smem.size, .start=smem.start+smem.size};
106 pn_do_tx_trace(logger, frame.channel, frame_payload);
107 } else {
108 pn_buffer_append(buffer, frame.frame_payload0.start, frame.frame_payload0.size);
109 pn_buffer_append(buffer, frame.frame_payload1.start, frame.frame_payload1.size);
110 }
111 pn_do_raw_trace(logger, buffer, AMQP_HEADER_SIZE+frame.extended.size+frame.frame_payload0.size+frame.frame_payload1.size);
112
113 return size;
114 } else {
115 return 0;
116 }
117 }
118
119 static inline void pn_post_frame(pn_buffer_t *output, pn_logger_t *logger, uint8_t type, uint16_t ch, pn_bytes_t performative, pn_bytes_t payload)
120 {
121 pn_frame_t frame = {
122 .type = type,
123 .channel = ch,
124 .frame_payload0 = performative,
125 .frame_payload1 = payload
126 };
127 pn_buffer_ensure(output, AMQP_HEADER_SIZE+frame.extended.size+frame.frame_payload0.size+frame.frame_payload1.size);
128 pn_write_frame(output, frame, logger);
129 }
130
131 int pn_framing_send_amqp(pn_transport_t *transport, uint16_t ch, pn_bytes_t performative)
132 {
133 if (!performative.start)
134 return PN_ERR;
135
136 pn_post_frame(transport->output_buffer, &transport->logger, AMQP_FRAME_TYPE, ch, performative, (pn_bytes_t){0, NULL});
137 transport->output_frames_ct += 1;
138 return 0;
139 }
140
141 int pn_framing_send_amqp_with_payload(pn_transport_t *transport, uint16_t ch, pn_bytes_t performative, pn_bytes_t payload)
142 {
143 if (!performative.start)
144 return PN_ERR;
145
146 pn_post_frame(transport->output_buffer, &transport->logger, AMQP_FRAME_TYPE, ch, performative, payload);
147 transport->output_frames_ct += 1;
148 return 0;
149 }
150
151 int pn_framing_send_sasl(pn_transport_t *transport, pn_bytes_t performative)
152 {
153 if (!performative.start)
154 return PN_ERR;
155
156 // All SASL frames go on channel 0
157 pn_post_frame(transport->output_buffer, &transport->logger, SASL_FRAME_TYPE, 0, performative, (pn_bytes_t){0, NULL});
158 transport->output_frames_ct += 1;
159 return 0;
160 }