buffer.c revision 1.2.2.2 1 1.2.2.2 snj /* $NetBSD: buffer.c,v 1.2.2.2 2014/12/25 02:13:11 snj Exp $ */
2 1.2.2.2 snj
3 1.2.2.2 snj /*
4 1.2.2.2 snj * Copyright (c) 2002-2007 Niels Provos <provos (at) citi.umich.edu>
5 1.2.2.2 snj * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
6 1.2.2.2 snj *
7 1.2.2.2 snj * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 snj * modification, are permitted provided that the following conditions
9 1.2.2.2 snj * are met:
10 1.2.2.2 snj * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 snj * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 snj * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.2.2 snj * notice, this list of conditions and the following disclaimer in the
14 1.2.2.2 snj * documentation and/or other materials provided with the distribution.
15 1.2.2.2 snj * 3. The name of the author may not be used to endorse or promote products
16 1.2.2.2 snj * derived from this software without specific prior written permission.
17 1.2.2.2 snj *
18 1.2.2.2 snj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.2.2.2 snj * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.2.2.2 snj * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.2.2.2 snj * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.2.2.2 snj * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.2.2.2 snj * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.2.2.2 snj * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.2.2.2 snj * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.2.2.2 snj * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.2.2.2 snj * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.2.2.2 snj */
29 1.2.2.2 snj
30 1.2.2.2 snj #include "event2/event-config.h"
31 1.2.2.2 snj #include "evconfig-private.h"
32 1.2.2.2 snj
33 1.2.2.2 snj #ifdef _WIN32
34 1.2.2.2 snj #include <winsock2.h>
35 1.2.2.2 snj #include <windows.h>
36 1.2.2.2 snj #include <io.h>
37 1.2.2.2 snj #endif
38 1.2.2.2 snj
39 1.2.2.2 snj #ifdef EVENT__HAVE_VASPRINTF
40 1.2.2.2 snj /* If we have vasprintf, we need to define _GNU_SOURCE before we include
41 1.2.2.2 snj * stdio.h. This comes from evconfig-private.h.
42 1.2.2.2 snj */
43 1.2.2.2 snj #endif
44 1.2.2.2 snj
45 1.2.2.2 snj #include <sys/types.h>
46 1.2.2.2 snj
47 1.2.2.2 snj #ifdef EVENT__HAVE_SYS_TIME_H
48 1.2.2.2 snj #include <sys/time.h>
49 1.2.2.2 snj #endif
50 1.2.2.2 snj
51 1.2.2.2 snj #ifdef EVENT__HAVE_SYS_SOCKET_H
52 1.2.2.2 snj #include <sys/socket.h>
53 1.2.2.2 snj #endif
54 1.2.2.2 snj
55 1.2.2.2 snj #ifdef EVENT__HAVE_SYS_UIO_H
56 1.2.2.2 snj #include <sys/uio.h>
57 1.2.2.2 snj #endif
58 1.2.2.2 snj
59 1.2.2.2 snj #ifdef EVENT__HAVE_SYS_IOCTL_H
60 1.2.2.2 snj #include <sys/ioctl.h>
61 1.2.2.2 snj #endif
62 1.2.2.2 snj
63 1.2.2.2 snj #ifdef EVENT__HAVE_SYS_MMAN_H
64 1.2.2.2 snj #include <sys/mman.h>
65 1.2.2.2 snj #endif
66 1.2.2.2 snj
67 1.2.2.2 snj #ifdef EVENT__HAVE_SYS_SENDFILE_H
68 1.2.2.2 snj #include <sys/sendfile.h>
69 1.2.2.2 snj #endif
70 1.2.2.2 snj #ifdef EVENT__HAVE_SYS_STAT_H
71 1.2.2.2 snj #include <sys/stat.h>
72 1.2.2.2 snj #endif
73 1.2.2.2 snj
74 1.2.2.2 snj
75 1.2.2.2 snj #include <errno.h>
76 1.2.2.2 snj #include <stdio.h>
77 1.2.2.2 snj #include <stdlib.h>
78 1.2.2.2 snj #include <string.h>
79 1.2.2.2 snj #ifdef EVENT__HAVE_STDARG_H
80 1.2.2.2 snj #include <stdarg.h>
81 1.2.2.2 snj #endif
82 1.2.2.2 snj #ifdef EVENT__HAVE_UNISTD_H
83 1.2.2.2 snj #include <unistd.h>
84 1.2.2.2 snj #endif
85 1.2.2.2 snj #include <limits.h>
86 1.2.2.2 snj
87 1.2.2.2 snj #include "event2/event.h"
88 1.2.2.2 snj #include "event2/buffer.h"
89 1.2.2.2 snj #include "event2/buffer_compat.h"
90 1.2.2.2 snj #include "event2/bufferevent.h"
91 1.2.2.2 snj #include "event2/bufferevent_compat.h"
92 1.2.2.2 snj #include "event2/bufferevent_struct.h"
93 1.2.2.2 snj #include "event2/thread.h"
94 1.2.2.2 snj #include "log-internal.h"
95 1.2.2.2 snj #include "mm-internal.h"
96 1.2.2.2 snj #include "util-internal.h"
97 1.2.2.2 snj #include "evthread-internal.h"
98 1.2.2.2 snj #include "evbuffer-internal.h"
99 1.2.2.2 snj #include "bufferevent-internal.h"
100 1.2.2.2 snj
101 1.2.2.2 snj /* some systems do not have MAP_FAILED */
102 1.2.2.2 snj #ifndef MAP_FAILED
103 1.2.2.2 snj #define MAP_FAILED ((void *)-1)
104 1.2.2.2 snj #endif
105 1.2.2.2 snj
106 1.2.2.2 snj /* send file support */
107 1.2.2.2 snj #if defined(EVENT__HAVE_SYS_SENDFILE_H) && defined(EVENT__HAVE_SENDFILE) && defined(__linux__)
108 1.2.2.2 snj #define USE_SENDFILE 1
109 1.2.2.2 snj #define SENDFILE_IS_LINUX 1
110 1.2.2.2 snj #elif defined(EVENT__HAVE_SENDFILE) && defined(__FreeBSD__)
111 1.2.2.2 snj #define USE_SENDFILE 1
112 1.2.2.2 snj #define SENDFILE_IS_FREEBSD 1
113 1.2.2.2 snj #elif defined(EVENT__HAVE_SENDFILE) && defined(__APPLE__)
114 1.2.2.2 snj #define USE_SENDFILE 1
115 1.2.2.2 snj #define SENDFILE_IS_MACOSX 1
116 1.2.2.2 snj #elif defined(EVENT__HAVE_SENDFILE) && defined(__sun__) && defined(__svr4__)
117 1.2.2.2 snj #define USE_SENDFILE 1
118 1.2.2.2 snj #define SENDFILE_IS_SOLARIS 1
119 1.2.2.2 snj #endif
120 1.2.2.2 snj
121 1.2.2.2 snj /* Mask of user-selectable callback flags. */
122 1.2.2.2 snj #define EVBUFFER_CB_USER_FLAGS 0xffff
123 1.2.2.2 snj /* Mask of all internal-use-only flags. */
124 1.2.2.2 snj #define EVBUFFER_CB_INTERNAL_FLAGS 0xffff0000
125 1.2.2.2 snj
126 1.2.2.2 snj /* Flag set if the callback is using the cb_obsolete function pointer */
127 1.2.2.2 snj #define EVBUFFER_CB_OBSOLETE 0x00040000
128 1.2.2.2 snj
129 1.2.2.2 snj /* evbuffer_chain support */
130 1.2.2.2 snj #define CHAIN_SPACE_PTR(ch) ((ch)->buffer + (ch)->misalign + (ch)->off)
131 1.2.2.2 snj #define CHAIN_SPACE_LEN(ch) ((ch)->flags & EVBUFFER_IMMUTABLE ? \
132 1.2.2.2 snj 0 : (ch)->buffer_len - ((ch)->misalign + (ch)->off))
133 1.2.2.2 snj
134 1.2.2.2 snj #define CHAIN_PINNED(ch) (((ch)->flags & EVBUFFER_MEM_PINNED_ANY) != 0)
135 1.2.2.2 snj #define CHAIN_PINNED_R(ch) (((ch)->flags & EVBUFFER_MEM_PINNED_R) != 0)
136 1.2.2.2 snj
137 1.2.2.2 snj /* evbuffer_ptr support */
138 1.2.2.2 snj #define PTR_NOT_FOUND(ptr) do { \
139 1.2.2.2 snj (ptr)->pos = -1; \
140 1.2.2.2 snj (ptr)->internal_.chain = NULL; \
141 1.2.2.2 snj (ptr)->internal_.pos_in_chain = 0; \
142 1.2.2.2 snj } while (0)
143 1.2.2.2 snj
144 1.2.2.2 snj static void evbuffer_chain_align(struct evbuffer_chain *chain);
145 1.2.2.2 snj static int evbuffer_chain_should_realign(struct evbuffer_chain *chain,
146 1.2.2.2 snj size_t datalen);
147 1.2.2.2 snj static void evbuffer_deferred_callback(struct event_callback *cb, void *arg);
148 1.2.2.2 snj static int evbuffer_ptr_memcmp(const struct evbuffer *buf,
149 1.2.2.2 snj const struct evbuffer_ptr *pos, const char *mem, size_t len);
150 1.2.2.2 snj static struct evbuffer_chain *evbuffer_expand_singlechain(struct evbuffer *buf,
151 1.2.2.2 snj size_t datlen);
152 1.2.2.2 snj static int evbuffer_ptr_subtract(struct evbuffer *buf, struct evbuffer_ptr *pos,
153 1.2.2.2 snj size_t howfar);
154 1.2.2.2 snj static int evbuffer_file_segment_materialize(struct evbuffer_file_segment *seg);
155 1.2.2.2 snj static inline void evbuffer_chain_incref(struct evbuffer_chain *chain);
156 1.2.2.2 snj
157 1.2.2.2 snj static struct evbuffer_chain *
158 1.2.2.2 snj evbuffer_chain_new(size_t size)
159 1.2.2.2 snj {
160 1.2.2.2 snj struct evbuffer_chain *chain;
161 1.2.2.2 snj size_t to_alloc;
162 1.2.2.2 snj
163 1.2.2.2 snj size += EVBUFFER_CHAIN_SIZE;
164 1.2.2.2 snj
165 1.2.2.2 snj /* get the next largest memory that can hold the buffer */
166 1.2.2.2 snj to_alloc = MIN_BUFFER_SIZE;
167 1.2.2.2 snj while (to_alloc < size)
168 1.2.2.2 snj to_alloc <<= 1;
169 1.2.2.2 snj
170 1.2.2.2 snj /* we get everything in one chunk */
171 1.2.2.2 snj if ((chain = mm_malloc(to_alloc)) == NULL)
172 1.2.2.2 snj return (NULL);
173 1.2.2.2 snj
174 1.2.2.2 snj memset(chain, 0, EVBUFFER_CHAIN_SIZE);
175 1.2.2.2 snj
176 1.2.2.2 snj chain->buffer_len = to_alloc - EVBUFFER_CHAIN_SIZE;
177 1.2.2.2 snj
178 1.2.2.2 snj /* this way we can manipulate the buffer to different addresses,
179 1.2.2.2 snj * which is required for mmap for example.
180 1.2.2.2 snj */
181 1.2.2.2 snj chain->buffer = EVBUFFER_CHAIN_EXTRA(u_char, chain);
182 1.2.2.2 snj
183 1.2.2.2 snj chain->refcnt = 1;
184 1.2.2.2 snj
185 1.2.2.2 snj return (chain);
186 1.2.2.2 snj }
187 1.2.2.2 snj
188 1.2.2.2 snj static inline void
189 1.2.2.2 snj evbuffer_chain_free(struct evbuffer_chain *chain)
190 1.2.2.2 snj {
191 1.2.2.2 snj EVUTIL_ASSERT(chain->refcnt > 0);
192 1.2.2.2 snj if (--chain->refcnt > 0) {
193 1.2.2.2 snj /* chain is still referenced by other chains */
194 1.2.2.2 snj return;
195 1.2.2.2 snj }
196 1.2.2.2 snj
197 1.2.2.2 snj if (CHAIN_PINNED(chain)) {
198 1.2.2.2 snj /* will get freed once no longer dangling */
199 1.2.2.2 snj chain->refcnt++;
200 1.2.2.2 snj chain->flags |= EVBUFFER_DANGLING;
201 1.2.2.2 snj return;
202 1.2.2.2 snj }
203 1.2.2.2 snj
204 1.2.2.2 snj /* safe to release chain, it's either a referencing
205 1.2.2.2 snj * chain or all references to it have been freed */
206 1.2.2.2 snj if (chain->flags & EVBUFFER_REFERENCE) {
207 1.2.2.2 snj struct evbuffer_chain_reference *info =
208 1.2.2.2 snj EVBUFFER_CHAIN_EXTRA(
209 1.2.2.2 snj struct evbuffer_chain_reference,
210 1.2.2.2 snj chain);
211 1.2.2.2 snj if (info->cleanupfn)
212 1.2.2.2 snj (*info->cleanupfn)(chain->buffer,
213 1.2.2.2 snj chain->buffer_len,
214 1.2.2.2 snj info->extra);
215 1.2.2.2 snj }
216 1.2.2.2 snj if (chain->flags & EVBUFFER_FILESEGMENT) {
217 1.2.2.2 snj struct evbuffer_chain_file_segment *info =
218 1.2.2.2 snj EVBUFFER_CHAIN_EXTRA(
219 1.2.2.2 snj struct evbuffer_chain_file_segment,
220 1.2.2.2 snj chain);
221 1.2.2.2 snj if (info->segment) {
222 1.2.2.2 snj #ifdef _WIN32
223 1.2.2.2 snj if (info->segment->is_mapping)
224 1.2.2.2 snj UnmapViewOfFile(chain->buffer);
225 1.2.2.2 snj #endif
226 1.2.2.2 snj evbuffer_file_segment_free(info->segment);
227 1.2.2.2 snj }
228 1.2.2.2 snj }
229 1.2.2.2 snj if (chain->flags & EVBUFFER_MULTICAST) {
230 1.2.2.2 snj struct evbuffer_multicast_parent *info =
231 1.2.2.2 snj EVBUFFER_CHAIN_EXTRA(
232 1.2.2.2 snj struct evbuffer_multicast_parent,
233 1.2.2.2 snj chain);
234 1.2.2.2 snj /* referencing chain is being freed, decrease
235 1.2.2.2 snj * refcounts of source chain and associated
236 1.2.2.2 snj * evbuffer (which get freed once both reach
237 1.2.2.2 snj * zero) */
238 1.2.2.2 snj EVUTIL_ASSERT(info->source != NULL);
239 1.2.2.2 snj EVUTIL_ASSERT(info->parent != NULL);
240 1.2.2.2 snj EVBUFFER_LOCK(info->source);
241 1.2.2.2 snj evbuffer_chain_free(info->parent);
242 1.2.2.2 snj evbuffer_decref_and_unlock_(info->source);
243 1.2.2.2 snj }
244 1.2.2.2 snj
245 1.2.2.2 snj mm_free(chain);
246 1.2.2.2 snj }
247 1.2.2.2 snj
248 1.2.2.2 snj static void
249 1.2.2.2 snj evbuffer_free_all_chains(struct evbuffer_chain *chain)
250 1.2.2.2 snj {
251 1.2.2.2 snj struct evbuffer_chain *next;
252 1.2.2.2 snj for (; chain; chain = next) {
253 1.2.2.2 snj next = chain->next;
254 1.2.2.2 snj evbuffer_chain_free(chain);
255 1.2.2.2 snj }
256 1.2.2.2 snj }
257 1.2.2.2 snj
258 1.2.2.2 snj #ifndef NDEBUG
259 1.2.2.2 snj static int
260 1.2.2.2 snj evbuffer_chains_all_empty(struct evbuffer_chain *chain)
261 1.2.2.2 snj {
262 1.2.2.2 snj for (; chain; chain = chain->next) {
263 1.2.2.2 snj if (chain->off)
264 1.2.2.2 snj return 0;
265 1.2.2.2 snj }
266 1.2.2.2 snj return 1;
267 1.2.2.2 snj }
268 1.2.2.2 snj #else
269 1.2.2.2 snj /* The definition is needed for EVUTIL_ASSERT, which uses sizeof to avoid
270 1.2.2.2 snj "unused variable" warnings. */
271 1.2.2.2 snj static inline int evbuffer_chains_all_empty(struct evbuffer_chain *chain) {
272 1.2.2.2 snj return 1;
273 1.2.2.2 snj }
274 1.2.2.2 snj #endif
275 1.2.2.2 snj
276 1.2.2.2 snj /* Free all trailing chains in 'buf' that are neither pinned nor empty, prior
277 1.2.2.2 snj * to replacing them all with a new chain. Return a pointer to the place
278 1.2.2.2 snj * where the new chain will go.
279 1.2.2.2 snj *
280 1.2.2.2 snj * Internal; requires lock. The caller must fix up buf->last and buf->first
281 1.2.2.2 snj * as needed; they might have been freed.
282 1.2.2.2 snj */
283 1.2.2.2 snj static struct evbuffer_chain **
284 1.2.2.2 snj evbuffer_free_trailing_empty_chains(struct evbuffer *buf)
285 1.2.2.2 snj {
286 1.2.2.2 snj struct evbuffer_chain **ch = buf->last_with_datap;
287 1.2.2.2 snj /* Find the first victim chain. It might be *last_with_datap */
288 1.2.2.2 snj while ((*ch) && ((*ch)->off != 0 || CHAIN_PINNED(*ch)))
289 1.2.2.2 snj ch = &(*ch)->next;
290 1.2.2.2 snj if (*ch) {
291 1.2.2.2 snj EVUTIL_ASSERT(evbuffer_chains_all_empty(*ch));
292 1.2.2.2 snj evbuffer_free_all_chains(*ch);
293 1.2.2.2 snj *ch = NULL;
294 1.2.2.2 snj }
295 1.2.2.2 snj return ch;
296 1.2.2.2 snj }
297 1.2.2.2 snj
298 1.2.2.2 snj /* Add a single chain 'chain' to the end of 'buf', freeing trailing empty
299 1.2.2.2 snj * chains as necessary. Requires lock. Does not schedule callbacks.
300 1.2.2.2 snj */
301 1.2.2.2 snj static void
302 1.2.2.2 snj evbuffer_chain_insert(struct evbuffer *buf,
303 1.2.2.2 snj struct evbuffer_chain *chain)
304 1.2.2.2 snj {
305 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(buf);
306 1.2.2.2 snj if (*buf->last_with_datap == NULL) {
307 1.2.2.2 snj /* There are no chains data on the buffer at all. */
308 1.2.2.2 snj EVUTIL_ASSERT(buf->last_with_datap == &buf->first);
309 1.2.2.2 snj EVUTIL_ASSERT(buf->first == NULL);
310 1.2.2.2 snj buf->first = buf->last = chain;
311 1.2.2.2 snj } else {
312 1.2.2.2 snj struct evbuffer_chain **chp;
313 1.2.2.2 snj chp = evbuffer_free_trailing_empty_chains(buf);
314 1.2.2.2 snj *chp = chain;
315 1.2.2.2 snj if (chain->off)
316 1.2.2.2 snj buf->last_with_datap = chp;
317 1.2.2.2 snj buf->last = chain;
318 1.2.2.2 snj }
319 1.2.2.2 snj buf->total_len += chain->off;
320 1.2.2.2 snj }
321 1.2.2.2 snj
322 1.2.2.2 snj static inline struct evbuffer_chain *
323 1.2.2.2 snj evbuffer_chain_insert_new(struct evbuffer *buf, size_t datlen)
324 1.2.2.2 snj {
325 1.2.2.2 snj struct evbuffer_chain *chain;
326 1.2.2.2 snj if ((chain = evbuffer_chain_new(datlen)) == NULL)
327 1.2.2.2 snj return NULL;
328 1.2.2.2 snj evbuffer_chain_insert(buf, chain);
329 1.2.2.2 snj return chain;
330 1.2.2.2 snj }
331 1.2.2.2 snj
332 1.2.2.2 snj void
333 1.2.2.2 snj evbuffer_chain_pin_(struct evbuffer_chain *chain, unsigned flag)
334 1.2.2.2 snj {
335 1.2.2.2 snj EVUTIL_ASSERT((chain->flags & flag) == 0);
336 1.2.2.2 snj chain->flags |= flag;
337 1.2.2.2 snj }
338 1.2.2.2 snj
339 1.2.2.2 snj void
340 1.2.2.2 snj evbuffer_chain_unpin_(struct evbuffer_chain *chain, unsigned flag)
341 1.2.2.2 snj {
342 1.2.2.2 snj EVUTIL_ASSERT((chain->flags & flag) != 0);
343 1.2.2.2 snj chain->flags &= ~flag;
344 1.2.2.2 snj if (chain->flags & EVBUFFER_DANGLING)
345 1.2.2.2 snj evbuffer_chain_free(chain);
346 1.2.2.2 snj }
347 1.2.2.2 snj
348 1.2.2.2 snj static inline void
349 1.2.2.2 snj evbuffer_chain_incref(struct evbuffer_chain *chain)
350 1.2.2.2 snj {
351 1.2.2.2 snj ++chain->refcnt;
352 1.2.2.2 snj }
353 1.2.2.2 snj
354 1.2.2.2 snj struct evbuffer *
355 1.2.2.2 snj evbuffer_new(void)
356 1.2.2.2 snj {
357 1.2.2.2 snj struct evbuffer *buffer;
358 1.2.2.2 snj
359 1.2.2.2 snj buffer = mm_calloc(1, sizeof(struct evbuffer));
360 1.2.2.2 snj if (buffer == NULL)
361 1.2.2.2 snj return (NULL);
362 1.2.2.2 snj
363 1.2.2.2 snj LIST_INIT(&buffer->callbacks);
364 1.2.2.2 snj buffer->refcnt = 1;
365 1.2.2.2 snj buffer->last_with_datap = &buffer->first;
366 1.2.2.2 snj
367 1.2.2.2 snj return (buffer);
368 1.2.2.2 snj }
369 1.2.2.2 snj
370 1.2.2.2 snj int
371 1.2.2.2 snj evbuffer_set_flags(struct evbuffer *buf, ev_uint64_t flags)
372 1.2.2.2 snj {
373 1.2.2.2 snj EVBUFFER_LOCK(buf);
374 1.2.2.2 snj buf->flags |= (ev_uint32_t)flags;
375 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
376 1.2.2.2 snj return 0;
377 1.2.2.2 snj }
378 1.2.2.2 snj
379 1.2.2.2 snj int
380 1.2.2.2 snj evbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags)
381 1.2.2.2 snj {
382 1.2.2.2 snj EVBUFFER_LOCK(buf);
383 1.2.2.2 snj buf->flags &= ~(ev_uint32_t)flags;
384 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
385 1.2.2.2 snj return 0;
386 1.2.2.2 snj }
387 1.2.2.2 snj
388 1.2.2.2 snj void
389 1.2.2.2 snj evbuffer_incref_(struct evbuffer *buf)
390 1.2.2.2 snj {
391 1.2.2.2 snj EVBUFFER_LOCK(buf);
392 1.2.2.2 snj ++buf->refcnt;
393 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
394 1.2.2.2 snj }
395 1.2.2.2 snj
396 1.2.2.2 snj void
397 1.2.2.2 snj evbuffer_incref_and_lock_(struct evbuffer *buf)
398 1.2.2.2 snj {
399 1.2.2.2 snj EVBUFFER_LOCK(buf);
400 1.2.2.2 snj ++buf->refcnt;
401 1.2.2.2 snj }
402 1.2.2.2 snj
403 1.2.2.2 snj int
404 1.2.2.2 snj evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base)
405 1.2.2.2 snj {
406 1.2.2.2 snj EVBUFFER_LOCK(buffer);
407 1.2.2.2 snj buffer->cb_queue = base;
408 1.2.2.2 snj buffer->deferred_cbs = 1;
409 1.2.2.2 snj event_deferred_cb_init_(&buffer->deferred,
410 1.2.2.2 snj event_base_get_npriorities(base) / 2,
411 1.2.2.2 snj evbuffer_deferred_callback, buffer);
412 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
413 1.2.2.2 snj return 0;
414 1.2.2.2 snj }
415 1.2.2.2 snj
416 1.2.2.2 snj int
417 1.2.2.2 snj evbuffer_enable_locking(struct evbuffer *buf, void *lock)
418 1.2.2.2 snj {
419 1.2.2.2 snj #ifdef EVENT__DISABLE_THREAD_SUPPORT
420 1.2.2.2 snj return -1;
421 1.2.2.2 snj #else
422 1.2.2.2 snj if (buf->lock)
423 1.2.2.2 snj return -1;
424 1.2.2.2 snj
425 1.2.2.2 snj if (!lock) {
426 1.2.2.2 snj EVTHREAD_ALLOC_LOCK(lock, EVTHREAD_LOCKTYPE_RECURSIVE);
427 1.2.2.2 snj if (!lock)
428 1.2.2.2 snj return -1;
429 1.2.2.2 snj buf->lock = lock;
430 1.2.2.2 snj buf->own_lock = 1;
431 1.2.2.2 snj } else {
432 1.2.2.2 snj buf->lock = lock;
433 1.2.2.2 snj buf->own_lock = 0;
434 1.2.2.2 snj }
435 1.2.2.2 snj
436 1.2.2.2 snj return 0;
437 1.2.2.2 snj #endif
438 1.2.2.2 snj }
439 1.2.2.2 snj
440 1.2.2.2 snj void
441 1.2.2.2 snj evbuffer_set_parent_(struct evbuffer *buf, struct bufferevent *bev)
442 1.2.2.2 snj {
443 1.2.2.2 snj EVBUFFER_LOCK(buf);
444 1.2.2.2 snj buf->parent = bev;
445 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
446 1.2.2.2 snj }
447 1.2.2.2 snj
448 1.2.2.2 snj static void
449 1.2.2.2 snj evbuffer_run_callbacks(struct evbuffer *buffer, int running_deferred)
450 1.2.2.2 snj {
451 1.2.2.2 snj struct evbuffer_cb_entry *cbent, *next;
452 1.2.2.2 snj struct evbuffer_cb_info info;
453 1.2.2.2 snj size_t new_size;
454 1.2.2.2 snj ev_uint32_t mask, masked_val;
455 1.2.2.2 snj int clear = 1;
456 1.2.2.2 snj
457 1.2.2.2 snj if (running_deferred) {
458 1.2.2.2 snj mask = EVBUFFER_CB_NODEFER|EVBUFFER_CB_ENABLED;
459 1.2.2.2 snj masked_val = EVBUFFER_CB_ENABLED;
460 1.2.2.2 snj } else if (buffer->deferred_cbs) {
461 1.2.2.2 snj mask = EVBUFFER_CB_NODEFER|EVBUFFER_CB_ENABLED;
462 1.2.2.2 snj masked_val = EVBUFFER_CB_NODEFER|EVBUFFER_CB_ENABLED;
463 1.2.2.2 snj /* Don't zero-out n_add/n_del, since the deferred callbacks
464 1.2.2.2 snj will want to see them. */
465 1.2.2.2 snj clear = 0;
466 1.2.2.2 snj } else {
467 1.2.2.2 snj mask = EVBUFFER_CB_ENABLED;
468 1.2.2.2 snj masked_val = EVBUFFER_CB_ENABLED;
469 1.2.2.2 snj }
470 1.2.2.2 snj
471 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(buffer);
472 1.2.2.2 snj
473 1.2.2.2 snj if (LIST_EMPTY(&buffer->callbacks)) {
474 1.2.2.2 snj buffer->n_add_for_cb = buffer->n_del_for_cb = 0;
475 1.2.2.2 snj return;
476 1.2.2.2 snj }
477 1.2.2.2 snj if (buffer->n_add_for_cb == 0 && buffer->n_del_for_cb == 0)
478 1.2.2.2 snj return;
479 1.2.2.2 snj
480 1.2.2.2 snj new_size = buffer->total_len;
481 1.2.2.2 snj info.orig_size = new_size + buffer->n_del_for_cb - buffer->n_add_for_cb;
482 1.2.2.2 snj info.n_added = buffer->n_add_for_cb;
483 1.2.2.2 snj info.n_deleted = buffer->n_del_for_cb;
484 1.2.2.2 snj if (clear) {
485 1.2.2.2 snj buffer->n_add_for_cb = 0;
486 1.2.2.2 snj buffer->n_del_for_cb = 0;
487 1.2.2.2 snj }
488 1.2.2.2 snj for (cbent = LIST_FIRST(&buffer->callbacks);
489 1.2.2.2 snj cbent != LIST_END(&buffer->callbacks);
490 1.2.2.2 snj cbent = next) {
491 1.2.2.2 snj /* Get the 'next' pointer now in case this callback decides
492 1.2.2.2 snj * to remove itself or something. */
493 1.2.2.2 snj next = LIST_NEXT(cbent, next);
494 1.2.2.2 snj
495 1.2.2.2 snj if ((cbent->flags & mask) != masked_val)
496 1.2.2.2 snj continue;
497 1.2.2.2 snj
498 1.2.2.2 snj if ((cbent->flags & EVBUFFER_CB_OBSOLETE))
499 1.2.2.2 snj cbent->cb.cb_obsolete(buffer,
500 1.2.2.2 snj info.orig_size, new_size, cbent->cbarg);
501 1.2.2.2 snj else
502 1.2.2.2 snj cbent->cb.cb_func(buffer, &info, cbent->cbarg);
503 1.2.2.2 snj }
504 1.2.2.2 snj }
505 1.2.2.2 snj
506 1.2.2.2 snj void
507 1.2.2.2 snj evbuffer_invoke_callbacks_(struct evbuffer *buffer)
508 1.2.2.2 snj {
509 1.2.2.2 snj if (LIST_EMPTY(&buffer->callbacks)) {
510 1.2.2.2 snj buffer->n_add_for_cb = buffer->n_del_for_cb = 0;
511 1.2.2.2 snj return;
512 1.2.2.2 snj }
513 1.2.2.2 snj
514 1.2.2.2 snj if (buffer->deferred_cbs) {
515 1.2.2.2 snj if (event_deferred_cb_schedule_(buffer->cb_queue, &buffer->deferred)) {
516 1.2.2.2 snj evbuffer_incref_and_lock_(buffer);
517 1.2.2.2 snj if (buffer->parent)
518 1.2.2.2 snj bufferevent_incref_(buffer->parent);
519 1.2.2.2 snj }
520 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
521 1.2.2.2 snj }
522 1.2.2.2 snj
523 1.2.2.2 snj evbuffer_run_callbacks(buffer, 0);
524 1.2.2.2 snj }
525 1.2.2.2 snj
526 1.2.2.2 snj static void
527 1.2.2.2 snj evbuffer_deferred_callback(struct event_callback *cb, void *arg)
528 1.2.2.2 snj {
529 1.2.2.2 snj struct bufferevent *parent = NULL;
530 1.2.2.2 snj struct evbuffer *buffer = arg;
531 1.2.2.2 snj
532 1.2.2.2 snj /* XXXX It would be better to run these callbacks without holding the
533 1.2.2.2 snj * lock */
534 1.2.2.2 snj EVBUFFER_LOCK(buffer);
535 1.2.2.2 snj parent = buffer->parent;
536 1.2.2.2 snj evbuffer_run_callbacks(buffer, 1);
537 1.2.2.2 snj evbuffer_decref_and_unlock_(buffer);
538 1.2.2.2 snj if (parent)
539 1.2.2.2 snj bufferevent_decref_(parent);
540 1.2.2.2 snj }
541 1.2.2.2 snj
542 1.2.2.2 snj static void
543 1.2.2.2 snj evbuffer_remove_all_callbacks(struct evbuffer *buffer)
544 1.2.2.2 snj {
545 1.2.2.2 snj struct evbuffer_cb_entry *cbent;
546 1.2.2.2 snj
547 1.2.2.2 snj while ((cbent = LIST_FIRST(&buffer->callbacks))) {
548 1.2.2.2 snj LIST_REMOVE(cbent, next);
549 1.2.2.2 snj mm_free(cbent);
550 1.2.2.2 snj }
551 1.2.2.2 snj }
552 1.2.2.2 snj
553 1.2.2.2 snj void
554 1.2.2.2 snj evbuffer_decref_and_unlock_(struct evbuffer *buffer)
555 1.2.2.2 snj {
556 1.2.2.2 snj struct evbuffer_chain *chain, *next;
557 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(buffer);
558 1.2.2.2 snj
559 1.2.2.2 snj EVUTIL_ASSERT(buffer->refcnt > 0);
560 1.2.2.2 snj
561 1.2.2.2 snj if (--buffer->refcnt > 0) {
562 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
563 1.2.2.2 snj return;
564 1.2.2.2 snj }
565 1.2.2.2 snj
566 1.2.2.2 snj for (chain = buffer->first; chain != NULL; chain = next) {
567 1.2.2.2 snj next = chain->next;
568 1.2.2.2 snj evbuffer_chain_free(chain);
569 1.2.2.2 snj }
570 1.2.2.2 snj evbuffer_remove_all_callbacks(buffer);
571 1.2.2.2 snj if (buffer->deferred_cbs)
572 1.2.2.2 snj event_deferred_cb_cancel_(buffer->cb_queue, &buffer->deferred);
573 1.2.2.2 snj
574 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
575 1.2.2.2 snj if (buffer->own_lock)
576 1.2.2.2 snj EVTHREAD_FREE_LOCK(buffer->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
577 1.2.2.2 snj mm_free(buffer);
578 1.2.2.2 snj }
579 1.2.2.2 snj
580 1.2.2.2 snj void
581 1.2.2.2 snj evbuffer_free(struct evbuffer *buffer)
582 1.2.2.2 snj {
583 1.2.2.2 snj EVBUFFER_LOCK(buffer);
584 1.2.2.2 snj evbuffer_decref_and_unlock_(buffer);
585 1.2.2.2 snj }
586 1.2.2.2 snj
587 1.2.2.2 snj void
588 1.2.2.2 snj evbuffer_lock(struct evbuffer *buf)
589 1.2.2.2 snj {
590 1.2.2.2 snj EVBUFFER_LOCK(buf);
591 1.2.2.2 snj }
592 1.2.2.2 snj
593 1.2.2.2 snj void
594 1.2.2.2 snj evbuffer_unlock(struct evbuffer *buf)
595 1.2.2.2 snj {
596 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
597 1.2.2.2 snj }
598 1.2.2.2 snj
599 1.2.2.2 snj size_t
600 1.2.2.2 snj evbuffer_get_length(const struct evbuffer *buffer)
601 1.2.2.2 snj {
602 1.2.2.2 snj size_t result;
603 1.2.2.2 snj
604 1.2.2.2 snj EVBUFFER_LOCK(buffer);
605 1.2.2.2 snj
606 1.2.2.2 snj result = (buffer->total_len);
607 1.2.2.2 snj
608 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
609 1.2.2.2 snj
610 1.2.2.2 snj return result;
611 1.2.2.2 snj }
612 1.2.2.2 snj
613 1.2.2.2 snj size_t
614 1.2.2.2 snj evbuffer_get_contiguous_space(const struct evbuffer *buf)
615 1.2.2.2 snj {
616 1.2.2.2 snj struct evbuffer_chain *chain;
617 1.2.2.2 snj size_t result;
618 1.2.2.2 snj
619 1.2.2.2 snj EVBUFFER_LOCK(buf);
620 1.2.2.2 snj chain = buf->first;
621 1.2.2.2 snj result = (chain != NULL ? chain->off : 0);
622 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
623 1.2.2.2 snj
624 1.2.2.2 snj return result;
625 1.2.2.2 snj }
626 1.2.2.2 snj
627 1.2.2.2 snj size_t
628 1.2.2.2 snj evbuffer_add_iovec(struct evbuffer * buf, struct evbuffer_iovec * vec, int n_vec) {
629 1.2.2.2 snj int n;
630 1.2.2.2 snj size_t res;
631 1.2.2.2 snj size_t to_alloc;
632 1.2.2.2 snj
633 1.2.2.2 snj EVBUFFER_LOCK(buf);
634 1.2.2.2 snj
635 1.2.2.2 snj res = to_alloc = 0;
636 1.2.2.2 snj
637 1.2.2.2 snj for (n = 0; n < n_vec; n++) {
638 1.2.2.2 snj to_alloc += vec[n].iov_len;
639 1.2.2.2 snj }
640 1.2.2.2 snj
641 1.2.2.2 snj if (evbuffer_expand_fast_(buf, to_alloc, 2) < 0) {
642 1.2.2.2 snj goto done;
643 1.2.2.2 snj }
644 1.2.2.2 snj
645 1.2.2.2 snj for (n = 0; n < n_vec; n++) {
646 1.2.2.2 snj /* XXX each 'add' call here does a bunch of setup that's
647 1.2.2.2 snj * obviated by evbuffer_expand_fast_, and some cleanup that we
648 1.2.2.2 snj * would like to do only once. Instead we should just extract
649 1.2.2.2 snj * the part of the code that's needed. */
650 1.2.2.2 snj
651 1.2.2.2 snj if (evbuffer_add(buf, vec[n].iov_base, vec[n].iov_len) < 0) {
652 1.2.2.2 snj goto done;
653 1.2.2.2 snj }
654 1.2.2.2 snj
655 1.2.2.2 snj res += vec[n].iov_len;
656 1.2.2.2 snj }
657 1.2.2.2 snj
658 1.2.2.2 snj done:
659 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
660 1.2.2.2 snj return res;
661 1.2.2.2 snj }
662 1.2.2.2 snj
663 1.2.2.2 snj int
664 1.2.2.2 snj evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
665 1.2.2.2 snj struct evbuffer_iovec *vec, int n_vecs)
666 1.2.2.2 snj {
667 1.2.2.2 snj struct evbuffer_chain *chain, **chainp;
668 1.2.2.2 snj int n = -1;
669 1.2.2.2 snj
670 1.2.2.2 snj EVBUFFER_LOCK(buf);
671 1.2.2.2 snj if (buf->freeze_end)
672 1.2.2.2 snj goto done;
673 1.2.2.2 snj if (n_vecs < 1)
674 1.2.2.2 snj goto done;
675 1.2.2.2 snj if (n_vecs == 1) {
676 1.2.2.2 snj if ((chain = evbuffer_expand_singlechain(buf, size)) == NULL)
677 1.2.2.2 snj goto done;
678 1.2.2.2 snj
679 1.2.2.2 snj vec[0].iov_base = CHAIN_SPACE_PTR(chain);
680 1.2.2.2 snj vec[0].iov_len = (size_t) CHAIN_SPACE_LEN(chain);
681 1.2.2.2 snj EVUTIL_ASSERT(size<0 || (size_t)vec[0].iov_len >= (size_t)size);
682 1.2.2.2 snj n = 1;
683 1.2.2.2 snj } else {
684 1.2.2.2 snj if (evbuffer_expand_fast_(buf, size, n_vecs)<0)
685 1.2.2.2 snj goto done;
686 1.2.2.2 snj n = evbuffer_read_setup_vecs_(buf, size, vec, n_vecs,
687 1.2.2.2 snj &chainp, 0);
688 1.2.2.2 snj }
689 1.2.2.2 snj
690 1.2.2.2 snj done:
691 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
692 1.2.2.2 snj return n;
693 1.2.2.2 snj
694 1.2.2.2 snj }
695 1.2.2.2 snj
696 1.2.2.2 snj static int
697 1.2.2.2 snj advance_last_with_data(struct evbuffer *buf)
698 1.2.2.2 snj {
699 1.2.2.2 snj int n = 0;
700 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(buf);
701 1.2.2.2 snj
702 1.2.2.2 snj if (!*buf->last_with_datap)
703 1.2.2.2 snj return 0;
704 1.2.2.2 snj
705 1.2.2.2 snj while ((*buf->last_with_datap)->next && (*buf->last_with_datap)->next->off) {
706 1.2.2.2 snj buf->last_with_datap = &(*buf->last_with_datap)->next;
707 1.2.2.2 snj ++n;
708 1.2.2.2 snj }
709 1.2.2.2 snj return n;
710 1.2.2.2 snj }
711 1.2.2.2 snj
712 1.2.2.2 snj int
713 1.2.2.2 snj evbuffer_commit_space(struct evbuffer *buf,
714 1.2.2.2 snj struct evbuffer_iovec *vec, int n_vecs)
715 1.2.2.2 snj {
716 1.2.2.2 snj struct evbuffer_chain *chain, **firstchainp, **chainp;
717 1.2.2.2 snj int result = -1;
718 1.2.2.2 snj size_t added = 0;
719 1.2.2.2 snj int i;
720 1.2.2.2 snj
721 1.2.2.2 snj EVBUFFER_LOCK(buf);
722 1.2.2.2 snj
723 1.2.2.2 snj if (buf->freeze_end)
724 1.2.2.2 snj goto done;
725 1.2.2.2 snj if (n_vecs == 0) {
726 1.2.2.2 snj result = 0;
727 1.2.2.2 snj goto done;
728 1.2.2.2 snj } else if (n_vecs == 1 &&
729 1.2.2.2 snj (buf->last && vec[0].iov_base == (void*)CHAIN_SPACE_PTR(buf->last))) {
730 1.2.2.2 snj /* The user only got or used one chain; it might not
731 1.2.2.2 snj * be the first one with space in it. */
732 1.2.2.2 snj if ((size_t)vec[0].iov_len > (size_t)CHAIN_SPACE_LEN(buf->last))
733 1.2.2.2 snj goto done;
734 1.2.2.2 snj buf->last->off += vec[0].iov_len;
735 1.2.2.2 snj added = vec[0].iov_len;
736 1.2.2.2 snj if (added)
737 1.2.2.2 snj advance_last_with_data(buf);
738 1.2.2.2 snj goto okay;
739 1.2.2.2 snj }
740 1.2.2.2 snj
741 1.2.2.2 snj /* Advance 'firstchain' to the first chain with space in it. */
742 1.2.2.2 snj firstchainp = buf->last_with_datap;
743 1.2.2.2 snj if (!*firstchainp)
744 1.2.2.2 snj goto done;
745 1.2.2.2 snj if (CHAIN_SPACE_LEN(*firstchainp) == 0) {
746 1.2.2.2 snj firstchainp = &(*firstchainp)->next;
747 1.2.2.2 snj }
748 1.2.2.2 snj
749 1.2.2.2 snj chain = *firstchainp;
750 1.2.2.2 snj /* pass 1: make sure that the pointers and lengths of vecs[] are in
751 1.2.2.2 snj * bounds before we try to commit anything. */
752 1.2.2.2 snj for (i=0; i<n_vecs; ++i) {
753 1.2.2.2 snj if (!chain)
754 1.2.2.2 snj goto done;
755 1.2.2.2 snj if (vec[i].iov_base != (void*)CHAIN_SPACE_PTR(chain) ||
756 1.2.2.2 snj (size_t)vec[i].iov_len > CHAIN_SPACE_LEN(chain))
757 1.2.2.2 snj goto done;
758 1.2.2.2 snj chain = chain->next;
759 1.2.2.2 snj }
760 1.2.2.2 snj /* pass 2: actually adjust all the chains. */
761 1.2.2.2 snj chainp = firstchainp;
762 1.2.2.2 snj for (i=0; i<n_vecs; ++i) {
763 1.2.2.2 snj (*chainp)->off += vec[i].iov_len;
764 1.2.2.2 snj added += vec[i].iov_len;
765 1.2.2.2 snj if (vec[i].iov_len) {
766 1.2.2.2 snj buf->last_with_datap = chainp;
767 1.2.2.2 snj }
768 1.2.2.2 snj chainp = &(*chainp)->next;
769 1.2.2.2 snj }
770 1.2.2.2 snj
771 1.2.2.2 snj okay:
772 1.2.2.2 snj buf->total_len += added;
773 1.2.2.2 snj buf->n_add_for_cb += added;
774 1.2.2.2 snj result = 0;
775 1.2.2.2 snj evbuffer_invoke_callbacks_(buf);
776 1.2.2.2 snj
777 1.2.2.2 snj done:
778 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
779 1.2.2.2 snj return result;
780 1.2.2.2 snj }
781 1.2.2.2 snj
782 1.2.2.2 snj static inline int
783 1.2.2.2 snj HAS_PINNED_R(struct evbuffer *buf)
784 1.2.2.2 snj {
785 1.2.2.2 snj return (buf->last && CHAIN_PINNED_R(buf->last));
786 1.2.2.2 snj }
787 1.2.2.2 snj
788 1.2.2.2 snj static inline void
789 1.2.2.2 snj ZERO_CHAIN(struct evbuffer *dst)
790 1.2.2.2 snj {
791 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(dst);
792 1.2.2.2 snj dst->first = NULL;
793 1.2.2.2 snj dst->last = NULL;
794 1.2.2.2 snj dst->last_with_datap = &(dst)->first;
795 1.2.2.2 snj dst->total_len = 0;
796 1.2.2.2 snj }
797 1.2.2.2 snj
798 1.2.2.2 snj /* Prepares the contents of src to be moved to another buffer by removing
799 1.2.2.2 snj * read-pinned chains. The first pinned chain is saved in first, and the
800 1.2.2.2 snj * last in last. If src has no read-pinned chains, first and last are set
801 1.2.2.2 snj * to NULL. */
802 1.2.2.2 snj static int
803 1.2.2.2 snj PRESERVE_PINNED(struct evbuffer *src, struct evbuffer_chain **first,
804 1.2.2.2 snj struct evbuffer_chain **last)
805 1.2.2.2 snj {
806 1.2.2.2 snj struct evbuffer_chain *chain, **pinned;
807 1.2.2.2 snj
808 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(src);
809 1.2.2.2 snj
810 1.2.2.2 snj if (!HAS_PINNED_R(src)) {
811 1.2.2.2 snj *first = *last = NULL;
812 1.2.2.2 snj return 0;
813 1.2.2.2 snj }
814 1.2.2.2 snj
815 1.2.2.2 snj pinned = src->last_with_datap;
816 1.2.2.2 snj if (!CHAIN_PINNED_R(*pinned))
817 1.2.2.2 snj pinned = &(*pinned)->next;
818 1.2.2.2 snj EVUTIL_ASSERT(CHAIN_PINNED_R(*pinned));
819 1.2.2.2 snj chain = *first = *pinned;
820 1.2.2.2 snj *last = src->last;
821 1.2.2.2 snj
822 1.2.2.2 snj /* If there's data in the first pinned chain, we need to allocate
823 1.2.2.2 snj * a new chain and copy the data over. */
824 1.2.2.2 snj if (chain->off) {
825 1.2.2.2 snj struct evbuffer_chain *tmp;
826 1.2.2.2 snj
827 1.2.2.2 snj EVUTIL_ASSERT(pinned == src->last_with_datap);
828 1.2.2.2 snj tmp = evbuffer_chain_new(chain->off);
829 1.2.2.2 snj if (!tmp)
830 1.2.2.2 snj return -1;
831 1.2.2.2 snj memcpy(tmp->buffer, chain->buffer + chain->misalign,
832 1.2.2.2 snj chain->off);
833 1.2.2.2 snj tmp->off = chain->off;
834 1.2.2.2 snj *src->last_with_datap = tmp;
835 1.2.2.2 snj src->last = tmp;
836 1.2.2.2 snj chain->misalign += chain->off;
837 1.2.2.2 snj chain->off = 0;
838 1.2.2.2 snj } else {
839 1.2.2.2 snj src->last = *src->last_with_datap;
840 1.2.2.2 snj *pinned = NULL;
841 1.2.2.2 snj }
842 1.2.2.2 snj
843 1.2.2.2 snj return 0;
844 1.2.2.2 snj }
845 1.2.2.2 snj
846 1.2.2.2 snj static inline void
847 1.2.2.2 snj RESTORE_PINNED(struct evbuffer *src, struct evbuffer_chain *pinned,
848 1.2.2.2 snj struct evbuffer_chain *last)
849 1.2.2.2 snj {
850 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(src);
851 1.2.2.2 snj
852 1.2.2.2 snj if (!pinned) {
853 1.2.2.2 snj ZERO_CHAIN(src);
854 1.2.2.2 snj return;
855 1.2.2.2 snj }
856 1.2.2.2 snj
857 1.2.2.2 snj src->first = pinned;
858 1.2.2.2 snj src->last = last;
859 1.2.2.2 snj src->last_with_datap = &src->first;
860 1.2.2.2 snj src->total_len = 0;
861 1.2.2.2 snj }
862 1.2.2.2 snj
863 1.2.2.2 snj static inline void
864 1.2.2.2 snj COPY_CHAIN(struct evbuffer *dst, struct evbuffer *src)
865 1.2.2.2 snj {
866 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(dst);
867 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(src);
868 1.2.2.2 snj dst->first = src->first;
869 1.2.2.2 snj if (src->last_with_datap == &src->first)
870 1.2.2.2 snj dst->last_with_datap = &dst->first;
871 1.2.2.2 snj else
872 1.2.2.2 snj dst->last_with_datap = src->last_with_datap;
873 1.2.2.2 snj dst->last = src->last;
874 1.2.2.2 snj dst->total_len = src->total_len;
875 1.2.2.2 snj }
876 1.2.2.2 snj
877 1.2.2.2 snj static void
878 1.2.2.2 snj APPEND_CHAIN(struct evbuffer *dst, struct evbuffer *src)
879 1.2.2.2 snj {
880 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(dst);
881 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(src);
882 1.2.2.2 snj dst->last->next = src->first;
883 1.2.2.2 snj if (src->last_with_datap == &src->first)
884 1.2.2.2 snj dst->last_with_datap = &dst->last->next;
885 1.2.2.2 snj else
886 1.2.2.2 snj dst->last_with_datap = src->last_with_datap;
887 1.2.2.2 snj dst->last = src->last;
888 1.2.2.2 snj dst->total_len += src->total_len;
889 1.2.2.2 snj }
890 1.2.2.2 snj
891 1.2.2.2 snj static inline void
892 1.2.2.2 snj APPEND_CHAIN_MULTICAST(struct evbuffer *dst, struct evbuffer *src)
893 1.2.2.2 snj {
894 1.2.2.2 snj struct evbuffer_chain *tmp;
895 1.2.2.2 snj struct evbuffer_chain *chain = src->first;
896 1.2.2.2 snj struct evbuffer_multicast_parent *extra;
897 1.2.2.2 snj
898 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(dst);
899 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(src);
900 1.2.2.2 snj
901 1.2.2.2 snj for (; chain; chain = chain->next) {
902 1.2.2.2 snj if (!chain->off || chain->flags & EVBUFFER_DANGLING) {
903 1.2.2.2 snj /* skip empty chains */
904 1.2.2.2 snj continue;
905 1.2.2.2 snj }
906 1.2.2.2 snj
907 1.2.2.2 snj tmp = evbuffer_chain_new(sizeof(struct evbuffer_multicast_parent));
908 1.2.2.2 snj if (!tmp) {
909 1.2.2.2 snj event_warn("%s: out of memory", __func__);
910 1.2.2.2 snj return;
911 1.2.2.2 snj }
912 1.2.2.2 snj extra = EVBUFFER_CHAIN_EXTRA(struct evbuffer_multicast_parent, tmp);
913 1.2.2.2 snj /* reference evbuffer containing source chain so it
914 1.2.2.2 snj * doesn't get released while the chain is still
915 1.2.2.2 snj * being referenced to */
916 1.2.2.2 snj evbuffer_incref_(src);
917 1.2.2.2 snj extra->source = src;
918 1.2.2.2 snj /* reference source chain which now becomes immutable */
919 1.2.2.2 snj evbuffer_chain_incref(chain);
920 1.2.2.2 snj extra->parent = chain;
921 1.2.2.2 snj chain->flags |= EVBUFFER_IMMUTABLE;
922 1.2.2.2 snj tmp->buffer_len = chain->buffer_len;
923 1.2.2.2 snj tmp->misalign = chain->misalign;
924 1.2.2.2 snj tmp->off = chain->off;
925 1.2.2.2 snj tmp->flags |= EVBUFFER_MULTICAST|EVBUFFER_IMMUTABLE;
926 1.2.2.2 snj tmp->buffer = chain->buffer;
927 1.2.2.2 snj evbuffer_chain_insert(dst, tmp);
928 1.2.2.2 snj }
929 1.2.2.2 snj }
930 1.2.2.2 snj
931 1.2.2.2 snj static void
932 1.2.2.2 snj PREPEND_CHAIN(struct evbuffer *dst, struct evbuffer *src)
933 1.2.2.2 snj {
934 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(dst);
935 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(src);
936 1.2.2.2 snj src->last->next = dst->first;
937 1.2.2.2 snj dst->first = src->first;
938 1.2.2.2 snj dst->total_len += src->total_len;
939 1.2.2.2 snj if (*dst->last_with_datap == NULL) {
940 1.2.2.2 snj if (src->last_with_datap == &(src)->first)
941 1.2.2.2 snj dst->last_with_datap = &dst->first;
942 1.2.2.2 snj else
943 1.2.2.2 snj dst->last_with_datap = src->last_with_datap;
944 1.2.2.2 snj } else if (dst->last_with_datap == &dst->first) {
945 1.2.2.2 snj dst->last_with_datap = &src->last->next;
946 1.2.2.2 snj }
947 1.2.2.2 snj }
948 1.2.2.2 snj
949 1.2.2.2 snj int
950 1.2.2.2 snj evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf)
951 1.2.2.2 snj {
952 1.2.2.2 snj struct evbuffer_chain *pinned, *last;
953 1.2.2.2 snj size_t in_total_len, out_total_len;
954 1.2.2.2 snj int result = 0;
955 1.2.2.2 snj
956 1.2.2.2 snj EVBUFFER_LOCK2(inbuf, outbuf);
957 1.2.2.2 snj in_total_len = inbuf->total_len;
958 1.2.2.2 snj out_total_len = outbuf->total_len;
959 1.2.2.2 snj
960 1.2.2.2 snj if (in_total_len == 0 || outbuf == inbuf)
961 1.2.2.2 snj goto done;
962 1.2.2.2 snj
963 1.2.2.2 snj if (outbuf->freeze_end || inbuf->freeze_start) {
964 1.2.2.2 snj result = -1;
965 1.2.2.2 snj goto done;
966 1.2.2.2 snj }
967 1.2.2.2 snj
968 1.2.2.2 snj if (PRESERVE_PINNED(inbuf, &pinned, &last) < 0) {
969 1.2.2.2 snj result = -1;
970 1.2.2.2 snj goto done;
971 1.2.2.2 snj }
972 1.2.2.2 snj
973 1.2.2.2 snj if (out_total_len == 0) {
974 1.2.2.2 snj /* There might be an empty chain at the start of outbuf; free
975 1.2.2.2 snj * it. */
976 1.2.2.2 snj evbuffer_free_all_chains(outbuf->first);
977 1.2.2.2 snj COPY_CHAIN(outbuf, inbuf);
978 1.2.2.2 snj } else {
979 1.2.2.2 snj APPEND_CHAIN(outbuf, inbuf);
980 1.2.2.2 snj }
981 1.2.2.2 snj
982 1.2.2.2 snj RESTORE_PINNED(inbuf, pinned, last);
983 1.2.2.2 snj
984 1.2.2.2 snj inbuf->n_del_for_cb += in_total_len;
985 1.2.2.2 snj outbuf->n_add_for_cb += in_total_len;
986 1.2.2.2 snj
987 1.2.2.2 snj evbuffer_invoke_callbacks_(inbuf);
988 1.2.2.2 snj evbuffer_invoke_callbacks_(outbuf);
989 1.2.2.2 snj
990 1.2.2.2 snj done:
991 1.2.2.2 snj EVBUFFER_UNLOCK2(inbuf, outbuf);
992 1.2.2.2 snj return result;
993 1.2.2.2 snj }
994 1.2.2.2 snj
995 1.2.2.2 snj int
996 1.2.2.2 snj evbuffer_add_buffer_reference(struct evbuffer *outbuf, struct evbuffer *inbuf)
997 1.2.2.2 snj {
998 1.2.2.2 snj size_t in_total_len, out_total_len;
999 1.2.2.2 snj struct evbuffer_chain *chain;
1000 1.2.2.2 snj int result = 0;
1001 1.2.2.2 snj
1002 1.2.2.2 snj EVBUFFER_LOCK2(inbuf, outbuf);
1003 1.2.2.2 snj in_total_len = inbuf->total_len;
1004 1.2.2.2 snj out_total_len = outbuf->total_len;
1005 1.2.2.2 snj chain = inbuf->first;
1006 1.2.2.2 snj
1007 1.2.2.2 snj if (in_total_len == 0)
1008 1.2.2.2 snj goto done;
1009 1.2.2.2 snj
1010 1.2.2.2 snj if (outbuf->freeze_end || outbuf == inbuf) {
1011 1.2.2.2 snj result = -1;
1012 1.2.2.2 snj goto done;
1013 1.2.2.2 snj }
1014 1.2.2.2 snj
1015 1.2.2.2 snj for (; chain; chain = chain->next) {
1016 1.2.2.2 snj if ((chain->flags & (EVBUFFER_FILESEGMENT|EVBUFFER_SENDFILE|EVBUFFER_MULTICAST)) != 0) {
1017 1.2.2.2 snj /* chain type can not be referenced */
1018 1.2.2.2 snj result = -1;
1019 1.2.2.2 snj goto done;
1020 1.2.2.2 snj }
1021 1.2.2.2 snj }
1022 1.2.2.2 snj
1023 1.2.2.2 snj if (out_total_len == 0) {
1024 1.2.2.2 snj /* There might be an empty chain at the start of outbuf; free
1025 1.2.2.2 snj * it. */
1026 1.2.2.2 snj evbuffer_free_all_chains(outbuf->first);
1027 1.2.2.2 snj }
1028 1.2.2.2 snj APPEND_CHAIN_MULTICAST(outbuf, inbuf);
1029 1.2.2.2 snj
1030 1.2.2.2 snj outbuf->n_add_for_cb += in_total_len;
1031 1.2.2.2 snj evbuffer_invoke_callbacks_(outbuf);
1032 1.2.2.2 snj
1033 1.2.2.2 snj done:
1034 1.2.2.2 snj EVBUFFER_UNLOCK2(inbuf, outbuf);
1035 1.2.2.2 snj return result;
1036 1.2.2.2 snj }
1037 1.2.2.2 snj
1038 1.2.2.2 snj int
1039 1.2.2.2 snj evbuffer_prepend_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf)
1040 1.2.2.2 snj {
1041 1.2.2.2 snj struct evbuffer_chain *pinned, *last;
1042 1.2.2.2 snj size_t in_total_len, out_total_len;
1043 1.2.2.2 snj int result = 0;
1044 1.2.2.2 snj
1045 1.2.2.2 snj EVBUFFER_LOCK2(inbuf, outbuf);
1046 1.2.2.2 snj
1047 1.2.2.2 snj in_total_len = inbuf->total_len;
1048 1.2.2.2 snj out_total_len = outbuf->total_len;
1049 1.2.2.2 snj
1050 1.2.2.2 snj if (!in_total_len || inbuf == outbuf)
1051 1.2.2.2 snj goto done;
1052 1.2.2.2 snj
1053 1.2.2.2 snj if (outbuf->freeze_start || inbuf->freeze_start) {
1054 1.2.2.2 snj result = -1;
1055 1.2.2.2 snj goto done;
1056 1.2.2.2 snj }
1057 1.2.2.2 snj
1058 1.2.2.2 snj if (PRESERVE_PINNED(inbuf, &pinned, &last) < 0) {
1059 1.2.2.2 snj result = -1;
1060 1.2.2.2 snj goto done;
1061 1.2.2.2 snj }
1062 1.2.2.2 snj
1063 1.2.2.2 snj if (out_total_len == 0) {
1064 1.2.2.2 snj /* There might be an empty chain at the start of outbuf; free
1065 1.2.2.2 snj * it. */
1066 1.2.2.2 snj evbuffer_free_all_chains(outbuf->first);
1067 1.2.2.2 snj COPY_CHAIN(outbuf, inbuf);
1068 1.2.2.2 snj } else {
1069 1.2.2.2 snj PREPEND_CHAIN(outbuf, inbuf);
1070 1.2.2.2 snj }
1071 1.2.2.2 snj
1072 1.2.2.2 snj RESTORE_PINNED(inbuf, pinned, last);
1073 1.2.2.2 snj
1074 1.2.2.2 snj inbuf->n_del_for_cb += in_total_len;
1075 1.2.2.2 snj outbuf->n_add_for_cb += in_total_len;
1076 1.2.2.2 snj
1077 1.2.2.2 snj evbuffer_invoke_callbacks_(inbuf);
1078 1.2.2.2 snj evbuffer_invoke_callbacks_(outbuf);
1079 1.2.2.2 snj done:
1080 1.2.2.2 snj EVBUFFER_UNLOCK2(inbuf, outbuf);
1081 1.2.2.2 snj return result;
1082 1.2.2.2 snj }
1083 1.2.2.2 snj
1084 1.2.2.2 snj int
1085 1.2.2.2 snj evbuffer_drain(struct evbuffer *buf, size_t len)
1086 1.2.2.2 snj {
1087 1.2.2.2 snj struct evbuffer_chain *chain, *next;
1088 1.2.2.2 snj size_t remaining, old_len;
1089 1.2.2.2 snj int result = 0;
1090 1.2.2.2 snj
1091 1.2.2.2 snj EVBUFFER_LOCK(buf);
1092 1.2.2.2 snj old_len = buf->total_len;
1093 1.2.2.2 snj
1094 1.2.2.2 snj if (old_len == 0)
1095 1.2.2.2 snj goto done;
1096 1.2.2.2 snj
1097 1.2.2.2 snj if (buf->freeze_start) {
1098 1.2.2.2 snj result = -1;
1099 1.2.2.2 snj goto done;
1100 1.2.2.2 snj }
1101 1.2.2.2 snj
1102 1.2.2.2 snj if (len >= old_len && !HAS_PINNED_R(buf)) {
1103 1.2.2.2 snj len = old_len;
1104 1.2.2.2 snj for (chain = buf->first; chain != NULL; chain = next) {
1105 1.2.2.2 snj next = chain->next;
1106 1.2.2.2 snj evbuffer_chain_free(chain);
1107 1.2.2.2 snj }
1108 1.2.2.2 snj
1109 1.2.2.2 snj ZERO_CHAIN(buf);
1110 1.2.2.2 snj } else {
1111 1.2.2.2 snj if (len >= old_len)
1112 1.2.2.2 snj len = old_len;
1113 1.2.2.2 snj
1114 1.2.2.2 snj buf->total_len -= len;
1115 1.2.2.2 snj remaining = len;
1116 1.2.2.2 snj for (chain = buf->first;
1117 1.2.2.2 snj remaining >= chain->off;
1118 1.2.2.2 snj chain = next) {
1119 1.2.2.2 snj next = chain->next;
1120 1.2.2.2 snj remaining -= chain->off;
1121 1.2.2.2 snj
1122 1.2.2.2 snj if (chain == *buf->last_with_datap) {
1123 1.2.2.2 snj buf->last_with_datap = &buf->first;
1124 1.2.2.2 snj }
1125 1.2.2.2 snj if (&chain->next == buf->last_with_datap)
1126 1.2.2.2 snj buf->last_with_datap = &buf->first;
1127 1.2.2.2 snj
1128 1.2.2.2 snj if (CHAIN_PINNED_R(chain)) {
1129 1.2.2.2 snj EVUTIL_ASSERT(remaining == 0);
1130 1.2.2.2 snj chain->misalign += chain->off;
1131 1.2.2.2 snj chain->off = 0;
1132 1.2.2.2 snj break;
1133 1.2.2.2 snj } else
1134 1.2.2.2 snj evbuffer_chain_free(chain);
1135 1.2.2.2 snj }
1136 1.2.2.2 snj
1137 1.2.2.2 snj buf->first = chain;
1138 1.2.2.2 snj chain->misalign += remaining;
1139 1.2.2.2 snj chain->off -= remaining;
1140 1.2.2.2 snj }
1141 1.2.2.2 snj
1142 1.2.2.2 snj buf->n_del_for_cb += len;
1143 1.2.2.2 snj /* Tell someone about changes in this buffer */
1144 1.2.2.2 snj evbuffer_invoke_callbacks_(buf);
1145 1.2.2.2 snj
1146 1.2.2.2 snj done:
1147 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
1148 1.2.2.2 snj return result;
1149 1.2.2.2 snj }
1150 1.2.2.2 snj
1151 1.2.2.2 snj /* Reads data from an event buffer and drains the bytes read */
1152 1.2.2.2 snj int
1153 1.2.2.2 snj evbuffer_remove(struct evbuffer *buf, void *data_out, size_t datlen)
1154 1.2.2.2 snj {
1155 1.2.2.2 snj ev_ssize_t n;
1156 1.2.2.2 snj EVBUFFER_LOCK(buf);
1157 1.2.2.2 snj n = evbuffer_copyout_from(buf, NULL, data_out, datlen);
1158 1.2.2.2 snj if (n > 0) {
1159 1.2.2.2 snj if (evbuffer_drain(buf, n)<0)
1160 1.2.2.2 snj n = -1;
1161 1.2.2.2 snj }
1162 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
1163 1.2.2.2 snj return (int)n;
1164 1.2.2.2 snj }
1165 1.2.2.2 snj
1166 1.2.2.2 snj ev_ssize_t
1167 1.2.2.2 snj evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen)
1168 1.2.2.2 snj {
1169 1.2.2.2 snj return evbuffer_copyout_from(buf, NULL, data_out, datlen);
1170 1.2.2.2 snj }
1171 1.2.2.2 snj
1172 1.2.2.2 snj ev_ssize_t
1173 1.2.2.2 snj evbuffer_copyout_from(struct evbuffer *buf, const struct evbuffer_ptr *pos,
1174 1.2.2.2 snj void *data_out, size_t datlen)
1175 1.2.2.2 snj {
1176 1.2.2.2 snj /*XXX fails badly on sendfile case. */
1177 1.2.2.2 snj struct evbuffer_chain *chain;
1178 1.2.2.2 snj char *data = data_out;
1179 1.2.2.2 snj size_t nread;
1180 1.2.2.2 snj ev_ssize_t result = 0;
1181 1.2.2.2 snj size_t pos_in_chain;
1182 1.2.2.2 snj
1183 1.2.2.2 snj EVBUFFER_LOCK(buf);
1184 1.2.2.2 snj
1185 1.2.2.2 snj if (pos) {
1186 1.2.2.2 snj chain = pos->internal_.chain;
1187 1.2.2.2 snj pos_in_chain = pos->internal_.pos_in_chain;
1188 1.2.2.2 snj if (datlen + pos->pos > buf->total_len)
1189 1.2.2.2 snj datlen = buf->total_len - pos->pos;
1190 1.2.2.2 snj } else {
1191 1.2.2.2 snj chain = buf->first;
1192 1.2.2.2 snj pos_in_chain = 0;
1193 1.2.2.2 snj if (datlen > buf->total_len)
1194 1.2.2.2 snj datlen = buf->total_len;
1195 1.2.2.2 snj }
1196 1.2.2.2 snj
1197 1.2.2.2 snj
1198 1.2.2.2 snj if (datlen == 0)
1199 1.2.2.2 snj goto done;
1200 1.2.2.2 snj
1201 1.2.2.2 snj if (buf->freeze_start) {
1202 1.2.2.2 snj result = -1;
1203 1.2.2.2 snj goto done;
1204 1.2.2.2 snj }
1205 1.2.2.2 snj
1206 1.2.2.2 snj nread = datlen;
1207 1.2.2.2 snj
1208 1.2.2.2 snj while (datlen && datlen >= chain->off - pos_in_chain) {
1209 1.2.2.2 snj size_t copylen = chain->off - pos_in_chain;
1210 1.2.2.2 snj memcpy(data,
1211 1.2.2.2 snj chain->buffer + chain->misalign + pos_in_chain,
1212 1.2.2.2 snj copylen);
1213 1.2.2.2 snj data += copylen;
1214 1.2.2.2 snj datlen -= copylen;
1215 1.2.2.2 snj
1216 1.2.2.2 snj chain = chain->next;
1217 1.2.2.2 snj pos_in_chain = 0;
1218 1.2.2.2 snj EVUTIL_ASSERT(chain || datlen==0);
1219 1.2.2.2 snj }
1220 1.2.2.2 snj
1221 1.2.2.2 snj if (datlen) {
1222 1.2.2.2 snj EVUTIL_ASSERT(chain);
1223 1.2.2.2 snj memcpy(data, chain->buffer + chain->misalign + pos_in_chain,
1224 1.2.2.2 snj datlen);
1225 1.2.2.2 snj }
1226 1.2.2.2 snj
1227 1.2.2.2 snj result = nread;
1228 1.2.2.2 snj done:
1229 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
1230 1.2.2.2 snj return result;
1231 1.2.2.2 snj }
1232 1.2.2.2 snj
1233 1.2.2.2 snj /* reads data from the src buffer to the dst buffer, avoids memcpy as
1234 1.2.2.2 snj * possible. */
1235 1.2.2.2 snj /* XXXX should return ev_ssize_t */
1236 1.2.2.2 snj int
1237 1.2.2.2 snj evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst,
1238 1.2.2.2 snj size_t datlen)
1239 1.2.2.2 snj {
1240 1.2.2.2 snj /*XXX We should have an option to force this to be zero-copy.*/
1241 1.2.2.2 snj
1242 1.2.2.2 snj /*XXX can fail badly on sendfile case. */
1243 1.2.2.2 snj struct evbuffer_chain *chain, *previous;
1244 1.2.2.2 snj size_t nread = 0;
1245 1.2.2.2 snj int result;
1246 1.2.2.2 snj
1247 1.2.2.2 snj EVBUFFER_LOCK2(src, dst);
1248 1.2.2.2 snj
1249 1.2.2.2 snj chain = previous = src->first;
1250 1.2.2.2 snj
1251 1.2.2.2 snj if (datlen == 0 || dst == src) {
1252 1.2.2.2 snj result = 0;
1253 1.2.2.2 snj goto done;
1254 1.2.2.2 snj }
1255 1.2.2.2 snj
1256 1.2.2.2 snj if (dst->freeze_end || src->freeze_start) {
1257 1.2.2.2 snj result = -1;
1258 1.2.2.2 snj goto done;
1259 1.2.2.2 snj }
1260 1.2.2.2 snj
1261 1.2.2.2 snj /* short-cut if there is no more data buffered */
1262 1.2.2.2 snj if (datlen >= src->total_len) {
1263 1.2.2.2 snj datlen = src->total_len;
1264 1.2.2.2 snj evbuffer_add_buffer(dst, src);
1265 1.2.2.2 snj result = (int)datlen; /*XXXX should return ev_ssize_t*/
1266 1.2.2.2 snj goto done;
1267 1.2.2.2 snj }
1268 1.2.2.2 snj
1269 1.2.2.2 snj /* removes chains if possible */
1270 1.2.2.2 snj while (chain->off <= datlen) {
1271 1.2.2.2 snj /* We can't remove the last with data from src unless we
1272 1.2.2.2 snj * remove all chains, in which case we would have done the if
1273 1.2.2.2 snj * block above */
1274 1.2.2.2 snj EVUTIL_ASSERT(chain != *src->last_with_datap);
1275 1.2.2.2 snj nread += chain->off;
1276 1.2.2.2 snj datlen -= chain->off;
1277 1.2.2.2 snj previous = chain;
1278 1.2.2.2 snj if (src->last_with_datap == &chain->next)
1279 1.2.2.2 snj src->last_with_datap = &src->first;
1280 1.2.2.2 snj chain = chain->next;
1281 1.2.2.2 snj }
1282 1.2.2.2 snj
1283 1.2.2.2 snj if (nread) {
1284 1.2.2.2 snj /* we can remove the chain */
1285 1.2.2.2 snj struct evbuffer_chain **chp;
1286 1.2.2.2 snj chp = evbuffer_free_trailing_empty_chains(dst);
1287 1.2.2.2 snj
1288 1.2.2.2 snj if (dst->first == NULL) {
1289 1.2.2.2 snj dst->first = src->first;
1290 1.2.2.2 snj } else {
1291 1.2.2.2 snj *chp = src->first;
1292 1.2.2.2 snj }
1293 1.2.2.2 snj dst->last = previous;
1294 1.2.2.2 snj previous->next = NULL;
1295 1.2.2.2 snj src->first = chain;
1296 1.2.2.2 snj advance_last_with_data(dst);
1297 1.2.2.2 snj
1298 1.2.2.2 snj dst->total_len += nread;
1299 1.2.2.2 snj dst->n_add_for_cb += nread;
1300 1.2.2.2 snj }
1301 1.2.2.2 snj
1302 1.2.2.2 snj /* we know that there is more data in the src buffer than
1303 1.2.2.2 snj * we want to read, so we manually drain the chain */
1304 1.2.2.2 snj evbuffer_add(dst, chain->buffer + chain->misalign, datlen);
1305 1.2.2.2 snj chain->misalign += datlen;
1306 1.2.2.2 snj chain->off -= datlen;
1307 1.2.2.2 snj nread += datlen;
1308 1.2.2.2 snj
1309 1.2.2.2 snj /* You might think we would want to increment dst->n_add_for_cb
1310 1.2.2.2 snj * here too. But evbuffer_add above already took care of that.
1311 1.2.2.2 snj */
1312 1.2.2.2 snj src->total_len -= nread;
1313 1.2.2.2 snj src->n_del_for_cb += nread;
1314 1.2.2.2 snj
1315 1.2.2.2 snj if (nread) {
1316 1.2.2.2 snj evbuffer_invoke_callbacks_(dst);
1317 1.2.2.2 snj evbuffer_invoke_callbacks_(src);
1318 1.2.2.2 snj }
1319 1.2.2.2 snj result = (int)nread;/*XXXX should change return type */
1320 1.2.2.2 snj
1321 1.2.2.2 snj done:
1322 1.2.2.2 snj EVBUFFER_UNLOCK2(src, dst);
1323 1.2.2.2 snj return result;
1324 1.2.2.2 snj }
1325 1.2.2.2 snj
1326 1.2.2.2 snj unsigned char *
1327 1.2.2.2 snj evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size)
1328 1.2.2.2 snj {
1329 1.2.2.2 snj struct evbuffer_chain *chain, *next, *tmp, *last_with_data;
1330 1.2.2.2 snj unsigned char *buffer, *result = NULL;
1331 1.2.2.2 snj ev_ssize_t remaining;
1332 1.2.2.2 snj int removed_last_with_data = 0;
1333 1.2.2.2 snj int removed_last_with_datap = 0;
1334 1.2.2.2 snj
1335 1.2.2.2 snj EVBUFFER_LOCK(buf);
1336 1.2.2.2 snj
1337 1.2.2.2 snj chain = buf->first;
1338 1.2.2.2 snj
1339 1.2.2.2 snj if (size < 0)
1340 1.2.2.2 snj size = buf->total_len;
1341 1.2.2.2 snj /* if size > buf->total_len, we cannot guarantee to the user that she
1342 1.2.2.2 snj * is going to have a long enough buffer afterwards; so we return
1343 1.2.2.2 snj * NULL */
1344 1.2.2.2 snj if (size == 0 || (size_t)size > buf->total_len)
1345 1.2.2.2 snj goto done;
1346 1.2.2.2 snj
1347 1.2.2.2 snj /* No need to pull up anything; the first size bytes are
1348 1.2.2.2 snj * already here. */
1349 1.2.2.2 snj if (chain->off >= (size_t)size) {
1350 1.2.2.2 snj result = chain->buffer + chain->misalign;
1351 1.2.2.2 snj goto done;
1352 1.2.2.2 snj }
1353 1.2.2.2 snj
1354 1.2.2.2 snj /* Make sure that none of the chains we need to copy from is pinned. */
1355 1.2.2.2 snj remaining = size - chain->off;
1356 1.2.2.2 snj EVUTIL_ASSERT(remaining >= 0);
1357 1.2.2.2 snj for (tmp=chain->next; tmp; tmp=tmp->next) {
1358 1.2.2.2 snj if (CHAIN_PINNED(tmp))
1359 1.2.2.2 snj goto done;
1360 1.2.2.2 snj if (tmp->off >= (size_t)remaining)
1361 1.2.2.2 snj break;
1362 1.2.2.2 snj remaining -= tmp->off;
1363 1.2.2.2 snj }
1364 1.2.2.2 snj
1365 1.2.2.2 snj if (CHAIN_PINNED(chain)) {
1366 1.2.2.2 snj size_t old_off = chain->off;
1367 1.2.2.2 snj if (CHAIN_SPACE_LEN(chain) < size - chain->off) {
1368 1.2.2.2 snj /* not enough room at end of chunk. */
1369 1.2.2.2 snj goto done;
1370 1.2.2.2 snj }
1371 1.2.2.2 snj buffer = CHAIN_SPACE_PTR(chain);
1372 1.2.2.2 snj tmp = chain;
1373 1.2.2.2 snj tmp->off = size;
1374 1.2.2.2 snj size -= old_off;
1375 1.2.2.2 snj chain = chain->next;
1376 1.2.2.2 snj } else if (chain->buffer_len - chain->misalign >= (size_t)size) {
1377 1.2.2.2 snj /* already have enough space in the first chain */
1378 1.2.2.2 snj size_t old_off = chain->off;
1379 1.2.2.2 snj buffer = chain->buffer + chain->misalign + chain->off;
1380 1.2.2.2 snj tmp = chain;
1381 1.2.2.2 snj tmp->off = size;
1382 1.2.2.2 snj size -= old_off;
1383 1.2.2.2 snj chain = chain->next;
1384 1.2.2.2 snj } else {
1385 1.2.2.2 snj if ((tmp = evbuffer_chain_new(size)) == NULL) {
1386 1.2.2.2 snj event_warn("%s: out of memory", __func__);
1387 1.2.2.2 snj goto done;
1388 1.2.2.2 snj }
1389 1.2.2.2 snj buffer = tmp->buffer;
1390 1.2.2.2 snj tmp->off = size;
1391 1.2.2.2 snj buf->first = tmp;
1392 1.2.2.2 snj }
1393 1.2.2.2 snj
1394 1.2.2.2 snj /* TODO(niels): deal with buffers that point to NULL like sendfile */
1395 1.2.2.2 snj
1396 1.2.2.2 snj /* Copy and free every chunk that will be entirely pulled into tmp */
1397 1.2.2.2 snj last_with_data = *buf->last_with_datap;
1398 1.2.2.2 snj for (; chain != NULL && (size_t)size >= chain->off; chain = next) {
1399 1.2.2.2 snj next = chain->next;
1400 1.2.2.2 snj
1401 1.2.2.2 snj memcpy(buffer, chain->buffer + chain->misalign, chain->off);
1402 1.2.2.2 snj size -= chain->off;
1403 1.2.2.2 snj buffer += chain->off;
1404 1.2.2.2 snj if (chain == last_with_data)
1405 1.2.2.2 snj removed_last_with_data = 1;
1406 1.2.2.2 snj if (&chain->next == buf->last_with_datap)
1407 1.2.2.2 snj removed_last_with_datap = 1;
1408 1.2.2.2 snj
1409 1.2.2.2 snj evbuffer_chain_free(chain);
1410 1.2.2.2 snj }
1411 1.2.2.2 snj
1412 1.2.2.2 snj if (chain != NULL) {
1413 1.2.2.2 snj memcpy(buffer, chain->buffer + chain->misalign, size);
1414 1.2.2.2 snj chain->misalign += size;
1415 1.2.2.2 snj chain->off -= size;
1416 1.2.2.2 snj } else {
1417 1.2.2.2 snj buf->last = tmp;
1418 1.2.2.2 snj }
1419 1.2.2.2 snj
1420 1.2.2.2 snj tmp->next = chain;
1421 1.2.2.2 snj
1422 1.2.2.2 snj if (removed_last_with_data) {
1423 1.2.2.2 snj buf->last_with_datap = &buf->first;
1424 1.2.2.2 snj } else if (removed_last_with_datap) {
1425 1.2.2.2 snj if (buf->first->next && buf->first->next->off)
1426 1.2.2.2 snj buf->last_with_datap = &buf->first->next;
1427 1.2.2.2 snj else
1428 1.2.2.2 snj buf->last_with_datap = &buf->first;
1429 1.2.2.2 snj }
1430 1.2.2.2 snj
1431 1.2.2.2 snj result = (tmp->buffer + tmp->misalign);
1432 1.2.2.2 snj
1433 1.2.2.2 snj done:
1434 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
1435 1.2.2.2 snj return result;
1436 1.2.2.2 snj }
1437 1.2.2.2 snj
1438 1.2.2.2 snj /*
1439 1.2.2.2 snj * Reads a line terminated by either '\r\n', '\n\r' or '\r' or '\n'.
1440 1.2.2.2 snj * The returned buffer needs to be freed by the called.
1441 1.2.2.2 snj */
1442 1.2.2.2 snj char *
1443 1.2.2.2 snj evbuffer_readline(struct evbuffer *buffer)
1444 1.2.2.2 snj {
1445 1.2.2.2 snj return evbuffer_readln(buffer, NULL, EVBUFFER_EOL_ANY);
1446 1.2.2.2 snj }
1447 1.2.2.2 snj
1448 1.2.2.2 snj static inline ev_ssize_t
1449 1.2.2.2 snj evbuffer_strchr(struct evbuffer_ptr *it, const char chr)
1450 1.2.2.2 snj {
1451 1.2.2.2 snj struct evbuffer_chain *chain = it->internal_.chain;
1452 1.2.2.2 snj size_t i = it->internal_.pos_in_chain;
1453 1.2.2.2 snj while (chain != NULL) {
1454 1.2.2.2 snj char *buffer = (char *)chain->buffer + chain->misalign;
1455 1.2.2.2 snj char *cp = memchr(buffer+i, chr, chain->off-i);
1456 1.2.2.2 snj if (cp) {
1457 1.2.2.2 snj it->internal_.chain = chain;
1458 1.2.2.2 snj it->internal_.pos_in_chain = cp - buffer;
1459 1.2.2.2 snj it->pos += (cp - buffer - i);
1460 1.2.2.2 snj return it->pos;
1461 1.2.2.2 snj }
1462 1.2.2.2 snj it->pos += chain->off - i;
1463 1.2.2.2 snj i = 0;
1464 1.2.2.2 snj chain = chain->next;
1465 1.2.2.2 snj }
1466 1.2.2.2 snj
1467 1.2.2.2 snj return (-1);
1468 1.2.2.2 snj }
1469 1.2.2.2 snj
1470 1.2.2.2 snj static inline char *
1471 1.2.2.2 snj find_eol_char(char *s, size_t len)
1472 1.2.2.2 snj {
1473 1.2.2.2 snj #define CHUNK_SZ 128
1474 1.2.2.2 snj /* Lots of benchmarking found this approach to be faster in practice
1475 1.2.2.2 snj * than doing two memchrs over the whole buffer, doin a memchr on each
1476 1.2.2.2 snj * char of the buffer, or trying to emulate memchr by hand. */
1477 1.2.2.2 snj char *s_end, *cr, *lf;
1478 1.2.2.2 snj s_end = s+len;
1479 1.2.2.2 snj while (s < s_end) {
1480 1.2.2.2 snj size_t chunk = (s + CHUNK_SZ < s_end) ? CHUNK_SZ : (s_end - s);
1481 1.2.2.2 snj cr = memchr(s, '\r', chunk);
1482 1.2.2.2 snj lf = memchr(s, '\n', chunk);
1483 1.2.2.2 snj if (cr) {
1484 1.2.2.2 snj if (lf && lf < cr)
1485 1.2.2.2 snj return lf;
1486 1.2.2.2 snj return cr;
1487 1.2.2.2 snj } else if (lf) {
1488 1.2.2.2 snj return lf;
1489 1.2.2.2 snj }
1490 1.2.2.2 snj s += CHUNK_SZ;
1491 1.2.2.2 snj }
1492 1.2.2.2 snj
1493 1.2.2.2 snj return NULL;
1494 1.2.2.2 snj #undef CHUNK_SZ
1495 1.2.2.2 snj }
1496 1.2.2.2 snj
1497 1.2.2.2 snj static ev_ssize_t
1498 1.2.2.2 snj evbuffer_find_eol_char(struct evbuffer_ptr *it)
1499 1.2.2.2 snj {
1500 1.2.2.2 snj struct evbuffer_chain *chain = it->internal_.chain;
1501 1.2.2.2 snj size_t i = it->internal_.pos_in_chain;
1502 1.2.2.2 snj while (chain != NULL) {
1503 1.2.2.2 snj char *buffer = (char *)chain->buffer + chain->misalign;
1504 1.2.2.2 snj char *cp = find_eol_char(buffer+i, chain->off-i);
1505 1.2.2.2 snj if (cp) {
1506 1.2.2.2 snj it->internal_.chain = chain;
1507 1.2.2.2 snj it->internal_.pos_in_chain = cp - buffer;
1508 1.2.2.2 snj it->pos += (cp - buffer) - i;
1509 1.2.2.2 snj return it->pos;
1510 1.2.2.2 snj }
1511 1.2.2.2 snj it->pos += chain->off - i;
1512 1.2.2.2 snj i = 0;
1513 1.2.2.2 snj chain = chain->next;
1514 1.2.2.2 snj }
1515 1.2.2.2 snj
1516 1.2.2.2 snj return (-1);
1517 1.2.2.2 snj }
1518 1.2.2.2 snj
1519 1.2.2.2 snj static inline int
1520 1.2.2.2 snj evbuffer_strspn(
1521 1.2.2.2 snj struct evbuffer_ptr *ptr, const char *chrset)
1522 1.2.2.2 snj {
1523 1.2.2.2 snj int count = 0;
1524 1.2.2.2 snj struct evbuffer_chain *chain = ptr->internal_.chain;
1525 1.2.2.2 snj size_t i = ptr->internal_.pos_in_chain;
1526 1.2.2.2 snj
1527 1.2.2.2 snj if (!chain)
1528 1.2.2.2 snj return 0;
1529 1.2.2.2 snj
1530 1.2.2.2 snj while (1) {
1531 1.2.2.2 snj char *buffer = (char *)chain->buffer + chain->misalign;
1532 1.2.2.2 snj for (; i < chain->off; ++i) {
1533 1.2.2.2 snj const char *p = chrset;
1534 1.2.2.2 snj while (*p) {
1535 1.2.2.2 snj if (buffer[i] == *p++)
1536 1.2.2.2 snj goto next;
1537 1.2.2.2 snj }
1538 1.2.2.2 snj ptr->internal_.chain = chain;
1539 1.2.2.2 snj ptr->internal_.pos_in_chain = i;
1540 1.2.2.2 snj ptr->pos += count;
1541 1.2.2.2 snj return count;
1542 1.2.2.2 snj next:
1543 1.2.2.2 snj ++count;
1544 1.2.2.2 snj }
1545 1.2.2.2 snj i = 0;
1546 1.2.2.2 snj
1547 1.2.2.2 snj if (! chain->next) {
1548 1.2.2.2 snj ptr->internal_.chain = chain;
1549 1.2.2.2 snj ptr->internal_.pos_in_chain = i;
1550 1.2.2.2 snj ptr->pos += count;
1551 1.2.2.2 snj return count;
1552 1.2.2.2 snj }
1553 1.2.2.2 snj
1554 1.2.2.2 snj chain = chain->next;
1555 1.2.2.2 snj }
1556 1.2.2.2 snj }
1557 1.2.2.2 snj
1558 1.2.2.2 snj
1559 1.2.2.2 snj static inline int
1560 1.2.2.2 snj evbuffer_getchr(struct evbuffer_ptr *it)
1561 1.2.2.2 snj {
1562 1.2.2.2 snj struct evbuffer_chain *chain = it->internal_.chain;
1563 1.2.2.2 snj size_t off = it->internal_.pos_in_chain;
1564 1.2.2.2 snj
1565 1.2.2.2 snj if (chain == NULL)
1566 1.2.2.2 snj return -1;
1567 1.2.2.2 snj
1568 1.2.2.2 snj return (unsigned char)chain->buffer[chain->misalign + off];
1569 1.2.2.2 snj }
1570 1.2.2.2 snj
1571 1.2.2.2 snj struct evbuffer_ptr
1572 1.2.2.2 snj evbuffer_search_eol(struct evbuffer *buffer,
1573 1.2.2.2 snj struct evbuffer_ptr *start, size_t *eol_len_out,
1574 1.2.2.2 snj enum evbuffer_eol_style eol_style)
1575 1.2.2.2 snj {
1576 1.2.2.2 snj struct evbuffer_ptr it, it2;
1577 1.2.2.2 snj size_t extra_drain = 0;
1578 1.2.2.2 snj int ok = 0;
1579 1.2.2.2 snj
1580 1.2.2.2 snj /* Avoid locking in trivial edge cases */
1581 1.2.2.2 snj if (start && start->internal_.chain == NULL) {
1582 1.2.2.2 snj PTR_NOT_FOUND(&it);
1583 1.2.2.2 snj if (eol_len_out)
1584 1.2.2.2 snj *eol_len_out = extra_drain;
1585 1.2.2.2 snj return it;
1586 1.2.2.2 snj }
1587 1.2.2.2 snj
1588 1.2.2.2 snj EVBUFFER_LOCK(buffer);
1589 1.2.2.2 snj
1590 1.2.2.2 snj if (start) {
1591 1.2.2.2 snj memcpy(&it, start, sizeof(it));
1592 1.2.2.2 snj } else {
1593 1.2.2.2 snj it.pos = 0;
1594 1.2.2.2 snj it.internal_.chain = buffer->first;
1595 1.2.2.2 snj it.internal_.pos_in_chain = 0;
1596 1.2.2.2 snj }
1597 1.2.2.2 snj
1598 1.2.2.2 snj /* the eol_style determines our first stop character and how many
1599 1.2.2.2 snj * characters we are going to drain afterwards. */
1600 1.2.2.2 snj switch (eol_style) {
1601 1.2.2.2 snj case EVBUFFER_EOL_ANY:
1602 1.2.2.2 snj if (evbuffer_find_eol_char(&it) < 0)
1603 1.2.2.2 snj goto done;
1604 1.2.2.2 snj memcpy(&it2, &it, sizeof(it));
1605 1.2.2.2 snj extra_drain = evbuffer_strspn(&it2, "\r\n");
1606 1.2.2.2 snj break;
1607 1.2.2.2 snj case EVBUFFER_EOL_CRLF_STRICT: {
1608 1.2.2.2 snj it = evbuffer_search(buffer, "\r\n", 2, &it);
1609 1.2.2.2 snj if (it.pos < 0)
1610 1.2.2.2 snj goto done;
1611 1.2.2.2 snj extra_drain = 2;
1612 1.2.2.2 snj break;
1613 1.2.2.2 snj }
1614 1.2.2.2 snj case EVBUFFER_EOL_CRLF: {
1615 1.2.2.2 snj ev_ssize_t start_pos = it.pos;
1616 1.2.2.2 snj /* Look for a LF ... */
1617 1.2.2.2 snj if (evbuffer_strchr(&it, '\n') < 0)
1618 1.2.2.2 snj goto done;
1619 1.2.2.2 snj extra_drain = 1;
1620 1.2.2.2 snj /* ... optionally preceeded by a CR. */
1621 1.2.2.2 snj if (it.pos == start_pos)
1622 1.2.2.2 snj break; /* If the first character is \n, don't back up */
1623 1.2.2.2 snj /* This potentially does an extra linear walk over the first
1624 1.2.2.2 snj * few chains. Probably, that's not too expensive unless you
1625 1.2.2.2 snj * have a really pathological setup. */
1626 1.2.2.2 snj memcpy(&it2, &it, sizeof(it));
1627 1.2.2.2 snj if (evbuffer_ptr_subtract(buffer, &it2, 1)<0)
1628 1.2.2.2 snj break;
1629 1.2.2.2 snj if (evbuffer_getchr(&it2) == '\r') {
1630 1.2.2.2 snj memcpy(&it, &it2, sizeof(it));
1631 1.2.2.2 snj extra_drain = 2;
1632 1.2.2.2 snj }
1633 1.2.2.2 snj break;
1634 1.2.2.2 snj }
1635 1.2.2.2 snj case EVBUFFER_EOL_LF:
1636 1.2.2.2 snj if (evbuffer_strchr(&it, '\n') < 0)
1637 1.2.2.2 snj goto done;
1638 1.2.2.2 snj extra_drain = 1;
1639 1.2.2.2 snj break;
1640 1.2.2.2 snj case EVBUFFER_EOL_NUL:
1641 1.2.2.2 snj if (evbuffer_strchr(&it, '\0') < 0)
1642 1.2.2.2 snj goto done;
1643 1.2.2.2 snj extra_drain = 1;
1644 1.2.2.2 snj break;
1645 1.2.2.2 snj default:
1646 1.2.2.2 snj goto done;
1647 1.2.2.2 snj }
1648 1.2.2.2 snj
1649 1.2.2.2 snj ok = 1;
1650 1.2.2.2 snj done:
1651 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
1652 1.2.2.2 snj
1653 1.2.2.2 snj if (!ok)
1654 1.2.2.2 snj PTR_NOT_FOUND(&it);
1655 1.2.2.2 snj if (eol_len_out)
1656 1.2.2.2 snj *eol_len_out = extra_drain;
1657 1.2.2.2 snj
1658 1.2.2.2 snj return it;
1659 1.2.2.2 snj }
1660 1.2.2.2 snj
1661 1.2.2.2 snj char *
1662 1.2.2.2 snj evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
1663 1.2.2.2 snj enum evbuffer_eol_style eol_style)
1664 1.2.2.2 snj {
1665 1.2.2.2 snj struct evbuffer_ptr it;
1666 1.2.2.2 snj char *line;
1667 1.2.2.2 snj size_t n_to_copy=0, extra_drain=0;
1668 1.2.2.2 snj char *result = NULL;
1669 1.2.2.2 snj
1670 1.2.2.2 snj EVBUFFER_LOCK(buffer);
1671 1.2.2.2 snj
1672 1.2.2.2 snj if (buffer->freeze_start) {
1673 1.2.2.2 snj goto done;
1674 1.2.2.2 snj }
1675 1.2.2.2 snj
1676 1.2.2.2 snj it = evbuffer_search_eol(buffer, NULL, &extra_drain, eol_style);
1677 1.2.2.2 snj if (it.pos < 0)
1678 1.2.2.2 snj goto done;
1679 1.2.2.2 snj n_to_copy = it.pos;
1680 1.2.2.2 snj
1681 1.2.2.2 snj if ((line = mm_malloc(n_to_copy+1)) == NULL) {
1682 1.2.2.2 snj event_warn("%s: out of memory", __func__);
1683 1.2.2.2 snj goto done;
1684 1.2.2.2 snj }
1685 1.2.2.2 snj
1686 1.2.2.2 snj evbuffer_remove(buffer, line, n_to_copy);
1687 1.2.2.2 snj line[n_to_copy] = '\0';
1688 1.2.2.2 snj
1689 1.2.2.2 snj evbuffer_drain(buffer, extra_drain);
1690 1.2.2.2 snj result = line;
1691 1.2.2.2 snj done:
1692 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
1693 1.2.2.2 snj
1694 1.2.2.2 snj if (n_read_out)
1695 1.2.2.2 snj *n_read_out = result ? n_to_copy : 0;
1696 1.2.2.2 snj
1697 1.2.2.2 snj return result;
1698 1.2.2.2 snj }
1699 1.2.2.2 snj
1700 1.2.2.2 snj #define EVBUFFER_CHAIN_MAX_AUTO_SIZE 4096
1701 1.2.2.2 snj
1702 1.2.2.2 snj /* Adds data to an event buffer */
1703 1.2.2.2 snj
1704 1.2.2.2 snj int
1705 1.2.2.2 snj evbuffer_add(struct evbuffer *buf, const void *data_in, size_t datlen)
1706 1.2.2.2 snj {
1707 1.2.2.2 snj struct evbuffer_chain *chain, *tmp;
1708 1.2.2.2 snj const unsigned char *data = data_in;
1709 1.2.2.2 snj size_t remain, to_alloc;
1710 1.2.2.2 snj int result = -1;
1711 1.2.2.2 snj
1712 1.2.2.2 snj EVBUFFER_LOCK(buf);
1713 1.2.2.2 snj
1714 1.2.2.2 snj if (buf->freeze_end) {
1715 1.2.2.2 snj goto done;
1716 1.2.2.2 snj }
1717 1.2.2.2 snj
1718 1.2.2.2 snj chain = buf->last;
1719 1.2.2.2 snj
1720 1.2.2.2 snj /* If there are no chains allocated for this buffer, allocate one
1721 1.2.2.2 snj * big enough to hold all the data. */
1722 1.2.2.2 snj if (chain == NULL) {
1723 1.2.2.2 snj chain = evbuffer_chain_new(datlen);
1724 1.2.2.2 snj if (!chain)
1725 1.2.2.2 snj goto done;
1726 1.2.2.2 snj evbuffer_chain_insert(buf, chain);
1727 1.2.2.2 snj }
1728 1.2.2.2 snj
1729 1.2.2.2 snj if ((chain->flags & EVBUFFER_IMMUTABLE) == 0) {
1730 1.2.2.2 snj remain = (size_t)(chain->buffer_len - chain->misalign - chain->off);
1731 1.2.2.2 snj if (remain >= datlen) {
1732 1.2.2.2 snj /* there's enough space to hold all the data in the
1733 1.2.2.2 snj * current last chain */
1734 1.2.2.2 snj memcpy(chain->buffer + chain->misalign + chain->off,
1735 1.2.2.2 snj data, datlen);
1736 1.2.2.2 snj chain->off += datlen;
1737 1.2.2.2 snj buf->total_len += datlen;
1738 1.2.2.2 snj buf->n_add_for_cb += datlen;
1739 1.2.2.2 snj goto out;
1740 1.2.2.2 snj } else if (!CHAIN_PINNED(chain) &&
1741 1.2.2.2 snj evbuffer_chain_should_realign(chain, datlen)) {
1742 1.2.2.2 snj /* we can fit the data into the misalignment */
1743 1.2.2.2 snj evbuffer_chain_align(chain);
1744 1.2.2.2 snj
1745 1.2.2.2 snj memcpy(chain->buffer + chain->off, data, datlen);
1746 1.2.2.2 snj chain->off += datlen;
1747 1.2.2.2 snj buf->total_len += datlen;
1748 1.2.2.2 snj buf->n_add_for_cb += datlen;
1749 1.2.2.2 snj goto out;
1750 1.2.2.2 snj }
1751 1.2.2.2 snj } else {
1752 1.2.2.2 snj /* we cannot write any data to the last chain */
1753 1.2.2.2 snj remain = 0;
1754 1.2.2.2 snj }
1755 1.2.2.2 snj
1756 1.2.2.2 snj /* we need to add another chain */
1757 1.2.2.2 snj to_alloc = chain->buffer_len;
1758 1.2.2.2 snj if (to_alloc <= EVBUFFER_CHAIN_MAX_AUTO_SIZE/2)
1759 1.2.2.2 snj to_alloc <<= 1;
1760 1.2.2.2 snj if (datlen > to_alloc)
1761 1.2.2.2 snj to_alloc = datlen;
1762 1.2.2.2 snj tmp = evbuffer_chain_new(to_alloc);
1763 1.2.2.2 snj if (tmp == NULL)
1764 1.2.2.2 snj goto done;
1765 1.2.2.2 snj
1766 1.2.2.2 snj if (remain) {
1767 1.2.2.2 snj memcpy(chain->buffer + chain->misalign + chain->off,
1768 1.2.2.2 snj data, remain);
1769 1.2.2.2 snj chain->off += remain;
1770 1.2.2.2 snj buf->total_len += remain;
1771 1.2.2.2 snj buf->n_add_for_cb += remain;
1772 1.2.2.2 snj }
1773 1.2.2.2 snj
1774 1.2.2.2 snj data += remain;
1775 1.2.2.2 snj datlen -= remain;
1776 1.2.2.2 snj
1777 1.2.2.2 snj memcpy(tmp->buffer, data, datlen);
1778 1.2.2.2 snj tmp->off = datlen;
1779 1.2.2.2 snj evbuffer_chain_insert(buf, tmp);
1780 1.2.2.2 snj buf->n_add_for_cb += datlen;
1781 1.2.2.2 snj
1782 1.2.2.2 snj out:
1783 1.2.2.2 snj evbuffer_invoke_callbacks_(buf);
1784 1.2.2.2 snj result = 0;
1785 1.2.2.2 snj done:
1786 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
1787 1.2.2.2 snj return result;
1788 1.2.2.2 snj }
1789 1.2.2.2 snj
1790 1.2.2.2 snj int
1791 1.2.2.2 snj evbuffer_prepend(struct evbuffer *buf, const void *data, size_t datlen)
1792 1.2.2.2 snj {
1793 1.2.2.2 snj struct evbuffer_chain *chain, *tmp;
1794 1.2.2.2 snj int result = -1;
1795 1.2.2.2 snj
1796 1.2.2.2 snj EVBUFFER_LOCK(buf);
1797 1.2.2.2 snj
1798 1.2.2.2 snj if (buf->freeze_start) {
1799 1.2.2.2 snj goto done;
1800 1.2.2.2 snj }
1801 1.2.2.2 snj
1802 1.2.2.2 snj chain = buf->first;
1803 1.2.2.2 snj
1804 1.2.2.2 snj if (chain == NULL) {
1805 1.2.2.2 snj chain = evbuffer_chain_new(datlen);
1806 1.2.2.2 snj if (!chain)
1807 1.2.2.2 snj goto done;
1808 1.2.2.2 snj evbuffer_chain_insert(buf, chain);
1809 1.2.2.2 snj }
1810 1.2.2.2 snj
1811 1.2.2.2 snj /* we cannot touch immutable buffers */
1812 1.2.2.2 snj if ((chain->flags & EVBUFFER_IMMUTABLE) == 0) {
1813 1.2.2.2 snj /* If this chain is empty, we can treat it as
1814 1.2.2.2 snj * 'empty at the beginning' rather than 'empty at the end' */
1815 1.2.2.2 snj if (chain->off == 0)
1816 1.2.2.2 snj chain->misalign = chain->buffer_len;
1817 1.2.2.2 snj
1818 1.2.2.2 snj if ((size_t)chain->misalign >= datlen) {
1819 1.2.2.2 snj /* we have enough space to fit everything */
1820 1.2.2.2 snj memcpy(chain->buffer + chain->misalign - datlen,
1821 1.2.2.2 snj data, datlen);
1822 1.2.2.2 snj chain->off += datlen;
1823 1.2.2.2 snj chain->misalign -= datlen;
1824 1.2.2.2 snj buf->total_len += datlen;
1825 1.2.2.2 snj buf->n_add_for_cb += datlen;
1826 1.2.2.2 snj goto out;
1827 1.2.2.2 snj } else if (chain->misalign) {
1828 1.2.2.2 snj /* we can only fit some of the data. */
1829 1.2.2.2 snj memcpy(chain->buffer,
1830 1.2.2.2 snj (char*)data + datlen - chain->misalign,
1831 1.2.2.2 snj (size_t)chain->misalign);
1832 1.2.2.2 snj chain->off += (size_t)chain->misalign;
1833 1.2.2.2 snj buf->total_len += (size_t)chain->misalign;
1834 1.2.2.2 snj buf->n_add_for_cb += (size_t)chain->misalign;
1835 1.2.2.2 snj datlen -= (size_t)chain->misalign;
1836 1.2.2.2 snj chain->misalign = 0;
1837 1.2.2.2 snj }
1838 1.2.2.2 snj }
1839 1.2.2.2 snj
1840 1.2.2.2 snj /* we need to add another chain */
1841 1.2.2.2 snj if ((tmp = evbuffer_chain_new(datlen)) == NULL)
1842 1.2.2.2 snj goto done;
1843 1.2.2.2 snj buf->first = tmp;
1844 1.2.2.2 snj if (buf->last_with_datap == &buf->first)
1845 1.2.2.2 snj buf->last_with_datap = &tmp->next;
1846 1.2.2.2 snj
1847 1.2.2.2 snj tmp->next = chain;
1848 1.2.2.2 snj
1849 1.2.2.2 snj tmp->off = datlen;
1850 1.2.2.2 snj tmp->misalign = tmp->buffer_len - datlen;
1851 1.2.2.2 snj
1852 1.2.2.2 snj memcpy(tmp->buffer + tmp->misalign, data, datlen);
1853 1.2.2.2 snj buf->total_len += datlen;
1854 1.2.2.2 snj buf->n_add_for_cb += (size_t)chain->misalign;
1855 1.2.2.2 snj
1856 1.2.2.2 snj out:
1857 1.2.2.2 snj evbuffer_invoke_callbacks_(buf);
1858 1.2.2.2 snj result = 0;
1859 1.2.2.2 snj done:
1860 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
1861 1.2.2.2 snj return result;
1862 1.2.2.2 snj }
1863 1.2.2.2 snj
1864 1.2.2.2 snj /** Helper: realigns the memory in chain->buffer so that misalign is 0. */
1865 1.2.2.2 snj static void
1866 1.2.2.2 snj evbuffer_chain_align(struct evbuffer_chain *chain)
1867 1.2.2.2 snj {
1868 1.2.2.2 snj EVUTIL_ASSERT(!(chain->flags & EVBUFFER_IMMUTABLE));
1869 1.2.2.2 snj EVUTIL_ASSERT(!(chain->flags & EVBUFFER_MEM_PINNED_ANY));
1870 1.2.2.2 snj memmove(chain->buffer, chain->buffer + chain->misalign, chain->off);
1871 1.2.2.2 snj chain->misalign = 0;
1872 1.2.2.2 snj }
1873 1.2.2.2 snj
1874 1.2.2.2 snj #define MAX_TO_COPY_IN_EXPAND 4096
1875 1.2.2.2 snj #define MAX_TO_REALIGN_IN_EXPAND 2048
1876 1.2.2.2 snj
1877 1.2.2.2 snj /** Helper: return true iff we should realign chain to fit datalen bytes of
1878 1.2.2.2 snj data in it. */
1879 1.2.2.2 snj static int
1880 1.2.2.2 snj evbuffer_chain_should_realign(struct evbuffer_chain *chain,
1881 1.2.2.2 snj size_t datlen)
1882 1.2.2.2 snj {
1883 1.2.2.2 snj return chain->buffer_len - chain->off >= datlen &&
1884 1.2.2.2 snj (chain->off < chain->buffer_len / 2) &&
1885 1.2.2.2 snj (chain->off <= MAX_TO_REALIGN_IN_EXPAND);
1886 1.2.2.2 snj }
1887 1.2.2.2 snj
1888 1.2.2.2 snj /* Expands the available space in the event buffer to at least datlen, all in
1889 1.2.2.2 snj * a single chunk. Return that chunk. */
1890 1.2.2.2 snj static struct evbuffer_chain *
1891 1.2.2.2 snj evbuffer_expand_singlechain(struct evbuffer *buf, size_t datlen)
1892 1.2.2.2 snj {
1893 1.2.2.2 snj struct evbuffer_chain *chain, **chainp;
1894 1.2.2.2 snj struct evbuffer_chain *result = NULL;
1895 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(buf);
1896 1.2.2.2 snj
1897 1.2.2.2 snj chainp = buf->last_with_datap;
1898 1.2.2.2 snj
1899 1.2.2.2 snj /* XXX If *chainp is no longer writeable, but has enough space in its
1900 1.2.2.2 snj * misalign, this might be a bad idea: we could still use *chainp, not
1901 1.2.2.2 snj * (*chainp)->next. */
1902 1.2.2.2 snj if (*chainp && CHAIN_SPACE_LEN(*chainp) == 0)
1903 1.2.2.2 snj chainp = &(*chainp)->next;
1904 1.2.2.2 snj
1905 1.2.2.2 snj /* 'chain' now points to the first chain with writable space (if any)
1906 1.2.2.2 snj * We will either use it, realign it, replace it, or resize it. */
1907 1.2.2.2 snj chain = *chainp;
1908 1.2.2.2 snj
1909 1.2.2.2 snj if (chain == NULL ||
1910 1.2.2.2 snj (chain->flags & (EVBUFFER_IMMUTABLE|EVBUFFER_MEM_PINNED_ANY))) {
1911 1.2.2.2 snj /* We can't use the last_with_data chain at all. Just add a
1912 1.2.2.2 snj * new one that's big enough. */
1913 1.2.2.2 snj goto insert_new;
1914 1.2.2.2 snj }
1915 1.2.2.2 snj
1916 1.2.2.2 snj /* If we can fit all the data, then we don't have to do anything */
1917 1.2.2.2 snj if (CHAIN_SPACE_LEN(chain) >= datlen) {
1918 1.2.2.2 snj result = chain;
1919 1.2.2.2 snj goto ok;
1920 1.2.2.2 snj }
1921 1.2.2.2 snj
1922 1.2.2.2 snj /* If the chain is completely empty, just replace it by adding a new
1923 1.2.2.2 snj * empty chain. */
1924 1.2.2.2 snj if (chain->off == 0) {
1925 1.2.2.2 snj goto insert_new;
1926 1.2.2.2 snj }
1927 1.2.2.2 snj
1928 1.2.2.2 snj /* If the misalignment plus the remaining space fulfills our data
1929 1.2.2.2 snj * needs, we could just force an alignment to happen. Afterwards, we
1930 1.2.2.2 snj * have enough space. But only do this if we're saving a lot of space
1931 1.2.2.2 snj * and not moving too much data. Otherwise the space savings are
1932 1.2.2.2 snj * probably offset by the time lost in copying.
1933 1.2.2.2 snj */
1934 1.2.2.2 snj if (evbuffer_chain_should_realign(chain, datlen)) {
1935 1.2.2.2 snj evbuffer_chain_align(chain);
1936 1.2.2.2 snj result = chain;
1937 1.2.2.2 snj goto ok;
1938 1.2.2.2 snj }
1939 1.2.2.2 snj
1940 1.2.2.2 snj /* At this point, we can either resize the last chunk with space in
1941 1.2.2.2 snj * it, use the next chunk after it, or If we add a new chunk, we waste
1942 1.2.2.2 snj * CHAIN_SPACE_LEN(chain) bytes in the former last chunk. If we
1943 1.2.2.2 snj * resize, we have to copy chain->off bytes.
1944 1.2.2.2 snj */
1945 1.2.2.2 snj
1946 1.2.2.2 snj /* Would expanding this chunk be affordable and worthwhile? */
1947 1.2.2.2 snj if (CHAIN_SPACE_LEN(chain) < chain->buffer_len / 8 ||
1948 1.2.2.2 snj chain->off > MAX_TO_COPY_IN_EXPAND) {
1949 1.2.2.2 snj /* It's not worth resizing this chain. Can the next one be
1950 1.2.2.2 snj * used? */
1951 1.2.2.2 snj if (chain->next && CHAIN_SPACE_LEN(chain->next) >= datlen) {
1952 1.2.2.2 snj /* Yes, we can just use the next chain (which should
1953 1.2.2.2 snj * be empty. */
1954 1.2.2.2 snj result = chain->next;
1955 1.2.2.2 snj goto ok;
1956 1.2.2.2 snj } else {
1957 1.2.2.2 snj /* No; append a new chain (which will free all
1958 1.2.2.2 snj * terminal empty chains.) */
1959 1.2.2.2 snj goto insert_new;
1960 1.2.2.2 snj }
1961 1.2.2.2 snj } else {
1962 1.2.2.2 snj /* Okay, we're going to try to resize this chain: Not doing so
1963 1.2.2.2 snj * would waste at least 1/8 of its current allocation, and we
1964 1.2.2.2 snj * can do so without having to copy more than
1965 1.2.2.2 snj * MAX_TO_COPY_IN_EXPAND bytes. */
1966 1.2.2.2 snj /* figure out how much space we need */
1967 1.2.2.2 snj size_t length = chain->off + datlen;
1968 1.2.2.2 snj struct evbuffer_chain *tmp = evbuffer_chain_new(length);
1969 1.2.2.2 snj if (tmp == NULL)
1970 1.2.2.2 snj goto err;
1971 1.2.2.2 snj
1972 1.2.2.2 snj /* copy the data over that we had so far */
1973 1.2.2.2 snj tmp->off = chain->off;
1974 1.2.2.2 snj memcpy(tmp->buffer, chain->buffer + chain->misalign,
1975 1.2.2.2 snj chain->off);
1976 1.2.2.2 snj /* fix up the list */
1977 1.2.2.2 snj EVUTIL_ASSERT(*chainp == chain);
1978 1.2.2.2 snj result = *chainp = tmp;
1979 1.2.2.2 snj
1980 1.2.2.2 snj if (buf->last == chain)
1981 1.2.2.2 snj buf->last = tmp;
1982 1.2.2.2 snj
1983 1.2.2.2 snj tmp->next = chain->next;
1984 1.2.2.2 snj evbuffer_chain_free(chain);
1985 1.2.2.2 snj goto ok;
1986 1.2.2.2 snj }
1987 1.2.2.2 snj
1988 1.2.2.2 snj insert_new:
1989 1.2.2.2 snj result = evbuffer_chain_insert_new(buf, datlen);
1990 1.2.2.2 snj if (!result)
1991 1.2.2.2 snj goto err;
1992 1.2.2.2 snj ok:
1993 1.2.2.2 snj EVUTIL_ASSERT(result);
1994 1.2.2.2 snj EVUTIL_ASSERT(CHAIN_SPACE_LEN(result) >= datlen);
1995 1.2.2.2 snj err:
1996 1.2.2.2 snj return result;
1997 1.2.2.2 snj }
1998 1.2.2.2 snj
1999 1.2.2.2 snj /* Make sure that datlen bytes are available for writing in the last n
2000 1.2.2.2 snj * chains. Never copies or moves data. */
2001 1.2.2.2 snj int
2002 1.2.2.2 snj evbuffer_expand_fast_(struct evbuffer *buf, size_t datlen, int n)
2003 1.2.2.2 snj {
2004 1.2.2.2 snj struct evbuffer_chain *chain = buf->last, *tmp, *next;
2005 1.2.2.2 snj size_t avail;
2006 1.2.2.2 snj int used;
2007 1.2.2.2 snj
2008 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(buf);
2009 1.2.2.2 snj EVUTIL_ASSERT(n >= 2);
2010 1.2.2.2 snj
2011 1.2.2.2 snj if (chain == NULL || (chain->flags & EVBUFFER_IMMUTABLE)) {
2012 1.2.2.2 snj /* There is no last chunk, or we can't touch the last chunk.
2013 1.2.2.2 snj * Just add a new chunk. */
2014 1.2.2.2 snj chain = evbuffer_chain_new(datlen);
2015 1.2.2.2 snj if (chain == NULL)
2016 1.2.2.2 snj return (-1);
2017 1.2.2.2 snj
2018 1.2.2.2 snj evbuffer_chain_insert(buf, chain);
2019 1.2.2.2 snj return (0);
2020 1.2.2.2 snj }
2021 1.2.2.2 snj
2022 1.2.2.2 snj used = 0; /* number of chains we're using space in. */
2023 1.2.2.2 snj avail = 0; /* how much space they have. */
2024 1.2.2.2 snj /* How many bytes can we stick at the end of buffer as it is? Iterate
2025 1.2.2.2 snj * over the chains at the end of the buffer, tring to see how much
2026 1.2.2.2 snj * space we have in the first n. */
2027 1.2.2.2 snj for (chain = *buf->last_with_datap; chain; chain = chain->next) {
2028 1.2.2.2 snj if (chain->off) {
2029 1.2.2.2 snj size_t space = (size_t) CHAIN_SPACE_LEN(chain);
2030 1.2.2.2 snj EVUTIL_ASSERT(chain == *buf->last_with_datap);
2031 1.2.2.2 snj if (space) {
2032 1.2.2.2 snj avail += space;
2033 1.2.2.2 snj ++used;
2034 1.2.2.2 snj }
2035 1.2.2.2 snj } else {
2036 1.2.2.2 snj /* No data in chain; realign it. */
2037 1.2.2.2 snj chain->misalign = 0;
2038 1.2.2.2 snj avail += chain->buffer_len;
2039 1.2.2.2 snj ++used;
2040 1.2.2.2 snj }
2041 1.2.2.2 snj if (avail >= datlen) {
2042 1.2.2.2 snj /* There is already enough space. Just return */
2043 1.2.2.2 snj return (0);
2044 1.2.2.2 snj }
2045 1.2.2.2 snj if (used == n)
2046 1.2.2.2 snj break;
2047 1.2.2.2 snj }
2048 1.2.2.2 snj
2049 1.2.2.2 snj /* There wasn't enough space in the first n chains with space in
2050 1.2.2.2 snj * them. Either add a new chain with enough space, or replace all
2051 1.2.2.2 snj * empty chains with one that has enough space, depending on n. */
2052 1.2.2.2 snj if (used < n) {
2053 1.2.2.2 snj /* The loop ran off the end of the chains before it hit n
2054 1.2.2.2 snj * chains; we can add another. */
2055 1.2.2.2 snj EVUTIL_ASSERT(chain == NULL);
2056 1.2.2.2 snj
2057 1.2.2.2 snj tmp = evbuffer_chain_new(datlen - avail);
2058 1.2.2.2 snj if (tmp == NULL)
2059 1.2.2.2 snj return (-1);
2060 1.2.2.2 snj
2061 1.2.2.2 snj buf->last->next = tmp;
2062 1.2.2.2 snj buf->last = tmp;
2063 1.2.2.2 snj /* (we would only set last_with_data if we added the first
2064 1.2.2.2 snj * chain. But if the buffer had no chains, we would have
2065 1.2.2.2 snj * just allocated a new chain earlier) */
2066 1.2.2.2 snj return (0);
2067 1.2.2.2 snj } else {
2068 1.2.2.2 snj /* Nuke _all_ the empty chains. */
2069 1.2.2.2 snj int rmv_all = 0; /* True iff we removed last_with_data. */
2070 1.2.2.2 snj chain = *buf->last_with_datap;
2071 1.2.2.2 snj if (!chain->off) {
2072 1.2.2.2 snj EVUTIL_ASSERT(chain == buf->first);
2073 1.2.2.2 snj rmv_all = 1;
2074 1.2.2.2 snj avail = 0;
2075 1.2.2.2 snj } else {
2076 1.2.2.2 snj avail = (size_t) CHAIN_SPACE_LEN(chain);
2077 1.2.2.2 snj chain = chain->next;
2078 1.2.2.2 snj }
2079 1.2.2.2 snj
2080 1.2.2.2 snj
2081 1.2.2.2 snj for (; chain; chain = next) {
2082 1.2.2.2 snj next = chain->next;
2083 1.2.2.2 snj EVUTIL_ASSERT(chain->off == 0);
2084 1.2.2.2 snj evbuffer_chain_free(chain);
2085 1.2.2.2 snj }
2086 1.2.2.2 snj tmp = evbuffer_chain_new(datlen - avail);
2087 1.2.2.2 snj if (tmp == NULL) {
2088 1.2.2.2 snj if (rmv_all) {
2089 1.2.2.2 snj ZERO_CHAIN(buf);
2090 1.2.2.2 snj } else {
2091 1.2.2.2 snj buf->last = *buf->last_with_datap;
2092 1.2.2.2 snj (*buf->last_with_datap)->next = NULL;
2093 1.2.2.2 snj }
2094 1.2.2.2 snj return (-1);
2095 1.2.2.2 snj }
2096 1.2.2.2 snj
2097 1.2.2.2 snj if (rmv_all) {
2098 1.2.2.2 snj buf->first = buf->last = tmp;
2099 1.2.2.2 snj buf->last_with_datap = &buf->first;
2100 1.2.2.2 snj } else {
2101 1.2.2.2 snj (*buf->last_with_datap)->next = tmp;
2102 1.2.2.2 snj buf->last = tmp;
2103 1.2.2.2 snj }
2104 1.2.2.2 snj return (0);
2105 1.2.2.2 snj }
2106 1.2.2.2 snj }
2107 1.2.2.2 snj
2108 1.2.2.2 snj int
2109 1.2.2.2 snj evbuffer_expand(struct evbuffer *buf, size_t datlen)
2110 1.2.2.2 snj {
2111 1.2.2.2 snj struct evbuffer_chain *chain;
2112 1.2.2.2 snj
2113 1.2.2.2 snj EVBUFFER_LOCK(buf);
2114 1.2.2.2 snj chain = evbuffer_expand_singlechain(buf, datlen);
2115 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
2116 1.2.2.2 snj return chain ? 0 : -1;
2117 1.2.2.2 snj }
2118 1.2.2.2 snj
2119 1.2.2.2 snj /*
2120 1.2.2.2 snj * Reads data from a file descriptor into a buffer.
2121 1.2.2.2 snj */
2122 1.2.2.2 snj
2123 1.2.2.2 snj #if defined(EVENT__HAVE_SYS_UIO_H) || defined(_WIN32)
2124 1.2.2.2 snj #define USE_IOVEC_IMPL
2125 1.2.2.2 snj #endif
2126 1.2.2.2 snj
2127 1.2.2.2 snj #ifdef USE_IOVEC_IMPL
2128 1.2.2.2 snj
2129 1.2.2.2 snj #ifdef EVENT__HAVE_SYS_UIO_H
2130 1.2.2.2 snj /* number of iovec we use for writev, fragmentation is going to determine
2131 1.2.2.2 snj * how much we end up writing */
2132 1.2.2.2 snj
2133 1.2.2.2 snj #define DEFAULT_WRITE_IOVEC 128
2134 1.2.2.2 snj
2135 1.2.2.2 snj #if defined(UIO_MAXIOV) && UIO_MAXIOV < DEFAULT_WRITE_IOVEC
2136 1.2.2.2 snj #define NUM_WRITE_IOVEC UIO_MAXIOV
2137 1.2.2.2 snj #elif defined(IOV_MAX) && IOV_MAX < DEFAULT_WRITE_IOVEC
2138 1.2.2.2 snj #define NUM_WRITE_IOVEC IOV_MAX
2139 1.2.2.2 snj #else
2140 1.2.2.2 snj #define NUM_WRITE_IOVEC DEFAULT_WRITE_IOVEC
2141 1.2.2.2 snj #endif
2142 1.2.2.2 snj
2143 1.2.2.2 snj #define IOV_TYPE struct iovec
2144 1.2.2.2 snj #define IOV_PTR_FIELD iov_base
2145 1.2.2.2 snj #define IOV_LEN_FIELD iov_len
2146 1.2.2.2 snj #define IOV_LEN_TYPE size_t
2147 1.2.2.2 snj #else
2148 1.2.2.2 snj #define NUM_WRITE_IOVEC 16
2149 1.2.2.2 snj #define IOV_TYPE WSABUF
2150 1.2.2.2 snj #define IOV_PTR_FIELD buf
2151 1.2.2.2 snj #define IOV_LEN_FIELD len
2152 1.2.2.2 snj #define IOV_LEN_TYPE unsigned long
2153 1.2.2.2 snj #endif
2154 1.2.2.2 snj #endif
2155 1.2.2.2 snj #define NUM_READ_IOVEC 4
2156 1.2.2.2 snj
2157 1.2.2.2 snj #define EVBUFFER_MAX_READ 4096
2158 1.2.2.2 snj
2159 1.2.2.2 snj /** Helper function to figure out which space to use for reading data into
2160 1.2.2.2 snj an evbuffer. Internal use only.
2161 1.2.2.2 snj
2162 1.2.2.2 snj @param buf The buffer to read into
2163 1.2.2.2 snj @param howmuch How much we want to read.
2164 1.2.2.2 snj @param vecs An array of two or more iovecs or WSABUFs.
2165 1.2.2.2 snj @param n_vecs_avail The length of vecs
2166 1.2.2.2 snj @param chainp A pointer to a variable to hold the first chain we're
2167 1.2.2.2 snj reading into.
2168 1.2.2.2 snj @param exact Boolean: if true, we do not provide more than 'howmuch'
2169 1.2.2.2 snj space in the vectors, even if more space is available.
2170 1.2.2.2 snj @return The number of buffers we're using.
2171 1.2.2.2 snj */
2172 1.2.2.2 snj int
2173 1.2.2.2 snj evbuffer_read_setup_vecs_(struct evbuffer *buf, ev_ssize_t howmuch,
2174 1.2.2.2 snj struct evbuffer_iovec *vecs, int n_vecs_avail,
2175 1.2.2.2 snj struct evbuffer_chain ***chainp, int exact)
2176 1.2.2.2 snj {
2177 1.2.2.2 snj struct evbuffer_chain *chain;
2178 1.2.2.2 snj struct evbuffer_chain **firstchainp;
2179 1.2.2.2 snj size_t so_far;
2180 1.2.2.2 snj int i;
2181 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(buf);
2182 1.2.2.2 snj
2183 1.2.2.2 snj if (howmuch < 0)
2184 1.2.2.2 snj return -1;
2185 1.2.2.2 snj
2186 1.2.2.2 snj so_far = 0;
2187 1.2.2.2 snj /* Let firstchain be the first chain with any space on it */
2188 1.2.2.2 snj firstchainp = buf->last_with_datap;
2189 1.2.2.2 snj if (CHAIN_SPACE_LEN(*firstchainp) == 0) {
2190 1.2.2.2 snj firstchainp = &(*firstchainp)->next;
2191 1.2.2.2 snj }
2192 1.2.2.2 snj
2193 1.2.2.2 snj chain = *firstchainp;
2194 1.2.2.2 snj for (i = 0; i < n_vecs_avail && so_far < (size_t)howmuch; ++i) {
2195 1.2.2.2 snj size_t avail = (size_t) CHAIN_SPACE_LEN(chain);
2196 1.2.2.2 snj if (avail > (howmuch - so_far) && exact)
2197 1.2.2.2 snj avail = howmuch - so_far;
2198 1.2.2.2 snj vecs[i].iov_base = CHAIN_SPACE_PTR(chain);
2199 1.2.2.2 snj vecs[i].iov_len = avail;
2200 1.2.2.2 snj so_far += avail;
2201 1.2.2.2 snj chain = chain->next;
2202 1.2.2.2 snj }
2203 1.2.2.2 snj
2204 1.2.2.2 snj *chainp = firstchainp;
2205 1.2.2.2 snj return i;
2206 1.2.2.2 snj }
2207 1.2.2.2 snj
2208 1.2.2.2 snj static int
2209 1.2.2.2 snj get_n_bytes_readable_on_socket(evutil_socket_t fd)
2210 1.2.2.2 snj {
2211 1.2.2.2 snj #if defined(FIONREAD) && defined(_WIN32)
2212 1.2.2.2 snj unsigned long lng = EVBUFFER_MAX_READ;
2213 1.2.2.2 snj if (ioctlsocket(fd, FIONREAD, &lng) < 0)
2214 1.2.2.2 snj return -1;
2215 1.2.2.2 snj return (int)lng;
2216 1.2.2.2 snj #elif defined(FIONREAD)
2217 1.2.2.2 snj int n = EVBUFFER_MAX_READ;
2218 1.2.2.2 snj if (ioctl(fd, FIONREAD, &n) < 0)
2219 1.2.2.2 snj return -1;
2220 1.2.2.2 snj return n;
2221 1.2.2.2 snj #else
2222 1.2.2.2 snj return EVBUFFER_MAX_READ;
2223 1.2.2.2 snj #endif
2224 1.2.2.2 snj }
2225 1.2.2.2 snj
2226 1.2.2.2 snj /* TODO(niels): should this function return ev_ssize_t and take ev_ssize_t
2227 1.2.2.2 snj * as howmuch? */
2228 1.2.2.2 snj int
2229 1.2.2.2 snj evbuffer_read(struct evbuffer *buf, evutil_socket_t fd, int howmuch)
2230 1.2.2.2 snj {
2231 1.2.2.2 snj struct evbuffer_chain **chainp;
2232 1.2.2.2 snj int n;
2233 1.2.2.2 snj int result;
2234 1.2.2.2 snj
2235 1.2.2.2 snj #ifdef USE_IOVEC_IMPL
2236 1.2.2.2 snj int nvecs, i, remaining;
2237 1.2.2.2 snj #else
2238 1.2.2.2 snj struct evbuffer_chain *chain;
2239 1.2.2.2 snj unsigned char *p;
2240 1.2.2.2 snj #endif
2241 1.2.2.2 snj
2242 1.2.2.2 snj EVBUFFER_LOCK(buf);
2243 1.2.2.2 snj
2244 1.2.2.2 snj if (buf->freeze_end) {
2245 1.2.2.2 snj result = -1;
2246 1.2.2.2 snj goto done;
2247 1.2.2.2 snj }
2248 1.2.2.2 snj
2249 1.2.2.2 snj n = get_n_bytes_readable_on_socket(fd);
2250 1.2.2.2 snj if (n <= 0 || n > EVBUFFER_MAX_READ)
2251 1.2.2.2 snj n = EVBUFFER_MAX_READ;
2252 1.2.2.2 snj if (howmuch < 0 || howmuch > n)
2253 1.2.2.2 snj howmuch = n;
2254 1.2.2.2 snj
2255 1.2.2.2 snj #ifdef USE_IOVEC_IMPL
2256 1.2.2.2 snj /* Since we can use iovecs, we're willing to use the last
2257 1.2.2.2 snj * NUM_READ_IOVEC chains. */
2258 1.2.2.2 snj if (evbuffer_expand_fast_(buf, howmuch, NUM_READ_IOVEC) == -1) {
2259 1.2.2.2 snj result = -1;
2260 1.2.2.2 snj goto done;
2261 1.2.2.2 snj } else {
2262 1.2.2.2 snj IOV_TYPE vecs[NUM_READ_IOVEC];
2263 1.2.2.2 snj #ifdef EVBUFFER_IOVEC_IS_NATIVE_
2264 1.2.2.2 snj nvecs = evbuffer_read_setup_vecs_(buf, howmuch, vecs,
2265 1.2.2.2 snj NUM_READ_IOVEC, &chainp, 1);
2266 1.2.2.2 snj #else
2267 1.2.2.2 snj /* We aren't using the native struct iovec. Therefore,
2268 1.2.2.2 snj we are on win32. */
2269 1.2.2.2 snj struct evbuffer_iovec ev_vecs[NUM_READ_IOVEC];
2270 1.2.2.2 snj nvecs = evbuffer_read_setup_vecs_(buf, howmuch, ev_vecs, 2,
2271 1.2.2.2 snj &chainp, 1);
2272 1.2.2.2 snj
2273 1.2.2.2 snj for (i=0; i < nvecs; ++i)
2274 1.2.2.2 snj WSABUF_FROM_EVBUFFER_IOV(&vecs[i], &ev_vecs[i]);
2275 1.2.2.2 snj #endif
2276 1.2.2.2 snj
2277 1.2.2.2 snj #ifdef _WIN32
2278 1.2.2.2 snj {
2279 1.2.2.2 snj DWORD bytesRead;
2280 1.2.2.2 snj DWORD flags=0;
2281 1.2.2.2 snj if (WSARecv(fd, vecs, nvecs, &bytesRead, &flags, NULL, NULL)) {
2282 1.2.2.2 snj /* The read failed. It might be a close,
2283 1.2.2.2 snj * or it might be an error. */
2284 1.2.2.2 snj if (WSAGetLastError() == WSAECONNABORTED)
2285 1.2.2.2 snj n = 0;
2286 1.2.2.2 snj else
2287 1.2.2.2 snj n = -1;
2288 1.2.2.2 snj } else
2289 1.2.2.2 snj n = bytesRead;
2290 1.2.2.2 snj }
2291 1.2.2.2 snj #else
2292 1.2.2.2 snj n = readv(fd, vecs, nvecs);
2293 1.2.2.2 snj #endif
2294 1.2.2.2 snj }
2295 1.2.2.2 snj
2296 1.2.2.2 snj #else /*!USE_IOVEC_IMPL*/
2297 1.2.2.2 snj /* If we don't have FIONREAD, we might waste some space here */
2298 1.2.2.2 snj /* XXX we _will_ waste some space here if there is any space left
2299 1.2.2.2 snj * over on buf->last. */
2300 1.2.2.2 snj if ((chain = evbuffer_expand_singlechain(buf, howmuch)) == NULL) {
2301 1.2.2.2 snj result = -1;
2302 1.2.2.2 snj goto done;
2303 1.2.2.2 snj }
2304 1.2.2.2 snj
2305 1.2.2.2 snj /* We can append new data at this point */
2306 1.2.2.2 snj p = chain->buffer + chain->misalign + chain->off;
2307 1.2.2.2 snj
2308 1.2.2.2 snj #ifndef _WIN32
2309 1.2.2.2 snj n = read(fd, p, howmuch);
2310 1.2.2.2 snj #else
2311 1.2.2.2 snj n = recv(fd, p, howmuch, 0);
2312 1.2.2.2 snj #endif
2313 1.2.2.2 snj #endif /* USE_IOVEC_IMPL */
2314 1.2.2.2 snj
2315 1.2.2.2 snj if (n == -1) {
2316 1.2.2.2 snj result = -1;
2317 1.2.2.2 snj goto done;
2318 1.2.2.2 snj }
2319 1.2.2.2 snj if (n == 0) {
2320 1.2.2.2 snj result = 0;
2321 1.2.2.2 snj goto done;
2322 1.2.2.2 snj }
2323 1.2.2.2 snj
2324 1.2.2.2 snj #ifdef USE_IOVEC_IMPL
2325 1.2.2.2 snj remaining = n;
2326 1.2.2.2 snj for (i=0; i < nvecs; ++i) {
2327 1.2.2.2 snj ev_ssize_t space = (ev_ssize_t) CHAIN_SPACE_LEN(*chainp);
2328 1.2.2.2 snj if (space < remaining) {
2329 1.2.2.2 snj (*chainp)->off += space;
2330 1.2.2.2 snj remaining -= (int)space;
2331 1.2.2.2 snj } else {
2332 1.2.2.2 snj (*chainp)->off += remaining;
2333 1.2.2.2 snj buf->last_with_datap = chainp;
2334 1.2.2.2 snj break;
2335 1.2.2.2 snj }
2336 1.2.2.2 snj chainp = &(*chainp)->next;
2337 1.2.2.2 snj }
2338 1.2.2.2 snj #else
2339 1.2.2.2 snj chain->off += n;
2340 1.2.2.2 snj advance_last_with_data(buf);
2341 1.2.2.2 snj #endif
2342 1.2.2.2 snj buf->total_len += n;
2343 1.2.2.2 snj buf->n_add_for_cb += n;
2344 1.2.2.2 snj
2345 1.2.2.2 snj /* Tell someone about changes in this buffer */
2346 1.2.2.2 snj evbuffer_invoke_callbacks_(buf);
2347 1.2.2.2 snj result = n;
2348 1.2.2.2 snj done:
2349 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
2350 1.2.2.2 snj return result;
2351 1.2.2.2 snj }
2352 1.2.2.2 snj
2353 1.2.2.2 snj #ifdef USE_IOVEC_IMPL
2354 1.2.2.2 snj static inline int
2355 1.2.2.2 snj evbuffer_write_iovec(struct evbuffer *buffer, evutil_socket_t fd,
2356 1.2.2.2 snj ev_ssize_t howmuch)
2357 1.2.2.2 snj {
2358 1.2.2.2 snj IOV_TYPE iov[NUM_WRITE_IOVEC];
2359 1.2.2.2 snj struct evbuffer_chain *chain = buffer->first;
2360 1.2.2.2 snj int n, i = 0;
2361 1.2.2.2 snj
2362 1.2.2.2 snj if (howmuch < 0)
2363 1.2.2.2 snj return -1;
2364 1.2.2.2 snj
2365 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(buffer);
2366 1.2.2.2 snj /* XXX make this top out at some maximal data length? if the
2367 1.2.2.2 snj * buffer has (say) 1MB in it, split over 128 chains, there's
2368 1.2.2.2 snj * no way it all gets written in one go. */
2369 1.2.2.2 snj while (chain != NULL && i < NUM_WRITE_IOVEC && howmuch) {
2370 1.2.2.2 snj #ifdef USE_SENDFILE
2371 1.2.2.2 snj /* we cannot write the file info via writev */
2372 1.2.2.2 snj if (chain->flags & EVBUFFER_SENDFILE)
2373 1.2.2.2 snj break;
2374 1.2.2.2 snj #endif
2375 1.2.2.2 snj iov[i].IOV_PTR_FIELD = (void *) (chain->buffer + chain->misalign);
2376 1.2.2.2 snj if ((size_t)howmuch >= chain->off) {
2377 1.2.2.2 snj /* XXXcould be problematic when windows supports mmap*/
2378 1.2.2.2 snj iov[i++].IOV_LEN_FIELD = (IOV_LEN_TYPE)chain->off;
2379 1.2.2.2 snj howmuch -= chain->off;
2380 1.2.2.2 snj } else {
2381 1.2.2.2 snj /* XXXcould be problematic when windows supports mmap*/
2382 1.2.2.2 snj iov[i++].IOV_LEN_FIELD = (IOV_LEN_TYPE)howmuch;
2383 1.2.2.2 snj break;
2384 1.2.2.2 snj }
2385 1.2.2.2 snj chain = chain->next;
2386 1.2.2.2 snj }
2387 1.2.2.2 snj if (! i)
2388 1.2.2.2 snj return 0;
2389 1.2.2.2 snj
2390 1.2.2.2 snj #ifdef _WIN32
2391 1.2.2.2 snj {
2392 1.2.2.2 snj DWORD bytesSent;
2393 1.2.2.2 snj if (WSASend(fd, iov, i, &bytesSent, 0, NULL, NULL))
2394 1.2.2.2 snj n = -1;
2395 1.2.2.2 snj else
2396 1.2.2.2 snj n = bytesSent;
2397 1.2.2.2 snj }
2398 1.2.2.2 snj #else
2399 1.2.2.2 snj n = writev(fd, iov, i);
2400 1.2.2.2 snj #endif
2401 1.2.2.2 snj return (n);
2402 1.2.2.2 snj }
2403 1.2.2.2 snj #endif
2404 1.2.2.2 snj
2405 1.2.2.2 snj #ifdef USE_SENDFILE
2406 1.2.2.2 snj static inline int
2407 1.2.2.2 snj evbuffer_write_sendfile(struct evbuffer *buffer, evutil_socket_t dest_fd,
2408 1.2.2.2 snj ev_ssize_t howmuch)
2409 1.2.2.2 snj {
2410 1.2.2.2 snj struct evbuffer_chain *chain = buffer->first;
2411 1.2.2.2 snj struct evbuffer_chain_file_segment *info =
2412 1.2.2.2 snj EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_file_segment,
2413 1.2.2.2 snj chain);
2414 1.2.2.2 snj const int source_fd = info->segment->fd;
2415 1.2.2.2 snj #if defined(SENDFILE_IS_MACOSX) || defined(SENDFILE_IS_FREEBSD)
2416 1.2.2.2 snj int res;
2417 1.2.2.2 snj ev_off_t len = chain->off;
2418 1.2.2.2 snj #elif defined(SENDFILE_IS_LINUX) || defined(SENDFILE_IS_SOLARIS)
2419 1.2.2.2 snj ev_ssize_t res;
2420 1.2.2.2 snj ev_off_t offset = chain->misalign;
2421 1.2.2.2 snj #endif
2422 1.2.2.2 snj
2423 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(buffer);
2424 1.2.2.2 snj
2425 1.2.2.2 snj #if defined(SENDFILE_IS_MACOSX)
2426 1.2.2.2 snj res = sendfile(source_fd, dest_fd, chain->misalign, &len, NULL, 0);
2427 1.2.2.2 snj if (res == -1 && !EVUTIL_ERR_RW_RETRIABLE(errno))
2428 1.2.2.2 snj return (-1);
2429 1.2.2.2 snj
2430 1.2.2.2 snj return (len);
2431 1.2.2.2 snj #elif defined(SENDFILE_IS_FREEBSD)
2432 1.2.2.2 snj res = sendfile(source_fd, dest_fd, chain->misalign, chain->off, NULL, &len, 0);
2433 1.2.2.2 snj if (res == -1 && !EVUTIL_ERR_RW_RETRIABLE(errno))
2434 1.2.2.2 snj return (-1);
2435 1.2.2.2 snj
2436 1.2.2.2 snj return (len);
2437 1.2.2.2 snj #elif defined(SENDFILE_IS_LINUX)
2438 1.2.2.2 snj /* TODO(niels): implement splice */
2439 1.2.2.2 snj res = sendfile(dest_fd, source_fd, &offset, chain->off);
2440 1.2.2.2 snj if (res == -1 && EVUTIL_ERR_RW_RETRIABLE(errno)) {
2441 1.2.2.2 snj /* if this is EAGAIN or EINTR return 0; otherwise, -1 */
2442 1.2.2.2 snj return (0);
2443 1.2.2.2 snj }
2444 1.2.2.2 snj return (res);
2445 1.2.2.2 snj #elif defined(SENDFILE_IS_SOLARIS)
2446 1.2.2.2 snj {
2447 1.2.2.2 snj const off_t offset_orig = offset;
2448 1.2.2.2 snj res = sendfile(dest_fd, source_fd, &offset, chain->off);
2449 1.2.2.2 snj if (res == -1 && EVUTIL_ERR_RW_RETRIABLE(errno)) {
2450 1.2.2.2 snj if (offset - offset_orig)
2451 1.2.2.2 snj return offset - offset_orig;
2452 1.2.2.2 snj /* if this is EAGAIN or EINTR and no bytes were
2453 1.2.2.2 snj * written, return 0 */
2454 1.2.2.2 snj return (0);
2455 1.2.2.2 snj }
2456 1.2.2.2 snj return (res);
2457 1.2.2.2 snj }
2458 1.2.2.2 snj #endif
2459 1.2.2.2 snj }
2460 1.2.2.2 snj #endif
2461 1.2.2.2 snj
2462 1.2.2.2 snj int
2463 1.2.2.2 snj evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd,
2464 1.2.2.2 snj ev_ssize_t howmuch)
2465 1.2.2.2 snj {
2466 1.2.2.2 snj int n = -1;
2467 1.2.2.2 snj
2468 1.2.2.2 snj EVBUFFER_LOCK(buffer);
2469 1.2.2.2 snj
2470 1.2.2.2 snj if (buffer->freeze_start) {
2471 1.2.2.2 snj goto done;
2472 1.2.2.2 snj }
2473 1.2.2.2 snj
2474 1.2.2.2 snj if (howmuch < 0 || (size_t)howmuch > buffer->total_len)
2475 1.2.2.2 snj howmuch = buffer->total_len;
2476 1.2.2.2 snj
2477 1.2.2.2 snj if (howmuch > 0) {
2478 1.2.2.2 snj #ifdef USE_SENDFILE
2479 1.2.2.2 snj struct evbuffer_chain *chain = buffer->first;
2480 1.2.2.2 snj if (chain != NULL && (chain->flags & EVBUFFER_SENDFILE))
2481 1.2.2.2 snj n = evbuffer_write_sendfile(buffer, fd, howmuch);
2482 1.2.2.2 snj else {
2483 1.2.2.2 snj #endif
2484 1.2.2.2 snj #ifdef USE_IOVEC_IMPL
2485 1.2.2.2 snj n = evbuffer_write_iovec(buffer, fd, howmuch);
2486 1.2.2.2 snj #elif defined(_WIN32)
2487 1.2.2.2 snj /* XXX(nickm) Don't disable this code until we know if
2488 1.2.2.2 snj * the WSARecv code above works. */
2489 1.2.2.2 snj void *p = evbuffer_pullup(buffer, howmuch);
2490 1.2.2.2 snj n = send(fd, p, howmuch, 0);
2491 1.2.2.2 snj #else
2492 1.2.2.2 snj void *p = evbuffer_pullup(buffer, howmuch);
2493 1.2.2.2 snj n = write(fd, p, howmuch);
2494 1.2.2.2 snj #endif
2495 1.2.2.2 snj #ifdef USE_SENDFILE
2496 1.2.2.2 snj }
2497 1.2.2.2 snj #endif
2498 1.2.2.2 snj }
2499 1.2.2.2 snj
2500 1.2.2.2 snj if (n > 0)
2501 1.2.2.2 snj evbuffer_drain(buffer, n);
2502 1.2.2.2 snj
2503 1.2.2.2 snj done:
2504 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
2505 1.2.2.2 snj return (n);
2506 1.2.2.2 snj }
2507 1.2.2.2 snj
2508 1.2.2.2 snj int
2509 1.2.2.2 snj evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd)
2510 1.2.2.2 snj {
2511 1.2.2.2 snj return evbuffer_write_atmost(buffer, fd, -1);
2512 1.2.2.2 snj }
2513 1.2.2.2 snj
2514 1.2.2.2 snj unsigned char *
2515 1.2.2.2 snj evbuffer_find(struct evbuffer *buffer, const unsigned char *what, size_t len)
2516 1.2.2.2 snj {
2517 1.2.2.2 snj unsigned char *search;
2518 1.2.2.2 snj struct evbuffer_ptr ptr;
2519 1.2.2.2 snj
2520 1.2.2.2 snj EVBUFFER_LOCK(buffer);
2521 1.2.2.2 snj
2522 1.2.2.2 snj ptr = evbuffer_search(buffer, (const char *)what, len, NULL);
2523 1.2.2.2 snj if (ptr.pos < 0) {
2524 1.2.2.2 snj search = NULL;
2525 1.2.2.2 snj } else {
2526 1.2.2.2 snj search = evbuffer_pullup(buffer, ptr.pos + len);
2527 1.2.2.2 snj if (search)
2528 1.2.2.2 snj search += ptr.pos;
2529 1.2.2.2 snj }
2530 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
2531 1.2.2.2 snj return search;
2532 1.2.2.2 snj }
2533 1.2.2.2 snj
2534 1.2.2.2 snj /* Subract <b>howfar</b> from the position of <b>pos</b> within
2535 1.2.2.2 snj * <b>buf</b>. Returns 0 on success, -1 on failure.
2536 1.2.2.2 snj *
2537 1.2.2.2 snj * This isn't exposed yet, because of potential inefficiency issues.
2538 1.2.2.2 snj * Maybe it should be. */
2539 1.2.2.2 snj static int
2540 1.2.2.2 snj evbuffer_ptr_subtract(struct evbuffer *buf, struct evbuffer_ptr *pos,
2541 1.2.2.2 snj size_t howfar)
2542 1.2.2.2 snj {
2543 1.2.2.2 snj if (howfar > (size_t)pos->pos)
2544 1.2.2.2 snj return -1;
2545 1.2.2.2 snj if (pos->internal_.chain && howfar <= pos->internal_.pos_in_chain) {
2546 1.2.2.2 snj pos->internal_.pos_in_chain -= howfar;
2547 1.2.2.2 snj pos->pos -= howfar;
2548 1.2.2.2 snj return 0;
2549 1.2.2.2 snj } else {
2550 1.2.2.2 snj const size_t newpos = pos->pos - howfar;
2551 1.2.2.2 snj /* Here's the inefficient part: it walks over the
2552 1.2.2.2 snj * chains until we hit newpos. */
2553 1.2.2.2 snj return evbuffer_ptr_set(buf, pos, newpos, EVBUFFER_PTR_SET);
2554 1.2.2.2 snj }
2555 1.2.2.2 snj }
2556 1.2.2.2 snj
2557 1.2.2.2 snj int
2558 1.2.2.2 snj evbuffer_ptr_set(struct evbuffer *buf, struct evbuffer_ptr *pos,
2559 1.2.2.2 snj size_t position, enum evbuffer_ptr_how how)
2560 1.2.2.2 snj {
2561 1.2.2.2 snj size_t left = position;
2562 1.2.2.2 snj struct evbuffer_chain *chain = NULL;
2563 1.2.2.2 snj int result = 0;
2564 1.2.2.2 snj
2565 1.2.2.2 snj EVBUFFER_LOCK(buf);
2566 1.2.2.2 snj
2567 1.2.2.2 snj switch (how) {
2568 1.2.2.2 snj case EVBUFFER_PTR_SET:
2569 1.2.2.2 snj chain = buf->first;
2570 1.2.2.2 snj pos->pos = position;
2571 1.2.2.2 snj position = 0;
2572 1.2.2.2 snj break;
2573 1.2.2.2 snj case EVBUFFER_PTR_ADD:
2574 1.2.2.2 snj /* this avoids iterating over all previous chains if
2575 1.2.2.2 snj we just want to advance the position */
2576 1.2.2.2 snj chain = pos->internal_.chain;
2577 1.2.2.2 snj pos->pos += position;
2578 1.2.2.2 snj position = pos->internal_.pos_in_chain;
2579 1.2.2.2 snj break;
2580 1.2.2.2 snj }
2581 1.2.2.2 snj
2582 1.2.2.2 snj while (chain && position + left >= chain->off) {
2583 1.2.2.2 snj left -= chain->off - position;
2584 1.2.2.2 snj chain = chain->next;
2585 1.2.2.2 snj position = 0;
2586 1.2.2.2 snj }
2587 1.2.2.2 snj if (chain) {
2588 1.2.2.2 snj pos->internal_.chain = chain;
2589 1.2.2.2 snj pos->internal_.pos_in_chain = position + left;
2590 1.2.2.2 snj } else if (left == 0) {
2591 1.2.2.2 snj /* The first byte in the (nonexistent) chain after the last chain */
2592 1.2.2.2 snj pos->internal_.chain = NULL;
2593 1.2.2.2 snj pos->internal_.pos_in_chain = 0;
2594 1.2.2.2 snj } else {
2595 1.2.2.2 snj PTR_NOT_FOUND(pos);
2596 1.2.2.2 snj result = -1;
2597 1.2.2.2 snj }
2598 1.2.2.2 snj
2599 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
2600 1.2.2.2 snj
2601 1.2.2.2 snj return result;
2602 1.2.2.2 snj }
2603 1.2.2.2 snj
2604 1.2.2.2 snj /**
2605 1.2.2.2 snj Compare the bytes in buf at position pos to the len bytes in mem. Return
2606 1.2.2.2 snj less than 0, 0, or greater than 0 as memcmp.
2607 1.2.2.2 snj */
2608 1.2.2.2 snj static int
2609 1.2.2.2 snj evbuffer_ptr_memcmp(const struct evbuffer *buf, const struct evbuffer_ptr *pos,
2610 1.2.2.2 snj const char *mem, size_t len)
2611 1.2.2.2 snj {
2612 1.2.2.2 snj struct evbuffer_chain *chain;
2613 1.2.2.2 snj size_t position;
2614 1.2.2.2 snj int r;
2615 1.2.2.2 snj
2616 1.2.2.2 snj ASSERT_EVBUFFER_LOCKED(buf);
2617 1.2.2.2 snj
2618 1.2.2.2 snj if (pos->pos + len > buf->total_len)
2619 1.2.2.2 snj return -1;
2620 1.2.2.2 snj
2621 1.2.2.2 snj chain = pos->internal_.chain;
2622 1.2.2.2 snj position = pos->internal_.pos_in_chain;
2623 1.2.2.2 snj while (len && chain) {
2624 1.2.2.2 snj size_t n_comparable;
2625 1.2.2.2 snj if (len + position > chain->off)
2626 1.2.2.2 snj n_comparable = chain->off - position;
2627 1.2.2.2 snj else
2628 1.2.2.2 snj n_comparable = len;
2629 1.2.2.2 snj r = memcmp(chain->buffer + chain->misalign + position, mem,
2630 1.2.2.2 snj n_comparable);
2631 1.2.2.2 snj if (r)
2632 1.2.2.2 snj return r;
2633 1.2.2.2 snj mem += n_comparable;
2634 1.2.2.2 snj len -= n_comparable;
2635 1.2.2.2 snj position = 0;
2636 1.2.2.2 snj chain = chain->next;
2637 1.2.2.2 snj }
2638 1.2.2.2 snj
2639 1.2.2.2 snj return 0;
2640 1.2.2.2 snj }
2641 1.2.2.2 snj
2642 1.2.2.2 snj struct evbuffer_ptr
2643 1.2.2.2 snj evbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start)
2644 1.2.2.2 snj {
2645 1.2.2.2 snj return evbuffer_search_range(buffer, what, len, start, NULL);
2646 1.2.2.2 snj }
2647 1.2.2.2 snj
2648 1.2.2.2 snj struct evbuffer_ptr
2649 1.2.2.2 snj evbuffer_search_range(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start, const struct evbuffer_ptr *end)
2650 1.2.2.2 snj {
2651 1.2.2.2 snj struct evbuffer_ptr pos;
2652 1.2.2.2 snj struct evbuffer_chain *chain, *last_chain = NULL;
2653 1.2.2.2 snj const unsigned char *p;
2654 1.2.2.2 snj char first;
2655 1.2.2.2 snj
2656 1.2.2.2 snj EVBUFFER_LOCK(buffer);
2657 1.2.2.2 snj
2658 1.2.2.2 snj if (start) {
2659 1.2.2.2 snj memcpy(&pos, start, sizeof(pos));
2660 1.2.2.2 snj chain = pos.internal_.chain;
2661 1.2.2.2 snj } else {
2662 1.2.2.2 snj pos.pos = 0;
2663 1.2.2.2 snj chain = pos.internal_.chain = buffer->first;
2664 1.2.2.2 snj pos.internal_.pos_in_chain = 0;
2665 1.2.2.2 snj }
2666 1.2.2.2 snj
2667 1.2.2.2 snj if (end)
2668 1.2.2.2 snj last_chain = end->internal_.chain;
2669 1.2.2.2 snj
2670 1.2.2.2 snj if (!len || len > EV_SSIZE_MAX)
2671 1.2.2.2 snj goto done;
2672 1.2.2.2 snj
2673 1.2.2.2 snj first = what[0];
2674 1.2.2.2 snj
2675 1.2.2.2 snj while (chain) {
2676 1.2.2.2 snj const unsigned char *start_at =
2677 1.2.2.2 snj chain->buffer + chain->misalign +
2678 1.2.2.2 snj pos.internal_.pos_in_chain;
2679 1.2.2.2 snj p = memchr(start_at, first,
2680 1.2.2.2 snj chain->off - pos.internal_.pos_in_chain);
2681 1.2.2.2 snj if (p) {
2682 1.2.2.2 snj pos.pos += p - start_at;
2683 1.2.2.2 snj pos.internal_.pos_in_chain += p - start_at;
2684 1.2.2.2 snj if (!evbuffer_ptr_memcmp(buffer, &pos, what, len)) {
2685 1.2.2.2 snj if (end && pos.pos + (ev_ssize_t)len > end->pos)
2686 1.2.2.2 snj goto not_found;
2687 1.2.2.2 snj else
2688 1.2.2.2 snj goto done;
2689 1.2.2.2 snj }
2690 1.2.2.2 snj ++pos.pos;
2691 1.2.2.2 snj ++pos.internal_.pos_in_chain;
2692 1.2.2.2 snj if (pos.internal_.pos_in_chain == chain->off) {
2693 1.2.2.2 snj chain = pos.internal_.chain = chain->next;
2694 1.2.2.2 snj pos.internal_.pos_in_chain = 0;
2695 1.2.2.2 snj }
2696 1.2.2.2 snj } else {
2697 1.2.2.2 snj if (chain == last_chain)
2698 1.2.2.2 snj goto not_found;
2699 1.2.2.2 snj pos.pos += chain->off - pos.internal_.pos_in_chain;
2700 1.2.2.2 snj chain = pos.internal_.chain = chain->next;
2701 1.2.2.2 snj pos.internal_.pos_in_chain = 0;
2702 1.2.2.2 snj }
2703 1.2.2.2 snj }
2704 1.2.2.2 snj
2705 1.2.2.2 snj not_found:
2706 1.2.2.2 snj PTR_NOT_FOUND(&pos);
2707 1.2.2.2 snj done:
2708 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
2709 1.2.2.2 snj return pos;
2710 1.2.2.2 snj }
2711 1.2.2.2 snj
2712 1.2.2.2 snj int
2713 1.2.2.2 snj evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
2714 1.2.2.2 snj struct evbuffer_ptr *start_at,
2715 1.2.2.2 snj struct evbuffer_iovec *vec, int n_vec)
2716 1.2.2.2 snj {
2717 1.2.2.2 snj struct evbuffer_chain *chain;
2718 1.2.2.2 snj int idx = 0;
2719 1.2.2.2 snj ev_ssize_t len_so_far = 0;
2720 1.2.2.2 snj
2721 1.2.2.2 snj /* Avoid locking in trivial edge cases */
2722 1.2.2.2 snj if (start_at && start_at->internal_.chain == NULL)
2723 1.2.2.2 snj return 0;
2724 1.2.2.2 snj
2725 1.2.2.2 snj EVBUFFER_LOCK(buffer);
2726 1.2.2.2 snj
2727 1.2.2.2 snj if (start_at) {
2728 1.2.2.2 snj chain = start_at->internal_.chain;
2729 1.2.2.2 snj len_so_far = chain->off
2730 1.2.2.2 snj - start_at->internal_.pos_in_chain;
2731 1.2.2.2 snj idx = 1;
2732 1.2.2.2 snj if (n_vec > 0) {
2733 1.2.2.2 snj vec[0].iov_base = chain->buffer + chain->misalign
2734 1.2.2.2 snj + start_at->internal_.pos_in_chain;
2735 1.2.2.2 snj vec[0].iov_len = len_so_far;
2736 1.2.2.2 snj }
2737 1.2.2.2 snj chain = chain->next;
2738 1.2.2.2 snj } else {
2739 1.2.2.2 snj chain = buffer->first;
2740 1.2.2.2 snj }
2741 1.2.2.2 snj
2742 1.2.2.2 snj if (n_vec == 0 && len < 0) {
2743 1.2.2.2 snj /* If no vectors are provided and they asked for "everything",
2744 1.2.2.2 snj * pretend they asked for the actual available amount. */
2745 1.2.2.2 snj len = buffer->total_len - len_so_far;
2746 1.2.2.2 snj }
2747 1.2.2.2 snj
2748 1.2.2.2 snj while (chain) {
2749 1.2.2.2 snj if (len >= 0 && len_so_far >= len)
2750 1.2.2.2 snj break;
2751 1.2.2.2 snj if (idx<n_vec) {
2752 1.2.2.2 snj vec[idx].iov_base = chain->buffer + chain->misalign;
2753 1.2.2.2 snj vec[idx].iov_len = chain->off;
2754 1.2.2.2 snj } else if (len<0) {
2755 1.2.2.2 snj break;
2756 1.2.2.2 snj }
2757 1.2.2.2 snj ++idx;
2758 1.2.2.2 snj len_so_far += chain->off;
2759 1.2.2.2 snj chain = chain->next;
2760 1.2.2.2 snj }
2761 1.2.2.2 snj
2762 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
2763 1.2.2.2 snj
2764 1.2.2.2 snj return idx;
2765 1.2.2.2 snj }
2766 1.2.2.2 snj
2767 1.2.2.2 snj
2768 1.2.2.2 snj int
2769 1.2.2.2 snj evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
2770 1.2.2.2 snj {
2771 1.2.2.2 snj char *buffer;
2772 1.2.2.2 snj size_t space;
2773 1.2.2.2 snj int sz, result = -1;
2774 1.2.2.2 snj va_list aq;
2775 1.2.2.2 snj struct evbuffer_chain *chain;
2776 1.2.2.2 snj
2777 1.2.2.2 snj
2778 1.2.2.2 snj EVBUFFER_LOCK(buf);
2779 1.2.2.2 snj
2780 1.2.2.2 snj if (buf->freeze_end) {
2781 1.2.2.2 snj goto done;
2782 1.2.2.2 snj }
2783 1.2.2.2 snj
2784 1.2.2.2 snj /* make sure that at least some space is available */
2785 1.2.2.2 snj if ((chain = evbuffer_expand_singlechain(buf, 64)) == NULL)
2786 1.2.2.2 snj goto done;
2787 1.2.2.2 snj
2788 1.2.2.2 snj for (;;) {
2789 1.2.2.2 snj #if 0
2790 1.2.2.2 snj size_t used = chain->misalign + chain->off;
2791 1.2.2.2 snj buffer = (char *)chain->buffer + chain->misalign + chain->off;
2792 1.2.2.2 snj EVUTIL_ASSERT(chain->buffer_len >= used);
2793 1.2.2.2 snj space = chain->buffer_len - used;
2794 1.2.2.2 snj #endif
2795 1.2.2.2 snj buffer = (char*) CHAIN_SPACE_PTR(chain);
2796 1.2.2.2 snj space = (size_t) CHAIN_SPACE_LEN(chain);
2797 1.2.2.2 snj
2798 1.2.2.2 snj #ifndef va_copy
2799 1.2.2.2 snj #define va_copy(dst, src) memcpy(&(dst), &(src), sizeof(va_list))
2800 1.2.2.2 snj #endif
2801 1.2.2.2 snj va_copy(aq, ap);
2802 1.2.2.2 snj
2803 1.2.2.2 snj sz = evutil_vsnprintf(buffer, space, fmt, aq);
2804 1.2.2.2 snj
2805 1.2.2.2 snj va_end(aq);
2806 1.2.2.2 snj
2807 1.2.2.2 snj if (sz < 0)
2808 1.2.2.2 snj goto done;
2809 1.2.2.2 snj if ((size_t)sz < space) {
2810 1.2.2.2 snj chain->off += sz;
2811 1.2.2.2 snj buf->total_len += sz;
2812 1.2.2.2 snj buf->n_add_for_cb += sz;
2813 1.2.2.2 snj
2814 1.2.2.2 snj advance_last_with_data(buf);
2815 1.2.2.2 snj evbuffer_invoke_callbacks_(buf);
2816 1.2.2.2 snj result = sz;
2817 1.2.2.2 snj goto done;
2818 1.2.2.2 snj }
2819 1.2.2.2 snj if ((chain = evbuffer_expand_singlechain(buf, sz + 1)) == NULL)
2820 1.2.2.2 snj goto done;
2821 1.2.2.2 snj }
2822 1.2.2.2 snj /* NOTREACHED */
2823 1.2.2.2 snj
2824 1.2.2.2 snj done:
2825 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
2826 1.2.2.2 snj return result;
2827 1.2.2.2 snj }
2828 1.2.2.2 snj
2829 1.2.2.2 snj int
2830 1.2.2.2 snj evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
2831 1.2.2.2 snj {
2832 1.2.2.2 snj int res = -1;
2833 1.2.2.2 snj va_list ap;
2834 1.2.2.2 snj
2835 1.2.2.2 snj va_start(ap, fmt);
2836 1.2.2.2 snj res = evbuffer_add_vprintf(buf, fmt, ap);
2837 1.2.2.2 snj va_end(ap);
2838 1.2.2.2 snj
2839 1.2.2.2 snj return (res);
2840 1.2.2.2 snj }
2841 1.2.2.2 snj
2842 1.2.2.2 snj int
2843 1.2.2.2 snj evbuffer_add_reference(struct evbuffer *outbuf,
2844 1.2.2.2 snj const void *data, size_t datlen,
2845 1.2.2.2 snj evbuffer_ref_cleanup_cb cleanupfn, void *extra)
2846 1.2.2.2 snj {
2847 1.2.2.2 snj struct evbuffer_chain *chain;
2848 1.2.2.2 snj struct evbuffer_chain_reference *info;
2849 1.2.2.2 snj int result = -1;
2850 1.2.2.2 snj
2851 1.2.2.2 snj chain = evbuffer_chain_new(sizeof(struct evbuffer_chain_reference));
2852 1.2.2.2 snj if (!chain)
2853 1.2.2.2 snj return (-1);
2854 1.2.2.2 snj chain->flags |= EVBUFFER_REFERENCE | EVBUFFER_IMMUTABLE;
2855 1.2.2.2 snj chain->buffer = (u_char *)data;
2856 1.2.2.2 snj chain->buffer_len = datlen;
2857 1.2.2.2 snj chain->off = datlen;
2858 1.2.2.2 snj
2859 1.2.2.2 snj info = EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_reference, chain);
2860 1.2.2.2 snj info->cleanupfn = cleanupfn;
2861 1.2.2.2 snj info->extra = extra;
2862 1.2.2.2 snj
2863 1.2.2.2 snj EVBUFFER_LOCK(outbuf);
2864 1.2.2.2 snj if (outbuf->freeze_end) {
2865 1.2.2.2 snj /* don't call chain_free; we do not want to actually invoke
2866 1.2.2.2 snj * the cleanup function */
2867 1.2.2.2 snj mm_free(chain);
2868 1.2.2.2 snj goto done;
2869 1.2.2.2 snj }
2870 1.2.2.2 snj evbuffer_chain_insert(outbuf, chain);
2871 1.2.2.2 snj outbuf->n_add_for_cb += datlen;
2872 1.2.2.2 snj
2873 1.2.2.2 snj evbuffer_invoke_callbacks_(outbuf);
2874 1.2.2.2 snj
2875 1.2.2.2 snj result = 0;
2876 1.2.2.2 snj done:
2877 1.2.2.2 snj EVBUFFER_UNLOCK(outbuf);
2878 1.2.2.2 snj
2879 1.2.2.2 snj return result;
2880 1.2.2.2 snj }
2881 1.2.2.2 snj
2882 1.2.2.2 snj /* TODO(niels): we may want to add to automagically convert to mmap, in
2883 1.2.2.2 snj * case evbuffer_remove() or evbuffer_pullup() are being used.
2884 1.2.2.2 snj */
2885 1.2.2.2 snj struct evbuffer_file_segment *
2886 1.2.2.2 snj evbuffer_file_segment_new(
2887 1.2.2.2 snj int fd, ev_off_t offset, ev_off_t length, unsigned flags)
2888 1.2.2.2 snj {
2889 1.2.2.2 snj struct evbuffer_file_segment *seg =
2890 1.2.2.2 snj mm_calloc(sizeof(struct evbuffer_file_segment), 1);
2891 1.2.2.2 snj if (!seg)
2892 1.2.2.2 snj return NULL;
2893 1.2.2.2 snj seg->refcnt = 1;
2894 1.2.2.2 snj seg->fd = fd;
2895 1.2.2.2 snj seg->flags = flags;
2896 1.2.2.2 snj seg->file_offset = offset;
2897 1.2.2.2 snj seg->cleanup_cb = NULL;
2898 1.2.2.2 snj seg->cleanup_cb_arg = NULL;
2899 1.2.2.2 snj #ifdef _WIN32
2900 1.2.2.2 snj #ifndef lseek
2901 1.2.2.2 snj #define lseek _lseeki64
2902 1.2.2.2 snj #endif
2903 1.2.2.2 snj #ifndef fstat
2904 1.2.2.2 snj #define fstat _fstat
2905 1.2.2.2 snj #endif
2906 1.2.2.2 snj #ifndef stat
2907 1.2.2.2 snj #define stat _stat
2908 1.2.2.2 snj #endif
2909 1.2.2.2 snj #endif
2910 1.2.2.2 snj if (length == -1) {
2911 1.2.2.2 snj struct stat st;
2912 1.2.2.2 snj if (fstat(fd, &st) < 0)
2913 1.2.2.2 snj goto err;
2914 1.2.2.2 snj length = st.st_size;
2915 1.2.2.2 snj }
2916 1.2.2.2 snj seg->length = length;
2917 1.2.2.2 snj
2918 1.2.2.2 snj #if defined(USE_SENDFILE)
2919 1.2.2.2 snj if (!(flags & EVBUF_FS_DISABLE_SENDFILE)) {
2920 1.2.2.2 snj seg->can_sendfile = 1;
2921 1.2.2.2 snj goto done;
2922 1.2.2.2 snj }
2923 1.2.2.2 snj #endif
2924 1.2.2.2 snj
2925 1.2.2.2 snj if (evbuffer_file_segment_materialize(seg)<0)
2926 1.2.2.2 snj goto err;
2927 1.2.2.2 snj
2928 1.2.2.2 snj #if defined(USE_SENDFILE)
2929 1.2.2.2 snj done:
2930 1.2.2.2 snj #endif
2931 1.2.2.2 snj if (!(flags & EVBUF_FS_DISABLE_LOCKING)) {
2932 1.2.2.2 snj EVTHREAD_ALLOC_LOCK(seg->lock, 0);
2933 1.2.2.2 snj }
2934 1.2.2.2 snj return seg;
2935 1.2.2.2 snj err:
2936 1.2.2.2 snj mm_free(seg);
2937 1.2.2.2 snj return NULL;
2938 1.2.2.2 snj }
2939 1.2.2.2 snj
2940 1.2.2.2 snj #ifdef EVENT__HAVE_MMAP
2941 1.2.2.2 snj static long
2942 1.2.2.2 snj get_page_size(void)
2943 1.2.2.2 snj {
2944 1.2.2.2 snj #ifdef SC_PAGE_SIZE
2945 1.2.2.2 snj return sysconf(SC_PAGE_SIZE);
2946 1.2.2.2 snj #elif defined(_SC_PAGE_SIZE)
2947 1.2.2.2 snj return sysconf(_SC_PAGE_SIZE);
2948 1.2.2.2 snj #else
2949 1.2.2.2 snj return 1;
2950 1.2.2.2 snj #endif
2951 1.2.2.2 snj }
2952 1.2.2.2 snj #endif
2953 1.2.2.2 snj
2954 1.2.2.2 snj /* DOCDOC */
2955 1.2.2.2 snj /* Requires lock */
2956 1.2.2.2 snj static int
2957 1.2.2.2 snj evbuffer_file_segment_materialize(struct evbuffer_file_segment *seg)
2958 1.2.2.2 snj {
2959 1.2.2.2 snj const unsigned flags = seg->flags;
2960 1.2.2.2 snj const int fd = seg->fd;
2961 1.2.2.2 snj const ev_off_t length = seg->length;
2962 1.2.2.2 snj const ev_off_t offset = seg->file_offset;
2963 1.2.2.2 snj
2964 1.2.2.2 snj if (seg->contents)
2965 1.2.2.2 snj return 0; /* already materialized */
2966 1.2.2.2 snj
2967 1.2.2.2 snj #if defined(EVENT__HAVE_MMAP)
2968 1.2.2.2 snj if (!(flags & EVBUF_FS_DISABLE_MMAP)) {
2969 1.2.2.2 snj off_t offset_rounded = 0, offset_leftover = 0;
2970 1.2.2.2 snj void *mapped;
2971 1.2.2.2 snj if (offset) {
2972 1.2.2.2 snj /* mmap implementations don't generally like us
2973 1.2.2.2 snj * to have an offset that isn't a round */
2974 1.2.2.2 snj long page_size = get_page_size();
2975 1.2.2.2 snj if (page_size == -1)
2976 1.2.2.2 snj goto err;
2977 1.2.2.2 snj offset_leftover = offset % page_size;
2978 1.2.2.2 snj offset_rounded = offset - offset_leftover;
2979 1.2.2.2 snj }
2980 1.2.2.2 snj mapped = mmap(NULL, length + offset_leftover,
2981 1.2.2.2 snj PROT_READ,
2982 1.2.2.2 snj #ifdef MAP_NOCACHE
2983 1.2.2.2 snj MAP_NOCACHE | /* ??? */
2984 1.2.2.2 snj #endif
2985 1.2.2.2 snj #ifdef MAP_FILE
2986 1.2.2.2 snj MAP_FILE |
2987 1.2.2.2 snj #endif
2988 1.2.2.2 snj MAP_PRIVATE,
2989 1.2.2.2 snj fd, offset_rounded);
2990 1.2.2.2 snj if (mapped == MAP_FAILED) {
2991 1.2.2.2 snj event_warn("%s: mmap(%d, %d, %zu) failed",
2992 1.2.2.2 snj __func__, fd, 0, (size_t)(offset + length));
2993 1.2.2.2 snj } else {
2994 1.2.2.2 snj seg->mapping = mapped;
2995 1.2.2.2 snj seg->contents = (char*)mapped+offset_leftover;
2996 1.2.2.2 snj seg->mmap_offset = 0;
2997 1.2.2.2 snj seg->is_mapping = 1;
2998 1.2.2.2 snj goto done;
2999 1.2.2.2 snj }
3000 1.2.2.2 snj }
3001 1.2.2.2 snj #endif
3002 1.2.2.2 snj #ifdef _WIN32
3003 1.2.2.2 snj if (!(flags & EVBUF_FS_DISABLE_MMAP)) {
3004 1.2.2.2 snj intptr_t h = _get_osfhandle(fd);
3005 1.2.2.2 snj HANDLE m;
3006 1.2.2.2 snj ev_uint64_t total_size = length+offset;
3007 1.2.2.2 snj if ((HANDLE)h == INVALID_HANDLE_VALUE)
3008 1.2.2.2 snj goto err;
3009 1.2.2.2 snj m = CreateFileMapping((HANDLE)h, NULL, PAGE_READONLY,
3010 1.2.2.2 snj (total_size >> 32), total_size & 0xfffffffful,
3011 1.2.2.2 snj NULL);
3012 1.2.2.2 snj if (m != INVALID_HANDLE_VALUE) { /* Does h leak? */
3013 1.2.2.2 snj seg->mapping_handle = m;
3014 1.2.2.2 snj seg->mmap_offset = offset;
3015 1.2.2.2 snj seg->is_mapping = 1;
3016 1.2.2.2 snj goto done;
3017 1.2.2.2 snj }
3018 1.2.2.2 snj }
3019 1.2.2.2 snj #endif
3020 1.2.2.2 snj {
3021 1.2.2.2 snj ev_off_t start_pos = lseek(fd, 0, SEEK_CUR), pos;
3022 1.2.2.2 snj ev_off_t read_so_far = 0;
3023 1.2.2.2 snj char *mem;
3024 1.2.2.2 snj int e;
3025 1.2.2.2 snj ev_ssize_t n = 0;
3026 1.2.2.2 snj if (!(mem = mm_malloc(length)))
3027 1.2.2.2 snj goto err;
3028 1.2.2.2 snj if (start_pos < 0) {
3029 1.2.2.2 snj mm_free(mem);
3030 1.2.2.2 snj goto err;
3031 1.2.2.2 snj }
3032 1.2.2.2 snj if (lseek(fd, offset, SEEK_SET) < 0) {
3033 1.2.2.2 snj mm_free(mem);
3034 1.2.2.2 snj goto err;
3035 1.2.2.2 snj }
3036 1.2.2.2 snj while (read_so_far < length) {
3037 1.2.2.2 snj n = read(fd, mem+read_so_far, length-read_so_far);
3038 1.2.2.2 snj if (n <= 0)
3039 1.2.2.2 snj break;
3040 1.2.2.2 snj read_so_far += n;
3041 1.2.2.2 snj }
3042 1.2.2.2 snj
3043 1.2.2.2 snj e = errno;
3044 1.2.2.2 snj pos = lseek(fd, start_pos, SEEK_SET);
3045 1.2.2.2 snj if (n < 0 || (n == 0 && length > read_so_far)) {
3046 1.2.2.2 snj mm_free(mem);
3047 1.2.2.2 snj errno = e;
3048 1.2.2.2 snj goto err;
3049 1.2.2.2 snj } else if (pos < 0) {
3050 1.2.2.2 snj mm_free(mem);
3051 1.2.2.2 snj goto err;
3052 1.2.2.2 snj }
3053 1.2.2.2 snj
3054 1.2.2.2 snj seg->contents = mem;
3055 1.2.2.2 snj }
3056 1.2.2.2 snj
3057 1.2.2.2 snj done:
3058 1.2.2.2 snj return 0;
3059 1.2.2.2 snj err:
3060 1.2.2.2 snj return -1;
3061 1.2.2.2 snj }
3062 1.2.2.2 snj
3063 1.2.2.2 snj void evbuffer_file_segment_add_cleanup_cb(struct evbuffer_file_segment *seg,
3064 1.2.2.2 snj evbuffer_file_segment_cleanup_cb cb, void* arg)
3065 1.2.2.2 snj {
3066 1.2.2.2 snj EVUTIL_ASSERT(seg->refcnt > 0);
3067 1.2.2.2 snj seg->cleanup_cb = cb;
3068 1.2.2.2 snj seg->cleanup_cb_arg = arg;
3069 1.2.2.2 snj }
3070 1.2.2.2 snj
3071 1.2.2.2 snj void
3072 1.2.2.2 snj evbuffer_file_segment_free(struct evbuffer_file_segment *seg)
3073 1.2.2.2 snj {
3074 1.2.2.2 snj int refcnt;
3075 1.2.2.2 snj EVLOCK_LOCK(seg->lock, 0);
3076 1.2.2.2 snj refcnt = --seg->refcnt;
3077 1.2.2.2 snj EVLOCK_UNLOCK(seg->lock, 0);
3078 1.2.2.2 snj if (refcnt > 0)
3079 1.2.2.2 snj return;
3080 1.2.2.2 snj EVUTIL_ASSERT(refcnt == 0);
3081 1.2.2.2 snj
3082 1.2.2.2 snj if (seg->is_mapping) {
3083 1.2.2.2 snj #ifdef _WIN32
3084 1.2.2.2 snj CloseHandle(seg->mapping_handle);
3085 1.2.2.2 snj #elif defined (EVENT__HAVE_MMAP)
3086 1.2.2.2 snj off_t offset_leftover;
3087 1.2.2.2 snj offset_leftover = seg->file_offset % get_page_size();
3088 1.2.2.2 snj if (munmap(seg->mapping, seg->length + offset_leftover) == -1)
3089 1.2.2.2 snj event_warn("%s: munmap failed", __func__);
3090 1.2.2.2 snj #endif
3091 1.2.2.2 snj } else if (seg->contents) {
3092 1.2.2.2 snj mm_free(seg->contents);
3093 1.2.2.2 snj }
3094 1.2.2.2 snj
3095 1.2.2.2 snj if ((seg->flags & EVBUF_FS_CLOSE_ON_FREE) && seg->fd >= 0) {
3096 1.2.2.2 snj close(seg->fd);
3097 1.2.2.2 snj }
3098 1.2.2.2 snj
3099 1.2.2.2 snj if (seg->cleanup_cb) {
3100 1.2.2.2 snj (*seg->cleanup_cb)((struct evbuffer_file_segment const*)seg,
3101 1.2.2.2 snj seg->flags, seg->cleanup_cb_arg);
3102 1.2.2.2 snj seg->cleanup_cb = NULL;
3103 1.2.2.2 snj seg->cleanup_cb_arg = NULL;
3104 1.2.2.2 snj }
3105 1.2.2.2 snj
3106 1.2.2.2 snj EVTHREAD_FREE_LOCK(seg->lock, 0);
3107 1.2.2.2 snj mm_free(seg);
3108 1.2.2.2 snj }
3109 1.2.2.2 snj
3110 1.2.2.2 snj int
3111 1.2.2.2 snj evbuffer_add_file_segment(struct evbuffer *buf,
3112 1.2.2.2 snj struct evbuffer_file_segment *seg, ev_off_t offset, ev_off_t length)
3113 1.2.2.2 snj {
3114 1.2.2.2 snj struct evbuffer_chain *chain;
3115 1.2.2.2 snj struct evbuffer_chain_file_segment *extra;
3116 1.2.2.2 snj int can_use_sendfile = 0;
3117 1.2.2.2 snj
3118 1.2.2.2 snj EVBUFFER_LOCK(buf);
3119 1.2.2.2 snj EVLOCK_LOCK(seg->lock, 0);
3120 1.2.2.2 snj if (buf->flags & EVBUFFER_FLAG_DRAINS_TO_FD) {
3121 1.2.2.2 snj can_use_sendfile = 1;
3122 1.2.2.2 snj } else {
3123 1.2.2.2 snj if (!seg->contents) {
3124 1.2.2.2 snj if (evbuffer_file_segment_materialize(seg)<0) {
3125 1.2.2.2 snj EVLOCK_UNLOCK(seg->lock, 0);
3126 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
3127 1.2.2.2 snj return -1;
3128 1.2.2.2 snj }
3129 1.2.2.2 snj }
3130 1.2.2.2 snj }
3131 1.2.2.2 snj ++seg->refcnt;
3132 1.2.2.2 snj EVLOCK_UNLOCK(seg->lock, 0);
3133 1.2.2.2 snj
3134 1.2.2.2 snj if (buf->freeze_end)
3135 1.2.2.2 snj goto err;
3136 1.2.2.2 snj
3137 1.2.2.2 snj if (length < 0) {
3138 1.2.2.2 snj if (offset > seg->length)
3139 1.2.2.2 snj goto err;
3140 1.2.2.2 snj length = seg->length - offset;
3141 1.2.2.2 snj }
3142 1.2.2.2 snj
3143 1.2.2.2 snj /* Can we actually add this? */
3144 1.2.2.2 snj if (offset+length > seg->length)
3145 1.2.2.2 snj goto err;
3146 1.2.2.2 snj
3147 1.2.2.2 snj chain = evbuffer_chain_new(sizeof(struct evbuffer_chain_file_segment));
3148 1.2.2.2 snj if (!chain)
3149 1.2.2.2 snj goto err;
3150 1.2.2.2 snj extra = EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_file_segment, chain);
3151 1.2.2.2 snj
3152 1.2.2.2 snj chain->flags |= EVBUFFER_IMMUTABLE|EVBUFFER_FILESEGMENT;
3153 1.2.2.2 snj if (can_use_sendfile && seg->can_sendfile) {
3154 1.2.2.2 snj chain->flags |= EVBUFFER_SENDFILE;
3155 1.2.2.2 snj chain->misalign = seg->file_offset + offset;
3156 1.2.2.2 snj chain->off = length;
3157 1.2.2.2 snj chain->buffer_len = chain->misalign + length;
3158 1.2.2.2 snj } else if (seg->is_mapping) {
3159 1.2.2.2 snj #ifdef _WIN32
3160 1.2.2.2 snj ev_uint64_t total_offset = seg->mmap_offset+offset;
3161 1.2.2.2 snj ev_uint64_t offset_rounded=0, offset_remaining=0;
3162 1.2.2.2 snj LPVOID data;
3163 1.2.2.2 snj if (total_offset) {
3164 1.2.2.2 snj SYSTEM_INFO si;
3165 1.2.2.2 snj memset(&si, 0, sizeof(si)); /* cargo cult */
3166 1.2.2.2 snj GetSystemInfo(&si);
3167 1.2.2.2 snj offset_remaining = total_offset % si.dwAllocationGranularity;
3168 1.2.2.2 snj offset_rounded = total_offset - offset_remaining;
3169 1.2.2.2 snj }
3170 1.2.2.2 snj data = MapViewOfFile(
3171 1.2.2.2 snj seg->mapping_handle,
3172 1.2.2.2 snj FILE_MAP_READ,
3173 1.2.2.2 snj offset_rounded >> 32,
3174 1.2.2.2 snj offset_rounded & 0xfffffffful,
3175 1.2.2.2 snj length + offset_remaining);
3176 1.2.2.2 snj if (data == NULL) {
3177 1.2.2.2 snj mm_free(chain);
3178 1.2.2.2 snj goto err;
3179 1.2.2.2 snj }
3180 1.2.2.2 snj chain->buffer = (unsigned char*) data;
3181 1.2.2.2 snj chain->buffer_len = length+offset_remaining;
3182 1.2.2.2 snj chain->misalign = offset_remaining;
3183 1.2.2.2 snj chain->off = length;
3184 1.2.2.2 snj #else
3185 1.2.2.2 snj chain->buffer = (unsigned char*)(seg->contents + offset);
3186 1.2.2.2 snj chain->buffer_len = length;
3187 1.2.2.2 snj chain->off = length;
3188 1.2.2.2 snj #endif
3189 1.2.2.2 snj } else {
3190 1.2.2.2 snj chain->buffer = (unsigned char*)(seg->contents + offset);
3191 1.2.2.2 snj chain->buffer_len = length;
3192 1.2.2.2 snj chain->off = length;
3193 1.2.2.2 snj }
3194 1.2.2.2 snj
3195 1.2.2.2 snj extra->segment = seg;
3196 1.2.2.2 snj buf->n_add_for_cb += length;
3197 1.2.2.2 snj evbuffer_chain_insert(buf, chain);
3198 1.2.2.2 snj
3199 1.2.2.2 snj evbuffer_invoke_callbacks_(buf);
3200 1.2.2.2 snj
3201 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
3202 1.2.2.2 snj
3203 1.2.2.2 snj return 0;
3204 1.2.2.2 snj err:
3205 1.2.2.2 snj EVBUFFER_UNLOCK(buf);
3206 1.2.2.2 snj evbuffer_file_segment_free(seg);
3207 1.2.2.2 snj return -1;
3208 1.2.2.2 snj }
3209 1.2.2.2 snj
3210 1.2.2.2 snj int
3211 1.2.2.2 snj evbuffer_add_file(struct evbuffer *buf, int fd, ev_off_t offset, ev_off_t length)
3212 1.2.2.2 snj {
3213 1.2.2.2 snj struct evbuffer_file_segment *seg;
3214 1.2.2.2 snj unsigned flags = EVBUF_FS_CLOSE_ON_FREE;
3215 1.2.2.2 snj int r;
3216 1.2.2.2 snj
3217 1.2.2.2 snj seg = evbuffer_file_segment_new(fd, offset, length, flags);
3218 1.2.2.2 snj if (!seg)
3219 1.2.2.2 snj return -1;
3220 1.2.2.2 snj r = evbuffer_add_file_segment(buf, seg, 0, length);
3221 1.2.2.2 snj if (r == 0)
3222 1.2.2.2 snj evbuffer_file_segment_free(seg);
3223 1.2.2.2 snj return r;
3224 1.2.2.2 snj }
3225 1.2.2.2 snj
3226 1.2.2.2 snj void
3227 1.2.2.2 snj evbuffer_setcb(struct evbuffer *buffer, evbuffer_cb cb, void *cbarg)
3228 1.2.2.2 snj {
3229 1.2.2.2 snj EVBUFFER_LOCK(buffer);
3230 1.2.2.2 snj
3231 1.2.2.2 snj if (!LIST_EMPTY(&buffer->callbacks))
3232 1.2.2.2 snj evbuffer_remove_all_callbacks(buffer);
3233 1.2.2.2 snj
3234 1.2.2.2 snj if (cb) {
3235 1.2.2.2 snj struct evbuffer_cb_entry *ent =
3236 1.2.2.2 snj evbuffer_add_cb(buffer, NULL, cbarg);
3237 1.2.2.2 snj ent->cb.cb_obsolete = cb;
3238 1.2.2.2 snj ent->flags |= EVBUFFER_CB_OBSOLETE;
3239 1.2.2.2 snj }
3240 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
3241 1.2.2.2 snj }
3242 1.2.2.2 snj
3243 1.2.2.2 snj struct evbuffer_cb_entry *
3244 1.2.2.2 snj evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg)
3245 1.2.2.2 snj {
3246 1.2.2.2 snj struct evbuffer_cb_entry *e;
3247 1.2.2.2 snj if (! (e = mm_calloc(1, sizeof(struct evbuffer_cb_entry))))
3248 1.2.2.2 snj return NULL;
3249 1.2.2.2 snj EVBUFFER_LOCK(buffer);
3250 1.2.2.2 snj e->cb.cb_func = cb;
3251 1.2.2.2 snj e->cbarg = cbarg;
3252 1.2.2.2 snj e->flags = EVBUFFER_CB_ENABLED;
3253 1.2.2.2 snj LIST_INSERT_HEAD(&buffer->callbacks, e, next);
3254 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
3255 1.2.2.2 snj return e;
3256 1.2.2.2 snj }
3257 1.2.2.2 snj
3258 1.2.2.2 snj int
3259 1.2.2.2 snj evbuffer_remove_cb_entry(struct evbuffer *buffer,
3260 1.2.2.2 snj struct evbuffer_cb_entry *ent)
3261 1.2.2.2 snj {
3262 1.2.2.2 snj EVBUFFER_LOCK(buffer);
3263 1.2.2.2 snj LIST_REMOVE(ent, next);
3264 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
3265 1.2.2.2 snj mm_free(ent);
3266 1.2.2.2 snj return 0;
3267 1.2.2.2 snj }
3268 1.2.2.2 snj
3269 1.2.2.2 snj int
3270 1.2.2.2 snj evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg)
3271 1.2.2.2 snj {
3272 1.2.2.2 snj struct evbuffer_cb_entry *cbent;
3273 1.2.2.2 snj int result = -1;
3274 1.2.2.2 snj EVBUFFER_LOCK(buffer);
3275 1.2.2.2 snj LIST_FOREACH(cbent, &buffer->callbacks, next) {
3276 1.2.2.2 snj if (cb == cbent->cb.cb_func && cbarg == cbent->cbarg) {
3277 1.2.2.2 snj result = evbuffer_remove_cb_entry(buffer, cbent);
3278 1.2.2.2 snj goto done;
3279 1.2.2.2 snj }
3280 1.2.2.2 snj }
3281 1.2.2.2 snj done:
3282 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
3283 1.2.2.2 snj return result;
3284 1.2.2.2 snj }
3285 1.2.2.2 snj
3286 1.2.2.2 snj int
3287 1.2.2.2 snj evbuffer_cb_set_flags(struct evbuffer *buffer,
3288 1.2.2.2 snj struct evbuffer_cb_entry *cb, ev_uint32_t flags)
3289 1.2.2.2 snj {
3290 1.2.2.2 snj /* the user isn't allowed to mess with these. */
3291 1.2.2.2 snj flags &= ~EVBUFFER_CB_INTERNAL_FLAGS;
3292 1.2.2.2 snj EVBUFFER_LOCK(buffer);
3293 1.2.2.2 snj cb->flags |= flags;
3294 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
3295 1.2.2.2 snj return 0;
3296 1.2.2.2 snj }
3297 1.2.2.2 snj
3298 1.2.2.2 snj int
3299 1.2.2.2 snj evbuffer_cb_clear_flags(struct evbuffer *buffer,
3300 1.2.2.2 snj struct evbuffer_cb_entry *cb, ev_uint32_t flags)
3301 1.2.2.2 snj {
3302 1.2.2.2 snj /* the user isn't allowed to mess with these. */
3303 1.2.2.2 snj flags &= ~EVBUFFER_CB_INTERNAL_FLAGS;
3304 1.2.2.2 snj EVBUFFER_LOCK(buffer);
3305 1.2.2.2 snj cb->flags &= ~flags;
3306 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
3307 1.2.2.2 snj return 0;
3308 1.2.2.2 snj }
3309 1.2.2.2 snj
3310 1.2.2.2 snj int
3311 1.2.2.2 snj evbuffer_freeze(struct evbuffer *buffer, int start)
3312 1.2.2.2 snj {
3313 1.2.2.2 snj EVBUFFER_LOCK(buffer);
3314 1.2.2.2 snj if (start)
3315 1.2.2.2 snj buffer->freeze_start = 1;
3316 1.2.2.2 snj else
3317 1.2.2.2 snj buffer->freeze_end = 1;
3318 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
3319 1.2.2.2 snj return 0;
3320 1.2.2.2 snj }
3321 1.2.2.2 snj
3322 1.2.2.2 snj int
3323 1.2.2.2 snj evbuffer_unfreeze(struct evbuffer *buffer, int start)
3324 1.2.2.2 snj {
3325 1.2.2.2 snj EVBUFFER_LOCK(buffer);
3326 1.2.2.2 snj if (start)
3327 1.2.2.2 snj buffer->freeze_start = 0;
3328 1.2.2.2 snj else
3329 1.2.2.2 snj buffer->freeze_end = 0;
3330 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
3331 1.2.2.2 snj return 0;
3332 1.2.2.2 snj }
3333 1.2.2.2 snj
3334 1.2.2.2 snj #if 0
3335 1.2.2.2 snj void
3336 1.2.2.2 snj evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb)
3337 1.2.2.2 snj {
3338 1.2.2.2 snj if (!(cb->flags & EVBUFFER_CB_SUSPENDED)) {
3339 1.2.2.2 snj cb->size_before_suspend = evbuffer_get_length(buffer);
3340 1.2.2.2 snj cb->flags |= EVBUFFER_CB_SUSPENDED;
3341 1.2.2.2 snj }
3342 1.2.2.2 snj }
3343 1.2.2.2 snj
3344 1.2.2.2 snj void
3345 1.2.2.2 snj evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb)
3346 1.2.2.2 snj {
3347 1.2.2.2 snj if ((cb->flags & EVBUFFER_CB_SUSPENDED)) {
3348 1.2.2.2 snj unsigned call = (cb->flags & EVBUFFER_CB_CALL_ON_UNSUSPEND);
3349 1.2.2.2 snj size_t sz = cb->size_before_suspend;
3350 1.2.2.2 snj cb->flags &= ~(EVBUFFER_CB_SUSPENDED|
3351 1.2.2.2 snj EVBUFFER_CB_CALL_ON_UNSUSPEND);
3352 1.2.2.2 snj cb->size_before_suspend = 0;
3353 1.2.2.2 snj if (call && (cb->flags & EVBUFFER_CB_ENABLED)) {
3354 1.2.2.2 snj cb->cb(buffer, sz, evbuffer_get_length(buffer), cb->cbarg);
3355 1.2.2.2 snj }
3356 1.2.2.2 snj }
3357 1.2.2.2 snj }
3358 1.2.2.2 snj #endif
3359 1.2.2.2 snj
3360 1.2.2.2 snj int
3361 1.2.2.2 snj evbuffer_get_callbacks_(struct evbuffer *buffer, struct event_callback **cbs,
3362 1.2.2.2 snj int max_cbs)
3363 1.2.2.2 snj {
3364 1.2.2.2 snj int r = 0;
3365 1.2.2.2 snj EVBUFFER_LOCK(buffer);
3366 1.2.2.2 snj if (buffer->deferred_cbs) {
3367 1.2.2.2 snj if (max_cbs < 1) {
3368 1.2.2.2 snj r = -1;
3369 1.2.2.2 snj goto done;
3370 1.2.2.2 snj }
3371 1.2.2.2 snj cbs[0] = &buffer->deferred;
3372 1.2.2.2 snj r = 1;
3373 1.2.2.2 snj }
3374 1.2.2.2 snj done:
3375 1.2.2.2 snj EVBUFFER_UNLOCK(buffer);
3376 1.2.2.2 snj return r;
3377 1.2.2.2 snj }
3378