2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/server_mst.c
23 * @brief convenience functions for handling inbound message buffers
24 * @author Christian Grothoff
28 #include "gnunet_common.h"
29 #include "gnunet_connection_lib.h"
30 #include "gnunet_scheduler_lib.h"
31 #include "gnunet_server_lib.h"
32 #include "gnunet_time_lib.h"
34 #define DEBUG_SERVER_MST GNUNET_NO
36 #if HAVE_UNALIGNED_64_ACCESS
37 #define ALIGN_FACTOR 4
39 #define ALIGN_FACTOR 8
44 * Handle to a message stream tokenizer.
46 struct GNUNET_SERVER_MessageStreamTokenizer
50 * Function to call on completed messages.
52 GNUNET_SERVER_MessageTokenizerCallback cb;
60 * Size of the buffer (starting at 'hdr').
65 * Maximum size of the buffer.
70 * How many bytes in buffer have we already processed?
75 * How many bytes in buffer are valid right now?
80 * Beginning of the buffer. Typed like this to force alignment.
82 struct GNUNET_MessageHeader *hdr;
89 * Create a message stream tokenizer.
91 * @param maxbuf maximum message size to support (typically
92 * GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
93 * @param cb function to call on completed messages
94 * @param cb_cls closure for cb
95 * @return handle to tokenizer
97 struct GNUNET_SERVER_MessageStreamTokenizer *
98 GNUNET_SERVER_mst_create (size_t maxbuf,
99 GNUNET_SERVER_MessageTokenizerCallback cb,
102 struct GNUNET_SERVER_MessageStreamTokenizer *ret;
104 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_MessageStreamTokenizer));
105 ret->hdr = GNUNET_malloc(GNUNET_SERVER_MIN_BUFFER_SIZE);
106 ret->curr_buf = GNUNET_SERVER_MIN_BUFFER_SIZE;
107 ret->maxbuf = maxbuf;
109 ret->cb_cls = cb_cls;
115 * Add incoming data to the receive buffer and call the
116 * callback for all complete messages.
118 * @param mst tokenizer to use
119 * @param client_identity ID of client for which this is a buffer
120 * @param buf input data to add
121 * @param size number of bytes in buf
122 * @param purge should any excess bytes in the buffer be discarded
123 * (i.e. for packet-based services like UDP)
124 * @param one_shot only call callback once, keep rest of message in buffer
125 * @return GNUNET_OK if we are done processing (need more data)
126 * GNUNET_NO if one_shot was set and we have another message ready
127 * GNUNET_SYSERR if the data stream is corrupt
130 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
131 void *client_identity,
137 const struct GNUNET_MessageHeader *hdr;
142 unsigned long offset;
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "Server-mst receives %u bytes with %u bytes already in private buffer\n",
150 (unsigned int) (mst->pos - mst->off));
152 if ((size > mst->curr_buf) && (size < mst->maxbuf)) /* Received bigger message than we can currently handle! */
154 newsize = mst->curr_buf + size; /* How much space do we need? */
155 if (newsize > mst->maxbuf)
156 newsize = mst->maxbuf; /* Check it's not bigger than maxbuf */
158 mst->hdr = GNUNET_realloc(mst->hdr, newsize);
159 mst->curr_buf = newsize;
163 ibuf = (char*)mst->hdr;
167 if ( (mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
168 (0 != (mst->off % ALIGN_FACTOR)) )
170 /* need to align or need more space */
171 mst->pos -= mst->off;
177 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
179 delta = GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) - (mst->pos - mst->off),
181 memcpy (&ibuf[mst->pos],
188 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
197 hdr = (const struct GNUNET_MessageHeader*) &ibuf[mst->off];
198 want = ntohs (hdr->size);
199 if (want < sizeof (struct GNUNET_MessageHeader))
202 return GNUNET_SYSERR;
204 if (mst->curr_buf - mst->off < want)
206 /* need more space */
207 mst->pos -= mst->off;
213 if (mst->pos - mst->off < want)
215 delta = GNUNET_MIN (want - (mst->pos - mst->off),
217 memcpy (&ibuf[mst->pos],
224 if (mst->pos - mst->off < want)
233 if (one_shot == GNUNET_SYSERR)
235 /* cannot call callback again, but return value saying that
236 we have another full message in the buffer */
240 if (one_shot == GNUNET_YES)
241 one_shot = GNUNET_SYSERR;
242 mst->cb (mst->cb_cls, client_identity, hdr);
244 if (mst->off == mst->pos)
246 /* reset to beginning of buffer, it's free right now! */
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Server-mst has %u bytes left in inbound buffer\n",
256 (unsigned int) size);
258 if (size < sizeof (struct GNUNET_MessageHeader))
260 offset = (unsigned long) buf;
261 need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO;
262 if (GNUNET_NO == need_align)
264 /* can try to do zero-copy and process directly from original buffer */
265 hdr = (const struct GNUNET_MessageHeader *) buf;
266 want = ntohs (hdr->size);
268 break; /* or not, buffer incomplete, so copy to private buffer... */
269 if (one_shot == GNUNET_SYSERR)
271 /* cannot call callback again, but return value saying that
272 we have another full message in the buffer */
276 if (one_shot == GNUNET_YES)
277 one_shot = GNUNET_SYSERR;
278 mst->cb (mst->cb_cls, client_identity, hdr);
284 /* need to copy to private buffer to align;
285 yes, we go a bit more spagetti than usual here */
290 if ( (size > 0) && (! purge) )
292 GNUNET_assert (mst->pos + size <= mst->curr_buf);
293 memcpy (&ibuf[mst->pos], buf, size);
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Server-mst leaves %u bytes in private buffer\n",
301 (unsigned int) (mst->pos - mst->off));
308 * Destroys a tokenizer.
310 * @param mst tokenizer to destroy
313 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst)
320 /* end of server_mst.c */