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 * How many bytes in buffer have we already processed?
70 * How many bytes in buffer are valid right now?
75 * Beginning of the buffer. Typed like this to force alignment.
77 struct GNUNET_MessageHeader *hdr;
84 * Create a message stream tokenizer.
86 * @param cb function to call on completed messages
87 * @param cb_cls closure for cb
88 * @return handle to tokenizer
90 struct GNUNET_SERVER_MessageStreamTokenizer *
91 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
94 struct GNUNET_SERVER_MessageStreamTokenizer *ret;
96 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_MessageStreamTokenizer));
97 ret->hdr = GNUNET_malloc(GNUNET_SERVER_MIN_BUFFER_SIZE);
98 ret->curr_buf = GNUNET_SERVER_MIN_BUFFER_SIZE;
100 ret->cb_cls = cb_cls;
106 * Add incoming data to the receive buffer and call the
107 * callback for all complete messages.
109 * @param mst tokenizer to use
110 * @param client_identity ID of client for which this is a buffer
111 * @param buf input data to add
112 * @param size number of bytes in buf
113 * @param purge should any excess bytes in the buffer be discarded
114 * (i.e. for packet-based services like UDP)
115 * @param one_shot only call callback once, keep rest of message in buffer
116 * @return GNUNET_OK if we are done processing (need more data)
117 * GNUNET_NO if one_shot was set and we have another message ready
118 * GNUNET_SYSERR if the data stream is corrupt
121 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
122 void *client_identity,
128 const struct GNUNET_MessageHeader *hdr;
133 unsigned long offset;
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138 "Server-mst receives %u bytes with %u bytes already in private buffer\n",
140 (unsigned int) (mst->pos - mst->off));
143 ibuf = (char*)mst->hdr;
147 if ( (mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
148 (0 != (mst->off % ALIGN_FACTOR)) )
150 /* need to align or need more space */
151 mst->pos -= mst->off;
157 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
159 delta = GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) - (mst->pos - mst->off),
161 memcpy (&ibuf[mst->pos],
168 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
177 hdr = (const struct GNUNET_MessageHeader*) &ibuf[mst->off];
178 want = ntohs (hdr->size);
179 if (want < sizeof (struct GNUNET_MessageHeader))
182 return GNUNET_SYSERR;
184 if (mst->curr_buf - mst->off < want)
186 /* need more space */
187 mst->pos -= mst->off;
193 if (want > mst->curr_buf)
195 mst->hdr = GNUNET_realloc(mst->hdr, want);
196 ibuf = (char*)mst->hdr;
197 mst->curr_buf = want;
199 hdr = (const struct GNUNET_MessageHeader*) &ibuf[mst->off];
200 if (mst->pos - mst->off < want)
202 delta = GNUNET_MIN (want - (mst->pos - mst->off),
204 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;
229 mst->cb (mst->cb_cls, client_identity, hdr);
231 if (mst->off == mst->pos)
233 /* reset to beginning of buffer, it's free right now! */
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242 "Server-mst has %u bytes left in inbound buffer\n",
243 (unsigned int) size);
245 if (size < sizeof (struct GNUNET_MessageHeader))
247 offset = (unsigned long) buf;
248 need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO;
249 if (GNUNET_NO == need_align)
251 /* can try to do zero-copy and process directly from original buffer */
252 hdr = (const struct GNUNET_MessageHeader *) buf;
253 want = ntohs (hdr->size);
254 if (want < sizeof (struct GNUNET_MessageHeader))
258 return GNUNET_SYSERR;
261 break; /* or not, buffer incomplete, so copy to private buffer... */
262 if (one_shot == GNUNET_SYSERR)
264 /* cannot call callback again, but return value saying that
265 we have another full message in the buffer */
269 if (one_shot == GNUNET_YES)
270 one_shot = GNUNET_SYSERR;
271 mst->cb (mst->cb_cls, client_identity, hdr);
277 /* need to copy to private buffer to align;
278 yes, we go a bit more spagetti than usual here */
283 if ( (size > 0) && (! purge) )
285 if (size + mst->pos > mst->curr_buf)
287 mst->hdr = GNUNET_realloc(mst->hdr, size + mst->pos);
288 ibuf = (char*)mst->hdr;
289 mst->curr_buf = size + mst->pos;
291 GNUNET_assert (mst->pos + size <= mst->curr_buf);
292 memcpy (&ibuf[mst->pos], buf, size);
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "Server-mst leaves %u bytes in private buffer\n",
300 (unsigned int) (mst->pos - mst->off));
307 * Destroys a tokenizer.
309 * @param mst tokenizer to destroy
312 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst)
314 GNUNET_free (mst->hdr);
320 /* end of server_mst.c */