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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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
37 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
41 * Handle to a message stream tokenizer.
43 struct GNUNET_SERVER_MessageStreamTokenizer
47 * Function to call on completed messages.
49 GNUNET_SERVER_MessageTokenizerCallback cb;
57 * Size of the buffer (starting at @e hdr).
62 * How many bytes in buffer have we already processed?
67 * How many bytes in buffer are valid right now?
72 * Beginning of the buffer. Typed like this to force alignment.
74 struct GNUNET_MessageHeader *hdr;
81 * Create a message stream tokenizer.
83 * @param cb function to call on completed messages
84 * @param cb_cls closure for @a cb
85 * @return handle to tokenizer
87 struct GNUNET_SERVER_MessageStreamTokenizer *
88 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
91 struct GNUNET_SERVER_MessageStreamTokenizer *ret;
93 ret = GNUNET_new (struct GNUNET_SERVER_MessageStreamTokenizer);
94 ret->hdr = GNUNET_malloc (GNUNET_SERVER_MIN_BUFFER_SIZE);
95 ret->curr_buf = GNUNET_SERVER_MIN_BUFFER_SIZE;
103 * Add incoming data to the receive buffer and call the
104 * callback for all complete messages.
106 * @param mst tokenizer to use
107 * @param client_identity ID of client for which this is a buffer
108 * @param buf input data to add
109 * @param size number of bytes in @a buf
110 * @param purge should any excess bytes in the buffer be discarded
111 * (i.e. for packet-based services like UDP)
112 * @param one_shot only call callback once, keep rest of message in buffer
113 * @return #GNUNET_OK if we are done processing (need more data)
114 * #GNUNET_NO if @a one_shot was set and we have another message ready
115 * #GNUNET_SYSERR if the data stream is corrupt
118 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
119 void *client_identity,
120 const char *buf, size_t size,
121 int purge, int one_shot)
123 const struct GNUNET_MessageHeader *hdr;
128 unsigned long offset;
131 GNUNET_assert (mst->off <= mst->pos);
132 GNUNET_assert (mst->pos <= mst->curr_buf);
133 LOG (GNUNET_ERROR_TYPE_DEBUG,
134 "Server-mst receives %u bytes with %u bytes already in private buffer\n",
135 (unsigned int) size, (unsigned int) (mst->pos - mst->off));
137 ibuf = (char *) mst->hdr;
141 GNUNET_assert (mst->pos >= mst->off);
142 if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
143 (0 != (mst->off % ALIGN_FACTOR)))
145 /* need to align or need more space */
146 mst->pos -= mst->off;
147 memmove (ibuf, &ibuf[mst->off], mst->pos);
150 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
153 GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) -
154 (mst->pos - mst->off), size);
155 GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
160 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
169 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
170 want = ntohs (hdr->size);
171 if (want < sizeof (struct GNUNET_MessageHeader))
174 return GNUNET_SYSERR;
176 if ( (mst->curr_buf - mst->off < want) &&
179 /* can get more space by moving */
180 mst->pos -= mst->off;
181 memmove (ibuf, &ibuf[mst->off], mst->pos);
184 if (mst->curr_buf < want)
186 /* need to get more space by growing buffer */
187 GNUNET_assert (0 == mst->off);
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 GNUNET_assert (mst->pos + delta <= mst->curr_buf);
197 GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
202 if (mst->pos - mst->off < want)
211 if (one_shot == GNUNET_SYSERR)
213 /* cannot call callback again, but return value saying that
214 * we have another full message in the buffer */
218 if (one_shot == GNUNET_YES)
219 one_shot = GNUNET_SYSERR;
221 if (GNUNET_SYSERR == mst->cb (mst->cb_cls, client_identity, hdr))
222 return GNUNET_SYSERR;
223 if (mst->off == mst->pos)
225 /* reset to beginning of buffer, it's free right now! */
230 GNUNET_assert (0 == mst->pos);
233 LOG (GNUNET_ERROR_TYPE_DEBUG,
234 "Server-mst has %u bytes left in inbound buffer\n",
235 (unsigned int) size);
236 if (size < sizeof (struct GNUNET_MessageHeader))
238 offset = (unsigned long) buf;
239 need_align = (0 != (offset % ALIGN_FACTOR)) ? GNUNET_YES : GNUNET_NO;
240 if (GNUNET_NO == need_align)
242 /* can try to do zero-copy and process directly from original buffer */
243 hdr = (const struct GNUNET_MessageHeader *) buf;
244 want = ntohs (hdr->size);
245 if (want < sizeof (struct GNUNET_MessageHeader))
249 return GNUNET_SYSERR;
252 break; /* or not: buffer incomplete, so copy to private buffer... */
253 if (one_shot == GNUNET_SYSERR)
255 /* cannot call callback again, but return value saying that
256 * we have another full message in the buffer */
260 if (one_shot == GNUNET_YES)
261 one_shot = GNUNET_SYSERR;
262 if (GNUNET_SYSERR == mst->cb (mst->cb_cls, client_identity, hdr))
263 return GNUNET_SYSERR;
269 /* need to copy to private buffer to align;
270 * yes, we go a bit more spagetti than usual here */
275 if ((size > 0) && (!purge))
277 if (size + mst->pos > mst->curr_buf)
279 mst->hdr = GNUNET_realloc (mst->hdr, size + mst->pos);
280 ibuf = (char *) mst->hdr;
281 mst->curr_buf = size + mst->pos;
283 GNUNET_assert (size + mst->pos <= mst->curr_buf);
284 GNUNET_memcpy (&ibuf[mst->pos], buf, size);
292 LOG (GNUNET_ERROR_TYPE_DEBUG,
293 "Server-mst leaves %u bytes in private buffer\n",
294 (unsigned int) (mst->pos - mst->off));
300 * Destroys a tokenizer.
302 * @param mst tokenizer to destroy
305 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst)
307 GNUNET_free (mst->hdr);
313 /* end of server_mst.c */