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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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;
130 GNUNET_assert (mst->off <= mst->pos);
131 GNUNET_assert (mst->pos <= mst->curr_buf);
132 LOG (GNUNET_ERROR_TYPE_DEBUG,
133 "MST receives %u bytes with %u bytes already in private buffer\n",
135 (unsigned int) (mst->pos - mst->off));
137 ibuf = (char *) mst->hdr;
141 GNUNET_assert (mst->pos >= mst->off);
142 if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
143 (0 != (mst->off % ALIGN_FACTOR)))
145 /* need to align or need more space */
146 mst->pos -= mst->off;
152 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
155 = GNUNET_MIN (sizeof (struct GNUNET_MessageHeader)
156 - (mst->pos - mst->off),
158 GNUNET_memcpy (&ibuf[mst->pos],
165 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
174 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
175 want = ntohs (hdr->size);
176 if (want < sizeof (struct GNUNET_MessageHeader))
179 return GNUNET_SYSERR;
181 if ( (mst->curr_buf - mst->off < want) &&
184 /* can get more space by moving */
185 mst->pos -= mst->off;
191 if (mst->curr_buf < want)
193 /* need to get more space by growing buffer */
194 GNUNET_assert (0 == mst->off);
195 mst->hdr = GNUNET_realloc (mst->hdr,
197 ibuf = (char *) mst->hdr;
198 mst->curr_buf = want;
200 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
201 if (mst->pos - mst->off < want)
203 delta = GNUNET_MIN (want - (mst->pos - mst->off),
205 GNUNET_assert (mst->pos + delta <= mst->curr_buf);
206 GNUNET_memcpy (&ibuf[mst->pos],
213 if (mst->pos - mst->off < want)
222 if (one_shot == GNUNET_SYSERR)
224 /* cannot call callback again, but return value saying that
225 * we have another full message in the buffer */
229 if (one_shot == GNUNET_YES)
230 one_shot = GNUNET_SYSERR;
232 if (GNUNET_SYSERR == mst->cb (mst->cb_cls,
235 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
236 "Failure processing message of type %u and size %u\n",
239 return GNUNET_SYSERR;
241 if (mst->off == mst->pos)
243 /* reset to beginning of buffer, it's free right now! */
248 GNUNET_assert (0 == mst->pos);
251 LOG (GNUNET_ERROR_TYPE_DEBUG,
252 "Server-mst has %u bytes left in inbound buffer\n",
253 (unsigned int) size);
254 if (size < sizeof (struct GNUNET_MessageHeader))
256 offset = (unsigned long) buf;
257 need_align = (0 != (offset % ALIGN_FACTOR)) ? GNUNET_YES : GNUNET_NO;
258 if (GNUNET_NO == need_align)
260 /* can try to do zero-copy and process directly from original buffer */
261 hdr = (const struct GNUNET_MessageHeader *) buf;
262 want = ntohs (hdr->size);
263 if (want < sizeof (struct GNUNET_MessageHeader))
267 return GNUNET_SYSERR;
270 break; /* or not: buffer incomplete, so copy to private buffer... */
271 if (one_shot == GNUNET_SYSERR)
273 /* cannot call callback again, but return value saying that
274 * we have another full message in the buffer */
278 if (one_shot == GNUNET_YES)
279 one_shot = GNUNET_SYSERR;
280 if (GNUNET_SYSERR == mst->cb (mst->cb_cls,
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 */