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 * @param one_shot only call callback once, keep rest of message in buffer
96 * @return GNUNET_OK if we are done processing (need more data)
97 * GNUNET_NO if one_shot was set and we have another message ready
98 * GNUNET_SYSERR if the data stream is corrupt
101 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
107 const struct GNUNET_MessageHeader *hdr;
112 unsigned long offset;
116 ibuf = (char*) &mst->hdr;
120 if (mst->off < sizeof (struct GNUNET_MessageHeader))
122 delta = GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) - mst->off,
124 memcpy (&ibuf[mst->off],
131 if (mst->off < sizeof (struct GNUNET_MessageHeader))
137 want = ntohs (mst->hdr.size);
138 if (want < sizeof (struct GNUNET_MessageHeader))
141 return GNUNET_SYSERR;
145 delta = GNUNET_MIN (want - mst->off,
147 memcpy (&ibuf[mst->off],
160 if (one_shot == GNUNET_SYSERR)
165 if (one_shot == GNUNET_YES)
166 one_shot = GNUNET_SYSERR;
167 mst->cb (mst->cb_cls, mst->client_identity, &mst->hdr);
172 if (size < sizeof (struct GNUNET_MessageHeader))
174 offset = (unsigned long) buf;
175 #if HAVE_UNALIGNED_64_ACCESS
176 need_align = (0 != offset % 4) ? GNUNET_YES : GNUNET_NO;
178 need_align = (0 != offset % 8) ? GNUNET_YES : GNUNET_NO;
180 if (GNUNET_NO == need_align)
182 /* can try to do zero-copy */
183 hdr = (const struct GNUNET_MessageHeader *) buf;
184 want = ntohs (hdr->size);
186 break; /* or not, buffer incomplete... */
187 if (one_shot == GNUNET_SYSERR)
192 if (one_shot == GNUNET_YES)
193 one_shot = GNUNET_SYSERR;
194 mst->cb (mst->cb_cls, mst->client_identity, hdr);
200 /* yes, we go a bit more spagetti than usual here */
205 if ( (size > 0) && (! purge) )
207 memcpy (&ibuf[mst->off], buf, size);
218 * Destroys a tokenizer.
220 * @param mst tokenizer to destroy
223 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst)
230 /* end of server_mst.c */