indentation
[oweals/gnunet.git] / src / util / server_mst.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file util/server_mst.c
23  * @brief convenience functions for handling inbound message buffers
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
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"
33
34 #define DEBUG_SERVER_MST GNUNET_NO
35
36 #if HAVE_UNALIGNED_64_ACCESS
37 #define ALIGN_FACTOR 4
38 #else
39 #define ALIGN_FACTOR 8
40 #endif
41
42
43 /**
44  * Handle to a message stream tokenizer.
45  */
46 struct GNUNET_SERVER_MessageStreamTokenizer
47 {
48
49   /**
50    * Function to call on completed messages.
51    */
52   GNUNET_SERVER_MessageTokenizerCallback cb;
53
54   /**
55    * Closure for cb.
56    */
57   void *cb_cls;
58
59   /**
60    * Size of the buffer (starting at 'hdr').
61    */
62   size_t curr_buf;
63
64   /**
65    * How many bytes in buffer have we already processed?
66    */
67   size_t off;
68
69   /**
70    * How many bytes in buffer are valid right now?
71    */
72   size_t pos;
73
74   /**
75    * Beginning of the buffer.  Typed like this to force alignment.
76    */
77   struct GNUNET_MessageHeader *hdr;
78
79 };
80
81
82
83 /**
84  * Create a message stream tokenizer.
85  *
86  * @param cb function to call on completed messages
87  * @param cb_cls closure for cb
88  * @return handle to tokenizer
89  */
90 struct GNUNET_SERVER_MessageStreamTokenizer *
91 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
92                           void *cb_cls)
93 {
94   struct GNUNET_SERVER_MessageStreamTokenizer *ret;
95
96   ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_MessageStreamTokenizer));
97   ret->hdr = GNUNET_malloc (GNUNET_SERVER_MIN_BUFFER_SIZE);
98   ret->curr_buf = GNUNET_SERVER_MIN_BUFFER_SIZE;
99   ret->cb = cb;
100   ret->cb_cls = cb_cls;
101   return ret;
102 }
103
104
105 /**
106  * Add incoming data to the receive buffer and call the
107  * callback for all complete messages.
108  *
109  * @param mst tokenizer to use
110  * @param client_identity ID of client for which this is a buffer
111  * @param buf input data to add
112  * @param size number of bytes in buf
113  * @param purge should any excess bytes in the buffer be discarded 
114  *       (i.e. for packet-based services like UDP)
115  * @param one_shot only call callback once, keep rest of message in buffer
116  * @return GNUNET_OK if we are done processing (need more data)
117  *         GNUNET_NO if one_shot was set and we have another message ready
118  *         GNUNET_SYSERR if the data stream is corrupt
119  */
120 int
121 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
122                            void *client_identity,
123                            const char *buf,
124                            size_t size, int purge, int one_shot)
125 {
126   const struct GNUNET_MessageHeader *hdr;
127   size_t delta;
128   uint16_t want;
129   char *ibuf;
130   int need_align;
131   unsigned long offset;
132   int ret;
133
134 #if DEBUG_SERVER_MST
135   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136               "Server-mst receives %u bytes with %u bytes already in private buffer\n",
137               (unsigned int) size, (unsigned int) (mst->pos - mst->off));
138 #endif
139   ret = GNUNET_OK;
140   ibuf = (char *) mst->hdr;
141   while (mst->pos > 0)
142   {
143 do_align:
144     if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
145         (0 != (mst->off % ALIGN_FACTOR)))
146     {
147       /* need to align or need more space */
148       mst->pos -= mst->off;
149       memmove (ibuf, &ibuf[mst->off], mst->pos);
150       mst->off = 0;
151     }
152     if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
153     {
154       delta =
155           GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) -
156                       (mst->pos - mst->off), size);
157       memcpy (&ibuf[mst->pos], buf, delta);
158       mst->pos += delta;
159       buf += delta;
160       size -= delta;
161     }
162     if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
163     {
164       if (purge)
165       {
166         mst->off = 0;
167         mst->pos = 0;
168       }
169       return GNUNET_OK;
170     }
171     hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
172     want = ntohs (hdr->size);
173     if (want < sizeof (struct GNUNET_MessageHeader))
174     {
175       GNUNET_break_op (0);
176       return GNUNET_SYSERR;
177     }
178     if (mst->curr_buf - mst->off < want)
179     {
180       /* need more space */
181       mst->pos -= mst->off;
182       memmove (ibuf, &ibuf[mst->off], mst->pos);
183       mst->off = 0;
184     }
185     if (want > mst->curr_buf)
186     {
187       mst->hdr = GNUNET_realloc (mst->hdr, want);
188       ibuf = (char *) mst->hdr;
189       mst->curr_buf = want;
190     }
191     hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
192     if (mst->pos - mst->off < want)
193     {
194       delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
195       memcpy (&ibuf[mst->pos], buf, delta);
196       mst->pos += delta;
197       buf += delta;
198       size -= delta;
199     }
200     if (mst->pos - mst->off < want)
201     {
202       if (purge)
203       {
204         mst->off = 0;
205         mst->pos = 0;
206       }
207       return GNUNET_OK;
208     }
209     if (one_shot == GNUNET_SYSERR)
210     {
211       /* cannot call callback again, but return value saying that
212        * we have another full message in the buffer */
213       ret = GNUNET_NO;
214       goto copy;
215     }
216     if (one_shot == GNUNET_YES)
217       one_shot = GNUNET_SYSERR;
218     mst->cb (mst->cb_cls, client_identity, hdr);
219     mst->off += want;
220     if (mst->off == mst->pos)
221     {
222       /* reset to beginning of buffer, it's free right now! */
223       mst->off = 0;
224       mst->pos = 0;
225     }
226   }
227   while (size > 0)
228   {
229 #if DEBUG_SERVER_MST
230     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
231                 "Server-mst has %u bytes left in inbound buffer\n",
232                 (unsigned int) size);
233 #endif
234     if (size < sizeof (struct GNUNET_MessageHeader))
235       break;
236     offset = (unsigned long) buf;
237     need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO;
238     if (GNUNET_NO == need_align)
239     {
240       /* can try to do zero-copy and process directly from original buffer */
241       hdr = (const struct GNUNET_MessageHeader *) buf;
242       want = ntohs (hdr->size);
243       if (want < sizeof (struct GNUNET_MessageHeader))
244       {
245         GNUNET_break_op (0);
246         mst->off = 0;
247         return GNUNET_SYSERR;
248       }
249       if (size < want)
250         break;                  /* or not, buffer incomplete, so copy to private buffer... */
251       if (one_shot == GNUNET_SYSERR)
252       {
253         /* cannot call callback again, but return value saying that
254          * we have another full message in the buffer */
255         ret = GNUNET_NO;
256         goto copy;
257       }
258       if (one_shot == GNUNET_YES)
259         one_shot = GNUNET_SYSERR;
260       mst->cb (mst->cb_cls, client_identity, hdr);
261       buf += want;
262       size -= want;
263     }
264     else
265     {
266       /* need to copy to private buffer to align;
267        * yes, we go a bit more spagetti than usual here */
268       goto do_align;
269     }
270   }
271 copy:
272   if ((size > 0) && (!purge))
273   {
274     if (size + mst->pos > mst->curr_buf)
275     {
276       mst->hdr = GNUNET_realloc (mst->hdr, size + mst->pos);
277       ibuf = (char *) mst->hdr;
278       mst->curr_buf = size + mst->pos;
279     }
280     GNUNET_assert (mst->pos + size <= mst->curr_buf);
281     memcpy (&ibuf[mst->pos], buf, size);
282     mst->pos += size;
283   }
284   if (purge)
285     mst->off = 0;
286 #if DEBUG_SERVER_MST
287   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288               "Server-mst leaves %u bytes in private buffer\n",
289               (unsigned int) (mst->pos - mst->off));
290 #endif
291   return ret;
292 }
293
294
295 /**
296  * Destroys a tokenizer.
297  *
298  * @param mst tokenizer to destroy
299  */
300 void
301 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst)
302 {
303   GNUNET_free (mst->hdr);
304   GNUNET_free (mst);
305 }
306
307
308
309 /* end of server_mst.c */