Debian Codegraph @

src/common/cockpitframe.c

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include "cockpitframe.h"
9
10 #include <assert.h>
11 #include <errno.h>
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #define MAX_FRAME_SIZE_BYTES 8
19
20 /**
21 * cockpit_frame_parse:
22 * @input: An buffer of bytes
23 * @length: The length of @input buffer
24 * @consumed: Number of bytes consumed from @input
25 *
26 * Parse message framing length string from the top
27 * of the @input buffer. These are used by Cockpit transport framing
28 * over a stream based protocol.
29 *
30 * Returns: The length, zero if more data is needed, or -1 if an error.
31 */
32 ssize_t
33 cockpit_frame_parse (unsigned char *input,
34 size_t length,
35 size_t *consumed)
36 {
37 size_t size = 0;
38 size_t i;
39
40 assert (input != NULL || length == 0);
41
42 size = 0;
43 for (i = 0; i < length; i++)
44 {
45 /* Check invalid characters, prevent integer overflow, limit max length */
46 if (i >= MAX_FRAME_SIZE_BYTES || (char)(input[i]) < '0' || (char)(input[i]) > '9')
47 break;
48 size *= 10;
49 size += (char)(input[i]) - '0';
50 }
51
52 /* Want more data */
53 if (i == length)
54 return 0;
55
56 /* Improperly formatted if any of the following cases:
57 * - no digits read
58 * - digits not followed by newline
59 * - size had a leading zero
60 */
61 if (size == 0 || input[i] != '\n' || input[0] == '0')
62 return -1;
63
64 if (consumed)
65 *consumed = i + 1;
66 return size;
67 }
68
69 ssize_t
70 cockpit_fd_write_all (int fd,
71 unsigned char *data,
72 size_t length)
73 {
74 ssize_t written = 0;
75 ssize_t res;
76
77 assert (data != NULL || length == 0);
78
79 while (length > 0)
80 {
81 res = write (fd, data, length);
82 if (res < 0)
83 {
84 if (errno != EAGAIN && errno != EINTR)
85 return -1;
86 }
87 else
88 {
89 data += res;
90 length -= res;
91 written += res;
92 }
93 }
94
95 return written;
96 }
97
98 ssize_t
99 cockpit_frame_write (int fd,
100 unsigned char *input,
101 size_t length)
102 {
103 char *prefix = NULL;
104 ssize_t ret = -1;
105 int errn = 0;
106
107 assert (length > 0);
108 assert (input != NULL);
109
110 if (asprintf (&prefix, "%u\n", (unsigned int)length) < 0)
111 {
112 errn = ENOMEM;
113 goto out;
114 }
115
116 ret = cockpit_fd_write_all (fd, (unsigned char *)prefix, strlen (prefix));
117 if (ret > 0)
118 ret = cockpit_fd_write_all (fd, input, length);
119 if (ret < 0)
120 errn = errno;
121
122 out:
123 free (prefix);
124 if (ret < 0)
125 errno = errn;
126 return ret;
127 }
128
129 /* read_exactly:
130 * @fd: a blocking file descriptor
131 * @buffer: where to read to
132 * @required_size: the exact number of bytes to read
133 * @was_eof: if EOF was encountered
134 *
135 * Reads exactly @required_size bytes from @fd into @buffer. If the
136 * correct number of bytes are read, then %TRUE is returned.
137 *
138 * On failure, returns %FALSE. If the failure was due to an underlying
139 * read error, then this error will be stored into errno. If the
140 * failure was due to an incorrect number of bytes being read, then
141 * errno will be set to %EBADMSG.
142 *
143 * The only permitted exception is if @was_eof is non-%NULL. In that
144 * case, if exactly 0 bytes are read from @fd (ie: EOF at the start of
145 * the message) then @was_eof will be set to %TRUE and the call will
146 * return successfully. Otherwise, @was_eof will be set to %FALSE on
147 * successful completion.
148 *
149 * If the required number of bytes couldn't be read, then any number of
150 * bytes may have been read from @fd. The only reasonable thing to do
151 * at this point is to treat the connection as broken, and close @fd.
152 * In particular, this is why @fd should never be non-blocking.
153 */
154 static bool
155 read_exactly (int fd,
156 unsigned char *buffer,
157 size_t required_size,
158 bool *was_eof)
159 {
160 size_t offset = 0;
161
162 while (offset < required_size)
163 {
164 ssize_t n = read (fd, buffer + offset, required_size - offset);
165 if (n == -1)
166 {
167 if (errno == EINTR)
168 continue;
169
170 if (errno != ECONNRESET)
171 return false;
172
173 /* ECONNRESET is treated as EOF */
174 n = 0;
175 }
176
177 if (n == 0)
178 {
179 if (was_eof != NULL && offset == 0)
180 {
181 *was_eof = true;
182 return true;
183 }
184
185 errno = EBADMSG;
186 return false;
187 }
188
189 offset += n;
190 }
191
192 if (was_eof)
193 *was_eof = false;
194
195 return true;
196 }
197
198 ssize_t
199 cockpit_frame_read (int fd,
200 unsigned char **output)
201 {
202 /* We first need to read the size of the frame, followed by the
203 * content of the frame. We want to do this efficiently as possible,
204 * while avoiding to read() more than the frame (since we can't put
205 * bytes back). MSG_PEEK is also not always available, since we're
206 * often talking to a pipe.
207 *
208 * Fortunately, we have a reasonable approach for this.
209 *
210 * Empty frames are invalid (cockpit_frame_parse() rejects size == 0),
211 * so the smallest possible frame has a length of at least 3: the
212 * single-digit size, the newline, then the single byte of body.
213 * Therefore it's always safe to read 3 bytes ("the initial read").
214 *
215 * Conveniently, reading three bytes is also always enough to tell us
216 * how many bytes it's safe to read in order to determine the size of
217 * the frame:
218 *
219 * - if we read a digit or two, followed by a newline, then we
220 * already know the size of the entire frame
221 *
222 * - if we read three digits, we know that the frame body is at
223 * least 100 bytes long. Since the maximum size of the length
224 * field is 8 (+1 for newline), we can safely read this entire
225 * amount.
226 *
227 * - if we get something other than digits or newlines, it's an
228 * error
229 */
230 size_t n_read = 3;
231 unsigned char headerbuf[MAX_FRAME_SIZE_BYTES + 1];
232 bool eof;
233
234 if (!read_exactly (fd, headerbuf, n_read, &eof))
235 return -1;
236
237 if (eof)
238 {
239 if (output)
240 *output = NULL;
241 return 0;
242 }
243
244 size_t n_consumed;
245 ssize_t size = cockpit_frame_parse (headerbuf, n_read, &n_consumed);
246 if (size == 0)
247 {
248 /* cockpit_frame_parse() asked to read more data. As explained
249 * above, it's safe to read the rest of the buffer now (6 bytes).
250 * This should always result in a defined (non-zero) result.
251 */
252 if (!read_exactly (fd, headerbuf + n_read, sizeof headerbuf - n_read, NULL))
253 return -1;
254
255 n_read = sizeof headerbuf;
256 size = cockpit_frame_parse (headerbuf, n_read, &n_consumed);
257 assert (size != 0);
258 }
259
260 if (size == -1)
261 {
262 errno = EBADMSG;
263 return -1;
264 }
265
266 /* We now have size equal to the number of bytes we need to return. Add an extra byte for a NUL terminator,
267 * to be friendly to callers for text frames. */
268 unsigned char *buffer = calloc (1, size + 1);
269 if (buffer == NULL)
270 return -1; /* ENOMEM */
271
272 /* Copy the non-consumed bytes from the header (might be zero) */
273 size_t bytes_from_header = n_read - n_consumed;
274 memcpy (buffer, headerbuf + n_consumed, bytes_from_header);
275
276 /* Get the rest of the body (might be zero) */
277 if (!read_exactly (fd, buffer + bytes_from_header, size - bytes_from_header, NULL))
278 {
279 free (buffer);
280 return -1;
281 }
282
283 if (output)
284 *output = buffer;
285 else
286 free (buffer);
287
288 return size;
289 }