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