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 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.
18 * @brief convenience functions for handling inbound message buffers
19 * @author Christian Grothoff
23 #include "gnunet_util_lib.h"
26 #if HAVE_UNALIGNED_64_ACCESS
27 #define ALIGN_FACTOR 4
29 #define ALIGN_FACTOR 8
32 #define LOG(kind,...) GNUNET_log_from (kind, "util-mst", __VA_ARGS__)
36 * Handle to a message stream tokenizer.
38 struct GNUNET_MessageStreamTokenizer
42 * Function to call on completed messages.
44 GNUNET_MessageTokenizerCallback cb;
52 * Size of the buffer (starting at @e hdr).
57 * How many bytes in buffer have we already processed?
62 * How many bytes in buffer are valid right now?
67 * Beginning of the buffer. Typed like this to force alignment.
69 struct GNUNET_MessageHeader *hdr;
75 * Create a message stream tokenizer.
77 * @param cb function to call on completed messages
78 * @param cb_cls closure for @a cb
79 * @return handle to tokenizer
81 struct GNUNET_MessageStreamTokenizer *
82 GNUNET_MST_create (GNUNET_MessageTokenizerCallback cb,
85 struct GNUNET_MessageStreamTokenizer *ret;
87 ret = GNUNET_new (struct GNUNET_MessageStreamTokenizer);
88 ret->hdr = GNUNET_malloc (GNUNET_MIN_MESSAGE_SIZE);
89 ret->curr_buf = GNUNET_MIN_MESSAGE_SIZE;
97 * Add incoming data to the receive buffer and call the
98 * callback for all complete messages.
100 * @param mst tokenizer to use
101 * @param buf input data to add
102 * @param size number of bytes in @a buf
103 * @param purge should any excess bytes in the buffer be discarded
104 * (i.e. for packet-based services like UDP)
105 * @param one_shot only call callback once, keep rest of message in buffer
106 * @return #GNUNET_OK if we are done processing (need more data)
107 * #GNUNET_NO if @a one_shot was set and we have another message ready
108 * #GNUNET_SYSERR if the data stream is corrupt
111 GNUNET_MST_from_buffer (struct GNUNET_MessageStreamTokenizer *mst,
117 const struct GNUNET_MessageHeader *hdr;
122 unsigned long offset;
126 GNUNET_assert (mst->off <= mst->pos);
127 GNUNET_assert (mst->pos <= mst->curr_buf);
128 LOG (GNUNET_ERROR_TYPE_DEBUG,
129 "MST receives %u bytes with %u bytes already in private buffer\n",
131 (unsigned int) (mst->pos - mst->off));
133 ibuf = (char *) mst->hdr;
137 GNUNET_assert (mst->pos >= mst->off);
138 if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
139 (0 != (mst->off % ALIGN_FACTOR)))
141 /* need to align or need more space */
142 mst->pos -= mst->off;
148 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
151 = GNUNET_MIN (sizeof (struct GNUNET_MessageHeader)
152 - (mst->pos - mst->off),
154 GNUNET_memcpy (&ibuf[mst->pos],
161 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
170 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
171 want = ntohs (hdr->size);
172 if (want < sizeof (struct GNUNET_MessageHeader))
175 return GNUNET_SYSERR;
177 if ( (mst->curr_buf - mst->off < want) &&
180 /* can get more space by moving */
181 mst->pos -= mst->off;
187 if (mst->curr_buf < want)
189 /* need to get more space by growing buffer */
190 GNUNET_assert (0 == mst->off);
191 mst->hdr = GNUNET_realloc (mst->hdr,
193 ibuf = (char *) mst->hdr;
194 mst->curr_buf = want;
196 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
197 if (mst->pos - mst->off < want)
199 delta = GNUNET_MIN (want - (mst->pos - mst->off),
201 GNUNET_assert (mst->pos + delta <= mst->curr_buf);
202 GNUNET_memcpy (&ibuf[mst->pos],
209 if (mst->pos - mst->off < want)
218 if (one_shot == GNUNET_SYSERR)
220 /* cannot call callback again, but return value saying that
221 * we have another full message in the buffer */
225 if (one_shot == GNUNET_YES)
226 one_shot = GNUNET_SYSERR;
229 (cbret = mst->cb (mst->cb_cls,
232 if (GNUNET_SYSERR == cbret)
233 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
234 "Failure processing message of type %u and size %u\n",
237 return GNUNET_SYSERR;
239 if (mst->off == mst->pos)
241 /* reset to beginning of buffer, it's free right now! */
246 GNUNET_assert (0 == mst->pos);
249 LOG (GNUNET_ERROR_TYPE_DEBUG,
250 "Server-mst has %u bytes left in inbound buffer\n",
251 (unsigned int) size);
252 if (size < sizeof (struct GNUNET_MessageHeader))
254 offset = (unsigned long) buf;
255 need_align = (0 != (offset % ALIGN_FACTOR)) ? GNUNET_YES : GNUNET_NO;
256 if (GNUNET_NO == need_align)
258 /* can try to do zero-copy and process directly from original buffer */
259 hdr = (const struct GNUNET_MessageHeader *) buf;
260 want = ntohs (hdr->size);
261 if (want < sizeof (struct GNUNET_MessageHeader))
265 return GNUNET_SYSERR;
268 break; /* or not: buffer incomplete, so copy to private buffer... */
269 if (one_shot == GNUNET_SYSERR)
271 /* cannot call callback again, but return value saying that
272 * we have another full message in the buffer */
276 if (one_shot == GNUNET_YES)
277 one_shot = GNUNET_SYSERR;
279 (cbret = mst->cb (mst->cb_cls,
282 if (GNUNET_SYSERR == cbret)
283 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
284 "Failure processing message of type %u and size %u\n",
287 return GNUNET_SYSERR;
294 /* need to copy to private buffer to align;
295 * yes, we go a bit more spagetti than usual here */
300 if ((size > 0) && (!purge))
302 if (size + mst->pos > mst->curr_buf)
304 mst->hdr = GNUNET_realloc (mst->hdr,
306 ibuf = (char *) mst->hdr;
307 mst->curr_buf = size + mst->pos;
309 GNUNET_assert (size + mst->pos <= mst->curr_buf);
310 GNUNET_memcpy (&ibuf[mst->pos],
320 LOG (GNUNET_ERROR_TYPE_DEBUG,
321 "Server-mst leaves %u bytes in private buffer\n",
322 (unsigned int) (mst->pos - mst->off));
328 * Add incoming data to the receive buffer and call the
329 * callback for all complete messages.
331 * @param mst tokenizer to use
332 * @param buf input data to add
333 * @param size number of bytes in @a buf
334 * @param purge should any excess bytes in the buffer be discarded
335 * (i.e. for packet-based services like UDP)
336 * @param one_shot only call callback once, keep rest of message in buffer
337 * @return #GNUNET_OK if we are done processing (need more data)
338 * #GNUNET_NO if one_shot was set and we have another message ready
339 * #GNUNET_SYSERR if the data stream is corrupt
342 GNUNET_MST_read (struct GNUNET_MessageStreamTokenizer *mst,
343 struct GNUNET_NETWORK_Handle *sock,
351 left = mst->curr_buf - mst->pos;
352 buf = (char *) mst->hdr;
353 ret = GNUNET_NETWORK_socket_recv (sock,
358 if ( (EAGAIN == errno) ||
361 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
363 return GNUNET_SYSERR;
367 /* other side closed connection, treat as error */
368 return GNUNET_SYSERR;
371 return GNUNET_MST_from_buffer (mst,
380 * Obtain the next message from the @a mst, assuming that
381 * there are more unprocessed messages in the internal buffer
384 * @param mst tokenizer to use
385 * @param one_shot only call callback once, keep rest of message in buffer
386 * @return #GNUNET_OK if we are done processing (need more data)
387 * #GNUNET_NO if one_shot was set and we have another message ready
388 * #GNUNET_SYSERR if the data stream is corrupt
391 GNUNET_MST_next (struct GNUNET_MessageStreamTokenizer *mst,
394 return GNUNET_MST_from_buffer (mst,
403 * Destroys a tokenizer.
405 * @param mst tokenizer to destroy
408 GNUNET_MST_destroy (struct GNUNET_MessageStreamTokenizer *mst)
410 GNUNET_free (mst->hdr);
416 /* end of server_mst.c */