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