2 This file is part of GNUnet.
3 (C) 2009 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"
36 * Handle to a message stream tokenizer.
38 struct GNUNET_SERVER_MessageStreamTokenizer
45 void *client_identity;
47 GNUNET_SERVER_MessageTokenizerCallback cb;
52 * Beginning of the buffer.
54 struct GNUNET_MessageHeader hdr;
61 * Create a message stream tokenizer.
63 * @param maxbuf maximum message size to support (typically
64 * GNUNET_SERVER_MAX_MESSAGE_SIZE)
65 * @param client_identity ID of client for which this is a buffer,
66 * can be NULL (will be passed back to 'cb')
67 * @return handle to tokenizer
69 struct GNUNET_SERVER_MessageStreamTokenizer *
70 GNUNET_SERVER_mst_create (size_t maxbuf,
71 void *client_identity,
72 GNUNET_SERVER_MessageTokenizerCallback cb,
75 struct GNUNET_SERVER_MessageStreamTokenizer *ret;
77 ret = GNUNET_malloc (maxbuf + sizeof (struct GNUNET_SERVER_MessageStreamTokenizer));
79 ret->client_identity = client_identity;
87 * Add incoming data to the receive buffer and call the
88 * callback for all complete messages.
90 * @param mst tokenizer to use
91 * @param buf input data to add
92 * @param size number of bytes in buf
93 * @param purge should any excess bytes in the buffer be discarded
94 * (i.e. for packet-based services like UDP)
95 * @return GNUNET_NO if the data stream is corrupt
96 * GNUNET_SYSERR if the data stream is corrupt beyond repair
99 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
104 const struct GNUNET_MessageHeader *hdr;
109 unsigned long offset;
111 ibuf = (char*) &mst->hdr;
115 if (mst->off < sizeof (struct GNUNET_MessageHeader))
117 delta = GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) - mst->off,
119 memcpy (&ibuf[mst->off],
126 if (mst->off < sizeof (struct GNUNET_MessageHeader))
132 want = ntohs (mst->hdr.size);
133 if (want < sizeof (struct GNUNET_MessageHeader))
138 return GNUNET_SYSERR;
142 delta = GNUNET_MIN (want - mst->off,
144 memcpy (&ibuf[mst->off],
157 mst->cb (mst->cb_cls, mst->client_identity, &mst->hdr);
162 if (size < sizeof (struct GNUNET_MessageHeader))
164 offset = (unsigned long) buf;
165 #if HAVE_UNALIGNED_64_ACCESS
166 need_align = (0 != offset % 4) ? GNUNET_YES : GNUNET_NO;
168 need_align = (0 != offset % 8) ? GNUNET_YES : GNUNET_NO;
170 if (GNUNET_NO == need_align)
172 /* can try to do zero-copy */
173 hdr = (const struct GNUNET_MessageHeader *) buf;
174 want = ntohs (hdr->size);
176 break; /* or not, buffer incomplete... */
177 mst->cb (mst->cb_cls, mst->client_identity, hdr);
183 /* yes, we go a bit more spagetti than usual here */
187 if ( (size > 0) && (! purge) )
189 memcpy (&mst->hdr, buf, size);
200 * Destroys a tokenizer.
202 * @param mst tokenizer to destroy
205 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst)
212 /* end of server_mst.c */