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 {
45 * Function to call on completed messages.
47 GNUNET_MessageTokenizerCallback cb;
55 * Size of the buffer (starting at @e hdr).
60 * How many bytes in buffer have we already processed?
65 * How many bytes in buffer are valid right now?
70 * Beginning of the buffer. Typed like this to force alignment.
72 struct GNUNET_MessageHeader *hdr;
77 * Create a message stream tokenizer.
79 * @param cb function to call on completed messages
80 * @param cb_cls closure for @a cb
81 * @return handle to tokenizer
83 struct GNUNET_MessageStreamTokenizer *
84 GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb,
87 struct GNUNET_MessageStreamTokenizer *ret;
89 ret = GNUNET_new(struct GNUNET_MessageStreamTokenizer);
90 ret->hdr = GNUNET_malloc(GNUNET_MIN_MESSAGE_SIZE);
91 ret->curr_buf = GNUNET_MIN_MESSAGE_SIZE;
99 * Add incoming data to the receive buffer and call the
100 * callback for all complete messages.
102 * @param mst tokenizer to use
103 * @param buf input data to add
104 * @param size number of bytes in @a buf
105 * @param purge should any excess bytes in the buffer be discarded
106 * (i.e. for packet-based services like UDP)
107 * @param one_shot only call callback once, keep rest of message in buffer
108 * @return #GNUNET_OK if we are done processing (need more data)
109 * #GNUNET_NO if @a one_shot was set and we have another message ready
110 * #GNUNET_SYSERR if the data stream is corrupt
113 GNUNET_MST_from_buffer(struct GNUNET_MessageStreamTokenizer *mst,
119 const struct GNUNET_MessageHeader *hdr;
124 unsigned long offset;
128 GNUNET_assert(mst->off <= mst->pos);
129 GNUNET_assert(mst->pos <= mst->curr_buf);
130 LOG(GNUNET_ERROR_TYPE_DEBUG,
131 "MST receives %u bytes with %u bytes already in private buffer\n",
133 (unsigned int)(mst->pos - mst->off));
135 ibuf = (char *)mst->hdr;
139 GNUNET_assert(mst->pos >= mst->off);
140 if ((mst->curr_buf - mst->off < sizeof(struct GNUNET_MessageHeader)) ||
141 (0 != (mst->off % ALIGN_FACTOR)))
143 /* need to align or need more space */
144 mst->pos -= mst->off;
150 if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader))
153 = GNUNET_MIN(sizeof(struct GNUNET_MessageHeader)
154 - (mst->pos - mst->off),
156 GNUNET_memcpy(&ibuf[mst->pos],
163 if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader))
172 hdr = (const struct GNUNET_MessageHeader *)&ibuf[mst->off];
173 want = ntohs(hdr->size);
174 if (want < sizeof(struct GNUNET_MessageHeader))
177 return GNUNET_SYSERR;
179 if ((mst->curr_buf - mst->off < want) &&
182 /* can get more space by moving */
183 mst->pos -= mst->off;
189 if (mst->curr_buf < want)
191 /* need to get more space by growing buffer */
192 GNUNET_assert(0 == mst->off);
193 mst->hdr = GNUNET_realloc(mst->hdr,
195 ibuf = (char *)mst->hdr;
196 mst->curr_buf = want;
198 hdr = (const struct GNUNET_MessageHeader *)&ibuf[mst->off];
199 if (mst->pos - mst->off < want)
201 delta = GNUNET_MIN(want - (mst->pos - mst->off),
203 GNUNET_assert(mst->pos + delta <= mst->curr_buf);
204 GNUNET_memcpy(&ibuf[mst->pos],
211 if (mst->pos - mst->off < want)
220 if (one_shot == GNUNET_SYSERR)
222 /* cannot call callback again, but return value saying that
223 * we have another full message in the buffer */
227 if (one_shot == GNUNET_YES)
228 one_shot = GNUNET_SYSERR;
231 (cbret = mst->cb(mst->cb_cls,
234 if (GNUNET_SYSERR == cbret)
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",
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;
281 (cbret = mst->cb(mst->cb_cls,
284 if (GNUNET_SYSERR == cbret)
285 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
286 "Failure processing message of type %u and size %u\n",
289 return GNUNET_SYSERR;
296 /* need to copy to private buffer to align;
297 * yes, we go a bit more spagetti than usual here */
302 if ((size > 0) && (!purge))
304 if (size + mst->pos > mst->curr_buf)
306 mst->hdr = GNUNET_realloc(mst->hdr,
308 ibuf = (char *)mst->hdr;
309 mst->curr_buf = size + mst->pos;
311 GNUNET_assert(size + mst->pos <= mst->curr_buf);
312 GNUNET_memcpy(&ibuf[mst->pos],
322 LOG(GNUNET_ERROR_TYPE_DEBUG,
323 "Server-mst leaves %u bytes in private buffer\n",
324 (unsigned int)(mst->pos - mst->off));
330 * Add incoming data to the receive buffer and call the
331 * callback for all complete messages.
333 * @param mst tokenizer to use
334 * @param buf input data to add
335 * @param size number of bytes in @a buf
336 * @param purge should any excess bytes in the buffer be discarded
337 * (i.e. for packet-based services like UDP)
338 * @param one_shot only call callback once, keep rest of message in buffer
339 * @return #GNUNET_OK if we are done processing (need more data)
340 * #GNUNET_NO if one_shot was set and we have another message ready
341 * #GNUNET_SYSERR if the data stream is corrupt
344 GNUNET_MST_read(struct GNUNET_MessageStreamTokenizer *mst,
345 struct GNUNET_NETWORK_Handle *sock,
353 left = mst->curr_buf - mst->pos;
354 buf = (char *)mst->hdr;
355 ret = GNUNET_NETWORK_socket_recv(sock,
360 if ((EAGAIN == errno) ||
363 GNUNET_log_strerror(GNUNET_ERROR_TYPE_INFO,
365 return GNUNET_SYSERR;
369 /* other side closed connection, treat as error */
370 return GNUNET_SYSERR;
373 return GNUNET_MST_from_buffer(mst,
382 * Obtain the next message from the @a mst, assuming that
383 * there are more unprocessed messages in the internal buffer
386 * @param mst tokenizer to use
387 * @param one_shot only call callback once, keep rest of message in buffer
388 * @return #GNUNET_OK if we are done processing (need more data)
389 * #GNUNET_NO if one_shot was set and we have another message ready
390 * #GNUNET_SYSERR if the data stream is corrupt
393 GNUNET_MST_next(struct GNUNET_MessageStreamTokenizer *mst,
396 return GNUNET_MST_from_buffer(mst,
405 * Destroys a tokenizer.
407 * @param mst tokenizer to destroy
410 GNUNET_MST_destroy(struct GNUNET_MessageStreamTokenizer *mst)
412 GNUNET_free(mst->hdr);
418 /* end of server_mst.c */