8df4e4b7d3af2faff255e8c4ec15716675bb899c
[oweals/gnunet.git] / src / util / server_mst.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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
35 /**
36  * Handle to a message stream tokenizer.
37  */
38 struct GNUNET_SERVER_MessageStreamTokenizer
39 {
40
41   size_t maxbuf;
42
43   size_t off;
44
45   void *client_identity;
46
47   GNUNET_SERVER_MessageTokenizerCallback cb;
48
49   void *cb_cls;
50
51   /**
52    * Beginning of the buffer.
53    */
54   struct GNUNET_MessageHeader hdr;
55
56 };
57
58
59
60 /**
61  * Create a message stream tokenizer.
62  *
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
68  */
69 struct GNUNET_SERVER_MessageStreamTokenizer *
70 GNUNET_SERVER_mst_create (size_t maxbuf,
71                           void *client_identity,
72                           GNUNET_SERVER_MessageTokenizerCallback cb,
73                           void *cb_cls)
74 {
75   struct GNUNET_SERVER_MessageStreamTokenizer *ret;
76
77   ret = GNUNET_malloc (maxbuf + sizeof (struct GNUNET_SERVER_MessageStreamTokenizer));
78   ret->maxbuf = maxbuf;
79   ret->client_identity = client_identity;
80   ret->cb = cb;
81   ret->cb_cls = cb_cls;
82   return ret;
83 }
84
85
86 /**
87  * Add incoming data to the receive buffer and call the
88  * callback for all complete messages.
89  *
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
99  */
100 int
101 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
102                            const char *buf,
103                            size_t size,
104                            int purge,
105                            int one_shot)
106 {
107   const struct GNUNET_MessageHeader *hdr;
108   size_t delta;
109   size_t want;
110   char *ibuf;
111   int need_align;
112   unsigned long offset;
113   int ret;
114
115   ret = GNUNET_OK;
116   ibuf = (char*) &mst->hdr;
117   if (mst->off > 0)
118     {
119     do_align:
120       if (mst->off < sizeof (struct GNUNET_MessageHeader))
121         {
122           delta = GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) - mst->off,
123                               size);
124           memcpy (&ibuf[mst->off],
125                   buf,
126                   delta);
127           mst->off += delta;
128           buf += delta;
129           size -= delta;
130         }
131       if (mst->off < sizeof (struct GNUNET_MessageHeader))
132         {
133           if (purge)
134             mst->off = 0;    
135           return GNUNET_OK;
136         }
137       want = ntohs (mst->hdr.size);
138       if (want < sizeof (struct GNUNET_MessageHeader))
139         {
140           GNUNET_break_op (0);
141           return GNUNET_SYSERR;
142         }
143       if (want < mst->off)
144         {
145           delta = GNUNET_MIN (want - mst->off,
146                               size);
147           memcpy (&ibuf[mst->off],
148                   buf,
149                   delta);
150           mst->off += delta;
151           buf += delta;
152           size -= delta;
153         }
154       if (want < mst->off)
155         {
156           if (purge)
157             mst->off = 0;    
158           return GNUNET_OK;
159         }
160       if (one_shot == GNUNET_SYSERR)
161         {
162           ret = GNUNET_NO;
163           goto copy;
164         }
165       if (one_shot == GNUNET_YES)
166         one_shot = GNUNET_SYSERR;
167       mst->cb (mst->cb_cls, mst->client_identity, &mst->hdr);
168       mst->off = 0;
169     }
170   while (size > 0)
171     {
172       if (size < sizeof (struct GNUNET_MessageHeader))
173         break;
174       offset = (unsigned long) buf;
175 #if HAVE_UNALIGNED_64_ACCESS
176       need_align = (0 != offset % 4) ? GNUNET_YES : GNUNET_NO;
177 #else
178       need_align = (0 != offset % 8) ? GNUNET_YES : GNUNET_NO;
179 #endif
180       if (GNUNET_NO == need_align)
181         {
182           /* can try to do zero-copy */
183           hdr = (const struct GNUNET_MessageHeader *) buf;
184           want = ntohs (hdr->size);
185           if (size < want)
186             break; /* or not, buffer incomplete... */
187           if (one_shot == GNUNET_SYSERR)
188             {
189               ret = GNUNET_NO;
190               goto copy;
191             }
192           if (one_shot == GNUNET_YES)
193             one_shot = GNUNET_SYSERR;
194           mst->cb (mst->cb_cls, mst->client_identity, hdr);
195           buf += want;
196           size -= want;
197         }
198       else
199         {
200           /* yes, we go a bit more spagetti than usual here */
201           goto do_align;
202         }
203     }
204  copy:
205   if ( (size > 0) && (! purge) )
206     {
207       memcpy (&ibuf[mst->off], buf, size);
208       mst->off += size;
209       size = 0;
210     }
211   if (purge)
212     mst->off = 0;    
213   return ret;
214 }
215
216
217 /**
218  * Destroys a tokenizer.
219  *
220  * @param mst tokenizer to destroy
221  */
222 void
223 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst)
224 {
225   GNUNET_free (mst);
226 }
227
228
229
230 /* end of server_mst.c */