19097c95bSNick MathewsonWhat's New In Libevent 2.0 so far: 29097c95bSNick Mathewson 34991669bSNick Mathewson1. Meta-issues 44991669bSNick Mathewson 54991669bSNick Mathewson1.1. About this document 69097c95bSNick Mathewson 79097c95bSNick Mathewson This document describes the key differences between Libevent 1.4 and 89097c95bSNick Mathewson Libevent 2.0, from a user's point of view. It was most recently 94991669bSNick Mathewson updated based on features in git master as of August 2010. 109097c95bSNick Mathewson 1174f75758SNick Mathewson NOTE: I am very sure that I missed some thing on this list. Caveat 124991669bSNick Mathewson haxxor. 134991669bSNick Mathewson 144991669bSNick Mathewson1.2. Better documentation 154991669bSNick Mathewson 164991669bSNick Mathewson There is now a book-in-progress that explains how to use Libevent and its 174991669bSNick Mathewson growing pile of APIs. As of this writing, it covers everything except the 184991669bSNick Mathewson http and rpc code. Check out the latest draft at 194991669bSNick Mathewson http://www.wangafu.net/~nickm/libevent-book/ . 201351e61cSNick Mathewson 211351e61cSNick Mathewson2. New and Improved Event APIs 229097c95bSNick Mathewson 239097c95bSNick Mathewson Many APIs are improved, refactored, or deprecated in Libevent 2.0. 249097c95bSNick Mathewson 251351e61cSNick Mathewson COMPATIBILITY: 261351e61cSNick Mathewson 27674bc6a2SMansour Moufid Nearly all existing code that worked with Libevent 1.4 should still 281351e61cSNick Mathewson work correctly with Libevent 2.0. However, if you are writing new code, 291351e61cSNick Mathewson or if you want to port old code, we strongly recommend using the new APIs 301351e61cSNick Mathewson and avoiding deprecated APIs as much as possible. 311351e61cSNick Mathewson 321351e61cSNick Mathewson Binaries linked against Libevent 1.4 will need to be recompiled to link 331351e61cSNick Mathewson against Libevent 2.0. This is nothing new; we have never been good at 341351e61cSNick Mathewson preserving binary compatibility between releases. We'll try harder in the 351351e61cSNick Mathewson future, though: see 2.1 below. 369097c95bSNick Mathewson 374991669bSNick Mathewson2.1. New header layout for improved forward-compatibility 389097c95bSNick Mathewson 399097c95bSNick Mathewson Libevent 2.0 has a new header layout to make it easier for programmers to 409097c95bSNick Mathewson write good, well-supported libevent code. The new headers are divided 419097c95bSNick Mathewson into three types. 429097c95bSNick Mathewson 439097c95bSNick Mathewson There are *regular headers*, like event2/event.h. These headers contain 449097c95bSNick Mathewson the functions that most programmers will want to use. 459097c95bSNick Mathewson 469097c95bSNick Mathewson There are *backward compatibility headers*, like event2/event_compat.h. 479097c95bSNick Mathewson These headers contain declarations for deprecated functions from older 481351e61cSNick Mathewson versions of Libevent. Documentation in these headers should suggest what's 491351e61cSNick Mathewson wrong with the old functions, and what functions you want to start using 501351e61cSNick Mathewson instead of the old ones. Some of these functions might be removed in a 511351e61cSNick Mathewson future release. New programs should generally not include these headers. 529097c95bSNick Mathewson 539097c95bSNick Mathewson Finally, there are *structure headers*, like event2/event_struct.h. 549097c95bSNick Mathewson These headers contain definitions of some structures that Libevent has 5574f75758SNick Mathewson historically exposed. Exposing them caused problems in the past, 5674f75758SNick Mathewson since programs that were compiled to work with one version of Libevent 5774f75758SNick Mathewson would often stop working with another version that changed the size or 5874f75758SNick Mathewson layout of some object. We've moving them into separate headers so 5974f75758SNick Mathewson that programmers can know that their code is not depending on any 6074f75758SNick Mathewson unstable aspect of the Libvent ABI. New programs should generally not 6174f75758SNick Mathewson include these headers unless they really know what they are doing, are 6274f75758SNick Mathewson willing to rebuild their software whenever they want to link it 6374f75758SNick Mathewson against a new version of Libevent, and are willing to risk their code 6474f75758SNick Mathewson breaking if and when data structures change. 659097c95bSNick Mathewson 669097c95bSNick Mathewson Functionality that once was located in event.h is now more subdivided. 679097c95bSNick Mathewson The core event logic is now in event2/event.h. The "evbuffer" functions 689097c95bSNick Mathewson for low-level buffer manipulation are in event2/buffer.h. The 699097c95bSNick Mathewson "bufferevent" functions for higher-level buffered IO are in 709097c95bSNick Mathewson event2/bufferevent.h. 719097c95bSNick Mathewson 721351e61cSNick Mathewson COMPATIBILITY: 731351e61cSNick Mathewson 749097c95bSNick Mathewson All of the old headers (event.h, evdns.h, evhttp.h, evrpc.h, and 759097c95bSNick Mathewson evutil.h) will continue to work by including the corresponding new 769097c95bSNick Mathewson headers. Old code should not be broken by this change. 779097c95bSNick Mathewson 78deb2f738SNick Mathewson2.2. New thread-safe, binary-compatible, harder-to-mess-up APIs 799097c95bSNick Mathewson 809097c95bSNick Mathewson Some aspects of the historical Libevent API have encouraged 819097c95bSNick Mathewson non-threadsafe code, or forced code built against one version of Libevent 829097c95bSNick Mathewson to no longer build with another. The problems with now-deprecated APIs 839097c95bSNick Mathewson fell into two categories: 849097c95bSNick Mathewson 859097c95bSNick Mathewson 1) Dependence on the "current" event_base. In an application with 869097c95bSNick Mathewson multiple event_bases, Libevent previously had a notion of the 871351e61cSNick Mathewson "current" event_base. New events were linked to this base, and 889097c95bSNick Mathewson the caller needed to explicitly reattach them to another base. 899097c95bSNick Mathewson This was horribly error-prone. 909097c95bSNick Mathewson 911351e61cSNick Mathewson Functions like "event_set" that worked with the "current" event_base 921351e61cSNick Mathewson are now deprecated but still available (see 2.1). There are new 931351e61cSNick Mathewson functions like "event_assign" that take an explicit event_base 941351e61cSNick Mathewson argument when setting up a structure. Using these functions will help 951351e61cSNick Mathewson prevent errors in your applications, and to be more threadsafe. 969097c95bSNick Mathewson 979097c95bSNick Mathewson 2) Structure dependence. Applications needed to allocate 'struct 989097c95bSNick Mathewson event' themselves, since there was no function in Libevent to do it 999097c95bSNick Mathewson for them. But since the size and contents of struct event can 1009097c95bSNick Mathewson change between libevent versions, this created binary-compatibility 1019097c95bSNick Mathewson nightmares. All structures of this kind are now isolated in 1029097c95bSNick Mathewson _struct.h header (see 2.1), and there are new allocate-and- 1039097c95bSNick Mathewson initialize functions you can use instead of the old initialize-only 1049097c95bSNick Mathewson functions. For example, instead of malloc and event_set, you 1059097c95bSNick Mathewson can use event_new(). 1069097c95bSNick Mathewson 1071351e61cSNick Mathewson (For people who do really want to allocate a struct event on the 1081351e61cSNick Mathewson stack, or put one inside another structure, you can still use 1091351e61cSNick Mathewson event2/event_compat.h.) 1109097c95bSNick Mathewson 1119097c95bSNick Mathewson So in the case where old code would look like this: 1129097c95bSNick Mathewson 1139097c95bSNick Mathewson #include <event.h> 1149097c95bSNick Mathewson ... 1159097c95bSNick Mathewson struct event *ev = malloc(sizeof(struct event)); 11674f75758SNick Mathewson /* This call will cause a buffer overrun if you compile with one version 11774f75758SNick Mathewson of Libevent and link dynamically against another. */ 1189097c95bSNick Mathewson event_set(ev, fd, EV_READ, cb, NULL); 1199097c95bSNick Mathewson /* If you forget this call, your code will break in hard-to-diagnose 1209097c95bSNick Mathewson ways in the presence of multiple event bases. */ 1219097c95bSNick Mathewson event_set_base(ev, base); 1229097c95bSNick Mathewson 1239097c95bSNick Mathewson New code will look more like this: 1249097c95bSNick Mathewson 1259097c95bSNick Mathewson #include <event2/event.h> 1269097c95bSNick Mathewson ... 1279097c95bSNick Mathewson struct event *ev; 1289097c95bSNick Mathewson ev = event_new(base, fd, EV_READ, cb, NULL); 1299097c95bSNick Mathewson 1309097c95bSNick Mathewson2.3. Overrideable allocation functions 1319097c95bSNick Mathewson 1329097c95bSNick Mathewson If you want to override the allocation functions used by libevent 1339097c95bSNick Mathewson (for example, to use a specialized allocator, or debug memory 1349097c95bSNick Mathewson issues, or so on), you can replace them by calling 1359097c95bSNick Mathewson event_set_mem_functions. It takes replacements for malloc(), 1369097c95bSNick Mathewson free(), and realloc(). 1379097c95bSNick Mathewson 1381351e61cSNick Mathewson If you're going to use this facility, you need to call it _before_ 1391351e61cSNick Mathewson Libevent does any memory allocation; otherwise, Libevent may allocate some 1401351e61cSNick Mathewson memory with malloc(), and free it with the free() function you provide. 1411351e61cSNick Mathewson 1421351e61cSNick Mathewson You can disable this feature when you are building Libevent by passing 1431351e61cSNick Mathewson the --disable-malloc-replacement argument to configure. 1441351e61cSNick Mathewson 1451351e61cSNick Mathewson2.4. Configurable event_base creation 1469097c95bSNick Mathewson 1479097c95bSNick Mathewson Older versions of Libevent would always got the fastest backend 1489097c95bSNick Mathewson available, unless you reconfigured their behavior with the environment 1499097c95bSNick Mathewson variables EVENT_NOSELECT, EVENT_NOPOLL, and so forth. This was annoying 1509097c95bSNick Mathewson to programmers who wanted to pick a backend explicitly without messing 1519097c95bSNick Mathewson with the environment. 1529097c95bSNick Mathewson 1539097c95bSNick Mathewson Also, despite our best efforts, not every backend supports every 1549097c95bSNick Mathewson operation we might like. Some features (like edge-triggered events, or 1559097c95bSNick Mathewson working with non-socket file descriptors) only work with some operating 1569097c95bSNick Mathewson systems' fast backends. Previously, programmers who cared about this 1579097c95bSNick Mathewson needed to know which backends supported what. This tended to get quite 1589097c95bSNick Mathewson ungainly. 1599097c95bSNick Mathewson 1609097c95bSNick Mathewson There is now an API to choose backends, either by name or by feature. 1619097c95bSNick Mathewson Here is an example: 1629097c95bSNick Mathewson 1639097c95bSNick Mathewson struct event_config_t *config; 1649097c95bSNick Mathewson struct event_base *base; 1659097c95bSNick Mathewson 1669097c95bSNick Mathewson /* Create a new configuration object. */ 1679097c95bSNick Mathewson config = event_config_new(); 1689097c95bSNick Mathewson /* We don't want to use the "select" method. */ 1699097c95bSNick Mathewson event_config_avoid_method(config, "select"); 1709097c95bSNick Mathewson /* We want a method that can work with non-socket file descriptors */ 1719097c95bSNick Mathewson event_config_require_features(config, EV_FEATURE_FDS); 1729097c95bSNick Mathewson 1739097c95bSNick Mathewson base = event_base_new_with_config(config); 1749097c95bSNick Mathewson if (!base) { 1759097c95bSNick Mathewson /* There is no backend method that does what we want. */ 1769097c95bSNick Mathewson exit(1); 1779097c95bSNick Mathewson } 1789097c95bSNick Mathewson event_config_free(config); 1799097c95bSNick Mathewson 1801351e61cSNick Mathewson Supported features are documented in event2/event.h 1819097c95bSNick Mathewson 1829097c95bSNick Mathewson2.5. Socket is now an abstract type 1839097c95bSNick Mathewson 1849097c95bSNick Mathewson All APIs that formerly accepted int as a socket type now accept 1859097c95bSNick Mathewson "evutil_socket_t". On Unix, this is just an alias for "int" as 1869097c95bSNick Mathewson before. On Windows, however, it's an alias for SOCKET, which can 1879097c95bSNick Mathewson be wider than int on 64-bit platforms. 1889097c95bSNick Mathewson 1899097c95bSNick Mathewson2.6. Timeouts and persistent events work together. 1909097c95bSNick Mathewson 1919097c95bSNick Mathewson Previously, it wasn't useful to set a timeout on a persistent event: 1929097c95bSNick Mathewson the timeout would trigger once, and never again. This is not what 1939097c95bSNick Mathewson applications tend to want. Instead, applications tend to want every 1949097c95bSNick Mathewson triggering of the event to re-set the timeout. So now, if you set 1959097c95bSNick Mathewson up an event like this: 1969097c95bSNick Mathewson struct event *ev; 1979097c95bSNick Mathewson struct timeval tv; 1989097c95bSNick Mathewson ev = event_new(base, fd, EV_READ|EV_PERSIST, cb, NULL); 1999097c95bSNick Mathewson tv.tv_sec = 1; 2009097c95bSNick Mathewson tv.tv_usec = 0; 2019097c95bSNick Mathewson event_add(ev, &tv); 2029097c95bSNick Mathewson 2039097c95bSNick Mathewson The callback 'cb' will be invoked whenever fd is ready to read, OR whenever 2049097c95bSNick Mathewson a second has passed since the last invocation of cb. 2059097c95bSNick Mathewson 2061351e61cSNick Mathewson2.7. Multiple events allowed per fd 2079097c95bSNick Mathewson 2089097c95bSNick Mathewson Older versions of Libevent allowed at most one EV_READ event and at most 2099097c95bSNick Mathewson one EV_WRITE event per socket, per event base. This restriction is no 2109097c95bSNick Mathewson longer present. 2119097c95bSNick Mathewson 2121351e61cSNick Mathewson2.8. evthread_* functions for thread-safe structures. 2139097c95bSNick Mathewson 21418adc3f0SNick Mathewson Libevent structures can now be built with locking support. This code 2154991669bSNick Mathewson makes it safe to add, remove, and activate events on an event base from a 2164991669bSNick Mathewson different thread. (Previously, if you wanted to write multithreaded code 2174991669bSNick Mathewson with Libevent, you could only an event_base or its events in one thread at 2184991669bSNick Mathewson a time.) 2199097c95bSNick Mathewson 2209097c95bSNick Mathewson If you want threading support and you're using pthreads, you can just 2219097c95bSNick Mathewson call evthread_use_pthreads(). (You'll need to link against the 2221351e61cSNick Mathewson libevent_pthreads library in addition to libevent_core. These functions are 223bdfe72f3SNick Mathewson not in libevent_core.) 2249097c95bSNick Mathewson 2259097c95bSNick Mathewson If you want threading support and you're using Windows, you can just 2269097c95bSNick Mathewson call evthread_use_windows_threads(). 2279097c95bSNick Mathewson 22818adc3f0SNick Mathewson If you are using some locking system besides Windows and pthreads, You 22918adc3f0SNick Mathewson can enable this on a per-event-base level by writing functions to 23018adc3f0SNick Mathewson implement mutexes, conditions, and thread IDs, and passing them to 23118adc3f0SNick Mathewson evthread_set_lock_callbacks and related functions in event2/thread.h. 23218adc3f0SNick Mathewson 2331351e61cSNick Mathewson Once locking functions are enabled, every new event_base is created with a 2341351e61cSNick Mathewson lock. You can prevent a single event_base from being built with a lock 2351351e61cSNick Mathewson disabled by using the EVENT_BASE_FLAG_NOLOCK flag in its 2361351e61cSNick Mathewson event_config. If an event_base is created with a lock, it is safe to call 2371351e61cSNick Mathewson event_del, event_add, and event_active on its events from any thread. The 2381351e61cSNick Mathewson event callbacks themselves are still all executed from the thread running 2391351e61cSNick Mathewson the event loop. 2409097c95bSNick Mathewson 2411351e61cSNick Mathewson To make an evbuffer or a bufferevent object threadsafe, call its 24218adc3f0SNick Mathewson *_enable_locking() function. 2439097c95bSNick Mathewson 244b7907a7bSNick Mathewson The HTTP api is not currently threadsafe. 2459097c95bSNick Mathewson 2461351e61cSNick Mathewson To build Libevent with threading support disabled, pass 2471351e61cSNick Mathewson --disable-thread-support to the configure script. 2489097c95bSNick Mathewson 2491351e61cSNick Mathewson2.9. Edge-triggered events on some backends. 2509097c95bSNick Mathewson 2511351e61cSNick Mathewson With some backends, it's now possible to add the EV_ET flag to an event 2521351e61cSNick Mathewson in order to request that the event's semantics be edge-triggered. Right 2531351e61cSNick Mathewson now, epoll and kqueue support this. 2549097c95bSNick Mathewson 2551351e61cSNick Mathewson The corresponding event_config feature is EV_FEATURE_ET; see 2.4 for more 2561351e61cSNick Mathewson information. 2579097c95bSNick Mathewson 2584991669bSNick Mathewson2.10. Better support for huge numbers of timeouts 2599097c95bSNick Mathewson 2604991669bSNick Mathewson The heap-based priority queue timer implementation for Libevent 1.4 is good 2614991669bSNick Mathewson for randomly distributed timeouts, but suboptimal if you have huge numbers 2624991669bSNick Mathewson of timeouts that all expire in the same amount of time after their 2634991669bSNick Mathewson creation. The new event_base_init_common_timeout() logic lets you signal 2644991669bSNick Mathewson that a given timeout interval will be very common, and should use a linked 2654991669bSNick Mathewson list implementation instead of a priority queue. 2669097c95bSNick Mathewson 2674991669bSNick Mathewson2.11. Improved debugging support 2684991669bSNick Mathewson 2694991669bSNick Mathewson It's been pretty easy to forget to delete all your events before you 2704991669bSNick Mathewson re-initialize them, or otherwise put Libevent in an internally inconsistent 2714991669bSNick Mathewson state. You can tell libevent to catch these and other common errors with 2724991669bSNick Mathewson the new event_enable_debug_mode() call. Just invoke it before you do 2734991669bSNick Mathewson any calls to other libevent functions, and it'll catch many common 2744991669bSNick Mathewson event-level errors in your code. 2754991669bSNick Mathewson 2764991669bSNick Mathewson2.12. Functions to access all event fields 2774991669bSNick Mathewson 2784991669bSNick Mathewson So that you don't have to access the struct event fields directly, Libevent 2794991669bSNick Mathewson now provides accessor functions to retrieve everything from an event that 2804991669bSNick Mathewson you set during event_new() or event_assign(). 2814991669bSNick Mathewson 2824991669bSNick Mathewson3. Backend-specific and performance improvements. 2834991669bSNick Mathewson 2844991669bSNick Mathewson3.1. Change-minimization on O(1) backends 2854991669bSNick Mathewson 2864991669bSNick Mathewson With previous versions of Libevent, if you called event_del() and 2874991669bSNick Mathewson event_add() repeatedly on a single event between trips to the backend's 2884991669bSNick Mathewson dispatch function, the backend might wind up making unnecessary calls or 2894991669bSNick Mathewson passing unnecessary data to the kernel. The new backend logic batches up 2904991669bSNick Mathewson redundant adds and deletes, and performs no more operations than necessary 2914991669bSNick Mathewson at the kernel level. 2929097c95bSNick Mathewson 29318adc3f0SNick Mathewson This logic is on for the kqueue backend, and available (but off by 29418adc3f0SNick Mathewson default) for the epoll backend. To turn it on for the epoll backend, 29518adc3f0SNick Mathewson set the EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST flag in the 29618adc3f0SNick Mathewson event_base_cofig, or set the EVENT_EPOLL_USE_CHANGELIST environment 29718adc3f0SNick Mathewson variable. Doing this with epoll may result in weird bugs if you give 29818adc3f0SNick Mathewson any fds closed by dup() or its variants. 29918adc3f0SNick Mathewson 3001351e61cSNick Mathewson3.2. Improved notification on Linux 3019097c95bSNick Mathewson 3021351e61cSNick Mathewson When we need to wake the event loop up from another thread, we use 3031351e61cSNick Mathewson an epollfd to do so, instead of a socketpair. This is supposed to be 3041351e61cSNick Mathewson faster. 3059097c95bSNick Mathewson 3064991669bSNick Mathewson3.3. Windows: better support for everything 3074991669bSNick Mathewson 30818adc3f0SNick Mathewson Bufferevents on Windows can use a new mechanism (off-by-default; see below) 30918adc3f0SNick Mathewson to send their data via Windows overlapped IO and get their notifications 3104991669bSNick Mathewson via the IOCP API. This should be much faster than using event-based 3114991669bSNick Mathewson notification. 3124991669bSNick Mathewson 3134991669bSNick Mathewson Other functions throughout the code have been fixed to work more 3144991669bSNick Mathewson consistently with Windows. Libevent now builds on Windows using either 3154991669bSNick Mathewson mingw, or using MSVC (with nmake). Libevent works fine with UNICODE 3164991669bSNick Mathewson defined, or not. 3174991669bSNick Mathewson 3184991669bSNick Mathewson Data structures are a little smarter: our lookups from socket to pending 3194991669bSNick Mathewson event are now done with O(1) hash tables rather than O(lg n) red-black 3204991669bSNick Mathewson trees. 3214991669bSNick Mathewson 32218adc3f0SNick Mathewson Unfortunately, the main Windows backend is still select()-based: from 3234991669bSNick Mathewson testing the IOCP backends on the mailing list, it seems that there isn't 32474f75758SNick Mathewson actually a way to tell for certain whether a socket is writable with IOCP. 32574f75758SNick Mathewson Libevent 2.1 may add a multithreaded WaitForMultipleEvents-based 3264991669bSNick Mathewson backend for better performance with many inactive sockets and better 3274991669bSNick Mathewson integration with Windows events. 3284991669bSNick Mathewson 3291351e61cSNick Mathewson4. Improvements to evbuffers 3309097c95bSNick Mathewson 3311351e61cSNick Mathewson Libevent has long had an "evbuffer" implementation to wrap access to an 3321351e61cSNick Mathewson input or output memory buffer. In previous versions, the implementation 3331351e61cSNick Mathewson was very inefficient and lacked some desirable features. We've made many 3341351e61cSNick Mathewson improvements in Libevent 2.0. 3351351e61cSNick Mathewson 3361351e61cSNick Mathewson4.1. Chunked-memory internal representation 3371351e61cSNick Mathewson 3381351e61cSNick Mathewson Previously, each evbuffer was a huge chunk of memory. When we ran out of 3391351e61cSNick Mathewson space in an evbuffer, we used realloc() to grow the chunk of memory. When 3401351e61cSNick Mathewson data was misaligned, we used memmove to move the data back to the front 3411351e61cSNick Mathewson of the buffer. 3421351e61cSNick Mathewson 3431351e61cSNick Mathewson Needless to say, this is a terrible interface for networked IO. 3441351e61cSNick Mathewson 3451351e61cSNick Mathewson Now, evbuffers are implemented as a linked list of memory chunks, like 34618adc3f0SNick Mathewson most Unix kernels use for network IO. (See Linux's skbuf interfaces, 34718adc3f0SNick Mathewson or *BSD's mbufs). Data is added at the end of the linked list and 34818adc3f0SNick Mathewson removed from the front, so that we don't ever need realloc huge chunks 34918adc3f0SNick Mathewson or memmove the whole buffer contents. 3501351e61cSNick Mathewson 35118adc3f0SNick Mathewson To avoid excessive calls to read and write, we use the readv/writev 3521351e61cSNick Mathewson interfaces (or WSASend/WSARecv on Windows) to do IO on multiple chunks at 3531351e61cSNick Mathewson once with a single system call. 3541351e61cSNick Mathewson 3551351e61cSNick Mathewson COMPATIBILITY NOTE: 3561351e61cSNick Mathewson The evbuffer struct is no longer exposed in a header. The code here is 3571351e61cSNick Mathewson too volatile to expose an official evbuffer structure, and there was never 3581351e61cSNick Mathewson any means provided to create an evbuffer except via evbuffer_new which 3591351e61cSNick Mathewson heap-allocated the buffer. 3601351e61cSNick Mathewson 361*a452811eSPatrick Pelletier If you need access to the whole buffer as a linear chunk of memory, the 3621351e61cSNick Mathewson EVBUFFER_DATA() function still works. Watch out, though: it needs to copy 3631351e61cSNick Mathewson the buffer's contents in a linear chunk before you can use it. 3641351e61cSNick Mathewson 3651351e61cSNick Mathewson4.2. More flexible readline support 3661351e61cSNick Mathewson 3671351e61cSNick Mathewson The old evbuffer_readline() function (which accepted any sequence of 3681351e61cSNick Mathewson CR and LF characters as a newline, and which couldn't handle lines 3691351e61cSNick Mathewson containing NUL characters), is now deprecated. The preferred 3701351e61cSNick Mathewson function is evbuffer_readln(), which supports a variety of 3711351e61cSNick Mathewson line-ending styles, and which can return the number of characters in 3721351e61cSNick Mathewson the line returned. 3731351e61cSNick Mathewson 374bdfe72f3SNick Mathewson You can also call evbuffer_search_eol() to find the end of a line 375bdfe72f3SNick Mathewson in an evbuffer without ever extracting the line. 376bdfe72f3SNick Mathewson 3771351e61cSNick Mathewson4.3. Support for file-based IO in evbuffers. 3781351e61cSNick Mathewson 3791351e61cSNick Mathewson You can now add chunks of a file into a evbuffer, and Libevent will have 3801351e61cSNick Mathewson your OS use mapped-memory functionality, sendfile, or splice to transfer 3811351e61cSNick Mathewson the data without ever copying it to userspace. On OSs where this is not 3821351e61cSNick Mathewson supported, Libevent just loads the data. 3831351e61cSNick Mathewson 3841351e61cSNick Mathewson There are probably some bugs remaining in this code. On some platforms 3851351e61cSNick Mathewson (like Windows), it just reads the relevant parts of the file into RAM. 3861351e61cSNick Mathewson 3874991669bSNick Mathewson4.4. Support for zero-copy ("scatter/gather") writes in evbuffers. 3881351e61cSNick Mathewson 389bdfe72f3SNick Mathewson You can add a piece of memory to an evbuffer without copying it. 390bdfe72f3SNick Mathewson Instead, Libevent adds a new element to the evbuffer's linked list of 391bdfe72f3SNick Mathewson chunks with a pointer to the memory you supplied. You can do this 392bdfe72f3SNick Mathewson either with a reference-counted chunk (via evbuffer_add_reference), or 393bdfe72f3SNick Mathewson by asking Libevent for a pointer to its internal vectors (via 394bdfe72f3SNick Mathewson evbuffer_reserve_space or evbuffer_peek()). 3951351e61cSNick Mathewson 3961351e61cSNick Mathewson4.5. Multiple callbacks per evbuffer 3971351e61cSNick Mathewson 3981351e61cSNick Mathewson Previously, you could only have one callback active on an evbuffer at a 3991351e61cSNick Mathewson time. In practice, this meant that if one part of Libevent was using an 4001351e61cSNick Mathewson evbuffer callback to notice when an internal evbuffer was reading or 4011351e61cSNick Mathewson writing data, you couldn't have your own callback on that evbuffer. 4021351e61cSNick Mathewson 4031351e61cSNick Mathewson Now, you can now use the evbuffer_add_cb() function to add a callback that 4041351e61cSNick Mathewson does not interfere with any other callbacks. 4051351e61cSNick Mathewson 4061351e61cSNick Mathewson The evbuffer_setcb() function is now deprecated. 4071351e61cSNick Mathewson 4081351e61cSNick Mathewson4.6. New callback interface 4091351e61cSNick Mathewson 4101351e61cSNick Mathewson Previously, evbuffer callbacks were invoked with the old size of the 4111351e61cSNick Mathewson buffer and the new size of the buffer. This interface could not capture 4121351e61cSNick Mathewson operations that simultaneously filled _and_ drained a buffer, or handle 4131351e61cSNick Mathewson cases where we needed to postpone callbacks until multiple operations were 4141351e61cSNick Mathewson complete. 4151351e61cSNick Mathewson 4161351e61cSNick Mathewson Callbacks that are set with evbuffer_setcb still use the old API. 4171351e61cSNick Mathewson Callbacks added with evbuffer_add_cb() use a new interface that takes a 4181351e61cSNick Mathewson pointer to a struct holding the total number of bytes drained read and the 4191351e61cSNick Mathewson total number of bytes written. See event2/buffer.h for full details. 4201351e61cSNick Mathewson 4211351e61cSNick Mathewson4.7. Misc new evbuffer features 4221351e61cSNick Mathewson 4231351e61cSNick Mathewson You can use evbuffer_remove() to move a given number of bytes from one 4241351e61cSNick Mathewson buffer to another. 4251351e61cSNick Mathewson 4261351e61cSNick Mathewson The evbuffer_search() function lets you search for repeated instances of 4271351e61cSNick Mathewson a pattern inside an evbuffer. 4281351e61cSNick Mathewson 4291351e61cSNick Mathewson You can use evbuffer_freeze() to temporarily suspend drains from or adds 4301351e61cSNick Mathewson to a given evbuffer. This is useful for code that exposes an evbuffer as 4311351e61cSNick Mathewson part of its public API, but wants users to treat it as a pure source or 4321351e61cSNick Mathewson sink. 4331351e61cSNick Mathewson 4344991669bSNick Mathewson There's an evbuffer_copyout() that looks at the data at the start of an 4354991669bSNick Mathewson evbuffer without doing a drain. 4364991669bSNick Mathewson 4371351e61cSNick Mathewson You can have an evbuffer defer all of its callbacks, so that rather than 4381351e61cSNick Mathewson being invoked immediately when the evbuffer's length changes, they are 4391351e61cSNick Mathewson invoked from within the event_loop. This is useful when you have a 4401351e61cSNick Mathewson complex set of callbacks that can change the length of other evbuffers, 4411351e61cSNick Mathewson and you want to avoid having them recurse and overflow your stack. 4421351e61cSNick Mathewson 4431351e61cSNick Mathewson5. Bufferevents improvements 4441351e61cSNick Mathewson 4451351e61cSNick Mathewson Libevent has long included a "bufferevents" structure and related 4461351e61cSNick Mathewson functions that were useful for generic buffered IO on a TCP connection. 4471351e61cSNick Mathewson This is what Libevent uses for its HTTP implementation. In addition to 4481351e61cSNick Mathewson the improvements that they get for free from the underlying evbuffer 4491351e61cSNick Mathewson implementation above, there are many new features in Libevent 2.0's 4501351e61cSNick Mathewson evbuffers. 4511351e61cSNick Mathewson 4521351e61cSNick Mathewson5.1. New OO implementations 4531351e61cSNick Mathewson 4541351e61cSNick Mathewson The "bufferevent" structure is now an abstract base type with multiple 4551351e61cSNick Mathewson implementations. This should not break existing code, which always 4561351e61cSNick Mathewson allocated bufferevents with bufferevent_new(). 4571351e61cSNick Mathewson 4581351e61cSNick Mathewson Current implementations of the bufferevent interface are described below. 4591351e61cSNick Mathewson 4601351e61cSNick Mathewson5.2. bufferevent_socket_new() replaces bufferevent_new() 4611351e61cSNick Mathewson 4621351e61cSNick Mathewson Since bufferevents that use a socket are not the only kind, 4631351e61cSNick Mathewson bufferevent_new() is now deprecated. Use bufferevent_socket_new() 4641351e61cSNick Mathewson instead. 4651351e61cSNick Mathewson 4661351e61cSNick Mathewson5.3. Filtered bufferevent IO 4671351e61cSNick Mathewson 4681351e61cSNick Mathewson You can use bufferevent_filter_new() to create a bufferevent that wraps 4691351e61cSNick Mathewson around another bufferevent and transforms data it is sending and 4701351e61cSNick Mathewson receiving. See test/regress_zlib.c for a toy example that uses zlib to 4711351e61cSNick Mathewson compress data before sending it over a bufferevent. 4721351e61cSNick Mathewson 4731351e61cSNick Mathewson5.3. Linked pairs of bufferevents 4741351e61cSNick Mathewson 475bdfe72f3SNick Mathewson You can use bufferevent_pair_new() to produce two linked 476bdfe72f3SNick Mathewson bufferevents. This is like using socketpair, but doesn't require 477bdfe72f3SNick Mathewson system-calls. 4781351e61cSNick Mathewson 479bdfe72f3SNick Mathewson5.4. SSL support for bufferevents with OpenSSL 4801351e61cSNick Mathewson 481bdfe72f3SNick Mathewson There is now a bufferevent type that supports SSL/TLS using the 482bdfe72f3SNick Mathewson OpenSSL library. The code for this is build in a separate 483bdfe72f3SNick Mathewson library, libevent_openssl, so that your programs don't need to 484bdfe72f3SNick Mathewson link against OpenSSL unless they actually want SSL support. 4851351e61cSNick Mathewson 486bdfe72f3SNick Mathewson There are two ways to construct one of these bufferevents, both 487bdfe72f3SNick Mathewson declared in <event2/bufferevent_ssl.h>. If you want to wrap an 488bdfe72f3SNick Mathewson SSL layer around an existing bufferevent, you would call the 489bdfe72f3SNick Mathewson bufferevent_openssl_filter_new() function. If you want to do SSL 490bdfe72f3SNick Mathewson on a socket directly, call bufferevent_openssl_socket_new(). 4911351e61cSNick Mathewson 492bdfe72f3SNick Mathewson5.5. IOCP support for bufferevents on Windows 493bdfe72f3SNick Mathewson 494bdfe72f3SNick Mathewson There is now a bufferevents backend that supports IOCP on Windows. 495bdfe72f3SNick Mathewson Supposedly, this will eventually make Windows IO much faster for 496bdfe72f3SNick Mathewson programs using bufferevents. We'll have to see; the code is not 497bdfe72f3SNick Mathewson currently optimized at all. To try it out, call the 498bdfe72f3SNick Mathewson event_base_start_iocp() method on an event_base before contructing 4991351e61cSNick Mathewson bufferevents. 5001351e61cSNick Mathewson 501bdfe72f3SNick Mathewson This is tricky code; there are probably some bugs hiding here. 502bdfe72f3SNick Mathewson 503bdfe72f3SNick Mathewson5.6. Improved connect support for bufferevents. 504bdfe72f3SNick Mathewson 505bdfe72f3SNick Mathewson You can now create a bufferevent that is not yet connected to any 506bdfe72f3SNick Mathewson host, and tell it to connect, either by address or by hostname. 507bdfe72f3SNick Mathewson 508bdfe72f3SNick Mathewson The functions to do this are bufferevent_socket_connect and 509bdfe72f3SNick Mathewson bufferevent_socket_connect_hostname. 510bdfe72f3SNick Mathewson 5114991669bSNick Mathewson5.7. Rate-limiting for bufferevents 5124991669bSNick Mathewson 5134991669bSNick Mathewson If you need to limit the number of bytes read/written by a single 5144991669bSNick Mathewson bufferevent, or by a group of them, you can do this with a new set of 5154991669bSNick Mathewson bufferevent rate-limiting calls. 5164991669bSNick Mathewson 5174991669bSNick Mathewson6. Other improvements 5181351e61cSNick Mathewson 51918adc3f0SNick Mathewson6.1. DNS improvements 5201351e61cSNick Mathewson 52118adc3f0SNick Mathewson6.1.1. DNS: IPv6 nameservers 5221351e61cSNick Mathewson 5231351e61cSNick Mathewson The evdns code now lets you have nameservers whose addresses are IPv6. 5241351e61cSNick Mathewson 52518adc3f0SNick Mathewson6.1.2. DNS: Better security 5261351e61cSNick Mathewson 5274991669bSNick Mathewson Libevent 2.0 tries harder to resist DNS answer-sniping attacks than 5284991669bSNick Mathewson earlier versions of evdns. See comments in the code for full details. 5291351e61cSNick Mathewson 5304991669bSNick Mathewson Notably, evdns now supports the "0x20 hack" to make it harder to 5314991669bSNick Mathewson impersonate a DNS server. Additionally, Libevent now uses a strong 5324991669bSNick Mathewson internal RNG to generate DNS transaction IDs, so you don't need to supply 5334991669bSNick Mathewson your own. 5349097c95bSNick Mathewson 53518adc3f0SNick Mathewson6.1.3. DNS: Getaddrinfo support 536bdfe72f3SNick Mathewson 537bdfe72f3SNick Mathewson There's now an asynchronous getaddrinfo clone, evdns_getaddrinfo(), 538bdfe72f3SNick Mathewson to make the results of the evdns functions more usable. It doesn't 539bdfe72f3SNick Mathewson support every feature of a typical platform getaddrinfo() yet, but it 540bdfe72f3SNick Mathewson is quite close. 541bdfe72f3SNick Mathewson 542bdfe72f3SNick Mathewson There is also a blocking evutil_getaddrinfo() declared in 543bdfe72f3SNick Mathewson event2/util.h, to provide a getaddrinfo() implementation for 544bdfe72f3SNick Mathewson platforms that don't have one, and smooth over the differences in 545bdfe72f3SNick Mathewson various platforms implementations of RFC3493. 546bdfe72f3SNick Mathewson 5474991669bSNick Mathewson Bufferevents provide bufferevent_connect_hostname(), which combines 5484991669bSNick Mathewson the name lookup and connect operations. 5494991669bSNick Mathewson 55018adc3f0SNick Mathewson6.1.4. DNS: No more evdns globals 5514991669bSNick Mathewson 5524991669bSNick Mathewson Like an event base, evdns operations are now supposed to use an evdns_base 5534991669bSNick Mathewson argument. This makes them easier to wrap for other (more OO) languages, 5544991669bSNick Mathewson and easier to control the lifetime of. The old evdns functions will 5554991669bSNick Mathewson still, of course, continue working. 5564991669bSNick Mathewson 5574991669bSNick Mathewson6.2. Listener support 5584991669bSNick Mathewson 5594991669bSNick Mathewson You can now more easily automate setting up a bound socket to listen for 5604991669bSNick Mathewson TCP connections. Just use the evconnlistener_*() functions in the 5614991669bSNick Mathewson event2/listener.h header. 5624991669bSNick Mathewson 56318adc3f0SNick Mathewson The listener code supports IOCP on Windows if available. 5644991669bSNick Mathewson 5654991669bSNick Mathewson6.3. Secure RNG support 5664991669bSNick Mathewson 5674991669bSNick Mathewson Network code very frequently needs a secure, hard-to-predict random number 5684991669bSNick Mathewson generator. Some operating systems provide a good C implementation of one; 5694991669bSNick Mathewson others do not. Libevent 2.0 now provides a consistent implementation 5704991669bSNick Mathewson based on the arc4random code originally from OpenBSD. Libevent (and you) 5714991669bSNick Mathewson can use the evutil_secure_rng_*() functions to access a fairly secure 5724991669bSNick Mathewson random stream of bytes. 5734991669bSNick Mathewson 57418adc3f0SNick Mathewson6.4. HTTP 57518adc3f0SNick Mathewson 57618adc3f0SNick Mathewson The evhttp uriencoding and uridecoding APIs have updated versions 57718adc3f0SNick Mathewson that behave more correctly, and can handle strings with internal NULs. 57818adc3f0SNick Mathewson 57918adc3f0SNick Mathewson The evhttp query parsing and URI parsing logic can now detect errors 58018adc3f0SNick Mathewson more usefully. Moreover, we include an actual URI parsing function 58118adc3f0SNick Mathewson (evhttp_uri_parse()) to correctly parse URIs, so as to discourage 58218adc3f0SNick Mathewson people from rolling their own ad-hoc parsing functions. 58318adc3f0SNick Mathewson 58418adc3f0SNick Mathewson There are now accessor functions for the useful fields of struct http 58518adc3f0SNick Mathewson and friends; it shouldn't be necessary to access them directly any 58618adc3f0SNick Mathewson more. 58718adc3f0SNick Mathewson 58818adc3f0SNick Mathewson Libevent now lets you declare support for all specified HTTP methods, 58918adc3f0SNick Mathewson including OPTIONS, PATCH, and so on. The default list is unchanged. 59018adc3f0SNick Mathewson 59118adc3f0SNick Mathewson Numerous evhttp bugs also got fixed. 59218adc3f0SNick Mathewson 5932c4c294eSNick Mathewson7. Infrastructure improvements 5949097c95bSNick Mathewson 5952c4c294eSNick Mathewson7.1. Better unit test framework 5961351e61cSNick Mathewson 5972c4c294eSNick Mathewson We now use a unit test framework that Nick wrote called "tinytest". 5982c4c294eSNick Mathewson The main benefit from Libevent's point of view is that tests which 5992c4c294eSNick Mathewson might mess with global state can all run each in their own 6002c4c294eSNick Mathewson subprocess. This way, when there's a bug that makes one unit test 6012c4c294eSNick Mathewson crash or mess up global state, it doesn't affect any others. 6022c4c294eSNick Mathewson 6032c4c294eSNick Mathewson7.2. Better unit tests 6042c4c294eSNick Mathewson 605bdfe72f3SNick Mathewson Despite all the code we've added, our unit tests are much better than 606bdfe72f3SNick Mathewson before. Right now, iterating over the different backends on various 607bdfe72f3SNick Mathewson platforms, I'm getting between 78% and 81% test coverage, compared 608bdfe72f3SNick Mathewson with less than 45% test coverage in Libevent 1.4. 6092c4c294eSNick Mathewson 610