2 This file is part of GNUnet.
3 Copyright (C) 2010, 2016, 2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
23 * @brief convenience functions for handling inbound message buffers
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
31 #if HAVE_UNALIGNED_64_ACCESS
32 #define ALIGN_FACTOR 4
34 #define ALIGN_FACTOR 8
37 #define LOG(kind,...) GNUNET_log_from (kind, "util-mst", __VA_ARGS__)
41 * Handle to a message stream tokenizer.
43 struct GNUNET_MessageStreamTokenizer
47 * Function to call on completed messages.
49 GNUNET_MessageTokenizerCallback cb;
57 * Size of the buffer (starting at @e hdr).
62 * How many bytes in buffer have we already processed?
67 * How many bytes in buffer are valid right now?
72 * Beginning of the buffer. Typed like this to force alignment.
74 struct GNUNET_MessageHeader *hdr;
80 * Create a message stream tokenizer.
82 * @param cb function to call on completed messages
83 * @param cb_cls closure for @a cb
84 * @return handle to tokenizer
86 struct GNUNET_MessageStreamTokenizer *
87 GNUNET_MST_create (GNUNET_MessageTokenizerCallback cb,
90 struct GNUNET_MessageStreamTokenizer *ret;
92 ret = GNUNET_new (struct GNUNET_MessageStreamTokenizer);
93 ret->hdr = GNUNET_malloc (GNUNET_MIN_MESSAGE_SIZE);
94 ret->curr_buf = GNUNET_MIN_MESSAGE_SIZE;
102 * Add incoming data to the receive buffer and call the
103 * callback for all complete messages.
105 * @param mst tokenizer to use
106 * @param buf input data to add
107 * @param size number of bytes in @a buf
108 * @param purge should any excess bytes in the buffer be discarded
109 * (i.e. for packet-based services like UDP)
110 * @param one_shot only call callback once, keep rest of message in buffer
111 * @return #GNUNET_OK if we are done processing (need more data)
112 * #GNUNET_NO if @a one_shot was set and we have another message ready
113 * #GNUNET_SYSERR if the data stream is corrupt
116 GNUNET_MST_from_buffer (struct GNUNET_MessageStreamTokenizer *mst,
122 const struct GNUNET_MessageHeader *hdr;
127 unsigned long offset;
131 GNUNET_assert (mst->off <= mst->pos);
132 GNUNET_assert (mst->pos <= mst->curr_buf);
133 LOG (GNUNET_ERROR_TYPE_DEBUG,
134 "MST receives %u bytes with %u bytes already in private buffer\n",
136 (unsigned int) (mst->pos - mst->off));
138 ibuf = (char *) mst->hdr;
142 GNUNET_assert (mst->pos >= mst->off);
143 if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
144 (0 != (mst->off % ALIGN_FACTOR)))
146 /* need to align or need more space */
147 mst->pos -= mst->off;
153 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
156 = GNUNET_MIN (sizeof (struct GNUNET_MessageHeader)
157 - (mst->pos - mst->off),
159 GNUNET_memcpy (&ibuf[mst->pos],
166 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
175 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
176 want = ntohs (hdr->size);
177 if (want < sizeof (struct GNUNET_MessageHeader))
180 return GNUNET_SYSERR;
182 if ( (mst->curr_buf - mst->off < want) &&
185 /* can get more space by moving */
186 mst->pos -= mst->off;
192 if (mst->curr_buf < want)
194 /* need to get more space by growing buffer */
195 GNUNET_assert (0 == mst->off);
196 mst->hdr = GNUNET_realloc (mst->hdr,
198 ibuf = (char *) mst->hdr;
199 mst->curr_buf = want;
201 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
202 if (mst->pos - mst->off < want)
204 delta = GNUNET_MIN (want - (mst->pos - mst->off),
206 GNUNET_assert (mst->pos + delta <= mst->curr_buf);
207 GNUNET_memcpy (&ibuf[mst->pos],
214 if (mst->pos - mst->off < want)
223 if (one_shot == GNUNET_SYSERR)
225 /* cannot call callback again, but return value saying that
226 * we have another full message in the buffer */
230 if (one_shot == GNUNET_YES)
231 one_shot = GNUNET_SYSERR;
234 (cbret = mst->cb (mst->cb_cls,
237 if (GNUNET_SYSERR == cbret)
238 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
239 "Failure processing message of type %u and size %u\n",
242 return GNUNET_SYSERR;
244 if (mst->off == mst->pos)
246 /* reset to beginning of buffer, it's free right now! */
251 GNUNET_assert (0 == mst->pos);
254 LOG (GNUNET_ERROR_TYPE_DEBUG,
255 "Server-mst has %u bytes left in inbound buffer\n",
256 (unsigned int) size);
257 if (size < sizeof (struct GNUNET_MessageHeader))
259 offset = (unsigned long) buf;
260 need_align = (0 != (offset % ALIGN_FACTOR)) ? GNUNET_YES : GNUNET_NO;
261 if (GNUNET_NO == need_align)
263 /* can try to do zero-copy and process directly from original buffer */
264 hdr = (const struct GNUNET_MessageHeader *) buf;
265 want = ntohs (hdr->size);
266 if (want < sizeof (struct GNUNET_MessageHeader))
270 return GNUNET_SYSERR;
273 break; /* or not: buffer incomplete, so copy to private buffer... */
274 if (one_shot == GNUNET_SYSERR)
276 /* cannot call callback again, but return value saying that
277 * we have another full message in the buffer */
281 if (one_shot == GNUNET_YES)
282 one_shot = GNUNET_SYSERR;
284 (cbret = mst->cb (mst->cb_cls,
287 if (GNUNET_SYSERR == cbret)
288 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
289 "Failure processing message of type %u and size %u\n",
292 return GNUNET_SYSERR;
299 /* need to copy to private buffer to align;
300 * yes, we go a bit more spagetti than usual here */
305 if ((size > 0) && (!purge))
307 if (size + mst->pos > mst->curr_buf)
309 mst->hdr = GNUNET_realloc (mst->hdr,
311 ibuf = (char *) mst->hdr;
312 mst->curr_buf = size + mst->pos;
314 GNUNET_assert (size + mst->pos <= mst->curr_buf);
315 GNUNET_memcpy (&ibuf[mst->pos],
325 LOG (GNUNET_ERROR_TYPE_DEBUG,
326 "Server-mst leaves %u bytes in private buffer\n",
327 (unsigned int) (mst->pos - mst->off));
333 * Add incoming data to the receive buffer and call the
334 * callback for all complete messages.
336 * @param mst tokenizer to use
337 * @param buf input data to add
338 * @param size number of bytes in @a buf
339 * @param purge should any excess bytes in the buffer be discarded
340 * (i.e. for packet-based services like UDP)
341 * @param one_shot only call callback once, keep rest of message in buffer
342 * @return #GNUNET_OK if we are done processing (need more data)
343 * #GNUNET_NO if one_shot was set and we have another message ready
344 * #GNUNET_SYSERR if the data stream is corrupt
347 GNUNET_MST_read (struct GNUNET_MessageStreamTokenizer *mst,
348 struct GNUNET_NETWORK_Handle *sock,
356 left = mst->curr_buf - mst->pos;
357 buf = (char *) mst->hdr;
358 ret = GNUNET_NETWORK_socket_recv (sock,
363 if ( (EAGAIN == errno) ||
366 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
368 return GNUNET_SYSERR;
372 /* other side closed connection, treat as error */
373 return GNUNET_SYSERR;
376 return GNUNET_MST_from_buffer (mst,
385 * Obtain the next message from the @a mst, assuming that
386 * there are more unprocessed messages in the internal buffer
389 * @param mst tokenizer to use
390 * @param one_shot only call callback once, keep rest of message in buffer
391 * @return #GNUNET_OK if we are done processing (need more data)
392 * #GNUNET_NO if one_shot was set and we have another message ready
393 * #GNUNET_SYSERR if the data stream is corrupt
396 GNUNET_MST_next (struct GNUNET_MessageStreamTokenizer *mst,
399 return GNUNET_MST_from_buffer (mst,
408 * Destroys a tokenizer.
410 * @param mst tokenizer to destroy
413 GNUNET_MST_destroy (struct GNUNET_MessageStreamTokenizer *mst)
415 GNUNET_free (mst->hdr);
421 /* end of server_mst.c */