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
46 * Function to call on completed messages.
48 GNUNET_MessageTokenizerCallback cb;
56 * Size of the buffer (starting at @e hdr).
61 * How many bytes in buffer have we already processed?
66 * How many bytes in buffer are valid right now?
71 * Beginning of the buffer. Typed like this to force alignment.
73 struct GNUNET_MessageHeader *hdr;
78 * Create a message stream tokenizer.
80 * @param cb function to call on completed messages
81 * @param cb_cls closure for @a cb
82 * @return handle to tokenizer
84 struct GNUNET_MessageStreamTokenizer *
85 GNUNET_MST_create (GNUNET_MessageTokenizerCallback cb,
88 struct GNUNET_MessageStreamTokenizer *ret;
90 ret = GNUNET_new (struct GNUNET_MessageStreamTokenizer);
91 ret->hdr = GNUNET_malloc (GNUNET_MIN_MESSAGE_SIZE);
92 ret->curr_buf = GNUNET_MIN_MESSAGE_SIZE;
100 * Add incoming data to the receive buffer and call the
101 * callback for all complete messages.
103 * @param mst tokenizer to use
104 * @param buf input data to add
105 * @param size number of bytes in @a buf
106 * @param purge should any excess bytes in the buffer be discarded
107 * (i.e. for packet-based services like UDP)
108 * @param one_shot only call callback once, keep rest of message in buffer
109 * @return #GNUNET_OK if we are done processing (need more data)
110 * #GNUNET_NO if @a one_shot was set and we have another message ready
111 * #GNUNET_SYSERR if the data stream is corrupt
114 GNUNET_MST_from_buffer (struct GNUNET_MessageStreamTokenizer *mst,
120 const struct GNUNET_MessageHeader *hdr;
125 unsigned long offset;
129 GNUNET_assert (mst->off <= mst->pos);
130 GNUNET_assert (mst->pos <= mst->curr_buf);
131 LOG (GNUNET_ERROR_TYPE_DEBUG,
132 "MST receives %u bytes with %u bytes already in private buffer\n",
134 (unsigned int) (mst->pos - mst->off));
136 ibuf = (char *) mst->hdr;
140 GNUNET_assert (mst->pos >= mst->off);
141 if ((mst->curr_buf - mst->off < sizeof(struct GNUNET_MessageHeader)) ||
142 (0 != (mst->off % ALIGN_FACTOR)))
144 /* need to align or need more space */
145 mst->pos -= mst->off;
151 if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader))
154 = GNUNET_MIN (sizeof(struct GNUNET_MessageHeader)
155 - (mst->pos - mst->off),
157 GNUNET_memcpy (&ibuf[mst->pos],
164 if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader))
173 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
174 want = ntohs (hdr->size);
175 if (want < sizeof(struct GNUNET_MessageHeader))
178 return GNUNET_SYSERR;
180 if ((mst->curr_buf - mst->off < want) &&
183 /* can get more space by moving */
184 mst->pos -= mst->off;
190 if (mst->curr_buf < want)
192 /* need to get more space by growing buffer */
193 GNUNET_assert (0 == mst->off);
194 mst->hdr = GNUNET_realloc (mst->hdr,
196 ibuf = (char *) mst->hdr;
197 mst->curr_buf = want;
199 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
200 if (mst->pos - mst->off < want)
202 delta = GNUNET_MIN (want - (mst->pos - mst->off),
204 GNUNET_assert (mst->pos + delta <= mst->curr_buf);
205 GNUNET_memcpy (&ibuf[mst->pos],
212 if (mst->pos - mst->off < want)
221 if (one_shot == GNUNET_SYSERR)
223 /* cannot call callback again, but return value saying that
224 * we have another full message in the buffer */
228 if (one_shot == GNUNET_YES)
229 one_shot = GNUNET_SYSERR;
232 (cbret = mst->cb (mst->cb_cls,
235 if (GNUNET_SYSERR == cbret)
236 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
237 "Failure processing message of type %u and size %u\n",
240 return GNUNET_SYSERR;
242 if (mst->off == mst->pos)
244 /* reset to beginning of buffer, it's free right now! */
249 GNUNET_assert (0 == mst->pos);
252 LOG (GNUNET_ERROR_TYPE_DEBUG,
253 "Server-mst has %u bytes left in inbound buffer\n",
254 (unsigned int) size);
255 if (size < sizeof(struct GNUNET_MessageHeader))
257 offset = (unsigned long) buf;
258 need_align = (0 != (offset % ALIGN_FACTOR)) ? GNUNET_YES : GNUNET_NO;
259 if (GNUNET_NO == need_align)
261 /* can try to do zero-copy and process directly from original buffer */
262 hdr = (const struct GNUNET_MessageHeader *) buf;
263 want = ntohs (hdr->size);
264 if (want < sizeof(struct GNUNET_MessageHeader))
268 return GNUNET_SYSERR;
271 break; /* or not: buffer incomplete, so copy to private buffer... */
272 if (one_shot == GNUNET_SYSERR)
274 /* cannot call callback again, but return value saying that
275 * we have another full message in the buffer */
279 if (one_shot == GNUNET_YES)
280 one_shot = GNUNET_SYSERR;
282 (cbret = mst->cb (mst->cb_cls,
285 if (GNUNET_SYSERR == cbret)
286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
287 "Failure processing message of type %u and size %u\n",
290 return GNUNET_SYSERR;
297 /* need to copy to private buffer to align;
298 * yes, we go a bit more spagetti than usual here */
303 if ((size > 0) && (! purge))
305 if (size + mst->pos > mst->curr_buf)
307 mst->hdr = GNUNET_realloc (mst->hdr,
309 ibuf = (char *) mst->hdr;
310 mst->curr_buf = size + mst->pos;
312 GNUNET_assert (size + mst->pos <= mst->curr_buf);
313 GNUNET_memcpy (&ibuf[mst->pos],
323 LOG (GNUNET_ERROR_TYPE_DEBUG,
324 "Server-mst leaves %u bytes in private buffer\n",
325 (unsigned int) (mst->pos - mst->off));
331 * Add incoming data to the receive buffer and call the
332 * callback for all complete messages.
334 * @param mst tokenizer to use
335 * @param buf input data to add
336 * @param size number of bytes in @a buf
337 * @param purge should any excess bytes in the buffer be discarded
338 * (i.e. for packet-based services like UDP)
339 * @param one_shot only call callback once, keep rest of message in buffer
340 * @return #GNUNET_OK if we are done processing (need more data)
341 * #GNUNET_NO if one_shot was set and we have another message ready
342 * #GNUNET_SYSERR if the data stream is corrupt
345 GNUNET_MST_read (struct GNUNET_MessageStreamTokenizer *mst,
346 struct GNUNET_NETWORK_Handle *sock,
354 left = mst->curr_buf - mst->pos;
355 buf = (char *) mst->hdr;
356 ret = GNUNET_NETWORK_socket_recv (sock,
361 if ((EAGAIN == errno) ||
364 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
366 return GNUNET_SYSERR;
370 /* other side closed connection, treat as error */
371 return GNUNET_SYSERR;
374 return GNUNET_MST_from_buffer (mst,
383 * Obtain the next message from the @a mst, assuming that
384 * there are more unprocessed messages in the internal buffer
387 * @param mst tokenizer to use
388 * @param one_shot only call callback once, keep rest of message in buffer
389 * @return #GNUNET_OK if we are done processing (need more data)
390 * #GNUNET_NO if one_shot was set and we have another message ready
391 * #GNUNET_SYSERR if the data stream is corrupt
394 GNUNET_MST_next (struct GNUNET_MessageStreamTokenizer *mst,
397 return GNUNET_MST_from_buffer (mst,
406 * Destroys a tokenizer.
408 * @param mst tokenizer to destroy
411 GNUNET_MST_destroy (struct GNUNET_MessageStreamTokenizer *mst)
413 GNUNET_free (mst->hdr);
418 /* end of server_mst.c */