2 This file is part of GNUnet.
3 Copyright (C) 2010 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
22 * @file util/server_mst.c
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
39 * Handle to a message stream tokenizer.
41 struct GNUNET_SERVER_MessageStreamTokenizer
45 * Function to call on completed messages.
47 GNUNET_SERVER_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;
79 * Create a message stream tokenizer.
81 * @param cb function to call on completed messages
82 * @param cb_cls closure for @a cb
83 * @return handle to tokenizer
85 struct GNUNET_SERVER_MessageStreamTokenizer *
86 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
89 struct GNUNET_SERVER_MessageStreamTokenizer *ret;
91 ret = GNUNET_new (struct GNUNET_SERVER_MessageStreamTokenizer);
92 ret->hdr = GNUNET_malloc (GNUNET_MIN_MESSAGE_SIZE);
93 ret->curr_buf = GNUNET_MIN_MESSAGE_SIZE;
101 * Add incoming data to the receive buffer and call the
102 * callback for all complete messages.
104 * @param mst tokenizer to use
105 * @param client_identity ID of client for which this is a buffer
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_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
117 void *client_identity,
118 const char *buf, size_t size,
119 int purge, int one_shot)
121 const struct GNUNET_MessageHeader *hdr;
126 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 "Server-mst receives %u bytes with %u bytes already in private buffer\n",
133 (unsigned int) size, (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;
145 memmove (ibuf, &ibuf[mst->off], mst->pos);
148 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
151 GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) -
152 (mst->pos - mst->off), size);
153 GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
158 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
167 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
168 want = ntohs (hdr->size);
169 if (want < sizeof (struct GNUNET_MessageHeader))
172 return GNUNET_SYSERR;
174 if ( (mst->curr_buf - mst->off < want) &&
177 /* can get more space by moving */
178 mst->pos -= mst->off;
179 memmove (ibuf, &ibuf[mst->off], mst->pos);
182 if (mst->curr_buf < want)
184 /* need to get more space by growing buffer */
185 GNUNET_assert (0 == mst->off);
186 mst->hdr = GNUNET_realloc (mst->hdr, want);
187 ibuf = (char *) mst->hdr;
188 mst->curr_buf = want;
190 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
191 if (mst->pos - mst->off < want)
193 delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
194 GNUNET_assert (mst->pos + delta <= mst->curr_buf);
195 GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
200 if (mst->pos - mst->off < want)
209 if (one_shot == GNUNET_SYSERR)
211 /* cannot call callback again, but return value saying that
212 * we have another full message in the buffer */
216 if (one_shot == GNUNET_YES)
217 one_shot = GNUNET_SYSERR;
219 if (GNUNET_SYSERR == mst->cb (mst->cb_cls, client_identity, hdr))
220 return GNUNET_SYSERR;
221 if (mst->off == mst->pos)
223 /* reset to beginning of buffer, it's free right now! */
228 GNUNET_assert (0 == mst->pos);
231 LOG (GNUNET_ERROR_TYPE_DEBUG,
232 "Server-mst has %u bytes left in inbound buffer\n",
233 (unsigned int) size);
234 if (size < sizeof (struct GNUNET_MessageHeader))
236 offset = (unsigned long) buf;
237 need_align = (0 != (offset % ALIGN_FACTOR)) ? GNUNET_YES : GNUNET_NO;
238 if (GNUNET_NO == need_align)
240 /* can try to do zero-copy and process directly from original buffer */
241 hdr = (const struct GNUNET_MessageHeader *) buf;
242 want = ntohs (hdr->size);
243 if (want < sizeof (struct GNUNET_MessageHeader))
247 return GNUNET_SYSERR;
250 break; /* or not: buffer incomplete, so copy to private buffer... */
251 if (one_shot == GNUNET_SYSERR)
253 /* cannot call callback again, but return value saying that
254 * we have another full message in the buffer */
258 if (one_shot == GNUNET_YES)
259 one_shot = GNUNET_SYSERR;
260 if (GNUNET_SYSERR == mst->cb (mst->cb_cls, client_identity, hdr))
261 return GNUNET_SYSERR;
267 /* need to copy to private buffer to align;
268 * yes, we go a bit more spagetti than usual here */
273 if ((size > 0) && (!purge))
275 if (size + mst->pos > mst->curr_buf)
277 mst->hdr = GNUNET_realloc (mst->hdr, size + mst->pos);
278 ibuf = (char *) mst->hdr;
279 mst->curr_buf = size + mst->pos;
281 GNUNET_assert (size + mst->pos <= mst->curr_buf);
282 GNUNET_memcpy (&ibuf[mst->pos], buf, size);
290 LOG (GNUNET_ERROR_TYPE_DEBUG,
291 "Server-mst leaves %u bytes in private buffer\n",
292 (unsigned int) (mst->pos - mst->off));
298 * Destroys a tokenizer.
300 * @param mst tokenizer to destroy
303 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst)
305 GNUNET_free (mst->hdr);
311 /* end of server_mst.c */