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
44 * Function to call on completed messages.
46 GNUNET_SERVER_MessageTokenizerCallback cb;
54 * Size of the buffer (starting at @e hdr).
59 * How many bytes in buffer have we already processed?
64 * How many bytes in buffer are valid right now?
69 * Beginning of the buffer. Typed like this to force alignment.
71 struct GNUNET_MessageHeader *hdr;
76 * Create a message stream tokenizer.
78 * @param cb function to call on completed messages
79 * @param cb_cls closure for @a cb
80 * @return handle to tokenizer
82 struct GNUNET_SERVER_MessageStreamTokenizer *
83 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
86 struct GNUNET_SERVER_MessageStreamTokenizer *ret;
88 ret = GNUNET_new (struct GNUNET_SERVER_MessageStreamTokenizer);
89 ret->hdr = GNUNET_malloc (GNUNET_MIN_MESSAGE_SIZE);
90 ret->curr_buf = GNUNET_MIN_MESSAGE_SIZE;
98 * Add incoming data to the receive buffer and call the
99 * callback for all complete messages.
101 * @param mst tokenizer to use
102 * @param client_identity ID of client for which this is a buffer
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_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
114 void *client_identity,
115 const char *buf, size_t size,
116 int purge, int one_shot)
118 const struct GNUNET_MessageHeader *hdr;
123 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 "Server-mst receives %u bytes with %u bytes already in private buffer\n",
130 (unsigned int) size, (unsigned int) (mst->pos - mst->off));
132 ibuf = (char *) mst->hdr;
136 GNUNET_assert (mst->pos >= mst->off);
137 if ((mst->curr_buf - mst->off < sizeof(struct GNUNET_MessageHeader)) ||
138 (0 != (mst->off % ALIGN_FACTOR)))
140 /* need to align or need more space */
141 mst->pos -= mst->off;
142 memmove (ibuf, &ibuf[mst->off], mst->pos);
145 if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader))
148 GNUNET_MIN (sizeof(struct GNUNET_MessageHeader)
149 - (mst->pos - mst->off), size);
150 GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
155 if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader))
164 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
165 want = ntohs (hdr->size);
166 if (want < sizeof(struct GNUNET_MessageHeader))
169 return GNUNET_SYSERR;
171 if ((mst->curr_buf - mst->off < want) &&
174 /* can get more space by moving */
175 mst->pos -= mst->off;
176 memmove (ibuf, &ibuf[mst->off], mst->pos);
179 if (mst->curr_buf < want)
181 /* need to get more space by growing buffer */
182 GNUNET_assert (0 == mst->off);
183 mst->hdr = GNUNET_realloc (mst->hdr, want);
184 ibuf = (char *) mst->hdr;
185 mst->curr_buf = want;
187 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
188 if (mst->pos - mst->off < want)
190 delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
191 GNUNET_assert (mst->pos + delta <= mst->curr_buf);
192 GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
197 if (mst->pos - mst->off < want)
206 if (one_shot == GNUNET_SYSERR)
208 /* cannot call callback again, but return value saying that
209 * we have another full message in the buffer */
213 if (one_shot == GNUNET_YES)
214 one_shot = GNUNET_SYSERR;
216 if (GNUNET_SYSERR == mst->cb (mst->cb_cls, client_identity, hdr))
217 return GNUNET_SYSERR;
218 if (mst->off == mst->pos)
220 /* reset to beginning of buffer, it's free right now! */
225 GNUNET_assert (0 == mst->pos);
228 LOG (GNUNET_ERROR_TYPE_DEBUG,
229 "Server-mst has %u bytes left in inbound buffer\n",
230 (unsigned int) size);
231 if (size < sizeof(struct GNUNET_MessageHeader))
233 offset = (unsigned long) buf;
234 need_align = (0 != (offset % ALIGN_FACTOR)) ? GNUNET_YES : GNUNET_NO;
235 if (GNUNET_NO == need_align)
237 /* can try to do zero-copy and process directly from original buffer */
238 hdr = (const struct GNUNET_MessageHeader *) buf;
239 want = ntohs (hdr->size);
240 if (want < sizeof(struct GNUNET_MessageHeader))
244 return GNUNET_SYSERR;
247 break; /* or not: buffer incomplete, so copy to private buffer... */
248 if (one_shot == GNUNET_SYSERR)
250 /* cannot call callback again, but return value saying that
251 * we have another full message in the buffer */
255 if (one_shot == GNUNET_YES)
256 one_shot = GNUNET_SYSERR;
257 if (GNUNET_SYSERR == mst->cb (mst->cb_cls, client_identity, hdr))
258 return GNUNET_SYSERR;
264 /* need to copy to private buffer to align;
265 * yes, we go a bit more spagetti than usual here */
270 if ((size > 0) && (! purge))
272 if (size + mst->pos > mst->curr_buf)
274 mst->hdr = GNUNET_realloc (mst->hdr, size + mst->pos);
275 ibuf = (char *) mst->hdr;
276 mst->curr_buf = size + mst->pos;
278 GNUNET_assert (size + mst->pos <= mst->curr_buf);
279 GNUNET_memcpy (&ibuf[mst->pos], buf, size);
287 LOG (GNUNET_ERROR_TYPE_DEBUG,
288 "Server-mst leaves %u bytes in private buffer\n",
289 (unsigned int) (mst->pos - mst->off));
295 * Destroys a tokenizer.
297 * @param mst tokenizer to destroy
300 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst)
302 GNUNET_free (mst->hdr);
307 /* end of server_mst.c */