1 /* Copyright (C) 2012 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file sideband.c Implementation of the sideband API. */
9 #include "portable_io.h"
13 /** Each sideband packet consists of a header and a data part. */
14 #define SIDEBAND_HEADER_SIZE 5
17 char header[SIDEBAND_HEADER_SIZE];
18 size_t bytes_dispatched; /* including header */
19 sb_transformation trafo;
27 * Prepare to receive a sideband packet.
29 * \param max_size Do not allocate more than this many bytes.
30 * \param t Optional sideband transformation.
31 * \param trafo_context Passed verbatim to \a t.
33 * \a trafo_context is ignored if \a t is \p NULL.
35 * \return An opaque sideband handle.
37 struct sb_context *sb_new_recv(size_t max_size, sb_transformation t,
40 struct sb_context *c = zalloc(sizeof(*c));
42 c->max_size = max_size;
44 c->trafo_context = trafo_context;
49 * Prepare to write a sideband packet.
51 * \param sbb Data and meta data to send.
52 * \param dont_free Do not try to deallocate the sideband buffer.
53 * \param t See \ref sb_new_recv().
54 * \param trafo_context See \ref sb_new_recv().
56 * It's OK to supply a zero-sized buffer in \a sbb. In this case only the band
57 * designator is sent through the sideband channel. Otherwise, if \a dont_free
58 * is false, the buffer of \a sbb is freed after the data has been sent.
60 * \return See \ref sb_new_recv().
62 struct sb_context *sb_new_send(struct sb_buffer *sbb, bool dont_free,
63 sb_transformation t, void *trafo_context)
65 struct sb_context *c = zalloc(sizeof(*c));
66 struct iovec src, dst, *srcp, *dstp;
70 c->trafo_context = trafo_context;
71 c->dont_free = dont_free;
73 write_u32(c->header, sbb->iov.iov_len);
74 write_u8(c->header + 4, sbb->band);
77 src = (typeof(src)){.iov_base = c->header, .iov_len = SIDEBAND_HEADER_SIZE};
78 t(&src, &dst, trafo_context);
79 if (src.iov_base != dst.iov_base) {
80 memcpy(c->header, dst.iov_base, SIDEBAND_HEADER_SIZE);
83 if (!iov_valid(&sbb->iov))
87 t(srcp, dstp, trafo_context);
88 if (srcp->iov_base != dstp->iov_base) {
98 * Deallocate all memory associated with a sideband handle.
100 * \param c The sideband handle.
102 * \a c must point to a handle previously returned by \ref sb_new_recv() or
103 * \ref sb_new_send(). It \a c is \p NULL, the function does nothing.
105 void sb_free(struct sb_context *c)
110 free(c->sbb.iov.iov_base);
115 * Obtain pointer(s) to the sideband send buffer(s).
117 * \param c The sideband handle.
118 * \param iov Array of two I/O vectors.
120 * \return The number of buffers that need to be sent, either 1 or 2.
122 * This function fills out the buffers described by \a iov. The result can be
123 * passed to \ref xwritev() or similar.
125 * \sa \ref sb_get_recv_buffer().
127 int sb_get_send_buffers(struct sb_context *c, struct iovec iov[2])
129 struct sb_buffer *sbb = &c->sbb;
130 size_t n = c->bytes_dispatched;
132 if (n < SIDEBAND_HEADER_SIZE) {
133 iov[0].iov_base = c->header + n;
134 iov[0].iov_len = SIDEBAND_HEADER_SIZE - n;
135 if (!iov_valid(&sbb->iov))
140 n -= SIDEBAND_HEADER_SIZE;
141 assert(n < sbb->iov.iov_len);
142 iov[0].iov_base = sbb->iov.iov_base + n;
143 iov[0].iov_len = sbb->iov.iov_len - n;
145 iov[1].iov_base = NULL;
151 * Update the sideband context after data has been sent.
153 * \param c The sideband handle.
154 * \param nbytes The number of sent bytes.
156 * \return False if more data must be sent to complete the sideband transfer,
157 * true if the transfer is complete. In this case all resources are freed and
158 * the sideband handle must not be used any more.
160 bool sb_sent(struct sb_context *c, size_t nbytes)
162 struct sb_buffer *sbb = &c->sbb;
163 size_t sz = SIDEBAND_HEADER_SIZE + sbb->iov.iov_len;
165 assert(c->bytes_dispatched + nbytes <= sz);
166 c->bytes_dispatched += nbytes;
167 if (c->bytes_dispatched < sz)
174 * Obtain a pointer to the next sideband read buffer.
176 * \param c The sideband handle.
177 * \param iov Result IO vector.
179 * This fills in \a iov to point to the buffer to which the next chunk of
180 * received data should be written.
182 void sb_get_recv_buffer(struct sb_context *c, struct iovec *iov)
184 struct sb_buffer *sbb = &c->sbb;
185 size_t n = c->bytes_dispatched;
187 if (n < SIDEBAND_HEADER_SIZE) {
188 iov->iov_base = c->header + n;
189 iov->iov_len = SIDEBAND_HEADER_SIZE - n;
192 n -= SIDEBAND_HEADER_SIZE;
193 assert(sbb->iov.iov_base);
194 assert(sbb->iov.iov_len > n);
195 iov->iov_base = sbb->iov.iov_base + n;
196 iov->iov_len = sbb->iov.iov_len - n;
200 * Update the sideband context after data has been received.
202 * \param c The sideband handle.
203 * \param nbytes The number of bytes that have been received.
204 * \param result The received sideband packet.
206 * \return Negative on errors, zero if more data needs to be read to complete
207 * this sideband packet, one if the sideband packet has been received
210 * Only if the function returns one, the sideband buffer pointed to by \a
211 * result is set to point to the received data.
213 int sb_received(struct sb_context *c, size_t nbytes, struct sb_buffer *result)
215 struct sb_buffer *sbb = &c->sbb;
216 size_t n = c->bytes_dispatched,
217 sz = SIDEBAND_HEADER_SIZE + sbb->iov.iov_len;
219 assert(n + nbytes <= sz);
220 c->bytes_dispatched += nbytes;
221 if (c->bytes_dispatched < SIDEBAND_HEADER_SIZE)
223 if (n >= SIDEBAND_HEADER_SIZE) { /* header has already been received */
224 if (c->bytes_dispatched < sz) /* need to recv more body data */
226 /* received everything, decrypt and return sbb */
229 c->trafo(&sbb->iov, &dst, c->trafo_context);
230 if (sbb->iov.iov_base != dst.iov_base) {
231 free(sbb->iov.iov_base);
232 sbb->iov.iov_base = dst.iov_base;
235 ((char *)(sbb->iov.iov_base))[sbb->iov.iov_len] = '\0';
238 /* header has been received, decrypt and decode it */
239 if (c->trafo) { /* decrypt */
240 struct iovec dst, src = (typeof(src)) {
241 .iov_base = c->header,
242 .iov_len = SIDEBAND_HEADER_SIZE
244 c->trafo(&src, &dst, c->trafo_context);
245 if (src.iov_base != dst.iov_base) {
246 memcpy(c->header, dst.iov_base,
247 SIDEBAND_HEADER_SIZE);
251 /* Decode header, setup sbb */
252 sbb->iov.iov_len = read_u32(c->header);
253 sbb->band = read_u8(c->header + 4);
254 sbb->iov.iov_base = NULL;
255 if (sbb->iov.iov_len == 0) /* zero-sized msg */
257 if (c->max_size > 0 && sbb->iov.iov_len > c->max_size) {
258 PARA_ERROR_LOG("packet too big (is %zu, max %zu)\n",
259 sbb->iov.iov_len, c->max_size);
260 return -E_SB_PACKET_SIZE;
263 * We like to reserve one extra byte for the terminating NULL
264 * character. However, we must make sure the +1 below does not
267 if (sbb->iov.iov_len == (size_t)-1)
268 return -E_SB_PACKET_SIZE;
269 sbb->iov.iov_base = alloc(sbb->iov.iov_len + 1);
270 return 0; /* ready to read body */