2 This file is part of GNUnet.
3 (C) 2008, 2011 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 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file conversation/mst.c
23 * @brief Message tokenizer
24 * @author Christian Grothoff
27 #include <gnunet/platform.h>
28 #include <gnunet/gnunet_constants.h>
31 * To what multiple do we align messages? 8 byte should suffice for everyone
34 #define ALIGN_FACTOR 8
37 * Smallest supported message.
39 #define MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
43 * Functions with this signature are called whenever a
44 * complete message is received by the tokenizer.
47 * @param message the actual message
49 typedef void (*MessageTokenizerCallback) (void *cls,
51 GNUNET_MessageHeader *
55 * Handle to a message stream tokenizer.
57 struct MessageStreamTokenizer
61 * Function to call on completed messages.
63 MessageTokenizerCallback cb;
71 * Size of the buffer (starting at 'hdr').
76 * How many bytes in buffer have we already processed?
81 * How many bytes in buffer are valid right now?
86 * Beginning of the buffer. Typed like this to force alignment.
88 struct GNUNET_MessageHeader *hdr;
94 * Create a message stream tokenizer.
96 * @param cb function to call on completed messages
97 * @param cb_cls closure for cb
98 * @return handle to tokenizer
100 static struct MessageStreamTokenizer *
101 mst_create (MessageTokenizerCallback cb,
104 struct MessageStreamTokenizer *ret;
106 ret = malloc (sizeof (struct MessageStreamTokenizer));
109 fprintf (stderr, "Failed to allocate buffer for tokenizer\n");
112 ret->hdr = malloc (MIN_BUFFER_SIZE);
113 if (NULL == ret->hdr)
115 fprintf (stderr, "Failed to allocate buffer for alignment\n");
118 ret->curr_buf = MIN_BUFFER_SIZE;
120 ret->cb_cls = cb_cls;
126 * Add incoming data to the receive buffer and call the
127 * callback for all complete messages.
129 * @param mst tokenizer to use
130 * @param buf input data to add
131 * @param size number of bytes in buf
132 * @return GNUNET_OK if we are done processing (need more data)
133 * GNUNET_SYSERR if the data stream is corrupt
136 mst_receive (struct MessageStreamTokenizer *mst,
137 const char *buf, size_t size)
139 const struct GNUNET_MessageHeader *hdr;
144 unsigned long offset;
148 ibuf = (char *) mst->hdr;
152 if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
153 (0 != (mst->off % ALIGN_FACTOR)))
155 /* need to align or need more space */
156 mst->pos -= mst->off;
157 memmove (ibuf, &ibuf[mst->off], mst->pos);
160 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
163 GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) -
164 (mst->pos - mst->off), size);
165 memcpy (&ibuf[mst->pos], buf, delta);
170 if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
174 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
175 want = ntohs (hdr->size);
176 if (want < sizeof (struct GNUNET_MessageHeader))
179 "Received invalid message from stdin\n");
182 if (mst->curr_buf - mst->off < want)
184 /* need more space */
185 mst->pos -= mst->off;
186 memmove (ibuf, &ibuf[mst->off], mst->pos);
189 if (want > mst->curr_buf)
191 mst->hdr = realloc (mst->hdr, want);
192 if (NULL == mst->hdr)
194 fprintf (stderr, "Failed to allocate buffer for alignment\n");
197 ibuf = (char *) mst->hdr;
198 mst->curr_buf = want;
200 hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
201 if (mst->pos - mst->off < want)
203 delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
204 memcpy (&ibuf[mst->pos], buf, delta);
209 if (mst->pos - mst->off < want)
213 mst->cb (mst->cb_cls, hdr);
215 if (mst->off == mst->pos)
217 /* reset to beginning of buffer, it's free right now! */
224 if (size < sizeof (struct GNUNET_MessageHeader))
226 offset = (unsigned long) buf;
227 need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO;
228 if (GNUNET_NO == need_align)
230 /* can try to do zero-copy and process directly from original buffer */
231 hdr = (const struct GNUNET_MessageHeader *) buf;
232 want = ntohs (hdr->size);
233 if (want < sizeof (struct GNUNET_MessageHeader))
236 "Received invalid message from stdin\n");
240 break; /* or not, buffer incomplete, so copy to private buffer... */
241 mst->cb (mst->cb_cls, hdr);
247 /* need to copy to private buffer to align;
248 * yes, we go a bit more spagetti than usual here */
254 if (size + mst->pos > mst->curr_buf)
256 mst->hdr = realloc (mst->hdr, size + mst->pos);
257 if (NULL == mst->hdr)
259 fprintf (stderr, "Failed to allocate buffer for alignment\n");
262 ibuf = (char *) mst->hdr;
263 mst->curr_buf = size + mst->pos;
265 if (mst->pos + size > mst->curr_buf)
268 "Assertion failed\n");
271 memcpy (&ibuf[mst->pos], buf, size);
279 * Destroys a tokenizer.
281 * @param mst tokenizer to destroy
284 mst_destroy (struct MessageStreamTokenizer *mst)