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