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