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/>.
20 * @file util/server_mst.c
21 * @brief convenience functions for handling inbound message buffers
22 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
29 #if HAVE_UNALIGNED_64_ACCESS
30 #define ALIGN_FACTOR 4
32 #define ALIGN_FACTOR 8
37 * Handle to a message stream tokenizer.
39 struct GNUNET_SERVER_MessageStreamTokenizer
43 * Function to call on completed messages.
45 GNUNET_SERVER_MessageTokenizerCallback cb;
53 * Size of the buffer (starting at @e hdr).
58 * How many bytes in buffer have we already processed?
63 * How many bytes in buffer are valid right now?
68 * Beginning of the buffer. Typed like this to force alignment.
70 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_SERVER_MessageStreamTokenizer *
84 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
87 struct GNUNET_SERVER_MessageStreamTokenizer *ret;
89 ret = GNUNET_new (struct GNUNET_SERVER_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 client_identity ID of client for which this is a buffer
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_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
115 void *client_identity,
116 const char *buf, size_t size,
117 int purge, int one_shot)
119 const struct GNUNET_MessageHeader *hdr;
124 unsigned long offset;
127 GNUNET_assert (mst->off <= mst->pos);
128 GNUNET_assert (mst->pos <= mst->curr_buf);
129 LOG (GNUNET_ERROR_TYPE_DEBUG,
130 "Server-mst receives %u bytes with %u bytes already in private buffer\n",
131 (unsigned int) size, (unsigned int) (mst->pos - mst->off));
133 ibuf = (char *) mst->hdr;
137 GNUNET_assert (mst->pos >= mst->off);
138 if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
139 (0 != (mst->off % ALIGN_FACTOR)))
141 /* need to align or need more space */
142 mst->pos -= mst->off;
143 memmove (ibuf, &ibuf[mst->off], mst->pos);
146 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
149 GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) -
150 (mst->pos - mst->off), size);
151 GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
156 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
165 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
166 want = ntohs (hdr->size);
167 if (want < sizeof (struct GNUNET_MessageHeader))
170 return GNUNET_SYSERR;
172 if ( (mst->curr_buf - mst->off < want) &&
175 /* can get more space by moving */
176 mst->pos -= mst->off;
177 memmove (ibuf, &ibuf[mst->off], mst->pos);
180 if (mst->curr_buf < want)
182 /* need to get more space by growing buffer */
183 GNUNET_assert (0 == mst->off);
184 mst->hdr = GNUNET_realloc (mst->hdr, want);
185 ibuf = (char *) mst->hdr;
186 mst->curr_buf = want;
188 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
189 if (mst->pos - mst->off < want)
191 delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
192 GNUNET_assert (mst->pos + delta <= mst->curr_buf);
193 GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
198 if (mst->pos - mst->off < want)
207 if (one_shot == GNUNET_SYSERR)
209 /* cannot call callback again, but return value saying that
210 * we have another full message in the buffer */
214 if (one_shot == GNUNET_YES)
215 one_shot = GNUNET_SYSERR;
217 if (GNUNET_SYSERR == mst->cb (mst->cb_cls, client_identity, hdr))
218 return GNUNET_SYSERR;
219 if (mst->off == mst->pos)
221 /* reset to beginning of buffer, it's free right now! */
226 GNUNET_assert (0 == mst->pos);
229 LOG (GNUNET_ERROR_TYPE_DEBUG,
230 "Server-mst has %u bytes left in inbound buffer\n",
231 (unsigned int) size);
232 if (size < sizeof (struct GNUNET_MessageHeader))
234 offset = (unsigned long) buf;
235 need_align = (0 != (offset % ALIGN_FACTOR)) ? GNUNET_YES : GNUNET_NO;
236 if (GNUNET_NO == need_align)
238 /* can try to do zero-copy and process directly from original buffer */
239 hdr = (const struct GNUNET_MessageHeader *) buf;
240 want = ntohs (hdr->size);
241 if (want < sizeof (struct GNUNET_MessageHeader))
245 return GNUNET_SYSERR;
248 break; /* or not: buffer incomplete, so copy to private buffer... */
249 if (one_shot == GNUNET_SYSERR)
251 /* cannot call callback again, but return value saying that
252 * we have another full message in the buffer */
256 if (one_shot == GNUNET_YES)
257 one_shot = GNUNET_SYSERR;
258 if (GNUNET_SYSERR == mst->cb (mst->cb_cls, client_identity, hdr))
259 return GNUNET_SYSERR;
265 /* need to copy to private buffer to align;
266 * yes, we go a bit more spagetti than usual here */
271 if ((size > 0) && (!purge))
273 if (size + mst->pos > mst->curr_buf)
275 mst->hdr = GNUNET_realloc (mst->hdr, size + mst->pos);
276 ibuf = (char *) mst->hdr;
277 mst->curr_buf = size + mst->pos;
279 GNUNET_assert (size + mst->pos <= mst->curr_buf);
280 GNUNET_memcpy (&ibuf[mst->pos], buf, size);
288 LOG (GNUNET_ERROR_TYPE_DEBUG,
289 "Server-mst leaves %u bytes in private buffer\n",
290 (unsigned int) (mst->pos - mst->off));
296 * Destroys a tokenizer.
298 * @param mst tokenizer to destroy
301 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst)
303 GNUNET_free (mst->hdr);
309 /* end of server_mst.c */