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_EXTRA_LOGGING
36 #if HAVE_UNALIGNED_64_ACCESS
37 #define ALIGN_FACTOR 4
39 #define ALIGN_FACTOR 8
42 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
46 * Handle to a message stream tokenizer.
48 struct GNUNET_SERVER_MessageStreamTokenizer
52 * Function to call on completed messages.
54 GNUNET_SERVER_MessageTokenizerCallback cb;
62 * Size of the buffer (starting at 'hdr').
67 * How many bytes in buffer have we already processed?
72 * How many bytes in buffer are valid right now?
77 * Beginning of the buffer. Typed like this to force alignment.
79 struct GNUNET_MessageHeader *hdr;
86 * Create a message stream tokenizer.
88 * @param cb function to call on completed messages
89 * @param cb_cls closure for cb
90 * @return handle to tokenizer
92 struct GNUNET_SERVER_MessageStreamTokenizer *
93 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
96 struct GNUNET_SERVER_MessageStreamTokenizer *ret;
98 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_MessageStreamTokenizer));
99 ret->hdr = GNUNET_malloc (GNUNET_SERVER_MIN_BUFFER_SIZE);
100 ret->curr_buf = GNUNET_SERVER_MIN_BUFFER_SIZE;
102 ret->cb_cls = cb_cls;
108 * Add incoming data to the receive buffer and call the
109 * callback for all complete messages.
111 * @param mst tokenizer to use
112 * @param client_identity ID of client for which this is a buffer
113 * @param buf input data to add
114 * @param size number of bytes in buf
115 * @param purge should any excess bytes in the buffer be discarded
116 * (i.e. for packet-based services like UDP)
117 * @param one_shot only call callback once, keep rest of message in buffer
118 * @return GNUNET_OK if we are done processing (need more data)
119 * GNUNET_NO if one_shot was set and we have another message ready
120 * GNUNET_SYSERR if the data stream is corrupt
123 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
124 void *client_identity, const char *buf, size_t size,
125 int purge, int one_shot)
127 const struct GNUNET_MessageHeader *hdr;
132 unsigned long offset;
136 LOG (GNUNET_ERROR_TYPE_DEBUG,
137 "Server-mst receives %u bytes with %u bytes already in private buffer\n",
138 (unsigned int) size, (unsigned int) (mst->pos - mst->off));
141 ibuf = (char *) mst->hdr;
145 if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
146 (0 != (mst->off % ALIGN_FACTOR)))
148 /* need to align or need more space */
149 mst->pos -= mst->off;
150 memmove (ibuf, &ibuf[mst->off], mst->pos);
153 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
156 GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) -
157 (mst->pos - mst->off), size);
158 memcpy (&ibuf[mst->pos], buf, delta);
163 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
172 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
173 want = ntohs (hdr->size);
174 if (want < sizeof (struct GNUNET_MessageHeader))
177 return GNUNET_SYSERR;
179 if (mst->curr_buf - mst->off < want)
181 /* need more space */
182 mst->pos -= mst->off;
183 memmove (ibuf, &ibuf[mst->off], mst->pos);
186 if (want > mst->curr_buf)
188 mst->hdr = GNUNET_realloc (mst->hdr, want);
189 ibuf = (char *) mst->hdr;
190 mst->curr_buf = want;
192 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
193 if (mst->pos - mst->off < want)
195 delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
196 memcpy (&ibuf[mst->pos], buf, delta);
201 if (mst->pos - mst->off < want)
210 if (one_shot == GNUNET_SYSERR)
212 /* cannot call callback again, but return value saying that
213 * we have another full message in the buffer */
217 if (one_shot == GNUNET_YES)
218 one_shot = GNUNET_SYSERR;
219 mst->cb (mst->cb_cls, client_identity, hdr);
221 if (mst->off == mst->pos)
223 /* reset to beginning of buffer, it's free right now! */
231 LOG (GNUNET_ERROR_TYPE_DEBUG,
232 "Server-mst has %u bytes left in inbound buffer\n",
233 (unsigned int) size);
235 if (size < sizeof (struct GNUNET_MessageHeader))
237 offset = (unsigned long) buf;
238 need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO;
239 if (GNUNET_NO == need_align)
241 /* can try to do zero-copy and process directly from original buffer */
242 hdr = (const struct GNUNET_MessageHeader *) buf;
243 want = ntohs (hdr->size);
244 if (want < sizeof (struct GNUNET_MessageHeader))
248 return GNUNET_SYSERR;
251 break; /* or not, buffer incomplete, so copy to private buffer... */
252 if (one_shot == GNUNET_SYSERR)
254 /* cannot call callback again, but return value saying that
255 * we have another full message in the buffer */
259 if (one_shot == GNUNET_YES)
260 one_shot = GNUNET_SYSERR;
261 mst->cb (mst->cb_cls, client_identity, hdr);
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 (mst->pos + size <= mst->curr_buf);
282 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));
297 * Destroys a tokenizer.
299 * @param mst tokenizer to destroy
302 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst)
304 GNUNET_free (mst->hdr);
310 /* end of server_mst.c */