fixed bug https://www.gnunet.org/bugs/view.php?id=1762
[oweals/gnunet.git] / src / util / server_tc.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_tc.c
23  * @brief convenience functions for transmission of
24  *        complex responses as a server
25  * @author Christian Grothoff
26  */
27
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_connection_lib.h"
31 #include "gnunet_scheduler_lib.h"
32 #include "gnunet_server_lib.h"
33 #include "gnunet_time_lib.h"
34
35
36
37 /**
38  * How much buffer space do we want to have at least
39  * before transmitting another increment?
40  */
41 #define MIN_BLOCK_SIZE 128
42
43
44
45 struct GNUNET_SERVER_TransmitContext
46 {
47   /**
48    * Which client are we transmitting to?
49    */
50   struct GNUNET_SERVER_Client *client;
51
52   /**
53    * Transmission buffer. (current offset for writing).
54    */
55   char *buf;
56
57   /**
58    * Number of bytes in buf.
59    */
60   size_t total;
61
62   /**
63    * Offset for writing in buf.
64    */
65   size_t off;
66
67   /**
68    * Timeout for this request.
69    */
70   struct GNUNET_TIME_Absolute timeout;
71 };
72
73
74 /**
75  * Helper function for incremental transmission of the response.
76  */
77 static size_t
78 transmit_response (void *cls, size_t size, void *buf)
79 {
80   struct GNUNET_SERVER_TransmitContext *tc = cls;
81   size_t msize;
82
83   if (buf == NULL)
84   {
85     GNUNET_SERVER_receive_done (tc->client, GNUNET_SYSERR);
86     GNUNET_SERVER_client_drop (tc->client);
87     GNUNET_free_non_null (tc->buf);
88     GNUNET_free (tc);
89     return 0;
90   }
91   if (tc->total - tc->off > size)
92     msize = size;
93   else
94     msize = tc->total - tc->off;
95   memcpy (buf, &tc->buf[tc->off], msize);
96   tc->off += msize;
97   if (tc->total == tc->off)
98   {
99     GNUNET_SERVER_receive_done (tc->client, GNUNET_OK);
100     GNUNET_SERVER_client_drop (tc->client);
101     GNUNET_free_non_null (tc->buf);
102     GNUNET_free (tc);
103   }
104   else
105   {
106     if (NULL ==
107         GNUNET_SERVER_notify_transmit_ready (tc->client,
108                                              GNUNET_MIN (MIN_BLOCK_SIZE,
109                                                          tc->total - tc->off),
110                                              GNUNET_TIME_absolute_get_remaining
111                                              (tc->timeout), &transmit_response,
112                                              tc))
113     {
114       GNUNET_break (0);
115       GNUNET_SERVER_receive_done (tc->client, GNUNET_SYSERR);
116       GNUNET_SERVER_client_drop (tc->client);
117       GNUNET_free_non_null (tc->buf);
118       GNUNET_free (tc);
119     }
120   }
121   return msize;
122 }
123
124
125 /**
126  * Create a new transmission context for the
127  * given client.
128  *
129  * @param client client to create the context for.
130  * @return NULL on error
131  */
132 struct GNUNET_SERVER_TransmitContext *
133 GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client)
134 {
135   struct GNUNET_SERVER_TransmitContext *tc;
136
137   GNUNET_assert (client != NULL);
138   tc = GNUNET_malloc (sizeof (struct GNUNET_SERVER_TransmitContext));
139   GNUNET_SERVER_client_keep (client);
140   tc->client = client;
141   return tc;
142 }
143
144
145 /**
146  * Append a message to the transmission context.
147  * All messages in the context will be sent by
148  * the transmit_context_run method.
149  *
150  * @param tc context to use
151  * @param data what to append to the result message
152  * @param length length of data
153  * @param type type of the message
154  */
155 void
156 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
157                                             *tc, const void *data,
158                                             size_t length, uint16_t type)
159 {
160   struct GNUNET_MessageHeader *msg;
161   size_t size;
162
163   GNUNET_assert (length < GNUNET_SERVER_MAX_MESSAGE_SIZE);
164   size = length + sizeof (struct GNUNET_MessageHeader);
165   GNUNET_assert (size > length);
166   tc->buf = GNUNET_realloc (tc->buf, tc->total + size);
167   msg = (struct GNUNET_MessageHeader *) &tc->buf[tc->total];
168   tc->total += size;
169   msg->size = htons (size);
170   msg->type = htons (type);
171   memcpy (&msg[1], data, length);
172 }
173
174
175 /**
176  * Append a message to the transmission context.
177  * All messages in the context will be sent by
178  * the transmit_context_run method.
179  *
180  * @param tc context to use
181  * @param msg message to append
182  */
183 void
184 GNUNET_SERVER_transmit_context_append_message (struct
185                                                GNUNET_SERVER_TransmitContext
186                                                *tc,
187                                                const struct GNUNET_MessageHeader
188                                                *msg)
189 {
190   struct GNUNET_MessageHeader *m;
191   uint16_t size;
192
193   size = ntohs (msg->size);
194   tc->buf = GNUNET_realloc (tc->buf, tc->total + size);
195   m = (struct GNUNET_MessageHeader *) &tc->buf[tc->total];
196   tc->total += size;
197   memcpy (m, msg, size);
198 }
199
200
201 /**
202  * Execute a transmission context.  If there is
203  * an error in the transmission, the receive_done
204  * method will be called with an error code (GNUNET_SYSERR),
205  * otherwise with GNUNET_OK.
206  *
207  * @param tc transmission context to use
208  * @param timeout when to time out and abort the transmission
209  */
210 void
211 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
212                                     struct GNUNET_TIME_Relative timeout)
213 {
214   tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
215   if (NULL ==
216       GNUNET_SERVER_notify_transmit_ready (tc->client,
217                                            GNUNET_MIN (MIN_BLOCK_SIZE,
218                                                        tc->total), timeout,
219                                            &transmit_response, tc))
220   {
221     GNUNET_break (0);
222     GNUNET_SERVER_receive_done (tc->client, GNUNET_SYSERR);
223     GNUNET_SERVER_client_drop (tc->client);
224     GNUNET_free_non_null (tc->buf);
225     GNUNET_free (tc);
226   }
227 }
228
229 /* end of server_tc.c */