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, const char *buf, size_t size,
123                            int purge, int one_shot)
124 {
125   const struct GNUNET_MessageHeader *hdr;
126   size_t delta;
127   uint16_t want;
128   char *ibuf;
129   int need_align;
130   unsigned long offset;
131   int ret;
132
133 #if DEBUG_SERVER_MST
134   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135               "Server-mst receives %u bytes with %u bytes already in private buffer\n",
136               (unsigned int) size, (unsigned int) (mst->pos - mst->off));
137 #endif
138   ret = GNUNET_OK;
139   ibuf = (char *) mst->hdr;
140   while (mst->pos > 0)
141   {
142 do_align:
143     if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
144         (0 != (mst->off % ALIGN_FACTOR)))
145     {
146       /* need to align or need more space */
147       mst->pos -= mst->off;
148       memmove (ibuf, &ibuf[mst->off], mst->pos);
149       mst->off = 0;
150     }
151     if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
152     {
153       delta =
154           GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) -
155                       (mst->pos - mst->off), size);
156       memcpy (&ibuf[mst->pos], buf, delta);
157       mst->pos += delta;
158       buf += delta;
159       size -= delta;
160     }
161     if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
162     {
163       if (purge)
164       {
165         mst->off = 0;
166         mst->pos = 0;
167       }
168       return GNUNET_OK;
169     }
170     hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
171     want = ntohs (hdr->size);
172     if (want < sizeof (struct GNUNET_MessageHeader))
173     {
174       GNUNET_break_op (0);
175       return GNUNET_SYSERR;
176     }
177     if (mst->curr_buf - mst->off < want)
178     {
179       /* need more space */
180       mst->pos -= mst->off;
181       memmove (ibuf, &ibuf[mst->off], mst->pos);
182       mst->off = 0;
183     }
184     if (want > mst->curr_buf)
185     {
186       mst->hdr = GNUNET_realloc (mst->hdr, want);
187       ibuf = (char *) mst->hdr;
188       mst->curr_buf = want;
189     }
190     hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
191     if (mst->pos - mst->off < want)
192     {
193       delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
194       memcpy (&ibuf[mst->pos], buf, delta);
195       mst->pos += delta;
196       buf += delta;
197       size -= delta;
198     }
199     if (mst->pos - mst->off < want)
200     {
201       if (purge)
202       {
203         mst->off = 0;
204         mst->pos = 0;
205       }
206       return GNUNET_OK;
207     }
208     if (one_shot == GNUNET_SYSERR)
209     {
210       /* cannot call callback again, but return value saying that
211        * we have another full message in the buffer */
212       ret = GNUNET_NO;
213       goto copy;
214     }
215     if (one_shot == GNUNET_YES)
216       one_shot = GNUNET_SYSERR;
217     mst->cb (mst->cb_cls, client_identity, hdr);
218     mst->off += want;
219     if (mst->off == mst->pos)
220     {
221       /* reset to beginning of buffer, it's free right now! */
222       mst->off = 0;
223       mst->pos = 0;
224     }
225   }
226   while (size > 0)
227   {
228 #if DEBUG_SERVER_MST
229     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230                 "Server-mst has %u bytes left in inbound buffer\n",
231                 (unsigned int) size);
232 #endif
233     if (size < sizeof (struct GNUNET_MessageHeader))
234       break;
235     offset = (unsigned long) buf;
236     need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO;
237     if (GNUNET_NO == need_align)
238     {
239       /* can try to do zero-copy and process directly from original buffer */
240       hdr = (const struct GNUNET_MessageHeader *) buf;
241       want = ntohs (hdr->size);
242       if (want < sizeof (struct GNUNET_MessageHeader))
243       {
244         GNUNET_break_op (0);
245         mst->off = 0;
246         return GNUNET_SYSERR;
247       }
248       if (size < want)
249         break;                  /* or not, buffer incomplete, so copy to private buffer... */
250       if (one_shot == GNUNET_SYSERR)
251       {
252         /* cannot call callback again, but return value saying that
253          * we have another full message in the buffer */
254         ret = GNUNET_NO;
255         goto copy;
256       }
257       if (one_shot == GNUNET_YES)
258         one_shot = GNUNET_SYSERR;
259       mst->cb (mst->cb_cls, client_identity, hdr);
260       buf += want;
261       size -= want;
262     }
263     else
264     {
265       /* need to copy to private buffer to align;
266        * yes, we go a bit more spagetti than usual here */
267       goto do_align;
268     }
269   }
270 copy:
271   if ((size > 0) && (!purge))
272   {
273     if (size + mst->pos > mst->curr_buf)
274     {
275       mst->hdr = GNUNET_realloc (mst->hdr, size + mst->pos);
276       ibuf = (char *) mst->hdr;
277       mst->curr_buf = size + mst->pos;
278     }
279     GNUNET_assert (mst->pos + size <= mst->curr_buf);
280     memcpy (&ibuf[mst->pos], buf, size);
281     mst->pos += size;
282   }
283   if (purge)
284     mst->off = 0;
285 #if DEBUG_SERVER_MST
286   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
287               "Server-mst leaves %u bytes in private buffer\n",
288               (unsigned int) (mst->pos - mst->off));
289 #endif
290   return ret;
291 }
292
293
294 /**
295  * Destroys a tokenizer.
296  *
297  * @param mst tokenizer to destroy
298  */
299 void
300 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst)
301 {
302   GNUNET_free (mst->hdr);
303   GNUNET_free (mst);
304 }
305
306
307
308 /* end of server_mst.c */