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
|
/* SPDX-License-Identifier: GPL-2.0 */
/** \file sideband.c Implementation of the sideband API. */
#include "para.h"
#include "error.h"
#include "portable_io.h"
#include "string.h"
#include "sideband.h"
/* Each sideband packet consists of a header and a data part. */
#define SIDEBAND_HEADER_SIZE 5
struct sb_context {
char header[SIDEBAND_HEADER_SIZE];
size_t bytes_dispatched; /* including header */
sb_transformation trafo;
void *trafo_context;
struct sb_buffer sbb;
size_t max_size;
bool dont_free;
};
/**
* Prepare to receive a sideband packet.
*
* \param max_size Do not allocate more than this many bytes.
* \param t Optional sideband transformation.
* \param trafo_context Passed verbatim to the transformation.
*
* If a subsequent call to \ref sb_received() indicates a packet size larger
* than the specified maximal size, the input will be rejected as invalid. The
* trafo context is ignored if the transformation pointer is NULL.
*
* \return An opaque sideband handle. This function never fails.
*/
struct sb_context *sb_new_recv(size_t max_size, sb_transformation t,
void *trafo_context)
{
struct sb_context *c = zalloc(sizeof(*c));
c->max_size = max_size;
c->trafo = t;
c->trafo_context = trafo_context;
return c;
}
/* Return true if iov points to a non-empty buffer. */
static bool iov_empty(const struct iovec *iov)
{
return !iov || !iov->iov_base || iov->iov_len == 0;
}
/**
* Prepare to write a sideband packet.
*
* \param sbb Data and meta data to send.
* \param dont_free Do not try to deallocate the sideband buffer.
* \param t See \ref sb_new_recv().
* \param trafo_context See \ref sb_new_recv().
*
* It's OK to supply a zero-sized sideband buffer. In this case only the band
* designator is sent through the sideband channel. Otherwise, if dont_free
* is false, the sideband buffer is freed after the data has been sent.
*
* \return See \ref sb_new_recv().
*/
struct sb_context *sb_new_send(struct sb_buffer *sbb, bool dont_free,
sb_transformation t, void *trafo_context)
{
struct sb_context *c = zalloc(sizeof(*c));
struct iovec src, dst, *srcp, *dstp;
assert(sbb);
c->trafo = t;
c->trafo_context = trafo_context;
c->dont_free = dont_free;
c->sbb = *sbb;
write_u32(c->header, sbb->iov.iov_len);
write_u8(c->header + 4, sbb->band);
if (!t)
goto out;
src = (typeof(src)){.iov_base = c->header, .iov_len = SIDEBAND_HEADER_SIZE};
t(&src, &dst, trafo_context);
if (src.iov_base != dst.iov_base) {
memcpy(c->header, dst.iov_base, SIDEBAND_HEADER_SIZE);
free(dst.iov_base);
}
if (iov_empty(&sbb->iov))
goto out;
srcp = &sbb->iov;
dstp = &c->sbb.iov;
t(srcp, dstp, trafo_context);
if (srcp->iov_base != dstp->iov_base) {
if (!c->dont_free)
free(srcp->iov_base);
c->dont_free = false;
}
out:
return c;
}
/**
* Deallocate all memory associated with a sideband handle.
*
* \param c The sideband handle.
*
* The handle is a pointer which was returned by an earlier call to \ref
* sb_new_recv() or \ref sb_new_send(). It's OK to pass NULL, the function
* does nothing in this case.
*/
void sb_free(struct sb_context *c)
{
if (!c)
return;
if (!c->dont_free)
free(c->sbb.iov.iov_base);
free(c);
}
/**
* Obtain pointer(s) to the sideband send buffer(s).
*
* This function initializes the buffer pointers and size values of the given
* I/O vector array for a subsequent call to \ref xwritev() or similar, keeping
* track of the amount of data that has already been written by earlier calls.
*
* \param c As returned by \ref sb_new_send().
* \param iov Identifies header and data.
*
* \return The number of buffers that need to be send to complete the sideband
* packet. Either 1 or 2, no errors possible.
*
* \sa \ref sb_get_recv_buffer(), \ref sb_sent().
*/
int sb_get_send_buffers(struct sb_context *c, struct iovec iov[2])
{
struct sb_buffer *sbb = &c->sbb;
size_t n = c->bytes_dispatched;
if (n < SIDEBAND_HEADER_SIZE) {
iov[0].iov_base = c->header + n;
iov[0].iov_len = SIDEBAND_HEADER_SIZE - n;
if (iov_empty(&sbb->iov))
goto out;
iov[1] = sbb->iov;
return 2;
}
n -= SIDEBAND_HEADER_SIZE;
assert(n < sbb->iov.iov_len);
iov[0].iov_base = sbb->iov.iov_base + n;
iov[0].iov_len = sbb->iov.iov_len - n;
out:
iov[1].iov_base = NULL;
iov[1].iov_len = 0;
return 1;
}
/**
* Update the sideband context after data has been sent.
*
* \param c As returned by \ref sb_new_send().
* \param nbytes The number of sent bytes.
*
* \return False if more data must be sent to complete the sideband transfer,
* true if the transfer is complete. In this case all resources are freed and
* the sideband handle must not be used any more.
*/
bool sb_sent(struct sb_context *c, size_t nbytes)
{
struct sb_buffer *sbb = &c->sbb;
size_t sz = SIDEBAND_HEADER_SIZE + sbb->iov.iov_len;
assert(c->bytes_dispatched + nbytes <= sz);
c->bytes_dispatched += nbytes;
if (c->bytes_dispatched < sz)
return false;
sb_free(c);
return true;
}
/**
* Obtain a pointer to the next sideband read buffer.
*
* \param c As returned by \ref sb_new_recv().
* \param iov Result IO vector.
*
* This sets the I/O vector pointer to the buffer for the next chunk of
* received data and the I/O vector length to the buffer size.
*/
void sb_get_recv_buffer(struct sb_context *c, struct iovec *iov)
{
struct sb_buffer *sbb = &c->sbb;
size_t n = c->bytes_dispatched;
if (n < SIDEBAND_HEADER_SIZE) {
iov->iov_base = c->header + n;
iov->iov_len = SIDEBAND_HEADER_SIZE - n;
return;
}
n -= SIDEBAND_HEADER_SIZE;
assert(sbb->iov.iov_base);
assert(sbb->iov.iov_len > n);
iov->iov_base = sbb->iov.iov_base + n;
iov->iov_len = sbb->iov.iov_len - n;
}
/**
* Update the sideband context after data has been received.
*
* \param c Handle returned by \ref sb_new_recv().
* \param nbytes The number of bytes that have been received.
* \param result The received sideband packet, including the band designator.
*
* \return Negative on errors, zero if more data needs to be read to complete
* this sideband packet, one if the sideband packet has been received
* completely.
*
* Only if the function returns one, the values if the result sideband buffer
* are defined.
*/
int sb_received(struct sb_context *c, size_t nbytes, struct sb_buffer *result)
{
struct sb_buffer *sbb = &c->sbb;
size_t n = c->bytes_dispatched,
sz = SIDEBAND_HEADER_SIZE + sbb->iov.iov_len;
assert(n + nbytes <= sz);
c->bytes_dispatched += nbytes;
if (c->bytes_dispatched < SIDEBAND_HEADER_SIZE)
return 0;
if (n >= SIDEBAND_HEADER_SIZE) { /* header has already been received */
if (c->bytes_dispatched < sz) /* need to recv more body data */
return 0;
/* received everything, decrypt and return sbb */
if (c->trafo) {
struct iovec dst;
c->trafo(&sbb->iov, &dst, c->trafo_context);
if (sbb->iov.iov_base != dst.iov_base) {
free(sbb->iov.iov_base);
sbb->iov.iov_base = dst.iov_base;
}
}
((char *)(sbb->iov.iov_base))[sbb->iov.iov_len] = '\0';
goto success;
}
/* header has been received, decrypt and decode it */
if (c->trafo) { /* decrypt */
struct iovec dst, src = (typeof(src)) {
.iov_base = c->header,
.iov_len = SIDEBAND_HEADER_SIZE
};
c->trafo(&src, &dst, c->trafo_context);
if (src.iov_base != dst.iov_base) {
memcpy(c->header, dst.iov_base,
SIDEBAND_HEADER_SIZE);
free(dst.iov_base);
}
}
/* Decode header, setup sbb */
sbb->iov.iov_len = read_u32(c->header);
sbb->band = read_u8(c->header + 4);
sbb->iov.iov_base = NULL;
if (sbb->iov.iov_len == 0) /* zero-sized msg */
goto success;
if (c->max_size > 0 && sbb->iov.iov_len > c->max_size) {
PARA_ERROR_LOG("packet too big (is %zu, max %zu)\n",
sbb->iov.iov_len, c->max_size);
return -E_SB_PACKET_SIZE;
}
/*
* We like to reserve one extra byte for the terminating NULL
* character. However, we must make sure the +1 below does not
* overflow iov_len.
*/
if (sbb->iov.iov_len == (size_t)-1)
return -E_SB_PACKET_SIZE;
sbb->iov.iov_base = alloc(sbb->iov.iov_len + 1);
return 0; /* ready to read body */
success:
*result = c->sbb;
free(c);
return 1;
}
|