aboutsummaryrefslogtreecommitdiff
path: root/src/util/server_mst.c
blob: 8df4e4b7d3af2faff255e8c4ec15716675bb899c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
     This file is part of GNUnet.
     (C) 2009 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file util/server_mst.c
 * @brief convenience functions for handling inbound message buffers
 * @author Christian Grothoff
 */

#include "platform.h"
#include "gnunet_common.h"
#include "gnunet_connection_lib.h"
#include "gnunet_scheduler_lib.h"
#include "gnunet_server_lib.h"
#include "gnunet_time_lib.h"


/**
 * Handle to a message stream tokenizer.
 */
struct GNUNET_SERVER_MessageStreamTokenizer
{

  size_t maxbuf;

  size_t off;

  void *client_identity;

  GNUNET_SERVER_MessageTokenizerCallback cb;

  void *cb_cls;

  /**
   * Beginning of the buffer.
   */
  struct GNUNET_MessageHeader hdr;

};



/**
 * Create a message stream tokenizer.
 *
 * @param maxbuf maximum message size to support (typically
 *    GNUNET_SERVER_MAX_MESSAGE_SIZE)
 * @param client_identity ID of client for which this is a buffer,
 *        can be NULL (will be passed back to 'cb')
 * @return handle to tokenizer
 */
struct GNUNET_SERVER_MessageStreamTokenizer *
GNUNET_SERVER_mst_create (size_t maxbuf,
			  void *client_identity,
			  GNUNET_SERVER_MessageTokenizerCallback cb,
			  void *cb_cls)
{
  struct GNUNET_SERVER_MessageStreamTokenizer *ret;

  ret = GNUNET_malloc (maxbuf + sizeof (struct GNUNET_SERVER_MessageStreamTokenizer));
  ret->maxbuf = maxbuf;
  ret->client_identity = client_identity;
  ret->cb = cb;
  ret->cb_cls = cb_cls;
  return ret;
}


/**
 * Add incoming data to the receive buffer and call the
 * callback for all complete messages.
 *
 * @param mst tokenizer to use
 * @param buf input data to add
 * @param size number of bytes in buf
 * @param purge should any excess bytes in the buffer be discarded 
 *       (i.e. for packet-based services like UDP)
 * @param one_shot only call callback once, keep rest of message in buffer
 * @return GNUNET_OK if we are done processing (need more data)
 *         GNUNET_NO if one_shot was set and we have another message ready
 *         GNUNET_SYSERR if the data stream is corrupt
 */
int
GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
			   const char *buf,
			   size_t size,
			   int purge,
			   int one_shot)
{
  const struct GNUNET_MessageHeader *hdr;
  size_t delta;
  size_t want;
  char *ibuf;
  int need_align;
  unsigned long offset;
  int ret;

  ret = GNUNET_OK;
  ibuf = (char*) &mst->hdr;
  if (mst->off > 0)
    {
    do_align:
      if (mst->off < sizeof (struct GNUNET_MessageHeader))
	{
	  delta = GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) - mst->off,
			      size);
	  memcpy (&ibuf[mst->off],
		  buf,
		  delta);
	  mst->off += delta;
	  buf += delta;
	  size -= delta;
	}
      if (mst->off < sizeof (struct GNUNET_MessageHeader))
	{
	  if (purge)
	    mst->off = 0;    
	  return GNUNET_OK;
	}
      want = ntohs (mst->hdr.size);
      if (want < sizeof (struct GNUNET_MessageHeader))
	{
	  GNUNET_break_op (0);
	  return GNUNET_SYSERR;
	}
      if (want < mst->off)
	{
	  delta = GNUNET_MIN (want - mst->off,
			      size);
	  memcpy (&ibuf[mst->off],
		  buf,
		  delta);
	  mst->off += delta;
	  buf += delta;
	  size -= delta;
	}
      if (want < mst->off)
	{
	  if (purge)
	    mst->off = 0;    
	  return GNUNET_OK;
	}
      if (one_shot == GNUNET_SYSERR)
	{
	  ret = GNUNET_NO;
	  goto copy;
	}
      if (one_shot == GNUNET_YES)
	one_shot = GNUNET_SYSERR;
      mst->cb (mst->cb_cls, mst->client_identity, &mst->hdr);
      mst->off = 0;
    }
  while (size > 0)
    {
      if (size < sizeof (struct GNUNET_MessageHeader))
	break;
      offset = (unsigned long) buf;
#if HAVE_UNALIGNED_64_ACCESS
      need_align = (0 != offset % 4) ? GNUNET_YES : GNUNET_NO;
#else
      need_align = (0 != offset % 8) ? GNUNET_YES : GNUNET_NO;
#endif
      if (GNUNET_NO == need_align)
	{
	  /* can try to do zero-copy */
	  hdr = (const struct GNUNET_MessageHeader *) buf;
	  want = ntohs (hdr->size);
	  if (size < want)
	    break; /* or not, buffer incomplete... */
	  if (one_shot == GNUNET_SYSERR)
	    {
	      ret = GNUNET_NO;
	      goto copy;
	    }
	  if (one_shot == GNUNET_YES)
	    one_shot = GNUNET_SYSERR;
	  mst->cb (mst->cb_cls, mst->client_identity, hdr);
	  buf += want;
	  size -= want;
	}
      else
	{
	  /* yes, we go a bit more spagetti than usual here */
	  goto do_align;
	}
    }
 copy:
  if ( (size > 0) && (! purge) )
    {
      memcpy (&ibuf[mst->off], buf, size);
      mst->off += size;
      size = 0;
    }
  if (purge)
    mst->off = 0;    
  return ret;
}


/**
 * Destroys a tokenizer.
 *
 * @param mst tokenizer to destroy
 */
void
GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst)
{
  GNUNET_free (mst);
}



/* end of server_mst.c */