summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore28
-rw-r--r--.travis.yml16
-rw-r--r--CMakeLists.txt1154
-rw-r--r--ChangeLog1935
-rw-r--r--ChangeLog-1.4231
-rw-r--r--ChangeLog-2.01280
-rw-r--r--Doxyfile2
-rw-r--r--LICENSE25
-rw-r--r--Makefile.am133
-rw-r--r--Makefile.nmake47
-rw-r--r--README198
-rw-r--r--README.md318
-rw-r--r--WIN32-Code/getopt.c149
-rw-r--r--WIN32-Code/getopt.h33
-rw-r--r--WIN32-Code/getopt_long.c233
-rw-r--r--WIN32-Code/nmake/evconfig-private.h6
-rw-r--r--WIN32-Code/nmake/event2/event-config.h (renamed from WIN32-Code/event2/event-config.h)231
-rw-r--r--WIN32-Code/tree.h677
-rw-r--r--arc4random.c49
-rw-r--r--buffer.c1187
-rw-r--r--buffer_iocp.c39
-rw-r--r--bufferevent-internal.h122
-rw-r--r--bufferevent.c306
-rw-r--r--bufferevent_async.c166
-rw-r--r--bufferevent_filter.c138
-rw-r--r--bufferevent_openssl.c220
-rw-r--r--bufferevent_pair.c47
-rw-r--r--bufferevent_ratelim.c299
-rw-r--r--bufferevent_sock.c156
-rw-r--r--changelist-internal.h15
-rw-r--r--cmake/COPYING-CMAKE-SCRIPTS22
-rw-r--r--cmake/CheckFileOffsetBits.c14
-rw-r--r--cmake/CheckFileOffsetBits.cmake43
-rw-r--r--cmake/CheckFunctionExistsEx.c30
-rw-r--r--cmake/CheckFunctionExistsEx.cmake69
-rw-r--r--cmake/CheckPrototypeDefinition.c.in29
-rw-r--r--cmake/CheckPrototypeDefinition.cmake84
-rw-r--r--cmake/CheckWorkingKqueue.cmake52
-rw-r--r--cmake/CodeCoverage.cmake162
-rw-r--r--cmake/Copyright.txt57
-rw-r--r--cmake/LibeventConfig.cmake.in17
-rw-r--r--cmake/LibeventConfigVersion.cmake.in11
-rw-r--r--compat/sys/queue.h8
-rw-r--r--configure.ac187
-rw-r--r--defer-internal.h72
-rw-r--r--devpoll.c15
-rw-r--r--epoll.c394
-rw-r--r--epoll_sub.c1
-rw-r--r--epolltable-internal.h1166
-rw-r--r--evbuffer-internal.h124
-rw-r--r--evconfig-private.h.cmake35
-rw-r--r--evconfig-private.h.in48
-rw-r--r--evdns.c319
-rw-r--r--event-config.h.cmake499
-rw-r--r--event-internal.h198
-rw-r--r--event.c2358
-rw-r--r--event_iocp.c25
-rwxr-xr-xevent_rpcgen.py41
-rw-r--r--event_tagging.c22
-rw-r--r--evmap-internal.h55
-rw-r--r--evmap.c548
-rw-r--r--evport.c208
-rw-r--r--evrpc-internal.h21
-rw-r--r--evrpc.c63
-rw-r--r--evsignal-internal.h15
-rw-r--r--evthread-internal.h215
-rw-r--r--evthread.c187
-rw-r--r--evthread_pthread.c36
-rw-r--r--evthread_win32.c48
-rw-r--r--evutil.c1005
-rw-r--r--evutil_rand.c60
-rw-r--r--evutil_time.c488
-rw-r--r--ht-internal.h96
-rw-r--r--http-internal.h57
-rw-r--r--http.c786
-rw-r--r--include/Makefile.am47
-rw-r--r--include/evdns.h (renamed from evdns.h)6
-rw-r--r--include/event.h (renamed from event.h)14
-rw-r--r--include/event2/buffer.h266
-rw-r--r--include/event2/buffer_compat.h9
-rw-r--r--include/event2/bufferevent.h186
-rw-r--r--include/event2/bufferevent_compat.h4
-rw-r--r--include/event2/bufferevent_ssl.h37
-rw-r--r--include/event2/bufferevent_struct.h10
-rw-r--r--include/event2/dns.h70
-rw-r--r--include/event2/dns_compat.h12
-rw-r--r--include/event2/dns_struct.h10
-rw-r--r--include/event2/event.h537
-rw-r--r--include/event2/event_compat.h20
-rw-r--r--include/event2/event_struct.h97
-rw-r--r--include/event2/http.h319
-rw-r--r--include/event2/http_compat.h10
-rw-r--r--include/event2/http_struct.h32
-rw-r--r--include/event2/keyvalq_struct.h12
-rw-r--r--include/event2/listener.h41
-rw-r--r--include/event2/rpc.h16
-rw-r--r--include/event2/rpc_compat.h6
-rw-r--r--include/event2/rpc_struct.h6
-rw-r--r--include/event2/tag.h32
-rw-r--r--include/event2/tag_compat.h6
-rw-r--r--include/event2/thread.h31
-rw-r--r--include/event2/util.h212
-rw-r--r--include/event2/visibility.h55
-rw-r--r--include/evhttp.h (renamed from evhttp.h)6
-rw-r--r--include/evrpc.h (renamed from evrpc.h)6
-rw-r--r--include/evutil.h (renamed from evutil.h)6
-rw-r--r--include/include.am46
-rw-r--r--iocp-internal.h56
-rw-r--r--ipv6-internal.h16
-rw-r--r--kqueue-internal.h39
-rw-r--r--kqueue.c131
-rw-r--r--libevent_openssl.pc.in4
-rw-r--r--listener.c137
-rw-r--r--log-internal.h38
-rw-r--r--log.c67
-rw-r--r--m4/libevent_openssl.m447
-rw-r--r--m4/ntp_pkg_config.m427
-rw-r--r--make-event-config.sed10
-rwxr-xr-xmake_epoll_table.py63
-rw-r--r--minheap-internal.h92
-rw-r--r--mm-internal.h34
-rw-r--r--poll.c36
-rw-r--r--ratelim-internal.h12
-rw-r--r--sample/Makefile.am29
-rw-r--r--sample/dns-example.c10
-rw-r--r--sample/event-read-fifo.c (renamed from sample/event-test.c)83
-rw-r--r--sample/hello-world.c4
-rw-r--r--sample/hostcheck.c217
-rw-r--r--sample/hostcheck.h30
-rw-r--r--sample/http-server.c43
-rw-r--r--sample/https-client.c439
-rw-r--r--sample/include.am53
-rw-r--r--sample/le-proxy.c12
-rw-r--r--sample/openssl_hostname_validation.c172
-rw-r--r--sample/openssl_hostname_validation.h56
-rw-r--r--sample/signal-test.c19
-rw-r--r--sample/time-test.c8
-rw-r--r--select.c27
-rw-r--r--signal.c108
-rw-r--r--strlcpy-internal.h11
-rw-r--r--strlcpy.c9
-rw-r--r--test/Makefile.am105
-rw-r--r--test/Makefile.nmake34
-rw-r--r--test/bench.c31
-rw-r--r--test/bench_cascade.c31
-rw-r--r--test/bench_http.c14
-rw-r--r--test/bench_httpclient.c21
-rwxr-xr-xtest/check-dumpevents.py54
-rw-r--r--test/include.am146
-rw-r--r--test/print-winsock-errors.c84
-rw-r--r--test/regress.c1055
-rw-r--r--test/regress.h19
-rw-r--r--test/regress_buffer.c724
-rw-r--r--test/regress_bufferevent.c200
-rw-r--r--test/regress_dns.c244
-rw-r--r--test/regress_et.c15
-rw-r--r--test/regress_finalize.c347
-rw-r--r--test/regress_http.c562
-rw-r--r--test/regress_iocp.c56
-rw-r--r--test/regress_listener.c14
-rw-r--r--test/regress_main.c90
-rw-r--r--test/regress_minheap.c25
-rw-r--r--test/regress_rpc.c25
-rw-r--r--test/regress_ssl.c12
-rw-r--r--test/regress_testutils.c11
-rw-r--r--test/regress_testutils.h6
-rw-r--r--test/regress_thread.c188
-rw-r--r--test/regress_thread.h48
-rw-r--r--test/regress_util.c419
-rw-r--r--test/regress_zlib.c21
-rwxr-xr-xtest/rpcgen_wrapper.sh9
-rw-r--r--test/test-changelist.c25
-rw-r--r--test/test-closed.c117
-rw-r--r--test/test-dumpevents.c179
-rw-r--r--test/test-eof.c12
-rw-r--r--test/test-fdleak.c249
-rw-r--r--test/test-init.c8
-rw-r--r--test/test-ratelim.c159
-rwxr-xr-xtest/test-ratelim.sh11
-rw-r--r--test/test-time.c15
-rw-r--r--test/test-weof.c14
-rwxr-xr-xtest/test.sh155
-rw-r--r--test/tinytest.c171
-rw-r--r--test/tinytest.h31
-rw-r--r--test/tinytest_demo.c47
-rw-r--r--test/tinytest_local.h4
-rw-r--r--test/tinytest_macros.h97
-rw-r--r--time-internal.h101
-rw-r--r--util-internal.h252
-rw-r--r--whatsnew-2.0.txt2
-rw-r--r--whatsnew-2.1.txt690
-rw-r--r--win32select.c51
192 files changed, 23636 insertions, 7100 deletions
diff --git a/.gitignore b/.gitignore
index 622ab170..522c5df3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -25,6 +25,7 @@
# Autotools stuff
.deps
+.dirstamp
Makefile
Makefile.in
@@ -56,6 +57,7 @@ libevent_openssl.pc
/aclocal.m4
/compile
/doxygen
+/config.cache
/config.guess
/config.log
/config.status
@@ -69,17 +71,21 @@ libevent_openssl.pc
/ltmain.sh
/missing
/stamp-h1
+/stamp-h2
/include/event2/event-config.h
+/evconfig-private.h
/sample/dns-example
-/sample/event-test
+/sample/event-read-fifo
/sample/hello-world
/sample/http-server
/sample/le-proxy
+/sample/https-client
/sample/signal-test
/sample/time-test
+/test-driver
/test/bench
/test/bench_cascade
/test/bench_http
@@ -88,11 +94,29 @@ libevent_openssl.pc
/test/regress.gen.c
/test/regress.gen.h
/test/rpcgen-attempted
+/test/test-dumpevents
/test/test-eof
+/test/test-closed
/test/test-init
/test/test-ratelim
/test/test-script.sh
/test/test-time
/test/test-weof
/test/test-changelist
-
+/test/test-fdleak
+
+
+# Files generated by cmake
+/CMakeCache.txt
+/CMakeFiles/
+/CTestTestfile.cmake
+/DartConfiguration.tcl
+/LibeventConfig.cmake
+/LibeventConfigVersion.cmake
+/LibeventTargets.cmake
+/bin/
+/cmake_install.cmake
+/include/evconfig-private.h
+/lib/
+/tmp/
+/verify_tests.sh
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 00000000..6f10ff62
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,16 @@
+env:
+ matrix:
+ - EVENT_BUILD_METHOD=cmake EVENT_CMAKE_OPTIONS=""
+ - EVENT_BUILD_METHOD=cmake EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_OPENSSL=ON"
+ - EVENT_BUILD_METHOD=cmake EVENT_CMAKE_OPTIONS="-DEVENT__DISABLE_THREAD_SUPPORT=ON"
+ - EVENT_BUILD_METHOD=autotools
+language: c
+compiler:
+ - gcc
+ - clang
+install:
+ - sudo apt-get update -qq
+ - sudo apt-get install -y -qq zlib1g-dev libssl-dev build-essential automake autoconf cmake
+script:
+ - if [ "$EVENT_BUILD_METHOD" = "autotools" ]; then ./autogen.sh && ./configure && make && make verify; fi
+ - if [ "$EVENT_BUILD_METHOD" = "cmake" ]; then mkdir build && cd build && cmake .. $EVENT_CMAKE_OPTIONS && cmake --build . && CTEST_OUTPUT_ON_FAILURE=1 cmake --build . --target verify; fi
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 00000000..1d38e3fd
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,1154 @@
+#
+# Libevent CMake project
+#
+# Based on initial work by:
+# Alexey Ozeritsky
+#
+# Additional changes:
+# Brodie Thiesfield
+# Joakim Soderberg
+# Trond Norbye
+# Sergei Nikulov
+#
+# Build example:
+#
+# cd libevent
+# md build
+# cd build
+# cmake -G "Visual Studio 10" ..
+# start libevent.sln
+#
+cmake_minimum_required(VERSION 2.8.0 FATAL_ERROR)
+
+# get rid of the extra default configurations
+set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Limited configurations" FORCE)
+
+project(libevent C)
+
+set(EVENT_VERSION_MAJOR 2)
+set(EVENT_VERSION_MINOR 1)
+set(EVENT_VERSION_PATCH 4)
+set(EVENT_NUMERIC_VERSION 0x02010401)
+set(EVENT_VERSION_DEVTAG "-dev")
+
+set(EVENT_ABI_MAJOR 2)
+set(EVENT_ABI_MINOR 1)
+set(EVENT_ABI_PATCH 4)
+set(EVENT_ABI_LIBVERSION "${EVENT_ABI_MAJOR}.${EVENT_ABI_MINOR}.${EVENT_ABI_PATCH}")
+
+set(EVENT_VERSION "${EVENT_VERSION_MAJOR}.${EVENT_VERSION_MINOR}.${EVENT_VERSION_PATCH}-alpha${EVENT_VERSION_DEVTAG}")
+set(EVENT_PACKAGE_VERSION "${EVENT_VERSION_MAJOR}.${EVENT_VERSION_MINOR}.${EVENT_VERSION_PATCH}")
+
+option(EVENT__BUILD_SHARED_LIBRARIES "Define if libevent should be built with shared libraries instead of archives" OFF)
+option(EVENT__DISABLE_DEBUG_MODE "Define if libevent should build without support for a debug mode" OFF)
+option(EVENT__ENABLE_VERBOSE_DEBUG "Enables verbose debugging" OFF)
+option(EVENT__DISABLE_MM_REPLACEMENT "Define if libevent should not allow replacing the mm functions" OFF)
+option(EVENT__DISABLE_THREAD_SUPPORT "Define if libevent should not be compiled with thread support" OFF)
+option(EVENT__DISABLE_OPENSSL "Define if libevent should build without support for OpenSSL encrpytion" OFF)
+option(EVENT__DISABLE_BENCHMARK "Defines if libevent should build without the benchmark exectuables" OFF)
+option(EVENT__DISABLE_TESTS "If tests should be compiled or not" OFF)
+option(EVENT__DISABLE_REGRESS "Disable the regress tests" OFF)
+option(EVENT__DISABLE_SAMPLES "Disable sample files" OFF)
+option(EVENT__FORCE_KQUEUE_CHECK "When crosscompiling forces running a test program that verifies that Kqueue works with pipes. Note that this requires you to manually run the test program on the the cross compilation target to verify that it works. See cmake documentation for try_run for more details" OFF)
+option(EVENT__COVERAGE "Enable running gcov to get a test coverage report (only works with GCC/CLang). Make sure to enable -DCMAKE_BUILD_TYPE=Debug as well." OFF)
+# TODO: Add --disable-largefile omit support for large files
+
+# Put the libaries and binaries that get built into directories at the
+# top of the build tree rather than in hard-to-find leaf directories.
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
+set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
+set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
+
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/")
+include(CheckFunctionExistsEx)
+include(CheckFileOffsetBits)
+include(CheckFunctionExists)
+include(CheckIncludeFile)
+include(CheckIncludeFiles)
+include(CheckTypeSize)
+include(CheckVariableExists)
+include(CheckSymbolExists)
+include(CheckStructHasMember)
+include(CheckCSourceCompiles)
+include(CheckPrototypeDefinition)
+
+if (EVENT__ENABLE_VERBOSE_DEBUG)
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DUSE_DEBUG=1")
+endif()
+
+# Setup compiler flags for coverage.
+if (EVENT__COVERAGE)
+ if(NOT CMAKE_COMPILER_IS_GNUCC)
+ if (NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
+ message(FATAL_ERROR "Trying to compile coverage support (--DEVENT__COVERAGE) but compiler is not GNU gcc! Aborting... You can set this on the command line using CC=/usr/bin/gcc CXX=/usr/bin/g++ cmake <options> ..")
+ endif()
+ endif()
+
+ if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
+ message(FATAL_ERROR "Code coverage results with an optimised (non-Debug) build may be misleading! Add -DCMAKE_BUILD_TYPE=Debug")
+ endif()
+
+ message("Setting coverage compiler flags")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
+endif()
+
+# GCC specific options.
+if (CMAKE_COMPILER_IS_GNUCC)
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
+
+ option(EVENT__DISABLE_GCC_WARNINGS "Disable verbose warnings with GCC" OFF)
+ if (EVENT__DISABLE_GCC_WARNINGS)
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w")
+ endif()
+
+ option(EVENT__ENABLE_GCC_HARDENING "Enable compiler security checks" OFF)
+ if (EVENT__ENABLE_GCC_HARDENING)
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_FORTIFY_SOURCE=2 -fstack-protector-all -fwrapv -fPIE -Wstack-protector --param ssp-buffer-size=1")
+ endif()
+
+ option(EVENT__ENABLE_GCC_FUNCTION_SECTIONS "Enable gcc function sections" OFF)
+ if (EVENT__ENABLE_GCC_FUNCTION_SECTIONS)
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections")
+ # TODO: Add --gc-sections support. We need some checks for NetBSD to ensure this works.
+ endif()
+
+ option(EVENT__ENABLE_GCC_WARNINGS "Make all GCC warnings into errors" OFF)
+ if (EVENT__ENABLE_GCC_WARNINGS)
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
+ endif()
+
+ # We need to test for at least gcc 2.95 here, because older versions don't
+ # have -fno-strict-aliasing
+ execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
+ OUTPUT_VARIABLE GCC_VERSION)
+ if (GCC_VERSION VERSION_GREATER 2.95)
+ message(STATUS "GCC Version >= 2.95 enabling no-strict-aliasing")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-strict-aliasing")
+ endif()
+endif()
+
+if (APPLE)
+ # Get rid of deprecated warnings for OpenSSL on OSX 10.7 and greater.
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=deprecated-declarations")
+ # Get rid of "clang: warning: argument unused during compilation: -I etc
+ if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Qunused-arguments")
+ endif()
+endif()
+
+# Winsock.
+if(WIN32)
+ set(CMAKE_EXTRA_INCLUDE_FILES winsock2.h ws2tcpip.h)
+ set(CMAKE_REQUIRED_LIBRARIES ws2_32.lib)
+ set(CMAKE_REQUIRED_DEFINITIONS -FIwinsock2.h -FIws2tcpip.h)
+endif()
+
+# Check if _GNU_SOURCE is available.
+CHECK_SYMBOL_EXISTS(__GNU_LIBRARY__ "features.h" _GNU_SOURCE)
+
+if (_GNU_SOURCE)
+ add_definitions(-D_GNU_SOURCE)
+endif()
+
+CHECK_INCLUDE_FILE(sys/types.h EVENT__HAVE_SYS_TYPES_H)
+if(EVENT__HAVE_SYS_TYPES_H)
+ list(APPEND CMAKE_EXTRA_INCLUDE_FILES sys/types.h)
+endif()
+
+CHECK_INCLUDE_FILE(sys/socket.h EVENT__HAVE_SYS_SOCKET_H)
+if(EVENT__HAVE_SYS_SOCKET_H)
+ list(APPEND CMAKE_EXTRA_INCLUDE_FILES sys/socket.h)
+endif()
+
+CHECK_INCLUDE_FILE(netinet/in.h EVENT__HAVE_NETINET_IN_H)
+if(EVENT__HAVE_NETINET_IN_H)
+ list(APPEND CMAKE_EXTRA_INCLUDE_FILES netinet/in.h)
+endif()
+
+CHECK_INCLUDE_FILE(netinet/in6.h EVENT__HAVE_NETINET_IN6_H)
+if(EVENT__HAVE_NETINET_IN6_H)
+ list(APPEND CMAKE_EXTRA_INCLUDE_FILES netinet/in6.h)
+endif()
+
+CHECK_INCLUDE_FILE(unistd.h EVENT__HAVE_UNISTD_H)
+CHECK_INCLUDE_FILE(netdb.h EVENT__HAVE_NETDB_H)
+CHECK_INCLUDE_FILE(dlfcn.h EVENT__HAVE_DLFCN_H)
+CHECK_INCLUDE_FILE(arpa/inet.h EVENT__HAVE_ARPA_INET_H)
+CHECK_INCLUDE_FILE(fcntl.h EVENT__HAVE_FCNTL_H)
+if(EVENT__HAVE_FCNTL_H)
+ list(APPEND CMAKE_EXTRA_INCLUDE_FILES fcntl.h)
+endif()
+CHECK_INCLUDE_FILE(inttypes.h EVENT__HAVE_INTTYPES_H)
+CHECK_INCLUDE_FILE(memory.h EVENT__HAVE_MEMORY_H)
+CHECK_INCLUDE_FILE(poll.h EVENT__HAVE_POLL_H)
+CHECK_INCLUDE_FILE(port.h EVENT__HAVE_PORT_H)
+if(EVENT__HAVE_PORT_H)
+ list(APPEND CMAKE_EXTRA_INCLUDE_FILES port.h)
+endif()
+CHECK_INCLUDE_FILE(signal.h EVENT__HAVE_SIGNAL_H)
+CHECK_INCLUDE_FILE(stdarg.h EVENT__HAVE_STDARG_H)
+CHECK_INCLUDE_FILE(stddef.h EVENT__HAVE_STDDEF_H)
+CHECK_INCLUDE_FILE(stdint.h EVENT__HAVE_STDINT_H)
+CHECK_INCLUDE_FILE(stdlib.h EVENT__HAVE_STDLIB_H)
+CHECK_INCLUDE_FILE(strings.h EVENT__HAVE_STRINGS_H)
+CHECK_INCLUDE_FILE(string.h EVENT__HAVE_STRING_H)
+CHECK_INCLUDE_FILE(sys/devpoll.h EVENT__HAVE_SYS_DEVPOLL_H)
+CHECK_INCLUDE_FILE(sys/epoll.h EVENT__HAVE_SYS_EPOLL_H)
+CHECK_INCLUDE_FILE(sys/eventfd.h EVENT__HAVE_SYS_EVENTFD_H)
+CHECK_INCLUDE_FILE(sys/event.h EVENT__HAVE_SYS_EVENT_H)
+CHECK_INCLUDE_FILE(sys/ioctl.h EVENT__HAVE_SYS_IOCTL_H)
+CHECK_INCLUDE_FILE(sys/mman.h EVENT__HAVE_SYS_MMAN_H)
+CHECK_INCLUDE_FILE(sys/param.h EVENT__HAVE_SYS_PARAM_H)
+CHECK_INCLUDE_FILE(sys/queue.h EVENT__HAVE_SYS_QUEUE_H)
+CHECK_INCLUDE_FILE(sys/select.h EVENT__HAVE_SYS_SELECT_H)
+CHECK_INCLUDE_FILE(sys/sendfile.h EVENT__HAVE_SYS_SENDFILE_H)
+CHECK_INCLUDE_FILE(sys/stat.h EVENT__HAVE_SYS_STAT_H)
+CHECK_INCLUDE_FILE(sys/time.h EVENT__HAVE_SYS_TIME_H)
+if(EVENT__HAVE_SYS_TIME_H)
+ list(APPEND CMAKE_EXTRA_INCLUDE_FILES sys/time.h)
+endif()
+CHECK_INCLUDE_FILE(sys/uio.h EVENT__HAVE_SYS_UIO_H)
+CHECK_INCLUDE_FILES("sys/types.h;ifaddrs.h" EVENT__HAVE_IFADDRS_H)
+CHECK_INCLUDE_FILE(mach/mach_time.h EVENT__HAVE_MACH_MACH_TIME_H)
+CHECK_INCLUDE_FILE(netdb.h EVENT__HAVE_NETDB_H)
+CHECK_INCLUDE_FILE(netinet/tcp.h EVENT__HAVE_NETINET_TCP_H)
+CHECK_INCLUDE_FILE(sys/wait.h EVENT__HAVE_SYS_WAIT_H)
+CHECK_INCLUDE_FILE(sys/resource.h EVENT__HAVE_SYS_RESOURCE_H)
+CHECK_INCLUDE_FILE(sys/sysctl.h EVENT__HAVE_SYS_SYSCTL_H)
+CHECK_INCLUDE_FILE(sys/timerfd.h EVENT__HAVE_SYS_TIMERFD_H)
+
+
+CHECK_FUNCTION_EXISTS_EX(epoll_create EVENT__HAVE_EPOLL)
+CHECK_FUNCTION_EXISTS_EX(epoll_ctl EVENT__HAVE_EPOLL_CTL)
+CHECK_FUNCTION_EXISTS_EX(eventfd EVENT__HAVE_EVENTFD)
+CHECK_FUNCTION_EXISTS_EX(clock_gettime EVENT__HAVE_CLOCK_GETTIME)
+CHECK_FUNCTION_EXISTS_EX(fcntl EVENT__HAVE_FCNTL)
+CHECK_FUNCTION_EXISTS_EX(getaddrinfo EVENT__HAVE_GETADDRINFO)
+CHECK_FUNCTION_EXISTS_EX(getnameinfo EVENT__HAVE_GETNAMEINFO)
+CHECK_FUNCTION_EXISTS_EX(gettimeofday EVENT__HAVE_GETTIMEOFDAY)
+CHECK_FUNCTION_EXISTS_EX(getprotobynumber EVENT__HAVE_GETPROTOBYNUMBER)
+CHECK_FUNCTION_EXISTS_EX(getservbyname EVENT__HAVE_GETSERVBYNAME)
+CHECK_FUNCTION_EXISTS_EX(inet_ntop EVENT__HAVE_INET_NTOP)
+CHECK_FUNCTION_EXISTS_EX(inet_pton EVENT__HAVE_INET_PTON)
+CHECK_FUNCTION_EXISTS_EX(kqueue EVENT__HAVE_KQUEUE)
+CHECK_FUNCTION_EXISTS_EX(mmap EVENT__HAVE_MMAP)
+CHECK_FUNCTION_EXISTS_EX(pipe EVENT__HAVE_PIPE)
+CHECK_FUNCTION_EXISTS_EX(pipe2 EVENT__HAVE_PIPE2)
+CHECK_FUNCTION_EXISTS_EX(poll EVENT__HAVE_POLL)
+CHECK_FUNCTION_EXISTS_EX(port_create EVENT__HAVE_PORT_CREATE)
+CHECK_FUNCTION_EXISTS_EX(sendfile EVENT__HAVE_SENDFILE)
+CHECK_FUNCTION_EXISTS_EX(sigaction EVENT__HAVE_SIGACTION)
+CHECK_FUNCTION_EXISTS_EX(signal EVENT__HAVE_SIGNAL)
+CHECK_FUNCTION_EXISTS_EX(splice EVENT__HAVE_SPLICE)
+CHECK_FUNCTION_EXISTS_EX(strlcpy EVENT__HAVE_STRLCPY)
+CHECK_FUNCTION_EXISTS_EX(strsep EVENT__HAVE_STRSEP)
+CHECK_FUNCTION_EXISTS_EX(strtok_r EVENT__HAVE_STRTOK_R)
+CHECK_FUNCTION_EXISTS_EX(strtoll EVENT__HAVE_STRTOLL)
+CHECK_FUNCTION_EXISTS_EX(vasprintf EVENT__HAVE_VASPRINTF)
+CHECK_FUNCTION_EXISTS_EX(sysctl EVENT__HAVE_SYSCTL)
+CHECK_FUNCTION_EXISTS_EX(accept4 EVENT__HAVE_ACCEPT4)
+CHECK_FUNCTION_EXISTS_EX(arc4random EVENT__HAVE_ARC4RANDOM)
+CHECK_FUNCTION_EXISTS_EX(arc4random_buf EVENT__HAVE_ARC4RANDOM_BUF)
+CHECK_FUNCTION_EXISTS_EX(epoll_create1 EVENT__HAVE_EPOLL_CREATE1)
+CHECK_FUNCTION_EXISTS_EX(getegid EVENT__HAVE_GETEGID)
+CHECK_FUNCTION_EXISTS_EX(geteuid EVENT__HAVE_GETEUID)
+CHECK_FUNCTION_EXISTS_EX(getifaddrs EVENT__HAVE_GETIFADDRS)
+CHECK_FUNCTION_EXISTS_EX(issetugid EVENT__HAVE_ISSETUGID)
+CHECK_FUNCTION_EXISTS_EX(mach_absolute_time EVENT__HAVE_MACH_ABSOLUTE_TIME)
+CHECK_FUNCTION_EXISTS_EX(nanosleep EVENT__HAVE_NANOSLEEP)
+CHECK_FUNCTION_EXISTS_EX(usleep EVENT__HAVE_USLEEP)
+CHECK_FUNCTION_EXISTS_EX(timeradd EVENT__HAVE_TIMERADD)
+CHECK_FUNCTION_EXISTS_EX(timerclear EVENT__HAVE_TIMERCLEAR)
+CHECK_FUNCTION_EXISTS_EX(timercmp EVENT__HAVE_TIMERCMP)
+CHECK_FUNCTION_EXISTS_EX(timerfd_create HAVE_TIMERFD_CREATE)
+CHECK_FUNCTION_EXISTS_EX(timerisset EVENT__HAVE_TIMERISSET)
+CHECK_FUNCTION_EXISTS_EX(putenv EVENT__HAVE_PUTENV)
+CHECK_FUNCTION_EXISTS_EX(setenv EVENT__HAVE_SETENV)
+CHECK_FUNCTION_EXISTS_EX(setrlimit EVENT__HAVE_SETRLIMIT)
+CHECK_FUNCTION_EXISTS_EX(umask EVENT__HAVE_UMASK)
+CHECK_FUNCTION_EXISTS_EX(unsetenv EVENT__HAVE_UNSETENV)
+
+# Get the gethostbyname_r prototype.
+CHECK_FUNCTION_EXISTS_EX(gethostbyname_r EVENT__HAVE_GETHOSTBYNAME_R)
+
+if(EVENT__HAVE_GETHOSTBYNAME_R)
+ CHECK_PROTOTYPE_DEFINITION(gethostbyname_r
+ "int gethostbyname_r(const char *name, struct hostent *hp, struct hostent_data *hdata)"
+ "0"
+ "netdb.h"
+ EVENT__HAVE_GETHOSTBYNAME_R_3_ARG)
+
+ CHECK_PROTOTYPE_DEFINITION(gethostbyname_r
+ "struct hostent *gethostbyname_r(const char *name, struct hostent *hp, char *buf, size_t buflen, int *herr)"
+ "NULL"
+ "netdb.h"
+ EVENT__HAVE_GETHOSTBYNAME_R_5_ARG)
+
+ CHECK_PROTOTYPE_DEFINITION(gethostbyname_r
+ "int gethostbyname_r(const char *name, struct hostent *hp, char *buf, size_t buflen, struct hostent **result, int *herr)"
+ "0"
+ "netdb.h"
+ EVENT__HAVE_GETHOSTBYNAME_R_6_ARG)
+endif()
+
+if(HAVE_PORT_H AND HAVE_PORT_CREATE)
+ set(EVENT__HAVE_EVENT_PORTS 1)
+endif()
+
+if(NOT WIN32)
+ CHECK_FUNCTION_EXISTS_EX(select EVENT__HAVE_SELECT)
+endif()
+
+CHECK_TYPE_SIZE("uint8_t" EVENT__HAVE_UINT8_T)
+CHECK_TYPE_SIZE("uint16_t" EVENT__HAVE_UINT16_T)
+CHECK_TYPE_SIZE("uint32_t" EVENT__HAVE_UINT32_T)
+CHECK_TYPE_SIZE("uint64_t" EVENT__HAVE_UINT64_T)
+CHECK_TYPE_SIZE("short" EVENT__SIZEOF_SHORT BUILTIN_TYPES_ONLY)
+CHECK_TYPE_SIZE("int" EVENT__SIZEOF_INT BUILTIN_TYPES_ONLY)
+CHECK_TYPE_SIZE("unsigned" EVENT__SIZEOF_UNSIGNED BUILTIN_TYPES_ONLY)
+CHECK_TYPE_SIZE("unsigned int" EVENT__SIZEOF_UNSIGNED_INT BUILTIN_TYPES_ONLY)
+CHECK_TYPE_SIZE("long" EVENT__SIZEOF_LONG BUILTIN_TYPES_ONLY)
+CHECK_TYPE_SIZE("long long" EVENT__SIZEOF_LONG_LONG BUILTIN_TYPES_ONLY)
+
+if(WIN32)
+ # These aren't available until Windows Vista.
+ # But you can still link them. They just won't be found when running the exe.
+ set(EVENT__HAVE_INET_NTOP 0)
+ set(EVENT__HAVE_INET_PTON 0)
+endif()
+
+# Check for different inline keyword versions.
+foreach(KEYWORD "inline" "__inline__" "__inline")
+ set(CMAKE_REQUIRED_DEFINITIONS "-DKEYWORD=${KEYWORD}")
+ CHECK_C_SOURCE_COMPILES(
+ "
+ #include <stdio.h>
+ KEYWORD void a() {}
+ int main(int argc, char **argv) { a(); return 0; }
+ " HAVE_${KEYWORD})
+endforeach()
+
+if (NOT HAVE_inline)
+ if (HAVE___inline__)
+ set(EVENT__inline __inline__)
+ elseif(HAVE___inline)
+ set(EVENT__inline __inline)
+ endif()
+endif()
+set(CMAKE_REQUIRED_DEFINITIONS "")
+
+# Check for different function name macros.
+foreach(KEYWORD "__func__" "__FUNCTION__")
+ set(CMAKE_REQUIRED_DEFINITIONS "-DKEYWORD=${KEYWORD}")
+ CHECK_C_SOURCE_COMPILES(
+ "
+ #include <stdio.h>
+ int main(int argc, char **argv) { const char *cp = KEYWORD; return 0; }
+ " HAVE_${KEYWORD})
+endforeach()
+set(CMAKE_REQUIRED_DEFINITIONS "")
+
+if (NOT HAVE___func__)
+ if (HAVE___FUNCTION__)
+ set(EVENT____func__ __FUNCTION__)
+ else()
+ # Substitute for __func__
+ set(EVENT____func__ __FILE__)
+ endif()
+endif()
+
+CHECK_SYMBOL_EXISTS(TAILQ_FOREACH sys/queue.h EVENT__HAVE_TAILQFOREACH)
+CHECK_SYMBOL_EXISTS(CTL_KERN sys/sysctl.h EVENT__HAVE_DECL_CTL_KERN)
+CHECK_SYMBOL_EXISTS(KERN_ARND sys/sysctl.h EVENT__HAVE_DECL_KERN_ARND)
+CHECK_SYMBOL_EXISTS(KERN_RANDOM sys/sysctl.h EVENT__HAVE_DECL_KERN_RANDOM)
+CHECK_SYMBOL_EXISTS(RANDOM_UUID sys/sysctl.h EVENT__HAVE_DECL_RANDOM_UUID)
+CHECK_SYMBOL_EXISTS(F_SETFD fcntl.h EVENT__HAVE_SETFD)
+
+CHECK_TYPE_SIZE(fd_mask EVENT__HAVE_FD_MASK)
+
+CHECK_TYPE_SIZE(size_t EVENT__SIZEOF_SIZE_T)
+if(NOT EVENT__SIZEOF_SIZE_T)
+ set(EVENT__size_t unsigned)
+ set(EVENT__SIZEOF_SIZE_T ${EVENT__SIZEOF_UNSIGNED})
+endif()
+
+CHECK_TYPE_SIZE("off_t" EVENT__SIZEOF_OFF_T)
+
+CHECK_TYPE_SIZE(ssize_t EVENT__SIZEOF_SSIZE_T)
+CHECK_TYPE_SIZE(SSIZE_T EVENT__SIZEOF_UPPERCASE_SSIZE_T)
+if(NOT EVENT__SIZEOF_SSIZE_T)
+ if(EVENT__SIZEOF_UPPERCASE_SSIZE_T)
+ set(EVENT__ssize_t SSIZE_T)
+ set(EVENT__SIZEOF_SSIZE_T ${EVENT__SIZEOF_UPPERCASE_SSIZE_T})
+ else()
+ set(EVENT__ssize_t int)
+ set(EVENT__SIZEOF_SSIZE_T ${EVENT__SIZEOF_INT})
+ endif()
+endif()
+
+CHECK_TYPE_SIZE(socklen_t EVENT__SIZEOF_SOCKLEN_T)
+if(NOT EVENT__SIZEOF_SOCKLEN_T)
+ set(EVENT__socklen_t "unsigned int")
+ set(EVENT__SIZEOF_SOCKLEN_T ${EVENT__SIZEOF_UNSIGNED_INT})
+endif()
+
+CHECK_TYPE_SIZE(pid_t EVENT__SIZEOF_PID_T)
+if(NOT EVENT__SIZEOF_PID_T)
+ set(EVENT__pid_t int)
+ set(EVENT__SIZEOF_PID_T ${EVENT__SIZEOF_INT})
+endif()
+
+CHECK_TYPE_SIZE(pthread_t EVENT__SIZEOF_PTHREAD_T)
+
+if(EVENT__HAVE_CLOCK_GETTIME)
+ set(EVENT__DNS_USE_CPU_CLOCK_FOR_ID 1)
+endif()
+
+CHECK_TYPE_SIZE("uintptr_t" EVENT__HAVE_UINTPTR_T)
+CHECK_TYPE_SIZE("void *" EVENT__SIZEOF_VOID_P)
+
+# Tests file offset bits.
+# TODO: Add AIX test for if -D_LARGE_FILES is needed.
+CHECK_FILE_OFFSET_BITS()
+set(EVENT___FILE_OFFSET_BITS _FILE_OFFSET_BITS)
+
+# Verify kqueue works with pipes.
+if (EVENT__HAVE_KQUEUE)
+ if (CMAKE_CROSSCOMPILING AND NOT EVENT__FORCE_KQUEUE_CHECK)
+ message(WARNING "Cannot check if kqueue works with pipes when crosscompiling, use EVENT__FORCE_KQUEUE_CHECK to be sure (this requires manually running a test program on the cross compilation target)")
+ set(EVENT__HAVE_WORKING_KQUEUE 1)
+ else()
+ message("Checking if kqueue works with pipes...")
+ include(CheckWorkingKqueue)
+ endif()
+endif()
+
+CHECK_SYMBOL_EXISTS(_MINIX "stdio.h" EVENT___MINIX)
+CHECK_SYMBOL_EXISTS(_POSIX_1_SOURCE "stdio.h" EVENT___POSIX_1_SOURCE)
+CHECK_SYMBOL_EXISTS(_POSIX_SOURCE "stdio.h" EVENT___POSIX_SOURCE)
+
+if(EVENT__HAVE_NETDB_H)
+ list(APPEND CMAKE_EXTRA_INCLUDE_FILES netdb.h)
+ CHECK_TYPE_SIZE("struct addrinfo" EVENT__HAVE_STRUCT_ADDRINFO)
+elseif(WIN32)
+ list(APPEND CMAKE_EXTRA_INCLUDE_FILES ws2tcpip.h)
+ CHECK_TYPE_SIZE("struct addrinfo" EVENT__HAVE_STRUCT_ADDRINFO)
+endif()
+
+# Check for sockaddr structure sizes.
+set(SOCKADDR_HEADERS)
+
+if (WIN32)
+ set(CMAKE_REQUIRED_DEFINITIONS "-DWIN32_LEAN_AND_MEAN")
+ if (_MSC_VER LESS 1300)
+ set(SOCKADDR_HEADERS winsock.h)
+ else()
+ set(SOCKADDR_HEADERS winsock2.h ws2tcpip.h)
+ endif()
+else()
+ if (EVENT__HAVE_NETINET_IN_H)
+ set(SOCKADDR_HEADERS ${SOCKADDR_HEADERS} netinet/in.h)
+ endif()
+
+ if (EVENT__HAVE_NETINET_IN6_H)
+ set(SOCKADDR_HEADERS ${SOCKADDR_HEADERS} netinet/in6.h)
+ endif()
+
+ if (EVENT__HAVE_SYS_SOCKET_H)
+ set(SOCKADDR_HEADERS ${SOCKADDR_HEADERS} sys/socket.h)
+ endif()
+
+ if (EVENT__HAVE_NETDB_H)
+ set(SOCKADDR_HEADERS ${SOCKADDR_HEADERS} netdb.h)
+ endif()
+endif()
+
+CHECK_TYPE_SIZE("struct in6_addr" EVENT__HAVE_STRUCT_IN6_ADDR)
+if(EVENT__HAVE_STRUCT_IN6_ADDR)
+ CHECK_STRUCT_HAS_MEMBER("struct in6_addr" s6_addr16 "${SOCKADDR_HEADERS}" EVENT__HAVE_STRUCT_IN6_ADDR_S6_ADDR16)
+ CHECK_STRUCT_HAS_MEMBER("struct in6_addr" s6_addr32 "${SOCKADDR_HEADERS}" EVENT__HAVE_STRUCT_IN6_ADDR_S6_ADDR32)
+endif()
+
+CHECK_TYPE_SIZE("sa_family_t" EVENT__HAVE_SA_FAMILY_T)
+CHECK_TYPE_SIZE("struct sockaddr_in6" EVENT__HAVE_STRUCT_SOCKADDR_IN6)
+if(EVENT__HAVE_STRUCT_SOCKADDR_IN6)
+ CHECK_STRUCT_HAS_MEMBER("struct sockaddr_in6" sin6_len "${SOCKADDR_HEADERS}" EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN)
+ CHECK_STRUCT_HAS_MEMBER("struct sockaddr_in6" sin_len "${SOCKADDR_HEADERS}" EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN)
+endif()
+
+CHECK_TYPE_SIZE("struct sockaddr_storage" EVENT__HAVE_STRUCT_SOCKADDR_STORAGE)
+if(EVENT__HAVE_STRUCT_SOCKADDR_STORAGE)
+ CHECK_STRUCT_HAS_MEMBER("struct sockaddr_storage" ss_family "${SOCKADDR_HEADERS}" EVENT__HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY)
+ CHECK_STRUCT_HAS_MEMBER("struct sockaddr_storage" __ss_family "${SOCKADDR_HEADERS}" EVENT__HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY)
+endif()
+
+# Group the source files.
+set(HDR_PRIVATE
+ bufferevent-internal.h
+ changelist-internal.h
+ defer-internal.h
+ epolltable-internal.h
+ evbuffer-internal.h
+ event-internal.h
+ evmap-internal.h
+ evrpc-internal.h
+ evsignal-internal.h
+ evthread-internal.h
+ ht-internal.h
+ http-internal.h
+ iocp-internal.h
+ ipv6-internal.h
+ log-internal.h
+ minheap-internal.h
+ mm-internal.h
+ ratelim-internal.h
+ strlcpy-internal.h
+ util-internal.h
+ evconfig-private.h
+ compat/sys/queue.h
+ )
+
+set(HDR_COMPAT
+ include/evdns.h
+ include/evrpc.h
+ include/event.h
+ include/evhttp.h
+ include/evutil.h
+ )
+
+set(HDR_PUBLIC
+ include/event2/buffer.h
+ include/event2/bufferevent.h
+ include/event2/bufferevent_compat.h
+ include/event2/bufferevent_struct.h
+ include/event2/buffer_compat.h
+ include/event2/dns.h
+ include/event2/dns_compat.h
+ include/event2/dns_struct.h
+ include/event2/event.h
+ include/event2/event_compat.h
+ include/event2/event_struct.h
+ include/event2/http.h
+ include/event2/http_compat.h
+ include/event2/http_struct.h
+ include/event2/keyvalq_struct.h
+ include/event2/listener.h
+ include/event2/rpc.h
+ include/event2/rpc_compat.h
+ include/event2/rpc_struct.h
+ include/event2/tag.h
+ include/event2/tag_compat.h
+ include/event2/thread.h
+ include/event2/util.h
+ include/event2/visibility.h
+ ${PROJECT_BINARY_DIR}/include/event2/event-config.h
+ )
+
+set(SRC_CORE
+ buffer.c
+ bufferevent.c
+ bufferevent_filter.c
+ bufferevent_pair.c
+ bufferevent_ratelim.c
+ bufferevent_sock.c
+ event.c
+ evmap.c
+ evthread.c
+ evutil.c
+ evutil_rand.c
+ evutil_time.c
+ listener.c
+ log.c
+ signal.c
+ strlcpy.c
+ )
+
+if(EVENT__HAVE_SELECT)
+ list(APPEND SRC_CORE select.c)
+endif()
+
+if(EVENT__HAVE_POLL)
+ list(APPEND SRC_CORE poll.c)
+endif()
+
+if(EVENT__HAVE_KQUEUE)
+ list(APPEND SRC_CORE kqueue.c)
+endif()
+
+if(EVENT__HAVE_DEVPOLL)
+ list(APPEND SRC_CORE devpoll.c)
+endif()
+
+if(EVENT__HAVE_EPOLL)
+ list(APPEND SRC_CORE epoll_sub.c epoll.c)
+endif()
+
+if(EVENT__HAVE_EVENT_PORTS)
+ list(APPEND SRC_CORE evport.c)
+endif()
+
+if (NOT EVENT__DISABLE_OPENSSL)
+ find_package(OpenSSL REQUIRED)
+ set(EVENT__HAVE_OPENSSL 1)
+ message("OpenSSL include: ${OPENSSL_INCLUDE_DIR}")
+ message("OpenSSL lib: ${OPENSSL_LIBRARIES}")
+ include_directories(${OPENSSL_INCLUDE_DIR})
+ list(APPEND SRC_CORE bufferevent_openssl.c)
+ list(APPEND HDR_PUBLIC include/event2/bufferevent_ssl.h)
+ list(APPEND LIB_APPS ${OPENSSL_LIBRARIES})
+endif()
+
+if (NOT EVENT__DISABLE_THREAD_SUPPORT)
+ if (WIN32)
+ list(APPEND SRC_CORE evthread_win32.c)
+ else()
+ find_package(Threads REQUIRED)
+ if (NOT CMAKE_USE_PTHREADS_INIT)
+ message(FATAL_ERROR "Failed to find Pthreads, set EVENT__DISABLE_THREAD_SUPPORT to turn off thread support")
+ endif()
+ set(EVENT__HAVE_PTHREADS 1)
+ list(APPEND SRC_CORE evthread_pthread.c)
+ list(APPEND LIB_APPS ${CMAKE_THREAD_LIBS_INIT})
+ endif()
+endif()
+
+if (NOT EVENT__DISABLE_TESTS)
+ # Zlib is only used for testing.
+ find_package(ZLIB)
+
+ if (ZLIB_LIBRARY)
+ set(EVENT__HAVE_ZLIB 1)
+ set(EVENT__HAVE_ZLIB_H)
+ include_directories(${ZLIB_INCLUDE_DIRS})
+ list(APPEND LIB_APPS ${ZLIB_LIBRARIES})
+ endif()
+endif()
+
+set(SRC_EXTRA
+ event_tagging.c
+ http.c
+ evdns.c
+ evrpc.c
+ )
+
+add_definitions(-DHAVE_CONFIG_H)
+
+# We use BEFORE here so we don't accidentally look in system directories
+# first for some previous versions of the headers that are installed.
+include_directories(BEFORE ${PROJECT_SOURCE_DIR}
+ ${PROJECT_SOURCE_DIR}/compat
+ ${PROJECT_SOURCE_DIR}/include)
+
+if(WIN32)
+ list(APPEND SRC_CORE
+ buffer_iocp.c
+ bufferevent_async.c
+ event_iocp.c
+ evthread_win32.c
+ win32select.c
+ )
+
+ list(APPEND HDR_PRIVATE WIN32-Code/getopt.h)
+
+ set(EVENT__DNS_USE_FTIME_FOR_ID 1)
+ add_definitions(-D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE)
+ set(LIB_PLATFORM ws2_32)
+ include_directories(./WIN32-Code)
+endif()
+
+if (UNIX)
+ list(APPEND LIB_PLATFORM m)
+endif()
+
+source_group("Headers Private" FILES ${HDR_PRIVATE})
+source_group("Header Compat" FILES ${HDR_COMPAT})
+source_group("Headers Public" FILES ${HDR_PUBLIC})
+source_group("Source Core" FILES ${SRC_CORE})
+source_group("Source Extra" FILES ${SRC_EXTRA})
+
+# Generate the configure headers.
+# (Place them in the build dir so we don't polute the source tree with generated files).
+include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR}/include)
+
+if (EVENT__BUILD_SHARED_LIBRARIES)
+ set(EVENT__LIBRARY_TYPE SHARED)
+
+ if (CMAKE_COMPILER_IS_GNUC)
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
+ elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
+ elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "SunPro")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -xldscope=hidden")
+ endif ()
+ set(EVENT__NEED_DLLIMPORT 1)
+
+else (EVENT__BUILD_SHARED_LIBRARIES)
+ set(EVENT__LIBRARY_TYPE STATIC)
+endif (EVENT__BUILD_SHARED_LIBRARIES)
+
+configure_file(
+ ${CMAKE_CURRENT_SOURCE_DIR}/event-config.h.cmake
+ ${CMAKE_CURRENT_BINARY_DIR}/include/event2/event-config.h)
+
+configure_file(
+ ${CMAKE_CURRENT_SOURCE_DIR}/evconfig-private.h.cmake
+ ${CMAKE_CURRENT_BINARY_DIR}/include/evconfig-private.h)
+
+#
+# Create the libraries.
+#
+
+# TODO: Add dynamic versions of the libraries as well.
+add_library(event_core ${EVENT__LIBRARY_TYPE}
+ ${HDR_PRIVATE}
+ ${HDR_COMPAT}
+ ${HDR_PUBLIC}
+ ${SRC_CORE}
+ )
+
+add_library(event_extra ${EVENT__LIBRARY_TYPE}
+ ${HDR_PRIVATE}
+ ${HDR_COMPAT}
+ ${HDR_PUBLIC}
+ ${SRC_CORE}
+ ${SRC_EXTRA}
+ )
+
+# library exists for historical reasons; it contains the contents of
+# both libevent_core and libevent_extra. You shouldn’t use it; it may
+# go away in a future version of Libevent.
+add_library(event ${EVENT__LIBRARY_TYPE}
+ ${HDR_PRIVATE}
+ ${HDR_COMPAT}
+ ${HDR_PUBLIC}
+ ${SRC_CORE}
+ ${SRC_EXTRA}
+ )
+
+if (EVENT__BUILD_SHARED_LIBRARIES)
+ target_link_libraries(event_core ${OPENSSL_LIBRARIES}
+ ${CMAKE_THREAD_LIBS_INIT}
+ ${LIB_PLATFORM})
+
+ target_link_libraries(event ${OPENSSL_LIBRARIES}
+ ${CMAKE_THREAD_LIBS_INIT}
+ ${LIB_PLATFORM})
+
+ target_link_libraries(event_extra ${OPENSSL_LIBRARIES}
+ ${CMAKE_THREAD_LIBS_INIT}
+ ${LIB_PLATFORM})
+
+ set_target_properties(event PROPERTIES SOVERSION ${EVENT_ABI_LIBVERSION})
+ set_target_properties(event_core PROPERTIES SOVERSION ${EVENT_ABI_LIBVERSION})
+ set_target_properties(event_extra PROPERTIES SOVERSION ${EVENT_ABI_LIBVERSION})
+
+endif (EVENT__BUILD_SHARED_LIBRARIES)
+
+#
+# Samples.
+#
+
+if (NOT EVENT__DISABLE_SAMPLES)
+ set(SAMPLES
+ dns-example
+ event-read-fifo
+ hello-world
+ signal-test
+ http-server
+ time-test)
+
+ if (NOT EVENT__DISABLE_OPENSSL AND OPENSSL_LIBRARIES)
+ # Special sample with more than one file.
+ add_executable(https-client
+ sample/https-client.c
+ sample/openssl_hostname_validation.c
+ sample/hostcheck.c)
+ target_link_libraries(https-client event_extra ${LIB_APPS} ${LIB_PLATFORM})
+ add_dependencies(https-client event_extra)
+
+ # Requires OpenSSL.
+ list(APPEND SAMPLES le-proxy)
+ endif()
+
+ foreach(SAMPLE ${SAMPLES})
+ add_executable(${SAMPLE} sample/${SAMPLE}.c)
+ target_link_libraries(${SAMPLE} event_extra ${LIB_APPS} ${LIB_PLATFORM})
+ add_dependencies(${SAMPLE} event_extra)
+ endforeach()
+endif()
+
+if (NOT EVENT__DISABLE_BENCHMARK)
+ foreach (BENCHMARK bench bench_cascade bench_http bench_httpclient)
+ set(BENCH_SRC test/${BENCHMARK}.c)
+
+ if (WIN32)
+ list(APPEND BENCH_SRC WIN32-Code/getopt.c WIN32-Code/getopt_long.c)
+ endif()
+
+ add_executable(${BENCHMARK} ${BENCH_SRC})
+ target_link_libraries(${BENCHMARK} event_extra ${LIB_PLATFORM})
+ add_dependencies(${BENCHMARK} event_extra)
+ endforeach()
+endif()
+
+if (NOT EVENT__DISABLE_TESTS)
+
+ #
+ # Generate Regress tests.
+ #
+ if (NOT EVENT__DISABLE_REGRESS)
+
+ # (We require python to generate the regress tests)
+ find_package(PythonInterp)
+ if (PYTHONINTERP_FOUND AND PYTHON_VERSION_STRING VERSION_LESS "3.0.0")
+ set(__FOUND_USABLE_PYTHON 1)
+ endif()
+
+ if (__FOUND_USABLE_PYTHON)
+ message("Generating regress tests...")
+ add_definitions(-DTINYTEST_LOCAL)
+ add_custom_command(
+ OUTPUT
+ ${CMAKE_CURRENT_SOURCE_DIR}/test/regress.gen.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/test/regress.gen.h
+ DEPENDS
+ event_rpcgen.py
+ test/regress.rpc
+ COMMAND ${PYTHON_EXECUTABLE} ../event_rpcgen.py regress.rpc
+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
+ )
+
+ list(APPEND SRC_REGRESS
+ test/regress.c
+ test/regress.gen.c
+ test/regress.gen.h
+ test/regress_buffer.c
+ test/regress_bufferevent.c
+ test/regress_dns.c
+ test/regress_et.c
+ test/regress_finalize.c
+ test/regress_http.c
+ test/regress_listener.c
+ test/regress_main.c
+ test/regress_minheap.c
+ test/regress_rpc.c
+ test/regress_testutils.c
+ test/regress_testutils.h
+ test/regress_util.c
+ test/tinytest.c
+ ${SRC_CORE}
+ ${SRC_EXTRA}
+ )
+
+ if (WIN32)
+ list(APPEND SRC_REGRESS test/regress_iocp.c)
+ list(APPEND SRC_REGRESS test/regress_thread.c)
+ endif()
+
+ if (CMAKE_USE_PTHREADS_INIT)
+ list(APPEND SRC_REGRESS test/regress_thread.c)
+ endif()
+
+ if (ZLIB_LIBRARY)
+ list(APPEND SRC_REGRESS test/regress_zlib.c)
+ endif()
+
+ if (OPENSSL_LIBRARIES)
+ list(APPEND SRC_REGRESS test/regress_ssl.c)
+ endif()
+
+ add_executable(regress ${SRC_REGRESS})
+ # While building the test suite we don't want the visibility
+ # header trying to "dllimport" the symbols on windows (it
+ # generates a ton of warnings due to different link
+ # attributes for all of the symbols)
+ SET_TARGET_PROPERTIES(regress PROPERTIES COMPILE_DEFINITIONS
+ "EVENT_BUILDING_REGRESS_TEST=1")
+
+ target_link_libraries(regress ${LIB_APPS} ${LIB_PLATFORM})
+ else()
+ message(WARNING "No suitable Python interpreter found, cannot generate regress tests!")
+ endif()
+ endif()
+
+ #
+ # Test programs.
+ #
+ set(TESTPROGS test-changelist
+ test-eof
+ test-fdleak
+ test-init
+ test-time
+ test-weof)
+
+ set(ALL_TESTPROGS ${TESTPROGS} test-dumpevents test-ratelim)
+
+ # Create test program executables.
+ foreach (TESTPROG ${ALL_TESTPROGS})
+ add_executable(${TESTPROG} test/${TESTPROG}.c)
+ target_link_libraries(${TESTPROG} event_extra ${LIB_PLATFORM})
+ add_dependencies(${TESTPROG} event_extra)
+ endforeach()
+
+ #
+ # We run all tests with the different backends turned on one at a time.
+ #
+
+ # Add event backends based on system introspection result.
+ set(BACKENDS "")
+
+ if (EVENT__HAVE_EPOLL)
+ list(APPEND BACKENDS EPOLL)
+ endif()
+
+ if (EVENT__HAVE_SELECT)
+ list(APPEND BACKENDS SELECT)
+ endif()
+
+ if (EVENT__HAVE_POLL)
+ list(APPEND BACKENDS POLL)
+ endif()
+
+ if (EVENT__HAVE_KQUEUE)
+ list(APPEND BACKENDS KQUEUE)
+ endif()
+
+ if (EVENT__HAVE_EVENT_PORTS)
+ list(APPEND BACKENDS EVPORT)
+ endif()
+
+ if (EVENT__HAVE_DEVPOLL)
+ list(APPEND BACKENDS DEVPOLL)
+ endif()
+
+ if (WIN32)
+ list(APPEND BACKENDS WIN32)
+ endif()
+
+ message("Available event backends: ${BACKENDS}")
+
+ # Default environment variables turns off all event systems,
+ # then we enable each one, one at a time when creating the tests.
+ set(DEFAULT_TEST_ENV_VARS "EVENT_SHOW_METHOD=1;")
+ foreach(BACKEND ${BACKENDS})
+ set(BACKEND_ENV_VAR "EVENT_NO${BACKEND}=1")
+ list(APPEND DEFAULT_TEST_ENV_VARS "${BACKEND_ENV_VAR}")
+ endforeach()
+
+ # Macro that creates the ctest test for a backend.
+ macro(add_backend_test BACKEND_TEST_NAME ENV_VARS)
+ set(TEST_NAMES "")
+
+ foreach (TESTPROG ${TESTPROGS})
+ set(TEST_NAME ${TESTPROG}__${BACKEND_TEST_NAME})
+ add_test(${TEST_NAME} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TESTPROG})
+ list(APPEND TEST_NAMES ${TEST_NAME})
+ set_tests_properties(${TEST_NAME} PROPERTIES ENVIRONMENT "${ENV_VARS}")
+ endforeach()
+
+ # Dump events test.
+ if (__FOUND_USABLE_PYTHON)
+ set(TEST_NAME test-dumpevents__${BACKEND_TEST_NAME})
+ add_test(${TEST_NAME} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-dumpevents | ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/check-dumpevents.py)
+ set_tests_properties(${TEST_NAME} PROPERTIES ENVIRONMENT "${ENV_VARS}")
+ else()
+ message(WARNING "test-dumpevents will be run without output check since python was not found!")
+ set(TEST_NAME test-dumpevents__${BACKEND_TEST_NAME}_no_check)
+ add_test(${TEST_NAME} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-dumpevents)
+ set_tests_properties(${TEST_NAME} PROPERTIES ENVIRONMENT "${ENV_VARS}")
+ endif()
+
+ # Regress tests.
+ if (NOT EVENT__DISABLE_REGRESS AND __FOUND_USABLE_PYTHON)
+ set(TEST_NAME regress__${BACKEND_TEST_NAME})
+ add_test(${TEST_NAME} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/regress)
+ set_tests_properties(${TEST_NAME} PROPERTIES ENVIRONMENT "${ENV_VARS}")
+ endif()
+ endmacro()
+
+ # Add the tests for each backend.
+ foreach(BACKEND ${BACKENDS})
+ # Enable this backend only.
+ set(BACKEND_ENV_VARS ${DEFAULT_TEST_ENV_VARS})
+ list(REMOVE_ITEM BACKEND_ENV_VARS EVENT_NO${BACKEND}=1)
+
+ # Epoll has some extra settings.
+ if (${BACKEND} STREQUAL "EPOLL")
+ add_backend_test(timerfd_${BACKEND} "${BACKEND_ENV_VARS};EVENT_PRECISE_TIMER=1")
+ add_backend_test(changelist_${BACKEND} "${BACKEND_ENV_VARS};EVENT_EPOLL_USE_CHANGELIST=yes")
+ add_backend_test(timerfd_changelist_${BACKEND} "${BACKEND_ENV_VARS};EVENT_EPOLL_USE_CHANGELIST=yes;EVENT_PRECISE_TIMER=1")
+ else()
+ add_backend_test(${BACKEND} "${BACKEND_ENV_VARS}")
+ endif()
+ endforeach()
+
+ #
+ # Rate limiter tests.
+ #
+
+ # Group limits, no connection limit.
+ add_test(test-ratelim__group_lim ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-ratelim -g 30000 -n 30 -t 100 --check-grouplimit 1000 --check-stddev 100)
+
+ # Connection limit, no group limit.
+ add_test(test-ratelim__con_lim ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-ratelim -c 1000 -n 30 -t 100 --check-connlimit 50 --check-stddev 50)
+
+ # Connection limit and group limit.
+ add_test(test-ratelim__group_con_lim ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-ratelim -c 1000 -g 30000 -n 30 -t 100 --check-grouplimit 1000 --check-connlimit 50 --check-stddev 50)
+
+ # Connection limit and group limit with independent drain.
+ add_test(test-ratelim__group_con_lim_drain ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-ratelim -c 1000 -g 35000 -n 30 -t 100 -G 500 --check-grouplimit 1000 --check-connlimit 50 --check-stddev 50)
+
+ # Add a "make verify" target, same as for autoconf.
+ # (Important! This will unset all EVENT_NO* environment variables.
+ # If they are set in the shell the tests are running using simply "ctest" or "make test" will fail)
+ if (WIN32)
+ # Windows doesn't have "unset". But you can use "set VAR=" instead.
+ # We need to guard against the possibility taht EVENT_NOWIN32 is set, and all test failing
+ # since no event backend being available.
+ file(TO_NATIVE_PATH ${CMAKE_CTEST_COMMAND} WINDOWS_CTEST_COMMAND)
+
+ file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/tmp/verify_tests.bat
+ "
+ set EVENT_NOWIN32=
+ \"${WINDOWS_CTEST_COMMAND}\"
+ ")
+
+ message("${WINDOWS_CTEST_COMMAND}")
+ file(COPY ${CMAKE_CURRENT_BINARY_DIR}/tmp/verify_tests.bat
+ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
+ FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
+
+ file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}/verify_tests.bat" VERIFY_PATH)
+
+ add_custom_target(verify COMMAND "${VERIFY_PATH}"
+ DEPENDS event ${ALL_TESTPROGS})
+ else()
+ # On some platforms doing exec(unset) as CMake does won't work, so make sure
+ # we run the unset command in a shell instead.
+ # First we write the script contents.
+ file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/tmp/verify_tests.sh
+ "
+ #!/bin/bash
+ unset EVENT_NOEPOLL; unset EVENT_NOPOLL; unset EVENT_NOSELECT; unset EVENT_NOWIN32; unset EVENT_NOEVPORT; unset EVENT_NOKQUEUE; unset EVENT_NODEVPOLL
+ ${CMAKE_CTEST_COMMAND}
+ ")
+
+ # Then we copy the file (this allows us to set execute permission on it)
+ file(COPY ${CMAKE_CURRENT_BINARY_DIR}/tmp/verify_tests.sh
+ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
+ FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
+
+ # Create the target that runs the script.
+ add_custom_target(verify COMMAND ${CMAKE_CURRENT_BINARY_DIR}/verify_tests.sh
+ DEPENDS event ${ALL_TESTPROGS})
+ endif()
+
+ if (NOT EVENT__DISABLE_REGRESS AND __FOUND_USABLE_PYTHON)
+ add_dependencies(verify regress)
+ endif()
+
+ if (EVENT__COVERAGE)
+ include(CodeCoverage)
+
+ setup_target_for_coverage(
+ verify_coverage # Coverage target name "make verify_coverage"
+ make # Test runner.
+ coverage # Output directory.
+ verify) # Arguments passed to test runner. "make verify"
+ endif()
+
+ enable_testing()
+
+ include(CTest)
+endif()
+
+#
+# Installation preparation.
+#
+
+# Allow the user to override installation directories.
+set(EVENT_INSTALL_LIB_DIR lib CACHE PATH "Installation directory for libraries")
+set(EVENT_INSTALL_BIN_DIR bin CACHE PATH "Installation directory for executables")
+set(EVENT_INSTALL_INCLUDE_DIR include CACHE PATH "Installation directory for header files")
+
+if(WIN32 AND NOT CYGWIN)
+ set(DEF_INSTALL_CMAKE_DIR cmake)
+else()
+ set(DEF_INSTALL_CMAKE_DIR lib/cmake/libevent)
+endif()
+
+set(EVENT_INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH "Installation directory for CMake files")
+
+# Make sure the paths are absolute.
+foreach(p LIB BIN INCLUDE CMAKE)
+ set(var EVENT_INSTALL_${p}_DIR)
+ if(NOT IS_ABSOLUTE "${${var}}")
+ set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
+ endif()
+endforeach()
+
+# Export targets (This is used for other CMake projects to easily find the libraries and include files).
+export(TARGETS event event_extra event_core
+ FILE "${PROJECT_BINARY_DIR}/LibeventTargets.cmake")
+export(PACKAGE libevent)
+
+# Generate the config file for the build-tree.
+set(EVENT__INCLUDE_DIRS
+ "${PROJECT_SOURCE_DIR}/include"
+ "${PROJECT_BINARY_DIR}/include")
+set(LIBEVENT_INCLUDE_DIRS ${EVENT__INCLUDE_DIRS} CACHE PATH "Libevent include directories")
+configure_file(${PROJECT_SOURCE_DIR}/cmake/LibeventConfig.cmake.in
+ ${PROJECT_BINARY_DIR}/LibeventConfig.cmake
+ @ONLY)
+
+# Generate the config file for the installation tree.
+file(RELATIVE_PATH
+ REL_INCLUDE_DIR
+ "${EVENT_INSTALL_CMAKE_DIR}"
+ "${EVENT_INSTALL_INCLUDE_DIR}") # Calculate the relative directory from the Cmake dir.
+
+# Note the EVENT_CMAKE_DIR is defined in LibeventConfig.cmake.in,
+# we escape it here so it's evaluated when it is included instead
+# so that the include dirs are givenrelative to where the
+# config file is located.
+set(EVENT__INCLUDE_DIRS
+ "\${EVENT_CMAKE_DIR}/${REL_INCLUDE_DIR}")
+configure_file(${PROJECT_SOURCE_DIR}/cmake/LibeventConfig.cmake.in
+ ${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/LibeventConfig.cmake
+ @ONLY)
+
+# Generate version info for both build-tree and install-tree.
+configure_file(${PROJECT_SOURCE_DIR}/cmake/LibeventConfigVersion.cmake.in
+ ${PROJECT_BINARY_DIR}/LibeventConfigVersion.cmake
+ @ONLY)
+
+# Define the public headers.
+set_target_properties(event event_core event_extra
+ PROPERTIES PUBLIC_HEADER "${HDR_PUBLIC}")
+
+#
+# Install targets.
+#
+install(TARGETS event event_core event_extra
+ EXPORT LibeventTargets
+ RUNTIME DESTINATION "${EVENT_INSTALL_BIN_DIR}" COMPONENT bin
+ LIBRARY DESTINATION "${EVENT_INSTALL_LIB_DIR}" COMPONENT lib
+ ARCHIVE DESTINATION "${EVENT_INSTALL_LIB_DIR}" COMPONENT lib
+ PUBLIC_HEADER DESTINATION "${EVENT_INSTALL_INCLUDE_DIR}/event2" COMPONENT dev)
+# Install compat headers
+install(FILES ${HDR_COMPAT}
+ DESTINATION "${EVENT_INSTALL_INCLUDE_DIR}"
+ COMPONENT dev)
+
+# Install the configs.
+install(FILES
+ ${PROJECT_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/LibeventConfig.cmake
+ ${PROJECT_BINARY_DIR}/LibeventConfigVersion.cmake
+ DESTINATION "${EVENT_INSTALL_CMAKE_DIR}" COMPONENT dev)
+
+
+# Install exports for the install-tree.
+install(EXPORT LibeventTargets
+ DESTINATION "${EVENT_INSTALL_CMAKE_DIR}" COMPONENT dev)
+
+set(LIBEVENT_LIBRARIES event event_core event_extra CACHE STRING "Libevent libraries")
diff --git a/ChangeLog b/ChangeLog
index d1c92749..f4f73ebc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,619 @@
-Changes in version 2.0.22-stable (?? Dec 2014)
-
- (As of 3c7d6fcaff330ff0f18e776da84ed836bf580d45)
+Changes in version 2.1.5-alpha (?? Dec 2014)
+
+ (As of 7fd4941465e42ec46721150e7ccf1aa2d2bf670b)
+
+ New APIs (evconnlistener)
+ o Provide support for SO_REUSEPORT through LEV_OPT_REUSABLE_PORT (b625361 Maciej Soltysiak)
+
+ Bugfixes (core)
+ o Fix use-after-free error in EV_CLOSURE_EVENT callback (3cc0eac John Ohl)
+
+ Bugfixes (evbuffer)
+ o Fix evbuffer_peek() with len==-1 and start_at non-NULL. (ba59923)
+ o Consistently check for failure from evbuffer_pullup() (60f8f72)
+
+ Bugfixes (windows, IOCP)
+ o be async: avoid double close() (f133b86 Azat Khuzhin)
+
+ Bugfixes (bufferevents)
+ o Fix issue #127, double free for filterevents that use BEV_OPT_CLOSE_ON_FREE (2c82aa0 John Ohl)
+ o make bufferevent_getwatermark api more robust (a21e510 ufo2243)
+ o [Bugfix] fix bufferevent setwatermark suspend_read (b34e4ac ufo2243)
+ o bufferevent_openssl: reset fd_is_set when setfd with -1 is called (3da84c2 Azat Khuzhin)
+ o Fix compilation for older OpenSSL versions. (5c7282f Joakim Soderberg)
+
+ New APIs (evhttp)
+ o Add evhttp_connection_set_family() to set addrinfo->family for DNS requests (12c29b0 Azat Khuzhin)
+ o Implement interface that provides the ability to have an outbound evhttp_connection free itself once all requests have completed (2b9ec4c,10fe4f John Ohl)
+
+ Bugfixes (evdns)
+ o Load hosts file on Windows. (a0b247c Vilmos Nebehaj)
+ o Don't truncate hosts file path on Windows. (d0dc861 Vilmos Nebehaj)
+ o Fix a crash in evdns related to shutting down evdns (9f39c88,e8fe749)
+ o evdns: avoid read-after-free in evdns_request_timeout_callback() (61262a0 Azat Khuzhin)
+ o Correctly handle allocation failures in evdns_getaddrinfo (6a53d15)
+ o evdns: fix EVDNS_BASE_DISABLE_WHEN_INACTIVE in case retransmit/retry (74d0eee Azat Khuzhin)
+ o evdns: add retry/reissue tests for EVDNS_BASE_DISABLE_WHEN_INACTIVE (3ca9d43 Azat Khuzhin)
+ o evdns: fail ns after we are failing/retrasmitting request (97c750d Azat Khuzhin)
+
+ Bugfixes (evhttp)
+ o http: reset connection before installing retry timer (fix http retries handling) (bc79cc5 Azat Khuzhin)
+
+
+ Testing
+ o regress_dns: fix leaks in getaddrinfo_async{,_cancel_stress} tests (2fdc5f2 Azat Khuzhin)
+ o test: add family argument for http_connection_test_() (177b8a7 Azat Khuzhin)
+ o test: add regress for evhttp_connection_set_family() with AF_INET and AF_UNSPEC (42aefeb Azat Khuzhin)
+ o test/http: add regress test for set family to AF_INET6 (3fbf3cc Azat Khuzhin)
+ o Update to a more recent tinytest_macros. (8da5a18)
+ o test/regress: add simplestsignal: to track reorder bugs separately (b897bef Azat Khuzhin)
+ o test/evbuffer_peek: add regress in case we have first buffer greater (e2d139d Azat Khuzhin)
+ o More evbuffer_peek() test cases (154006a)
+ o use correct tt macro for pointer compare (08c88ea)
+ o regress_buffer: fix 'memcmp' compare size (79800df Maks Naumov)
+ o Fix a use-after-free in unit tests. CID 752027 (3739057)
+ o Fix a dead-code warning in unit tests. CID 1193548 (c119f24)
+ o Use evutil_weakrand() in unit tests. (a677b72, 364c110)
+ o Use a more precise calculation for max in time-ratelim.c (ca5b5c7)
+ o Make a buffer larger in the tests to avoid a scary evbuffer_copyout_from() (fb57b8b)
+ o Fix several memory leaks in the unit tests. (89c1a3b)
+ o Add test for evhttp_connection_free_on_completion (b0e9924 John Ohl)
+
+ Sample code
+ o Make http-server.c output into good html5 (6d72bdc)
+ o Use FindClose for handle from FindFirstFile in http-server.c (6466e88)
+ o https-client: add -retries argument, for connection retries (d9da844 Azat Khuzhin)
+
+ Bugfixes (build)
+ o Add missing headerfile for cmake (15d90cc Trond Norbye)
+ o ignore one more test binary (b6593aa Michael Richardson)
+ o ignore config.cache/test-driver files (c83f333 Mike Frysinger)
+ o add a --disable-samples configure flag (0c492b3 Mike Frysinger)
+ o Add a few files created by "make verify" to .gitignore. (1a8295a Pierre Phaneuf)
+ o updates in cmake build (27bd9fa Sergey Nikulov)
+ o Fix cmake error when the Module path has more than one entry. (befbd13 Acer Yang)
+
+Bugfixes (miscellaneous)
+ o tree.h: drop duplicated content of tree.h (6193187 Azat Khuzhin)
+ o evdns: disable probing with EVDNS_BASE_DISABLE_WHEN_INACTIVE (610410b,ad0493e,fea86a6,d83b337,5ca9e97 Azat Khuzhin)
+ o [Bugfix] fix grammer error (3a4d249 ufo2243)
+ o Change return type of evutil_load_windows_system_library_ to HMODULE (f691389)
+ o Fix a c90 warning (76643dd)
+ o Fix a typo in a doxygen comment. Reported by 亦得. (be1aeff)
+ o remove trailing comma from enum (b361b8a Jean-Philippe Ouellet)
+
+ Bugfixes (FreeBSD)
+ o Handle ENOTCAPABLE from FreeBSD - this is returned if an event in the changelist is for an FD that has been closed. (6fd7394 Adrian Chadd)
+
+
+
+Changes in version 2.1.4-alpha (21 Mar 2014)
+
+ Libevent 2.1.4-alpha adds a number of new miscellaneous APIs to make
+ Libevent more useful, including support for early close detection with
+ epoll via EPOLLRDHUP, triggering bufferevent callbacks, adding more
+ evhttp callbacks, and more. There are also numerous bugfixes, including
+ a number for finalize-related issues from 2.1.3-alpha; and an
+ alternative (non-primary!) cmake-based build mechanism.
+
+ New APIs (core)
+ o Added event_base_get_num_events() (0fa107d Mobai Zhang)
+ o New event_base_active_by_fd API (865a142 Greg Hazel, 5c9da9a, 87fa2b0)
+ o Add event_base_active_by_signal by analogy (4865943)
+ o Add access to max event count stats (5173bef, efbd3dc, 26230a2
+ Andrew Sweeney)
+ o Implemented EV_CLOSED event for epoll backend
+ (EPOLLRDHUP). (b1b69ac Diego Giagio, 53d2793, 43ffcf6, dfe1e52
+ Marcin Juszkiewicz, ff26633 Joakim Soderberg, 3908a5e)
+
+ New APIs (evutil_secure_rng)
+ o Add evutil_secure_rng_set_urandom_device_file (2bbb5d7)
+
+ New APIs (bufferevents)
+ o Add function to fetch underlying ratelimit cfg (4b3d5af Mark Ellzey)
+ o Pass and return const for bufferevent_get_token_bucket_cfg (1c77fbb
+ Mark Ellzey)
+ o Add watermark introspection (4ce242b Ondřej Kuzník)
+ o Add an option to trigger bufferevent I/O callbacks (61ee18b Ondřej Kuzník)
+ o Add an option to trigger bufferevent event callbacks (a7384c7
+ Ondřej Kuzník)
+ o Clarifications in response to merge req. comments (bd41947 Ondřej
+ Kuzník)
+ o Minor optimizations on bufferevent_trigger options (a3172a4)
+
+ New APIs (evhttp)
+ o Add evhttp_connection_get_server(). (a7f82a3 Maxime Henrion)
+ o add a http default content type option (5a5acd9 Nicolas Martyanoff)
+ o http: implement new evhttp_connection_get_addr() api. (0c7f040 Azat
+ Khuzhin)
+ o Add a variant of evhttp_send_reply_chunk() with a callback on
+ evhttp_write_buffer() (8d8decf Julien BLACHE)
+ o Allow registering callback for parsing HTTP headers (b0bd7fe Balint Reczey)
+ o Provide on request complete callback facility (b083ca0 Andrew Sweeney)
+ o evhttp_request_set_on_complete_cb to be more specific about what
+ the function actually does and usage (da86dda Andrew Sweeney)
+ o Update unit test to make sure that the callback happens after the
+ output data is written (b85f398 Andrew Sweeney)
+
+ Features (evdns)
+ o bug fix for issues #293 evdns_base_load_hosts doesn't remove
+ outdated addresses (954d2f9, f03d353, 45eba6f Kuldeep Gupta)
+
+ Features: (cmake build support)
+ o Initial CMake commit. (e415196 Joakim Soderberg)
+ o Add all tests and benchmarks to CMake project. (e9fc014 Joakim Soderberg)
+ o More work on adding tests to CMake project (99c1dc3 Joakim Soderberg)
+ o Generate a dummy evconfig-private.h so things build
+ properly. (ce14def Joakim Soderberg)
+ o Link libm on unix platforms. (58fcd42 Joakim Soderberg)
+ o Added some GCC specific options. (19222e5 Joakim Soderberg)
+ o Use evutil_closesocket instead. (dbf2b51 Joakim Soderberg)
+ o Add copyright and licensing files for CMake modules. (c259d53
+ Joakim Soderberg)
+ o Only include WIN32 getopt where it is used. (9bbce0b Joakim Soderberg)
+ o Fix bench_cascade program on Windows. (78da644 Joakim Soderberg)
+ o Don't segfault on no found event backend. (8f2af50 Joakim Soderberg)
+ o Only test the event backends available on the system. (7ea4159
+ Joakim Soderberg)
+ o Added a "make verify" target. (e053c4f Joakim Soderberg)
+ o Fix the make "verify" target on Windows. (67e5d74 Joakim Soderberg)
+ o Get rid of deprecation warnings for OpenSSL on OSX 10.7+ (69c3516
+ Joakim Söderberg)
+ o Fix kqueue support. (a831f2f Joakim Söderberg)
+ o Added a test for testing if kqueue works with pipes. (2799b35
+ Joakim Söderberg)
+ o Change the BSD license from 4 to 3-clause. (86df3ed Joakim Soderberg)
+ o Minimum required python version is 2.4. (968e97b Joakim Soderberg)
+ o Get rid of unknown pragma warnings. (0ef1d04 Joakim Soderberg)
+ o Add a "make verify_coverage" target generation coverage
+ info. (f2483f8 Joakim Soderberg)
+ o Fix the "make verify" target on NetBSD (4ac086a Joakim Soderberg)
+ o Only look for ZLib when it is used (if tests are
+ included). (f780593 Joakim Soderberg)
+ o Added EVENT__ENABLE_GCC_WARNINGS, turns all warnings into
+ errors. (dd413bd Joakim Soderberg)
+ o Add CMake config and install targets. (f3446ed Joakim Soderberg)
+ o Fix typo (4b754df Joakim Soderberg)
+ o Some work on making it possible to simply do add_subdirectory() on
+ the project. (49ab363 Joakim Soderberg)
+ o Set USE_DEBUG=1 on EVENT__ENABLE_VERBOSE_DEBUG (fd42e70 Joakim Soderberg)
+ o Fix so that old nmake project still builds. (24d6466 Joakim
+ Soderberg)
+ o Rename README to README.md and use markdown to format. (d2bc39a
+ Joakim Soderberg)
+ o Update README with CMake build instructions. (604b8cc Joakim Soderberg)
+ o Clean up the README some. (8d4cb35 JoakimSoderberg)
+ o Forgotten headers for old nmake project compatability. (8697b99
+ Joakim Soderberg)
+ o Change all uses of WIN32 to _WIN32 (4e14395 Joakim Söderberg)
+ o Fix include bug. (2024467 Joakim Söderberg)
+ o Check if we're on OSX before disabling deprecation in le-proxy
+ (8b40a5b Joakim Söderberg)
+ o Fix broken autotools build. (ae1bd82 Joakim Söderberg)
+ o Disclaimerize cmake a little in the README (d03b5bf)
+ o Fix CMake compile when OpenSSL is disabled. (e423d42 Joakim
+ Söderberg)
+ o CMake: Get rid of python not found warning when regress tests
+ turned off. (d38d798 Joakim Söderberg)
+ o Fix https-client compilation on Windows. (d7be788 Joakim Soderberg)
+ o Guard against EVENT_NOWIN32 being set during testing. (f1715b4
+ Joakim Soderberg)
+ o Check for OSX when checking for clang. (e212c54 Joakim Soderberg)
+ o Added a Travis-CI configuration file. (8c0f0a9 Joakim Soderberg)
+ o Added -Qunused-arguments for clang on macosx (ed99d92 Trond Norbye)
+ o Rename event_extras to event_extra (a0dd5df Trond Norbye)
+ o Add option to build shared library (4545fa9 Trond Norbye)
+ o Add -Qunused-arguments for clang on macos (b56611d Trond Norbye)
+ o Add cmake-related files to .gitignore (e061321 Trond Norbye)
+ o Export event_extra not event_extras. (2b41bcf Joakim Söderberg)
+
+ Bugfixes (core)
+ o If evsel->del() fails, don't leave the evmap in an inconsistent
+ state (9b5a527 Maxime Henrion)
+ o Move event_debug_note_teardown_ before mm_free. (69b5c64)
+ o Check CLOCK_MONOTONIC_* at runtime if needed. (911abf3)
+ o Fix reinit of fds with EV_WRITE but not EV_READ. (ebfd8a8 maksqwe)
+ o Tweaked callbacks to prevent race condition
+ (https://github.com/libevent/libevent/issues/104) (40830f1, 2ea15ed
+ John Ohl)
+ o Move assert(ev) to before we use ev in EV_CLOSURE_EVENT_FINALIZE
+ case (9805972)
+
+ Bugfixes (evhttp)
+ o Fix a double close() bug in evhttp when the underlying bufferevent uses
+ BEV_OPT_CLOSE_ON_FREE. (31db8a0 Maxime Henrion)
+ o Fix an unlikely but possible error case for http connections (f22049e)
+ o Avoid racy bufferevent activation (5eb1788 Nate Rosenblum)
+
+ Bugfixes on 2.0 (Windows)
+ o Use windows vsnprintf fixup logic on all windows environments (e826f19)
+ o libevent/win32_dealloc() : fix sizeof(pointer) vs sizeof(*pointer)
+ (b8f5980 Frank Denis)
+
+ Bugfixes (evutil_secure_rng)
+ o When we seed from /proc/sys/kernel/random/uuid, count it as success
+ (e35b540)
+ o We should return after arc4random_buf() (1ea1f26 Makoto Kato)
+ o Avoid other RNG initialization FS reads when urandom file is
+ specified (9695e9c)
+ o Really remove RNG seeds from the stack (f5ced88)
+ o Fix another arc4random_buf-related warning (e64a2b0)
+
+ Bugfixes (bufferevents)
+ o Initialize async bufferevent timeout CBs unconditionally (af9b2a7)
+
+ Bugfixes (evdns)
+ o Checking request nameserver for NULL, before using it. (5c710c0
+ Belobrov Andrey)
+ o Fix SEGFAULT after evdns_base_resume if no nameservers
+ installed. (14971a8 Azat Khuzhin)
+ o Actually use the log facility for reporting evdns problems. (e1766a1)
+ o Fix SEGFAULT after evdns_base_resume if no nameservers
+ installed. (f8d7df8 Azat Khuzhin)
+ o fix for ServFail from RIPE Atlas release (62f596b Antony Antony)
+
+ Bugfixes (compilation)
+ o Fix test compilation with nmake: add the gdi.lib dependency (5ba8ab7)
+ o Whoops. It is gdi.lib, not gdi32.lib. (github issue #61) (8ab612e)
+ o Don't use return since return type is void and build error occurs
+ using clang (838161d Makoto Kato)
+ o Use void casts to suppress some "unchecked return value" warns (7080d55)
+ o rpcgen: Generate regress.gen.[c,h] in build rather than src dir
+ (243386c Ross Lagerwall)
+ o Fix a compiler warning when checking for arc4random_buf linker
+ breakage. (5cb3865)
+ o Fix 'make distcheck' by adding regress.gen.[ch] to DISTCLEANFILES
+ (239d834)
+
+ o Fix a c90 warning (c207682)
+ o Fix consts in WIN32-Code/getopt*.[ch] (57abb35)
+
+ Bugfixes (locks, synchronization)
+ o Missed lock acquire/release in event_base_cancel_single_callback_()
+ (d3d999a Azat Khuzhin)
+ o Fix locking in bufferevent_get_options_(). (dbc9cd4 Maxime Henrion)
+
+ Bugfixes (leaks)
+ o Avoid leaking segment mappings when offset is not a page multiple (d409514)
+
+ Testing
+ o Add tests for evdns_base_resume(). (1cd9ff5 Azat Khuzhin)
+ o Fix dns/leak_resume_send_err test. (7e876df Azat Khuzhin)
+ o Add checks for evhttp_connection_get_server() in unit
+ tests. (fbc323b Maxime Henrion)
+ o Fix a (failure-only) null dereference in the unit tests (1104d0b)
+ o Fix a logic error in test_evbuffer_freeze (7765884)
+ o Add missing check to test_evbuffer_file_segment_add_cleanup_cb (eba4506)
+ o Fix some crash-on-fail cases in DNS regression tests (87cd6f0)
+ o DNS tests: add a missing check (f314900)
+ o Finalize tests: add a missing check (82b6956)
+ o test_evutil_rtrim: add another missing check. (e193c95)
+ o regress_main: logging all if env EVENT_DEBUG_LOGGING_ALL isset
+ (611e28b Azat Khuzhin)
+ o regress_http: add tests for evhttp_connection_get_addr() (4dd500c
+ Azat Khuzhin)
+ o Update to the latest version of tinytest (7a80476)
+ o Heap-allocate zlib data structure in regress_zlib tests (4947c18)
+
+ Performance tweaks (core)
+ o Avoid redundant syscall to make a nonblocking socket nonblocking
+ (42c03da Maxime Henrion)
+ o Avoid redundant syscall if making a socket cloexec twice (1f29b18)
+ o Avoid redundant invocations of init_extension_functions for IOCP (3b77d62)
+
+ Documentation
+ o Document that arc4random is not a great cryptographic PRNG. (6e49696)
+ o Small doxygen tweaks (6e67b51)
+ o Try another doxygen tweak (ccf432b)
+ o Clarify event_base_loop exit conditions (031a803)
+ o Fix a typo (be7bf2c Ondřej Kuzník)
+ o Document deferred eventcb behaviour (13a9a02 Ondřej Kuzník)
+ o Typo fixes from Linus Nordberg (cec62cb, 8cd695b)
+ o Fix duplicate paragraph in evbuffer_ptr documentation (58408ee)
+
+ Code Improvements (coverity)
+ o Fix a pile of coverity warnings in the unit tests (867f401)
+ o Fix coverity warnings in benchmark tools. (ff7f739)
+ o Whoops; fix compilation in bench.c (544cf88)
+ o Remove spurious checks in evrpc.c error cases (coverity) (991b362)
+ o Fix a couple of compilation warnings in regress_http.c (860767e)
+ o Fix even more coverity warnings. (d240328)
+ o Stop checking for inet_aton; we don't use it. (f665d5c)
+ o Add an include to evrpc-internal to fix openbsd compilation warning
+ (5e161c6)
+
+ Cleanups
+ o Remove an unreachable return statement in minheap-internal.h (e639a9e)
+ o Refactor evmap_{io,signal}_active_() to tolerate bad inputs (974c60e)
+ o Fix needless bufferevent includes in evdns.c (254c04e)
+ o Fix a couple of "#ifdef WIN32" instances (88ecda3)
+ o Remove unneeded declaration in bufferevent-internal.h (4c8ebcd)
+
+ Sample code
+ o le-proxy: Fail more gracefully if opening listener fails (44b2491)
+ o http-server: drop uri_root from base_url in http-server. (6171e1c Azat Khuzhin)
+ o https-client: POST supported, args supported (c5887f7 Alexey Ozeritsky)
+ o https-client: code cleanup (29af65e Alexey Ozeritsky)
+ o https-client: Small tweaks to https-client.c (90786eb)
+ o https-client: Set hostname for SNI extension (by f69m) (d1976f8)
+ o https-client: add a cast to https-client.c (462e6b6)
+
+
+
+Changes in version 2.1.3-alpha (1 May 2013)
+
+ Libevent 2.1.3-alpha fixes various bugs, adds new unit tests, and cleans
+ up the code in a couple of places. It has a new callback in evhttp for
+ reporting errors during a request, a new feature for allowing evdns to
+ not keep the event_base looping when there are no requests inflight, and
+ example code for writing an https client.
+
+ Libevent 2.1.3-alpha also has an important new (experimental) event
+ finalization feature to allow safe event teardown in multithreaded
+ programs. This ought to fix the longstanding bug with deadlocks in
+ multithreaded use of SSL-based bufferevents that some people have been
+ experiencing since Libevent 2.0.
+
+
+ Core (event finalization)
+ o Implement event_finalize() and related functions to avoid certain
+ deadlocks (8eedeab)
+ o Use finalization feature so bufferevents can avoid deadlocks (02fbf68)
+ o Always run pending finalizers when event_base_free() is called (e9ebef8)
+ o Remove bufferevent_del_generic_timeout_cbs as now unused (4ea4c6a)
+ o More documentation for finalization feature (a800b91)
+ o Make the event_finalize* functions return an error code (5d11f4f)
+ o Mark the finalize stuff as experiemental in case it needs to
+ change (23e2e29)
+
+ Evdns
+ o evdns: New flag to make evdns not prevent the event loop from
+ exiting (6b7fa62 Azat Khuzhin)
+
+ Bugfixes (Core)
+ o Make event_remove_timer behave correctly with persistent timers (5623e80)
+ o Unit test for event_remove_timer with EV_PERSIST. (96150dd)
+ o Double-check next timeout when adding events (9443868 Nate Rosenblum)
+ o event_base_update_cache_time should be a no-op if the loop isn't
+ running (5e6fa2a)
+
+ Bugfixes (evhttp, crash fix, from 2.0)
+ o fix #73 and fix http_connection_fail_test to catch it (b618204 Greg Hazel)
+
+ Bugfixes (compilation and portability, from 2.0)
+ o Fix compilation with WIN32_HAVE_CONDITION_VARIABLES enabled (7e45739)
+ o Fix missing AC_PROG_SED on older Autoconfs (9ab2b3f Tay Ray Chuan)
+ o Backport libevent to vanilla Autoconf 2.59 (as used in RHEL5)
+ (74d4c44 Kevin Bowling)
+ o Use AC_CONFIG_HEADERS in place of AM_CONFIG_HEADERS for autmake
+ 1.13 compat (817ea36)
+ o Rename configure.in to configure.ac to appease newer autoconfs (0c79787)
+ o Avoid using top_srcdir in TESTS: new automakes do not like this (a55514e)
+
+ Bugfixes (resource leaks/lock errors on error, from 2.0)
+ o Avoid leaking fds on evconnlistener with no callback set (69db261)
+ o Avoid double-close on getsockname error in evutil_ersatz_socketpair
+ (0a822a6)
+ o Fix a locking error in bufferevent_socket_get_dns_error. (0a5eb2e)
+
+ Documentation Fixes (from 2.0)
+ o Fix a mistake in evbuffer_remove() arguments in example http server code
+ (c322c20 Gyepi Sam)
+ o Fix a typo in a comment in buffer.h. Spotted by Alt_F4 (773b0a5)
+
+ Documentation Fixes
+ o minor documentation typos (809586a Patrick Pelletier)
+ o Fix cut-and-paste err in whatsnew-2.1 (49905ac)
+ o Fix comment to refer to sample/include.am correctly (9e8cdf3 Sebastian
+ Hahn)
+ o Fix typo : Dispatching instead of Dispaching (0c2bacc Volker Lendecke)
+ o fix some hinky indentation in evhttp_make_request (80e220e Patrick
+ Pelletier)
+ o "buffer" spelling (a452811 Patrick Pelletier)
+ o Specify return behavior in header for evbuffer_pullup() in corner case
+ (cf8d1cd Dan Petro)
+ o Clarify an important point about event_base_foreach_event() (920a5e6)
+
+ Compilation Fixes/Tool Support
+ o avoid valgrind false positive by zeroing epoll_event (1258614 Patrick
+ Pelletier)
+ o Fix harmless clang enum warning (b452a43 Sebastian Hahn)
+ o remove all exes on "make clean", not just regress.exe (974bfa0 Patrick
+ Pelletier)
+ o Make --disable-libevent-regress work again (787fd74)
+ o Do not build strlcpy.c when it will have no code. (4914620)
+
+ Portability Fixes
+ o When EWOULDBLOCK is not EAGAIN, treat it as equivalent to it (bf7a0ff)
+ o Preliminary changes for Minix3. (0dda56a Nicholas Heath)
+ o Use AC_CONFIG_HEADERS in place of AM_CONFIG_HEADERS for autmake 1.13
+ compat (bf278b)
+ o Avoid using $(top_srcdir) in TESTS. (2863c83)
+ o build test/test-script.sh on systems with a less-featureful $< (f935e21)
+ o Implement EVUTIL_ERR_IS_EAGAIN on windows. (42aaf4d)
+
+ Evhttp changes:
+ o Fix ipv6 support for http. When URL contain domain, not IP
+ address. (71e709c Azat Khuzhin)
+ o uri decode: fix for warning "use of uninitialised value" (64b6ece Azat
+ Khuzhin)
+ o uri decode: changed the test for the existence of the next character
+ (e1903e3 Azat Khuzhin)
+ o Move prototype of evhttp_decode_uri_internal() to http-internal.h
+ (de8101a Azat Khuzhin)
+ o Test: decoding just part of string with evhttp_decode_uri_internal()
+ (1367653 Azat Khuzhin)
+ o Add new error_cb for actual reporting of HTTP request errors. (7b07719
+ Azat Khuzhin)
+ o Add test for EVREQ_HTTP_REQUEST_CANCEL into http_cancel_test() (862c217
+ Azat Khuzhin)
+ o Drop extra header http_struct.h from regress_http.c (54cc800 Azat Khuzhin)
+
+ Testing
+ o Add regress test ipv6_for_domain. (9ec88bd Azat Khuzhin)
+ o Add an environment variable (EVENT_DEBUG_MODE) to run unit tests in debug
+ mode (2fad0f3)
+ o Add a test with an active_later event at event_base_free time. (1c3147f)
+ o Make all tests pass under EVENT_DEBUG_MODE=1 (b1b054f)
+ o Add some verbose notes to bufferevent unit tests (9d893c9)
+ o New test for active_later->active transition on event_active (a153874)
+ o New tests for event_base_foreach_event() (0b096ef)
+ o Unit tests for event_base_gettimeofday_cached() and
+ event_base_update_cache_time() (30ea291)
+ o A test for event_get_assignment() (f09629e)
+ o More unit tests for initializing common timeouts. (d596739)
+ o Fix a bug in the new main/event_foreach test (702c9aa)
+
+ Windows:
+ o use FormatMessage for winsock errors (0c6ec5d, 2078e9b, 4ccdd53, c9ad3af
+ Patrick Pelletier)
+ o a program to print out the error strings for winsock errors (7296512
+ Patrick Pelletier)
+ o Fix a warning introduced in 0c6ec5d8 (eeb700c)
+ o Fix another warning introduced in 0c6ec5d8 (ed26561)
+
+ Examples (http)
+ o Add sample/https-client.c, an example of stacking evhttp as a client on
+ top of bufferevent_ssl. (be46c99 Catalin Patulea)
+ o use ${OPENSSL_LIBS} instead of -lssl -lcrypto (bf31fa5 Patrick Pelletier)
+ o https-client was putting newlines at 256-byte boundaries (42d7441 Patrick
+ Pelletier)
+ o better handling of OpenSSL errors (5754d96 Patrick Pelletier)
+ o use Debian's default root certificate location (aacd674 Patrick Pelletier)
+ o use iSECPartners code to validate hostname in certificate (64d9f16
+ Patrick Pelletier)
+ o avoid sign mismatch warning in openssl_hostname_validation.c (6021cb5
+ Patrick Pelletier)
+ o pull in wildcard matching code from cURL (4db9da6 Patrick Pelletier)
+ o Another tweak to https-client.c (95acdaa)
+ o Remove http_struct.h usage in sample/https-client.c (8a90a85)
+
+
+
+Changes in version 2.1.2-alpha (18 Nov 2012)
+
+ Libevent 2.1.2-alpha includes more portable for monotonic timers,
+ refactors much of Libevent's internal and external infrastructure,
+ closes some longstanding gaps in the interface, makde other
+ improvements. Ths log below tries to organize features by rough area of
+ effect. It omits a few commits which were pure bugfixes on other commits
+ listed below. For more detail, see the git changelogs. For more
+ insight, see the "whatsnew-2.1.txt" document included in the Libevent
+ 2.1.2-alpha distribution.
+
+ Libevent 2.1.2-alpha also includes all changes made in 2.0.19-stable
+ through 2.0.21-stable inclusive.
+
+ Performance (core):
+ o Replace pipe-based notification with EVFILT_USER where possible. This
+ should make multithreaded programs on OSX and *BSD alert the main thread a
+ little faster. (53a07fe)
+ o Make th_base_lock nonrecursive. (9cd5acb)
+
+ New/Changed API Functions:
+ o New event_get_priority() function to return an event's priority (f90e255)
+ o Add a bufferevent_get_priority() function (bd39554)
+ o Add an event_base_loopcontinue() to tell Libevent to rescan for more
+ events right away (7d6aa5e)
+ o Add a new callback to get called on evbuffer_file_segment free
+ (e9f8feb yangacer, 64051b9)
+ o Expose event_base_foreach_event() as a public API. (84fd6d7 Roman
+ Puls, 232055e, ffe1643)
+ o Add an event_remove_timer() to remove timer on an event without
+ deleting it (e3b2e08)
+ o Make bufferevent_set_timeouts(bev, NULL, NULL) have plausible
+ semantics (9dee36b)
+ o Rename event_enable_lock_debuging() to ..._debugging(). (The old name
+ should still work.) (07e132e)
+ o Add missing implementation for event_enable_debug_logging (3b3e21d)
+
+ PORTABLE MONOTONIC TIMERS:
+
+ Libevent 2.1.2 includes internal support for monotonic timers on
+ (nearly) all supported platforms, including Windows, and OSX. Libevent
+ applications should now be more resilient to jumps forwards or backwards
+ in the system clock. Also, on Linux systems with epoll, we now
+ optionally support microsecond-level timeouts (whereas epoll only
+ supports millisecond-precision timeouts).
+
+ o Use mach_absolute_time() for monotonic clock support on OSX. (b8fd6f9)
+ o Do not track use_monotonic field when is no monotonic clock (cb653a0)
+ o EVENT_BASE_FLAG_PRECISE_TIMER indicates we want fine timer precision
+ (ddd69d3)
+ o On Linux, use CLOCK_MONOTONIC_COARSE by default (55780a7)
+ o Implement a GetTickCount-based monotonic timer for Windows (d5e1d5a)
+ o Refactor monotonic timer handling into a new type and set of
+ functions; add a gettimeofday-based ratcheting implementation (f5e4eb0)
+ o Add EVENT_PRECISE_TIMER environment var for selecting precise-but-slow
+ timer (a2598ec)
+ o Implement fast/precise monotonic clocks on Windows (2c47045)
+ o Simple unit tests for monotonic timers (630f077)
+ o Improve the monotonic-time unit test: make it check the step size (7428c78)
+ o When PRECISE_TIMERS is set with epoll, use timerfd for microsecond
+ precision (26c7582)
+ o Split out time-related evutil functions into a new evutil_time.c (c419485)
+ o Split out time-related prototypes into time-internal.h (71bca50)
+ o Add evutil_time.obj to Makefile.nmake (0ba0683)
+ o Avoid giving a spurious warning when timerfd support is unavailable
+ (1aaf9f0 Dave Hart)
+ o Make test_evutil_monotonic a little more tolerant (def3b83)
+ o Avoid unused-var warning on systems with clock_gettime but without
+ CLOCK_MONOTONIC_COARSE (9be5468)
+
+EVENT_BASE_ONCE LEAKS:
+ If a callback added by event_base_once() is never invoked, Libevent no
+ longer leaks internal memory.
+
+ o Free dangling event_once objects on event_base_free() (c17dd59)
+ o Add a unit test in which an event is created with event_base_once()
+ but never fires (4343edf)
+
+TESTING SUPPORT, FIXES AND IMPROVEMENTS:
+
+ Libevent now disables by default its unit tests that would touch the
+ network, or that tend to fail on heavily-loaded systems. To re-enable
+ them, invoke the ./test/regress program with the @all alias.
+
+ o Simplify test.sh code significantly. (9b856fd Ross Lagerwall)
+ o Make all tests that hit the network disabled by default (f2cea87)
+ o Avoid a resource leak on error in http client benchmark (ea92fba)
+ o Update to latest tinytest (911b4f0349377) (ef7c4f7)
+ o Avoid (unlikely) overflow in bench_httpclient.c (5671033)
+ o Shave 700 msec off the persistent_timeout_jump test (21205b8)
+ o Check return value of write() in regress.c (c8009d2)
+ o Make load-dependent monotonic timer tests off-by-default (2b6fe8b)
+ o Add deferred_cb_skew to list of timing-dependent tests (34c8f31)
+ o Avoid test -e; older shs don't have one. (f1bd938)
+ o Fix renegotiation test to work around openssl 1.0.1 bug (c2f3086)
+ o Fix a couple of compile warnings in the unit tests (5a9a014)
+
+MISC:
+ o Change evutil_weakrand_() to avoid platform random() (e86af4b Nicholas
+ Marriott, 3aa4415)
+
+INFRASTRUCTURE (Active-later events):
+ As a simplification and optimization to Libevent's "deferred callback"
+ logic (introduced in 2.0 to avoid callback recursion), Libevent now
+ treats all of its deferrable callback types using the same logic it uses
+ for active events. Now deferred events no longer cause priority
+ inversion, no longer require special code to cancel them, and so on.
+
+ o Refactor the callback part of an event into its own event_callback
+ type (cba59e5)
+ o Add "active later" event_callbacks to supersede deferred (745a63d)
+ o event_base_assert_ok: check value of event_active_count for
+ correctness (fec8bae)
+ o Replace deferred_cbs with event_callback-based implementation. (ae2b84b)
+ o Replace more deferred_cb names with event_callback (a4079aa)
+ o Give event_base_process_active a single exit path (581b5be)
+ o Restore our priority-inversion-prevention code with deferreds (c0e425a)
+ o Refactor event_persist_closure: raise and extract some common logic
+ (bec22b4)
+ o Remove the unused bits from EVLIST_ALL (9889a3d)
+||||||| merged common ancestors
+Changes in version 2.0.22-stable (?? Dec 2013)
+
+ (As of 3b77d62829c4393bda6f9105a5d3b73b48a64b71.)
BUGFIXES (evhttp)
o fix #73 and fix http_connection_fail_test to catch it (crash fix) (b618204 Greg Hazel)
@@ -16,7 +629,6 @@ BUGFIXES (compilation and portability)
o Use windows vsnprintf fixup logic on all windows environments (e826f19)
o Fix a compiler warning when checking for arc4random_buf linker breakage. (5cb3865)
o Fix another arc4random_buf-related warning (e64a2b0)
- o Add -Qunused-arguments for clang on macos (b56611d Trond Norbye)
BUGFIXES (resource leaks/lock errors on error)
o Avoid leaking fds on evconnlistener with no callback set (69db261)
@@ -24,28 +636,15 @@ BUGFIXES (resource leaks/lock errors on error)
o Fix a locking error in bufferevent_socket_get_dns_error. (0a5eb2e)
o libevent/win32_dealloc() : fix sizeof(pointer) vs sizeof(*pointer) (b8f5980 Frank Denis)
-BUGFIXES: (other stability)
- o bufferevent_pair: don't call downcast(NULL) (f2428a2)
- o Consistently check for failure from evbuffer_pullup() (60f8f72)
- o Fix race caused by event_active (3c7d6fc vjpai)
-
BUGFIXES (miscellaneous)
+ o Avoid other RNG initialization FS reads when urandom file is specified (9695e9c, bb52471)
o Avoid redundant invocations of init_extension_functions for IOCP (3b77d62)
- o Typo fixes from Linus Nordberg (cec62cb, 8cd695b)
- o Add a few files created by "make verify" to .gitignore. (1a8295a Pierre Phaneuf)
- o regress_buffer: fix 'memcmp' compare size (79800df Maks Naumov)
- o Fix bufferevent setwatermark suspend_read (b34e4ac ufo2243)
BUFGIXES (evdns)
o Checking request nameserver for NULL, before using it. (5c710c0 Belobrov Andrey)
o Fix SEGFAULT after evdns_base_resume if no nameservers installed. (f8d7df8 Azat Khuzhin)
- o Fix a crash in evdns related to shutting down evdns (9f39c88,e8fe749)
-
-BUGFIXES (epoll)
- o Check does arch have the epoll_create and __NR_epoll_wait syscalls. (dfe1e52 Marcin Juszkiewicz)
BUGFIXES (evutil_secure_random)
- o Avoid other RNG initialization FS reads when urandom file is specified (9695e9c, bb52471)
o When we seed from /proc/sys/kernel/random/uuid, count it as success (e35b540)
o Document that arc4random is not a great cryptographic PRNG. (6e49696)
o Add evutil_secure_rng_set_urandom_device_file (2bbb5d7)
@@ -55,8 +654,7 @@ BUGFIXES (evutil_secure_random)
DOCUMENTATION FIXES
o Fix a mistake in evbuffer_remove() arguments in example http server code (c322c20 Gyepi Sam)
o Fix a typo in a comment in buffer.h. Spotted by Alt_F4 (773b0a5)
- o Clarify event_base_loop exit conditions (031a803)
- o Use FindClose for handle from FindFirstFile in http-server.c (6466e88)
+
Changes in version 2.0.21-stable (18 Nov 2012)
@@ -434,960 +1032,361 @@ DOCUMENTATION:
o Add comment to configure.in to explain gc-sections test logic (c621359)
o Fix a couple of memory leaks in samples/http-server.c. Found by Dave Hart. (2e9f665)
-BUILD IMPROVEMENTS:
- o Use the gcc -ffunction-segments feature to allow gc when linking with static libevent (0965c56 Dave Hart)
- o Add configure options to disable installation, regression tests (49e9bb7 Dave Hart)
-
-
-
-Changes in version 2.0.10-stable (16 Dec 2010)
- [Autogenerated from the Git log, sorted and cleaned by hand.]
-BUGFIXES
- o Minor fix for IOCP shutdown handling fix (2599b2d Kelly Brock)
- o Correctly notify the main thread when activating an event from a subthread (5beeec9)
- o Reject overlong http requests early when Expect:100-continue is set (d23839f Constantine Verutin)
- o EVUTIL_ASSERT: Use sizeof() to avoid "unused variable" warnings with -DNDEBUG. (b63ab17 Evan Jones)
-
-CODE CLEANUPS
- o bufferevent-internal.h: Use the new event2/util.h header, not evutil.h (ef5e65a Evan Jones)
- o Use relative includes instead of system includes consistently. (fbe64f2 Evan Jones)
- o Make whitespace more consistent
-
-TESTING
- o tests: Use new event2 headers instead of old compatibility headers. (4f33209 Evan Jones)
-
-DOCUMENTATION
- o Document that the cpu_hint is only used on Windows with IOCP for now (57689c4)
- o Add stuff to "whats new in 2.0" based on reading include changes since August. (18adc3f)
-
-
-Changes in 2.0.9-rc (30 Nov 2010):
- [Autogenerated from the Git log, sorted and cleaned by hand.]
-NEW AND MODIFIED APIs
- o Add a function to change a listener's callback. (46ee061)
- o Make evbuffer_add_file take ev_off_t, not off_t (ac7e52d)
- o Make rate-limits go up to SIZE_MAX/EV_SSIZE_MAX, not just INT32_MAX (2cbb1a1)
- o Add a bufferevent_get_base function (aab49b6)
-
-MAJOR BUGFIXES
- o Disable changelist for epoll by default because of Linux dup() bug; add an option and/or an envvar to reenable it for speed. (9531763)
- o Fix a 100%-CPU bug where an SSL connection would sometimes never stop trying to write (1213d3d)
- o Fix a nasty bug related to use of dup() with epoll on Linux (c281aba)
- o Fix bugs in posix thread-id calculation when sizeof(pthread_t) != sizeof(long) (fbaf077)
- o Fix some ints to evutil_socket_t; make tests pass on win64. (f817bfa Dimitre Piskyulev)
- o Set _EVENT_SIZEOF_VOID_P correctly on win32 and win64 (1ae82cd Dimitre Piskyulev)
- o Avoid double-invocation of user callback with EVUTIL_EAI_CANCEL (abf01ed)
- o Set SO_UPDATE_ACCEPT_CONTEXT on sockets from AcceptEx so that shutdown() can work (52aa419)
- o When closing a filtering bufferevent, clear callbacks on the underlying bufferevent (fc7b1b0)
-
-NEW AND MODIFIED HTTP APIs
- o Add evhttp_parse_query_str to be used with evhttp_uri_parse. (2075fbc)
- o Add evhttp_response_code to remove one more reason to include http_struct.h (22e0a9b)
- o Define enumerators for all HTTP methods, including PATCH from RFC5789 (75a7341 Felix Nawothnig)
- o Functions to actually use evhttp_bound_socket with/as evconnlistener. (006efa7)
- o Add evhttp_request_get_command so code can tell GET from POST without peeking at the struct. (49f4bf7)
- o Introduce absolute URI parsing helpers. (86dd720 Pavel Plesov)
- o Revise evhttp_uri_parse implementation to handle more of RFC3986 (eaa5f1d)
- o Add evhttp_connection_get_base() to get the event_base from an http connection (cd00079)
- o Let evhttp_parse_query return -1 on failure (b1756d0)
- o New evhttp_uri(encode|decode) functions to handle + and NUL characters right (a8148ce)
- o Add evhttp_response_code to remove one more reason to include http_struct.h (22e0a9b)
- o Tweak interface for allowed methods (f5b391e)
- o Add evhttp server alias interface, correct flagging of proxy requests. (aab8c38 Christopher Davis)
-
-HTTP BUGFIXES
- o Add some comments to http.c and make a few functions static. (90b3ed5)
- o Fix Content-Length when trying send more than 100GB of data (!) on an evhttp. (525da3e)
- o Fix a bug where we would read too much data in HTTP bodies or requests. (58a1cc6)
- o Correctly count req->body_size on http usage without Content-Length (8e342e5)
- o Avoid missed-request bug when entire http request arrives before data is flushed (74c0e86)
- o reset "chunked" flag when sending non-chunked reply (aa5f55f Joachim Bauch)
- o evhttp_encode_uri encodes all reserved characters, including !$'()*+,/:=@ (2e63a60)
- o Replace exact-version checks for HTTP/1.1 with >= or < checks (647e094)
- o evhttp: Return 501 when we get an unrecognized method, not 400. (536311a)
- o Don't disable reading from the HTTP connection after sending the request to be notified of connection-close in time (c76640b Felix Nawothnig)
- o Never call evhttp_readcb while writing. (0512487)
- o Try to fix an assertion failure related to close detection (0faaa39)
- o Correctly detect timeouts during http connects (04861d5)
- o Preliminary support for Continue expectation in evhttp. (fa9305f Christopher Davis)
-
-OTHER BUGFIXES
- o Correct logic for realigning a chain in evbuffer_add (e4f34e8)
- o Fix a minor syntax error that most compilers didn't care about (e56ff65)
- o Fix some uses of int for socket in regress (5d389dc)
- o Check return value for ioctlsocket on win32 (f5ad31c Trond Norbye)
- o Fix som event_warns that should have been event_warnx (19c71e7)
- o Fix signal handler types for win64. (b81217f)
- o Try to clear up more size_t vs int/long issues. (598d133)
- o Make sure IOCP evconnlistener uses virtual events. (7b40a00 Christopher Davis)
- o Don't free evdns_request handles until after the callback is invoked (9ed30de)
- o Fix some more cancel-related bugs in getaddrinfo_async (c7cfbcf)
- o Make evdns_getaddrinfo_cancel threadsafe (d51b2fc)
- o Only clear underlying callbacks when the user hasn't reset them. (1ac5b23)
- o Fix bug in bufferevent_connect on an openssl bufferevent that already had an fd (4f228a1)
- o Resolve an evport bug in the thread/forking test (3a67d0b)
- o Make sure the CLOEXEC flag is set on fds we open for base notification (3ab578f)
- o Fix IRIX build. sa_family collides with a #define in sys/socket.h on IRIX. (e874982 Kevin Bowling)
- o If not WIN32, include <sys/socket.h> in event2/util.h. (1cd45e5 Kevin Bowling)
- o Fix some C99-style comments to work with the xlC compiler. (c2e5e22 Kevin Bowling)
- o Add some checks since lack of TAILQ_FOREACH doesn't imply lack of FIRST, END, NEXT, or INSERT_BEFORE. Quiet some warnings in XL C. (c4dc335 Kevin Bowling)
- o Reworked AIX __ss_family workaround to use AC_STRUCT_MEMBER. (2e2a3d7 Kevin Bowling)
- o Take select from <sys/select.h> when testing in autoconf. AIX build fix. (a3a9f6b Kevin Bowling)
- o Fix snprintf related failures on IRIX. (3239073 Kevin Bowling)
- o Remove _event_initialized(); make event_initialized() a function(); make it consistent on windows and non-windows (652024b)
- o Do not let EVLOOP_ONCE exit the loop until all deferred callbacks have run (2d5e1bd)
- o Make EVLOOP_ONCE ignore internal events (0617a81)
- o Possible crash fix when freeing an underlying bufferevent of an openssl bufferevent (29f7623)
-
-HTTP CLEANUPS
- o Stop using Libevent-1 headers in regress_http (1f507d7)
- o Modernize header usage in bench_http.c (e587069)
- o fix signed/unsigned warnings in http.c (74a91e5)
- o Update the HTTP regression tests to use Libevent2 apis for non-http stuff (d9ffa89)
- o Start porting http tests to not use legacy interfaces (8505a74)
- o Convert the rest of the http tests to be non-legacy unit tests. (9bb8239)
- o Rename the confusing "base" static variable in regress_http.c (353402a)
- o Stop accessing http request struct directly from in the unit tests. (0b137f4)
- o Refactor http version parsing into a single function (a38140b)
-
-TESTING
- o Improvements to tinytest_macros.h (ad923a1)
- o Add a huge pile of tests for the new URI functions, and make them pass. (a5a76e6)
- o Unit tests for evhttp_uri_set* (bc98f5e)
- o Increase the skew tolerance to 2 seconds in thread/deferred_cb_skew (f806476 Christopher Davis)
- o Reorder backends in test.sh to match preference order in event.c (ece974f)
- o Add a stress test for getaddrinfo_cancel (da1bf52)
- o Units test for unexpected evhttp methods. (75e3320)
-DOCUMENTATION
- o Document behavior of URI parsing more thoroughly. (3a33462)
- o Document that two bufferevent functions only work on socket bufferevents (70e1b60)
- o add a requested docstring for event_rpcgen.CommandLine.__init__ (f1250eb)
- o Fix a mistake in http documentation found by Julien Blache (229714d)
- o Add a basic example of how to write a static HTTP server. (4e794d5)
- o Document event_get_assignment (88be27d)
- o Note that reentrant calls to libevent from logging cbs may fail badly (e431bcd)
- o Clarify EVLOOP_* documentation to be more precise. (057a514)
-
-CLEANUPS
- o Simplify the logic for choosing EPOLL_CTL_ADD vs EPOLL_CTL_MOD (2c66983)
- o Rename "size" variables in win32select that were really fd counts. (b6a158c)
- o Fix even more win64 warnings (7484df6)
- o Fix even more win64 warnings: buffer, event_tagging, http, evdns, evrpc (545a611)
- o Fix more wn64 warnings. (34b84b9 Christopher Davis)
- o Use the label_len local variable in evdns instead of recalculating it over and over (ba01456)
- o Fix some irix compilation warnings spotted by Kevin Bowling (7bcace2)
-
-
-
-Changes in 2.0.8-rc (14 Oct 2010):
- [Autogenerated from the Git log, sorted and cleaned by hand.]
-NEW APIS
- o Add error callback to evconnlistener (c4be8d8 Simon Perreault)
- o Add a LEV_OPT_THREADSAFE option for threadsafe evconnlisteners (127d4f2)
-
-CHANGED BEHAVIOR
- o Correct logic on disabling underlying bufferevents when disabling a filter (ac27eb8)
-
-BUGFIXES
- o Obey enabled status when unsuspending (040a019 Simon Perreault)
- o Warn when using the error-prone EV_SIGNAL interface in an error-prone way. Also, fix a couple of race conditions in signal.c (720bd93)
- O Make default signal backend fully threadsafe (95a7d41)
- o Put internal events at highest priority (90651b3)
- o Fix warnings in the main codebase flagged by -Wsigned-compare (9c8db0, 5e4bafb, 5c214a, 6be589a, e06f514)
- o Fix compile in kqueue.c (b395392 Sebastian Hahn)
- o Do not search outside of the system directory for windows DLLs (d49b5e3)
- o Fix a spurious-call bug on epoll.c (0faaee0)
- o Send a shutdown(SHUT_WR) before closing an http connection (e0fd870 Christopher Davis)
- o Fix warnings on mingw with gcc 4.5 (5b7a370)
- o Fix an EINVAL on evbuffer_write_iovec on OpenSolaris. (fdc640b)
- o Fix allocation error for IOCP listeners. Probably harmless, since struct event is big (481ef92)
- o Make iocp/listener/error work; don't accept again if lev is disabled. (62b429a Christopher Davis)
- o Handle rate-limiting for reading on OpenSSL bufferevents correctly. (819b171)
- o Fix serious bugs in per-bufferevent rate-limiting code (34d64f8)
- o Avoid spurious reads from just-created open openssl bufferevents (223ee40)
- o Fix a case where an ssl bufferevent with CLOSE_ON_FREE didn't close its fd (93bb7d8)
- o The corrected bufferevent filter semantics let us fix our openssl tests (34331e4)
-
-TESTING
- o Make SSL tests cover enabling/disabling EV_READ. (a5ce9ad)
- o Bump to the latest version of tinytest (f0bd83e)
- o Unit tests for listener error callbacks (045eef4)
- o New unit test for ssl bufferevents starting with connected SSLs. (02f6259)
-
-DEBUGGABILITY
- o Make debugging output for epoll backend more comprehensive (ec2b05e)
- o Make event.c debugging messages report fds (e119899)
- o Make the --enable-gcc-warnings option include signed comparison warnings (d3b096c)
-
-DEADCODE REMOVAL
- o Remove the now-useless evsig_caught and evsig_process (4858b79)
- o Remove event_base.evsigbase; nothing used it. (38d0960)
-
-
-
-Changes in 2.0.7-rc (9 Sep 2010):
- [Autogenerated from the Git log, sorted and cleaned by hand.]
-NEW APIS
- o Expose a evdns_base_nameserver_sockaddr_add() function to add a nameserver by sockaddr (1952143)
- o Add event_config_set_num_cpus_hint() for tuning win32 IOCP thread pools, etc. (2447fe8 Christopher Davis)
-BUGFIXES
- o Fix a nasty dangling-event bug when using rate-limiting groups (0bffe43)
- o Clean up syntax on TAILQ_ENTRY() usage to build correctly with recent MSVC (60433a0 Gilad Benjamini)
- o Make definition of WIN32_LEAN_AND_MEAN in event.h conditional (3920172 Gilad Benjamini)
- o Correctly detect failure to delete bufferevent read-timeout event (da6e7cd)
- o Set close-on-exec bit for filedescriptors created by dns subsystem (d0b8843)
- o Fix kqueue correctness test on x84_64 (6123d12)
- o Detect events with no ev_base; warn instead of crashing (f1074b7)
- o Fix an issue with forking and signal socketpairs in select/poll backends (d61b2f3)
- o Stop using global arrays to implement the EVUTIL_ctype functions (1fdec20)
- o On windows, make lock/thread function tables static (5de2bcb)
- o Close th_notify_fds and open a new pair on reinit (495ed66)
- o Declare signal handler function as "__cdecl" on Windows (f0056d0)
- o Use the _func() replacements for open, fstat, etc in evutil.c on win32 (e50c0fc)
- o Only process up to MAX_DEFERRED deferred_cbs at a time (17a14f1 Christopher Davis)
-
-THREADING BUGFIXES
- o Avoid deadlock when activating signals (970e6ad)
- o Add a condition variable backend, with implementations for pthreads and win32 (d4977b5)
- o Use conditions instead of current_event_lock to fix a deadlock (e0972c2)
- o Fix logic error in win32 TRY_LOCK that caused problems with rate-limiting (4c32b9d)
- o Avoid needlessly calling evthread_notify_base() when the loop is not running (c7a06bf)
- o Minimize calls to base_notify implementation functions, thereby avoiding needless syscalls (4632b78)
-
-IOCP BUGFIXES
- o IOCP-related evbuffer fixes (03afa20 Christopher Davis)
- o Stop IOCP when freeing the event_base (d844242 Christopher Davis)
- o Some IOCP bufferevent tweaks (76f7e7a Christopher Davis)
-
-TESTS
- o Make the regress_pthread.c tests work on windows with current test APIs (d74ae38)
- o Add a unit test for conditions (5fb1095)
- o Allow more than one copy of regression tests to run at once (a97320a)
- o Fix event_del(0) instance in bench.c (b0f284c Shuo Chen)
- o Fix a few memory leaks in the tests (1115366)
- o IOCP-related unit test tweaks (499452f Christopher Davis)
- o Improve testing of when thread-notification occurs (ce85280)
-
-BUILD AND DISTRIBUTION
- o Add pkgconfig files for libevent_{openssl,pthreads} (ebcb1f0)
- o Change include order in Makefile.nmake (4022b28)
- o Make include/event2/event-config.h not included in source dist (a4af9be)
- o Honor NDEBUG; build without warnings with NDEBUG; make NDEBUG always-off in unit test code (743f866)
- o Declare evkeyvalq and event_list even if event_struct.h comes before sys/queue.h (d3ceca8)
- o Move evkeyvalq into a separate header for evhttp_parse_query users (ca9048f)
- o Prefer autoreconf -ivf to manual autogen.sh (7ea8e89)
-
-CLEANUP
- o Completely remove the (mostly-removed) obsolete thread functions (3808168)
- o Rename regress_pthread.c to regress_thread.c (041989f)
- o Make defer-internal.h use lock macros, not direct calls to lock fns (5218d2a)
+BUILD IMPROVEMENTS:
+ Libevent 2.1.2-alpha modernizes Libevent's use of autotools, and makes
+ numerous other build system. Parallel builds should be faster, and all
+ builds should be quieter.
+
+ o Split long lists in Makefile.am into one-item-per-line (2711cda)
+ o Remove unnecessary code in configure.in. (e65914f Ross Lagerwall)
+ o attempt to support OpenSSL in Makefile.nmake (eba0eb2 Patrick Pelletier)
+ o Use newer syntax for autoconf/automake init (7d60ba8)
+ o Enable silent build rules by default. Override with V=1 (7b18e5c)
+ o Switch to non-recursive makefiles (7092f3b)
+ o Rename subordinate Makefile.ams to include.am (6cdfeeb)
+ o Make quiet build even quieter (371a123)
+ o New --quiet option for event_rpcgen.py (aa59c1e)
+ o Be quiet when making regress.gen.[ch] (607a8ff)
+ o Fix handling of no-python case for nonrecursive make (1e3123d)
+ o We now require automake 1.9 or later. Modernize! (b7f6e89)
+ o Rename configure.in to configure.ac. (b3fea67 Ross Lagerwall)
+ o Use correct openssl libs and includes in pkgconfig file (d70af27)
+ o Use the same CFLAGS for openssl when building unit tests as with
+ libevent (1d9d511)
DOCUMENTATION
- o Document that DNS_NO_SEARCH is an obsolete alias for DNS_QUERY_NO_SEARCH (33200e7)
- o Update the whatsnew-2.0.txt document (4991669)
+ o Note that make_base_notifiable should not be necessary (26ee5f9)
+ o Be more clear that LEV_OPT_DEFERRED_ACCEPT has tricky prereqs (371efeb)
+ o Add caveat to docs about bufferevent_free() with data in outbuf (6fab9ee)
+ o Make it more clear that NOLOCK means "I promise, no multithreading"
+ (9444524)
+ o Fix a comment in test-fdleak after 077c7e949. (3881d8f Ross Lagerwall)
+ o Make the Makefile.nmake warning slightly less dire (e7bf4c8)
+ o Fix typo : events instead of evets (05f1aca Azat Khuzhin)
+ o Additional comments about OPENSSL_DIR variable, prompted by Dave Hart
+ (6bde2ef Patrick Pelletier)
+
+EVHTTP:
+ o ignore LWS after field-content in headers (370a2c0 Artem Germanov)
+ o Clean up rtrim implementation (aa59d80)
+ o Remove trailing tabs in HTTP headers as well. (ac42519)
+ o Remove internal ws from multiline http headers correctly (c6ff381)
+ o Move evutil_rtrim_lws_ to evutil.c where it belongs (61b93af)
+ o add evhttp_request_get_response_code_line (4f4d0c9 Jay R. Wren)
+ o Use EVUTIL_SOCKET_ERROR() wrapper to save/restore errno in
+ evhttp_connection_fail_ (7afbd60)
+ o preserve errno in evhttp_connection_fail_ for inspection by the
+ callback (36d0ee5 Patrick Pelletier)
+BUGFIXES:
+ o Correctly handle running on a system where accept4 doesn't work. (9fbfe9b)
+ o Avoid double-free on error in evbuffer_add_file. Found by
+ coverity. (6a81b1f)
+ o Fix another possible uninitialized read in dns regression tests. Found
+ by coverity. (13525c5)
+ o Add checks for functions in test-ratelim.c; found by Coverity (aa501e1)
+ o Avoid memory leak in test_event_calloc unit test; found by coverity
+ (92817a1)
+ o Fix a shadowed variable in addfile_test_readcb; found by coverity
+ (225344c)
+ o Check return value when using LEV_OPT_DEFERRED_ACCEPT. Found by
+ coverity (6487f63)
+ o Prevent reference leak of bufferevent if getaddrinfo fails. (b757786
+ Joachim Bauch)
+ o Make event_base_getnpriorities work with old "implicit base" code
+ (c46cb9c)
+ o Simplify and correct evutil_open_closeonexec_ (0de587f)
+ o Fix event_dlist definition when sys/queue not included (81b6209
+ Derrick Pallas)
+
+
+
+Changes in version 2.1.1-alpha (4 Apr 2012)
+
+ Libevent 2.1.1-alpha includes a number of new features and performance
+ improvements. The log below tries to organize them by rough area of
+ effect. It omits some commits which were pure bugfixes on other commits
+ listed below. For more detail, see the git changelogs. For more
+ insight, see the "whatsnew-2.1.txt" document included in the Libevent
+ 2.1.1-alpha distribution.
+
+ Performance: Core
+ o Replace several TAILQ users with LIST. LIST can be a little faster than
+ TAILQ for cases where we don't need queue-like behavior. (f9db33d,
+ 6494772, d313c29, 974d004)
+ o Disabled code to optimize the case where we reinsert an existing
+ timeout (e47042f, 09cbc3d)
+ o Remove a needless base-notify when rescheduling the first timeout (77a96fd)
+ o Save a needless comparison when removing/adjusting timeouts (dd5189b)
+ o Possible optimization: split event_queue_insert/remove into
+ separate functions. needs testing (efc4dc5)
+ o Make event_count maintenance branchless at the expense of an
+ extra shift. Needs benchmarking (d1cee3b)
+ o In the 2.1 branch, let's try out lazy gettimeofday/clock_gettime
+ comparison (2a83ecc)
+ o Optimization in event_process_active(): ignore maxcb & endtime
+ for highest priority events. (a9866aa Alexander Drozdov)
+ o Bypass event_add when using event_base_once() for a 0-sec timeout (35c5c95)
+ o Remove the eventqueue list and the ev_next pointers. (604569b 066775e)
+
+ Performance: Evbuffers
+ o Roughly 20% speed increase when line-draining a buffer using
+ EVBUFFER_EOL_CRLF (5dde0f0 Mina Naguib)
+ o Try to squeeze a little more speed out of EVBUFFER_EOL_CRLF (7b9d139)
+ o Fix a bug in the improved EOL_CRLF code (d927965)
+ o Remove a needless branch in evbuffer_drain() (d19a326)
+
+ Performance: Linux
+ o Infrastructure for using faster/fewer syscalls when creating
+ sockets (a1c042b)
+ o Minimize syscalls during socket creation in listener.c (7e9e289)
+ o Use a wrapper function to create the notification
+ pipe/socketpair/eventfd (ca76cd9)
+ o Use pipes for telling signals to main thread when possible (a35f396)
+ o Save syscalls when constructing listener sockets for evhttp (af6c9d8)
+ o Save some syscalls when creating evdns sockets (713e570)
+ o Save some syscalls when constructing a socket for a bufferevent (33fca62)
+ o Prefer epoll_create1 on Linuxen that have it (bac906c)
+
+ Performance: Epoll backend
+ o Use current event set rather than current pending change when
+ deciding whether to no-op a del (04ba27e Mike Smellie)
+ o Replace big chain of if/thens in epoll.c with a table lookup (8c83eb6)
+ o Clean up error handling in epoll_apply_one_change() a little (2d55a19)
+
+ Performance: Evport backend
+ o evport: use evmap_io to track fdinfo status. Should save time and
+ RAM. (4687ce4)
+ o evport: Remove a linear search over recent events when
+ reactivating them (0f77efe)
+ o evport: Use portev_user to remember fdinfo struct (276ec0e)
+ o evport: don't scan more events in ed_pending than needed (849a5cf)
+ o evport: Remove artificial low limit on max events per getn call (c04d927)
+ o Reenable main/many_events_slow_add for evport in 2.1 (e903db3)
+
+ Performance: Windows
+ o Use GetSystemTimeAsFileTime to implement gettimeofday on
+ win32. It's faster and more accurate than our old
+ approach. (b8b8aa5)
+
+ New functions and features: debugging
+ o Add event_enable_debug_logging() to control use of debug logs (e30a82f)
+
+ New functions and features: core
+ o Add event_config function to limit time/callbacks between calls
+ to dispatch (fd4de1e, 9fa56bd, a37a0c0, 3c63edd)
+ o New EVLOOP_NO_EXIT_ON_EMPTY option to keep looping even when no
+ events are pending (084e68f)
+ o Add event_base_get_npriorities() function. (ee3a4ee Alexander Drozdov)
+ o Make evbase_priority_init() and evbase_get_npriorities()
+ threadsafe (3c55b5e)
+ o New event_base_update_cache_time() to set cached_tv to current
+ time (212533e Abel Mathew)
+ o Add event_self_cbarg() to be used in conjunction with
+ event_new(). (ed36e6a Ross Lagerwall, fa931bb, 09a1906, 1338e6c,
+ 33e43ef)
+ o Add a new libevent_global_shutdown() to free all globals before
+ exiting. (041ca00 Mark Ellzey, f98c158, 15296d0, 55e991b)
+ o Use getifaddrs to detect our interfaces if possible (7085a45)
+ o Add event_base_get_running_event() to get the event* whose cb we
+ are in (c5732fd, 13dad99)
+
+ New functions and features: building
+ o Implement --enable-gcc-hardening configure option (7550267 Sebastian Hahn)
+
+ New functions and features: evbuffers
+ o Add evbuffer_add_file_segment() so one fd can be used efficiently
+ in more than one evbuffer_add_file at a time (e72afae, c2d9884,
+ 3f405d2, 0aad014)
+ o Fix windows file segment mappings (8254de7)
+ o Allow evbuffer_ptr_set to yield a point just after the end of the
+ buffer. (e6fe1da)
+ o Allow evbuffer_ptr to point to position 0 in an empty evbuffer
+ (7aeb2fd Nir Soffer)
+ o Set the special "not found" evbuffer_ptr consistently. (e3e97ae Nir Soffer)
+ o support adding buffers to other buffers non-destructively
+ (9d7368a Joachim Bauch)
+ o prevent nested multicast references, reworked locking (26041a8
+ Joachim Bauch)
+ o New EVBUFFER_EOL_NUL to read NUL-terminated strings from an
+ evbuffer (d7a8b36 Andrea Montefusco, 54142c9)
+ o Make evbuffer_file_segment_types adaptable (c6bbbf1)
+ o Added evbuffer_add_iovec and unit tests. (aaec5ac Mark Ellzey, 27b5398)
+ o Add evbuffer_copyout_from to copy data from the middle of a
+ buffer (27e2225)
+
+ New functions and features: bufferevents
+ o Allow users to set allow_dirty_shutdown (099d27d Catalin Patulea)
+ o Tweak allow_dirty_shutdown documentation (a44cd2b)
+ o Fix two issues in the allow_dirty_shutdown code. (f3b89de)
+ o Add a bufferevent_getcb() to find a bufferevent's current
+ callbacks (a650394)
+ o bufferevent: Add functions to set/get max_single_read/write
+ values. (998c813 Alexander Drozdov)
+ o bev_ssl: Be more specific in event callbacks. evhttp in particular gets
+ confused without at least one of BEV_EVENT_{READING|WRITING}. (f7eb69a
+ Catalin Patulea)
+
+ New functions and features: evconnlisteners
+ o Support TCP_DEFER_ACCEPT sockopts for listeners (5880e4a Mark Ellzey,
+ a270728)
+ o Add another caveat to the TCP_DEFER_ACCEPT documentation (a270728)
+ o Allow evconnlistener to be created in disabled state. (9593a33
+ Alexander Drozdov)
+ o The LEV_OPT_CLOSE_ON_EXEC flag now applies to accepted listener
+ sockets too (4970329)
+
+ Evhttp:
+ o Add new evhttp_{connection_}set_timeout_tv() functions to set
+ finger-grained http timeouts (6350e6c Constantine Verutin)
+ o Performance tweak to evhttp_parse_request_line. (aee1a97 Mark Ellzey)
+ o Add missing break to evhttp_parse_request_line (0fcc536)
+ o Add evhttp callback for bufferevent creation; this lets evhttp
+ support SSL. (8d3a850)
+ o Remove calls to deprecated bufferevent functions from evhttp.c (4d63758)
+ o evhttp: Add evhttp_foreach_bound_socket. (a2c48e3 Samy Al Bahra)
+
+ Build improvements:
+ o Add AC_USE_SYSTEM_EXTENSIONS to configure.in. Requires follow on
+ patches for correctness and robustness. (1fa7dbe Kevin Bowling)
+ o Filter '# define' statements from autoconf and generate
+ event-private.h (321b558 Kevin Bowling)
+ o Remove internal usage of _GNU_SOURCE (3b26541 Kevin Bowling)
+ o Eliminate a couple more manual internal _GNU_SOURCE defines (c51ef93
+ Kevin Bowling)
+ o Add AC_GNU_SOURCE to the fallback case. (ea8fa4c Kevin Bowling)
+ o Use a Configuration Header Template for evconfig-private.h (868f888
+ Kevin Bowling)
+ o Fix a comment warning and add evconfig-private.h to .gitignore
+ (f6d66bc Kevin Bowling)
+ o Include evconfig-private.h in internal files for great good. (0915ca0
+ Kevin Bowling)
+ o Backport libevent to vanilla Autoconf 2.59 (as used in RHEL5)
+ (ad03952 Kevin Bowling)
+ o Prefer the ./configure evconfig-private.h in MinGW, just in
+ case. (f964b72 Kevin Bowling)
+ o Shell hack for weird mkdir -p commands (fd7b5a8 Kevin Bowling)
+ o Add evconfig-private to remaining files (ded0a09 Kevin Bowling)
+ o Allow use of --enable-silent-rules for quieter compilation with
+ automake 1.11 (f1f8514 Dave Hart)
+ o Use "_WIN32", not WIN32: it's standard and we don't need to fake it
+ (9f560b)
+ o In configure, test for _WIN32 not WIN32. (85078b1 Peter Rosin)
+ o Do not define WIN32 in Makefile.nmake (d41f3ea Peter Rosin)
+ o Provide the autoconf m4 macros for the new OpenSSL via pkg-config
+ stuff. (674dc3d Harlan Stenn)
+ o Use pkg-config (if available) to handle OpenSSL. (1c63860 Harlan Stenn)
+ o We need AM_CPPFLAGS when compiling bufferevent_openssl.c (6d2613b
+ Harlan Stenn)
+ o Fix OSX build: $(OPENSSL_INCS) needs to be after
+ $(AM_CPPFLAGS). (46f1769 Zack Weinberg)
+ o Make gcc warnings on by default, and --enable-gcc-warnings only add
+ -Werror (d46517e Sebastian Hahn)
+ o Split up extra-long AC_CHECK_FUNCS/HEADERS lines in configure.in (88a30ad)
+ o Move libevent 1.x headers to include/, to put all public headers in
+ one place. (bbea8d6)
+ o Put #ifdef around some files to support alternate build
+ systems. (76d4c92 Ross Lagerwall)
+ o Also make win32select.c conditional for IDE users (bf2c5a7)
+
+ Debugging:
+ o Add a magic number to debug_locks to better catch lock-coding
+ errors. (b4a29c0 Dave Hart)
+ o munge the debug_lock signature before freeing it: it might help us
+ catch use-after-free (f28084d)
+ o Added --enable-event-debugging in configure (bc7b4e4, a9c2c9a Mark Ellzey)
+ o Debug addition for printing usec on TIMEOUT debugging. (ac43ce0 Mark Ellzey)
+ o Added usec debug in another area for debug (3baab0d Mark Ellzey)
+ o added timeout debug logs to include event ptr. (4b7d298 Mark Ellzey)
+ o more event dbg updates (6727543 Mark Ellzey)
+ o Clarify event_enable_debug_logging a little (6207826)
+ o Make --enable-verbose-debug option match its help text (10c3450)
+ o Add argument checks to some memory functions in `event.c'. (c8953d1
+ Mansour Moufid)
+
+ Testing:
+ o More abstraction in test.sh (cd74c4e)
+ o Add failing test for evbuffer_search_range. (8e26154 Nir Soffer)
+ o Tweaks to return types with end-of-buf ptrs (9ab8ab8)
+ o Add an (internal) usleep function for use by unit tests (f25d9d3)
+ o Synchronize with upstream tinytest (6c81be7)
+ o Make test-changelist faster (7622d26)
+ o Reduce the timeout in the main/fork test. (ab14f7c)
+ o New evhttp function to adjust initial retry timeout (350a3c4)
+ o Make regression tests run over 3x faster. (67a1763)
+ o Use test_timeval_diff_eq more consistently (b77b43f)
+ o Allow more slop in deferred_cb_skew test; freebsd needs it (b9f7e5f)
+ o When including an -internal.h header outside the main tree, do so
+ early (95e2455)
+ o Add a new test: test-fdleak which tests for fd leaks by creating many
+ sockets. (2ef9278 Ross Lagerwall, f7af194, 1c4288f, etc)
+ o Add a unit test for event_base_dump_events() (7afe48a, 8d08cce)
+ o Test more bufferevent_ratelim features (c24f91a)
+
+ Documentation:
+ o Improve evbuffer_ptr documentation (261ba63)
+ o added comments to describe refcounting of multicast chains (ba24f61
+ Joachim Bauch)
+ o Add doxygen for event_base_dump_events (cad5753)
+
+ OSX:
+ o Use "unlimited select" on OSX so that we can have more than
+ FD_SETSIZE fds (1fb5cc6)
+
+ KQueue:
+ o Use SIG_IGN instead of a do-nothing handler for signal events with
+ kqueue (148458e Zack Weinberg)
+
+ evprc:
+ o event_rpcgen.py now prints status information to stdout and errors to
+ stderr. (ffb0ba0 Ross Lagerwall)
+
+ Code improvement and refactoring:
+ o Make event_reinit() more robust and maintainable (272033e)
+ o Restore fast-path event_reinit() for slower backends (2c4b5de)
+ o Check changelist as part of checking representational integrity (39b3f38)
+ o Fix a compile warning in event_reinit (e4a56ed Sebastian Hahn)
+ o Refactor the functions that run over every event. (c89b4e6)
+ o Remove the last vestiges of _EVENT_USE_EVENTLIST (a3cec90)
+ o Make event-config.h depend on Makefile.am (2958a5c)
+
+ Build fixes:
+ o Don't do clang version detection when disabling some flags (083296b
+ Sebastian Hahn)
+
+ C standards conformance:
+ o Check for NULL return on win32 mm_calloc, and set ENOMEM. (af7ba69)
+ o Convert event-config.h macros to avoid reserved identifiers (68120d9)
+ o Generate event-config.h using the correct macros. (f82c57e)
+ o Convert include-guard macro convention to avoid reserved identifiers
+ (3f8c7cd)
+ o Make event_rpcgen.py output conform to identifier conventions (372bff1)
+ o Stop referring to an obsolete include guard in bench_http.h (5c0f7e0)
+ o Make the generated event-config.h use correct include guards (639383a)
+ o Fix all identifiers with names beginning with underscore. (cb9da0b)
+ o Make event_rpcgen.py output conform to identifier conventions, more
+ (bcefd24)
+ o Fix some problems introduced by automated identifier cleanup script
+ (c963534)
+ o Have all visible internal function names end with an underscore. (8ac3c4c)
+ o Apply the naming convention to our EVUTIL_IS* functions (c7848fa)
+ o Clean up lingering _identifiers. (946b584)
+ o Fix doxygen to use new macro conventions (da455e9)
+
+ Bugfixes:
+ o Do not use system EAI/AI values if we are not using the system
+ getaddrinfo. (7bcac07)
+
+ Sample Code:
+ o Fix up sample/event-test.c to use newer interfaces and make it
+ actually work. (19bab4f Ross Lagerwall)
+ o On Unix, remove event.fifo left by sample/event-test.c. (c0dacd2 Ross
+ Lagerwall)
+ o Rename event-test.c to event-read-fifo.c. (a5b370a Ross Lagerwall)
+ o event-read-fifo: Use EV_PERSIST appropriately (24dab0b)
-Changes in 2.0.6-rc (6 Aug 2010):
- [Autogenerated from the Git log, sorted by hand.]
-DOCUMENTATION
- o Document a change in the semantics of event_get_struct_event_size() (e21f5d1)
- o Add a comment to describe our plan for library versioning (9659ece)
- o Fix sentence fragment in docs for event_get_struct_event_size() (7b259b6)
-
-NEW FEATURES AND INTERFACE CHANGES
- o Remove the obsolete evthread interfaces (c5bab56)
- o Let evhttp_send_error infer the right error reasons (3990669)
- o Add a function to retrieve the other side of a bufferevent pair (17a8e2d)
- o Add bufferevent_lock()/bufferevent_unlock() (215e629)
- o Stop asserting when asked for a (unsupported) TCP dns port. Just return NULL. (7e87a59)
- o Replace (unused,always 0) is_tcp argument to evdns_add_server_port*() with flags (e1c1167)
- o Constify a couple of arguments to evdns_server_request_add_*_reply (cc2379d)
- o Add an interface to expose min_share in ratelimiting groups (6ae53d6)
-BUGFIXES
- o Avoid event_del on uninitialized event in event_base_free (6d19510)
- o Add some missing includes to fix Linux build again (75701e8)
- o Avoid close of uninitialized socket in evbuffer unit test (bda21e7)
- o Correctly recognize .255 addresses as link-local when looking for interfaces (8c3452b)
- o If no evdns request can be launched, return NULL, not a handle (b14f151)
- o Use generic win32 interfaces, not ASCII-only ones, where possible. (899b0a3)
- o Fix the default HTTP error template (06bd056 Felix Nawothnig)
- o Close the file in evutil_read_file whether there's an error or not. (0798dd1 Pierre Phaneuf)
- o Fix possible nullptr dereference in evhttp_send_reply_end() (29b2e23 Felix Nawothnig)
- o never let bufferevent_rlim functions return negative (0859870)
- o Make sample/hello_world work on windows (d89fdba)
- o Fix a deadlock related to event-base notification. Diagnosed by Zhou Li, Avi Bab, and Scott Lamb. (17522d2)
- o Possible fix to 100% cpu usage with epoll and openssl (cf249e7 Mike Smellie)
- o Don't race when calling event_active/event_add on a running signal event (fc5e0a2)
- o Suppress a spurious EPERM warning in epoll.c (e73cbde)
- o Fix wrong size calculation of iovec buffers when exact=1 (65abdc2 niks)
- o Change bufferevent_openssl::do_write so it doesn't call SSL_write with a 0 length buffer (c991317 Mike Smellie)
- o Fixed compilation of sample/le-proxy.c on win32 (13b912e Trond Norbye)
- o Fix rate-limit calculation on openssl bufferevents. (009f300)
- o Remember to initialize timeout events for bufferevent_async (de1f5d6 Christopher Davis)
-
-BUILD AND DISTRIBUTION CHANGES
- o Test the unlocked-deferred callback case of bufferevents (dfb75ab)
- o Remove the now-unusable EVTHREAD_LOCK/UNLOCK constants (fdfc3fc)
- o Use -Wlogical-op on gcc 4.5 or higher (d14bb92)
- o Add the libtool-generated /m4/* stuff to .gitignore (c21c663)
- o Remove some automake-generated files from version control. (9b14911)
- o Have autogen.sh pass --force-missing to automake (8a44062)
- o Set library version for libevent_pthreads correctly (b2d7440)
- o Really only add libevent_core.la to LIBADD on mingw (1425003 Sebastian Hahn)
- o Build more cleanly with NetBSDs that dislike toupper(char) (42a8c71)
- o Fix unit tests with -DUSE_DEBUG enabled (28f31a4)
- o Fix evdns build with -DUNICODE (5fa30d2)
- o Move event-config.h to include/event2 (ec347b9)
-
-TESTING
- o Add options to test-ratelim.c to check its results (2b44dcc)
- o Make test-ratelim clean up after itself better. (b5bfc44)
- o Remove the now-obsolete setup_test() and cleanup_test() functions (e73f1d7)
- o Remove all non-error prints from test/regress.c (8bc1e3d)
- o Make test.sh exit with nonzero status if tests fail (faf2a04)
- o Have the unit tests report errors from test.sh (3689bd2)
- o Fix logic in correcting high values from FIONREAD (3467f2f)
- o Add test for behavior on remote socket close (44d57ee)
- o Unit test for event_get_struct_event_size() (7510aac)
- o Make test/test.sh call test-changelist (7c92691)
- o Fix badly-behaved subtest of dns/bufferevent_connect_hostname (840a72f Joachim Bauch)
- o Add option to test-ratelim to test min_share (42f6b62)
- o Fix an assertion bug in test-ratelim (b2c6202)
- o Make tests quieter on local dns resolver failure (e996b3d)
- o Increase the tolerance in our unit tests for sloppy clocks. (170ffd2)
- o Use AF_INET socketpair to test sendfile on Solaris (9b60209)
- o Make test-changelist count cpu usage right on win32 (ea1ea3d)
-
-INTERNALS, PERFORMANCE, AND CODE CLEANUPS
- o Mark the event_err() functions as __attribute__((noreturn)) (33bbbed)
- o Do not check that event_base is set in EVBASE_ACQUIRE_LOCK (218a3c3)
- o Replace (safe) use of strcpy with memcpy to appease OpenBSD (caca2f4)
- o Remove some dead assignments (47c5dfb)
- o Fix a pedantic gcc 4.4 warning in event2/event.h (276e7ee)
- o Drain th_notify_fd[0] more bytes at a time. (a5bc15b)
- o Tidy up the code in evthread_make_base_notifiable a little (61e1eee)
- o Pass flags to fcntl(F_SETFL) and fcntl(F_SETFD) as int, not long (7c2dea1)
- o Remove unused variables in test/test-changelist.c (b00d4c0)
- o Fix whitespace. (cb927a5)
- o Improve error message for failed epoll to make debugging easier. (9e725f7)
- o Turn our socketpair() replacement into its own function (57b30cd)
-
-
-
-Changes in 2.0.5-beta (10 May 2010):
- [Autogenerated from the Git log, sorted by hand.]
-DOCUMENTATION
- o Update all our copyright notices to say "2010" (17efc1c)
- o Add Christopher Clark and Maxim Yegorushkin to the LICENSE file (38b7b57)
- o Clarify Christopher Clark's status as writer of original ht code. (78772c3)
- o Try to comment some of the event code more (cdd4c49)
- o Add a few more evmap/changelist comments (c247adc)
- o Add a comment to explain why evdns_request is now separte from request (ceefbe8)
- o Document evutil_secure_rng_init() and evutil_secure_rng_add_bytes() (a5bf43a)
- o Stop distributing and installing manpages: they were too inaccurate (7731ec8)
-
-NEW FEATURES AND INTERFACE CHANGES
- o Remove signal_assign() and signal_new() macros. (2fac0f7)
- o Make evdns use the regular logging system by default (b2f2be6)
- o Allow evbuffer_read() to split across more than 2 iovecs (e470ad3)
- o Functions to manipulate existing rate limiting groups. (ee41aca)
- o Functions to track the total bytes sent over a rate limit group. (fb366c1)
- o Detect and refuse reentrant event_base_loop() calls (b557b17)
- o Limit the maximum number of events on each socket to 65535 (819f949)
- o Add evbuffer_copyout to copy data from an evbuffer without draining (eb86c8c)
- o Expose the request and reply members of rpc_req_generic() (07edf78 Shuo Chen)
- o Add void* arguments to request_new and reply_new evrpc hooks (755fbf1 Shuo Chen)
- o Seed the RNG using sysctl() as well as /dev/urandom (71fc3eb)
- o Make evutil_secure_rng_init() work even with builtin arc4random (f980716)
- o Report DNS error when lookup fails during bufferevent_socket_connect_hostname. (0ef4070 Christopher Davis)
- o Release locks on bufferevents while executing callbacks (a5208fe Joachim Bauch) o Make debug mode catch mixed ET and non-ET events on an fd (cb67074)
- o Catch attempts to enable debug_mode too late (9ecf0d4)
- o Refuse null keys in evhttp_parse_query() (953e229 Frank Denis)
-BUGFIXES
- o Avoid a spurious close(-1) on Linux (70a44b6)
- o Do not close(-1) when freeing an uninitialized socket bufferevent (b34abf3)
- o Free evdns_base->req_heads on evdns_base_free (859af67)
- o Avoid an (untriggerable so far) crash bug in bufferevent_free() (0cf1431)
- o Set mem_offset for every bufferevent type (657d1b6)
- o Fix infrequent memory leak in bufferevent_init_common(). (8398641 Jardel Weyrich)
- o Make evutil_signal_active() match declaration. (e1e703d Patrick Galbraith)
- o Fix minheap code to use replacement malloc functions (a527618)
- o Fix a free(NULL) in minheap-internal.h (6f20492)
- o Fix critical bug in evbuffer_write when writev is not available (cda56ab)
- o Make the no_iovecs case of write_atmost compile (8e227b0)
- o Fix a memory leak when appending/prepending to a buffer with unused space. (45068a3)
- o Clean up a mistake in pointer manipulation in evbuffer_remove (28bfed4 Christopher Davis)
- o Always round up when there's a fractional number of msecs. (8f9e60c Christopher Davis)
- o Fix compiler warnings under WIN32 (d469c50 Giuseppe Scrivano)
- o Clean up properly when adding a signal handler fails. (b84b598 Gilad Benjamini) o Ensure that evdns_request is a persistent handle. (15bb82d Christopher Davis)
- o Free search state when finished searching to avoid an infinite loop. (a625840 Christopher Davis)
- o Assert for valid requests as necessary. (67072f3 Christopher Davis)
- o do not leak the request object on persistent connections (9d8edf2)
- o Make evdns logging threadsafe (b1c7950)
- o Fix a couple of bugs in the BSD sysctl arc4seed logic (a47a4b7)
- o Remove one last bug in last_with_datap logic. Found with valgrind (d49b92a)
- o fix a leak when unpausing evrpc requests (94ee125)
- o Fix a memory leak when unmarshalling RPC object arrays (f6ab2a2)
- o Fix compilation when openssl support is disabled (40c301b)
- o Allow empty reason line in HTTP status (739e688 Pierre Phaneuf)
- o Fix a compile warning introduced in 739e688 (bd1ed5f Sebastian Hahn)
- o Fix nonstandard TAILQ_FOREACH_REVERSE() definition (71afc52 Frank Denis)
- o Try /proc on Linux as entropy fallback; use sysctl as last resort (20fda29)
- o Fix symbol conflict between mm_*() macros and libmm (99e50e9)
- o Fix some crazy macro mistakes in arc4random.c (90d4225)
- o Make evbuffer_add_file() work on windows (dcdae6b)
- o Fix unused-variable warning when building with threads disabled (ad811cd)
- o Numerous opensolaris compilation fixes (c44de06)
- o Fix getaddrinfo with protocol unset on Solaris 9. Found by Dagobert Michelsen (2cf2a28)
- o Fix another nasty solaris getaddrinfo() behavior (3557071)
- o Define _REENTRANT as needed on Solaris, elsewhere (c1cd32a)
- o Fix some autoconf issues on OpenBSD (7c519df)
-
-BUILD AND DISTRIBUTION CHANGES
- o Distribute libevent.pc.in, not libevent.pc (22aff04)
- o Avoid errors in evutil.c when building with _UNICODE defined (b677032 Brodie Thiesfield)
- o Avoid errors in http.c when building with VC 2003 .NET (13e4f3b Brodie Thiesfield)
- o Support the standard 'make check' target in place of 'make verify' (426c8fb)
- o Remove redundant stuff from EXTRA_DIST (b660edf)
- o Switch to using AM conditionals in place of AC_LIBOBJ (2e898f5)
- o Remove an orphaned RELEASE flag in Makefile.am (0794b0d)
- o Give a better warning for bad automake versions. (77c917d)
- o Use dist_bin_SCRIPTS, not EXTRA_DIST, to distribute scripts (9eb2fd7)
- o Never test for select() on windows (3eb044d Trond Norbye)
- o Do not inhibit automake dependencies generation (10c4c90 Giuseppe Scrivano)
- o Create shared libraries under Windows (3cbca86 Giuseppe Scrivano)
- o Add ctags/etags files to .gitignore (0861d17)
- o Only specify -no-undefined on mingw (25433b9)
- o Only add libevent_core.la to LIBADD on mingw (fdc6297)
-
-TESTING
- o Get bench_http to work on Windows; add a switch to enable IOCP. (4ac38a5 Christopher Davis)
- o VC has no getopt(), so do without in bench_http. (1273d2f Christopher Davis)
- o Fix an obnoxious typo in the bufferevent_timeout_filter test (0d047c3)
- o Fix a write of uninitialized RAM in regression tests (68dc742)
- o Fix some memory leaks in the unit tests (274a7bd)
- o Make 'main/many_events' test 70 fds, not 64. (33874b0)
- o Unit-test every evbuffer_add_file() implementation. (06a4443)
- o Add more unit tests for evbuffer_expand (8c83e99)
- o Test another case of evbuffer_prepend (1234b95)
- o Fix a possible double-free bug in SSL bufferevents with CLOSE_ON_FREE (7501895) o Add dns/search_cancel unit test. (39b870b Christopher Davis)
- o Make http_base_test stop leaking an event_base. (96730d3)
- o Detect broken unsetenv at unit-test runtime (f37cd4c)
- o Implement regress_make_tempfile on win32 to test evbuffer_add_file (b4f12a1)
- o add more (currently skipped) add_file tests on win32 (05de45d)
- o Fix bench_http build on win32. (384d124)
- o Make unit test for add_file able to tell "error" from "done" (88a543f)
- o Make test for bufferevent_connect_hostname system-neutral (f89168e)
- o Make test.sh support mingw/msys on win32 (0ee6f6c)
- o Fix test.sh on freebsd (3d9e05b)
-
-INTERNALS, PERFORMANCE, AND AND CODE CLEANUPS
- o Improve the speed of evbuffer_readln() (cc1600a)
- o more whitespace normalization (2c2618d)
- o Revise evbuffer to add last_with_data (2a6d2a1)
- o Use last_with_data in place of previous_to_last (c8ac57f)
- o Remove previous_to_last from evbuffer (6f47bd1)
- o Fix last_with_data compilation on windows (1e7b986)
- o Add some glass-box tests for the last_with_data code. (17da042)
- o Improve robustness for refcounting (f1bc125)
- o Remove a needless min_heap_shift_up_() call (7204b91)
- o Increase MIN_BUFFER_SIZE to 512 (1024 on 64-bit) (2014ae4)
- o Do not use evbuffer_expand() to add the first chain to a buffer (5c0ebb3)
- o Make evbuffer_prepend handle empty buffers better (c87272b)
- o Replace last_with_data with a slightly smarter version (b7442f8)
- o Turn the increasingly complex *_CHAIN() macros into functions (96865c4)
- o Rewrite evbuffer_expand and its users (d5ebcf3)
- o Add evutil_tv_to_msec for safe conversion of timevals to milliseconds. (850c3ff Christopher Davis)
- o Initialize last_with_datap correctly in evbuffer_overlapped (a0983b6)
- o Replace EVUTIL_CLOSESOCKET macro with a function (899c1dc Sebastian Sjöberg)
- o Move domain search state to evdns_request. (beaa14a Christopher Davis)
- o Remove redundant checks for lock!=NULL before calling EVLOCK_LOCK (50ec59f)
- o Rename current_base symbol to event_global_current_base_ (c16e684)
- o Fix whitespace in evutil.c (935e150)
- o Replace users of "int fd" with "evutil_socket_t fd" in portable code (c7cf6f0)
-
-
-
-Changes in 2.0.4-alpha (28 Feb 2010):
- [Autogenerated from the Git log, sorted by hand.]
-DOCUMENTATION
- o Add stub header for 2.0.4-alpha changelog. (94d0065)
- o Improve the README with more information and links. (0b42726)
- o Add more people who wrote patches to the acknowledgments (0af10d5)
- o Add a warning about the use of event_initialized. (f32b575)
- o Add a LICENSE file so people can find our license easily (7067006)
- o Add a new "hello world" sample program (becb9f9)
- o Clarify status of example programs (d60a1bd)
- o Update time-test.c to use event2 (f4190bf)
- o Add the arc4random.c license to the LICENSE file. (e15e1e9)
-
-NEW FEATURES AND INTERFACE CHANGES
- o Improved optional lock debugging. (0cd3bb9)
- o Rate-limiting for bufferevents; group and individual limits are supported. (737c9cd)
- o Testing code for bufferevent rate-limiting. (f0c0124)
- o Make the initial nameserver probe timeout configurable. (1e56a32)
- o Revise the locking API: deprecate the old locking callbacks and add trylock. (347952f)
- o Do not make bufferevent_setfd implicitly disable EV_READ and EV_WRITE. (8274379)
- o Do not ignore bufferevent_enable(EV_READ) before bufferevent_connect(). (4a5b534)
- o Introduced evutil_make_socket_closeonexec() to preserve fd flags for F_SETFD. (d0939d2 Jardel Weyrich)
- o evdns_getaddrinfo() now supports the /etc/hosts file. (72dd666)
- o Look at the proper /etc/hosts file on windows. (66c02c7)
- o Allow http connections to use evdns for hostname looksups. (c698b77)
- o Changelist code to defer event changes until just before dispatch (27308aa)
- o do not use a function to assign the evdns base; instead assign it via evhttp_connection_base_new() which is a new function introduced in 2.0 (5032e52)
- o Functions to access more fields of struct event. (0683950)
- o Make kqueue use changelists. (45e5ae3)
- o Remove kqueue->pend_changes. (3225dfb)
- o Minimize epoll_ctl calls by using changelist (c8c6a89)
- o Add support for a "debug mode" to try to catch common errors. (cd17c3a)
- o Note a missing ratelim function (361da8f)
- o Add ev_[u]intptr_t to include/event2/util.h (1fa4c81)
- o const-ify a few more functions in event.h (d38a7a1)
- o Deprecate EVENT_FD and EVENT_SIGNAL. (f6b2694)
- o Remove EVUTIL_CHECK_FMT. (6c21c89)
- o Add EV_*_MAX macros to event2/util.h to expose limits for ev_* types. (aba1fff) o Functions to view and manipulate rate-limiting buckets. (85047a6)
- o Add the rest of the integer limits, and add a test for them. (60742d5)
- o Remove the 'flags' argument from evdns_base_set_option() (1dd7e6d)
- o Add an arc4random implementation for use by evdns (d4de062)
- o Use off_t for the length parameter of evbuffer_add_file (3fe60fd)
- o Construct Windows locks using InitializeCriticalSectionAndSpinCount (32c6f1b)
- o Expose view of current rate limit as constrained by group limit (162ce8a)
- o Provide consistent, tested semantics for bufferevent timeouts (d328829)
-
-BUGFIXES AND TESTS
- o Tolerate code that returns from a fatal_cb. (91fe23f)
- o Parenthesize macro arguments more aggressively (07e9e9b)
- o Fix memory-leak of signal handler array with kqueue. (e1ffbb8)
- o Stop passing EVTHREAD_READ and EVTHREAD_WRITE to non-rw locks. (76cd2b7)
- o Fix two use-after-free bugs in unit tests spoted by lock debugging (d84d838)
- o Fix a locking bug in event_base_loop() (da1718b)
- o Fix an evdns lock violation. (2df1f82 Zhuang Yuyao)
- o Valgrind fix: Clear struct kevent before checking for OSX bug. (56771a3 William Ahern)
- o Fix up evthread compilation on windows (bd6f1ba Roman Puls)
- o Fix regress_iocp.c usage of old lock allocation macros. (31687b4 unknown)
- o Update nmake makefile to build evthread.c (b62d979 unknown)
- o Fix a crash when reading badly formatted resolve.conf; from Yasuoka Masahiko (6c7c579 Yasuoka Masahiko)
- o Fix a snow leopard compile warning in the unit tests. (7ae9445)
- o Fix compile on Snow Leopard with gcc warnings enabled (70cdfe4 Sebastian Hahn)
- o Only define _GNU_SOURCE if it is not already defined. (ea6b1df Joachim Bauch)
- o Update sample/signal-test.c to use newer APIs and not leak. (f6430ac Evan Jones)
- o Fix a segfault when writing a very fragmented evbuffer onto an SSL (a6adeca Joachim Bauch)
- o Fix a segfault when freeing SSL bufferevents in an unusual order (a773df5 Joachim Bauch)
- o Drop install-sh from our git repo: a mismatched version could break "make dist" (6799527)
- o Set all instances of the version number correctly. (5a112d3)
- o Fix a few locking issues on windows. (c51bb3c unknown)
- o Use evutil_socket_t, not int, when logging socket errors. (292467c)
- o Fix up behavior of never-defered callbacks a little (390e056)
- o Replace some cases of uint32_t with ev_uint32_t. (a47d88d)
- o Fix compilation of devpoll.c by adding missing thread includes. (fee2c77 Dagobert Michelsen)
- o Make evutil_make_socket_nonblocking() leave any other flags alone. (4c8b7cd Jardel Weyrich)
- o Fix an fd leak in evconnlistener_new_bind(). (24fb502 Jardel Weyrich)
- o Fix a bogus free in evutil_new_addrinfo() (0d64051 Jardel Weyrich)
- o Adjusted fcntl() retval comparison on evutil_make_socket_nonblocking(). (4df7dbc Jardel Weyrich)
- o Fix the code that allowed DNS options to not end with : (ee4953f)
- o Fix crash bugs when a bufferevent's eventcb is not set. (2e8eeea)
- o Fix test-ratelim compilation on Linux. (885b427)
- o Fix compilation of rate-limiting code on win32. (165d30e)
- o Eradicated the last free() call. Let mm_free() take care of deallocation. (0546ce1 Jardel Weyrich)
- o Fix byte counts when mixing deferred and non-deferred evbuffer callbacks. (29151e6)
- o Fixed a memory leak on windows threads implementation. The CRITICAL_SECTION was not being free'd in evthread_win32_lock_free(). (2f33e00 Jardel Weyrich)
- o Fixed a fd leak in start_accepting(), plus cosmetic changes (4367a33 Jardel Weyrich)
- o Improved error handling in evconnlistener_new_async(). Also keeping the fd open because it is not opened by this function, so the caller is responsible for closing it. Additionally, since evconnlistener_new_bind() creates a socket and passes it to the function above, it required error checking to close the same socket. (fec66f9 Jardel Weyrich)
- o Don't use a bind address for nameservers on loopback (8d4aaf9)
- o Fix compilation of rate-limit code when threading support is disabled (97a8c79)
- o Detect setenv/unsetenv; skip main/base_environ test if we can't fake them. (7296971)
- o Check more internal event_add() calls for failure (ff3f6cd)
- o Fix windows and msvc build (5c7a7bc)
- o Call event_debug_unassign on internal events (a19b4a0)
- o Try to fix a warning in hash_debug_entry (137f2c6)
- o Fix a dumb typo in ev_intptr_t definitions. (27c9a40)
- o do not fail while sending on http connections the client closed. (93d7369)
- o make evhttp_send() safe against terminated connections, too (3978180)
- o Make Libevent 1.4.12 build on win32 with Unicode enabled. (000a33e Brodie Thiesfield)
- o Fix some additional -DUNICODE issues on win32. (a7a9431)
- o Add a check to make soure our EVUTIL_AI flags do not conflict with the native ones (c18490e)
- o Always use our own gai_strerror() replacement. (6810bdb)
- o Make RNG work when we have arc4random() but not arc4random_buf() (4ec8fea)
- o validate close cb on server when client connection closes (2f782af)
- o Fix two unlocked reads in evbuffer. (7116bf2)
- o When working without a current event base, don't try to use IOCP listeners (cb52838)
- o Fix getpid() usage on Windows (ff2a134)
- o Add a unit test for secure rng. (48a29b6)
- o Add some headers to fix freebsd compilation (b72be50)
- o When connect() succeeds immediately, don't invoke the callback immediately. (7515de9)
- o Suspend read/write on bufferevents during hostname lookup (db08f64)
- o Make bufferevent_free() clear all callbacks immediately. (b2fbeb3)
- o Fix some race conditions in persistent events and event_reinit (e2642f0)
- o Fix a bug in resetting timeouts on persistent events when IO triggers. (38ec0a7)
- o Add a test for timeouts on filtering bufferevents. (c02bfe1)
- o Add test for periodic timers that get activated for other reasons (8fcb7a1)
- o Use new timeval diff comparison function in bufferevent test (f3dfe46)
- o Delete stack-alloced event in new unit test before returning. (7ffd387)
- o Fix mingw compilation (23170a6)
- o Try to define a sane _EVENT_SIZEOF_SIZE_T for msvc compilation (1e14f82)
- o Fix arc4random compilation on MSVC. (98edb89)
- o deal with connect() failing immediately (7bc48bf)
- o Small cleanups on freebsd-connect-refused patch. (57b7248)
-
-BUILD AND DISTRIBUTION CHANGES
- o Remove the contents of WIN32-Prj as unmaintained. (c69d5a5)
- o Allow the user to redirect the verbose output of test/test.sh to a file (c382de6)
- o Allow test.sh to be run as ./test/test.sh (7dfbe94)
- o Never believe that we have pthreads on win32, even if gcc thinks we do. (78ed097)
- o Make it compile under gcc --std=c89. (e2ca403)
- o Fix a number of warnings from gcc -pedantic (918e9c5)
- o Add the msvc-generated .lib files to .gitignore. (e244a2e)
- o Add the "compile" script to gitignore. (1ba6bed)
-
-INTERNALS AND CODE CLEANUPS
- o Add a .gitignore file. (ba34071)
- o New EVTHREAD_TRY_LOCK function to try to grab a lock. (689fc09)
- o Add the abilitity to mark some buffer callbacks as never-deferred. (438f9ed)
- o Refactor our 'suspend operation' logic on bufferevents. (0d744aa)
- o Simplify the read high-watermark checking. (5846bf6)
- o Improve readability of evutil_unparse_protoname() (5a43df8 Jardel Weyrich)
- o Expose our cached gettimeofday value with a new interface (47854a8)
- o Whitespace fixes in test.sh (0b151a9)
- o Enable branch-prediction hints with EVUTIL_UNLIKELY. (eaaf27f)
- o Refactor code from evdns into a new internal "read a file" function. (0f7144f)
- o Comestic changes in evconnlistener_new(), new_accepting_socket(), accepted_socket_invoke_user_cb() and iocp_listener_enable(). (510ab6b Jardel Weyrich)
- o Add unit-test for bad_request bug fixed in 1.4 recently. (6cc79c6 Pavel Plesov) o Add a comment on evthread_enable_lock_debuging. (b9f43b2)
- o Fix test.sh on shells without echo -n (94131e9)
- o More unit tests for getaddrinfo_async: v4timeout and cancel. (a334b31)
- o Make http use evconnlistener. (ec34533)
- o move dns utility functions into a separate file so that we can use them for http testing (b822639)
- o add a test for evhttp_connection_base_new with a dns_base (26714ca)
- o forgot to add void to test function (78a50fe)
- o Add a forgotten header (changelist-internal.h) (4b9f307)
- o Remove some commented-out code in evutil (26e1b6f)
- o Remove a needless include of rpc_compat.h (70a4a3e)
- o Use less memory for each entry in a hashtable (a66e947)
- o Try to untangle the logic in server_port_flush(). (439aea0)
- o Use ev_[u]intptr_t types in place of [u]intptr_t (cef61a2)
- o Reduce windows header includes in our own headers. (da6135e)
- o clean up terminate_chunked test (e8a9782)
- o Increment the submicro version number. (63e868e)
- o Update event-config.h version number to match configure.in (aae7db5)
- o Clean up formatting: Disallow space-before-tab. (8fdf09c)
- o Clean up formatting: use tabs, not 8-spaces, to indent. (e5bbd40)
- o Clean up formatting: remove trailing spaces (e5cf987)
- o Clean up formatting: function/keyword spacing consistency. (4faeaea)
-
-
-
-Changes in 2.0.3-alpha (20 Nov 2009):
- o Add a new code to support SSL/TLS on bufferevents, using the OpenSSL library (where available).
- o Fix a bug where we didn't allocate enough memory in event_get_supported_methods().
- o Avoid segfault during failed allocation of locked evdns_base. (Found by Rocco Carbone.)
- o Export new evutil_ascii_* functions to perform locale-independent character type operations.
- o Try to compile better with MSVC: patches from Brodie Thiesfield
- o New evconnlistener_get_fd function to expose a listener's associated socket.
- o Expose an ev_socklen_t type for consistent use across platforms.
- o Make bufferevent_socket_connect() work when the original fd was -1.
- o Fix a bug in bufferevent_socket_connect() when the connection succeeds too quickly.
- o Export an evutil_sockaddr_cmp() to compare to sockaddr objects for equality.
- o Add a bufferevent_get_enabled() to tell what a bufferevent has been configured to do.
- o Add an evbuffer_search_eol() function to locate the end of a line nondestructively.
- o Add an evbuffer_search_range() function to search a bounded range of a buffer.
- o Fix a rare crash bug in evdns.
- o Have bufferevent_socket_connect() with no arguments put a bufferevent into connecting mode.
- o Support sendfile on Solaris: patch from Caitlin Mercer.
- o New functions to explicitly reference a socket used by an evhttp object. Patches from David Reiss.
- o When we send a BEV_EVENT_CONNECTED to indicate connected status, we no longer invoke the write callback as well unless we actually wrote data too.
- o If the kernel tells us that there are a negative number of bytes to read from a socket, do not believe it. Fixes bug 2841177; found by Alexander Pronchenkov.
- o Do not detect whether we have monotonic clock support every time a new event base is created: instead do it only once. Patch taken from Chromium.
- o Do not allocate the maximum event queue for the epoll backend at startup. Instead, start out accepting 32 events at a time, and double the queue's size when it seems that the OS is generating events faster than we're requesting them. Saves up to 374K per epoll-based event_base. Resolves bug 2839240.
- o Treat an event with a negative fd as valid but untriggerable by Libevent. This is useful for applications that want to manually activate events.
- o Fix compilation on Android, which forgot to define fd_mask in its sys/select.h
- o Do not drop data from evbuffer when out of memory; reported by Jacek Masiulaniec
- o New event_base_got_exit() and event_base_got_break() functions to tell whether an event loop exited because of an event_base_loopexit() or an event_base_loopbreak(). Patch from Ka-Hing Cheung.
- o When adding or deleting an event from a non-main thread, only wake up the main thread when its behavior actually needs to change.
- o Fix some bugs when using the old evdns interfaces to initialize the evdns module.
- o Detect errors during bufferevent_connect(). Patch from Christopher Davis.
- o Fix compilation for listener.h for C++ - missing extern "C". Patch from Ferenc Szalai.
- o Make the event_base_loop() family of functions respect thread-safety better. This should clear up a few hard-to-debug race conditions.
- o Fix a bug when using a specialized memory allocator on win32.
- o Have the win32 select() backend label TCP-socket-connected events as EV_WRITE, not EV_READ. This should bring it in line with the other backends, and improve portability. Patch from Christopher Davis.
- o Stop using enums as arguments or return values when what we mean is a bitfield of enum values. C++ doesn't believe that you can OR two enum values together and get another enum, and C++ takes its typing seriously. Patch from Christopher Davis.
- o Add an API to replace all fatal calls to exit() with a user-provided panic function.
- o Replace all assert() calls with a variant that is aware of the user-provided logging and panic functions.
- o Add a return value to event_assign so that it can fail rather than asserting when the user gives it bad input. event_set still dies on bad input.
- o The event_base_new() and event_base_new_with_config() functions now never call exit() on failure. For backward "compatibility", event_init() still does, but more consistently.
- o Remove compat/sys/_time.h. It interfered with system headers on HPUX, and its functionality has been subsumed by event2/util.h and util-internal.h.
- o Add a new bufferevent_socket_connect_hostname() to encapsulate the resolve-then-connect operation.
- o Build kqueue.c correctly on GNU/kFreeBSD platforms. Patch pulled upstream from Debian.
- o Alternative queue-based timeout algorithm for programs that use a large number of timeouts with the same value.
- o New event_base_config option to disable the timeval cache entirely.
- o Make EV_PERSIST timeouts more accurate: schedule the next event based on the scheduled time of the previous event, not based on the current time.
- o Allow http.c to handle cases where getaddrinfo returns an IPv6 address. Patch from Ryan Phillips.
- o Fix a problem with excessive memory allocation when using multiple event priorities.
- o Default to using arc4random for DNS transaction IDs on systems that have it; from OpenBSD.
- o Never check the environment when we're running setuid or setgid; from OpenBSD.
- o Options passed to evdns_set_option() no longer need to end with a colon.
- o Add an evutil_getaddrinfo() function to clone getaddrinfo on platforms that don't have it.
- o Add an evdns_getaddrinfo() function to provide a nonblocking getaddrinfo using evdns, so programs can perform useful hostname lookup.
- o Finally expose the IOCP-based bufferevent backend. It passes its unit tests, but probably still has some bugs remaining. Code by Nick Mathewson and Christopher Davis.
- o Numerous other bugfixes.
- o On FreeBSD and other OSes, connect can return ECONREFUSED immediately; instead of failing the function call, pretend with faileld in the callback.
- o Fix a race condition in the pthreads test case; found by Nick Mathewson
- o Remove most calls to event_err() in http and deal with memory errors instead
-
-
-
-Changes in 2.0.2-alpha (25 Jul 2009):
- o Add a new flag to bufferevents to make all callbacks automatically deferred.
- o Make evdns functionality locked, and automatically defer dns callbacks.
- o Fix a possible free(NULL) when freeing an event_base with no signals.
- o Add a flag to disable checking environment varibles when making an event_base
- o Disallow setting less than 1 priority.
- o Fix a bug when removing a timeout from the heap. [Patch from Marko Kreen]
- o Use signal.h, not sys/signal.h. [Patch from mmadia]
- o Try harder to build with certain older c99 compilers.
- o Make sure that an event_config's flags field is always initialized to 0. [Bug report from Victor Goya]
- o Avoid data corruption when reading data entirely into the second-to-last chain of an evbuffer. [Bug report from Victor Goya]
- o Make sendfile work on FreeBSD
- o Do not use vararg macros for accessing evrpc structures; this is not backwards compatible, but we did not promise any backwards compatibility for the rpc code.
- o Actually define the event_config_set_flag() function.
- o Try harder to compile with Visual C++.
- o Move event_set() and its allies to event2/event_compat.h where they belong.
- o Remove the event_gotsig code, which has long been deprecated and unused.
- o Add an event_get_base() function to return the base assigned to an event.
- o New function to automate connecting on a socket-based bufferevent.
- o New functions to automate listening for incoming TCP connections.
- o Do case-insensitive checks with a locale-independent comparison function.
- o Rename the evbuffercb and everrorcb callbacks to bufferevent_data_cb and bufferevent_event_cb respectively. The old names are available in bufferevent_compat.h.
- o Rename the EVBUFFER_* codes used by bufferevent event callbacks to BEV_EVENT_*, to avoid namespace collision with evbuffer flags. The old names are available in bufferevent_compat.h.
- o Move the EVBUFFER_INPUT and EVBUFFER_OUTPUT macros to bufferevent_compat.h
- o Add a bufferevent_getfd() function to mirror bufferevent_setfd()
- o Make bufferevent_setfd() return an error code if the operation is not successful.
- o Shave 22 bytes off struct event on 32-bit platforms by shrinking and re-ordering fields. The savings on 64-bit platforms is likely higher.
- o Cap the maximum number of priorities at 256.
- o Change the semantics of evbuffer_cb_set_flags() to be set-flag only; add a new evbuffer_cb_clear_flags() to remove set flags.
- o Change the interface of evbuffer_add_reference so that the cleanup callback gets more information
- o Revise the new evbuffer_reserve_space/evbuffer_commit_space() interfaces so that you can use them without causing extraneous copies or leaving gaps in the evbuffer.
- o Add a new evbuffer_peek() interface to inspect data in an evbuffer without removing it.
- o Fix a deadlock when suspending reads in a bufferevent due to a full buffer. (Spotted by Joachim Bauch.)
- o Fix a memory error when freeing a thread-enabled event base with registered events. (Spotted by Joachim Bauch.)
- o Try to contain degree of failure when running on a win32 version so heavily firewalled that we can't fake a socketpair.
- o Activate fd events in a pseudorandom order with O(N) backends, so that we don't systematically favor low fds (select) or earlier-added fds (poll, win32).
- o Replace some read()/write() instances with send()/recv() to work properly on win32.
- o Set truncated flag correctly in evdns server replies.
- o Raise RpcGenError in event_rpcgen.py; from jmanison and Zack Weinberg
- o Fix preamble of rpcgen-generated files to rely on event2 includes; based on work by jmansion; patch from Zack Weinberg.
- o Allow specifying the output filename for rpcgen; based on work by jmansion; patch from Zack Weinberg.
- o Allow C identifiers as struct names; allow multiple comments in .rpc files; from Zack Weinberg
- o Mitigate a race condition when using socket bufferevents in multiple threads.
- o Use AC_SEARCH_LIBS, not AC_CHECK_LIB to avoid needless library use.
- o Do not allow event_del(ev) to return while that event's callback is executing in another thread. This fixes a nasty race condition.
- o event_get_supported_methods() now lists methods that have been disabled with the EVENT_NO* environment options.
- o Rename encode_int[64] to evtag_encode_int[64] to avoid polluting the global namespace. The old method names are still available as macros in event2/tag_compat.h.
-
-
-
-Changes in 2.0.1-alpha (17 Apr 2009):
- o free minheap on event_base_free(); from Christopher Layne
- o debug cleanups in signal.c; from Christopher Layne
- o provide event_base_new() that does not set the current_base global
- o bufferevent_write now uses a const source argument; report from Charles Kerr
- o improve documentation on event_base_loopexit; patch from Scott Lamb
- o New function, event_{base_}loopbreak. Like event_loopexit, it makes an event loop stop executing and return. Unlike event_loopexit, it keeps subsequent pending events from getting executed. Patch from Scott Lamb
- o Check return value of event_add in signal.c
- o provide event_reinit() to reintialize an event_base after fork
- o New function event_set_mem_functinons. It allows the user to give libevent replacement functions to use for memory management in place of malloc(), free(), etc. This should be generally useful for memory instrumentation, specialized allocators, and so on.
- o The kqueue implementation now catches signals that are raised after event_add() is called but before the event_loop() call. This makes it match the other implementations.
- o The kqueue implementation now restores original signal handlers correctly when its signal events are removed.
- o Check return value of event_add in signal.c
- o Add a more powerful evbuffer_readln as a replacement for evbuffer_readline. The new function handles more newline styles, and is more useful with buffers that may contain a nul characters.
- o Do not mangle socket handles on 64-bit windows.
- o The configure script now takes an --enable-gcc-warnigns option that turns on many optional gcc warnings. (Nick has been building with these for a while, but they might be useful to other developers.)
- o move EV_PERSIST handling out of the event backends
- o small improvements to evhttp documentation
- o always generate Date and Content-Length headers for HTTP/1.1 replies
- o set the correct event base for HTTP close events
- o When building with GCC, use the "format" attribute to verify type correctness of calls to printf-like functions.
- o Rewrite win32.c backend to be O(n lg n) rather than O(n^2).
- o Removed obsoleted recalc code
- o support for 32-bit tag numbers in rpc structures; this is wire compatible, but changes the API slightly.
- o pull setters/getters out of RPC structures into a base class to which we just need to store a pointer; this reduces the memory footprint of these structures.
- o prefix {encode,decode}_tag functions with evtag to avoid collisions
- o fix a bug with event_rpcgen for integers
- o Correctly handle DNS replies with no answers set (Fixes bug 1846282)
- o add -Wstrict-aliasing to warnings and more cleanup
- o removed linger from http server socket; reported by Ilya Martynov
- o event_rpcgen now allows creating integer arrays
- o support string arrays in event_rpcgen
- o change evrpc hooking to allow pausing of RPCs; this will make it possible for the hook to do some meaning ful work; this is not backwards compatible.
- o allow an http request callback to take ownership of a request structure
- o allow association of meta data with RPC requests for hook processing
- o associate more context for hooks to query such as the connection object
- o remove pending timeouts on event_base_free()
- o also check EAGAIN for Solaris' event ports; from W.C.A. Wijngaards
- o devpoll and evport need reinit; tested by W.C.A Wijngaards
- o event_base_get_method; from Springande Ulv
- o Send CRLF after each chunk in HTTP output, for compliance with RFC2626. Patch from "propanbutan". Fixes bug 1894184.
- o Add a int64_t parsing function, with unit tests, so we can apply Scott Lamb's fix to allow large HTTP values.
- o Use a 64-bit field to hold HTTP content-lengths. Patch from Scott Lamb.
- o Allow regression code to build even without Python installed
- o remove NDEBUG ifdefs from evdns.c
- o detect integer types properly on platforms without stdint.h
- o udpate documentation of event_loop and event_base_loop; from Tani Hosokawa.
- o simplify evbuffer by removing orig_buffer
- o do not insert event into list when evsel->add fails
- o add support for PUT/DELETE requests; from Josh Rotenberg
- o introduce evhttp_accept_socket() to accept from an already created socket
- o include Content-Length in reply for HTTP/1.0 requests with keep-alive
- o increase listen queue for http sockets to 128; if that is not enough the evhttp_accpet_socket() api can be used with a prepared socket.
- o Patch from Tani Hosokawa: make some functions in http.c threadsafe.
- o test support for PUT/DELETE requests; from Josh Rotenberg
- o rewrite of the evbuffer code to reduce memory copies
- o Some older Solaris versions demand that _REENTRANT be defined to get strtok_r(); do so.
- o Do not free the kqop file descriptor in other processes, also allow it to be 0; from Andrei Nigmatulin
- o Provide OpenSSL style support for multiple threads accessing the same event_base
- o make event_rpcgen.py generate code include event-config.h; reported by Sam Banks.
- o switch thread support so that locks get allocated as they are needed.
- o make event methods static so that they are not exported; from Andrei Nigmatulin
- o make RPC replies use application/octet-stream as mime type
- o do not delete uninitialized timeout event in evdns
- o Correct the documentation on buffer printf functions.
- o Don't warn on unimplemented epoll_create(): this isn't a problem, just a reason to fall back to poll or select.
- o Correctly handle timeouts larger than 35 minutes on Linux with epoll.c. This is probably a kernel defect, but we'll have to support old kernels anyway even if it gets fixed.
- o Make name_from_addr() threadsafe in http.c
- o Add new thread-safe interfaces to evdns functions.
- o Make all event_tagging interfaces threadsafe.
- o Rename internal memory management functions.
- o New functions (event_assign, event_new, event_free) for use by apps that want to be safely threadsafe, or want to remain ignorant of the contents of struct event.
- o introduce bufferevent_read_buffer; allows reading without memory copy.
- o expose bufferevent_setwatermark via header files and fix high watermark on read
- o fix a bug in buffrevent read water marks and add a test for them
- o fix a bug in which bufferevent_write_buffer would not schedule a write event
- o provide bufferevent_input and bufferevent_output without requiring knowledge of the structure
- o introduce bufferevent_setcb and bufferevent_setfd to allow better manipulation of bufferevents
- o convert evhttp_connection to use bufferevents.
- o use libevent's internal timercmp on all platforms, to avoid bugs on old platforms where timercmp(a,b,<=) is buggy.
- o Remove the never-exported, never-used evhttp_hostportfile function.
- o Support input/output filters for bufferevents; somewhat similar to libio's model. This will allow us to implement SSL, compression, etc, transparently to users of bufferevents such as the http layer.
- o allow connections to be removed from an rpc pool
- o add new evtimer_assign, signal_assign, evtimer_new, and signal_new functions to manipulate timer and signal events, analagous to the now-recommended event_assign and event_new
- o switch internal uses of event_set over to use event_assign.
- o introduce evbuffer_contiguous_space() api that tells a user how much data is available in the first buffer chain
- o introduce evbuffer_reserve_space() and evbuffer_commit_space() to make processing in filters more efficient.
- o reduce system calls for getting current time by caching it.
- o separate signal events from io events; making the code less complex.
- o support for periodic timeouts
- o support for virtual HTTP hosts.
- o turn event_initialized() into a function, and add function equivalents to EVENT_SIGNAL and EVENT_FD so that people don't need to include event_struct.h
- o Build test directory correctly with CPPFLAGS set.
- o Provide an API for retrieving the supported event mechanisms.
- o event_base_new_with_config() and corresponding config APIs.
- o migrate the evhttp header to event2/ but accessors are still missing.
- o deprecate timeout_* event functions by moving them to event_compat.h
- o Move windows gettimeofday replacement into a new evutil_gettimeofday().
- o Make configure script work on IRIX.
- o provide a method for canceling ongoing http requests.
- o Make vsnprintf() returns consistent on win32.
- o Fix connection keep-alive behavior for HTTP/1.0
- o Fix use of freed memory in event_reinit; pointed out by Peter Postma
- o constify struct timeval * where possible
- o make event_get_supported_methods obey environment variables
- o support for edge-triggered events on epoll and kqueue backends: patch from Valery Kholodkov
- o support for selecting event backends by their features, and for querying the features of a backend.
- o change failing behavior of event_base_new_with_config: if a config is provided and no backend is selected, return NULL instead of aborting.
- o deliver partial data to request callbacks when chunked callback is set even if there is no chunking on the http level; allows cancelation of requests from within the chunked callback; from Scott Lamb.
- o allow min_heap_erase to be called on removed members; from liusifan.
- o Rename INPUT and OUTPUT to EVRPC_INPUT and EVRPC_OUTPUT. Retain INPUT/OUTPUT aliases on on-win32 platforms for backwards compatibility.
- o Do not use SO_REUSEADDR when connecting
- o Support 64-bit integers in RPC structs
- o Correct handling of trailing headers in chunked replies; from Scott Lamb.
- o Support multi-line HTTP headers; based on a patch from Moshe Litvin
- o Reject negative Content-Length headers; anonymous bug report
- o Detect CLOCK_MONOTONIC at runtime for evdns; anonymous bug report
- o Various HTTP correctness fixes from Scott Lamb
- o Fix a bug where deleting signals with the kqueue backend would cause subsequent adds to fail
- o Support multiple events listening on the same signal; make signals regular events that go on the same event queue; problem report by Alexander Drozdov.
- o Fix a problem with epoll() and reinit; problem report by Alexander Drozdov.
- o Fix off-by-one errors in devpoll; from Ian Bell
- o Make event_add not change any state if it fails; reported by Ian Bell.
- o Fix a bug where headers arriving in multiple packets were not parsed; fix from Jiang Hong; test by me.
- o Match the query in DNS replies to the query in the request; from Vsevolod Stakhov.
- o Add new utility functions to correctly observe and log winsock errors.
- o Do not remove Accept-Encoding header
- o Clear the timer cache on entering the event loop; reported by Victor Chang
- o Only bind the socket on connect when a local address has been provided; reported by Alejo Sanchez
- o Allow setting of local port for evhttp connections to support millions of connections from a single system; from Richard Jones.
- o Clear the timer cache when leaving the event loop; reported by Robin Haberkorn
- o Fix a typo in setting the global event base; reported by lance.
- o Set the 0x20 bit on outgoing alphabetic characters in DNS requests randomly, and insist on a match in replies. This helps resist DNS poisoning attacks.
- o Make the http connection close detection work properly with bufferevents and fix a potential memory leak associated with it.
- o Restructure the event backends so that they do not need to keep track of events themselves, as a side effect multiple events can use the same fd or signal.
- o Add generic implementations for parsing and emiting IPv6 addresses on platforms that do not have inet_ntop and/or inet_pton.
- o Allow DNS servers that have IPv6 addresses.
- o Add an evbuffer_write_atmost() function to write a limited number of bytes to an fd.
- o Refactor internal notify-main-thread logic to prefer eventfd to pipe, then pipe to socketpair, and only use socketpairs as a last resort.
- o Try harder to pack all evbuffer reads into as few chains as possible, using readv/WSARecv as appropriate.
- o New evthread_use_windows_threads() and evthread_use_pthreads() functions to set up the evthread callbacks with reasonable defaults.
- o Change the semantics of timeouts in conjunction with EV_PERSIST; timeouts in that case will now repeat until deleted.
- o sendfile, mmap and memory reference support for evbuffers.
- o New evutil_make_listen_socket_reuseable() to abstract SO_REUSEADDR.
- o New bind-to option to allow DNS clients to bind to an arbitrary port for outgoing requests.
- o evbuffers can now be "frozen" to prevent operations at one or both ends.
- o Bufferevents now notice external attempts to add data to an inbuf or remove it from an outbuf, and stop them.
- o Fix parsing of queries where the encoded queries contained \r, \n or +
- o Do not allow internal events to starve lower-priority events.
-
-Changes in 1.4.0:
- o allow \r or \n individually to separate HTTP headers instead of the standard "\r\n"; from Charles Kerr.
- o demote most http warnings to debug messages
- o Fix Solaris compilation; from Magne Mahre
- o Add a "Date" header to HTTP responses, as required by HTTP 1.1.
- o Support specifying the local address of an evhttp_connection using set_local_address
- o Fix a memory leak in which failed HTTP connections whould not free the request object
- o Make adding of array members in event_rpcgen more efficient, but doubling memory allocation
- o Fix a memory leak in the DNS server
- o Fix compilation when DNS_USE_OPENSSL_FOR_ID is enabled
- o Fix buffer size and string generation in evdns_resolve_reverse_ipv6().
- o Respond to nonstandard DNS queries with "NOTIMPL" rather than by ignoring them.
- o In DNS responses, the CD flag should be preserved, not the TC flag.
- o Fix http.c to compile properly with USE_DEBUG; from Christopher Layne
- o Handle NULL timeouts correctly on Solaris; from Trond Norbye
- o Recalculate pending events properly when reallocating event array on Solaris; from Trond Norbye
- o Add Doxygen documentation to header files; from Mark Heily
- o Add a evdns_set_transaction_id_fn() function to override the default
- transaction ID generation code.
- o Add an evutil module (with header evutil.h) to implement our standard cross-platform hacks, on the theory that somebody else would like to use them too.
- o Fix signals implementation on windows.
- o Fix http module on windows to close sockets properly.
- o Make autogen.sh script run correctly on systems where /bin/sh isn't bash. (Patch from Trond Norbye, rewritten by Hagne Mahre and then Hannah Schroeter.)
- o Skip calling gettime() in timeout_process if we are not in fact waiting for any events. (Patch from Trond Norbye)
- o Make test subdirectory compile under mingw.
- o Fix win32 buffer.c behavior so that it is correct for sockets (which do not like ReadFile and WriteFile).
- o Make the test.sh script run unit tests for the evpoll method.
- o Make the entire evdns.h header enclosed in "extern C" as appropriate.
- o Fix implementation of strsep on platforms that lack it
- o Fix implementation of getaddrinfo on platforms that lack it; mainly, this will make Windows http.c work better. Original patch by Lubomir Marinov.
- o Fix evport implementation: port_disassociate called on unassociated events resulting in bogus errors; more efficient memory management; from Trond Norbye and Prakash Sangappa
- o support for hooks on rpc input and output; can be used to implement rpc independent processing such as compression or authentication.
- o use a min heap instead of a red-black tree for timeouts; as a result finding the min is a O(1) operation now; from Maxim Yegorushkin
- o associate an event base with an rpc pool
- o added two additional libraries: libevent_core and libevent_extra in addition to the regular libevent. libevent_core contains only the event core whereas libevent_extra contains dns, http and rpc support
- o Begin using libtool's library versioning support correctly. If we don't mess up, this will more or less guarantee binaries linked against old versions of libevent continue working when we make changes to libevent that do not break backward compatibility.
- o Fix evhttp.h compilation when TAILQ_ENTRY is not defined.
- o Small code cleanups in epoll_dispatch().
- o Increase the maximum number of addresses read from a packet in evdns to 32.
- o Remove support for the rtsig method: it hasn't compiled for a while, and nobody seems to miss it very much. Let us know if there's a good reason to put it back in.
- o Rename the "class" field in evdns_server_request to dns_question_class, so that it won't break compilation under C++. Use a macro so that old code won't break. Mark the macro as deprecated.
- o Fix DNS unit tests so that having a DNS server with broken IPv6 support is no longer cause for aborting the unit tests.
- o Make event_base_free() succeed even if there are pending non-internal events on a base. This may still leak memory and fds, but at least it no longer crashes.
- o Post-process the config.h file into a new, installed event-config.h file that we can install, and whose macros will be safe to include in header files.
- o Remove the long-deprecated acconfig.h file.
- o Do not require #include <sys/types.h> before #include <event.h>.
- o Add new evutil_timer* functions to wrap (or replace) the regular timeval manipulation functions.
- o Fix many build issues when using the Microsoft C compiler.
- o Remove a bash-ism in autogen.sh
- o When calling event_del on a signal, restore the signal handler's previous value rather than setting it to SIG_DFL. Patch from Christopher Layne.
- o Make the logic for active events work better with internal events; patch from Christopher Layne.
- o We do not need to specially remove a timeout before calling event_del; patch from Christopher Layne.
diff --git a/ChangeLog-1.4 b/ChangeLog-1.4
new file mode 100644
index 00000000..166d3087
--- /dev/null
+++ b/ChangeLog-1.4
@@ -0,0 +1,231 @@
+Changes in 1.4.14b-stable
+ o Set the VERSION_INFO correctly for 1.4.14
+
+Changes in 1.4.14-stable
+ o Add a .gitignore file for the 1.4 branch. (d014edb)
+ o Backport evbuffer_readln(). (b04cc60 Nicholas Marriott)
+ o Make the evbuffer_readln backport follow the current API (c545485)
+ o Valgrind fix: Clear struct kevent before checking for OSX bug. (5713d5d William Ahern)
+ o Fix a crash when reading badly formatted resolve.conf (5b10d00 Yasuoka Masahiko)
+ o Fix memory-leak of signal handler array with kqueue. [backport] (01f3775)
+ o Update sample/signal-test.c to use newer APIs and not leak. (891765c Evan Jones)
+ o Correct all versions in 1.4 branch (ac0d213)
+ o Make evutil_make_socket_nonblocking() leave any other flags alone. (81c26ba Jardel Weyrich)
+ o Adjusted fcntl() retval comparison on evutil_make_socket_nonblocking(). (5f2e250 Jardel Weyrich)
+ o Correct a debug message in evhttp_parse_request_line (35df59e)
+ o Merge branch 'readln-backport' into patches-1.4 (8771d5b)
+ o Do not send an HTTP error when we've already closed or responded. (4fd2dd9 Pavel Plesov)
+ o Re-add event_siglcb; some old code _was_ still using it. :( (bd03d06)
+ o Make Libevent 1.4 build on win32 with Unicode enabled. (bce58d6 Brodie Thiesfield)
+ o Distribute nmake makefile for 1.4 (20d706d)
+ o do not fail while sending on http connections the client closed. (5c8b446)
+ o make evhttp_send() safe against terminated connections, too (01ea0c5)
+ o Fix a free(NULL) in min_heap.h (2458934)
+ o Fix memory leak when setting up priorities; reported by Alexander Drozdov (cb1a722)
+ o Clean up properly when adding a signal handler fails. (ae6ece0 Gilad Benjamini)
+ o Do not abort HTTP requests missing a reason string. (29d7b32 Pierre Phaneuf)
+ o Fix compile warning in http.c (906d573)
+ o Define _REENTRANT as needed on Solaris, elsewhere (6cbea13)
+
+
+Changes in 1.4.13-stable:
+ o If the kernel tells us that there are a negative number of bytes to read from a socket, do not believe it. Fixes bug 2841177; found by Alexander Pronchenkov.
+ o Do not allocate the maximum event queue and fd array for the epoll backend at startup. Instead, start out accepting 32 events at a time, and double the queue's size when it seems that the OS is generating events faster than we're requesting them. Saves up to 512K per epoll-based event_base. Resolves bug 2839240.
+ o Fix compilation on Android, which forgot to define fd_mask in its sys/select.h
+ o Do not drop data from evbuffer when out of memory; reported by Jacek Masiulaniec
+ o Rename our replacement compat/sys/_time.h header to avoid build a conflict on HPUX; reported by Kathryn Hogg.
+ o Build kqueue.c correctly on GNU/kFreeBSD platforms. Patch pulled upstream from Debian.
+ o Fix a problem with excessive memory allocation when using multiple event priorities.
+ o When running set[ug]id, don't check the environment. Based on a patch from OpenBSD.
+
+
+Changes in 1.4.12-stable:
+ o Try to contain degree of failure when running on a win32 version so heavily firewalled that we can't fake a socketpair.
+ o Fix an obscure timing-dependent, allocator-dependent crash in the evdns code.
+ o Use __VA_ARGS__ syntax for varargs macros in event_rpcgen when compiler is not GCC.
+ o Activate fd events in a pseudorandom order with O(N) backends, so that we don't systematically favor low fds (select) or earlier-added fds (poll, win32).
+ o Fix another pair of fencepost bugs in epoll.c. [Patch from Adam Langley.]
+ o Do not break evdns connections to nameservers when our IP changes.
+ o Set truncated flag correctly in evdns server replies.
+ o Disable strict aliasing with GCC: our code is not compliant with it.
+
+Changes in 1.4.11-stable:
+ o Fix a bug when removing a timeout from the heap. [Patch from Marko Kreen]
+ o Remove the limit on size of HTTP headers by removing static buffers.
+ o Fix a nasty dangling pointer bug in epoll.c that could occur after epoll_recalc(). [Patch from Kevin Springborn]
+ o Distribute Win32-Code/event-config.h, not ./event-config.h
+
+Changes in 1.4.10-stable:
+ o clean up buffered http connection data on reset; reported by Brian O'Kelley
+ o bug fix and potential race condition in signal handling; from Alexander Drozdov
+ o rename the Solaris event ports backend to evport
+ o support compilation on Haiku
+ o fix signal processing when a signal callback delivers a signal; from Alexander Drozdov
+ o const-ify some arguments to evdns functions.
+ o off-by-one error in epoll_recalc; reported by Victor Goya
+ o include Doxyfile in tar ball; from Jeff Garzik
+ o correctly parse queries with encoded \r, \n or + characters
+
+Changes in 1.4.9-stable:
+ o event_add would not return error for some backends; from Dean McNamee
+ o Clear the timer cache on entering the event loop; reported by Victor Chang
+ o Only bind the socket on connect when a local address has been provided; reported by Alejo Sanchez
+ o Allow setting of local port for evhttp connections to support millions of connections from a single system; from Richard Jones.
+ o Clear the timer cache when leaving the event loop; reported by Robin Haberkorn
+ o Fix a typo in setting the global event base; reported by lance.
+ o Fix a memory leak when reading multi-line headers
+ o Fix a memory leak by not running explicit close detection for server connections
+
+Changes in 1.4.8-stable:
+ o Match the query in DNS replies to the query in the request; from Vsevolod Stakhov.
+ o Fix a merge problem in which name_from_addr returned pointers to the stack; found by Jiang Hong.
+ o Do not remove Accept-Encoding header
+
+Changes in 1.4.7-stable:
+ o Fix a bug where headers arriving in multiple packets were not parsed; fix from Jiang Hong; test by me.
+
+Changes in 1.4.6-stable:
+ o evutil.h now includes <stdarg.h> directly
+ o switch all uses of [v]snprintf over to evutil
+ o Correct handling of trailing headers in chunked replies; from Scott Lamb.
+ o Support multi-line HTTP headers; based on a patch from Moshe Litvin
+ o Reject negative Content-Length headers; anonymous bug report
+ o Detect CLOCK_MONOTONIC at runtime for evdns; anonymous bug report
+ o Fix a bug where deleting signals with the kqueue backend would cause subsequent adds to fail
+ o Support multiple events listening on the same signal; make signals regular events that go on the same event queue; problem report by Alexander Drozdov.
+ o Deal with evbuffer_read() returning -1 on EINTR|EAGAIN; from Adam Langley.
+ o Fix a bug in which the DNS server would incorrectly set the type of a cname reply to a.
+ o Fix a bug where setting the timeout on a bufferevent would take not effect if the event was already pending.
+ o Fix a memory leak when using signals for some event bases; reported by Alexander Drozdov.
+ o Add libevent.vcproj file to distribution to help with Windows build.
+ o Fix a problem with epoll() and reinit; problem report by Alexander Drozdov.
+ o Fix off-by-one errors in devpoll; from Ian Bell
+ o Make event_add not change any state if it fails; reported by Ian Bell.
+ o Do not warn on accept when errno is either EAGAIN or EINTR
+
+Changes in 1.4.5-stable:
+ o Fix connection keep-alive behavior for HTTP/1.0
+ o Fix use of freed memory in event_reinit; pointed out by Peter Postma
+ o Constify struct timeval * where possible; pointed out by Forest Wilkinson
+ o allow min_heap_erase to be called on removed members; from liusifan.
+ o Rename INPUT and OUTPUT to EVRPC_INPUT and EVRPC_OUTPUT. Retain INPUT/OUTPUT aliases on on-win32 platforms for backwards compatibility.
+ o Do not use SO_REUSEADDR when connecting
+ o Fix Windows build
+ o Fix a bug in event_rpcgen when generated fixed-sized entries
+
+Changes in 1.4.4-stable:
+ o Correct the documentation on buffer printf functions.
+ o Don't warn on unimplemented epoll_create(): this isn't a problem, just a reason to fall back to poll or select.
+ o Correctly handle timeouts larger than 35 minutes on Linux with epoll.c. This is probably a kernel defect, but we'll have to support old kernels anyway even if it gets fixed.
+ o Fix a potential stack corruption bug in tagging on 64-bit CPUs.
+ o expose bufferevent_setwatermark via header files and fix high watermark on read
+ o fix a bug in bufferevent read water marks and add a test for them
+ o introduce bufferevent_setcb and bufferevent_setfd to allow better manipulation of bufferevents
+ o use libevent's internal timercmp on all platforms, to avoid bugs on old platforms where timercmp(a,b,<=) is buggy.
+ o reduce system calls for getting current time by caching it.
+ o fix evhttp_bind_socket() so that multiple sockets can be bound by the same http server.
+ o Build test directory correctly with CPPFLAGS set.
+ o Fix build under Visual C++ 2005.
+ o Expose evhttp_accept_socket() API.
+ o Merge windows gettimeofday() replacement into a new evutil_gettimeofday() function.
+ o Fix autoconf script behavior on IRIX.
+ o Make sure winsock2.h include always comes before windows.h include.
+
+Changes in 1.4.3-stable:
+ o include Content-Length in reply for HTTP/1.0 requests with keep-alive
+ o Patch from Tani Hosokawa: make some functions in http.c threadsafe.
+ o Do not free the kqop file descriptor in other processes, also allow it to be 0; from Andrei Nigmatulin
+ o make event_rpcgen.py generate code include event-config.h; reported by Sam Banks.
+ o make event methods static so that they are not exported; from Andrei Nigmatulin
+ o make RPC replies use application/octet-stream as mime type
+ o do not delete uninitialized timeout event in evdns
+
+Changes in 1.4.2-rc:
+ o remove pending timeouts on event_base_free()
+ o also check EAGAIN for Solaris' event ports; from W.C.A. Wijngaards
+ o devpoll and evport need reinit; tested by W.C.A Wijngaards
+ o event_base_get_method; from Springande Ulv
+ o Send CRLF after each chunk in HTTP output, for compliance with RFC2626. Patch from "propanbutan". Fixes bug 1894184.
+ o Add a int64_t parsing function, with unit tests, so we can apply Scott Lamb's fix to allow large HTTP values.
+ o Use a 64-bit field to hold HTTP content-lengths. Patch from Scott Lamb.
+ o Allow regression code to build even without Python installed
+ o remove NDEBUG ifdefs from evdns.c
+ o update documentation of event_loop and event_base_loop; from Tani Hosokawa.
+ o detect integer types properly on platforms without stdint.h
+ o Remove "AM_MAINTAINER_MODE" declaration in configure.in: now makefiles and configure should get re-generated automatically when Makefile.am or configure.in chanes.
+ o do not insert event into list when evsel->add fails
+
+Changes in 1.4.1-beta:
+ o free minheap on event_base_free(); from Christopher Layne
+ o debug cleanups in signal.c; from Christopher Layne
+ o provide event_base_new() that does not set the current_base global
+ o bufferevent_write now uses a const source argument; report from Charles Kerr
+ o better documentation for event_base_loopexit; from Scott Lamb.
+ o Make kqueue have the same behavior as other backends when a signal is caught between event_add() and event_loop(). Previously, it would catch and ignore such signals.
+ o Make kqueue restore signal handlers correctly when event_del() is called.
+ o provide event_reinit() to reintialize an event_base after fork
+ o small improvements to evhttp documentation
+ o always generate Date and Content-Length headers for HTTP/1.1 replies
+ o set the correct event base for HTTP close events
+ o New function, event_{base_}loopbreak. Like event_loopexit, it makes an event loop stop executing and return. Unlike event_loopexit, it keeps subsequent pending events from getting executed. Patch from Scott Lamb
+ o Removed obsoleted recalc code
+ o pull setters/getters out of RPC structures into a base class to which we just need to store a pointer; this reduces the memory footprint of these structures.
+ o fix a bug with event_rpcgen for integers
+ o move EV_PERSIST handling out of the event backends
+ o support for 32-bit tag numbers in rpc structures; this is wire compatible, but changes the API slightly.
+ o prefix {encode,decode}_tag functions with evtag to avoid collisions
+ o Correctly handle DNS replies with no answers set (Fixes bug 1846282)
+ o The configure script now takes an --enable-gcc-warnigns option that turns on many optional gcc warnings. (Nick has been building with these for a while, but they might be useful to other developers.)
+ o When building with GCC, use the "format" attribute to verify type correctness of calls to printf-like functions.
+ o removed linger from http server socket; reported by Ilya Martynov
+ o allow \r or \n individually to separate HTTP headers instead of the standard "\r\n"; from Charles Kerr.
+ o demote most http warnings to debug messages
+ o Fix Solaris compilation; from Magne Mahre
+ o Add a "Date" header to HTTP responses, as required by HTTP 1.1.
+ o Support specifying the local address of an evhttp_connection using set_local_address
+ o Fix a memory leak in which failed HTTP connections would not free the request object
+ o Make adding of array members in event_rpcgen more efficient, but doubling memory allocation
+ o Fix a memory leak in the DNS server
+ o Fix compilation when DNS_USE_OPENSSL_FOR_ID is enabled
+ o Fix buffer size and string generation in evdns_resolve_reverse_ipv6().
+ o Respond to nonstandard DNS queries with "NOTIMPL" rather than by ignoring them.
+ o In DNS responses, the CD flag should be preserved, not the TC flag.
+ o Fix http.c to compile properly with USE_DEBUG; from Christopher Layne
+ o Handle NULL timeouts correctly on Solaris; from Trond Norbye
+ o Recalculate pending events properly when reallocating event array on Solaris; from Trond Norbye
+ o Add Doxygen documentation to header files; from Mark Heily
+ o Add a evdns_set_transaction_id_fn() function to override the default
+ transaction ID generation code.
+ o Add an evutil module (with header evutil.h) to implement our standard cross-platform hacks, on the theory that somebody else would like to use them too.
+ o Fix signals implementation on windows.
+ o Fix http module on windows to close sockets properly.
+ o Make autogen.sh script run correctly on systems where /bin/sh isn't bash. (Patch from Trond Norbye, rewritten by Hagne Mahre and then Hannah Schroeter.)
+ o Skip calling gettime() in timeout_process if we are not in fact waiting for any events. (Patch from Trond Norbye)
+ o Make test subdirectory compile under mingw.
+ o Fix win32 buffer.c behavior so that it is correct for sockets (which do not like ReadFile and WriteFile).
+ o Make the test.sh script run unit tests for the evpoll method.
+ o Make the entire evdns.h header enclosed in "extern C" as appropriate.
+ o Fix implementation of strsep on platforms that lack it
+ o Fix implementation of getaddrinfo on platforms that lack it; mainly, this will make Windows http.c work better. Original patch by Lubomir Marinov.
+ o Fix evport implementation: port_disassociate called on unassociated events resulting in bogus errors; more efficient memory management; from Trond Norbye and Prakash Sangappa
+ o support for hooks on rpc input and output; can be used to implement rpc independent processing such as compression or authentication.
+ o use a min heap instead of a red-black tree for timeouts; as a result finding the min is a O(1) operation now; from Maxim Yegorushkin
+ o associate an event base with an rpc pool
+ o added two additional libraries: libevent_core and libevent_extra in addition to the regular libevent. libevent_core contains only the event core whereas libevent_extra contains dns, http and rpc support
+ o Begin using libtool's library versioning support correctly. If we don't mess up, this will more or less guarantee binaries linked against old versions of libevent continue working when we make changes to libevent that do not break backward compatibility.
+ o Fix evhttp.h compilation when TAILQ_ENTRY is not defined.
+ o Small code cleanups in epoll_dispatch().
+ o Increase the maximum number of addresses read from a packet in evdns to 32.
+ o Remove support for the rtsig method: it hasn't compiled for a while, and nobody seems to miss it very much. Let us know if there's a good reason to put it back in.
+ o Rename the "class" field in evdns_server_request to dns_question_class, so that it won't break compilation under C++. Use a macro so that old code won't break. Mark the macro as deprecated.
+ o Fix DNS unit tests so that having a DNS server with broken IPv6 support is no longer cause for aborting the unit tests.
+ o Make event_base_free() succeed even if there are pending non-internal events on a base. This may still leak memory and fds, but at least it no longer crashes.
+ o Post-process the config.h file into a new, installed event-config.h file that we can install, and whose macros will be safe to include in header files.
+ o Remove the long-deprecated acconfig.h file.
+ o Do not require #include <sys/types.h> before #include <event.h>.
+ o Add new evutil_timer* functions to wrap (or replace) the regular timeval manipulation functions.
+ o Fix many build issues when using the Microsoft C compiler.
+ o Remove a bash-ism in autogen.sh
+ o When calling event_del on a signal, restore the signal handler's previous value rather than setting it to SIG_DFL. Patch from Christopher Layne.
+ o Make the logic for active events work better with internal events; patch from Christopher Layne.
+ o We do not need to specially remove a timeout before calling event_del; patch from Christopher Layne.
diff --git a/ChangeLog-2.0 b/ChangeLog-2.0
new file mode 100644
index 00000000..a925d33b
--- /dev/null
+++ b/ChangeLog-2.0
@@ -0,0 +1,1280 @@
+Changes in version 2.0.21-stable (18 Nov 2012)
+BUGFIXES:
+ o ssl: Don't discard SSL read event when timeout and read come close together (576b29f)
+ o ssl: Stop looping in "consider_reading" if reading is suspended. (f719b8a Joachim Bauch)
+ o ssl: No need to reserve space if reading is suspended. (1acf2eb Joachim Bauch)
+ o dns: Avoid a memory-leak on OOM in evdns. (73e85dd, f2bff75 George Danchev)
+ o build: Use python2 rather than python (0eb0109 Ross Lagerwall)
+ o build: Compile without warnings on mingw64 (94866c2)
+ o build: Fix compilation on mingw64 with -DUSE_DEBUG (62bd2c4)
+ o build: Make rpcgen_wrapper.sh work on systems without a "python2" binary (f3009e4)
+ o iocp: Close IOCP listener socket on free when LEV_OPT_CLOSE_ON_FREE is set (cb853ea Juan Pablo Fernandez)
+ o core: Avoid crash when event_pending() called with no event_base set on event (e3cccf3)
+ o misc: remove stray 'x' so print_err will compile when uncommented (ac35650 Patrick Pelletier)
+ o tests: Fix renegotiation test to work around openssl 1.0.1 bug (c2f3086)
+ o tests: Warn when openssl version in unit test mismatches compiled version. (ac009f9)
+
+
+Changes in version 2.0.20-stable (23 Aug 2012)
+BUGFIXES:
+ o core: Make event_pending() threadsafe. (be7a95c Simon Liu)
+ o win32: avoid crash when waiting forever on zero fds. (160e58b)
+ o evhttp: Fix a memory leak on error in evhttp_uriencode (11c8b31)
+ o evbuffer: Avoid possible needless call to writev. Found by coverity. (6a4ec5c)
+ o evdns: memset sockaddr_in before using it. Found by coverity. (a1a0e67)
+ o evhttp: Check more setsockopt return values when binding sockets. Found by coverity (a0912e3)
+ o evdns: Avoid segfault on weird timeout during name lookup. (dc32077 Greg Hazel)
+ o bufferevent_ssl: Correctly invoke callbacks when a SSL bufferevent reads some and then blocks. (606ac43)
+
+
+PORTABILITY FIXES:
+ o check for arc4random_buf at runtime, on OS X (bff5f94 Greg Hazel)
+ o Correctly check for arc4random_buf (fcec3e8 Sebastian Hahn)
+ o Add explicit AC_PROG_SED to configure.in so all autoconfs will expose $(SED) (ca80ea6)
+
+BUILD FIXES:
+ o Add GCC annotations so that the vsprintf functions get checked properly (117e327)
+ o Fix an unused variable warning on *BSD. (c0720c1)
+
+UNIT TEST FIXES:
+ o Fix a couple of memory leaks (found with Valgrind). (3b2529a Ross Lagerwall)
+ o Remove deadcode in http regression tests. Found by coverity. (5553346)
+ o Fix possible uninitialized read in dns regression tests. Found by coverity. (2259777)
+ o Set umask before calling mkstemp in unit tests. Found by coverity (f1ce15d)
+ o Fix various check-after-dereference issues in unit tests: found by coverity (4f3732d)
+ o Fix resource leaks in the unit tests; found by coverity (270f279)
+ o Add some missing null checks to unit tests; found by coverity (f021c3d)
+ o Avoid more crashes/bad calls in unit tests; found by coverity (3cde5bf)
+ o Remove unused variable; spotted by coverity (6355b2a)
+ o Add checks to various return values in unit tests. Found by coverity (b9e7329)
+ o Move assignment outside tt_assert in ssl unit tests. Appeases coverity. (a2006c0)
+
+
+
+Changes in version 2.0.19-stable (3 May 2012)
+BUGFIXES (CORE):
+ o Refactor event_persist_closure: raise and extract some common logic (bec22b4)
+ o If time has jumped so we'd reschedule a periodic event in the past, schedule it for the future instead (dfd808c)
+ o If a higher-priority event becomes active, don't continue running events of the current priority. (2bfda40)
+
+BUGFIXES (SSL):
+ o Fixed potential double-readcb execution with openssl bufferevents. (4e62cd1 Mark Ellzey)
+
+BUGFIXES (DNS):
+ o Cancel a probe request when the server is freed, and ignore cancelled probe callbacks (94d2336 Greg Hazel)
+ o Remove redundant DNS_ERR_CANCEL check, move comment (46b8060 Greg Hazel)
+ o When retransmitting a timed-out DNS request, pick a fresh nameserver. (3d9e52a)
+
+DOCUMENTATION FIXES:
+ o Fix a typo in the bufferevent documentation (98e9119)
+ o Add missing ) to changelog; spotted by rransom (4c7ee6b)
+ o Fix the website URL in the readme (f775521)
+
+COMPILATION FIXES:
+ o Fix a compilation error with MSVC 2005 due to use of mode_t (336dcae)
+ o Configure with gcc older than 2.95 (4a6fd43 Sebastian Hahn)
+ o Generate event-config.h with a single sed script (30b6f88 Zack Weinberg)
+
+FORWARD-COMPATIBILITY:
+ o Backport: provide EVENT_LOG_* names, and deprecate _EVENT_LOG_* (d1a03b2)
+
+TESTING/DEBUGGING SUPPORT:
+ o dns-example.c can now take a resolv.conf file on the commandline (6610fa5)
+ o Make some evdns.c debug logs more verbose (d873d67)
+ o Work-around a stupid gcov-breaking bug in OSX 10.6 (b3887cd)
+
+
+
+Changes in version 2.0.18-stable (22 Mar 2012)
+BUGFIXES (core):
+ o Make uses of open() close-on-exec safe by introducing an internal evutil_open_closeonexec. (d2b5f72 Ross Lagerwall, 03dce42)
+
+BUGFIXES (kqueue):
+ o Properly zero the kevent in kq_setup_kevent() (c2c7b39 Sebastian Hahn)
+
+BUILD FIXES:
+ o Added OPENSSL_LDFLAGS env variable which is appended to SSL checks. (9278196 Mark Ellzey)
+ o Changed OPENSSL_LDFLAGS to OPENSSL_LIBADD (2d67b63 Mark Ellzey)
+ o Don't do clang version detection when disabling some flags (083296b Sebastian Hahn)
+
+BUGFIXES (dns):
+ o Stop crashing in evdns when nameserver probes give a weird error (bec5068)
+
+
+Changes in version 2.0.17-stable (10 Feb 2012)
+
+BUGFIXES (core):
+ o Be absolutely sure to clear pncalls before leaving event_signal_closure (11f36a5)
+ o check for sysctl before we use it (358c745 Mike Frysinger)
+ o Remove bogus casts of socket to int before calling ev_callback (f032516)
+ o Make evconnlistener work around bug in older Linux when getting nmapped (ecfc720)
+ o Fix a list corruption bug when using event_reinit() with signals present (6e41cdc)
+ o Fix a fd leak in event_reinit() (3f18ad1)
+ o Do a memberwise comparison of threading function tables (c94a5f2 Nate R)
+ o Use C-style comments in C source files (for compatibility with compilers such as xlc on AIX). (d84d917 Greg Hewgill)
+ o Avoid crash when freeing event_iocp and using event_set_mem_functions (19715a6)
+ o In the kqueue backend, do not report EBADF as an EV_READ (5d7bfa1 Nicholas Marriott)
+
+BUGFIXES (evbuffer and bufferevents):
+ o Fix behavior of evbuffer_peek(buf,-1,NULL,NULL,0) (c986f23 Zack Weinberg)
+ o Loop on filtering SSL reads until we are blocked or exhausted. (5b4b812)
+
+BUGFIXES (evhttp):
+ o Force strict validation of HTTP version in response. (790f6b3 Catalin Patulea)
+
+BUGFIXES (evdns):
+ o evdns: fix a bug in circular-queue implementation (d6094b1)
+
+BUILD FIXES:
+ o Fix a silly compilation error with the sun compiler (1927776 Colin Watt)
+ o Suppress a gcc warning from ignoring fwrite return in http-sample.c (7206e8c)
+
+DOCUMENTATION FIXES:
+ o Slightly clarify evbuffer_peek documentation (7bbf6ca)
+ o Update copyright notices to 2012 (e49e289)
+
+NEW APIS:
+ o Backport evhttp_connection_get_bufferevent to Libevent 2.0 (da70fa7 Arno Bakker)
+
+TESTS AND TEST FIXES:
+ o Fix a race condition in the dns/bufferevent_connect_hostname test. (cba48c7)
+ o Add function to check referential integrity of an event_base (27737d5)
+ o Check event_base correctness at end of each unit test (3312b02)
+ o Workaround in the unit tests for an apparent epoll bug in Linux 3.2 (dab9187)
+ o Better workaround for Linux 3.2 edge-triggered epoll bug (9f9e259)
+
+Changes in version 2.0.16-stable (18 Nov 2011)
+BUGFIXES (core):
+ o More detailed message in case of libevent self-debugging failure. (9e6a4ef Leonid Evdokimov)
+ o epoll: close fd on alloc fail at initialization (1aee718 Jamie Iles)
+ o Fix compile warning from saying event2/*.h inside a comment (447b0ba)
+ o Warn when unable to construct base because of failing make_base_notifiable (4e797f3)
+ o Don't try to make notifiable event_base when no threading fns are configured (e787413)
+
+BUGFIXES (evbuffer):
+ o unit test for remove_buffer bug (90bd620 Greg Hazel)
+ o Fix an evbuffer crash in evbuffer_remove_buffer() (c37069c)
+
+BUGFIXES (bufferevent_openssl):
+ o Refactor amount-to-read calculations in buffervent_ssl consider_reading() (a186e73 Mark Ellzey)
+ o Move SSL rate-limit enforcement into bytes_to_read() (96c562f)
+ o Avoid spinning on OpenSSL reads (2aa036f Mark Ellzey)
+
+BUGFIXES (dns)
+ o Empty DNS reply with OK status is another way to say NODATA. (21a08d6 Leonid Evdokimov)
+
+TESTING:
+ o Tests for 94fba5b and f72e8f6 (d58c15e Leonid Evdokimov)
+ o Test for commit aff6ba1 (f7841bf Leonid Evdokimov)
+ o Style and comment tweaks for dns/leak* tests (5e42202)
+ o improve test to remove at least one buffer from src (7eb52eb Greg Hazel)
+
+DOCUMENTATION:
+ o Add note about evhttp_send_reply_end to its doxygen (724bfb5)
+ o Update copyright dates to 2011. (3c824bd)
+ o Fix typo in whatsnew-2.0.txt (674bc6a Mansour Moufid)
+ o Improve win32 behavior of dns-sample.c code (a3f320e Gisle Vanem)
+
+
+
+Changes in version 2.0.15-stable (12 Oct 2011)
+BUGFIXES (DNS):
+ o DNS: add ttl for negative answers using RFC 2308 idea. (f72e8f6 Leonid Evdokimov)
+ o Add DNS_ERR_NODATA error code to handle empty replies. (94fba5b Leonid Evdokimov)
+
+BUFGIXES (bufferevents and evbuffers):
+ o Make evbuffer callbacks get the right n_added value after evbuffer_add (1ef1f68 Alex)
+ o Prefer mmap to sendfile unless a DRAINS_TO_FD flag is set. Allows add_file to work with SSL. (0ba0af9)
+
+BUGFIXES (event loop):
+ o When a signal callback is activated to run multiple times, allow event_base_loopbreak to work even before they all have run. (4e8eb6a)
+
+DOCUMENTATION FIXES:
+ o Fix docstring in dns.h (2b6eae5 Leonid Evdokimov)
+ o refer to non-deprecated evdns functions in comments (ba5c27d Greg Hazel)
+
+BUILD AND TESTING FIXES:
+ o le-proxy and regress depend on openssl directly (9ae061a Sergey Avseyev)
+ o Use _SOURCES, not _sources, in sample/Makefile.am (7f82382)
+ o Fixed compiler warnings for unchecked read/write calls. (c3b62fd Mark Ellzey)
+ o Make write-checking fixes use tt_fail_perror (2b76847)
+ o Fix some "value never used" warnings with gcc 4.6.1 (39c0cf7)
+
+
+
+Changes in version 2.0.14-stable (31 Aug 2011)
+BUGFIXES (bufferevents and evbuffers):
+ o Propagate errors on the underlying bufferevent to the user. (4a34394 Joachim Bauch)
+ o Ignore OpenSSL deprecation warnings on OS X (5d1b255 Sebastian Hahn)
+ o Fix handling of group rate limits under 64 bytes of burst (6d5440e)
+ o Solaris sendfile: correctly detect amount of data sent (643922e Michael Herf)
+ o Make rate limiting work with common_timeout logic (5b18f13)
+ o clear read watermark on underlying bufferevent when creating filtering bev to fix potentially failing fragmented ssl handshakes (54f7e61 Joachim Bauch)
+
+BUGFIXES (IOCP):
+ o IOCP: don't launch reads or writes on an unconnected socket (495c227)
+ o Make IOCP rate-limiting group support stricter and less surprising. (a98da7b)
+ o Have test-ratelim.c support IOCP (0ff2c5a)
+ o Make overlapped reads result in evbuffer callbacks getting invoked (6acfbdd)
+ o Correctly terminate IO on an async bufferevent on bufferevent_free (e6af35d)
+
+BUGFIXES (other):
+ o Fix evsig_dealloc memory leak with debugging turned on. (9b724b2 Leonid Evdokimov)
+ o Fix request_finished memory leak with debugging turned on. (aff6ba1 Leonid Evdokimov)
+
+BUILD AND TESTING FIXES:
+ o Allow OS-neutral builds for platforms where some versions have arc4random_buf (b442302 Mitchell Livingston)
+ o Try to fix 'make distcheck' errors when building out-of-tree (04656ea Dave Hart)
+ o Clean up some problems identified by Coverity. (7c11e51 Harlan Stenn)
+
+
+Changes in version 2.0.13-stable (18 Jul 2011)
+BUGFIXES
+ o Avoid race-condition when initializing global locks (b683cae)
+ o Fix bug in SSL bufferevents backed by a bev with a write high-watermarks (e050703 Joachim Bauch)
+ o Speed up invoke_callbacks on evbuffers when there are no callbacks (f87f568 Mark Ellzey)
+ o Avoid a segfault when all methods are disabled or broken (27ce38b)
+ o Fix incorrect results from evbuffer_search_eol(EOL_LF) (4461f1a)
+ o Add some missing checks for mm_calloc failures (89d5e09)
+ o Replace an assertion for event_base_free(NULL) with a check-and-warn (09fe97d)
+ o Report kqueue ebadf, epipe, and eperm as EV_READ events (1fd34ab)
+ o Check if the `evhttp_new_object' function in `http.c' returns NULL. (446cc7a Mansour Moufid)
+ o Use the correct printf args when formatting size_t (3203f88)
+ o Complain if the caller tries to change threading cbs after setting them (cb6ecee)
+
+DOCUMENTATION FIXES AND IMPROVEMENTS
+ o Revise the event/evbuffer/bufferevent doxygen for clarity and accuracy (2888fac)
+ o Update Doxyfile to produce more useful output (aea0555)
+
+TEST FIXES
+ o Fix up test_evutil_snprintf (caf695a)
+ o Fix tinytest invocation from windows shell (57def34 Ed Day)
+
+BUILD FIXES
+ o Use AM_CPPFLAGS in sample/Makefile.am, not AM_CFLAGS (4a5c82d)
+ o Fix select.c compilation on systems with no NFDBITS (49d1136)
+ o Fix a few warnings on OpenBSD (8ee9f9c Nicholas Marriott)
+ o Don't break when building tests from git without python installed (b031adf)
+ o Don't install event_rpcgen.py when --disable-libevent-install is used (e23cda3 Harlan Stenn)
+ o Fix AIX build issue with TAILQ_FOREACH definition (e934096)
+
+
+Changes in version 2.0.12-stable (4 Jun 2011)
+BUGFIXES
+ o Fix a warn-and-fail bug in kqueue by providing kevent() room to report errors (28317a0)
+ o Fix an assert-inducing fencepost bug in the select backend (d90149d)
+ o Fix failing http assertion introducd in commit 0d6622e (0848814 Kevin Ko)
+ o Fix a bug that prevented us from configuring IPv6 nameservers. (74760f1)
+ o Prevent size_t overflow in evhttp_htmlescape. (06c51cd Mansour Moufid)
+ o Added several checks for under/overflow conditions in evhttp_handle_chunked_read (a279272 Mark Ellzey)
+ o Added overflow checks in evhttp_read_body and evhttp_get_body (84560fc Mark Ellzey)
+
+DOCUMENTATION:
+ o Add missing words to EVLOOP_NONBLOCK documentation (9556a7d)
+
+BUILD FIXES
+ o libssl depends on libcrypto, not the other way around. (274dd03 Peter Rosin)
+ o Libtool brings in the dependencies of libevent_openssl.la automatically (7b819f2 Peter Rosin)
+ o Use OPENSSL_LIBS in Makefile.am (292092e Sebastian Hahn)
+ o Move the win32 detection in configure.in (ceb03b9 Sebastian Hahn)
+ o Correctly detect openssl on windows (6619385 Sebastian Hahn)
+ o Fix a compile warning with zlib 1.2.4 and 1.2.5 (5786b91 Sebastian Hahn)
+ o Fix compilation with GCC 2, which had no __builtin_expect (09d39a1 Dave Hart)
+ o Fix new warnings from GCC 4.6 (06a714f)
+ o Link with -lshell32 and -ladvapi32 on Win32. (86090ee Peter Rosin)
+ o Make the tests build when OpenSSL is not available. (07c41be Peter Rosin)
+ o Bring in the compile script from automake, if needed. (f3c7a4c Peter Rosin)
+ o MSVC does not provide S_ISDIR, so provide it manually. (70be7d1 Peter Rosin)
+ o unistd.h and sys/time.h might not exist. (fe93022 Peter Rosin)
+ o Make sure TINYTEST_LOCAL is defined when building tinytest.c (8fa030c Peter Rosin)
+ o Fix winsock2.h #include issues with MSVC (3d768dc Peter Rosin)
+ o Use evutil_gettimeofday instead of relying on the system gettimeofday. (0de87fe Peter Rosin)
+ o Always use evutil_snprintf, even if OS provides it (d1b2d11 Sebastian Hahn)
+ o InitializeCriticalSectionAndSpinCount requires _WIN32_WINNT >= 0x0403. (816115a Peter Rosin)
+ o cygwin: make it possible to build DLLs (d54d3fc)
+
+
+
+Changes in version 2.0.11-stable (27 Apr 2011)
+ [Autogenerated from the Git log, sorted and cleaned by hand.]
+BUGFIXES:
+ o Fix evport handling of POLLHUP and POLLERR (b42ce4b)
+ o Fix compilation on Windows with NDEBUG (cb8059d)
+ o Check for POLLERR, POLLHUP and POLLNVAL for Solaris event ports (0144886 Trond Norbye)
+ o Detect and handle more allocation failures. (666b096 Jardel Weyrich)
+ o Use event_err() only if the failure is truly unrecoverable. (3f8d22a Jardel Weyrich)
+ o Handle resize failures in the select backend better. (83e805a)
+ o Correctly free selectop fields when select_resize fails in select_init (0c0ec0b)
+ o Make --enable-gcc-warnings a no-op if not using gcc (3267703)
+ o Fix a type error in our (unused) arc4random_stir() (f736198)
+ o Correctly detect and stop non-chunked http requests when the body is too long (63a715e)
+ o Have event_base_gettimeofday_cached() always return wall-clock time (a459ef7)
+ o Workaround for http crash bug 3078187 (5dc5662 Tomash Brechko)
+ o Fix incorrect assertions and possible use-after-free in evrpc_free() (4b8f02f Christophe Fillot)
+ o Reset outgoing http connection when read data in idle state. (272823f Tomash Brechko)
+ o Fix subtle recursion in evhttp_connection_cb_cleanup(). (218cf19 Tomash Brechko)
+ o Fix the case when failed evhttp_make_request() leaved request in the queue. (0d6622e Tomash Brechko)
+ o Fix a crash bug in evdns server circular list code (00e91b3)
+ o Handle calloc failure in evdns. (Found by Dave Hart) (364291e)
+ o Fix a memory leak on win32 socket->event map. (b4f89f0)
+ o Add a forgotten NULL check to evhttp_parse_headers (12311ff Sebastian Hahn)
+ o Fix possible NULL-deref in evdns_cancel_request (5208544 Sebastian Hahn)
+
+PORTABILITY:
+ o Fall back to sscanf if we have no other way to implement strtoll (453317b)
+ o Build correctly on platforms without sockaddr_storage (9184563)
+ o Try to build correctly on platforms with no IPv6 support (713c254)
+ o Build on systems without AI_PASSIVE (cb92113)
+ o Fix http unit test on non-windows platforms without getaddrinfo (6092f12)
+ o Do not check for gethostbyname_r versions if we have getaddrinfo (c1260b0)
+ o Include arpa/inet.h as needed on HPUX (10c834c Harlan Stenn)
+ o Include util-internal.h as needed to build on platforms with no sockaddr_storage (bbf5515 Harlan Stenn)
+ o Check for getservbyname even if not on win32. (af08a94 Harlan Stenn)
+ o Add -D_OSF_SOURCE to fix hpux builds (0b33479 Harlan Stenn)
+ o Check for allocation failures in apply_socktype_protocol_hack (637d17a)
+ o Fix the check for multicast or broadcast addresses in evutil_check_interfaces (1a21d7b)
+ o Avoid a free(NULL) if out-of-memory in evdns_getaddrinfo. Found by Dave Hart (3417f68)
+
+DEFENSIVE PROGRAMMING:
+ o Add compile-time check for AF_UNSPEC==PF_UNSPEC (3c8f4e7)
+
+BUGS IN TESTS:
+ o Fix test.sh output on solaris (b4f89b6 Dave Hart)
+ o Make test-eof fail with a timeout if we never get an eof. (05a2c22 Harlan Stenn)
+ o Use %s with printf in test.sh (039b9bd)
+ o Add an assert to appease clang's static analyzer (b0ff7eb Sebastian Hahn)
+ o Add a forgotten return value check in the unit tests (3819b62 Sebastian Hahn)
+ o Actually send NULL request in http_bad_request_test (b693c32 Sebastian Hahn)
+ o add some (void) casts for unused variables (65707d7 Sebastian Hahn)
+ o Refactor test_getaddrinfo_async_cancel_stress() (48c44a6 Sebastian Hahn)
+ o Be nice and "handle" error return values in sample code (4bac793 Sebastian Hahn)
+ o Check return value of evbuffer_add_cb in tests (93a1abb Sebastian Hahn)
+ o Remote some dead code from dns-example.c (744c745 Sebastian Hahn)
+ o Zero a struct sockaddr_in before using it (646f9fe Sebastian Hahn)
+
+BUILD FIXES:
+ o Fix warnings about AC_LANG_PROGRAM usage (f663112 Sebastian Hahn)
+ o Skip check for zlib if we have no zlib.h (a317c06 Harlan Stenn)
+ o Fix autoconf bracket issues; make check for getaddrinfo include netdb.h (833e5e9 Harlan Stenn)
+ o Correct an AM_CFLAGS to an AM_CPPFLAGS in test/Makefile.am (9c469db Dave Hart)
+ o Fix make distcheck & installation of libevent 1 headers (b5a1f9f Dave Hart)
+ o Fix compilation under LLVM/clang with --enable-gcc-warnings (ad9ff58 Sebastian Hahn)
+
+FEATURES:
+ o Make URI parser able to tolerate nonconformant URIs. (95060b5)
+
+DOCUMENTATION:
+ o Clarify event_set_mem_functions doc (926f816)
+ o Correct evhttp_del_accept_socket documentation on whether socket is closed (f665924)
+ o fix spelling mistake in whatsnew-2.0.txt (deb2f73)
+ o Fix sample/http-server ipv6 fixes (eb692be)
+ o Comment internal headers used in sample code. (4eb281c)
+ o Be explicit about how long event loops run in event.h documentation (f95bafb)
+ o Add comment to configure.in to explain gc-sections test logic (c621359)
+ o Fix a couple of memory leaks in samples/http-server.c. Found by Dave Hart. (2e9f665)
+
+BUILD IMPROVEMENTS:
+ o Use the gcc -ffunction-segments feature to allow gc when linking with static libevent (0965c56 Dave Hart)
+ o Add configure options to disable installation, regression tests (49e9bb7 Dave Hart)
+
+
+
+Changes in version 2.0.10-stable (16 Dec 2010)
+ [Autogenerated from the Git log, sorted and cleaned by hand.]
+BUGFIXES
+ o Minor fix for IOCP shutdown handling fix (2599b2d Kelly Brock)
+ o Correctly notify the main thread when activating an event from a subthread (5beeec9)
+ o Reject overlong http requests early when Expect:100-continue is set (d23839f Constantine Verutin)
+ o EVUTIL_ASSERT: Use sizeof() to avoid "unused variable" warnings with -DNDEBUG. (b63ab17 Evan Jones)
+
+CODE CLEANUPS
+ o bufferevent-internal.h: Use the new event2/util.h header, not evutil.h (ef5e65a Evan Jones)
+ o Use relative includes instead of system includes consistently. (fbe64f2 Evan Jones)
+ o Make whitespace more consistent
+
+TESTING
+ o tests: Use new event2 headers instead of old compatibility headers. (4f33209 Evan Jones)
+
+DOCUMENTATION
+ o Document that the cpu_hint is only used on Windows with IOCP for now (57689c4)
+ o Add stuff to "whats new in 2.0" based on reading include changes since August. (18adc3f)
+
+
+Changes in 2.0.9-rc (30 Nov 2010):
+ [Autogenerated from the Git log, sorted and cleaned by hand.]
+NEW AND MODIFIED APIs
+ o Add a function to change a listener's callback. (46ee061)
+ o Make evbuffer_add_file take ev_off_t, not off_t (ac7e52d)
+ o Make rate-limits go up to SIZE_MAX/EV_SSIZE_MAX, not just INT32_MAX (2cbb1a1)
+ o Add a bufferevent_get_base function (aab49b6)
+
+MAJOR BUGFIXES
+ o Disable changelist for epoll by default because of Linux dup() bug; add an option and/or an envvar to reenable it for speed. (9531763)
+ o Fix a 100%-CPU bug where an SSL connection would sometimes never stop trying to write (1213d3d)
+ o Fix a nasty bug related to use of dup() with epoll on Linux (c281aba)
+ o Fix bugs in posix thread-id calculation when sizeof(pthread_t) != sizeof(long) (fbaf077)
+ o Fix some ints to evutil_socket_t; make tests pass on win64. (f817bfa Dimitre Piskyulev)
+ o Set _EVENT_SIZEOF_VOID_P correctly on win32 and win64 (1ae82cd Dimitre Piskyulev)
+ o Avoid double-invocation of user callback with EVUTIL_EAI_CANCEL (abf01ed)
+ o Set SO_UPDATE_ACCEPT_CONTEXT on sockets from AcceptEx so that shutdown() can work (52aa419)
+ o When closing a filtering bufferevent, clear callbacks on the underlying bufferevent (fc7b1b0)
+
+NEW AND MODIFIED HTTP APIs
+ o Add evhttp_parse_query_str to be used with evhttp_uri_parse. (2075fbc)
+ o Add evhttp_response_code to remove one more reason to include http_struct.h (22e0a9b)
+ o Define enumerators for all HTTP methods, including PATCH from RFC5789 (75a7341 Felix Nawothnig)
+ o Functions to actually use evhttp_bound_socket with/as evconnlistener. (006efa7)
+ o Add evhttp_request_get_command so code can tell GET from POST without peeking at the struct. (49f4bf7)
+ o Introduce absolute URI parsing helpers. (86dd720 Pavel Plesov)
+ o Revise evhttp_uri_parse implementation to handle more of RFC3986 (eaa5f1d)
+ o Add evhttp_connection_get_base() to get the event_base from an http connection (cd00079)
+ o Let evhttp_parse_query return -1 on failure (b1756d0)
+ o New evhttp_uri(encode|decode) functions to handle + and NUL characters right (a8148ce)
+ o Add evhttp_response_code to remove one more reason to include http_struct.h (22e0a9b)
+ o Tweak interface for allowed methods (f5b391e)
+ o Add evhttp server alias interface, correct flagging of proxy requests. (aab8c38 Christopher Davis)
+
+HTTP BUGFIXES
+ o Add some comments to http.c and make a few functions static. (90b3ed5)
+ o Fix Content-Length when trying send more than 100GB of data (!) on an evhttp. (525da3e)
+ o Fix a bug where we would read too much data in HTTP bodies or requests. (58a1cc6)
+ o Correctly count req->body_size on http usage without Content-Length (8e342e5)
+ o Avoid missed-request bug when entire http request arrives before data is flushed (74c0e86)
+ o reset "chunked" flag when sending non-chunked reply (aa5f55f Joachim Bauch)
+ o evhttp_encode_uri encodes all reserved characters, including !$'()*+,/:=@ (2e63a60)
+ o Replace exact-version checks for HTTP/1.1 with >= or < checks (647e094)
+ o evhttp: Return 501 when we get an unrecognized method, not 400. (536311a)
+ o Don't disable reading from the HTTP connection after sending the request to be notified of connection-close in time (c76640b Felix Nawothnig)
+ o Never call evhttp_readcb while writing. (0512487)
+ o Try to fix an assertion failure related to close detection (0faaa39)
+ o Correctly detect timeouts during http connects (04861d5)
+ o Preliminary support for Continue expectation in evhttp. (fa9305f Christopher Davis)
+
+OTHER BUGFIXES
+ o Correct logic for realigning a chain in evbuffer_add (e4f34e8)
+ o Fix a minor syntax error that most compilers didn't care about (e56ff65)
+ o Fix some uses of int for socket in regress (5d389dc)
+ o Check return value for ioctlsocket on win32 (f5ad31c Trond Norbye)
+ o Fix som event_warns that should have been event_warnx (19c71e7)
+ o Fix signal handler types for win64. (b81217f)
+ o Try to clear up more size_t vs int/long issues. (598d133)
+ o Make sure IOCP evconnlistener uses virtual events. (7b40a00 Christopher Davis)
+ o Don't free evdns_request handles until after the callback is invoked (9ed30de)
+ o Fix some more cancel-related bugs in getaddrinfo_async (c7cfbcf)
+ o Make evdns_getaddrinfo_cancel threadsafe (d51b2fc)
+ o Only clear underlying callbacks when the user hasn't reset them. (1ac5b23)
+ o Fix bug in bufferevent_connect on an openssl bufferevent that already had an fd (4f228a1)
+ o Resolve an evport bug in the thread/forking test (3a67d0b)
+ o Make sure the CLOEXEC flag is set on fds we open for base notification (3ab578f)
+ o Fix IRIX build. sa_family collides with a #define in sys/socket.h on IRIX. (e874982 Kevin Bowling)
+ o If not WIN32, include <sys/socket.h> in event2/util.h. (1cd45e5 Kevin Bowling)
+ o Fix some C99-style comments to work with the xlC compiler. (c2e5e22 Kevin Bowling)
+ o Add some checks since lack of TAILQ_FOREACH doesn't imply lack of FIRST, END, NEXT, or INSERT_BEFORE. Quiet some warnings in XL C. (c4dc335 Kevin Bowling)
+ o Reworked AIX __ss_family workaround to use AC_STRUCT_MEMBER. (2e2a3d7 Kevin Bowling)
+ o Take select from <sys/select.h> when testing in autoconf. AIX build fix. (a3a9f6b Kevin Bowling)
+ o Fix snprintf related failures on IRIX. (3239073 Kevin Bowling)
+ o Remove _event_initialized(); make event_initialized() a function(); make it consistent on windows and non-windows (652024b)
+ o Do not let EVLOOP_ONCE exit the loop until all deferred callbacks have run (2d5e1bd)
+ o Make EVLOOP_ONCE ignore internal events (0617a81)
+ o Possible crash fix when freeing an underlying bufferevent of an openssl bufferevent (29f7623)
+
+HTTP CLEANUPS
+ o Stop using Libevent-1 headers in regress_http (1f507d7)
+ o Modernize header usage in bench_http.c (e587069)
+ o fix signed/unsigned warnings in http.c (74a91e5)
+ o Update the HTTP regression tests to use Libevent2 apis for non-http stuff (d9ffa89)
+ o Start porting http tests to not use legacy interfaces (8505a74)
+ o Convert the rest of the http tests to be non-legacy unit tests. (9bb8239)
+ o Rename the confusing "base" static variable in regress_http.c (353402a)
+ o Stop accessing http request struct directly from in the unit tests. (0b137f4)
+ o Refactor http version parsing into a single function (a38140b)
+
+TESTING
+ o Improvements to tinytest_macros.h (ad923a1)
+ o Add a huge pile of tests for the new URI functions, and make them pass. (a5a76e6)
+ o Unit tests for evhttp_uri_set* (bc98f5e)
+ o Increase the skew tolerance to 2 seconds in thread/deferred_cb_skew (f806476 Christopher Davis)
+ o Reorder backends in test.sh to match preference order in event.c (ece974f)
+ o Add a stress test for getaddrinfo_cancel (da1bf52)
+ o Units test for unexpected evhttp methods. (75e3320)
+
+DOCUMENTATION
+ o Document behavior of URI parsing more thoroughly. (3a33462)
+ o Document that two bufferevent functions only work on socket bufferevents (70e1b60)
+ o add a requested docstring for event_rpcgen.CommandLine.__init__ (f1250eb)
+ o Fix a mistake in http documentation found by Julien Blache (229714d)
+ o Add a basic example of how to write a static HTTP server. (4e794d5)
+ o Document event_get_assignment (88be27d)
+ o Note that reentrant calls to libevent from logging cbs may fail badly (e431bcd)
+ o Clarify EVLOOP_* documentation to be more precise. (057a514)
+
+CLEANUPS
+ o Simplify the logic for choosing EPOLL_CTL_ADD vs EPOLL_CTL_MOD (2c66983)
+ o Rename "size" variables in win32select that were really fd counts. (b6a158c)
+ o Fix even more win64 warnings (7484df6)
+ o Fix even more win64 warnings: buffer, event_tagging, http, evdns, evrpc (545a611)
+ o Fix more wn64 warnings. (34b84b9 Christopher Davis)
+ o Use the label_len local variable in evdns instead of recalculating it over and over (ba01456)
+ o Fix some irix compilation warnings spotted by Kevin Bowling (7bcace2)
+
+
+
+Changes in 2.0.8-rc (14 Oct 2010):
+ [Autogenerated from the Git log, sorted and cleaned by hand.]
+NEW APIS
+ o Add error callback to evconnlistener (c4be8d8 Simon Perreault)
+ o Add a LEV_OPT_THREADSAFE option for threadsafe evconnlisteners (127d4f2)
+
+CHANGED BEHAVIOR
+ o Correct logic on disabling underlying bufferevents when disabling a filter (ac27eb8)
+
+BUGFIXES
+ o Obey enabled status when unsuspending (040a019 Simon Perreault)
+ o Warn when using the error-prone EV_SIGNAL interface in an error-prone way. Also, fix a couple of race conditions in signal.c (720bd93)
+ O Make default signal backend fully threadsafe (95a7d41)
+ o Put internal events at highest priority (90651b3)
+ o Fix warnings in the main codebase flagged by -Wsigned-compare (9c8db0, 5e4bafb, 5c214a, 6be589a, e06f514)
+ o Fix compile in kqueue.c (b395392 Sebastian Hahn)
+ o Do not search outside of the system directory for windows DLLs (d49b5e3)
+ o Fix a spurious-call bug on epoll.c (0faaee0)
+ o Send a shutdown(SHUT_WR) before closing an http connection (e0fd870 Christopher Davis)
+ o Fix warnings on mingw with gcc 4.5 (5b7a370)
+ o Fix an EINVAL on evbuffer_write_iovec on OpenSolaris. (fdc640b)
+ o Fix allocation error for IOCP listeners. Probably harmless, since struct event is big (481ef92)
+ o Make iocp/listener/error work; don't accept again if lev is disabled. (62b429a Christopher Davis)
+ o Handle rate-limiting for reading on OpenSSL bufferevents correctly. (819b171)
+ o Fix serious bugs in per-bufferevent rate-limiting code (34d64f8)
+ o Avoid spurious reads from just-created open openssl bufferevents (223ee40)
+ o Fix a case where an ssl bufferevent with CLOSE_ON_FREE didn't close its fd (93bb7d8)
+ o The corrected bufferevent filter semantics let us fix our openssl tests (34331e4)
+
+TESTING
+ o Make SSL tests cover enabling/disabling EV_READ. (a5ce9ad)
+ o Bump to the latest version of tinytest (f0bd83e)
+ o Unit tests for listener error callbacks (045eef4)
+ o New unit test for ssl bufferevents starting with connected SSLs. (02f6259)
+
+DEBUGGABILITY
+ o Make debugging output for epoll backend more comprehensive (ec2b05e)
+ o Make event.c debugging messages report fds (e119899)
+ o Make the --enable-gcc-warnings option include signed comparison warnings (d3b096c)
+
+DEADCODE REMOVAL
+ o Remove the now-useless evsig_caught and evsig_process (4858b79)
+ o Remove event_base.evsigbase; nothing used it. (38d0960)
+
+
+
+Changes in 2.0.7-rc (9 Sep 2010):
+ [Autogenerated from the Git log, sorted and cleaned by hand.]
+NEW APIS
+ o Expose a evdns_base_nameserver_sockaddr_add() function to add a nameserver by sockaddr (1952143)
+ o Add event_config_set_num_cpus_hint() for tuning win32 IOCP thread pools, etc. (2447fe8 Christopher Davis)
+
+BUGFIXES
+ o Fix a nasty dangling-event bug when using rate-limiting groups (0bffe43)
+ o Clean up syntax on TAILQ_ENTRY() usage to build correctly with recent MSVC (60433a0 Gilad Benjamini)
+ o Make definition of WIN32_LEAN_AND_MEAN in event.h conditional (3920172 Gilad Benjamini)
+ o Correctly detect failure to delete bufferevent read-timeout event (da6e7cd)
+ o Set close-on-exec bit for filedescriptors created by dns subsystem (d0b8843)
+ o Fix kqueue correctness test on x84_64 (6123d12)
+ o Detect events with no ev_base; warn instead of crashing (f1074b7)
+ o Fix an issue with forking and signal socketpairs in select/poll backends (d61b2f3)
+ o Stop using global arrays to implement the EVUTIL_ctype functions (1fdec20)
+ o On windows, make lock/thread function tables static (5de2bcb)
+ o Close th_notify_fds and open a new pair on reinit (495ed66)
+ o Declare signal handler function as "__cdecl" on Windows (f0056d0)
+ o Use the _func() replacements for open, fstat, etc in evutil.c on win32 (e50c0fc)
+ o Only process up to MAX_DEFERRED deferred_cbs at a time (17a14f1 Christopher Davis)
+
+THREADING BUGFIXES
+ o Avoid deadlock when activating signals (970e6ad)
+ o Add a condition variable backend, with implementations for pthreads and win32 (d4977b5)
+ o Use conditions instead of current_event_lock to fix a deadlock (e0972c2)
+ o Fix logic error in win32 TRY_LOCK that caused problems with rate-limiting (4c32b9d)
+ o Avoid needlessly calling evthread_notify_base() when the loop is not running (c7a06bf)
+ o Minimize calls to base_notify implementation functions, thereby avoiding needless syscalls (4632b78)
+
+IOCP BUGFIXES
+ o IOCP-related evbuffer fixes (03afa20 Christopher Davis)
+ o Stop IOCP when freeing the event_base (d844242 Christopher Davis)
+ o Some IOCP bufferevent tweaks (76f7e7a Christopher Davis)
+
+TESTS
+ o Make the regress_pthread.c tests work on windows with current test APIs (d74ae38)
+ o Add a unit test for conditions (5fb1095)
+ o Allow more than one copy of regression tests to run at once (a97320a)
+ o Fix event_del(0) instance in bench.c (b0f284c Shuo Chen)
+ o Fix a few memory leaks in the tests (1115366)
+ o IOCP-related unit test tweaks (499452f Christopher Davis)
+ o Improve testing of when thread-notification occurs (ce85280)
+
+BUILD AND DISTRIBUTION
+ o Add pkgconfig files for libevent_{openssl,pthreads} (ebcb1f0)
+ o Change include order in Makefile.nmake (4022b28)
+ o Make include/event2/event-config.h not included in source dist (a4af9be)
+ o Honor NDEBUG; build without warnings with NDEBUG; make NDEBUG always-off in unit test code (743f866)
+ o Declare evkeyvalq and event_list even if event_struct.h comes before sys/queue.h (d3ceca8)
+ o Move evkeyvalq into a separate header for evhttp_parse_query users (ca9048f)
+ o Prefer autoreconf -ivf to manual autogen.sh (7ea8e89)
+
+CLEANUP
+ o Completely remove the (mostly-removed) obsolete thread functions (3808168)
+ o Rename regress_pthread.c to regress_thread.c (041989f)
+ o Make defer-internal.h use lock macros, not direct calls to lock fns (5218d2a)
+
+DOCUMENTATION
+ o Document that DNS_NO_SEARCH is an obsolete alias for DNS_QUERY_NO_SEARCH (33200e7)
+ o Update the whatsnew-2.0.txt document (4991669)
+
+
+
+Changes in 2.0.6-rc (6 Aug 2010):
+ [Autogenerated from the Git log, sorted by hand.]
+DOCUMENTATION
+ o Document a change in the semantics of event_get_struct_event_size() (e21f5d1)
+ o Add a comment to describe our plan for library versioning (9659ece)
+ o Fix sentence fragment in docs for event_get_struct_event_size() (7b259b6)
+
+NEW FEATURES AND INTERFACE CHANGES
+ o Remove the obsolete evthread interfaces (c5bab56)
+ o Let evhttp_send_error infer the right error reasons (3990669)
+ o Add a function to retrieve the other side of a bufferevent pair (17a8e2d)
+ o Add bufferevent_lock()/bufferevent_unlock() (215e629)
+ o Stop asserting when asked for a (unsupported) TCP dns port. Just return NULL. (7e87a59)
+ o Replace (unused,always 0) is_tcp argument to evdns_add_server_port*() with flags (e1c1167)
+ o Constify a couple of arguments to evdns_server_request_add_*_reply (cc2379d)
+ o Add an interface to expose min_share in ratelimiting groups (6ae53d6)
+
+BUGFIXES
+ o Avoid event_del on uninitialized event in event_base_free (6d19510)
+ o Add some missing includes to fix Linux build again (75701e8)
+ o Avoid close of uninitialized socket in evbuffer unit test (bda21e7)
+ o Correctly recognize .255 addresses as link-local when looking for interfaces (8c3452b)
+ o If no evdns request can be launched, return NULL, not a handle (b14f151)
+ o Use generic win32 interfaces, not ASCII-only ones, where possible. (899b0a3)
+ o Fix the default HTTP error template (06bd056 Felix Nawothnig)
+ o Close the file in evutil_read_file whether there's an error or not. (0798dd1 Pierre Phaneuf)
+ o Fix possible nullptr dereference in evhttp_send_reply_end() (29b2e23 Felix Nawothnig)
+ o never let bufferevent_rlim functions return negative (0859870)
+ o Make sample/hello_world work on windows (d89fdba)
+ o Fix a deadlock related to event-base notification. Diagnosed by Zhou Li, Avi Bab, and Scott Lamb. (17522d2)
+ o Possible fix to 100% cpu usage with epoll and openssl (cf249e7 Mike Smellie)
+ o Don't race when calling event_active/event_add on a running signal event (fc5e0a2)
+ o Suppress a spurious EPERM warning in epoll.c (e73cbde)
+ o Fix wrong size calculation of iovec buffers when exact=1 (65abdc2 niks)
+ o Change bufferevent_openssl::do_write so it doesn't call SSL_write with a 0 length buffer (c991317 Mike Smellie)
+ o Fixed compilation of sample/le-proxy.c on win32 (13b912e Trond Norbye)
+ o Fix rate-limit calculation on openssl bufferevents. (009f300)
+ o Remember to initialize timeout events for bufferevent_async (de1f5d6 Christopher Davis)
+
+BUILD AND DISTRIBUTION CHANGES
+ o Test the unlocked-deferred callback case of bufferevents (dfb75ab)
+ o Remove the now-unusable EVTHREAD_LOCK/UNLOCK constants (fdfc3fc)
+ o Use -Wlogical-op on gcc 4.5 or higher (d14bb92)
+ o Add the libtool-generated /m4/* stuff to .gitignore (c21c663)
+ o Remove some automake-generated files from version control. (9b14911)
+ o Have autogen.sh pass --force-missing to automake (8a44062)
+ o Set library version for libevent_pthreads correctly (b2d7440)
+ o Really only add libevent_core.la to LIBADD on mingw (1425003 Sebastian Hahn)
+ o Build more cleanly with NetBSDs that dislike toupper(char) (42a8c71)
+ o Fix unit tests with -DUSE_DEBUG enabled (28f31a4)
+ o Fix evdns build with -DUNICODE (5fa30d2)
+ o Move event-config.h to include/event2 (ec347b9)
+
+TESTING
+ o Add options to test-ratelim.c to check its results (2b44dcc)
+ o Make test-ratelim clean up after itself better. (b5bfc44)
+ o Remove the now-obsolete setup_test() and cleanup_test() functions (e73f1d7)
+ o Remove all non-error prints from test/regress.c (8bc1e3d)
+ o Make test.sh exit with nonzero status if tests fail (faf2a04)
+ o Have the unit tests report errors from test.sh (3689bd2)
+ o Fix logic in correcting high values from FIONREAD (3467f2f)
+ o Add test for behavior on remote socket close (44d57ee)
+ o Unit test for event_get_struct_event_size() (7510aac)
+ o Make test/test.sh call test-changelist (7c92691)
+ o Fix badly-behaved subtest of dns/bufferevent_connect_hostname (840a72f Joachim Bauch)
+ o Add option to test-ratelim to test min_share (42f6b62)
+ o Fix an assertion bug in test-ratelim (b2c6202)
+ o Make tests quieter on local dns resolver failure (e996b3d)
+ o Increase the tolerance in our unit tests for sloppy clocks. (170ffd2)
+ o Use AF_INET socketpair to test sendfile on Solaris (9b60209)
+ o Make test-changelist count cpu usage right on win32 (ea1ea3d)
+
+INTERNALS, PERFORMANCE, AND CODE CLEANUPS
+ o Mark the event_err() functions as __attribute__((noreturn)) (33bbbed)
+ o Do not check that event_base is set in EVBASE_ACQUIRE_LOCK (218a3c3)
+ o Replace (safe) use of strcpy with memcpy to appease OpenBSD (caca2f4)
+ o Remove some dead assignments (47c5dfb)
+ o Fix a pedantic gcc 4.4 warning in event2/event.h (276e7ee)
+ o Drain th_notify_fd[0] more bytes at a time. (a5bc15b)
+ o Tidy up the code in evthread_make_base_notifiable a little (61e1eee)
+ o Pass flags to fcntl(F_SETFL) and fcntl(F_SETFD) as int, not long (7c2dea1)
+ o Remove unused variables in test/test-changelist.c (b00d4c0)
+ o Fix whitespace. (cb927a5)
+ o Improve error message for failed epoll to make debugging easier. (9e725f7)
+ o Turn our socketpair() replacement into its own function (57b30cd)
+
+
+
+Changes in 2.0.5-beta (10 May 2010):
+ [Autogenerated from the Git log, sorted by hand.]
+DOCUMENTATION
+ o Update all our copyright notices to say "2010" (17efc1c)
+ o Add Christopher Clark and Maxim Yegorushkin to the LICENSE file (38b7b57)
+ o Clarify Christopher Clark's status as writer of original ht code. (78772c3)
+ o Try to comment some of the event code more (cdd4c49)
+ o Add a few more evmap/changelist comments (c247adc)
+ o Add a comment to explain why evdns_request is now separte from request (ceefbe8)
+ o Document evutil_secure_rng_init() and evutil_secure_rng_add_bytes() (a5bf43a)
+ o Stop distributing and installing manpages: they were too inaccurate (7731ec8)
+
+NEW FEATURES AND INTERFACE CHANGES
+ o Remove signal_assign() and signal_new() macros. (2fac0f7)
+ o Make evdns use the regular logging system by default (b2f2be6)
+ o Allow evbuffer_read() to split across more than 2 iovecs (e470ad3)
+ o Functions to manipulate existing rate limiting groups. (ee41aca)
+ o Functions to track the total bytes sent over a rate limit group. (fb366c1)
+ o Detect and refuse reentrant event_base_loop() calls (b557b17)
+ o Limit the maximum number of events on each socket to 65535 (819f949)
+ o Add evbuffer_copyout to copy data from an evbuffer without draining (eb86c8c)
+ o Expose the request and reply members of rpc_req_generic() (07edf78 Shuo Chen)
+ o Add void* arguments to request_new and reply_new evrpc hooks (755fbf1 Shuo Chen)
+ o Seed the RNG using sysctl() as well as /dev/urandom (71fc3eb)
+ o Make evutil_secure_rng_init() work even with builtin arc4random (f980716)
+ o Report DNS error when lookup fails during bufferevent_socket_connect_hostname. (0ef4070 Christopher Davis)
+ o Release locks on bufferevents while executing callbacks (a5208fe Joachim Bauch) o Make debug mode catch mixed ET and non-ET events on an fd (cb67074)
+ o Catch attempts to enable debug_mode too late (9ecf0d4)
+ o Refuse null keys in evhttp_parse_query() (953e229 Frank Denis)
+
+BUGFIXES
+ o Avoid a spurious close(-1) on Linux (70a44b6)
+ o Do not close(-1) when freeing an uninitialized socket bufferevent (b34abf3)
+ o Free evdns_base->req_heads on evdns_base_free (859af67)
+ o Avoid an (untriggerable so far) crash bug in bufferevent_free() (0cf1431)
+ o Set mem_offset for every bufferevent type (657d1b6)
+ o Fix infrequent memory leak in bufferevent_init_common(). (8398641 Jardel Weyrich)
+ o Make evutil_signal_active() match declaration. (e1e703d Patrick Galbraith)
+ o Fix minheap code to use replacement malloc functions (a527618)
+ o Fix a free(NULL) in minheap-internal.h (6f20492)
+ o Fix critical bug in evbuffer_write when writev is not available (cda56ab)
+ o Make the no_iovecs case of write_atmost compile (8e227b0)
+ o Fix a memory leak when appending/prepending to a buffer with unused space. (45068a3)
+ o Clean up a mistake in pointer manipulation in evbuffer_remove (28bfed4 Christopher Davis)
+ o Always round up when there's a fractional number of msecs. (8f9e60c Christopher Davis)
+ o Fix compiler warnings under WIN32 (d469c50 Giuseppe Scrivano)
+ o Clean up properly when adding a signal handler fails. (b84b598 Gilad Benjamini) o Ensure that evdns_request is a persistent handle. (15bb82d Christopher Davis)
+ o Free search state when finished searching to avoid an infinite loop. (a625840 Christopher Davis)
+ o Assert for valid requests as necessary. (67072f3 Christopher Davis)
+ o do not leak the request object on persistent connections (9d8edf2)
+ o Make evdns logging threadsafe (b1c7950)
+ o Fix a couple of bugs in the BSD sysctl arc4seed logic (a47a4b7)
+ o Remove one last bug in last_with_datap logic. Found with valgrind (d49b92a)
+ o fix a leak when unpausing evrpc requests (94ee125)
+ o Fix a memory leak when unmarshalling RPC object arrays (f6ab2a2)
+ o Fix compilation when openssl support is disabled (40c301b)
+ o Allow empty reason line in HTTP status (739e688 Pierre Phaneuf)
+ o Fix a compile warning introduced in 739e688 (bd1ed5f Sebastian Hahn)
+ o Fix nonstandard TAILQ_FOREACH_REVERSE() definition (71afc52 Frank Denis)
+ o Try /proc on Linux as entropy fallback; use sysctl as last resort (20fda29)
+ o Fix symbol conflict between mm_*() macros and libmm (99e50e9)
+ o Fix some crazy macro mistakes in arc4random.c (90d4225)
+ o Make evbuffer_add_file() work on windows (dcdae6b)
+ o Fix unused-variable warning when building with threads disabled (ad811cd)
+ o Numerous opensolaris compilation fixes (c44de06)
+ o Fix getaddrinfo with protocol unset on Solaris 9. Found by Dagobert Michelsen (2cf2a28)
+ o Fix another nasty solaris getaddrinfo() behavior (3557071)
+ o Define _REENTRANT as needed on Solaris, elsewhere (c1cd32a)
+ o Fix some autoconf issues on OpenBSD (7c519df)
+
+BUILD AND DISTRIBUTION CHANGES
+ o Distribute libevent.pc.in, not libevent.pc (22aff04)
+ o Avoid errors in evutil.c when building with _UNICODE defined (b677032 Brodie Thiesfield)
+ o Avoid errors in http.c when building with VC 2003 .NET (13e4f3b Brodie Thiesfield)
+ o Support the standard 'make check' target in place of 'make verify' (426c8fb)
+ o Remove redundant stuff from EXTRA_DIST (b660edf)
+ o Switch to using AM conditionals in place of AC_LIBOBJ (2e898f5)
+ o Remove an orphaned RELEASE flag in Makefile.am (0794b0d)
+ o Give a better warning for bad automake versions. (77c917d)
+ o Use dist_bin_SCRIPTS, not EXTRA_DIST, to distribute scripts (9eb2fd7)
+ o Never test for select() on windows (3eb044d Trond Norbye)
+ o Do not inhibit automake dependencies generation (10c4c90 Giuseppe Scrivano)
+ o Create shared libraries under Windows (3cbca86 Giuseppe Scrivano)
+ o Add ctags/etags files to .gitignore (0861d17)
+ o Only specify -no-undefined on mingw (25433b9)
+ o Only add libevent_core.la to LIBADD on mingw (fdc6297)
+
+TESTING
+ o Get bench_http to work on Windows; add a switch to enable IOCP. (4ac38a5 Christopher Davis)
+ o VC has no getopt(), so do without in bench_http. (1273d2f Christopher Davis)
+ o Fix an obnoxious typo in the bufferevent_timeout_filter test (0d047c3)
+ o Fix a write of uninitialized RAM in regression tests (68dc742)
+ o Fix some memory leaks in the unit tests (274a7bd)
+ o Make 'main/many_events' test 70 fds, not 64. (33874b0)
+ o Unit-test every evbuffer_add_file() implementation. (06a4443)
+ o Add more unit tests for evbuffer_expand (8c83e99)
+ o Test another case of evbuffer_prepend (1234b95)
+ o Fix a possible double-free bug in SSL bufferevents with CLOSE_ON_FREE (7501895) o Add dns/search_cancel unit test. (39b870b Christopher Davis)
+ o Make http_base_test stop leaking an event_base. (96730d3)
+ o Detect broken unsetenv at unit-test runtime (f37cd4c)
+ o Implement regress_make_tempfile on win32 to test evbuffer_add_file (b4f12a1)
+ o add more (currently skipped) add_file tests on win32 (05de45d)
+ o Fix bench_http build on win32. (384d124)
+ o Make unit test for add_file able to tell "error" from "done" (88a543f)
+ o Make test for bufferevent_connect_hostname system-neutral (f89168e)
+ o Make test.sh support mingw/msys on win32 (0ee6f6c)
+ o Fix test.sh on freebsd (3d9e05b)
+
+INTERNALS, PERFORMANCE, AND AND CODE CLEANUPS
+ o Improve the speed of evbuffer_readln() (cc1600a)
+ o more whitespace normalization (2c2618d)
+ o Revise evbuffer to add last_with_data (2a6d2a1)
+ o Use last_with_data in place of previous_to_last (c8ac57f)
+ o Remove previous_to_last from evbuffer (6f47bd1)
+ o Fix last_with_data compilation on windows (1e7b986)
+ o Add some glass-box tests for the last_with_data code. (17da042)
+ o Improve robustness for refcounting (f1bc125)
+ o Remove a needless min_heap_shift_up_() call (7204b91)
+ o Increase MIN_BUFFER_SIZE to 512 (1024 on 64-bit) (2014ae4)
+ o Do not use evbuffer_expand() to add the first chain to a buffer (5c0ebb3)
+ o Make evbuffer_prepend handle empty buffers better (c87272b)
+ o Replace last_with_data with a slightly smarter version (b7442f8)
+ o Turn the increasingly complex *_CHAIN() macros into functions (96865c4)
+ o Rewrite evbuffer_expand and its users (d5ebcf3)
+ o Add evutil_tv_to_msec for safe conversion of timevals to milliseconds. (850c3ff Christopher Davis)
+ o Initialize last_with_datap correctly in evbuffer_overlapped (a0983b6)
+ o Replace EVUTIL_CLOSESOCKET macro with a function (899c1dc Sebastian Sjöberg)
+ o Move domain search state to evdns_request. (beaa14a Christopher Davis)
+ o Remove redundant checks for lock!=NULL before calling EVLOCK_LOCK (50ec59f)
+ o Rename current_base symbol to event_global_current_base_ (c16e684)
+ o Fix whitespace in evutil.c (935e150)
+ o Replace users of "int fd" with "evutil_socket_t fd" in portable code (c7cf6f0)
+
+
+
+Changes in 2.0.4-alpha (28 Feb 2010):
+ [Autogenerated from the Git log, sorted by hand.]
+DOCUMENTATION
+ o Add stub header for 2.0.4-alpha changelog. (94d0065)
+ o Improve the README with more information and links. (0b42726)
+ o Add more people who wrote patches to the acknowledgments (0af10d5)
+ o Add a warning about the use of event_initialized. (f32b575)
+ o Add a LICENSE file so people can find our license easily (7067006)
+ o Add a new "hello world" sample program (becb9f9)
+ o Clarify status of example programs (d60a1bd)
+ o Update time-test.c to use event2 (f4190bf)
+ o Add the arc4random.c license to the LICENSE file. (e15e1e9)
+
+NEW FEATURES AND INTERFACE CHANGES
+ o Improved optional lock debugging. (0cd3bb9)
+ o Rate-limiting for bufferevents; group and individual limits are supported. (737c9cd)
+ o Testing code for bufferevent rate-limiting. (f0c0124)
+ o Make the initial nameserver probe timeout configurable. (1e56a32)
+ o Revise the locking API: deprecate the old locking callbacks and add trylock. (347952f)
+ o Do not make bufferevent_setfd implicitly disable EV_READ and EV_WRITE. (8274379)
+ o Do not ignore bufferevent_enable(EV_READ) before bufferevent_connect(). (4a5b534)
+ o Introduced evutil_make_socket_closeonexec() to preserve fd flags for F_SETFD. (d0939d2 Jardel Weyrich)
+ o evdns_getaddrinfo() now supports the /etc/hosts file. (72dd666)
+ o Look at the proper /etc/hosts file on windows. (66c02c7)
+ o Allow http connections to use evdns for hostname looksups. (c698b77)
+ o Changelist code to defer event changes until just before dispatch (27308aa)
+ o do not use a function to assign the evdns base; instead assign it via evhttp_connection_base_new() which is a new function introduced in 2.0 (5032e52)
+ o Functions to access more fields of struct event. (0683950)
+ o Make kqueue use changelists. (45e5ae3)
+ o Remove kqueue->pend_changes. (3225dfb)
+ o Minimize epoll_ctl calls by using changelist (c8c6a89)
+ o Add support for a "debug mode" to try to catch common errors. (cd17c3a)
+ o Note a missing ratelim function (361da8f)
+ o Add ev_[u]intptr_t to include/event2/util.h (1fa4c81)
+ o const-ify a few more functions in event.h (d38a7a1)
+ o Deprecate EVENT_FD and EVENT_SIGNAL. (f6b2694)
+ o Remove EVUTIL_CHECK_FMT. (6c21c89)
+ o Add EV_*_MAX macros to event2/util.h to expose limits for ev_* types. (aba1fff) o Functions to view and manipulate rate-limiting buckets. (85047a6)
+ o Add the rest of the integer limits, and add a test for them. (60742d5)
+ o Remove the 'flags' argument from evdns_base_set_option() (1dd7e6d)
+ o Add an arc4random implementation for use by evdns (d4de062)
+ o Use off_t for the length parameter of evbuffer_add_file (3fe60fd)
+ o Construct Windows locks using InitializeCriticalSectionAndSpinCount (32c6f1b)
+ o Expose view of current rate limit as constrained by group limit (162ce8a)
+ o Provide consistent, tested semantics for bufferevent timeouts (d328829)
+
+BUGFIXES AND TESTS
+ o Tolerate code that returns from a fatal_cb. (91fe23f)
+ o Parenthesize macro arguments more aggressively (07e9e9b)
+ o Fix memory-leak of signal handler array with kqueue. (e1ffbb8)
+ o Stop passing EVTHREAD_READ and EVTHREAD_WRITE to non-rw locks. (76cd2b7)
+ o Fix two use-after-free bugs in unit tests spoted by lock debugging (d84d838)
+ o Fix a locking bug in event_base_loop() (da1718b)
+ o Fix an evdns lock violation. (2df1f82 Zhuang Yuyao)
+ o Valgrind fix: Clear struct kevent before checking for OSX bug. (56771a3 William Ahern)
+ o Fix up evthread compilation on windows (bd6f1ba Roman Puls)
+ o Fix regress_iocp.c usage of old lock allocation macros. (31687b4 unknown)
+ o Update nmake makefile to build evthread.c (b62d979 unknown)
+ o Fix a crash when reading badly formatted resolve.conf; from Yasuoka Masahiko (6c7c579 Yasuoka Masahiko)
+ o Fix a snow leopard compile warning in the unit tests. (7ae9445)
+ o Fix compile on Snow Leopard with gcc warnings enabled (70cdfe4 Sebastian Hahn)
+ o Only define _GNU_SOURCE if it is not already defined. (ea6b1df Joachim Bauch)
+ o Update sample/signal-test.c to use newer APIs and not leak. (f6430ac Evan Jones)
+ o Fix a segfault when writing a very fragmented evbuffer onto an SSL (a6adeca Joachim Bauch)
+ o Fix a segfault when freeing SSL bufferevents in an unusual order (a773df5 Joachim Bauch)
+ o Drop install-sh from our git repo: a mismatched version could break "make dist" (6799527)
+ o Set all instances of the version number correctly. (5a112d3)
+ o Fix a few locking issues on windows. (c51bb3c unknown)
+ o Use evutil_socket_t, not int, when logging socket errors. (292467c)
+ o Fix up behavior of never-defered callbacks a little (390e056)
+ o Replace some cases of uint32_t with ev_uint32_t. (a47d88d)
+ o Fix compilation of devpoll.c by adding missing thread includes. (fee2c77 Dagobert Michelsen)
+ o Make evutil_make_socket_nonblocking() leave any other flags alone. (4c8b7cd Jardel Weyrich)
+ o Fix an fd leak in evconnlistener_new_bind(). (24fb502 Jardel Weyrich)
+ o Fix a bogus free in evutil_new_addrinfo() (0d64051 Jardel Weyrich)
+ o Adjusted fcntl() retval comparison on evutil_make_socket_nonblocking(). (4df7dbc Jardel Weyrich)
+ o Fix the code that allowed DNS options to not end with : (ee4953f)
+ o Fix crash bugs when a bufferevent's eventcb is not set. (2e8eeea)
+ o Fix test-ratelim compilation on Linux. (885b427)
+ o Fix compilation of rate-limiting code on win32. (165d30e)
+ o Eradicated the last free() call. Let mm_free() take care of deallocation. (0546ce1 Jardel Weyrich)
+ o Fix byte counts when mixing deferred and non-deferred evbuffer callbacks. (29151e6)
+ o Fixed a memory leak on windows threads implementation. The CRITICAL_SECTION was not being free'd in evthread_win32_lock_free(). (2f33e00 Jardel Weyrich)
+ o Fixed a fd leak in start_accepting(), plus cosmetic changes (4367a33 Jardel Weyrich)
+ o Improved error handling in evconnlistener_new_async(). Also keeping the fd open because it is not opened by this function, so the caller is responsible for closing it. Additionally, since evconnlistener_new_bind() creates a socket and passes it to the function above, it required error checking to close the same socket. (fec66f9 Jardel Weyrich)
+ o Don't use a bind address for nameservers on loopback (8d4aaf9)
+ o Fix compilation of rate-limit code when threading support is disabled (97a8c79)
+ o Detect setenv/unsetenv; skip main/base_environ test if we can't fake them. (7296971)
+ o Check more internal event_add() calls for failure (ff3f6cd)
+ o Fix windows and msvc build (5c7a7bc)
+ o Call event_debug_unassign on internal events (a19b4a0)
+ o Try to fix a warning in hash_debug_entry (137f2c6)
+ o Fix a dumb typo in ev_intptr_t definitions. (27c9a40)
+ o do not fail while sending on http connections the client closed. (93d7369)
+ o make evhttp_send() safe against terminated connections, too (3978180)
+ o Make Libevent 1.4.12 build on win32 with Unicode enabled. (000a33e Brodie Thiesfield)
+ o Fix some additional -DUNICODE issues on win32. (a7a9431)
+ o Add a check to make soure our EVUTIL_AI flags do not conflict with the native ones (c18490e)
+ o Always use our own gai_strerror() replacement. (6810bdb)
+ o Make RNG work when we have arc4random() but not arc4random_buf() (4ec8fea)
+ o validate close cb on server when client connection closes (2f782af)
+ o Fix two unlocked reads in evbuffer. (7116bf2)
+ o When working without a current event base, don't try to use IOCP listeners (cb52838)
+ o Fix getpid() usage on Windows (ff2a134)
+ o Add a unit test for secure rng. (48a29b6)
+ o Add some headers to fix freebsd compilation (b72be50)
+ o When connect() succeeds immediately, don't invoke the callback immediately. (7515de9)
+ o Suspend read/write on bufferevents during hostname lookup (db08f64)
+ o Make bufferevent_free() clear all callbacks immediately. (b2fbeb3)
+ o Fix some race conditions in persistent events and event_reinit (e2642f0)
+ o Fix a bug in resetting timeouts on persistent events when IO triggers. (38ec0a7)
+ o Add a test for timeouts on filtering bufferevents. (c02bfe1)
+ o Add test for periodic timers that get activated for other reasons (8fcb7a1)
+ o Use new timeval diff comparison function in bufferevent test (f3dfe46)
+ o Delete stack-alloced event in new unit test before returning. (7ffd387)
+ o Fix mingw compilation (23170a6)
+ o Try to define a sane _EVENT_SIZEOF_SIZE_T for msvc compilation (1e14f82)
+ o Fix arc4random compilation on MSVC. (98edb89)
+ o deal with connect() failing immediately (7bc48bf)
+ o Small cleanups on freebsd-connect-refused patch. (57b7248)
+
+BUILD AND DISTRIBUTION CHANGES
+ o Remove the contents of WIN32-Prj as unmaintained. (c69d5a5)
+ o Allow the user to redirect the verbose output of test/test.sh to a file (c382de6)
+ o Allow test.sh to be run as ./test/test.sh (7dfbe94)
+ o Never believe that we have pthreads on win32, even if gcc thinks we do. (78ed097)
+ o Make it compile under gcc --std=c89. (e2ca403)
+ o Fix a number of warnings from gcc -pedantic (918e9c5)
+ o Add the msvc-generated .lib files to .gitignore. (e244a2e)
+ o Add the "compile" script to gitignore. (1ba6bed)
+
+INTERNALS AND CODE CLEANUPS
+ o Add a .gitignore file. (ba34071)
+ o New EVTHREAD_TRY_LOCK function to try to grab a lock. (689fc09)
+ o Add the abilitity to mark some buffer callbacks as never-deferred. (438f9ed)
+ o Refactor our 'suspend operation' logic on bufferevents. (0d744aa)
+ o Simplify the read high-watermark checking. (5846bf6)
+ o Improve readability of evutil_unparse_protoname() (5a43df8 Jardel Weyrich)
+ o Expose our cached gettimeofday value with a new interface (47854a8)
+ o Whitespace fixes in test.sh (0b151a9)
+ o Enable branch-prediction hints with EVUTIL_UNLIKELY. (eaaf27f)
+ o Refactor code from evdns into a new internal "read a file" function. (0f7144f)
+ o Comestic changes in evconnlistener_new(), new_accepting_socket(), accepted_socket_invoke_user_cb() and iocp_listener_enable(). (510ab6b Jardel Weyrich)
+ o Add unit-test for bad_request bug fixed in 1.4 recently. (6cc79c6 Pavel Plesov) o Add a comment on evthread_enable_lock_debuging. (b9f43b2)
+ o Fix test.sh on shells without echo -n (94131e9)
+ o More unit tests for getaddrinfo_async: v4timeout and cancel. (a334b31)
+ o Make http use evconnlistener. (ec34533)
+ o move dns utility functions into a separate file so that we can use them for http testing (b822639)
+ o add a test for evhttp_connection_base_new with a dns_base (26714ca)
+ o forgot to add void to test function (78a50fe)
+ o Add a forgotten header (changelist-internal.h) (4b9f307)
+ o Remove some commented-out code in evutil (26e1b6f)
+ o Remove a needless include of rpc_compat.h (70a4a3e)
+ o Use less memory for each entry in a hashtable (a66e947)
+ o Try to untangle the logic in server_port_flush(). (439aea0)
+ o Use ev_[u]intptr_t types in place of [u]intptr_t (cef61a2)
+ o Reduce windows header includes in our own headers. (da6135e)
+ o clean up terminate_chunked test (e8a9782)
+ o Increment the submicro version number. (63e868e)
+ o Update event-config.h version number to match configure.in (aae7db5)
+ o Clean up formatting: Disallow space-before-tab. (8fdf09c)
+ o Clean up formatting: use tabs, not 8-spaces, to indent. (e5bbd40)
+ o Clean up formatting: remove trailing spaces (e5cf987)
+ o Clean up formatting: function/keyword spacing consistency. (4faeaea)
+
+
+
+Changes in 2.0.3-alpha (20 Nov 2009):
+ o Add a new code to support SSL/TLS on bufferevents, using the OpenSSL library (where available).
+ o Fix a bug where we didn't allocate enough memory in event_get_supported_methods().
+ o Avoid segfault during failed allocation of locked evdns_base. (Found by Rocco Carbone.)
+ o Export new evutil_ascii_* functions to perform locale-independent character type operations.
+ o Try to compile better with MSVC: patches from Brodie Thiesfield
+ o New evconnlistener_get_fd function to expose a listener's associated socket.
+ o Expose an ev_socklen_t type for consistent use across platforms.
+ o Make bufferevent_socket_connect() work when the original fd was -1.
+ o Fix a bug in bufferevent_socket_connect() when the connection succeeds too quickly.
+ o Export an evutil_sockaddr_cmp() to compare to sockaddr objects for equality.
+ o Add a bufferevent_get_enabled() to tell what a bufferevent has been configured to do.
+ o Add an evbuffer_search_eol() function to locate the end of a line nondestructively.
+ o Add an evbuffer_search_range() function to search a bounded range of a buffer.
+ o Fix a rare crash bug in evdns.
+ o Have bufferevent_socket_connect() with no arguments put a bufferevent into connecting mode.
+ o Support sendfile on Solaris: patch from Caitlin Mercer.
+ o New functions to explicitly reference a socket used by an evhttp object. Patches from David Reiss.
+ o When we send a BEV_EVENT_CONNECTED to indicate connected status, we no longer invoke the write callback as well unless we actually wrote data too.
+ o If the kernel tells us that there are a negative number of bytes to read from a socket, do not believe it. Fixes bug 2841177; found by Alexander Pronchenkov.
+ o Do not detect whether we have monotonic clock support every time a new event base is created: instead do it only once. Patch taken from Chromium.
+ o Do not allocate the maximum event queue for the epoll backend at startup. Instead, start out accepting 32 events at a time, and double the queue's size when it seems that the OS is generating events faster than we're requesting them. Saves up to 374K per epoll-based event_base. Resolves bug 2839240.
+ o Treat an event with a negative fd as valid but untriggerable by Libevent. This is useful for applications that want to manually activate events.
+ o Fix compilation on Android, which forgot to define fd_mask in its sys/select.h
+ o Do not drop data from evbuffer when out of memory; reported by Jacek Masiulaniec
+ o New event_base_got_exit() and event_base_got_break() functions to tell whether an event loop exited because of an event_base_loopexit() or an event_base_loopbreak(). Patch from Ka-Hing Cheung.
+ o When adding or deleting an event from a non-main thread, only wake up the main thread when its behavior actually needs to change.
+ o Fix some bugs when using the old evdns interfaces to initialize the evdns module.
+ o Detect errors during bufferevent_connect(). Patch from Christopher Davis.
+ o Fix compilation for listener.h for C++ - missing extern "C". Patch from Ferenc Szalai.
+ o Make the event_base_loop() family of functions respect thread-safety better. This should clear up a few hard-to-debug race conditions.
+ o Fix a bug when using a specialized memory allocator on win32.
+ o Have the win32 select() backend label TCP-socket-connected events as EV_WRITE, not EV_READ. This should bring it in line with the other backends, and improve portability. Patch from Christopher Davis.
+ o Stop using enums as arguments or return values when what we mean is a bitfield of enum values. C++ doesn't believe that you can OR two enum values together and get another enum, and C++ takes its typing seriously. Patch from Christopher Davis.
+ o Add an API to replace all fatal calls to exit() with a user-provided panic function.
+ o Replace all assert() calls with a variant that is aware of the user-provided logging and panic functions.
+ o Add a return value to event_assign so that it can fail rather than asserting when the user gives it bad input. event_set still dies on bad input.
+ o The event_base_new() and event_base_new_with_config() functions now never call exit() on failure. For backward "compatibility", event_init() still does, but more consistently.
+ o Remove compat/sys/_time.h. It interfered with system headers on HPUX, and its functionality has been subsumed by event2/util.h and util-internal.h.
+ o Add a new bufferevent_socket_connect_hostname() to encapsulate the resolve-then-connect operation.
+ o Build kqueue.c correctly on GNU/kFreeBSD platforms. Patch pulled upstream from Debian.
+ o Alternative queue-based timeout algorithm for programs that use a large number of timeouts with the same value.
+ o New event_base_config option to disable the timeval cache entirely.
+ o Make EV_PERSIST timeouts more accurate: schedule the next event based on the scheduled time of the previous event, not based on the current time.
+ o Allow http.c to handle cases where getaddrinfo returns an IPv6 address. Patch from Ryan Phillips.
+ o Fix a problem with excessive memory allocation when using multiple event priorities.
+ o Default to using arc4random for DNS transaction IDs on systems that have it; from OpenBSD.
+ o Never check the environment when we're running setuid or setgid; from OpenBSD.
+ o Options passed to evdns_set_option() no longer need to end with a colon.
+ o Add an evutil_getaddrinfo() function to clone getaddrinfo on platforms that don't have it.
+ o Add an evdns_getaddrinfo() function to provide a nonblocking getaddrinfo using evdns, so programs can perform useful hostname lookup.
+ o Finally expose the IOCP-based bufferevent backend. It passes its unit tests, but probably still has some bugs remaining. Code by Nick Mathewson and Christopher Davis.
+ o Numerous other bugfixes.
+ o On FreeBSD and other OSes, connect can return ECONREFUSED immediately; instead of failing the function call, pretend with faileld in the callback.
+ o Fix a race condition in the pthreads test case; found by Nick Mathewson
+ o Remove most calls to event_err() in http and deal with memory errors instead
+
+
+
+Changes in 2.0.2-alpha (25 Jul 2009):
+ o Add a new flag to bufferevents to make all callbacks automatically deferred.
+ o Make evdns functionality locked, and automatically defer dns callbacks.
+ o Fix a possible free(NULL) when freeing an event_base with no signals.
+ o Add a flag to disable checking environment varibles when making an event_base
+ o Disallow setting less than 1 priority.
+ o Fix a bug when removing a timeout from the heap. [Patch from Marko Kreen]
+ o Use signal.h, not sys/signal.h. [Patch from mmadia]
+ o Try harder to build with certain older c99 compilers.
+ o Make sure that an event_config's flags field is always initialized to 0. [Bug report from Victor Goya]
+ o Avoid data corruption when reading data entirely into the second-to-last chain of an evbuffer. [Bug report from Victor Goya]
+ o Make sendfile work on FreeBSD
+ o Do not use vararg macros for accessing evrpc structures; this is not backwards compatible, but we did not promise any backwards compatibility for the rpc code.
+ o Actually define the event_config_set_flag() function.
+ o Try harder to compile with Visual C++.
+ o Move event_set() and its allies to event2/event_compat.h where they belong.
+ o Remove the event_gotsig code, which has long been deprecated and unused.
+ o Add an event_get_base() function to return the base assigned to an event.
+ o New function to automate connecting on a socket-based bufferevent.
+ o New functions to automate listening for incoming TCP connections.
+ o Do case-insensitive checks with a locale-independent comparison function.
+ o Rename the evbuffercb and everrorcb callbacks to bufferevent_data_cb and bufferevent_event_cb respectively. The old names are available in bufferevent_compat.h.
+ o Rename the EVBUFFER_* codes used by bufferevent event callbacks to BEV_EVENT_*, to avoid namespace collision with evbuffer flags. The old names are available in bufferevent_compat.h.
+ o Move the EVBUFFER_INPUT and EVBUFFER_OUTPUT macros to bufferevent_compat.h
+ o Add a bufferevent_getfd() function to mirror bufferevent_setfd()
+ o Make bufferevent_setfd() return an error code if the operation is not successful.
+ o Shave 22 bytes off struct event on 32-bit platforms by shrinking and re-ordering fields. The savings on 64-bit platforms is likely higher.
+ o Cap the maximum number of priorities at 256.
+ o Change the semantics of evbuffer_cb_set_flags() to be set-flag only; add a new evbuffer_cb_clear_flags() to remove set flags.
+ o Change the interface of evbuffer_add_reference so that the cleanup callback gets more information
+ o Revise the new evbuffer_reserve_space/evbuffer_commit_space() interfaces so that you can use them without causing extraneous copies or leaving gaps in the evbuffer.
+ o Add a new evbuffer_peek() interface to inspect data in an evbuffer without removing it.
+ o Fix a deadlock when suspending reads in a bufferevent due to a full buffer. (Spotted by Joachim Bauch.)
+ o Fix a memory error when freeing a thread-enabled event base with registered events. (Spotted by Joachim Bauch.)
+ o Try to contain degree of failure when running on a win32 version so heavily firewalled that we can't fake a socketpair.
+ o Activate fd events in a pseudorandom order with O(N) backends, so that we don't systematically favor low fds (select) or earlier-added fds (poll, win32).
+ o Replace some read()/write() instances with send()/recv() to work properly on win32.
+ o Set truncated flag correctly in evdns server replies.
+ o Raise RpcGenError in event_rpcgen.py; from jmanison and Zack Weinberg
+ o Fix preamble of rpcgen-generated files to rely on event2 includes; based on work by jmansion; patch from Zack Weinberg.
+ o Allow specifying the output filename for rpcgen; based on work by jmansion; patch from Zack Weinberg.
+ o Allow C identifiers as struct names; allow multiple comments in .rpc files; from Zack Weinberg
+ o Mitigate a race condition when using socket bufferevents in multiple threads.
+ o Use AC_SEARCH_LIBS, not AC_CHECK_LIB to avoid needless library use.
+ o Do not allow event_del(ev) to return while that event's callback is executing in another thread. This fixes a nasty race condition.
+ o event_get_supported_methods() now lists methods that have been disabled with the EVENT_NO* environment options.
+ o Rename encode_int[64] to evtag_encode_int[64] to avoid polluting the global namespace. The old method names are still available as macros in event2/tag_compat.h.
+
+
+
+Changes in 2.0.1-alpha (17 Apr 2009):
+ o free minheap on event_base_free(); from Christopher Layne
+ o debug cleanups in signal.c; from Christopher Layne
+ o provide event_base_new() that does not set the current_base global
+ o bufferevent_write now uses a const source argument; report from Charles Kerr
+ o improve documentation on event_base_loopexit; patch from Scott Lamb
+ o New function, event_{base_}loopbreak. Like event_loopexit, it makes an event loop stop executing and return. Unlike event_loopexit, it keeps subsequent pending events from getting executed. Patch from Scott Lamb
+ o Check return value of event_add in signal.c
+ o provide event_reinit() to reintialize an event_base after fork
+ o New function event_set_mem_functinons. It allows the user to give libevent replacement functions to use for memory management in place of malloc(), free(), etc. This should be generally useful for memory instrumentation, specialized allocators, and so on.
+ o The kqueue implementation now catches signals that are raised after event_add() is called but before the event_loop() call. This makes it match the other implementations.
+ o The kqueue implementation now restores original signal handlers correctly when its signal events are removed.
+ o Check return value of event_add in signal.c
+ o Add a more powerful evbuffer_readln as a replacement for evbuffer_readline. The new function handles more newline styles, and is more useful with buffers that may contain a nul characters.
+ o Do not mangle socket handles on 64-bit windows.
+ o The configure script now takes an --enable-gcc-warnigns option that turns on many optional gcc warnings. (Nick has been building with these for a while, but they might be useful to other developers.)
+ o move EV_PERSIST handling out of the event backends
+ o small improvements to evhttp documentation
+ o always generate Date and Content-Length headers for HTTP/1.1 replies
+ o set the correct event base for HTTP close events
+ o When building with GCC, use the "format" attribute to verify type correctness of calls to printf-like functions.
+ o Rewrite win32.c backend to be O(n lg n) rather than O(n^2).
+ o Removed obsoleted recalc code
+ o support for 32-bit tag numbers in rpc structures; this is wire compatible, but changes the API slightly.
+ o pull setters/getters out of RPC structures into a base class to which we just need to store a pointer; this reduces the memory footprint of these structures.
+ o prefix {encode,decode}_tag functions with evtag to avoid collisions
+ o fix a bug with event_rpcgen for integers
+ o Correctly handle DNS replies with no answers set (Fixes bug 1846282)
+ o add -Wstrict-aliasing to warnings and more cleanup
+ o removed linger from http server socket; reported by Ilya Martynov
+ o event_rpcgen now allows creating integer arrays
+ o support string arrays in event_rpcgen
+ o change evrpc hooking to allow pausing of RPCs; this will make it possible for the hook to do some meaning ful work; this is not backwards compatible.
+ o allow an http request callback to take ownership of a request structure
+ o allow association of meta data with RPC requests for hook processing
+ o associate more context for hooks to query such as the connection object
+ o remove pending timeouts on event_base_free()
+ o also check EAGAIN for Solaris' event ports; from W.C.A. Wijngaards
+ o devpoll and evport need reinit; tested by W.C.A Wijngaards
+ o event_base_get_method; from Springande Ulv
+ o Send CRLF after each chunk in HTTP output, for compliance with RFC2626. Patch from "propanbutan". Fixes bug 1894184.
+ o Add a int64_t parsing function, with unit tests, so we can apply Scott Lamb's fix to allow large HTTP values.
+ o Use a 64-bit field to hold HTTP content-lengths. Patch from Scott Lamb.
+ o Allow regression code to build even without Python installed
+ o remove NDEBUG ifdefs from evdns.c
+ o detect integer types properly on platforms without stdint.h
+ o udpate documentation of event_loop and event_base_loop; from Tani Hosokawa.
+ o simplify evbuffer by removing orig_buffer
+ o do not insert event into list when evsel->add fails
+ o add support for PUT/DELETE requests; from Josh Rotenberg
+ o introduce evhttp_accept_socket() to accept from an already created socket
+ o include Content-Length in reply for HTTP/1.0 requests with keep-alive
+ o increase listen queue for http sockets to 128; if that is not enough the evhttp_accpet_socket() api can be used with a prepared socket.
+ o Patch from Tani Hosokawa: make some functions in http.c threadsafe.
+ o test support for PUT/DELETE requests; from Josh Rotenberg
+ o rewrite of the evbuffer code to reduce memory copies
+ o Some older Solaris versions demand that _REENTRANT be defined to get strtok_r(); do so.
+ o Do not free the kqop file descriptor in other processes, also allow it to be 0; from Andrei Nigmatulin
+ o Provide OpenSSL style support for multiple threads accessing the same event_base
+ o make event_rpcgen.py generate code include event-config.h; reported by Sam Banks.
+ o switch thread support so that locks get allocated as they are needed.
+ o make event methods static so that they are not exported; from Andrei Nigmatulin
+ o make RPC replies use application/octet-stream as mime type
+ o do not delete uninitialized timeout event in evdns
+ o Correct the documentation on buffer printf functions.
+ o Don't warn on unimplemented epoll_create(): this isn't a problem, just a reason to fall back to poll or select.
+ o Correctly handle timeouts larger than 35 minutes on Linux with epoll.c. This is probably a kernel defect, but we'll have to support old kernels anyway even if it gets fixed.
+ o Make name_from_addr() threadsafe in http.c
+ o Add new thread-safe interfaces to evdns functions.
+ o Make all event_tagging interfaces threadsafe.
+ o Rename internal memory management functions.
+ o New functions (event_assign, event_new, event_free) for use by apps that want to be safely threadsafe, or want to remain ignorant of the contents of struct event.
+ o introduce bufferevent_read_buffer; allows reading without memory copy.
+ o expose bufferevent_setwatermark via header files and fix high watermark on read
+ o fix a bug in buffrevent read water marks and add a test for them
+ o fix a bug in which bufferevent_write_buffer would not schedule a write event
+ o provide bufferevent_input and bufferevent_output without requiring knowledge of the structure
+ o introduce bufferevent_setcb and bufferevent_setfd to allow better manipulation of bufferevents
+ o convert evhttp_connection to use bufferevents.
+ o use libevent's internal timercmp on all platforms, to avoid bugs on old platforms where timercmp(a,b,<=) is buggy.
+ o Remove the never-exported, never-used evhttp_hostportfile function.
+ o Support input/output filters for bufferevents; somewhat similar to libio's model. This will allow us to implement SSL, compression, etc, transparently to users of bufferevents such as the http layer.
+ o allow connections to be removed from an rpc pool
+ o add new evtimer_assign, signal_assign, evtimer_new, and signal_new functions to manipulate timer and signal events, analagous to the now-recommended event_assign and event_new
+ o switch internal uses of event_set over to use event_assign.
+ o introduce evbuffer_contiguous_space() api that tells a user how much data is available in the first buffer chain
+ o introduce evbuffer_reserve_space() and evbuffer_commit_space() to make processing in filters more efficient.
+ o reduce system calls for getting current time by caching it.
+ o separate signal events from io events; making the code less complex.
+ o support for periodic timeouts
+ o support for virtual HTTP hosts.
+ o turn event_initialized() into a function, and add function equivalents to EVENT_SIGNAL and EVENT_FD so that people don't need to include event_struct.h
+ o Build test directory correctly with CPPFLAGS set.
+ o Provide an API for retrieving the supported event mechanisms.
+ o event_base_new_with_config() and corresponding config APIs.
+ o migrate the evhttp header to event2/ but accessors are still missing.
+ o deprecate timeout_* event functions by moving them to event_compat.h
+ o Move windows gettimeofday replacement into a new evutil_gettimeofday().
+ o Make configure script work on IRIX.
+ o provide a method for canceling ongoing http requests.
+ o Make vsnprintf() returns consistent on win32.
+ o Fix connection keep-alive behavior for HTTP/1.0
+ o Fix use of freed memory in event_reinit; pointed out by Peter Postma
+ o constify struct timeval * where possible
+ o make event_get_supported_methods obey environment variables
+ o support for edge-triggered events on epoll and kqueue backends: patch from Valery Kholodkov
+ o support for selecting event backends by their features, and for querying the features of a backend.
+ o change failing behavior of event_base_new_with_config: if a config is provided and no backend is selected, return NULL instead of aborting.
+ o deliver partial data to request callbacks when chunked callback is set even if there is no chunking on the http level; allows cancelation of requests from within the chunked callback; from Scott Lamb.
+ o allow min_heap_erase to be called on removed members; from liusifan.
+ o Rename INPUT and OUTPUT to EVRPC_INPUT and EVRPC_OUTPUT. Retain INPUT/OUTPUT aliases on on-win32 platforms for backwards compatibility.
+ o Do not use SO_REUSEADDR when connecting
+ o Support 64-bit integers in RPC structs
+ o Correct handling of trailing headers in chunked replies; from Scott Lamb.
+ o Support multi-line HTTP headers; based on a patch from Moshe Litvin
+ o Reject negative Content-Length headers; anonymous bug report
+ o Detect CLOCK_MONOTONIC at runtime for evdns; anonymous bug report
+ o Various HTTP correctness fixes from Scott Lamb
+ o Fix a bug where deleting signals with the kqueue backend would cause subsequent adds to fail
+ o Support multiple events listening on the same signal; make signals regular events that go on the same event queue; problem report by Alexander Drozdov.
+ o Fix a problem with epoll() and reinit; problem report by Alexander Drozdov.
+ o Fix off-by-one errors in devpoll; from Ian Bell
+ o Make event_add not change any state if it fails; reported by Ian Bell.
+ o Fix a bug where headers arriving in multiple packets were not parsed; fix from Jiang Hong; test by me.
+ o Match the query in DNS replies to the query in the request; from Vsevolod Stakhov.
+ o Add new utility functions to correctly observe and log winsock errors.
+ o Do not remove Accept-Encoding header
+ o Clear the timer cache on entering the event loop; reported by Victor Chang
+ o Only bind the socket on connect when a local address has been provided; reported by Alejo Sanchez
+ o Allow setting of local port for evhttp connections to support millions of connections from a single system; from Richard Jones.
+ o Clear the timer cache when leaving the event loop; reported by Robin Haberkorn
+ o Fix a typo in setting the global event base; reported by lance.
+ o Set the 0x20 bit on outgoing alphabetic characters in DNS requests randomly, and insist on a match in replies. This helps resist DNS poisoning attacks.
+ o Make the http connection close detection work properly with bufferevents and fix a potential memory leak associated with it.
+ o Restructure the event backends so that they do not need to keep track of events themselves, as a side effect multiple events can use the same fd or signal.
+ o Add generic implementations for parsing and emiting IPv6 addresses on platforms that do not have inet_ntop and/or inet_pton.
+ o Allow DNS servers that have IPv6 addresses.
+ o Add an evbuffer_write_atmost() function to write a limited number of bytes to an fd.
+ o Refactor internal notify-main-thread logic to prefer eventfd to pipe, then pipe to socketpair, and only use socketpairs as a last resort.
+ o Try harder to pack all evbuffer reads into as few chains as possible, using readv/WSARecv as appropriate.
+ o New evthread_use_windows_threads() and evthread_use_pthreads() functions to set up the evthread callbacks with reasonable defaults.
+ o Change the semantics of timeouts in conjunction with EV_PERSIST; timeouts in that case will now repeat until deleted.
+ o sendfile, mmap and memory reference support for evbuffers.
+ o New evutil_make_listen_socket_reuseable() to abstract SO_REUSEADDR.
+ o New bind-to option to allow DNS clients to bind to an arbitrary port for outgoing requests.
+ o evbuffers can now be "frozen" to prevent operations at one or both ends.
+ o Bufferevents now notice external attempts to add data to an inbuf or remove it from an outbuf, and stop them.
+ o Fix parsing of queries where the encoded queries contained \r, \n or +
+ o Do not allow internal events to starve lower-priority events.
+
diff --git a/Doxyfile b/Doxyfile
index 5d3865e7..d9d66034 100644
--- a/Doxyfile
+++ b/Doxyfile
@@ -239,7 +239,7 @@ INCLUDE_FILE_PATTERNS =
# undefined via #undef or recursively expanded use the := operator
# instead of the = operator.
-PREDEFINED = TAILQ_ENTRY RB_ENTRY _EVENT_DEFINED_TQENTRY _EVENT_IN_DOXYGEN
+PREDEFINED = TAILQ_ENTRY RB_ENTRY EVENT_DEFINED_TQENTRY_ EVENT_IN_DOXYGEN_
# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then
# this tag can be used to specify a list of macro names that should be expanded.
diff --git a/LICENSE b/LICENSE
index 70d76b3d..402ca508 100644
--- a/LICENSE
+++ b/LICENSE
@@ -72,3 +72,28 @@ The arc4module is available under the following, sometimes called the
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+==============================
+
+The Windows timer code is based on code from libutp, which is
+distributed under this license, sometimes called the "MIT" license.
+
+
+Copyright (c) 2010 BitTorrent, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/Makefile.am b/Makefile.am
index 42879a31..e2ce2928 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -5,8 +5,8 @@
# See LICENSE for copying information.
# 'foreign' means that we're not enforcing GNU package rules strictly.
-# '1.7' means that we need automake 1.7 or later (and we do).
-AUTOMAKE_OPTIONS = foreign 1.7
+# '1.9' means that we need automake 1.9 or later (and we do).
+AUTOMAKE_OPTIONS = foreign 1.9 subdir-objects
ACLOCAL_AMFLAGS = -I m4
@@ -18,7 +18,7 @@ ACLOCAL_AMFLAGS = -I m4
# will increment for each series until we revise our interfaces enough
# that we can seriously expect ABI compatibility between series.
#
-RELEASE = -release 2.0
+RELEASE = -release 2.1
# This is the version info for the libevent binary API. It has three
# numbers:
@@ -38,7 +38,7 @@ RELEASE = -release 2.0
#
# Once an RC is out, DO NOT MAKE ANY ABI-BREAKING CHANGES IN THAT SERIES
# UNLESS YOU REALLY REALLY HAVE TO.
-VERSION_INFO = 6:9:1
+VERSION_INFO = 4:0:0
# History: RELEASE VERSION_INFO
# 2.0.1-alpha -- 2.0 1:0:0
@@ -65,7 +65,9 @@ VERSION_INFO = 6:9:1
#
# For Libevent 2.1:
# 2.1.1-alpha -- 2.1 1:0:0
-
+# 2.1.2-alpha -- 2.1 1:0:0 (should have been 2:0:1)
+# 2.1.3-alpha -- 2.1 3:0:0 (ABI changed slightly)
+# 2.1.4-alpha -- 2.1 4:0:0 (ABI changed slightly)
# ABI version history for this package effectively restarts every time
# we change RELEASE. Version 1.4.x had RELEASE of 1.4.
@@ -92,21 +94,24 @@ endif
pkgconfigdir=$(libdir)/pkgconfig
LIBEVENT_PKGCONFIG=libevent.pc
-# These sources are conditionally added by configure.in or conditionally
+# These sources are conditionally added by configure.ac or conditionally
# included from other files.
PLATFORM_DEPENDENT_SRC = \
- epoll_sub.c \
- arc4random.c
+ arc4random.c \
+ epoll_sub.c
EXTRA_DIST = \
+ ChangeLog-1.4 \
+ ChangeLog-2.0 \
+ Doxyfile \
LICENSE \
+ Makefile.nmake test/Makefile.nmake \
autogen.sh \
event_rpcgen.py \
libevent.pc.in \
make-event-config.sed \
- Doxyfile \
whatsnew-2.0.txt \
- Makefile.nmake test/Makefile.nmake \
+ whatsnew-2.1.txt \
$(PLATFORM_DEPENDENT_SRC)
LIBEVENT_LIBS_LA = libevent.la libevent_core.la libevent_extra.la
@@ -126,14 +131,23 @@ else
noinst_LTLIBRARIES = $(LIBEVENT_LIBS_LA)
endif
-SUBDIRS = . include sample test
+EXTRA_SOURCE=
+noinst_HEADERS=
+noinst_PROGRAMS=
+EXTRA_PROGRAMS=
+CLEANFILES=
+DISTCLEANFILES=
+BUILT_SOURCES =
+include include/include.am
+include sample/include.am
+include test/include.am
if BUILD_WIN32
SYS_LIBS = -lws2_32 -lshell32 -ladvapi32
SYS_SRC = win32select.c evthread_win32.c buffer_iocp.c event_iocp.c \
bufferevent_async.c
-SYS_INCLUDES = -IWIN32-Code
+SYS_INCLUDES = -IWIN32-Code -IWIN32-Code/nmake
else
@@ -143,6 +157,9 @@ SYS_INCLUDES =
endif
+if STRLCPY_IMPL
+SYS_SRC += strlcpy.c
+endif
if SELECT_BACKEND
SYS_SRC += select.c
endif
@@ -165,18 +182,35 @@ if SIGNAL_SUPPORT
SYS_SRC += signal.c
endif
-BUILT_SOURCES = include/event2/event-config.h
+BUILT_SOURCES += include/event2/event-config.h
include/event2/event-config.h: config.h make-event-config.sed
- test -d include/event2 || $(MKDIR_P) include/event2
- $(SED) -f $(srcdir)/make-event-config.sed < config.h > $@T
- mv -f $@T $@
-
-CORE_SRC = event.c evthread.c buffer.c \
- bufferevent.c bufferevent_sock.c bufferevent_filter.c \
- bufferevent_pair.c listener.c bufferevent_ratelim.c \
- evmap.c log.c evutil.c evutil_rand.c strlcpy.c $(SYS_SRC)
-EXTRA_SRC = event_tagging.c http.c evdns.c evrpc.c
+ $(AM_V_GEN)test -d include/event2 || $(MKDIR_P) include/event2
+ $(AM_V_at)$(SED) -f $(srcdir)/make-event-config.sed < config.h > $@T
+ $(AM_V_at)mv -f $@T $@
+
+CORE_SRC = \
+ buffer.c \
+ bufferevent.c \
+ bufferevent_filter.c \
+ bufferevent_pair.c \
+ bufferevent_ratelim.c \
+ bufferevent_sock.c \
+ event.c \
+ evmap.c \
+ evthread.c \
+ evutil.c \
+ evutil_rand.c \
+ evutil_time.c \
+ listener.c \
+ log.c \
+ $(SYS_SRC)
+
+EXTRAS_SRC = \
+ evdns.c \
+ event_tagging.c \
+ evrpc.c \
+ http.c
if BUILD_WITH_NO_UNDEFINED
NO_UNDEFINED = -no-undefined
@@ -188,7 +222,7 @@ endif
GENERIC_LDFLAGS = -version-info $(VERSION_INFO) $(RELEASE) $(NO_UNDEFINED)
-libevent_la_SOURCES = $(CORE_SRC) $(EXTRA_SRC)
+libevent_la_SOURCES = $(CORE_SRC) $(EXTRAS_SRC)
libevent_la_LIBADD = @LTLIBOBJS@ $(SYS_LIBS)
libevent_la_LDFLAGS = $(GENERIC_LDFLAGS)
@@ -202,7 +236,7 @@ libevent_pthreads_la_LIBADD = $(MAYBE_CORE)
libevent_pthreads_la_LDFLAGS = $(GENERIC_LDFLAGS)
endif
-libevent_extra_la_SOURCES = $(EXTRA_SRC)
+libevent_extra_la_SOURCES = $(EXTRAS_SRC)
libevent_extra_la_LIBADD = $(MAYBE_CORE) $(SYS_LIBS)
libevent_extra_la_LDFLAGS = $(GENERIC_LDFLAGS)
@@ -210,20 +244,45 @@ if OPENSSL
libevent_openssl_la_SOURCES = bufferevent_openssl.c
libevent_openssl_la_LIBADD = $(MAYBE_CORE) $(OPENSSL_LIBS)
libevent_openssl_la_LDFLAGS = $(GENERIC_LDFLAGS)
+libevent_openssl_la_CPPFLAGS = $(AM_CPPFLAGS) $(OPENSSL_INCS)
endif
-noinst_HEADERS = util-internal.h mm-internal.h ipv6-internal.h \
- evrpc-internal.h strlcpy-internal.h evbuffer-internal.h \
- bufferevent-internal.h http-internal.h event-internal.h \
- evthread-internal.h ht-internal.h defer-internal.h \
- minheap-internal.h log-internal.h evsignal-internal.h evmap-internal.h \
- changelist-internal.h iocp-internal.h \
- ratelim-internal.h \
- WIN32-Code/event2/event-config.h \
- WIN32-Code/tree.h \
- compat/sys/queue.h
-
-EVENT1_HDRS = event.h evhttp.h evdns.h evrpc.h evutil.h
+noinst_HEADERS += \
+ WIN32-Code/nmake/evconfig-private.h \
+ WIN32-Code/nmake/event2/event-config.h \
+ WIN32-Code/tree.h \
+ bufferevent-internal.h \
+ changelist-internal.h \
+ compat/sys/queue.h \
+ defer-internal.h \
+ epolltable-internal.h \
+ evbuffer-internal.h \
+ evconfig-private.h \
+ event-internal.h \
+ evmap-internal.h \
+ evrpc-internal.h \
+ evsignal-internal.h \
+ evthread-internal.h \
+ ht-internal.h \
+ http-internal.h \
+ iocp-internal.h \
+ ipv6-internal.h \
+ kqueue-internal.h \
+ log-internal.h \
+ minheap-internal.h \
+ mm-internal.h \
+ ratelim-internal.h \
+ ratelim-internal.h \
+ strlcpy-internal.h \
+ time-internal.h \
+ util-internal.h
+
+EVENT1_HDRS = \
+ include/evdns.h \
+ include/event.h \
+ include/evhttp.h \
+ include/evrpc.h \
+ include/evutil.h
if INSTALL_LIBEVENT
include_HEADERS = $(EVENT1_HDRS)
@@ -239,5 +298,5 @@ doxygen: FORCE
doxygen $(srcdir)/Doxyfile
FORCE:
-DISTCLEANFILES = *~ libevent.pc ./include/event2/event-config.h
+DISTCLEANFILES += *~ libevent.pc ./include/event2/event-config.h
diff --git a/Makefile.nmake b/Makefile.nmake
index 4cd04c13..f27cd619 100644
--- a/Makefile.nmake
+++ b/Makefile.nmake
@@ -1,8 +1,26 @@
-# WATCH OUT! This makefile is a work in progress. It is probably missing
-# tons of important things. DO NOT RELY ON IT TO BUILD A GOOD LIBEVENT.
+# WATCH OUT! This makefile is a work in progress. -*- makefile -*-
+#
+# I'm not very knowledgeable about MSVC and nmake beyond their most basic
+# aspects. If anything here looks wrong to you, please let me know.
+
+# If OPENSSL_DIR is not set, builds without OpenSSL support. If you want
+# OpenSSL support, you can set the OPENSSL_DIR variable to where you
+# installed OpenSSL. This can be done in the environment:
+# set OPENSSL_DIR=c:\openssl
+# Or on the nmake command line:
+# nmake OPENSSL_DIR=C:\openssl -f Makefile.nmake
+# Or by uncommenting the following line here in the makefile...
+
+# OPENSSL_DIR=c:\openssl
+
+!IFDEF OPENSSL_DIR
+SSL_CFLAGS=/I$(OPENSSL_DIR)\include /DEVENT__HAVE_OPENSSL
+!ELSE
+SSL_CFLAGS=
+!ENDIF
# Needed for correctness
-CFLAGS=/IWIN32-Code /Iinclude /Icompat /DWIN32 /DHAVE_CONFIG_H /I.
+CFLAGS=/IWIN32-Code /IWIN32-Code/nmake /Iinclude /Icompat /DHAVE_CONFIG_H /I. $(SSL_CFLAGS)
# For optimization and warnings
CFLAGS=$(CFLAGS) /Ox /W3 /wd4996 /nologo
@@ -14,13 +32,21 @@ LIBFLAGS=/nologo
CORE_OBJS=event.obj buffer.obj bufferevent.obj bufferevent_sock.obj \
bufferevent_pair.obj listener.obj evmap.obj log.obj evutil.obj \
strlcpy.obj signal.obj bufferevent_filter.obj evthread.obj \
- bufferevent_ratelim.obj evutil_rand.obj
+ bufferevent_ratelim.obj evutil_rand.obj evutil_time.obj
WIN_OBJS=win32select.obj evthread_win32.obj buffer_iocp.obj \
event_iocp.obj bufferevent_async.obj
EXTRA_OBJS=event_tagging.obj http.obj evdns.obj evrpc.obj
-ALL_OBJS=$(CORE_OBJS) $(WIN_OBJS) $(EXTRA_OBJS)
-STATIC_LIBS=libevent_core.lib libevent_extras.lib libevent.lib
+!IFDEF OPENSSL_DIR
+SSL_OBJS=bufferevent_openssl.obj
+SSL_LIBS=libevent_openssl.lib
+!ELSE
+SSL_OBJS=
+SSL_LIBS=
+!ENDIF
+
+ALL_OBJS=$(CORE_OBJS) $(WIN_OBJS) $(EXTRA_OBJS) $(SSL_OBJS)
+STATIC_LIBS=libevent_core.lib libevent_extras.lib libevent.lib $(SSL_LIBS)
all: static_libs tests
@@ -36,12 +62,21 @@ libevent_extras.lib: $(EXTRA_OBJS)
libevent.lib: $(CORE_OBJS) $(WIN_OBJS) $(EXTRA_OBJS)
lib $(LIBFLAGS) $(CORE_OBJS) $(EXTRA_OBJS) $(WIN_OBJS) /out:libevent.lib
+libevent_openssl.lib: $(SSL_OBJS)
+ lib $(LIBFLAGS) $(SSL_OBJS) /out:libevent_openssl.lib
+
clean:
del $(ALL_OBJS)
del $(STATIC_LIBS)
cd test
$(MAKE) /F Makefile.nmake clean
+ cd ..
tests:
cd test
+!IFDEF OPENSSL_DIR
+ $(MAKE) OPENSSL_DIR=$(OPENSSL_DIR) /F Makefile.nmake
+!ELSE
$(MAKE) /F Makefile.nmake
+!ENDIF
+ cd ..
diff --git a/README b/README
deleted file mode 100644
index 23b769f9..00000000
--- a/README
+++ /dev/null
@@ -1,198 +0,0 @@
-0. BUILDING AND INSTALLATION (Briefly)
-
-$ ./configure
-$ make
-$ make verify # (optional)
-$ sudo make install
-
-1. BUILDING AND INSTALLATION (In Depth)
-
-To build libevent, type
-
-$ ./configure && make
-
- (If you got libevent from the git repository, you will
- first need to run the included "autogen.sh" script in order to
- generate the configure script.)
-
-You can run the regression tests by running
-
-$ make verify
-
-Install as root via
-
-# make install
-
-Before, reporting any problems, please run the regression tests.
-
-To enable the low-level tracing build the library as:
-
- CFLAGS=-DUSE_DEBUG ./configure [...]
-
-Standard configure flags should work. In particular, see:
-
- --disable-shared Only build static libraries
- --prefix Install all files relative to this directory.
-
-
-The configure script also supports the following flags:
-
- --enable-gcc-warnings Enable extra compiler checking with GCC.
- --disable-malloc-replacement
- Don't let applications replace our memory
- management functions
- --disable-openssl Disable support for OpenSSL encryption.
- --disable-thread-support Don't support multithreaded environments.
-
-2. USEFUL LINKS:
-
-For the latest released version of Libevent, see the official website at
-http://libevent.org/ .
-
-There's a pretty good work-in-progress manual up at
- http://www.wangafu.net/~nickm/libevent-book/ .
-
-For the latest development versions of Libevent, access our Git repository
-via
- "git clone git://levent.git.sourceforge.net/gitroot/levent/libevent"
-
-You can browse the git repository online at
-http://levent.git.sourceforge.net/git/gitweb-index.cgi .
-
-To report bugs, request features, or submit patches to Libevent,
-use the Sourceforge trackers at
-https://sourceforge.net/tracker/?group_id=50884 .
-
-There's also a libevent-users mailing list for talking about Libevent
-use and development: http://archives.seul.org/libevent/users/
-
-3. ACKNOWLEDGMENTS
-
-The following people have helped with suggestions, ideas, code or
-fixing bugs:
-
- Arno Bakker
- Alejo
- Weston Andros Adamson
- William Ahern
- Ivan Andropov
- Sergey Avseyev
- Avi Bab
- Gilad Benjamini
- Stas Bekman
- Joachim Bauch
- Denis Bilenko
- Julien Blache
- Kevin Bowling
- Tomash Brechko
- Kelly Brock
- Ralph Castain
- Adrian Chadd
- Lawnstein Chan
- Shuo Chen
- Ka-Hing Cheung
- Andrew Cox
- George Danchev
- Andrew Danforth
- Antony Dovgal
- Ed Day
- Christopher Davis
- Mike Davis
- Mihai Draghicioiu
- Mark Ellzey
- Shie Erlich
- Leonid Evdokimov
- Juan Pablo Fernandez
- Christophe Fillot
- Mike Frysinger
- Remi Gacogne
- Alexander von Gernler
- Artur Grabowski
- Sebastian Hahn
- Dave Hart
- Greg Hazel
- Michael Herf
- Savg He
- Mark Heily
- Greg Hewgill
- Andrew Hochhaus
- Aaron Hopkins
- Tani Hosokawa
- Jamie Iles
- Claudio Jeker
- Evan Jones
- George Kadianakis
- Phua Keat
- Kevin Ko
- Brian Koehmstedt
- Marko Kreen
- Valery Kyholodov
- Ross Lagerwall
- Scott Lamb
- Christopher Layne
- Adam Langley
- Philip Lewis
- Zhou Li
- David Libenzi
- Yan Lin
- Moshe Litvin
- Simon Liu
- Mitchell Livingston
- Hagne Mahre
- Lubomir Marinov
- Abilio Marques
- Nick Mathewson
- James Mansion
- Nicholas Marriott
- Andrey Matveev
- Caitlin Mercer
- Dagobert Michelsen
- Mansour Moufid
- Felix Nawothnig
- Trond Norbye
- Linus Nordberg
- Richard Nyberg
- Jon Oberheide
- Phil Oleson
- Dave Pacheco
- Tassilo von Parseval
- Catalin Patulea
- Patrick Pelletier
- Simon Perreault
- Pierre Phaneuf
- Ryan Phillips
- Dimitre Piskyulev
- Pavel Plesov
- Jon Poland
- Nate R
- Robert Ransom
- Bert JW Regeer
- Peter Rosin
- Maseeb Abdul Qadir
- Wang Qin
- Alex S
- Hanna Schroeter
- Ralf Schmitt
- Mike Smellie
- Kevin Springborn
- Harlan Stenn
- Steve Snyder
- Dug Song
- Dongsheng Song
- Hannes Sowa
- Ferenc Szalai
- Brodie Thiesfield
- Jason Toffaletti
- Gisle Vanem
- Bas Verhoeven
- Constantine Verutin
- Colin Watt
- Zack Weinberg
- Jardel Weyrich
- Alex
- Taral
- propanbutan
- mmadia
-
-
-If we have forgotten your name, please contact us.
diff --git a/README.md b/README.md
new file mode 100644
index 00000000..a7c82b01
--- /dev/null
+++ b/README.md
@@ -0,0 +1,318 @@
+# 0. BUILDING AND INSTALLATION (Briefly)
+
+## Autoconf
+
+ $ ./configure
+ $ make
+ $ make verify # (optional)
+ $ sudo make install
+
+## CMake (Windows)
+
+Install CMake: <http://www.cmake.org>
+
+
+ $ md build && cd build
+ $ cmake -G "Visual Studio 10" .. # Or whatever generator you want to use cmake --help for a list.
+ $ start libevent.sln
+
+## CMake (Unix)
+
+ $ mkdir build && cd build
+ $ cmake .. # Default to Unix Makefiles.
+ $ make
+ $ make verify # (optional)
+
+
+# 1. BUILDING AND INSTALLATION (In Depth)
+
+## Autoconf
+
+To build libevent, type
+
+ $ ./configure && make
+
+
+ (If you got libevent from the git repository, you will
+ first need to run the included "autogen.sh" script in order to
+ generate the configure script.)
+
+You can run the regression tests by running
+
+ $ make verify
+
+Install as root via
+
+ $ make install
+
+Before reporting any problems, please run the regression tests.
+
+To enable the low-level tracing build the library as:
+
+ $ CFLAGS=-DUSE_DEBUG ./configure [...]
+
+Standard configure flags should work. In particular, see:
+
+ --disable-shared Only build static libraries
+ --prefix Install all files relative to this directory.
+
+
+The configure script also supports the following flags:
+
+ --enable-gcc-warnings Enable extra compiler checking with GCC.
+ --disable-malloc-replacement
+ Don't let applications replace our memory
+ management functions
+ --disable-openssl Disable support for OpenSSL encryption.
+ --disable-thread-support Don't support multithreaded environments.
+
+## CMake (Windows)
+
+(Note that autoconf is currently the most mature and supported build
+enviroment for libevent; the cmake instructions here are new and
+experimental, though they _should_ be solid. We hope that cmake will
+still be supported in future versions of Libevent, and will try to
+make sure that happens.)
+
+First of all install <http://www.cmake.org>.
+
+To build libevent using Microsoft Visual studio open the "Visual Studio Command prompt" and type:
+
+```
+$ cd <libevent source dir>
+$ mkdir build && cd build
+$ cmake -G "Visual Studio 10" .. # Or whatever generator you want to use cmake --help for a list.
+$ start libevent.sln
+```
+
+In the above, the ".." refers to the dir containing the Libevent source code.
+You can build multiple versions (with different compile time settings) from the same source tree
+by creating other build directories.
+
+It is highly recommended to build "out of source" when using
+CMake instead of "in source" like the normal behaviour of autoconf for this reason.
+
+The "NMake Makefiles" CMake generator can be used to build entirely via the command line.
+
+To get a list of settings available for the project you can type:
+
+```
+$ cmake -LH ..
+```
+
+### GUI
+
+CMake also provides a GUI that lets you specify the source directory and output (binary) directory
+that the build should be placed in.
+
+### OpenSSL support
+
+To build Libevent with OpenSSL support you will need to have OpenSSL binaries available when building,
+these can be found here: <http://www.openssl.org/related/binaries.html>
+
+# 2. USEFUL LINKS:
+
+For the latest released version of Libevent, see the official website at
+<http://libevent.org/> .
+
+There's a pretty good work-in-progress manual up at
+ <http://www.wangafu.net/~nickm/libevent-book/> .
+
+For the latest development versions of Libevent, access our Git repository
+via
+
+```
+$ git clone git://levent.git.sourceforge.net/gitroot/levent/libevent
+```
+
+You can browse the git repository online at:
+
+<http://levent.git.sourceforge.net/git/gitweb-index.cgi>
+
+<https://github.com/libevent/Libevent>
+
+To report bugs, request features, or submit patches to Libevent,
+use the Sourceforge trackers at
+
+<https://sourceforge.net/tracker/?group_id=50884>
+
+There's also a libevent-users mailing list for talking about Libevent
+use and development:
+
+<http://archives.seul.org/libevent/users/>
+
+# 3. ACKNOWLEDGMENTS
+
+The following people have helped with suggestions, ideas, code or
+fixing bugs:
+
+ * Samy Al Bahra
+ * Antony Antony
+ * Jacob Appelbaum
+ * Arno Bakker
+ * Weston Andros Adamson
+ * William Ahern
+ * Ivan Andropov
+ * Sergey Avseyev
+ * Avi Bab
+ * Joachim Bauch
+ * Andrey Belobrov
+ * Gilad Benjamini
+ * Stas Bekman
+ * Denis Bilenko
+ * Julien Blache
+ * Kevin Bowling
+ * Tomash Brechko
+ * Kelly Brock
+ * Ralph Castain
+ * Adrian Chadd
+ * Lawnstein Chan
+ * Shuo Chen
+ * Ka-Hing Cheung
+ * Andrew Cox
+ * Paul Croome
+ * George Danchev
+ * Andrew Danforth
+ * Ed Day
+ * Christopher Davis
+ * Mike Davis
+ * Frank Denis
+ * Antony Dovgal
+ * Mihai Draghicioiu
+ * Alexander Drozdov
+ * Mark Ellzey
+ * Shie Erlich
+ * Leonid Evdokimov
+ * Juan Pablo Fernandez
+ * Christophe Fillot
+ * Mike Frysinger
+ * Remi Gacogne
+ * Artem Germanov
+ * Alexander von Gernler
+ * Diego Giagio
+ * Artur Grabowski
+ * Diwaker Gupta
+ * Kuldeep Gupta
+ * Sebastian Hahn
+ * Dave Hart
+ * Greg Hazel
+ * Nicholas Heath
+ * Michael Herf
+ * Sebastian Hahn
+ * Savg He
+ * Mark Heily
+ * Maxime Henrion
+ * Michael Herf
+ * Greg Hewgill
+ * Andrew Hochhaus
+ * Aaron Hopkins
+ * Tani Hosokawa
+ * Jamie Iles
+ * Xiuqiang Jiang
+ * Claudio Jeker
+ * Evan Jones
+ * Marcin Juszkiewicz
+ * George Kadianakis
+ * Makoto Kato
+ * Phua Keat
+ * Azat Khuzhin
+ * Alexander Klauer
+ * Kevin Ko
+ * Brian Koehmstedt
+ * Marko Kreen
+ * Ondřej Kuzník
+ * Valery Kyholodov
+ * Ross Lagerwall
+ * Scott Lamb
+ * Christopher Layne
+ * Adam Langley
+ * Graham Leggett
+ * Volker Lendecke
+ * Philip Lewis
+ * Zhou Li
+ * David Libenzi
+ * Yan Lin
+ * Moshe Litvin
+ * Simon Liu
+ * Mitchell Livingston
+ * Hagne Mahre
+ * Lubomir Marinov
+ * Abilio Marques
+ * Nicolas Martyanoff
+ * Abel Mathew
+ * Nick Mathewson
+ * James Mansion
+ * Nicholas Marriott
+ * Andrey Matveev
+ * Caitlin Mercer
+ * Dagobert Michelsen
+ * Andrea Montefusco
+ * Mansour Moufid
+ * Mina Naguib
+ * Felix Nawothnig
+ * Trond Norbye
+ * Linus Nordberg
+ * Richard Nyberg
+ * Jon Oberheide
+ * John Ohl
+ * Phil Oleson
+ * Alexey Ozeritsky
+ * Dave Pacheco
+ * Derrick Pallas
+ * Tassilo von Parseval
+ * Catalin Patulea
+ * Patrick Pelletier
+ * Simon Perreault
+ * Dan Petro
+ * Pierre Phaneuf
+ * Amarin Phaosawasdi
+ * Ryan Phillips
+ * Dimitre Piskyulev
+ * Pavel Plesov
+ * Jon Poland
+ * Roman Puls
+ * Nate R
+ * Robert Ransom
+ * Balint Reczey
+ * Bert JW Regeer
+ * Nate Rosenblum
+ * Peter Rosin
+ * Maseeb Abdul Qadir
+ * Wang Qin
+ * Alex S
+ * Gyepi Sam
+ * Hanna Schroeter
+ * Ralf Schmitt
+ * Mike Smellie
+ * Steve Snyder
+ * Nir Soffer
+ * Dug Song
+ * Dongsheng Song
+ * Hannes Sowa
+ * Joakim Soderberg
+ * Joseph Spadavecchia
+ * Kevin Springborn
+ * Harlan Stenn
+ * Andrew Sweeney
+ * Ferenc Szalai
+ * Brodie Thiesfield
+ * Jason Toffaletti
+ * Brian Utterback
+ * Gisle Vanem
+ * Bas Verhoeven
+ * Constantine Verutin
+ * Colin Watt
+ * Zack Weinberg
+ * Jardel Weyrich
+ * Jay R. Wren
+ * Zack Weinberg
+ * Mobai Zhang
+ * Alejo
+ * Alex
+ * Taral
+ * propanbutan
+ * masksqwe
+ * mmadia
+ * yangacer
+
+If we have forgotten your name, please contact us.
diff --git a/WIN32-Code/getopt.c b/WIN32-Code/getopt.c
new file mode 100644
index 00000000..0fcba5d9
--- /dev/null
+++ b/WIN32-Code/getopt.c
@@ -0,0 +1,149 @@
+/* $NetBSD: getopt.c,v 1.16 1999/12/02 13:15:56 kleink Exp $ */
+
+/*
+ * Copyright (c) 1987, 1993, 1994, 1995
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the names of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
+ * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#if 0
+static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
+#endif
+
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+#define __P(x) x
+#define _DIAGASSERT(x) assert(x)
+
+#ifdef __weak_alias
+__weak_alias(getopt,_getopt);
+#endif
+
+
+int opterr = 1, /* if error message should be printed */
+ optind = 1, /* index into parent argv vector */
+ optopt, /* character checked for validity */
+ optreset; /* reset getopt */
+char *optarg; /* argument associated with option */
+
+static char * _progname __P((char *));
+int getopt_internal __P((int, char * const *, const char *));
+
+static char *
+_progname(nargv0)
+ char * nargv0;
+{
+ char * tmp;
+
+ _DIAGASSERT(nargv0 != NULL);
+
+ tmp = strrchr(nargv0, '/');
+ if (tmp)
+ tmp++;
+ else
+ tmp = nargv0;
+ return(tmp);
+}
+
+#define BADCH (int)'?'
+#define BADARG (int)':'
+#define EMSG ""
+
+/*
+ * getopt --
+ * Parse argc/argv argument vector.
+ */
+int
+getopt(nargc, nargv, ostr)
+ int nargc;
+ char * const nargv[];
+ const char *ostr;
+{
+ static char *__progname = 0;
+ static char *place = EMSG; /* option letter processing */
+ char *oli; /* option letter list index */
+ __progname = __progname?__progname:_progname(*nargv);
+
+ _DIAGASSERT(nargv != NULL);
+ _DIAGASSERT(ostr != NULL);
+
+ if (optreset || !*place) { /* update scanning pointer */
+ optreset = 0;
+ if (optind >= nargc || *(place = nargv[optind]) != '-') {
+ place = EMSG;
+ return (-1);
+ }
+ if (place[1] && *++place == '-' /* found "--" */
+ && place[1] == '\0') {
+ ++optind;
+ place = EMSG;
+ return (-1);
+ }
+ } /* option letter okay? */
+ if ((optopt = (int)*place++) == (int)':' ||
+ !(oli = strchr(ostr, optopt))) {
+ /*
+ * if the user didn't specify '-' as an option,
+ * assume it means -1.
+ */
+ if (optopt == (int)'-')
+ return (-1);
+ if (!*place)
+ ++optind;
+ if (opterr && *ostr != ':')
+ (void)fprintf(stderr,
+ "%s: illegal option -- %c\n", __progname, optopt);
+ return (BADCH);
+ }
+ if (*++oli != ':') { /* don't need argument */
+ optarg = NULL;
+ if (!*place)
+ ++optind;
+ }
+ else { /* need an argument */
+ if (*place) /* no white space */
+ optarg = place;
+ else if (nargc <= ++optind) { /* no arg */
+ place = EMSG;
+ if (*ostr == ':')
+ return (BADARG);
+ if (opterr)
+ (void)fprintf(stderr,
+ "%s: option requires an argument -- %c\n",
+ __progname, optopt);
+ return (BADCH);
+ }
+ else /* white space */
+ optarg = nargv[optind];
+ place = EMSG;
+ ++optind;
+ }
+ return (optopt); /* dump back option letter */
+}
+
diff --git a/WIN32-Code/getopt.h b/WIN32-Code/getopt.h
new file mode 100644
index 00000000..796f4550
--- /dev/null
+++ b/WIN32-Code/getopt.h
@@ -0,0 +1,33 @@
+#ifndef __GETOPT_H__
+#define __GETOPT_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern int opterr; /* if error message should be printed */
+extern int optind; /* index into parent argv vector */
+extern int optopt; /* character checked for validity */
+extern int optreset; /* reset getopt */
+extern char *optarg; /* argument associated with option */
+
+struct option
+{
+ const char *name;
+ int has_arg;
+ int *flag;
+ int val;
+};
+
+#define no_argument 0
+#define required_argument 1
+#define optional_argument 2
+
+int getopt(int, char**, const char*);
+int getopt_long(int, char**, const char*, const struct option*, int*);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __GETOPT_H__ */
diff --git a/WIN32-Code/getopt_long.c b/WIN32-Code/getopt_long.c
new file mode 100644
index 00000000..03f0c01a
--- /dev/null
+++ b/WIN32-Code/getopt_long.c
@@ -0,0 +1,233 @@
+
+/*
+ * Copyright (c) 1987, 1993, 1994, 1996
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the names of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
+ * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "getopt.h"
+
+extern int opterr; /* if error message should be printed */
+extern int optind; /* index into parent argv vector */
+extern int optopt; /* character checked for validity */
+extern int optreset; /* reset getopt */
+extern char *optarg; /* argument associated with option */
+
+#define __P(x) x
+#define _DIAGASSERT(x) assert(x)
+
+static char * __progname __P((char *));
+int getopt_internal __P((int, char * const *, const char *));
+
+static char *
+__progname(nargv0)
+ char * nargv0;
+{
+ char * tmp;
+
+ _DIAGASSERT(nargv0 != NULL);
+
+ tmp = strrchr(nargv0, '/');
+ if (tmp)
+ tmp++;
+ else
+ tmp = nargv0;
+ return(tmp);
+}
+
+#define BADCH (int)'?'
+#define BADARG (int)':'
+#define EMSG ""
+
+/*
+ * getopt --
+ * Parse argc/argv argument vector.
+ */
+int
+getopt_internal(nargc, nargv, ostr)
+ int nargc;
+ char * const *nargv;
+ const char *ostr;
+{
+ static char *place = EMSG; /* option letter processing */
+ char *oli; /* option letter list index */
+
+ _DIAGASSERT(nargv != NULL);
+ _DIAGASSERT(ostr != NULL);
+
+ if (optreset || !*place) { /* update scanning pointer */
+ optreset = 0;
+ if (optind >= nargc || *(place = nargv[optind]) != '-') {
+ place = EMSG;
+ return (-1);
+ }
+ if (place[1] && *++place == '-') { /* found "--" */
+ /* ++optind; */
+ place = EMSG;
+ return (-2);
+ }
+ } /* option letter okay? */
+ if ((optopt = (int)*place++) == (int)':' ||
+ !(oli = strchr(ostr, optopt))) {
+ /*
+ * if the user didn't specify '-' as an option,
+ * assume it means -1.
+ */
+ if (optopt == (int)'-')
+ return (-1);
+ if (!*place)
+ ++optind;
+ if (opterr && *ostr != ':')
+ (void)fprintf(stderr,
+ "%s: illegal option -- %c\n", __progname(nargv[0]), optopt);
+ return (BADCH);
+ }
+ if (*++oli != ':') { /* don't need argument */
+ optarg = NULL;
+ if (!*place)
+ ++optind;
+ } else { /* need an argument */
+ if (*place) /* no white space */
+ optarg = place;
+ else if (nargc <= ++optind) { /* no arg */
+ place = EMSG;
+ if ((opterr) && (*ostr != ':'))
+ (void)fprintf(stderr,
+ "%s: option requires an argument -- %c\n",
+ __progname(nargv[0]), optopt);
+ return (BADARG);
+ } else /* white space */
+ optarg = nargv[optind];
+ place = EMSG;
+ ++optind;
+ }
+ return (optopt); /* dump back option letter */
+}
+
+#if 0
+/*
+ * getopt --
+ * Parse argc/argv argument vector.
+ */
+int
+getopt2(nargc, nargv, ostr)
+ int nargc;
+ char * const *nargv;
+ const char *ostr;
+{
+ int retval;
+
+ if ((retval = getopt_internal(nargc, nargv, ostr)) == -2) {
+ retval = -1;
+ ++optind;
+ }
+ return(retval);
+}
+#endif
+
+/*
+ * getopt_long --
+ * Parse argc/argv argument vector.
+ */
+int
+getopt_long(nargc, nargv, options, long_options, index)
+ int nargc;
+ char ** nargv;
+ const char * options;
+ const struct option * long_options;
+ int * index;
+{
+ int retval;
+
+ _DIAGASSERT(nargv != NULL);
+ _DIAGASSERT(options != NULL);
+ _DIAGASSERT(long_options != NULL);
+ /* index may be NULL */
+
+ if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
+ char *current_argv = nargv[optind++] + 2, *has_equal;
+ int i, current_argv_len, match = -1;
+
+ if (*current_argv == '\0') {
+ return(-1);
+ }
+ if ((has_equal = strchr(current_argv, '=')) != NULL) {
+ current_argv_len = has_equal - current_argv;
+ has_equal++;
+ } else
+ current_argv_len = strlen(current_argv);
+
+ for (i = 0; long_options[i].name; i++) {
+ if (strncmp(current_argv, long_options[i].name, current_argv_len))
+ continue;
+
+ if (strlen(long_options[i].name) == (unsigned)current_argv_len) {
+ match = i;
+ break;
+ }
+ if (match == -1)
+ match = i;
+ }
+ if (match != -1) {
+ if (long_options[match].has_arg == required_argument ||
+ long_options[match].has_arg == optional_argument) {
+ if (has_equal)
+ optarg = has_equal;
+ else
+ optarg = nargv[optind++];
+ }
+ if ((long_options[match].has_arg == required_argument)
+ && (optarg == NULL)) {
+ /*
+ * Missing argument, leading :
+ * indicates no error should be generated
+ */
+ if ((opterr) && (*options != ':'))
+ (void)fprintf(stderr,
+ "%s: option requires an argument -- %s\n",
+ __progname(nargv[0]), current_argv);
+ return (BADARG);
+ }
+ } else { /* No matching argument */
+ if ((opterr) && (*options != ':'))
+ (void)fprintf(stderr,
+ "%s: illegal option -- %s\n", __progname(nargv[0]), current_argv);
+ return (BADCH);
+ }
+ if (long_options[match].flag) {
+ *long_options[match].flag = long_options[match].val;
+ retval = 0;
+ } else
+ retval = long_options[match].val;
+ if (index)
+ *index = match;
+ }
+ return(retval);
+}
diff --git a/WIN32-Code/nmake/evconfig-private.h b/WIN32-Code/nmake/evconfig-private.h
new file mode 100644
index 00000000..88e20627
--- /dev/null
+++ b/WIN32-Code/nmake/evconfig-private.h
@@ -0,0 +1,6 @@
+#if !defined(EVENT_EVCONFIG__PRIVATE_H_) && !defined(__MINGW32__)
+#define EVENT_EVCONFIG__PRIVATE_H_
+
+/* Nothing to see here. Move along. */
+
+#endif
diff --git a/WIN32-Code/event2/event-config.h b/WIN32-Code/nmake/event2/event-config.h
index da8c23ac..5ba2d78e 100644
--- a/WIN32-Code/event2/event-config.h
+++ b/WIN32-Code/nmake/event2/event-config.h
@@ -7,357 +7,354 @@
*
* Do not rely on macros in this file existing in later versions.
*/
-#ifndef _EVENT_CONFIG_H_
-#define _EVENT_CONFIG_H_
+#ifndef EVENT_CONFIG_H__
+#define EVENT_CONFIG_H__
/* config.h. Generated by configure. */
/* config.h.in. Generated from configure.in by autoheader. */
/* Define if libevent should not allow replacing the mm functions */
-/* #undef _EVENT_DISABLE_MM_REPLACEMENT */
+/* #undef EVENT__DISABLE_MM_REPLACEMENT */
/* Define if libevent should not be compiled with thread support */
-/* #undef _EVENT_DISABLE_THREAD_SUPPORT */
+/* #undef EVENT__DISABLE_THREAD_SUPPORT */
/* Define if clock_gettime is available in libc */
/* #undef _EVENT_DNS_USE_CPU_CLOCK_FOR_ID */
/* Define is no secure id variant is available */
/* #define _EVENT_DNS_USE_GETTIMEOFDAY_FOR_ID 1 */
-#define _EVENT_DNS_USE_FTIME_FOR_ID 1
+#define EVENT_DNS_USE_FTIME_FOR_ID_ 1
/* Define to 1 if you have the <arpa/inet.h> header file. */
-/* #undef _EVENT_HAVE_ARPA_INET_H */
+/* #undef EVENT__HAVE_ARPA_INET_H */
/* Define to 1 if you have the `clock_gettime' function. */
-/* #undef _EVENT_HAVE_CLOCK_GETTIME */
+/* #undef EVENT__HAVE_CLOCK_GETTIME */
/* Define if /dev/poll is available */
-/* #undef _EVENT_HAVE_DEVPOLL */
+/* #undef EVENT__HAVE_DEVPOLL */
/* Define to 1 if you have the <dlfcn.h> header file. */
-/* #undef _EVENT_HAVE_DLFCN_H */
+/* #undef EVENT__HAVE_DLFCN_H */
/* Define if your system supports the epoll system calls */
-/* #undef _EVENT_HAVE_EPOLL */
+/* #undef EVENT__HAVE_EPOLL */
/* Define to 1 if you have the `epoll_ctl' function. */
-/* #undef _EVENT_HAVE_EPOLL_CTL */
+/* #undef EVENT__HAVE_EPOLL_CTL */
/* Define to 1 if you have the `eventfd' function. */
-/* #undef _EVENT_HAVE_EVENTFD */
+/* #undef EVENT__HAVE_EVENTFD */
/* Define if your system supports event ports */
-/* #undef _EVENT_HAVE_EVENT_PORTS */
+/* #undef EVENT__HAVE_EVENT_PORTS */
/* Define to 1 if you have the `fcntl' function. */
-/* #undef _EVENT_HAVE_FCNTL */
+/* #undef EVENT__HAVE_FCNTL */
/* Define to 1 if you have the <fcntl.h> header file. */
-#define _EVENT_HAVE_FCNTL_H 1
+#define EVENT__HAVE_FCNTL_H 1
/* Define to 1 if you have the `getaddrinfo' function. */
-#define _EVENT_HAVE_GETADDRINFO 1
+#define EVENT__HAVE_GETADDRINFO 1
/* Define to 1 if you have the `getnameinfo' function. */
-#define _EVENT_HAVE_GETNAMEINFO 1
+#define EVENT__HAVE_GETNAMEINFO 1
/* Define to 1 if you have the `getprotobynumber' function. */
-#define _EVENT_HAVE_GETPROTOBYNUMBER 1
+#define EVENT__HAVE_GETPROTOBYNUMBER 1
/* Define to 1 if you have the `getservbyname' function. */
-#define _EVENT_HAVE_GETSERVBYNAME 1
+#define EVENT__HAVE_GETSERVBYNAME 1
/* Define to 1 if you have the `gettimeofday' function. */
-/* #define _EVENT_HAVE_GETTIMEOFDAY 1 */
-
-/* Define to 1 if you have the `inet_aton' function. */
-/* #undef _EVENT_HAVE_INET_ATON */
+/* #define EVENT__HAVE_GETTIMEOFDAY 1 */
/* Define to 1 if you have the `inet_ntop' function. */
-/* #undef _EVENT_HAVE_INET_NTOP */
+/* #undef EVENT__HAVE_INET_NTOP */
/* Define to 1 if you have the `inet_pton' function. */
-/* #undef _EVENT_HAVE_INET_PTON */
+/* #undef EVENT__HAVE_INET_PTON */
/* Define to 1 if you have the <inttypes.h> header file. */
-/* #define _EVENT_HAVE_INTTYPES_H 1 */
+/* #define EVENT__HAVE_INTTYPES_H 1 */
/* Define to 1 if you have the `kqueue' function. */
-/* #undef _EVENT_HAVE_KQUEUE */
+/* #undef EVENT__HAVE_KQUEUE */
/* Define if the system has zlib */
-/* #undef _EVENT_HAVE_LIBZ */
+/* #undef EVENT__HAVE_LIBZ */
/* Define to 1 if you have the <memory.h> header file. */
-#define _EVENT_HAVE_MEMORY_H 1
+#define EVENT__HAVE_MEMORY_H 1
/* Define to 1 if you have the `mmap' function. */
-/* #undef _EVENT_HAVE_MMAP */
+/* #undef EVENT__HAVE_MMAP */
/* Define to 1 if you have the <netinet/in6.h> header file. */
-/* #undef _EVENT_HAVE_NETINET_IN6_H */
+/* #undef EVENT__HAVE_NETINET_IN6_H */
/* Define to 1 if you have the <netinet/in.h> header file. */
-/* #undef _EVENT_HAVE_NETINET_IN_H */
+/* #undef EVENT__HAVE_NETINET_IN_H */
/* Define to 1 if you have the `pipe' function. */
-/* #undef _EVENT_HAVE_PIPE */
+/* #undef EVENT__HAVE_PIPE */
/* Define to 1 if you have the `poll' function. */
-/* #undef _EVENT_HAVE_POLL */
+/* #undef EVENT__HAVE_POLL */
/* Define to 1 if you have the <poll.h> header file. */
-/* #undef _EVENT_HAVE_POLL_H */
+/* #undef EVENT__HAVE_POLL_H */
/* Define to 1 if you have the `port_create' function. */
-/* #undef _EVENT_HAVE_PORT_CREATE */
+/* #undef EVENT__HAVE_PORT_CREATE */
/* Define to 1 if you have the <port.h> header file. */
-/* #undef _EVENT_HAVE_PORT_H */
+/* #undef EVENT__HAVE_PORT_H */
/* Define if you have POSIX threads libraries and header files. */
-/* #undef _EVENT_HAVE_PTHREAD */
+/* #undef EVENT__HAVE_PTHREAD */
/* Define if we have pthreads on this system */
-/* #undef _EVENT_HAVE_PTHREADS */
+/* #undef EVENT__HAVE_PTHREADS */
/* Define to 1 if the system has the type `sa_family_t'. */
-/* #undef _EVENT_HAVE_SA_FAMILY_T */
+/* #undef EVENT__HAVE_SA_FAMILY_T */
/* Define to 1 if you have the `select' function. */
-/* #undef _EVENT_HAVE_SELECT */
+/* #undef EVENT__HAVE_SELECT */
/* Define to 1 if you have the `sendfile' function. */
-/* #undef _EVENT_HAVE_SENDFILE */
+/* #undef EVENT__HAVE_SENDFILE */
/* Define if F_SETFD is defined in <fcntl.h> */
-/* #undef _EVENT_HAVE_SETFD */
+/* #undef EVENT__HAVE_SETFD */
/* Define to 1 if you have the `sigaction' function. */
-/* #undef _EVENT_HAVE_SIGACTION */
+/* #undef EVENT__HAVE_SIGACTION */
/* Define to 1 if you have the `signal' function. */
-#define _EVENT_HAVE_SIGNAL 1
+#define EVENT__HAVE_SIGNAL 1
/* Define to 1 if you have the `splice' function. */
-/* #undef _EVENT_HAVE_SPLICE */
+/* #undef EVENT__HAVE_SPLICE */
/* Define to 1 if you have the <stdarg.h> header file. */
-#define _EVENT_HAVE_STDARG_H 1
+#define EVENT__HAVE_STDARG_H 1
/* Define to 1 if you have the <stddef.h> header file. */
-#define _EVENT_HAVE_STDDEF_H 1
+#define EVENT__HAVE_STDDEF_H 1
/* Define to 1 if you have the <stdint.h> header file. */
-/* #define _EVENT_HAVE_STDINT_H 1 */
+/* #define EVENT__HAVE_STDINT_H 1 */
/* Define to 1 if you have the <stdlib.h> header file. */
-#define _EVENT_HAVE_STDLIB_H 1
+#define EVENT__HAVE_STDLIB_H 1
/* Define to 1 if you have the <strings.h> header file. */
-#define _EVENT_HAVE_STRINGS_H 1
+#define EVENT__HAVE_STRINGS_H 1
/* Define to 1 if you have the <string.h> header file. */
-#define _EVENT_HAVE_STRING_H 1
+#define EVENT__HAVE_STRING_H 1
/* Define to 1 if you have the `strlcpy' function. */
-/* #undef _EVENT_HAVE_STRLCPY */
+/* #undef EVENT__HAVE_STRLCPY */
/* Define to 1 if you have the `strsep' function. */
-/* #undef _EVENT_HAVE_STRSEP */
+/* #undef EVENT__HAVE_STRSEP */
/* Define to 1 if you have the `strtok_r' function. */
-/* #undef _EVENT_HAVE_STRTOK_R */
+/* #undef EVENT__HAVE_STRTOK_R */
/* Define to 1 if you have the `strtoll' function. */
-/* #define _EVENT_HAVE_STRTOLL 1 */
+/* #define EVENT__HAVE_STRTOLL 1 */
-#define _EVENT_HAVE_STRUCT_ADDRINFO 1
+#define EVENT__HAVE_STRUCT_ADDRINFO 1
/* Define to 1 if the system has the type `struct in6_addr'. */
-#define _EVENT_HAVE_STRUCT_IN6_ADDR 1
+#define EVENT__HAVE_STRUCT_IN6_ADDR 1
/* Define to 1 if `s6_addr16' is member of `struct in6_addr'. */
-#define _EVENT_HAVE_STRUCT_IN6_ADDR_S6_ADDR16 1
+#define EVENT__HAVE_STRUCT_IN6_ADDR_S6_ADDR16 1
/* Define to 1 if `s6_addr32' is member of `struct in6_addr'. */
-#define _EVENT_HAVE_STRUCT_IN6_ADDR_S6_ADDR32 1
+#define EVENT__HAVE_STRUCT_IN6_ADDR_S6_ADDR32 1
/* Define to 1 if the system has the type `struct sockaddr_in6'. */
-#define _EVENT_HAVE_STRUCT_SOCKADDR_IN6 1
+#define EVENT__HAVE_STRUCT_SOCKADDR_IN6 1
/* Define to 1 if `sin6_len' is member of `struct sockaddr_in6'. */
-/* #undef _EVENT_HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN */
+/* #undef EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN */
/* Define to 1 if `sin_len' is member of `struct sockaddr_in'. */
-/* #undef _EVENT_HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
+/* #undef EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
/* Define to 1 if the system has the type `struct sockaddr_storage'. */
-#define _EVENT_HAVE_STRUCT_SOCKADDR_STORAGE 1
+#define EVENT__HAVE_STRUCT_SOCKADDR_STORAGE 1
/* Define to 1 if you have the <sys/devpoll.h> header file. */
-/* #undef _EVENT_HAVE_SYS_DEVPOLL_H */
+/* #undef EVENT__HAVE_SYS_DEVPOLL_H */
/* Define to 1 if you have the <sys/epoll.h> header file. */
-/* #undef _EVENT_HAVE_SYS_EPOLL_H */
+/* #undef EVENT__HAVE_SYS_EPOLL_H */
/* Define to 1 if you have the <sys/eventfd.h> header file. */
-/* #undef _EVENT_HAVE_SYS_EVENTFD_H */
+/* #undef EVENT__HAVE_SYS_EVENTFD_H */
/* Define to 1 if you have the <sys/event.h> header file. */
-/* #undef _EVENT_HAVE_SYS_EVENT_H */
+/* #undef EVENT__HAVE_SYS_EVENT_H */
/* Define to 1 if you have the <sys/ioctl.h> header file. */
-/* #undef _EVENT_HAVE_SYS_IOCTL_H */
+/* #undef EVENT__HAVE_SYS_IOCTL_H */
/* Define to 1 if you have the <sys/mman.h> header file. */
-/* #undef _EVENT_HAVE_SYS_MMAN_H */
+/* #undef EVENT__HAVE_SYS_MMAN_H */
/* Define to 1 if you have the <sys/param.h> header file. */
-/* #define _EVENT_HAVE_SYS_PARAM_H 1 */
+/* #define EVENT__HAVE_SYS_PARAM_H 1 */
/* Define to 1 if you have the <sys/queue.h> header file. */
-/* #undef _EVENT_HAVE_SYS_QUEUE_H */
+/* #undef EVENT__HAVE_SYS_QUEUE_H */
/* Define to 1 if you have the <sys/select.h> header file. */
-/* #undef _EVENT_HAVE_SYS_SELECT_H */
+/* #undef EVENT__HAVE_SYS_SELECT_H */
/* Define to 1 if you have the <sys/sendfile.h> header file. */
-/* #undef _EVENT_HAVE_SYS_SENDFILE_H */
+/* #undef EVENT__HAVE_SYS_SENDFILE_H */
/* Define to 1 if you have the <sys/socket.h> header file. */
-/* #undef _EVENT_HAVE_SYS_SOCKET_H */
+/* #undef EVENT__HAVE_SYS_SOCKET_H */
/* Define to 1 if you have the <sys/stat.h> header file. */
-#define _EVENT_HAVE_SYS_STAT_H 1
+#define EVENT__HAVE_SYS_STAT_H 1
/* Define to 1 if you have the <sys/time.h> header file. */
-/* #define _EVENT_HAVE_SYS_TIME_H 1 */
+/* #define EVENT__HAVE_SYS_TIME_H 1 */
/* Define to 1 if you have the <sys/types.h> header file. */
-#define _EVENT_HAVE_SYS_TYPES_H 1
+#define EVENT__HAVE_SYS_TYPES_H 1
/* Define to 1 if you have the <sys/uio.h> header file. */
-/* #undef _EVENT_HAVE_SYS_UIO_H */
+/* #undef EVENT__HAVE_SYS_UIO_H */
/* Define if TAILQ_FOREACH is defined in <sys/queue.h> */
-/* #undef _EVENT_HAVE_TAILQFOREACH */
+/* #undef EVENT__HAVE_TAILQFOREACH */
/* Define if timeradd is defined in <sys/time.h> */
-/* #undef _EVENT_HAVE_TIMERADD */
+/* #undef EVENT__HAVE_TIMERADD */
/* Define if timerclear is defined in <sys/time.h> */
-#define _EVENT_HAVE_TIMERCLEAR 1
+#define EVENT__HAVE_TIMERCLEAR 1
/* Define if timercmp is defined in <sys/time.h> */
-#define _EVENT_HAVE_TIMERCMP 1
+#define EVENT__HAVE_TIMERCMP 1
/* Define if timerisset is defined in <sys/time.h> */
-#define _EVENT_HAVE_TIMERISSET 1
+#define EVENT__HAVE_TIMERISSET 1
/* Define to 1 if the system has the type `uint16_t'. */
-/* #define _EVENT_HAVE_UINT16_T 1 */
+/* #define EVENT__HAVE_UINT16_T 1 */
/* Define to 1 if the system has the type `uint32_t'. */
-/* #define _EVENT_HAVE_UINT32_T 1 */
+/* #define EVENT__HAVE_UINT32_T 1 */
/* Define to 1 if the system has the type `uint64_t'. */
-/* #define _EVENT_HAVE_UINT64_T 1 */
+/* #define EVENT__HAVE_UINT64_T 1 */
/* Define to 1 if the system has the type `uint8_t'. */
-/* #define _EVENT_HAVE_UINT8_T 1 */
+/* #define EVENT__HAVE_UINT8_T 1 */
/* Define to 1 if you have the <unistd.h> header file. */
-/* #define _EVENT_HAVE_UNISTD_H 1 */
+/* #define EVENT__HAVE_UNISTD_H 1 */
/* Define to 1 if you have the `vasprintf' function. */
-/* #undef _EVENT_HAVE_VASPRINTF */
+/* #undef EVENT__HAVE_VASPRINTF */
/* Define if kqueue works correctly with pipes */
-/* #undef _EVENT_HAVE_WORKING_KQUEUE */
+/* #undef EVENT__HAVE_WORKING_KQUEUE */
/* Numeric representation of the version */
-#define _EVENT_NUMERIC_VERSION 0x02001501
+#define EVENT__NUMERIC_VERSION 0x02010401
/* Name of package */
-#define _EVENT_PACKAGE "libevent"
+#define EVENT__PACKAGE "libevent"
/* Define to the address where bug reports for this package should be sent. */
-#define _EVENT_PACKAGE_BUGREPORT ""
+#define EVENT__PACKAGE_BUGREPORT ""
/* Define to the full name of this package. */
-#define _EVENT_PACKAGE_NAME ""
+#define EVENT__PACKAGE_NAME ""
/* Define to the full name and version of this package. */
-#define _EVENT_PACKAGE_STRING ""
+#define EVENT__PACKAGE_STRING ""
/* Define to the one symbol short name of this package. */
-#define _EVENT_PACKAGE_TARNAME ""
+#define EVENT__PACKAGE_TARNAME ""
/* Define to the version of this package. */
-#define _EVENT_PACKAGE_VERSION ""
+#define EVENT__PACKAGE_VERSION ""
/* Define to necessary symbol if this constant uses a non-standard name on
your system. */
-/* #undef _EVENT_PTHREAD_CREATE_JOINABLE */
+/* #undef EVENT__PTHREAD_CREATE_JOINABLE */
/* The size of a `int', as computed by sizeof. */
-#define _EVENT_SIZEOF_INT 4
+#define EVENT__SIZEOF_INT 4
/* The size of a `long', as computed by sizeof. */
-#define _EVENT_SIZEOF_LONG 4
+#define EVENT__SIZEOF_LONG 4
/* The size of a `long long', as computed by sizeof. */
-#define _EVENT_SIZEOF_LONG_LONG 8
+#define EVENT__SIZEOF_LONG_LONG 8
/* The size of a `short', as computed by sizeof. */
-#define _EVENT_SIZEOF_SHORT 2
+#define EVENT__SIZEOF_SHORT 2
/* The size of `size_t', as computed by sizeof. */
#ifdef _WIN64
-#define _EVENT_SIZEOF_SIZE_T 8
+#define EVENT__SIZEOF_SIZE_T 8
#else
-#define _EVENT_SIZEOF_SIZE_T 4
+#define EVENT__SIZEOF_SIZE_T 4
#endif
/* The size of `void *', as computed by sizeof. */
#ifdef _WIN64
-#define _EVENT_SIZEOF_VOID_P 8
+#define EVENT__SIZEOF_VOID_P 8
#else
-#define _EVENT_SIZEOF_VOID_P 4
+#define EVENT__SIZEOF_VOID_P 4
#endif
/* Define to 1 if you have the ANSI C header files. */
-#define _EVENT_STDC_HEADERS 1
+#define EVENT__STDC_HEADERS 1
/* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
-#define _EVENT_TIME_WITH_SYS_TIME 1
+#define EVENT__TIME_WITH_SYS_TIME 1
/* Version number of package */
-#define _EVENT_VERSION "2.0.21-stable-dev"
+#define EVENT__VERSION "2.1.4-alpha-dev"
/* Define to appropriate substitue if compiler doesnt have __func__ */
-#define _EVENT___func__ __FUNCTION__
+#define EVENT____func__ __FUNCTION__
/* Define to empty if `const' does not conform to ANSI C. */
-/* #undef _EVENT_const */
+/* #undef EVENT__const */
/* Define to `__inline__' or `__inline' if that's what the C compiler
calls it, or to nothing if 'inline' is not supported under any name. */
#ifndef _EVENT___cplusplus
-#define _EVENT_inline __inline
+#define EVENT__inline __inline
#endif
/* Define to `int' if <sys/types.h> does not define. */
-/* #undef _EVENT_pid_t */
+/* #undef EVENT__pid_t */
/* Define to `unsigned' if <sys/types.h> does not define. */
-/* #undef _EVENT_size_t */
+/* #undef EVENT__size_t */
/* Define to unsigned int if you dont have it */
-#define _EVENT_socklen_t unsigned int
+#define EVENT__socklen_t unsigned int
/* Define to `int' if <sys/types.h> does not define. */
-#define _EVENT_ssize_t SSIZE_T
+#define EVENT__ssize_t SSIZE_T
#endif
diff --git a/WIN32-Code/tree.h b/WIN32-Code/tree.h
index 585618dd..2ccfbf20 100644
--- a/WIN32-Code/tree.h
+++ b/WIN32-Code/tree.h
@@ -675,680 +675,3 @@ name##_RB_MINMAX(struct name *head, int val) \
(x) = name##_RB_NEXT(x))
#endif /* _SYS_TREE_H_ */
-/* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */
-/*
- * Copyright 2002 Niels Provos <provos@citi.umich.edu>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef _SYS_TREE_H_
-#define _SYS_TREE_H_
-
-/*
- * This file defines data structures for different types of trees:
- * splay trees and red-black trees.
- *
- * A splay tree is a self-organizing data structure. Every operation
- * on the tree causes a splay to happen. The splay moves the requested
- * node to the root of the tree and partly rebalances it.
- *
- * This has the benefit that request locality causes faster lookups as
- * the requested nodes move to the top of the tree. On the other hand,
- * every lookup causes memory writes.
- *
- * The Balance Theorem bounds the total access time for m operations
- * and n inserts on an initially empty tree as O((m + n)lg n). The
- * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
- *
- * A red-black tree is a binary search tree with the node color as an
- * extra attribute. It fulfills a set of conditions:
- * - every search path from the root to a leaf consists of the
- * same number of black nodes,
- * - each red node (except for the root) has a black parent,
- * - each leaf node is black.
- *
- * Every operation on a red-black tree is bounded as O(lg n).
- * The maximum height of a red-black tree is 2lg (n+1).
- */
-
-#define SPLAY_HEAD(name, type) \
-struct name { \
- struct type *sph_root; /* root of the tree */ \
-}
-
-#define SPLAY_INITIALIZER(root) \
- { NULL }
-
-#define SPLAY_INIT(root) do { \
- (root)->sph_root = NULL; \
-} while (0)
-
-#define SPLAY_ENTRY(type) \
-struct { \
- struct type *spe_left; /* left element */ \
- struct type *spe_right; /* right element */ \
-}
-
-#define SPLAY_LEFT(elm, field) (elm)->field.spe_left
-#define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
-#define SPLAY_ROOT(head) (head)->sph_root
-#define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
-
-/* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
-#define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
- SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
- SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
- (head)->sph_root = tmp; \
-} while (0)
-
-#define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
- SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
- SPLAY_LEFT(tmp, field) = (head)->sph_root; \
- (head)->sph_root = tmp; \
-} while (0)
-
-#define SPLAY_LINKLEFT(head, tmp, field) do { \
- SPLAY_LEFT(tmp, field) = (head)->sph_root; \
- tmp = (head)->sph_root; \
- (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
-} while (0)
-
-#define SPLAY_LINKRIGHT(head, tmp, field) do { \
- SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
- tmp = (head)->sph_root; \
- (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
-} while (0)
-
-#define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
- SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
- SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
- SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
- SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
-} while (0)
-
-/* Generates prototypes and inline functions */
-
-#define SPLAY_PROTOTYPE(name, type, field, cmp) \
-void name##_SPLAY(struct name *, struct type *); \
-void name##_SPLAY_MINMAX(struct name *, int); \
-struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
-struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
- \
-/* Finds the node with the same key as elm */ \
-static __inline struct type * \
-name##_SPLAY_FIND(struct name *head, struct type *elm) \
-{ \
- if (SPLAY_EMPTY(head)) \
- return(NULL); \
- name##_SPLAY(head, elm); \
- if ((cmp)(elm, (head)->sph_root) == 0) \
- return (head->sph_root); \
- return (NULL); \
-} \
- \
-static __inline struct type * \
-name##_SPLAY_NEXT(struct name *head, struct type *elm) \
-{ \
- name##_SPLAY(head, elm); \
- if (SPLAY_RIGHT(elm, field) != NULL) { \
- elm = SPLAY_RIGHT(elm, field); \
- while (SPLAY_LEFT(elm, field) != NULL) { \
- elm = SPLAY_LEFT(elm, field); \
- } \
- } else \
- elm = NULL; \
- return (elm); \
-} \
- \
-static __inline struct type * \
-name##_SPLAY_MIN_MAX(struct name *head, int val) \
-{ \
- name##_SPLAY_MINMAX(head, val); \
- return (SPLAY_ROOT(head)); \
-}
-
-/* Main splay operation.
- * Moves node close to the key of elm to top
- */
-#define SPLAY_GENERATE(name, type, field, cmp) \
-struct type * \
-name##_SPLAY_INSERT(struct name *head, struct type *elm) \
-{ \
- if (SPLAY_EMPTY(head)) { \
- SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
- } else { \
- int __comp; \
- name##_SPLAY(head, elm); \
- __comp = (cmp)(elm, (head)->sph_root); \
- if(__comp < 0) { \
- SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
- SPLAY_RIGHT(elm, field) = (head)->sph_root; \
- SPLAY_LEFT((head)->sph_root, field) = NULL; \
- } else if (__comp > 0) { \
- SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
- SPLAY_LEFT(elm, field) = (head)->sph_root; \
- SPLAY_RIGHT((head)->sph_root, field) = NULL; \
- } else \
- return ((head)->sph_root); \
- } \
- (head)->sph_root = (elm); \
- return (NULL); \
-} \
- \
-struct type * \
-name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
-{ \
- struct type *__tmp; \
- if (SPLAY_EMPTY(head)) \
- return (NULL); \
- name##_SPLAY(head, elm); \
- if ((cmp)(elm, (head)->sph_root) == 0) { \
- if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
- (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
- } else { \
- __tmp = SPLAY_RIGHT((head)->sph_root, field); \
- (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
- name##_SPLAY(head, elm); \
- SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
- } \
- return (elm); \
- } \
- return (NULL); \
-} \
- \
-void \
-name##_SPLAY(struct name *head, struct type *elm) \
-{ \
- struct type __node, *__left, *__right, *__tmp; \
- int __comp; \
-\
- SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
- __left = __right = &__node; \
-\
- while ((__comp = (cmp)(elm, (head)->sph_root))) { \
- if (__comp < 0) { \
- __tmp = SPLAY_LEFT((head)->sph_root, field); \
- if (__tmp == NULL) \
- break; \
- if ((cmp)(elm, __tmp) < 0){ \
- SPLAY_ROTATE_RIGHT(head, __tmp, field); \
- if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
- break; \
- } \
- SPLAY_LINKLEFT(head, __right, field); \
- } else if (__comp > 0) { \
- __tmp = SPLAY_RIGHT((head)->sph_root, field); \
- if (__tmp == NULL) \
- break; \
- if ((cmp)(elm, __tmp) > 0){ \
- SPLAY_ROTATE_LEFT(head, __tmp, field); \
- if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
- break; \
- } \
- SPLAY_LINKRIGHT(head, __left, field); \
- } \
- } \
- SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
-} \
- \
-/* Splay with either the minimum or the maximum element \
- * Used to find minimum or maximum element in tree. \
- */ \
-void name##_SPLAY_MINMAX(struct name *head, int __comp) \
-{ \
- struct type __node, *__left, *__right, *__tmp; \
-\
- SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
- __left = __right = &__node; \
-\
- while (1) { \
- if (__comp < 0) { \
- __tmp = SPLAY_LEFT((head)->sph_root, field); \
- if (__tmp == NULL) \
- break; \
- if (__comp < 0){ \
- SPLAY_ROTATE_RIGHT(head, __tmp, field); \
- if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
- break; \
- } \
- SPLAY_LINKLEFT(head, __right, field); \
- } else if (__comp > 0) { \
- __tmp = SPLAY_RIGHT((head)->sph_root, field); \
- if (__tmp == NULL) \
- break; \
- if (__comp > 0) { \
- SPLAY_ROTATE_LEFT(head, __tmp, field); \
- if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
- break; \
- } \
- SPLAY_LINKRIGHT(head, __left, field); \
- } \
- } \
- SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
-}
-
-#define SPLAY_NEGINF -1
-#define SPLAY_INF 1
-
-#define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
-#define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
-#define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
-#define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
-#define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
- : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
-#define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
- : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
-
-#define SPLAY_FOREACH(x, name, head) \
- for ((x) = SPLAY_MIN(name, head); \
- (x) != NULL; \
- (x) = SPLAY_NEXT(name, head, x))
-
-/* Macros that define a red-back tree */
-#define RB_HEAD(name, type) \
-struct name { \
- struct type *rbh_root; /* root of the tree */ \
-}
-
-#define RB_INITIALIZER(root) \
- { NULL }
-
-#define RB_INIT(root) do { \
- (root)->rbh_root = NULL; \
-} while (0)
-
-#define RB_BLACK 0
-#define RB_RED 1
-#define RB_ENTRY(type) \
-struct { \
- struct type *rbe_left; /* left element */ \
- struct type *rbe_right; /* right element */ \
- struct type *rbe_parent; /* parent element */ \
- int rbe_color; /* node color */ \
-}
-
-#define RB_LEFT(elm, field) (elm)->field.rbe_left
-#define RB_RIGHT(elm, field) (elm)->field.rbe_right
-#define RB_PARENT(elm, field) (elm)->field.rbe_parent
-#define RB_COLOR(elm, field) (elm)->field.rbe_color
-#define RB_ROOT(head) (head)->rbh_root
-#define RB_EMPTY(head) (RB_ROOT(head) == NULL)
-
-#define RB_SET(elm, parent, field) do { \
- RB_PARENT(elm, field) = parent; \
- RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
- RB_COLOR(elm, field) = RB_RED; \
-} while (0)
-
-#define RB_SET_BLACKRED(black, red, field) do { \
- RB_COLOR(black, field) = RB_BLACK; \
- RB_COLOR(red, field) = RB_RED; \
-} while (0)
-
-#ifndef RB_AUGMENT
-#define RB_AUGMENT(x)
-#endif
-
-#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
- (tmp) = RB_RIGHT(elm, field); \
- if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) { \
- RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
- } \
- RB_AUGMENT(elm); \
- if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
- if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
- RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
- else \
- RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
- } else \
- (head)->rbh_root = (tmp); \
- RB_LEFT(tmp, field) = (elm); \
- RB_PARENT(elm, field) = (tmp); \
- RB_AUGMENT(tmp); \
- if ((RB_PARENT(tmp, field))) \
- RB_AUGMENT(RB_PARENT(tmp, field)); \
-} while (0)
-
-#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
- (tmp) = RB_LEFT(elm, field); \
- if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) { \
- RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
- } \
- RB_AUGMENT(elm); \
- if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
- if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
- RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
- else \
- RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
- } else \
- (head)->rbh_root = (tmp); \
- RB_RIGHT(tmp, field) = (elm); \
- RB_PARENT(elm, field) = (tmp); \
- RB_AUGMENT(tmp); \
- if ((RB_PARENT(tmp, field))) \
- RB_AUGMENT(RB_PARENT(tmp, field)); \
-} while (0)
-
-/* Generates prototypes and inline functions */
-#define RB_PROTOTYPE(name, type, field, cmp) \
-void name##_RB_INSERT_COLOR(struct name *, struct type *); \
-void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
-struct type *name##_RB_REMOVE(struct name *, struct type *); \
-struct type *name##_RB_INSERT(struct name *, struct type *); \
-struct type *name##_RB_FIND(struct name *, struct type *); \
-struct type *name##_RB_NEXT(struct type *); \
-struct type *name##_RB_MINMAX(struct name *, int); \
- \
-
-/* Main rb operation.
- * Moves node close to the key of elm to top
- */
-#define RB_GENERATE(name, type, field, cmp) \
-void \
-name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
-{ \
- struct type *parent, *gparent, *tmp; \
- while ((parent = RB_PARENT(elm, field)) && \
- RB_COLOR(parent, field) == RB_RED) { \
- gparent = RB_PARENT(parent, field); \
- if (parent == RB_LEFT(gparent, field)) { \
- tmp = RB_RIGHT(gparent, field); \
- if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
- RB_COLOR(tmp, field) = RB_BLACK; \
- RB_SET_BLACKRED(parent, gparent, field);\
- elm = gparent; \
- continue; \
- } \
- if (RB_RIGHT(parent, field) == elm) { \
- RB_ROTATE_LEFT(head, parent, tmp, field);\
- tmp = parent; \
- parent = elm; \
- elm = tmp; \
- } \
- RB_SET_BLACKRED(parent, gparent, field); \
- RB_ROTATE_RIGHT(head, gparent, tmp, field); \
- } else { \
- tmp = RB_LEFT(gparent, field); \
- if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
- RB_COLOR(tmp, field) = RB_BLACK; \
- RB_SET_BLACKRED(parent, gparent, field);\
- elm = gparent; \
- continue; \
- } \
- if (RB_LEFT(parent, field) == elm) { \
- RB_ROTATE_RIGHT(head, parent, tmp, field);\
- tmp = parent; \
- parent = elm; \
- elm = tmp; \
- } \
- RB_SET_BLACKRED(parent, gparent, field); \
- RB_ROTATE_LEFT(head, gparent, tmp, field); \
- } \
- } \
- RB_COLOR(head->rbh_root, field) = RB_BLACK; \
-} \
- \
-void \
-name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
-{ \
- struct type *tmp; \
- while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
- elm != RB_ROOT(head)) { \
- if (RB_LEFT(parent, field) == elm) { \
- tmp = RB_RIGHT(parent, field); \
- if (RB_COLOR(tmp, field) == RB_RED) { \
- RB_SET_BLACKRED(tmp, parent, field); \
- RB_ROTATE_LEFT(head, parent, tmp, field);\
- tmp = RB_RIGHT(parent, field); \
- } \
- if ((RB_LEFT(tmp, field) == NULL || \
- RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
- (RB_RIGHT(tmp, field) == NULL || \
- RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
- RB_COLOR(tmp, field) = RB_RED; \
- elm = parent; \
- parent = RB_PARENT(elm, field); \
- } else { \
- if (RB_RIGHT(tmp, field) == NULL || \
- RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
- struct type *oleft; \
- if ((oleft = RB_LEFT(tmp, field)))\
- RB_COLOR(oleft, field) = RB_BLACK;\
- RB_COLOR(tmp, field) = RB_RED; \
- RB_ROTATE_RIGHT(head, tmp, oleft, field);\
- tmp = RB_RIGHT(parent, field); \
- } \
- RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
- RB_COLOR(parent, field) = RB_BLACK; \
- if (RB_RIGHT(tmp, field)) \
- RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
- RB_ROTATE_LEFT(head, parent, tmp, field);\
- elm = RB_ROOT(head); \
- break; \
- } \
- } else { \
- tmp = RB_LEFT(parent, field); \
- if (RB_COLOR(tmp, field) == RB_RED) { \
- RB_SET_BLACKRED(tmp, parent, field); \
- RB_ROTATE_RIGHT(head, parent, tmp, field);\
- tmp = RB_LEFT(parent, field); \
- } \
- if ((RB_LEFT(tmp, field) == NULL || \
- RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
- (RB_RIGHT(tmp, field) == NULL || \
- RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
- RB_COLOR(tmp, field) = RB_RED; \
- elm = parent; \
- parent = RB_PARENT(elm, field); \
- } else { \
- if (RB_LEFT(tmp, field) == NULL || \
- RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
- struct type *oright; \
- if ((oright = RB_RIGHT(tmp, field)))\
- RB_COLOR(oright, field) = RB_BLACK;\
- RB_COLOR(tmp, field) = RB_RED; \
- RB_ROTATE_LEFT(head, tmp, oright, field);\
- tmp = RB_LEFT(parent, field); \
- } \
- RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
- RB_COLOR(parent, field) = RB_BLACK; \
- if (RB_LEFT(tmp, field)) \
- RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
- RB_ROTATE_RIGHT(head, parent, tmp, field);\
- elm = RB_ROOT(head); \
- break; \
- } \
- } \
- } \
- if (elm) \
- RB_COLOR(elm, field) = RB_BLACK; \
-} \
- \
-struct type * \
-name##_RB_REMOVE(struct name *head, struct type *elm) \
-{ \
- struct type *child, *parent, *old = elm; \
- int color; \
- if (RB_LEFT(elm, field) == NULL) \
- child = RB_RIGHT(elm, field); \
- else if (RB_RIGHT(elm, field) == NULL) \
- child = RB_LEFT(elm, field); \
- else { \
- struct type *left; \
- elm = RB_RIGHT(elm, field); \
- while ((left = RB_LEFT(elm, field))) \
- elm = left; \
- child = RB_RIGHT(elm, field); \
- parent = RB_PARENT(elm, field); \
- color = RB_COLOR(elm, field); \
- if (child) \
- RB_PARENT(child, field) = parent; \
- if (parent) { \
- if (RB_LEFT(parent, field) == elm) \
- RB_LEFT(parent, field) = child; \
- else \
- RB_RIGHT(parent, field) = child; \
- RB_AUGMENT(parent); \
- } else \
- RB_ROOT(head) = child; \
- if (RB_PARENT(elm, field) == old) \
- parent = elm; \
- (elm)->field = (old)->field; \
- if (RB_PARENT(old, field)) { \
- if (RB_LEFT(RB_PARENT(old, field), field) == old)\
- RB_LEFT(RB_PARENT(old, field), field) = elm;\
- else \
- RB_RIGHT(RB_PARENT(old, field), field) = elm;\
- RB_AUGMENT(RB_PARENT(old, field)); \
- } else \
- RB_ROOT(head) = elm; \
- RB_PARENT(RB_LEFT(old, field), field) = elm; \
- if (RB_RIGHT(old, field)) \
- RB_PARENT(RB_RIGHT(old, field), field) = elm; \
- if (parent) { \
- left = parent; \
- do { \
- RB_AUGMENT(left); \
- } while ((left = RB_PARENT(left, field))); \
- } \
- goto color; \
- } \
- parent = RB_PARENT(elm, field); \
- color = RB_COLOR(elm, field); \
- if (child) \
- RB_PARENT(child, field) = parent; \
- if (parent) { \
- if (RB_LEFT(parent, field) == elm) \
- RB_LEFT(parent, field) = child; \
- else \
- RB_RIGHT(parent, field) = child; \
- RB_AUGMENT(parent); \
- } else \
- RB_ROOT(head) = child; \
-color: \
- if (color == RB_BLACK) \
- name##_RB_REMOVE_COLOR(head, parent, child); \
- return (old); \
-} \
- \
-/* Inserts a node into the RB tree */ \
-struct type * \
-name##_RB_INSERT(struct name *head, struct type *elm) \
-{ \
- struct type *tmp; \
- struct type *parent = NULL; \
- int comp = 0; \
- tmp = RB_ROOT(head); \
- while (tmp) { \
- parent = tmp; \
- comp = (cmp)(elm, parent); \
- if (comp < 0) \
- tmp = RB_LEFT(tmp, field); \
- else if (comp > 0) \
- tmp = RB_RIGHT(tmp, field); \
- else \
- return (tmp); \
- } \
- RB_SET(elm, parent, field); \
- if (parent != NULL) { \
- if (comp < 0) \
- RB_LEFT(parent, field) = elm; \
- else \
- RB_RIGHT(parent, field) = elm; \
- RB_AUGMENT(parent); \
- } else \
- RB_ROOT(head) = elm; \
- name##_RB_INSERT_COLOR(head, elm); \
- return (NULL); \
-} \
- \
-/* Finds the node with the same key as elm */ \
-struct type * \
-name##_RB_FIND(struct name *head, struct type *elm) \
-{ \
- struct type *tmp = RB_ROOT(head); \
- int comp; \
- while (tmp) { \
- comp = cmp(elm, tmp); \
- if (comp < 0) \
- tmp = RB_LEFT(tmp, field); \
- else if (comp > 0) \
- tmp = RB_RIGHT(tmp, field); \
- else \
- return (tmp); \
- } \
- return (NULL); \
-} \
- \
-struct type * \
-name##_RB_NEXT(struct type *elm) \
-{ \
- if (RB_RIGHT(elm, field)) { \
- elm = RB_RIGHT(elm, field); \
- while (RB_LEFT(elm, field)) \
- elm = RB_LEFT(elm, field); \
- } else { \
- if (RB_PARENT(elm, field) && \
- (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
- elm = RB_PARENT(elm, field); \
- else { \
- while (RB_PARENT(elm, field) && \
- (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
- elm = RB_PARENT(elm, field); \
- elm = RB_PARENT(elm, field); \
- } \
- } \
- return (elm); \
-} \
- \
-struct type * \
-name##_RB_MINMAX(struct name *head, int val) \
-{ \
- struct type *tmp = RB_ROOT(head); \
- struct type *parent = NULL; \
- while (tmp) { \
- parent = tmp; \
- if (val < 0) \
- tmp = RB_LEFT(tmp, field); \
- else \
- tmp = RB_RIGHT(tmp, field); \
- } \
- return (parent); \
-}
-
-#define RB_NEGINF -1
-#define RB_INF 1
-
-#define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
-#define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
-#define RB_FIND(name, x, y) name##_RB_FIND(x, y)
-#define RB_NEXT(name, x, y) name##_RB_NEXT(y)
-#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
-#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
-
-#define RB_FOREACH(x, name, head) \
- for ((x) = RB_MIN(name, head); \
- (x) != NULL; \
- (x) = name##_RB_NEXT(x))
-
-#endif /* _SYS_TREE_H_ */
diff --git a/arc4random.c b/arc4random.c
index 39773399..a2338e69 100644
--- a/arc4random.c
+++ b/arc4random.c
@@ -50,7 +50,8 @@
#endif
#ifndef ARC4RANDOM_NO_INCLUDES
-#ifdef WIN32
+#include "evconfig-private.h"
+#ifdef _WIN32
#include <wincrypt.h>
#include <process.h>
#else
@@ -58,7 +59,7 @@
#include <unistd.h>
#include <sys/param.h>
#include <sys/time.h>
-#ifdef _EVENT_HAVE_SYS_SYSCTL_H
+#ifdef EVENT__HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#endif
@@ -79,7 +80,7 @@ struct arc4_stream {
unsigned char s[256];
};
-#ifdef WIN32
+#ifdef _WIN32
#define getpid _getpid
#define pid_t int
#endif
@@ -120,7 +121,7 @@ arc4_addrandom(const unsigned char *dat, int datlen)
rs.j = rs.i;
}
-#ifndef WIN32
+#ifndef _WIN32
static ssize_t
read_all(int fd, unsigned char *buf, size_t count)
{
@@ -140,7 +141,7 @@ read_all(int fd, unsigned char *buf, size_t count)
}
#endif
-#ifdef WIN32
+#ifdef _WIN32
#define TRY_SEED_WIN32
static int
arc4_seed_win32(void)
@@ -167,8 +168,8 @@ arc4_seed_win32(void)
}
#endif
-#if defined(_EVENT_HAVE_SYS_SYSCTL_H) && defined(_EVENT_HAVE_SYSCTL)
-#if _EVENT_HAVE_DECL_CTL_KERN && _EVENT_HAVE_DECL_KERN_RANDOM && _EVENT_HAVE_DECL_RANDOM_UUID
+#if defined(EVENT__HAVE_SYS_SYSCTL_H) && defined(EVENT__HAVE_SYSCTL)
+#if EVENT__HAVE_DECL_CTL_KERN && EVENT__HAVE_DECL_KERN_RANDOM && EVENT__HAVE_DECL_RANDOM_UUID
#define TRY_SEED_SYSCTL_LINUX
static int
arc4_seed_sysctl_linux(void)
@@ -205,7 +206,7 @@ arc4_seed_sysctl_linux(void)
}
#endif
-#if _EVENT_HAVE_DECL_CTL_KERN && _EVENT_HAVE_DECL_KERN_ARND
+#if EVENT__HAVE_DECL_CTL_KERN && EVENT__HAVE_DECL_KERN_ARND
#define TRY_SEED_SYSCTL_BSD
static int
arc4_seed_sysctl_bsd(void)
@@ -244,7 +245,7 @@ arc4_seed_sysctl_bsd(void)
return 0;
}
#endif
-#endif /* defined(_EVENT_HAVE_SYS_SYSCTL_H) */
+#endif /* defined(EVENT__HAVE_SYS_SYSCTL_H) */
#ifdef __linux__
#define TRY_SEED_PROC_SYS_KERNEL_RANDOM_UUID
@@ -260,7 +261,7 @@ arc4_seed_proc_sys_kernel_random_uuid(void)
unsigned char entropy[64];
int bytes, n, i, nybbles;
for (bytes = 0; bytes<ADD_ENTROPY; ) {
- fd = evutil_open_closeonexec("/proc/sys/kernel/random/uuid", O_RDONLY, 0);
+ fd = evutil_open_closeonexec_("/proc/sys/kernel/random/uuid", O_RDONLY, 0);
if (fd < 0)
return -1;
n = read(fd, buf, sizeof(buf));
@@ -269,8 +270,8 @@ arc4_seed_proc_sys_kernel_random_uuid(void)
return -1;
memset(entropy, 0, sizeof(entropy));
for (i=nybbles=0; i<n; ++i) {
- if (EVUTIL_ISXDIGIT(buf[i])) {
- int nyb = evutil_hex_char_to_int(buf[i]);
+ if (EVUTIL_ISXDIGIT_(buf[i])) {
+ int nyb = evutil_hex_char_to_int_(buf[i]);
if (nybbles & 1) {
entropy[nybbles/2] |= nyb;
} else {
@@ -291,7 +292,7 @@ arc4_seed_proc_sys_kernel_random_uuid(void)
}
#endif
-#ifndef WIN32
+#ifndef _WIN32
#define TRY_SEED_URANDOM
static char *arc4random_urandom_filename = NULL;
@@ -301,7 +302,7 @@ static int arc4_seed_urandom_helper_(const char *fname)
int fd;
size_t n;
- fd = evutil_open_closeonexec(fname, O_RDONLY, 0);
+ fd = evutil_open_closeonexec_(fname, O_RDONLY, 0);
if (fd<0)
return -1;
n = read_all(fd, buf, sizeof(buf));
@@ -453,9 +454,9 @@ ARC4RANDOM_EXPORT int
arc4random_stir(void)
{
int val;
- _ARC4_LOCK();
+ ARC4_LOCK_();
val = arc4_stir();
- _ARC4_UNLOCK();
+ ARC4_UNLOCK_();
return val;
}
#endif
@@ -465,7 +466,7 @@ ARC4RANDOM_EXPORT void
arc4random_addrandom(const unsigned char *dat, int datlen)
{
int j;
- _ARC4_LOCK();
+ ARC4_LOCK_();
if (!rs_initialized)
arc4_stir();
for (j = 0; j < datlen; j += 256) {
@@ -475,7 +476,7 @@ arc4random_addrandom(const unsigned char *dat, int datlen)
* crazy like passing us all the files in /var/log. */
arc4_addrandom(dat + j, datlen - j);
}
- _ARC4_UNLOCK();
+ ARC4_UNLOCK_();
}
#endif
@@ -484,27 +485,27 @@ ARC4RANDOM_EXPORT ARC4RANDOM_UINT32
arc4random(void)
{
ARC4RANDOM_UINT32 val;
- _ARC4_LOCK();
+ ARC4_LOCK_();
arc4_count -= 4;
arc4_stir_if_needed();
val = arc4_getword();
- _ARC4_UNLOCK();
+ ARC4_UNLOCK_();
return val;
}
#endif
ARC4RANDOM_EXPORT void
-arc4random_buf(void *_buf, size_t n)
+arc4random_buf(void *buf_, size_t n)
{
- unsigned char *buf = _buf;
- _ARC4_LOCK();
+ unsigned char *buf = buf_;
+ ARC4_LOCK_();
arc4_stir_if_needed();
while (n--) {
if (--arc4_count <= 0)
arc4_stir();
buf[n] = arc4_getbyte();
}
- _ARC4_UNLOCK();
+ ARC4_UNLOCK_();
}
#ifndef ARC4RANDOM_NOUNIFORM
diff --git a/buffer.c b/buffer.c
index 388615cd..231f1914 100644
--- a/buffer.c
+++ b/buffer.c
@@ -26,52 +26,58 @@
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#include <io.h>
#endif
-#ifdef _EVENT_HAVE_VASPRINTF
-/* If we have vasprintf, we need to define this before we include stdio.h. */
-#define _GNU_SOURCE
+#ifdef EVENT__HAVE_VASPRINTF
+/* If we have vasprintf, we need to define _GNU_SOURCE before we include
+ * stdio.h. This comes from evconfig-private.h.
+ */
#endif
#include <sys/types.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
-#ifdef _EVENT_HAVE_SYS_SOCKET_H
+#ifdef EVENT__HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
-#ifdef _EVENT_HAVE_SYS_UIO_H
+#ifdef EVENT__HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
-#ifdef _EVENT_HAVE_SYS_IOCTL_H
+#ifdef EVENT__HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
-#ifdef _EVENT_HAVE_SYS_MMAN_H
+#ifdef EVENT__HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
-#ifdef _EVENT_HAVE_SYS_SENDFILE_H
+#ifdef EVENT__HAVE_SYS_SENDFILE_H
#include <sys/sendfile.h>
#endif
+#ifdef EVENT__HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#ifdef _EVENT_HAVE_STDARG_H
+#ifdef EVENT__HAVE_STDARG_H
#include <stdarg.h>
#endif
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <limits.h>
@@ -83,7 +89,6 @@
#include "event2/bufferevent_compat.h"
#include "event2/bufferevent_struct.h"
#include "event2/thread.h"
-#include "event2/event-config.h"
#include "log-internal.h"
#include "mm-internal.h"
#include "util-internal.h"
@@ -97,28 +102,20 @@
#endif
/* send file support */
-#if defined(_EVENT_HAVE_SYS_SENDFILE_H) && defined(_EVENT_HAVE_SENDFILE) && defined(__linux__)
+#if defined(EVENT__HAVE_SYS_SENDFILE_H) && defined(EVENT__HAVE_SENDFILE) && defined(__linux__)
#define USE_SENDFILE 1
#define SENDFILE_IS_LINUX 1
-#elif defined(_EVENT_HAVE_SENDFILE) && defined(__FreeBSD__)
+#elif defined(EVENT__HAVE_SENDFILE) && defined(__FreeBSD__)
#define USE_SENDFILE 1
#define SENDFILE_IS_FREEBSD 1
-#elif defined(_EVENT_HAVE_SENDFILE) && defined(__APPLE__)
+#elif defined(EVENT__HAVE_SENDFILE) && defined(__APPLE__)
#define USE_SENDFILE 1
#define SENDFILE_IS_MACOSX 1
-#elif defined(_EVENT_HAVE_SENDFILE) && defined(__sun__) && defined(__svr4__)
+#elif defined(EVENT__HAVE_SENDFILE) && defined(__sun__) && defined(__svr4__)
#define USE_SENDFILE 1
#define SENDFILE_IS_SOLARIS 1
#endif
-#ifdef USE_SENDFILE
-static int use_sendfile = 1;
-#endif
-#ifdef _EVENT_HAVE_MMAP
-static int use_mmap = 1;
-#endif
-
-
/* Mask of user-selectable callback flags. */
#define EVBUFFER_CB_USER_FLAGS 0xffff
/* Mask of all internal-use-only flags. */
@@ -135,21 +132,25 @@ static int use_mmap = 1;
#define CHAIN_PINNED(ch) (((ch)->flags & EVBUFFER_MEM_PINNED_ANY) != 0)
#define CHAIN_PINNED_R(ch) (((ch)->flags & EVBUFFER_MEM_PINNED_R) != 0)
+/* evbuffer_ptr support */
+#define PTR_NOT_FOUND(ptr) do { \
+ (ptr)->pos = -1; \
+ (ptr)->internal_.chain = NULL; \
+ (ptr)->internal_.pos_in_chain = 0; \
+} while (0)
+
static void evbuffer_chain_align(struct evbuffer_chain *chain);
static int evbuffer_chain_should_realign(struct evbuffer_chain *chain,
size_t datalen);
-static void evbuffer_deferred_callback(struct deferred_cb *cb, void *arg);
+static void evbuffer_deferred_callback(struct event_callback *cb, void *arg);
static int evbuffer_ptr_memcmp(const struct evbuffer *buf,
const struct evbuffer_ptr *pos, const char *mem, size_t len);
static struct evbuffer_chain *evbuffer_expand_singlechain(struct evbuffer *buf,
size_t datlen);
-
-#ifdef WIN32
-static int evbuffer_readfile(struct evbuffer *buf, evutil_socket_t fd,
- ev_ssize_t howmuch);
-#else
-#define evbuffer_readfile evbuffer_read
-#endif
+static int evbuffer_ptr_subtract(struct evbuffer *buf, struct evbuffer_ptr *pos,
+ size_t howfar);
+static int evbuffer_file_segment_materialize(struct evbuffer_file_segment *seg);
+static inline void evbuffer_chain_incref(struct evbuffer_chain *chain);
static struct evbuffer_chain *
evbuffer_chain_new(size_t size)
@@ -177,50 +178,66 @@ evbuffer_chain_new(size_t size)
*/
chain->buffer = EVBUFFER_CHAIN_EXTRA(u_char, chain);
+ chain->refcnt = 1;
+
return (chain);
}
static inline void
evbuffer_chain_free(struct evbuffer_chain *chain)
{
+ EVUTIL_ASSERT(chain->refcnt > 0);
+ if (--chain->refcnt > 0) {
+ /* chain is still referenced by other chains */
+ return;
+ }
+
if (CHAIN_PINNED(chain)) {
+ /* will get freed once no longer dangling */
+ chain->refcnt++;
chain->flags |= EVBUFFER_DANGLING;
return;
}
- if (chain->flags & (EVBUFFER_MMAP|EVBUFFER_SENDFILE|
- EVBUFFER_REFERENCE)) {
- if (chain->flags & EVBUFFER_REFERENCE) {
- struct evbuffer_chain_reference *info =
- EVBUFFER_CHAIN_EXTRA(
- struct evbuffer_chain_reference,
- chain);
- if (info->cleanupfn)
- (*info->cleanupfn)(chain->buffer,
- chain->buffer_len,
- info->extra);
- }
-#ifdef _EVENT_HAVE_MMAP
- if (chain->flags & EVBUFFER_MMAP) {
- struct evbuffer_chain_fd *info =
- EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_fd,
- chain);
- if (munmap(chain->buffer, chain->buffer_len) == -1)
- event_warn("%s: munmap failed", __func__);
- if (close(info->fd) == -1)
- event_warn("%s: close(%d) failed",
- __func__, info->fd);
- }
+
+ /* safe to release chain, it's either a referencing
+ * chain or all references to it have been freed */
+ if (chain->flags & EVBUFFER_REFERENCE) {
+ struct evbuffer_chain_reference *info =
+ EVBUFFER_CHAIN_EXTRA(
+ struct evbuffer_chain_reference,
+ chain);
+ if (info->cleanupfn)
+ (*info->cleanupfn)(chain->buffer,
+ chain->buffer_len,
+ info->extra);
+ }
+ if (chain->flags & EVBUFFER_FILESEGMENT) {
+ struct evbuffer_chain_file_segment *info =
+ EVBUFFER_CHAIN_EXTRA(
+ struct evbuffer_chain_file_segment,
+ chain);
+ if (info->segment) {
+#ifdef _WIN32
+ if (info->segment->is_mapping)
+ UnmapViewOfFile(chain->buffer);
#endif
-#ifdef USE_SENDFILE
- if (chain->flags & EVBUFFER_SENDFILE) {
- struct evbuffer_chain_fd *info =
- EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_fd,
- chain);
- if (close(info->fd) == -1)
- event_warn("%s: close(%d) failed",
- __func__, info->fd);
+ evbuffer_file_segment_free(info->segment);
}
-#endif
+ }
+ if (chain->flags & EVBUFFER_MULTICAST) {
+ struct evbuffer_multicast_parent *info =
+ EVBUFFER_CHAIN_EXTRA(
+ struct evbuffer_multicast_parent,
+ chain);
+ /* referencing chain is being freed, decrease
+ * refcounts of source chain and associated
+ * evbuffer (which get freed once both reach
+ * zero) */
+ EVUTIL_ASSERT(info->source != NULL);
+ EVUTIL_ASSERT(info->parent != NULL);
+ EVBUFFER_LOCK(info->source);
+ evbuffer_chain_free(info->parent);
+ evbuffer_decref_and_unlock_(info->source);
}
mm_free(chain);
@@ -290,21 +307,11 @@ evbuffer_chain_insert(struct evbuffer *buf,
EVUTIL_ASSERT(buf->first == NULL);
buf->first = buf->last = chain;
} else {
- struct evbuffer_chain **ch = buf->last_with_datap;
- /* Find the first victim chain. It might be *last_with_datap */
- while ((*ch) && ((*ch)->off != 0 || CHAIN_PINNED(*ch)))
- ch = &(*ch)->next;
- if (*ch == NULL) {
- /* There is no victim; just append this new chain. */
- buf->last->next = chain;
- if (chain->off)
- buf->last_with_datap = &buf->last->next;
- } else {
- /* Replace all victim chains with this chain. */
- EVUTIL_ASSERT(evbuffer_chains_all_empty(*ch));
- evbuffer_free_all_chains(*ch);
- *ch = chain;
- }
+ struct evbuffer_chain **chp;
+ chp = evbuffer_free_trailing_empty_chains(buf);
+ *chp = chain;
+ if (chain->off)
+ buf->last_with_datap = chp;
buf->last = chain;
}
buf->total_len += chain->off;
@@ -321,14 +328,14 @@ evbuffer_chain_insert_new(struct evbuffer *buf, size_t datlen)
}
void
-_evbuffer_chain_pin(struct evbuffer_chain *chain, unsigned flag)
+evbuffer_chain_pin_(struct evbuffer_chain *chain, unsigned flag)
{
EVUTIL_ASSERT((chain->flags & flag) == 0);
chain->flags |= flag;
}
void
-_evbuffer_chain_unpin(struct evbuffer_chain *chain, unsigned flag)
+evbuffer_chain_unpin_(struct evbuffer_chain *chain, unsigned flag)
{
EVUTIL_ASSERT((chain->flags & flag) != 0);
chain->flags &= ~flag;
@@ -336,6 +343,12 @@ _evbuffer_chain_unpin(struct evbuffer_chain *chain, unsigned flag)
evbuffer_chain_free(chain);
}
+static inline void
+evbuffer_chain_incref(struct evbuffer_chain *chain)
+{
+ ++chain->refcnt;
+}
+
struct evbuffer *
evbuffer_new(void)
{
@@ -345,7 +358,7 @@ evbuffer_new(void)
if (buffer == NULL)
return (NULL);
- TAILQ_INIT(&buffer->callbacks);
+ LIST_INIT(&buffer->callbacks);
buffer->refcnt = 1;
buffer->last_with_datap = &buffer->first;
@@ -371,7 +384,7 @@ evbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags)
}
void
-_evbuffer_incref(struct evbuffer *buf)
+evbuffer_incref_(struct evbuffer *buf)
{
EVBUFFER_LOCK(buf);
++buf->refcnt;
@@ -379,7 +392,7 @@ _evbuffer_incref(struct evbuffer *buf)
}
void
-_evbuffer_incref_and_lock(struct evbuffer *buf)
+evbuffer_incref_and_lock_(struct evbuffer *buf)
{
EVBUFFER_LOCK(buf);
++buf->refcnt;
@@ -389,9 +402,10 @@ int
evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base)
{
EVBUFFER_LOCK(buffer);
- buffer->cb_queue = event_base_get_deferred_cb_queue(base);
+ buffer->cb_queue = base;
buffer->deferred_cbs = 1;
- event_deferred_cb_init(&buffer->deferred,
+ event_deferred_cb_init_(&buffer->deferred,
+ event_base_get_npriorities(base) / 2,
evbuffer_deferred_callback, buffer);
EVBUFFER_UNLOCK(buffer);
return 0;
@@ -400,7 +414,7 @@ evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base)
int
evbuffer_enable_locking(struct evbuffer *buf, void *lock)
{
-#ifdef _EVENT_DISABLE_THREAD_SUPPORT
+#ifdef EVENT__DISABLE_THREAD_SUPPORT
return -1;
#else
if (buf->lock)
@@ -422,7 +436,7 @@ evbuffer_enable_locking(struct evbuffer *buf, void *lock)
}
void
-evbuffer_set_parent(struct evbuffer *buf, struct bufferevent *bev)
+evbuffer_set_parent_(struct evbuffer *buf, struct bufferevent *bev)
{
EVBUFFER_LOCK(buf);
buf->parent = bev;
@@ -454,7 +468,7 @@ evbuffer_run_callbacks(struct evbuffer *buffer, int running_deferred)
ASSERT_EVBUFFER_LOCKED(buffer);
- if (TAILQ_EMPTY(&buffer->callbacks)) {
+ if (LIST_EMPTY(&buffer->callbacks)) {
buffer->n_add_for_cb = buffer->n_del_for_cb = 0;
return;
}
@@ -469,12 +483,12 @@ evbuffer_run_callbacks(struct evbuffer *buffer, int running_deferred)
buffer->n_add_for_cb = 0;
buffer->n_del_for_cb = 0;
}
- for (cbent = TAILQ_FIRST(&buffer->callbacks);
- cbent != TAILQ_END(&buffer->callbacks);
+ for (cbent = LIST_FIRST(&buffer->callbacks);
+ cbent != LIST_END(&buffer->callbacks);
cbent = next) {
/* Get the 'next' pointer now in case this callback decides
* to remove itself or something. */
- next = TAILQ_NEXT(cbent, next);
+ next = LIST_NEXT(cbent, next);
if ((cbent->flags & mask) != masked_val)
continue;
@@ -488,28 +502,27 @@ evbuffer_run_callbacks(struct evbuffer *buffer, int running_deferred)
}
void
-evbuffer_invoke_callbacks(struct evbuffer *buffer)
+evbuffer_invoke_callbacks_(struct evbuffer *buffer)
{
- if (TAILQ_EMPTY(&buffer->callbacks)) {
+ if (LIST_EMPTY(&buffer->callbacks)) {
buffer->n_add_for_cb = buffer->n_del_for_cb = 0;
return;
}
if (buffer->deferred_cbs) {
- if (buffer->deferred.queued)
- return;
- _evbuffer_incref_and_lock(buffer);
- if (buffer->parent)
- bufferevent_incref(buffer->parent);
+ if (event_deferred_cb_schedule_(buffer->cb_queue, &buffer->deferred)) {
+ evbuffer_incref_and_lock_(buffer);
+ if (buffer->parent)
+ bufferevent_incref_(buffer->parent);
+ }
EVBUFFER_UNLOCK(buffer);
- event_deferred_cb_schedule(buffer->cb_queue, &buffer->deferred);
}
evbuffer_run_callbacks(buffer, 0);
}
static void
-evbuffer_deferred_callback(struct deferred_cb *cb, void *arg)
+evbuffer_deferred_callback(struct event_callback *cb, void *arg)
{
struct bufferevent *parent = NULL;
struct evbuffer *buffer = arg;
@@ -519,9 +532,9 @@ evbuffer_deferred_callback(struct deferred_cb *cb, void *arg)
EVBUFFER_LOCK(buffer);
parent = buffer->parent;
evbuffer_run_callbacks(buffer, 1);
- _evbuffer_decref_and_unlock(buffer);
+ evbuffer_decref_and_unlock_(buffer);
if (parent)
- bufferevent_decref(parent);
+ bufferevent_decref_(parent);
}
static void
@@ -529,14 +542,14 @@ evbuffer_remove_all_callbacks(struct evbuffer *buffer)
{
struct evbuffer_cb_entry *cbent;
- while ((cbent = TAILQ_FIRST(&buffer->callbacks))) {
- TAILQ_REMOVE(&buffer->callbacks, cbent, next);
- mm_free(cbent);
+ while ((cbent = LIST_FIRST(&buffer->callbacks))) {
+ LIST_REMOVE(cbent, next);
+ mm_free(cbent);
}
}
void
-_evbuffer_decref_and_unlock(struct evbuffer *buffer)
+evbuffer_decref_and_unlock_(struct evbuffer *buffer)
{
struct evbuffer_chain *chain, *next;
ASSERT_EVBUFFER_LOCKED(buffer);
@@ -554,7 +567,7 @@ _evbuffer_decref_and_unlock(struct evbuffer *buffer)
}
evbuffer_remove_all_callbacks(buffer);
if (buffer->deferred_cbs)
- event_deferred_cb_cancel(buffer->cb_queue, &buffer->deferred);
+ event_deferred_cb_cancel_(buffer->cb_queue, &buffer->deferred);
EVBUFFER_UNLOCK(buffer);
if (buffer->own_lock)
@@ -566,7 +579,7 @@ void
evbuffer_free(struct evbuffer *buffer)
{
EVBUFFER_LOCK(buffer);
- _evbuffer_decref_and_unlock(buffer);
+ evbuffer_decref_and_unlock_(buffer);
}
void
@@ -609,6 +622,42 @@ evbuffer_get_contiguous_space(const struct evbuffer *buf)
return result;
}
+size_t
+evbuffer_add_iovec(struct evbuffer * buf, struct evbuffer_iovec * vec, int n_vec) {
+ int n;
+ size_t res;
+ size_t to_alloc;
+
+ EVBUFFER_LOCK(buf);
+
+ res = to_alloc = 0;
+
+ for (n = 0; n < n_vec; n++) {
+ to_alloc += vec[n].iov_len;
+ }
+
+ if (evbuffer_expand_fast_(buf, to_alloc, 2) < 0) {
+ goto done;
+ }
+
+ for (n = 0; n < n_vec; n++) {
+ /* XXX each 'add' call here does a bunch of setup that's
+ * obviated by evbuffer_expand_fast_, and some cleanup that we
+ * would like to do only once. Instead we should just extract
+ * the part of the code that's needed. */
+
+ if (evbuffer_add(buf, vec[n].iov_base, vec[n].iov_len) < 0) {
+ goto done;
+ }
+
+ res += vec[n].iov_len;
+ }
+
+done:
+ EVBUFFER_UNLOCK(buf);
+ return res;
+}
+
int
evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
struct evbuffer_iovec *vec, int n_vecs)
@@ -630,9 +679,9 @@ evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
EVUTIL_ASSERT(size<0 || (size_t)vec[0].iov_len >= (size_t)size);
n = 1;
} else {
- if (_evbuffer_expand_fast(buf, size, n_vecs)<0)
+ if (evbuffer_expand_fast_(buf, size, n_vecs)<0)
goto done;
- n = _evbuffer_read_setup_vecs(buf, size, vec, n_vecs,
+ n = evbuffer_read_setup_vecs_(buf, size, vec, n_vecs,
&chainp, 0);
}
@@ -721,7 +770,7 @@ okay:
buf->total_len += added;
buf->n_add_for_cb += added;
result = 0;
- evbuffer_invoke_callbacks(buf);
+ evbuffer_invoke_callbacks_(buf);
done:
EVBUFFER_UNLOCK(buf);
@@ -837,6 +886,46 @@ APPEND_CHAIN(struct evbuffer *dst, struct evbuffer *src)
dst->total_len += src->total_len;
}
+static inline void
+APPEND_CHAIN_MULTICAST(struct evbuffer *dst, struct evbuffer *src)
+{
+ struct evbuffer_chain *tmp;
+ struct evbuffer_chain *chain = src->first;
+ struct evbuffer_multicast_parent *extra;
+
+ ASSERT_EVBUFFER_LOCKED(dst);
+ ASSERT_EVBUFFER_LOCKED(src);
+
+ for (; chain; chain = chain->next) {
+ if (!chain->off || chain->flags & EVBUFFER_DANGLING) {
+ /* skip empty chains */
+ continue;
+ }
+
+ tmp = evbuffer_chain_new(sizeof(struct evbuffer_multicast_parent));
+ if (!tmp) {
+ event_warn("%s: out of memory", __func__);
+ return;
+ }
+ extra = EVBUFFER_CHAIN_EXTRA(struct evbuffer_multicast_parent, tmp);
+ /* reference evbuffer containing source chain so it
+ * doesn't get released while the chain is still
+ * being referenced to */
+ evbuffer_incref_(src);
+ extra->source = src;
+ /* reference source chain which now becomes immutable */
+ evbuffer_chain_incref(chain);
+ extra->parent = chain;
+ chain->flags |= EVBUFFER_IMMUTABLE;
+ tmp->buffer_len = chain->buffer_len;
+ tmp->misalign = chain->misalign;
+ tmp->off = chain->off;
+ tmp->flags |= EVBUFFER_MULTICAST|EVBUFFER_IMMUTABLE;
+ tmp->buffer = chain->buffer;
+ evbuffer_chain_insert(dst, tmp);
+ }
+}
+
static void
PREPEND_CHAIN(struct evbuffer *dst, struct evbuffer *src)
{
@@ -893,8 +982,51 @@ evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf)
inbuf->n_del_for_cb += in_total_len;
outbuf->n_add_for_cb += in_total_len;
- evbuffer_invoke_callbacks(inbuf);
- evbuffer_invoke_callbacks(outbuf);
+ evbuffer_invoke_callbacks_(inbuf);
+ evbuffer_invoke_callbacks_(outbuf);
+
+done:
+ EVBUFFER_UNLOCK2(inbuf, outbuf);
+ return result;
+}
+
+int
+evbuffer_add_buffer_reference(struct evbuffer *outbuf, struct evbuffer *inbuf)
+{
+ size_t in_total_len, out_total_len;
+ struct evbuffer_chain *chain;
+ int result = 0;
+
+ EVBUFFER_LOCK2(inbuf, outbuf);
+ in_total_len = inbuf->total_len;
+ out_total_len = outbuf->total_len;
+ chain = inbuf->first;
+
+ if (in_total_len == 0)
+ goto done;
+
+ if (outbuf->freeze_end || outbuf == inbuf) {
+ result = -1;
+ goto done;
+ }
+
+ for (; chain; chain = chain->next) {
+ if ((chain->flags & (EVBUFFER_FILESEGMENT|EVBUFFER_SENDFILE|EVBUFFER_MULTICAST)) != 0) {
+ /* chain type can not be referenced */
+ result = -1;
+ goto done;
+ }
+ }
+
+ if (out_total_len == 0) {
+ /* There might be an empty chain at the start of outbuf; free
+ * it. */
+ evbuffer_free_all_chains(outbuf->first);
+ }
+ APPEND_CHAIN_MULTICAST(outbuf, inbuf);
+
+ outbuf->n_add_for_cb += in_total_len;
+ evbuffer_invoke_callbacks_(outbuf);
done:
EVBUFFER_UNLOCK2(inbuf, outbuf);
@@ -940,8 +1072,8 @@ evbuffer_prepend_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf)
inbuf->n_del_for_cb += in_total_len;
outbuf->n_add_for_cb += in_total_len;
- evbuffer_invoke_callbacks(inbuf);
- evbuffer_invoke_callbacks(outbuf);
+ evbuffer_invoke_callbacks_(inbuf);
+ evbuffer_invoke_callbacks_(outbuf);
done:
EVBUFFER_UNLOCK2(inbuf, outbuf);
return result;
@@ -1001,15 +1133,13 @@ evbuffer_drain(struct evbuffer *buf, size_t len)
}
buf->first = chain;
- if (chain) {
- chain->misalign += remaining;
- chain->off -= remaining;
- }
+ chain->misalign += remaining;
+ chain->off -= remaining;
}
buf->n_del_for_cb += len;
/* Tell someone about changes in this buffer */
- evbuffer_invoke_callbacks(buf);
+ evbuffer_invoke_callbacks_(buf);
done:
EVBUFFER_UNLOCK(buf);
@@ -1022,7 +1152,7 @@ evbuffer_remove(struct evbuffer *buf, void *data_out, size_t datlen)
{
ev_ssize_t n;
EVBUFFER_LOCK(buf);
- n = evbuffer_copyout(buf, data_out, datlen);
+ n = evbuffer_copyout_from(buf, NULL, data_out, datlen);
if (n > 0) {
if (evbuffer_drain(buf, n)<0)
n = -1;
@@ -1034,18 +1164,34 @@ evbuffer_remove(struct evbuffer *buf, void *data_out, size_t datlen)
ev_ssize_t
evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen)
{
+ return evbuffer_copyout_from(buf, NULL, data_out, datlen);
+}
+
+ev_ssize_t
+evbuffer_copyout_from(struct evbuffer *buf, const struct evbuffer_ptr *pos,
+ void *data_out, size_t datlen)
+{
/*XXX fails badly on sendfile case. */
struct evbuffer_chain *chain;
char *data = data_out;
size_t nread;
ev_ssize_t result = 0;
+ size_t pos_in_chain;
EVBUFFER_LOCK(buf);
- chain = buf->first;
+ if (pos) {
+ chain = pos->internal_.chain;
+ pos_in_chain = pos->internal_.pos_in_chain;
+ if (datlen + pos->pos > buf->total_len)
+ datlen = buf->total_len - pos->pos;
+ } else {
+ chain = buf->first;
+ pos_in_chain = 0;
+ if (datlen > buf->total_len)
+ datlen = buf->total_len;
+ }
- if (datlen >= buf->total_len)
- datlen = buf->total_len;
if (datlen == 0)
goto done;
@@ -1057,18 +1203,23 @@ evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen)
nread = datlen;
- while (datlen && datlen >= chain->off) {
- memcpy(data, chain->buffer + chain->misalign, chain->off);
- data += chain->off;
- datlen -= chain->off;
+ while (datlen && datlen >= chain->off - pos_in_chain) {
+ size_t copylen = chain->off - pos_in_chain;
+ memcpy(data,
+ chain->buffer + chain->misalign + pos_in_chain,
+ copylen);
+ data += copylen;
+ datlen -= copylen;
chain = chain->next;
+ pos_in_chain = 0;
EVUTIL_ASSERT(chain || datlen==0);
}
if (datlen) {
EVUTIL_ASSERT(chain);
- memcpy(data, chain->buffer + chain->misalign, datlen);
+ memcpy(data, chain->buffer + chain->misalign + pos_in_chain,
+ datlen);
}
result = nread;
@@ -1160,8 +1311,8 @@ evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst,
src->n_del_for_cb += nread;
if (nread) {
- evbuffer_invoke_callbacks(dst);
- evbuffer_invoke_callbacks(src);
+ evbuffer_invoke_callbacks_(dst);
+ evbuffer_invoke_callbacks_(src);
}
result = (int)nread;/*XXXX should change return type */
@@ -1295,14 +1446,14 @@ evbuffer_readline(struct evbuffer *buffer)
static inline ev_ssize_t
evbuffer_strchr(struct evbuffer_ptr *it, const char chr)
{
- struct evbuffer_chain *chain = it->_internal.chain;
- size_t i = it->_internal.pos_in_chain;
+ struct evbuffer_chain *chain = it->internal_.chain;
+ size_t i = it->internal_.pos_in_chain;
while (chain != NULL) {
char *buffer = (char *)chain->buffer + chain->misalign;
char *cp = memchr(buffer+i, chr, chain->off-i);
if (cp) {
- it->_internal.chain = chain;
- it->_internal.pos_in_chain = cp - buffer;
+ it->internal_.chain = chain;
+ it->internal_.pos_in_chain = cp - buffer;
it->pos += (cp - buffer - i);
return it->pos;
}
@@ -1344,14 +1495,14 @@ find_eol_char(char *s, size_t len)
static ev_ssize_t
evbuffer_find_eol_char(struct evbuffer_ptr *it)
{
- struct evbuffer_chain *chain = it->_internal.chain;
- size_t i = it->_internal.pos_in_chain;
+ struct evbuffer_chain *chain = it->internal_.chain;
+ size_t i = it->internal_.pos_in_chain;
while (chain != NULL) {
char *buffer = (char *)chain->buffer + chain->misalign;
char *cp = find_eol_char(buffer+i, chain->off-i);
if (cp) {
- it->_internal.chain = chain;
- it->_internal.pos_in_chain = cp - buffer;
+ it->internal_.chain = chain;
+ it->internal_.pos_in_chain = cp - buffer;
it->pos += (cp - buffer) - i;
return it->pos;
}
@@ -1368,11 +1519,11 @@ evbuffer_strspn(
struct evbuffer_ptr *ptr, const char *chrset)
{
int count = 0;
- struct evbuffer_chain *chain = ptr->_internal.chain;
- size_t i = ptr->_internal.pos_in_chain;
+ struct evbuffer_chain *chain = ptr->internal_.chain;
+ size_t i = ptr->internal_.pos_in_chain;
if (!chain)
- return -1;
+ return 0;
while (1) {
char *buffer = (char *)chain->buffer + chain->misalign;
@@ -1382,8 +1533,8 @@ evbuffer_strspn(
if (buffer[i] == *p++)
goto next;
}
- ptr->_internal.chain = chain;
- ptr->_internal.pos_in_chain = i;
+ ptr->internal_.chain = chain;
+ ptr->internal_.pos_in_chain = i;
ptr->pos += count;
return count;
next:
@@ -1392,8 +1543,8 @@ evbuffer_strspn(
i = 0;
if (! chain->next) {
- ptr->_internal.chain = chain;
- ptr->_internal.pos_in_chain = i;
+ ptr->internal_.chain = chain;
+ ptr->internal_.pos_in_chain = i;
ptr->pos += count;
return count;
}
@@ -1403,13 +1554,16 @@ evbuffer_strspn(
}
-static inline char
+static inline int
evbuffer_getchr(struct evbuffer_ptr *it)
{
- struct evbuffer_chain *chain = it->_internal.chain;
- size_t off = it->_internal.pos_in_chain;
+ struct evbuffer_chain *chain = it->internal_.chain;
+ size_t off = it->internal_.pos_in_chain;
- return chain->buffer[chain->misalign + off];
+ if (chain == NULL)
+ return -1;
+
+ return (unsigned char)chain->buffer[chain->misalign + off];
}
struct evbuffer_ptr
@@ -1421,14 +1575,22 @@ evbuffer_search_eol(struct evbuffer *buffer,
size_t extra_drain = 0;
int ok = 0;
+ /* Avoid locking in trivial edge cases */
+ if (start && start->internal_.chain == NULL) {
+ PTR_NOT_FOUND(&it);
+ if (eol_len_out)
+ *eol_len_out = extra_drain;
+ return it;
+ }
+
EVBUFFER_LOCK(buffer);
if (start) {
memcpy(&it, start, sizeof(it));
} else {
it.pos = 0;
- it._internal.chain = buffer->first;
- it._internal.pos_in_chain = 0;
+ it.internal_.chain = buffer->first;
+ it.internal_.pos_in_chain = 0;
}
/* the eol_style determines our first stop character and how many
@@ -1447,29 +1609,37 @@ evbuffer_search_eol(struct evbuffer *buffer,
extra_drain = 2;
break;
}
- case EVBUFFER_EOL_CRLF:
- while (1) {
- if (evbuffer_find_eol_char(&it) < 0)
- goto done;
- if (evbuffer_getchr(&it) == '\n') {
- extra_drain = 1;
- break;
- } else if (!evbuffer_ptr_memcmp(
- buffer, &it, "\r\n", 2)) {
- extra_drain = 2;
- break;
- } else {
- if (evbuffer_ptr_set(buffer, &it, 1,
- EVBUFFER_PTR_ADD)<0)
- goto done;
- }
+ case EVBUFFER_EOL_CRLF: {
+ ev_ssize_t start_pos = it.pos;
+ /* Look for a LF ... */
+ if (evbuffer_strchr(&it, '\n') < 0)
+ goto done;
+ extra_drain = 1;
+ /* ... optionally preceeded by a CR. */
+ if (it.pos == start_pos)
+ break; /* If the first character is \n, don't back up */
+ /* This potentially does an extra linear walk over the first
+ * few chains. Probably, that's not too expensive unless you
+ * have a really pathological setup. */
+ memcpy(&it2, &it, sizeof(it));
+ if (evbuffer_ptr_subtract(buffer, &it2, 1)<0)
+ break;
+ if (evbuffer_getchr(&it2) == '\r') {
+ memcpy(&it, &it2, sizeof(it));
+ extra_drain = 2;
}
break;
+ }
case EVBUFFER_EOL_LF:
if (evbuffer_strchr(&it, '\n') < 0)
goto done;
extra_drain = 1;
break;
+ case EVBUFFER_EOL_NUL:
+ if (evbuffer_strchr(&it, '\0') < 0)
+ goto done;
+ extra_drain = 1;
+ break;
default:
goto done;
}
@@ -1478,9 +1648,8 @@ evbuffer_search_eol(struct evbuffer *buffer,
done:
EVBUFFER_UNLOCK(buffer);
- if (!ok) {
- it.pos = -1;
- }
+ if (!ok)
+ PTR_NOT_FOUND(&it);
if (eol_len_out)
*eol_len_out = extra_drain;
@@ -1609,7 +1778,7 @@ evbuffer_add(struct evbuffer *buf, const void *data_in, size_t datlen)
buf->n_add_for_cb += datlen;
out:
- evbuffer_invoke_callbacks(buf);
+ evbuffer_invoke_callbacks_(buf);
result = 0;
done:
EVBUFFER_UNLOCK(buf);
@@ -1683,7 +1852,7 @@ evbuffer_prepend(struct evbuffer *buf, const void *data, size_t datlen)
buf->n_add_for_cb += (size_t)chain->misalign;
out:
- evbuffer_invoke_callbacks(buf);
+ evbuffer_invoke_callbacks_(buf);
result = 0;
done:
EVBUFFER_UNLOCK(buf);
@@ -1828,7 +1997,7 @@ err:
/* Make sure that datlen bytes are available for writing in the last n
* chains. Never copies or moves data. */
int
-_evbuffer_expand_fast(struct evbuffer *buf, size_t datlen, int n)
+evbuffer_expand_fast_(struct evbuffer *buf, size_t datlen, int n)
{
struct evbuffer_chain *chain = buf->last, *tmp, *next;
size_t avail;
@@ -1949,13 +2118,13 @@ evbuffer_expand(struct evbuffer *buf, size_t datlen)
* Reads data from a file descriptor into a buffer.
*/
-#if defined(_EVENT_HAVE_SYS_UIO_H) || defined(WIN32)
+#if defined(EVENT__HAVE_SYS_UIO_H) || defined(_WIN32)
#define USE_IOVEC_IMPL
#endif
#ifdef USE_IOVEC_IMPL
-#ifdef _EVENT_HAVE_SYS_UIO_H
+#ifdef EVENT__HAVE_SYS_UIO_H
/* number of iovec we use for writev, fragmentation is going to determine
* how much we end up writing */
@@ -1999,7 +2168,7 @@ evbuffer_expand(struct evbuffer *buf, size_t datlen)
@return The number of buffers we're using.
*/
int
-_evbuffer_read_setup_vecs(struct evbuffer *buf, ev_ssize_t howmuch,
+evbuffer_read_setup_vecs_(struct evbuffer *buf, ev_ssize_t howmuch,
struct evbuffer_iovec *vecs, int n_vecs_avail,
struct evbuffer_chain ***chainp, int exact)
{
@@ -2037,7 +2206,7 @@ _evbuffer_read_setup_vecs(struct evbuffer *buf, ev_ssize_t howmuch,
static int
get_n_bytes_readable_on_socket(evutil_socket_t fd)
{
-#if defined(FIONREAD) && defined(WIN32)
+#if defined(FIONREAD) && defined(_WIN32)
unsigned long lng = EVBUFFER_MAX_READ;
if (ioctlsocket(fd, FIONREAD, &lng) < 0)
return -1;
@@ -2084,26 +2253,26 @@ evbuffer_read(struct evbuffer *buf, evutil_socket_t fd, int howmuch)
#ifdef USE_IOVEC_IMPL
/* Since we can use iovecs, we're willing to use the last
* NUM_READ_IOVEC chains. */
- if (_evbuffer_expand_fast(buf, howmuch, NUM_READ_IOVEC) == -1) {
+ if (evbuffer_expand_fast_(buf, howmuch, NUM_READ_IOVEC) == -1) {
result = -1;
goto done;
} else {
IOV_TYPE vecs[NUM_READ_IOVEC];
-#ifdef _EVBUFFER_IOVEC_IS_NATIVE
- nvecs = _evbuffer_read_setup_vecs(buf, howmuch, vecs,
+#ifdef EVBUFFER_IOVEC_IS_NATIVE_
+ nvecs = evbuffer_read_setup_vecs_(buf, howmuch, vecs,
NUM_READ_IOVEC, &chainp, 1);
#else
/* We aren't using the native struct iovec. Therefore,
we are on win32. */
struct evbuffer_iovec ev_vecs[NUM_READ_IOVEC];
- nvecs = _evbuffer_read_setup_vecs(buf, howmuch, ev_vecs, 2,
+ nvecs = evbuffer_read_setup_vecs_(buf, howmuch, ev_vecs, 2,
&chainp, 1);
for (i=0; i < nvecs; ++i)
WSABUF_FROM_EVBUFFER_IOV(&vecs[i], &ev_vecs[i]);
#endif
-#ifdef WIN32
+#ifdef _WIN32
{
DWORD bytesRead;
DWORD flags=0;
@@ -2134,7 +2303,7 @@ evbuffer_read(struct evbuffer *buf, evutil_socket_t fd, int howmuch)
/* We can append new data at this point */
p = chain->buffer + chain->misalign + chain->off;
-#ifndef WIN32
+#ifndef _WIN32
n = read(fd, p, howmuch);
#else
n = recv(fd, p, howmuch, 0);
@@ -2172,62 +2341,12 @@ evbuffer_read(struct evbuffer *buf, evutil_socket_t fd, int howmuch)
buf->n_add_for_cb += n;
/* Tell someone about changes in this buffer */
- evbuffer_invoke_callbacks(buf);
- result = n;
-done:
- EVBUFFER_UNLOCK(buf);
- return result;
-}
-
-#ifdef WIN32
-static int
-evbuffer_readfile(struct evbuffer *buf, evutil_socket_t fd, ev_ssize_t howmuch)
-{
- int result;
- int nchains, n;
- struct evbuffer_iovec v[2];
-
- EVBUFFER_LOCK(buf);
-
- if (buf->freeze_end) {
- result = -1;
- goto done;
- }
-
- if (howmuch < 0)
- howmuch = 16384;
-
-
- /* XXX we _will_ waste some space here if there is any space left
- * over on buf->last. */
- nchains = evbuffer_reserve_space(buf, howmuch, v, 2);
- if (nchains < 1 || nchains > 2) {
- result = -1;
- goto done;
- }
- n = read((int)fd, v[0].iov_base, (unsigned int)v[0].iov_len);
- if (n <= 0) {
- result = n;
- goto done;
- }
- v[0].iov_len = (IOV_LEN_TYPE) n; /* XXXX another problem with big n.*/
- if (nchains > 1) {
- n = read((int)fd, v[1].iov_base, (unsigned int)v[1].iov_len);
- if (n <= 0) {
- result = (unsigned long) v[0].iov_len;
- evbuffer_commit_space(buf, v, 1);
- goto done;
- }
- v[1].iov_len = n;
- }
- evbuffer_commit_space(buf, v, nchains);
-
+ evbuffer_invoke_callbacks_(buf);
result = n;
done:
EVBUFFER_UNLOCK(buf);
return result;
}
-#endif
#ifdef USE_IOVEC_IMPL
static inline int
@@ -2265,7 +2384,8 @@ evbuffer_write_iovec(struct evbuffer *buffer, evutil_socket_t fd,
}
if (! i)
return 0;
-#ifdef WIN32
+
+#ifdef _WIN32
{
DWORD bytesSent;
if (WSASend(fd, iov, i, &bytesSent, 0, NULL, NULL))
@@ -2282,37 +2402,39 @@ evbuffer_write_iovec(struct evbuffer *buffer, evutil_socket_t fd,
#ifdef USE_SENDFILE
static inline int
-evbuffer_write_sendfile(struct evbuffer *buffer, evutil_socket_t fd,
+evbuffer_write_sendfile(struct evbuffer *buffer, evutil_socket_t dest_fd,
ev_ssize_t howmuch)
{
struct evbuffer_chain *chain = buffer->first;
- struct evbuffer_chain_fd *info =
- EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_fd, chain);
+ struct evbuffer_chain_file_segment *info =
+ EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_file_segment,
+ chain);
+ const int source_fd = info->segment->fd;
#if defined(SENDFILE_IS_MACOSX) || defined(SENDFILE_IS_FREEBSD)
int res;
- off_t len = chain->off;
+ ev_off_t len = chain->off;
#elif defined(SENDFILE_IS_LINUX) || defined(SENDFILE_IS_SOLARIS)
ev_ssize_t res;
- off_t offset = chain->misalign;
+ ev_off_t offset = chain->misalign;
#endif
ASSERT_EVBUFFER_LOCKED(buffer);
#if defined(SENDFILE_IS_MACOSX)
- res = sendfile(info->fd, fd, chain->misalign, &len, NULL, 0);
+ res = sendfile(source_fd, dest_fd, chain->misalign, &len, NULL, 0);
if (res == -1 && !EVUTIL_ERR_RW_RETRIABLE(errno))
return (-1);
return (len);
#elif defined(SENDFILE_IS_FREEBSD)
- res = sendfile(info->fd, fd, chain->misalign, chain->off, NULL, &len, 0);
+ res = sendfile(source_fd, dest_fd, chain->misalign, chain->off, NULL, &len, 0);
if (res == -1 && !EVUTIL_ERR_RW_RETRIABLE(errno))
return (-1);
return (len);
#elif defined(SENDFILE_IS_LINUX)
/* TODO(niels): implement splice */
- res = sendfile(fd, info->fd, &offset, chain->off);
+ res = sendfile(dest_fd, source_fd, &offset, chain->off);
if (res == -1 && EVUTIL_ERR_RW_RETRIABLE(errno)) {
/* if this is EAGAIN or EINTR return 0; otherwise, -1 */
return (0);
@@ -2321,7 +2443,7 @@ evbuffer_write_sendfile(struct evbuffer *buffer, evutil_socket_t fd,
#elif defined(SENDFILE_IS_SOLARIS)
{
const off_t offset_orig = offset;
- res = sendfile(fd, info->fd, &offset, chain->off);
+ res = sendfile(dest_fd, source_fd, &offset, chain->off);
if (res == -1 && EVUTIL_ERR_RW_RETRIABLE(errno)) {
if (offset - offset_orig)
return offset - offset_orig;
@@ -2359,7 +2481,7 @@ evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd,
#endif
#ifdef USE_IOVEC_IMPL
n = evbuffer_write_iovec(buffer, fd, howmuch);
-#elif defined(WIN32)
+#elif defined(_WIN32)
/* XXX(nickm) Don't disable this code until we know if
* the WSARecv code above works. */
void *p = evbuffer_pullup(buffer, howmuch);
@@ -2409,12 +2531,36 @@ evbuffer_find(struct evbuffer *buffer, const unsigned char *what, size_t len)
return search;
}
+/* Subract <b>howfar</b> from the position of <b>pos</b> within
+ * <b>buf</b>. Returns 0 on success, -1 on failure.
+ *
+ * This isn't exposed yet, because of potential inefficiency issues.
+ * Maybe it should be. */
+static int
+evbuffer_ptr_subtract(struct evbuffer *buf, struct evbuffer_ptr *pos,
+ size_t howfar)
+{
+ if (howfar > (size_t)pos->pos)
+ return -1;
+ if (pos->internal_.chain && howfar <= pos->internal_.pos_in_chain) {
+ pos->internal_.pos_in_chain -= howfar;
+ pos->pos -= howfar;
+ return 0;
+ } else {
+ const size_t newpos = pos->pos - howfar;
+ /* Here's the inefficient part: it walks over the
+ * chains until we hit newpos. */
+ return evbuffer_ptr_set(buf, pos, newpos, EVBUFFER_PTR_SET);
+ }
+}
+
int
evbuffer_ptr_set(struct evbuffer *buf, struct evbuffer_ptr *pos,
size_t position, enum evbuffer_ptr_how how)
{
size_t left = position;
struct evbuffer_chain *chain = NULL;
+ int result = 0;
EVBUFFER_LOCK(buf);
@@ -2427,9 +2573,9 @@ evbuffer_ptr_set(struct evbuffer *buf, struct evbuffer_ptr *pos,
case EVBUFFER_PTR_ADD:
/* this avoids iterating over all previous chains if
we just want to advance the position */
- chain = pos->_internal.chain;
+ chain = pos->internal_.chain;
pos->pos += position;
- position = pos->_internal.pos_in_chain;
+ position = pos->internal_.pos_in_chain;
break;
}
@@ -2439,16 +2585,20 @@ evbuffer_ptr_set(struct evbuffer *buf, struct evbuffer_ptr *pos,
position = 0;
}
if (chain) {
- pos->_internal.chain = chain;
- pos->_internal.pos_in_chain = position + left;
+ pos->internal_.chain = chain;
+ pos->internal_.pos_in_chain = position + left;
+ } else if (left == 0) {
+ /* The first byte in the (nonexistent) chain after the last chain */
+ pos->internal_.chain = NULL;
+ pos->internal_.pos_in_chain = 0;
} else {
- pos->_internal.chain = NULL;
- pos->pos = -1;
+ PTR_NOT_FOUND(pos);
+ result = -1;
}
EVBUFFER_UNLOCK(buf);
- return chain != NULL ? 0 : -1;
+ return result;
}
/**
@@ -2468,8 +2618,8 @@ evbuffer_ptr_memcmp(const struct evbuffer *buf, const struct evbuffer_ptr *pos,
if (pos->pos + len > buf->total_len)
return -1;
- chain = pos->_internal.chain;
- position = pos->_internal.pos_in_chain;
+ chain = pos->internal_.chain;
+ position = pos->internal_.pos_in_chain;
while (len && chain) {
size_t n_comparable;
if (len + position > chain->off)
@@ -2507,15 +2657,15 @@ evbuffer_search_range(struct evbuffer *buffer, const char *what, size_t len, con
if (start) {
memcpy(&pos, start, sizeof(pos));
- chain = pos._internal.chain;
+ chain = pos.internal_.chain;
} else {
pos.pos = 0;
- chain = pos._internal.chain = buffer->first;
- pos._internal.pos_in_chain = 0;
+ chain = pos.internal_.chain = buffer->first;
+ pos.internal_.pos_in_chain = 0;
}
if (end)
- last_chain = end->_internal.chain;
+ last_chain = end->internal_.chain;
if (!len || len > EV_SSIZE_MAX)
goto done;
@@ -2525,12 +2675,12 @@ evbuffer_search_range(struct evbuffer *buffer, const char *what, size_t len, con
while (chain) {
const unsigned char *start_at =
chain->buffer + chain->misalign +
- pos._internal.pos_in_chain;
+ pos.internal_.pos_in_chain;
p = memchr(start_at, first,
- chain->off - pos._internal.pos_in_chain);
+ chain->off - pos.internal_.pos_in_chain);
if (p) {
pos.pos += p - start_at;
- pos._internal.pos_in_chain += p - start_at;
+ pos.internal_.pos_in_chain += p - start_at;
if (!evbuffer_ptr_memcmp(buffer, &pos, what, len)) {
if (end && pos.pos + (ev_ssize_t)len > end->pos)
goto not_found;
@@ -2538,23 +2688,22 @@ evbuffer_search_range(struct evbuffer *buffer, const char *what, size_t len, con
goto done;
}
++pos.pos;
- ++pos._internal.pos_in_chain;
- if (pos._internal.pos_in_chain == chain->off) {
- chain = pos._internal.chain = chain->next;
- pos._internal.pos_in_chain = 0;
+ ++pos.internal_.pos_in_chain;
+ if (pos.internal_.pos_in_chain == chain->off) {
+ chain = pos.internal_.chain = chain->next;
+ pos.internal_.pos_in_chain = 0;
}
} else {
if (chain == last_chain)
goto not_found;
- pos.pos += chain->off - pos._internal.pos_in_chain;
- chain = pos._internal.chain = chain->next;
- pos._internal.pos_in_chain = 0;
+ pos.pos += chain->off - pos.internal_.pos_in_chain;
+ chain = pos.internal_.chain = chain->next;
+ pos.internal_.pos_in_chain = 0;
}
}
not_found:
- pos.pos = -1;
- pos._internal.chain = NULL;
+ PTR_NOT_FOUND(&pos);
done:
EVBUFFER_UNLOCK(buffer);
return pos;
@@ -2569,16 +2718,20 @@ evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
int idx = 0;
ev_ssize_t len_so_far = 0;
+ /* Avoid locking in trivial edge cases */
+ if (start_at && start_at->internal_.chain == NULL)
+ return 0;
+
EVBUFFER_LOCK(buffer);
if (start_at) {
- chain = start_at->_internal.chain;
+ chain = start_at->internal_.chain;
len_so_far = chain->off
- - start_at->_internal.pos_in_chain;
+ - start_at->internal_.pos_in_chain;
idx = 1;
if (n_vec > 0) {
vec[0].iov_base = chain->buffer + chain->misalign
- + start_at->_internal.pos_in_chain;
+ + start_at->internal_.pos_in_chain;
vec[0].iov_len = len_so_far;
}
chain = chain->next;
@@ -2662,7 +2815,7 @@ evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
buf->n_add_for_cb += sz;
advance_last_with_data(buf);
- evbuffer_invoke_callbacks(buf);
+ evbuffer_invoke_callbacks_(buf);
result = sz;
goto done;
}
@@ -2720,7 +2873,7 @@ evbuffer_add_reference(struct evbuffer *outbuf,
evbuffer_chain_insert(outbuf, chain);
outbuf->n_add_for_cb += datlen;
- evbuffer_invoke_callbacks(outbuf);
+ evbuffer_invoke_callbacks_(outbuf);
result = 0;
done:
@@ -2729,169 +2882,356 @@ done:
return result;
}
-/* TODO(niels): maybe we don't want to own the fd, however, in that
- * case, we should dup it - dup is cheap. Perhaps, we should use a
- * callback instead?
- */
/* TODO(niels): we may want to add to automagically convert to mmap, in
* case evbuffer_remove() or evbuffer_pullup() are being used.
*/
-int
-evbuffer_add_file(struct evbuffer *outbuf, int fd,
- ev_off_t offset, ev_off_t length)
+struct evbuffer_file_segment *
+evbuffer_file_segment_new(
+ int fd, ev_off_t offset, ev_off_t length, unsigned flags)
{
-#if defined(USE_SENDFILE) || defined(_EVENT_HAVE_MMAP)
- struct evbuffer_chain *chain;
- struct evbuffer_chain_fd *info;
+ struct evbuffer_file_segment *seg =
+ mm_calloc(sizeof(struct evbuffer_file_segment), 1);
+ if (!seg)
+ return NULL;
+ seg->refcnt = 1;
+ seg->fd = fd;
+ seg->flags = flags;
+ seg->file_offset = offset;
+ seg->cleanup_cb = NULL;
+ seg->cleanup_cb_arg = NULL;
+#ifdef _WIN32
+#ifndef lseek
+#define lseek _lseeki64
+#endif
+#ifndef fstat
+#define fstat _fstat
+#endif
+#ifndef stat
+#define stat _stat
#endif
-#if defined(USE_SENDFILE)
- int sendfile_okay = 1;
#endif
- int ok = 1;
+ if (length == -1) {
+ struct stat st;
+ if (fstat(fd, &st) < 0)
+ goto err;
+ length = st.st_size;
+ }
+ seg->length = length;
#if defined(USE_SENDFILE)
- if (use_sendfile) {
- EVBUFFER_LOCK(outbuf);
- sendfile_okay = outbuf->flags & EVBUFFER_FLAG_DRAINS_TO_FD;
- EVBUFFER_UNLOCK(outbuf);
+ if (!(flags & EVBUF_FS_DISABLE_SENDFILE)) {
+ seg->can_sendfile = 1;
+ goto done;
}
+#endif
- if (use_sendfile && sendfile_okay) {
- chain = evbuffer_chain_new(sizeof(struct evbuffer_chain_fd));
- if (chain == NULL) {
- event_warn("%s: out of memory", __func__);
- return (-1);
- }
+ if (evbuffer_file_segment_materialize(seg)<0)
+ goto err;
- chain->flags |= EVBUFFER_SENDFILE | EVBUFFER_IMMUTABLE;
- chain->buffer = NULL; /* no reading possible */
- chain->buffer_len = length + offset;
- chain->off = length;
- chain->misalign = offset;
+#if defined(USE_SENDFILE)
+done:
+#endif
+ if (!(flags & EVBUF_FS_DISABLE_LOCKING)) {
+ EVTHREAD_ALLOC_LOCK(seg->lock, 0);
+ }
+ return seg;
+err:
+ mm_free(seg);
+ return NULL;
+}
- info = EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_fd, chain);
- info->fd = fd;
+#ifdef EVENT__HAVE_MMAP
+static long
+get_page_size(void)
+{
+#ifdef SC_PAGE_SIZE
+ return sysconf(SC_PAGE_SIZE);
+#elif defined(_SC_PAGE_SIZE)
+ return sysconf(_SC_PAGE_SIZE);
+#else
+ return 1;
+#endif
+}
+#endif
- EVBUFFER_LOCK(outbuf);
- if (outbuf->freeze_end) {
- mm_free(chain);
- ok = 0;
- } else {
- outbuf->n_add_for_cb += length;
- evbuffer_chain_insert(outbuf, chain);
+/* DOCDOC */
+/* Requires lock */
+static int
+evbuffer_file_segment_materialize(struct evbuffer_file_segment *seg)
+{
+ const unsigned flags = seg->flags;
+ const int fd = seg->fd;
+ const ev_off_t length = seg->length;
+ const ev_off_t offset = seg->file_offset;
+
+ if (seg->contents)
+ return 0; /* already materialized */
+
+#if defined(EVENT__HAVE_MMAP)
+ if (!(flags & EVBUF_FS_DISABLE_MMAP)) {
+ off_t offset_rounded = 0, offset_leftover = 0;
+ void *mapped;
+ if (offset) {
+ /* mmap implementations don't generally like us
+ * to have an offset that isn't a round */
+ long page_size = get_page_size();
+ if (page_size == -1)
+ goto err;
+ offset_leftover = offset % page_size;
+ offset_rounded = offset - offset_leftover;
}
- } else
-#endif
-#if defined(_EVENT_HAVE_MMAP)
- if (use_mmap) {
- void *mapped = mmap(NULL, length + offset, PROT_READ,
+ mapped = mmap(NULL, length + offset_leftover,
+ PROT_READ,
#ifdef MAP_NOCACHE
- MAP_NOCACHE |
+ MAP_NOCACHE | /* ??? */
#endif
#ifdef MAP_FILE
MAP_FILE |
#endif
MAP_PRIVATE,
- fd, 0);
- /* some mmap implementations require offset to be a multiple of
- * the page size. most users of this api, are likely to use 0
- * so mapping everything is not likely to be a problem.
- * TODO(niels): determine page size and round offset to that
- * page size to avoid mapping too much memory.
- */
+ fd, offset_rounded);
if (mapped == MAP_FAILED) {
event_warn("%s: mmap(%d, %d, %zu) failed",
__func__, fd, 0, (size_t)(offset + length));
- return (-1);
+ } else {
+ seg->mapping = mapped;
+ seg->contents = (char*)mapped+offset_leftover;
+ seg->mmap_offset = 0;
+ seg->is_mapping = 1;
+ goto done;
}
- chain = evbuffer_chain_new(sizeof(struct evbuffer_chain_fd));
- if (chain == NULL) {
- event_warn("%s: out of memory", __func__);
- munmap(mapped, length);
- return (-1);
+ }
+#endif
+#ifdef _WIN32
+ if (!(flags & EVBUF_FS_DISABLE_MMAP)) {
+ intptr_t h = _get_osfhandle(fd);
+ HANDLE m;
+ ev_uint64_t total_size = length+offset;
+ if ((HANDLE)h == INVALID_HANDLE_VALUE)
+ goto err;
+ m = CreateFileMapping((HANDLE)h, NULL, PAGE_READONLY,
+ (total_size >> 32), total_size & 0xfffffffful,
+ NULL);
+ if (m != INVALID_HANDLE_VALUE) { /* Does h leak? */
+ seg->mapping_handle = m;
+ seg->mmap_offset = offset;
+ seg->is_mapping = 1;
+ goto done;
+ }
+ }
+#endif
+ {
+ ev_off_t start_pos = lseek(fd, 0, SEEK_CUR), pos;
+ ev_off_t read_so_far = 0;
+ char *mem;
+ int e;
+ ev_ssize_t n = 0;
+ if (!(mem = mm_malloc(length)))
+ goto err;
+ if (start_pos < 0) {
+ mm_free(mem);
+ goto err;
+ }
+ if (lseek(fd, offset, SEEK_SET) < 0) {
+ mm_free(mem);
+ goto err;
+ }
+ while (read_so_far < length) {
+ n = read(fd, mem+read_so_far, length-read_so_far);
+ if (n <= 0)
+ break;
+ read_so_far += n;
}
- chain->flags |= EVBUFFER_MMAP | EVBUFFER_IMMUTABLE;
- chain->buffer = mapped;
- chain->buffer_len = length + offset;
- chain->off = length + offset;
+ e = errno;
+ pos = lseek(fd, start_pos, SEEK_SET);
+ if (n < 0 || (n == 0 && length > read_so_far)) {
+ mm_free(mem);
+ errno = e;
+ goto err;
+ } else if (pos < 0) {
+ mm_free(mem);
+ goto err;
+ }
- info = EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_fd, chain);
- info->fd = fd;
+ seg->contents = mem;
+ }
- EVBUFFER_LOCK(outbuf);
- if (outbuf->freeze_end) {
- info->fd = -1;
- evbuffer_chain_free(chain);
- ok = 0;
- } else {
- outbuf->n_add_for_cb += length;
+done:
+ return 0;
+err:
+ return -1;
+}
- evbuffer_chain_insert(outbuf, chain);
+void evbuffer_file_segment_add_cleanup_cb(struct evbuffer_file_segment *seg,
+ evbuffer_file_segment_cleanup_cb cb, void* arg)
+{
+ EVUTIL_ASSERT(seg->refcnt > 0);
+ seg->cleanup_cb = cb;
+ seg->cleanup_cb_arg = arg;
+}
- /* we need to subtract whatever we don't need */
- evbuffer_drain(outbuf, offset);
- }
- } else
+void
+evbuffer_file_segment_free(struct evbuffer_file_segment *seg)
+{
+ int refcnt;
+ EVLOCK_LOCK(seg->lock, 0);
+ refcnt = --seg->refcnt;
+ EVLOCK_UNLOCK(seg->lock, 0);
+ if (refcnt > 0)
+ return;
+ EVUTIL_ASSERT(refcnt == 0);
+
+ if (seg->is_mapping) {
+#ifdef _WIN32
+ CloseHandle(seg->mapping_handle);
+#elif defined (EVENT__HAVE_MMAP)
+ off_t offset_leftover;
+ offset_leftover = seg->file_offset % get_page_size();
+ if (munmap(seg->mapping, seg->length + offset_leftover) == -1)
+ event_warn("%s: munmap failed", __func__);
#endif
- {
- /* the default implementation */
- struct evbuffer *tmp = evbuffer_new();
- ev_ssize_t read;
+ } else if (seg->contents) {
+ mm_free(seg->contents);
+ }
- if (tmp == NULL)
- return (-1);
+ if ((seg->flags & EVBUF_FS_CLOSE_ON_FREE) && seg->fd >= 0) {
+ close(seg->fd);
+ }
+
+ if (seg->cleanup_cb) {
+ (*seg->cleanup_cb)((struct evbuffer_file_segment const*)seg,
+ seg->flags, seg->cleanup_cb_arg);
+ seg->cleanup_cb = NULL;
+ seg->cleanup_cb_arg = NULL;
+ }
-#ifdef WIN32
-#define lseek _lseeki64
-#endif
- if (lseek(fd, offset, SEEK_SET) == -1) {
- evbuffer_free(tmp);
- return (-1);
- }
+ EVTHREAD_FREE_LOCK(seg->lock, 0);
+ mm_free(seg);
+}
- /* we add everything to a temporary buffer, so that we
- * can abort without side effects if the read fails.
- */
- while (length) {
- read = evbuffer_readfile(tmp, fd, (ev_ssize_t)length);
- if (read == -1) {
- evbuffer_free(tmp);
- return (-1);
- }
+int
+evbuffer_add_file_segment(struct evbuffer *buf,
+ struct evbuffer_file_segment *seg, ev_off_t offset, ev_off_t length)
+{
+ struct evbuffer_chain *chain;
+ struct evbuffer_chain_file_segment *extra;
+ int can_use_sendfile = 0;
- length -= read;
+ EVBUFFER_LOCK(buf);
+ EVLOCK_LOCK(seg->lock, 0);
+ if (buf->flags & EVBUFFER_FLAG_DRAINS_TO_FD) {
+ can_use_sendfile = 1;
+ } else {
+ if (!seg->contents) {
+ if (evbuffer_file_segment_materialize(seg)<0) {
+ EVLOCK_UNLOCK(seg->lock, 0);
+ EVBUFFER_UNLOCK(buf);
+ return -1;
+ }
}
+ }
+ ++seg->refcnt;
+ EVLOCK_UNLOCK(seg->lock, 0);
- EVBUFFER_LOCK(outbuf);
- if (outbuf->freeze_end) {
- evbuffer_free(tmp);
- ok = 0;
- } else {
- evbuffer_add_buffer(outbuf, tmp);
- evbuffer_free(tmp);
+ if (buf->freeze_end)
+ goto err;
-#ifdef WIN32
-#define close _close
-#endif
- close(fd);
+ if (length < 0) {
+ if (offset > seg->length)
+ goto err;
+ length = seg->length - offset;
+ }
+
+ /* Can we actually add this? */
+ if (offset+length > seg->length)
+ goto err;
+
+ chain = evbuffer_chain_new(sizeof(struct evbuffer_chain_file_segment));
+ if (!chain)
+ goto err;
+ extra = EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_file_segment, chain);
+
+ chain->flags |= EVBUFFER_IMMUTABLE|EVBUFFER_FILESEGMENT;
+ if (can_use_sendfile && seg->can_sendfile) {
+ chain->flags |= EVBUFFER_SENDFILE;
+ chain->misalign = seg->file_offset + offset;
+ chain->off = length;
+ chain->buffer_len = chain->misalign + length;
+ } else if (seg->is_mapping) {
+#ifdef _WIN32
+ ev_uint64_t total_offset = seg->mmap_offset+offset;
+ ev_uint64_t offset_rounded=0, offset_remaining=0;
+ LPVOID data;
+ if (total_offset) {
+ SYSTEM_INFO si;
+ memset(&si, 0, sizeof(si)); /* cargo cult */
+ GetSystemInfo(&si);
+ offset_remaining = total_offset % si.dwAllocationGranularity;
+ offset_rounded = total_offset - offset_remaining;
}
+ data = MapViewOfFile(
+ seg->mapping_handle,
+ FILE_MAP_READ,
+ offset_rounded >> 32,
+ offset_rounded & 0xfffffffful,
+ length + offset_remaining);
+ if (data == NULL) {
+ mm_free(chain);
+ goto err;
+ }
+ chain->buffer = (unsigned char*) data;
+ chain->buffer_len = length+offset_remaining;
+ chain->misalign = offset_remaining;
+ chain->off = length;
+#else
+ chain->buffer = (unsigned char*)(seg->contents + offset);
+ chain->buffer_len = length;
+ chain->off = length;
+#endif
+ } else {
+ chain->buffer = (unsigned char*)(seg->contents + offset);
+ chain->buffer_len = length;
+ chain->off = length;
}
- if (ok)
- evbuffer_invoke_callbacks(outbuf);
- EVBUFFER_UNLOCK(outbuf);
+ extra->segment = seg;
+ buf->n_add_for_cb += length;
+ evbuffer_chain_insert(buf, chain);
- return ok ? 0 : -1;
+ evbuffer_invoke_callbacks_(buf);
+
+ EVBUFFER_UNLOCK(buf);
+
+ return 0;
+err:
+ EVBUFFER_UNLOCK(buf);
+ evbuffer_file_segment_free(seg); /* Lowers the refcount */
+ return -1;
}
+int
+evbuffer_add_file(struct evbuffer *buf, int fd, ev_off_t offset, ev_off_t length)
+{
+ struct evbuffer_file_segment *seg;
+ unsigned flags = EVBUF_FS_CLOSE_ON_FREE;
+ int r;
+
+ seg = evbuffer_file_segment_new(fd, offset, length, flags);
+ if (!seg)
+ return -1;
+ r = evbuffer_add_file_segment(buf, seg, 0, length);
+ if (r == 0)
+ evbuffer_file_segment_free(seg);
+ return r;
+}
void
evbuffer_setcb(struct evbuffer *buffer, evbuffer_cb cb, void *cbarg)
{
EVBUFFER_LOCK(buffer);
- if (!TAILQ_EMPTY(&buffer->callbacks))
+ if (!LIST_EMPTY(&buffer->callbacks))
evbuffer_remove_all_callbacks(buffer);
if (cb) {
@@ -2913,7 +3253,7 @@ evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg)
e->cb.cb_func = cb;
e->cbarg = cbarg;
e->flags = EVBUFFER_CB_ENABLED;
- TAILQ_INSERT_HEAD(&buffer->callbacks, e, next);
+ LIST_INSERT_HEAD(&buffer->callbacks, e, next);
EVBUFFER_UNLOCK(buffer);
return e;
}
@@ -2923,7 +3263,7 @@ evbuffer_remove_cb_entry(struct evbuffer *buffer,
struct evbuffer_cb_entry *ent)
{
EVBUFFER_LOCK(buffer);
- TAILQ_REMOVE(&buffer->callbacks, ent, next);
+ LIST_REMOVE(ent, next);
EVBUFFER_UNLOCK(buffer);
mm_free(ent);
return 0;
@@ -2935,7 +3275,7 @@ evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg)
struct evbuffer_cb_entry *cbent;
int result = -1;
EVBUFFER_LOCK(buffer);
- TAILQ_FOREACH(cbent, &buffer->callbacks, next) {
+ LIST_FOREACH(cbent, &buffer->callbacks, next) {
if (cb == cbent->cb.cb_func && cbarg == cbent->cbarg) {
result = evbuffer_remove_cb_entry(buffer, cbent);
goto done;
@@ -3020,50 +3360,21 @@ evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb)
}
#endif
-/* These hooks are exposed so that the unit tests can temporarily disable
- * sendfile support in order to test mmap, or both to test linear
- * access. Don't use it; if we need to add a way to disable sendfile support
- * in the future, it will probably be via an alternate version of
- * evbuffer_add_file() with a 'flags' argument.
- */
-int _evbuffer_testing_use_sendfile(void);
-int _evbuffer_testing_use_mmap(void);
-int _evbuffer_testing_use_linear_file_access(void);
-
-int
-_evbuffer_testing_use_sendfile(void)
-{
- int ok = 0;
-#ifdef USE_SENDFILE
- use_sendfile = 1;
- ok = 1;
-#endif
-#ifdef _EVENT_HAVE_MMAP
- use_mmap = 0;
-#endif
- return ok;
-}
int
-_evbuffer_testing_use_mmap(void)
+evbuffer_get_callbacks_(struct evbuffer *buffer, struct event_callback **cbs,
+ int max_cbs)
{
- int ok = 0;
-#ifdef USE_SENDFILE
- use_sendfile = 0;
-#endif
-#ifdef _EVENT_HAVE_MMAP
- use_mmap = 1;
- ok = 1;
-#endif
- return ok;
-}
-int
-_evbuffer_testing_use_linear_file_access(void)
-{
-#ifdef USE_SENDFILE
- use_sendfile = 0;
-#endif
-#ifdef _EVENT_HAVE_MMAP
- use_mmap = 0;
-#endif
- return 1;
+ int r = 0;
+ EVBUFFER_LOCK(buffer);
+ if (buffer->deferred_cbs) {
+ if (max_cbs < 1) {
+ r = -1;
+ goto done;
+ }
+ cbs[0] = &buffer->deferred;
+ r = 1;
+ }
+done:
+ EVBUFFER_UNLOCK(buffer);
+ return r;
}
diff --git a/buffer_iocp.c b/buffer_iocp.c
index 5d5303eb..2d76a90e 100644
--- a/buffer_iocp.c
+++ b/buffer_iocp.c
@@ -30,12 +30,13 @@
This module implements overlapped read and write functions for evbuffer
objects on Windows.
*/
+#include "event2/event-config.h"
+#include "evconfig-private.h"
#include "event2/buffer.h"
#include "event2/buffer_compat.h"
#include "event2/util.h"
#include "event2/thread.h"
-#include "event2/event-config.h"
#include "util-internal.h"
#include "evthread-internal.h"
#include "evbuffer-internal.h"
@@ -87,13 +88,13 @@ pin_release(struct evbuffer_overlapped *eo, unsigned flag)
for (i = 0; i < eo->n_buffers; ++i) {
EVUTIL_ASSERT(chain);
next = chain->next;
- _evbuffer_chain_unpin(chain, flag);
+ evbuffer_chain_unpin_(chain, flag);
chain = next;
}
}
void
-evbuffer_commit_read(struct evbuffer *evbuf, ev_ssize_t nBytes)
+evbuffer_commit_read_(struct evbuffer *evbuf, ev_ssize_t nBytes)
{
struct evbuffer_overlapped *buf = upcast_evbuffer(evbuf);
struct evbuffer_chain **chainp;
@@ -128,13 +129,13 @@ evbuffer_commit_read(struct evbuffer *evbuf, ev_ssize_t nBytes)
evbuf->total_len += nBytes;
evbuf->n_add_for_cb += nBytes;
- evbuffer_invoke_callbacks(evbuf);
+ evbuffer_invoke_callbacks_(evbuf);
- _evbuffer_decref_and_unlock(evbuf);
+ evbuffer_decref_and_unlock_(evbuf);
}
void
-evbuffer_commit_write(struct evbuffer *evbuf, ev_ssize_t nBytes)
+evbuffer_commit_write_(struct evbuffer *evbuf, ev_ssize_t nBytes)
{
struct evbuffer_overlapped *buf = upcast_evbuffer(evbuf);
@@ -144,11 +145,11 @@ evbuffer_commit_write(struct evbuffer *evbuf, ev_ssize_t nBytes)
evbuffer_drain(evbuf, nBytes);
pin_release(buf,EVBUFFER_MEM_PINNED_W);
buf->write_in_progress = 0;
- _evbuffer_decref_and_unlock(evbuf);
+ evbuffer_decref_and_unlock_(evbuf);
}
struct evbuffer *
-evbuffer_overlapped_new(evutil_socket_t fd)
+evbuffer_overlapped_new_(evutil_socket_t fd)
{
struct evbuffer_overlapped *evo;
@@ -156,7 +157,7 @@ evbuffer_overlapped_new(evutil_socket_t fd)
if (!evo)
return NULL;
- TAILQ_INIT(&evo->buffer.callbacks);
+ LIST_INIT(&evo->buffer.callbacks);
evo->buffer.refcnt = 1;
evo->buffer.last_with_datap = &evo->buffer.first;
@@ -167,7 +168,7 @@ evbuffer_overlapped_new(evutil_socket_t fd)
}
int
-evbuffer_launch_write(struct evbuffer *buf, ev_ssize_t at_most,
+evbuffer_launch_write_(struct evbuffer *buf, ev_ssize_t at_most,
struct event_overlapped *ol)
{
struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf);
@@ -203,7 +204,7 @@ evbuffer_launch_write(struct evbuffer *buf, ev_ssize_t at_most,
for (i=0; i < MAX_WSABUFS && chain; ++i, chain=chain->next) {
WSABUF *b = &buf_o->buffers[i];
b->buf = (char*)( chain->buffer + chain->misalign );
- _evbuffer_chain_pin(chain, EVBUFFER_MEM_PINNED_W);
+ evbuffer_chain_pin_(chain, EVBUFFER_MEM_PINNED_W);
if ((size_t)at_most > chain->off) {
/* XXXX Cast is safe for now, since win32 has no
@@ -220,7 +221,7 @@ evbuffer_launch_write(struct evbuffer *buf, ev_ssize_t at_most,
}
buf_o->n_buffers = i;
- _evbuffer_incref(buf);
+ evbuffer_incref_(buf);
if (WSASend(buf_o->fd, buf_o->buffers, i, &bytesSent, 0,
&ol->overlapped, NULL)) {
int error = WSAGetLastError();
@@ -241,7 +242,7 @@ done:
}
int
-evbuffer_launch_read(struct evbuffer *buf, size_t at_most,
+evbuffer_launch_read_(struct evbuffer *buf, size_t at_most,
struct event_overlapped *ol)
{
struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf);
@@ -264,11 +265,11 @@ evbuffer_launch_read(struct evbuffer *buf, size_t at_most,
buf_o->n_buffers = 0;
memset(buf_o->buffers, 0, sizeof(buf_o->buffers));
- if (_evbuffer_expand_fast(buf, at_most, MAX_WSABUFS) == -1)
+ if (evbuffer_expand_fast_(buf, at_most, MAX_WSABUFS) == -1)
goto done;
evbuffer_freeze(buf, 0);
- nvecs = _evbuffer_read_setup_vecs(buf, at_most,
+ nvecs = evbuffer_read_setup_vecs_(buf, at_most,
vecs, MAX_WSABUFS, &chainp, 1);
for (i=0;i<nvecs;++i) {
WSABUF_FROM_EVBUFFER_IOV(
@@ -281,12 +282,12 @@ evbuffer_launch_read(struct evbuffer *buf, size_t at_most,
npin=0;
for ( ; chain; chain = chain->next) {
- _evbuffer_chain_pin(chain, EVBUFFER_MEM_PINNED_R);
+ evbuffer_chain_pin_(chain, EVBUFFER_MEM_PINNED_R);
++npin;
}
EVUTIL_ASSERT(npin == nvecs);
- _evbuffer_incref(buf);
+ evbuffer_incref_(buf);
if (WSARecv(buf_o->fd, buf_o->buffers, nvecs, &bytesRead, &flags,
&ol->overlapped, NULL)) {
int error = WSAGetLastError();
@@ -307,14 +308,14 @@ done:
}
evutil_socket_t
-_evbuffer_overlapped_get_fd(struct evbuffer *buf)
+evbuffer_overlapped_get_fd_(struct evbuffer *buf)
{
struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf);
return buf_o ? buf_o->fd : -1;
}
void
-_evbuffer_overlapped_set_fd(struct evbuffer *buf, evutil_socket_t fd)
+evbuffer_overlapped_set_fd_(struct evbuffer *buf, evutil_socket_t fd)
{
struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf);
EVBUFFER_LOCK(buf);
diff --git a/bufferevent-internal.h b/bufferevent-internal.h
index 8348ab7a..134bb337 100644
--- a/bufferevent-internal.h
+++ b/bufferevent-internal.h
@@ -23,14 +23,16 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _BUFFEREVENT_INTERNAL_H_
-#define _BUFFEREVENT_INTERNAL_H_
+#ifndef BUFFEREVENT_INTERNAL_H_INCLUDED_
+#define BUFFEREVENT_INTERNAL_H_INCLUDED_
#ifdef __cplusplus
extern "C" {
#endif
#include "event2/event-config.h"
+#include "event2/event_struct.h"
+#include "evconfig-private.h"
#include "event2/util.h"
#include "defer-internal.h"
#include "evthread-internal.h"
@@ -65,7 +67,7 @@ typedef ev_uint16_t bufferevent_suspend_flags;
struct bufferevent_rate_limit_group {
/** List of all members in the group */
- TAILQ_HEAD(rlim_group_member_list, bufferevent_private) members;
+ LIST_HEAD(rlim_group_member_list, bufferevent_private) members;
/** Current limits for the group. */
struct ev_token_bucket rate_limit;
struct ev_token_bucket_cfg rate_limit_cfg;
@@ -103,6 +105,10 @@ struct bufferevent_rate_limit_group {
/** Timeout event that goes off once a tick, when the bucket is ready
* to refill. */
struct event master_refill_event;
+
+ /** Seed for weak random number generator. Protected by 'lock' */
+ struct evutil_weakrand_state weakrand_seed;
+
/** Lock to protect the members of this group. This lock should nest
* within every bufferevent lock: if you are holding this lock, do
* not assume you can lock another bufferevent. */
@@ -116,7 +122,7 @@ struct bufferevent_rate_limit {
*
* Note that this field is supposed to be protected by the group
* lock */
- TAILQ_ENTRY(bufferevent_private) next_in_group;
+ LIST_ENTRY(bufferevent_private) next_in_group;
/** The rate-limiting group for this bufferevent, or NULL if it is
* only rate-limited on its own. */
struct bufferevent_rate_limit_group *group;
@@ -177,7 +183,7 @@ struct bufferevent_private {
int dns_error;
/** Used to implement deferred callbacks */
- struct deferred_cb deferred;
+ struct event_callback deferred;
/** The options this bufferevent was constructed with */
enum bufferevent_options options;
@@ -189,6 +195,14 @@ struct bufferevent_private {
* If NULL, locking is disabled. */
void *lock;
+ /** No matter how big our bucket gets, don't try to read more than this
+ * much in a single read operation. */
+ ev_ssize_t max_single_read;
+
+ /** No matter how big our bucket gets, don't try to write more than this
+ * much in a single write operation. */
+ ev_ssize_t max_single_write;
+
/** Rate-limiting information for this bufferevent */
struct bufferevent_rate_limit *rate_limiting;
};
@@ -238,8 +252,13 @@ struct bufferevent_ops {
*/
int (*disable)(struct bufferevent *, short);
+ /** Detatches the bufferevent from related data structures. Called as
+ * soon as its reference count reaches 0. */
+ void (*unlink)(struct bufferevent *);
+
/** Free any storage and deallocate any extra data or structures used
- in this implementation.
+ in this implementation. Called when the bufferevent is
+ finalized.
*/
void (*destruct)(struct bufferevent *);
@@ -262,7 +281,7 @@ extern const struct bufferevent_ops bufferevent_ops_pair;
#define BEV_IS_FILTER(bevp) ((bevp)->be_ops == &bufferevent_ops_filter)
#define BEV_IS_PAIR(bevp) ((bevp)->be_ops == &bufferevent_ops_pair)
-#ifdef WIN32
+#ifdef _WIN32
extern const struct bufferevent_ops bufferevent_ops_async;
#define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async)
#else
@@ -270,69 +289,89 @@ extern const struct bufferevent_ops bufferevent_ops_async;
#endif
/** Initialize the shared parts of a bufferevent. */
-int bufferevent_init_common(struct bufferevent_private *, struct event_base *, const struct bufferevent_ops *, enum bufferevent_options options);
+int bufferevent_init_common_(struct bufferevent_private *, struct event_base *, const struct bufferevent_ops *, enum bufferevent_options options);
/** For internal use: temporarily stop all reads on bufev, until the conditions
* in 'what' are over. */
-void bufferevent_suspend_read(struct bufferevent *bufev, bufferevent_suspend_flags what);
+void bufferevent_suspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what);
/** For internal use: clear the conditions 'what' on bufev, and re-enable
* reading if there are no conditions left. */
-void bufferevent_unsuspend_read(struct bufferevent *bufev, bufferevent_suspend_flags what);
+void bufferevent_unsuspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what);
/** For internal use: temporarily stop all writes on bufev, until the conditions
* in 'what' are over. */
-void bufferevent_suspend_write(struct bufferevent *bufev, bufferevent_suspend_flags what);
+void bufferevent_suspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what);
/** For internal use: clear the conditions 'what' on bufev, and re-enable
* writing if there are no conditions left. */
-void bufferevent_unsuspend_write(struct bufferevent *bufev, bufferevent_suspend_flags what);
+void bufferevent_unsuspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what);
#define bufferevent_wm_suspend_read(b) \
- bufferevent_suspend_read((b), BEV_SUSPEND_WM)
+ bufferevent_suspend_read_((b), BEV_SUSPEND_WM)
#define bufferevent_wm_unsuspend_read(b) \
- bufferevent_unsuspend_read((b), BEV_SUSPEND_WM)
+ bufferevent_unsuspend_read_((b), BEV_SUSPEND_WM)
/*
Disable a bufferevent. Equivalent to bufferevent_disable(), but
first resets 'connecting' flag to force EV_WRITE down for sure.
XXXX this method will go away in the future; try not to add new users.
- See comment in evhttp_connection_reset() for discussion.
+ See comment in evhttp_connection_reset_() for discussion.
@param bufev the bufferevent to be disabled
@param event any combination of EV_READ | EV_WRITE.
@return 0 if successful, or -1 if an error occurred
@see bufferevent_disable()
*/
-int bufferevent_disable_hard(struct bufferevent *bufev, short event);
+int bufferevent_disable_hard_(struct bufferevent *bufev, short event);
/** Internal: Set up locking on a bufferevent. If lock is set, use it.
* Otherwise, use a new lock. */
-int bufferevent_enable_locking(struct bufferevent *bufev, void *lock);
+int bufferevent_enable_locking_(struct bufferevent *bufev, void *lock);
/** Internal: Increment the reference count on bufev. */
-void bufferevent_incref(struct bufferevent *bufev);
+void bufferevent_incref_(struct bufferevent *bufev);
/** Internal: Lock bufev and increase its reference count.
* unlocking it otherwise. */
-void _bufferevent_incref_and_lock(struct bufferevent *bufev);
+void bufferevent_incref_and_lock_(struct bufferevent *bufev);
/** Internal: Decrement the reference count on bufev. Returns 1 if it freed
* the bufferevent.*/
-int bufferevent_decref(struct bufferevent *bufev);
+int bufferevent_decref_(struct bufferevent *bufev);
/** Internal: Drop the reference count on bufev, freeing as necessary, and
* unlocking it otherwise. Returns 1 if it freed the bufferevent. */
-int _bufferevent_decref_and_unlock(struct bufferevent *bufev);
+int bufferevent_decref_and_unlock_(struct bufferevent *bufev);
/** Internal: If callbacks are deferred and we have a read callback, schedule
- * a readcb. Otherwise just run the readcb. */
-void _bufferevent_run_readcb(struct bufferevent *bufev);
+ * a readcb. Otherwise just run the readcb. Ignores watermarks. */
+void bufferevent_run_readcb_(struct bufferevent *bufev, int options);
/** Internal: If callbacks are deferred and we have a write callback, schedule
- * a writecb. Otherwise just run the writecb. */
-void _bufferevent_run_writecb(struct bufferevent *bufev);
+ * a writecb. Otherwise just run the writecb. Ignores watermarks. */
+void bufferevent_run_writecb_(struct bufferevent *bufev, int options);
/** Internal: If callbacks are deferred and we have an eventcb, schedule
- * it to run with events "what". Otherwise just run the eventcb. */
-void _bufferevent_run_eventcb(struct bufferevent *bufev, short what);
+ * it to run with events "what". Otherwise just run the eventcb.
+ * See bufferevent_trigger_event for meaning of "options". */
+void bufferevent_run_eventcb_(struct bufferevent *bufev, short what, int options);
+
+/** Internal: Run or schedule (if deferred or options contain
+ * BEV_TRIG_DEFER_CALLBACKS) I/O callbacks specified in iotype.
+ * Must already hold the bufev lock. Honors watermarks unless
+ * BEV_TRIG_IGNORE_WATERMARKS is in options. */
+static inline void bufferevent_trigger_nolock_(struct bufferevent *bufev, short iotype, int options);
+
+/* Making this inline since all of the common-case calls to this function in
+ * libevent use constant arguments. */
+static inline void
+bufferevent_trigger_nolock_(struct bufferevent *bufev, short iotype, int options)
+{
+ if ((iotype & EV_READ) && ((options & BEV_TRIG_IGNORE_WATERMARKS) ||
+ evbuffer_get_length(bufev->input) >= bufev->wm_read.low))
+ bufferevent_run_readcb_(bufev, options);
+ if ((iotype & EV_WRITE) && ((options & BEV_TRIG_IGNORE_WATERMARKS) ||
+ evbuffer_get_length(bufev->output) <= bufev->wm_write.low))
+ bufferevent_run_writecb_(bufev, options);
+}
/** Internal: Add the event 'ev' with timeout tv, unless tv is set to 0, in
* which case add ev with no timeout. */
-int _bufferevent_add_event(struct event *ev, const struct timeval *tv);
+int bufferevent_add_event_(struct event *ev, const struct timeval *tv);
/* =========
* These next functions implement timeouts for bufferevents that aren't doing
@@ -341,15 +380,14 @@ int _bufferevent_add_event(struct event *ev, const struct timeval *tv);
/** Internal use: Set up the ev_read and ev_write callbacks so that
* the other "generic_timeout" functions will work on it. Call this from
* the constructor function. */
-void _bufferevent_init_generic_timeout_cbs(struct bufferevent *bev);
-/** Internal use: Delete the ev_read and ev_write callbacks if they're pending.
- * Call this from the destructor function. */
-int _bufferevent_del_generic_timeout_cbs(struct bufferevent *bev);
+void bufferevent_init_generic_timeout_cbs_(struct bufferevent *bev);
/** Internal use: Add or delete the generic timeout events as appropriate.
* (If an event is enabled and a timeout is set, we add the event. Otherwise
* we delete it.) Call this from anything that changes the timeout values,
* that enabled EV_READ or EV_WRITE, or that disables EV_READ or EV_WRITE. */
-int _bufferevent_generic_adj_timeouts(struct bufferevent *bev);
+int bufferevent_generic_adj_timeouts_(struct bufferevent *bev);
+
+enum bufferevent_options bufferevent_get_options_(struct bufferevent *bev);
/** Internal use: We have just successfully read data into an inbuf, so
* reset the read timeout (if any). */
@@ -375,9 +413,9 @@ int _bufferevent_generic_adj_timeouts(struct bufferevent *bev);
* bufferevent_private. */
#define BEV_UPCAST(b) EVUTIL_UPCAST((b), struct bufferevent_private, bev)
-#ifdef _EVENT_DISABLE_THREAD_SUPPORT
-#define BEV_LOCK(b) _EVUTIL_NIL_STMT
-#define BEV_UNLOCK(b) _EVUTIL_NIL_STMT
+#ifdef EVENT__DISABLE_THREAD_SUPPORT
+#define BEV_LOCK(b) EVUTIL_NIL_STMT_
+#define BEV_UNLOCK(b) EVUTIL_NIL_STMT_
#else
/** Internal: Grab the lock (if any) on a bufferevent */
#define BEV_LOCK(b) do { \
@@ -395,16 +433,18 @@ int _bufferevent_generic_adj_timeouts(struct bufferevent *bev);
/* ==== For rate-limiting. */
-int _bufferevent_decrement_write_buckets(struct bufferevent_private *bev,
+int bufferevent_decrement_write_buckets_(struct bufferevent_private *bev,
ev_ssize_t bytes);
-int _bufferevent_decrement_read_buckets(struct bufferevent_private *bev,
+int bufferevent_decrement_read_buckets_(struct bufferevent_private *bev,
ev_ssize_t bytes);
-ev_ssize_t _bufferevent_get_read_max(struct bufferevent_private *bev);
-ev_ssize_t _bufferevent_get_write_max(struct bufferevent_private *bev);
+ev_ssize_t bufferevent_get_read_max_(struct bufferevent_private *bev);
+ev_ssize_t bufferevent_get_write_max_(struct bufferevent_private *bev);
+
+int bufferevent_ratelim_init_(struct bufferevent_private *bev);
#ifdef __cplusplus
}
#endif
-#endif /* _BUFFEREVENT_INTERNAL_H_ */
+#endif /* BUFFEREVENT_INTERNAL_H_INCLUDED_ */
diff --git a/bufferevent.c b/bufferevent.c
index 7518c3b1..d298d0b3 100644
--- a/bufferevent.c
+++ b/bufferevent.c
@@ -25,11 +25,12 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <sys/types.h>
-
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#include <sys/types.h>
+
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -37,11 +38,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#ifdef _EVENT_HAVE_STDARG_H
+#ifdef EVENT__HAVE_STDARG_H
#include <stdarg.h>
#endif
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#endif
#include <errno.h>
@@ -53,17 +54,18 @@
#include "event2/bufferevent_struct.h"
#include "event2/bufferevent_compat.h"
#include "event2/event.h"
+#include "event-internal.h"
#include "log-internal.h"
#include "mm-internal.h"
#include "bufferevent-internal.h"
#include "evbuffer-internal.h"
#include "util-internal.h"
-static void _bufferevent_cancel_all(struct bufferevent *bev);
-
+static void bufferevent_cancel_all_(struct bufferevent *bev);
+static void bufferevent_finalize_cb_(struct event_callback *evcb, void *arg_);
void
-bufferevent_suspend_read(struct bufferevent *bufev, bufferevent_suspend_flags what)
+bufferevent_suspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what)
{
struct bufferevent_private *bufev_private =
EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
@@ -75,7 +77,7 @@ bufferevent_suspend_read(struct bufferevent *bufev, bufferevent_suspend_flags wh
}
void
-bufferevent_unsuspend_read(struct bufferevent *bufev, bufferevent_suspend_flags what)
+bufferevent_unsuspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what)
{
struct bufferevent_private *bufev_private =
EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
@@ -87,7 +89,7 @@ bufferevent_unsuspend_read(struct bufferevent *bufev, bufferevent_suspend_flags
}
void
-bufferevent_suspend_write(struct bufferevent *bufev, bufferevent_suspend_flags what)
+bufferevent_suspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what)
{
struct bufferevent_private *bufev_private =
EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
@@ -99,7 +101,7 @@ bufferevent_suspend_write(struct bufferevent *bufev, bufferevent_suspend_flags w
}
void
-bufferevent_unsuspend_write(struct bufferevent *bufev, bufferevent_suspend_flags what)
+bufferevent_unsuspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what)
{
struct bufferevent_private *bufev_private =
EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
@@ -130,7 +132,7 @@ bufferevent_inbuf_wm_cb(struct evbuffer *buf,
}
static void
-bufferevent_run_deferred_callbacks_locked(struct deferred_cb *_, void *arg)
+bufferevent_run_deferred_callbacks_locked(struct event_callback *cb, void *arg)
{
struct bufferevent_private *bufev_private = arg;
struct bufferevent *bufev = &bufev_private->bev;
@@ -159,11 +161,11 @@ bufferevent_run_deferred_callbacks_locked(struct deferred_cb *_, void *arg)
EVUTIL_SET_SOCKET_ERROR(err);
bufev->errorcb(bufev, what, bufev->cbarg);
}
- _bufferevent_decref_and_unlock(bufev);
+ bufferevent_decref_and_unlock_(bufev);
}
static void
-bufferevent_run_deferred_callbacks_unlocked(struct deferred_cb *_, void *arg)
+bufferevent_run_deferred_callbacks_unlocked(struct event_callback *cb, void *arg)
{
struct bufferevent_private *bufev_private = arg;
struct bufferevent *bufev = &bufev_private->bev;
@@ -203,73 +205,91 @@ bufferevent_run_deferred_callbacks_unlocked(struct deferred_cb *_, void *arg)
EVUTIL_SET_SOCKET_ERROR(err);
UNLOCKED(errorcb(bufev,what,cbarg));
}
- _bufferevent_decref_and_unlock(bufev);
+ bufferevent_decref_and_unlock_(bufev);
#undef UNLOCKED
}
#define SCHEDULE_DEFERRED(bevp) \
do { \
- bufferevent_incref(&(bevp)->bev); \
- event_deferred_cb_schedule( \
- event_base_get_deferred_cb_queue((bevp)->bev.ev_base), \
- &(bevp)->deferred); \
+ if (event_deferred_cb_schedule_( \
+ (bevp)->bev.ev_base, \
+ &(bevp)->deferred)) \
+ bufferevent_incref_(&(bevp)->bev); \
} while (0)
void
-_bufferevent_run_readcb(struct bufferevent *bufev)
+bufferevent_run_readcb_(struct bufferevent *bufev, int options)
{
/* Requires that we hold the lock and a reference */
struct bufferevent_private *p =
EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
if (bufev->readcb == NULL)
return;
- if (p->options & BEV_OPT_DEFER_CALLBACKS) {
+ if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
p->readcb_pending = 1;
- if (!p->deferred.queued)
- SCHEDULE_DEFERRED(p);
+ SCHEDULE_DEFERRED(p);
} else {
bufev->readcb(bufev, bufev->cbarg);
}
}
void
-_bufferevent_run_writecb(struct bufferevent *bufev)
+bufferevent_run_writecb_(struct bufferevent *bufev, int options)
{
/* Requires that we hold the lock and a reference */
struct bufferevent_private *p =
EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
if (bufev->writecb == NULL)
return;
- if (p->options & BEV_OPT_DEFER_CALLBACKS) {
+ if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
p->writecb_pending = 1;
- if (!p->deferred.queued)
- SCHEDULE_DEFERRED(p);
+ SCHEDULE_DEFERRED(p);
} else {
bufev->writecb(bufev, bufev->cbarg);
}
}
+#define BEV_TRIG_ALL_OPTS ( \
+ BEV_TRIG_IGNORE_WATERMARKS| \
+ BEV_TRIG_DEFER_CALLBACKS \
+ )
+
+void
+bufferevent_trigger(struct bufferevent *bufev, short iotype, int options)
+{
+ bufferevent_incref_and_lock_(bufev);
+ bufferevent_trigger_nolock_(bufev, iotype, options&BEV_TRIG_ALL_OPTS);
+ bufferevent_decref_and_unlock_(bufev);
+}
+
void
-_bufferevent_run_eventcb(struct bufferevent *bufev, short what)
+bufferevent_run_eventcb_(struct bufferevent *bufev, short what, int options)
{
/* Requires that we hold the lock and a reference */
struct bufferevent_private *p =
EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
if (bufev->errorcb == NULL)
return;
- if (p->options & BEV_OPT_DEFER_CALLBACKS) {
+ if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
p->eventcb_pending |= what;
p->errno_pending = EVUTIL_SOCKET_ERROR();
- if (!p->deferred.queued)
- SCHEDULE_DEFERRED(p);
+ SCHEDULE_DEFERRED(p);
} else {
bufev->errorcb(bufev, what, bufev->cbarg);
}
}
+void
+bufferevent_trigger_event(struct bufferevent *bufev, short what, int options)
+{
+ bufferevent_incref_and_lock_(bufev);
+ bufferevent_run_eventcb_(bufev, what, options&BEV_TRIG_ALL_OPTS);
+ bufferevent_decref_and_unlock_(bufev);
+}
+
int
-bufferevent_init_common(struct bufferevent_private *bufev_private,
+bufferevent_init_common_(struct bufferevent_private *bufev_private,
struct event_base *base,
const struct bufferevent_ops *ops,
enum bufferevent_options options)
@@ -297,6 +317,8 @@ bufferevent_init_common(struct bufferevent_private *bufev_private,
bufev->be_ops = ops;
+ bufferevent_ratelim_init_(bufev_private);
+
/*
* Set to EV_WRITE so that using bufferevent_write is going to
* trigger a callback. Reading needs to be explicitly enabled
@@ -304,9 +326,9 @@ bufferevent_init_common(struct bufferevent_private *bufev_private,
*/
bufev->enabled = EV_WRITE;
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
if (options & BEV_OPT_THREADSAFE) {
- if (bufferevent_enable_locking(bufev, NULL) < 0) {
+ if (bufferevent_enable_locking_(bufev, NULL) < 0) {
/* cleanup */
evbuffer_free(bufev->input);
evbuffer_free(bufev->output);
@@ -321,21 +343,23 @@ bufferevent_init_common(struct bufferevent_private *bufev_private,
event_warnx("UNLOCK_CALLBACKS requires DEFER_CALLBACKS");
return -1;
}
- if (options & BEV_OPT_DEFER_CALLBACKS) {
- if (options & BEV_OPT_UNLOCK_CALLBACKS)
- event_deferred_cb_init(&bufev_private->deferred,
- bufferevent_run_deferred_callbacks_unlocked,
- bufev_private);
- else
- event_deferred_cb_init(&bufev_private->deferred,
- bufferevent_run_deferred_callbacks_locked,
- bufev_private);
- }
+ if (options & BEV_OPT_UNLOCK_CALLBACKS)
+ event_deferred_cb_init_(
+ &bufev_private->deferred,
+ event_base_get_npriorities(base) / 2,
+ bufferevent_run_deferred_callbacks_unlocked,
+ bufev_private);
+ else
+ event_deferred_cb_init_(
+ &bufev_private->deferred,
+ event_base_get_npriorities(base) / 2,
+ bufferevent_run_deferred_callbacks_locked,
+ bufev_private);
bufev_private->options = options;
- evbuffer_set_parent(bufev->input, bufev);
- evbuffer_set_parent(bufev->output, bufev);
+ evbuffer_set_parent_(bufev->input, bufev);
+ evbuffer_set_parent_(bufev->output, bufev);
return 0;
}
@@ -355,6 +379,26 @@ bufferevent_setcb(struct bufferevent *bufev,
BEV_UNLOCK(bufev);
}
+void
+bufferevent_getcb(struct bufferevent *bufev,
+ bufferevent_data_cb *readcb_ptr,
+ bufferevent_data_cb *writecb_ptr,
+ bufferevent_event_cb *eventcb_ptr,
+ void **cbarg_ptr)
+{
+ BEV_LOCK(bufev);
+ if (readcb_ptr)
+ *readcb_ptr = bufev->readcb;
+ if (writecb_ptr)
+ *writecb_ptr = bufev->writecb;
+ if (eventcb_ptr)
+ *eventcb_ptr = bufev->errorcb;
+ if (cbarg_ptr)
+ *cbarg_ptr = bufev->cbarg;
+
+ BEV_UNLOCK(bufev);
+}
+
struct evbuffer *
bufferevent_get_input(struct bufferevent *bufev)
{
@@ -374,6 +418,16 @@ bufferevent_get_base(struct bufferevent *bufev)
}
int
+bufferevent_get_priority(const struct bufferevent *bufev)
+{
+ if (event_initialized(&bufev->ev_read)) {
+ return event_get_priority(&bufev->ev_read);
+ } else {
+ return event_base_get_npriorities(bufev->ev_base) / 2;
+ }
+}
+
+int
bufferevent_write(struct bufferevent *bufev, const void *data, size_t size)
{
if (evbuffer_add(bufev->output, data, size) == -1)
@@ -411,7 +465,7 @@ bufferevent_enable(struct bufferevent *bufev, short event)
short impl_events = event;
int r = 0;
- _bufferevent_incref_and_lock(bufev);
+ bufferevent_incref_and_lock_(bufev);
if (bufev_private->read_suspended)
impl_events &= ~EV_READ;
if (bufev_private->write_suspended)
@@ -422,7 +476,7 @@ bufferevent_enable(struct bufferevent *bufev, short event)
if (impl_events && bufev->be_ops->enable(bufev, impl_events) < 0)
r = -1;
- _bufferevent_decref_and_unlock(bufev);
+ bufferevent_decref_and_unlock_(bufev);
return r;
}
@@ -477,7 +531,7 @@ bufferevent_settimeout(struct bufferevent *bufev,
int
-bufferevent_disable_hard(struct bufferevent *bufev, short event)
+bufferevent_disable_hard_(struct bufferevent *bufev, short event)
{
int r = 0;
struct bufferevent_private *bufev_private =
@@ -562,6 +616,32 @@ bufferevent_setwatermark(struct bufferevent *bufev, short events,
}
int
+bufferevent_getwatermark(struct bufferevent *bufev, short events,
+ size_t *lowmark, size_t *highmark)
+{
+ if (events == EV_WRITE) {
+ BEV_LOCK(bufev);
+ if (lowmark)
+ *lowmark = bufev->wm_write.low;
+ if (highmark)
+ *highmark = bufev->wm_write.high;
+ BEV_UNLOCK(bufev);
+ return 0;
+ }
+
+ if (events == EV_READ) {
+ BEV_LOCK(bufev);
+ if (lowmark)
+ *lowmark = bufev->wm_read.low;
+ if (highmark)
+ *highmark = bufev->wm_read.high;
+ BEV_UNLOCK(bufev);
+ return 0;
+ }
+ return -1;
+}
+
+int
bufferevent_flush(struct bufferevent *bufev,
short iotype,
enum bufferevent_flush_mode mode)
@@ -575,7 +655,7 @@ bufferevent_flush(struct bufferevent *bufev,
}
void
-_bufferevent_incref_and_lock(struct bufferevent *bufev)
+bufferevent_incref_and_lock_(struct bufferevent *bufev)
{
struct bufferevent_private *bufev_private =
BEV_UPCAST(bufev);
@@ -585,7 +665,7 @@ _bufferevent_incref_and_lock(struct bufferevent *bufev)
#if 0
static void
-_bufferevent_transfer_lock_ownership(struct bufferevent *donor,
+bufferevent_transfer_lock_ownership_(struct bufferevent *donor,
struct bufferevent *recipient)
{
struct bufferevent_private *d = BEV_UPCAST(donor);
@@ -602,11 +682,13 @@ _bufferevent_transfer_lock_ownership(struct bufferevent *donor,
#endif
int
-_bufferevent_decref_and_unlock(struct bufferevent *bufev)
+bufferevent_decref_and_unlock_(struct bufferevent *bufev)
{
struct bufferevent_private *bufev_private =
EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
- struct bufferevent *underlying;
+ int n_cbs = 0;
+#define MAX_CBS 16
+ struct event_callback *cbs[MAX_CBS];
EVUTIL_ASSERT(bufev_private->refcnt > 0);
@@ -615,6 +697,41 @@ _bufferevent_decref_and_unlock(struct bufferevent *bufev)
return 0;
}
+ if (bufev->be_ops->unlink)
+ bufev->be_ops->unlink(bufev);
+
+ /* Okay, we're out of references. Let's finalize this once all the
+ * callbacks are done running. */
+ cbs[0] = &bufev->ev_read.ev_evcallback;
+ cbs[1] = &bufev->ev_write.ev_evcallback;
+ cbs[2] = &bufev_private->deferred;
+ n_cbs = 3;
+ if (bufev_private->rate_limiting) {
+ struct event *e = &bufev_private->rate_limiting->refill_bucket_event;
+ if (event_initialized(e))
+ cbs[n_cbs++] = &e->ev_evcallback;
+ }
+ n_cbs += evbuffer_get_callbacks_(bufev->input, cbs+n_cbs, MAX_CBS-n_cbs);
+ n_cbs += evbuffer_get_callbacks_(bufev->output, cbs+n_cbs, MAX_CBS-n_cbs);
+
+ event_callback_finalize_many_(bufev->ev_base, n_cbs, cbs,
+ bufferevent_finalize_cb_);
+
+#undef MAX_CBS
+ BEV_UNLOCK(bufev);
+
+ return 1;
+}
+
+static void
+bufferevent_finalize_cb_(struct event_callback *evcb, void *arg_)
+{
+ struct bufferevent *bufev = arg_;
+ struct bufferevent *underlying;
+ struct bufferevent_private *bufev_private =
+ EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
+
+ BEV_LOCK(bufev);
underlying = bufferevent_get_underlying(bufev);
/* Clean up the shared info */
@@ -630,18 +747,14 @@ _bufferevent_decref_and_unlock(struct bufferevent *bufev)
if (bufev_private->rate_limiting) {
if (bufev_private->rate_limiting->group)
- bufferevent_remove_from_rate_limit_group_internal(bufev,0);
- if (event_initialized(&bufev_private->rate_limiting->refill_bucket_event))
- event_del(&bufev_private->rate_limiting->refill_bucket_event);
- event_debug_unassign(&bufev_private->rate_limiting->refill_bucket_event);
+ bufferevent_remove_from_rate_limit_group_internal_(bufev,0);
mm_free(bufev_private->rate_limiting);
bufev_private->rate_limiting = NULL;
}
- event_debug_unassign(&bufev->ev_read);
- event_debug_unassign(&bufev->ev_write);
BEV_UNLOCK(bufev);
+
if (bufev_private->own_lock)
EVTHREAD_FREE_LOCK(bufev_private->lock,
EVTHREAD_LOCKTYPE_RECURSIVE);
@@ -660,16 +773,14 @@ _bufferevent_decref_and_unlock(struct bufferevent *bufev)
* It would probably save us some headaches.
*/
if (underlying)
- bufferevent_decref(underlying);
-
- return 1;
+ bufferevent_decref_(underlying);
}
int
-bufferevent_decref(struct bufferevent *bufev)
+bufferevent_decref_(struct bufferevent *bufev)
{
BEV_LOCK(bufev);
- return _bufferevent_decref_and_unlock(bufev);
+ return bufferevent_decref_and_unlock_(bufev);
}
void
@@ -677,12 +788,12 @@ bufferevent_free(struct bufferevent *bufev)
{
BEV_LOCK(bufev);
bufferevent_setcb(bufev, NULL, NULL, NULL, NULL);
- _bufferevent_cancel_all(bufev);
- _bufferevent_decref_and_unlock(bufev);
+ bufferevent_cancel_all_(bufev);
+ bufferevent_decref_and_unlock_(bufev);
}
void
-bufferevent_incref(struct bufferevent *bufev)
+bufferevent_incref_(struct bufferevent *bufev)
{
struct bufferevent_private *bufev_private =
EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
@@ -693,9 +804,9 @@ bufferevent_incref(struct bufferevent *bufev)
}
int
-bufferevent_enable_locking(struct bufferevent *bufev, void *lock)
+bufferevent_enable_locking_(struct bufferevent *bufev, void *lock)
{
-#ifdef _EVENT_DISABLE_THREAD_SUPPORT
+#ifdef EVENT__DISABLE_THREAD_SUPPORT
return -1;
#else
struct bufferevent *underlying;
@@ -722,7 +833,7 @@ bufferevent_enable_locking(struct bufferevent *bufev, void *lock)
evbuffer_enable_locking(bufev->output, lock);
if (underlying && !BEV_UPCAST(underlying)->lock)
- bufferevent_enable_locking(underlying, lock);
+ bufferevent_enable_locking_(underlying, lock);
return 0;
#endif
@@ -754,8 +865,22 @@ bufferevent_getfd(struct bufferevent *bev)
return (res<0) ? -1 : d.fd;
}
+enum bufferevent_options
+bufferevent_get_options_(struct bufferevent *bev)
+{
+ struct bufferevent_private *bev_p =
+ EVUTIL_UPCAST(bev, struct bufferevent_private, bev);
+ enum bufferevent_options options;
+
+ BEV_LOCK(bev);
+ options = bev_p->options;
+ BEV_UNLOCK(bev);
+ return options;
+}
+
+
static void
-_bufferevent_cancel_all(struct bufferevent *bev)
+bufferevent_cancel_all_(struct bufferevent *bev)
{
union bufferevent_ctrl_data d;
memset(&d, 0, sizeof(d));
@@ -792,43 +917,32 @@ static void
bufferevent_generic_read_timeout_cb(evutil_socket_t fd, short event, void *ctx)
{
struct bufferevent *bev = ctx;
- _bufferevent_incref_and_lock(bev);
+ bufferevent_incref_and_lock_(bev);
bufferevent_disable(bev, EV_READ);
- _bufferevent_run_eventcb(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_READING);
- _bufferevent_decref_and_unlock(bev);
+ bufferevent_run_eventcb_(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_READING, 0);
+ bufferevent_decref_and_unlock_(bev);
}
static void
bufferevent_generic_write_timeout_cb(evutil_socket_t fd, short event, void *ctx)
{
struct bufferevent *bev = ctx;
- _bufferevent_incref_and_lock(bev);
+ bufferevent_incref_and_lock_(bev);
bufferevent_disable(bev, EV_WRITE);
- _bufferevent_run_eventcb(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING);
- _bufferevent_decref_and_unlock(bev);
+ bufferevent_run_eventcb_(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING, 0);
+ bufferevent_decref_and_unlock_(bev);
}
void
-_bufferevent_init_generic_timeout_cbs(struct bufferevent *bev)
+bufferevent_init_generic_timeout_cbs_(struct bufferevent *bev)
{
- evtimer_assign(&bev->ev_read, bev->ev_base,
+ event_assign(&bev->ev_read, bev->ev_base, -1, EV_FINALIZE,
bufferevent_generic_read_timeout_cb, bev);
- evtimer_assign(&bev->ev_write, bev->ev_base,
+ event_assign(&bev->ev_write, bev->ev_base, -1, EV_FINALIZE,
bufferevent_generic_write_timeout_cb, bev);
}
int
-_bufferevent_del_generic_timeout_cbs(struct bufferevent *bev)
-{
- int r1,r2;
- r1 = event_del(&bev->ev_read);
- r2 = event_del(&bev->ev_write);
- if (r1<0 || r2<0)
- return -1;
- return 0;
-}
-
-int
-_bufferevent_generic_adj_timeouts(struct bufferevent *bev)
+bufferevent_generic_adj_timeouts_(struct bufferevent *bev)
{
const short enabled = bev->enabled;
struct bufferevent_private *bev_p =
@@ -852,7 +966,7 @@ _bufferevent_generic_adj_timeouts(struct bufferevent *bev)
}
int
-_bufferevent_add_event(struct event *ev, const struct timeval *tv)
+bufferevent_add_event_(struct event *ev, const struct timeval *tv)
{
if (tv->tv_sec == 0 && tv->tv_usec == 0)
return event_add(ev, NULL);
@@ -861,15 +975,15 @@ _bufferevent_add_event(struct event *ev, const struct timeval *tv)
}
/* For use by user programs only; internally, we should be calling
- either _bufferevent_incref_and_lock(), or BEV_LOCK. */
+ either bufferevent_incref_and_lock_(), or BEV_LOCK. */
void
bufferevent_lock(struct bufferevent *bev)
{
- _bufferevent_incref_and_lock(bev);
+ bufferevent_incref_and_lock_(bev);
}
void
bufferevent_unlock(struct bufferevent *bev)
{
- _bufferevent_decref_and_unlock(bev);
+ bufferevent_decref_and_unlock_(bev);
}
diff --git a/bufferevent_async.c b/bufferevent_async.c
index f34ca5f1..6395e57a 100644
--- a/bufferevent_async.c
+++ b/bufferevent_async.c
@@ -27,8 +27,9 @@
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -36,14 +37,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#ifdef _EVENT_HAVE_STDARG_H
+#ifdef EVENT__HAVE_STDARG_H
#include <stdarg.h>
#endif
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
@@ -92,8 +93,9 @@ const struct bufferevent_ops bufferevent_ops_async = {
evutil_offsetof(struct bufferevent_async, bev.bev),
be_async_enable,
be_async_disable,
+ NULL, /* Unlink */
be_async_destruct,
- _bufferevent_generic_adj_timeouts,
+ bufferevent_generic_adj_timeouts_,
be_async_flush,
be_async_ctrl,
};
@@ -142,7 +144,7 @@ bev_async_del_write(struct bufferevent_async *beva)
if (beva->write_added) {
beva->write_added = 0;
- event_base_del_virtual(bev->ev_base);
+ event_base_del_virtual_(bev->ev_base);
}
}
@@ -153,7 +155,7 @@ bev_async_del_read(struct bufferevent_async *beva)
if (beva->read_added) {
beva->read_added = 0;
- event_base_del_virtual(bev->ev_base);
+ event_base_del_virtual_(bev->ev_base);
}
}
@@ -164,7 +166,7 @@ bev_async_add_write(struct bufferevent_async *beva)
if (!beva->write_added) {
beva->write_added = 1;
- event_base_add_virtual(bev->ev_base);
+ event_base_add_virtual_(bev->ev_base);
}
}
@@ -175,7 +177,7 @@ bev_async_add_read(struct bufferevent_async *beva)
if (!beva->read_added) {
beva->read_added = 1;
- event_base_add_virtual(bev->ev_base);
+ event_base_add_virtual_(bev->ev_base);
}
}
@@ -200,7 +202,7 @@ bev_async_consider_writing(struct bufferevent_async *beva)
/* This is safe so long as bufferevent_get_write_max never returns
* more than INT_MAX. That's true for now. XXXX */
- limit = (int)_bufferevent_get_write_max(&beva->bev);
+ limit = (int)bufferevent_get_write_max_(&beva->bev);
if (at_most >= (size_t)limit && limit >= 0)
at_most = limit;
@@ -210,15 +212,15 @@ bev_async_consider_writing(struct bufferevent_async *beva)
}
/* XXXX doesn't respect low-water mark very well. */
- bufferevent_incref(bev);
- if (evbuffer_launch_write(bev->output, at_most,
+ bufferevent_incref_(bev);
+ if (evbuffer_launch_write_(bev->output, at_most,
&beva->write_overlapped)) {
- bufferevent_decref(bev);
+ bufferevent_decref_(bev);
beva->ok = 0;
- _bufferevent_run_eventcb(bev, BEV_EVENT_ERROR);
+ bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, 0);
} else {
beva->write_in_progress = at_most;
- _bufferevent_decrement_write_buckets(&beva->bev, at_most);
+ bufferevent_decrement_write_buckets_(&beva->bev, at_most);
bev_async_add_write(beva);
}
}
@@ -255,8 +257,8 @@ bev_async_consider_reading(struct bufferevent_async *beva)
}
/* XXXX This over-commits. */
- /* XXXX see also not above on cast on _bufferevent_get_write_max() */
- limit = (int)_bufferevent_get_read_max(&beva->bev);
+ /* XXXX see also not above on cast on bufferevent_get_write_max_() */
+ limit = (int)bufferevent_get_read_max_(&beva->bev);
if (at_most >= (size_t)limit && limit >= 0)
at_most = limit;
@@ -265,14 +267,14 @@ bev_async_consider_reading(struct bufferevent_async *beva)
return;
}
- bufferevent_incref(bev);
- if (evbuffer_launch_read(bev->input, at_most, &beva->read_overlapped)) {
+ bufferevent_incref_(bev);
+ if (evbuffer_launch_read_(bev->input, at_most, &beva->read_overlapped)) {
beva->ok = 0;
- _bufferevent_run_eventcb(bev, BEV_EVENT_ERROR);
- bufferevent_decref(bev);
+ bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, 0);
+ bufferevent_decref_(bev);
} else {
beva->read_in_progress = at_most;
- _bufferevent_decrement_read_buckets(&beva->bev, at_most);
+ bufferevent_decrement_read_buckets_(&beva->bev, at_most);
bev_async_add_read(beva);
}
@@ -290,12 +292,12 @@ be_async_outbuf_callback(struct evbuffer *buf,
/* If we added data to the outbuf and were not writing before,
* we may want to write now. */
- _bufferevent_incref_and_lock(bev);
+ bufferevent_incref_and_lock_(bev);
if (cbinfo->n_added)
bev_async_consider_writing(bev_async);
- _bufferevent_decref_and_unlock(bev);
+ bufferevent_decref_and_unlock_(bev);
}
static void
@@ -309,12 +311,12 @@ be_async_inbuf_callback(struct evbuffer *buf,
/* If we drained data from the inbuf and were not reading before,
* we may want to read now */
- _bufferevent_incref_and_lock(bev);
+ bufferevent_incref_and_lock_(bev);
if (cbinfo->n_deleted)
bev_async_consider_reading(bev_async);
- _bufferevent_decref_and_unlock(bev);
+ bufferevent_decref_and_unlock_(bev);
}
static int
@@ -378,15 +380,11 @@ be_async_destruct(struct bufferevent *bev)
bev_async_del_read(bev_async);
bev_async_del_write(bev_async);
- fd = _evbuffer_overlapped_get_fd(bev->input);
- if (bev_p->options & BEV_OPT_CLOSE_ON_FREE) {
- /* XXXX possible double-close */
+ fd = evbuffer_overlapped_get_fd_(bev->input);
+ if (fd != (evutil_socket_t)INVALID_SOCKET &&
+ (bev_p->options & BEV_OPT_CLOSE_ON_FREE)) {
evutil_closesocket(fd);
- }
- /* delete this in case non-blocking connect was used */
- if (event_initialized(&bev->ev_write)) {
- event_del(&bev->ev_write);
- _bufferevent_del_generic_timeout_cbs(bev);
+ evbuffer_overlapped_set_fd_(bev->input, INVALID_SOCKET);
}
}
@@ -398,7 +396,7 @@ bev_async_set_wsa_error(struct bufferevent *bev, struct event_overlapped *eo)
DWORD bytes, flags;
evutil_socket_t fd;
- fd = _evbuffer_overlapped_get_fd(bev->input);
+ fd = evbuffer_overlapped_get_fd_(bev->input);
WSAGetOverlappedResult(fd, &eo->overlapped, &bytes, FALSE, &flags);
}
@@ -421,21 +419,21 @@ connect_complete(struct event_overlapped *eo, ev_uintptr_t key,
EVUTIL_ASSERT(bev_a->bev.connecting);
bev_a->bev.connecting = 0;
- sock = _evbuffer_overlapped_get_fd(bev_a->bev.bev.input);
+ sock = evbuffer_overlapped_get_fd_(bev_a->bev.bev.input);
/* XXXX Handle error? */
setsockopt(sock, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0);
if (ok)
- bufferevent_async_set_connected(bev);
+ bufferevent_async_set_connected_(bev);
else
bev_async_set_wsa_error(bev, eo);
- _bufferevent_run_eventcb(bev,
- ok? BEV_EVENT_CONNECTED : BEV_EVENT_ERROR);
+ bufferevent_run_eventcb_(bev,
+ ok? BEV_EVENT_CONNECTED : BEV_EVENT_ERROR, 0);
- event_base_del_virtual(bev->ev_base);
+ event_base_del_virtual_(bev->ev_base);
- _bufferevent_decref_and_unlock(bev);
+ bufferevent_decref_and_unlock_(bev);
}
static void
@@ -450,10 +448,10 @@ read_complete(struct event_overlapped *eo, ev_uintptr_t key,
EVUTIL_ASSERT(bev_a->read_in_progress);
amount_unread = bev_a->read_in_progress - nbytes;
- evbuffer_commit_read(bev->input, nbytes);
+ evbuffer_commit_read_(bev->input, nbytes);
bev_a->read_in_progress = 0;
if (amount_unread)
- _bufferevent_decrement_read_buckets(&bev_a->bev, -amount_unread);
+ bufferevent_decrement_read_buckets_(&bev_a->bev, -amount_unread);
if (!ok)
bev_async_set_wsa_error(bev, eo);
@@ -461,21 +459,20 @@ read_complete(struct event_overlapped *eo, ev_uintptr_t key,
if (bev_a->ok) {
if (ok && nbytes) {
BEV_RESET_GENERIC_READ_TIMEOUT(bev);
- if (evbuffer_get_length(bev->input) >= bev->wm_read.low)
- _bufferevent_run_readcb(bev);
+ bufferevent_trigger_nolock_(bev, EV_READ, 0);
bev_async_consider_reading(bev_a);
} else if (!ok) {
what |= BEV_EVENT_ERROR;
bev_a->ok = 0;
- _bufferevent_run_eventcb(bev, what);
+ bufferevent_run_eventcb_(bev, what, 0);
} else if (!nbytes) {
what |= BEV_EVENT_EOF;
bev_a->ok = 0;
- _bufferevent_run_eventcb(bev, what);
+ bufferevent_run_eventcb_(bev, what, 0);
}
}
- _bufferevent_decref_and_unlock(bev);
+ bufferevent_decref_and_unlock_(bev);
}
static void
@@ -491,11 +488,11 @@ write_complete(struct event_overlapped *eo, ev_uintptr_t key,
EVUTIL_ASSERT(bev_a->write_in_progress);
amount_unwritten = bev_a->write_in_progress - nbytes;
- evbuffer_commit_write(bev->output, nbytes);
+ evbuffer_commit_write_(bev->output, nbytes);
bev_a->write_in_progress = 0;
if (amount_unwritten)
- _bufferevent_decrement_write_buckets(&bev_a->bev,
+ bufferevent_decrement_write_buckets_(&bev_a->bev,
-amount_unwritten);
@@ -505,26 +502,24 @@ write_complete(struct event_overlapped *eo, ev_uintptr_t key,
if (bev_a->ok) {
if (ok && nbytes) {
BEV_RESET_GENERIC_WRITE_TIMEOUT(bev);
- if (evbuffer_get_length(bev->output) <=
- bev->wm_write.low)
- _bufferevent_run_writecb(bev);
+ bufferevent_trigger_nolock_(bev, EV_WRITE, 0);
bev_async_consider_writing(bev_a);
} else if (!ok) {
what |= BEV_EVENT_ERROR;
bev_a->ok = 0;
- _bufferevent_run_eventcb(bev, what);
+ bufferevent_run_eventcb_(bev, what, 0);
} else if (!nbytes) {
what |= BEV_EVENT_EOF;
bev_a->ok = 0;
- _bufferevent_run_eventcb(bev, what);
+ bufferevent_run_eventcb_(bev, what, 0);
}
}
- _bufferevent_decref_and_unlock(bev);
+ bufferevent_decref_and_unlock_(bev);
}
struct bufferevent *
-bufferevent_async_new(struct event_base *base,
+bufferevent_async_new_(struct event_base *base,
evutil_socket_t fd, int options)
{
struct bufferevent_async *bev_a;
@@ -533,10 +528,10 @@ bufferevent_async_new(struct event_base *base,
options |= BEV_OPT_THREADSAFE;
- if (!(iocp = event_base_get_iocp(base)))
+ if (!(iocp = event_base_get_iocp_(base)))
return NULL;
- if (fd >= 0 && event_iocp_port_associate(iocp, fd, 1)<0) {
+ if (fd >= 0 && event_iocp_port_associate_(iocp, fd, 1)<0) {
int err = GetLastError();
/* We may have alrady associated this fd with a port.
* Let's hope it's this port, and that the error code
@@ -549,30 +544,30 @@ bufferevent_async_new(struct event_base *base,
return NULL;
bev = &bev_a->bev.bev;
- if (!(bev->input = evbuffer_overlapped_new(fd))) {
+ if (!(bev->input = evbuffer_overlapped_new_(fd))) {
mm_free(bev_a);
return NULL;
}
- if (!(bev->output = evbuffer_overlapped_new(fd))) {
+ if (!(bev->output = evbuffer_overlapped_new_(fd))) {
evbuffer_free(bev->input);
mm_free(bev_a);
return NULL;
}
- if (bufferevent_init_common(&bev_a->bev, base, &bufferevent_ops_async,
+ if (bufferevent_init_common_(&bev_a->bev, base, &bufferevent_ops_async,
options)<0)
goto err;
evbuffer_add_cb(bev->input, be_async_inbuf_callback, bev);
evbuffer_add_cb(bev->output, be_async_outbuf_callback, bev);
- event_overlapped_init(&bev_a->connect_overlapped, connect_complete);
- event_overlapped_init(&bev_a->read_overlapped, read_complete);
- event_overlapped_init(&bev_a->write_overlapped, write_complete);
+ event_overlapped_init_(&bev_a->connect_overlapped, connect_complete);
+ event_overlapped_init_(&bev_a->read_overlapped, read_complete);
+ event_overlapped_init_(&bev_a->write_overlapped, write_complete);
+
+ bufferevent_init_generic_timeout_cbs_(bev);
bev_a->ok = fd >= 0;
- if (bev_a->ok)
- _bufferevent_init_generic_timeout_cbs(bev);
return bev;
err:
@@ -581,23 +576,23 @@ err:
}
void
-bufferevent_async_set_connected(struct bufferevent *bev)
+bufferevent_async_set_connected_(struct bufferevent *bev)
{
struct bufferevent_async *bev_async = upcast(bev);
bev_async->ok = 1;
- _bufferevent_init_generic_timeout_cbs(bev);
+ bufferevent_init_generic_timeout_cbs_(bev);
/* Now's a good time to consider reading/writing */
be_async_enable(bev, bev->enabled);
}
int
-bufferevent_async_can_connect(struct bufferevent *bev)
+bufferevent_async_can_connect_(struct bufferevent *bev)
{
const struct win32_extension_fns *ext =
- event_get_win32_extension_fns();
+ event_get_win32_extension_fns_();
if (BEV_IS_ASYNC(bev) &&
- event_base_get_iocp(bev->ev_base) &&
+ event_base_get_iocp_(bev->ev_base) &&
ext && ext->ConnectEx)
return 1;
@@ -605,14 +600,14 @@ bufferevent_async_can_connect(struct bufferevent *bev)
}
int
-bufferevent_async_connect(struct bufferevent *bev, evutil_socket_t fd,
+bufferevent_async_connect_(struct bufferevent *bev, evutil_socket_t fd,
const struct sockaddr *sa, int socklen)
{
BOOL rc;
struct bufferevent_async *bev_async = upcast(bev);
struct sockaddr_storage ss;
const struct win32_extension_fns *ext =
- event_get_win32_extension_fns();
+ event_get_win32_extension_fns_();
EVUTIL_ASSERT(ext && ext->ConnectEx && fd >= 0 && sa != NULL);
@@ -637,15 +632,15 @@ bufferevent_async_connect(struct bufferevent *bev, evutil_socket_t fd,
WSAGetLastError() != WSAEINVAL)
return -1;
- event_base_add_virtual(bev->ev_base);
- bufferevent_incref(bev);
+ event_base_add_virtual_(bev->ev_base);
+ bufferevent_incref_(bev);
rc = ext->ConnectEx(fd, sa, socklen, NULL, 0, NULL,
&bev_async->connect_overlapped.overlapped);
if (rc || WSAGetLastError() == ERROR_IO_PENDING)
return 0;
- event_base_del_virtual(bev->ev_base);
- bufferevent_decref(bev);
+ event_base_del_virtual_(bev->ev_base);
+ bufferevent_decref_(bev);
return -1;
}
@@ -656,27 +651,28 @@ be_async_ctrl(struct bufferevent *bev, enum bufferevent_ctrl_op op,
{
switch (op) {
case BEV_CTRL_GET_FD:
- data->fd = _evbuffer_overlapped_get_fd(bev->input);
+ data->fd = evbuffer_overlapped_get_fd_(bev->input);
return 0;
case BEV_CTRL_SET_FD: {
struct event_iocp_port *iocp;
- if (data->fd == _evbuffer_overlapped_get_fd(bev->input))
+ if (data->fd == evbuffer_overlapped_get_fd_(bev->input))
return 0;
- if (!(iocp = event_base_get_iocp(bev->ev_base)))
+ if (!(iocp = event_base_get_iocp_(bev->ev_base)))
return -1;
- if (event_iocp_port_associate(iocp, data->fd, 1) < 0)
+ if (event_iocp_port_associate_(iocp, data->fd, 1) < 0)
return -1;
- _evbuffer_overlapped_set_fd(bev->input, data->fd);
- _evbuffer_overlapped_set_fd(bev->output, data->fd);
+ evbuffer_overlapped_set_fd_(bev->input, data->fd);
+ evbuffer_overlapped_set_fd_(bev->output, data->fd);
return 0;
}
case BEV_CTRL_CANCEL_ALL: {
struct bufferevent_async *bev_a = upcast(bev);
- evutil_socket_t fd = _evbuffer_overlapped_get_fd(bev->input);
+ evutil_socket_t fd = evbuffer_overlapped_get_fd_(bev->input);
if (fd != (evutil_socket_t)INVALID_SOCKET &&
(bev_a->bev.options & BEV_OPT_CLOSE_ON_FREE)) {
closesocket(fd);
+ evbuffer_overlapped_set_fd_(bev->input, INVALID_SOCKET);
}
bev_a->ok = 0;
return 0;
diff --git a/bufferevent_filter.c b/bufferevent_filter.c
index 557f8cce..4d9be43e 100644
--- a/bufferevent_filter.c
+++ b/bufferevent_filter.c
@@ -26,11 +26,13 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "evconfig-private.h"
+
#include <sys/types.h>
#include "event2/event-config.h"
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -38,11 +40,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#ifdef _EVENT_HAVE_STDARG_H
+#ifdef EVENT__HAVE_STDARG_H
#include <stdarg.h>
#endif
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#endif
@@ -59,6 +61,7 @@
/* prototypes */
static int be_filter_enable(struct bufferevent *, short);
static int be_filter_disable(struct bufferevent *, short);
+static void be_filter_unlink(struct bufferevent *);
static void be_filter_destruct(struct bufferevent *);
static void be_filter_readcb(struct bufferevent *, void *);
@@ -97,8 +100,9 @@ const struct bufferevent_ops bufferevent_ops_filter = {
evutil_offsetof(struct bufferevent_filtered, bev.bev),
be_filter_enable,
be_filter_disable,
+ be_filter_unlink,
be_filter_destruct,
- _bufferevent_generic_adj_timeouts,
+ bufferevent_generic_adj_timeouts_,
be_filter_flush,
be_filter_ctrl,
};
@@ -180,13 +184,13 @@ bufferevent_filter_new(struct bufferevent *underlying,
if (!bufev_f)
return NULL;
- if (bufferevent_init_common(&bufev_f->bev, underlying->ev_base,
+ if (bufferevent_init_common_(&bufev_f->bev, underlying->ev_base,
&bufferevent_ops_filter, tmp_options) < 0) {
mm_free(bufev_f);
return NULL;
}
if (options & BEV_OPT_THREADSAFE) {
- bufferevent_enable_locking(downcast(bufev_f), NULL);
+ bufferevent_enable_locking_(downcast(bufev_f), NULL);
}
bufev_f->underlying = underlying;
@@ -202,25 +206,23 @@ bufferevent_filter_new(struct bufferevent *underlying,
bufev_f->outbuf_cb = evbuffer_add_cb(downcast(bufev_f)->output,
bufferevent_filtered_outbuf_cb, bufev_f);
- _bufferevent_init_generic_timeout_cbs(downcast(bufev_f));
- bufferevent_incref(underlying);
+ bufferevent_init_generic_timeout_cbs_(downcast(bufev_f));
+ bufferevent_incref_(underlying);
bufferevent_enable(underlying, EV_READ|EV_WRITE);
- bufferevent_suspend_read(underlying, BEV_SUSPEND_FILT_READ);
+ bufferevent_suspend_read_(underlying, BEV_SUSPEND_FILT_READ);
return downcast(bufev_f);
}
static void
-be_filter_destruct(struct bufferevent *bev)
+be_filter_unlink(struct bufferevent *bev)
{
struct bufferevent_filtered *bevf = upcast(bev);
EVUTIL_ASSERT(bevf);
- if (bevf->free_context)
- bevf->free_context(bevf->context);
if (bevf->bev.options & BEV_OPT_CLOSE_ON_FREE) {
- /* Yes, there is also a decref in bufferevent_decref.
+ /* Yes, there is also a decref in bufferevent_decref_.
* That decref corresponds to the incref when we set
* underlying for the first time. This decref is an
* extra one to remove the last reference.
@@ -236,12 +238,19 @@ be_filter_destruct(struct bufferevent *bev)
if (bevf->underlying->errorcb == be_filter_eventcb)
bufferevent_setcb(bevf->underlying,
NULL, NULL, NULL, NULL);
- bufferevent_unsuspend_read(bevf->underlying,
+ bufferevent_unsuspend_read_(bevf->underlying,
BEV_SUSPEND_FILT_READ);
}
}
+}
- _bufferevent_del_generic_timeout_cbs(bev);
+static void
+be_filter_destruct(struct bufferevent *bev)
+{
+ struct bufferevent_filtered *bevf = upcast(bev);
+ EVUTIL_ASSERT(bevf);
+ if (bevf->free_context)
+ bevf->free_context(bevf->context);
}
static int
@@ -253,7 +262,7 @@ be_filter_enable(struct bufferevent *bev, short event)
if (event & EV_READ) {
BEV_RESET_GENERIC_READ_TIMEOUT(bev);
- bufferevent_unsuspend_read(bevf->underlying,
+ bufferevent_unsuspend_read_(bevf->underlying,
BEV_SUSPEND_FILT_READ);
}
return 0;
@@ -267,7 +276,7 @@ be_filter_disable(struct bufferevent *bev, short event)
BEV_DEL_GENERIC_WRITE_TIMEOUT(bev);
if (event & EV_READ) {
BEV_DEL_GENERIC_READ_TIMEOUT(bev);
- bufferevent_suspend_read(bevf->underlying,
+ bufferevent_suspend_read_(bevf->underlying,
BEV_SUSPEND_FILT_READ);
}
return 0;
@@ -367,10 +376,9 @@ be_filter_process_output(struct bufferevent_filtered *bevf,
/* Or if we have filled the underlying output buffer. */
!be_underlying_writebuf_full(bevf,state));
- if (processed &&
- evbuffer_get_length(bufev->output) <= bufev->wm_write.low) {
+ if (processed) {
/* call the write callback.*/
- _bufferevent_run_writecb(bufev);
+ bufferevent_trigger_nolock_(bufev, EV_WRITE, 0);
if (res == BEV_OK &&
(bufev->enabled & EV_WRITE) &&
@@ -403,68 +411,94 @@ bufferevent_filtered_outbuf_cb(struct evbuffer *buf,
int processed_any = 0;
/* Somebody added more data to the output buffer. Try to
* process it, if we should. */
- _bufferevent_incref_and_lock(bev);
+ bufferevent_incref_and_lock_(bev);
be_filter_process_output(bevf, BEV_NORMAL, &processed_any);
- _bufferevent_decref_and_unlock(bev);
+ bufferevent_decref_and_unlock_(bev);
}
}
/* Called when the underlying socket has read. */
static void
-be_filter_readcb(struct bufferevent *underlying, void *_me)
+be_filter_readcb(struct bufferevent *underlying, void *me_)
{
- struct bufferevent_filtered *bevf = _me;
+ struct bufferevent_filtered *bevf = me_;
enum bufferevent_filter_result res;
enum bufferevent_flush_mode state;
struct bufferevent *bufev = downcast(bevf);
+ struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
int processed_any = 0;
- _bufferevent_incref_and_lock(bufev);
+ BEV_LOCK(bufev);
- if (bevf->got_eof)
- state = BEV_FINISHED;
- else
- state = BEV_NORMAL;
+ // It's possible our refcount is 0 at this point if another thread free'd our filterevent
+ EVUTIL_ASSERT(bufev_private->refcnt >= 0);
- /* XXXX use return value */
- res = be_filter_process_input(bevf, state, &processed_any);
- (void)res;
+ // If our refcount is > 0
+ if (bufev_private->refcnt > 0) {
- /* XXX This should be in process_input, not here. There are
- * other places that can call process-input, and they should
- * force readcb calls as needed. */
- if (processed_any &&
- evbuffer_get_length(bufev->input) >= bufev->wm_read.low)
- _bufferevent_run_readcb(bufev);
+ if (bevf->got_eof)
+ state = BEV_FINISHED;
+ else
+ state = BEV_NORMAL;
- _bufferevent_decref_and_unlock(bufev);
+ /* XXXX use return value */
+ res = be_filter_process_input(bevf, state, &processed_any);
+ (void)res;
+
+ /* XXX This should be in process_input, not here. There are
+ * other places that can call process-input, and they should
+ * force readcb calls as needed. */
+ if (processed_any)
+ bufferevent_trigger_nolock_(bufev, EV_READ, 0);
+ }
+
+ BEV_UNLOCK(bufev);
}
/* Called when the underlying socket has drained enough that we can write to
it. */
static void
-be_filter_writecb(struct bufferevent *underlying, void *_me)
+be_filter_writecb(struct bufferevent *underlying, void *me_)
{
- struct bufferevent_filtered *bevf = _me;
+ struct bufferevent_filtered *bevf = me_;
struct bufferevent *bev = downcast(bevf);
+ struct bufferevent_private *bufev_private = BEV_UPCAST(bev);
int processed_any = 0;
- _bufferevent_incref_and_lock(bev);
- be_filter_process_output(bevf, BEV_NORMAL, &processed_any);
- _bufferevent_decref_and_unlock(bev);
+ BEV_LOCK(bev);
+
+ // It's possible our refcount is 0 at this point if another thread free'd our filterevent
+ EVUTIL_ASSERT(bufev_private->refcnt >= 0);
+
+ // If our refcount is > 0
+ if (bufev_private->refcnt > 0) {
+ be_filter_process_output(bevf, BEV_NORMAL, &processed_any);
+ }
+
+ BEV_UNLOCK(bev);
}
/* Called when the underlying socket has given us an error */
static void
-be_filter_eventcb(struct bufferevent *underlying, short what, void *_me)
+be_filter_eventcb(struct bufferevent *underlying, short what, void *me_)
{
- struct bufferevent_filtered *bevf = _me;
+ struct bufferevent_filtered *bevf = me_;
struct bufferevent *bev = downcast(bevf);
+ struct bufferevent_private *bufev_private = BEV_UPCAST(bev);
+
+ BEV_LOCK(bev);
+
+ // It's possible our refcount is 0 at this point if another thread free'd our filterevent
+ EVUTIL_ASSERT(bufev_private->refcnt >= 0);
+
+ // If our refcount is > 0
+ if (bufev_private->refcnt > 0) {
+
+ /* All we can really to is tell our own eventcb. */
+ bufferevent_run_eventcb_(bev, what, 0);
+ }
- _bufferevent_incref_and_lock(bev);
- /* All we can really to is tell our own eventcb. */
- _bufferevent_run_eventcb(bev, what);
- _bufferevent_decref_and_unlock(bev);
+ BEV_UNLOCK(bev);
}
static int
@@ -475,7 +509,7 @@ be_filter_flush(struct bufferevent *bufev,
int processed_any = 0;
EVUTIL_ASSERT(bevf);
- _bufferevent_incref_and_lock(bufev);
+ bufferevent_incref_and_lock_(bufev);
if (iotype & EV_READ) {
be_filter_process_input(bevf, mode, &processed_any);
@@ -487,7 +521,7 @@ be_filter_flush(struct bufferevent *bufev,
/* XXX does this want to recursively call lower-level flushes? */
bufferevent_flush(bevf->underlying, iotype, mode);
- _bufferevent_decref_and_unlock(bufev);
+ bufferevent_decref_and_unlock_(bufev);
return processed_any;
}
diff --git a/bufferevent_openssl.c b/bufferevent_openssl.c
index 7582d9b4..b30f90ff 100644
--- a/bufferevent_openssl.c
+++ b/bufferevent_openssl.c
@@ -24,11 +24,17 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <sys/types.h>
+// Get rid of OSX 10.7 and greater deprecation warnings.
+#if defined(__APPLE__) && defined(__clang__)
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+#endif
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#include <sys/types.h>
+
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -36,14 +42,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#ifdef _EVENT_HAVE_STDARG_H
+#ifdef EVENT__HAVE_STDARG_H
#include <stdarg.h>
#endif
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#endif
@@ -312,7 +318,7 @@ struct bufferevent_openssl {
unsigned read_blocked_on_write : 1;
/* When we next get data, we should say "write" instead of "read". */
unsigned write_blocked_on_read : 1;
- /* XXX */
+ /* Treat TCP close before SSL close on SSL >= v3 as clean EOF. */
unsigned allow_dirty_shutdown : 1;
/* XXXX */
unsigned fd_is_set : 1;
@@ -325,6 +331,7 @@ struct bufferevent_openssl {
static int be_openssl_enable(struct bufferevent *, short);
static int be_openssl_disable(struct bufferevent *, short);
+static void be_openssl_unlink(struct bufferevent *);
static void be_openssl_destruct(struct bufferevent *);
static int be_openssl_adj_timeouts(struct bufferevent *);
static int be_openssl_flush(struct bufferevent *bufev,
@@ -336,6 +343,7 @@ const struct bufferevent_ops bufferevent_ops_openssl = {
evutil_offsetof(struct bufferevent_openssl, bev.bev),
be_openssl_enable,
be_openssl_disable,
+ be_openssl_unlink,
be_openssl_destruct,
be_openssl_adj_timeouts,
be_openssl_flush,
@@ -376,15 +384,15 @@ static int
start_reading(struct bufferevent_openssl *bev_ssl)
{
if (bev_ssl->underlying) {
- bufferevent_unsuspend_read(bev_ssl->underlying,
+ bufferevent_unsuspend_read_(bev_ssl->underlying,
BEV_SUSPEND_FILT_READ);
return 0;
} else {
struct bufferevent *bev = &bev_ssl->bev.bev;
int r;
- r = _bufferevent_add_event(&bev->ev_read, &bev->timeout_read);
+ r = bufferevent_add_event_(&bev->ev_read, &bev->timeout_read);
if (r == 0 && bev_ssl->read_blocked_on_write)
- r = _bufferevent_add_event(&bev->ev_write,
+ r = bufferevent_add_event_(&bev->ev_write,
&bev->timeout_write);
return r;
}
@@ -401,9 +409,9 @@ start_writing(struct bufferevent_openssl *bev_ssl)
;
} else {
struct bufferevent *bev = &bev_ssl->bev.bev;
- r = _bufferevent_add_event(&bev->ev_write, &bev->timeout_write);
+ r = bufferevent_add_event_(&bev->ev_write, &bev->timeout_write);
if (!r && bev_ssl->write_blocked_on_read)
- r = _bufferevent_add_event(&bev->ev_read,
+ r = bufferevent_add_event_(&bev->ev_read,
&bev->timeout_read);
}
return r;
@@ -415,7 +423,7 @@ stop_reading(struct bufferevent_openssl *bev_ssl)
if (bev_ssl->write_blocked_on_read)
return;
if (bev_ssl->underlying) {
- bufferevent_suspend_read(bev_ssl->underlying,
+ bufferevent_suspend_read_(bev_ssl->underlying,
BEV_SUSPEND_FILT_READ);
} else {
struct bufferevent *bev = &bev_ssl->bev.bev;
@@ -482,7 +490,7 @@ clear_wbor(struct bufferevent_openssl *bev_ssl)
}
static void
-conn_closed(struct bufferevent_openssl *bev_ssl, int errcode, int ret)
+conn_closed(struct bufferevent_openssl *bev_ssl, int when, int errcode, int ret)
{
int event = BEV_EVENT_ERROR;
int dirty_shutdown = 0;
@@ -528,7 +536,9 @@ conn_closed(struct bufferevent_openssl *bev_ssl, int errcode, int ret)
stop_reading(bev_ssl);
stop_writing(bev_ssl);
- _bufferevent_run_eventcb(&bev_ssl->bev.bev, event);
+ /* when is BEV_EVENT_{READING|WRITING} */
+ event = when | event;
+ bufferevent_run_eventcb_(&bev_ssl->bev.bev, event, 0);
}
static void
@@ -549,9 +559,9 @@ decrement_buckets(struct bufferevent_openssl *bev_ssl)
unsigned long w = num_w - bev_ssl->counts.n_written;
unsigned long r = num_r - bev_ssl->counts.n_read;
if (w)
- _bufferevent_decrement_write_buckets(&bev_ssl->bev, w);
+ bufferevent_decrement_write_buckets_(&bev_ssl->bev, w);
if (r)
- _bufferevent_decrement_read_buckets(&bev_ssl->bev, r);
+ bufferevent_decrement_read_buckets_(&bev_ssl->bev, r);
bev_ssl->counts.n_written = num_w;
bev_ssl->counts.n_read = num_r;
}
@@ -574,7 +584,7 @@ do_read(struct bufferevent_openssl *bev_ssl, int n_to_read) {
if (bev_ssl->bev.read_suspended)
return 0;
- atmost = _bufferevent_get_read_max(&bev_ssl->bev);
+ atmost = bufferevent_get_read_max_(&bev_ssl->bev);
if (n_to_read > atmost)
n_to_read = atmost;
@@ -612,7 +622,7 @@ do_read(struct bufferevent_openssl *bev_ssl, int n_to_read) {
return OP_ERR | result;
break;
default:
- conn_closed(bev_ssl, err, r);
+ conn_closed(bev_ssl, BEV_EVENT_READING, err, r);
break;
}
result |= OP_BLOCKED;
@@ -643,7 +653,7 @@ do_write(struct bufferevent_openssl *bev_ssl, int atmost)
if (bev_ssl->last_write > 0)
atmost = bev_ssl->last_write;
else
- atmost = _bufferevent_get_write_max(&bev_ssl->bev);
+ atmost = bufferevent_get_write_max_(&bev_ssl->bev);
n = evbuffer_peek(output, atmost, NULL, space, 8);
if (n < 0)
@@ -691,7 +701,7 @@ do_write(struct bufferevent_openssl *bev_ssl, int atmost)
bev_ssl->last_write = space[i].iov_len;
break;
default:
- conn_closed(bev_ssl, err, r);
+ conn_closed(bev_ssl, BEV_EVENT_WRITING, err, r);
bev_ssl->last_write = -1;
break;
}
@@ -704,8 +714,7 @@ do_write(struct bufferevent_openssl *bev_ssl, int atmost)
if (bev_ssl->underlying)
BEV_RESET_GENERIC_WRITE_TIMEOUT(bev);
- if (evbuffer_get_length(output) <= bev->wm_write.low)
- _bufferevent_run_writecb(bev);
+ bufferevent_trigger_nolock_(bev, EV_WRITE, 0);
}
return result;
}
@@ -749,7 +758,7 @@ bytes_to_read(struct bufferevent_openssl *bev)
}
/* Respect the rate limit */
- limit = _bufferevent_get_read_max(&bev->bev);
+ limit = bufferevent_get_read_max_(&bev->bev);
if (result > limit) {
result = limit;
}
@@ -819,11 +828,8 @@ consider_reading(struct bufferevent_openssl *bev_ssl)
if (all_result_flags & OP_MADE_PROGRESS) {
struct bufferevent *bev = &bev_ssl->bev.bev;
- struct evbuffer *input = bev->input;
- if (evbuffer_get_length(input) >= bev->wm_read.low) {
- _bufferevent_run_readcb(bev);
- }
+ bufferevent_trigger_nolock_(bev, EV_READ, 0);
}
if (!bev_ssl->underlying) {
@@ -847,11 +853,8 @@ consider_writing(struct bufferevent_openssl *bev_ssl)
r = do_read(bev_ssl, 1024); /* XXXX 1024 is a hack */
if (r & OP_MADE_PROGRESS) {
struct bufferevent *bev = &bev_ssl->bev.bev;
- struct evbuffer *input = bev->input;
- if (evbuffer_get_length(input) >= bev->wm_read.low) {
- _bufferevent_run_readcb(bev);
- }
+ bufferevent_trigger_nolock_(bev, EV_READ, 0);
}
if (r & (OP_ERR|OP_BLOCKED))
break;
@@ -923,35 +926,35 @@ be_openssl_eventcb(struct bufferevent *bev_base, short what, void *ctx)
eat it. */
}
if (event)
- _bufferevent_run_eventcb(&bev_ssl->bev.bev, event);
+ bufferevent_run_eventcb_(&bev_ssl->bev.bev, event, 0);
}
static void
be_openssl_readeventcb(evutil_socket_t fd, short what, void *ptr)
{
struct bufferevent_openssl *bev_ssl = ptr;
- _bufferevent_incref_and_lock(&bev_ssl->bev.bev);
+ bufferevent_incref_and_lock_(&bev_ssl->bev.bev);
if (what == EV_TIMEOUT) {
- _bufferevent_run_eventcb(&bev_ssl->bev.bev,
- BEV_EVENT_TIMEOUT|BEV_EVENT_READING);
+ bufferevent_run_eventcb_(&bev_ssl->bev.bev,
+ BEV_EVENT_TIMEOUT|BEV_EVENT_READING, 0);
} else {
consider_reading(bev_ssl);
}
- _bufferevent_decref_and_unlock(&bev_ssl->bev.bev);
+ bufferevent_decref_and_unlock_(&bev_ssl->bev.bev);
}
static void
be_openssl_writeeventcb(evutil_socket_t fd, short what, void *ptr)
{
struct bufferevent_openssl *bev_ssl = ptr;
- _bufferevent_incref_and_lock(&bev_ssl->bev.bev);
+ bufferevent_incref_and_lock_(&bev_ssl->bev.bev);
if (what == EV_TIMEOUT) {
- _bufferevent_run_eventcb(&bev_ssl->bev.bev,
- BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING);
+ bufferevent_run_eventcb_(&bev_ssl->bev.bev,
+ BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING, 0);
} else {
consider_writing(bev_ssl);
}
- _bufferevent_decref_and_unlock(&bev_ssl->bev.bev);
+ bufferevent_decref_and_unlock_(&bev_ssl->bev.bev);
}
static int
@@ -974,13 +977,15 @@ set_open_callbacks(struct bufferevent_openssl *bev_ssl, evutil_socket_t fd)
event_del(&bev->ev_write);
}
event_assign(&bev->ev_read, bev->ev_base, fd,
- EV_READ|EV_PERSIST, be_openssl_readeventcb, bev_ssl);
+ EV_READ|EV_PERSIST|EV_FINALIZE,
+ be_openssl_readeventcb, bev_ssl);
event_assign(&bev->ev_write, bev->ev_base, fd,
- EV_WRITE|EV_PERSIST, be_openssl_writeeventcb, bev_ssl);
+ EV_WRITE|EV_PERSIST|EV_FINALIZE,
+ be_openssl_writeeventcb, bev_ssl);
if (rpending)
- r1 = _bufferevent_add_event(&bev->ev_read, &bev->timeout_read);
+ r1 = bufferevent_add_event_(&bev->ev_read, &bev->timeout_read);
if (wpending)
- r2 = _bufferevent_add_event(&bev->ev_write, &bev->timeout_write);
+ r2 = bufferevent_add_event_(&bev->ev_write, &bev->timeout_write);
if (fd >= 0) {
bev_ssl->fd_is_set = 1;
}
@@ -1011,8 +1016,8 @@ do_handshake(struct bufferevent_openssl *bev_ssl)
set_open_callbacks(bev_ssl, -1); /* XXXX handle failure */
/* Call do_read and do_write as needed */
bufferevent_enable(&bev_ssl->bev.bev, bev_ssl->bev.bev.enabled);
- _bufferevent_run_eventcb(&bev_ssl->bev.bev,
- BEV_EVENT_CONNECTED);
+ bufferevent_run_eventcb_(&bev_ssl->bev.bev,
+ BEV_EVENT_CONNECTED, 0);
return 1;
} else {
int err = SSL_get_error(bev_ssl->ssl, r);
@@ -1031,7 +1036,7 @@ do_handshake(struct bufferevent_openssl *bev_ssl)
}
return 0;
default:
- conn_closed(bev_ssl, err, r);
+ conn_closed(bev_ssl, BEV_EVENT_READING, err, r);
return -1;
}
}
@@ -1049,12 +1054,12 @@ be_openssl_handshakeeventcb(evutil_socket_t fd, short what, void *ptr)
{
struct bufferevent_openssl *bev_ssl = ptr;
- _bufferevent_incref_and_lock(&bev_ssl->bev.bev);
+ bufferevent_incref_and_lock_(&bev_ssl->bev.bev);
if (what & EV_TIMEOUT) {
- _bufferevent_run_eventcb(&bev_ssl->bev.bev, BEV_EVENT_TIMEOUT);
+ bufferevent_run_eventcb_(&bev_ssl->bev.bev, BEV_EVENT_TIMEOUT, 0);
} else
do_handshake(bev_ssl);/* XXX handle failure */
- _bufferevent_decref_and_unlock(&bev_ssl->bev.bev);
+ bufferevent_decref_and_unlock_(&bev_ssl->bev.bev);
}
static int
@@ -1076,12 +1081,14 @@ set_handshake_callbacks(struct bufferevent_openssl *bev_ssl, evutil_socket_t fd)
event_del(&bev->ev_write);
}
event_assign(&bev->ev_read, bev->ev_base, fd,
- EV_READ|EV_PERSIST, be_openssl_handshakeeventcb, bev_ssl);
+ EV_READ|EV_PERSIST|EV_FINALIZE,
+ be_openssl_handshakeeventcb, bev_ssl);
event_assign(&bev->ev_write, bev->ev_base, fd,
- EV_WRITE|EV_PERSIST, be_openssl_handshakeeventcb, bev_ssl);
+ EV_WRITE|EV_PERSIST|EV_FINALIZE,
+ be_openssl_handshakeeventcb, bev_ssl);
if (fd >= 0) {
- r1 = _bufferevent_add_event(&bev->ev_read, &bev->timeout_read);
- r2 = _bufferevent_add_event(&bev->ev_write, &bev->timeout_write);
+ r1 = bufferevent_add_event_(&bev->ev_read, &bev->timeout_read);
+ r2 = bufferevent_add_event_(&bev->ev_write, &bev->timeout_write);
bev_ssl->fd_is_set = 1;
}
return (r1<0 || r2<0) ? -1 : 0;
@@ -1114,7 +1121,7 @@ be_openssl_outbuf_cb(struct evbuffer *buf,
if (cbinfo->n_added && bev_ssl->state == BUFFEREVENT_SSL_OPEN) {
if (cbinfo->orig_size == 0)
- r = _bufferevent_add_event(&bev_ssl->bev.bev.ev_write,
+ r = bufferevent_add_event_(&bev_ssl->bev.bev.ev_write,
&bev_ssl->bev.bev.timeout_write);
consider_writing(bev_ssl);
}
@@ -1173,17 +1180,10 @@ be_openssl_disable(struct bufferevent *bev, short events)
}
static void
-be_openssl_destruct(struct bufferevent *bev)
+be_openssl_unlink(struct bufferevent *bev)
{
struct bufferevent_openssl *bev_ssl = upcast(bev);
- if (bev_ssl->underlying) {
- _bufferevent_del_generic_timeout_cbs(bev);
- } else {
- event_del(&bev->ev_read);
- event_del(&bev->ev_write);
- }
-
if (bev_ssl->bev.options & BEV_OPT_CLOSE_ON_FREE) {
if (bev_ssl->underlying) {
if (BEV_UPCAST(bev_ssl->underlying)->refcnt < 2) {
@@ -1191,41 +1191,64 @@ be_openssl_destruct(struct bufferevent *bev)
"bufferevent with too few references");
} else {
bufferevent_free(bev_ssl->underlying);
- bev_ssl->underlying = NULL;
+ /* We still have a reference to it, via our
+ * BIO. So we don't drop this. */
+ // bev_ssl->underlying = NULL;
}
- } else {
- evutil_socket_t fd = -1;
- BIO *bio = SSL_get_wbio(bev_ssl->ssl);
- if (bio)
- fd = BIO_get_fd(bio, NULL);
- if (fd >= 0)
- evutil_closesocket(fd);
}
- SSL_free(bev_ssl->ssl);
} else {
if (bev_ssl->underlying) {
if (bev_ssl->underlying->errorcb == be_openssl_eventcb)
bufferevent_setcb(bev_ssl->underlying,
NULL,NULL,NULL,NULL);
- bufferevent_unsuspend_read(bev_ssl->underlying,
+ bufferevent_unsuspend_read_(bev_ssl->underlying,
BEV_SUSPEND_FILT_READ);
}
}
}
+static void
+be_openssl_destruct(struct bufferevent *bev)
+{
+ struct bufferevent_openssl *bev_ssl = upcast(bev);
+
+ if (bev_ssl->bev.options & BEV_OPT_CLOSE_ON_FREE) {
+ if (! bev_ssl->underlying) {
+ evutil_socket_t fd = -1;
+ BIO *bio = SSL_get_wbio(bev_ssl->ssl);
+ if (bio)
+ fd = BIO_get_fd(bio, NULL);
+ if (fd >= 0)
+ evutil_closesocket(fd);
+ }
+ SSL_free(bev_ssl->ssl);
+ }
+}
+
static int
be_openssl_adj_timeouts(struct bufferevent *bev)
{
struct bufferevent_openssl *bev_ssl = upcast(bev);
- if (bev_ssl->underlying)
- return _bufferevent_generic_adj_timeouts(bev);
- else {
+ if (bev_ssl->underlying) {
+ return bufferevent_generic_adj_timeouts_(bev);
+ } else {
int r1=0, r2=0;
- if (event_pending(&bev->ev_read, EV_READ, NULL))
- r1 = _bufferevent_add_event(&bev->ev_read, &bev->timeout_read);
- if (event_pending(&bev->ev_write, EV_WRITE, NULL))
- r2 = _bufferevent_add_event(&bev->ev_write, &bev->timeout_write);
+ if (event_pending(&bev->ev_read, EV_READ, NULL)) {
+ if (evutil_timerisset(&bev->timeout_read)) {
+ r1 = bufferevent_add_event_(&bev->ev_read, &bev->timeout_read);
+ } else {
+ event_remove_timer(&bev->ev_read);
+ }
+ }
+ if (event_pending(&bev->ev_write, EV_WRITE, NULL)) {
+ if (evutil_timerisset(&bev->timeout_write)) {
+ r2 = bufferevent_add_event_(&bev->ev_write, &bev->timeout_write);
+ } else {
+ event_remove_timer(&bev->ev_write);
+ }
+ }
+
return (r1<0 || r2<0) ? -1 : 0;
}
}
@@ -1253,6 +1276,8 @@ be_openssl_ctrl(struct bufferevent *bev,
SSL_set_bio(bev_ssl->ssl, bio, bio);
bev_ssl->fd_is_set = 1;
}
+ if (data->fd == -1)
+ bev_ssl->fd_is_set = 0;
if (bev_ssl->state == BUFFEREVENT_SSL_OPEN)
return set_open_callbacks(bev_ssl, data->fd);
else {
@@ -1305,7 +1330,7 @@ bufferevent_openssl_new_impl(struct event_base *base,
bev_p = &bev_ssl->bev;
- if (bufferevent_init_common(bev_p, base,
+ if (bufferevent_init_common_(bev_p, base,
&bufferevent_ops_openssl, tmp_options) < 0)
goto err;
@@ -1320,11 +1345,11 @@ bufferevent_openssl_new_impl(struct event_base *base,
be_openssl_outbuf_cb, bev_ssl);
if (options & BEV_OPT_THREADSAFE)
- bufferevent_enable_locking(&bev_ssl->bev.bev, NULL);
+ bufferevent_enable_locking_(&bev_ssl->bev.bev, NULL);
if (underlying) {
- _bufferevent_init_generic_timeout_cbs(&bev_ssl->bev.bev);
- bufferevent_incref(underlying);
+ bufferevent_init_generic_timeout_cbs_(&bev_ssl->bev.bev);
+ bufferevent_incref_(underlying);
}
bev_ssl->state = state;
@@ -1355,7 +1380,7 @@ bufferevent_openssl_new_impl(struct event_base *base,
bufferevent_setwatermark(underlying, EV_READ, 0, 0);
bufferevent_enable(underlying, EV_READ|EV_WRITE);
if (state == BUFFEREVENT_SSL_OPEN)
- bufferevent_suspend_read(underlying,
+ bufferevent_suspend_read_(underlying,
BEV_SUSPEND_FILT_READ);
} else {
bev_ssl->bev.bev.enabled = EV_READ|EV_WRITE;
@@ -1439,6 +1464,31 @@ bufferevent_openssl_socket_new(struct event_base *base,
base, NULL, fd, ssl, state, options);
}
+int
+bufferevent_openssl_get_allow_dirty_shutdown(struct bufferevent *bev)
+{
+ int allow_dirty_shutdown = -1;
+ struct bufferevent_openssl *bev_ssl;
+ BEV_LOCK(bev);
+ bev_ssl = upcast(bev);
+ if (bev_ssl)
+ allow_dirty_shutdown = bev_ssl->allow_dirty_shutdown;
+ BEV_UNLOCK(bev);
+ return allow_dirty_shutdown;
+}
+
+void
+bufferevent_openssl_set_allow_dirty_shutdown(struct bufferevent *bev,
+ int allow_dirty_shutdown)
+{
+ struct bufferevent_openssl *bev_ssl;
+ BEV_LOCK(bev);
+ bev_ssl = upcast(bev);
+ if (bev_ssl)
+ bev_ssl->allow_dirty_shutdown = !!allow_dirty_shutdown;
+ BEV_UNLOCK(bev);
+}
+
unsigned long
bufferevent_get_openssl_error(struct bufferevent *bev)
{
diff --git a/bufferevent_pair.c b/bufferevent_pair.c
index e9ed9f59..4340f237 100644
--- a/bufferevent_pair.c
+++ b/bufferevent_pair.c
@@ -23,15 +23,15 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "event2/event-config.h"
+#include "evconfig-private.h"
#include <sys/types.h>
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#endif
-#include "event2/event-config.h"
-
#include "event2/util.h"
#include "event2/buffer.h"
#include "event2/bufferevent.h"
@@ -67,10 +67,10 @@ static inline void
incref_and_lock(struct bufferevent *b)
{
struct bufferevent_pair *bevp;
- _bufferevent_incref_and_lock(b);
+ bufferevent_incref_and_lock_(b);
bevp = upcast(b);
if (bevp->partner)
- _bufferevent_incref_and_lock(downcast(bevp->partner));
+ bufferevent_incref_and_lock_(downcast(bevp->partner));
}
static inline void
@@ -78,8 +78,8 @@ decref_and_unlock(struct bufferevent *b)
{
struct bufferevent_pair *bevp = upcast(b);
if (bevp->partner)
- _bufferevent_decref_and_unlock(downcast(bevp->partner));
- _bufferevent_decref_and_unlock(b);
+ bufferevent_decref_and_unlock_(downcast(bevp->partner));
+ bufferevent_decref_and_unlock_(b);
}
/* XXX Handle close */
@@ -94,7 +94,7 @@ bufferevent_pair_elt_new(struct event_base *base,
struct bufferevent_pair *bufev;
if (! (bufev = mm_calloc(1, sizeof(struct bufferevent_pair))))
return NULL;
- if (bufferevent_init_common(&bufev->bev, base, &bufferevent_ops_pair,
+ if (bufferevent_init_common_(&bufev->bev, base, &bufferevent_ops_pair,
options)) {
mm_free(bufev);
return NULL;
@@ -104,7 +104,7 @@ bufferevent_pair_elt_new(struct event_base *base,
return NULL;
}
- _bufferevent_init_generic_timeout_cbs(&bufev->bev.bev);
+ bufferevent_init_generic_timeout_cbs_(&bufev->bev.bev);
return bufev;
}
@@ -130,7 +130,7 @@ bufferevent_pair_new(struct event_base *base, int options,
if (options & BEV_OPT_THREADSAFE) {
/*XXXX check return */
- bufferevent_enable_locking(downcast(bufev2), bufev1->bev.lock);
+ bufferevent_enable_locking_(downcast(bufev2), bufev1->bev.lock);
}
bufev1->partner = bufev2;
@@ -151,7 +151,7 @@ static void
be_pair_transfer(struct bufferevent *src, struct bufferevent *dst,
int ignore_wm)
{
- size_t src_size, dst_size;
+ size_t dst_size;
size_t n;
evbuffer_unfreeze(src->output, 1);
@@ -182,15 +182,8 @@ be_pair_transfer(struct bufferevent *src, struct bufferevent *dst,
BEV_DEL_GENERIC_WRITE_TIMEOUT(dst);
}
- src_size = evbuffer_get_length(src->output);
- dst_size = evbuffer_get_length(dst->input);
-
- if (dst_size >= dst->wm_read.low) {
- _bufferevent_run_readcb(dst);
- }
- if (src_size <= src->wm_write.low) {
- _bufferevent_run_writecb(src);
- }
+ bufferevent_trigger_nolock_(dst, EV_READ, 0);
+ bufferevent_trigger_nolock_(src, EV_WRITE, 0);
done:
evbuffer_freeze(src->output, 1);
evbuffer_freeze(dst->input, 0);
@@ -260,13 +253,14 @@ be_pair_disable(struct bufferevent *bev, short events)
if (events & EV_READ) {
BEV_DEL_GENERIC_READ_TIMEOUT(bev);
}
- if (events & EV_WRITE)
+ if (events & EV_WRITE) {
BEV_DEL_GENERIC_WRITE_TIMEOUT(bev);
+ }
return 0;
}
static void
-be_pair_destruct(struct bufferevent *bev)
+be_pair_unlink(struct bufferevent *bev)
{
struct bufferevent_pair *bev_p = upcast(bev);
@@ -274,8 +268,6 @@ be_pair_destruct(struct bufferevent *bev)
bev_p->partner->partner = NULL;
bev_p->partner = NULL;
}
-
- _bufferevent_del_generic_timeout_cbs(bev);
}
static int
@@ -300,7 +292,7 @@ be_pair_flush(struct bufferevent *bev, short iotype,
be_pair_transfer(bev, partner, 1);
if (mode == BEV_FINISHED) {
- _bufferevent_run_eventcb(partner, iotype|BEV_EVENT_EOF);
+ bufferevent_run_eventcb_(partner, iotype|BEV_EVENT_EOF, 0);
}
decref_and_unlock(bev);
return 0;
@@ -327,8 +319,9 @@ const struct bufferevent_ops bufferevent_ops_pair = {
evutil_offsetof(struct bufferevent_pair, bev.bev),
be_pair_enable,
be_pair_disable,
- be_pair_destruct,
- _bufferevent_generic_adj_timeouts,
+ be_pair_unlink,
+ NULL, /* be_pair_destruct, */
+ bufferevent_generic_adj_timeouts_,
be_pair_flush,
NULL, /* ctrl */
};
diff --git a/bufferevent_ratelim.c b/bufferevent_ratelim.c
index 7965390e..bde19202 100644
--- a/bufferevent_ratelim.c
+++ b/bufferevent_ratelim.c
@@ -25,6 +25,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "evconfig-private.h"
#include <sys/types.h>
#include <limits.h>
@@ -46,7 +47,7 @@
#include "event-internal.h"
int
-ev_token_bucket_init(struct ev_token_bucket *bucket,
+ev_token_bucket_init_(struct ev_token_bucket *bucket,
const struct ev_token_bucket_cfg *cfg,
ev_uint32_t current_tick,
int reinitialize)
@@ -70,7 +71,7 @@ ev_token_bucket_init(struct ev_token_bucket *bucket,
}
int
-ev_token_bucket_update(struct ev_token_bucket *bucket,
+ev_token_bucket_update_(struct ev_token_bucket *bucket,
const struct ev_token_bucket_cfg *cfg,
ev_uint32_t current_tick)
{
@@ -116,14 +117,14 @@ bufferevent_update_buckets(struct bufferevent_private *bev)
struct timeval now;
unsigned tick;
event_base_gettimeofday_cached(bev->bev.ev_base, &now);
- tick = ev_token_bucket_get_tick(&now, bev->rate_limiting->cfg);
+ tick = ev_token_bucket_get_tick_(&now, bev->rate_limiting->cfg);
if (tick != bev->rate_limiting->limit.last_updated)
- ev_token_bucket_update(&bev->rate_limiting->limit,
+ ev_token_bucket_update_(&bev->rate_limiting->limit,
bev->rate_limiting->cfg, tick);
}
ev_uint32_t
-ev_token_bucket_get_tick(const struct timeval *tv,
+ev_token_bucket_get_tick_(const struct timeval *tv,
const struct ev_token_bucket_cfg *cfg)
{
/* This computation uses two multiplies and a divide. We could do
@@ -177,30 +178,27 @@ ev_token_bucket_cfg_free(struct ev_token_bucket_cfg *cfg)
mm_free(cfg);
}
-/* No matter how big our bucket gets, don't try to read more than this
- * much in a single read operation. */
-#define MAX_TO_READ_EVER 16384
-/* No matter how big our bucket gets, don't try to write more than this
- * much in a single write operation. */
-#define MAX_TO_WRITE_EVER 16384
+/* Default values for max_single_read & max_single_write variables. */
+#define MAX_SINGLE_READ_DEFAULT 16384
+#define MAX_SINGLE_WRITE_DEFAULT 16384
#define LOCK_GROUP(g) EVLOCK_LOCK((g)->lock, 0)
#define UNLOCK_GROUP(g) EVLOCK_UNLOCK((g)->lock, 0)
-static int _bev_group_suspend_reading(struct bufferevent_rate_limit_group *g);
-static int _bev_group_suspend_writing(struct bufferevent_rate_limit_group *g);
-static void _bev_group_unsuspend_reading(struct bufferevent_rate_limit_group *g);
-static void _bev_group_unsuspend_writing(struct bufferevent_rate_limit_group *g);
+static int bev_group_suspend_reading_(struct bufferevent_rate_limit_group *g);
+static int bev_group_suspend_writing_(struct bufferevent_rate_limit_group *g);
+static void bev_group_unsuspend_reading_(struct bufferevent_rate_limit_group *g);
+static void bev_group_unsuspend_writing_(struct bufferevent_rate_limit_group *g);
/** Helper: figure out the maximum amount we should write if is_write, or
the maximum amount we should read if is_read. Return that maximum, or
0 if our bucket is wholly exhausted.
*/
static inline ev_ssize_t
-_bufferevent_get_rlim_max(struct bufferevent_private *bev, int is_write)
+bufferevent_get_rlim_max_(struct bufferevent_private *bev, int is_write)
{
/* needs lock on bev. */
- ev_ssize_t max_so_far = is_write?MAX_TO_WRITE_EVER:MAX_TO_READ_EVER;
+ ev_ssize_t max_so_far = is_write?bev->max_single_write:bev->max_single_read;
#define LIM(x) \
(is_write ? (x).write_limit : (x).read_limit)
@@ -237,10 +235,10 @@ _bufferevent_get_rlim_max(struct bufferevent_private *bev, int is_write)
* particular bufferevent while suspending the whole
* group. */
if (is_write)
- bufferevent_suspend_write(&bev->bev,
+ bufferevent_suspend_write_(&bev->bev,
BEV_SUSPEND_BW_GROUP);
else
- bufferevent_suspend_read(&bev->bev,
+ bufferevent_suspend_read_(&bev->bev,
BEV_SUSPEND_BW_GROUP);
share = 0;
} else {
@@ -260,19 +258,19 @@ _bufferevent_get_rlim_max(struct bufferevent_private *bev, int is_write)
}
ev_ssize_t
-_bufferevent_get_read_max(struct bufferevent_private *bev)
+bufferevent_get_read_max_(struct bufferevent_private *bev)
{
- return _bufferevent_get_rlim_max(bev, 0);
+ return bufferevent_get_rlim_max_(bev, 0);
}
ev_ssize_t
-_bufferevent_get_write_max(struct bufferevent_private *bev)
+bufferevent_get_write_max_(struct bufferevent_private *bev)
{
- return _bufferevent_get_rlim_max(bev, 1);
+ return bufferevent_get_rlim_max_(bev, 1);
}
int
-_bufferevent_decrement_read_buckets(struct bufferevent_private *bev, ev_ssize_t bytes)
+bufferevent_decrement_read_buckets_(struct bufferevent_private *bev, ev_ssize_t bytes)
{
/* XXXXX Make sure all users of this function check its return value */
int r = 0;
@@ -283,14 +281,14 @@ _bufferevent_decrement_read_buckets(struct bufferevent_private *bev, ev_ssize_t
if (bev->rate_limiting->cfg) {
bev->rate_limiting->limit.read_limit -= bytes;
if (bev->rate_limiting->limit.read_limit <= 0) {
- bufferevent_suspend_read(&bev->bev, BEV_SUSPEND_BW);
+ bufferevent_suspend_read_(&bev->bev, BEV_SUSPEND_BW);
if (event_add(&bev->rate_limiting->refill_bucket_event,
&bev->rate_limiting->cfg->tick_timeout) < 0)
r = -1;
} else if (bev->read_suspended & BEV_SUSPEND_BW) {
if (!(bev->write_suspended & BEV_SUSPEND_BW))
event_del(&bev->rate_limiting->refill_bucket_event);
- bufferevent_unsuspend_read(&bev->bev, BEV_SUSPEND_BW);
+ bufferevent_unsuspend_read_(&bev->bev, BEV_SUSPEND_BW);
}
}
@@ -299,9 +297,9 @@ _bufferevent_decrement_read_buckets(struct bufferevent_private *bev, ev_ssize_t
bev->rate_limiting->group->rate_limit.read_limit -= bytes;
bev->rate_limiting->group->total_read += bytes;
if (bev->rate_limiting->group->rate_limit.read_limit <= 0) {
- _bev_group_suspend_reading(bev->rate_limiting->group);
+ bev_group_suspend_reading_(bev->rate_limiting->group);
} else if (bev->rate_limiting->group->read_suspended) {
- _bev_group_unsuspend_reading(bev->rate_limiting->group);
+ bev_group_unsuspend_reading_(bev->rate_limiting->group);
}
UNLOCK_GROUP(bev->rate_limiting->group);
}
@@ -310,7 +308,7 @@ _bufferevent_decrement_read_buckets(struct bufferevent_private *bev, ev_ssize_t
}
int
-_bufferevent_decrement_write_buckets(struct bufferevent_private *bev, ev_ssize_t bytes)
+bufferevent_decrement_write_buckets_(struct bufferevent_private *bev, ev_ssize_t bytes)
{
/* XXXXX Make sure all users of this function check its return value */
int r = 0;
@@ -321,14 +319,14 @@ _bufferevent_decrement_write_buckets(struct bufferevent_private *bev, ev_ssize_t
if (bev->rate_limiting->cfg) {
bev->rate_limiting->limit.write_limit -= bytes;
if (bev->rate_limiting->limit.write_limit <= 0) {
- bufferevent_suspend_write(&bev->bev, BEV_SUSPEND_BW);
+ bufferevent_suspend_write_(&bev->bev, BEV_SUSPEND_BW);
if (event_add(&bev->rate_limiting->refill_bucket_event,
&bev->rate_limiting->cfg->tick_timeout) < 0)
r = -1;
} else if (bev->write_suspended & BEV_SUSPEND_BW) {
if (!(bev->read_suspended & BEV_SUSPEND_BW))
event_del(&bev->rate_limiting->refill_bucket_event);
- bufferevent_unsuspend_write(&bev->bev, BEV_SUSPEND_BW);
+ bufferevent_unsuspend_write_(&bev->bev, BEV_SUSPEND_BW);
}
}
@@ -337,9 +335,9 @@ _bufferevent_decrement_write_buckets(struct bufferevent_private *bev, ev_ssize_t
bev->rate_limiting->group->rate_limit.write_limit -= bytes;
bev->rate_limiting->group->total_written += bytes;
if (bev->rate_limiting->group->rate_limit.write_limit <= 0) {
- _bev_group_suspend_writing(bev->rate_limiting->group);
+ bev_group_suspend_writing_(bev->rate_limiting->group);
} else if (bev->rate_limiting->group->write_suspended) {
- _bev_group_unsuspend_writing(bev->rate_limiting->group);
+ bev_group_unsuspend_writing_(bev->rate_limiting->group);
}
UNLOCK_GROUP(bev->rate_limiting->group);
}
@@ -349,22 +347,22 @@ _bufferevent_decrement_write_buckets(struct bufferevent_private *bev, ev_ssize_t
/** Stop reading on every bufferevent in <b>g</b> */
static int
-_bev_group_suspend_reading(struct bufferevent_rate_limit_group *g)
+bev_group_suspend_reading_(struct bufferevent_rate_limit_group *g)
{
/* Needs group lock */
struct bufferevent_private *bev;
g->read_suspended = 1;
g->pending_unsuspend_read = 0;
- /* Note that in this loop we call EVLOCK_TRY_LOCK instead of BEV_LOCK,
+ /* Note that in this loop we call EVLOCK_TRY_LOCK_ instead of BEV_LOCK,
to prevent a deadlock. (Ordinarily, the group lock nests inside
the bufferevent locks. If we are unable to lock any individual
bufferevent, it will find out later when it looks at its limit
- and sees that its group is suspended.
+ and sees that its group is suspended.)
*/
- TAILQ_FOREACH(bev, &g->members, rate_limiting->next_in_group) {
- if (EVLOCK_TRY_LOCK(bev->lock)) {
- bufferevent_suspend_read(&bev->bev,
+ LIST_FOREACH(bev, &g->members, rate_limiting->next_in_group) {
+ if (EVLOCK_TRY_LOCK_(bev->lock)) {
+ bufferevent_suspend_read_(&bev->bev,
BEV_SUSPEND_BW_GROUP);
EVLOCK_UNLOCK(bev->lock, 0);
}
@@ -374,15 +372,15 @@ _bev_group_suspend_reading(struct bufferevent_rate_limit_group *g)
/** Stop writing on every bufferevent in <b>g</b> */
static int
-_bev_group_suspend_writing(struct bufferevent_rate_limit_group *g)
+bev_group_suspend_writing_(struct bufferevent_rate_limit_group *g)
{
/* Needs group lock */
struct bufferevent_private *bev;
g->write_suspended = 1;
g->pending_unsuspend_write = 0;
- TAILQ_FOREACH(bev, &g->members, rate_limiting->next_in_group) {
- if (EVLOCK_TRY_LOCK(bev->lock)) {
- bufferevent_suspend_write(&bev->bev,
+ LIST_FOREACH(bev, &g->members, rate_limiting->next_in_group) {
+ if (EVLOCK_TRY_LOCK_(bev->lock)) {
+ bufferevent_suspend_write_(&bev->bev,
BEV_SUSPEND_BW_GROUP);
EVLOCK_UNLOCK(bev->lock, 0);
}
@@ -393,7 +391,7 @@ _bev_group_suspend_writing(struct bufferevent_rate_limit_group *g)
/** Timer callback invoked on a single bufferevent with one or more exhausted
buckets when they are ready to refill. */
static void
-_bev_refill_callback(evutil_socket_t fd, short what, void *arg)
+bev_refill_callback_(evutil_socket_t fd, short what, void *arg)
{
unsigned tick;
struct timeval now;
@@ -407,22 +405,22 @@ _bev_refill_callback(evutil_socket_t fd, short what, void *arg)
/* First, update the bucket */
event_base_gettimeofday_cached(bev->bev.ev_base, &now);
- tick = ev_token_bucket_get_tick(&now,
+ tick = ev_token_bucket_get_tick_(&now,
bev->rate_limiting->cfg);
- ev_token_bucket_update(&bev->rate_limiting->limit,
+ ev_token_bucket_update_(&bev->rate_limiting->limit,
bev->rate_limiting->cfg,
tick);
/* Now unsuspend any read/write operations as appropriate. */
if ((bev->read_suspended & BEV_SUSPEND_BW)) {
if (bev->rate_limiting->limit.read_limit > 0)
- bufferevent_unsuspend_read(&bev->bev, BEV_SUSPEND_BW);
+ bufferevent_unsuspend_read_(&bev->bev, BEV_SUSPEND_BW);
else
again = 1;
}
if ((bev->write_suspended & BEV_SUSPEND_BW)) {
if (bev->rate_limiting->limit.write_limit > 0)
- bufferevent_unsuspend_write(&bev->bev, BEV_SUSPEND_BW);
+ bufferevent_unsuspend_write_(&bev->bev, BEV_SUSPEND_BW);
else
again = 1;
}
@@ -440,9 +438,12 @@ _bev_refill_callback(evutil_socket_t fd, short what, void *arg)
BEV_UNLOCK(&bev->bev);
}
-/** Helper: grab a random element from a bufferevent group. */
+/** Helper: grab a random element from a bufferevent group.
+ *
+ * Requires that we hold the lock on the group.
+ */
static struct bufferevent_private *
-_bev_group_random_element(struct bufferevent_rate_limit_group *group)
+bev_group_random_element_(struct bufferevent_rate_limit_group *group)
{
int which;
struct bufferevent_private *bev;
@@ -452,13 +453,13 @@ _bev_group_random_element(struct bufferevent_rate_limit_group *group)
if (!group->n_members)
return NULL;
- EVUTIL_ASSERT(! TAILQ_EMPTY(&group->members));
+ EVUTIL_ASSERT(! LIST_EMPTY(&group->members));
- which = _evutil_weakrand() % group->n_members;
+ which = evutil_weakrand_range_(&group->weakrand_seed, group->n_members);
- bev = TAILQ_FIRST(&group->members);
+ bev = LIST_FIRST(&group->members);
while (which--)
- bev = TAILQ_NEXT(bev, rate_limiting->next_in_group);
+ bev = LIST_NEXT(bev, rate_limiting->next_in_group);
return bev;
}
@@ -472,27 +473,27 @@ _bev_group_random_element(struct bufferevent_rate_limit_group *group)
*/
#define FOREACH_RANDOM_ORDER(block) \
do { \
- first = _bev_group_random_element(g); \
- for (bev = first; bev != TAILQ_END(&g->members); \
- bev = TAILQ_NEXT(bev, rate_limiting->next_in_group)) { \
+ first = bev_group_random_element_(g); \
+ for (bev = first; bev != LIST_END(&g->members); \
+ bev = LIST_NEXT(bev, rate_limiting->next_in_group)) { \
block ; \
} \
- for (bev = TAILQ_FIRST(&g->members); bev && bev != first; \
- bev = TAILQ_NEXT(bev, rate_limiting->next_in_group)) { \
+ for (bev = LIST_FIRST(&g->members); bev && bev != first; \
+ bev = LIST_NEXT(bev, rate_limiting->next_in_group)) { \
block ; \
} \
} while (0)
static void
-_bev_group_unsuspend_reading(struct bufferevent_rate_limit_group *g)
+bev_group_unsuspend_reading_(struct bufferevent_rate_limit_group *g)
{
int again = 0;
struct bufferevent_private *bev, *first;
g->read_suspended = 0;
FOREACH_RANDOM_ORDER({
- if (EVLOCK_TRY_LOCK(bev->lock)) {
- bufferevent_unsuspend_read(&bev->bev,
+ if (EVLOCK_TRY_LOCK_(bev->lock)) {
+ bufferevent_unsuspend_read_(&bev->bev,
BEV_SUSPEND_BW_GROUP);
EVLOCK_UNLOCK(bev->lock, 0);
} else {
@@ -503,15 +504,15 @@ _bev_group_unsuspend_reading(struct bufferevent_rate_limit_group *g)
}
static void
-_bev_group_unsuspend_writing(struct bufferevent_rate_limit_group *g)
+bev_group_unsuspend_writing_(struct bufferevent_rate_limit_group *g)
{
int again = 0;
struct bufferevent_private *bev, *first;
g->write_suspended = 0;
FOREACH_RANDOM_ORDER({
- if (EVLOCK_TRY_LOCK(bev->lock)) {
- bufferevent_unsuspend_write(&bev->bev,
+ if (EVLOCK_TRY_LOCK_(bev->lock)) {
+ bufferevent_unsuspend_write_(&bev->bev,
BEV_SUSPEND_BW_GROUP);
EVLOCK_UNLOCK(bev->lock, 0);
} else {
@@ -525,7 +526,7 @@ _bev_group_unsuspend_writing(struct bufferevent_rate_limit_group *g)
and unsuspend group members as needed.
*/
static void
-_bev_group_refill_callback(evutil_socket_t fd, short what, void *arg)
+bev_group_refill_callback_(evutil_socket_t fd, short what, void *arg)
{
struct bufferevent_rate_limit_group *g = arg;
unsigned tick;
@@ -535,16 +536,16 @@ _bev_group_refill_callback(evutil_socket_t fd, short what, void *arg)
LOCK_GROUP(g);
- tick = ev_token_bucket_get_tick(&now, &g->rate_limit_cfg);
- ev_token_bucket_update(&g->rate_limit, &g->rate_limit_cfg, tick);
+ tick = ev_token_bucket_get_tick_(&now, &g->rate_limit_cfg);
+ ev_token_bucket_update_(&g->rate_limit, &g->rate_limit_cfg, tick);
if (g->pending_unsuspend_read ||
(g->read_suspended && (g->rate_limit.read_limit >= g->min_share))) {
- _bev_group_unsuspend_reading(g);
+ bev_group_unsuspend_reading_(g);
}
if (g->pending_unsuspend_write ||
(g->write_suspended && (g->rate_limit.write_limit >= g->min_share))){
- _bev_group_unsuspend_writing(g);
+ bev_group_unsuspend_writing_(g);
}
/* XXXX Rather than waiting to the next tick to unsuspend stuff
@@ -574,8 +575,8 @@ bufferevent_set_rate_limit(struct bufferevent *bev,
if (bevp->rate_limiting) {
rlim = bevp->rate_limiting;
rlim->cfg = NULL;
- bufferevent_unsuspend_read(bev, BEV_SUSPEND_BW);
- bufferevent_unsuspend_write(bev, BEV_SUSPEND_BW);
+ bufferevent_unsuspend_read_(bev, BEV_SUSPEND_BW);
+ bufferevent_unsuspend_write_(bev, BEV_SUSPEND_BW);
if (event_initialized(&rlim->refill_bucket_event))
event_del(&rlim->refill_bucket_event);
}
@@ -584,7 +585,7 @@ bufferevent_set_rate_limit(struct bufferevent *bev,
}
event_base_gettimeofday_cached(bev->ev_base, &now);
- tick = ev_token_bucket_get_tick(&now, cfg);
+ tick = ev_token_bucket_get_tick_(&now, cfg);
if (bevp->rate_limiting && bevp->rate_limiting->cfg == cfg) {
/* no-op */
@@ -602,25 +603,25 @@ bufferevent_set_rate_limit(struct bufferevent *bev,
reinit = rlim->cfg != NULL;
rlim->cfg = cfg;
- ev_token_bucket_init(&rlim->limit, cfg, tick, reinit);
+ ev_token_bucket_init_(&rlim->limit, cfg, tick, reinit);
if (reinit) {
EVUTIL_ASSERT(event_initialized(&rlim->refill_bucket_event));
event_del(&rlim->refill_bucket_event);
}
- evtimer_assign(&rlim->refill_bucket_event, bev->ev_base,
- _bev_refill_callback, bevp);
+ event_assign(&rlim->refill_bucket_event, bev->ev_base,
+ -1, EV_FINALIZE, bev_refill_callback_, bevp);
if (rlim->limit.read_limit > 0) {
- bufferevent_unsuspend_read(bev, BEV_SUSPEND_BW);
+ bufferevent_unsuspend_read_(bev, BEV_SUSPEND_BW);
} else {
- bufferevent_suspend_read(bev, BEV_SUSPEND_BW);
+ bufferevent_suspend_read_(bev, BEV_SUSPEND_BW);
suspended=1;
}
if (rlim->limit.write_limit > 0) {
- bufferevent_unsuspend_write(bev, BEV_SUSPEND_BW);
+ bufferevent_unsuspend_write_(bev, BEV_SUSPEND_BW);
} else {
- bufferevent_suspend_write(bev, BEV_SUSPEND_BW);
+ bufferevent_suspend_write_(bev, BEV_SUSPEND_BW);
suspended = 1;
}
@@ -643,18 +644,18 @@ bufferevent_rate_limit_group_new(struct event_base *base,
ev_uint32_t tick;
event_base_gettimeofday_cached(base, &now);
- tick = ev_token_bucket_get_tick(&now, cfg);
+ tick = ev_token_bucket_get_tick_(&now, cfg);
g = mm_calloc(1, sizeof(struct bufferevent_rate_limit_group));
if (!g)
return NULL;
memcpy(&g->rate_limit_cfg, cfg, sizeof(g->rate_limit_cfg));
- TAILQ_INIT(&g->members);
+ LIST_INIT(&g->members);
- ev_token_bucket_init(&g->rate_limit, cfg, tick, 0);
+ ev_token_bucket_init_(&g->rate_limit, cfg, tick, 0);
- event_assign(&g->master_refill_event, base, -1, EV_PERSIST,
- _bev_group_refill_callback, g);
+ event_assign(&g->master_refill_event, base, -1, EV_PERSIST|EV_FINALIZE,
+ bev_group_refill_callback_, g);
/*XXXX handle event_add failure */
event_add(&g->master_refill_event, &cfg->tick_timeout);
@@ -662,6 +663,9 @@ bufferevent_rate_limit_group_new(struct event_base *base,
bufferevent_rate_limit_group_set_min_share(g, 64);
+ evutil_weakrand_seed_(&g->weakrand_seed,
+ (ev_uint32_t) ((now.tv_sec + now.tv_usec) + (ev_intptr_t)g));
+
return g;
}
@@ -744,8 +748,8 @@ bufferevent_add_to_rate_limit_group(struct bufferevent *bev,
BEV_UNLOCK(bev);
return -1;
}
- evtimer_assign(&rlim->refill_bucket_event, bev->ev_base,
- _bev_refill_callback, bevp);
+ event_assign(&rlim->refill_bucket_event, bev->ev_base,
+ -1, EV_FINALIZE, bev_refill_callback_, bevp);
bevp->rate_limiting = rlim;
}
@@ -759,7 +763,7 @@ bufferevent_add_to_rate_limit_group(struct bufferevent *bev,
LOCK_GROUP(g);
bevp->rate_limiting->group = g;
++g->n_members;
- TAILQ_INSERT_TAIL(&g->members, bevp, rate_limiting->next_in_group);
+ LIST_INSERT_HEAD(&g->members, bevp, rate_limiting->next_in_group);
rsuspend = g->read_suspended;
wsuspend = g->write_suspended;
@@ -767,9 +771,9 @@ bufferevent_add_to_rate_limit_group(struct bufferevent *bev,
UNLOCK_GROUP(g);
if (rsuspend)
- bufferevent_suspend_read(bev, BEV_SUSPEND_BW_GROUP);
+ bufferevent_suspend_read_(bev, BEV_SUSPEND_BW_GROUP);
if (wsuspend)
- bufferevent_suspend_write(bev, BEV_SUSPEND_BW_GROUP);
+ bufferevent_suspend_write_(bev, BEV_SUSPEND_BW_GROUP);
BEV_UNLOCK(bev);
return 0;
@@ -778,11 +782,11 @@ bufferevent_add_to_rate_limit_group(struct bufferevent *bev,
int
bufferevent_remove_from_rate_limit_group(struct bufferevent *bev)
{
- return bufferevent_remove_from_rate_limit_group_internal(bev, 1);
+ return bufferevent_remove_from_rate_limit_group_internal_(bev, 1);
}
int
-bufferevent_remove_from_rate_limit_group_internal(struct bufferevent *bev,
+bufferevent_remove_from_rate_limit_group_internal_(struct bufferevent *bev,
int unsuspend)
{
struct bufferevent_private *bevp =
@@ -794,12 +798,12 @@ bufferevent_remove_from_rate_limit_group_internal(struct bufferevent *bev,
LOCK_GROUP(g);
bevp->rate_limiting->group = NULL;
--g->n_members;
- TAILQ_REMOVE(&g->members, bevp, rate_limiting->next_in_group);
+ LIST_REMOVE(bevp, rate_limiting->next_in_group);
UNLOCK_GROUP(g);
}
if (unsuspend) {
- bufferevent_unsuspend_read(bev, BEV_SUSPEND_BW_GROUP);
- bufferevent_unsuspend_write(bev, BEV_SUSPEND_BW_GROUP);
+ bufferevent_unsuspend_read_(bev, BEV_SUSPEND_BW_GROUP);
+ bufferevent_unsuspend_write_(bev, BEV_SUSPEND_BW_GROUP);
}
BEV_UNLOCK(bev);
return 0;
@@ -813,7 +817,7 @@ bufferevent_remove_from_rate_limit_group_internal(struct bufferevent *bev,
* === */
/* Mostly you don't want to use this function from inside libevent;
- * _bufferevent_get_read_max() is more likely what you want*/
+ * bufferevent_get_read_max_() is more likely what you want*/
ev_ssize_t
bufferevent_get_read_limit(struct bufferevent *bev)
{
@@ -832,7 +836,7 @@ bufferevent_get_read_limit(struct bufferevent *bev)
}
/* Mostly you don't want to use this function from inside libevent;
- * _bufferevent_get_write_max() is more likely what you want*/
+ * bufferevent_get_write_max_() is more likely what you want*/
ev_ssize_t
bufferevent_get_write_limit(struct bufferevent *bev)
{
@@ -850,12 +854,62 @@ bufferevent_get_write_limit(struct bufferevent *bev)
return r;
}
+int
+bufferevent_set_max_single_read(struct bufferevent *bev, size_t size)
+{
+ struct bufferevent_private *bevp;
+ BEV_LOCK(bev);
+ bevp = BEV_UPCAST(bev);
+ if (size == 0 || size > EV_SSIZE_MAX)
+ bevp->max_single_read = MAX_SINGLE_READ_DEFAULT;
+ else
+ bevp->max_single_read = size;
+ BEV_UNLOCK(bev);
+ return 0;
+}
+
+int
+bufferevent_set_max_single_write(struct bufferevent *bev, size_t size)
+{
+ struct bufferevent_private *bevp;
+ BEV_LOCK(bev);
+ bevp = BEV_UPCAST(bev);
+ if (size == 0 || size > EV_SSIZE_MAX)
+ bevp->max_single_write = MAX_SINGLE_WRITE_DEFAULT;
+ else
+ bevp->max_single_write = size;
+ BEV_UNLOCK(bev);
+ return 0;
+}
+
+ev_ssize_t
+bufferevent_get_max_single_read(struct bufferevent *bev)
+{
+ ev_ssize_t r;
+
+ BEV_LOCK(bev);
+ r = BEV_UPCAST(bev)->max_single_read;
+ BEV_UNLOCK(bev);
+ return r;
+}
+
+ev_ssize_t
+bufferevent_get_max_single_write(struct bufferevent *bev)
+{
+ ev_ssize_t r;
+
+ BEV_LOCK(bev);
+ r = BEV_UPCAST(bev)->max_single_write;
+ BEV_UNLOCK(bev);
+ return r;
+}
+
ev_ssize_t
bufferevent_get_max_to_read(struct bufferevent *bev)
{
ev_ssize_t r;
BEV_LOCK(bev);
- r = _bufferevent_get_read_max(BEV_UPCAST(bev));
+ r = bufferevent_get_read_max_(BEV_UPCAST(bev));
BEV_UNLOCK(bev);
return r;
}
@@ -865,14 +919,31 @@ bufferevent_get_max_to_write(struct bufferevent *bev)
{
ev_ssize_t r;
BEV_LOCK(bev);
- r = _bufferevent_get_write_max(BEV_UPCAST(bev));
+ r = bufferevent_get_write_max_(BEV_UPCAST(bev));
BEV_UNLOCK(bev);
return r;
}
+const struct ev_token_bucket_cfg *
+bufferevent_get_token_bucket_cfg(const struct bufferevent *bev) {
+ struct bufferevent_private *bufev_private = BEV_UPCAST(bev);
+ struct ev_token_bucket_cfg *cfg;
+
+ BEV_LOCK(bev);
+
+ if (bufev_private->rate_limiting) {
+ cfg = bufev_private->rate_limiting->cfg;
+ } else {
+ cfg = NULL;
+ }
+
+ BEV_UNLOCK(bev);
+
+ return cfg;
+}
/* Mostly you don't want to use this function from inside libevent;
- * _bufferevent_get_read_max() is more likely what you want*/
+ * bufferevent_get_read_max_() is more likely what you want*/
ev_ssize_t
bufferevent_rate_limit_group_get_read_limit(
struct bufferevent_rate_limit_group *grp)
@@ -885,7 +956,7 @@ bufferevent_rate_limit_group_get_read_limit(
}
/* Mostly you don't want to use this function from inside libevent;
- * _bufferevent_get_write_max() is more likely what you want. */
+ * bufferevent_get_write_max_() is more likely what you want. */
ev_ssize_t
bufferevent_rate_limit_group_get_write_limit(
struct bufferevent_rate_limit_group *grp)
@@ -910,14 +981,14 @@ bufferevent_decrement_read_limit(struct bufferevent *bev, ev_ssize_t decr)
new_limit = (bevp->rate_limiting->limit.read_limit -= decr);
if (old_limit > 0 && new_limit <= 0) {
- bufferevent_suspend_read(bev, BEV_SUSPEND_BW);
+ bufferevent_suspend_read_(bev, BEV_SUSPEND_BW);
if (event_add(&bevp->rate_limiting->refill_bucket_event,
&bevp->rate_limiting->cfg->tick_timeout) < 0)
r = -1;
} else if (old_limit <= 0 && new_limit > 0) {
if (!(bevp->write_suspended & BEV_SUSPEND_BW))
event_del(&bevp->rate_limiting->refill_bucket_event);
- bufferevent_unsuspend_read(bev, BEV_SUSPEND_BW);
+ bufferevent_unsuspend_read_(bev, BEV_SUSPEND_BW);
}
BEV_UNLOCK(bev);
@@ -939,14 +1010,14 @@ bufferevent_decrement_write_limit(struct bufferevent *bev, ev_ssize_t decr)
new_limit = (bevp->rate_limiting->limit.write_limit -= decr);
if (old_limit > 0 && new_limit <= 0) {
- bufferevent_suspend_write(bev, BEV_SUSPEND_BW);
+ bufferevent_suspend_write_(bev, BEV_SUSPEND_BW);
if (event_add(&bevp->rate_limiting->refill_bucket_event,
&bevp->rate_limiting->cfg->tick_timeout) < 0)
r = -1;
} else if (old_limit <= 0 && new_limit > 0) {
if (!(bevp->read_suspended & BEV_SUSPEND_BW))
event_del(&bevp->rate_limiting->refill_bucket_event);
- bufferevent_unsuspend_write(bev, BEV_SUSPEND_BW);
+ bufferevent_unsuspend_write_(bev, BEV_SUSPEND_BW);
}
BEV_UNLOCK(bev);
@@ -964,9 +1035,9 @@ bufferevent_rate_limit_group_decrement_read(
new_limit = (grp->rate_limit.read_limit -= decr);
if (old_limit > 0 && new_limit <= 0) {
- _bev_group_suspend_reading(grp);
+ bev_group_suspend_reading_(grp);
} else if (old_limit <= 0 && new_limit > 0) {
- _bev_group_unsuspend_reading(grp);
+ bev_group_unsuspend_reading_(grp);
}
UNLOCK_GROUP(grp);
@@ -984,9 +1055,9 @@ bufferevent_rate_limit_group_decrement_write(
new_limit = (grp->rate_limit.write_limit -= decr);
if (old_limit > 0 && new_limit <= 0) {
- _bev_group_suspend_writing(grp);
+ bev_group_suspend_writing_(grp);
} else if (old_limit <= 0 && new_limit > 0) {
- _bev_group_unsuspend_writing(grp);
+ bev_group_unsuspend_writing_(grp);
}
UNLOCK_GROUP(grp);
@@ -1009,3 +1080,13 @@ bufferevent_rate_limit_group_reset_totals(struct bufferevent_rate_limit_group *g
{
grp->total_read = grp->total_written = 0;
}
+
+int
+bufferevent_ratelim_init_(struct bufferevent_private *bev)
+{
+ bev->rate_limiting = NULL;
+ bev->max_single_read = MAX_SINGLE_READ_DEFAULT;
+ bev->max_single_write = MAX_SINGLE_WRITE_DEFAULT;
+
+ return 0;
+}
diff --git a/bufferevent_sock.c b/bufferevent_sock.c
index 9b0caf24..49ebc0be 100644
--- a/bufferevent_sock.c
+++ b/bufferevent_sock.c
@@ -26,11 +26,12 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <sys/types.h>
-
#include "event2/event-config.h"
+#include "evconfig-private.h"
+
+#include <sys/types.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -38,25 +39,25 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#ifdef _EVENT_HAVE_STDARG_H
+#ifdef EVENT__HAVE_STDARG_H
#include <stdarg.h>
#endif
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
-#ifdef _EVENT_HAVE_SYS_SOCKET_H
+#ifdef EVENT__HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
-#ifdef _EVENT_HAVE_NETINET_IN_H
+#ifdef EVENT__HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
-#ifdef _EVENT_HAVE_NETINET_IN6_H
+#ifdef EVENT__HAVE_NETINET_IN6_H
#include <netinet/in6.h>
#endif
@@ -70,7 +71,7 @@
#include "mm-internal.h"
#include "bufferevent-internal.h"
#include "util-internal.h"
-#ifdef WIN32
+#ifdef _WIN32
#include "iocp-internal.h"
#endif
@@ -89,6 +90,7 @@ const struct bufferevent_ops bufferevent_ops_socket = {
evutil_offsetof(struct bufferevent_private, bev),
be_socket_enable,
be_socket_disable,
+ NULL, /* unlink */
be_socket_destruct,
be_socket_adj_timeouts,
be_socket_flush,
@@ -96,7 +98,7 @@ const struct bufferevent_ops bufferevent_ops_socket = {
};
#define be_socket_add(ev, t) \
- _bufferevent_add_event((ev), (t))
+ bufferevent_add_event_((ev), (t))
static void
bufferevent_socket_outbuf_cb(struct evbuffer *buf,
@@ -130,7 +132,7 @@ bufferevent_readcb(evutil_socket_t fd, short event, void *arg)
short what = BEV_EVENT_READING;
ev_ssize_t howmuch = -1, readmax=-1;
- _bufferevent_incref_and_lock(bufev);
+ bufferevent_incref_and_lock_(bufev);
if (event == EV_TIMEOUT) {
/* Note that we only check for event==EV_TIMEOUT. If
@@ -154,7 +156,7 @@ bufferevent_readcb(evutil_socket_t fd, short event, void *arg)
goto done;
}
}
- readmax = _bufferevent_get_read_max(bufev_p);
+ readmax = bufferevent_get_read_max_(bufev_p);
if (howmuch < 0 || howmuch > readmax) /* The use of -1 for "unlimited"
* uglifies this code. XXXX */
howmuch = readmax;
@@ -179,11 +181,10 @@ bufferevent_readcb(evutil_socket_t fd, short event, void *arg)
if (res <= 0)
goto error;
- _bufferevent_decrement_read_buckets(bufev_p, res);
+ bufferevent_decrement_read_buckets_(bufev_p, res);
/* Invoke the user callback - must always be called last */
- if (evbuffer_get_length(input) >= bufev->wm_read.low)
- _bufferevent_run_readcb(bufev);
+ bufferevent_trigger_nolock_(bufev, EV_READ, 0);
goto done;
@@ -192,10 +193,10 @@ bufferevent_readcb(evutil_socket_t fd, short event, void *arg)
error:
bufferevent_disable(bufev, EV_READ);
- _bufferevent_run_eventcb(bufev, what);
+ bufferevent_run_eventcb_(bufev, what, 0);
done:
- _bufferevent_decref_and_unlock(bufev);
+ bufferevent_decref_and_unlock_(bufev);
}
static void
@@ -209,7 +210,7 @@ bufferevent_writecb(evutil_socket_t fd, short event, void *arg)
int connected = 0;
ev_ssize_t atmost = -1;
- _bufferevent_incref_and_lock(bufev);
+ bufferevent_incref_and_lock_(bufev);
if (event == EV_TIMEOUT) {
/* Note that we only check for event==EV_TIMEOUT. If
@@ -219,7 +220,7 @@ bufferevent_writecb(evutil_socket_t fd, short event, void *arg)
goto error;
}
if (bufev_p->connecting) {
- int c = evutil_socket_finished_connecting(fd);
+ int c = evutil_socket_finished_connecting_(fd);
/* we need to fake the error if the connection was refused
* immediately - usually connection to localhost on BSD */
if (bufev_p->connection_refused) {
@@ -234,21 +235,21 @@ bufferevent_writecb(evutil_socket_t fd, short event, void *arg)
if (c < 0) {
event_del(&bufev->ev_write);
event_del(&bufev->ev_read);
- _bufferevent_run_eventcb(bufev, BEV_EVENT_ERROR);
+ bufferevent_run_eventcb_(bufev, BEV_EVENT_ERROR, 0);
goto done;
} else {
connected = 1;
-#ifdef WIN32
+#ifdef _WIN32
if (BEV_IS_ASYNC(bufev)) {
event_del(&bufev->ev_write);
- bufferevent_async_set_connected(bufev);
- _bufferevent_run_eventcb(bufev,
- BEV_EVENT_CONNECTED);
+ bufferevent_async_set_connected_(bufev);
+ bufferevent_run_eventcb_(bufev,
+ BEV_EVENT_CONNECTED, 0);
goto done;
}
#endif
- _bufferevent_run_eventcb(bufev,
- BEV_EVENT_CONNECTED);
+ bufferevent_run_eventcb_(bufev,
+ BEV_EVENT_CONNECTED, 0);
if (!(bufev->enabled & EV_WRITE) ||
bufev_p->write_suspended) {
event_del(&bufev->ev_write);
@@ -257,7 +258,7 @@ bufferevent_writecb(evutil_socket_t fd, short event, void *arg)
}
}
- atmost = _bufferevent_get_write_max(bufev_p);
+ atmost = bufferevent_get_write_max_(bufev_p);
if (bufev_p->write_suspended)
goto done;
@@ -281,7 +282,7 @@ bufferevent_writecb(evutil_socket_t fd, short event, void *arg)
if (res <= 0)
goto error;
- _bufferevent_decrement_write_buckets(bufev_p, res);
+ bufferevent_decrement_write_buckets_(bufev_p, res);
}
if (evbuffer_get_length(bufev->output) == 0) {
@@ -292,9 +293,8 @@ bufferevent_writecb(evutil_socket_t fd, short event, void *arg)
* Invoke the user callback if our buffer is drained or below the
* low watermark.
*/
- if ((res || !connected) &&
- evbuffer_get_length(bufev->output) <= bufev->wm_write.low) {
- _bufferevent_run_writecb(bufev);
+ if (res || !connected) {
+ bufferevent_trigger_nolock_(bufev, EV_WRITE, 0);
}
goto done;
@@ -307,10 +307,10 @@ bufferevent_writecb(evutil_socket_t fd, short event, void *arg)
error:
bufferevent_disable(bufev, EV_WRITE);
- _bufferevent_run_eventcb(bufev, what);
+ bufferevent_run_eventcb_(bufev, what, 0);
done:
- _bufferevent_decref_and_unlock(bufev);
+ bufferevent_decref_and_unlock_(bufev);
}
struct bufferevent *
@@ -320,15 +320,15 @@ bufferevent_socket_new(struct event_base *base, evutil_socket_t fd,
struct bufferevent_private *bufev_p;
struct bufferevent *bufev;
-#ifdef WIN32
- if (base && event_base_get_iocp(base))
- return bufferevent_async_new(base, fd, options);
+#ifdef _WIN32
+ if (base && event_base_get_iocp_(base))
+ return bufferevent_async_new_(base, fd, options);
#endif
if ((bufev_p = mm_calloc(1, sizeof(struct bufferevent_private)))== NULL)
return NULL;
- if (bufferevent_init_common(bufev_p, base, &bufferevent_ops_socket,
+ if (bufferevent_init_common_(bufev_p, base, &bufferevent_ops_socket,
options) < 0) {
mm_free(bufev_p);
return NULL;
@@ -337,9 +337,9 @@ bufferevent_socket_new(struct event_base *base, evutil_socket_t fd,
evbuffer_set_flags(bufev->output, EVBUFFER_FLAG_DRAINS_TO_FD);
event_assign(&bufev->ev_read, bufev->ev_base, fd,
- EV_READ|EV_PERSIST, bufferevent_readcb, bufev);
+ EV_READ|EV_PERSIST|EV_FINALIZE, bufferevent_readcb, bufev);
event_assign(&bufev->ev_write, bufev->ev_base, fd,
- EV_WRITE|EV_PERSIST, bufferevent_writecb, bufev);
+ EV_WRITE|EV_PERSIST|EV_FINALIZE, bufferevent_writecb, bufev);
evbuffer_add_cb(bufev->output, bufferevent_socket_outbuf_cb, bufev);
@@ -361,7 +361,7 @@ bufferevent_socket_connect(struct bufferevent *bev,
int result=-1;
int ownfd = 0;
- _bufferevent_incref_and_lock(bev);
+ bufferevent_incref_and_lock_(bev);
if (!bufev_p)
goto done;
@@ -370,18 +370,17 @@ bufferevent_socket_connect(struct bufferevent *bev,
if (fd < 0) {
if (!sa)
goto done;
- fd = socket(sa->sa_family, SOCK_STREAM, 0);
+ fd = evutil_socket_(sa->sa_family,
+ SOCK_STREAM|EVUTIL_SOCK_NONBLOCK, 0);
if (fd < 0)
goto done;
- if (evutil_make_socket_nonblocking(fd)<0)
- goto done;
ownfd = 1;
}
if (sa) {
-#ifdef WIN32
- if (bufferevent_async_can_connect(bev)) {
+#ifdef _WIN32
+ if (bufferevent_async_can_connect_(bev)) {
bufferevent_setfd(bev, fd);
- r = bufferevent_async_connect(bev, fd, sa, socklen);
+ r = bufferevent_async_connect_(bev, fd, sa, socklen);
if (r < 0)
goto freesock;
bufev_p->connecting = 1;
@@ -389,17 +388,17 @@ bufferevent_socket_connect(struct bufferevent *bev,
goto done;
} else
#endif
- r = evutil_socket_connect(&fd, sa, socklen);
+ r = evutil_socket_connect_(&fd, sa, socklen);
if (r < 0)
goto freesock;
}
-#ifdef WIN32
+#ifdef _WIN32
/* ConnectEx() isn't always around, even when IOCP is enabled.
* Here, we borrow the socket object's write handler to fall back
* on a non-blocking connect() when ConnectEx() is unavailable. */
if (BEV_IS_ASYNC(bev)) {
event_assign(&bev->ev_write, bev->ev_base, fd,
- EV_WRITE|EV_PERSIST, bufferevent_writecb, bev);
+ EV_WRITE|EV_PERSIST|EV_FINALIZE, bufferevent_writecb, bev);
}
#endif
bufferevent_setfd(bev, fd);
@@ -425,12 +424,12 @@ bufferevent_socket_connect(struct bufferevent *bev,
goto done;
freesock:
- _bufferevent_run_eventcb(bev, BEV_EVENT_ERROR);
+ bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, 0);
if (ownfd)
evutil_closesocket(fd);
/* do something about the error? */
done:
- _bufferevent_decref_and_unlock(bev);
+ bufferevent_decref_and_unlock_(bev);
return result;
}
@@ -444,13 +443,13 @@ bufferevent_connect_getaddrinfo_cb(int result, struct evutil_addrinfo *ai,
int r;
BEV_LOCK(bev);
- bufferevent_unsuspend_write(bev, BEV_SUSPEND_LOOKUP);
- bufferevent_unsuspend_read(bev, BEV_SUSPEND_LOOKUP);
+ bufferevent_unsuspend_write_(bev, BEV_SUSPEND_LOOKUP);
+ bufferevent_unsuspend_read_(bev, BEV_SUSPEND_LOOKUP);
if (result != 0) {
bev_p->dns_error = result;
- _bufferevent_run_eventcb(bev, BEV_EVENT_ERROR);
- _bufferevent_decref_and_unlock(bev);
+ bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, 0);
+ bufferevent_decref_and_unlock_(bev);
if (ai)
evutil_freeaddrinfo(ai);
return;
@@ -460,7 +459,7 @@ bufferevent_connect_getaddrinfo_cb(int result, struct evutil_addrinfo *ai,
/* XXX use this return value */
r = bufferevent_socket_connect(bev, ai->ai_addr, (int)ai->ai_addrlen);
(void)r;
- _bufferevent_decref_and_unlock(bev);
+ bufferevent_decref_and_unlock_(bev);
evutil_freeaddrinfo(ai);
}
@@ -490,18 +489,19 @@ bufferevent_socket_connect_hostname(struct bufferevent *bev,
hint.ai_protocol = IPPROTO_TCP;
hint.ai_socktype = SOCK_STREAM;
- bufferevent_suspend_write(bev, BEV_SUSPEND_LOOKUP);
- bufferevent_suspend_read(bev, BEV_SUSPEND_LOOKUP);
+ bufferevent_suspend_write_(bev, BEV_SUSPEND_LOOKUP);
+ bufferevent_suspend_read_(bev, BEV_SUSPEND_LOOKUP);
- bufferevent_incref(bev);
- err = evutil_getaddrinfo_async(evdns_base, hostname, portbuf,
+ bufferevent_incref_(bev);
+ err = evutil_getaddrinfo_async_(evdns_base, hostname, portbuf,
&hint, bufferevent_connect_getaddrinfo_cb, bev);
if (err == 0) {
return 0;
} else {
- bufferevent_unsuspend_write(bev, BEV_SUSPEND_LOOKUP);
- bufferevent_unsuspend_read(bev, BEV_SUSPEND_LOOKUP);
+ bufferevent_unsuspend_write_(bev, BEV_SUSPEND_LOOKUP);
+ bufferevent_unsuspend_read_(bev, BEV_SUSPEND_LOOKUP);
+ bufferevent_decref_(bev);
return -1;
}
}
@@ -588,9 +588,6 @@ be_socket_destruct(struct bufferevent *bufev)
fd = event_get_fd(&bufev->ev_read);
- event_del(&bufev->ev_read);
- event_del(&bufev->ev_write);
-
if ((bufev_p->options & BEV_OPT_CLOSE_ON_FREE) && fd >= 0)
EVUTIL_CLOSESOCKET(fd);
}
@@ -599,12 +596,21 @@ static int
be_socket_adj_timeouts(struct bufferevent *bufev)
{
int r = 0;
- if (event_pending(&bufev->ev_read, EV_READ, NULL))
- if (be_socket_add(&bufev->ev_read, &bufev->timeout_read) < 0)
- r = -1;
+ if (event_pending(&bufev->ev_read, EV_READ, NULL)) {
+ if (evutil_timerisset(&bufev->timeout_read)) {
+ if (be_socket_add(&bufev->ev_read, &bufev->timeout_read) < 0)
+ r = -1;
+ } else {
+ event_remove_timer(&bufev->ev_read);
+ }
+ }
if (event_pending(&bufev->ev_write, EV_WRITE, NULL)) {
- if (be_socket_add(&bufev->ev_write, &bufev->timeout_write) < 0)
- r = -1;
+ if (evutil_timerisset(&bufev->timeout_write)) {
+ if (be_socket_add(&bufev->ev_write, &bufev->timeout_write) < 0)
+ r = -1;
+ } else {
+ event_remove_timer(&bufev->ev_write);
+ }
}
return r;
}
@@ -627,9 +633,9 @@ be_socket_setfd(struct bufferevent *bufev, evutil_socket_t fd)
event_del(&bufev->ev_write);
event_assign(&bufev->ev_read, bufev->ev_base, fd,
- EV_READ|EV_PERSIST, bufferevent_readcb, bufev);
+ EV_READ|EV_PERSIST|EV_FINALIZE, bufferevent_readcb, bufev);
event_assign(&bufev->ev_write, bufev->ev_base, fd,
- EV_WRITE|EV_PERSIST, bufferevent_writecb, bufev);
+ EV_WRITE|EV_PERSIST|EV_FINALIZE, bufferevent_writecb, bufev);
if (fd >= 0)
bufferevent_enable(bufev, bufev->enabled);
@@ -642,6 +648,8 @@ int
bufferevent_priority_set(struct bufferevent *bufev, int priority)
{
int r = -1;
+ struct bufferevent_private *bufev_p =
+ EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
BEV_LOCK(bufev);
if (bufev->be_ops != &bufferevent_ops_socket)
@@ -652,6 +660,8 @@ bufferevent_priority_set(struct bufferevent *bufev, int priority)
if (event_priority_set(&bufev->ev_write, priority) == -1)
goto done;
+ event_deferred_cb_set_priority_(&bufev_p->deferred, priority);
+
r = 0;
done:
BEV_UNLOCK(bufev);
diff --git a/changelist-internal.h b/changelist-internal.h
index 6ed432a2..98fc52ae 100644
--- a/changelist-internal.h
+++ b/changelist-internal.h
@@ -23,8 +23,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _CHANGELIST_H_
-#define _CHANGELIST_H_
+#ifndef CHANGELIST_INTERNAL_H_INCLUDED_
+#define CHANGELIST_INTERNAL_H_INCLUDED_
/*
A "changelist" is a list of all the fd status changes that should be made
@@ -62,6 +62,7 @@ struct event_change {
* and write_change is unused. */
ev_uint8_t read_change;
ev_uint8_t write_change;
+ ev_uint8_t close_change;
};
/* Flags for read_change and write_change. */
@@ -82,20 +83,20 @@ struct event_change {
#define EVENT_CHANGELIST_FDINFO_SIZE sizeof(int)
/** Set up the data fields in a changelist. */
-void event_changelist_init(struct event_changelist *changelist);
+void event_changelist_init_(struct event_changelist *changelist);
/** Remove every change in the changelist, and make corresponding changes
* in the event maps in the base. This function is generally used right
* after making all the changes in the changelist. */
-void event_changelist_remove_all(struct event_changelist *changelist,
+void event_changelist_remove_all_(struct event_changelist *changelist,
struct event_base *base);
/** Free all memory held in a changelist. */
-void event_changelist_freemem(struct event_changelist *changelist);
+void event_changelist_freemem_(struct event_changelist *changelist);
/** Implementation of eventop_add that queues the event in a changelist. */
-int event_changelist_add(struct event_base *base, evutil_socket_t fd, short old, short events,
+int event_changelist_add_(struct event_base *base, evutil_socket_t fd, short old, short events,
void *p);
/** Implementation of eventop_del that queues the event in a changelist. */
-int event_changelist_del(struct event_base *base, evutil_socket_t fd, short old, short events,
+int event_changelist_del_(struct event_base *base, evutil_socket_t fd, short old, short events,
void *p);
#endif
diff --git a/cmake/COPYING-CMAKE-SCRIPTS b/cmake/COPYING-CMAKE-SCRIPTS
new file mode 100644
index 00000000..ab3c4d25
--- /dev/null
+++ b/cmake/COPYING-CMAKE-SCRIPTS
@@ -0,0 +1,22 @@
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. The name of the author may not be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file
diff --git a/cmake/CheckFileOffsetBits.c b/cmake/CheckFileOffsetBits.c
new file mode 100644
index 00000000..d948fecf
--- /dev/null
+++ b/cmake/CheckFileOffsetBits.c
@@ -0,0 +1,14 @@
+#include <sys/types.h>
+
+#define KB ((off_t)1024)
+#define MB ((off_t)1024 * KB)
+#define GB ((off_t)1024 * MB)
+#define TB ((off_t)1024 * GB)
+int t2[(((64 * GB -1) % 671088649) == 268434537)
+ && (((TB - (64 * GB -1) + 255) % 1792151290) == 305159546)? 1: -1];
+
+int main()
+{
+ ;
+ return 0;
+}
diff --git a/cmake/CheckFileOffsetBits.cmake b/cmake/CheckFileOffsetBits.cmake
new file mode 100644
index 00000000..12534401
--- /dev/null
+++ b/cmake/CheckFileOffsetBits.cmake
@@ -0,0 +1,43 @@
+# - Check if _FILE_OFFSET_BITS macro needed for large files
+# CHECK_FILE_OFFSET_BITS ()
+#
+# The following variables may be set before calling this macro to
+# modify the way the check is run:
+#
+# CMAKE_REQUIRED_FLAGS = string of compile command line flags
+# CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
+# CMAKE_REQUIRED_INCLUDES = list of include directories
+# Copyright (c) 2009, Michihiro NAKAJIMA
+#
+# Redistribution and use is allowed according to the terms of the BSD license.
+# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+
+#INCLUDE(CheckCSourceCompiles)
+
+GET_FILENAME_COMPONENT(_selfdir_CheckFileOffsetBits
+ "${CMAKE_CURRENT_LIST_FILE}" PATH)
+
+MACRO (CHECK_FILE_OFFSET_BITS)
+ IF(NOT DEFINED _FILE_OFFSET_BITS)
+ MESSAGE(STATUS "Cheking _FILE_OFFSET_BITS for large files")
+ TRY_COMPILE(__WITHOUT_FILE_OFFSET_BITS_64
+ ${CMAKE_CURRENT_BINARY_DIR}
+ ${_selfdir_CheckFileOffsetBits}/CheckFileOffsetBits.c
+ COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS})
+ IF(NOT __WITHOUT_FILE_OFFSET_BITS_64)
+ TRY_COMPILE(__WITH_FILE_OFFSET_BITS_64
+ ${CMAKE_CURRENT_BINARY_DIR}
+ ${_selfdir_CheckFileOffsetBits}/CheckFileOffsetBits.c
+ COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} -D_FILE_OFFSET_BITS=64)
+ ENDIF(NOT __WITHOUT_FILE_OFFSET_BITS_64)
+
+ IF(NOT __WITHOUT_FILE_OFFSET_BITS_64 AND __WITH_FILE_OFFSET_BITS_64)
+ SET(_FILE_OFFSET_BITS 64 CACHE INTERNAL "_FILE_OFFSET_BITS macro needed for large files")
+ MESSAGE(STATUS "Cheking _FILE_OFFSET_BITS for large files - needed")
+ ELSE(NOT __WITHOUT_FILE_OFFSET_BITS_64 AND __WITH_FILE_OFFSET_BITS_64)
+ SET(_FILE_OFFSET_BITS "" CACHE INTERNAL "_FILE_OFFSET_BITS macro needed for large files")
+ MESSAGE(STATUS "Cheking _FILE_OFFSET_BITS for large files - not needed")
+ ENDIF(NOT __WITHOUT_FILE_OFFSET_BITS_64 AND __WITH_FILE_OFFSET_BITS_64)
+ ENDIF(NOT DEFINED _FILE_OFFSET_BITS)
+
+ENDMACRO (CHECK_FILE_OFFSET_BITS)
diff --git a/cmake/CheckFunctionExistsEx.c b/cmake/CheckFunctionExistsEx.c
new file mode 100644
index 00000000..5ee3e591
--- /dev/null
+++ b/cmake/CheckFunctionExistsEx.c
@@ -0,0 +1,30 @@
+#ifdef CHECK_FUNCTION_EXISTS
+
+#ifndef _WIN32
+char CHECK_FUNCTION_EXISTS();
+#endif
+
+#ifdef __CLASSIC_C__
+int main(){
+ int ac;
+ char*av[];
+#else
+int main(int ac, char*av[]){
+#endif
+#ifdef _WIN32
+ void * p = &CHECK_FUNCTION_EXISTS;
+#else
+ CHECK_FUNCTION_EXISTS();
+#endif
+ if(ac > 1000)
+ {
+ return *av[0];
+ }
+ return 0;
+}
+
+#else /* CHECK_FUNCTION_EXISTS */
+
+# error "CHECK_FUNCTION_EXISTS has to specify the function"
+
+#endif /* CHECK_FUNCTION_EXISTS */
diff --git a/cmake/CheckFunctionExistsEx.cmake b/cmake/CheckFunctionExistsEx.cmake
new file mode 100644
index 00000000..f513f4e1
--- /dev/null
+++ b/cmake/CheckFunctionExistsEx.cmake
@@ -0,0 +1,69 @@
+# - Check if a C function can be linked
+# CHECK_FUNCTION_EXISTS(<function> <variable>)
+#
+# Check that the <function> is provided by libraries on the system and
+# store the result in a <variable>. This does not verify that any
+# system header file declares the function, only that it can be found
+# at link time (considure using CheckSymbolExists).
+#
+# The following variables may be set before calling this macro to
+# modify the way the check is run:
+#
+# CMAKE_REQUIRED_FLAGS = string of compile command line flags
+# CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
+# CMAKE_REQUIRED_INCLUDES = list of include directories
+# CMAKE_REQUIRED_LIBRARIES = list of libraries to link
+
+#=============================================================================
+# Copyright 2002-2011 Kitware, Inc.
+#
+# Distributed under the OSI-approved BSD License (the "License");
+# see accompanying file Copyright.txt for details.
+#
+# This software is distributed WITHOUT ANY WARRANTY; without even the
+# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the License for more information.
+#=============================================================================
+# (To distribute this file outside of CMake, substitute the full
+# License text for the above reference.)
+
+MACRO(CHECK_FUNCTION_EXISTS_EX FUNCTION VARIABLE)
+ IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
+ SET(MACRO_CHECK_FUNCTION_DEFINITIONS
+ "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
+ MESSAGE(STATUS "Looking for ${FUNCTION}")
+ IF(CMAKE_REQUIRED_LIBRARIES)
+ SET(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
+ "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
+ ELSE(CMAKE_REQUIRED_LIBRARIES)
+ SET(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
+ ENDIF(CMAKE_REQUIRED_LIBRARIES)
+ IF(CMAKE_REQUIRED_INCLUDES)
+ SET(CHECK_FUNCTION_EXISTS_ADD_INCLUDES
+ "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
+ ELSE(CMAKE_REQUIRED_INCLUDES)
+ SET(CHECK_FUNCTION_EXISTS_ADD_INCLUDES)
+ ENDIF(CMAKE_REQUIRED_INCLUDES)
+ TRY_COMPILE(${VARIABLE}
+ ${CMAKE_BINARY_DIR}
+ ${PROJECT_SOURCE_DIR}/cmake/CheckFunctionExistsEx.c
+ COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
+ CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
+ "${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}"
+ "${CHECK_FUNCTION_EXISTS_ADD_INCLUDES}"
+ OUTPUT_VARIABLE OUTPUT)
+ IF(${VARIABLE})
+ SET(${VARIABLE} 1 CACHE INTERNAL "Have function ${FUNCTION}")
+ MESSAGE(STATUS "Looking for ${FUNCTION} - found")
+ FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
+ "Determining if the function ${FUNCTION} exists passed with the following output:\n"
+ "${OUTPUT}\n\n")
+ ELSE(${VARIABLE})
+ MESSAGE(STATUS "Looking for ${FUNCTION} - not found")
+ SET(${VARIABLE} "" CACHE INTERNAL "Have function ${FUNCTION}")
+ FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
+ "Determining if the function ${FUNCTION} exists failed with the following output:\n"
+ "${OUTPUT}\n\n")
+ ENDIF(${VARIABLE})
+ ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
+ENDMACRO(CHECK_FUNCTION_EXISTS_EX)
diff --git a/cmake/CheckPrototypeDefinition.c.in b/cmake/CheckPrototypeDefinition.c.in
new file mode 100644
index 00000000..a97344ac
--- /dev/null
+++ b/cmake/CheckPrototypeDefinition.c.in
@@ -0,0 +1,29 @@
+@CHECK_PROTOTYPE_DEFINITION_HEADER@
+
+static void cmakeRequireSymbol(int dummy, ...) {
+ (void) dummy;
+}
+
+static void checkSymbol(void) {
+#ifndef @CHECK_PROTOTYPE_DEFINITION_SYMBOL@
+ cmakeRequireSymbol(0, &@CHECK_PROTOTYPE_DEFINITION_SYMBOL@);
+#endif
+}
+
+@CHECK_PROTOTYPE_DEFINITION_PROTO@ {
+ return @CHECK_PROTOTYPE_DEFINITION_RETURN@;
+}
+
+#ifdef __CLASSIC_C__
+int main() {
+ int ac;
+ char*av[];
+#else
+int main(int ac, char *av[]) {
+#endif
+ checkSymbol();
+ if (ac > 1000) {
+ return *av[0];
+ }
+ return 0;
+}
diff --git a/cmake/CheckPrototypeDefinition.cmake b/cmake/CheckPrototypeDefinition.cmake
new file mode 100644
index 00000000..e0c6a572
--- /dev/null
+++ b/cmake/CheckPrototypeDefinition.cmake
@@ -0,0 +1,84 @@
+# - Check if the protoype we expect is correct.
+# check_prototype_definition(FUNCTION PROTOTYPE RETURN HEADER VARIABLE)
+#
+# FUNCTION - The name of the function (used to check if prototype exists)
+# PROTOTYPE- The prototype to check.
+# RETURN - The return value of the function.
+# HEADER - The header files required.
+# VARIABLE - The variable to store the result.
+#
+# Example:
+#
+# check_prototype_definition(getpwent_r
+# "struct passwd *getpwent_r(struct passwd *src, char *buf, int buflen)"
+# "NULL"
+# "unistd.h;pwd.h"
+# SOLARIS_GETPWENT_R)
+#
+# The following variables may be set before calling this macro to
+# modify the way the check is run:
+#
+# CMAKE_REQUIRED_FLAGS = string of compile command line flags
+# CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
+# CMAKE_REQUIRED_INCLUDES = list of include directories
+# CMAKE_REQUIRED_LIBRARIES = list of libraries to link
+
+
+function(CHECK_PROTOTYPE_DEFINITION _FUNCTION _PROTOTYPE _RETURN _HEADER _VARIABLE)
+
+ if ("${_VARIABLE}" MATCHES "^${_VARIABLE}$")
+ set(CHECK_PROTOTYPE_DEFINITION_CONTENT "/* */\n")
+
+ set(CHECK_PROTOTYPE_DEFINITION_FLAGS ${CMAKE_REQUIRED_FLAGS})
+ if (CMAKE_REQUIRED_LIBRARIES)
+ set(CHECK_PROTOTYPE_DEFINITION_LIBS
+ "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
+ else(CMAKE_REQUIRED_LIBRARIES)
+ set(CHECK_PROTOTYPE_DEFINITION_LIBS)
+ endif(CMAKE_REQUIRED_LIBRARIES)
+ if (CMAKE_REQUIRED_INCLUDES)
+ set(CMAKE_SYMBOL_EXISTS_INCLUDES
+ "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
+ else(CMAKE_REQUIRED_INCLUDES)
+ set(CMAKE_SYMBOL_EXISTS_INCLUDES)
+ endif(CMAKE_REQUIRED_INCLUDES)
+
+ foreach(_FILE ${_HEADER})
+ set(CHECK_PROTOTYPE_DEFINITION_HEADER
+ "${CHECK_PROTOTYPE_DEFINITION_HEADER}#include <${_FILE}>\n")
+ endforeach(_FILE)
+
+ set(CHECK_PROTOTYPE_DEFINITION_SYMBOL ${_FUNCTION})
+ set(CHECK_PROTOTYPE_DEFINITION_PROTO ${_PROTOTYPE})
+ set(CHECK_PROTOTYPE_DEFINITION_RETURN ${_RETURN})
+
+ configure_file("${PROJECT_SOURCE_DIR}/cmake/CheckPrototypeDefinition.c.in"
+ "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c" @ONLY)
+
+ file(READ ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c _SOURCE)
+
+ try_compile(${_VARIABLE}
+ ${CMAKE_BINARY_DIR}
+ ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c
+ COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
+ CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${CHECK_PROTOTYPE_DEFINITION_FLAGS}
+ "${CHECK_PROTOTYPE_DEFINITION_LIBS}"
+ "${CMAKE_SYMBOL_EXISTS_INCLUDES}"
+ OUTPUT_VARIABLE OUTPUT)
+
+ if (${_VARIABLE})
+ set(${_VARIABLE} 1 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
+ message(STATUS "Checking prototype ${_FUNCTION} for ${_VARIABLE} - True")
+ file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
+ "Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} passed with the following output:\n"
+ "${OUTPUT}\n\n")
+ else (${_VARIABLE})
+ message(STATUS "Checking prototype ${_FUNCTION} for ${_VARIABLE} - False")
+ set(${_VARIABLE} 0 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
+ file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
+ "Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} failed with the following output:\n"
+ "${OUTPUT}\n\n${_SOURCE}\n\n")
+ endif (${_VARIABLE})
+ endif("${_VARIABLE}" MATCHES "^${_VARIABLE}$")
+
+endfunction(CHECK_PROTOTYPE_DEFINITION)
diff --git a/cmake/CheckWorkingKqueue.cmake b/cmake/CheckWorkingKqueue.cmake
new file mode 100644
index 00000000..47bf4e83
--- /dev/null
+++ b/cmake/CheckWorkingKqueue.cmake
@@ -0,0 +1,52 @@
+include(CheckCSourceRuns)
+
+check_c_source_runs(
+"
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/event.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+int
+main(int argc, char **argv)
+{
+ int kq;
+ int n;
+ int fd[2];
+ struct kevent ev;
+ struct timespec ts;
+ char buf[8000];
+
+ if (pipe(fd) == -1)
+ exit(1);
+ if (fcntl(fd[1], F_SETFL, O_NONBLOCK) == -1)
+ exit(1);
+
+ while ((n = write(fd[1], buf, sizeof(buf))) == sizeof(buf))
+ ;
+
+ if ((kq = kqueue()) == -1)
+ exit(1);
+
+ memset(&ev, 0, sizeof(ev));
+ ev.ident = fd[1];
+ ev.filter = EVFILT_WRITE;
+ ev.flags = EV_ADD | EV_ENABLE;
+ n = kevent(kq, &ev, 1, NULL, 0, NULL);
+ if (n == -1)
+ exit(1);
+
+ read(fd[0], buf, sizeof(buf));
+
+ ts.tv_sec = 0;
+ ts.tv_nsec = 0;
+ n = kevent(kq, NULL, 0, &ev, 1, &ts);
+ if (n == -1 || n == 0)
+ exit(1);
+
+ exit(0);
+}
+
+" EVENT__HAVE_WORKING_KQUEUE) \ No newline at end of file
diff --git a/cmake/CodeCoverage.cmake b/cmake/CodeCoverage.cmake
new file mode 100644
index 00000000..969f2732
--- /dev/null
+++ b/cmake/CodeCoverage.cmake
@@ -0,0 +1,162 @@
+#
+# Boost Software License - Version 1.0 - August 17th, 2003
+#
+# Permission is hereby granted, free of charge, to any person or organization
+# obtaining a copy of the software and accompanying documentation covered by
+# this license (the "Software") to use, reproduce, display, distribute,
+# execute, and transmit the Software, and to prepare derivative works of the
+# Software, and to permit third-parties to whom the Software is furnished to
+# do so, all subject to the following:
+#
+# The copyright notices in the Software and this entire statement, including
+# the above license grant, this restriction and the following disclaimer,
+# must be included in all copies of the Software, in whole or in part, and
+# all derivative works of the Software, unless such copies or derivative
+# works are solely in the form of machine-executable object code generated by
+# a source language processor.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
+# SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
+# FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
+# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+# DEALINGS IN THE SOFTWARE.
+#
+# 2012-01-31, Lars Bilke
+# - Enable Code Coverage
+#
+# 2013-09-17, Joakim Söderberg
+# - Added support for Clang.
+# - Some additional usage instructions.
+#
+# USAGE:
+# 1. Copy this file into your cmake modules path.
+#
+# 2. Add the following line to your CMakeLists.txt:
+# INCLUDE(CodeCoverage)
+#
+# 3. Set compiler flags to turn off optimization and enable coverage:
+# SET(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
+# SET(CMAKE_C_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
+#
+# 3. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target
+# which runs your test executable and produces a lcov code coverage report:
+# Example:
+# SETUP_TARGET_FOR_COVERAGE(
+# my_coverage_target # Name for custom target.
+# test_driver # Name of the test driver executable that runs the tests.
+# # NOTE! This should always have a ZERO as exit code
+# # otherwise the coverage generation will not complete.
+# coverage # Name of output directory.
+# )
+#
+# 4. Build a Debug build:
+# cmake -DCMAKE_BUILD_TYPE=Debug ..
+# make
+# make my_coverage_target
+#
+#
+
+# Check prereqs
+FIND_PROGRAM( GCOV_PATH gcov )
+FIND_PROGRAM( LCOV_PATH lcov )
+FIND_PROGRAM( GENHTML_PATH genhtml )
+FIND_PROGRAM( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/tests)
+
+IF(NOT GCOV_PATH)
+ MESSAGE(FATAL_ERROR "gcov not found! Aborting...")
+ENDIF() # NOT GCOV_PATH
+
+IF(NOT CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_COMPILER_IS_GNUCXX)
+ # Clang version 3.0.0 and greater now supports gcov as well.
+ MESSAGE(WARNING "Compiler is not GNU gcc! Clang Version 3.0.0 and greater supports gcov as well, but older versions don't.")
+
+ IF(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
+ MESSAGE(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
+ ENDIF()
+ENDIF() # NOT CMAKE_COMPILER_IS_GNUCC
+
+IF ( NOT CMAKE_BUILD_TYPE STREQUAL "Debug" )
+ MESSAGE( WARNING "Code coverage results with an optimized (non-Debug) build may be misleading" )
+ENDIF() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
+
+
+# Param _targetname The name of new the custom make target
+# Param _testrunner The name of the target which runs the tests.
+# MUST return ZERO always, even on errors.
+# If not, no coverage report will be created!
+# Param _outputname lcov output is generated as _outputname.info
+# HTML report is generated in _outputname/index.html
+# Optional fourth parameter is passed as arguments to _testrunner
+# Pass them in list form, e.g.: "-j;2" for -j 2
+FUNCTION(SETUP_TARGET_FOR_COVERAGE _targetname _testrunner _outputname)
+
+ IF(NOT LCOV_PATH)
+ MESSAGE(FATAL_ERROR "lcov not found! Aborting...")
+ ENDIF() # NOT LCOV_PATH
+
+ IF(NOT GENHTML_PATH)
+ MESSAGE(FATAL_ERROR "genhtml not found! Aborting...")
+ ENDIF() # NOT GENHTML_PATH
+
+ # Setup target
+ ADD_CUSTOM_TARGET(${_targetname}
+
+ # Cleanup lcov
+ ${LCOV_PATH} --directory . --zerocounters
+
+ # Run tests
+ COMMAND ${_testrunner} ${ARGV3}
+
+ # Capturing lcov counters and generating report
+ COMMAND ${LCOV_PATH} --directory . --capture --output-file ${_outputname}.info
+ COMMAND ${LCOV_PATH} --remove ${_outputname}.info 'tests/*' '/usr/*' --output-file ${_outputname}.info.cleaned
+ COMMAND ${GENHTML_PATH} -o ${_outputname} ${_outputname}.info.cleaned
+ COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info ${_outputname}.info.cleaned
+
+ WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+ COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
+ )
+
+ # Show info where to find the report
+ ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
+ COMMAND ;
+ COMMENT "Open ./${_outputname}/index.html in your browser to view the coverage report."
+ )
+
+ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE
+
+# Param _targetname The name of new the custom make target
+# Param _testrunner The name of the target which runs the tests
+# Param _outputname cobertura output is generated as _outputname.xml
+# Optional fourth parameter is passed as arguments to _testrunner
+# Pass them in list form, e.g.: "-j;2" for -j 2
+FUNCTION(SETUP_TARGET_FOR_COVERAGE_COBERTURA _targetname _testrunner _outputname)
+
+ IF(NOT PYTHON_EXECUTABLE)
+ MESSAGE(FATAL_ERROR "Python not found! Aborting...")
+ ENDIF() # NOT PYTHON_EXECUTABLE
+
+ IF(NOT GCOVR_PATH)
+ MESSAGE(FATAL_ERROR "gcovr not found! Aborting...")
+ ENDIF() # NOT GCOVR_PATH
+
+ ADD_CUSTOM_TARGET(${_targetname}
+
+ # Run tests
+ ${_testrunner} ${ARGV3}
+
+ # Running gcovr
+ COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} -e '${CMAKE_SOURCE_DIR}/tests/' -o ${_outputname}.xml
+ WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+ COMMENT "Running gcovr to produce Cobertura code coverage report."
+ )
+
+ # Show info where to find the report
+ ADD_CUSTOM_COMMAND(TARGET ${_targetname} POST_BUILD
+ COMMAND ;
+ COMMENT "Cobertura code coverage report saved in ${_outputname}.xml."
+ )
+
+ENDFUNCTION() # SETUP_TARGET_FOR_COVERAGE_COBERTURA
diff --git a/cmake/Copyright.txt b/cmake/Copyright.txt
new file mode 100644
index 00000000..813124f0
--- /dev/null
+++ b/cmake/Copyright.txt
@@ -0,0 +1,57 @@
+CMake - Cross Platform Makefile Generator
+Copyright 2000-2013 Kitware, Inc.
+Copyright 2000-2011 Insight Software Consortium
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+* Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+* Neither the names of Kitware, Inc., the Insight Software Consortium,
+ nor the names of their contributors may be used to endorse or promote
+ products derived from this software without specific prior written
+ permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+------------------------------------------------------------------------------
+
+The above copyright and license notice applies to distributions of
+CMake in source and binary form. Some source files contain additional
+notices of original copyright by their contributors; see each source
+for details. Third-party software packages supplied with CMake under
+compatible licenses provide their own copyright notices documented in
+corresponding subdirectories.
+
+------------------------------------------------------------------------------
+
+CMake was initially developed by Kitware with the following sponsorship:
+
+ * National Library of Medicine at the National Institutes of Health
+ as part of the Insight Segmentation and Registration Toolkit (ITK).
+
+ * US National Labs (Los Alamos, Livermore, Sandia) ASC Parallel
+ Visualization Initiative.
+
+ * National Alliance for Medical Image Computing (NAMIC) is funded by the
+ National Institutes of Health through the NIH Roadmap for Medical Research,
+ Grant U54 EB005149.
+
+ * Kitware, Inc. \ No newline at end of file
diff --git a/cmake/LibeventConfig.cmake.in b/cmake/LibeventConfig.cmake.in
new file mode 100644
index 00000000..b28cacb5
--- /dev/null
+++ b/cmake/LibeventConfig.cmake.in
@@ -0,0 +1,17 @@
+# - Config file for the Libevent package
+# It defines the following variables
+# LIBEVENT_INCLUDE_DIRS - include directories for FooBar
+# LIBEVENT_LIBRARIES - libraries to link against
+
+# Get the path of the current file.
+get_filename_component(LIBEVENT_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
+
+# Set the include directories.
+set(LIBEVENT_INCLUDE_DIRS "@EVENT_INSTALL_INCLUDE_DIR@")
+
+# Include the project Targets file, this contains definitions for IMPORTED targets.
+include(${LIBEVENT_CMAKE_DIR}/LibeventTargets.cmake)
+
+# IMPORTED targets from LibeventTargets.cmake
+set(LIBEVENT_LIBRARIES event event_core event_extra)
+
diff --git a/cmake/LibeventConfigVersion.cmake.in b/cmake/LibeventConfigVersion.cmake.in
new file mode 100644
index 00000000..56371a8f
--- /dev/null
+++ b/cmake/LibeventConfigVersion.cmake.in
@@ -0,0 +1,11 @@
+set(PACKAGE_VERSION "@EVENT_PACKAGE_VERSION@")
+
+# Check whether the requested PACKAGE_FIND_VERSION is compatible
+if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
+ set(PACKAGE_VERSION_COMPATIBLE FALSE)
+else()
+ set(PACKAGE_VERSION_COMPATIBLE TRUE)
+ if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")
+ set(PACKAGE_VERSION_EXACT TRUE)
+ endif()
+endif()
diff --git a/compat/sys/queue.h b/compat/sys/queue.h
index 53dd10d9..c387bdcf 100644
--- a/compat/sys/queue.h
+++ b/compat/sys/queue.h
@@ -32,8 +32,8 @@
* @(#)queue.h 8.5 (Berkeley) 8/20/94
*/
-#ifndef _SYS_QUEUE_H_
-#define _SYS_QUEUE_H_
+#ifndef SYS_QUEUE_H__
+#define SYS_QUEUE_H__
/*
* This file defines five types of data structures: singly-linked lists,
@@ -93,7 +93,7 @@ struct name { \
#define SLIST_HEAD_INITIALIZER(head) \
{ NULL }
-#ifndef WIN32
+#ifndef _WIN32
#define SLIST_ENTRY(type) \
struct { \
struct type *sle_next; /* next element */ \
@@ -485,4 +485,4 @@ struct { \
(elm2)->field.cqe_prev->field.cqe_next = (elm2); \
} while (0)
-#endif /* !_SYS_QUEUE_H_ */
+#endif /* !SYS_QUEUE_H__ */
diff --git a/configure.ac b/configure.ac
index a00a7677..8027d6dd 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,4 +1,3 @@
-dnl configure.in for libevent
dnl Copyright 2000-2007 Niels Provos
dnl Copyright 2007-2012 Niels Provos and Nick Mathewson
dnl
@@ -6,34 +5,44 @@ dnl See LICENSE for copying information.
dnl
dnl Original version Dug Song <dugsong@monkey.org>
+AC_INIT(libevent,2.1.4-alpha-dev)
AC_PREREQ(2.59)
-AC_INIT(event.c)
+AC_CONFIG_SRCDIR(event.c)
AC_CONFIG_MACRO_DIR([m4])
-
-AM_INIT_AUTOMAKE(libevent,2.0.21-stable-dev)
-AC_CONFIG_HEADERS(config.h)
-AC_DEFINE(NUMERIC_VERSION, 0x02001501, [Numeric representation of the version])
+AM_INIT_AUTOMAKE
+dnl AM_SILENT_RULES req. automake 1.11. [no] defaults V=1
+m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
+AC_CONFIG_HEADERS(config.h evconfig-private.h:evconfig-private.h.in)
+AC_DEFINE(NUMERIC_VERSION, 0x02010401, [Numeric representation of the version])
dnl Initialize prefix.
if test "$prefix" = "NONE"; then
prefix="/usr/local"
fi
+dnl Try and get a full POSIX environment on obscure systems
+ifdef([AC_USE_SYSTEM_EXTENSIONS], [
+AC_USE_SYSTEM_EXTENSIONS
+], [
+AC_AIX
+AC_GNU_SOURCE
+AC_MINIX
+])
+
AC_CANONICAL_BUILD
AC_CANONICAL_HOST
dnl the 'build' machine is where we run configure and compile
dnl the 'host' machine is where the resulting stuff runs.
-case "$host_os" in
-
- osf5*)
- CFLAGS="$CFLAGS -D_OSF_SOURCE"
- ;;
-esac
+#case "$host_os" in
+#
+# osf5*)
+# CFLAGS="$CFLAGS -D_OSF_SOURCE"
+# ;;
+#esac
dnl Checks for programs.
-AC_PROG_CC
AM_PROG_CC_C_O
AC_PROG_INSTALL
AC_PROG_LN_S
@@ -74,7 +83,16 @@ case "$host_os" in
esac
AC_ARG_ENABLE(gcc-warnings,
- AS_HELP_STRING(--enable-gcc-warnings, enable verbose warnings with GCC))
+ AS_HELP_STRING(--disable-gcc-warnings, disable verbose warnings with GCC))
+
+AC_ARG_ENABLE(gcc-hardening,
+ AS_HELP_STRING(--enable-gcc-hardening, enable compiler security checks),
+[if test x$enableval = xyes; then
+ CFLAGS="$CFLAGS -D_FORTIFY_SOURCE=2 -fstack-protector-all"
+ CFLAGS="$CFLAGS -fwrapv -fPIE -Wstack-protector"
+ CFLAGS="$CFLAGS --param ssp-buffer-size=1"
+fi])
+
AC_ARG_ENABLE(thread-support,
AS_HELP_STRING(--disable-thread-support, disable support for threading),
[], [enable_thread_support=yes])
@@ -93,19 +111,26 @@ AC_ARG_ENABLE([libevent-install],
AC_ARG_ENABLE([libevent-regress],
AS_HELP_STRING([--disable-libevent-regress, skip regress in make check]),
[], [enable_libevent_regress=yes])
+AC_ARG_ENABLE([samples],
+ AS_HELP_STRING([--disable-samples, skip building of sample programs]),
+ [], [enable_samples=yes])
AC_ARG_ENABLE([function-sections],
AS_HELP_STRING([--enable-function-sections, make static library allow smaller binaries with --gc-sections]),
[], [enable_function_sections=no])
+AC_ARG_ENABLE([verbose-debug],
+ AS_HELP_STRING([--enable-verbose-debug, verbose debug logging]),
+ [], [enable_verbose_debug=no])
AC_PROG_LIBTOOL
-dnl Uncomment "AC_DISABLE_SHARED" to make shared librraries not get
+dnl Uncomment "AC_DISABLE_SHARED" to make shared libraries not get
dnl built by default. You can also turn shared libs on and off from
dnl the command line with --enable-shared and --disable-shared.
dnl AC_DISABLE_SHARED
AC_SUBST(LIBTOOL_DEPS)
+AM_CONDITIONAL([BUILD_SAMPLES], [test "$enable_samples" = "yes"])
AM_CONDITIONAL([BUILD_REGRESS], [test "$enable_libevent_regress" = "yes"])
dnl Checks for libraries.
@@ -115,12 +140,12 @@ AC_SEARCH_LIBS([inet_aton], [resolv])
AC_SEARCH_LIBS([clock_gettime], [rt])
AC_SEARCH_LIBS([sendfile], [sendfile])
-dnl - check if the macro WIN32 is defined on this compiler.
-dnl - (this is how we check for a windows version of GCC)
+dnl - check if the macro _WIN32 is defined on this compiler.
+dnl - (this is how we check for a windows compiler)
AC_MSG_CHECKING(for WIN32)
AC_TRY_COMPILE(,
[
-#ifndef WIN32
+#ifndef _WIN32
die horribly
#endif
],
@@ -171,27 +196,43 @@ AC_SUBST(EV_LIB_WS32)
AC_SUBST(EV_LIB_GDI)
AC_SUBST(OPENSSL_LIBADD)
-AC_CHECK_HEADERS([openssl/bio.h])
+AC_SYS_LARGEFILE
-if test "$enable_openssl" = "yes"; then
-save_LIBS="$LIBS"
-LIBS=""
-OPENSSL_LIBS=""
-have_openssl=no
-AC_SEARCH_LIBS([SSL_new], [ssl],
- [have_openssl=yes
- OPENSSL_LIBS="$LIBS -lcrypto $EV_LIB_GDI $EV_LIB_WS32 $OPENSSL_LIBADD"
- AC_DEFINE(HAVE_OPENSSL, 1, [Define if the system has openssl])],
- [have_openssl=no],
- [-lcrypto $EV_LIB_GDI $EV_LIB_WS32 $OPENSSL_LIBADD])
-LIBS="$save_LIBS"
-AC_SUBST(OPENSSL_LIBS)
-fi
+LIBEVENT_OPENSSL
dnl Checks for header files.
-AC_HEADER_STDC
-AC_CHECK_HEADERS([fcntl.h stdarg.h inttypes.h stdint.h stddef.h poll.h unistd.h sys/epoll.h sys/time.h sys/queue.h sys/event.h sys/param.h sys/ioctl.h sys/select.h sys/devpoll.h port.h netinet/in.h netinet/in6.h sys/socket.h sys/uio.h arpa/inet.h sys/eventfd.h sys/mman.h sys/sendfile.h sys/wait.h netdb.h])
-AC_CHECK_HEADERS([sys/stat.h])
+AC_CHECK_HEADERS([ \
+ arpa/inet.h \
+ fcntl.h \
+ ifaddrs.h \
+ mach/mach_time.h \
+ netdb.h \
+ netinet/in.h \
+ netinet/in6.h \
+ netinet/tcp.h \
+ poll.h \
+ port.h \
+ stdarg.h \
+ stddef.h \
+ sys/devpoll.h \
+ sys/epoll.h \
+ sys/event.h \
+ sys/eventfd.h \
+ sys/ioctl.h \
+ sys/mman.h \
+ sys/param.h \
+ sys/queue.h \
+ sys/resource.h \
+ sys/select.h \
+ sys/sendfile.h \
+ sys/socket.h \
+ sys/stat.h \
+ sys/time.h \
+ sys/timerfd.h \
+ sys/uio.h \
+ sys/wait.h \
+])
+
AC_CHECK_HEADERS(sys/sysctl.h, [], [], [
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
@@ -289,9 +330,47 @@ AC_C_INLINE
AC_HEADER_TIME
dnl Checks for library functions.
-AC_CHECK_FUNCS([gettimeofday vasprintf fcntl clock_gettime strtok_r strsep])
-AC_CHECK_FUNCS([getnameinfo strlcpy inet_ntop inet_pton signal sigaction strtoll inet_aton pipe eventfd sendfile mmap splice arc4random arc4random_buf issetugid geteuid getegid getprotobynumber setenv unsetenv putenv sysctl])
-AC_CHECK_FUNCS([umask])
+AC_CHECK_FUNCS([ \
+ accept4 \
+ arc4random \
+ arc4random_buf \
+ clock_gettime \
+ eventfd \
+ epoll_create1 \
+ fcntl \
+ getegid \
+ geteuid \
+ getifaddrs \
+ getnameinfo \
+ getprotobynumber \
+ gettimeofday \
+ inet_ntop \
+ inet_pton \
+ issetugid \
+ mach_absolute_time \
+ mmap \
+ nanosleep \
+ pipe \
+ pipe2 \
+ putenv \
+ sendfile \
+ setenv \
+ setrlimit \
+ sigaction \
+ signal \
+ splice \
+ strlcpy \
+ strsep \
+ strtok_r \
+ strtoll \
+ sysctl \
+ timerfd_create \
+ umask \
+ unsetenv \
+ usleep \
+ vasprintf \
+])
+AM_CONDITIONAL(STRLCPY_IMPL, [test x"$ac_cv_func_strlcpy" = xno])
AC_CACHE_CHECK(
[for getaddrinfo],
@@ -374,8 +453,6 @@ AC_CHECK_FUNC(gethostbyname_r, [
fi
-AC_CHECK_SIZEOF(long)
-
AC_MSG_CHECKING(for F_SETFD in fcntl.h)
AC_EGREP_CPP(yes,
[
@@ -558,6 +635,7 @@ AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(size_t)
AC_CHECK_SIZEOF(void *)
+AC_CHECK_SIZEOF(off_t)
AC_CHECK_TYPES([struct in6_addr, struct sockaddr_in6, sa_family_t, struct addrinfo, struct sockaddr_storage], , ,
[#define _GNU_SOURCE
@@ -574,7 +652,7 @@ AC_CHECK_TYPES([struct in6_addr, struct sockaddr_in6, sa_family_t, struct addrin
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
-#ifdef WIN32
+#ifdef _WIN32
#define WIN32_WINNT 0x400
#define _WIN32_WINNT 0x400
#define WIN32_LEAN_AND_MEAN
@@ -597,7 +675,7 @@ AC_CHECK_MEMBERS([struct in6_addr.s6_addr32, struct in6_addr.s6_addr16, struct s
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
-#ifdef WIN32
+#ifdef _WIN32
#define WIN32_WINNT 0x400
#define _WIN32_WINNT 0x400
#define WIN32_LEAN_AND_MEAN
@@ -610,6 +688,14 @@ AC_CHECK_MEMBERS([struct in6_addr.s6_addr32, struct in6_addr.s6_addr16, struct s
#endif
])
+AC_CHECK_TYPES([struct so_linger],
+[#define HAVE_SO_LINGER], ,
+[
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+])
+
AC_MSG_CHECKING([for socklen_t])
AC_TRY_COMPILE([
#include <sys/types.h>
@@ -670,12 +756,17 @@ if test x$enable_debug_mode = xno; then
[Define if libevent should build without support for a debug mode])
fi
+# check if we should enable verbose debugging
+if test x$enable_verbose_debug = xyes; then
+ CFLAGS="$CFLAGS -DUSE_DEBUG"
+fi
+
# check if we have and should use openssl
AM_CONDITIONAL(OPENSSL, [test "$enable_openssl" != "no" && test "$have_openssl" = "yes"])
# Add some more warnings which we use in development but not in the
# released versions. (Some relevant gcc versions can't handle these.)
-if test x$enable_gcc_warnings = xyes && test "$GCC" = "yes"; then
+if test x$enable_gcc_warnings != xno && test "$GCC" = "yes"; then
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [
#if !defined(__GNUC__) || (__GNUC__ < 4)
@@ -697,7 +788,11 @@ if test x$enable_gcc_warnings = xyes && test "$GCC" = "yes"; then
#error
#endif])], have_clang=yes, have_clang=no)
- CFLAGS="$CFLAGS -W -Wfloat-equal -Wundef -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes -Wwrite-strings -Wredundant-decls -Wchar-subscripts -Wcomment -Wformat -Wwrite-strings -Wmissing-declarations -Wredundant-decls -Wnested-externs -Wbad-function-cast -Wswitch-enum -Werror"
+ CFLAGS="$CFLAGS -W -Wfloat-equal -Wundef -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes -Wwrite-strings -Wredundant-decls -Wchar-subscripts -Wcomment -Wformat -Wwrite-strings -Wmissing-declarations -Wredundant-decls -Wnested-externs -Wbad-function-cast -Wswitch-enum"
+ if test x$enable_gcc_warnings = xyes; then
+ CFLAGS="$CFLAGS -Werror"
+ fi
+
CFLAGS="$CFLAGS -Wno-unused-parameter -Wstrict-aliasing"
if test x$have_gcc4 = xyes ; then
@@ -817,4 +912,4 @@ AC_SUBST([LIBEVENT_GC_SECTIONS])
AM_CONDITIONAL([INSTALL_LIBEVENT], [test "$enable_libevent_install" = "yes"])
AC_CONFIG_FILES( [libevent.pc libevent_openssl.pc libevent_pthreads.pc] )
-AC_OUTPUT(Makefile include/Makefile test/Makefile sample/Makefile)
+AC_OUTPUT(Makefile)
diff --git a/defer-internal.h b/defer-internal.h
index 96a5cacc..d8cf32f4 100644
--- a/defer-internal.h
+++ b/defer-internal.h
@@ -23,78 +23,48 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _DEFER_INTERNAL_H_
-#define _DEFER_INTERNAL_H_
+#ifndef DEFER_INTERNAL_H_INCLUDED_
+#define DEFER_INTERNAL_H_INCLUDED_
#ifdef __cplusplus
extern "C" {
#endif
#include "event2/event-config.h"
-#include <sys/queue.h>
-
-struct deferred_cb;
-
-typedef void (*deferred_cb_fn)(struct deferred_cb *, void *);
-
-/** A deferred_cb is a callback that can be scheduled to run as part of
- * an event_base's event_loop, rather than running immediately. */
-struct deferred_cb {
- /** Links to the adjacent active (pending) deferred_cb objects. */
- TAILQ_ENTRY (deferred_cb) cb_next;
- /** True iff this deferred_cb is pending in an event_base. */
- unsigned queued : 1;
- /** The function to execute when the callback runs. */
- deferred_cb_fn cb;
- /** The function's second argument. */
- void *arg;
-};
-
-/** A deferred_cb_queue is a list of deferred_cb that we can add to and run. */
-struct deferred_cb_queue {
- /** Lock used to protect the queue. */
- void *lock;
+#include "evconfig-private.h"
- /** How many entries are in the queue? */
- int active_count;
-
- /** Function called when adding to the queue from another thread. */
- void (*notify_fn)(struct deferred_cb_queue *, void *);
- void *notify_arg;
+#include <sys/queue.h>
- /** Deferred callback management: a list of deferred callbacks to
- * run active the active events. */
- TAILQ_HEAD (deferred_cb_list, deferred_cb) deferred_cb_list;
-};
+struct event_callback;
+typedef void (*deferred_cb_fn)(struct event_callback *, void *);
/**
- Initialize an empty, non-pending deferred_cb.
+ Initialize an empty, non-pending event_callback.
- @param deferred The deferred_cb structure to initialize.
- @param cb The function to run when the deferred_cb executes.
+ @param deferred The struct event_callback structure to initialize.
+ @param priority The priority that the callback should run at.
+ @param cb The function to run when the struct event_callback executes.
@param arg The function's second argument.
*/
-void event_deferred_cb_init(struct deferred_cb *, deferred_cb_fn, void *);
+void event_deferred_cb_init_(struct event_callback *, ev_uint8_t, deferred_cb_fn, void *);
/**
- Cancel a deferred_cb if it is currently scheduled in an event_base.
+ Change the priority of a non-pending event_callback.
*/
-void event_deferred_cb_cancel(struct deferred_cb_queue *, struct deferred_cb *);
+void event_deferred_cb_set_priority_(struct event_callback *, ev_uint8_t);
/**
- Activate a deferred_cb if it is not currently scheduled in an event_base.
+ Cancel a struct event_callback if it is currently scheduled in an event_base.
*/
-void event_deferred_cb_schedule(struct deferred_cb_queue *, struct deferred_cb *);
+void event_deferred_cb_cancel_(struct event_base *, struct event_callback *);
+/**
+ Activate a struct event_callback if it is not currently scheduled in an event_base.
-#define LOCK_DEFERRED_QUEUE(q) \
- EVLOCK_LOCK((q)->lock, 0)
-#define UNLOCK_DEFERRED_QUEUE(q) \
- EVLOCK_UNLOCK((q)->lock, 0)
+ Return true iff it was not previously scheduled.
+ */
+int event_deferred_cb_schedule_(struct event_base *, struct event_callback *);
#ifdef __cplusplus
}
#endif
-void event_deferred_cb_queue_init(struct deferred_cb_queue *);
-struct deferred_cb_queue *event_base_get_deferred_cb_queue(struct event_base *);
-
-#endif /* _EVENT_INTERNAL_H_ */
+#endif /* EVENT_INTERNAL_H_INCLUDED_ */
diff --git a/devpoll.c b/devpoll.c
index f99cc013..3a2f86d6 100644
--- a/devpoll.c
+++ b/devpoll.c
@@ -25,10 +25,13 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
+
+#ifdef EVENT__HAVE_DEVPOLL
#include <sys/types.h>
#include <sys/resource.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
@@ -129,7 +132,7 @@ devpoll_init(struct event_base *base)
nfiles = rl.rlim_cur;
/* Initialize the kernel queue */
- if ((dpfd = evutil_open_closeonexec("/dev/poll", O_RDWR, 0)) == -1) {
+ if ((dpfd = evutil_open_closeonexec_("/dev/poll", O_RDWR, 0)) == -1) {
event_warn("open: /dev/poll");
mm_free(devpollop);
return (NULL);
@@ -156,7 +159,7 @@ devpoll_init(struct event_base *base)
return (NULL);
}
- evsig_init(base);
+ evsig_init_(base);
return (devpollop);
}
@@ -214,7 +217,7 @@ devpoll_dispatch(struct event_base *base, struct timeval *tv)
continue;
/* XXX(niels): not sure if this works for devpoll */
- evmap_io_active(base, events[i].fd, which);
+ evmap_io_active_(base, events[i].fd, which);
}
return (0);
@@ -293,7 +296,7 @@ devpoll_dealloc(struct event_base *base)
{
struct devpollop *devpollop = base->evbase;
- evsig_dealloc(base);
+ evsig_dealloc_(base);
if (devpollop->events)
mm_free(devpollop->events);
if (devpollop->changes)
@@ -304,3 +307,5 @@ devpoll_dealloc(struct event_base *base)
memset(devpollop, 0, sizeof(struct devpollop));
mm_free(devpollop);
}
+
+#endif /* EVENT__HAVE_DEVPOLL */
diff --git a/epoll.c b/epoll.c
index 4c5c3da0..aa41f84e 100644
--- a/epoll.c
+++ b/epoll.c
@@ -25,11 +25,14 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
+
+#ifdef EVENT__HAVE_EPOLL
#include <stdint.h>
#include <sys/types.h>
#include <sys/resource.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
@@ -41,9 +44,12 @@
#include <string.h>
#include <unistd.h>
#include <errno.h>
-#ifdef _EVENT_HAVE_FCNTL_H
+#ifdef EVENT__HAVE_FCNTL_H
#include <fcntl.h>
#endif
+#ifdef EVENT__HAVE_SYS_TIMERFD_H
+#include <sys/timerfd.h>
+#endif
#include "event-internal.h"
#include "evsignal-internal.h"
@@ -52,11 +58,38 @@
#include "log-internal.h"
#include "evmap-internal.h"
#include "changelist-internal.h"
+#include "time-internal.h"
+
+/* Since Linux 2.6.17, epoll is able to report about peer half-closed connection
+ using special EPOLLRDHUP flag on a read event.
+*/
+#if !defined(EPOLLRDHUP)
+#define EPOLLRDHUP 0
+#define EARLY_CLOSE_IF_HAVE_RDHUP 0
+#else
+#define EARLY_CLOSE_IF_HAVE_RDHUP EV_FEATURE_EARLY_CLOSE
+#endif
+
+#include "epolltable-internal.h"
+
+#if defined(EVENT__HAVE_SYS_TIMERFD_H) && \
+ defined(EVENT__HAVE_TIMERFD_CREATE) && \
+ defined(HAVE_POSIX_MONOTONIC) && defined(TFD_NONBLOCK) && \
+ defined(TFD_CLOEXEC)
+/* Note that we only use timerfd if TFD_NONBLOCK and TFD_CLOEXEC are available
+ and working. This means that we can't support it on 2.6.25 (where timerfd
+ was introduced) or 2.6.26, since 2.6.27 introduced those flags.
+ */
+#define USING_TIMERFD
+#endif
struct epollop {
struct epoll_event *events;
int nevents;
int epfd;
+#ifdef USING_TIMERFD
+ int timerfd;
+#endif
};
static void *epoll_init(struct event_base *);
@@ -66,12 +99,12 @@ static void epoll_dealloc(struct event_base *);
static const struct eventop epollops_changelist = {
"epoll (with changelist)",
epoll_init,
- event_changelist_add,
- event_changelist_del,
+ event_changelist_add_,
+ event_changelist_del_,
epoll_dispatch,
epoll_dealloc,
1, /* need reinit */
- EV_FEATURE_ET|EV_FEATURE_O1,
+ EV_FEATURE_ET|EV_FEATURE_O1| EARLY_CLOSE_IF_HAVE_RDHUP,
EVENT_CHANGELIST_FDINFO_SIZE
};
@@ -89,7 +122,7 @@ const struct eventop epollops = {
epoll_dispatch,
epoll_dealloc,
1, /* need reinit */
- EV_FEATURE_ET|EV_FEATURE_O1,
+ EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_EARLY_CLOSE,
0
};
@@ -107,19 +140,24 @@ const struct eventop epollops = {
static void *
epoll_init(struct event_base *base)
{
- int epfd;
+ int epfd = -1;
struct epollop *epollop;
- /* Initialize the kernel queue. (The size field is ignored since
- * 2.6.8.) */
- if ((epfd = epoll_create(32000)) == -1) {
- if (errno != ENOSYS)
- event_warn("epoll_create");
- return (NULL);
+#ifdef EVENT__HAVE_EPOLL_CREATE1
+ /* First, try the shiny new epoll_create1 interface, if we have it. */
+ epfd = epoll_create1(EPOLL_CLOEXEC);
+#endif
+ if (epfd == -1) {
+ /* Initialize the kernel queue using the old interface. (The
+ size field is ignored since 2.6.8.) */
+ if ((epfd = epoll_create(32000)) == -1) {
+ if (errno != ENOSYS)
+ event_warn("epoll_create");
+ return (NULL);
+ }
+ evutil_make_socket_closeonexec(epfd);
}
- evutil_make_socket_closeonexec(epfd);
-
if (!(epollop = mm_calloc(1, sizeof(struct epollop)))) {
close(epfd);
return (NULL);
@@ -138,10 +176,48 @@ epoll_init(struct event_base *base)
if ((base->flags & EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST) != 0 ||
((base->flags & EVENT_BASE_FLAG_IGNORE_ENV) == 0 &&
- evutil_getenv("EVENT_EPOLL_USE_CHANGELIST") != NULL))
+ evutil_getenv_("EVENT_EPOLL_USE_CHANGELIST") != NULL)) {
+
base->evsel = &epollops_changelist;
+ }
- evsig_init(base);
+#ifdef USING_TIMERFD
+ /*
+ The epoll interface ordinarily gives us one-millisecond precision,
+ so on Linux it makes perfect sense to use the CLOCK_MONOTONIC_COARSE
+ timer. But when the user has set the new PRECISE_TIMER flag for an
+ event_base, we can try to use timerfd to give them finer granularity.
+ */
+ if ((base->flags & EVENT_BASE_FLAG_PRECISE_TIMER) &&
+ base->monotonic_timer.monotonic_clock == CLOCK_MONOTONIC) {
+ int fd;
+ fd = epollop->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
+ if (epollop->timerfd >= 0) {
+ struct epoll_event epev;
+ memset(&epev, 0, sizeof(epev));
+ epev.data.fd = epollop->timerfd;
+ epev.events = EPOLLIN;
+ if (epoll_ctl(epollop->epfd, EPOLL_CTL_ADD, fd, &epev) < 0) {
+ event_warn("epoll_ctl(timerfd)");
+ close(fd);
+ epollop->timerfd = -1;
+ }
+ } else {
+ if (errno != EINVAL && errno != ENOSYS) {
+ /* These errors probably mean that we were
+ * compiled with timerfd/TFD_* support, but
+ * we're running on a kernel that lacks those.
+ */
+ event_warn("timerfd_create");
+ }
+ epollop->timerfd = -1;
+ }
+ } else {
+ epollop->timerfd = -1;
+ }
+#endif
+
+ evsig_init_(base);
return (epollop);
}
@@ -177,156 +253,104 @@ epoll_apply_one_change(struct event_base *base,
{
struct epoll_event epev;
int op, events = 0;
+ int idx;
- if (1) {
- /* The logic here is a little tricky. If we had no events set
- on the fd before, we need to set op="ADD" and set
- events=the events we want to add. If we had any events set
- on the fd before, and we want any events to remain on the
- fd, we need to say op="MOD" and set events=the events we
- want to remain. But if we want to delete the last event,
- we say op="DEL" and set events=the remaining events. What
- fun!
- */
+ idx = EPOLL_OP_TABLE_INDEX(ch);
+ op = epoll_op_table[idx].op;
+ events = epoll_op_table[idx].events;
- /* TODO: Turn this into a switch or a table lookup. */
-
- if ((ch->read_change & EV_CHANGE_ADD) ||
- (ch->write_change & EV_CHANGE_ADD)) {
- /* If we are adding anything at all, we'll want to do
- * either an ADD or a MOD. */
- events = 0;
- op = EPOLL_CTL_ADD;
- if (ch->read_change & EV_CHANGE_ADD) {
- events |= EPOLLIN;
- } else if (ch->read_change & EV_CHANGE_DEL) {
- ;
- } else if (ch->old_events & EV_READ) {
- events |= EPOLLIN;
- }
- if (ch->write_change & EV_CHANGE_ADD) {
- events |= EPOLLOUT;
- } else if (ch->write_change & EV_CHANGE_DEL) {
- ;
- } else if (ch->old_events & EV_WRITE) {
- events |= EPOLLOUT;
- }
- if ((ch->read_change|ch->write_change) & EV_ET)
- events |= EPOLLET;
-
- if (ch->old_events) {
- /* If MOD fails, we retry as an ADD, and if
- * ADD fails we will retry as a MOD. So the
- * only hard part here is to guess which one
- * will work. As a heuristic, we'll try
- * MOD first if we think there were old
- * events and ADD if we think there were none.
- *
- * We can be wrong about the MOD if the file
- * has in fact been closed and re-opened.
- *
- * We can be wrong about the ADD if the
- * the fd has been re-created with a dup()
- * of the same file that it was before.
- */
- op = EPOLL_CTL_MOD;
- }
- } else if ((ch->read_change & EV_CHANGE_DEL) ||
- (ch->write_change & EV_CHANGE_DEL)) {
- /* If we're deleting anything, we'll want to do a MOD
- * or a DEL. */
- op = EPOLL_CTL_DEL;
-
- if (ch->read_change & EV_CHANGE_DEL) {
- if (ch->write_change & EV_CHANGE_DEL) {
- events = EPOLLIN|EPOLLOUT;
- } else if (ch->old_events & EV_WRITE) {
- events = EPOLLOUT;
- op = EPOLL_CTL_MOD;
- } else {
- events = EPOLLIN;
- }
- } else if (ch->write_change & EV_CHANGE_DEL) {
- if (ch->old_events & EV_READ) {
- events = EPOLLIN;
- op = EPOLL_CTL_MOD;
- } else {
- events = EPOLLOUT;
- }
- }
- }
+ if (!events) {
+ EVUTIL_ASSERT(op == 0);
+ return 0;
+ }
- if (!events)
- return 0;
+ if ((ch->read_change|ch->write_change) & EV_CHANGE_ET)
+ events |= EPOLLET;
+
+ memset(&epev, 0, sizeof(epev));
+ epev.data.fd = ch->fd;
+ epev.events = events;
+ if (epoll_ctl(epollop->epfd, op, ch->fd, &epev) == 0) {
+ event_debug(("Epoll %s(%d) on fd %d okay. [old events were %d; read change was %d; write change was %d; close change was %d]",
+ epoll_op_to_string(op),
+ (int)epev.events,
+ (int)ch->fd,
+ ch->old_events,
+ ch->read_change,
+ ch->write_change,
+ ch->close_change));
+ return 0;
+ }
- memset(&epev, 0, sizeof(epev));
- epev.data.fd = ch->fd;
- epev.events = events;
- if (epoll_ctl(epollop->epfd, op, ch->fd, &epev) == -1) {
- if (op == EPOLL_CTL_MOD && errno == ENOENT) {
- /* If a MOD operation fails with ENOENT, the
- * fd was probably closed and re-opened. We
- * should retry the operation as an ADD.
- */
- if (epoll_ctl(epollop->epfd, EPOLL_CTL_ADD, ch->fd, &epev) == -1) {
- event_warn("Epoll MOD(%d) on %d retried as ADD; that failed too",
- (int)epev.events, ch->fd);
- return -1;
- } else {
- event_debug(("Epoll MOD(%d) on %d retried as ADD; succeeded.",
- (int)epev.events,
- ch->fd));
- }
- } else if (op == EPOLL_CTL_ADD && errno == EEXIST) {
- /* If an ADD operation fails with EEXIST,
- * either the operation was redundant (as with a
- * precautionary add), or we ran into a fun
- * kernel bug where using dup*() to duplicate the
- * same file into the same fd gives you the same epitem
- * rather than a fresh one. For the second case,
- * we must retry with MOD. */
- if (epoll_ctl(epollop->epfd, EPOLL_CTL_MOD, ch->fd, &epev) == -1) {
- event_warn("Epoll ADD(%d) on %d retried as MOD; that failed too",
- (int)epev.events, ch->fd);
- return -1;
- } else {
- event_debug(("Epoll ADD(%d) on %d retried as MOD; succeeded.",
- (int)epev.events,
- ch->fd));
- }
- } else if (op == EPOLL_CTL_DEL &&
- (errno == ENOENT || errno == EBADF ||
- errno == EPERM)) {
- /* If a delete fails with one of these errors,
- * that's fine too: we closed the fd before we
- * got around to calling epoll_dispatch. */
- event_debug(("Epoll DEL(%d) on fd %d gave %s: DEL was unnecessary.",
- (int)epev.events,
- ch->fd,
- strerror(errno)));
+ switch (op) {
+ case EPOLL_CTL_MOD:
+ if (errno == ENOENT) {
+ /* If a MOD operation fails with ENOENT, the
+ * fd was probably closed and re-opened. We
+ * should retry the operation as an ADD.
+ */
+ if (epoll_ctl(epollop->epfd, EPOLL_CTL_ADD, ch->fd, &epev) == -1) {
+ event_warn("Epoll MOD(%d) on %d retried as ADD; that failed too",
+ (int)epev.events, ch->fd);
+ return -1;
} else {
- event_warn("Epoll %s(%d) on fd %d failed. Old events were %d; read change was %d (%s); write change was %d (%s)",
- epoll_op_to_string(op),
- (int)epev.events,
- ch->fd,
- ch->old_events,
- ch->read_change,
- change_to_string(ch->read_change),
- ch->write_change,
- change_to_string(ch->write_change));
+ event_debug(("Epoll MOD(%d) on %d retried as ADD; succeeded.",
+ (int)epev.events,
+ ch->fd));
+ return 0;
+ }
+ }
+ break;
+ case EPOLL_CTL_ADD:
+ if (errno == EEXIST) {
+ /* If an ADD operation fails with EEXIST,
+ * either the operation was redundant (as with a
+ * precautionary add), or we ran into a fun
+ * kernel bug where using dup*() to duplicate the
+ * same file into the same fd gives you the same epitem
+ * rather than a fresh one. For the second case,
+ * we must retry with MOD. */
+ if (epoll_ctl(epollop->epfd, EPOLL_CTL_MOD, ch->fd, &epev) == -1) {
+ event_warn("Epoll ADD(%d) on %d retried as MOD; that failed too",
+ (int)epev.events, ch->fd);
return -1;
+ } else {
+ event_debug(("Epoll ADD(%d) on %d retried as MOD; succeeded.",
+ (int)epev.events,
+ ch->fd));
+ return 0;
}
- } else {
- event_debug(("Epoll %s(%d) on fd %d okay. [old events were %d; read change was %d; write change was %d]",
- epoll_op_to_string(op),
+ }
+ break;
+ case EPOLL_CTL_DEL:
+ if (errno == ENOENT || errno == EBADF || errno == EPERM) {
+ /* If a delete fails with one of these errors,
+ * that's fine too: we closed the fd before we
+ * got around to calling epoll_dispatch. */
+ event_debug(("Epoll DEL(%d) on fd %d gave %s: DEL was unnecessary.",
(int)epev.events,
- (int)ch->fd,
- ch->old_events,
- ch->read_change,
- ch->write_change));
+ ch->fd,
+ strerror(errno)));
+ return 0;
}
+ break;
+ default:
+ break;
}
- return 0;
+
+ event_warn("Epoll %s(%d) on fd %d failed. Old events were %d; read change was %d (%s); write change was %d (%s); close change was %d (%s)",
+ epoll_op_to_string(op),
+ (int)epev.events,
+ ch->fd,
+ ch->old_events,
+ ch->read_change,
+ change_to_string(ch->read_change),
+ ch->write_change,
+ change_to_string(ch->write_change),
+ ch->close_change,
+ change_to_string(ch->close_change));
+
+ return -1;
}
static int
@@ -355,13 +379,16 @@ epoll_nochangelist_add(struct event_base *base, evutil_socket_t fd,
struct event_change ch;
ch.fd = fd;
ch.old_events = old;
- ch.read_change = ch.write_change = 0;
+ ch.read_change = ch.write_change = ch.close_change = 0;
if (events & EV_WRITE)
ch.write_change = EV_CHANGE_ADD |
(events & EV_ET);
if (events & EV_READ)
ch.read_change = EV_CHANGE_ADD |
(events & EV_ET);
+ if (events & EV_CLOSED)
+ ch.close_change = EV_CHANGE_ADD |
+ (events & EV_ET);
return epoll_apply_one_change(base, base->evbase, &ch);
}
@@ -373,11 +400,13 @@ epoll_nochangelist_del(struct event_base *base, evutil_socket_t fd,
struct event_change ch;
ch.fd = fd;
ch.old_events = old;
- ch.read_change = ch.write_change = 0;
+ ch.read_change = ch.write_change = ch.close_change = 0;
if (events & EV_WRITE)
ch.write_change = EV_CHANGE_DEL;
if (events & EV_READ)
ch.read_change = EV_CHANGE_DEL;
+ if (events & EV_CLOSED)
+ ch.close_change = EV_CHANGE_DEL;
return epoll_apply_one_change(base, base->evbase, &ch);
}
@@ -390,8 +419,35 @@ epoll_dispatch(struct event_base *base, struct timeval *tv)
int i, res;
long timeout = -1;
+#ifdef USING_TIMERFD
+ if (epollop->timerfd >= 0) {
+ struct itimerspec is;
+ is.it_interval.tv_sec = 0;
+ is.it_interval.tv_nsec = 0;
+ if (tv == NULL) {
+ /* No timeout; disarm the timer. */
+ is.it_value.tv_sec = 0;
+ is.it_value.tv_nsec = 0;
+ } else {
+ if (tv->tv_sec == 0 && tv->tv_usec == 0) {
+ /* we need to exit immediately; timerfd can't
+ * do that. */
+ timeout = 0;
+ }
+ is.it_value.tv_sec = tv->tv_sec;
+ is.it_value.tv_nsec = tv->tv_usec * 1000;
+ }
+ /* TODO: we could avoid unnecessary syscalls here by only
+ calling timerfd_settime when the top timeout changes, or
+ when we're called with a different timeval.
+ */
+ if (timerfd_settime(epollop->timerfd, 0, &is, NULL) < 0) {
+ event_warn("timerfd_settime");
+ }
+ } else
+#endif
if (tv != NULL) {
- timeout = evutil_tv_to_msec(tv);
+ timeout = evutil_tv_to_msec_(tv);
if (timeout < 0 || timeout > MAX_EPOLL_TIMEOUT_MSEC) {
/* Linux kernels can wait forever if the timeout is
* too big; see comment on MAX_EPOLL_TIMEOUT_MSEC. */
@@ -400,7 +456,7 @@ epoll_dispatch(struct event_base *base, struct timeval *tv)
}
epoll_apply_changes(base);
- event_changelist_remove_all(&base->changelist, base);
+ event_changelist_remove_all_(&base->changelist, base);
EVBASE_RELEASE_LOCK(base, th_base_lock);
@@ -423,6 +479,10 @@ epoll_dispatch(struct event_base *base, struct timeval *tv)
for (i = 0; i < res; i++) {
int what = events[i].events;
short ev = 0;
+#ifdef USING_TIMERFD
+ if (events[i].data.fd == epollop->timerfd)
+ continue;
+#endif
if (what & (EPOLLHUP|EPOLLERR)) {
ev = EV_READ | EV_WRITE;
@@ -431,12 +491,14 @@ epoll_dispatch(struct event_base *base, struct timeval *tv)
ev |= EV_READ;
if (what & EPOLLOUT)
ev |= EV_WRITE;
+ if (what & EPOLLRDHUP)
+ ev |= EV_CLOSED;
}
if (!ev)
continue;
- evmap_io_active(base, events[i].data.fd, ev | EV_ET);
+ evmap_io_active_(base, events[i].data.fd, ev | EV_ET);
}
if (res == epollop->nevents && epollop->nevents < MAX_NEVENT) {
@@ -462,12 +524,18 @@ epoll_dealloc(struct event_base *base)
{
struct epollop *epollop = base->evbase;
- evsig_dealloc(base);
+ evsig_dealloc_(base);
if (epollop->events)
mm_free(epollop->events);
if (epollop->epfd >= 0)
close(epollop->epfd);
+#ifdef USING_TIMERFD
+ if (epollop->timerfd >= 0)
+ close(epollop->timerfd);
+#endif
memset(epollop, 0, sizeof(struct epollop));
mm_free(epollop);
}
+
+#endif /* EVENT__HAVE_EPOLL */
diff --git a/epoll_sub.c b/epoll_sub.c
index facbc09f..3f01f6a6 100644
--- a/epoll_sub.c
+++ b/epoll_sub.c
@@ -24,6 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "evconfig-private.h"
#include <stdint.h>
#include <sys/param.h>
diff --git a/epolltable-internal.h b/epolltable-internal.h
new file mode 100644
index 00000000..da30e097
--- /dev/null
+++ b/epolltable-internal.h
@@ -0,0 +1,1166 @@
+/*
+ * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
+ * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef EPOLLTABLE_INTERNAL_H_INCLUDED_
+#define EPOLLTABLE_INTERNAL_H_INCLUDED_
+
+/*
+ Here are the values we're masking off to decide what operations to do.
+ Note that since EV_READ|EV_WRITE.
+
+ Note also that this table is a little sparse, since ADD+DEL is
+ nonsensical ("xxx" in the list below.)
+
+ Note also also that we are shifting old_events by only 5 bits, since
+ EV_READ is 2 and EV_WRITE is 4.
+
+ The table was auto-generated with a python script, according to this
+ pseudocode:[*0]
+
+ If either the read or the write change is add+del:
+ This is impossible; Set op==-1, events=0.
+ Else, if either the read or the write change is add:
+ Set events to 0.
+ If the read change is add, or
+ (the read change is not del, and ev_read is in old_events):
+ Add EPOLLIN to events.
+ If the write change is add, or
+ (the write change is not del, and ev_write is in old_events):
+ Add EPOLLOUT to events.
+
+ If old_events is set:
+ Set op to EPOLL_CTL_MOD [*1,*2]
+ Else:
+ Set op to EPOLL_CTL_ADD [*3]
+
+ Else, if the read or the write change is del:
+ Set op to EPOLL_CTL_DEL.
+ If the read change is del:
+ If the write change is del:
+ Set events to EPOLLIN|EPOLLOUT
+ Else if ev_write is in old_events:
+ Set events to EPOLLOUT
+ Set op to EPOLL_CTL_MOD
+ Else
+ Set events to EPOLLIN
+ Else:
+ {The write change is del.}
+ If ev_read is in old_events:
+ Set events to EPOLLIN
+ Set op to EPOLL_CTL_MOD
+ Else:
+ Set the events to EPOLLOUT
+
+ Else:
+ There is no read or write change; set op to 0 and events to 0.
+
+ The logic is a little tricky, since we had no events set on the fd before,
+ we need to set op="ADD" and set events=the events we want to add. If we
+ had any events set on the fd before, and we want any events to remain on
+ the fd, we need to say op="MOD" and set events=the events we want to
+ remain. But if we want to delete the last event, we say op="DEL" and
+ set events=(any non-null pointer).
+
+ [*0] Actually, the Python script has gotten a bit more complicated, to
+ support EPOLLRDHUP.
+
+ [*1] This MOD is only a guess. MOD might fail with ENOENT if the file was
+ closed and a new file was opened with the same fd. If so, we'll retry
+ with ADD.
+
+ [*2] We can't replace this with a no-op even if old_events is the same as
+ the new events: if the file was closed and reopened, we need to retry
+ with an ADD. (We do a MOD in this case since "no change" is more
+ common than "close and reopen", so we'll usually wind up doing 1
+ syscalls instead of 2.)
+
+ [*3] This ADD is only a guess. There is a fun Linux kernel issue where if
+ you have two fds for the same file (via dup) and you ADD one to an
+ epfd, then close it, then re-create it with the same fd (via dup2 or an
+ unlucky dup), then try to ADD it again, you'll get an EEXIST, since the
+ struct epitem is not actually removed from the struct eventpoll until
+ the file itself is closed.
+
+ EV_CHANGE_ADD==1
+ EV_CHANGE_DEL==2
+ EV_READ ==2
+ EV_WRITE ==4
+ EV_CLOSED ==0x80
+
+ Bit 0: close change is add
+ Bit 1: close change is del
+ Bit 2: read change is add
+ Bit 3: read change is del
+ Bit 4: write change is add
+ Bit 5: write change is del
+ Bit 6: old events had EV_READ
+ Bit 7: old events had EV_WRITE
+ Bit 8: old events had EV_CLOSED
+*/
+
+#define EPOLL_OP_TABLE_INDEX(c) \
+ ( (((c)->close_change&(EV_CHANGE_ADD|EV_CHANGE_DEL))) | \
+ (((c)->read_change&(EV_CHANGE_ADD|EV_CHANGE_DEL)) << 2) | \
+ (((c)->write_change&(EV_CHANGE_ADD|EV_CHANGE_DEL)) << 4) | \
+ (((c)->old_events&(EV_READ|EV_WRITE)) << 5) | \
+ (((c)->old_events&(EV_CLOSED)) << 1) \
+ )
+
+#if EV_READ != 2 || EV_WRITE != 4 || EV_CLOSED != 0x80 || EV_CHANGE_ADD != 1 || EV_CHANGE_DEL != 2
+#error "Libevent's internals changed! Regenerate the op_table in epolltable-internal.h"
+#endif
+
+static const struct operation {
+ int events;
+ int op;
+} epoll_op_table[] = {
+ /* old= 0, write: 0, read: 0, close: 0 */
+ { 0, 0 },
+ /* old= 0, write: 0, read: 0, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_ADD },
+ /* old= 0, write: 0, read: 0, close:del */
+ { EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= 0, write: 0, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= 0, write: 0, read:add, close: 0 */
+ { EPOLLIN, EPOLL_CTL_ADD },
+ /* old= 0, write: 0, read:add, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_ADD },
+ /* old= 0, write: 0, read:add, close:del */
+ { EPOLLIN, EPOLL_CTL_ADD },
+ /* old= 0, write: 0, read:add, close:xxx */
+ { 0, 255 },
+ /* old= 0, write: 0, read:del, close: 0 */
+ { EPOLLIN, EPOLL_CTL_DEL },
+ /* old= 0, write: 0, read:del, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_ADD },
+ /* old= 0, write: 0, read:del, close:del */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= 0, write: 0, read:del, close:xxx */
+ { 0, 255 },
+ /* old= 0, write: 0, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= 0, write: 0, read:xxx, close:add */
+ { 0, 255 },
+ /* old= 0, write: 0, read:xxx, close:del */
+ { 0, 255 },
+ /* old= 0, write: 0, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= 0, write:add, read: 0, close: 0 */
+ { EPOLLOUT, EPOLL_CTL_ADD },
+ /* old= 0, write:add, read: 0, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_ADD },
+ /* old= 0, write:add, read: 0, close:del */
+ { EPOLLOUT, EPOLL_CTL_ADD },
+ /* old= 0, write:add, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= 0, write:add, read:add, close: 0 */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_ADD },
+ /* old= 0, write:add, read:add, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_ADD },
+ /* old= 0, write:add, read:add, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_ADD },
+ /* old= 0, write:add, read:add, close:xxx */
+ { 0, 255 },
+ /* old= 0, write:add, read:del, close: 0 */
+ { EPOLLOUT, EPOLL_CTL_ADD },
+ /* old= 0, write:add, read:del, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_ADD },
+ /* old= 0, write:add, read:del, close:del */
+ { EPOLLOUT, EPOLL_CTL_ADD },
+ /* old= 0, write:add, read:del, close:xxx */
+ { 0, 255 },
+ /* old= 0, write:add, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= 0, write:add, read:xxx, close:add */
+ { 0, 255 },
+ /* old= 0, write:add, read:xxx, close:del */
+ { 0, 255 },
+ /* old= 0, write:add, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= 0, write:del, read: 0, close: 0 */
+ { EPOLLOUT, EPOLL_CTL_DEL },
+ /* old= 0, write:del, read: 0, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_ADD },
+ /* old= 0, write:del, read: 0, close:del */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= 0, write:del, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= 0, write:del, read:add, close: 0 */
+ { EPOLLIN, EPOLL_CTL_ADD },
+ /* old= 0, write:del, read:add, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_ADD },
+ /* old= 0, write:del, read:add, close:del */
+ { EPOLLIN, EPOLL_CTL_ADD },
+ /* old= 0, write:del, read:add, close:xxx */
+ { 0, 255 },
+ /* old= 0, write:del, read:del, close: 0 */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_DEL },
+ /* old= 0, write:del, read:del, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_ADD },
+ /* old= 0, write:del, read:del, close:del */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= 0, write:del, read:del, close:xxx */
+ { 0, 255 },
+ /* old= 0, write:del, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= 0, write:del, read:xxx, close:add */
+ { 0, 255 },
+ /* old= 0, write:del, read:xxx, close:del */
+ { 0, 255 },
+ /* old= 0, write:del, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= 0, write:xxx, read: 0, close: 0 */
+ { 0, 255 },
+ /* old= 0, write:xxx, read: 0, close:add */
+ { 0, 255 },
+ /* old= 0, write:xxx, read: 0, close:del */
+ { 0, 255 },
+ /* old= 0, write:xxx, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= 0, write:xxx, read:add, close: 0 */
+ { 0, 255 },
+ /* old= 0, write:xxx, read:add, close:add */
+ { 0, 255 },
+ /* old= 0, write:xxx, read:add, close:del */
+ { 0, 255 },
+ /* old= 0, write:xxx, read:add, close:xxx */
+ { 0, 255 },
+ /* old= 0, write:xxx, read:del, close: 0 */
+ { 0, 255 },
+ /* old= 0, write:xxx, read:del, close:add */
+ { 0, 255 },
+ /* old= 0, write:xxx, read:del, close:del */
+ { 0, 255 },
+ /* old= 0, write:xxx, read:del, close:xxx */
+ { 0, 255 },
+ /* old= 0, write:xxx, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= 0, write:xxx, read:xxx, close:add */
+ { 0, 255 },
+ /* old= 0, write:xxx, read:xxx, close:del */
+ { 0, 255 },
+ /* old= 0, write:xxx, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= r, write: 0, read: 0, close: 0 */
+ { 0, 0 },
+ /* old= r, write: 0, read: 0, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= r, write: 0, read: 0, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= r, write: 0, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= r, write: 0, read:add, close: 0 */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= r, write: 0, read:add, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= r, write: 0, read:add, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= r, write: 0, read:add, close:xxx */
+ { 0, 255 },
+ /* old= r, write: 0, read:del, close: 0 */
+ { EPOLLIN, EPOLL_CTL_DEL },
+ /* old= r, write: 0, read:del, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= r, write: 0, read:del, close:del */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= r, write: 0, read:del, close:xxx */
+ { 0, 255 },
+ /* old= r, write: 0, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= r, write: 0, read:xxx, close:add */
+ { 0, 255 },
+ /* old= r, write: 0, read:xxx, close:del */
+ { 0, 255 },
+ /* old= r, write: 0, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= r, write:add, read: 0, close: 0 */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= r, write:add, read: 0, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= r, write:add, read: 0, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= r, write:add, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= r, write:add, read:add, close: 0 */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= r, write:add, read:add, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= r, write:add, read:add, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= r, write:add, read:add, close:xxx */
+ { 0, 255 },
+ /* old= r, write:add, read:del, close: 0 */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= r, write:add, read:del, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= r, write:add, read:del, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= r, write:add, read:del, close:xxx */
+ { 0, 255 },
+ /* old= r, write:add, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= r, write:add, read:xxx, close:add */
+ { 0, 255 },
+ /* old= r, write:add, read:xxx, close:del */
+ { 0, 255 },
+ /* old= r, write:add, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= r, write:del, read: 0, close: 0 */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= r, write:del, read: 0, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= r, write:del, read: 0, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= r, write:del, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= r, write:del, read:add, close: 0 */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= r, write:del, read:add, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= r, write:del, read:add, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= r, write:del, read:add, close:xxx */
+ { 0, 255 },
+ /* old= r, write:del, read:del, close: 0 */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_DEL },
+ /* old= r, write:del, read:del, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= r, write:del, read:del, close:del */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= r, write:del, read:del, close:xxx */
+ { 0, 255 },
+ /* old= r, write:del, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= r, write:del, read:xxx, close:add */
+ { 0, 255 },
+ /* old= r, write:del, read:xxx, close:del */
+ { 0, 255 },
+ /* old= r, write:del, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= r, write:xxx, read: 0, close: 0 */
+ { 0, 255 },
+ /* old= r, write:xxx, read: 0, close:add */
+ { 0, 255 },
+ /* old= r, write:xxx, read: 0, close:del */
+ { 0, 255 },
+ /* old= r, write:xxx, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= r, write:xxx, read:add, close: 0 */
+ { 0, 255 },
+ /* old= r, write:xxx, read:add, close:add */
+ { 0, 255 },
+ /* old= r, write:xxx, read:add, close:del */
+ { 0, 255 },
+ /* old= r, write:xxx, read:add, close:xxx */
+ { 0, 255 },
+ /* old= r, write:xxx, read:del, close: 0 */
+ { 0, 255 },
+ /* old= r, write:xxx, read:del, close:add */
+ { 0, 255 },
+ /* old= r, write:xxx, read:del, close:del */
+ { 0, 255 },
+ /* old= r, write:xxx, read:del, close:xxx */
+ { 0, 255 },
+ /* old= r, write:xxx, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= r, write:xxx, read:xxx, close:add */
+ { 0, 255 },
+ /* old= r, write:xxx, read:xxx, close:del */
+ { 0, 255 },
+ /* old= r, write:xxx, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= w, write: 0, read: 0, close: 0 */
+ { 0, 0 },
+ /* old= w, write: 0, read: 0, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= w, write: 0, read: 0, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= w, write: 0, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= w, write: 0, read:add, close: 0 */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= w, write: 0, read:add, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= w, write: 0, read:add, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= w, write: 0, read:add, close:xxx */
+ { 0, 255 },
+ /* old= w, write: 0, read:del, close: 0 */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= w, write: 0, read:del, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= w, write: 0, read:del, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= w, write: 0, read:del, close:xxx */
+ { 0, 255 },
+ /* old= w, write: 0, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= w, write: 0, read:xxx, close:add */
+ { 0, 255 },
+ /* old= w, write: 0, read:xxx, close:del */
+ { 0, 255 },
+ /* old= w, write: 0, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= w, write:add, read: 0, close: 0 */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= w, write:add, read: 0, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= w, write:add, read: 0, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= w, write:add, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= w, write:add, read:add, close: 0 */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= w, write:add, read:add, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= w, write:add, read:add, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= w, write:add, read:add, close:xxx */
+ { 0, 255 },
+ /* old= w, write:add, read:del, close: 0 */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= w, write:add, read:del, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= w, write:add, read:del, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= w, write:add, read:del, close:xxx */
+ { 0, 255 },
+ /* old= w, write:add, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= w, write:add, read:xxx, close:add */
+ { 0, 255 },
+ /* old= w, write:add, read:xxx, close:del */
+ { 0, 255 },
+ /* old= w, write:add, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= w, write:del, read: 0, close: 0 */
+ { EPOLLOUT, EPOLL_CTL_DEL },
+ /* old= w, write:del, read: 0, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= w, write:del, read: 0, close:del */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= w, write:del, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= w, write:del, read:add, close: 0 */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= w, write:del, read:add, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= w, write:del, read:add, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= w, write:del, read:add, close:xxx */
+ { 0, 255 },
+ /* old= w, write:del, read:del, close: 0 */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_DEL },
+ /* old= w, write:del, read:del, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= w, write:del, read:del, close:del */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= w, write:del, read:del, close:xxx */
+ { 0, 255 },
+ /* old= w, write:del, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= w, write:del, read:xxx, close:add */
+ { 0, 255 },
+ /* old= w, write:del, read:xxx, close:del */
+ { 0, 255 },
+ /* old= w, write:del, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= w, write:xxx, read: 0, close: 0 */
+ { 0, 255 },
+ /* old= w, write:xxx, read: 0, close:add */
+ { 0, 255 },
+ /* old= w, write:xxx, read: 0, close:del */
+ { 0, 255 },
+ /* old= w, write:xxx, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= w, write:xxx, read:add, close: 0 */
+ { 0, 255 },
+ /* old= w, write:xxx, read:add, close:add */
+ { 0, 255 },
+ /* old= w, write:xxx, read:add, close:del */
+ { 0, 255 },
+ /* old= w, write:xxx, read:add, close:xxx */
+ { 0, 255 },
+ /* old= w, write:xxx, read:del, close: 0 */
+ { 0, 255 },
+ /* old= w, write:xxx, read:del, close:add */
+ { 0, 255 },
+ /* old= w, write:xxx, read:del, close:del */
+ { 0, 255 },
+ /* old= w, write:xxx, read:del, close:xxx */
+ { 0, 255 },
+ /* old= w, write:xxx, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= w, write:xxx, read:xxx, close:add */
+ { 0, 255 },
+ /* old= w, write:xxx, read:xxx, close:del */
+ { 0, 255 },
+ /* old= w, write:xxx, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= rw, write: 0, read: 0, close: 0 */
+ { 0, 0 },
+ /* old= rw, write: 0, read: 0, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= rw, write: 0, read: 0, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= rw, write: 0, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= rw, write: 0, read:add, close: 0 */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= rw, write: 0, read:add, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= rw, write: 0, read:add, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= rw, write: 0, read:add, close:xxx */
+ { 0, 255 },
+ /* old= rw, write: 0, read:del, close: 0 */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= rw, write: 0, read:del, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= rw, write: 0, read:del, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= rw, write: 0, read:del, close:xxx */
+ { 0, 255 },
+ /* old= rw, write: 0, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= rw, write: 0, read:xxx, close:add */
+ { 0, 255 },
+ /* old= rw, write: 0, read:xxx, close:del */
+ { 0, 255 },
+ /* old= rw, write: 0, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= rw, write:add, read: 0, close: 0 */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= rw, write:add, read: 0, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= rw, write:add, read: 0, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= rw, write:add, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= rw, write:add, read:add, close: 0 */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= rw, write:add, read:add, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= rw, write:add, read:add, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= rw, write:add, read:add, close:xxx */
+ { 0, 255 },
+ /* old= rw, write:add, read:del, close: 0 */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= rw, write:add, read:del, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= rw, write:add, read:del, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= rw, write:add, read:del, close:xxx */
+ { 0, 255 },
+ /* old= rw, write:add, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= rw, write:add, read:xxx, close:add */
+ { 0, 255 },
+ /* old= rw, write:add, read:xxx, close:del */
+ { 0, 255 },
+ /* old= rw, write:add, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= rw, write:del, read: 0, close: 0 */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= rw, write:del, read: 0, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= rw, write:del, read: 0, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= rw, write:del, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= rw, write:del, read:add, close: 0 */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= rw, write:del, read:add, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= rw, write:del, read:add, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= rw, write:del, read:add, close:xxx */
+ { 0, 255 },
+ /* old= rw, write:del, read:del, close: 0 */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_DEL },
+ /* old= rw, write:del, read:del, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= rw, write:del, read:del, close:del */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= rw, write:del, read:del, close:xxx */
+ { 0, 255 },
+ /* old= rw, write:del, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= rw, write:del, read:xxx, close:add */
+ { 0, 255 },
+ /* old= rw, write:del, read:xxx, close:del */
+ { 0, 255 },
+ /* old= rw, write:del, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= rw, write:xxx, read: 0, close: 0 */
+ { 0, 255 },
+ /* old= rw, write:xxx, read: 0, close:add */
+ { 0, 255 },
+ /* old= rw, write:xxx, read: 0, close:del */
+ { 0, 255 },
+ /* old= rw, write:xxx, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= rw, write:xxx, read:add, close: 0 */
+ { 0, 255 },
+ /* old= rw, write:xxx, read:add, close:add */
+ { 0, 255 },
+ /* old= rw, write:xxx, read:add, close:del */
+ { 0, 255 },
+ /* old= rw, write:xxx, read:add, close:xxx */
+ { 0, 255 },
+ /* old= rw, write:xxx, read:del, close: 0 */
+ { 0, 255 },
+ /* old= rw, write:xxx, read:del, close:add */
+ { 0, 255 },
+ /* old= rw, write:xxx, read:del, close:del */
+ { 0, 255 },
+ /* old= rw, write:xxx, read:del, close:xxx */
+ { 0, 255 },
+ /* old= rw, write:xxx, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= rw, write:xxx, read:xxx, close:add */
+ { 0, 255 },
+ /* old= rw, write:xxx, read:xxx, close:del */
+ { 0, 255 },
+ /* old= rw, write:xxx, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= c, write: 0, read: 0, close: 0 */
+ { 0, 0 },
+ /* old= c, write: 0, read: 0, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write: 0, read: 0, close:del */
+ { EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= c, write: 0, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= c, write: 0, read:add, close: 0 */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write: 0, read:add, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write: 0, read:add, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= c, write: 0, read:add, close:xxx */
+ { 0, 255 },
+ /* old= c, write: 0, read:del, close: 0 */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write: 0, read:del, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write: 0, read:del, close:del */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= c, write: 0, read:del, close:xxx */
+ { 0, 255 },
+ /* old= c, write: 0, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= c, write: 0, read:xxx, close:add */
+ { 0, 255 },
+ /* old= c, write: 0, read:xxx, close:del */
+ { 0, 255 },
+ /* old= c, write: 0, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= c, write:add, read: 0, close: 0 */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write:add, read: 0, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write:add, read: 0, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= c, write:add, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= c, write:add, read:add, close: 0 */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write:add, read:add, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write:add, read:add, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= c, write:add, read:add, close:xxx */
+ { 0, 255 },
+ /* old= c, write:add, read:del, close: 0 */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write:add, read:del, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write:add, read:del, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= c, write:add, read:del, close:xxx */
+ { 0, 255 },
+ /* old= c, write:add, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= c, write:add, read:xxx, close:add */
+ { 0, 255 },
+ /* old= c, write:add, read:xxx, close:del */
+ { 0, 255 },
+ /* old= c, write:add, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= c, write:del, read: 0, close: 0 */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write:del, read: 0, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write:del, read: 0, close:del */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= c, write:del, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= c, write:del, read:add, close: 0 */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write:del, read:add, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write:del, read:add, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= c, write:del, read:add, close:xxx */
+ { 0, 255 },
+ /* old= c, write:del, read:del, close: 0 */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write:del, read:del, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= c, write:del, read:del, close:del */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= c, write:del, read:del, close:xxx */
+ { 0, 255 },
+ /* old= c, write:del, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= c, write:del, read:xxx, close:add */
+ { 0, 255 },
+ /* old= c, write:del, read:xxx, close:del */
+ { 0, 255 },
+ /* old= c, write:del, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= c, write:xxx, read: 0, close: 0 */
+ { 0, 255 },
+ /* old= c, write:xxx, read: 0, close:add */
+ { 0, 255 },
+ /* old= c, write:xxx, read: 0, close:del */
+ { 0, 255 },
+ /* old= c, write:xxx, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= c, write:xxx, read:add, close: 0 */
+ { 0, 255 },
+ /* old= c, write:xxx, read:add, close:add */
+ { 0, 255 },
+ /* old= c, write:xxx, read:add, close:del */
+ { 0, 255 },
+ /* old= c, write:xxx, read:add, close:xxx */
+ { 0, 255 },
+ /* old= c, write:xxx, read:del, close: 0 */
+ { 0, 255 },
+ /* old= c, write:xxx, read:del, close:add */
+ { 0, 255 },
+ /* old= c, write:xxx, read:del, close:del */
+ { 0, 255 },
+ /* old= c, write:xxx, read:del, close:xxx */
+ { 0, 255 },
+ /* old= c, write:xxx, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= c, write:xxx, read:xxx, close:add */
+ { 0, 255 },
+ /* old= c, write:xxx, read:xxx, close:del */
+ { 0, 255 },
+ /* old= c, write:xxx, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= cr, write: 0, read: 0, close: 0 */
+ { 0, 0 },
+ /* old= cr, write: 0, read: 0, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write: 0, read: 0, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= cr, write: 0, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= cr, write: 0, read:add, close: 0 */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write: 0, read:add, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write: 0, read:add, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= cr, write: 0, read:add, close:xxx */
+ { 0, 255 },
+ /* old= cr, write: 0, read:del, close: 0 */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write: 0, read:del, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write: 0, read:del, close:del */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= cr, write: 0, read:del, close:xxx */
+ { 0, 255 },
+ /* old= cr, write: 0, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= cr, write: 0, read:xxx, close:add */
+ { 0, 255 },
+ /* old= cr, write: 0, read:xxx, close:del */
+ { 0, 255 },
+ /* old= cr, write: 0, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= cr, write:add, read: 0, close: 0 */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write:add, read: 0, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write:add, read: 0, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= cr, write:add, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= cr, write:add, read:add, close: 0 */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write:add, read:add, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write:add, read:add, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= cr, write:add, read:add, close:xxx */
+ { 0, 255 },
+ /* old= cr, write:add, read:del, close: 0 */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write:add, read:del, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write:add, read:del, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= cr, write:add, read:del, close:xxx */
+ { 0, 255 },
+ /* old= cr, write:add, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= cr, write:add, read:xxx, close:add */
+ { 0, 255 },
+ /* old= cr, write:add, read:xxx, close:del */
+ { 0, 255 },
+ /* old= cr, write:add, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= cr, write:del, read: 0, close: 0 */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write:del, read: 0, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write:del, read: 0, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= cr, write:del, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= cr, write:del, read:add, close: 0 */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write:del, read:add, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write:del, read:add, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= cr, write:del, read:add, close:xxx */
+ { 0, 255 },
+ /* old= cr, write:del, read:del, close: 0 */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write:del, read:del, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cr, write:del, read:del, close:del */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= cr, write:del, read:del, close:xxx */
+ { 0, 255 },
+ /* old= cr, write:del, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= cr, write:del, read:xxx, close:add */
+ { 0, 255 },
+ /* old= cr, write:del, read:xxx, close:del */
+ { 0, 255 },
+ /* old= cr, write:del, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= cr, write:xxx, read: 0, close: 0 */
+ { 0, 255 },
+ /* old= cr, write:xxx, read: 0, close:add */
+ { 0, 255 },
+ /* old= cr, write:xxx, read: 0, close:del */
+ { 0, 255 },
+ /* old= cr, write:xxx, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= cr, write:xxx, read:add, close: 0 */
+ { 0, 255 },
+ /* old= cr, write:xxx, read:add, close:add */
+ { 0, 255 },
+ /* old= cr, write:xxx, read:add, close:del */
+ { 0, 255 },
+ /* old= cr, write:xxx, read:add, close:xxx */
+ { 0, 255 },
+ /* old= cr, write:xxx, read:del, close: 0 */
+ { 0, 255 },
+ /* old= cr, write:xxx, read:del, close:add */
+ { 0, 255 },
+ /* old= cr, write:xxx, read:del, close:del */
+ { 0, 255 },
+ /* old= cr, write:xxx, read:del, close:xxx */
+ { 0, 255 },
+ /* old= cr, write:xxx, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= cr, write:xxx, read:xxx, close:add */
+ { 0, 255 },
+ /* old= cr, write:xxx, read:xxx, close:del */
+ { 0, 255 },
+ /* old= cr, write:xxx, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= cw, write: 0, read: 0, close: 0 */
+ { 0, 0 },
+ /* old= cw, write: 0, read: 0, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write: 0, read: 0, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= cw, write: 0, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= cw, write: 0, read:add, close: 0 */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write: 0, read:add, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write: 0, read:add, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= cw, write: 0, read:add, close:xxx */
+ { 0, 255 },
+ /* old= cw, write: 0, read:del, close: 0 */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write: 0, read:del, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write: 0, read:del, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= cw, write: 0, read:del, close:xxx */
+ { 0, 255 },
+ /* old= cw, write: 0, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= cw, write: 0, read:xxx, close:add */
+ { 0, 255 },
+ /* old= cw, write: 0, read:xxx, close:del */
+ { 0, 255 },
+ /* old= cw, write: 0, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= cw, write:add, read: 0, close: 0 */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write:add, read: 0, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write:add, read: 0, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= cw, write:add, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= cw, write:add, read:add, close: 0 */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write:add, read:add, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write:add, read:add, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= cw, write:add, read:add, close:xxx */
+ { 0, 255 },
+ /* old= cw, write:add, read:del, close: 0 */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write:add, read:del, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write:add, read:del, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old= cw, write:add, read:del, close:xxx */
+ { 0, 255 },
+ /* old= cw, write:add, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= cw, write:add, read:xxx, close:add */
+ { 0, 255 },
+ /* old= cw, write:add, read:xxx, close:del */
+ { 0, 255 },
+ /* old= cw, write:add, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= cw, write:del, read: 0, close: 0 */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write:del, read: 0, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write:del, read: 0, close:del */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= cw, write:del, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= cw, write:del, read:add, close: 0 */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write:del, read:add, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write:del, read:add, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old= cw, write:del, read:add, close:xxx */
+ { 0, 255 },
+ /* old= cw, write:del, read:del, close: 0 */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write:del, read:del, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old= cw, write:del, read:del, close:del */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old= cw, write:del, read:del, close:xxx */
+ { 0, 255 },
+ /* old= cw, write:del, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= cw, write:del, read:xxx, close:add */
+ { 0, 255 },
+ /* old= cw, write:del, read:xxx, close:del */
+ { 0, 255 },
+ /* old= cw, write:del, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old= cw, write:xxx, read: 0, close: 0 */
+ { 0, 255 },
+ /* old= cw, write:xxx, read: 0, close:add */
+ { 0, 255 },
+ /* old= cw, write:xxx, read: 0, close:del */
+ { 0, 255 },
+ /* old= cw, write:xxx, read: 0, close:xxx */
+ { 0, 255 },
+ /* old= cw, write:xxx, read:add, close: 0 */
+ { 0, 255 },
+ /* old= cw, write:xxx, read:add, close:add */
+ { 0, 255 },
+ /* old= cw, write:xxx, read:add, close:del */
+ { 0, 255 },
+ /* old= cw, write:xxx, read:add, close:xxx */
+ { 0, 255 },
+ /* old= cw, write:xxx, read:del, close: 0 */
+ { 0, 255 },
+ /* old= cw, write:xxx, read:del, close:add */
+ { 0, 255 },
+ /* old= cw, write:xxx, read:del, close:del */
+ { 0, 255 },
+ /* old= cw, write:xxx, read:del, close:xxx */
+ { 0, 255 },
+ /* old= cw, write:xxx, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old= cw, write:xxx, read:xxx, close:add */
+ { 0, 255 },
+ /* old= cw, write:xxx, read:xxx, close:del */
+ { 0, 255 },
+ /* old= cw, write:xxx, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old=crw, write: 0, read: 0, close: 0 */
+ { 0, 0 },
+ /* old=crw, write: 0, read: 0, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write: 0, read: 0, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old=crw, write: 0, read: 0, close:xxx */
+ { 0, 255 },
+ /* old=crw, write: 0, read:add, close: 0 */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write: 0, read:add, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write: 0, read:add, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old=crw, write: 0, read:add, close:xxx */
+ { 0, 255 },
+ /* old=crw, write: 0, read:del, close: 0 */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write: 0, read:del, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write: 0, read:del, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old=crw, write: 0, read:del, close:xxx */
+ { 0, 255 },
+ /* old=crw, write: 0, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old=crw, write: 0, read:xxx, close:add */
+ { 0, 255 },
+ /* old=crw, write: 0, read:xxx, close:del */
+ { 0, 255 },
+ /* old=crw, write: 0, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old=crw, write:add, read: 0, close: 0 */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write:add, read: 0, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write:add, read: 0, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old=crw, write:add, read: 0, close:xxx */
+ { 0, 255 },
+ /* old=crw, write:add, read:add, close: 0 */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write:add, read:add, close:add */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write:add, read:add, close:del */
+ { EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },
+ /* old=crw, write:add, read:add, close:xxx */
+ { 0, 255 },
+ /* old=crw, write:add, read:del, close: 0 */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write:add, read:del, close:add */
+ { EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write:add, read:del, close:del */
+ { EPOLLOUT, EPOLL_CTL_MOD },
+ /* old=crw, write:add, read:del, close:xxx */
+ { 0, 255 },
+ /* old=crw, write:add, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old=crw, write:add, read:xxx, close:add */
+ { 0, 255 },
+ /* old=crw, write:add, read:xxx, close:del */
+ { 0, 255 },
+ /* old=crw, write:add, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old=crw, write:del, read: 0, close: 0 */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write:del, read: 0, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write:del, read: 0, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old=crw, write:del, read: 0, close:xxx */
+ { 0, 255 },
+ /* old=crw, write:del, read:add, close: 0 */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write:del, read:add, close:add */
+ { EPOLLIN|EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write:del, read:add, close:del */
+ { EPOLLIN, EPOLL_CTL_MOD },
+ /* old=crw, write:del, read:add, close:xxx */
+ { 0, 255 },
+ /* old=crw, write:del, read:del, close: 0 */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write:del, read:del, close:add */
+ { EPOLLRDHUP, EPOLL_CTL_MOD },
+ /* old=crw, write:del, read:del, close:del */
+ { EPOLLIN|EPOLLOUT|EPOLLRDHUP, EPOLL_CTL_DEL },
+ /* old=crw, write:del, read:del, close:xxx */
+ { 0, 255 },
+ /* old=crw, write:del, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old=crw, write:del, read:xxx, close:add */
+ { 0, 255 },
+ /* old=crw, write:del, read:xxx, close:del */
+ { 0, 255 },
+ /* old=crw, write:del, read:xxx, close:xxx */
+ { 0, 255 },
+ /* old=crw, write:xxx, read: 0, close: 0 */
+ { 0, 255 },
+ /* old=crw, write:xxx, read: 0, close:add */
+ { 0, 255 },
+ /* old=crw, write:xxx, read: 0, close:del */
+ { 0, 255 },
+ /* old=crw, write:xxx, read: 0, close:xxx */
+ { 0, 255 },
+ /* old=crw, write:xxx, read:add, close: 0 */
+ { 0, 255 },
+ /* old=crw, write:xxx, read:add, close:add */
+ { 0, 255 },
+ /* old=crw, write:xxx, read:add, close:del */
+ { 0, 255 },
+ /* old=crw, write:xxx, read:add, close:xxx */
+ { 0, 255 },
+ /* old=crw, write:xxx, read:del, close: 0 */
+ { 0, 255 },
+ /* old=crw, write:xxx, read:del, close:add */
+ { 0, 255 },
+ /* old=crw, write:xxx, read:del, close:del */
+ { 0, 255 },
+ /* old=crw, write:xxx, read:del, close:xxx */
+ { 0, 255 },
+ /* old=crw, write:xxx, read:xxx, close: 0 */
+ { 0, 255 },
+ /* old=crw, write:xxx, read:xxx, close:add */
+ { 0, 255 },
+ /* old=crw, write:xxx, read:xxx, close:del */
+ { 0, 255 },
+ /* old=crw, write:xxx, read:xxx, close:xxx */
+ { 0, 255 },
+};
+
+#endif
diff --git a/evbuffer-internal.h b/evbuffer-internal.h
index e68a59df..fb67ec09 100644
--- a/evbuffer-internal.h
+++ b/evbuffer-internal.h
@@ -24,15 +24,17 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVBUFFER_INTERNAL_H_
-#define _EVBUFFER_INTERNAL_H_
+#ifndef EVBUFFER_INTERNAL_H_INCLUDED_
+#define EVBUFFER_INTERNAL_H_INCLUDED_
#ifdef __cplusplus
extern "C" {
#endif
#include "event2/event-config.h"
+#include "evconfig-private.h"
#include "event2/util.h"
+#include "event2/event_struct.h"
#include "util-internal.h"
#include "defer-internal.h"
@@ -41,7 +43,7 @@ extern "C" {
* arguments. */
#define EVBUFFER_CB_NODEFER 2
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#endif
#include <sys/queue.h>
@@ -49,7 +51,7 @@ extern "C" {
/* Minimum allocation for a chain. We define this so that we're burning no
* more than 5% of each allocation on overhead. It would be nice to lose even
* less space, though. */
-#if _EVENT_SIZEOF_VOID_P < 8
+#if EVENT__SIZEOF_VOID_P < 8
#define MIN_BUFFER_SIZE 512
#else
#define MIN_BUFFER_SIZE 1024
@@ -59,7 +61,7 @@ extern "C" {
* when bytes are added to or removed from the evbuffer. */
struct evbuffer_cb_entry {
/** Structures to implement a doubly-linked queue of callbacks */
- TAILQ_ENTRY(evbuffer_cb_entry) next;
+ LIST_ENTRY(evbuffer_cb_entry) next;
/** The callback function to invoke when this callback is called.
If EVBUFFER_CB_OBSOLETE is set in flags, the cb_obsolete field is
valid; otherwise, cb_func is valid. */
@@ -106,7 +108,7 @@ struct evbuffer {
* tried to invoke callbacks. */
size_t n_del_for_cb;
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
/** A lock used to mediate access to this buffer. */
void *lock;
#endif
@@ -125,7 +127,7 @@ struct evbuffer {
* overflows when we have mutually recursive callbacks, and for
* serializing callbacks in a single thread. */
unsigned deferred_cbs : 1;
-#ifdef WIN32
+#ifdef _WIN32
/** True iff this buffer is set up for overlapped IO. */
unsigned is_overlapped : 1;
#endif
@@ -133,7 +135,7 @@ struct evbuffer {
ev_uint32_t flags;
/** Used to implement deferred callbacks. */
- struct deferred_cb_queue *cb_queue;
+ struct event_base *cb_queue;
/** A reference count on this evbuffer. When the reference count
* reaches 0, the buffer is destroyed. Manipulated with
@@ -141,12 +143,12 @@ struct evbuffer {
* evbuffer_free. */
int refcnt;
- /** A deferred_cb handle to make all of this buffer's callbacks
+ /** A struct event_callback handle to make all of this buffer's callbacks
* invoked from the event loop. */
- struct deferred_cb deferred;
+ struct event_callback deferred;
/** A doubly-linked-list of callback functions */
- TAILQ_HEAD(evbuffer_cb_queue, evbuffer_cb_entry) callbacks;
+ LIST_HEAD(evbuffer_cb_queue, evbuffer_cb_entry) callbacks;
/** The parent bufferevent object this evbuffer belongs to.
* NULL if the evbuffer stands alone. */
@@ -172,8 +174,8 @@ struct evbuffer_chain {
/** Set if special handling is required for this chain */
unsigned flags;
-#define EVBUFFER_MMAP 0x0001 /**< memory in buffer is mmaped */
-#define EVBUFFER_SENDFILE 0x0002 /**< a chain used for sendfile */
+#define EVBUFFER_FILESEGMENT 0x0001 /**< A chain used for a file segment */
+#define EVBUFFER_SENDFILE 0x0002 /**< a chain used with sendfile */
#define EVBUFFER_REFERENCE 0x0004 /**< a chain with a mem reference */
#define EVBUFFER_IMMUTABLE 0x0008 /**< read-only chain */
/** a chain that mustn't be reallocated or freed, or have its contents
@@ -184,6 +186,11 @@ struct evbuffer_chain {
/** a chain that should be freed, but can't be freed until it is
* un-pinned. */
#define EVBUFFER_DANGLING 0x0040
+ /** a chain that is a referenced copy of another chain */
+#define EVBUFFER_MULTICAST 0x0080
+
+ /** number of references to this chain */
+ int refcnt;
/** Usually points to the read-write memory belonging to this
* buffer allocated as part of the evbuffer_chain allocation.
@@ -194,21 +201,67 @@ struct evbuffer_chain {
unsigned char *buffer;
};
-/* this is currently used by both mmap and sendfile */
-/* TODO(niels): something strange needs to happen for Windows here, I am not
- * sure what that is, but it needs to get looked into.
- */
-struct evbuffer_chain_fd {
- int fd; /**< the fd associated with this chain */
-};
-
-/** callback for a reference buffer; lets us know what to do with it when
- * we're done with it. */
+/** callback for a reference chain; lets us know what to do with it when
+ * we're done with it. Lives at the end of an evbuffer_chain with the
+ * EVBUFFER_REFERENCE flag set */
struct evbuffer_chain_reference {
evbuffer_ref_cleanup_cb cleanupfn;
void *extra;
};
+/** File segment for a file-segment chain. Lives at the end of an
+ * evbuffer_chain with the EVBUFFER_FILESEGMENT flag set. */
+struct evbuffer_chain_file_segment {
+ struct evbuffer_file_segment *segment;
+#ifdef _WIN32
+ /** If we're using CreateFileMapping, this is the handle to the view. */
+ HANDLE view_handle;
+#endif
+};
+
+/* Declared in event2/buffer.h; defined here. */
+struct evbuffer_file_segment {
+ void *lock; /**< lock prevent concurrent access to refcnt */
+ int refcnt; /**< Reference count for this file segment */
+ unsigned flags; /**< combination of EVBUF_FS_* flags */
+
+ /** What kind of file segment is this? */
+ unsigned can_sendfile : 1;
+ unsigned is_mapping : 1;
+
+ /** The fd that we read the data from. */
+ int fd;
+ /** If we're using mmap, this is the raw mapped memory. */
+ void *mapping;
+#ifdef _WIN32
+ /** If we're using CreateFileMapping, this is the mapping */
+ HANDLE mapping_handle;
+#endif
+ /** If we're using mmap or IO, this is the content of the file
+ * segment. */
+ char *contents;
+ /** Position of this segment within the file. */
+ ev_off_t file_offset;
+ /** If we're using mmap, this is the offset within 'mapping' where
+ * this data segment begins. */
+ ev_off_t mmap_offset;
+ /** The length of this segment. */
+ ev_off_t length;
+ /** Cleanup callback function */
+ evbuffer_file_segment_cleanup_cb cleanup_cb;
+ /** Argument to be pass to cleanup callback function */
+ void *cleanup_cb_arg;
+};
+
+/** Information about the multicast parent of a chain. Lives at the
+ * end of an evbuffer_chain with the EVBUFFER_MULTICAST flag set. */
+struct evbuffer_multicast_parent {
+ /** source buffer the multicast parent belongs to */
+ struct evbuffer *source;
+ /** multicast parent for this chain */
+ struct evbuffer_chain *parent;
+};
+
#define EVBUFFER_CHAIN_SIZE sizeof(struct evbuffer_chain)
/** Return a pointer to extra data allocated along with an evbuffer. */
#define EVBUFFER_CHAIN_EXTRA(t, c) (t *)((struct evbuffer_chain *)(c) + 1)
@@ -235,21 +288,21 @@ struct evbuffer_chain_reference {
} while (0)
/** Increase the reference count of buf by one. */
-void _evbuffer_incref(struct evbuffer *buf);
+void evbuffer_incref_(struct evbuffer *buf);
/** Increase the reference count of buf by one and acquire the lock. */
-void _evbuffer_incref_and_lock(struct evbuffer *buf);
+void evbuffer_incref_and_lock_(struct evbuffer *buf);
/** Pin a single buffer chain using a given flag. A pinned chunk may not be
* moved or freed until it is unpinned. */
-void _evbuffer_chain_pin(struct evbuffer_chain *chain, unsigned flag);
+void evbuffer_chain_pin_(struct evbuffer_chain *chain, unsigned flag);
/** Unpin a single buffer chain using a given flag. */
-void _evbuffer_chain_unpin(struct evbuffer_chain *chain, unsigned flag);
+void evbuffer_chain_unpin_(struct evbuffer_chain *chain, unsigned flag);
/** As evbuffer_free, but requires that we hold a lock on the buffer, and
* releases the lock before freeing it and the buffer. */
-void _evbuffer_decref_and_unlock(struct evbuffer *buffer);
+void evbuffer_decref_and_unlock_(struct evbuffer *buffer);
/** As evbuffer_expand, but does not guarantee that the newly allocated memory
* is contiguous. Instead, it may be split across two or more chunks. */
-int _evbuffer_expand_fast(struct evbuffer *, size_t, int);
+int evbuffer_expand_fast_(struct evbuffer *, size_t, int);
/** Helper: prepares for a readv/WSARecv call by expanding the buffer to
* hold enough memory to read 'howmuch' bytes in possibly noncontiguous memory.
@@ -257,7 +310,7 @@ int _evbuffer_expand_fast(struct evbuffer *, size_t, int);
* extent, and *chainp to point to the first chain that we'll try to read into.
* Returns the number of vecs used.
*/
-int _evbuffer_read_setup_vecs(struct evbuffer *buf, ev_ssize_t howmuch,
+int evbuffer_read_setup_vecs_(struct evbuffer *buf, ev_ssize_t howmuch,
struct evbuffer_iovec *vecs, int n_vecs, struct evbuffer_chain ***chainp,
int exact);
@@ -270,12 +323,17 @@ int _evbuffer_read_setup_vecs(struct evbuffer *buf, ev_ssize_t howmuch,
* See note in buffer_iocp's launch_write function */
/** Set the parent bufferevent object for buf to bev */
-void evbuffer_set_parent(struct evbuffer *buf, struct bufferevent *bev);
+void evbuffer_set_parent_(struct evbuffer *buf, struct bufferevent *bev);
+
+void evbuffer_invoke_callbacks_(struct evbuffer *buf);
+
-void evbuffer_invoke_callbacks(struct evbuffer *buf);
+int evbuffer_get_callbacks_(struct evbuffer *buffer,
+ struct event_callback **cbs,
+ int max_cbs);
#ifdef __cplusplus
}
#endif
-#endif /* _EVBUFFER_INTERNAL_H_ */
+#endif /* EVBUFFER_INTERNAL_H_INCLUDED_ */
diff --git a/evconfig-private.h.cmake b/evconfig-private.h.cmake
new file mode 100644
index 00000000..32f04794
--- /dev/null
+++ b/evconfig-private.h.cmake
@@ -0,0 +1,35 @@
+
+#ifndef EVCONFIG_PRIVATE_H_INCLUDED_
+#define EVCONFIG_PRIVATE_H_INCLUDED_
+
+/* Enable extensions on AIX 3, Interix. */
+#cmakedefine _ALL_SOURCE
+
+/* Enable GNU extensions on systems that have them. */
+#cmakedefine _GNU_SOURCE 1
+
+/* Enable threading extensions on Solaris. */
+#cmakedefine _POSIX_PTHREAD_SEMANTICS 1
+
+/* Enable extensions on HP NonStop. */
+#cmakedefine _TANDEM_SOURCE 1
+
+/* Enable general extensions on Solaris. */
+#cmakedefine __EXTENSIONS__
+
+/* Number of bits in a file offset, on hosts where this is settable. */
+#cmakedefine _FILE_OFFSET_BITS 1
+/* Define for large files, on AIX-style hosts. */
+#cmakedefine _LARGE_FILES 1
+
+/* Define to 1 if on MINIX. */
+#cmakedefine _MINIX 1
+
+/* Define to 2 if the system does not provide POSIX.1 features except with
+ this defined. */
+#cmakedefine _POSIX_1_SOURCE 1
+
+/* Define to 1 if you need to in order for `stat' and other things to work. */
+#cmakedefine _POSIX_SOURCE 1
+
+#endif
diff --git a/evconfig-private.h.in b/evconfig-private.h.in
new file mode 100644
index 00000000..7b3dfdb1
--- /dev/null
+++ b/evconfig-private.h.in
@@ -0,0 +1,48 @@
+/* evconfig-private.h template - see "Configuration Header Templates" */
+/* in AC manual. Kevin Bowling <kevin.bowling@kev009.com */
+#ifndef EVCONFIG_PRIVATE_H_INCLUDED_
+#define EVCONFIG_PRIVATE_H_INCLUDED_
+
+/* Enable extensions on AIX 3, Interix. */
+#ifndef _ALL_SOURCE
+# undef _ALL_SOURCE
+#endif
+/* Enable GNU extensions on systems that have them. */
+#ifndef _GNU_SOURCE
+# undef _GNU_SOURCE
+#endif
+/* Enable threading extensions on Solaris. */
+#ifndef _POSIX_PTHREAD_SEMANTICS
+# undef _POSIX_PTHREAD_SEMANTICS
+#endif
+/* Enable extensions on HP NonStop. */
+#ifndef _TANDEM_SOURCE
+# undef _TANDEM_SOURCE
+#endif
+/* Enable general extensions on Solaris. */
+#ifndef __EXTENSIONS__
+# undef __EXTENSIONS__
+#endif
+
+/* Number of bits in a file offset, on hosts where this is settable. */
+#undef _FILE_OFFSET_BITS
+/* Define for large files, on AIX-style hosts. */
+#undef _LARGE_FILES
+
+/* Define to 1 if on MINIX. */
+#ifndef _MINIX
+#undef _MINIX
+#endif
+
+/* Define to 2 if the system does not provide POSIX.1 features except with
+ this defined. */
+#ifndef _POSIX_1_SOURCE
+#undef _POSIX_1_SOURCE
+#endif
+
+/* Define to 1 if you need to in order for `stat' and other things to work. */
+#ifndef _POSIX_SOURCE
+#undef _POSIX_SOURCE
+#endif
+
+#endif
diff --git a/evdns.c b/evdns.c
index 60b10485..177d56c1 100644
--- a/evdns.c
+++ b/evdns.c
@@ -48,8 +48,10 @@
* Version: 0.1b
*/
-#include <sys/types.h>
#include "event2/event-config.h"
+#include "evconfig-private.h"
+
+#include <sys/types.h>
#ifndef _FORTIFY_SOURCE
#define _FORTIFY_SOURCE 3
@@ -57,23 +59,23 @@
#include <string.h>
#include <fcntl.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
-#ifdef _EVENT_HAVE_STDINT_H
+#ifdef EVENT__HAVE_STDINT_H
#include <stdint.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <errno.h>
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <limits.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdarg.h>
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#ifndef _WIN32_IE
@@ -90,10 +92,6 @@
#include "event2/event_struct.h"
#include "event2/thread.h"
-#include "event2/bufferevent.h"
-#include "event2/bufferevent_struct.h"
-#include "bufferevent-internal.h"
-
#include "defer-internal.h"
#include "log-internal.h"
#include "mm-internal.h"
@@ -101,7 +99,7 @@
#include "ipv6-internal.h"
#include "util-internal.h"
#include "evthread-internal.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <ctype.h>
#include <winsock2.h>
#include <windows.h>
@@ -113,13 +111,13 @@
#include <arpa/inet.h>
#endif
-#ifdef _EVENT_HAVE_NETINET_IN6_H
+#ifdef EVENT__HAVE_NETINET_IN6_H
#include <netinet/in6.h>
#endif
-#define EVDNS_LOG_DEBUG 0
-#define EVDNS_LOG_WARN 1
-#define EVDNS_LOG_MSG 2
+#define EVDNS_LOG_DEBUG EVENT_LOG_DEBUG
+#define EVDNS_LOG_WARN EVENT_LOG_WARN
+#define EVDNS_LOG_MSG EVENT_LOG_MSG
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 255
@@ -234,6 +232,10 @@ struct nameserver {
char choked; /* true if we have an EAGAIN from this server's socket */
char write_waiting; /* true if we are waiting for EV_WRITE events */
struct evdns_base *base;
+
+ /* Number of currently inflight requests: used
+ * to track when we should add/del the event. */
+ int requests_inflight;
};
@@ -251,7 +253,7 @@ struct evdns_server_port {
struct server_request *pending_replies;
struct event_base *event_base;
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
void *lock;
#endif
};
@@ -353,9 +355,11 @@ struct evdns_base {
TAILQ_HEAD(hosts_list, hosts_entry) hostsdb;
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
void *lock;
#endif
+
+ int disable_when_inactive;
};
struct hosts_entry {
@@ -408,13 +412,14 @@ static int evdns_base_resolv_conf_parse_impl(struct evdns_base *base, int flags,
static int evdns_base_set_option_impl(struct evdns_base *base,
const char *option, const char *val, int flags);
static void evdns_base_free_and_unlock(struct evdns_base *base, int fail_requests);
+static void evdns_request_timeout_callback(evutil_socket_t fd, short events, void *arg);
static int strtoint(const char *const str);
-#ifdef _EVENT_DISABLE_THREAD_SUPPORT
-#define EVDNS_LOCK(base) _EVUTIL_NIL_STMT
-#define EVDNS_UNLOCK(base) _EVUTIL_NIL_STMT
-#define ASSERT_LOCKED(base) _EVUTIL_NIL_STMT
+#ifdef EVENT__DISABLE_THREAD_SUPPORT
+#define EVDNS_LOCK(base) EVUTIL_NIL_STMT_
+#define EVDNS_UNLOCK(base) EVUTIL_NIL_STMT_
+#define ASSERT_LOCKED(base) EVUTIL_NIL_STMT_
#else
#define EVDNS_LOCK(base) \
EVLOCK_LOCK((base)->lock, 0)
@@ -424,17 +429,6 @@ static int strtoint(const char *const str);
EVLOCK_ASSERT_LOCKED((base)->lock)
#endif
-static void
-default_evdns_log_fn(int warning, const char *buf)
-{
- if (warning == EVDNS_LOG_WARN)
- event_warnx("[evdns] %s", buf);
- else if (warning == EVDNS_LOG_MSG)
- event_msgx("[evdns] %s", buf);
- else
- event_debug(("[evdns] %s", buf));
-}
-
static evdns_debug_log_fn_type evdns_log_fn = NULL;
void
@@ -449,28 +443,24 @@ evdns_set_log_fn(evdns_debug_log_fn_type fn)
#define EVDNS_LOG_CHECK
#endif
-static void _evdns_log(int warn, const char *fmt, ...) EVDNS_LOG_CHECK;
+static void evdns_log_(int severity, const char *fmt, ...) EVDNS_LOG_CHECK;
static void
-_evdns_log(int warn, const char *fmt, ...)
+evdns_log_(int severity, const char *fmt, ...)
{
va_list args;
- char buf[512];
- if (!evdns_log_fn)
- return;
va_start(args,fmt);
- evutil_vsnprintf(buf, sizeof(buf), fmt, args);
- va_end(args);
if (evdns_log_fn) {
- if (warn == EVDNS_LOG_MSG)
- warn = EVDNS_LOG_WARN;
- evdns_log_fn(warn, buf);
+ char buf[512];
+ int is_warn = (severity == EVDNS_LOG_WARN);
+ evutil_vsnprintf(buf, sizeof(buf), fmt, args);
+ evdns_log_fn(is_warn, buf);
} else {
- default_evdns_log_fn(warn, buf);
+ event_logv_(severity, NULL, fmt, args);
}
-
+ va_end(args);
}
-#define log _evdns_log
+#define log evdns_log_
/* This walks the list of inflight requests to find the */
/* one with a matching transaction id. Returns NULL on */
@@ -545,12 +535,23 @@ nameserver_probe_failed(struct nameserver *const ns) {
char addrbuf[128];
log(EVDNS_LOG_WARN,
"Error from libevent when adding timer event for %s",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&ns->address,
addrbuf, sizeof(addrbuf)));
}
}
+static void
+request_swap_ns(struct request *req, struct nameserver *ns) {
+ if (ns && req->ns != ns) {
+ EVUTIL_ASSERT(req->ns->requests_inflight > 0);
+ req->ns->requests_inflight--;
+ ns->requests_inflight++;
+
+ req->ns = ns;
+ }
+}
+
/* called when a nameserver has been deemed to have failed. For example, too */
/* many packets have timed out etc */
static void
@@ -566,7 +567,7 @@ nameserver_failed(struct nameserver *const ns, const char *msg) {
if (!ns->state) return;
log(EVDNS_LOG_MSG, "Nameserver %s has failed: %s",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&ns->address,
addrbuf, sizeof(addrbuf)),
msg);
@@ -584,7 +585,7 @@ nameserver_failed(struct nameserver *const ns, const char *msg) {
&base->global_nameserver_probe_initial_timeout) < 0) {
log(EVDNS_LOG_WARN,
"Error from libevent when adding timer event for %s",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&ns->address,
addrbuf, sizeof(addrbuf)));
/* ???? Do more? */
@@ -605,7 +606,7 @@ nameserver_failed(struct nameserver *const ns, const char *msg) {
if (req->tx_count == 0 && req->ns == ns) {
/* still waiting to go out, can be moved */
/* to another server */
- req->ns = nameserver_pick(base);
+ request_swap_ns(req, nameserver_pick(base));
}
req = req->next;
} while (req != started_at);
@@ -620,7 +621,7 @@ nameserver_up(struct nameserver *const ns)
ASSERT_LOCKED(ns->base);
if (ns->state) return;
log(EVDNS_LOG_MSG, "Nameserver %s is back up",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&ns->address,
addrbuf, sizeof(addrbuf)));
evtimer_del(&ns->timeout_event);
@@ -658,12 +659,20 @@ request_finished(struct request *const req, struct request **head, int free_hand
if (was_inflight) {
evtimer_del(&req->timeout_event);
base->global_requests_inflight--;
+ req->ns->requests_inflight--;
} else {
base->global_requests_waiting--;
}
/* it was initialized during request_new / evtimer_assign */
event_debug_unassign(&req->timeout_event);
+ if (req->ns &&
+ req->ns->requests_inflight == 0 &&
+ req->base->disable_when_inactive) {
+ event_del(&req->ns->event);
+ evtimer_del(&req->ns->timeout_event);
+ }
+
if (!req->request_appended) {
/* need to free the request data on it's own */
mm_free(req->request);
@@ -710,7 +719,7 @@ request_reissue(struct request *req) {
/* the last nameserver should have been marked as failing */
/* by the caller of this function, therefore pick will try */
/* not to return it */
- req->ns = nameserver_pick(req->base);
+ request_swap_ns(req, nameserver_pick(req->base));
if (req->ns == last_ns) {
/* ... but pick did return it */
/* not a lot of point in trying again with the */
@@ -745,6 +754,8 @@ evdns_requests_pump_waiting_queue(struct evdns_base *base) {
return;
/* move a request from the waiting queue to the inflight queue */
+ req->ns->requests_inflight++;
+
evdns_request_remove(req, &base->req_waiting_head);
base->global_requests_waiting--;
@@ -760,7 +771,7 @@ evdns_requests_pump_waiting_queue(struct evdns_base *base) {
/* TODO(nickm) document */
struct deferred_reply_callback {
- struct deferred_cb deferred;
+ struct event_callback deferred;
struct evdns_request *handle;
u8 request_type;
u8 have_reply;
@@ -771,7 +782,7 @@ struct deferred_reply_callback {
};
static void
-reply_run_callback(struct deferred_cb *d, void *user_pointer)
+reply_run_callback(struct event_callback *d, void *user_pointer)
{
struct deferred_reply_callback *cb =
EVUTIL_UPCAST(d, struct deferred_reply_callback, deferred);
@@ -842,10 +853,13 @@ reply_schedule_callback(struct request *const req, u32 ttl, u32 err, struct repl
d->handle = req->handle;
}
- event_deferred_cb_init(&d->deferred, reply_run_callback,
+ event_deferred_cb_init_(
+ &d->deferred,
+ event_get_priority(&req->timeout_event),
+ reply_run_callback,
req->user_pointer);
- event_deferred_cb_schedule(
- event_base_get_deferred_cb_queue(req->base->event_base),
+ event_deferred_cb_schedule_(
+ req->base->event_base,
&d->deferred);
}
@@ -899,10 +913,12 @@ reply_handle(struct request *const req, u16 flags, u32 ttl, struct reply *reply)
*/
log(EVDNS_LOG_DEBUG, "Got a SERVERFAILED from nameserver"
"at %s; will allow the request to time out.",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&req->ns->address,
addrbuf, sizeof(addrbuf)));
- break;
+ /* Call the timeout function */
+ evdns_request_timeout_callback(0, 0, req);
+ return;
default:
/* we got a good reply from the nameserver: it is up. */
if (req->handle == req->ns->probe_request) {
@@ -945,8 +961,8 @@ name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len) {
int name_end = -1;
int j = *idx;
int ptr_count = 0;
-#define GET32(x) do { if (j + 4 > length) goto err; memcpy(&_t32, packet + j, 4); j += 4; x = ntohl(_t32); } while (0)
-#define GET16(x) do { if (j + 2 > length) goto err; memcpy(&_t, packet + j, 2); j += 2; x = ntohs(_t); } while (0)
+#define GET32(x) do { if (j + 4 > length) goto err; memcpy(&t32_, packet + j, 4); j += 4; x = ntohl(t32_); } while (0)
+#define GET16(x) do { if (j + 2 > length) goto err; memcpy(&t_, packet + j, 2); j += 2; x = ntohs(t_); } while (0)
#define GET8(x) do { if (j >= length) goto err; x = packet[j++]; } while (0)
char *cp = name_out;
@@ -1000,8 +1016,8 @@ name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len) {
static int
reply_parse(struct evdns_base *base, u8 *packet, int length) {
int j = 0, k = 0; /* index into packet */
- u16 _t; /* used by the macros */
- u32 _t32; /* used by the macros */
+ u16 t_; /* used by the macros */
+ u32 t32_; /* used by the macros */
char tmp_name[256], cmp_name[256]; /* used by the macros */
int name_matches = 0;
@@ -1202,7 +1218,7 @@ static int
request_parse(u8 *packet, int length, struct evdns_server_port *port, struct sockaddr *addr, ev_socklen_t addrlen)
{
int j = 0; /* index into packet */
- u16 _t; /* used by the macros */
+ u16 t_; /* used by the macros */
char tmp_name[256]; /* used by the macros */
int i;
@@ -1377,7 +1393,7 @@ nameserver_read(struct nameserver *ns) {
(struct sockaddr*)&ns->address, 0)) {
log(EVDNS_LOG_WARN, "Address mismatch on received "
"DNS packet. Apparent source was %s",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&ss,
addrbuf, sizeof(addrbuf)));
return;
@@ -1467,7 +1483,7 @@ nameserver_write_waiting(struct nameserver *ns, char waiting) {
if (event_add(&ns->event, NULL) < 0) {
char addrbuf[128];
log(EVDNS_LOG_WARN, "Error from libevent when adding event for %s",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&ns->address,
addrbuf, sizeof(addrbuf)));
/* ???? Do more? */
@@ -1588,20 +1604,20 @@ dnsname_to_labels(u8 *const buf, size_t buf_len, off_t j,
struct dnslabel_table *table) {
const char *end = name + name_len;
int ref = 0;
- u16 _t;
+ u16 t_;
#define APPEND16(x) do { \
if (j + 2 > (off_t)buf_len) \
goto overflow; \
- _t = htons(x); \
- memcpy(buf + j, &_t, 2); \
+ t_ = htons(x); \
+ memcpy(buf + j, &t_, 2); \
j += 2; \
} while (0)
#define APPEND32(x) do { \
if (j + 4 > (off_t)buf_len) \
goto overflow; \
- _t32 = htonl(x); \
- memcpy(buf + j, &_t32, 4); \
+ t32_ = htonl(x); \
+ memcpy(buf + j, &t32_, 4); \
j += 4; \
} while (0)
@@ -1667,7 +1683,7 @@ evdns_request_data_build(const char *const name, const size_t name_len,
const u16 trans_id, const u16 type, const u16 class,
u8 *const buf, size_t buf_len) {
off_t j = 0; /* current offset into buf */
- u16 _t; /* used by the macros */
+ u16 t_; /* used by the macros */
APPEND16(trans_id);
APPEND16(0x0100); /* standard query, recusion needed */
@@ -1742,9 +1758,9 @@ evdns_close_server_port(struct evdns_server_port *port)
/* exported function */
int
-evdns_server_request_add_reply(struct evdns_server_request *_req, int section, const char *name, int type, int class, int ttl, int datalen, int is_name, const char *data)
+evdns_server_request_add_reply(struct evdns_server_request *req_, int section, const char *name, int type, int class, int ttl, int datalen, int is_name, const char *data)
{
- struct server_request *req = TO_SERVER_REQUEST(_req);
+ struct server_request *req = TO_SERVER_REQUEST(req_);
struct server_reply_item **itemp, *item;
int *countp;
int result = -1;
@@ -1879,8 +1895,8 @@ evdns_server_request_format_response(struct server_request *req, int err)
unsigned char buf[1500];
size_t buf_len = sizeof(buf);
off_t j = 0, r;
- u16 _t;
- u32 _t32;
+ u16 t_;
+ u32 t32_;
int i;
u16 flags;
struct dnslabel_table table;
@@ -1938,8 +1954,8 @@ evdns_server_request_format_response(struct server_request *req, int err)
if (r < 0)
goto overflow;
j = r;
- _t = htons( (short) (j-name_start) );
- memcpy(buf+len_idx, &_t, 2);
+ t_ = htons( (short) (j-name_start) );
+ memcpy(buf+len_idx, &t_, 2);
} else {
APPEND16(item->datalen);
if (j+item->datalen > (off_t)buf_len)
@@ -1972,9 +1988,9 @@ overflow:
/* exported function */
int
-evdns_server_request_respond(struct evdns_server_request *_req, int err)
+evdns_server_request_respond(struct evdns_server_request *req_, int err)
{
- struct server_request *req = TO_SERVER_REQUEST(_req);
+ struct server_request *req = TO_SERVER_REQUEST(req_);
struct evdns_server_port *port = req->port;
int r = -1;
@@ -2120,18 +2136,18 @@ server_port_free(struct evdns_server_port *port)
/* exported function */
int
-evdns_server_request_drop(struct evdns_server_request *_req)
+evdns_server_request_drop(struct evdns_server_request *req_)
{
- struct server_request *req = TO_SERVER_REQUEST(_req);
+ struct server_request *req = TO_SERVER_REQUEST(req_);
server_request_free(req);
return 0;
}
/* exported function */
int
-evdns_server_request_get_requesting_addr(struct evdns_server_request *_req, struct sockaddr *sa, int addr_len)
+evdns_server_request_get_requesting_addr(struct evdns_server_request *req_, struct sockaddr *sa, int addr_len)
{
- struct server_request *req = TO_SERVER_REQUEST(_req);
+ struct server_request *req = TO_SERVER_REQUEST(req_);
if (addr_len < (int)req->addrlen)
return -1;
memcpy(sa, &(req->addr), req->addrlen);
@@ -2154,29 +2170,30 @@ evdns_request_timeout_callback(evutil_socket_t fd, short events, void *arg) {
log(EVDNS_LOG_DEBUG, "Request %p timed out", arg);
EVDNS_LOCK(base);
- req->ns->timedout++;
- if (req->ns->timedout > req->base->global_max_nameserver_timeout) {
- req->ns->timedout = 0;
- nameserver_failed(req->ns, "request timed out.");
- }
-
if (req->tx_count >= req->base->global_max_retransmits) {
/* this request has failed */
log(EVDNS_LOG_DEBUG, "Giving up on request %p; tx_count==%d",
arg, req->tx_count);
reply_schedule_callback(req, 0, DNS_ERR_TIMEOUT, NULL);
+
+ struct nameserver *ns = req->ns;
request_finished(req, &REQ_HEAD(req->base, req->trans_id), 1);
+ nameserver_failed(ns, "request timed out.");
} else {
/* retransmit it */
- struct nameserver *new_ns;
log(EVDNS_LOG_DEBUG, "Retransmitting request %p; tx_count==%d",
arg, req->tx_count);
(void) evtimer_del(&req->timeout_event);
- new_ns = nameserver_pick(base);
- if (new_ns)
- req->ns = new_ns;
+ request_swap_ns(req, nameserver_pick(base));
evdns_request_transmit(req);
+
+ req->ns->timedout++;
+ if (req->ns->timedout > req->base->global_max_nameserver_timeout) {
+ req->ns->timedout = 0;
+ nameserver_failed(req->ns, "request timed out.");
+ }
}
+
EVDNS_UNLOCK(base);
}
@@ -2191,6 +2208,13 @@ evdns_request_transmit_to(struct request *req, struct nameserver *server) {
int r;
ASSERT_LOCKED(req->base);
ASSERT_VALID_REQUEST(req);
+
+ if (server->requests_inflight == 1 &&
+ req->base->disable_when_inactive &&
+ event_add(&server->event, NULL) < 0) {
+ return 1;
+ }
+
r = sendto(server->socket, (void*)req->request, req->request_len, 0,
(struct sockaddr *)&server->address, server->addrlen);
if (r < 0) {
@@ -2299,7 +2323,7 @@ nameserver_send_probe(struct nameserver *const ns) {
ASSERT_LOCKED(ns->base);
log(EVDNS_LOG_DEBUG, "Sending probe to %s",
- evutil_format_sockaddr_port(
+ evutil_format_sockaddr_port_(
(struct sockaddr *)&ns->address,
addrbuf, sizeof(addrbuf)));
handle = mm_calloc(1, sizeof(*handle));
@@ -2460,7 +2484,7 @@ evdns_resume(void)
}
static int
-_evdns_nameserver_add_impl(struct evdns_base *base, const struct sockaddr *address, int addrlen) {
+evdns_nameserver_add_impl_(struct evdns_base *base, const struct sockaddr *address, int addrlen) {
/* first check to see if we already have this nameserver */
const struct nameserver *server = base->server_head, *const started_at = base->server_head;
@@ -2488,13 +2512,12 @@ _evdns_nameserver_add_impl(struct evdns_base *base, const struct sockaddr *addre
evtimer_assign(&ns->timeout_event, ns->base->event_base, nameserver_prod_callback, ns);
- ns->socket = socket(address->sa_family, SOCK_DGRAM, 0);
+ ns->socket = evutil_socket_(address->sa_family,
+ SOCK_DGRAM|EVUTIL_SOCK_NONBLOCK|EVUTIL_SOCK_CLOEXEC, 0);
if (ns->socket < 0) { err = 1; goto out1; }
- evutil_make_socket_closeonexec(ns->socket);
- evutil_make_socket_nonblocking(ns->socket);
if (base->global_outgoing_addrlen &&
- !evutil_sockaddr_is_loopback(address)) {
+ !evutil_sockaddr_is_loopback_(address)) {
if (bind(ns->socket,
(struct sockaddr*)&base->global_outgoing_address,
base->global_outgoing_addrlen) < 0) {
@@ -2507,14 +2530,15 @@ _evdns_nameserver_add_impl(struct evdns_base *base, const struct sockaddr *addre
memcpy(&ns->address, address, addrlen);
ns->addrlen = addrlen;
ns->state = 1;
- event_assign(&ns->event, ns->base->event_base, ns->socket, EV_READ | EV_PERSIST, nameserver_ready_callback, ns);
- if (event_add(&ns->event, NULL) < 0) {
+ event_assign(&ns->event, ns->base->event_base, ns->socket,
+ EV_READ | EV_PERSIST, nameserver_ready_callback, ns);
+ if (!base->disable_when_inactive && event_add(&ns->event, NULL) < 0) {
err = 2;
goto out2;
}
log(EVDNS_LOG_DEBUG, "Added nameserver %s as %p",
- evutil_format_sockaddr_port(address, addrbuf, sizeof(addrbuf)), ns);
+ evutil_format_sockaddr_port_(address, addrbuf, sizeof(addrbuf)), ns);
/* insert this nameserver into the list of them */
if (!base->server_head) {
@@ -2537,7 +2561,7 @@ out1:
event_debug_unassign(&ns->event);
mm_free(ns);
log(EVDNS_LOG_WARN, "Unable to add nameserver %s: error %d",
- evutil_format_sockaddr_port(address, addrbuf, sizeof(addrbuf)), err);
+ evutil_format_sockaddr_port_(address, addrbuf, sizeof(addrbuf)), err);
return err;
}
@@ -2552,7 +2576,7 @@ evdns_base_nameserver_add(struct evdns_base *base, unsigned long int address)
sin.sin_port = htons(53);
sin.sin_family = AF_INET;
EVDNS_LOCK(base);
- res = _evdns_nameserver_add_impl(base, (struct sockaddr*)&sin, sizeof(sin));
+ res = evdns_nameserver_add_impl_(base, (struct sockaddr*)&sin, sizeof(sin));
EVDNS_UNLOCK(base);
return res;
}
@@ -2604,7 +2628,7 @@ evdns_base_nameserver_ip_add(struct evdns_base *base, const char *ip_as_string)
sockaddr_setport(sa, 53);
EVDNS_LOCK(base);
- res = _evdns_nameserver_add_impl(base, sa, len);
+ res = evdns_nameserver_add_impl_(base, sa, len);
EVDNS_UNLOCK(base);
return res;
}
@@ -2623,7 +2647,7 @@ evdns_base_nameserver_sockaddr_add(struct evdns_base *base,
int res;
EVUTIL_ASSERT(base);
EVDNS_LOCK(base);
- res = _evdns_nameserver_add_impl(base, sa, len);
+ res = evdns_nameserver_add_impl_(base, sa, len);
EVDNS_UNLOCK(base);
return res;
}
@@ -2731,7 +2755,7 @@ request_new(struct evdns_base *base, struct evdns_request *handle, int type,
strlcpy(namebuf, name, sizeof(namebuf));
evutil_secure_rng_get_bytes(randbits, (name_len+7)/8);
for (i = 0; i < name_len; ++i) {
- if (EVUTIL_ISALPHA(namebuf[i])) {
+ if (EVUTIL_ISALPHA_(namebuf[i])) {
if ((randbits[i >> 3] & (1<<(i & 7))))
namebuf[i] |= 0x20;
else
@@ -2779,7 +2803,10 @@ request_submit(struct request *const req) {
/* if it has a nameserver assigned then this is going */
/* straight into the inflight queue */
evdns_request_insert(req, &REQ_HEAD(base, req->trans_id));
+
base->global_requests_inflight++;
+ req->ns->requests_inflight++;
+
evdns_request_transmit(req);
} else {
evdns_request_insert(req, &base->req_waiting_head);
@@ -3263,7 +3290,7 @@ evdns_resolv_set_defaults(struct evdns_base *base, int flags) {
if (flags & DNS_OPTION_NAMESERVERS) evdns_base_nameserver_ip_add(base,"127.0.0.1");
}
-#ifndef _EVENT_HAVE_STRTOK_R
+#ifndef EVENT__HAVE_STRTOK_R
static char *
strtok_r(char *s, const char *delim, char **state) {
char *cp, *start;
@@ -3528,7 +3555,7 @@ evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *con
static char *
evdns_get_default_hosts_filename(void)
{
-#ifdef WIN32
+#ifdef _WIN32
/* Windows is a little coy about where it puts its configuration
* files. Sure, they're _usually_ in C:\windows\system32, but
* there's no reason in principle they couldn't be in
@@ -3541,8 +3568,8 @@ evdns_get_default_hosts_filename(void)
if (! SHGetSpecialFolderPathA(NULL, path, CSIDL_SYSTEM, 0))
return NULL;
- len_out = strlen(path)+strlen(hostfile);
- path_out = mm_malloc(len_out+1);
+ len_out = strlen(path)+strlen(hostfile)+1;
+ path_out = mm_malloc(len_out);
evutil_snprintf(path_out, len_out, "%s%s", path, hostfile);
return path_out;
#else
@@ -3566,7 +3593,7 @@ evdns_base_resolv_conf_parse_impl(struct evdns_base *base, int flags, const char
mm_free(fname);
}
- if ((err = evutil_read_file(filename, &resolv, &n, 0)) < 0) {
+ if ((err = evutil_read_file_(filename, &resolv, &n, 0)) < 0) {
if (err == -1) {
/* No file. */
evdns_resolv_set_defaults(base, flags);
@@ -3610,7 +3637,7 @@ evdns_resolv_conf_parse(int flags, const char *const filename) {
}
-#ifdef WIN32
+#ifdef _WIN32
/* Add multiple nameservers from a space-or-comma-separated list. */
static int
evdns_nameserver_ip_add_line(struct evdns_base *base, const char *ips) {
@@ -3653,7 +3680,7 @@ load_nameservers_with_getnetworkparams(struct evdns_base *base)
GetNetworkParams_fn_t fn;
ASSERT_LOCKED(base);
- if (!(handle = evutil_load_windows_system_library(
+ if (!(handle = evutil_load_windows_system_library_(
TEXT("iphlpapi.dll")))) {
log(EVDNS_LOG_WARN, "Could not open iphlpapi.dll");
status = -1;
@@ -3812,17 +3839,18 @@ evdns_base_config_windows_nameservers(struct evdns_base *base)
if (base == NULL)
return -1;
EVDNS_LOCK(base);
+ fname = evdns_get_default_hosts_filename();
+ log(EVDNS_LOG_DEBUG, "Loading hosts entries from %s", fname);
+ evdns_base_load_hosts(base, fname);
+ if (fname)
+ mm_free(fname);
+
if (load_nameservers_with_getnetworkparams(base) == 0) {
EVDNS_UNLOCK(base);
return 0;
}
r = load_nameservers_from_registry(base);
- fname = evdns_get_default_hosts_filename();
- evdns_base_load_hosts(base, fname);
- if (fname)
- mm_free(fname);
-
EVDNS_UNLOCK(base);
return r;
}
@@ -3840,7 +3868,7 @@ evdns_config_windows_nameservers(void)
#endif
struct evdns_base *
-evdns_base_new(struct event_base *event_base, int initialize_nameservers)
+evdns_base_new(struct event_base *event_base, int flags)
{
struct evdns_base *base;
@@ -3853,7 +3881,7 @@ evdns_base_new(struct event_base *event_base, int initialize_nameservers)
/* Give the evutil library a hook into its evdns-enabled
* functionality. We can't just call evdns_getaddrinfo directly or
* else libevent-core will depend on libevent-extras. */
- evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo);
+ evutil_set_evdns_getaddrinfo_fn_(evdns_getaddrinfo);
base = mm_malloc(sizeof(struct evdns_base));
if (base == NULL)
@@ -3888,9 +3916,18 @@ evdns_base_new(struct event_base *event_base, int initialize_nameservers)
TAILQ_INIT(&base->hostsdb);
- if (initialize_nameservers) {
+#define EVDNS_BASE_ALL_FLAGS (0x8001)
+ if (flags & ~EVDNS_BASE_ALL_FLAGS) {
+ flags = EVDNS_BASE_INITIALIZE_NAMESERVERS;
+ log(EVDNS_LOG_WARN,
+ "Unrecognized flag passed to evdns_base_new(). Assuming "
+ "you meant EVDNS_BASE_INITIALIZE_NAMESERVERS.");
+ }
+#undef EVDNS_BASE_ALL_FLAGS
+
+ if (flags & EVDNS_BASE_INITIALIZE_NAMESERVERS) {
int r;
-#ifdef WIN32
+#ifdef _WIN32
r = evdns_base_config_windows_nameservers(base);
#else
r = evdns_base_resolv_conf_parse(base, DNS_OPTIONS_ALL, "/etc/resolv.conf");
@@ -3900,6 +3937,10 @@ evdns_base_new(struct event_base *event_base, int initialize_nameservers)
return NULL;
}
}
+ if (flags & EVDNS_BASE_DISABLE_WHEN_INACTIVE) {
+ base->disable_when_inactive = 1;
+ }
+
EVDNS_UNLOCK(base);
return base;
}
@@ -4022,6 +4063,18 @@ evdns_base_free(struct evdns_base *base, int fail_requests)
}
void
+evdns_base_clear_host_addresses(struct evdns_base *base)
+{
+ struct hosts_entry *victim;
+ EVDNS_LOCK(base);
+ while ((victim = TAILQ_FIRST(&base->hostsdb))) {
+ TAILQ_REMOVE(&base->hostsdb, victim, next);
+ mm_free(victim);
+ }
+ EVDNS_UNLOCK(base);
+}
+
+void
evdns_shutdown(int fail_requests)
{
if (current_base) {
@@ -4096,7 +4149,7 @@ evdns_base_load_hosts_impl(struct evdns_base *base, const char *hosts_fname)
ASSERT_LOCKED(base);
if (hosts_fname == NULL ||
- (err = evutil_read_file(hosts_fname, &str, &len, 0)) < 0) {
+ (err = evutil_read_file_(hosts_fname, &str, &len, 0)) < 0) {
char tmp[64];
strlcpy(tmp, "127.0.0.1 localhost", sizeof(tmp));
evdns_base_parse_hosts_line(base, tmp);
@@ -4430,7 +4483,7 @@ evdns_getaddrinfo_gotresolve(int result, char type, int count,
for (i=0; i < count; ++i) {
struct evutil_addrinfo *ai;
memcpy(addrp, ((char*)addresses)+i*addrlen, addrlen);
- ai = evutil_new_addrinfo(sa, socklen, &data->hints);
+ ai = evutil_new_addrinfo_(sa, socklen, &data->hints);
if (!ai) {
if (other_req->r) {
evdns_cancel_request(NULL, other_req->r);
@@ -4443,7 +4496,7 @@ evdns_getaddrinfo_gotresolve(int result, char type, int count,
free_getaddrinfo_request(data);
return;
}
- res = evutil_addrinfo_append(res, ai);
+ res = evutil_addrinfo_append_(res, ai);
}
if (other_req->r) {
@@ -4457,10 +4510,10 @@ evdns_getaddrinfo_gotresolve(int result, char type, int count,
* results (if any) and return them. */
if (data->pending_result) {
if (req->type == DNS_IPv4_A)
- res = evutil_addrinfo_append(res,
+ res = evutil_addrinfo_append_(res,
data->pending_result);
else
- res = evutil_addrinfo_append(
+ res = evutil_addrinfo_append_(
data->pending_result, res);
data->pending_result = NULL;
}
@@ -4510,13 +4563,13 @@ evdns_getaddrinfo_fromhosts(struct evdns_base *base,
if ((e->addr.sa.sa_family == AF_INET && f == PF_INET6) ||
(e->addr.sa.sa_family == AF_INET6 && f == PF_INET))
continue;
- ai_new = evutil_new_addrinfo(&e->addr.sa, e->addrlen, hints);
+ ai_new = evutil_new_addrinfo_(&e->addr.sa, e->addrlen, hints);
if (!ai_new) {
n_found = 0;
goto out;
}
sockaddr_setport(ai_new->ai_addr, port);
- ai = evutil_addrinfo_append(ai, ai_new);
+ ai = evutil_addrinfo_append_(ai, ai_new);
}
EVDNS_UNLOCK(base);
out:
@@ -4571,14 +4624,14 @@ evdns_getaddrinfo(struct evdns_base *dns_base,
hints.ai_family = PF_UNSPEC;
}
- evutil_adjust_hints_for_addrconfig(&hints);
+ evutil_adjust_hints_for_addrconfig_(&hints);
/* Now try to see if we _can_ answer immediately. */
/* (It would be nice to do this by calling getaddrinfo directly, with
* AI_NUMERICHOST, on plaforms that have it, but we can't: there isn't
* a reliable way to distinguish the "that wasn't a numeric host!" case
* from any other EAI_NONAME cases.) */
- err = evutil_getaddrinfo_common(nodename, servname, &hints, &res, &port);
+ err = evutil_getaddrinfo_common_(nodename, servname, &hints, &res, &port);
if (err != EVUTIL_EAI_NEED_RESOLVE) {
cb(err, res, arg);
return NULL;
@@ -4629,7 +4682,7 @@ evdns_getaddrinfo(struct evdns_base *dns_base,
data->ipv4_request.r = evdns_base_resolve_ipv4(dns_base,
nodename, 0, evdns_getaddrinfo_gotresolve,
&data->ipv4_request);
- if (want_cname)
+ if (want_cname && data->ipv4_request.r)
data->ipv4_request.r->current_req->put_cname_in_ptr =
&data->cname_result;
}
@@ -4640,7 +4693,7 @@ evdns_getaddrinfo(struct evdns_base *dns_base,
data->ipv6_request.r = evdns_base_resolve_ipv6(dns_base,
nodename, 0, evdns_getaddrinfo_gotresolve,
&data->ipv6_request);
- if (want_cname)
+ if (want_cname && data->ipv6_request.r)
data->ipv6_request.r->current_req->put_cname_in_ptr =
&data->cname_result;
}
diff --git a/event-config.h.cmake b/event-config.h.cmake
new file mode 100644
index 00000000..1efb07ad
--- /dev/null
+++ b/event-config.h.cmake
@@ -0,0 +1,499 @@
+/* event-config.h
+ *
+ * This file was generated by cmake when the makefiles were generated.
+ *
+ * DO NOT EDIT THIS FILE.
+ *
+ * Do not rely on macros in this file existing in later versions.
+ */
+#ifndef EVENT2_EVENT_CONFIG_H_INCLUDED_
+#define EVENT2_EVENT_CONFIG_H_INCLUDED_
+
+/* Numeric representation of the version */
+#define EVENT__NUMERIC_VERSION @EVENT_NUMERIC_VERSION@
+
+#define EVENT__PACKAGE_VERSION @EVENT_PACKAGE_VERSION@
+
+/* Version number of package */
+#define EVENT__VERSION "@EVENT_VERSION@"
+
+/* Name of package */
+#define EVENT__PACKAGE "libevent"
+
+/* Define to the address where bug reports for this package should be sent. */
+#define EVENT__PACKAGE_BUGREPORT ""
+
+/* Define to the full name of this package. */
+#define EVENT__PACKAGE_NAME ""
+
+/* Define to the full name and version of this package. */
+#define EVENT__PACKAGE_STRING ""
+
+/* Define to the one symbol short name of this package. */
+#define EVENT__PACKAGE_TARNAME ""
+
+/* Define if libevent should build without support for a debug mode */
+#cmakedefine EVENT__DISABLE_DEBUG_MODE 0
+
+/* Define if libevent should not allow replacing the mm functions */
+#cmakedefine EVENT__DISABLE_MM_REPLACEMENT 0
+
+/* Define if libevent should not be compiled with thread support */
+#cmakedefine EVENT__DISABLE_THREAD_SUPPORT 0
+
+/* Define to 1 if you have the `accept4' function. */
+#cmakedefine EVENT__HAVE_ACCEPT4 1
+
+/* Define to 1 if you have the `arc4random' function. */
+#cmakedefine EVENT__HAVE_ARC4RANDOM 1
+
+/* Define to 1 if you have the `arc4random_buf' function. */
+#cmakedefine EVENT__HAVE_ARC4RANDOM_BUF 1
+
+/* Define if clock_gettime is available in libc */
+#cmakedefine EVENT__DNS_USE_CPU_CLOCK_FOR_ID 1
+
+/* Define is no secure id variant is available */
+#cmakedefine EVENT__DNS_USE_GETTIMEOFDAY_FOR_ID 1
+#cmakedefine EVENT__DNS_USE_FTIME_FOR_ID 1
+
+/* Define to 1 if you have the <arpa/inet.h> header file. */
+#cmakedefine EVENT__HAVE_ARPA_INET_H 1
+
+/* Define to 1 if you have the `clock_gettime' function. */
+#cmakedefine EVENT__HAVE_CLOCK_GETTIME 1
+
+/* Define to 1 if you have the declaration of `CTL_KERN'. */
+#cmakedefine EVENT__HAVE_DECL_CTL_KERN 1
+
+/* Define to 1 if you have the declaration of `KERN_ARND'. */
+#cmakedefine EVENT__HAVE_DECL_KERN_ARND 0
+
+/* Define to 1 if you have the declaration of `KERN_RANDOM'. */
+#cmakedefine EVENT__HAVE_DECL_KERN_RANDOM 1
+
+/* Define if /dev/poll is available */
+#cmakedefine EVENT__HAVE_DEVPOLL 1
+
+/* Define to 1 if you have the <netdb.h> header file. */
+#cmakedefine EVENT__HAVE_NETDB_H 1
+
+/* Define to 1 if fd_mask type is defined */
+#cmakedefine EVENT__HAVE_FD_MASK 1
+
+/* Define to 1 if the <sys/queue.h> header file defines TAILQ_FOREACH. */
+#cmakedefine EVENT__HAVE_TAILQFOREACH 1
+
+/* Define to 1 if you have the <dlfcn.h> header file. */
+#cmakedefine EVENT__HAVE_DLFCN_H 1
+
+/* Define if your system supports the epoll system calls */
+#cmakedefine EVENT__HAVE_EPOLL 1
+
+/* Define to 1 if you have the `epoll_create1' function. */
+#cmakedefine EVENT__HAVE_EPOLL_CREATE1 1
+
+/* Define to 1 if you have the `epoll_ctl' function. */
+#cmakedefine EVENT__HAVE_EPOLL_CTL 1
+
+/* Define to 1 if you have the `eventfd' function. */
+#cmakedefine EVENT__HAVE_EVENTFD 1
+
+/* Define if your system supports event ports */
+#cmakedefine EVENT__HAVE_EVENT_PORTS 1
+
+/* Define to 1 if you have the `fcntl' function. */
+#cmakedefine EVENT__HAVE_FCNTL 1
+
+/* Define to 1 if you have the <fcntl.h> header file. */
+#cmakedefine EVENT__HAVE_FCNTL_H 1
+
+/* Define to 1 if you have the `getaddrinfo' function. */
+#cmakedefine EVENT__HAVE_GETADDRINFO 1
+
+/* Define to 1 if you have the `getegid' function. */
+#cmakedefine EVENT__HAVE_GETEGID 1
+
+/* Define to 1 if you have the `geteuid' function. */
+#cmakedefine EVENT__HAVE_GETEUID 1
+
+/* TODO: Check for different gethostname argument counts. CheckPrototypeDefinition.cmake can be used. */
+/* Define this if you have any gethostbyname_r() */
+#cmakedefine EVENT__HAVE_GETHOSTBYNAME_R 1
+
+/* Define this if gethostbyname_r takes 3 arguments */
+#cmakedefine EVENT__HAVE_GETHOSTBYNAME_R_3_ARG 1
+
+/* Define this if gethostbyname_r takes 5 arguments */
+#cmakedefine EVENT__HAVE_GETHOSTBYNAME_R_5_ARG 1
+
+/* Define this if gethostbyname_r takes 6 arguments */
+#cmakedefine EVENT__HAVE_GETHOSTBYNAME_R_6_ARG 1
+
+/* Define to 1 if you have the `getifaddrs' function. */
+#cmakedefine EVENT__HAVE_GETIFADDRS 1
+
+/* Define to 1 if you have the `getnameinfo' function. */
+#cmakedefine EVENT__HAVE_GETNAMEINFO 1
+
+/* Define to 1 if you have the `getprotobynumber' function. */
+#cmakedefine EVENT__HAVE_GETPROTOBYNUMBER 1
+
+/* Define to 1 if you have the `getservbyname' function. */
+#cmakedefine EVENT__HAVE_GETSERVBYNAME 1
+
+/* Define to 1 if you have the `gettimeofday' function. */
+#cmakedefine EVENT__HAVE_GETTIMEOFDAY 1
+
+/* Define to 1 if you have the <ifaddrs.h> header file. */
+#cmakedefine EVENT__HAVE_IFADDRS_H 1
+
+/* Define to 1 if you have the `inet_ntop' function. */
+#cmakedefine EVENT__HAVE_INET_NTOP 1
+
+/* Define to 1 if you have the `inet_pton' function. */
+#cmakedefine EVENT__HAVE_INET_PTON 1
+
+/* Define to 1 if you have the <inttypes.h> header file. */
+#cmakedefine EVENT__HAVE_INTTYPES_H 1
+
+/* Define to 1 if you have the `issetugid' function. */
+#cmakedefine EVENT__HAVE_ISSETUGID 1
+
+/* Define to 1 if you have the `kqueue' function. */
+#cmakedefine EVENT__HAVE_KQUEUE 1
+
+/* Define if the system has zlib */
+#cmakedefine EVENT__HAVE_LIBZ 1
+
+/* Define to 1 if you have the `mach_absolute_time' function. */
+#cmakedefine EVENT__HAVE_MACH_ABSOLUTE_TIME 1
+
+/* Define to 1 if you have the <mach/mach_time.h> header file. */
+#cmakedefine EVENT__HAVE_MACH_MACH_TIME_H 1
+
+/* Define to 1 if you have the <memory.h> header file. */
+#cmakedefine EVENT__HAVE_MEMORY_H 1
+
+/* Define to 1 if you have the `mmap' function. */
+#cmakedefine EVENT__HAVE_MMAP 1
+
+/* Define to 1 if you have the `nanosleep' function. */
+#cmakedefine EVENT__HAVE_NANOSLEEP 1
+
+/* Define to 1 if you have the `usleep' function. */
+#cmakedefine EVENT__HAVE_USLEEP 1
+
+/* Define to 1 if you have the <netdb.h> header file. */
+#cmakedefine EVENT__HAVE_NETDB_H 1
+
+/* Define to 1 if you have the <netinet/in6.h> header file. */
+#cmakedefine EVENT__HAVE_NETINET_IN6_H 1
+
+/* Define to 1 if you have the <netinet/in.h> header file. */
+#cmakedefine EVENT__HAVE_NETINET_IN_H 1
+
+/* Define to 1 if you have the <netinet/tcp.h> header file. */
+#cmakedefine EVENT__HAVE_NETINET_TCP_H 1
+
+/* Define if the system has openssl */
+#cmakedefine EVENT__HAVE_OPENSSL 1
+
+/* Defines if the system has zlib */
+#cmakedefine EVENT__HAVE_ZLIB 1
+
+/* Define to 1 if you have the `pipe' function. */
+#cmakedefine EVENT__HAVE_PIPE 1
+
+/* Define to 1 if you have the `pipe2' function. */
+#cmakedefine EVENT__HAVE_PIPE2 1
+
+/* Define to 1 if you have the `poll' function. */
+#cmakedefine EVENT__HAVE_POLL 1
+
+/* Define to 1 if you have the <poll.h> header file. */
+#cmakedefine EVENT__HAVE_POLL_H 1
+
+/* Define to 1 if you have the `port_create' function. */
+#cmakedefine EVENT__HAVE_PORT_CREATE 1
+
+/* Define to 1 if you have the <port.h> header file. */
+#cmakedefine EVENT__HAVE_PORT_H 1
+
+/* Define if you have POSIX threads libraries and header files. */
+#cmakedefine EVENT__HAVE_PTHREAD 1
+
+/* Define if we have pthreads on this system */
+#cmakedefine EVENT__HAVE_PTHREADS 1
+
+/* Define to 1 if you have the `putenv' function. */
+#cmakedefine EVENT__HAVE_PUTENV 1
+
+/* Define to 1 if the system has the type `sa_family_t'. */
+#cmakedefine EVENT__HAVE_SA_FAMILY_T 1
+
+/* Define to 1 if you have the `select' function. */
+#cmakedefine EVENT__HAVE_SELECT 1
+
+/* Define to 1 if you have the `setenv' function. */
+#cmakedefine EVENT__HAVE_SETENV 1
+
+/* Define if F_SETFD is defined in <fcntl.h> */
+#cmakedefine EVENT__HAVE_SETFD 1
+
+/* Define to 1 if you have the `setrlimit' function. */
+#cmakedefine EVENT__HAVE_SETRLIMIT 1
+
+/* Define to 1 if you have the `sendfile' function. */
+#cmakedefine EVENT__HAVE_SENDFILE 1
+
+/* Define if F_SETFD is defined in <fcntl.h> */
+#cmakedefine EVENT__HAVE_SETFD 1
+
+/* Define to 1 if you have the `sigaction' function. */
+#cmakedefine EVENT__HAVE_SIGACTION 1
+
+/* Define to 1 if you have the `signal' function. */
+#cmakedefine EVENT__HAVE_SIGNAL 1
+
+/* Define to 1 if you have the `splice' function. */
+#cmakedefine EVENT__HAVE_SPLICE 1
+
+/* Define to 1 if you have the <stdarg.h> header file. */
+#cmakedefine EVENT__HAVE_STDARG_H 1
+
+/* Define to 1 if you have the <stddef.h> header file. */
+#cmakedefine EVENT__HAVE_STDDEF_H 1
+
+/* Define to 1 if you have the <stdint.h> header file. */
+#cmakedefine EVENT__HAVE_STDINT_H 1
+
+/* Define to 1 if you have the <stdlib.h> header file. */
+#cmakedefine EVENT__HAVE_STDLIB_H 1
+
+/* Define to 1 if you have the <strings.h> header file. */
+#cmakedefine EVENT__HAVE_STRINGS_H 1
+
+/* Define to 1 if you have the <string.h> header file. */
+#cmakedefine EVENT__HAVE_STRING_H 1
+
+/* Define to 1 if you have the `strlcpy' function. */
+#cmakedefine EVENT__HAVE_STRLCPY 1
+
+/* Define to 1 if you have the `strsep' function. */
+#cmakedefine EVENT__HAVE_STRSEP 1
+
+/* Define to 1 if you have the `strtok_r' function. */
+#cmakedefine EVENT__HAVE_STRTOK_R 1
+
+/* Define to 1 if you have the `strtoll' function. */
+#cmakedefine EVENT__HAVE_STRTOLL 1
+
+/* Define to 1 if the system has the type `struct addrinfo'. */
+#cmakedefine EVENT__HAVE_STRUCT_ADDRINFO 1
+
+/* Define to 1 if the system has the type `struct in6_addr'. */
+#cmakedefine EVENT__HAVE_STRUCT_IN6_ADDR 1
+
+/* Define to 1 if `s6_addr16' is member of `struct in6_addr'. */
+#cmakedefine EVENT__HAVE_STRUCT_IN6_ADDR_S6_ADDR16 1
+
+/* Define to 1 if `s6_addr32' is member of `struct in6_addr'. */
+#cmakedefine EVENT__HAVE_STRUCT_IN6_ADDR_S6_ADDR32 1
+
+/* Define to 1 if the system has the type `struct sockaddr_in6'. */
+#cmakedefine EVENT__HAVE_STRUCT_SOCKADDR_IN6 1
+
+/* Define to 1 if `sin6_len' is member of `struct sockaddr_in6'. */
+#cmakedefine EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN 1
+
+/* Define to 1 if `sin_len' is member of `struct sockaddr_in'. */
+#cmakedefine EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN 1
+
+/* Define to 1 if the system has the type `struct sockaddr_storage'. */
+#cmakedefine EVENT__HAVE_STRUCT_SOCKADDR_STORAGE 1
+
+/* Define to 1 if `ss_family' is a member of `struct sockaddr_storage'. */
+#cmakedefine EVENT__HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY 1
+
+/* Define to 1 if `__ss_family' is a member of `struct sockaddr_storage'. */
+#cmakedefine EVENT__HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY 1
+
+/* Define to 1 if you have the `sysctl' function. */
+#cmakedefine EVENT__HAVE_SYSCTL 1
+
+/* Define to 1 if you have the <sys/devpoll.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_DEVPOLL_H 1
+
+/* Define to 1 if you have the <sys/epoll.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_EPOLL_H 1
+
+/* Define to 1 if you have the <sys/eventfd.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_EVENTFD_H 1
+
+/* Define to 1 if you have the <sys/event.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_EVENT_H 1
+
+/* Define to 1 if you have the <sys/ioctl.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_IOCTL_H 1
+
+/* Define to 1 if you have the <sys/mman.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_MMAN_H 1
+
+/* Define to 1 if you have the <sys/param.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_PARAM_H 1
+
+/* Define to 1 if you have the <sys/queue.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_QUEUE_H 1
+
+/* Define to 1 if you have the <sys/resource.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_RESOURCE_H 1
+
+/* Define to 1 if you have the <sys/select.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_SELECT_H 1
+
+/* Define to 1 if you have the <sys/sendfile.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_SENDFILE_H 1
+
+/* Define to 1 if you have the <sys/socket.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_SOCKET_H 1
+
+/* Define to 1 if you have the <sys/stat.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_STAT_H 1
+
+/* Define to 1 if you have the <sys/sysctl.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_SYSCTL_H 1
+
+/* Define to 1 if you have the <sys/timerfd.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_TIMERFD_H */
+
+/* Define to 1 if you have the <sys/time.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_TIME_H 1
+
+/* Define to 1 if you have the <sys/types.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_TYPES_H 1
+
+/* Define to 1 if you have the <sys/uio.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_UIO_H 1
+
+/* Define to 1 if you have the <sys/wait.h> header file. */
+#cmakedefine EVENT__HAVE_SYS_WAIT_H 1
+
+/* Define if TAILQ_FOREACH is defined in <sys/queue.h> */
+#cmakedefine EVENT__HAVE_TAILQFOREACH 1
+
+/* Define if timeradd is defined in <sys/time.h> */
+#cmakedefine EVENT__HAVE_TIMERADD 1
+
+/* Define if timerclear is defined in <sys/time.h> */
+#cmakedefine EVENT__HAVE_TIMERCLEAR 1
+
+/* Define if timercmp is defined in <sys/time.h> */
+#cmakedefine EVENT__HAVE_TIMERCMP 1
+
+/* Define to 1 if you have the `timerfd_create' function. */
+#cmakedefine EVENT__HAVE_TIMERFD_CREATE 1
+
+/* Define if timerisset is defined in <sys/time.h> */
+#cmakedefine EVENT__HAVE_TIMERISSET 1
+
+/* Define to 1 if the system has the type `uint8_t'. */
+#cmakedefine EVENT__HAVE_UINT8_T 1
+
+/* Define to 1 if the system has the type `uint16_t'. */
+#cmakedefine EVENT__HAVE_UINT16_T 1
+
+/* Define to 1 if the system has the type `uint32_t'. */
+#cmakedefine EVENT__HAVE_UINT32_T 1
+
+/* Define to 1 if the system has the type `uint64_t'. */
+#cmakedefine EVENT__HAVE_UINT64_T 1
+
+/* Define to 1 if the system has the type `uintptr_t'. */
+#cmakedefine EVENT__HAVE_UINTPTR_T 1
+
+/* Define to 1 if you have the `umask' function. */
+#cmakedefine EVENT__HAVE_UMASK 1
+
+/* Define to 1 if you have the <unistd.h> header file. */
+#cmakedefine EVENT__HAVE_UNISTD_H 1
+
+/* Define to 1 if you have the `unsetenv' function. */
+#cmakedefine EVENT__HAVE_UNSETENV 1
+
+/* Define to 1 if you have the `vasprintf' function. */
+#cmakedefine EVENT__HAVE_VASPRINTF 1
+
+/* Define if kqueue works correctly with pipes */
+#cmakedefine EVENT__HAVE_WORKING_KQUEUE 1
+
+/* Define to necessary symbol if this constant uses a non-standard name on
+ your system. */
+#cmakedefine EVENT__PTHREAD_CREATE_JOINABLE ${EVENT__PTHREAD_CREATE_JOINABLE}
+
+/* The size of `pthread_t', as computed by sizeof. */
+#cmakedefine EVENT__SIZEOF_PTHREAD_T ${EVENT__SIZEOF_PTHREAD_T}
+
+/* The size of a `int', as computed by sizeof. */
+#cmakedefine EVENT__SIZEOF_INT ${EVENT__SIZEOF_INT}
+
+/* The size of a `long', as computed by sizeof. */
+#cmakedefine EVENT__SIZEOF_LONG ${EVENT__SIZEOF_LONG}
+
+/* The size of a `long long', as computed by sizeof. */
+#cmakedefine EVENT__SIZEOF_LONG_LONG ${EVENT__SIZEOF_LONG_LONG}
+
+/* The size of `off_t', as computed by sizeof. */
+#cmakedefine EVENT__SIZEOF_OFF_T ${EVENT__SIZEOF_OFF_T}
+
+/* The size of a `short', as computed by sizeof. */
+#cmakedefine EVENT__SIZEOF_SHORT ${EVENT__SIZEOF_SHORT}
+
+/* The size of `size_t', as computed by sizeof. */
+#cmakedefine EVENT__SIZEOF_SIZE_T ${EVENT__SIZEOF_SIZE_T}
+
+/* Define to 1 if you have the ANSI C header files. */
+#cmakedefine EVENT__STDC_HEADERS ${EVENT__STDC_HEADERS}
+
+/* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
+#cmakedefine EVENT__TIME_WITH_SYS_TIME ${EVENT__TIME_WITH_SYS_TIME}
+
+/* The size of `socklen_t', as computed by sizeof. */
+#cmakedefine EVENT__SIZEOF_SOCKLEN_T ${EVENT__SIZEOF_SOCKLEN_T}
+
+/* The size of 'void *', as computer by sizeof */
+#cmakedefine EVENT__SIZEOF_VOID_P ${EVENT__SIZEOF_VOID_P}
+
+/* Define to appropriate substitute if compiler doesnt have __func__ */
+#cmakedefine EVENT____func__ ${EVENT____func__}
+
+/* Number of bits in a file offset, on hosts where this is settable. */
+#cmakedefine EVENT___FILE_OFFSET_BITS ${EVENT___FILE_OFFSET_BITS}
+
+/* Define for large files, on AIX-style hosts. */
+/* #undef _LARGE_FILES */
+
+/* Define to empty if `const' does not conform to ANSI C. */
+/* #undef EVENT__const */
+
+/* Define to `__inline__' or `__inline' if that's what the C compiler
+ calls it, or to nothing if 'inline' is not supported under any name. */
+#ifndef __cplusplus
+#cmakedefine EVENT__inline ${EVENT__inline}
+#endif
+
+/* Define to `int' if <sys/types.h> does not define. */
+#cmakedefine EVENT__pid_t ${EVENT__pid_t}
+
+/* Define to `unsigned' if <sys/types.h> does not define. */
+#cmakedefine EVENT__size_t ${EVENT__size_t}
+
+/* Define to unsigned int if you dont have it */
+#cmakedefine EVENT__socklen_t ${EVENT__socklen_t}
+
+/* Define to `int' if <sys/types.h> does not define. */
+#cmakedefine EVENT__ssize_t ${EVENT__ssize_t}
+
+#cmakedefine EVENT__NEED_DLLIMPORT ${EVENT__NEED_DLLIMPORT}
+
+#endif
diff --git a/event-internal.h b/event-internal.h
index 4163a7d7..5208fbe0 100644
--- a/event-internal.h
+++ b/event-internal.h
@@ -24,14 +24,16 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT_INTERNAL_H_
-#define _EVENT_INTERNAL_H_
+#ifndef EVENT_INTERNAL_H_INCLUDED_
+#define EVENT_INTERNAL_H_INCLUDED_
#ifdef __cplusplus
extern "C" {
#endif
#include "event2/event-config.h"
+#include "evconfig-private.h"
+
#include <time.h>
#include <sys/queue.h>
#include "event2/event_struct.h"
@@ -43,18 +45,42 @@ extern "C" {
/* map union members back */
/* mutually exclusive */
-#define ev_signal_next _ev.ev_signal.ev_signal_next
-#define ev_io_next _ev.ev_io.ev_io_next
-#define ev_io_timeout _ev.ev_io.ev_timeout
+#define ev_signal_next ev_.ev_signal.ev_signal_next
+#define ev_io_next ev_.ev_io.ev_io_next
+#define ev_io_timeout ev_.ev_io.ev_timeout
/* used only by signals */
-#define ev_ncalls _ev.ev_signal.ev_ncalls
-#define ev_pncalls _ev.ev_signal.ev_pncalls
+#define ev_ncalls ev_.ev_signal.ev_ncalls
+#define ev_pncalls ev_.ev_signal.ev_pncalls
+
+#define ev_pri ev_evcallback.evcb_pri
+#define ev_flags ev_evcallback.evcb_flags
+#define ev_closure ev_evcallback.evcb_closure
+#define ev_callback ev_evcallback.evcb_cb_union.evcb_callback
+#define ev_arg ev_evcallback.evcb_arg
+
+/** @name Event closure codes
+
+ Possible values for evcb_closure in struct event_callback
-/* Possible values for ev_closure in struct event. */
-#define EV_CLOSURE_NONE 0
-#define EV_CLOSURE_SIGNAL 1
-#define EV_CLOSURE_PERSIST 2
+ @{
+ */
+/** A regular event. Uses the evcb_callback callback */
+#define EV_CLOSURE_EVENT 0
+/** A signal event. Uses the evcb_callback callback */
+#define EV_CLOSURE_EVENT_SIGNAL 1
+/** A persistent non-signal event. Uses the evcb_callback callback */
+#define EV_CLOSURE_EVENT_PERSIST 2
+/** A simple callback. Uses the evcb_selfcb callback. */
+#define EV_CLOSURE_CB_SELF 3
+/** A finalizing callback. Uses the evcb_cbfinalize callback. */
+#define EV_CLOSURE_CB_FINALIZE 4
+/** A finalizing event. Uses the evcb_evfinalize callback. */
+#define EV_CLOSURE_EVENT_FINALIZE 5
+/** A finalizing event that should get freed after. Uses the evcb_evfinalize
+ * callback. */
+#define EV_CLOSURE_EVENT_FINALIZE_FREE 6
+/** @} */
/** Structure to define the backend of a given event_base. */
struct eventop {
@@ -99,7 +125,7 @@ struct eventop {
size_t fdinfo_len;
};
-#ifdef WIN32
+#ifdef _WIN32
/* If we're on win32, then file descriptors are not nice low densely packed
integers. Instead, they are pointer-like windows handles, and we want to
use a hashtable instead of an array to map fds to events.
@@ -110,6 +136,7 @@ struct eventop {
/* #define HT_CACHE_HASH_VALS */
#ifdef EVMAP_USE_HT
+#define HT_NO_CACHE_HASH_VALUES
#include "ht-internal.h"
struct event_map_entry;
HT_HEAD(event_io_map, event_map_entry);
@@ -159,14 +186,25 @@ struct event_changelist {
int changes_size;
};
-#ifndef _EVENT_DISABLE_DEBUG_MODE
+#ifndef EVENT__DISABLE_DEBUG_MODE
/* Global internal flag: set to one if debug mode is on. */
-extern int _event_debug_mode_on;
-#define EVENT_DEBUG_MODE_IS_ON() (_event_debug_mode_on)
+extern int event_debug_mode_on_;
+#define EVENT_DEBUG_MODE_IS_ON() (event_debug_mode_on_)
#else
#define EVENT_DEBUG_MODE_IS_ON() (0)
#endif
+TAILQ_HEAD(evcallback_list, event_callback);
+
+/* Sets up an event for processing once */
+struct event_once {
+ LIST_ENTRY(event_once) next_once;
+ struct event ev;
+
+ void (*cb)(evutil_socket_t, short, void *);
+ void *arg;
+};
+
struct event_base {
/** Function pointers and other data to describe this event_base's
* backend. */
@@ -186,10 +224,16 @@ struct event_base {
/** Number of virtual events */
int virtual_event_count;
+ /** Maximum number of virtual events active */
+ int virtual_event_count_max;
/** Number of total events added to this event_base */
int event_count;
+ /** Maximum number of total events added to this event_base */
+ int event_count_max;
/** Number of total events active in this event_base */
int event_count_active;
+ /** Maximum number of total events active in this event_base */
+ int event_count_active_max;
/** Set if we should terminate the loop once we're done processing
* events. */
@@ -206,14 +250,23 @@ struct event_base {
* reentrant invocation. */
int running_loop;
+ /** Set to the number of deferred_cbs we've made 'active' in the
+ * loop. This is a hack to prevent starvation; it would be smarter
+ * to just use event_config_set_max_dispatch_interval's max_callbacks
+ * feature */
+ int n_deferreds_queued;
+
/* Active event management. */
- /** An array of nactivequeues queues for active events (ones that
- * have triggered, and whose callbacks need to be called). Low
+ /** An array of nactivequeues queues for active event_callbacks (ones
+ * that have triggered, and whose callbacks need to be called). Low
* priority numbers are more important, and stall higher ones.
*/
- struct event_list *activequeues;
+ struct evcallback_list *activequeues;
/** The length of the activequeues array */
int nactivequeues;
+ /** A list of event_callbacks that should become active the next time
+ * we process events, but not this time. */
+ struct evcallback_list active_later_queue;
/* common timeout logic */
@@ -225,22 +278,12 @@ struct event_base {
/** The total size of common_timeout_queues. */
int n_common_timeouts_allocated;
- /** List of defered_cb that are active. We run these after the active
- * events. */
- struct deferred_cb_queue defer_queue;
-
/** Mapping from file descriptors to enabled (added) events */
struct event_io_map io;
/** Mapping from signal numbers to enabled (added) events. */
struct event_signal_map sigmap;
- /** All events that have been enabled (added) in this event_base */
- struct event_list eventqueue;
-
- /** Stored timeval; used to detect when time is running backwards. */
- struct timeval event_tv;
-
/** Priority queue of events with timeouts. */
struct min_heap timeheap;
@@ -248,30 +291,30 @@ struct event_base {
* too often. */
struct timeval tv_cache;
-#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
+ struct evutil_monotonic_timer monotonic_timer;
+
/** Difference between internal time (maybe from clock_gettime) and
* gettimeofday. */
struct timeval tv_clock_diff;
/** Second in which we last updated tv_clock_diff, in monotonic time. */
time_t last_updated_clock_diff;
-#endif
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
/* threading support */
/** The thread currently running the event_loop for this base */
unsigned long th_owner_id;
/** A lock to prevent conflicting accesses to this event_base */
void *th_base_lock;
- /** The event whose callback is executing right now */
- struct event *current_event;
/** A condition that gets signalled when we're done processing an
* event with waiters on it. */
void *current_event_cond;
/** Number of threads blocking on current_event_cond. */
int current_event_waiters;
#endif
+ /** The event whose callback is executing right now */
+ struct event_callback *current_event;
-#ifdef WIN32
+#ifdef _WIN32
/** IOCP support structure, if IOCP is enabled. */
struct event_iocp_port *iocp;
#endif
@@ -279,6 +322,10 @@ struct event_base {
/** Flags that this base was configured with */
enum event_base_config_flag flags;
+ struct timeval max_dispatch_time;
+ int max_dispatch_callbacks;
+ int limit_callbacks_after_prio;
+
/* Notify main thread to wake up break, etc. */
/** True if the base already has a pending notify, and we don't need
* to add any more. */
@@ -291,6 +338,14 @@ struct event_base {
struct event th_notify;
/** A function used to wake up the main thread from another thread. */
int (*th_notify_fn)(struct event_base *base);
+
+ /** Saved seed for weak random number generator. Some backends use
+ * this to produce fairness among sockets. Protected by th_base_lock. */
+ struct evutil_weakrand_state weakrand_seed;
+
+ /** List of event_onces that have not yet fired. */
+ LIST_HEAD(once_event_list, event_once) once_events;
+
};
struct event_config_entry {
@@ -305,12 +360,15 @@ struct event_config {
TAILQ_HEAD(event_configq, event_config_entry) entries;
int n_cpus_hint;
+ struct timeval max_dispatch_interval;
+ int max_dispatch_callbacks;
+ int limit_callbacks_after_prio;
enum event_method_feature require_features;
enum event_base_config_flag flags;
};
/* Internal use only: Functions that might be missing from <sys/queue.h> */
-#if defined(_EVENT_HAVE_SYS_QUEUE_H) && !defined(_EVENT_HAVE_TAILQFOREACH)
+#if defined(EVENT__HAVE_SYS_QUEUE_H) && !defined(EVENT__HAVE_TAILQFOREACH)
#ifndef TAILQ_FIRST
#define TAILQ_FIRST(head) ((head)->tqh_first)
#endif
@@ -339,18 +397,54 @@ struct event_config {
#endif /* TAILQ_FOREACH */
#define N_ACTIVE_CALLBACKS(base) \
- ((base)->event_count_active + (base)->defer_queue.active_count)
+ ((base)->event_count_active)
-int _evsig_set_handler(struct event_base *base, int evsignal,
+int evsig_set_handler_(struct event_base *base, int evsignal,
void (*fn)(int));
-int _evsig_restore_handler(struct event_base *base, int evsignal);
-
-
-void event_active_nolock(struct event *ev, int res, short count);
+int evsig_restore_handler_(struct event_base *base, int evsignal);
+
+int event_add_nolock_(struct event *ev,
+ const struct timeval *tv, int tv_is_absolute);
+/** Argument for event_del_nolock_. Tells event_del not to block on the event
+ * if it's running in another thread. */
+#define EVENT_DEL_NOBLOCK 0
+/** Argument for event_del_nolock_. Tells event_del to block on the event
+ * if it's running in another thread, regardless of its value for EV_FINALIZE
+ */
+#define EVENT_DEL_BLOCK 1
+/** Argument for event_del_nolock_. Tells event_del to block on the event
+ * if it is running in another thread and it doesn't have EV_FINALIZE set.
+ */
+#define EVENT_DEL_AUTOBLOCK 2
+/** Argument for event_del_nolock_. Tells event_del to procede even if the
+ * event is set up for finalization rather for regular use.*/
+#define EVENT_DEL_EVEN_IF_FINALIZING 3
+int event_del_nolock_(struct event *ev, int blocking);
+int event_remove_timer_nolock_(struct event *ev);
+
+void event_active_nolock_(struct event *ev, int res, short count);
+int event_callback_activate_(struct event_base *, struct event_callback *);
+int event_callback_activate_nolock_(struct event_base *, struct event_callback *);
+int event_callback_cancel_(struct event_base *base,
+ struct event_callback *evcb);
+
+void event_callback_finalize_nolock_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *));
+void event_callback_finalize_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *));
+int event_callback_finalize_many_(struct event_base *base, int n_cbs, struct event_callback **evcb, void (*cb)(struct event_callback *, void *));
+
+
+void event_active_later_(struct event *ev, int res);
+void event_active_later_nolock_(struct event *ev, int res);
+void event_callback_activate_later_nolock_(struct event_base *base,
+ struct event_callback *evcb);
+int event_callback_cancel_nolock_(struct event_base *base,
+ struct event_callback *evcb, int even_if_finalizing);
+void event_callback_init_(struct event_base *base,
+ struct event_callback *cb);
/* FIXME document. */
-void event_base_add_virtual(struct event_base *base);
-void event_base_del_virtual(struct event_base *base);
+void event_base_add_virtual_(struct event_base *base);
+void event_base_del_virtual_(struct event_base *base);
/** For debugging: unless assertions are disabled, verify the referential
integrity of the internal data structures of 'base'. This operation can
@@ -358,11 +452,23 @@ void event_base_del_virtual(struct event_base *base);
Returns on success; aborts on failure.
*/
-void event_base_assert_ok(struct event_base *base);
+void event_base_assert_ok_(struct event_base *base);
+void event_base_assert_ok_nolock_(struct event_base *base);
+
+
+/* Helper function: Call 'fn' exactly once every inserted or active event in
+ * the event_base 'base'.
+ *
+ * If fn returns 0, continue on to the next event. Otherwise, return the same
+ * value that fn returned.
+ *
+ * Requires that 'base' be locked.
+ */
+int event_base_foreach_event_nolock_(struct event_base *base,
+ event_base_foreach_event_cb cb, void *arg);
#ifdef __cplusplus
}
#endif
-#endif /* _EVENT_INTERNAL_H_ */
-
+#endif /* EVENT_INTERNAL_H_INCLUDED_ */
diff --git a/event.c b/event.c
index fab419af..d3e46011 100644
--- a/event.c
+++ b/event.c
@@ -25,34 +25,33 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
#endif
#include <sys/types.h>
-#if !defined(WIN32) && defined(_EVENT_HAVE_SYS_TIME_H)
+#if !defined(_WIN32) && defined(EVENT__HAVE_SYS_TIME_H)
#include <sys/time.h>
#endif
#include <sys/queue.h>
-#ifdef _EVENT_HAVE_SYS_SOCKET_H
+#ifdef EVENT__HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <stdio.h>
#include <stdlib.h>
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
-#ifdef _EVENT_HAVE_SYS_EVENTFD_H
-#include <sys/eventfd.h>
-#endif
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <time.h>
+#include <limits.h>
#include "event2/event.h"
#include "event2/event_struct.h"
@@ -66,52 +65,58 @@
#include "evmap-internal.h"
#include "iocp-internal.h"
#include "changelist-internal.h"
+#define HT_NO_CACHE_HASH_VALUES
#include "ht-internal.h"
#include "util-internal.h"
-#ifdef _EVENT_HAVE_EVENT_PORTS
+
+#ifdef EVENT__HAVE_WORKING_KQUEUE
+#include "kqueue-internal.h"
+#endif
+
+#ifdef EVENT__HAVE_EVENT_PORTS
extern const struct eventop evportops;
#endif
-#ifdef _EVENT_HAVE_SELECT
+#ifdef EVENT__HAVE_SELECT
extern const struct eventop selectops;
#endif
-#ifdef _EVENT_HAVE_POLL
+#ifdef EVENT__HAVE_POLL
extern const struct eventop pollops;
#endif
-#ifdef _EVENT_HAVE_EPOLL
+#ifdef EVENT__HAVE_EPOLL
extern const struct eventop epollops;
#endif
-#ifdef _EVENT_HAVE_WORKING_KQUEUE
+#ifdef EVENT__HAVE_WORKING_KQUEUE
extern const struct eventop kqops;
#endif
-#ifdef _EVENT_HAVE_DEVPOLL
+#ifdef EVENT__HAVE_DEVPOLL
extern const struct eventop devpollops;
#endif
-#ifdef WIN32
+#ifdef _WIN32
extern const struct eventop win32ops;
#endif
/* Array of backends in order of preference. */
static const struct eventop *eventops[] = {
-#ifdef _EVENT_HAVE_EVENT_PORTS
+#ifdef EVENT__HAVE_EVENT_PORTS
&evportops,
#endif
-#ifdef _EVENT_HAVE_WORKING_KQUEUE
+#ifdef EVENT__HAVE_WORKING_KQUEUE
&kqops,
#endif
-#ifdef _EVENT_HAVE_EPOLL
+#ifdef EVENT__HAVE_EPOLL
&epollops,
#endif
-#ifdef _EVENT_HAVE_DEVPOLL
+#ifdef EVENT__HAVE_DEVPOLL
&devpollops,
#endif
-#ifdef _EVENT_HAVE_POLL
+#ifdef EVENT__HAVE_POLL
&pollops,
#endif
-#ifdef _EVENT_HAVE_SELECT
+#ifdef EVENT__HAVE_SELECT
&selectops,
#endif
-#ifdef WIN32
+#ifdef _WIN32
&win32ops,
#endif
NULL
@@ -123,29 +128,43 @@ struct event_base *event_global_current_base_ = NULL;
/* Global state */
-static int use_monotonic;
+static void *event_self_cbarg_ptr_ = NULL;
/* Prototypes */
-static inline int event_add_internal(struct event *ev,
- const struct timeval *tv, int tv_is_absolute);
-static inline int event_del_internal(struct event *ev);
+static void event_queue_insert_active(struct event_base *, struct event_callback *);
+static void event_queue_insert_active_later(struct event_base *, struct event_callback *);
+static void event_queue_insert_timeout(struct event_base *, struct event *);
+static void event_queue_insert_inserted(struct event_base *, struct event *);
+static void event_queue_remove_active(struct event_base *, struct event_callback *);
+static void event_queue_remove_active_later(struct event_base *, struct event_callback *);
+static void event_queue_remove_timeout(struct event_base *, struct event *);
+static void event_queue_remove_inserted(struct event_base *, struct event *);
+static void event_queue_make_later_events_active(struct event_base *base);
+
+static int evthread_make_base_notifiable_nolock_(struct event_base *base);
+static int event_del_(struct event *ev, int blocking);
+
+#ifdef USE_REINSERT_TIMEOUT
+/* This code seems buggy; only turn it on if we find out what the trouble is. */
+static void event_queue_reinsert_timeout(struct event_base *,struct event *, int was_common, int is_common, int old_timeout_idx);
+#endif
-static void event_queue_insert(struct event_base *, struct event *, int);
-static void event_queue_remove(struct event_base *, struct event *, int);
static int event_haveevents(struct event_base *);
static int event_process_active(struct event_base *);
static int timeout_next(struct event_base *, struct timeval **);
static void timeout_process(struct event_base *);
-static void timeout_correct(struct event_base *, struct timeval *);
static inline void event_signal_closure(struct event_base *, struct event *ev);
static inline void event_persist_closure(struct event_base *, struct event *ev);
static int evthread_notify_base(struct event_base *base);
-#ifndef _EVENT_DISABLE_DEBUG_MODE
+static void insert_common_timeout_inorder(struct common_timeout_list *ctl,
+ struct event *ev);
+
+#ifndef EVENT__DISABLE_DEBUG_MODE
/* These functions implement a hashtable of which 'struct event *' structures
* have been setup or added. We don't want to trust the content of the struct
* event itself, since we're trying to work through cases where an event gets
@@ -179,11 +198,11 @@ eq_debug_entry(const struct event_debug_entry *a,
return a->ptr == b->ptr;
}
-int _event_debug_mode_on = 0;
+int event_debug_mode_on_ = 0;
/* Set if it's too late to enable event_debug_mode. */
static int event_debug_mode_too_late = 0;
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
-static void *_event_debug_map_lock = NULL;
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
+static void *event_debug_map_lock_ = NULL;
#endif
static HT_HEAD(event_debug_map, event_debug_entry) global_debug_map =
HT_INITIALIZER();
@@ -194,11 +213,11 @@ HT_GENERATE(event_debug_map, event_debug_entry, node, hash_debug_entry,
eq_debug_entry, 0.5, mm_malloc, mm_realloc, mm_free)
/* Macro: record that ev is now setup (that is, ready for an add) */
-#define _event_debug_note_setup(ev) do { \
- if (_event_debug_mode_on) { \
+#define event_debug_note_setup_(ev) do { \
+ if (event_debug_mode_on_) { \
struct event_debug_entry *dent,find; \
find.ptr = (ev); \
- EVLOCK_LOCK(_event_debug_map_lock, 0); \
+ EVLOCK_LOCK(event_debug_map_lock_, 0); \
dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
if (dent) { \
dent->added = 0; \
@@ -211,142 +230,123 @@ HT_GENERATE(event_debug_map, event_debug_entry, node, hash_debug_entry,
dent->added = 0; \
HT_INSERT(event_debug_map, &global_debug_map, dent); \
} \
- EVLOCK_UNLOCK(_event_debug_map_lock, 0); \
+ EVLOCK_UNLOCK(event_debug_map_lock_, 0); \
} \
event_debug_mode_too_late = 1; \
} while (0)
/* Macro: record that ev is no longer setup */
-#define _event_debug_note_teardown(ev) do { \
- if (_event_debug_mode_on) { \
+#define event_debug_note_teardown_(ev) do { \
+ if (event_debug_mode_on_) { \
struct event_debug_entry *dent,find; \
find.ptr = (ev); \
- EVLOCK_LOCK(_event_debug_map_lock, 0); \
+ EVLOCK_LOCK(event_debug_map_lock_, 0); \
dent = HT_REMOVE(event_debug_map, &global_debug_map, &find); \
if (dent) \
mm_free(dent); \
- EVLOCK_UNLOCK(_event_debug_map_lock, 0); \
+ EVLOCK_UNLOCK(event_debug_map_lock_, 0); \
} \
event_debug_mode_too_late = 1; \
} while (0)
/* Macro: record that ev is now added */
-#define _event_debug_note_add(ev) do { \
- if (_event_debug_mode_on) { \
+#define event_debug_note_add_(ev) do { \
+ if (event_debug_mode_on_) { \
struct event_debug_entry *dent,find; \
find.ptr = (ev); \
- EVLOCK_LOCK(_event_debug_map_lock, 0); \
+ EVLOCK_LOCK(event_debug_map_lock_, 0); \
dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
if (dent) { \
dent->added = 1; \
} else { \
- event_errx(_EVENT_ERR_ABORT, \
+ event_errx(EVENT_ERR_ABORT_, \
"%s: noting an add on a non-setup event %p" \
" (events: 0x%x, fd: "EV_SOCK_FMT \
", flags: 0x%x)", \
__func__, (ev), (ev)->ev_events, \
EV_SOCK_ARG((ev)->ev_fd), (ev)->ev_flags); \
} \
- EVLOCK_UNLOCK(_event_debug_map_lock, 0); \
+ EVLOCK_UNLOCK(event_debug_map_lock_, 0); \
} \
event_debug_mode_too_late = 1; \
} while (0)
/* Macro: record that ev is no longer added */
-#define _event_debug_note_del(ev) do { \
- if (_event_debug_mode_on) { \
+#define event_debug_note_del_(ev) do { \
+ if (event_debug_mode_on_) { \
struct event_debug_entry *dent,find; \
find.ptr = (ev); \
- EVLOCK_LOCK(_event_debug_map_lock, 0); \
+ EVLOCK_LOCK(event_debug_map_lock_, 0); \
dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
if (dent) { \
dent->added = 0; \
} else { \
- event_errx(_EVENT_ERR_ABORT, \
+ event_errx(EVENT_ERR_ABORT_, \
"%s: noting a del on a non-setup event %p" \
" (events: 0x%x, fd: "EV_SOCK_FMT \
", flags: 0x%x)", \
__func__, (ev), (ev)->ev_events, \
EV_SOCK_ARG((ev)->ev_fd), (ev)->ev_flags); \
} \
- EVLOCK_UNLOCK(_event_debug_map_lock, 0); \
+ EVLOCK_UNLOCK(event_debug_map_lock_, 0); \
} \
event_debug_mode_too_late = 1; \
} while (0)
/* Macro: assert that ev is setup (i.e., okay to add or inspect) */
-#define _event_debug_assert_is_setup(ev) do { \
- if (_event_debug_mode_on) { \
+#define event_debug_assert_is_setup_(ev) do { \
+ if (event_debug_mode_on_) { \
struct event_debug_entry *dent,find; \
find.ptr = (ev); \
- EVLOCK_LOCK(_event_debug_map_lock, 0); \
+ EVLOCK_LOCK(event_debug_map_lock_, 0); \
dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
if (!dent) { \
- event_errx(_EVENT_ERR_ABORT, \
+ event_errx(EVENT_ERR_ABORT_, \
"%s called on a non-initialized event %p" \
" (events: 0x%x, fd: "EV_SOCK_FMT\
", flags: 0x%x)", \
__func__, (ev), (ev)->ev_events, \
EV_SOCK_ARG((ev)->ev_fd), (ev)->ev_flags); \
} \
- EVLOCK_UNLOCK(_event_debug_map_lock, 0); \
+ EVLOCK_UNLOCK(event_debug_map_lock_, 0); \
} \
} while (0)
/* Macro: assert that ev is not added (i.e., okay to tear down or set
* up again) */
-#define _event_debug_assert_not_added(ev) do { \
- if (_event_debug_mode_on) { \
+#define event_debug_assert_not_added_(ev) do { \
+ if (event_debug_mode_on_) { \
struct event_debug_entry *dent,find; \
find.ptr = (ev); \
- EVLOCK_LOCK(_event_debug_map_lock, 0); \
+ EVLOCK_LOCK(event_debug_map_lock_, 0); \
dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
if (dent && dent->added) { \
- event_errx(_EVENT_ERR_ABORT, \
+ event_errx(EVENT_ERR_ABORT_, \
"%s called on an already added event %p" \
" (events: 0x%x, fd: "EV_SOCK_FMT", " \
"flags: 0x%x)", \
__func__, (ev), (ev)->ev_events, \
EV_SOCK_ARG((ev)->ev_fd), (ev)->ev_flags); \
} \
- EVLOCK_UNLOCK(_event_debug_map_lock, 0); \
+ EVLOCK_UNLOCK(event_debug_map_lock_, 0); \
} \
} while (0)
#else
-#define _event_debug_note_setup(ev) \
+#define event_debug_note_setup_(ev) \
((void)0)
-#define _event_debug_note_teardown(ev) \
+#define event_debug_note_teardown_(ev) \
((void)0)
-#define _event_debug_note_add(ev) \
+#define event_debug_note_add_(ev) \
((void)0)
-#define _event_debug_note_del(ev) \
+#define event_debug_note_del_(ev) \
((void)0)
-#define _event_debug_assert_is_setup(ev) \
+#define event_debug_assert_is_setup_(ev) \
((void)0)
-#define _event_debug_assert_not_added(ev) \
+#define event_debug_assert_not_added_(ev) \
((void)0)
#endif
#define EVENT_BASE_ASSERT_LOCKED(base) \
EVLOCK_ASSERT_LOCKED((base)->th_base_lock)
-/* The first time this function is called, it sets use_monotonic to 1
- * if we have a clock function that supports monotonic time */
-static void
-detect_monotonic(void)
-{
-#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
- struct timespec ts;
- static int use_monotonic_initialized = 0;
-
- if (use_monotonic_initialized)
- return;
-
- if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
- use_monotonic = 1;
-
- use_monotonic_initialized = 1;
-#endif
-}
-
/* How often (in seconds) do we check for changes in wall clock time relative
* to monotonic time? Set this to -1 for 'never.' */
-#define CLOCK_SYNC_INTERVAL -1
+#define CLOCK_SYNC_INTERVAL 5
/** Set 'tp' to the current time according to 'base'. We must hold the lock
* on 'base'. If there is a cached time, return it. Otherwise, use
@@ -363,28 +363,19 @@ gettime(struct event_base *base, struct timeval *tp)
return (0);
}
-#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
- if (use_monotonic) {
- struct timespec ts;
-
- if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
- return (-1);
-
- tp->tv_sec = ts.tv_sec;
- tp->tv_usec = ts.tv_nsec / 1000;
- if (base->last_updated_clock_diff + CLOCK_SYNC_INTERVAL
- < ts.tv_sec) {
- struct timeval tv;
- evutil_gettimeofday(&tv,NULL);
- evutil_timersub(&tv, tp, &base->tv_clock_diff);
- base->last_updated_clock_diff = ts.tv_sec;
- }
+ if (evutil_gettime_monotonic_(&base->monotonic_timer, tp) == -1) {
+ return -1;
+ }
- return (0);
+ if (base->last_updated_clock_diff + CLOCK_SYNC_INTERVAL
+ < tp->tv_sec) {
+ struct timeval tv;
+ evutil_gettimeofday(&tv,NULL);
+ evutil_timersub(&tv, tp, &base->tv_clock_diff);
+ base->last_updated_clock_diff = tp->tv_sec;
}
-#endif
- return (evutil_gettimeofday(tp, NULL));
+ return 0;
}
int
@@ -401,11 +392,7 @@ event_base_gettimeofday_cached(struct event_base *base, struct timeval *tv)
if (base->tv_cache.tv_sec == 0) {
r = evutil_gettimeofday(tv, NULL);
} else {
-#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
evutil_timeradd(&base->tv_cache, &base->tv_clock_diff, tv);
-#else
- *tv = base->tv_cache;
-#endif
r = 0;
}
EVBASE_RELEASE_LOCK(base, th_base_lock);
@@ -428,6 +415,36 @@ update_time_cache(struct event_base *base)
gettime(base, &base->tv_cache);
}
+int
+event_base_update_cache_time(struct event_base *base)
+{
+
+ if (!base) {
+ base = current_base;
+ if (!current_base)
+ return -1;
+ }
+
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ if (base->running_loop)
+ update_time_cache(base);
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ return 0;
+}
+
+static inline struct event *
+event_callback_to_event(struct event_callback *evcb)
+{
+ EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_INIT));
+ return EVUTIL_UPCAST(evcb, struct event, ev_evcallback);
+}
+
+static inline struct event_callback *
+event_to_event_callback(struct event *ev)
+{
+ return &ev->ev_evcallback;
+}
+
struct event_base *
event_init(void)
{
@@ -481,10 +498,10 @@ event_is_method_disabled(const char *name)
evutil_snprintf(environment, sizeof(environment), "EVENT_NO%s", name);
for (i = 8; environment[i] != '\0'; ++i)
- environment[i] = EVUTIL_TOUPPER(environment[i]);
- /* Note that evutil_getenv() ignores the environment entirely if
+ environment[i] = EVUTIL_TOUPPER_(environment[i]);
+ /* Note that evutil_getenv_() ignores the environment entirely if
* we're setuid */
- return (evutil_getenv(environment) != NULL);
+ return (evutil_getenv_(environment) != NULL);
}
int
@@ -494,38 +511,16 @@ event_base_get_features(const struct event_base *base)
}
void
-event_deferred_cb_queue_init(struct deferred_cb_queue *cb)
-{
- memset(cb, 0, sizeof(struct deferred_cb_queue));
- TAILQ_INIT(&cb->deferred_cb_list);
-}
-
-/** Helper for the deferred_cb queue: wake up the event base. */
-static void
-notify_base_cbq_callback(struct deferred_cb_queue *cb, void *baseptr)
-{
- struct event_base *base = baseptr;
- if (EVBASE_NEED_NOTIFY(base))
- evthread_notify_base(base);
-}
-
-struct deferred_cb_queue *
-event_base_get_deferred_cb_queue(struct event_base *base)
-{
- return base ? &base->defer_queue : NULL;
-}
-
-void
event_enable_debug_mode(void)
{
-#ifndef _EVENT_DISABLE_DEBUG_MODE
- if (_event_debug_mode_on)
+#ifndef EVENT__DISABLE_DEBUG_MODE
+ if (event_debug_mode_on_)
event_errx(1, "%s was called twice!", __func__);
if (event_debug_mode_too_late)
event_errx(1, "%s must be called *before* creating any events "
"or event_bases",__func__);
- _event_debug_mode_on = 1;
+ event_debug_mode_on_ = 1;
HT_INIT(event_debug_map, &global_debug_map);
#endif
@@ -537,14 +532,14 @@ event_disable_debug_mode(void)
{
struct event_debug_entry **ent, *victim;
- EVLOCK_LOCK(_event_debug_map_lock, 0);
+ EVLOCK_LOCK(event_debug_map_lock_, 0);
for (ent = HT_START(event_debug_map, &global_debug_map); ent; ) {
victim = *ent;
ent = HT_NEXT_RMV(event_debug_map,&global_debug_map, ent);
mm_free(victim);
}
HT_CLEAR(event_debug_map, &global_debug_map);
- EVLOCK_UNLOCK(_event_debug_map_lock , 0);
+ EVLOCK_UNLOCK(event_debug_map_lock_ , 0);
}
#endif
@@ -555,7 +550,7 @@ event_base_new_with_config(const struct event_config *cfg)
struct event_base *base;
int should_check_environment;
-#ifndef _EVENT_DISABLE_DEBUG_MODE
+#ifndef EVENT__DISABLE_DEBUG_MODE
event_debug_mode_too_late = 1;
#endif
@@ -563,30 +558,60 @@ event_base_new_with_config(const struct event_config *cfg)
event_warn("%s: calloc", __func__);
return NULL;
}
- detect_monotonic();
- gettime(base, &base->event_tv);
- min_heap_ctor(&base->timeheap);
- TAILQ_INIT(&base->eventqueue);
+ if (cfg)
+ base->flags = cfg->flags;
+
+ should_check_environment =
+ !(cfg && (cfg->flags & EVENT_BASE_FLAG_IGNORE_ENV));
+
+ {
+ struct timeval tmp;
+ int precise_time =
+ cfg && (cfg->flags & EVENT_BASE_FLAG_PRECISE_TIMER);
+ int flags;
+ if (should_check_environment && !precise_time) {
+ precise_time = evutil_getenv_("EVENT_PRECISE_TIMER") != NULL;
+ base->flags |= EVENT_BASE_FLAG_PRECISE_TIMER;
+ }
+ flags = precise_time ? EV_MONOT_PRECISE : 0;
+ evutil_configure_monotonic_time_(&base->monotonic_timer, flags);
+
+ gettime(base, &tmp);
+ }
+
+ min_heap_ctor_(&base->timeheap);
+
base->sig.ev_signal_pair[0] = -1;
base->sig.ev_signal_pair[1] = -1;
base->th_notify_fd[0] = -1;
base->th_notify_fd[1] = -1;
- event_deferred_cb_queue_init(&base->defer_queue);
- base->defer_queue.notify_fn = notify_base_cbq_callback;
- base->defer_queue.notify_arg = base;
- if (cfg)
- base->flags = cfg->flags;
+ TAILQ_INIT(&base->active_later_queue);
- evmap_io_initmap(&base->io);
- evmap_signal_initmap(&base->sigmap);
- event_changelist_init(&base->changelist);
+ evmap_io_initmap_(&base->io);
+ evmap_signal_initmap_(&base->sigmap);
+ event_changelist_init_(&base->changelist);
base->evbase = NULL;
- should_check_environment =
- !(cfg && (cfg->flags & EVENT_BASE_FLAG_IGNORE_ENV));
+ if (cfg) {
+ memcpy(&base->max_dispatch_time,
+ &cfg->max_dispatch_interval, sizeof(struct timeval));
+ base->limit_callbacks_after_prio =
+ cfg->limit_callbacks_after_prio;
+ } else {
+ base->max_dispatch_time.tv_sec = -1;
+ base->limit_callbacks_after_prio = 1;
+ }
+ if (cfg && cfg->max_dispatch_callbacks >= 0) {
+ base->max_dispatch_callbacks = cfg->max_dispatch_callbacks;
+ } else {
+ base->max_dispatch_callbacks = INT_MAX;
+ }
+ if (base->max_dispatch_callbacks == INT_MAX &&
+ base->max_dispatch_time.tv_sec == -1)
+ base->limit_callbacks_after_prio = INT_MAX;
for (i = 0; eventops[i] && !base->evbase; i++) {
if (cfg != NULL) {
@@ -617,7 +642,7 @@ event_base_new_with_config(const struct event_config *cfg)
return NULL;
}
- if (evutil_getenv("EVENT_SHOW_METHOD"))
+ if (evutil_getenv_("EVENT_SHOW_METHOD"))
event_msgx("libevent using: %s", base->evsel->name);
/* allocate a single active event queue */
@@ -628,13 +653,11 @@ event_base_new_with_config(const struct event_config *cfg)
/* prepare for threading */
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
if (EVTHREAD_LOCKING_ENABLED() &&
(!cfg || !(cfg->flags & EVENT_BASE_FLAG_NOLOCK))) {
int r;
- EVTHREAD_ALLOC_LOCK(base->th_base_lock,
- EVTHREAD_LOCKTYPE_RECURSIVE);
- base->defer_queue.lock = base->th_base_lock;
+ EVTHREAD_ALLOC_LOCK(base->th_base_lock, 0);
EVTHREAD_ALLOC_COND(base->current_event_cond);
r = evthread_make_base_notifiable(base);
if (r<0) {
@@ -645,21 +668,21 @@ event_base_new_with_config(const struct event_config *cfg)
}
#endif
-#ifdef WIN32
+#ifdef _WIN32
if (cfg && (cfg->flags & EVENT_BASE_FLAG_STARTUP_IOCP))
- event_base_start_iocp(base, cfg->n_cpus_hint);
+ event_base_start_iocp_(base, cfg->n_cpus_hint);
#endif
return (base);
}
int
-event_base_start_iocp(struct event_base *base, int n_cpus)
+event_base_start_iocp_(struct event_base *base, int n_cpus)
{
-#ifdef WIN32
+#ifdef _WIN32
if (base->iocp)
return 0;
- base->iocp = event_iocp_port_launch(n_cpus);
+ base->iocp = event_iocp_port_launch_(n_cpus);
if (!base->iocp) {
event_warnx("%s: Couldn't launch IOCP", __func__);
return -1;
@@ -671,21 +694,61 @@ event_base_start_iocp(struct event_base *base, int n_cpus)
}
void
-event_base_stop_iocp(struct event_base *base)
+event_base_stop_iocp_(struct event_base *base)
{
-#ifdef WIN32
+#ifdef _WIN32
int rv;
if (!base->iocp)
return;
- rv = event_iocp_shutdown(base->iocp, -1);
+ rv = event_iocp_shutdown_(base->iocp, -1);
EVUTIL_ASSERT(rv >= 0);
base->iocp = NULL;
#endif
}
-void
-event_base_free(struct event_base *base)
+static int
+event_base_cancel_single_callback_(struct event_base *base,
+ struct event_callback *evcb,
+ int run_finalizers)
+{
+ int result = 0;
+
+ if (evcb->evcb_flags & EVLIST_INIT) {
+ struct event *ev = event_callback_to_event(evcb);
+ if (!(ev->ev_flags & EVLIST_INTERNAL)) {
+ event_del_(ev, EVENT_DEL_EVEN_IF_FINALIZING);
+ result = 1;
+ }
+ } else {
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ event_callback_cancel_nolock_(base, evcb, 1);
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ result = 1;
+ }
+
+ if (run_finalizers && (evcb->evcb_flags & EVLIST_FINALIZING)) {
+ switch (evcb->evcb_closure) {
+ case EV_CLOSURE_EVENT_FINALIZE:
+ case EV_CLOSURE_EVENT_FINALIZE_FREE: {
+ struct event *ev = event_callback_to_event(evcb);
+ ev->ev_evcallback.evcb_cb_union.evcb_evfinalize(ev, ev->ev_arg);
+ if (evcb->evcb_closure == EV_CLOSURE_EVENT_FINALIZE_FREE)
+ mm_free(ev);
+ break;
+ }
+ case EV_CLOSURE_CB_FINALIZE:
+ evcb->evcb_cb_union.evcb_cbfinalize(evcb, evcb->evcb_arg);
+ break;
+ default:
+ break;
+ }
+ }
+ return result;
+}
+
+static void
+event_base_free_(struct event_base *base, int run_finalizers)
{
int i, n_deleted=0;
struct event *ev;
@@ -696,9 +759,6 @@ event_base_free(struct event_base *base)
* made it with event_init and forgot to hold a reference to it. */
if (base == NULL && current_base)
base = current_base;
- /* If we're freeing current_base, there won't be a current_base. */
- if (base == current_base)
- current_base = NULL;
/* Don't actually free NULL. */
if (base == NULL) {
event_warnx("%s: no base to free", __func__);
@@ -706,8 +766,8 @@ event_base_free(struct event_base *base)
}
/* XXX(niels) - check for internal events first */
-#ifdef WIN32
- event_base_stop_iocp(base);
+#ifdef _WIN32
+ event_base_stop_iocp_(base);
#endif
/* threading fds if we have them */
@@ -722,15 +782,9 @@ event_base_free(struct event_base *base)
}
/* Delete all non-internal events. */
- for (ev = TAILQ_FIRST(&base->eventqueue); ev; ) {
- struct event *next = TAILQ_NEXT(ev, ev_next);
- if (!(ev->ev_flags & EVLIST_INTERNAL)) {
- event_del(ev);
- ++n_deleted;
- }
- ev = next;
- }
- while ((ev = min_heap_top(&base->timeheap)) != NULL) {
+ evmap_delete_all_(base);
+
+ while ((ev = min_heap_top_(&base->timeheap)) != NULL) {
event_del(ev);
++n_deleted;
}
@@ -754,92 +808,135 @@ event_base_free(struct event_base *base)
mm_free(base->common_timeout_queues);
for (i = 0; i < base->nactivequeues; ++i) {
- for (ev = TAILQ_FIRST(&base->activequeues[i]); ev; ) {
- struct event *next = TAILQ_NEXT(ev, ev_active_next);
- if (!(ev->ev_flags & EVLIST_INTERNAL)) {
- event_del(ev);
- ++n_deleted;
- }
- ev = next;
+ struct event_callback *evcb, *next;
+ for (evcb = TAILQ_FIRST(&base->activequeues[i]); evcb; ) {
+ next = TAILQ_NEXT(evcb, evcb_active_next);
+ n_deleted += event_base_cancel_single_callback_(base, evcb, run_finalizers);
+ evcb = next;
+ }
+ }
+ {
+ struct event_callback *evcb;
+ while ((evcb = TAILQ_FIRST(&base->active_later_queue))) {
+ n_deleted += event_base_cancel_single_callback_(base, evcb, run_finalizers);
}
}
+
if (n_deleted)
event_debug(("%s: %d events were still set in base",
__func__, n_deleted));
+ while (LIST_FIRST(&base->once_events)) {
+ struct event_once *eonce = LIST_FIRST(&base->once_events);
+ LIST_REMOVE(eonce, next_once);
+ mm_free(eonce);
+ }
+
if (base->evsel != NULL && base->evsel->dealloc != NULL)
base->evsel->dealloc(base);
for (i = 0; i < base->nactivequeues; ++i)
EVUTIL_ASSERT(TAILQ_EMPTY(&base->activequeues[i]));
- EVUTIL_ASSERT(min_heap_empty(&base->timeheap));
- min_heap_dtor(&base->timeheap);
+ EVUTIL_ASSERT(min_heap_empty_(&base->timeheap));
+ min_heap_dtor_(&base->timeheap);
mm_free(base->activequeues);
- EVUTIL_ASSERT(TAILQ_EMPTY(&base->eventqueue));
+ evmap_io_clear_(&base->io);
+ evmap_signal_clear_(&base->sigmap);
+ event_changelist_freemem_(&base->changelist);
- evmap_io_clear(&base->io);
- evmap_signal_clear(&base->sigmap);
- event_changelist_freemem(&base->changelist);
-
- EVTHREAD_FREE_LOCK(base->th_base_lock, EVTHREAD_LOCKTYPE_RECURSIVE);
+ EVTHREAD_FREE_LOCK(base->th_base_lock, 0);
EVTHREAD_FREE_COND(base->current_event_cond);
+ /* If we're freeing current_base, there won't be a current_base. */
+ if (base == current_base)
+ current_base = NULL;
mm_free(base);
}
+void
+event_base_free_nofinalize(struct event_base *base)
+{
+ event_base_free_(base, 0);
+}
+
+void
+event_base_free(struct event_base *base)
+{
+ event_base_free_(base, 1);
+}
+
+/* Fake eventop; used to disable the backend temporarily inside event_reinit
+ * so that we can call event_del() on an event without telling the backend.
+ */
+static int
+nil_backend_del(struct event_base *b, evutil_socket_t fd, short old,
+ short events, void *fdinfo)
+{
+ return 0;
+}
+const struct eventop nil_eventop = {
+ "nil",
+ NULL, /* init: unused. */
+ NULL, /* add: unused. */
+ nil_backend_del, /* del: used, so needs to be killed. */
+ NULL, /* dispatch: unused. */
+ NULL, /* dealloc: unused. */
+ 0, 0, 0
+};
+
/* reinitialize the event base after a fork */
int
event_reinit(struct event_base *base)
{
const struct eventop *evsel;
int res = 0;
- struct event *ev;
int was_notifiable = 0;
+ int had_signal_added = 0;
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
evsel = base->evsel;
-#if 0
- /* Right now, reinit always takes effect, since even if the
- backend doesn't require it, the signal socketpair code does.
+ /* check if this event mechanism requires reinit on the backend */
+ if (evsel->need_reinit) {
+ /* We're going to call event_del() on our notify events (the
+ * ones that tell about signals and wakeup events). But we
+ * don't actually want to tell the backend to change its
+ * state, since it might still share some resource (a kqueue,
+ * an epoll fd) with the parent process, and we don't want to
+ * delete the fds from _that_ backend, we temporarily stub out
+ * the evsel with a replacement.
+ */
+ base->evsel = &nil_eventop;
+ }
- XXX
+ /* We need to re-create a new signal-notification fd and a new
+ * thread-notification fd. Otherwise, we'll still share those with
+ * the parent process, which would make any notification sent to them
+ * get received by one or both of the event loops, more or less at
+ * random.
*/
- /* check if this event mechanism requires reinit */
- if (!evsel->need_reinit)
- goto done;
-#endif
-
- /* prevent internal delete */
if (base->sig.ev_signal_added) {
- /* we cannot call event_del here because the base has
- * not been reinitialized yet. */
- event_queue_remove(base, &base->sig.ev_signal,
- EVLIST_INSERTED);
- if (base->sig.ev_signal.ev_flags & EVLIST_ACTIVE)
- event_queue_remove(base, &base->sig.ev_signal,
- EVLIST_ACTIVE);
+ event_del_nolock_(&base->sig.ev_signal, EVENT_DEL_AUTOBLOCK);
+ event_debug_unassign(&base->sig.ev_signal);
+ memset(&base->sig.ev_signal, 0, sizeof(base->sig.ev_signal));
if (base->sig.ev_signal_pair[0] != -1)
EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[0]);
if (base->sig.ev_signal_pair[1] != -1)
EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[1]);
+ had_signal_added = 1;
base->sig.ev_signal_added = 0;
}
- if (base->th_notify_fd[0] != -1) {
- /* we cannot call event_del here because the base has
- * not been reinitialized yet. */
+ if (base->th_notify_fn != NULL) {
was_notifiable = 1;
- event_queue_remove(base, &base->th_notify,
- EVLIST_INSERTED);
- if (base->th_notify.ev_flags & EVLIST_ACTIVE)
- event_queue_remove(base, &base->th_notify,
- EVLIST_ACTIVE);
- base->sig.ev_signal_added = 0;
+ base->th_notify_fn = NULL;
+ }
+ if (base->th_notify_fd[0] != -1) {
+ event_del_nolock_(&base->th_notify, EVENT_DEL_AUTOBLOCK);
EVUTIL_CLOSESOCKET(base->th_notify_fd[0]);
if (base->th_notify_fd[1] != -1)
EVUTIL_CLOSESOCKET(base->th_notify_fd[1]);
@@ -848,39 +945,48 @@ event_reinit(struct event_base *base)
event_debug_unassign(&base->th_notify);
}
- if (base->evsel->dealloc != NULL)
- base->evsel->dealloc(base);
- base->evbase = evsel->init(base);
- if (base->evbase == NULL) {
- event_errx(1, "%s: could not reinitialize event mechanism",
- __func__);
- res = -1;
- goto done;
- }
-
- event_changelist_freemem(&base->changelist); /* XXX */
- evmap_io_clear(&base->io);
- evmap_signal_clear(&base->sigmap);
+ /* Replace the original evsel. */
+ base->evsel = evsel;
- TAILQ_FOREACH(ev, &base->eventqueue, ev_next) {
- if (ev->ev_events & (EV_READ|EV_WRITE)) {
- if (ev == &base->sig.ev_signal) {
- /* If we run into the ev_signal event, it's only
- * in eventqueue because some signal event was
- * added, which made evsig_add re-add ev_signal.
- * So don't double-add it. */
- continue;
- }
- if (evmap_io_add(base, ev->ev_fd, ev) == -1)
- res = -1;
- } else if (ev->ev_events & EV_SIGNAL) {
- if (evmap_signal_add(base, (int)ev->ev_fd, ev) == -1)
- res = -1;
+ if (evsel->need_reinit) {
+ /* Reconstruct the backend through brute-force, so that we do
+ * not share any structures with the parent process. For some
+ * backends, this is necessary: epoll and kqueue, for
+ * instance, have events associated with a kernel
+ * structure. If didn't reinitialize, we'd share that
+ * structure with the parent process, and any changes made by
+ * the parent would affect our backend's behavior (and vice
+ * versa).
+ */
+ if (base->evsel->dealloc != NULL)
+ base->evsel->dealloc(base);
+ base->evbase = evsel->init(base);
+ if (base->evbase == NULL) {
+ event_errx(1,
+ "%s: could not reinitialize event mechanism",
+ __func__);
+ res = -1;
+ goto done;
}
+
+ /* Empty out the changelist (if any): we are starting from a
+ * blank slate. */
+ event_changelist_freemem_(&base->changelist);
+
+ /* Tell the event maps to re-inform the backend about all
+ * pending events. This will make the signal notification
+ * event get re-created if necessary. */
+ if (evmap_reinit_(base) < 0)
+ res = -1;
+ } else {
+ if (had_signal_added)
+ res = evsig_init_(base);
}
+ /* If we were notifiable before, and nothing just exploded, become
+ * notifiable again. */
if (was_notifiable && res == 0)
- res = evthread_make_base_notifiable(base);
+ res = evthread_make_base_notifiable_nolock_(base);
done:
EVBASE_RELEASE_LOCK(base, th_base_lock);
@@ -928,6 +1034,9 @@ event_config_new(void)
return (NULL);
TAILQ_INIT(&cfg->entries);
+ cfg->max_dispatch_interval.tv_sec = -1;
+ cfg->max_dispatch_callbacks = INT_MAX;
+ cfg->limit_callbacks_after_prio = 1;
return (cfg);
}
@@ -998,6 +1107,23 @@ event_config_set_num_cpus_hint(struct event_config *cfg, int cpus)
}
int
+event_config_set_max_dispatch_interval(struct event_config *cfg,
+ const struct timeval *max_interval, int max_callbacks, int min_priority)
+{
+ if (max_interval)
+ memcpy(&cfg->max_dispatch_interval, max_interval,
+ sizeof(struct timeval));
+ else
+ cfg->max_dispatch_interval.tv_sec = -1;
+ cfg->max_dispatch_callbacks =
+ max_callbacks >= 0 ? max_callbacks : INT_MAX;
+ if (min_priority < 0)
+ min_priority = 0;
+ cfg->limit_callbacks_after_prio = min_priority;
+ return (0);
+}
+
+int
event_priority_init(int npriorities)
{
return event_base_priority_init(current_base, npriorities);
@@ -1006,14 +1132,17 @@ event_priority_init(int npriorities)
int
event_base_priority_init(struct event_base *base, int npriorities)
{
- int i;
+ int i, r;
+ r = -1;
+
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
if (N_ACTIVE_CALLBACKS(base) || npriorities < 1
|| npriorities >= EVENT_MAX_PRIORITIES)
- return (-1);
+ goto err;
if (npriorities == base->nactivequeues)
- return (0);
+ goto ok;
if (base->nactivequeues) {
mm_free(base->activequeues);
@@ -1021,11 +1150,11 @@ event_base_priority_init(struct event_base *base, int npriorities)
}
/* Allocate our priority queues */
- base->activequeues = (struct event_list *)
- mm_calloc(npriorities, sizeof(struct event_list));
+ base->activequeues = (struct evcallback_list *)
+ mm_calloc(npriorities, sizeof(struct evcallback_list));
if (base->activequeues == NULL) {
event_warn("%s: calloc", __func__);
- return (-1);
+ goto err;
}
base->nactivequeues = npriorities;
@@ -1033,7 +1162,76 @@ event_base_priority_init(struct event_base *base, int npriorities)
TAILQ_INIT(&base->activequeues[i]);
}
- return (0);
+ok:
+ r = 0;
+err:
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ return (r);
+}
+
+int
+event_base_get_npriorities(struct event_base *base)
+{
+
+ int n;
+ if (base == NULL)
+ base = current_base;
+
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ n = base->nactivequeues;
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ return (n);
+}
+
+int
+event_base_get_num_events(struct event_base *base, unsigned int type)
+{
+ int r = 0;
+
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+
+ if (type & EVENT_BASE_COUNT_ACTIVE)
+ r += base->event_count_active;
+
+ if (type & EVENT_BASE_COUNT_VIRTUAL)
+ r += base->virtual_event_count;
+
+ if (type & EVENT_BASE_COUNT_ADDED)
+ r += base->event_count;
+
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+
+ return r;
+}
+
+int
+event_base_get_max_events(struct event_base *base, unsigned int type, int clear)
+{
+ int r = 0;
+
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+
+ if (type & EVENT_BASE_COUNT_ACTIVE) {
+ r += base->event_count_active_max;
+ if (clear)
+ base->event_count_active_max = 0;
+ }
+
+ if (type & EVENT_BASE_COUNT_VIRTUAL) {
+ r += base->virtual_event_count_max;
+ if (clear)
+ base->virtual_event_count_max = 0;
+ }
+
+ if (type & EVENT_BASE_COUNT_ADDED) {
+ r += base->event_count_max;
+ if (clear)
+ base->event_count_max = 0;
+ }
+
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+
+ return r;
}
/* Returns true iff we're currently watching any events. */
@@ -1147,7 +1345,7 @@ common_timeout_schedule(struct common_timeout_list *ctl,
{
struct timeval timeout = head->ev_timeout;
timeout.tv_usec &= MICROSECONDS_MASK;
- event_add_internal(&ctl->timeout_event, &timeout, 1);
+ event_add_nolock_(&ctl->timeout_event, &timeout, 1);
}
/* Callback: invoked when the timeout for a common timeout queue triggers.
@@ -1168,8 +1366,8 @@ common_timeout_callback(evutil_socket_t fd, short what, void *arg)
(ev->ev_timeout.tv_sec == now.tv_sec &&
(ev->ev_timeout.tv_usec&MICROSECONDS_MASK) > now.tv_usec))
break;
- event_del_internal(ev);
- event_active_nolock(ev, EV_TIMEOUT, 1);
+ event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
+ event_active_nolock_(ev, EV_TIMEOUT, 1);
}
if (ev)
common_timeout_schedule(ctl, &now, ev);
@@ -1256,7 +1454,6 @@ done:
static inline void
event_persist_closure(struct event_base *base, struct event *ev)
{
- // Define our callback, we use this to store our callback before it's executed
void (*evcb_callback)(evutil_socket_t, short, void *);
// Other fields of *ev that must be stored before executing
@@ -1303,7 +1500,7 @@ event_persist_closure(struct event_base *base, struct event *ev)
evutil_timeradd(&now, &delay, &run_at);
}
run_at.tv_usec |= usec_mask;
- event_add_internal(ev, &run_at, 1);
+ event_add_nolock_(ev, &run_at, 1);
}
// Save our callback before we release the lock
@@ -1324,55 +1521,103 @@ event_persist_closure(struct event_base *base, struct event *ev)
releasing the lock as we go. This function requires that the lock be held
when it's invoked. Returns -1 if we get a signal or an event_break that
means we should stop processing any active events now. Otherwise returns
- the number of non-internal events that we processed.
+ the number of non-internal event_callbacks that we processed.
*/
static int
event_process_active_single_queue(struct event_base *base,
- struct event_list *activeq)
+ struct evcallback_list *activeq,
+ int max_to_process, const struct timeval *endtime)
{
- struct event *ev;
+ struct event_callback *evcb;
int count = 0;
EVUTIL_ASSERT(activeq != NULL);
- for (ev = TAILQ_FIRST(activeq); ev; ev = TAILQ_FIRST(activeq)) {
- if (ev->ev_events & EV_PERSIST)
- event_queue_remove(base, ev, EVLIST_ACTIVE);
- else
- event_del_internal(ev);
- if (!(ev->ev_flags & EVLIST_INTERNAL))
+ for (evcb = TAILQ_FIRST(activeq); evcb; evcb = TAILQ_FIRST(activeq)) {
+ struct event *ev=NULL;
+ if (evcb->evcb_flags & EVLIST_INIT) {
+ ev = event_callback_to_event(evcb);
+
+ if (ev->ev_events & EV_PERSIST || ev->ev_flags & EVLIST_FINALIZING)
+ event_queue_remove_active(base, evcb);
+ else
+ event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
+ event_debug((
+ "event_process_active: event: %p, %s%s%scall %p",
+ ev,
+ ev->ev_res & EV_READ ? "EV_READ " : " ",
+ ev->ev_res & EV_WRITE ? "EV_WRITE " : " ",
+ ev->ev_res & EV_CLOSED ? "EV_CLOSED " : " ",
+ ev->ev_callback));
+ } else {
+ event_queue_remove_active(base, evcb);
+ event_debug(("event_process_active: event_callback %p, "
+ "closure %d, call %p",
+ evcb, evcb->evcb_closure, evcb->evcb_cb_union.evcb_callback));
+ }
+
+ if (!(evcb->evcb_flags & EVLIST_INTERNAL))
++count;
- event_debug((
- "event_process_active: event: %p, %s%scall %p",
- ev,
- ev->ev_res & EV_READ ? "EV_READ " : " ",
- ev->ev_res & EV_WRITE ? "EV_WRITE " : " ",
- ev->ev_callback));
-
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
- base->current_event = ev;
+
+ base->current_event = evcb;
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
base->current_event_waiters = 0;
#endif
- switch (ev->ev_closure) {
- case EV_CLOSURE_SIGNAL:
+ switch (evcb->evcb_closure) {
+ case EV_CLOSURE_EVENT_SIGNAL:
+ EVUTIL_ASSERT(ev != NULL);
event_signal_closure(base, ev);
break;
- case EV_CLOSURE_PERSIST:
+ case EV_CLOSURE_EVENT_PERSIST:
+ EVUTIL_ASSERT(ev != NULL);
event_persist_closure(base, ev);
break;
- default:
- case EV_CLOSURE_NONE:
+ case EV_CLOSURE_EVENT: {
+ void (*evcb_callback)(evutil_socket_t, short, void *);
+ EVUTIL_ASSERT(ev != NULL);
+ evcb_callback = *ev->ev_callback;
EVBASE_RELEASE_LOCK(base, th_base_lock);
- (*ev->ev_callback)(
- ev->ev_fd, ev->ev_res, ev->ev_arg);
- break;
+ evcb_callback(ev->ev_fd, ev->ev_res, ev->ev_arg);
+ }
+ break;
+ case EV_CLOSURE_CB_SELF: {
+ void (*evcb_selfcb)(struct event_callback *, void *) = evcb->evcb_cb_union.evcb_selfcb;
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ evcb_selfcb(evcb, evcb->evcb_arg);
+ }
+ break;
+ case EV_CLOSURE_EVENT_FINALIZE:
+ case EV_CLOSURE_EVENT_FINALIZE_FREE: {
+ void (*evcb_evfinalize)(struct event *, void *);
+ int evcb_closure = evcb->evcb_closure;
+ EVUTIL_ASSERT(ev != NULL);
+ base->current_event = NULL;
+ evcb_evfinalize = ev->ev_evcallback.evcb_cb_union.evcb_evfinalize;
+ EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_FINALIZING));
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ evcb_evfinalize(ev, ev->ev_arg);
+ event_debug_note_teardown_(ev);
+ if (evcb_closure == EV_CLOSURE_EVENT_FINALIZE_FREE)
+ mm_free(ev);
+ }
+ break;
+ case EV_CLOSURE_CB_FINALIZE: {
+ void (*evcb_cbfinalize)(struct event_callback *, void *) = evcb->evcb_cb_union.evcb_cbfinalize;
+ base->current_event = NULL;
+ EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_FINALIZING));
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ evcb_cbfinalize(evcb, evcb->evcb_arg);
+ }
+ break;
+ default:
+ EVUTIL_ASSERT(0);
}
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
base->current_event = NULL;
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
if (base->current_event_waiters) {
base->current_event_waiters = 0;
EVTHREAD_COND_BROADCAST(base->current_event_cond);
@@ -1381,6 +1626,15 @@ event_process_active_single_queue(struct event_base *base,
if (base->event_break)
return -1;
+ if (count >= max_to_process)
+ return count;
+ if (count && endtime) {
+ struct timeval now;
+ update_time_cache(base);
+ gettime(base, &now);
+ if (evutil_timercmp(&now, endtime, >=))
+ return count;
+ }
if (base->event_continue)
break;
}
@@ -1388,37 +1642,6 @@ event_process_active_single_queue(struct event_base *base,
}
/*
- Process up to MAX_DEFERRED of the defered_cb entries in 'queue'. If
- *breakptr becomes set to 1, stop. Requires that we start out holding
- the lock on 'queue'; releases the lock around 'queue' for each deferred_cb
- we process.
- */
-static int
-event_process_deferred_callbacks(struct deferred_cb_queue *queue, int *breakptr)
-{
- int count = 0;
- struct deferred_cb *cb;
-
-#define MAX_DEFERRED 16
- while ((cb = TAILQ_FIRST(&queue->deferred_cb_list))) {
- cb->queued = 0;
- TAILQ_REMOVE(&queue->deferred_cb_list, cb, cb_next);
- --queue->active_count;
- UNLOCK_DEFERRED_QUEUE(queue);
-
- cb->cb(cb, cb->arg);
-
- LOCK_DEFERRED_QUEUE(queue);
- if (*breakptr)
- return -1;
- if (++count == MAX_DEFERRED)
- break;
- }
-#undef MAX_DEFERRED
- return count;
-}
-
-/*
* Active events are stored in priority queues. Lower priorities are always
* process before higher priorities. Low priority events can starve high
* priority ones.
@@ -1428,17 +1651,33 @@ static int
event_process_active(struct event_base *base)
{
/* Caller must hold th_base_lock */
- struct event_list *activeq = NULL;
+ struct evcallback_list *activeq = NULL;
int i, c = 0;
+ const struct timeval *endtime;
+ struct timeval tv;
+ const int maxcb = base->max_dispatch_callbacks;
+ const int limit_after_prio = base->limit_callbacks_after_prio;
+ if (base->max_dispatch_time.tv_sec >= 0) {
+ update_time_cache(base);
+ gettime(base, &tv);
+ evutil_timeradd(&base->max_dispatch_time, &tv, &tv);
+ endtime = &tv;
+ } else {
+ endtime = NULL;
+ }
for (i = 0; i < base->nactivequeues; ++i) {
if (TAILQ_FIRST(&base->activequeues[i]) != NULL) {
base->event_running_priority = i;
activeq = &base->activequeues[i];
- c = event_process_active_single_queue(base, activeq);
+ if (i < limit_after_prio)
+ c = event_process_active_single_queue(base, activeq,
+ INT_MAX, NULL);
+ else
+ c = event_process_active_single_queue(base, activeq,
+ maxcb, endtime);
if (c < 0) {
- base->event_running_priority = -1;
- return -1;
+ goto done;
} else if (c > 0)
break; /* Processed a real event; do not
* consider lower-priority events */
@@ -1447,8 +1686,9 @@ event_process_active(struct event_base *base)
}
}
- event_process_deferred_callbacks(&base->defer_queue,&base->event_break);
+done:
base->event_running_priority = -1;
+
return c;
}
@@ -1524,6 +1764,25 @@ event_base_loopbreak(struct event_base *event_base)
}
int
+event_base_loopcontinue(struct event_base *event_base)
+{
+ int r = 0;
+ if (event_base == NULL)
+ return (-1);
+
+ EVBASE_ACQUIRE_LOCK(event_base, th_base_lock);
+ event_base->event_continue = 1;
+
+ if (EVBASE_NEED_NOTIFY(event_base)) {
+ r = evthread_notify_base(event_base);
+ } else {
+ r = (0);
+ }
+ EVBASE_RELEASE_LOCK(event_base, th_base_lock);
+ return r;
+}
+
+int
event_base_got_break(struct event_base *event_base)
{
int res;
@@ -1575,11 +1834,11 @@ event_base_loop(struct event_base *base, int flags)
clear_time_cache(base);
if (base->sig.ev_signal_added && base->sig.ev_n_signals_added)
- evsig_set_base(base);
+ evsig_set_base_(base);
done = 0;
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
base->th_owner_id = EVTHREAD_GET_ID();
#endif
@@ -1587,6 +1846,7 @@ event_base_loop(struct event_base *base, int flags)
while (!done) {
base->event_continue = 0;
+ base->n_deferreds_queued = 0;
/* Terminate the loop if we have been asked to */
if (base->event_gotterm) {
@@ -1597,8 +1857,6 @@ event_base_loop(struct event_base *base, int flags)
break;
}
- timeout_correct(base, &tv);
-
tv_p = &tv;
if (!N_ACTIVE_CALLBACKS(base) && !(flags & EVLOOP_NONBLOCK)) {
timeout_next(base, &tv_p);
@@ -1611,14 +1869,14 @@ event_base_loop(struct event_base *base, int flags)
}
/* If we have no events, we just exit */
- if (!event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) {
+ if (0==(flags&EVLOOP_NO_EXIT_ON_EMPTY) &&
+ !event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) {
event_debug(("%s: no events registered.", __func__));
retval = 1;
goto done;
}
- /* update last old time */
- gettime(base, &base->event_tv);
+ event_queue_make_later_events_active(base);
clear_time_cache(base);
@@ -1655,14 +1913,6 @@ done:
return (retval);
}
-/* Sets up an event for processing once */
-struct event_once {
- struct event ev;
-
- void (*cb)(evutil_socket_t, short, void *);
- void *arg;
-};
-
/* One-time callback to implement event_base_once: invokes the user callback,
* then deletes the allocated storage */
static void
@@ -1671,6 +1921,9 @@ event_once_cb(evutil_socket_t fd, short events, void *arg)
struct event_once *eonce = arg;
(*eonce->cb)(fd, events, eonce->arg);
+ EVBASE_ACQUIRE_LOCK(eonce->ev.ev_base, th_base_lock);
+ LIST_REMOVE(eonce, next_once);
+ EVBASE_RELEASE_LOCK(eonce->ev.ev_base, th_base_lock);
event_debug_unassign(&eonce->ev);
mm_free(eonce);
}
@@ -1691,8 +1944,8 @@ event_base_once(struct event_base *base, evutil_socket_t fd, short events,
void *arg, const struct timeval *tv)
{
struct event_once *eonce;
- struct timeval etv;
int res = 0;
+ int activate = 0;
/* We cannot support signals that just fire once, or persistent
* events. */
@@ -1705,15 +1958,18 @@ event_base_once(struct event_base *base, evutil_socket_t fd, short events,
eonce->cb = callback;
eonce->arg = arg;
- if (events == EV_TIMEOUT) {
- if (tv == NULL) {
- evutil_timerclear(&etv);
- tv = &etv;
- }
-
+ if ((events & (EV_TIMEOUT|EV_SIGNAL|EV_READ|EV_WRITE|EV_CLOSED)) == EV_TIMEOUT) {
evtimer_assign(&eonce->ev, base, event_once_cb, eonce);
- } else if (events & (EV_READ|EV_WRITE)) {
- events &= EV_READ|EV_WRITE;
+
+ if (tv == NULL || ! evutil_timerisset(tv)) {
+ /* If the event is going to become active immediately,
+ * don't put it on the timeout queue. This is one
+ * idiom for scheduling a callback, so let's make
+ * it fast (and order-preserving). */
+ activate = 1;
+ }
+ } else if (events & (EV_READ|EV_WRITE|EV_CLOSED)) {
+ events &= EV_READ|EV_WRITE|EV_CLOSED;
event_assign(&eonce->ev, base, fd, events, event_once_cb, eonce);
} else {
@@ -1722,11 +1978,20 @@ event_base_once(struct event_base *base, evutil_socket_t fd, short events,
return (-1);
}
- if (res == 0)
- res = event_add(&eonce->ev, tv);
- if (res != 0) {
- mm_free(eonce);
- return (res);
+ if (res == 0) {
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ if (activate)
+ event_active_nolock_(&eonce->ev, EV_TIMEOUT, 1);
+ else
+ res = event_add_nolock_(&eonce->ev, tv, 0);
+
+ if (res != 0) {
+ mm_free(eonce);
+ return (res);
+ } else {
+ LIST_INSERT_HEAD(&base->once_events, eonce, next_once);
+ }
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
}
return (0);
@@ -1737,8 +2002,10 @@ event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, shor
{
if (!base)
base = current_base;
+ if (arg == &event_self_cbarg_ptr_)
+ arg = ev;
- _event_debug_assert_not_added(ev);
+ event_debug_assert_not_added_(ev);
ev->ev_base = base;
@@ -1752,29 +2019,29 @@ event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, shor
ev->ev_pncalls = NULL;
if (events & EV_SIGNAL) {
- if ((events & (EV_READ|EV_WRITE)) != 0) {
+ if ((events & (EV_READ|EV_WRITE|EV_CLOSED)) != 0) {
event_warnx("%s: EV_SIGNAL is not compatible with "
- "EV_READ or EV_WRITE", __func__);
+ "EV_READ, EV_WRITE or EV_CLOSED", __func__);
return -1;
}
- ev->ev_closure = EV_CLOSURE_SIGNAL;
+ ev->ev_closure = EV_CLOSURE_EVENT_SIGNAL;
} else {
if (events & EV_PERSIST) {
evutil_timerclear(&ev->ev_io_timeout);
- ev->ev_closure = EV_CLOSURE_PERSIST;
+ ev->ev_closure = EV_CLOSURE_EVENT_PERSIST;
} else {
- ev->ev_closure = EV_CLOSURE_NONE;
+ ev->ev_closure = EV_CLOSURE_EVENT;
}
}
- min_heap_elem_init(ev);
+ min_heap_elem_init_(ev);
if (base != NULL) {
/* by default, we put new events into the middle priority */
ev->ev_pri = base->nactivequeues / 2;
}
- _event_debug_note_setup(ev);
+ event_debug_note_setup_(ev);
return 0;
}
@@ -1786,7 +2053,7 @@ event_base_set(struct event_base *base, struct event *ev)
if (ev->ev_flags != EVLIST_INIT)
return (-1);
- _event_debug_assert_is_setup(ev);
+ event_debug_assert_is_setup_(ev);
ev->ev_base = base;
ev->ev_pri = base->nactivequeues/2;
@@ -1803,6 +2070,26 @@ event_set(struct event *ev, evutil_socket_t fd, short events,
EVUTIL_ASSERT(r == 0);
}
+void *
+event_self_cbarg(void)
+{
+ return &event_self_cbarg_ptr_;
+}
+
+struct event *
+event_base_get_running_event(struct event_base *base)
+{
+ struct event *ev = NULL;
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ if (EVBASE_IN_THREAD(base)) {
+ struct event_callback *evcb = base->current_event;
+ if (evcb->evcb_flags & EVLIST_INIT)
+ ev = event_callback_to_event(evcb);
+ }
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ return ev;
+}
+
struct event *
event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg)
{
@@ -1821,11 +2108,13 @@ event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(
void
event_free(struct event *ev)
{
- _event_debug_assert_is_setup(ev);
+ /* This is disabled, so that events which have been finalized be a
+ * valid target for event_free(). That's */
+ // event_debug_assert_is_setup_(ev);
/* make sure that this event won't be coming back to haunt us. */
event_del(ev);
- _event_debug_note_teardown(ev);
+ event_debug_note_teardown_(ev);
mm_free(ev);
}
@@ -1833,12 +2122,117 @@ event_free(struct event *ev)
void
event_debug_unassign(struct event *ev)
{
- _event_debug_assert_not_added(ev);
- _event_debug_note_teardown(ev);
+ event_debug_assert_not_added_(ev);
+ event_debug_note_teardown_(ev);
ev->ev_flags &= ~EVLIST_INIT;
}
+#define EVENT_FINALIZE_FREE_ 0x10000
+static int
+event_finalize_nolock_(struct event_base *base, unsigned flags, struct event *ev, event_finalize_callback_fn cb)
+{
+ ev_uint8_t closure = (flags & EVENT_FINALIZE_FREE_) ?
+ EV_CLOSURE_EVENT_FINALIZE_FREE : EV_CLOSURE_EVENT_FINALIZE;
+
+ event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
+ ev->ev_closure = closure;
+ ev->ev_evcallback.evcb_cb_union.evcb_evfinalize = cb;
+ event_active_nolock_(ev, EV_FINALIZE, 1);
+ ev->ev_flags |= EVLIST_FINALIZING;
+ return 0;
+}
+
+static int
+event_finalize_impl_(unsigned flags, struct event *ev, event_finalize_callback_fn cb)
+{
+ int r;
+ struct event_base *base = ev->ev_base;
+ if (EVUTIL_FAILURE_CHECK(!base)) {
+ event_warnx("%s: event has no event_base set.", __func__);
+ return -1;
+ }
+
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ r = event_finalize_nolock_(base, flags, ev, cb);
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ return r;
+}
+
+int
+event_finalize(unsigned flags, struct event *ev, event_finalize_callback_fn cb)
+{
+ return event_finalize_impl_(flags, ev, cb);
+}
+
+int
+event_free_finalize(unsigned flags, struct event *ev, event_finalize_callback_fn cb)
+{
+ return event_finalize_impl_(flags|EVENT_FINALIZE_FREE_, ev, cb);
+}
+
+void
+event_callback_finalize_nolock_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *))
+{
+ struct event *ev = NULL;
+ if (evcb->evcb_flags & EVLIST_INIT) {
+ ev = event_callback_to_event(evcb);
+ event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
+ } else {
+ event_callback_cancel_nolock_(base, evcb, 0); /*XXX can this fail?*/
+ }
+
+ evcb->evcb_closure = EV_CLOSURE_CB_FINALIZE;
+ evcb->evcb_cb_union.evcb_cbfinalize = cb;
+ event_callback_activate_nolock_(base, evcb); /* XXX can this really fail?*/
+ evcb->evcb_flags |= EVLIST_FINALIZING;
+}
+
+void
+event_callback_finalize_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *))
+{
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ event_callback_finalize_nolock_(base, flags, evcb, cb);
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+}
+
+/** Internal: Finalize all of the n_cbs callbacks in evcbs. The provided
+ * callback will be invoked on *one of them*, after they have *all* been
+ * finalized. */
+int
+event_callback_finalize_many_(struct event_base *base, int n_cbs, struct event_callback **evcbs, void (*cb)(struct event_callback *, void *))
+{
+ int n_pending = 0, i;
+
+ if (base == NULL)
+ base = current_base;
+
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+
+ event_debug(("%s: %d events finalizing", __func__, n_cbs));
+
+ /* At most one can be currently executing; the rest we just
+ * cancel... But we always make sure that the finalize callback
+ * runs. */
+ for (i = 0; i < n_cbs; ++i) {
+ struct event_callback *evcb = evcbs[i];
+ if (evcb == base->current_event) {
+ event_callback_finalize_nolock_(base, 0, evcb, cb);
+ ++n_pending;
+ } else {
+ event_callback_cancel_nolock_(base, evcb, 0);
+ }
+ }
+
+ if (n_pending == 0) {
+ /* Just do the first one. */
+ event_callback_finalize_nolock_(base, 0, evcbs[0], cb);
+ }
+
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ return 0;
+}
+
/*
* Set's the priority of an event - if an event is already scheduled
* changing the priority is going to fail.
@@ -1847,7 +2241,7 @@ event_debug_unassign(struct event *ev)
int
event_priority_set(struct event *ev, int pri)
{
- _event_debug_assert_is_setup(ev);
+ event_debug_assert_is_setup_(ev);
if (ev->ev_flags & EVLIST_ACTIVE)
return (-1);
@@ -1874,27 +2268,23 @@ event_pending(const struct event *ev, short event, struct timeval *tv)
}
EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
- _event_debug_assert_is_setup(ev);
+ event_debug_assert_is_setup_(ev);
if (ev->ev_flags & EVLIST_INSERTED)
- flags |= (ev->ev_events & (EV_READ|EV_WRITE|EV_SIGNAL));
- if (ev->ev_flags & EVLIST_ACTIVE)
+ flags |= (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL));
+ if (ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))
flags |= ev->ev_res;
if (ev->ev_flags & EVLIST_TIMEOUT)
flags |= EV_TIMEOUT;
- event &= (EV_TIMEOUT|EV_READ|EV_WRITE|EV_SIGNAL);
+ event &= (EV_TIMEOUT|EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL);
/* See if there is a timeout that we should report */
if (tv != NULL && (flags & event & EV_TIMEOUT)) {
struct timeval tmp = ev->ev_timeout;
tmp.tv_usec &= MICROSECONDS_MASK;
-#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
/* correctly remamp to real time */
evutil_timeradd(&ev->ev_base->tv_clock_diff, &tmp, tv);
-#else
- *tv = tmp;
-#endif
}
EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
@@ -1914,7 +2304,7 @@ event_initialized(const struct event *ev)
void
event_get_assignment(const struct event *event, struct event_base **base_out, evutil_socket_t *fd_out, short *events_out, event_callback_fn *callback_out, void **arg_out)
{
- _event_debug_assert_is_setup(event);
+ event_debug_assert_is_setup_(event);
if (base_out)
*base_out = event->ev_base;
@@ -1937,39 +2327,46 @@ event_get_struct_event_size(void)
evutil_socket_t
event_get_fd(const struct event *ev)
{
- _event_debug_assert_is_setup(ev);
+ event_debug_assert_is_setup_(ev);
return ev->ev_fd;
}
struct event_base *
event_get_base(const struct event *ev)
{
- _event_debug_assert_is_setup(ev);
+ event_debug_assert_is_setup_(ev);
return ev->ev_base;
}
short
event_get_events(const struct event *ev)
{
- _event_debug_assert_is_setup(ev);
+ event_debug_assert_is_setup_(ev);
return ev->ev_events;
}
event_callback_fn
event_get_callback(const struct event *ev)
{
- _event_debug_assert_is_setup(ev);
+ event_debug_assert_is_setup_(ev);
return ev->ev_callback;
}
void *
event_get_callback_arg(const struct event *ev)
{
- _event_debug_assert_is_setup(ev);
+ event_debug_assert_is_setup_(ev);
return ev->ev_arg;
}
int
+event_get_priority(const struct event *ev)
+{
+ event_debug_assert_is_setup_(ev);
+ return ev->ev_pri;
+}
+
+int
event_add(struct event *ev, const struct timeval *tv)
{
int res;
@@ -1981,7 +2378,7 @@ event_add(struct event *ev, const struct timeval *tv)
EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
- res = event_add_internal(ev, tv, 0);
+ res = event_add_nolock_(ev, tv, 0);
EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
@@ -1998,15 +2395,15 @@ evthread_notify_base_default(struct event_base *base)
char buf[1];
int r;
buf[0] = (char) 0;
-#ifdef WIN32
+#ifdef _WIN32
r = send(base->th_notify_fd[1], buf, 1, 0);
#else
r = write(base->th_notify_fd[1], buf, 1);
#endif
- return (r < 0 && errno != EAGAIN) ? -1 : 0;
+ return (r < 0 && ! EVUTIL_ERR_IS_EAGAIN(errno)) ? -1 : 0;
}
-#if defined(_EVENT_HAVE_EVENTFD) && defined(_EVENT_HAVE_SYS_EVENTFD_H)
+#ifdef EVENT__HAVE_EVENTFD
/* Helper callback: wake an event_base from another thread. This version
* assumes that you have a working eventfd() implementation. */
static int
@@ -2022,9 +2419,10 @@ evthread_notify_base_eventfd(struct event_base *base)
}
#endif
+
/** Tell the thread currently running the event_loop for base (if any) that it
* needs to stop waiting in its dispatch function (if it is) and process all
- * active events and deferred callbacks (if there are any). */
+ * active callbacks. */
static int
evthread_notify_base(struct event_base *base)
{
@@ -2037,12 +2435,52 @@ evthread_notify_base(struct event_base *base)
return base->th_notify_fn(base);
}
+/* Implementation function to remove a timeout on a currently pending event.
+ */
+int
+event_remove_timer_nolock_(struct event *ev)
+{
+ struct event_base *base = ev->ev_base;
+
+ EVENT_BASE_ASSERT_LOCKED(base);
+ event_debug_assert_is_setup_(ev);
+
+ event_debug(("event_remove_timer_nolock: event: %p", ev));
+
+ /* If it's not pending on a timeout, we don't need to do anything. */
+ if (ev->ev_flags & EVLIST_TIMEOUT) {
+ event_queue_remove_timeout(base, ev);
+ evutil_timerclear(&ev->ev_.ev_io.ev_timeout);
+ }
+
+ return (0);
+}
+
+int
+event_remove_timer(struct event *ev)
+{
+ int res;
+
+ if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) {
+ event_warnx("%s: event has no event_base set.", __func__);
+ return -1;
+ }
+
+ EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
+
+ res = event_remove_timer_nolock_(ev);
+
+ EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
+
+ return (res);
+}
+
/* Implementation function to add an event. Works just like event_add,
* except: 1) it requires that we have the lock. 2) if tv_is_absolute is set,
* we treat tv as an absolute time, not as an interval to add to the current
* time */
-static inline int
-event_add_internal(struct event *ev, const struct timeval *tv,
+int
+event_add_nolock_(struct event *ev, const struct timeval *tv,
int tv_is_absolute)
{
struct event_base *base = ev->ev_base;
@@ -2050,26 +2488,32 @@ event_add_internal(struct event *ev, const struct timeval *tv,
int notify = 0;
EVENT_BASE_ASSERT_LOCKED(base);
- _event_debug_assert_is_setup(ev);
+ event_debug_assert_is_setup_(ev);
event_debug((
- "event_add: event: %p (fd "EV_SOCK_FMT"), %s%s%scall %p",
+ "event_add: event: %p (fd "EV_SOCK_FMT"), %s%s%s%scall %p",
ev,
EV_SOCK_ARG(ev->ev_fd),
ev->ev_events & EV_READ ? "EV_READ " : " ",
ev->ev_events & EV_WRITE ? "EV_WRITE " : " ",
+ ev->ev_events & EV_CLOSED ? "EV_CLOSED " : " ",
tv ? "EV_TIMEOUT " : " ",
ev->ev_callback));
EVUTIL_ASSERT(!(ev->ev_flags & ~EVLIST_ALL));
+ if (ev->ev_flags & EVLIST_FINALIZING) {
+ /* XXXX debug */
+ return (-1);
+ }
+
/*
* prepare for timeout insertion further below, if we get a
* failure on any step, we should not change any state.
*/
if (tv != NULL && !(ev->ev_flags & EVLIST_TIMEOUT)) {
- if (min_heap_reserve(&base->timeheap,
- 1 + min_heap_size(&base->timeheap)) == -1)
+ if (min_heap_reserve_(&base->timeheap,
+ 1 + min_heap_size_(&base->timeheap)) == -1)
return (-1); /* ENOMEM == errno */
}
@@ -2077,22 +2521,23 @@ event_add_internal(struct event *ev, const struct timeval *tv,
* callback, and we are not the main thread, then we want to wait
* until the callback is done before we mess with the event, or else
* we can race on ev_ncalls and ev_pncalls below. */
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
- if (base->current_event == ev && (ev->ev_events & EV_SIGNAL)
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
+ if (base->current_event == event_to_event_callback(ev) &&
+ (ev->ev_events & EV_SIGNAL)
&& !EVBASE_IN_THREAD(base)) {
++base->current_event_waiters;
EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock);
}
#endif
- if ((ev->ev_events & (EV_READ|EV_WRITE|EV_SIGNAL)) &&
- !(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE))) {
- if (ev->ev_events & (EV_READ|EV_WRITE))
- res = evmap_io_add(base, ev->ev_fd, ev);
+ if ((ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL)) &&
+ !(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) {
+ if (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED))
+ res = evmap_io_add_(base, ev->ev_fd, ev);
else if (ev->ev_events & EV_SIGNAL)
- res = evmap_signal_add(base, (int)ev->ev_fd, ev);
+ res = evmap_signal_add_(base, (int)ev->ev_fd, ev);
if (res != -1)
- event_queue_insert(base, ev, EVLIST_INSERTED);
+ event_queue_insert_inserted(base, ev);
if (res == 1) {
/* evmap says we need to notify the main thread. */
notify = 1;
@@ -2107,6 +2552,10 @@ event_add_internal(struct event *ev, const struct timeval *tv,
if (res != -1 && tv != NULL) {
struct timeval now;
int common_timeout;
+#ifdef USE_REINSERT_TIMEOUT
+ int was_common;
+ int old_timeout_idx;
+#endif
/*
* for persistent timeout events, we remember the
@@ -2114,19 +2563,14 @@ event_add_internal(struct event *ev, const struct timeval *tv,
*
* If tv_is_absolute, this was already set.
*/
- if (ev->ev_closure == EV_CLOSURE_PERSIST && !tv_is_absolute)
+ if (ev->ev_closure == EV_CLOSURE_EVENT_PERSIST && !tv_is_absolute)
ev->ev_io_timeout = *tv;
- /*
- * we already reserved memory above for the case where we
- * are not replacing an existing timeout.
- */
+#ifndef USE_REINSERT_TIMEOUT
if (ev->ev_flags & EVLIST_TIMEOUT) {
- /* XXX I believe this is needless. */
- if (min_heap_elt_is_top(ev))
- notify = 1;
- event_queue_remove(base, ev, EVLIST_TIMEOUT);
+ event_queue_remove_timeout(base, ev);
}
+#endif
/* Check if it is active due to a timeout. Rescheduling
* this timeout before the callback can be executed
@@ -2143,12 +2587,17 @@ event_add_internal(struct event *ev, const struct timeval *tv,
}
}
- event_queue_remove(base, ev, EVLIST_ACTIVE);
+ event_queue_remove_active(base, event_to_event_callback(ev));
}
gettime(base, &now);
common_timeout = is_common_timeout(tv, base);
+#ifdef USE_REINSERT_TIMEOUT
+ was_common = is_common_timeout(&ev->ev_timeout, base);
+ old_timeout_idx = COMMON_TIMEOUT_IDX(&ev->ev_timeout);
+#endif
+
if (tv_is_absolute) {
ev->ev_timeout = *tv;
} else if (common_timeout) {
@@ -2162,10 +2611,15 @@ event_add_internal(struct event *ev, const struct timeval *tv,
}
event_debug((
- "event_add: timeout in %d seconds, call %p",
- (int)tv->tv_sec, ev->ev_callback));
+ "event_add: event %p, timeout in %d seconds %d useconds, call %p",
+ ev, (int)tv->tv_sec, (int)tv->tv_usec, ev->ev_callback));
+
+#ifdef USE_REINSERT_TIMEOUT
+ event_queue_reinsert_timeout(base, ev, was_common, common_timeout, old_timeout_idx);
+#else
+ event_queue_insert_timeout(base, ev);
+#endif
- event_queue_insert(base, ev, EVLIST_TIMEOUT);
if (common_timeout) {
struct common_timeout_list *ctl =
get_common_timeout_list(base, &ev->ev_timeout);
@@ -2173,11 +2627,17 @@ event_add_internal(struct event *ev, const struct timeval *tv,
common_timeout_schedule(ctl, &now, ev);
}
} else {
+ struct event* top = NULL;
/* See if the earliest timeout is now earlier than it
* was before: if so, we will need to tell the main
- * thread to wake up earlier than it would
- * otherwise. */
- if (min_heap_elt_is_top(ev))
+ * thread to wake up earlier than it would otherwise.
+ * We double check the timeout of the top element to
+ * handle time distortions due to system suspension.
+ */
+ if (min_heap_elt_is_top_(ev))
+ notify = 1;
+ else if ((top = min_heap_top_(&base->timeheap)) != NULL &&
+ evutil_timercmp(&top->ev_timeout, &now, <))
notify = 1;
}
}
@@ -2186,13 +2646,13 @@ event_add_internal(struct event *ev, const struct timeval *tv,
if (res != -1 && notify && EVBASE_NEED_NOTIFY(base))
evthread_notify_base(base);
- _event_debug_note_add(ev);
+ event_debug_note_add_(ev);
return (res);
}
-int
-event_del(struct event *ev)
+static int
+event_del_(struct event *ev, int blocking)
{
int res;
@@ -2203,16 +2663,38 @@ event_del(struct event *ev)
EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
- res = event_del_internal(ev);
+ res = event_del_nolock_(ev, blocking);
EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
return (res);
}
-/* Helper for event_del: always called with th_base_lock held. */
-static inline int
-event_del_internal(struct event *ev)
+int
+event_del(struct event *ev)
+{
+ return event_del_(ev, EVENT_DEL_AUTOBLOCK);
+}
+
+int
+event_del_block(struct event *ev)
+{
+ return event_del_(ev, EVENT_DEL_BLOCK);
+}
+
+int
+event_del_noblock(struct event *ev)
+{
+ return event_del_(ev, EVENT_DEL_NOBLOCK);
+}
+
+/** Helper for event_del: always called with th_base_lock held.
+ *
+ * "blocking" must be one of the EVENT_DEL_{BLOCK, NOBLOCK, AUTOBLOCK,
+ * EVEN_IF_FINALIZING} values. See those for more information.
+ */
+int
+event_del_nolock_(struct event *ev, int blocking)
{
struct event_base *base;
int res = 0, notify = 0;
@@ -2226,14 +2708,24 @@ event_del_internal(struct event *ev)
EVENT_BASE_ASSERT_LOCKED(ev->ev_base);
+ if (blocking != EVENT_DEL_EVEN_IF_FINALIZING) {
+ if (ev->ev_flags & EVLIST_FINALIZING) {
+ /* XXXX Debug */
+ return 0;
+ }
+ }
+
/* If the main thread is currently executing this event's callback,
* and we are not the main thread, then we want to wait until the
* callback is done before we start removing the event. That way,
* when this function returns, it will be safe to free the
* user-supplied argument. */
base = ev->ev_base;
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
- if (base->current_event == ev && !EVBASE_IN_THREAD(base)) {
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
+ if (blocking != EVENT_DEL_NOBLOCK &&
+ base->current_event == event_to_event_callback(ev) &&
+ !EVBASE_IN_THREAD(base) &&
+ (blocking == EVENT_DEL_BLOCK || !(ev->ev_events & EV_FINALIZE))) {
++base->current_event_waiters;
EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock);
}
@@ -2257,18 +2749,20 @@ event_del_internal(struct event *ev)
* dispatch loop early anyway, so we wouldn't gain anything by
* doing it.
*/
- event_queue_remove(base, ev, EVLIST_TIMEOUT);
+ event_queue_remove_timeout(base, ev);
}
if (ev->ev_flags & EVLIST_ACTIVE)
- event_queue_remove(base, ev, EVLIST_ACTIVE);
+ event_queue_remove_active(base, event_to_event_callback(ev));
+ else if (ev->ev_flags & EVLIST_ACTIVE_LATER)
+ event_queue_remove_active_later(base, event_to_event_callback(ev));
if (ev->ev_flags & EVLIST_INSERTED) {
- event_queue_remove(base, ev, EVLIST_INSERTED);
- if (ev->ev_events & (EV_READ|EV_WRITE))
- res = evmap_io_del(base, ev->ev_fd, ev);
+ event_queue_remove_inserted(base, ev);
+ if (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED))
+ res = evmap_io_del_(base, ev->ev_fd, ev);
else
- res = evmap_signal_del(base, (int)ev->ev_fd, ev);
+ res = evmap_signal_del_(base, (int)ev->ev_fd, ev);
if (res == 1) {
/* evmap says we need to notify the main thread. */
notify = 1;
@@ -2280,7 +2774,7 @@ event_del_internal(struct event *ev)
if (res != -1 && notify && EVBASE_NEED_NOTIFY(base))
evthread_notify_base(base);
- _event_debug_note_del(ev);
+ event_debug_note_del_(ev);
return (res);
}
@@ -2295,41 +2789,54 @@ event_active(struct event *ev, int res, short ncalls)
EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
- _event_debug_assert_is_setup(ev);
+ event_debug_assert_is_setup_(ev);
- event_active_nolock(ev, res, ncalls);
+ event_active_nolock_(ev, res, ncalls);
EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
}
void
-event_active_nolock(struct event *ev, int res, short ncalls)
+event_active_nolock_(struct event *ev, int res, short ncalls)
{
struct event_base *base;
event_debug(("event_active: %p (fd "EV_SOCK_FMT"), res %d, callback %p",
ev, EV_SOCK_ARG(ev->ev_fd), (int)res, ev->ev_callback));
+ base = ev->ev_base;
+ EVENT_BASE_ASSERT_LOCKED(base);
- /* We get different kinds of events, add them together */
- if (ev->ev_flags & EVLIST_ACTIVE) {
- ev->ev_res |= res;
+ if (ev->ev_flags & EVLIST_FINALIZING) {
+ /* XXXX debug */
return;
}
- base = ev->ev_base;
-
- EVENT_BASE_ASSERT_LOCKED(base);
-
- ev->ev_res = res;
+ switch ((ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) {
+ default:
+ case EVLIST_ACTIVE|EVLIST_ACTIVE_LATER:
+ EVUTIL_ASSERT(0);
+ break;
+ case EVLIST_ACTIVE:
+ /* We get different kinds of events, add them together */
+ ev->ev_res |= res;
+ return;
+ case EVLIST_ACTIVE_LATER:
+ ev->ev_res |= res;
+ break;
+ case 0:
+ ev->ev_res = res;
+ break;
+ }
if (ev->ev_pri < base->event_running_priority)
base->event_continue = 1;
if (ev->ev_events & EV_SIGNAL) {
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
- if (base->current_event == ev && !EVBASE_IN_THREAD(base)) {
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
+ if (base->current_event == event_to_event_callback(ev) &&
+ !EVBASE_IN_THREAD(base)) {
++base->current_event_waiters;
EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock);
}
@@ -2338,60 +2845,176 @@ event_active_nolock(struct event *ev, int res, short ncalls)
ev->ev_pncalls = NULL;
}
- event_queue_insert(base, ev, EVLIST_ACTIVE);
+ event_callback_activate_nolock_(base, event_to_event_callback(ev));
+}
+
+void
+event_active_later_(struct event *ev, int res)
+{
+ EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
+ event_active_later_nolock_(ev, res);
+ EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
+}
+
+void
+event_active_later_nolock_(struct event *ev, int res)
+{
+ struct event_base *base = ev->ev_base;
+ EVENT_BASE_ASSERT_LOCKED(base);
+
+ if (ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) {
+ /* We get different kinds of events, add them together */
+ ev->ev_res |= res;
+ return;
+ }
+
+ ev->ev_res = res;
+
+ event_callback_activate_later_nolock_(base, event_to_event_callback(ev));
+}
+
+int
+event_callback_activate_(struct event_base *base,
+ struct event_callback *evcb)
+{
+ int r;
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ r = event_callback_activate_nolock_(base, evcb);
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ return r;
+}
+
+int
+event_callback_activate_nolock_(struct event_base *base,
+ struct event_callback *evcb)
+{
+ int r = 1;
+
+ if (evcb->evcb_flags & EVLIST_FINALIZING)
+ return 0;
+
+ switch (evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) {
+ default:
+ EVUTIL_ASSERT(0);
+ case EVLIST_ACTIVE_LATER:
+ event_queue_remove_active_later(base, evcb);
+ r = 0;
+ break;
+ case EVLIST_ACTIVE:
+ return 0;
+ case 0:
+ break;
+ }
+
+ event_queue_insert_active(base, evcb);
if (EVBASE_NEED_NOTIFY(base))
evthread_notify_base(base);
+
+ return r;
}
void
-event_deferred_cb_init(struct deferred_cb *cb, deferred_cb_fn fn, void *arg)
+event_callback_activate_later_nolock_(struct event_base *base,
+ struct event_callback *evcb)
{
- memset(cb, 0, sizeof(struct deferred_cb));
- cb->cb = fn;
- cb->arg = arg;
+ if (evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))
+ return;
+
+ event_queue_insert_active_later(base, evcb);
+ if (EVBASE_NEED_NOTIFY(base))
+ evthread_notify_base(base);
}
void
-event_deferred_cb_cancel(struct deferred_cb_queue *queue,
- struct deferred_cb *cb)
+event_callback_init_(struct event_base *base,
+ struct event_callback *cb)
{
- if (!queue) {
- if (current_base)
- queue = &current_base->defer_queue;
- else
- return;
- }
+ memset(cb, 0, sizeof(*cb));
+ cb->evcb_pri = base->nactivequeues - 1;
+}
+
+int
+event_callback_cancel_(struct event_base *base,
+ struct event_callback *evcb)
+{
+ int r;
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ r = event_callback_cancel_nolock_(base, evcb, 0);
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ return r;
+}
+
+int
+event_callback_cancel_nolock_(struct event_base *base,
+ struct event_callback *evcb, int even_if_finalizing)
+{
+ if ((evcb->evcb_flags & EVLIST_FINALIZING) && !even_if_finalizing)
+ return 0;
+
+ if (evcb->evcb_flags & EVLIST_INIT)
+ return event_del_nolock_(event_callback_to_event(evcb),
+ even_if_finalizing ? EVENT_DEL_EVEN_IF_FINALIZING : EVENT_DEL_AUTOBLOCK);
- LOCK_DEFERRED_QUEUE(queue);
- if (cb->queued) {
- TAILQ_REMOVE(&queue->deferred_cb_list, cb, cb_next);
- --queue->active_count;
- cb->queued = 0;
+ switch ((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) {
+ default:
+ case EVLIST_ACTIVE|EVLIST_ACTIVE_LATER:
+ EVUTIL_ASSERT(0);
+ break;
+ case EVLIST_ACTIVE:
+ /* We get different kinds of events, add them together */
+ event_queue_remove_active(base, evcb);
+ return 0;
+ case EVLIST_ACTIVE_LATER:
+ event_queue_remove_active_later(base, evcb);
+ break;
+ case 0:
+ break;
}
- UNLOCK_DEFERRED_QUEUE(queue);
+
+ return 0;
}
void
-event_deferred_cb_schedule(struct deferred_cb_queue *queue,
- struct deferred_cb *cb)
+event_deferred_cb_init_(struct event_callback *cb, ev_uint8_t priority, deferred_cb_fn fn, void *arg)
{
- if (!queue) {
- if (current_base)
- queue = &current_base->defer_queue;
- else
- return;
- }
+ memset(cb, 0, sizeof(*cb));
+ cb->evcb_cb_union.evcb_selfcb = fn;
+ cb->evcb_arg = arg;
+ cb->evcb_pri = priority;
+ cb->evcb_closure = EV_CLOSURE_CB_SELF;
+}
- LOCK_DEFERRED_QUEUE(queue);
- if (!cb->queued) {
- cb->queued = 1;
- TAILQ_INSERT_TAIL(&queue->deferred_cb_list, cb, cb_next);
- ++queue->active_count;
- if (queue->notify_fn)
- queue->notify_fn(queue, queue->notify_arg);
+void
+event_deferred_cb_set_priority_(struct event_callback *cb, ev_uint8_t priority)
+{
+ cb->evcb_pri = priority;
+}
+
+void
+event_deferred_cb_cancel_(struct event_base *base, struct event_callback *cb)
+{
+ if (!base)
+ base = current_base;
+ event_callback_cancel_(base, cb);
+}
+
+#define MAX_DEFERREDS_QUEUED 32
+int
+event_deferred_cb_schedule_(struct event_base *base, struct event_callback *cb)
+{
+ int r = 1;
+ if (!base)
+ base = current_base;
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ if (base->n_deferreds_queued > MAX_DEFERREDS_QUEUED) {
+ event_callback_activate_later_nolock_(base, cb);
+ } else {
+ ++base->n_deferreds_queued;
+ r = event_callback_activate_nolock_(base, cb);
}
- UNLOCK_DEFERRED_QUEUE(queue);
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ return r;
}
static int
@@ -2403,7 +3026,7 @@ timeout_next(struct event_base *base, struct timeval **tv_p)
struct timeval *tv = *tv_p;
int res = 0;
- ev = min_heap_top(&base->timeheap);
+ ev = min_heap_top_(&base->timeheap);
if (ev == NULL) {
/* if no time-based events are active wait for I/O */
@@ -2425,70 +3048,12 @@ timeout_next(struct event_base *base, struct timeval **tv_p)
EVUTIL_ASSERT(tv->tv_sec >= 0);
EVUTIL_ASSERT(tv->tv_usec >= 0);
- event_debug(("timeout_next: in %d seconds", (int)tv->tv_sec));
+ event_debug(("timeout_next: event: %p, in %d seconds, %d useconds", ev, (int)tv->tv_sec, (int)tv->tv_usec));
out:
return (res);
}
-/*
- * Determines if the time is running backwards by comparing the current time
- * against the last time we checked. Not needed when using clock monotonic.
- * If time is running backwards, we adjust the firing time of every event by
- * the amount that time seems to have jumped.
- */
-static void
-timeout_correct(struct event_base *base, struct timeval *tv)
-{
- /* Caller must hold th_base_lock. */
- struct event **pev;
- unsigned int size;
- struct timeval off;
- int i;
-
- if (use_monotonic)
- return;
-
- /* Check if time is running backwards */
- gettime(base, tv);
-
- if (evutil_timercmp(tv, &base->event_tv, >=)) {
- base->event_tv = *tv;
- return;
- }
-
- event_debug(("%s: time is running backwards, corrected",
- __func__));
- evutil_timersub(&base->event_tv, tv, &off);
-
- /*
- * We can modify the key element of the node without destroying
- * the minheap property, because we change every element.
- */
- pev = base->timeheap.p;
- size = base->timeheap.n;
- for (; size-- > 0; ++pev) {
- struct timeval *ev_tv = &(**pev).ev_timeout;
- evutil_timersub(ev_tv, &off, ev_tv);
- }
- for (i=0; i<base->n_common_timeouts; ++i) {
- struct event *ev;
- struct common_timeout_list *ctl =
- base->common_timeout_queues[i];
- TAILQ_FOREACH(ev, &ctl->events,
- ev_timeout_pos.ev_next_with_common_timeout) {
- struct timeval *ev_tv = &ev->ev_timeout;
- ev_tv->tv_usec &= MICROSECONDS_MASK;
- evutil_timersub(ev_tv, &off, ev_tv);
- ev_tv->tv_usec |= COMMON_TIMEOUT_MAGIC |
- (i<<COMMON_TIMEOUT_IDX_SHIFT);
- }
- }
-
- /* Now remember what the new time turned out to be. */
- base->event_tv = *tv;
-}
-
/* Activate every event whose timeout has elapsed. */
static void
timeout_process(struct event_base *base)
@@ -2497,64 +3062,151 @@ timeout_process(struct event_base *base)
struct timeval now;
struct event *ev;
- if (min_heap_empty(&base->timeheap)) {
+ if (min_heap_empty_(&base->timeheap)) {
return;
}
gettime(base, &now);
- while ((ev = min_heap_top(&base->timeheap))) {
+ while ((ev = min_heap_top_(&base->timeheap))) {
if (evutil_timercmp(&ev->ev_timeout, &now, >))
break;
/* delete this event from the I/O queues */
- event_del_internal(ev);
+ event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
+
+ event_debug(("timeout_process: event: %p, call %p",
+ ev, ev->ev_callback));
+ event_active_nolock_(ev, EV_TIMEOUT, 1);
+ }
+}
- event_debug(("timeout_process: call %p",
- ev->ev_callback));
- event_active_nolock(ev, EV_TIMEOUT, 1);
+#if (EVLIST_INTERNAL >> 4) != 1
+#error "Mismatch for value of EVLIST_INTERNAL"
+#endif
+
+#ifndef MAX
+#define MAX(a,b) (((a)>(b))?(a):(b))
+#endif
+
+#define MAX_EVENT_COUNT(var, v) var = MAX(var, v)
+
+/* These are a fancy way to spell
+ if (flags & EVLIST_INTERNAL)
+ base->event_count--/++;
+*/
+#define DECR_EVENT_COUNT(base,flags) \
+ ((base)->event_count -= (~((flags) >> 4) & 1))
+#define INCR_EVENT_COUNT(base,flags) do { \
+ ((base)->event_count += (~((flags) >> 4) & 1)); \
+ MAX_EVENT_COUNT((base)->event_count_max, (base)->event_count); \
+} while (0)
+
+static void
+event_queue_remove_inserted(struct event_base *base, struct event *ev)
+{
+ EVENT_BASE_ASSERT_LOCKED(base);
+ if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_INSERTED))) {
+ event_errx(1, "%s: %p(fd "EV_SOCK_FMT") not on queue %x", __func__,
+ ev, EV_SOCK_ARG(ev->ev_fd), EVLIST_INSERTED);
+ return;
}
+ DECR_EVENT_COUNT(base, ev->ev_flags);
+ ev->ev_flags &= ~EVLIST_INSERTED;
}
+static void
+event_queue_remove_active(struct event_base *base, struct event_callback *evcb)
+{
+ EVENT_BASE_ASSERT_LOCKED(base);
+ if (EVUTIL_FAILURE_CHECK(!(evcb->evcb_flags & EVLIST_ACTIVE))) {
+ event_errx(1, "%s: %p not on queue %x", __func__,
+ evcb, EVLIST_ACTIVE);
+ return;
+ }
+ DECR_EVENT_COUNT(base, evcb->evcb_flags);
+ evcb->evcb_flags &= ~EVLIST_ACTIVE;
+ base->event_count_active--;
-/* Remove 'ev' from 'queue' (EVLIST_...) in base. */
+ TAILQ_REMOVE(&base->activequeues[evcb->evcb_pri],
+ evcb, evcb_active_next);
+}
static void
-event_queue_remove(struct event_base *base, struct event *ev, int queue)
+event_queue_remove_active_later(struct event_base *base, struct event_callback *evcb)
{
EVENT_BASE_ASSERT_LOCKED(base);
+ if (EVUTIL_FAILURE_CHECK(!(evcb->evcb_flags & EVLIST_ACTIVE_LATER))) {
+ event_errx(1, "%s: %p not on queue %x", __func__,
+ evcb, EVLIST_ACTIVE_LATER);
+ return;
+ }
+ DECR_EVENT_COUNT(base, evcb->evcb_flags);
+ evcb->evcb_flags &= ~EVLIST_ACTIVE_LATER;
+ base->event_count_active--;
- if (!(ev->ev_flags & queue)) {
+ TAILQ_REMOVE(&base->active_later_queue, evcb, evcb_active_next);
+}
+static void
+event_queue_remove_timeout(struct event_base *base, struct event *ev)
+{
+ EVENT_BASE_ASSERT_LOCKED(base);
+ if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_TIMEOUT))) {
event_errx(1, "%s: %p(fd "EV_SOCK_FMT") not on queue %x", __func__,
- ev, EV_SOCK_ARG(ev->ev_fd), queue);
+ ev, EV_SOCK_ARG(ev->ev_fd), EVLIST_TIMEOUT);
return;
}
+ DECR_EVENT_COUNT(base, ev->ev_flags);
+ ev->ev_flags &= ~EVLIST_TIMEOUT;
- if (~ev->ev_flags & EVLIST_INTERNAL)
- base->event_count--;
+ if (is_common_timeout(&ev->ev_timeout, base)) {
+ struct common_timeout_list *ctl =
+ get_common_timeout_list(base, &ev->ev_timeout);
+ TAILQ_REMOVE(&ctl->events, ev,
+ ev_timeout_pos.ev_next_with_common_timeout);
+ } else {
+ min_heap_erase_(&base->timeheap, ev);
+ }
+}
- ev->ev_flags &= ~queue;
- switch (queue) {
- case EVLIST_INSERTED:
- TAILQ_REMOVE(&base->eventqueue, ev, ev_next);
+#ifdef USE_REINSERT_TIMEOUT
+/* Remove and reinsert 'ev' into the timeout queue. */
+static void
+event_queue_reinsert_timeout(struct event_base *base, struct event *ev,
+ int was_common, int is_common, int old_timeout_idx)
+{
+ struct common_timeout_list *ctl;
+ if (!(ev->ev_flags & EVLIST_TIMEOUT)) {
+ event_queue_insert_timeout(base, ev);
+ return;
+ }
+
+ switch ((was_common<<1) | is_common) {
+ case 3: /* Changing from one common timeout to another */
+ ctl = base->common_timeout_queues[old_timeout_idx];
+ TAILQ_REMOVE(&ctl->events, ev,
+ ev_timeout_pos.ev_next_with_common_timeout);
+ ctl = get_common_timeout_list(base, &ev->ev_timeout);
+ insert_common_timeout_inorder(ctl, ev);
break;
- case EVLIST_ACTIVE:
- base->event_count_active--;
- TAILQ_REMOVE(&base->activequeues[ev->ev_pri],
- ev, ev_active_next);
+ case 2: /* Was common; is no longer common */
+ ctl = base->common_timeout_queues[old_timeout_idx];
+ TAILQ_REMOVE(&ctl->events, ev,
+ ev_timeout_pos.ev_next_with_common_timeout);
+ min_heap_push_(&base->timeheap, ev);
break;
- case EVLIST_TIMEOUT:
- if (is_common_timeout(&ev->ev_timeout, base)) {
- struct common_timeout_list *ctl =
- get_common_timeout_list(base, &ev->ev_timeout);
- TAILQ_REMOVE(&ctl->events, ev,
- ev_timeout_pos.ev_next_with_common_timeout);
- } else {
- min_heap_erase(&base->timeheap, ev);
- }
+ case 1: /* Wasn't common; has become common. */
+ min_heap_erase_(&base->timeheap, ev);
+ ctl = get_common_timeout_list(base, &ev->ev_timeout);
+ insert_common_timeout_inorder(ctl, ev);
+ break;
+ case 0: /* was in heap; is still on heap. */
+ min_heap_adjust_(&base->timeheap, ev);
break;
default:
- event_errx(1, "%s: unknown queue %x", __func__, queue);
+ EVUTIL_ASSERT(0); /* unreachable */
+ break;
}
}
+#endif
/* Add 'ev' to the common timeout list in 'ev'. */
static void
@@ -2588,44 +3240,95 @@ insert_common_timeout_inorder(struct common_timeout_list *ctl,
}
static void
-event_queue_insert(struct event_base *base, struct event *ev, int queue)
+event_queue_insert_inserted(struct event_base *base, struct event *ev)
+{
+ EVENT_BASE_ASSERT_LOCKED(base);
+
+ if (EVUTIL_FAILURE_CHECK(ev->ev_flags & EVLIST_INSERTED)) {
+ event_errx(1, "%s: %p(fd "EV_SOCK_FMT") already inserted", __func__,
+ ev, EV_SOCK_ARG(ev->ev_fd));
+ return;
+ }
+
+ INCR_EVENT_COUNT(base, ev->ev_flags);
+
+ ev->ev_flags |= EVLIST_INSERTED;
+}
+
+static void
+event_queue_insert_active(struct event_base *base, struct event_callback *evcb)
{
EVENT_BASE_ASSERT_LOCKED(base);
- if (ev->ev_flags & queue) {
+ if (evcb->evcb_flags & EVLIST_ACTIVE) {
/* Double insertion is possible for active events */
- if (queue & EVLIST_ACTIVE)
- return;
+ return;
+ }
+
+ INCR_EVENT_COUNT(base, evcb->evcb_flags);
- event_errx(1, "%s: %p(fd "EV_SOCK_FMT") already on queue %x", __func__,
- ev, EV_SOCK_ARG(ev->ev_fd), queue);
+ evcb->evcb_flags |= EVLIST_ACTIVE;
+
+ base->event_count_active++;
+ MAX_EVENT_COUNT(base->event_count_active_max, base->event_count_active);
+ EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues);
+ TAILQ_INSERT_TAIL(&base->activequeues[evcb->evcb_pri],
+ evcb, evcb_active_next);
+}
+
+static void
+event_queue_insert_active_later(struct event_base *base, struct event_callback *evcb)
+{
+ EVENT_BASE_ASSERT_LOCKED(base);
+ if (evcb->evcb_flags & (EVLIST_ACTIVE_LATER|EVLIST_ACTIVE)) {
+ /* Double insertion is possible */
return;
}
- if (~ev->ev_flags & EVLIST_INTERNAL)
- base->event_count++;
+ INCR_EVENT_COUNT(base, evcb->evcb_flags);
+ evcb->evcb_flags |= EVLIST_ACTIVE_LATER;
+ base->event_count_active++;
+ MAX_EVENT_COUNT(base->event_count_active_max, base->event_count_active);
+ EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues);
+ TAILQ_INSERT_TAIL(&base->active_later_queue, evcb, evcb_active_next);
+}
- ev->ev_flags |= queue;
- switch (queue) {
- case EVLIST_INSERTED:
- TAILQ_INSERT_TAIL(&base->eventqueue, ev, ev_next);
- break;
- case EVLIST_ACTIVE:
- base->event_count_active++;
- TAILQ_INSERT_TAIL(&base->activequeues[ev->ev_pri],
- ev,ev_active_next);
- break;
- case EVLIST_TIMEOUT: {
- if (is_common_timeout(&ev->ev_timeout, base)) {
- struct common_timeout_list *ctl =
- get_common_timeout_list(base, &ev->ev_timeout);
- insert_common_timeout_inorder(ctl, ev);
- } else
- min_heap_push(&base->timeheap, ev);
- break;
+static void
+event_queue_insert_timeout(struct event_base *base, struct event *ev)
+{
+ EVENT_BASE_ASSERT_LOCKED(base);
+
+ if (EVUTIL_FAILURE_CHECK(ev->ev_flags & EVLIST_TIMEOUT)) {
+ event_errx(1, "%s: %p(fd "EV_SOCK_FMT") already on timeout", __func__,
+ ev, EV_SOCK_ARG(ev->ev_fd));
+ return;
}
- default:
- event_errx(1, "%s: unknown queue %x", __func__, queue);
+
+ INCR_EVENT_COUNT(base, ev->ev_flags);
+
+ ev->ev_flags |= EVLIST_TIMEOUT;
+
+ if (is_common_timeout(&ev->ev_timeout, base)) {
+ struct common_timeout_list *ctl =
+ get_common_timeout_list(base, &ev->ev_timeout);
+ insert_common_timeout_inorder(ctl, ev);
+ } else {
+ min_heap_push_(&base->timeheap, ev);
+ }
+}
+
+static void
+event_queue_make_later_events_active(struct event_base *base)
+{
+ struct event_callback *evcb;
+ EVENT_BASE_ASSERT_LOCKED(base);
+
+ while ((evcb = TAILQ_FIRST(&base->active_later_queue))) {
+ TAILQ_REMOVE(&base->active_later_queue, evcb, evcb_active_next);
+ evcb->evcb_flags = (evcb->evcb_flags & ~EVLIST_ACTIVE_LATER) | EVLIST_ACTIVE;
+ EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues);
+ TAILQ_INSERT_TAIL(&base->activequeues[evcb->evcb_pri], evcb, evcb_active_next);
+ base->n_deferreds_queued += (evcb->evcb_closure == EV_CLOSURE_CB_SELF);
}
}
@@ -2634,13 +3337,13 @@ event_queue_insert(struct event_base *base, struct event *ev, int queue)
const char *
event_get_version(void)
{
- return (_EVENT_VERSION);
+ return (EVENT__VERSION);
}
ev_uint32_t
event_get_version_number(void)
{
- return (_EVENT_NUMERIC_VERSION);
+ return (EVENT__NUMERIC_VERSION);
}
/*
@@ -2654,16 +3357,19 @@ event_get_method(void)
return (current_base->evsel->name);
}
-#ifndef _EVENT_DISABLE_MM_REPLACEMENT
-static void *(*_mm_malloc_fn)(size_t sz) = NULL;
-static void *(*_mm_realloc_fn)(void *p, size_t sz) = NULL;
-static void (*_mm_free_fn)(void *p) = NULL;
+#ifndef EVENT__DISABLE_MM_REPLACEMENT
+static void *(*mm_malloc_fn_)(size_t sz) = NULL;
+static void *(*mm_realloc_fn_)(void *p, size_t sz) = NULL;
+static void (*mm_free_fn_)(void *p) = NULL;
void *
event_mm_malloc_(size_t sz)
{
- if (_mm_malloc_fn)
- return _mm_malloc_fn(sz);
+ if (sz == 0)
+ return NULL;
+
+ if (mm_malloc_fn_)
+ return mm_malloc_fn_(sz);
else
return malloc(sz);
}
@@ -2671,38 +3377,65 @@ event_mm_malloc_(size_t sz)
void *
event_mm_calloc_(size_t count, size_t size)
{
- if (_mm_malloc_fn) {
+ if (count == 0 || size == 0)
+ return NULL;
+
+ if (mm_malloc_fn_) {
size_t sz = count * size;
- void *p = _mm_malloc_fn(sz);
+ void *p = NULL;
+ if (count > EV_SIZE_MAX / size)
+ goto error;
+ p = mm_malloc_fn_(sz);
if (p)
- memset(p, 0, sz);
+ return memset(p, 0, sz);
+ } else {
+ void *p = calloc(count, size);
+#ifdef _WIN32
+ /* Windows calloc doesn't reliably set ENOMEM */
+ if (p == NULL)
+ goto error;
+#endif
return p;
- } else
- return calloc(count, size);
+ }
+
+error:
+ errno = ENOMEM;
+ return NULL;
}
char *
event_mm_strdup_(const char *str)
{
- if (_mm_malloc_fn) {
+ if (!str) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ if (mm_malloc_fn_) {
size_t ln = strlen(str);
- void *p = _mm_malloc_fn(ln+1);
+ void *p = NULL;
+ if (ln == EV_SIZE_MAX)
+ goto error;
+ p = mm_malloc_fn_(ln+1);
if (p)
- memcpy(p, str, ln+1);
- return p;
+ return memcpy(p, str, ln+1);
} else
-#ifdef WIN32
+#ifdef _WIN32
return _strdup(str);
#else
return strdup(str);
#endif
+
+error:
+ errno = ENOMEM;
+ return NULL;
}
void *
event_mm_realloc_(void *ptr, size_t sz)
{
- if (_mm_realloc_fn)
- return _mm_realloc_fn(ptr, sz);
+ if (mm_realloc_fn_)
+ return mm_realloc_fn_(ptr, sz);
else
return realloc(ptr, sz);
}
@@ -2710,8 +3443,8 @@ event_mm_realloc_(void *ptr, size_t sz)
void
event_mm_free_(void *ptr)
{
- if (_mm_free_fn)
- _mm_free_fn(ptr);
+ if (mm_free_fn_)
+ mm_free_fn_(ptr);
else
free(ptr);
}
@@ -2721,13 +3454,13 @@ event_set_mem_functions(void *(*malloc_fn)(size_t sz),
void *(*realloc_fn)(void *ptr, size_t sz),
void (*free_fn)(void *ptr))
{
- _mm_malloc_fn = malloc_fn;
- _mm_realloc_fn = realloc_fn;
- _mm_free_fn = free_fn;
+ mm_malloc_fn_ = malloc_fn;
+ mm_realloc_fn_ = realloc_fn;
+ mm_free_fn_ = free_fn;
}
#endif
-#if defined(_EVENT_HAVE_EVENTFD) && defined(_EVENT_HAVE_SYS_EVENTFD_H)
+#ifdef EVENT__HAVE_EVENTFD
static void
evthread_notify_drain_eventfd(evutil_socket_t fd, short what, void *arg)
{
@@ -2750,7 +3483,7 @@ evthread_notify_drain_default(evutil_socket_t fd, short what, void *arg)
{
unsigned char buf[1024];
struct event_base *base = arg;
-#ifdef WIN32
+#ifdef _WIN32
while (recv(fd, (char*)buf, sizeof(buf), 0) > 0)
;
#else
@@ -2766,71 +3499,54 @@ evthread_notify_drain_default(evutil_socket_t fd, short what, void *arg)
int
evthread_make_base_notifiable(struct event_base *base)
{
- void (*cb)(evutil_socket_t, short, void *) = evthread_notify_drain_default;
- int (*notify)(struct event_base *) = evthread_notify_base_default;
-
- /* XXXX grab the lock here? */
+ int r;
if (!base)
return -1;
- if (base->th_notify_fd[0] >= 0)
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ r = evthread_make_base_notifiable_nolock_(base);
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ return r;
+}
+
+static int
+evthread_make_base_notifiable_nolock_(struct event_base *base)
+{
+ void (*cb)(evutil_socket_t, short, void *);
+ int (*notify)(struct event_base *);
+
+ if (base->th_notify_fn != NULL) {
+ /* The base is already notifiable: we're doing fine. */
return 0;
+ }
-#if defined(_EVENT_HAVE_EVENTFD) && defined(_EVENT_HAVE_SYS_EVENTFD_H)
-#ifndef EFD_CLOEXEC
-#define EFD_CLOEXEC 0
+#if defined(EVENT__HAVE_WORKING_KQUEUE)
+ if (base->evsel == &kqops && event_kq_add_notify_event_(base) == 0) {
+ base->th_notify_fn = event_kq_notify_base_;
+ /* No need to add an event here; the backend can wake
+ * itself up just fine. */
+ return 0;
+ }
#endif
- base->th_notify_fd[0] = eventfd(0, EFD_CLOEXEC);
+
+#ifdef EVENT__HAVE_EVENTFD
+ base->th_notify_fd[0] = evutil_eventfd_(0,
+ EVUTIL_EFD_CLOEXEC|EVUTIL_EFD_NONBLOCK);
if (base->th_notify_fd[0] >= 0) {
- evutil_make_socket_closeonexec(base->th_notify_fd[0]);
+ base->th_notify_fd[1] = -1;
notify = evthread_notify_base_eventfd;
cb = evthread_notify_drain_eventfd;
- }
-#endif
-#if defined(_EVENT_HAVE_PIPE)
- if (base->th_notify_fd[0] < 0) {
- if ((base->evsel->features & EV_FEATURE_FDS)) {
- if (pipe(base->th_notify_fd) < 0) {
- event_warn("%s: pipe", __func__);
- } else {
- evutil_make_socket_closeonexec(base->th_notify_fd[0]);
- evutil_make_socket_closeonexec(base->th_notify_fd[1]);
- }
- }
- }
-#endif
-
-#ifdef WIN32
-#define LOCAL_SOCKETPAIR_AF AF_INET
-#else
-#define LOCAL_SOCKETPAIR_AF AF_UNIX
+ } else
#endif
- if (base->th_notify_fd[0] < 0) {
- if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0,
- base->th_notify_fd) == -1) {
- event_sock_warn(-1, "%s: socketpair", __func__);
- return (-1);
- } else {
- evutil_make_socket_closeonexec(base->th_notify_fd[0]);
- evutil_make_socket_closeonexec(base->th_notify_fd[1]);
- }
+ if (evutil_make_internal_pipe_(base->th_notify_fd) == 0) {
+ notify = evthread_notify_base_default;
+ cb = evthread_notify_drain_default;
+ } else {
+ return -1;
}
- evutil_make_socket_nonblocking(base->th_notify_fd[0]);
-
base->th_notify_fn = notify;
- /*
- Making the second socket nonblocking is a bit subtle, given that we
- ignore any EAGAIN returns when writing to it, and you don't usally
- do that for a nonblocking socket. But if the kernel gives us EAGAIN,
- then there's no need to add any more data to the buffer, since
- the main thread is already either about to wake up and drain it,
- or woken up and in the process of draining it.
- */
- if (base->th_notify_fd[1] > 0)
- evutil_make_socket_nonblocking(base->th_notify_fd[1]);
-
/* prepare an event that we can use for wakeup */
event_assign(&base->th_notify, base, base->th_notify_fd[0],
EV_READ|EV_PERSIST, cb, base);
@@ -2839,50 +3555,182 @@ evthread_make_base_notifiable(struct event_base *base)
base->th_notify.ev_flags |= EVLIST_INTERNAL;
event_priority_set(&base->th_notify, 0);
- return event_add(&base->th_notify, NULL);
+ return event_add_nolock_(&base->th_notify, NULL, 0);
}
-void
-event_base_dump_events(struct event_base *base, FILE *output)
+int
+event_base_foreach_event_nolock_(struct event_base *base,
+ event_base_foreach_event_cb fn, void *arg)
{
- struct event *e;
- int i;
- fprintf(output, "Inserted events:\n");
- TAILQ_FOREACH(e, &base->eventqueue, ev_next) {
- fprintf(output, " %p [fd "EV_SOCK_FMT"]%s%s%s%s%s\n",
- (void*)e, EV_SOCK_ARG(e->ev_fd),
- (e->ev_events&EV_READ)?" Read":"",
- (e->ev_events&EV_WRITE)?" Write":"",
- (e->ev_events&EV_SIGNAL)?" Signal":"",
- (e->ev_events&EV_TIMEOUT)?" Timeout":"",
- (e->ev_events&EV_PERSIST)?" Persist":"");
+ int r, i;
+ unsigned u;
+ struct event *ev;
+
+ /* Start out with all the EVLIST_INSERTED events. */
+ if ((r = evmap_foreach_event_(base, fn, arg)))
+ return r;
+ /* Okay, now we deal with those events that have timeouts and are in
+ * the min-heap. */
+ for (u = 0; u < base->timeheap.n; ++u) {
+ ev = base->timeheap.p[u];
+ if (ev->ev_flags & EVLIST_INSERTED) {
+ /* we already processed this one */
+ continue;
+ }
+ if ((r = fn(base, ev, arg)))
+ return r;
+ }
+
+ /* Now for the events in one of the timeout queues.
+ * the min-heap. */
+ for (i = 0; i < base->n_common_timeouts; ++i) {
+ struct common_timeout_list *ctl =
+ base->common_timeout_queues[i];
+ TAILQ_FOREACH(ev, &ctl->events,
+ ev_timeout_pos.ev_next_with_common_timeout) {
+ if (ev->ev_flags & EVLIST_INSERTED) {
+ /* we already processed this one */
+ continue;
+ }
+ if ((r = fn(base, ev, arg)))
+ return r;
+ }
}
+
+ /* Finally, we deal wit all the active events that we haven't touched
+ * yet. */
for (i = 0; i < base->nactivequeues; ++i) {
- if (TAILQ_EMPTY(&base->activequeues[i]))
- continue;
- fprintf(output, "Active events [priority %d]:\n", i);
- TAILQ_FOREACH(e, &base->eventqueue, ev_next) {
- fprintf(output, " %p [fd "EV_SOCK_FMT"]%s%s%s%s\n",
- (void*)e, EV_SOCK_ARG(e->ev_fd),
- (e->ev_res&EV_READ)?" Read active":"",
- (e->ev_res&EV_WRITE)?" Write active":"",
- (e->ev_res&EV_SIGNAL)?" Signal active":"",
- (e->ev_res&EV_TIMEOUT)?" Timeout active":"");
+ struct event_callback *evcb;
+ TAILQ_FOREACH(evcb, &base->activequeues[i], evcb_active_next) {
+ if ((evcb->evcb_flags & (EVLIST_INIT|EVLIST_INSERTED|EVLIST_TIMEOUT)) != EVLIST_INIT) {
+ /* This isn't an event (evlist_init clear), or
+ * we already processed it. (inserted or
+ * timeout set */
+ continue;
+ }
+ ev = event_callback_to_event(evcb);
+ if ((r = fn(base, ev, arg)))
+ return r;
}
}
+
+ return 0;
+}
+
+/* Helper for event_base_dump_events: called on each event in the event base;
+ * dumps only the inserted events. */
+static int
+dump_inserted_event_fn(const struct event_base *base, const struct event *e, void *arg)
+{
+ FILE *output = arg;
+ const char *gloss = (e->ev_events & EV_SIGNAL) ?
+ "sig" : "fd ";
+
+ if (! (e->ev_flags & (EVLIST_INSERTED|EVLIST_TIMEOUT)))
+ return 0;
+
+ fprintf(output, " %p [%s "EV_SOCK_FMT"]%s%s%s%s%s%s",
+ (void*)e, gloss, EV_SOCK_ARG(e->ev_fd),
+ (e->ev_events&EV_READ)?" Read":"",
+ (e->ev_events&EV_WRITE)?" Write":"",
+ (e->ev_events&EV_CLOSED)?" EOF":"",
+ (e->ev_events&EV_SIGNAL)?" Signal":"",
+ (e->ev_events&EV_PERSIST)?" Persist":"",
+ (e->ev_flags&EVLIST_INTERNAL)?" Internal":"");
+ if (e->ev_flags & EVLIST_TIMEOUT) {
+ struct timeval tv;
+ tv.tv_sec = e->ev_timeout.tv_sec;
+ tv.tv_usec = e->ev_timeout.tv_usec & MICROSECONDS_MASK;
+ evutil_timeradd(&tv, &base->tv_clock_diff, &tv);
+ fprintf(output, " Timeout=%ld.%06d",
+ (long)tv.tv_sec, (int)(tv.tv_usec & MICROSECONDS_MASK));
+ }
+ fputc('\n', output);
+
+ return 0;
+}
+
+/* Helper for event_base_dump_events: called on each event in the event base;
+ * dumps only the active events. */
+static int
+dump_active_event_fn(const struct event_base *base, const struct event *e, void *arg)
+{
+ FILE *output = arg;
+ const char *gloss = (e->ev_events & EV_SIGNAL) ?
+ "sig" : "fd ";
+
+ if (! (e->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)))
+ return 0;
+
+ fprintf(output, " %p [%s "EV_SOCK_FMT", priority=%d]%s%s%s%s%s active%s%s\n",
+ (void*)e, gloss, EV_SOCK_ARG(e->ev_fd), e->ev_pri,
+ (e->ev_res&EV_READ)?" Read":"",
+ (e->ev_res&EV_WRITE)?" Write":"",
+ (e->ev_res&EV_CLOSED)?" EOF":"",
+ (e->ev_res&EV_SIGNAL)?" Signal":"",
+ (e->ev_res&EV_TIMEOUT)?" Timeout":"",
+ (e->ev_flags&EVLIST_INTERNAL)?" [Internal]":"",
+ (e->ev_flags&EVLIST_ACTIVE_LATER)?" [NextTime]":"");
+
+ return 0;
+}
+
+int
+event_base_foreach_event(struct event_base *base,
+ event_base_foreach_event_cb fn, void *arg)
+{
+ int r;
+ if ((!fn) || (!base)) {
+ return -1;
+ }
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ r = event_base_foreach_event_nolock_(base, fn, arg);
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+ return r;
+}
+
+
+void
+event_base_dump_events(struct event_base *base, FILE *output)
+{
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ fprintf(output, "Inserted events:\n");
+ event_base_foreach_event_nolock_(base, dump_inserted_event_fn, output);
+
+ fprintf(output, "Active events:\n");
+ event_base_foreach_event_nolock_(base, dump_active_event_fn, output);
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+}
+
+void
+event_base_active_by_fd(struct event_base *base, evutil_socket_t fd, short events)
+{
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ evmap_io_active_(base, fd, events & (EV_READ|EV_WRITE|EV_CLOSED));
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
}
void
-event_base_add_virtual(struct event_base *base)
+event_base_active_by_signal(struct event_base *base, int sig)
+{
+ EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+ evmap_signal_active_(base, sig, 1);
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+}
+
+
+void
+event_base_add_virtual_(struct event_base *base)
{
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
base->virtual_event_count++;
+ MAX_EVENT_COUNT(base->virtual_event_count_max, base->virtual_event_count);
EVBASE_RELEASE_LOCK(base, th_base_lock);
}
void
-event_base_del_virtual(struct event_base *base)
+event_base_del_virtual_(struct event_base *base)
{
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
EVUTIL_ASSERT(base->virtual_event_count > 0);
@@ -2892,15 +3740,63 @@ event_base_del_virtual(struct event_base *base)
EVBASE_RELEASE_LOCK(base, th_base_lock);
}
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+static void
+event_free_debug_globals_locks(void)
+{
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_DEBUG_MODE
+ if (event_debug_map_lock_ != NULL) {
+ EVTHREAD_FREE_LOCK(event_debug_map_lock_, 0);
+ event_debug_map_lock_ = NULL;
+ }
+#endif /* EVENT__DISABLE_DEBUG_MODE */
+#endif /* EVENT__DISABLE_THREAD_SUPPORT */
+ return;
+}
+
+static void
+event_free_debug_globals(void)
+{
+ event_free_debug_globals_locks();
+}
+
+static void
+event_free_evsig_globals(void)
+{
+ evsig_free_globals_();
+}
+
+static void
+event_free_evutil_globals(void)
+{
+ evutil_free_globals_();
+}
+
+static void
+event_free_globals(void)
+{
+ event_free_debug_globals();
+ event_free_evsig_globals();
+ event_free_evutil_globals();
+}
+
+void
+libevent_global_shutdown(void)
+{
+ event_free_globals();
+}
+
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
int
event_global_setup_locks_(const int enable_locks)
{
-#ifndef _EVENT_DISABLE_DEBUG_MODE
- EVTHREAD_SETUP_GLOBAL_LOCK(_event_debug_map_lock, 0);
+#ifndef EVENT__DISABLE_DEBUG_MODE
+ EVTHREAD_SETUP_GLOBAL_LOCK(event_debug_map_lock_, 0);
#endif
if (evsig_global_setup_locks_(enable_locks) < 0)
return -1;
+ if (evutil_global_setup_locks_(enable_locks) < 0)
+ return -1;
if (evutil_secure_rng_global_setup_locks_(enable_locks) < 0)
return -1;
return 0;
@@ -2908,11 +3804,21 @@ event_global_setup_locks_(const int enable_locks)
#endif
void
-event_base_assert_ok(struct event_base *base)
+event_base_assert_ok_(struct event_base *base)
{
- int i;
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
- evmap_check_integrity(base);
+ event_base_assert_ok_nolock_(base);
+ EVBASE_RELEASE_LOCK(base, th_base_lock);
+}
+
+void
+event_base_assert_ok_nolock_(struct event_base *base)
+{
+ int i;
+ int count;
+
+ /* First do checks on the per-fd and per-signal lists */
+ evmap_check_integrity_(base);
/* Check the heap property */
for (i = 1; i < (int)base->timeheap.n; ++i) {
@@ -2920,7 +3826,7 @@ event_base_assert_ok(struct event_base *base)
struct event *ev, *p_ev;
ev = base->timeheap.p[i];
p_ev = base->timeheap.p[parent];
- EVUTIL_ASSERT(ev->ev_flags & EV_TIMEOUT);
+ EVUTIL_ASSERT(ev->ev_flags & EVLIST_TIMEOUT);
EVUTIL_ASSERT(evutil_timercmp(&p_ev->ev_timeout, &ev->ev_timeout, <=));
EVUTIL_ASSERT(ev->ev_timeout_pos.min_heap_idx == i);
}
@@ -2929,15 +3835,37 @@ event_base_assert_ok(struct event_base *base)
for (i = 0; i < base->n_common_timeouts; ++i) {
struct common_timeout_list *ctl = base->common_timeout_queues[i];
struct event *last=NULL, *ev;
+
+ EVUTIL_ASSERT_TAILQ_OK(&ctl->events, event, ev_timeout_pos.ev_next_with_common_timeout);
+
TAILQ_FOREACH(ev, &ctl->events, ev_timeout_pos.ev_next_with_common_timeout) {
if (last)
EVUTIL_ASSERT(evutil_timercmp(&last->ev_timeout, &ev->ev_timeout, <=));
- EVUTIL_ASSERT(ev->ev_flags & EV_TIMEOUT);
+ EVUTIL_ASSERT(ev->ev_flags & EVLIST_TIMEOUT);
EVUTIL_ASSERT(is_common_timeout(&ev->ev_timeout,base));
EVUTIL_ASSERT(COMMON_TIMEOUT_IDX(&ev->ev_timeout) == i);
last = ev;
}
}
- EVBASE_RELEASE_LOCK(base, th_base_lock);
+ /* Check the active queues. */
+ count = 0;
+ for (i = 0; i < base->nactivequeues; ++i) {
+ struct event_callback *evcb;
+ EVUTIL_ASSERT_TAILQ_OK(&base->activequeues[i], event_callback, evcb_active_next);
+ TAILQ_FOREACH(evcb, &base->activequeues[i], evcb_active_next) {
+ EVUTIL_ASSERT((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) == EVLIST_ACTIVE);
+ EVUTIL_ASSERT(evcb->evcb_pri == i);
+ ++count;
+ }
+ }
+
+ {
+ struct event_callback *evcb;
+ TAILQ_FOREACH(evcb, &base->active_later_queue, evcb_active_next) {
+ EVUTIL_ASSERT((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) == EVLIST_ACTIVE_LATER);
+ ++count;
+ }
+ }
+ EVUTIL_ASSERT(count == base->event_count_active);
}
diff --git a/event_iocp.c b/event_iocp.c
index 9648366d..a9902fbc 100644
--- a/event_iocp.c
+++ b/event_iocp.c
@@ -23,6 +23,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "evconfig-private.h"
#ifndef _WIN32_WINNT
/* Minimum required for InitializeCriticalSectionAndSpinCount */
@@ -45,7 +46,7 @@
#define NOTIFICATION_KEY ((ULONG_PTR)-1)
void
-event_overlapped_init(struct event_overlapped *o, iocp_callback cb)
+event_overlapped_init_(struct event_overlapped *o, iocp_callback cb)
{
memset(o, 0, sizeof(struct event_overlapped));
o->cb = cb;
@@ -60,9 +61,9 @@ handle_entry(OVERLAPPED *o, ULONG_PTR completion_key, DWORD nBytes, int ok)
}
static void
-loop(void *_port)
+loop(void *port_)
{
- struct event_iocp_port *port = _port;
+ struct event_iocp_port *port = port_;
long ms = port->ms;
HANDLE p = port->port;
@@ -98,7 +99,7 @@ loop(void *_port)
}
int
-event_iocp_port_associate(struct event_iocp_port *port, evutil_socket_t fd,
+event_iocp_port_associate_(struct event_iocp_port *port, evutil_socket_t fd,
ev_uintptr_t key)
{
HANDLE h;
@@ -164,7 +165,7 @@ init_extension_functions(struct win32_extension_fns *ext)
static struct win32_extension_fns the_extension_fns;
const struct win32_extension_fns *
-event_get_win32_extension_fns(void)
+event_get_win32_extension_fns_(void)
{
return &the_extension_fns;
}
@@ -172,7 +173,7 @@ event_get_win32_extension_fns(void)
#define N_CPUS_DEFAULT 2
struct event_iocp_port *
-event_iocp_port_launch(int n_cpus)
+event_iocp_port_launch_(int n_cpus)
{
struct event_iocp_port *port;
int i;
@@ -223,7 +224,7 @@ err:
}
static void
-_event_iocp_port_unlock_and_free(struct event_iocp_port *port)
+event_iocp_port_unlock_and_free_(struct event_iocp_port *port)
{
DeleteCriticalSection(&port->lock);
CloseHandle(port->port);
@@ -246,7 +247,7 @@ event_iocp_notify_all(struct event_iocp_port *port)
}
int
-event_iocp_shutdown(struct event_iocp_port *port, long waitMsec)
+event_iocp_shutdown_(struct event_iocp_port *port, long waitMsec)
{
DWORD ms = INFINITE;
int n;
@@ -264,7 +265,7 @@ event_iocp_shutdown(struct event_iocp_port *port, long waitMsec)
n = port->n_live_threads;
LeaveCriticalSection(&port->lock);
if (n == 0) {
- _event_iocp_port_unlock_and_free(port);
+ event_iocp_port_unlock_and_free_(port);
return 0;
} else {
return -1;
@@ -272,7 +273,7 @@ event_iocp_shutdown(struct event_iocp_port *port, long waitMsec)
}
int
-event_iocp_activate_overlapped(
+event_iocp_activate_overlapped_(
struct event_iocp_port *port, struct event_overlapped *o,
ev_uintptr_t key, ev_uint32_t n)
{
@@ -283,9 +284,9 @@ event_iocp_activate_overlapped(
}
struct event_iocp_port *
-event_base_get_iocp(struct event_base *base)
+event_base_get_iocp_(struct event_base *base)
{
-#ifdef WIN32
+#ifdef _WIN32
return base->iocp;
#else
return NULL;
diff --git a/event_rpcgen.py b/event_rpcgen.py
index dead34c2..c5375656 100755
--- a/event_rpcgen.py
+++ b/event_rpcgen.py
@@ -32,6 +32,12 @@ structdef = re.compile(r'^struct +[a-zA-Z_][a-zA-Z0-9_]* *{$')
headerdirect = []
cppdirect = []
+QUIETLY = 0
+
+def declare(s):
+ if not QUIETLY:
+ print s
+
def TranslateList(mylist, mydict):
return map(lambda x: x % mydict, mylist)
@@ -48,7 +54,7 @@ class Struct:
self._name = name
self._entries = []
self._tags = {}
- print >>sys.stderr, ' Created struct: %s' % name
+ declare(' Created struct: %s' % name)
def AddEntry(self, entry):
if self._tags.has_key(entry.Tag()):
@@ -58,7 +64,7 @@ class Struct:
self._tags[entry.Tag()], line_count))
self._entries.append(entry)
self._tags[entry.Tag()] = entry.Name()
- print >>sys.stderr, ' Added entry: %s' % entry.Name()
+ declare(' Added entry: %s' % entry.Name())
def Name(self):
return self._name
@@ -148,7 +154,7 @@ int evtag_unmarshal_%(name)s(struct evbuffer *, ev_uint32_t,
' */\n') % self._name
print >>file, \
- 'static struct %(name)s_access_ __%(name)s_base = {' % \
+ 'static struct %(name)s_access_ %(name)s_base__ = {' % \
{ 'name' : self._name }
for entry in self._entries:
self.PrintIndented(file, ' ', entry.CodeBase())
@@ -170,7 +176,7 @@ int evtag_unmarshal_%(name)s(struct evbuffer *, ev_uint32_t,
' event_warn("%%s: malloc", __func__);\n'
' return (NULL);\n'
' }\n'
- ' tmp->base = &__%(name)s_base;\n') % { 'name' : self._name }
+ ' tmp->base = &%(name)s_base__;\n') % { 'name' : self._name }
for entry in self._entries:
self.PrintIndented(file, ' ', entry.CodeInitialize('tmp'))
@@ -332,11 +338,11 @@ int evtag_unmarshal_%(name)s(struct evbuffer *, ev_uint32_t,
'evtag_marshal_%(name)s(struct evbuffer *evbuf, ev_uint32_t tag, '
'const struct %(name)s *msg)\n'
'{\n'
- ' struct evbuffer *_buf = evbuffer_new();\n'
- ' assert(_buf != NULL);\n'
- ' %(name)s_marshal(_buf, msg);\n'
- ' evtag_marshal_buffer(evbuf, tag, _buf);\n '
- ' evbuffer_free(_buf);\n'
+ ' struct evbuffer *buf_ = evbuffer_new();\n'
+ ' assert(buf_ != NULL);\n'
+ ' %(name)s_marshal(buf_, msg);\n'
+ ' evtag_marshal_buffer(evbuf, tag, buf_);\n '
+ ' evbuffer_free(buf_);\n'
'}\n' ) % { 'name' : self._name }
class Entry:
@@ -1532,7 +1538,7 @@ class CCodeGenerator:
# Use the complete provided path to the input file, with all
# non-identifier characters replaced with underscores, to
# reduce the chance of a collision between guard macros.
- return '_' + nonident.sub('_', name).upper() + '_'
+ return 'EVENT_RPCOUT_' + nonident.sub('_', name).upper() + '_'
def HeaderPreamble(self, name):
guard = self.GuardName(name)
@@ -1579,8 +1585,8 @@ class CCodeGenerator:
'#include <event2/event.h>\n'
'#include <event2/buffer.h>\n'
'#include <event2/tag.h>\n\n'
- '#ifdef _EVENT___func__\n'
- '#define __func__ _EVENT___func__\n'
+ '#ifdef EVENT____func__\n'
+ '#define __func__ EVENT____func__\n'
'#endif\n\n'
)
@@ -1638,6 +1644,11 @@ class CommandLine:
self.impl_file = None
self.factory = CCodeGenerator()
+ if len(argv) >= 2 and argv[1] == '--quiet':
+ global QUIETLY
+ QUIETLY = 1
+ del argv[1]
+
if len(argv) < 2 or len(argv) > 4:
raise Usage(argv[0])
@@ -1668,13 +1679,13 @@ class CommandLine:
impl_file = self.impl_file
factory = self.factory
- print >>sys.stderr, 'Reading \"%s\"' % filename
+ declare('Reading \"%s\"' % filename)
fp = open(filename, 'r')
entities = Parse(factory, fp)
fp.close()
- print >>sys.stderr, '... creating "%s"' % header_file
+ declare('... creating "%s"' % header_file)
header_fp = open(header_file, 'w')
print >>header_fp, factory.HeaderPreamble(filename)
@@ -1690,7 +1701,7 @@ class CommandLine:
print >>header_fp, factory.HeaderPostamble(filename)
header_fp.close()
- print >>sys.stderr, '... creating "%s"' % impl_file
+ declare('... creating "%s"' % impl_file)
impl_fp = open(impl_file, 'w')
print >>impl_fp, factory.BodyPreamble(filename, header_file)
for entry in entities:
diff --git a/event_tagging.c b/event_tagging.c
index eea4bfce..64b100b8 100644
--- a/event_tagging.c
+++ b/event_tagging.c
@@ -26,15 +26,16 @@
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_SYS_PARAM_H
+#ifdef EVENT__HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
-#ifdef WIN32
+#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
@@ -44,7 +45,7 @@
#endif
#include <sys/queue.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -52,10 +53,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <syslog.h>
#endif
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <limits.h>
@@ -211,7 +212,14 @@ decode_tag_internal(ev_uint32_t *ptag, struct evbuffer *evbuf, int dodrain)
while (count++ < len) {
ev_uint8_t lower = *data++;
- number |= (lower & 0x7f) << shift;
+ if (shift >= 28) {
+ /* Make sure it fits into 32 bits */
+ if (shift > 28)
+ return (-1);
+ if ((lower & 0x7f) > 15)
+ return (-1);
+ }
+ number |= (lower & (unsigned)0x7f) << shift;
shift += 7;
if (!(lower & 0x80)) {
diff --git a/evmap-internal.h b/evmap-internal.h
index 23b5a8a0..dfc81d50 100644
--- a/evmap-internal.h
+++ b/evmap-internal.h
@@ -23,8 +23,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVMAP_H_
-#define _EVMAP_H_
+#ifndef EVMAP_INTERNAL_H_INCLUDED_
+#define EVMAP_INTERNAL_H_INCLUDED_
/** @file evmap-internal.h
*
@@ -39,15 +39,15 @@ struct event;
/** Initialize an event_map for use.
*/
-void evmap_io_initmap(struct event_io_map* ctx);
-void evmap_signal_initmap(struct event_signal_map* ctx);
+void evmap_io_initmap_(struct event_io_map* ctx);
+void evmap_signal_initmap_(struct event_signal_map* ctx);
/** Remove all entries from an event_map.
@param ctx the map to clear.
*/
-void evmap_io_clear(struct event_io_map* ctx);
-void evmap_signal_clear(struct event_signal_map* ctx);
+void evmap_io_clear_(struct event_io_map* ctx);
+void evmap_signal_clear_(struct event_signal_map* ctx);
/** Add an IO event (some combination of EV_READ or EV_WRITE) to an
event_base's list of events on a given file descriptor, and tell the
@@ -59,7 +59,7 @@ void evmap_signal_clear(struct event_signal_map* ctx);
@param fd the file descriptor corresponding to ev.
@param ev the event to add.
*/
-int evmap_io_add(struct event_base *base, evutil_socket_t fd, struct event *ev);
+int evmap_io_add_(struct event_base *base, evutil_socket_t fd, struct event *ev);
/** Remove an IO event (some combination of EV_READ or EV_WRITE) to an
event_base's list of events on a given file descriptor, and tell the
underlying eventops about the fd if its state has changed.
@@ -68,25 +68,50 @@ int evmap_io_add(struct event_base *base, evutil_socket_t fd, struct event *ev);
@param fd the file descriptor corresponding to ev.
@param ev the event to remove.
*/
-int evmap_io_del(struct event_base *base, evutil_socket_t fd, struct event *ev);
+int evmap_io_del_(struct event_base *base, evutil_socket_t fd, struct event *ev);
/** Active the set of events waiting on an event_base for a given fd.
@param base the event_base to operate on.
@param fd the file descriptor that has become active.
@param events a bitmask of EV_READ|EV_WRITE|EV_ET.
*/
-void evmap_io_active(struct event_base *base, evutil_socket_t fd, short events);
+void evmap_io_active_(struct event_base *base, evutil_socket_t fd, short events);
/* These functions behave in the same way as evmap_io_*, except they work on
* signals rather than fds. signals use a linear map everywhere; fds use
* either a linear map or a hashtable. */
-int evmap_signal_add(struct event_base *base, int signum, struct event *ev);
-int evmap_signal_del(struct event_base *base, int signum, struct event *ev);
-void evmap_signal_active(struct event_base *base, evutil_socket_t signum, int ncalls);
+int evmap_signal_add_(struct event_base *base, int signum, struct event *ev);
+int evmap_signal_del_(struct event_base *base, int signum, struct event *ev);
+void evmap_signal_active_(struct event_base *base, evutil_socket_t signum, int ncalls);
-void *evmap_io_get_fdinfo(struct event_io_map *ctx, evutil_socket_t fd);
+/* Return the fdinfo object associated with a given fd. If the fd has no
+ * events associated with it, the result may be NULL.
+ */
+void *evmap_io_get_fdinfo_(struct event_io_map *ctx, evutil_socket_t fd);
+
+/* Helper for event_reinit(): Tell the backend to re-add every fd and signal
+ * for which we have a pending event.
+ */
+int evmap_reinit_(struct event_base *base);
+
+/* Helper for event_base_free(): Call event_del() on every pending fd and
+ * signal event.
+ */
+void evmap_delete_all_(struct event_base *base);
+
+/* Helper for event_base_assert_ok_(): Check referential integrity of the
+ * evmaps.
+ */
+void evmap_check_integrity_(struct event_base *base);
-void evmap_check_integrity(struct event_base *base);
+/* Helper: Call fn on every fd or signal event, passing as its arguments the
+ * provided event_base, the event, and arg. If fn returns 0, process the next
+ * event. If it returns any other value, return that value and process no
+ * more events.
+ */
+int evmap_foreach_event_(struct event_base *base,
+ event_base_foreach_event_cb fn,
+ void *arg);
-#endif /* _EVMAP_H_ */
+#endif /* EVMAP_INTERNAL_H_INCLUDED_ */
diff --git a/evmap.c b/evmap.c
index 69c1067d..3f76dd0a 100644
--- a/evmap.c
+++ b/evmap.c
@@ -24,21 +24,22 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
#endif
#include <sys/types.h>
-#if !defined(WIN32) && defined(_EVENT_HAVE_SYS_TIME_H)
+#if !defined(_WIN32) && defined(EVENT__HAVE_SYS_TIME_H)
#include <sys/time.h>
#endif
#include <sys/queue.h>
#include <stdio.h>
#include <stdlib.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <unistd.h>
#endif
#include <errno.h>
@@ -55,15 +56,16 @@
write on a given fd, and the number of each.
*/
struct evmap_io {
- struct event_list events;
+ struct event_dlist events;
ev_uint16_t nread;
ev_uint16_t nwrite;
+ ev_uint16_t nclose;
};
/* An entry for an evmap_signal list: notes all the events that want to know
when a signal triggers. */
struct evmap_signal {
- struct event_list events;
+ struct event_dlist events;
};
/* On some platforms, fds start at 0 and increment by 1 as they are
@@ -110,38 +112,38 @@ HT_GENERATE(event_io_map, event_map_entry, map_node, hashsocket, eqsocket,
#define GET_IO_SLOT(x, map, slot, type) \
do { \
- struct event_map_entry _key, *_ent; \
- _key.fd = slot; \
- _ent = HT_FIND(event_io_map, map, &_key); \
- (x) = _ent ? &_ent->ent.type : NULL; \
+ struct event_map_entry key_, *ent_; \
+ key_.fd = slot; \
+ ent_ = HT_FIND(event_io_map, map, &key_); \
+ (x) = ent_ ? &ent_->ent.type : NULL; \
} while (0);
#define GET_IO_SLOT_AND_CTOR(x, map, slot, type, ctor, fdinfo_len) \
do { \
- struct event_map_entry _key, *_ent; \
- _key.fd = slot; \
- _HT_FIND_OR_INSERT(event_io_map, map_node, hashsocket, map, \
- event_map_entry, &_key, ptr, \
+ struct event_map_entry key_, *ent_; \
+ key_.fd = slot; \
+ HT_FIND_OR_INSERT_(event_io_map, map_node, hashsocket, map, \
+ event_map_entry, &key_, ptr, \
{ \
- _ent = *ptr; \
+ ent_ = *ptr; \
}, \
{ \
- _ent = mm_calloc(1,sizeof(struct event_map_entry)+fdinfo_len); \
- if (EVUTIL_UNLIKELY(_ent == NULL)) \
+ ent_ = mm_calloc(1,sizeof(struct event_map_entry)+fdinfo_len); \
+ if (EVUTIL_UNLIKELY(ent_ == NULL)) \
return (-1); \
- _ent->fd = slot; \
- (ctor)(&_ent->ent.type); \
- _HT_FOI_INSERT(map_node, map, &_key, _ent, ptr) \
+ ent_->fd = slot; \
+ (ctor)(&ent_->ent.type); \
+ HT_FOI_INSERT_(map_node, map, &key_, ent_, ptr) \
}); \
- (x) = &_ent->ent.type; \
+ (x) = &ent_->ent.type; \
} while (0)
-void evmap_io_initmap(struct event_io_map *ctx)
+void evmap_io_initmap_(struct event_io_map *ctx)
{
HT_INIT(event_io_map, ctx);
}
-void evmap_io_clear(struct event_io_map *ctx)
+void evmap_io_clear_(struct event_io_map *ctx)
{
struct event_map_entry **ent, **next, *this;
for (ent = HT_START(event_io_map, ctx); ent; ent = next) {
@@ -183,14 +185,14 @@ void evmap_io_clear(struct event_io_map *ctx)
GET_SIGNAL_SLOT_AND_CTOR(x,map,slot,type,ctor,fdinfo_len)
#define FDINFO_OFFSET sizeof(struct evmap_io)
void
-evmap_io_initmap(struct event_io_map* ctx)
+evmap_io_initmap_(struct event_io_map* ctx)
{
- evmap_signal_initmap(ctx);
+ evmap_signal_initmap_(ctx);
}
void
-evmap_io_clear(struct event_io_map* ctx)
+evmap_io_clear_(struct event_io_map* ctx)
{
- evmap_signal_clear(ctx);
+ evmap_signal_clear_(ctx);
}
#endif
@@ -223,14 +225,14 @@ evmap_make_space(struct event_signal_map *map, int slot, int msize)
}
void
-evmap_signal_initmap(struct event_signal_map *ctx)
+evmap_signal_initmap_(struct event_signal_map *ctx)
{
ctx->nentries = 0;
ctx->entries = NULL;
}
void
-evmap_signal_clear(struct event_signal_map *ctx)
+evmap_signal_clear_(struct event_signal_map *ctx)
{
if (ctx->entries != NULL) {
int i;
@@ -251,21 +253,22 @@ evmap_signal_clear(struct event_signal_map *ctx)
static void
evmap_io_init(struct evmap_io *entry)
{
- TAILQ_INIT(&entry->events);
+ LIST_INIT(&entry->events);
entry->nread = 0;
entry->nwrite = 0;
+ entry->nclose = 0;
}
/* return -1 on error, 0 on success if nothing changed in the event backend,
* and 1 on success if something did. */
int
-evmap_io_add(struct event_base *base, evutil_socket_t fd, struct event *ev)
+evmap_io_add_(struct event_base *base, evutil_socket_t fd, struct event *ev)
{
const struct eventop *evsel = base->evsel;
struct event_io_map *io = &base->io;
struct evmap_io *ctx = NULL;
- int nread, nwrite, retval = 0;
+ int nread, nwrite, nclose, retval = 0;
short res = 0, old = 0;
struct event *old_ev;
@@ -285,11 +288,14 @@ evmap_io_add(struct event_base *base, evutil_socket_t fd, struct event *ev)
nread = ctx->nread;
nwrite = ctx->nwrite;
+ nclose = ctx->nclose;
if (nread)
old |= EV_READ;
if (nwrite)
old |= EV_WRITE;
+ if (nclose)
+ old |= EV_CLOSED;
if (ev->ev_events & EV_READ) {
if (++nread == 1)
@@ -299,13 +305,17 @@ evmap_io_add(struct event_base *base, evutil_socket_t fd, struct event *ev)
if (++nwrite == 1)
res |= EV_WRITE;
}
- if (EVUTIL_UNLIKELY(nread > 0xffff || nwrite > 0xffff)) {
+ if (ev->ev_events & EV_CLOSED) {
+ if (++nclose == 1)
+ res |= EV_CLOSED;
+ }
+ if (EVUTIL_UNLIKELY(nread > 0xffff || nwrite > 0xffff || nclose > 0xffff)) {
event_warnx("Too many events reading or writing on fd %d",
(int)fd);
return -1;
}
if (EVENT_DEBUG_MODE_IS_ON() &&
- (old_ev = TAILQ_FIRST(&ctx->events)) &&
+ (old_ev = LIST_FIRST(&ctx->events)) &&
(old_ev->ev_events&EV_ET) != (ev->ev_events&EV_ET)) {
event_warnx("Tried to mix edge-triggered and non-edge-triggered"
" events on fd %d", (int)fd);
@@ -325,7 +335,8 @@ evmap_io_add(struct event_base *base, evutil_socket_t fd, struct event *ev)
ctx->nread = (ev_uint16_t) nread;
ctx->nwrite = (ev_uint16_t) nwrite;
- TAILQ_INSERT_TAIL(&ctx->events, ev, ev_io_next);
+ ctx->nclose = (ev_uint16_t) nclose;
+ LIST_INSERT_HEAD(&ctx->events, ev, ev_io_next);
return (retval);
}
@@ -333,12 +344,12 @@ evmap_io_add(struct event_base *base, evutil_socket_t fd, struct event *ev)
/* return -1 on error, 0 on success if nothing changed in the event backend,
* and 1 on success if something did. */
int
-evmap_io_del(struct event_base *base, evutil_socket_t fd, struct event *ev)
+evmap_io_del_(struct event_base *base, evutil_socket_t fd, struct event *ev)
{
const struct eventop *evsel = base->evsel;
struct event_io_map *io = &base->io;
struct evmap_io *ctx;
- int nread, nwrite, retval = 0;
+ int nread, nwrite, nclose, retval = 0;
short res = 0, old = 0;
if (fd < 0)
@@ -355,11 +366,14 @@ evmap_io_del(struct event_base *base, evutil_socket_t fd, struct event *ev)
nread = ctx->nread;
nwrite = ctx->nwrite;
+ nclose = ctx->nclose;
if (nread)
old |= EV_READ;
if (nwrite)
old |= EV_WRITE;
+ if (nclose)
+ old |= EV_CLOSED;
if (ev->ev_events & EV_READ) {
if (--nread == 0)
@@ -371,37 +385,47 @@ evmap_io_del(struct event_base *base, evutil_socket_t fd, struct event *ev)
res |= EV_WRITE;
EVUTIL_ASSERT(nwrite >= 0);
}
+ if (ev->ev_events & EV_CLOSED) {
+ if (--nclose == 0)
+ res |= EV_CLOSED;
+ EVUTIL_ASSERT(nclose >= 0);
+ }
if (res) {
void *extra = ((char*)ctx) + sizeof(struct evmap_io);
- if (evsel->del(base, ev->ev_fd, old, res, extra) == -1)
- return (-1);
- retval = 1;
+ if (evsel->del(base, ev->ev_fd, old, res, extra) == -1) {
+ retval = -1;
+ } else {
+ retval = 1;
+ }
}
ctx->nread = nread;
ctx->nwrite = nwrite;
- TAILQ_REMOVE(&ctx->events, ev, ev_io_next);
+ ctx->nclose = nclose;
+ LIST_REMOVE(ev, ev_io_next);
return (retval);
}
void
-evmap_io_active(struct event_base *base, evutil_socket_t fd, short events)
+evmap_io_active_(struct event_base *base, evutil_socket_t fd, short events)
{
struct event_io_map *io = &base->io;
struct evmap_io *ctx;
struct event *ev;
#ifndef EVMAP_USE_HT
- EVUTIL_ASSERT(fd < io->nentries);
+ if (fd < 0 || fd >= io->nentries)
+ return;
#endif
GET_IO_SLOT(ctx, io, fd, evmap_io);
- EVUTIL_ASSERT(ctx);
- TAILQ_FOREACH(ev, &ctx->events, ev_io_next) {
+ if (NULL == ctx)
+ return;
+ LIST_FOREACH(ev, &ctx->events, ev_io_next) {
if (ev->ev_events & events)
- event_active_nolock(ev, ev->ev_events & events, 1);
+ event_active_nolock_(ev, ev->ev_events & events, 1);
}
}
@@ -410,12 +434,12 @@ evmap_io_active(struct event_base *base, evutil_socket_t fd, short events)
static void
evmap_signal_init(struct evmap_signal *entry)
{
- TAILQ_INIT(&entry->events);
+ LIST_INIT(&entry->events);
}
int
-evmap_signal_add(struct event_base *base, int sig, struct event *ev)
+evmap_signal_add_(struct event_base *base, int sig, struct event *ev)
{
const struct eventop *evsel = base->evsigsel;
struct event_signal_map *map = &base->sigmap;
@@ -429,19 +453,19 @@ evmap_signal_add(struct event_base *base, int sig, struct event *ev)
GET_SIGNAL_SLOT_AND_CTOR(ctx, map, sig, evmap_signal, evmap_signal_init,
base->evsigsel->fdinfo_len);
- if (TAILQ_EMPTY(&ctx->events)) {
+ if (LIST_EMPTY(&ctx->events)) {
if (evsel->add(base, ev->ev_fd, 0, EV_SIGNAL, NULL)
== -1)
return (-1);
}
- TAILQ_INSERT_TAIL(&ctx->events, ev, ev_signal_next);
+ LIST_INSERT_HEAD(&ctx->events, ev, ev_signal_next);
return (1);
}
int
-evmap_signal_del(struct event_base *base, int sig, struct event *ev)
+evmap_signal_del_(struct event_base *base, int sig, struct event *ev)
{
const struct eventop *evsel = base->evsigsel;
struct event_signal_map *map = &base->sigmap;
@@ -452,32 +476,35 @@ evmap_signal_del(struct event_base *base, int sig, struct event *ev)
GET_SIGNAL_SLOT(ctx, map, sig, evmap_signal);
- if (TAILQ_FIRST(&ctx->events) == TAILQ_LAST(&ctx->events, event_list)) {
+ LIST_REMOVE(ev, ev_signal_next);
+
+ if (LIST_FIRST(&ctx->events) == NULL) {
if (evsel->del(base, ev->ev_fd, 0, EV_SIGNAL, NULL) == -1)
return (-1);
}
- TAILQ_REMOVE(&ctx->events, ev, ev_signal_next);
-
return (1);
}
void
-evmap_signal_active(struct event_base *base, evutil_socket_t sig, int ncalls)
+evmap_signal_active_(struct event_base *base, evutil_socket_t sig, int ncalls)
{
struct event_signal_map *map = &base->sigmap;
struct evmap_signal *ctx;
struct event *ev;
- EVUTIL_ASSERT(sig < map->nentries);
+ if (sig < 0 || sig >= map->nentries)
+ return;
GET_SIGNAL_SLOT(ctx, map, sig, evmap_signal);
- TAILQ_FOREACH(ev, &ctx->events, ev_signal_next)
- event_active_nolock(ev, EV_SIGNAL, ncalls);
+ if (!ctx)
+ return;
+ LIST_FOREACH(ev, &ctx->events, ev_signal_next)
+ event_active_nolock_(ev, EV_SIGNAL, ncalls);
}
void *
-evmap_io_get_fdinfo(struct event_io_map *map, evutil_socket_t fd)
+evmap_io_get_fdinfo_(struct event_io_map *map, evutil_socket_t fd)
{
struct evmap_io *ctx;
GET_IO_SLOT(ctx, map, fd, evmap_io);
@@ -487,6 +514,177 @@ evmap_io_get_fdinfo(struct event_io_map *map, evutil_socket_t fd)
return NULL;
}
+/* Callback type for evmap_io_foreach_fd */
+typedef int (*evmap_io_foreach_fd_cb)(
+ struct event_base *, evutil_socket_t, struct evmap_io *, void *);
+
+/* Multipurpose helper function: Iterate over every file descriptor event_base
+ * for which we could have EV_READ or EV_WRITE events. For each such fd, call
+ * fn(base, signum, evmap_io, arg), where fn is the user-provided
+ * function, base is the event_base, signum is the signal number, evmap_io
+ * is an evmap_io structure containing a list of events pending on the
+ * file descriptor, and arg is the user-supplied argument.
+ *
+ * If fn returns 0, continue on to the next signal. Otherwise, return the same
+ * value that fn returned.
+ *
+ * Note that there is no guarantee that the file descriptors will be processed
+ * in any particular order.
+ */
+static int
+evmap_io_foreach_fd(struct event_base *base,
+ evmap_io_foreach_fd_cb fn,
+ void *arg)
+{
+ evutil_socket_t fd;
+ struct event_io_map *iomap = &base->io;
+ int r = 0;
+#ifdef EVMAP_USE_HT
+ struct event_map_entry **mapent;
+ HT_FOREACH(mapent, event_io_map, iomap) {
+ struct evmap_io *ctx = &(*mapent)->ent.evmap_io;
+ fd = (*mapent)->fd;
+#else
+ for (fd = 0; fd < iomap->nentries; ++fd) {
+ struct evmap_io *ctx = iomap->entries[fd];
+ if (!ctx)
+ continue;
+#endif
+ if ((r = fn(base, fd, ctx, arg)))
+ break;
+ }
+ return r;
+}
+
+/* Callback type for evmap_signal_foreach_signal */
+typedef int (*evmap_signal_foreach_signal_cb)(
+ struct event_base *, int, struct evmap_signal *, void *);
+
+/* Multipurpose helper function: Iterate over every signal number in the
+ * event_base for which we could have signal events. For each such signal,
+ * call fn(base, signum, evmap_signal, arg), where fn is the user-provided
+ * function, base is the event_base, signum is the signal number, evmap_signal
+ * is an evmap_signal structure containing a list of events pending on the
+ * signal, and arg is the user-supplied argument.
+ *
+ * If fn returns 0, continue on to the next signal. Otherwise, return the same
+ * value that fn returned.
+ */
+static int
+evmap_signal_foreach_signal(struct event_base *base,
+ evmap_signal_foreach_signal_cb fn,
+ void *arg)
+{
+ struct event_signal_map *sigmap = &base->sigmap;
+ int r = 0;
+ int signum;
+
+ for (signum = 0; signum < sigmap->nentries; ++signum) {
+ struct evmap_signal *ctx = sigmap->entries[signum];
+ if (!ctx)
+ continue;
+ if ((r = fn(base, signum, ctx, arg)))
+ break;
+ }
+ return r;
+}
+
+/* Helper for evmap_reinit_: tell the backend to add every fd for which we have
+ * pending events, with the appropriate combination of EV_READ, EV_WRITE, and
+ * EV_ET. */
+static int
+evmap_io_reinit_iter_fn(struct event_base *base, evutil_socket_t fd,
+ struct evmap_io *ctx, void *arg)
+{
+ const struct eventop *evsel = base->evsel;
+ void *extra;
+ int *result = arg;
+ short events = 0;
+ struct event *ev;
+ EVUTIL_ASSERT(ctx);
+
+ extra = ((char*)ctx) + sizeof(struct evmap_io);
+ if (ctx->nread)
+ events |= EV_READ;
+ if (ctx->nwrite)
+ events |= EV_WRITE;
+ if (ctx->nclose)
+ events |= EV_CLOSED;
+ if (evsel->fdinfo_len)
+ memset(extra, 0, evsel->fdinfo_len);
+ if (events &&
+ (ev = LIST_FIRST(&ctx->events)) &&
+ (ev->ev_events & EV_ET))
+ events |= EV_ET;
+ if (evsel->add(base, fd, 0, events, extra) == -1)
+ *result = -1;
+
+ return 0;
+}
+
+/* Helper for evmap_reinit_: tell the backend to add every signal for which we
+ * have pending events. */
+static int
+evmap_signal_reinit_iter_fn(struct event_base *base,
+ int signum, struct evmap_signal *ctx, void *arg)
+{
+ const struct eventop *evsel = base->evsigsel;
+ int *result = arg;
+
+ if (!LIST_EMPTY(&ctx->events)) {
+ if (evsel->add(base, signum, 0, EV_SIGNAL, NULL) == -1)
+ *result = -1;
+ }
+ return 0;
+}
+
+int
+evmap_reinit_(struct event_base *base)
+{
+ int result = 0;
+
+ evmap_io_foreach_fd(base, evmap_io_reinit_iter_fn, &result);
+ if (result < 0)
+ return -1;
+ evmap_signal_foreach_signal(base, evmap_signal_reinit_iter_fn, &result);
+ if (result < 0)
+ return -1;
+ return 0;
+}
+
+/* Helper for evmap_delete_all_: delete every event in an event_dlist. */
+static int
+delete_all_in_dlist(struct event_dlist *dlist)
+{
+ struct event *ev;
+ while ((ev = LIST_FIRST(dlist)))
+ event_del(ev);
+ return 0;
+}
+
+/* Helper for evmap_delete_all_: delete every event pending on an fd. */
+static int
+evmap_io_delete_all_iter_fn(struct event_base *base, evutil_socket_t fd,
+ struct evmap_io *io_info, void *arg)
+{
+ return delete_all_in_dlist(&io_info->events);
+}
+
+/* Helper for evmap_delete_all_: delete every event pending on a signal. */
+static int
+evmap_signal_delete_all_iter_fn(struct event_base *base, int signum,
+ struct evmap_signal *sig_info, void *arg)
+{
+ return delete_all_in_dlist(&sig_info->events);
+}
+
+void
+evmap_delete_all_(struct event_base *base)
+{
+ evmap_signal_foreach_signal(base, evmap_signal_delete_all_iter_fn, NULL);
+ evmap_io_foreach_fd(base, evmap_io_delete_all_iter_fn, NULL);
+}
+
/** Per-fd structure for use with changelists. It keeps track, for each fd or
* signal using the changelist, of where its entry in the changelist is.
*/
@@ -496,7 +694,7 @@ struct event_changelist_fdinfo {
};
void
-event_changelist_init(struct event_changelist *changelist)
+event_changelist_init_(struct event_changelist *changelist)
{
changelist->changes = NULL;
changelist->changes_size = 0;
@@ -521,10 +719,26 @@ event_change_get_fdinfo(struct event_base *base,
return (void*)ptr;
}
-#ifdef DEBUG_CHANGELIST
+/** Callback helper for event_changelist_assert_ok */
+static int
+event_changelist_assert_ok_foreach_iter_fn(
+ struct event_base *base,
+ evutil_socket_t fd, struct evmap_io *io, void *arg)
+{
+ struct event_changelist *changelist = &base->changelist;
+ struct event_changelist_fdinfo *f;
+ f = (void*)
+ ( ((char*)io) + sizeof(struct evmap_io) );
+ if (f->idxplus1) {
+ struct event_change *c = &changelist->changes[f->idxplus1 - 1];
+ EVUTIL_ASSERT(c->fd == fd);
+ }
+ return 0;
+}
+
/** Make sure that the changelist is consistent with the evmap structures. */
static void
-event_changelist_check(struct event_base *base)
+event_changelist_assert_ok(struct event_base *base)
{
int i;
struct event_changelist *changelist = &base->changelist;
@@ -539,25 +753,19 @@ event_changelist_check(struct event_base *base)
EVUTIL_ASSERT(f->idxplus1 == i + 1);
}
- for (i = 0; i < base->io.nentries; ++i) {
- struct evmap_io *io = base->io.entries[i];
- struct event_changelist_fdinfo *f;
- if (!io)
- continue;
- f = (void*)
- ( ((char*)io) + sizeof(struct evmap_io) );
- if (f->idxplus1) {
- struct event_change *c = &changelist->changes[f->idxplus1 - 1];
- EVUTIL_ASSERT(c->fd == i);
- }
- }
+ evmap_io_foreach_fd(base,
+ event_changelist_assert_ok_foreach_iter_fn,
+ NULL);
}
+
+#ifdef DEBUG_CHANGELIST
+#define event_changelist_check(base) event_changelist_assert_ok((base))
#else
#define event_changelist_check(base) ((void)0)
#endif
void
-event_changelist_remove_all(struct event_changelist *changelist,
+event_changelist_remove_all_(struct event_changelist *changelist,
struct event_base *base)
{
int i;
@@ -578,11 +786,11 @@ event_changelist_remove_all(struct event_changelist *changelist,
}
void
-event_changelist_freemem(struct event_changelist *changelist)
+event_changelist_freemem_(struct event_changelist *changelist)
{
if (changelist->changes)
mm_free(changelist->changes);
- event_changelist_init(changelist); /* zero it all out. */
+ event_changelist_init_(changelist); /* zero it all out. */
}
/** Increase the size of 'changelist' to hold more changes. */
@@ -644,7 +852,7 @@ event_changelist_get_or_construct(struct event_changelist *changelist,
}
int
-event_changelist_add(struct event_base *base, evutil_socket_t fd, short old, short events,
+event_changelist_add_(struct event_base *base, evutil_socket_t fd, short old, short events,
void *p)
{
struct event_changelist *changelist = &base->changelist;
@@ -669,13 +877,17 @@ event_changelist_add(struct event_base *base, evutil_socket_t fd, short old, sho
change->write_change = EV_CHANGE_ADD |
(events & (EV_ET|EV_PERSIST|EV_SIGNAL));
}
+ if (events & EV_CLOSED) {
+ change->close_change = EV_CHANGE_ADD |
+ (events & (EV_ET|EV_PERSIST|EV_SIGNAL));
+ }
event_changelist_check(base);
return (0);
}
int
-event_changelist_del(struct event_base *base, evutil_socket_t fd, short old, short events,
+event_changelist_del_(struct event_base *base, evutil_socket_t fd, short old, short events,
void *p)
{
struct event_changelist *changelist = &base->changelist;
@@ -688,14 +900,11 @@ event_changelist_del(struct event_base *base, evutil_socket_t fd, short old, sho
if (!change)
return -1;
- /* A delete removes any previous add, rather than replacing it:
- on those platforms where "add, delete, dispatch" is not the same
- as "no-op, dispatch", we want the no-op behavior.
-
- As well as checking the current operation we should also check
- the original set of events to make sure were not ignoring
- the case where the add operation is present on an event that
- was already set.
+ /* A delete on an event set that doesn't contain the event to be
+ deleted produces a no-op. This effectively emoves any previous
+ uncommitted add, rather than replacing it: on those platforms where
+ "add, delete, dispatch" is not the same as "no-op, dispatch", we
+ want the no-op behavior.
If we have a no-op item, we could remove it it from the list
entirely, but really there's not much point: skipping the no-op
@@ -707,93 +916,140 @@ event_changelist_del(struct event_base *base, evutil_socket_t fd, short old, sho
*/
if (events & (EV_READ|EV_SIGNAL)) {
- if (!(change->old_events & (EV_READ | EV_SIGNAL)) &&
- (change->read_change & EV_CHANGE_ADD))
+ if (!(change->old_events & (EV_READ | EV_SIGNAL)))
change->read_change = 0;
else
change->read_change = EV_CHANGE_DEL;
}
if (events & EV_WRITE) {
- if (!(change->old_events & EV_WRITE) &&
- (change->write_change & EV_CHANGE_ADD))
+ if (!(change->old_events & EV_WRITE))
change->write_change = 0;
else
change->write_change = EV_CHANGE_DEL;
}
+ if (events & EV_CLOSED) {
+ if (!(change->old_events & EV_CLOSED))
+ change->close_change = 0;
+ else
+ change->close_change = EV_CHANGE_DEL;
+ }
event_changelist_check(base);
return (0);
}
-void
-evmap_check_integrity(struct event_base *base)
+/* Helper for evmap_check_integrity_: verify that all of the events pending on
+ * given fd are set up correctly, and that the nread and nwrite counts on that
+ * fd are correct. */
+static int
+evmap_io_check_integrity_fn(struct event_base *base, evutil_socket_t fd,
+ struct evmap_io *io_info, void *arg)
{
-#define EVLIST_X_SIGFOUND 0x1000
-#define EVLIST_X_IOFOUND 0x2000
-
- evutil_socket_t i;
struct event *ev;
- struct event_io_map *io = &base->io;
- struct event_signal_map *sigmap = &base->sigmap;
-#ifdef EVMAP_USE_HT
- struct event_map_entry **mapent;
-#endif
- int nsignals, ntimers, nio;
- nsignals = ntimers = nio = 0;
+ int n_read = 0, n_write = 0, n_close = 0;
+
+ /* First, make sure the list itself isn't corrupt. Otherwise,
+ * running LIST_FOREACH could be an exciting adventure. */
+ EVUTIL_ASSERT_LIST_OK(&io_info->events, event, ev_io_next);
- TAILQ_FOREACH(ev, &base->eventqueue, ev_next) {
+ LIST_FOREACH(ev, &io_info->events, ev_io_next) {
EVUTIL_ASSERT(ev->ev_flags & EVLIST_INSERTED);
- EVUTIL_ASSERT(ev->ev_flags & EVLIST_INIT);
- ev->ev_flags &= ~(EVLIST_X_SIGFOUND|EVLIST_X_IOFOUND);
+ EVUTIL_ASSERT(ev->ev_fd == fd);
+ EVUTIL_ASSERT(!(ev->ev_events & EV_SIGNAL));
+ EVUTIL_ASSERT((ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED)));
+ if (ev->ev_events & EV_READ)
+ ++n_read;
+ if (ev->ev_events & EV_WRITE)
+ ++n_write;
+ if (ev->ev_events & EV_CLOSED)
+ ++n_close;
}
-#ifdef EVMAP_USE_HT
- HT_FOREACH(mapent, event_io_map, io) {
- struct evmap_io *ctx = &(*mapent)->ent.evmap_io;
- i = (*mapent)->fd;
-#else
- for (i = 0; i < io->nentries; ++i) {
- struct evmap_io *ctx = io->entries[i];
+ EVUTIL_ASSERT(n_read == io_info->nread);
+ EVUTIL_ASSERT(n_write == io_info->nwrite);
+ EVUTIL_ASSERT(n_close == io_info->nclose);
- if (!ctx)
- continue;
-#endif
+ return 0;
+}
- TAILQ_FOREACH(ev, &ctx->events, ev_io_next) {
- EVUTIL_ASSERT(!(ev->ev_flags & EVLIST_X_IOFOUND));
- EVUTIL_ASSERT(ev->ev_fd == i);
- ev->ev_flags |= EVLIST_X_IOFOUND;
- nio++;
- }
+/* Helper for evmap_check_integrity_: verify that all of the events pending
+ * on given signal are set up correctly. */
+static int
+evmap_signal_check_integrity_fn(struct event_base *base,
+ int signum, struct evmap_signal *sig_info, void *arg)
+{
+ struct event *ev;
+ /* First, make sure the list itself isn't corrupt. */
+ EVUTIL_ASSERT_LIST_OK(&sig_info->events, event, ev_signal_next);
+
+ LIST_FOREACH(ev, &sig_info->events, ev_io_next) {
+ EVUTIL_ASSERT(ev->ev_flags & EVLIST_INSERTED);
+ EVUTIL_ASSERT(ev->ev_fd == signum);
+ EVUTIL_ASSERT((ev->ev_events & EV_SIGNAL));
+ EVUTIL_ASSERT(!(ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED)));
}
+ return 0;
+}
- for (i = 0; i < sigmap->nentries; ++i) {
- struct evmap_signal *ctx = sigmap->entries[i];
- if (!ctx)
- continue;
+void
+evmap_check_integrity_(struct event_base *base)
+{
+ evmap_io_foreach_fd(base, evmap_io_check_integrity_fn, NULL);
+ evmap_signal_foreach_signal(base, evmap_signal_check_integrity_fn, NULL);
- TAILQ_FOREACH(ev, &ctx->events, ev_signal_next) {
- EVUTIL_ASSERT(!(ev->ev_flags & EVLIST_X_SIGFOUND));
- EVUTIL_ASSERT(ev->ev_fd == i);
- ev->ev_flags |= EVLIST_X_SIGFOUND;
- nsignals++;
- }
+ if (base->evsel->add == event_changelist_add_)
+ event_changelist_assert_ok(base);
+}
+
+/* Helper type for evmap_foreach_event_: Bundles a function to call on every
+ * event, and the user-provided void* to use as its third argument. */
+struct evmap_foreach_event_helper {
+ event_base_foreach_event_cb fn;
+ void *arg;
+};
+
+/* Helper for evmap_foreach_event_: calls a provided function on every event
+ * pending on a given fd. */
+static int
+evmap_io_foreach_event_fn(struct event_base *base, evutil_socket_t fd,
+ struct evmap_io *io_info, void *arg)
+{
+ struct evmap_foreach_event_helper *h = arg;
+ struct event *ev;
+ int r;
+ LIST_FOREACH(ev, &io_info->events, ev_io_next) {
+ if ((r = h->fn(base, ev, h->arg)))
+ return r;
}
+ return 0;
+}
- TAILQ_FOREACH(ev, &base->eventqueue, ev_next) {
- if (ev->ev_events & (EV_READ|EV_WRITE)) {
- EVUTIL_ASSERT(ev->ev_flags & EVLIST_X_IOFOUND);
- --nio;
- }
- if (ev->ev_events & EV_SIGNAL) {
- EVUTIL_ASSERT(ev->ev_flags & EVLIST_X_SIGFOUND);
- --nsignals;
- }
+/* Helper for evmap_foreach_event_: calls a provided function on every event
+ * pending on a given signal. */
+static int
+evmap_signal_foreach_event_fn(struct event_base *base, int signum,
+ struct evmap_signal *sig_info, void *arg)
+{
+ struct event *ev;
+ struct evmap_foreach_event_helper *h = arg;
+ int r;
+ LIST_FOREACH(ev, &sig_info->events, ev_signal_next) {
+ if ((r = h->fn(base, ev, h->arg)))
+ return r;
}
+ return 0;
+}
- EVUTIL_ASSERT(nio == 0);
- EVUTIL_ASSERT(nsignals == 0);
- /* There is no "EVUTIL_ASSERT(ntimers == 0)": eventqueue is only for
- * pending signals and io events.
- */
+int
+evmap_foreach_event_(struct event_base *base,
+ event_base_foreach_event_cb fn, void *arg)
+{
+ struct evmap_foreach_event_helper h;
+ int r;
+ h.fn = fn;
+ h.arg = arg;
+ if ((r = evmap_io_foreach_fd(base, evmap_io_foreach_event_fn, &h)))
+ return r;
+ return evmap_signal_foreach_signal(base, evmap_signal_foreach_event_fn, &h);
}
+
diff --git a/evport.c b/evport.c
index 0d714d81..a014386b 100644
--- a/evport.c
+++ b/evport.c
@@ -54,6 +54,9 @@
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
+
+#ifdef EVENT__HAVE_EVENT_PORTS
#include <sys/time.h>
#include <sys/queue.h>
@@ -75,20 +78,8 @@
#include "evsignal-internal.h"
#include "evmap-internal.h"
-/*
- * Default value for ed_nevents, which is the maximum file descriptor number we
- * can handle. If an event comes in for a file descriptor F > nevents, we will
- * grow the array of file descriptors, doubling its size.
- */
-#define DEFAULT_NFDS 16
-
-
-/*
- * EVENTS_PER_GETN is the maximum number of events to retrieve from port_getn on
- * any particular call. You can speed things up by increasing this, but it will
- * (obviously) require more memory.
- */
-#define EVENTS_PER_GETN 8
+#define INITIAL_EVENTS_PER_GETN 8
+#define MAX_EVENTS_PER_GETN 4096
/*
* Per-file-descriptor information about what events we're subscribed to. These
@@ -96,7 +87,12 @@
*/
struct fd_info {
- short fdi_what; /* combinations of EV_READ and EV_WRITE */
+ /* combinations of EV_READ and EV_WRITE */
+ short fdi_what;
+ /* Index of this fd within ed_pending, plus 1. Zero if this fd is
+ * not in ed_pending. (The +1 is a hack so that memset(0) will set
+ * it to a nil index. */
+ int pending_idx_plus_1;
};
#define FDI_HAS_READ(fdi) ((fdi)->fdi_what & EV_READ)
@@ -107,10 +103,15 @@ struct fd_info {
struct evport_data {
int ed_port; /* event port for system events */
- int ed_nevents; /* number of allocated fdi's */
- struct fd_info *ed_fds; /* allocated fdi table */
+ /* How many elements of ed_pending should we look at? */
+ int ed_npending;
+ /* How many elements are allocated in ed_pending and pevtlist? */
+ int ed_maxevents;
/* fdi's that we need to reassoc */
- int ed_pending[EVENTS_PER_GETN]; /* fd's with pending events */
+ int *ed_pending;
+ /* storage space for incoming events. */
+ port_event_t *ed_pevtlist;
+
};
static void* evport_init(struct event_base *);
@@ -118,6 +119,7 @@ static int evport_add(struct event_base *, int fd, short old, short events, void
static int evport_del(struct event_base *, int fd, short old, short events, void *);
static int evport_dispatch(struct event_base *, struct timeval *);
static void evport_dealloc(struct event_base *);
+static int grow(struct evport_data *, int min_events);
const struct eventop evportops = {
"evport",
@@ -128,7 +130,7 @@ const struct eventop evportops = {
evport_dealloc,
1, /* need reinit */
0, /* features */
- 0, /* fdinfo length */
+ sizeof(struct fd_info), /* fdinfo length */
};
/*
@@ -139,7 +141,6 @@ static void*
evport_init(struct event_base *base)
{
struct evport_data *evpd;
- int i;
if (!(evpd = mm_calloc(1, sizeof(struct evport_data))))
return (NULL);
@@ -149,24 +150,47 @@ evport_init(struct event_base *base)
return (NULL);
}
- /*
- * Initialize file descriptor structure
- */
- evpd->ed_fds = mm_calloc(DEFAULT_NFDS, sizeof(struct fd_info));
- if (evpd->ed_fds == NULL) {
+ if (grow(evpd, INITIAL_EVENTS_PER_GETN) < 0) {
close(evpd->ed_port);
mm_free(evpd);
- return (NULL);
+ return NULL;
}
- evpd->ed_nevents = DEFAULT_NFDS;
- for (i = 0; i < EVENTS_PER_GETN; i++)
- evpd->ed_pending[i] = -1;
+
+ evpd->ed_npending = 0;
- evsig_init(base);
+ evsig_init_(base);
return (evpd);
}
+static int
+grow(struct evport_data *data, int min_events)
+{
+ int newsize;
+ int *new_pending;
+ port_event_t *new_pevtlist;
+ if (data->ed_maxevents) {
+ newsize = data->ed_maxevents;
+ do {
+ newsize *= 2;
+ } while (newsize < min_events);
+ } else {
+ newsize = min_events;
+ }
+
+ new_pending = mm_realloc(data->ed_pending, sizeof(int)*newsize);
+ if (new_pending == NULL)
+ return -1;
+ data->ed_pending = new_pending;
+ new_pevtlist = mm_realloc(data->ed_pevtlist, sizeof(port_event_t)*newsize);
+ if (new_pevtlist == NULL)
+ return -1;
+ data->ed_pevtlist = new_pevtlist;
+
+ data->ed_maxevents = newsize;
+ return 0;
+}
+
#ifdef CHECK_INVARIANTS
/*
* Checks some basic properties about the evport_data structure. Because it
@@ -178,9 +202,7 @@ static void
check_evportop(struct evport_data *evpd)
{
EVUTIL_ASSERT(evpd);
- EVUTIL_ASSERT(evpd->ed_nevents > 0);
EVUTIL_ASSERT(evpd->ed_port > 0);
- EVUTIL_ASSERT(evpd->ed_fds > 0);
}
/*
@@ -196,7 +218,6 @@ check_event(port_event_t* pevt)
* PORT_SOURCE_FD.
*/
EVUTIL_ASSERT(pevt->portev_source == PORT_SOURCE_FD);
- EVUTIL_ASSERT(pevt->portev_user == NULL);
}
#else
@@ -205,33 +226,6 @@ check_event(port_event_t* pevt)
#endif /* CHECK_INVARIANTS */
/*
- * Doubles the size of the allocated file descriptor array.
- */
-static int
-grow(struct evport_data *epdp, int factor)
-{
- struct fd_info *tmp;
- int oldsize = epdp->ed_nevents;
- int newsize = factor * oldsize;
- EVUTIL_ASSERT(factor > 1);
-
- check_evportop(epdp);
-
- tmp = mm_realloc(epdp->ed_fds, sizeof(struct fd_info) * newsize);
- if (NULL == tmp)
- return -1;
- epdp->ed_fds = tmp;
- memset((char*) (epdp->ed_fds + oldsize), 0,
- (newsize - oldsize)*sizeof(struct fd_info));
- epdp->ed_nevents = newsize;
-
- check_evportop(epdp);
-
- return 0;
-}
-
-
-/*
* (Re)associates the given file descriptor with the event port. The OS events
* are specified (implicitly) from the fd_info struct.
*/
@@ -242,7 +236,7 @@ reassociate(struct evport_data *epdp, struct fd_info *fdip, int fd)
if (sysevents != 0) {
if (port_associate(epdp->ed_port, PORT_SOURCE_FD,
- fd, sysevents, NULL) == -1) {
+ fd, sysevents, fdip) == -1) {
event_warn("port_associate");
return (-1);
}
@@ -263,12 +257,12 @@ evport_dispatch(struct event_base *base, struct timeval *tv)
{
int i, res;
struct evport_data *epdp = base->evbase;
- port_event_t pevtlist[EVENTS_PER_GETN];
+ port_event_t *pevtlist = epdp->ed_pevtlist;
/*
* port_getn will block until it has at least nevents events. It will
* also return how many it's given us (which may be more than we asked
- * for, as long as it's less than our maximum (EVENTS_PER_GETN)) in
+ * for, as long as it's less than our maximum (ed_maxevents)) in
* nevents.
*/
int nevents = 1;
@@ -291,22 +285,25 @@ evport_dispatch(struct event_base *base, struct timeval *tv)
* last time which need reassociation. See comment at the end of the
* loop below.
*/
- for (i = 0; i < EVENTS_PER_GETN; ++i) {
+ for (i = 0; i < epdp->ed_npending; ++i) {
struct fd_info *fdi = NULL;
- if (epdp->ed_pending[i] != -1) {
- fdi = &(epdp->ed_fds[epdp->ed_pending[i]]);
+ const int fd = epdp->ed_pending[i];
+ if (fd != -1) {
+ /* We might have cleared out this event; we need
+ * to be sure that it's still set. */
+ fdi = evmap_io_get_fdinfo_(&base->io, fd);
}
if (fdi != NULL && FDI_HAS_EVENTS(fdi)) {
- int fd = epdp->ed_pending[i];
reassociate(epdp, fdi, fd);
- epdp->ed_pending[i] = -1;
+ /* epdp->ed_pending[i] = -1; */
+ fdi->pending_idx_plus_1 = 0;
}
}
EVBASE_RELEASE_LOCK(base, th_base_lock);
- res = port_getn(epdp->ed_port, pevtlist, EVENTS_PER_GETN,
+ res = port_getn(epdp->ed_port, pevtlist, epdp->ed_maxevents,
(unsigned int *) &nevents, ts_p);
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
@@ -326,13 +323,15 @@ evport_dispatch(struct event_base *base, struct timeval *tv)
event_debug(("%s: port_getn reports %d events", __func__, nevents));
for (i = 0; i < nevents; ++i) {
- struct fd_info *fdi;
port_event_t *pevt = &pevtlist[i];
int fd = (int) pevt->portev_object;
+ struct fd_info *fdi = pevt->portev_user;
+ /*EVUTIL_ASSERT(evmap_io_get_fdinfo_(&base->io, fd) == fdi);*/
check_evportop(epdp);
check_event(pevt);
epdp->ed_pending[i] = fd;
+ fdi->pending_idx_plus_1 = i + 1;
/*
* Figure out what kind of event it was
@@ -354,11 +353,16 @@ evport_dispatch(struct event_base *base, struct timeval *tv)
if (pevt->portev_events & (POLLERR|POLLHUP|POLLNVAL))
res |= EV_READ|EV_WRITE;
- EVUTIL_ASSERT(epdp->ed_nevents > fd);
- fdi = &(epdp->ed_fds[fd]);
-
- evmap_io_active(base, fd, res);
+ evmap_io_active_(base, fd, res);
} /* end of all events gotten */
+ epdp->ed_npending = nevents;
+
+ if (nevents == epdp->ed_maxevents &&
+ epdp->ed_maxevents < MAX_EVENTS_PER_GETN) {
+ /* we used all the space this time. We should be ready
+ * for more events next time around. */
+ grow(epdp, epdp->ed_maxevents * 2);
+ }
check_evportop(epdp);
@@ -375,27 +379,10 @@ static int
evport_add(struct event_base *base, int fd, short old, short events, void *p)
{
struct evport_data *evpd = base->evbase;
- struct fd_info *fdi;
- int factor;
- (void)p;
+ struct fd_info *fdi = p;
check_evportop(evpd);
- /*
- * If necessary, grow the file descriptor info table
- */
-
- factor = 1;
- while (fd >= factor * evpd->ed_nevents)
- factor *= 2;
-
- if (factor > 1) {
- if (-1 == grow(evpd, factor)) {
- return (-1);
- }
- }
-
- fdi = &evpd->ed_fds[fd];
fdi->fdi_what |= events;
return reassociate(evpd, fdi, fd);
@@ -409,29 +396,12 @@ static int
evport_del(struct event_base *base, int fd, short old, short events, void *p)
{
struct evport_data *evpd = base->evbase;
- struct fd_info *fdi;
- int i;
- int associated = 1;
- (void)p;
+ struct fd_info *fdi = p;
+ int associated = ! fdi->pending_idx_plus_1;
check_evportop(evpd);
- if (evpd->ed_nevents < fd) {
- return (-1);
- }
-
- for (i = 0; i < EVENTS_PER_GETN; ++i) {
- if (evpd->ed_pending[i] == fd) {
- associated = 0;
- break;
- }
- }
-
- fdi = &evpd->ed_fds[fd];
- if (events & EV_READ)
- fdi->fdi_what &= ~EV_READ;
- if (events & EV_WRITE)
- fdi->fdi_what &= ~EV_WRITE;
+ fdi->fdi_what &= ~(events &(EV_READ|EV_WRITE));
if (associated) {
if (!FDI_HAS_EVENTS(fdi) &&
@@ -451,7 +421,10 @@ evport_del(struct event_base *base, int fd, short old, short events, void *p)
}
} else {
if ((fdi->fdi_what & (EV_READ|EV_WRITE)) == 0) {
+ const int i = fdi->pending_idx_plus_1 - 1;
+ EVUTIL_ASSERT(evpd->ed_pending[i] == fd);
evpd->ed_pending[i] = -1;
+ fdi->pending_idx_plus_1 = 0;
}
}
return 0;
@@ -463,11 +436,16 @@ evport_dealloc(struct event_base *base)
{
struct evport_data *evpd = base->evbase;
- evsig_dealloc(base);
+ evsig_dealloc_(base);
close(evpd->ed_port);
- if (evpd->ed_fds)
- mm_free(evpd->ed_fds);
+ if (evpd->ed_pending)
+ mm_free(evpd->ed_pending);
+ if (evpd->ed_pevtlist)
+ mm_free(evpd->ed_pevtlist);
+
mm_free(evpd);
}
+
+#endif /* EVENT__HAVE_EVENT_PORTS */
diff --git a/evrpc-internal.h b/evrpc-internal.h
index d6ea41a7..9eb37638 100644
--- a/evrpc-internal.h
+++ b/evrpc-internal.h
@@ -24,9 +24,10 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVRPC_INTERNAL_H_
-#define _EVRPC_INTERNAL_H_
+#ifndef EVRPC_INTERNAL_H_INCLUDED_
+#define EVRPC_INTERNAL_H_INCLUDED_
+#include "event2/http.h"
#include "http-internal.h"
struct evrpc;
@@ -56,7 +57,7 @@ TAILQ_HEAD(evrpc_hook_list, evrpc_hook);
struct evrpc_hook_ctx;
TAILQ_HEAD(evrpc_pause_list, evrpc_hook_ctx);
-struct _evrpc_hooks {
+struct evrpc_hooks_ {
/* hooks for processing outbound and inbound rpcs */
struct evrpc_hook_list in_hooks;
struct evrpc_hook_list out_hooks;
@@ -69,7 +70,7 @@ struct _evrpc_hooks {
#define paused_requests common.pause_requests
struct evrpc_base {
- struct _evrpc_hooks common;
+ struct evrpc_hooks_ common;
/* the HTTP server under which we register our RPC calls */
struct evhttp* http_server;
@@ -79,11 +80,11 @@ struct evrpc_base {
};
struct evrpc_req_generic;
-void evrpc_reqstate_free(struct evrpc_req_generic* rpc_state);
+void evrpc_reqstate_free_(struct evrpc_req_generic* rpc_state);
/* A pool for holding evhttp_connection objects */
struct evrpc_pool {
- struct _evrpc_hooks common;
+ struct evrpc_hooks_ common;
struct event_base *base;
@@ -117,14 +118,14 @@ struct evrpc_hook_meta {
};
/* allows association of meta data with a request */
-static void evrpc_hook_associate_meta(struct evrpc_hook_meta **pctx,
+static void evrpc_hook_associate_meta_(struct evrpc_hook_meta **pctx,
struct evhttp_connection *evcon);
/* creates a new meta data store */
-static struct evrpc_hook_meta *evrpc_hook_meta_new(void);
+static struct evrpc_hook_meta *evrpc_hook_meta_new_(void);
/* frees the meta data associated with a request */
-static void evrpc_hook_context_free(struct evrpc_hook_meta *ctx);
+static void evrpc_hook_context_free_(struct evrpc_hook_meta *ctx);
/* the server side of an rpc */
@@ -201,4 +202,4 @@ struct evrpc_request_wrapper {
int (*reply_unmarshal)(void *, struct evbuffer*);
};
-#endif /* _EVRPC_INTERNAL_H_ */
+#endif /* EVRPC_INTERNAL_H_INCLUDED_ */
diff --git a/evrpc.c b/evrpc.c
index 4bf3d2b4..2443ab27 100644
--- a/evrpc.c
+++ b/evrpc.c
@@ -25,8 +25,9 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#ifdef WIN32
+#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
@@ -34,16 +35,16 @@
#endif
#include <sys/types.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/socket.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
#include <stdio.h>
#include <stdlib.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <unistd.h>
#endif
#include <errno.h>
@@ -121,7 +122,7 @@ evrpc_add_hook(void *vbase,
int (*cb)(void *, struct evhttp_request *, struct evbuffer *, void *),
void *cb_arg)
{
- struct _evrpc_hooks *base = vbase;
+ struct evrpc_hooks_ *base = vbase;
struct evrpc_hook_list *head = NULL;
struct evrpc_hook *hook = NULL;
switch (hook_type) {
@@ -167,7 +168,7 @@ evrpc_remove_hook_internal(struct evrpc_hook_list *head, void *handle)
int
evrpc_remove_hook(void *vbase, enum EVRPC_HOOK_TYPE hook_type, void *handle)
{
- struct _evrpc_hooks *base = vbase;
+ struct evrpc_hooks_ *base = vbase;
struct evrpc_hook_list *head = NULL;
switch (hook_type) {
case EVRPC_INPUT:
@@ -301,7 +302,7 @@ evrpc_request_cb(struct evhttp_request *req, void *arg)
if (TAILQ_FIRST(&rpc->base->input_hooks) != NULL) {
int hook_res;
- evrpc_hook_associate_meta(&rpc_state->hook_meta, req->evcon);
+ evrpc_hook_associate_meta_(&rpc_state->hook_meta, req->evcon);
/*
* allow hooks to modify the outgoing request
@@ -328,8 +329,7 @@ evrpc_request_cb(struct evhttp_request *req, void *arg)
return;
error:
- if (rpc_state != NULL)
- evrpc_reqstate_free(rpc_state);
+ evrpc_reqstate_free_(rpc_state);
evhttp_send_error(req, HTTP_SERVUNAVAIL, NULL);
return;
}
@@ -371,15 +371,14 @@ evrpc_request_cb_closure(void *arg, enum EVRPC_HOOK_RESULT hook_res)
return;
error:
- if (rpc_state != NULL)
- evrpc_reqstate_free(rpc_state);
+ evrpc_reqstate_free_(rpc_state);
evhttp_send_error(req, HTTP_SERVUNAVAIL, NULL);
return;
}
void
-evrpc_reqstate_free(struct evrpc_req_generic* rpc_state)
+evrpc_reqstate_free_(struct evrpc_req_generic* rpc_state)
{
struct evrpc *rpc;
EVUTIL_ASSERT(rpc_state != NULL);
@@ -387,7 +386,7 @@ evrpc_reqstate_free(struct evrpc_req_generic* rpc_state)
/* clean up all memory */
if (rpc_state->hook_meta != NULL)
- evrpc_hook_context_free(rpc_state->hook_meta);
+ evrpc_hook_context_free_(rpc_state->hook_meta);
if (rpc_state->request != NULL)
rpc->request_free(rpc_state->request);
if (rpc_state->reply != NULL)
@@ -427,7 +426,7 @@ evrpc_request_done(struct evrpc_req_generic *rpc_state)
if (TAILQ_FIRST(&rpc->base->output_hooks) != NULL) {
int hook_res;
- evrpc_hook_associate_meta(&rpc_state->hook_meta, req->evcon);
+ evrpc_hook_associate_meta_(&rpc_state->hook_meta, req->evcon);
/* do hook based tweaks to the request */
hook_res = evrpc_process_hooks(&rpc->base->output_hooks,
@@ -453,8 +452,7 @@ evrpc_request_done(struct evrpc_req_generic *rpc_state)
return;
error:
- if (rpc_state != NULL)
- evrpc_reqstate_free(rpc_state);
+ evrpc_reqstate_free_(rpc_state);
evhttp_send_error(req, HTTP_SERVUNAVAIL, NULL);
return;
}
@@ -489,13 +487,12 @@ evrpc_request_done_closure(void *arg, enum EVRPC_HOOK_RESULT hook_res)
}
evhttp_send_reply(req, HTTP_OK, "OK", rpc_state->rpc_data);
- evrpc_reqstate_free(rpc_state);
+ evrpc_reqstate_free_(rpc_state);
return;
error:
- if (rpc_state != NULL)
- evrpc_reqstate_free(rpc_state);
+ evrpc_reqstate_free_(rpc_state);
evhttp_send_error(req, HTTP_SERVUNAVAIL, NULL);
return;
}
@@ -531,7 +528,7 @@ static void
evrpc_request_wrapper_free(struct evrpc_request_wrapper *request)
{
if (request->hook_meta != NULL)
- evrpc_hook_context_free(request->hook_meta);
+ evrpc_hook_context_free_(request->hook_meta);
mm_free(request->name);
mm_free(request);
}
@@ -595,8 +592,8 @@ evrpc_pool_add_connection(struct evrpc_pool *pool,
* unless a timeout was specifically set for a connection,
* the connection inherits the timeout from the pool.
*/
- if (connection->timeout == -1)
- connection->timeout = pool->timeout;
+ if (!evutil_timerisset(&connection->timeout))
+ evhttp_connection_set_timeout(connection, pool->timeout);
/*
* if we have any requests pending, schedule them with the new
@@ -623,7 +620,7 @@ evrpc_pool_set_timeout(struct evrpc_pool *pool, int timeout_in_secs)
{
struct evhttp_connection *evcon;
TAILQ_FOREACH(evcon, &pool->connections, next) {
- evcon->timeout = timeout_in_secs;
+ evhttp_connection_set_timeout(evcon, timeout_in_secs);
}
pool->timeout = timeout_in_secs;
}
@@ -680,7 +677,7 @@ evrpc_schedule_request(struct evhttp_connection *connection,
if (TAILQ_FIRST(&pool->output_hooks) != NULL) {
int hook_res;
- evrpc_hook_associate_meta(&ctx->hook_meta, connection);
+ evrpc_hook_associate_meta_(&ctx->hook_meta, connection);
/* apply hooks to the outgoing request */
hook_res = evrpc_process_hooks(&pool->output_hooks,
@@ -765,7 +762,7 @@ static int
evrpc_pause_request(void *vbase, void *ctx,
void (*cb)(void *, enum EVRPC_HOOK_RESULT))
{
- struct _evrpc_hooks *base = vbase;
+ struct evrpc_hooks_ *base = vbase;
struct evrpc_hook_ctx *pause = mm_malloc(sizeof(*pause));
if (pause == NULL)
return (-1);
@@ -780,7 +777,7 @@ evrpc_pause_request(void *vbase, void *ctx,
int
evrpc_resume_request(void *vbase, void *ctx, enum EVRPC_HOOK_RESULT res)
{
- struct _evrpc_hooks *base = vbase;
+ struct evrpc_hooks_ *base = vbase;
struct evrpc_pause_list *head = &base->pause_requests;
struct evrpc_hook_ctx *pause;
@@ -877,7 +874,7 @@ evrpc_reply_done(struct evhttp_request *req, void *arg)
}
if (TAILQ_FIRST(&pool->input_hooks) != NULL) {
- evrpc_hook_associate_meta(&ctx->hook_meta, ctx->evcon);
+ evrpc_hook_associate_meta_(&ctx->hook_meta, ctx->evcon);
/* apply hooks to the incoming request */
hook_res = evrpc_process_hooks(&pool->input_hooks,
@@ -976,7 +973,7 @@ evrpc_request_timeout(evutil_socket_t fd, short what, void *arg)
struct evhttp_connection *evcon = ctx->evcon;
EVUTIL_ASSERT(evcon != NULL);
- evhttp_connection_fail(evcon, EVCON_HTTP_TIMEOUT);
+ evhttp_connection_fail_(evcon, EVREQ_HTTP_TIMEOUT);
}
/*
@@ -998,7 +995,7 @@ evrpc_meta_data_free(struct evrpc_meta_list *meta_data)
}
static struct evrpc_hook_meta *
-evrpc_hook_meta_new(void)
+evrpc_hook_meta_new_(void)
{
struct evrpc_hook_meta *ctx;
ctx = mm_malloc(sizeof(struct evrpc_hook_meta));
@@ -1011,17 +1008,17 @@ evrpc_hook_meta_new(void)
}
static void
-evrpc_hook_associate_meta(struct evrpc_hook_meta **pctx,
+evrpc_hook_associate_meta_(struct evrpc_hook_meta **pctx,
struct evhttp_connection *evcon)
{
struct evrpc_hook_meta *ctx = *pctx;
if (ctx == NULL)
- *pctx = ctx = evrpc_hook_meta_new();
+ *pctx = ctx = evrpc_hook_meta_new_();
ctx->evcon = evcon;
}
static void
-evrpc_hook_context_free(struct evrpc_hook_meta *ctx)
+evrpc_hook_context_free_(struct evrpc_hook_meta *ctx)
{
evrpc_meta_data_free(&ctx->meta_data);
mm_free(ctx);
@@ -1037,7 +1034,7 @@ evrpc_hook_add_meta(void *ctx, const char *key,
struct evrpc_meta *meta = NULL;
if ((store = req->hook_meta) == NULL)
- store = req->hook_meta = evrpc_hook_meta_new();
+ store = req->hook_meta = evrpc_hook_meta_new_();
meta = mm_malloc(sizeof(struct evrpc_meta));
EVUTIL_ASSERT(meta != NULL);
diff --git a/evsignal-internal.h b/evsignal-internal.h
index 426f4a4d..5cff03b5 100644
--- a/evsignal-internal.h
+++ b/evsignal-internal.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVSIGNAL_H_
-#define _EVSIGNAL_H_
+#ifndef EVSIGNAL_INTERNAL_H_INCLUDED_
+#define EVSIGNAL_INTERNAL_H_INCLUDED_
#ifndef evutil_socket_t
#include "event2/util.h"
@@ -48,7 +48,7 @@ struct evsig_info {
/* Array of previous signal handler objects before Libevent started
* messing with them. Used to restore old signal handlers. */
-#ifdef _EVENT_HAVE_SIGACTION
+#ifdef EVENT__HAVE_SIGACTION
struct sigaction **sh_old;
#else
ev_sighandler_t **sh_old;
@@ -56,9 +56,10 @@ struct evsig_info {
/* Size of sh_old. */
int sh_old_max;
};
-int evsig_init(struct event_base *);
-void evsig_dealloc(struct event_base *);
+int evsig_init_(struct event_base *);
+void evsig_dealloc_(struct event_base *);
-void evsig_set_base(struct event_base *base);
+void evsig_set_base_(struct event_base *base);
+void evsig_free_globals_(void);
-#endif /* _EVSIGNAL_H_ */
+#endif /* EVSIGNAL_INTERNAL_H_INCLUDED_ */
diff --git a/evthread-internal.h b/evthread-internal.h
index ccfcdde8..346b2bb9 100644
--- a/evthread-internal.h
+++ b/evthread-internal.h
@@ -23,20 +23,22 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVTHREAD_INTERNAL_H_
-#define _EVTHREAD_INTERNAL_H_
+#ifndef EVTHREAD_INTERNAL_H_INCLUDED_
+#define EVTHREAD_INTERNAL_H_INCLUDED_
#ifdef __cplusplus
extern "C" {
#endif
-#include "event2/thread.h"
#include "event2/event-config.h"
+#include "evconfig-private.h"
+
+#include "event2/thread.h"
#include "util-internal.h"
struct event_base;
-#ifndef WIN32
+#ifndef _WIN32
/* On Windows, the way we currently make DLLs, it's not allowed for us to
* have shared global structures. Thus, we only do the direct-call-to-function
* code path if we know that the local shared library system supports it.
@@ -44,62 +46,62 @@ struct event_base;
#define EVTHREAD_EXPOSE_STRUCTS
#endif
-#if ! defined(_EVENT_DISABLE_THREAD_SUPPORT) && defined(EVTHREAD_EXPOSE_STRUCTS)
+#if ! defined(EVENT__DISABLE_THREAD_SUPPORT) && defined(EVTHREAD_EXPOSE_STRUCTS)
/* Global function pointers to lock-related functions. NULL if locking isn't
enabled. */
-extern struct evthread_lock_callbacks _evthread_lock_fns;
-extern struct evthread_condition_callbacks _evthread_cond_fns;
-extern unsigned long (*_evthread_id_fn)(void);
-extern int _evthread_lock_debugging_enabled;
+extern struct evthread_lock_callbacks evthread_lock_fns_;
+extern struct evthread_condition_callbacks evthread_cond_fns_;
+extern unsigned long (*evthread_id_fn_)(void);
+extern int evthread_lock_debugging_enabled_;
/** Return the ID of the current thread, or 1 if threading isn't enabled. */
#define EVTHREAD_GET_ID() \
- (_evthread_id_fn ? _evthread_id_fn() : 1)
+ (evthread_id_fn_ ? evthread_id_fn_() : 1)
/** Return true iff we're in the thread that is currently (or most recently)
* running a given event_base's loop. Requires lock. */
#define EVBASE_IN_THREAD(base) \
- (_evthread_id_fn == NULL || \
- (base)->th_owner_id == _evthread_id_fn())
+ (evthread_id_fn_ == NULL || \
+ (base)->th_owner_id == evthread_id_fn_())
/** Return true iff we need to notify the base's main thread about changes to
* its state, because it's currently running the main loop in another
* thread. Requires lock. */
#define EVBASE_NEED_NOTIFY(base) \
- (_evthread_id_fn != NULL && \
+ (evthread_id_fn_ != NULL && \
(base)->running_loop && \
- (base)->th_owner_id != _evthread_id_fn())
+ (base)->th_owner_id != evthread_id_fn_())
/** Allocate a new lock, and store it in lockvar, a void*. Sets lockvar to
NULL if locking is not enabled. */
#define EVTHREAD_ALLOC_LOCK(lockvar, locktype) \
- ((lockvar) = _evthread_lock_fns.alloc ? \
- _evthread_lock_fns.alloc(locktype) : NULL)
+ ((lockvar) = evthread_lock_fns_.alloc ? \
+ evthread_lock_fns_.alloc(locktype) : NULL)
/** Free a given lock, if it is present and locking is enabled. */
#define EVTHREAD_FREE_LOCK(lockvar, locktype) \
do { \
- void *_lock_tmp_ = (lockvar); \
- if (_lock_tmp_ && _evthread_lock_fns.free) \
- _evthread_lock_fns.free(_lock_tmp_, (locktype)); \
+ void *lock_tmp_ = (lockvar); \
+ if (lock_tmp_ && evthread_lock_fns_.free) \
+ evthread_lock_fns_.free(lock_tmp_, (locktype)); \
} while (0)
/** Acquire a lock. */
#define EVLOCK_LOCK(lockvar,mode) \
do { \
if (lockvar) \
- _evthread_lock_fns.lock(mode, lockvar); \
+ evthread_lock_fns_.lock(mode, lockvar); \
} while (0)
/** Release a lock */
#define EVLOCK_UNLOCK(lockvar,mode) \
do { \
if (lockvar) \
- _evthread_lock_fns.unlock(mode, lockvar); \
+ evthread_lock_fns_.unlock(mode, lockvar); \
} while (0)
/** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
-#define _EVLOCK_SORTLOCKS(lockvar1, lockvar2) \
+#define EVLOCK_SORTLOCKS_(lockvar1, lockvar2) \
do { \
if (lockvar1 && lockvar2 && lockvar1 > lockvar2) { \
void *tmp = lockvar1; \
@@ -123,19 +125,19 @@ extern int _evthread_lock_debugging_enabled;
* locked and held by us. */
#define EVLOCK_ASSERT_LOCKED(lock) \
do { \
- if ((lock) && _evthread_lock_debugging_enabled) { \
- EVUTIL_ASSERT(_evthread_is_debug_lock_held(lock)); \
+ if ((lock) && evthread_lock_debugging_enabled_) { \
+ EVUTIL_ASSERT(evthread_is_debug_lock_held_(lock)); \
} \
} while (0)
/** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
* manage to get it. */
-static inline int EVLOCK_TRY_LOCK(void *lock);
+static inline int EVLOCK_TRY_LOCK_(void *lock);
static inline int
-EVLOCK_TRY_LOCK(void *lock)
+EVLOCK_TRY_LOCK_(void *lock)
{
- if (lock && _evthread_lock_fns.lock) {
- int r = _evthread_lock_fns.lock(EVTHREAD_TRY, lock);
+ if (lock && evthread_lock_fns_.lock) {
+ int r = evthread_lock_fns_.lock(EVTHREAD_TRY, lock);
return !r;
} else {
/* Locking is disabled either globally or for this thing;
@@ -147,79 +149,79 @@ EVLOCK_TRY_LOCK(void *lock)
/** Allocate a new condition variable and store it in the void *, condvar */
#define EVTHREAD_ALLOC_COND(condvar) \
do { \
- (condvar) = _evthread_cond_fns.alloc_condition ? \
- _evthread_cond_fns.alloc_condition(0) : NULL; \
+ (condvar) = evthread_cond_fns_.alloc_condition ? \
+ evthread_cond_fns_.alloc_condition(0) : NULL; \
} while (0)
/** Deallocate and free a condition variable in condvar */
#define EVTHREAD_FREE_COND(cond) \
do { \
if (cond) \
- _evthread_cond_fns.free_condition((cond)); \
+ evthread_cond_fns_.free_condition((cond)); \
} while (0)
/** Signal one thread waiting on cond */
#define EVTHREAD_COND_SIGNAL(cond) \
- ( (cond) ? _evthread_cond_fns.signal_condition((cond), 0) : 0 )
+ ( (cond) ? evthread_cond_fns_.signal_condition((cond), 0) : 0 )
/** Signal all threads waiting on cond */
#define EVTHREAD_COND_BROADCAST(cond) \
- ( (cond) ? _evthread_cond_fns.signal_condition((cond), 1) : 0 )
+ ( (cond) ? evthread_cond_fns_.signal_condition((cond), 1) : 0 )
/** Wait until the condition 'cond' is signalled. Must be called while
* holding 'lock'. The lock will be released until the condition is
* signalled, at which point it will be acquired again. Returns 0 for
* success, -1 for failure. */
#define EVTHREAD_COND_WAIT(cond, lock) \
- ( (cond) ? _evthread_cond_fns.wait_condition((cond), (lock), NULL) : 0 )
+ ( (cond) ? evthread_cond_fns_.wait_condition((cond), (lock), NULL) : 0 )
/** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed. Returns 1
* on timeout. */
#define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv) \
- ( (cond) ? _evthread_cond_fns.wait_condition((cond), (lock), (tv)) : 0 )
+ ( (cond) ? evthread_cond_fns_.wait_condition((cond), (lock), (tv)) : 0 )
/** True iff locking functions have been configured. */
#define EVTHREAD_LOCKING_ENABLED() \
- (_evthread_lock_fns.lock != NULL)
-
-#elif ! defined(_EVENT_DISABLE_THREAD_SUPPORT)
-
-unsigned long _evthreadimpl_get_id(void);
-int _evthreadimpl_is_lock_debugging_enabled(void);
-void *_evthreadimpl_lock_alloc(unsigned locktype);
-void _evthreadimpl_lock_free(void *lock, unsigned locktype);
-int _evthreadimpl_lock_lock(unsigned mode, void *lock);
-int _evthreadimpl_lock_unlock(unsigned mode, void *lock);
-void *_evthreadimpl_cond_alloc(unsigned condtype);
-void _evthreadimpl_cond_free(void *cond);
-int _evthreadimpl_cond_signal(void *cond, int broadcast);
-int _evthreadimpl_cond_wait(void *cond, void *lock, const struct timeval *tv);
-int _evthreadimpl_locking_enabled(void);
-
-#define EVTHREAD_GET_ID() _evthreadimpl_get_id()
+ (evthread_lock_fns_.lock != NULL)
+
+#elif ! defined(EVENT__DISABLE_THREAD_SUPPORT)
+
+unsigned long evthreadimpl_get_id_(void);
+int evthreadimpl_is_lock_debugging_enabled_(void);
+void *evthreadimpl_lock_alloc_(unsigned locktype);
+void evthreadimpl_lock_free_(void *lock, unsigned locktype);
+int evthreadimpl_lock_lock_(unsigned mode, void *lock);
+int evthreadimpl_lock_unlock_(unsigned mode, void *lock);
+void *evthreadimpl_cond_alloc_(unsigned condtype);
+void evthreadimpl_cond_free_(void *cond);
+int evthreadimpl_cond_signal_(void *cond, int broadcast);
+int evthreadimpl_cond_wait_(void *cond, void *lock, const struct timeval *tv);
+int evthreadimpl_locking_enabled_(void);
+
+#define EVTHREAD_GET_ID() evthreadimpl_get_id_()
#define EVBASE_IN_THREAD(base) \
- ((base)->th_owner_id == _evthreadimpl_get_id())
+ ((base)->th_owner_id == evthreadimpl_get_id_())
#define EVBASE_NEED_NOTIFY(base) \
((base)->running_loop && \
- ((base)->th_owner_id != _evthreadimpl_get_id()))
+ ((base)->th_owner_id != evthreadimpl_get_id_()))
#define EVTHREAD_ALLOC_LOCK(lockvar, locktype) \
- ((lockvar) = _evthreadimpl_lock_alloc(locktype))
+ ((lockvar) = evthreadimpl_lock_alloc_(locktype))
#define EVTHREAD_FREE_LOCK(lockvar, locktype) \
do { \
- void *_lock_tmp_ = (lockvar); \
- if (_lock_tmp_) \
- _evthreadimpl_lock_free(_lock_tmp_, (locktype)); \
+ void *lock_tmp_ = (lockvar); \
+ if (lock_tmp_) \
+ evthreadimpl_lock_free_(lock_tmp_, (locktype)); \
} while (0)
/** Acquire a lock. */
#define EVLOCK_LOCK(lockvar,mode) \
do { \
if (lockvar) \
- _evthreadimpl_lock_lock(mode, lockvar); \
+ evthreadimpl_lock_lock_(mode, lockvar); \
} while (0)
/** Release a lock */
#define EVLOCK_UNLOCK(lockvar,mode) \
do { \
if (lockvar) \
- _evthreadimpl_lock_unlock(mode, lockvar); \
+ evthreadimpl_lock_unlock_(mode, lockvar); \
} while (0)
/** Lock an event_base, if it is set up for locking. Acquires the lock
@@ -237,19 +239,19 @@ int _evthreadimpl_locking_enabled(void);
* locked and held by us. */
#define EVLOCK_ASSERT_LOCKED(lock) \
do { \
- if ((lock) && _evthreadimpl_is_lock_debugging_enabled()) { \
- EVUTIL_ASSERT(_evthread_is_debug_lock_held(lock)); \
+ if ((lock) && evthreadimpl_is_lock_debugging_enabled_()) { \
+ EVUTIL_ASSERT(evthread_is_debug_lock_held_(lock)); \
} \
} while (0)
/** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
* manage to get it. */
-static inline int EVLOCK_TRY_LOCK(void *lock);
+static inline int EVLOCK_TRY_LOCK_(void *lock);
static inline int
-EVLOCK_TRY_LOCK(void *lock)
+EVLOCK_TRY_LOCK_(void *lock)
{
if (lock) {
- int r = _evthreadimpl_lock_lock(EVTHREAD_TRY, lock);
+ int r = evthreadimpl_lock_lock_(EVTHREAD_TRY, lock);
return !r;
} else {
/* Locking is disabled either globally or for this thing;
@@ -261,68 +263,68 @@ EVLOCK_TRY_LOCK(void *lock)
/** Allocate a new condition variable and store it in the void *, condvar */
#define EVTHREAD_ALLOC_COND(condvar) \
do { \
- (condvar) = _evthreadimpl_cond_alloc(0); \
+ (condvar) = evthreadimpl_cond_alloc_(0); \
} while (0)
/** Deallocate and free a condition variable in condvar */
#define EVTHREAD_FREE_COND(cond) \
do { \
if (cond) \
- _evthreadimpl_cond_free((cond)); \
+ evthreadimpl_cond_free_((cond)); \
} while (0)
/** Signal one thread waiting on cond */
#define EVTHREAD_COND_SIGNAL(cond) \
- ( (cond) ? _evthreadimpl_cond_signal((cond), 0) : 0 )
+ ( (cond) ? evthreadimpl_cond_signal_((cond), 0) : 0 )
/** Signal all threads waiting on cond */
#define EVTHREAD_COND_BROADCAST(cond) \
- ( (cond) ? _evthreadimpl_cond_signal((cond), 1) : 0 )
+ ( (cond) ? evthreadimpl_cond_signal_((cond), 1) : 0 )
/** Wait until the condition 'cond' is signalled. Must be called while
* holding 'lock'. The lock will be released until the condition is
* signalled, at which point it will be acquired again. Returns 0 for
* success, -1 for failure. */
#define EVTHREAD_COND_WAIT(cond, lock) \
- ( (cond) ? _evthreadimpl_cond_wait((cond), (lock), NULL) : 0 )
+ ( (cond) ? evthreadimpl_cond_wait_((cond), (lock), NULL) : 0 )
/** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed. Returns 1
* on timeout. */
#define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv) \
- ( (cond) ? _evthreadimpl_cond_wait((cond), (lock), (tv)) : 0 )
+ ( (cond) ? evthreadimpl_cond_wait_((cond), (lock), (tv)) : 0 )
#define EVTHREAD_LOCKING_ENABLED() \
- (_evthreadimpl_locking_enabled())
+ (evthreadimpl_locking_enabled_())
-#else /* _EVENT_DISABLE_THREAD_SUPPORT */
+#else /* EVENT__DISABLE_THREAD_SUPPORT */
#define EVTHREAD_GET_ID() 1
-#define EVTHREAD_ALLOC_LOCK(lockvar, locktype) _EVUTIL_NIL_STMT
-#define EVTHREAD_FREE_LOCK(lockvar, locktype) _EVUTIL_NIL_STMT
+#define EVTHREAD_ALLOC_LOCK(lockvar, locktype) EVUTIL_NIL_STMT_
+#define EVTHREAD_FREE_LOCK(lockvar, locktype) EVUTIL_NIL_STMT_
-#define EVLOCK_LOCK(lockvar, mode) _EVUTIL_NIL_STMT
-#define EVLOCK_UNLOCK(lockvar, mode) _EVUTIL_NIL_STMT
-#define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) _EVUTIL_NIL_STMT
-#define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) _EVUTIL_NIL_STMT
+#define EVLOCK_LOCK(lockvar, mode) EVUTIL_NIL_STMT_
+#define EVLOCK_UNLOCK(lockvar, mode) EVUTIL_NIL_STMT_
+#define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) EVUTIL_NIL_STMT_
+#define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) EVUTIL_NIL_STMT_
#define EVBASE_IN_THREAD(base) 1
#define EVBASE_NEED_NOTIFY(base) 0
-#define EVBASE_ACQUIRE_LOCK(base, lock) _EVUTIL_NIL_STMT
-#define EVBASE_RELEASE_LOCK(base, lock) _EVUTIL_NIL_STMT
-#define EVLOCK_ASSERT_LOCKED(lock) _EVUTIL_NIL_STMT
+#define EVBASE_ACQUIRE_LOCK(base, lock) EVUTIL_NIL_STMT_
+#define EVBASE_RELEASE_LOCK(base, lock) EVUTIL_NIL_STMT_
+#define EVLOCK_ASSERT_LOCKED(lock) EVUTIL_NIL_STMT_
-#define EVLOCK_TRY_LOCK(lock) 1
+#define EVLOCK_TRY_LOCK_(lock) 1
-#define EVTHREAD_ALLOC_COND(condvar) _EVUTIL_NIL_STMT
-#define EVTHREAD_FREE_COND(cond) _EVUTIL_NIL_STMT
-#define EVTHREAD_COND_SIGNAL(cond) _EVUTIL_NIL_STMT
-#define EVTHREAD_COND_BROADCAST(cond) _EVUTIL_NIL_STMT
-#define EVTHREAD_COND_WAIT(cond, lock) _EVUTIL_NIL_STMT
-#define EVTHREAD_COND_WAIT_TIMED(cond, lock, howlong) _EVUTIL_NIL_STMT
+#define EVTHREAD_ALLOC_COND(condvar) EVUTIL_NIL_STMT_
+#define EVTHREAD_FREE_COND(cond) EVUTIL_NIL_STMT_
+#define EVTHREAD_COND_SIGNAL(cond) EVUTIL_NIL_STMT_
+#define EVTHREAD_COND_BROADCAST(cond) EVUTIL_NIL_STMT_
+#define EVTHREAD_COND_WAIT(cond, lock) EVUTIL_NIL_STMT_
+#define EVTHREAD_COND_WAIT_TIMED(cond, lock, howlong) EVUTIL_NIL_STMT_
#define EVTHREAD_LOCKING_ENABLED() 0
#endif
/* This code is shared between both lock impls */
-#if ! defined(_EVENT_DISABLE_THREAD_SUPPORT)
+#if ! defined(EVENT__DISABLE_THREAD_SUPPORT)
/** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
-#define _EVLOCK_SORTLOCKS(lockvar1, lockvar2) \
+#define EVLOCK_SORTLOCKS_(lockvar1, lockvar2) \
do { \
if (lockvar1 && lockvar2 && lockvar1 > lockvar2) { \
void *tmp = lockvar1; \
@@ -335,26 +337,26 @@ EVLOCK_TRY_LOCK(void *lock)
* so that two threads locking two locks with LOCK2 will not deadlock. */
#define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) \
do { \
- void *_lock1_tmplock = (lock1); \
- void *_lock2_tmplock = (lock2); \
- _EVLOCK_SORTLOCKS(_lock1_tmplock,_lock2_tmplock); \
- EVLOCK_LOCK(_lock1_tmplock,mode1); \
- if (_lock2_tmplock != _lock1_tmplock) \
- EVLOCK_LOCK(_lock2_tmplock,mode2); \
+ void *lock1_tmplock_ = (lock1); \
+ void *lock2_tmplock_ = (lock2); \
+ EVLOCK_SORTLOCKS_(lock1_tmplock_,lock2_tmplock_); \
+ EVLOCK_LOCK(lock1_tmplock_,mode1); \
+ if (lock2_tmplock_ != lock1_tmplock_) \
+ EVLOCK_LOCK(lock2_tmplock_,mode2); \
} while (0)
/** Release both lock1 and lock2. */
#define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) \
do { \
- void *_lock1_tmplock = (lock1); \
- void *_lock2_tmplock = (lock2); \
- _EVLOCK_SORTLOCKS(_lock1_tmplock,_lock2_tmplock); \
- if (_lock2_tmplock != _lock1_tmplock) \
- EVLOCK_UNLOCK(_lock2_tmplock,mode2); \
- EVLOCK_UNLOCK(_lock1_tmplock,mode1); \
+ void *lock1_tmplock_ = (lock1); \
+ void *lock2_tmplock_ = (lock2); \
+ EVLOCK_SORTLOCKS_(lock1_tmplock_,lock2_tmplock_); \
+ if (lock2_tmplock_ != lock1_tmplock_) \
+ EVLOCK_UNLOCK(lock2_tmplock_,mode2); \
+ EVLOCK_UNLOCK(lock1_tmplock_,mode1); \
} while (0)
-int _evthread_is_debug_lock_held(void *lock);
-void *_evthread_debug_get_real_lock(void *lock);
+int evthread_is_debug_lock_held_(void *lock);
+void *evthread_debug_get_real_lock_(void *lock);
void *evthread_setup_global_lock_(void *lock_, unsigned locktype,
int enable_locks);
@@ -371,6 +373,7 @@ void *evthread_setup_global_lock_(void *lock_, unsigned locktype,
int event_global_setup_locks_(const int enable_locks);
int evsig_global_setup_locks_(const int enable_locks);
+int evutil_global_setup_locks_(const int enable_locks);
int evutil_secure_rng_global_setup_locks_(const int enable_locks);
#endif
@@ -379,4 +382,4 @@ int evutil_secure_rng_global_setup_locks_(const int enable_locks);
}
#endif
-#endif /* _EVTHREAD_INTERNAL_H_ */
+#endif /* EVTHREAD_INTERNAL_H_INCLUDED_ */
diff --git a/evthread.c b/evthread.c
index 90e195d5..4da5d24e 100644
--- a/evthread.c
+++ b/evthread.c
@@ -25,8 +25,9 @@
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
#include "event2/thread.h"
@@ -45,41 +46,41 @@
#endif
/* globals */
-GLOBAL int _evthread_lock_debugging_enabled = 0;
-GLOBAL struct evthread_lock_callbacks _evthread_lock_fns = {
+GLOBAL int evthread_lock_debugging_enabled_ = 0;
+GLOBAL struct evthread_lock_callbacks evthread_lock_fns_ = {
0, 0, NULL, NULL, NULL, NULL
};
-GLOBAL unsigned long (*_evthread_id_fn)(void) = NULL;
-GLOBAL struct evthread_condition_callbacks _evthread_cond_fns = {
+GLOBAL unsigned long (*evthread_id_fn_)(void) = NULL;
+GLOBAL struct evthread_condition_callbacks evthread_cond_fns_ = {
0, NULL, NULL, NULL, NULL
};
/* Used for debugging */
-static struct evthread_lock_callbacks _original_lock_fns = {
+static struct evthread_lock_callbacks original_lock_fns_ = {
0, 0, NULL, NULL, NULL, NULL
};
-static struct evthread_condition_callbacks _original_cond_fns = {
+static struct evthread_condition_callbacks original_cond_fns_ = {
0, NULL, NULL, NULL, NULL
};
void
evthread_set_id_callback(unsigned long (*id_fn)(void))
{
- _evthread_id_fn = id_fn;
+ evthread_id_fn_ = id_fn;
}
int
evthread_set_lock_callbacks(const struct evthread_lock_callbacks *cbs)
{
struct evthread_lock_callbacks *target =
- _evthread_lock_debugging_enabled
- ? &_original_lock_fns : &_evthread_lock_fns;
+ evthread_lock_debugging_enabled_
+ ? &original_lock_fns_ : &evthread_lock_fns_;
if (!cbs) {
if (target->alloc)
event_warnx("Trying to disable lock functions after "
"they have been set up will probaby not work.");
- memset(target, 0, sizeof(_evthread_lock_fns));
+ memset(target, 0, sizeof(evthread_lock_fns_));
return 0;
}
if (target->alloc) {
@@ -98,7 +99,7 @@ evthread_set_lock_callbacks(const struct evthread_lock_callbacks *cbs)
return -1;
}
if (cbs->alloc && cbs->free && cbs->lock && cbs->unlock) {
- memcpy(target, cbs, sizeof(_evthread_lock_fns));
+ memcpy(target, cbs, sizeof(evthread_lock_fns_));
return event_global_setup_locks_(1);
} else {
return -1;
@@ -109,15 +110,15 @@ int
evthread_set_condition_callbacks(const struct evthread_condition_callbacks *cbs)
{
struct evthread_condition_callbacks *target =
- _evthread_lock_debugging_enabled
- ? &_original_cond_fns : &_evthread_cond_fns;
+ evthread_lock_debugging_enabled_
+ ? &original_cond_fns_ : &evthread_cond_fns_;
if (!cbs) {
if (target->alloc_condition)
event_warnx("Trying to disable condition functions "
"after they have been set up will probaby not "
"work.");
- memset(target, 0, sizeof(_evthread_cond_fns));
+ memset(target, 0, sizeof(evthread_cond_fns_));
return 0;
}
if (target->alloc_condition) {
@@ -136,17 +137,20 @@ evthread_set_condition_callbacks(const struct evthread_condition_callbacks *cbs)
}
if (cbs->alloc_condition && cbs->free_condition &&
cbs->signal_condition && cbs->wait_condition) {
- memcpy(target, cbs, sizeof(_evthread_cond_fns));
+ memcpy(target, cbs, sizeof(evthread_cond_fns_));
}
- if (_evthread_lock_debugging_enabled) {
- _evthread_cond_fns.alloc_condition = cbs->alloc_condition;
- _evthread_cond_fns.free_condition = cbs->free_condition;
- _evthread_cond_fns.signal_condition = cbs->signal_condition;
+ if (evthread_lock_debugging_enabled_) {
+ evthread_cond_fns_.alloc_condition = cbs->alloc_condition;
+ evthread_cond_fns_.free_condition = cbs->free_condition;
+ evthread_cond_fns_.signal_condition = cbs->signal_condition;
}
return 0;
}
+#define DEBUG_LOCK_SIG 0xdeb0b10c
+
struct debug_lock {
+ unsigned signature;
unsigned locktype;
unsigned long held_by;
/* XXXX if we ever use read-write locks, we will need a separate
@@ -161,8 +165,8 @@ debug_lock_alloc(unsigned locktype)
struct debug_lock *result = mm_malloc(sizeof(struct debug_lock));
if (!result)
return NULL;
- if (_original_lock_fns.alloc) {
- if (!(result->lock = _original_lock_fns.alloc(
+ if (original_lock_fns_.alloc) {
+ if (!(result->lock = original_lock_fns_.alloc(
locktype|EVTHREAD_LOCKTYPE_RECURSIVE))) {
mm_free(result);
return NULL;
@@ -170,6 +174,7 @@ debug_lock_alloc(unsigned locktype)
} else {
result->lock = NULL;
}
+ result->signature = DEBUG_LOCK_SIG;
result->locktype = locktype;
result->count = 0;
result->held_by = 0;
@@ -182,24 +187,27 @@ debug_lock_free(void *lock_, unsigned locktype)
struct debug_lock *lock = lock_;
EVUTIL_ASSERT(lock->count == 0);
EVUTIL_ASSERT(locktype == lock->locktype);
- if (_original_lock_fns.free) {
- _original_lock_fns.free(lock->lock,
+ EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
+ if (original_lock_fns_.free) {
+ original_lock_fns_.free(lock->lock,
lock->locktype|EVTHREAD_LOCKTYPE_RECURSIVE);
}
lock->lock = NULL;
lock->count = -100;
+ lock->signature = 0x12300fda;
mm_free(lock);
}
static void
evthread_debug_lock_mark_locked(unsigned mode, struct debug_lock *lock)
{
+ EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
++lock->count;
if (!(lock->locktype & EVTHREAD_LOCKTYPE_RECURSIVE))
EVUTIL_ASSERT(lock->count == 1);
- if (_evthread_id_fn) {
+ if (evthread_id_fn_) {
unsigned long me;
- me = _evthread_id_fn();
+ me = evthread_id_fn_();
if (lock->count > 1)
EVUTIL_ASSERT(lock->held_by == me);
lock->held_by = me;
@@ -215,8 +223,8 @@ debug_lock_lock(unsigned mode, void *lock_)
EVUTIL_ASSERT(mode & (EVTHREAD_READ|EVTHREAD_WRITE));
else
EVUTIL_ASSERT((mode & (EVTHREAD_READ|EVTHREAD_WRITE)) == 0);
- if (_original_lock_fns.lock)
- res = _original_lock_fns.lock(mode, lock->lock);
+ if (original_lock_fns_.lock)
+ res = original_lock_fns_.lock(mode, lock->lock);
if (!res) {
evthread_debug_lock_mark_locked(mode, lock);
}
@@ -226,12 +234,15 @@ debug_lock_lock(unsigned mode, void *lock_)
static void
evthread_debug_lock_mark_unlocked(unsigned mode, struct debug_lock *lock)
{
+ EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
if (lock->locktype & EVTHREAD_LOCKTYPE_READWRITE)
EVUTIL_ASSERT(mode & (EVTHREAD_READ|EVTHREAD_WRITE));
else
EVUTIL_ASSERT((mode & (EVTHREAD_READ|EVTHREAD_WRITE)) == 0);
- if (_evthread_id_fn) {
- EVUTIL_ASSERT(lock->held_by == _evthread_id_fn());
+ if (evthread_id_fn_) {
+ unsigned long me;
+ me = evthread_id_fn_();
+ EVUTIL_ASSERT(lock->held_by == me);
if (lock->count == 1)
lock->held_by = 0;
}
@@ -245,27 +256,35 @@ debug_lock_unlock(unsigned mode, void *lock_)
struct debug_lock *lock = lock_;
int res = 0;
evthread_debug_lock_mark_unlocked(mode, lock);
- if (_original_lock_fns.unlock)
- res = _original_lock_fns.unlock(mode, lock->lock);
+ if (original_lock_fns_.unlock)
+ res = original_lock_fns_.unlock(mode, lock->lock);
return res;
}
static int
-debug_cond_wait(void *_cond, void *_lock, const struct timeval *tv)
+debug_cond_wait(void *cond_, void *lock_, const struct timeval *tv)
{
int r;
- struct debug_lock *lock = _lock;
+ struct debug_lock *lock = lock_;
EVUTIL_ASSERT(lock);
- EVLOCK_ASSERT_LOCKED(_lock);
+ EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
+ EVLOCK_ASSERT_LOCKED(lock_);
evthread_debug_lock_mark_unlocked(0, lock);
- r = _original_cond_fns.wait_condition(_cond, lock->lock, tv);
+ r = original_cond_fns_.wait_condition(cond_, lock->lock, tv);
evthread_debug_lock_mark_locked(0, lock);
return r;
}
+/* misspelled version for backward compatibility */
void
evthread_enable_lock_debuging(void)
{
+ evthread_enable_lock_debugging();
+}
+
+void
+evthread_enable_lock_debugging(void)
+{
struct evthread_lock_callbacks cbs = {
EVTHREAD_LOCK_API_VERSION,
EVTHREAD_LOCKTYPE_RECURSIVE,
@@ -274,30 +293,30 @@ evthread_enable_lock_debuging(void)
debug_lock_lock,
debug_lock_unlock
};
- if (_evthread_lock_debugging_enabled)
+ if (evthread_lock_debugging_enabled_)
return;
- memcpy(&_original_lock_fns, &_evthread_lock_fns,
+ memcpy(&original_lock_fns_, &evthread_lock_fns_,
sizeof(struct evthread_lock_callbacks));
- memcpy(&_evthread_lock_fns, &cbs,
+ memcpy(&evthread_lock_fns_, &cbs,
sizeof(struct evthread_lock_callbacks));
- memcpy(&_original_cond_fns, &_evthread_cond_fns,
+ memcpy(&original_cond_fns_, &evthread_cond_fns_,
sizeof(struct evthread_condition_callbacks));
- _evthread_cond_fns.wait_condition = debug_cond_wait;
- _evthread_lock_debugging_enabled = 1;
+ evthread_cond_fns_.wait_condition = debug_cond_wait;
+ evthread_lock_debugging_enabled_ = 1;
/* XXX return value should get checked. */
event_global_setup_locks_(0);
}
int
-_evthread_is_debug_lock_held(void *lock_)
+evthread_is_debug_lock_held_(void *lock_)
{
struct debug_lock *lock = lock_;
if (! lock->count)
return 0;
- if (_evthread_id_fn) {
- unsigned long me = _evthread_id_fn();
+ if (evthread_id_fn_) {
+ unsigned long me = evthread_id_fn_();
if (lock->held_by != me)
return 0;
}
@@ -305,7 +324,7 @@ _evthread_is_debug_lock_held(void *lock_)
}
void *
-_evthread_debug_get_real_lock(void *lock_)
+evthread_debug_get_real_lock_(void *lock_)
{
struct debug_lock *lock = lock_;
return lock->lock;
@@ -320,23 +339,23 @@ evthread_setup_global_lock_(void *lock_, unsigned locktype, int enable_locks)
3) we're turning on locking; debugging is not on.
4) we're turning on locking; debugging is on. */
- if (!enable_locks && _original_lock_fns.alloc == NULL) {
+ if (!enable_locks && original_lock_fns_.alloc == NULL) {
/* Case 1: allocate a debug lock. */
EVUTIL_ASSERT(lock_ == NULL);
return debug_lock_alloc(locktype);
- } else if (!enable_locks && _original_lock_fns.alloc != NULL) {
+ } else if (!enable_locks && original_lock_fns_.alloc != NULL) {
/* Case 2: wrap the lock in a debug lock. */
struct debug_lock *lock;
EVUTIL_ASSERT(lock_ != NULL);
if (!(locktype & EVTHREAD_LOCKTYPE_RECURSIVE)) {
/* We can't wrap it: We need a recursive lock */
- _original_lock_fns.free(lock_, locktype);
+ original_lock_fns_.free(lock_, locktype);
return debug_lock_alloc(locktype);
}
lock = mm_malloc(sizeof(struct debug_lock));
if (!lock) {
- _original_lock_fns.free(lock_, locktype);
+ original_lock_fns_.free(lock_, locktype);
return NULL;
}
lock->lock = lock_;
@@ -344,18 +363,18 @@ evthread_setup_global_lock_(void *lock_, unsigned locktype, int enable_locks)
lock->count = 0;
lock->held_by = 0;
return lock;
- } else if (enable_locks && ! _evthread_lock_debugging_enabled) {
+ } else if (enable_locks && ! evthread_lock_debugging_enabled_) {
/* Case 3: allocate a regular lock */
EVUTIL_ASSERT(lock_ == NULL);
- return _evthread_lock_fns.alloc(locktype);
+ return evthread_lock_fns_.alloc(locktype);
} else {
/* Case 4: Fill in a debug lock with a real lock */
struct debug_lock *lock = lock_;
EVUTIL_ASSERT(enable_locks &&
- _evthread_lock_debugging_enabled);
+ evthread_lock_debugging_enabled_);
EVUTIL_ASSERT(lock->locktype == locktype);
EVUTIL_ASSERT(lock->lock == NULL);
- lock->lock = _original_lock_fns.alloc(
+ lock->lock = original_lock_fns_.alloc(
locktype|EVTHREAD_LOCKTYPE_RECURSIVE);
if (!lock->lock) {
lock->count = -200;
@@ -369,76 +388,76 @@ evthread_setup_global_lock_(void *lock_, unsigned locktype, int enable_locks)
#ifndef EVTHREAD_EXPOSE_STRUCTS
unsigned long
-_evthreadimpl_get_id()
+evthreadimpl_get_id_()
{
- return _evthread_id_fn ? _evthread_id_fn() : 1;
+ return evthread_id_fn_ ? evthread_id_fn_() : 1;
}
void *
-_evthreadimpl_lock_alloc(unsigned locktype)
+evthreadimpl_lock_alloc_(unsigned locktype)
{
- return _evthread_lock_fns.alloc ?
- _evthread_lock_fns.alloc(locktype) : NULL;
+ return evthread_lock_fns_.alloc ?
+ evthread_lock_fns_.alloc(locktype) : NULL;
}
void
-_evthreadimpl_lock_free(void *lock, unsigned locktype)
+evthreadimpl_lock_free_(void *lock, unsigned locktype)
{
- if (_evthread_lock_fns.free)
- _evthread_lock_fns.free(lock, locktype);
+ if (evthread_lock_fns_.free)
+ evthread_lock_fns_.free(lock, locktype);
}
int
-_evthreadimpl_lock_lock(unsigned mode, void *lock)
+evthreadimpl_lock_lock_(unsigned mode, void *lock)
{
- if (_evthread_lock_fns.lock)
- return _evthread_lock_fns.lock(mode, lock);
+ if (evthread_lock_fns_.lock)
+ return evthread_lock_fns_.lock(mode, lock);
else
return 0;
}
int
-_evthreadimpl_lock_unlock(unsigned mode, void *lock)
+evthreadimpl_lock_unlock_(unsigned mode, void *lock)
{
- if (_evthread_lock_fns.unlock)
- return _evthread_lock_fns.unlock(mode, lock);
+ if (evthread_lock_fns_.unlock)
+ return evthread_lock_fns_.unlock(mode, lock);
else
return 0;
}
void *
-_evthreadimpl_cond_alloc(unsigned condtype)
+evthreadimpl_cond_alloc_(unsigned condtype)
{
- return _evthread_cond_fns.alloc_condition ?
- _evthread_cond_fns.alloc_condition(condtype) : NULL;
+ return evthread_cond_fns_.alloc_condition ?
+ evthread_cond_fns_.alloc_condition(condtype) : NULL;
}
void
-_evthreadimpl_cond_free(void *cond)
+evthreadimpl_cond_free_(void *cond)
{
- if (_evthread_cond_fns.free_condition)
- _evthread_cond_fns.free_condition(cond);
+ if (evthread_cond_fns_.free_condition)
+ evthread_cond_fns_.free_condition(cond);
}
int
-_evthreadimpl_cond_signal(void *cond, int broadcast)
+evthreadimpl_cond_signal_(void *cond, int broadcast)
{
- if (_evthread_cond_fns.signal_condition)
- return _evthread_cond_fns.signal_condition(cond, broadcast);
+ if (evthread_cond_fns_.signal_condition)
+ return evthread_cond_fns_.signal_condition(cond, broadcast);
else
return 0;
}
int
-_evthreadimpl_cond_wait(void *cond, void *lock, const struct timeval *tv)
+evthreadimpl_cond_wait_(void *cond, void *lock, const struct timeval *tv)
{
- if (_evthread_cond_fns.wait_condition)
- return _evthread_cond_fns.wait_condition(cond, lock, tv);
+ if (evthread_cond_fns_.wait_condition)
+ return evthread_cond_fns_.wait_condition(cond, lock, tv);
else
return 0;
}
int
-_evthreadimpl_is_lock_debugging_enabled(void)
+evthreadimpl_is_lock_debugging_enabled_(void)
{
- return _evthread_lock_debugging_enabled;
+ return evthread_lock_debugging_enabled_;
}
int
-_evthreadimpl_locking_enabled(void)
+evthreadimpl_locking_enabled_(void)
{
- return _evthread_lock_fns.lock != NULL;
+ return evthread_lock_fns_.lock != NULL;
}
#endif
diff --git a/evthread_pthread.c b/evthread_pthread.c
index c4d06ea5..4e11f749 100644
--- a/evthread_pthread.c
+++ b/evthread_pthread.c
@@ -24,9 +24,11 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
-/* With glibc we need to define this to get PTHREAD_MUTEX_RECURSIVE. */
-#define _GNU_SOURCE
+/* With glibc we need to define _GNU_SOURCE to get PTHREAD_MUTEX_RECURSIVE.
+ * This comes from evconfig-private.h
+ */
#include <pthread.h>
struct event_base;
@@ -56,17 +58,17 @@ evthread_posix_lock_alloc(unsigned locktype)
}
static void
-evthread_posix_lock_free(void *_lock, unsigned locktype)
+evthread_posix_lock_free(void *lock_, unsigned locktype)
{
- pthread_mutex_t *lock = _lock;
+ pthread_mutex_t *lock = lock_;
pthread_mutex_destroy(lock);
mm_free(lock);
}
static int
-evthread_posix_lock(unsigned mode, void *_lock)
+evthread_posix_lock(unsigned mode, void *lock_)
{
- pthread_mutex_t *lock = _lock;
+ pthread_mutex_t *lock = lock_;
if (mode & EVTHREAD_TRY)
return pthread_mutex_trylock(lock);
else
@@ -74,9 +76,9 @@ evthread_posix_lock(unsigned mode, void *_lock)
}
static int
-evthread_posix_unlock(unsigned mode, void *_lock)
+evthread_posix_unlock(unsigned mode, void *lock_)
{
- pthread_mutex_t *lock = _lock;
+ pthread_mutex_t *lock = lock_;
return pthread_mutex_unlock(lock);
}
@@ -85,13 +87,13 @@ evthread_posix_get_id(void)
{
union {
pthread_t thr;
-#if _EVENT_SIZEOF_PTHREAD_T > _EVENT_SIZEOF_LONG
+#if EVENT__SIZEOF_PTHREAD_T > EVENT__SIZEOF_LONG
ev_uint64_t id;
#else
unsigned long id;
#endif
} r;
-#if _EVENT_SIZEOF_PTHREAD_T < _EVENT_SIZEOF_LONG
+#if EVENT__SIZEOF_PTHREAD_T < EVENT__SIZEOF_LONG
memset(&r, 0, sizeof(r));
#endif
r.thr = pthread_self();
@@ -112,17 +114,17 @@ evthread_posix_cond_alloc(unsigned condflags)
}
static void
-evthread_posix_cond_free(void *_cond)
+evthread_posix_cond_free(void *cond_)
{
- pthread_cond_t *cond = _cond;
+ pthread_cond_t *cond = cond_;
pthread_cond_destroy(cond);
mm_free(cond);
}
static int
-evthread_posix_cond_signal(void *_cond, int broadcast)
+evthread_posix_cond_signal(void *cond_, int broadcast)
{
- pthread_cond_t *cond = _cond;
+ pthread_cond_t *cond = cond_;
int r;
if (broadcast)
r = pthread_cond_broadcast(cond);
@@ -132,11 +134,11 @@ evthread_posix_cond_signal(void *_cond, int broadcast)
}
static int
-evthread_posix_cond_wait(void *_cond, void *_lock, const struct timeval *tv)
+evthread_posix_cond_wait(void *cond_, void *lock_, const struct timeval *tv)
{
int r;
- pthread_cond_t *cond = _cond;
- pthread_mutex_t *lock = _lock;
+ pthread_cond_t *cond = cond_;
+ pthread_mutex_t *lock = lock_;
if (tv) {
struct timeval now, abstime;
diff --git a/evthread_win32.c b/evthread_win32.c
index fabd9813..2ec80560 100644
--- a/evthread_win32.c
+++ b/evthread_win32.c
@@ -24,8 +24,9 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#ifdef WIN32
+#ifdef _WIN32
#ifndef _WIN32_WINNT
/* Minimum required for InitializeCriticalSectionAndSpinCount */
#define _WIN32_WINNT 0x0403
@@ -42,6 +43,7 @@ struct event_base;
#include "mm-internal.h"
#include "evthread-internal.h"
+#include "time-internal.h"
#define SPIN_COUNT 2000
@@ -59,17 +61,17 @@ evthread_win32_lock_create(unsigned locktype)
}
static void
-evthread_win32_lock_free(void *_lock, unsigned locktype)
+evthread_win32_lock_free(void *lock_, unsigned locktype)
{
- CRITICAL_SECTION *lock = _lock;
+ CRITICAL_SECTION *lock = lock_;
DeleteCriticalSection(lock);
mm_free(lock);
}
static int
-evthread_win32_lock(unsigned mode, void *_lock)
+evthread_win32_lock(unsigned mode, void *lock_)
{
- CRITICAL_SECTION *lock = _lock;
+ CRITICAL_SECTION *lock = lock_;
if ((mode & EVTHREAD_TRY)) {
return ! TryEnterCriticalSection(lock);
} else {
@@ -79,9 +81,9 @@ evthread_win32_lock(unsigned mode, void *_lock)
}
static int
-evthread_win32_unlock(unsigned mode, void *_lock)
+evthread_win32_unlock(unsigned mode, void *lock_)
{
- CRITICAL_SECTION *lock = _lock;
+ CRITICAL_SECTION *lock = lock_;
LeaveCriticalSection(lock);
return 0;
}
@@ -133,17 +135,17 @@ evthread_win32_condvar_alloc(unsigned condflags)
}
static void
-evthread_win32_condvar_free(void *_cond)
+evthread_win32_condvar_free(void *cond_)
{
- CONDITION_VARIABLE *cond = _cond;
+ CONDITION_VARIABLE *cond = cond_;
/* There doesn't _seem_ to be a cleaup fn here... */
mm_free(cond);
}
static int
-evthread_win32_condvar_signal(void *_cond, int broadcast)
+evthread_win32_condvar_signal(void *cond, int broadcast)
{
- CONDITION_VARIABLE *cond = _cond;
+ CONDITION_VARIABLE *cond = cond_;
if (broadcast)
WakeAllConditionVariable_fn(cond);
else
@@ -152,15 +154,15 @@ evthread_win32_condvar_signal(void *_cond, int broadcast)
}
static int
-evthread_win32_condvar_wait(void *_cond, void *_lock, const struct timeval *tv)
+evthread_win32_condvar_wait(void *cond_, void *lock_, const struct timeval *tv)
{
- CONDITION_VARIABLE *cond = _cond;
- CRITICAL_SECTION *lock = _lock;
+ CONDITION_VARIABLE *cond = cond_;
+ CRITICAL_SECTION *lock = lock_;
DWORD ms, err;
BOOL result;
if (tv)
- ms = evutil_tv_to_msec(tv);
+ ms = evutil_tv_to_msec_(tv);
else
ms = INFINITE;
result = SleepConditionVariableCS_fn(cond, lock, ms);
@@ -204,18 +206,18 @@ evthread_win32_cond_alloc(unsigned flags)
}
static void
-evthread_win32_cond_free(void *_cond)
+evthread_win32_cond_free(void *cond_)
{
- struct evthread_win32_cond *cond = _cond;
+ struct evthread_win32_cond *cond = cond_;
DeleteCriticalSection(&cond->lock);
CloseHandle(cond->event);
mm_free(cond);
}
static int
-evthread_win32_cond_signal(void *_cond, int broadcast)
+evthread_win32_cond_signal(void *cond_, int broadcast)
{
- struct evthread_win32_cond *cond = _cond;
+ struct evthread_win32_cond *cond = cond_;
EnterCriticalSection(&cond->lock);
if (broadcast)
cond->n_to_wake = cond->n_waiting;
@@ -228,16 +230,16 @@ evthread_win32_cond_signal(void *_cond, int broadcast)
}
static int
-evthread_win32_cond_wait(void *_cond, void *_lock, const struct timeval *tv)
+evthread_win32_cond_wait(void *cond_, void *lock_, const struct timeval *tv)
{
- struct evthread_win32_cond *cond = _cond;
- CRITICAL_SECTION *lock = _lock;
+ struct evthread_win32_cond *cond = cond_;
+ CRITICAL_SECTION *lock = lock_;
int generation_at_start;
int waiting = 1;
int result = -1;
DWORD ms = INFINITE, ms_orig = INFINITE, startTime, endTime;
if (tv)
- ms_orig = ms = evutil_tv_to_msec(tv);
+ ms_orig = ms = evutil_tv_to_msec_(tv);
EnterCriticalSection(&cond->lock);
++cond->n_waiting;
diff --git a/evutil.c b/evutil.c
index 33445170..3d72e403 100644
--- a/evutil.c
+++ b/evutil.c
@@ -25,10 +25,9 @@
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#define _GNU_SOURCE
-
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#define WIN32_LEAN_AND_MEAN
@@ -36,77 +35,92 @@
#undef WIN32_LEAN_AND_MEAN
#include <io.h>
#include <tchar.h>
+#include <process.h>
+#undef _WIN32_WINNT
+/* For structs needed by GetAdaptersAddresses */
+#define _WIN32_WINNT 0x0501
+#include <iphlpapi.h>
#endif
#include <sys/types.h>
-#ifdef _EVENT_HAVE_SYS_SOCKET_H
+#ifdef EVENT__HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
-#ifdef _EVENT_HAVE_FCNTL_H
+#ifdef EVENT__HAVE_FCNTL_H
#include <fcntl.h>
#endif
-#ifdef _EVENT_HAVE_STDLIB_H
+#ifdef EVENT__HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
-#ifdef _EVENT_HAVE_NETINET_IN_H
+#ifdef EVENT__HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
-#ifdef _EVENT_HAVE_NETINET_IN6_H
+#ifdef EVENT__HAVE_NETINET_IN6_H
#include <netinet/in6.h>
#endif
-#ifdef _EVENT_HAVE_ARPA_INET_H
+#ifdef EVENT__HAVE_NETINET_TCP_H
+#include <netinet/tcp.h>
+#endif
+#ifdef EVENT__HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
-
-#ifndef _EVENT_HAVE_GETTIMEOFDAY
-#include <sys/timeb.h>
#include <time.h>
-#endif
#include <sys/stat.h>
+#ifdef EVENT__HAVE_IFADDRS_H
+#include <ifaddrs.h>
+#endif
#include "event2/util.h"
#include "util-internal.h"
#include "log-internal.h"
#include "mm-internal.h"
+#include "evthread-internal.h"
#include "strlcpy-internal.h"
#include "ipv6-internal.h"
-#ifdef WIN32
+#ifdef _WIN32
+#define HT_NO_CACHE_HASH_VALUES
+#include "ht-internal.h"
#define open _open
#define read _read
#define close _close
+#ifndef fstat
#define fstat _fstati64
+#endif
+#ifndef stat
#define stat _stati64
+#endif
#define mode_t int
#endif
int
-evutil_open_closeonexec(const char *pathname, int flags, unsigned mode)
+evutil_open_closeonexec_(const char *pathname, int flags, unsigned mode)
{
int fd;
#ifdef O_CLOEXEC
- flags |= O_CLOEXEC;
+ fd = open(pathname, flags|O_CLOEXEC, (mode_t)mode);
+ if (fd >= 0 || errno == EINVAL)
+ return fd;
+ /* If we got an EINVAL, fall through and try without O_CLOEXEC */
#endif
-
- if (flags & O_CREAT)
- fd = open(pathname, flags, (mode_t)mode);
- else
- fd = open(pathname, flags);
+ fd = open(pathname, flags, (mode_t)mode);
if (fd < 0)
return -1;
-#if !defined(O_CLOEXEC) && defined(FD_CLOEXEC)
- if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
+#if defined(FD_CLOEXEC)
+ if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
+ close(fd);
return -1;
+ }
#endif
return fd;
@@ -123,7 +137,7 @@ evutil_open_closeonexec(const char *pathname, int flags, unsigned mode)
Used internally only; may go away in a future version.
*/
int
-evutil_read_file(const char *filename, char **content_out, size_t *len_out,
+evutil_read_file_(const char *filename, char **content_out, size_t *len_out,
int is_binary)
{
int fd, r;
@@ -142,7 +156,7 @@ evutil_read_file(const char *filename, char **content_out, size_t *len_out,
mode |= O_BINARY;
#endif
- fd = evutil_open_closeonexec(filename, mode, 0);
+ fd = evutil_open_closeonexec_(filename, mode, 0);
if (fd < 0)
return -1;
if (fstat(fd, &st) || st.st_size < 0 ||
@@ -156,7 +170,7 @@ evutil_read_file(const char *filename, char **content_out, size_t *len_out,
return -2;
}
read_so_far = 0;
-#ifdef WIN32
+#ifdef _WIN32
#define N_TO_READ(x) ((x) > INT_MAX) ? INT_MAX : ((int)(x))
#else
#define N_TO_READ(x) (x)
@@ -182,15 +196,15 @@ evutil_read_file(const char *filename, char **content_out, size_t *len_out,
int
evutil_socketpair(int family, int type, int protocol, evutil_socket_t fd[2])
{
-#ifndef WIN32
+#ifndef _WIN32
return socketpair(family, type, protocol, fd);
#else
- return evutil_ersatz_socketpair(family, type, protocol, fd);
+ return evutil_ersatz_socketpair_(family, type, protocol, fd);
#endif
}
int
-evutil_ersatz_socketpair(int family, int type, int protocol,
+evutil_ersatz_socketpair_(int family, int type, int protocol,
evutil_socket_t fd[2])
{
/* This code is originally from Tor. Used with permission. */
@@ -200,7 +214,7 @@ evutil_ersatz_socketpair(int family, int type, int protocol,
* for now, and really, when localhost is down sometimes, we
* have other problems too.
*/
-#ifdef WIN32
+#ifdef _WIN32
#define ERR(e) WSA##e
#else
#define ERR(e) e
@@ -294,7 +308,7 @@ evutil_ersatz_socketpair(int family, int type, int protocol,
int
evutil_make_socket_nonblocking(evutil_socket_t fd)
{
-#ifdef WIN32
+#ifdef _WIN32
{
u_long nonblocking = 1;
if (ioctlsocket(fd, FIONBIO, &nonblocking) == SOCKET_ERROR) {
@@ -309,19 +323,39 @@ evutil_make_socket_nonblocking(evutil_socket_t fd)
event_warn("fcntl(%d, F_GETFL)", fd);
return -1;
}
- if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
- event_warn("fcntl(%d, F_SETFL)", fd);
- return -1;
+ if (!(flags & O_NONBLOCK)) {
+ if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
+ event_warn("fcntl(%d, F_SETFL)", fd);
+ return -1;
+ }
}
}
#endif
return 0;
}
+/* Faster version of evutil_make_socket_nonblocking for internal use.
+ *
+ * Requires that no F_SETFL flags were previously set on the fd.
+ */
+static int
+evutil_fast_socket_nonblocking(evutil_socket_t fd)
+{
+#ifdef _WIN32
+ return evutil_make_socket_nonblocking(fd);
+#else
+ if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
+ event_warn("fcntl(%d, F_SETFL)", fd);
+ return -1;
+ }
+ return 0;
+#endif
+}
+
int
evutil_make_listen_socket_reuseable(evutil_socket_t sock)
{
-#ifndef WIN32
+#ifndef _WIN32
int one = 1;
/* REUSEADDR on Unix means, "don't hang on to this address after the
* listener is closed." On Windows, though, it means "don't keep other
@@ -334,15 +368,61 @@ evutil_make_listen_socket_reuseable(evutil_socket_t sock)
}
int
+evutil_make_listen_socket_reuseable_port(evutil_socket_t sock)
+{
+#if defined __linux__ && defined(SO_REUSEPORT)
+ int one = 1;
+ /* REUSEPORT on Linux 3.9+ means, "Multiple servers (processes or
+ * threads) can bind to the same port if they each set the option. */
+ return setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void*) &one,
+ (ev_socklen_t)sizeof(one));
+#else
+ return 0;
+#endif
+}
+
+int
+evutil_make_tcp_listen_socket_deferred(evutil_socket_t sock)
+{
+#if defined(EVENT__HAVE_NETINET_TCP_H) && defined(TCP_DEFER_ACCEPT)
+ int one = 1;
+
+ /* TCP_DEFER_ACCEPT tells the kernel to call defer accept() only after data
+ * has arrived and ready to read */
+ return setsockopt(sock, IPPROTO_TCP, TCP_DEFER_ACCEPT, &one,
+ (ev_socklen_t)sizeof(one));
+#endif
+ return 0;
+}
+
+int
evutil_make_socket_closeonexec(evutil_socket_t fd)
{
-#if !defined(WIN32) && defined(_EVENT_HAVE_SETFD)
+#if !defined(_WIN32) && defined(EVENT__HAVE_SETFD)
int flags;
if ((flags = fcntl(fd, F_GETFD, NULL)) < 0) {
event_warn("fcntl(%d, F_GETFD)", fd);
return -1;
}
- if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
+ if (!(flags & FD_CLOEXEC)) {
+ if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
+ event_warn("fcntl(%d, F_SETFD)", fd);
+ return -1;
+ }
+ }
+#endif
+ return 0;
+}
+
+/* Faster version of evutil_make_socket_closeonexec for internal use.
+ *
+ * Requires that no F_SETFD flags were previously set on the fd.
+ */
+static int
+evutil_fast_socket_closeonexec(evutil_socket_t fd)
+{
+#if !defined(_WIN32) && defined(EVENT__HAVE_SETFD)
+ if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
event_warn("fcntl(%d, F_SETFD)", fd);
return -1;
}
@@ -353,7 +433,7 @@ evutil_make_socket_closeonexec(evutil_socket_t fd)
int
evutil_closesocket(evutil_socket_t sock)
{
-#ifndef WIN32
+#ifndef _WIN32
return close(sock);
#else
return closesocket(sock);
@@ -363,11 +443,11 @@ evutil_closesocket(evutil_socket_t sock)
ev_int64_t
evutil_strtoll(const char *s, char **endptr, int base)
{
-#ifdef _EVENT_HAVE_STRTOLL
+#ifdef EVENT__HAVE_STRTOLL
return (ev_int64_t)strtoll(s, endptr, base);
-#elif _EVENT_SIZEOF_LONG == 8
+#elif EVENT__SIZEOF_LONG == 8
return (ev_int64_t)strtol(s, endptr, base);
-#elif defined(WIN32) && defined(_MSC_VER) && _MSC_VER < 1300
+#elif defined(_WIN32) && defined(_MSC_VER) && _MSC_VER < 1300
/* XXXX on old versions of MS APIs, we only support base
* 10. */
ev_int64_t r;
@@ -383,9 +463,9 @@ evutil_strtoll(const char *s, char **endptr, int base)
if (endptr)
*endptr = (char*) s;
return r;
-#elif defined(WIN32)
+#elif defined(_WIN32)
return (ev_int64_t) _strtoi64(s, endptr, base);
-#elif defined(_EVENT_SIZEOF_LONG_LONG) && _EVENT_SIZEOF_LONG_LONG == 8
+#elif defined(EVENT__SIZEOF_LONG_LONG) && EVENT__SIZEOF_LONG_LONG == 8
long long r;
int n;
if (base != 10 && base != 16)
@@ -401,15 +481,15 @@ evutil_strtoll(const char *s, char **endptr, int base)
}
if (n != 1)
return 0;
- while (EVUTIL_ISSPACE(*s))
+ while (EVUTIL_ISSPACE_(*s))
++s;
if (*s == '-')
++s;
if (base == 10) {
- while (EVUTIL_ISDIGIT(*s))
+ while (EVUTIL_ISDIGIT_(*s))
++s;
} else {
- while (EVUTIL_ISXDIGIT(*s))
+ while (EVUTIL_ISXDIGIT_(*s))
++s;
}
if (endptr)
@@ -420,32 +500,7 @@ evutil_strtoll(const char *s, char **endptr, int base)
#endif
}
-#ifndef _EVENT_HAVE_GETTIMEOFDAY
-/* No gettimeofday; this muse be windows. */
-int
-evutil_gettimeofday(struct timeval *tv, struct timezone *tz)
-{
- struct _timeb tb;
-
- if (tv == NULL)
- return -1;
-
- /* XXXX
- * _ftime is not the greatest interface here; GetSystemTimeAsFileTime
- * would give us better resolution, whereas something cobbled together
- * with GetTickCount could maybe give us monotonic behavior.
- *
- * Either way, I think this value might be skewed to ignore the
- * timezone, and just return local time. That's not so good.
- */
- _ftime(&tb);
- tv->tv_sec = (long) tb.time;
- tv->tv_usec = ((int) tb.millitm) * 1000;
- return 0;
-}
-#endif
-
-#ifdef WIN32
+#ifdef _WIN32
int
evutil_socket_geterror(evutil_socket_t sock)
{
@@ -465,7 +520,7 @@ evutil_socket_geterror(evutil_socket_t sock)
/* XXX we should use an enum here. */
/* 2 for connection refused, 1 for connected, 0 for not yet, -1 for error. */
int
-evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen)
+evutil_socket_connect_(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen)
{
int made_fd = 0;
@@ -502,7 +557,7 @@ err:
error case, set the current socket errno to the error that happened during
the connect operation. */
int
-evutil_socket_finished_connecting(evutil_socket_t fd)
+evutil_socket_finished_connecting_(evutil_socket_t fd)
{
int e;
ev_socklen_t elen = sizeof(e);
@@ -545,28 +600,150 @@ static int have_checked_interfaces, had_ipv4_address, had_ipv6_address;
*/
#define EVUTIL_V4ADDR_IS_CLASSD(addr) ((((addr)>>24) & 0xf0) == 0xe0)
+static void
+evutil_found_ifaddr(const struct sockaddr *sa)
+{
+ const char ZEROES[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00";
+
+ if (sa->sa_family == AF_INET) {
+ const struct sockaddr_in *sin = (struct sockaddr_in *)sa;
+ ev_uint32_t addr = ntohl(sin->sin_addr.s_addr);
+ if (addr == 0 ||
+ EVUTIL_V4ADDR_IS_LOCALHOST(addr) ||
+ EVUTIL_V4ADDR_IS_CLASSD(addr)) {
+ /* Not actually a usable external address. */
+ } else {
+ event_debug(("Detected an IPv4 interface"));
+ had_ipv4_address = 1;
+ }
+ } else if (sa->sa_family == AF_INET6) {
+ const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
+ const unsigned char *addr =
+ (unsigned char*)sin6->sin6_addr.s6_addr;
+ if (!memcmp(addr, ZEROES, 8) ||
+ ((addr[0] & 0xfe) == 0xfc) ||
+ (addr[0] == 0xfe && (addr[1] & 0xc0) == 0x80) ||
+ (addr[0] == 0xfe && (addr[1] & 0xc0) == 0xc0) ||
+ (addr[0] == 0xff)) {
+ /* This is a reserved, ipv4compat, ipv4map, loopback,
+ * link-local, multicast, or unspecified address. */
+ } else {
+ event_debug(("Detected an IPv6 interface"));
+ had_ipv6_address = 1;
+ }
+ }
+}
+
+#ifdef _WIN32
+typedef ULONG (WINAPI *GetAdaptersAddresses_fn_t)(
+ ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG);
+#endif
+
+static int
+evutil_check_ifaddrs(void)
+{
+#if defined(EVENT__HAVE_GETIFADDRS)
+ /* Most free Unixy systems provide getifaddrs, which gives us a linked list
+ * of struct ifaddrs. */
+ struct ifaddrs *ifa = NULL;
+ const struct ifaddrs *i;
+ if (getifaddrs(&ifa) < 0) {
+ event_warn("Unable to call getifaddrs()");
+ return -1;
+ }
+
+ for (i = ifa; i; i = i->ifa_next) {
+ if (!i->ifa_addr)
+ continue;
+ evutil_found_ifaddr(i->ifa_addr);
+ }
+
+ freeifaddrs(ifa);
+ return 0;
+#elif defined(_WIN32)
+ /* Windows XP began to provide GetAdaptersAddresses. Windows 2000 had a
+ "GetAdaptersInfo", but that's deprecated; let's just try
+ GetAdaptersAddresses and fall back to connect+getsockname.
+ */
+ HMODULE lib = evutil_load_windows_system_library_(TEXT("ihplapi.dll"));
+ GetAdaptersAddresses_fn_t fn;
+ ULONG size, res;
+ IP_ADAPTER_ADDRESSES *addresses = NULL, *address;
+ int result = -1;
+
+#define FLAGS (GAA_FLAG_SKIP_ANYCAST | \
+ GAA_FLAG_SKIP_MULTICAST | \
+ GAA_FLAG_SKIP_DNS_SERVER)
+
+ if (!lib)
+ goto done;
+
+ if (!(fn = (GetAdaptersAddresses_fn_t) GetProcAddress(lib, "GetAdaptersAddresses")))
+ goto done;
+
+ /* Guess how much space we need. */
+ size = 15*1024;
+ addresses = mm_malloc(size);
+ if (!addresses)
+ goto done;
+ res = fn(AF_UNSPEC, FLAGS, NULL, addresses, &size);
+ if (res == ERROR_BUFFER_OVERFLOW) {
+ /* we didn't guess that we needed enough space; try again */
+ mm_free(addresses);
+ addresses = mm_malloc(size);
+ if (!addresses)
+ goto done;
+ res = fn(AF_UNSPEC, FLAGS, NULL, addresses, &size);
+ }
+ if (res != NO_ERROR)
+ goto done;
+
+ for (address = addresses; address; address = address->Next) {
+ IP_ADAPTER_UNICAST_ADDRESS *a;
+ for (a = address->FirstUnicastAddress; a; a = a->Next) {
+ /* Yes, it's a linked list inside a linked list */
+ struct sockaddr *sa = a->Address.lpSockaddr;
+ evutil_found_ifaddr(sa);
+ }
+ }
+
+ result = 0;
+done:
+ if (lib)
+ FreeLibrary(lib);
+ if (addresses)
+ mm_free(addresses);
+ return result;
+#else
+ return -1;
+#endif
+}
+
/* Test whether we have an ipv4 interface and an ipv6 interface. Return 0 if
* the test seemed successful. */
static int
evutil_check_interfaces(int force_recheck)
{
- const char ZEROES[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
- "\x00\x00\x00\x00\x00\x00\x00\x00";
evutil_socket_t fd = -1;
struct sockaddr_in sin, sin_out;
struct sockaddr_in6 sin6, sin6_out;
ev_socklen_t sin_out_len = sizeof(sin_out);
ev_socklen_t sin6_out_len = sizeof(sin6_out);
int r;
- char buf[128];
if (have_checked_interfaces && !force_recheck)
return 0;
- /* To check whether we have an interface open for a given protocol, we
- * try to make a UDP 'connection' to a remote host on the internet.
- * We don't actually use it, so the address doesn't matter, but we
- * want to pick one that keep us from using a host- or link-local
- * interface. */
+ if (evutil_check_ifaddrs() == 0) {
+ /* Use a nice sane interface, if this system has one. */
+ return 0;
+ }
+
+ /* Ugh. There was no nice sane interface. So to check whether we have
+ * an interface open for a given protocol, will try to make a UDP
+ * 'connection' to a remote host on the internet. We don't actually
+ * use it, so the address doesn't matter, but we want to pick one that
+ * keep us from using a host- or link-local interface. */
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(53);
@@ -587,21 +764,7 @@ evutil_check_interfaces(int force_recheck)
connect(fd, (struct sockaddr*)&sin, sizeof(sin)) == 0 &&
getsockname(fd, (struct sockaddr*)&sin_out, &sin_out_len) == 0) {
/* We might have an IPv4 interface. */
- ev_uint32_t addr = ntohl(sin_out.sin_addr.s_addr);
- if (addr == 0 ||
- EVUTIL_V4ADDR_IS_LOCALHOST(addr) ||
- EVUTIL_V4ADDR_IS_CLASSD(addr)) {
- evutil_inet_ntop(AF_INET, &sin_out.sin_addr,
- buf, sizeof(buf));
- /* This is a reserved, ipv4compat, ipv4map, loopback,
- * link-local or unspecified address. The host should
- * never have given it to us; it could never connect
- * to sin. */
- event_warnx("Got a strange local ipv4 address %s",buf);
- } else {
- event_debug(("Detected an IPv4 interface"));
- had_ipv4_address = 1;
- }
+ evutil_found_ifaddr((struct sockaddr*) &sin_out);
}
if (fd >= 0)
evutil_closesocket(fd);
@@ -610,21 +773,7 @@ evutil_check_interfaces(int force_recheck)
connect(fd, (struct sockaddr*)&sin6, sizeof(sin6)) == 0 &&
getsockname(fd, (struct sockaddr*)&sin6_out, &sin6_out_len) == 0) {
/* We might have an IPv6 interface. */
- const unsigned char *addr =
- (unsigned char*)sin6_out.sin6_addr.s6_addr;
- if (!memcmp(addr, ZEROES, 8) ||
- (addr[0] == 0xfe && (addr[1] & 0xc0) == 0x80)) {
- /* This is a reserved, ipv4compat, ipv4map, loopback,
- * link-local or unspecified address. The host should
- * never have given it to us; it could never connect
- * to sin6. */
- evutil_inet_ntop(AF_INET6, &sin6_out.sin6_addr,
- buf, sizeof(buf));
- event_warnx("Got a strange local ipv6 address %s",buf);
- } else {
- event_debug(("Detected an IPv4 interface"));
- had_ipv6_address = 1;
- }
+ evutil_found_ifaddr((struct sockaddr*) &sin6_out);
}
if (fd >= 0)
@@ -645,7 +794,7 @@ evutil_check_interfaces(int force_recheck)
* allocate both a TCP and a UDP addrinfo.
*/
struct evutil_addrinfo *
-evutil_new_addrinfo(struct sockaddr *sa, ev_socklen_t socklen,
+evutil_new_addrinfo_(struct sockaddr *sa, ev_socklen_t socklen,
const struct evutil_addrinfo *hints)
{
struct evutil_addrinfo *res;
@@ -657,11 +806,11 @@ evutil_new_addrinfo(struct sockaddr *sa, ev_socklen_t socklen,
struct evutil_addrinfo tmp;
memcpy(&tmp, hints, sizeof(tmp));
tmp.ai_socktype = SOCK_STREAM; tmp.ai_protocol = IPPROTO_TCP;
- r1 = evutil_new_addrinfo(sa, socklen, &tmp);
+ r1 = evutil_new_addrinfo_(sa, socklen, &tmp);
if (!r1)
return NULL;
tmp.ai_socktype = SOCK_DGRAM; tmp.ai_protocol = IPPROTO_UDP;
- r2 = evutil_new_addrinfo(sa, socklen, &tmp);
+ r2 = evutil_new_addrinfo_(sa, socklen, &tmp);
if (!r2) {
evutil_freeaddrinfo(r1);
return NULL;
@@ -690,7 +839,7 @@ evutil_new_addrinfo(struct sockaddr *sa, ev_socklen_t socklen,
* the list. Either element can be NULL, in which case we return the element
* that is not NULL. */
struct evutil_addrinfo *
-evutil_addrinfo_append(struct evutil_addrinfo *first,
+evutil_addrinfo_append_(struct evutil_addrinfo *first,
struct evutil_addrinfo *append)
{
struct evutil_addrinfo *ai = first;
@@ -725,7 +874,7 @@ evutil_parse_servname(const char *servname, const char *protocol,
int n = parse_numeric_servname(servname);
if (n>=0)
return n;
-#if defined(_EVENT_HAVE_GETSERVBYNAME) || defined(WIN32)
+#if defined(EVENT__HAVE_GETSERVBYNAME) || defined(_WIN32)
if (!(hints->ai_flags & EVUTIL_AI_NUMERICSERV)) {
struct servent *ent = getservbyname(servname, protocol);
if (ent) {
@@ -753,7 +902,7 @@ evutil_unparse_protoname(int proto)
return "sctp";
#endif
default:
-#ifdef _EVENT_HAVE_GETPROTOBYNUMBER
+#ifdef EVENT__HAVE_GETPROTOBYNUMBER
{
struct protoent *ent = getprotobynumber(proto);
if (ent)
@@ -806,7 +955,7 @@ evutil_getaddrinfo_infer_protocols(struct evutil_addrinfo *hints)
* set *res as getaddrinfo would.
*/
int
-evutil_getaddrinfo_common(const char *nodename, const char *servname,
+evutil_getaddrinfo_common_(const char *nodename, const char *servname,
struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum)
{
int port = 0;
@@ -848,7 +997,7 @@ evutil_getaddrinfo_common(const char *nodename, const char *servname,
/* connect to ::1 */
sin6.sin6_addr.s6_addr[15] = 1;
}
- res6 = evutil_new_addrinfo((struct sockaddr*)&sin6,
+ res6 = evutil_new_addrinfo_((struct sockaddr*)&sin6,
sizeof(sin6), hints);
if (!res6)
return EVUTIL_EAI_MEMORY;
@@ -865,7 +1014,7 @@ evutil_getaddrinfo_common(const char *nodename, const char *servname,
/* connect to 127.0.0.1 */
sin.sin_addr.s_addr = htonl(0x7f000001);
}
- res4 = evutil_new_addrinfo((struct sockaddr*)&sin,
+ res4 = evutil_new_addrinfo_((struct sockaddr*)&sin,
sizeof(sin), hints);
if (!res4) {
if (res6)
@@ -873,7 +1022,7 @@ evutil_getaddrinfo_common(const char *nodename, const char *servname,
return EVUTIL_EAI_MEMORY;
}
}
- *res = evutil_addrinfo_append(res4, res6);
+ *res = evutil_addrinfo_append_(res4, res6);
return 0;
}
@@ -887,7 +1036,7 @@ evutil_getaddrinfo_common(const char *nodename, const char *servname,
/* Got an ipv6 address. */
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons(port);
- *res = evutil_new_addrinfo((struct sockaddr*)&sin6,
+ *res = evutil_new_addrinfo_((struct sockaddr*)&sin6,
sizeof(sin6), hints);
if (!*res)
return EVUTIL_EAI_MEMORY;
@@ -903,7 +1052,7 @@ evutil_getaddrinfo_common(const char *nodename, const char *servname,
/* Got an ipv6 address. */
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
- *res = evutil_new_addrinfo((struct sockaddr*)&sin,
+ *res = evutil_new_addrinfo_((struct sockaddr*)&sin,
sizeof(sin), hints);
if (!*res)
return EVUTIL_EAI_MEMORY;
@@ -922,7 +1071,7 @@ evutil_getaddrinfo_common(const char *nodename, const char *servname,
return EVUTIL_EAI_NEED_RESOLVE;
}
-#ifdef _EVENT_HAVE_GETADDRINFO
+#ifdef EVENT__HAVE_GETADDRINFO
#define USE_NATIVE_GETADDRINFO
#endif
@@ -1021,12 +1170,12 @@ addrinfo_from_hostent(const struct hostent *ent,
for (i = 0; ent->h_addr_list[i]; ++i) {
memcpy(addrp, ent->h_addr_list[i], ent->h_length);
- ai = evutil_new_addrinfo(sa, socklen, hints);
+ ai = evutil_new_addrinfo_(sa, socklen, hints);
if (!ai) {
evutil_freeaddrinfo(res);
return NULL;
}
- res = evutil_addrinfo_append(res, ai);
+ res = evutil_addrinfo_append_(res, ai);
}
if (res && ((hints->ai_flags & EVUTIL_AI_CANONNAME) && ent->h_name)) {
@@ -1046,7 +1195,7 @@ addrinfo_from_hostent(const struct hostent *ent,
* that we'll only get addresses we could maybe connect to.
*/
void
-evutil_adjust_hints_for_addrconfig(struct evutil_addrinfo *hints)
+evutil_adjust_hints_for_addrconfig_(struct evutil_addrinfo *hints)
{
if (!(hints->ai_flags & EVUTIL_AI_ADDRCONFIG))
return;
@@ -1072,7 +1221,7 @@ static int tested_for_getaddrinfo_hacks=0;
turns out that the bug is present, then:
- If nodename==NULL and servname is numeric, we build an answer
- ourselves using evutil_getaddrinfo_common().
+ ourselves using evutil_getaddrinfo_common_().
- If nodename!=NULL and servname is numeric, then we set
servname=NULL when calling getaddrinfo, and post-process the
@@ -1202,7 +1351,7 @@ evutil_getaddrinfo(const char *nodename, const char *servname,
/* Not every system has AI_ADDRCONFIG, so fake it. */
if (hints.ai_family == PF_UNSPEC &&
(hints.ai_flags & EVUTIL_AI_ADDRCONFIG)) {
- evutil_adjust_hints_for_addrconfig(&hints);
+ evutil_adjust_hints_for_addrconfig_(&hints);
}
#endif
@@ -1225,10 +1374,10 @@ evutil_getaddrinfo(const char *nodename, const char *servname,
* ever resolving even a literal IPv6 address when
* ai_addrtype is PF_UNSPEC.
*/
-#ifdef WIN32
+#ifdef _WIN32
{
int tmp_port;
- err = evutil_getaddrinfo_common(nodename,servname,&hints,
+ err = evutil_getaddrinfo_common_(nodename,servname,&hints,
res, &tmp_port);
if (err == 0 ||
err == EVUTIL_EAI_MEMORY ||
@@ -1244,7 +1393,7 @@ evutil_getaddrinfo(const char *nodename, const char *servname,
&& ((portnum=parse_numeric_servname(servname)) >= 0);
if (need_np_hack) {
if (!nodename)
- return evutil_getaddrinfo_common(
+ return evutil_getaddrinfo_common_(
NULL,servname,&hints, res, &portnum);
servname = NULL;
}
@@ -1289,9 +1438,9 @@ evutil_getaddrinfo(const char *nodename, const char *servname,
hints.ai_family = PF_UNSPEC;
}
- evutil_adjust_hints_for_addrconfig(&hints);
+ evutil_adjust_hints_for_addrconfig_(&hints);
- err = evutil_getaddrinfo_common(nodename, servname, &hints, res, &port);
+ err = evutil_getaddrinfo_common_(nodename, servname, &hints, res, &port);
if (err != EVUTIL_EAI_NEED_RESOLVE) {
/* We either succeeded or failed. No need to continue */
return err;
@@ -1300,19 +1449,19 @@ evutil_getaddrinfo(const char *nodename, const char *servname,
err = 0;
/* Use any of the various gethostbyname_r variants as available. */
{
-#ifdef _EVENT_HAVE_GETHOSTBYNAME_R_6_ARG
+#ifdef EVENT__HAVE_GETHOSTBYNAME_R_6_ARG
/* This one is what glibc provides. */
char buf[2048];
struct hostent hostent;
int r;
r = gethostbyname_r(nodename, &hostent, buf, sizeof(buf), &ent,
&err);
-#elif defined(_EVENT_HAVE_GETHOSTBYNAME_R_5_ARG)
+#elif defined(EVENT__HAVE_GETHOSTBYNAME_R_5_ARG)
char buf[2048];
struct hostent hostent;
ent = gethostbyname_r(nodename, &hostent, buf, sizeof(buf),
&err);
-#elif defined(_EVENT_HAVE_GETHOSTBYNAME_R_3_ARG)
+#elif defined(EVENT__HAVE_GETHOSTBYNAME_R_3_ARG)
struct hostent_data data;
struct hostent hostent;
memset(&data, 0, sizeof(data));
@@ -1322,7 +1471,7 @@ evutil_getaddrinfo(const char *nodename, const char *servname,
/* fall back to gethostbyname. */
/* XXXX This needs a lock everywhere but Windows. */
ent = gethostbyname(nodename);
-#ifdef WIN32
+#ifdef _WIN32
err = WSAGetLastError();
#else
err = h_errno;
@@ -1377,7 +1526,7 @@ evutil_getaddrinfo(const char *nodename, const char *servname,
void
evutil_freeaddrinfo(struct evutil_addrinfo *ai)
{
-#ifdef _EVENT_HAVE_GETADDRINFO
+#ifdef EVENT__HAVE_GETADDRINFO
if (!(ai->ai_flags & EVUTIL_AI_LIBEVENT_ALLOCATED)) {
freeaddrinfo(ai);
return;
@@ -1395,7 +1544,7 @@ evutil_freeaddrinfo(struct evutil_addrinfo *ai)
static evdns_getaddrinfo_fn evdns_getaddrinfo_impl = NULL;
void
-evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo_fn fn)
+evutil_set_evdns_getaddrinfo_fn_(evdns_getaddrinfo_fn fn)
{
if (!evdns_getaddrinfo_impl)
evdns_getaddrinfo_impl = fn;
@@ -1406,7 +1555,7 @@ evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo_fn fn)
* way that evdns_getaddrinfo would.
*/
int
-evutil_getaddrinfo_async(struct evdns_base *dns_base,
+evutil_getaddrinfo_async_(struct evdns_base *dns_base,
const char *nodename, const char *servname,
const struct evutil_addrinfo *hints_in,
void (*cb)(int, struct evutil_addrinfo *, void *), void *arg)
@@ -1458,7 +1607,7 @@ evutil_gai_strerror(int err)
case EVUTIL_EAI_SYSTEM:
return "system error";
default:
-#if defined(USE_NATIVE_GETADDRINFO) && defined(WIN32)
+#if defined(USE_NATIVE_GETADDRINFO) && defined(_WIN32)
return gai_strerrorA(err);
#elif defined(USE_NATIVE_GETADDRINFO)
return gai_strerror(err);
@@ -1468,83 +1617,180 @@ evutil_gai_strerror(int err)
}
}
-#ifdef WIN32
-#define E(code, s) { code, (s " [" #code " ]") }
-static struct { int code; const char *msg; } windows_socket_errors[] = {
- E(WSAEINTR, "Interrupted function call"),
- E(WSAEACCES, "Permission denied"),
- E(WSAEFAULT, "Bad address"),
- E(WSAEINVAL, "Invalid argument"),
- E(WSAEMFILE, "Too many open files"),
- E(WSAEWOULDBLOCK, "Resource temporarily unavailable"),
- E(WSAEINPROGRESS, "Operation now in progress"),
- E(WSAEALREADY, "Operation already in progress"),
- E(WSAENOTSOCK, "Socket operation on nonsocket"),
- E(WSAEDESTADDRREQ, "Destination address required"),
- E(WSAEMSGSIZE, "Message too long"),
- E(WSAEPROTOTYPE, "Protocol wrong for socket"),
- E(WSAENOPROTOOPT, "Bad protocol option"),
- E(WSAEPROTONOSUPPORT, "Protocol not supported"),
- E(WSAESOCKTNOSUPPORT, "Socket type not supported"),
- /* What's the difference between NOTSUPP and NOSUPPORT? :) */
- E(WSAEOPNOTSUPP, "Operation not supported"),
- E(WSAEPFNOSUPPORT, "Protocol family not supported"),
- E(WSAEAFNOSUPPORT, "Address family not supported by protocol family"),
- E(WSAEADDRINUSE, "Address already in use"),
- E(WSAEADDRNOTAVAIL, "Cannot assign requested address"),
- E(WSAENETDOWN, "Network is down"),
- E(WSAENETUNREACH, "Network is unreachable"),
- E(WSAENETRESET, "Network dropped connection on reset"),
- E(WSAECONNABORTED, "Software caused connection abort"),
- E(WSAECONNRESET, "Connection reset by peer"),
- E(WSAENOBUFS, "No buffer space available"),
- E(WSAEISCONN, "Socket is already connected"),
- E(WSAENOTCONN, "Socket is not connected"),
- E(WSAESHUTDOWN, "Cannot send after socket shutdown"),
- E(WSAETIMEDOUT, "Connection timed out"),
- E(WSAECONNREFUSED, "Connection refused"),
- E(WSAEHOSTDOWN, "Host is down"),
- E(WSAEHOSTUNREACH, "No route to host"),
- E(WSAEPROCLIM, "Too many processes"),
-
- /* Yes, some of these start with WSA, not WSAE. No, I don't know why. */
- E(WSASYSNOTREADY, "Network subsystem is unavailable"),
- E(WSAVERNOTSUPPORTED, "Winsock.dll out of range"),
- E(WSANOTINITIALISED, "Successful WSAStartup not yet performed"),
- E(WSAEDISCON, "Graceful shutdown now in progress"),
-#ifdef WSATYPE_NOT_FOUND
- E(WSATYPE_NOT_FOUND, "Class type not found"),
-#endif
- E(WSAHOST_NOT_FOUND, "Host not found"),
- E(WSATRY_AGAIN, "Nonauthoritative host not found"),
- E(WSANO_RECOVERY, "This is a nonrecoverable error"),
- E(WSANO_DATA, "Valid name, no data record of requested type)"),
-
- /* There are some more error codes whose numeric values are marked
- * <b>OS dependent</b>. They start with WSA_, apparently for the same
- * reason that practitioners of some craft traditions deliberately
- * introduce imperfections into their baskets and rugs "to allow the
- * evil spirits to escape." If we catch them, then our binaries
- * might not report consistent results across versions of Windows.
- * Thus, I'm going to let them all fall through.
- */
- { -1, NULL },
+#ifdef _WIN32
+/* destructively remove a trailing line terminator from s */
+static void
+chomp (char *s)
+{
+ size_t len;
+ if (s && (len = strlen (s)) > 0 && s[len - 1] == '\n') {
+ s[--len] = 0;
+ if (len > 0 && s[len - 1] == '\r')
+ s[--len] = 0;
+ }
+}
+
+/* FormatMessage returns allocated strings, but evutil_socket_error_to_string
+ * is supposed to return a string which is good indefinitely without having
+ * to be freed. To make this work without leaking memory, we cache the
+ * string the first time FormatMessage is called on a particular error
+ * code, and then return the cached string on subsequent calls with the
+ * same code. The strings aren't freed until libevent_global_shutdown
+ * (or never). We use a linked list to cache the errors, because we
+ * only expect there to be a few dozen, and that should be fast enough.
+ */
+
+struct cached_sock_errs_entry {
+ HT_ENTRY(cached_sock_errs_entry) node;
+ DWORD code;
+ char *msg; /* allocated with LocalAlloc; free with LocalFree */
};
-#undef E
+
+static inline unsigned
+hash_cached_sock_errs(const struct cached_sock_errs_entry *e)
+{
+ /* Use Murmur3's 32-bit finalizer as an integer hash function */
+ DWORD h = e->code;
+ h ^= h >> 16;
+ h *= 0x85ebca6b;
+ h ^= h >> 13;
+ h *= 0xc2b2ae35;
+ h ^= h >> 16;
+ return h;
+}
+
+static inline int
+eq_cached_sock_errs(const struct cached_sock_errs_entry *a,
+ const struct cached_sock_errs_entry *b)
+{
+ return a->code == b->code;
+}
+
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
+static void *windows_socket_errors_lock_ = NULL;
+#endif
+
+static HT_HEAD(cached_sock_errs_map, cached_sock_errs_entry)
+ windows_socket_errors = HT_INITIALIZER();
+
+HT_PROTOTYPE(cached_sock_errs_map,
+ cached_sock_errs_entry,
+ node,
+ hash_cached_sock_errs,
+ eq_cached_sock_errs);
+
+HT_GENERATE(cached_sock_errs_map,
+ cached_sock_errs_entry,
+ node,
+ hash_cached_sock_errs,
+ eq_cached_sock_errs,
+ 0.5,
+ mm_malloc,
+ mm_realloc,
+ mm_free);
+
/** Equivalent to strerror, but for windows socket errors. */
const char *
evutil_socket_error_to_string(int errcode)
{
- /* XXXX Is there really no built-in function to do this? */
- int i;
- for (i=0; windows_socket_errors[i].code >= 0; ++i) {
- if (errcode == windows_socket_errors[i].code)
- return windows_socket_errors[i].msg;
- }
- return strerror(errcode);
+ struct cached_sock_errs_entry *errs, *newerr, find;
+ char *msg = NULL;
+
+ EVLOCK_LOCK(windows_socket_errors_lock_, 0);
+
+ find.code = errcode;
+ errs = HT_FIND(cached_sock_errs_map, &windows_socket_errors, &find);
+ if (errs) {
+ msg = errs->msg;
+ goto done;
+ }
+
+ if (0 != FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS |
+ FORMAT_MESSAGE_ALLOCATE_BUFFER,
+ NULL, errcode, 0, (LPTSTR)&msg, 0, NULL))
+ chomp (msg); /* because message has trailing newline */
+ else {
+ size_t len = 50;
+ /* use LocalAlloc because FormatMessage does */
+ msg = LocalAlloc(LMEM_FIXED, len);
+ if (!msg) {
+ msg = (char *)"LocalAlloc failed during Winsock error";
+ goto done;
+ }
+ evutil_snprintf(msg, len, "winsock error 0x%08x", errcode);
+ }
+
+ newerr = (struct cached_sock_errs_entry *)
+ mm_malloc(sizeof (struct cached_sock_errs_entry));
+
+ if (!newerr) {
+ LocalFree(msg);
+ msg = (char *)"malloc failed during Winsock error";
+ goto done;
+ }
+
+ newerr->code = errcode;
+ newerr->msg = msg;
+ HT_INSERT(cached_sock_errs_map, &windows_socket_errors, newerr);
+
+ done:
+ EVLOCK_UNLOCK(windows_socket_errors_lock_, 0);
+
+ return msg;
+}
+
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
+int
+evutil_global_setup_locks_(const int enable_locks)
+{
+ EVTHREAD_SETUP_GLOBAL_LOCK(windows_socket_errors_lock_, 0);
+ return 0;
+}
+#endif
+
+static void
+evutil_free_sock_err_globals(void)
+{
+ struct cached_sock_errs_entry **errs, *tofree;
+
+ for (errs = HT_START(cached_sock_errs_map, &windows_socket_errors)
+ ; errs; ) {
+ tofree = *errs;
+ errs = HT_NEXT_RMV(cached_sock_errs_map,
+ &windows_socket_errors,
+ errs);
+ LocalFree(tofree->msg);
+ mm_free(tofree);
+ }
+
+ HT_CLEAR(cached_sock_errs_map, &windows_socket_errors);
+
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
+ if (windows_socket_errors_lock_ != NULL) {
+ EVTHREAD_FREE_LOCK(windows_socket_errors_lock_, 0);
+ windows_socket_errors_lock_ = NULL;
+ }
+#endif
+}
+
+#else
+
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
+int
+evutil_global_setup_locks_(const int enable_locks)
+{
+ return 0;
}
#endif
+static void
+evutil_free_sock_err_globals(void)
+{
+}
+
+#endif
+
int
evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
{
@@ -1562,7 +1808,7 @@ evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap)
int r;
if (!buflen)
return 0;
-#if defined(_MSC_VER) || defined(WIN32)
+#if defined(_MSC_VER) || defined(_WIN32)
r = _vsnprintf(buf, buflen, format, ap);
if (r < 0)
r = _vscprintf(format, ap);
@@ -1586,7 +1832,7 @@ evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap)
const char *
evutil_inet_ntop(int af, const void *src, char *dst, size_t len)
{
-#if defined(_EVENT_HAVE_INET_NTOP) && !defined(USE_INTERNAL_NTOP)
+#if defined(EVENT__HAVE_INET_NTOP) && !defined(USE_INTERNAL_NTOP)
return inet_ntop(af, src, dst, len);
#else
if (af == AF_INET) {
@@ -1682,19 +1928,19 @@ evutil_inet_ntop(int af, const void *src, char *dst, size_t len)
int
evutil_inet_pton(int af, const char *src, void *dst)
{
-#if defined(_EVENT_HAVE_INET_PTON) && !defined(USE_INTERNAL_PTON)
+#if defined(EVENT__HAVE_INET_PTON) && !defined(USE_INTERNAL_PTON)
return inet_pton(af, src, dst);
#else
if (af == AF_INET) {
- int a,b,c,d;
+ unsigned a,b,c,d;
char more;
struct in_addr *addr = dst;
- if (sscanf(src, "%d.%d.%d.%d%c", &a,&b,&c,&d,&more) != 4)
+ if (sscanf(src, "%u.%u.%u.%u%c", &a,&b,&c,&d,&more) != 4)
return 0;
- if (a < 0 || a > 255) return 0;
- if (b < 0 || b > 255) return 0;
- if (c < 0 || c > 255) return 0;
- if (d < 0 || d > 255) return 0;
+ if (a > 255) return 0;
+ if (b > 255) return 0;
+ if (c > 255) return 0;
+ if (d > 255) return 0;
addr->s_addr = htonl((a<<24) | (b<<16) | (c<<8) | d);
return 1;
#ifdef AF_INET6
@@ -1709,22 +1955,22 @@ evutil_inet_pton(int af, const char *src, void *dst)
else if (!dot)
eow = src+strlen(src);
else {
- int byte1,byte2,byte3,byte4;
+ unsigned byte1,byte2,byte3,byte4;
char more;
- for (eow = dot-1; eow >= src && EVUTIL_ISDIGIT(*eow); --eow)
+ for (eow = dot-1; eow >= src && EVUTIL_ISDIGIT_(*eow); --eow)
;
++eow;
/* We use "scanf" because some platform inet_aton()s are too lax
* about IPv4 addresses of the form "1.2.3" */
- if (sscanf(eow, "%d.%d.%d.%d%c",
+ if (sscanf(eow, "%u.%u.%u.%u%c",
&byte1,&byte2,&byte3,&byte4,&more) != 4)
return 0;
- if (byte1 > 255 || byte1 < 0 ||
- byte2 > 255 || byte2 < 0 ||
- byte3 > 255 || byte3 < 0 ||
- byte4 > 255 || byte4 < 0)
+ if (byte1 > 255 ||
+ byte2 > 255 ||
+ byte3 > 255 ||
+ byte4 > 255)
return 0;
words[6] = (byte1<<8) | byte2;
@@ -1736,7 +1982,7 @@ evutil_inet_pton(int af, const char *src, void *dst)
while (src < eow) {
if (i > 7)
return 0;
- if (EVUTIL_ISXDIGIT(*src)) {
+ if (EVUTIL_ISXDIGIT_(*src)) {
char *next;
long r = strtol(src, &next, 16);
if (next > 4+src)
@@ -1859,7 +2105,7 @@ evutil_parse_sockaddr_port(const char *ip_as_string, struct sockaddr *out, int *
{
struct sockaddr_in6 sin6;
memset(&sin6, 0, sizeof(sin6));
-#ifdef _EVENT_HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
+#ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
sin6.sin6_len = sizeof(sin6);
#endif
sin6.sin6_family = AF_INET6;
@@ -1878,7 +2124,7 @@ evutil_parse_sockaddr_port(const char *ip_as_string, struct sockaddr *out, int *
{
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
-#ifdef _EVENT_HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
+#ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
sin.sin_len = sizeof(sin);
#endif
sin.sin_family = AF_INET;
@@ -1895,7 +2141,7 @@ evutil_parse_sockaddr_port(const char *ip_as_string, struct sockaddr *out, int *
}
const char *
-evutil_format_sockaddr_port(const struct sockaddr *sa, char *out, size_t outlen)
+evutil_format_sockaddr_port_(const struct sockaddr *sa, char *out, size_t outlen)
{
char b[128];
const char *res=NULL;
@@ -2019,7 +2265,7 @@ static const unsigned char EVUTIL_TOLOWER_TABLE[256] = {
};
#define IMPL_CTYPE_FN(name) \
- int EVUTIL_##name(char c) { \
+ int EVUTIL_##name##_(char c) { \
ev_uint8_t u = c; \
return !!(EVUTIL_##name##_TABLE[(u >> 5) & 7] & (1 << (u & 31))); \
}
@@ -2032,11 +2278,11 @@ IMPL_CTYPE_FN(ISPRINT)
IMPL_CTYPE_FN(ISLOWER)
IMPL_CTYPE_FN(ISUPPER)
-char EVUTIL_TOLOWER(char c)
+char EVUTIL_TOLOWER_(char c)
{
return ((char)EVUTIL_TOLOWER_TABLE[(ev_uint8_t)c]);
}
-char EVUTIL_TOUPPER(char c)
+char EVUTIL_TOUPPER_(char c)
{
return ((char)EVUTIL_TOUPPER_TABLE[(ev_uint8_t)c]);
}
@@ -2045,8 +2291,8 @@ evutil_ascii_strcasecmp(const char *s1, const char *s2)
{
char c1, c2;
while (1) {
- c1 = EVUTIL_TOLOWER(*s1++);
- c2 = EVUTIL_TOLOWER(*s2++);
+ c1 = EVUTIL_TOLOWER_(*s1++);
+ c2 = EVUTIL_TOLOWER_(*s2++);
if (c1 < c2)
return -1;
else if (c1 > c2)
@@ -2059,8 +2305,8 @@ int evutil_ascii_strncasecmp(const char *s1, const char *s2, size_t n)
{
char c1, c2;
while (n--) {
- c1 = EVUTIL_TOLOWER(*s1++);
- c2 = EVUTIL_TOLOWER(*s2++);
+ c1 = EVUTIL_TOLOWER_(*s1++);
+ c2 = EVUTIL_TOLOWER_(*s2++);
if (c1 < c2)
return -1;
else if (c1 > c2)
@@ -2071,18 +2317,39 @@ int evutil_ascii_strncasecmp(const char *s1, const char *s2, size_t n)
return 0;
}
+void
+evutil_rtrim_lws_(char *str)
+{
+ char *cp;
+
+ if (str == NULL)
+ return;
+
+ if ((cp = strchr(str, '\0')) == NULL || (cp == str))
+ return;
+
+ --cp;
+
+ while (*cp == ' ' || *cp == '\t') {
+ *cp = '\0';
+ if (cp == str)
+ break;
+ --cp;
+ }
+}
+
static int
evutil_issetugid(void)
{
-#ifdef _EVENT_HAVE_ISSETUGID
+#ifdef EVENT__HAVE_ISSETUGID
return issetugid();
#else
-#ifdef _EVENT_HAVE_GETEUID
+#ifdef EVENT__HAVE_GETEUID
if (getuid() != geteuid())
return 1;
#endif
-#ifdef _EVENT_HAVE_GETEGID
+#ifdef EVENT__HAVE_GETEGID
if (getgid() != getegid())
return 1;
#endif
@@ -2091,7 +2358,7 @@ evutil_issetugid(void)
}
const char *
-evutil_getenv(const char *varname)
+evutil_getenv_(const char *varname)
{
if (evutil_issetugid())
return NULL;
@@ -2099,14 +2366,52 @@ evutil_getenv(const char *varname)
return getenv(varname);
}
-long
-_evutil_weakrand(void)
+ev_uint32_t
+evutil_weakrand_seed_(struct evutil_weakrand_state *state, ev_uint32_t seed)
{
-#ifdef WIN32
- return rand();
+ if (seed == 0) {
+ struct timeval tv;
+ evutil_gettimeofday(&tv, NULL);
+ seed = (ev_uint32_t)tv.tv_sec + (ev_uint32_t)tv.tv_usec;
+#ifdef _WIN32
+ seed += (ev_uint32_t) _getpid();
#else
- return random();
+ seed += (ev_uint32_t) getpid();
#endif
+ }
+ state->seed = seed;
+ return seed;
+}
+
+ev_int32_t
+evutil_weakrand_(struct evutil_weakrand_state *state)
+{
+ /* This RNG implementation is a linear congruential generator, with
+ * modulus 2^31, multiplier 1103515245, and addend 12345. It's also
+ * used by OpenBSD, and by Glibc's TYPE_0 RNG.
+ *
+ * The linear congruential generator is not an industrial-strength
+ * RNG! It's fast, but it can have higher-order patterns. Notably,
+ * the low bits tend to have periodicity.
+ */
+ state->seed = ((state->seed) * 1103515245 + 12345) & 0x7fffffff;
+ return (ev_int32_t)(state->seed);
+}
+
+ev_int32_t
+evutil_weakrand_range_(struct evutil_weakrand_state *state, ev_int32_t top)
+{
+ ev_int32_t divisor, result;
+
+ /* We can't just do weakrand() % top, since the low bits of the LCG
+ * are less random than the high ones. (Specifically, since the LCG
+ * modulus is 2^N, every 2^m for m<N will divide the modulus, and so
+ * therefore the low m bits of the LCG will have period 2^m.) */
+ divisor = EVUTIL_WEAKRAND_MAX / top;
+ do {
+ result = evutil_weakrand_(state) / divisor;
+ } while (result >= top);
+ return result;
}
/**
@@ -2122,7 +2427,7 @@ evutil_memclear_(void *mem, size_t len)
}
int
-evutil_sockaddr_is_loopback(const struct sockaddr *addr)
+evutil_sockaddr_is_loopback_(const struct sockaddr *addr)
{
static const char LOOPBACK_S6[16] =
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1";
@@ -2136,20 +2441,8 @@ evutil_sockaddr_is_loopback(const struct sockaddr *addr)
return 0;
}
-#define MAX_SECONDS_IN_MSEC_LONG \
- (((LONG_MAX) - 999) / 1000)
-
-long
-evutil_tv_to_msec(const struct timeval *tv)
-{
- if (tv->tv_usec > 1000000 || tv->tv_sec > MAX_SECONDS_IN_MSEC_LONG)
- return -1;
-
- return (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);
-}
-
int
-evutil_hex_char_to_int(char c)
+evutil_hex_char_to_int_(char c)
{
switch(c)
{
@@ -2173,9 +2466,9 @@ evutil_hex_char_to_int(char c)
return -1;
}
-#ifdef WIN32
-HANDLE
-evutil_load_windows_system_library(const TCHAR *library_name)
+#ifdef _WIN32
+HMODULE
+evutil_load_windows_system_library_(const TCHAR *library_name)
{
TCHAR path[MAX_PATH];
unsigned n;
@@ -2188,3 +2481,183 @@ evutil_load_windows_system_library(const TCHAR *library_name)
}
#endif
+/* Internal wrapper around 'socket' to provide Linux-style support for
+ * syscall-saving methods where available.
+ *
+ * In addition to regular socket behavior, you can use a bitwise or to set the
+ * flags EVUTIL_SOCK_NONBLOCK and EVUTIL_SOCK_CLOEXEC in the 'type' argument,
+ * to make the socket nonblocking or close-on-exec with as few syscalls as
+ * possible.
+ */
+evutil_socket_t
+evutil_socket_(int domain, int type, int protocol)
+{
+ evutil_socket_t r;
+#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
+ r = socket(domain, type, protocol);
+ if (r >= 0)
+ return r;
+ else if ((type & (SOCK_NONBLOCK|SOCK_CLOEXEC)) == 0)
+ return -1;
+#endif
+#define SOCKET_TYPE_MASK (~(EVUTIL_SOCK_NONBLOCK|EVUTIL_SOCK_CLOEXEC))
+ r = socket(domain, type & SOCKET_TYPE_MASK, protocol);
+ if (r < 0)
+ return -1;
+ if (type & EVUTIL_SOCK_NONBLOCK) {
+ if (evutil_fast_socket_nonblocking(r) < 0) {
+ evutil_closesocket(r);
+ return -1;
+ }
+ }
+ if (type & EVUTIL_SOCK_CLOEXEC) {
+ if (evutil_fast_socket_closeonexec(r) < 0) {
+ evutil_closesocket(r);
+ return -1;
+ }
+ }
+ return r;
+}
+
+/* Internal wrapper around 'accept' or 'accept4' to provide Linux-style
+ * support for syscall-saving methods where available.
+ *
+ * In addition to regular accept behavior, you can set one or more of flags
+ * EVUTIL_SOCK_NONBLOCK and EVUTIL_SOCK_CLOEXEC in the 'flags' argument, to
+ * make the socket nonblocking or close-on-exec with as few syscalls as
+ * possible.
+ */
+evutil_socket_t
+evutil_accept4_(evutil_socket_t sockfd, struct sockaddr *addr,
+ ev_socklen_t *addrlen, int flags)
+{
+ evutil_socket_t result;
+#if defined(EVENT__HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
+ result = accept4(sockfd, addr, addrlen, flags);
+ if (result >= 0 || (errno != EINVAL && errno != ENOSYS)) {
+ /* A nonnegative result means that we succeeded, so return.
+ * Failing with EINVAL means that an option wasn't supported,
+ * and failing with ENOSYS means that the syscall wasn't
+ * there: in those cases we want to fall back. Otherwise, we
+ * got a real error, and we should return. */
+ return result;
+ }
+#endif
+ result = accept(sockfd, addr, addrlen);
+ if (result < 0)
+ return result;
+
+ if (flags & EVUTIL_SOCK_CLOEXEC) {
+ if (evutil_fast_socket_closeonexec(result) < 0) {
+ evutil_closesocket(result);
+ return -1;
+ }
+ }
+ if (flags & EVUTIL_SOCK_NONBLOCK) {
+ if (evutil_fast_socket_nonblocking(result) < 0) {
+ evutil_closesocket(result);
+ return -1;
+ }
+ }
+ return result;
+}
+
+/* Internal function: Set fd[0] and fd[1] to a pair of fds such that writes on
+ * fd[0] get read from fd[1]. Make both fds nonblocking and close-on-exec.
+ * Return 0 on success, -1 on failure.
+ */
+int
+evutil_make_internal_pipe_(evutil_socket_t fd[2])
+{
+ /*
+ Making the second socket nonblocking is a bit subtle, given that we
+ ignore any EAGAIN returns when writing to it, and you don't usally
+ do that for a nonblocking socket. But if the kernel gives us EAGAIN,
+ then there's no need to add any more data to the buffer, since
+ the main thread is already either about to wake up and drain it,
+ or woken up and in the process of draining it.
+ */
+
+#if defined(EVENT__HAVE_PIPE2)
+ if (pipe2(fd, O_NONBLOCK|O_CLOEXEC) == 0)
+ return 0;
+#endif
+#if defined(EVENT__HAVE_PIPE)
+ if (pipe(fd) == 0) {
+ if (evutil_fast_socket_nonblocking(fd[0]) < 0 ||
+ evutil_fast_socket_nonblocking(fd[1]) < 0 ||
+ evutil_fast_socket_closeonexec(fd[0]) < 0 ||
+ evutil_fast_socket_closeonexec(fd[1]) < 0) {
+ close(fd[0]);
+ close(fd[1]);
+ fd[0] = fd[1] = -1;
+ return -1;
+ }
+ return 0;
+ } else {
+ event_warn("%s: pipe", __func__);
+ }
+#endif
+
+#ifdef _WIN32
+#define LOCAL_SOCKETPAIR_AF AF_INET
+#else
+#define LOCAL_SOCKETPAIR_AF AF_UNIX
+#endif
+ if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, fd) == 0) {
+ if (evutil_fast_socket_nonblocking(fd[0]) < 0 ||
+ evutil_fast_socket_nonblocking(fd[1]) < 0 ||
+ evutil_fast_socket_closeonexec(fd[0]) < 0 ||
+ evutil_fast_socket_closeonexec(fd[1]) < 0) {
+ evutil_closesocket(fd[0]);
+ evutil_closesocket(fd[1]);
+ fd[0] = fd[1] = -1;
+ return -1;
+ }
+ return 0;
+ }
+ fd[0] = fd[1] = -1;
+ return -1;
+}
+
+/* Wrapper around eventfd on systems that provide it. Unlike the system
+ * eventfd, it always supports EVUTIL_EFD_CLOEXEC and EVUTIL_EFD_NONBLOCK as
+ * flags. Returns -1 on error or if eventfd is not supported.
+ */
+evutil_socket_t
+evutil_eventfd_(unsigned initval, int flags)
+{
+#if defined(EVENT__HAVE_EVENTFD) && defined(EVENT__HAVE_SYS_EVENTFD_H)
+ int r;
+#if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
+ r = eventfd(initval, flags);
+ if (r >= 0 || flags == 0)
+ return r;
+#endif
+ r = eventfd(initval, 0);
+ if (r < 0)
+ return r;
+ if (flags & EVUTIL_EFD_CLOEXEC) {
+ if (evutil_fast_socket_closeonexec(r) < 0) {
+ evutil_closesocket(r);
+ return -1;
+ }
+ }
+ if (flags & EVUTIL_EFD_NONBLOCK) {
+ if (evutil_fast_socket_nonblocking(r) < 0) {
+ evutil_closesocket(r);
+ return -1;
+ }
+ }
+ return r;
+#else
+ return -1;
+#endif
+}
+
+void
+evutil_free_globals_(void)
+{
+ evutil_free_secure_rng_globals_();
+ evutil_free_sock_err_globals();
+}
diff --git a/evutil_rand.c b/evutil_rand.c
index 284341cf..046a14b0 100644
--- a/evutil_rand.c
+++ b/evutil_rand.c
@@ -33,13 +33,14 @@
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
#include <limits.h>
#include "util-internal.h"
#include "evthread-internal.h"
-#ifdef _EVENT_HAVE_ARC4RANDOM
+#ifdef EVENT__HAVE_ARC4RANDOM
#include <stdlib.h>
#include <string.h>
int
@@ -55,21 +56,28 @@ evutil_secure_rng_init(void)
(void) arc4random();
return 0;
}
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
int
evutil_secure_rng_global_setup_locks_(const int enable_locks)
{
return 0;
}
+#endif
+static void
+evutil_free_secure_rng_globals_locks(void)
+{
+}
static void
ev_arc4random_buf(void *buf, size_t n)
{
-#if defined(_EVENT_HAVE_ARC4RANDOM_BUF) && !defined(__APPLE__)
- return arc4random_buf(buf, n);
+#if defined(EVENT__HAVE_ARC4RANDOM_BUF) && !defined(__APPLE__)
+ arc4random_buf(buf, n);
+ return;
#else
unsigned char *b = buf;
-#if defined(_EVENT_HAVE_ARC4RANDOM_BUF)
+#if defined(EVENT__HAVE_ARC4RANDOM_BUF)
/* OSX 10.7 introducd arc4random_buf, so if you build your program
* there, you'll get surprised when older versions of OSX fail to run.
* To solve this, we can check whether the function pointer is set,
@@ -80,7 +88,8 @@ ev_arc4random_buf(void *buf, size_t n)
void (*tptr)(void *,size_t) =
(void (*)(void*,size_t))arc4random_buf;
if (tptr != NULL) {
- return arc4random_buf(buf, n);
+ arc4random_buf(buf, n);
+ return;
}
}
#endif
@@ -105,15 +114,15 @@ ev_arc4random_buf(void *buf, size_t n)
#endif
}
-#else /* !_EVENT_HAVE_ARC4RANDOM { */
+#else /* !EVENT__HAVE_ARC4RANDOM { */
-#ifdef _EVENT_ssize_t
-#define ssize_t _EVENT_SSIZE_t
+#ifdef EVENT__ssize_t
+#define ssize_t EVENT__ssize_t
#endif
#define ARC4RANDOM_EXPORT static
-#define _ARC4_LOCK() EVLOCK_LOCK(arc4rand_lock, 0)
-#define _ARC4_UNLOCK() EVLOCK_UNLOCK(arc4rand_lock, 0)
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#define ARC4_LOCK_() EVLOCK_LOCK(arc4rand_lock, 0)
+#define ARC4_UNLOCK_() EVLOCK_UNLOCK(arc4rand_lock, 0)
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
static void *arc4rand_lock;
#endif
@@ -124,7 +133,7 @@ static void *arc4rand_lock;
#include "./arc4random.c"
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
int
evutil_secure_rng_global_setup_locks_(const int enable_locks)
{
@@ -133,13 +142,25 @@ evutil_secure_rng_global_setup_locks_(const int enable_locks)
}
#endif
+static void
+evutil_free_secure_rng_globals_locks(void)
+{
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
+ if (arc4rand_lock != NULL) {
+ EVTHREAD_FREE_LOCK(arc4rand_lock, 0);
+ arc4rand_lock = NULL;
+ }
+#endif
+ return;
+}
+
int
evutil_secure_rng_set_urandom_device_file(char *fname)
{
#ifdef TRY_SEED_URANDOM
- _ARC4_LOCK();
+ ARC4_LOCK_();
arc4random_urandom_filename = fname;
- _ARC4_UNLOCK();
+ ARC4_UNLOCK_();
#endif
return 0;
}
@@ -149,11 +170,11 @@ evutil_secure_rng_init(void)
{
int val;
- _ARC4_LOCK();
+ ARC4_LOCK_();
if (!arc4_seeded_ok)
arc4_stir();
val = arc4_seeded_ok ? 0 : -1;
- _ARC4_UNLOCK();
+ ARC4_UNLOCK_();
return val;
}
@@ -163,7 +184,7 @@ ev_arc4random_buf(void *buf, size_t n)
arc4random_buf(buf, n);
}
-#endif /* } !_EVENT_HAVE_ARC4RANDOM */
+#endif /* } !EVENT__HAVE_ARC4RANDOM */
void
evutil_secure_rng_get_bytes(void *buf, size_t n)
@@ -178,3 +199,8 @@ evutil_secure_rng_add_bytes(const char *buf, size_t n)
n>(size_t)INT_MAX ? INT_MAX : (int)n);
}
+void
+evutil_free_secure_rng_globals_(void)
+{
+ evutil_free_secure_rng_globals_locks();
+}
diff --git a/evutil_time.c b/evutil_time.c
new file mode 100644
index 00000000..e433043e
--- /dev/null
+++ b/evutil_time.c
@@ -0,0 +1,488 @@
+/*
+ * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "event2/event-config.h"
+#include "evconfig-private.h"
+
+#ifdef _WIN32
+#include <winsock2.h>
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#undef WIN32_LEAN_AND_MEAN
+#endif
+
+#include <sys/types.h>
+#ifdef EVENT__HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#include <errno.h>
+#include <limits.h>
+#ifndef EVENT__HAVE_GETTIMEOFDAY
+#include <sys/timeb.h>
+#endif
+#if !defined(EVENT__HAVE_NANOSLEEP) && !defined(EVENT_HAVE_USLEEP) && \
+ !defined(_WIN32)
+#include <sys/select.h>
+#endif
+#include <time.h>
+#include <sys/stat.h>
+#include <string.h>
+
+#include "event2/util.h"
+#include "util-internal.h"
+#include "log-internal.h"
+
+#ifndef EVENT__HAVE_GETTIMEOFDAY
+/* No gettimeofday; this must be windows. */
+int
+evutil_gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+#ifdef _MSC_VER
+#define U64_LITERAL(n) n##ui64
+#else
+#define U64_LITERAL(n) n##llu
+#endif
+
+ /* Conversion logic taken from Tor, which in turn took it
+ * from Perl. GetSystemTimeAsFileTime returns its value as
+ * an unaligned (!) 64-bit value containing the number of
+ * 100-nanosecond intervals since 1 January 1601 UTC. */
+#define EPOCH_BIAS U64_LITERAL(116444736000000000)
+#define UNITS_PER_SEC U64_LITERAL(10000000)
+#define USEC_PER_SEC U64_LITERAL(1000000)
+#define UNITS_PER_USEC U64_LITERAL(10)
+ union {
+ FILETIME ft_ft;
+ ev_uint64_t ft_64;
+ } ft;
+
+ if (tv == NULL)
+ return -1;
+
+ GetSystemTimeAsFileTime(&ft.ft_ft);
+
+ if (EVUTIL_UNLIKELY(ft.ft_64 < EPOCH_BIAS)) {
+ /* Time before the unix epoch. */
+ return -1;
+ }
+ ft.ft_64 -= EPOCH_BIAS;
+ tv->tv_sec = (long) (ft.ft_64 / UNITS_PER_SEC);
+ tv->tv_usec = (long) ((ft.ft_64 / UNITS_PER_USEC) % USEC_PER_SEC);
+ return 0;
+}
+#endif
+
+#define MAX_SECONDS_IN_MSEC_LONG \
+ (((LONG_MAX) - 999) / 1000)
+
+long
+evutil_tv_to_msec_(const struct timeval *tv)
+{
+ if (tv->tv_usec > 1000000 || tv->tv_sec > MAX_SECONDS_IN_MSEC_LONG)
+ return -1;
+
+ return (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);
+}
+
+/*
+ Replacement for usleep on platforms that don't have one. Not guaranteed to
+ be any more finegrained than 1 msec.
+ */
+void
+evutil_usleep_(const struct timeval *tv)
+{
+ if (!tv)
+ return;
+#if defined(_WIN32)
+ {
+ long msec = evutil_tv_to_msec_(tv);
+ Sleep((DWORD)msec);
+ }
+#elif defined(EVENT__HAVE_NANOSLEEP)
+ {
+ struct timespec ts;
+ ts.tv_sec = tv->tv_sec;
+ ts.tv_nsec = tv->tv_usec*1000;
+ nanosleep(&ts, NULL);
+ }
+#elif defined(EVENT__HAVE_USLEEP)
+ /* Some systems don't like to usleep more than 999999 usec */
+ sleep(tv->tv_sec);
+ usleep(tv->tv_usec);
+#else
+ select(0, NULL, NULL, NULL, tv);
+#endif
+}
+
+/*
+ This function assumes it's called repeatedly with a
+ not-actually-so-monotonic time source whose outputs are in 'tv'. It
+ implements a trivial ratcheting mechanism so that the values never go
+ backwards.
+ */
+static void
+adjust_monotonic_time(struct evutil_monotonic_timer *base,
+ struct timeval *tv)
+{
+ evutil_timeradd(tv, &base->adjust_monotonic_clock, tv);
+
+ if (evutil_timercmp(tv, &base->last_time, <)) {
+ /* Guess it wasn't monotonic after all. */
+ struct timeval adjust;
+ evutil_timersub(&base->last_time, tv, &adjust);
+ evutil_timeradd(&adjust, &base->adjust_monotonic_clock,
+ &base->adjust_monotonic_clock);
+ *tv = base->last_time;
+ }
+ base->last_time = *tv;
+}
+
+#if defined(HAVE_POSIX_MONOTONIC)
+/* =====
+ The POSIX clock_gettime() interface provides a few ways to get at a
+ monotonic clock. CLOCK_MONOTONIC is most widely supported. Linux also
+ provides a CLOCK_MONOTONIC_COARSE with accuracy of about 1-4 msec.
+
+ On all platforms I'm aware of, CLOCK_MONOTONIC really is monotonic.
+ Platforms don't agree about whether it should jump on a sleep/resume.
+ */
+
+int
+evutil_configure_monotonic_time_(struct evutil_monotonic_timer *base,
+ int flags)
+{
+ /* CLOCK_MONOTONIC exists on FreeBSD, Linux, and Solaris. You need to
+ * check for it at runtime, because some older kernel versions won't
+ * have it working. */
+#ifdef CLOCK_MONOTONIC_COARSE
+ const int precise = flags & EV_MONOT_PRECISE;
+#endif
+ const int fallback = flags & EV_MONOT_FALLBACK;
+ struct timespec ts;
+
+#ifdef CLOCK_MONOTONIC_COARSE
+ if (CLOCK_MONOTONIC_COARSE < 0) {
+ /* Technically speaking, nothing keeps CLOCK_* from being
+ * negative (as far as I know). This check and the one below
+ * make sure that it's safe for us to use -1 as an "unset"
+ * value. */
+ event_errx(1,"I didn't expect CLOCK_MONOTONIC_COARSE to be < 0");
+ }
+ if (! precise && ! fallback) {
+ if (clock_gettime(CLOCK_MONOTONIC_COARSE, &ts) == 0) {
+ base->monotonic_clock = CLOCK_MONOTONIC_COARSE;
+ return 0;
+ }
+ }
+#endif
+ if (!fallback && clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
+ base->monotonic_clock = CLOCK_MONOTONIC;
+ return 0;
+ }
+
+ if (CLOCK_MONOTONIC < 0) {
+ event_errx(1,"I didn't expect CLOCK_MONOTONIC to be < 0");
+ }
+
+ base->monotonic_clock = -1;
+ return 0;
+}
+
+int
+evutil_gettime_monotonic_(struct evutil_monotonic_timer *base,
+ struct timeval *tp)
+{
+ struct timespec ts;
+
+ if (base->monotonic_clock < 0) {
+ if (evutil_gettimeofday(tp, NULL) < 0)
+ return -1;
+ adjust_monotonic_time(base, tp);
+ return 0;
+ }
+
+ if (clock_gettime(base->monotonic_clock, &ts) == -1)
+ return -1;
+ tp->tv_sec = ts.tv_sec;
+ tp->tv_usec = ts.tv_nsec / 1000;
+
+ return 0;
+}
+#endif
+
+#if defined(HAVE_MACH_MONOTONIC)
+/* ======
+ Apple is a little late to the POSIX party. And why not? Instead of
+ clock_gettime(), they provide mach_absolute_time(). Its units are not
+ fixed; we need to use mach_timebase_info() to get the right functions to
+ convert its units into nanoseconds.
+
+ To all appearances, mach_absolute_time() seems to be honest-to-goodness
+ monotonic. Whether it stops during sleep or not is unspecified in
+ principle, and dependent on CPU architecture in practice.
+ */
+
+int
+evutil_configure_monotonic_time_(struct evutil_monotonic_timer *base,
+ int flags)
+{
+ const int fallback = flags & EV_MONOT_FALLBACK;
+ struct mach_timebase_info mi;
+ memset(base, 0, sizeof(*base));
+ /* OSX has mach_absolute_time() */
+ if (!fallback &&
+ mach_timebase_info(&mi) == 0 &&
+ mach_absolute_time() != 0) {
+ /* mach_timebase_info tells us how to convert
+ * mach_absolute_time() into nanoseconds, but we
+ * want to use microseconds instead. */
+ mi.denom *= 1000;
+ memcpy(&base->mach_timebase_units, &mi, sizeof(mi));
+ } else {
+ base->mach_timebase_units.numer = 0;
+ }
+ return 0;
+}
+
+int
+evutil_gettime_monotonic_(struct evutil_monotonic_timer *base,
+ struct timeval *tp)
+{
+ ev_uint64_t abstime, usec;
+ if (base->mach_timebase_units.numer == 0) {
+ if (evutil_gettimeofday(tp, NULL) < 0)
+ return -1;
+ adjust_monotonic_time(base, tp);
+ return 0;
+ }
+
+ abstime = mach_absolute_time();
+ usec = (abstime * base->mach_timebase_units.numer)
+ / (base->mach_timebase_units.denom);
+ tp->tv_sec = usec / 1000000;
+ tp->tv_usec = usec % 1000000;
+
+ return 0;
+}
+#endif
+
+#if defined(HAVE_WIN32_MONOTONIC)
+/* =====
+ Turn we now to Windows. Want monontonic time on Windows?
+
+ Windows has QueryPerformanceCounter(), which gives time most high-
+ resolution time. It's a pity it's not so monotonic in practice; it's
+ also got some fun bugs, especially: with older Windowses, under
+ virtualizations, with funny hardware, on multiprocessor systems, and so
+ on. PEP418 [1] has a nice roundup of the issues here.
+
+ There's GetTickCount64() on Vista and later, which gives a number of 1-msec
+ ticks since startup. The accuracy here might be as bad as 10-20 msec, I
+ hear. There's an undocumented function (NtSetTimerResolution) that
+ allegedly increases the accuracy. Good luck!
+
+ There's also GetTickCount(), which is only 32 bits, but seems to be
+ supported on pre-Vista versions of Windows. Apparently, you can coax
+ another 14 bits out of it, giving you 2231 years before rollover.
+
+ The less said about timeGetTime() the better.
+
+ "We don't care. We don't have to. We're the Phone Company."
+ -- Lily Tomlin, SNL
+
+ Our strategy, if precise timers are turned off, is to just use the best
+ GetTickCount equivalent available. If we've been asked for precise timing,
+ then we mostly[2] assume that GetTickCount is monotonic, and correct
+ GetPerformanceCounter to approximate it.
+
+ [1] http://www.python.org/dev/peps/pep-0418
+ [2] Of course, we feed the Windows stuff into adjust_monotonic_time()
+ anyway, just in case it isn't.
+
+ */
+/*
+ Parts of our logic in the win32 timer code here are closely based on
+ BitTorrent's libUTP library. That code is subject to the following
+ license:
+
+ Copyright (c) 2010 BitTorrent, Inc.
+
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+static ev_uint64_t
+evutil_GetTickCount_(struct evutil_monotonic_timer *base)
+{
+ if (base->GetTickCount64_fn) {
+ /* Let's just use GetTickCount64 if we can. */
+ return base->GetTickCount64_fn();
+ } else if (base->GetTickCount_fn) {
+ /* Greg Hazel assures me that this works, that BitTorrent has
+ * done it for years, and this it won't turn around and
+ * bite us. He says they found it on some game programmers'
+ * forum some time around 2007.
+ */
+ ev_uint64_t v = base->GetTickCount_fn();
+ return (DWORD)v | ((v >> 18) & 0xFFFFFFFF00000000);
+ } else {
+ /* Here's the fallback implementation. We have to use
+ * GetTickCount() with its given signature, so we only get
+ * 32 bits worth of milliseconds, which will roll ove every
+ * 49 days or so. */
+ DWORD ticks = GetTickCount();
+ if (ticks < base->last_tick_count) {
+ base->adjust_tick_count += ((ev_uint64_t)1) << 32;
+ }
+ base->last_tick_count = ticks;
+ return ticks + base->adjust_tick_count;
+ }
+}
+
+int
+evutil_configure_monotonic_time_(struct evutil_monotonic_timer *base,
+ int flags)
+{
+ const int precise = flags & EV_MONOT_PRECISE;
+ const int fallback = flags & EV_MONOT_FALLBACK;
+ HANDLE h;
+ memset(base, 0, sizeof(*base));
+
+ h = evutil_load_windows_system_library_(TEXT("kernel32.dll"));
+ if (h != NULL && !fallback) {
+ base->GetTickCount64_fn = (ev_GetTickCount_func)GetProcAddress(h, "GetTickCount64");
+ base->GetTickCount_fn = (ev_GetTickCount_func)GetProcAddress(h, "GetTickCount");
+ }
+
+ base->first_tick = base->last_tick_count = evutil_GetTickCount_(base);
+ if (precise && !fallback) {
+ LARGE_INTEGER freq;
+ if (QueryPerformanceFrequency(&freq)) {
+ LARGE_INTEGER counter;
+ QueryPerformanceCounter(&counter);
+ base->first_counter = counter.QuadPart;
+ base->usec_per_count = 1.0e6 / freq.QuadPart;
+ base->use_performance_counter = 1;
+ }
+ }
+
+ return 0;
+}
+
+static inline ev_int64_t
+abs64(ev_int64_t i)
+{
+ return i < 0 ? -i : i;
+}
+
+
+int
+evutil_gettime_monotonic_(struct evutil_monotonic_timer *base,
+ struct timeval *tp)
+{
+ ev_uint64_t ticks = evutil_GetTickCount_(base);
+ if (base->use_performance_counter) {
+ /* Here's a trick we took from BitTorrent's libutp, at Greg
+ * Hazel's recommendation. We use QueryPerformanceCounter for
+ * our high-resolution timer, but use GetTickCount*() to keep
+ * it sane, and adjust_monotonic_time() to keep it monotonic.
+ */
+ LARGE_INTEGER counter;
+ ev_int64_t counter_elapsed, counter_usec_elapsed, ticks_elapsed;
+ QueryPerformanceCounter(&counter);
+ counter_elapsed = (ev_int64_t)
+ (counter.QuadPart - base->first_counter);
+ ticks_elapsed = ticks - base->first_tick;
+ /* TODO: This may upset VC6. If you need this to work with
+ * VC6, please supply an appropriate patch. */
+ counter_usec_elapsed = (ev_int64_t)
+ (counter_elapsed * base->usec_per_count);
+
+ if (abs64(ticks_elapsed*1000 - counter_usec_elapsed) > 1000000) {
+ /* It appears that the QueryPerformanceCounter()
+ * result is more than 1 second away from
+ * GetTickCount() result. Let's adjust it to be as
+ * accurate as we can; adjust_monotnonic_time() below
+ * will keep it monotonic. */
+ counter_usec_elapsed = ticks_elapsed * 1000;
+ base->first_counter = (ev_uint64_t) (counter.QuadPart - counter_usec_elapsed / base->usec_per_count);
+ }
+ tp->tv_sec = (time_t) (counter_usec_elapsed / 1000000);
+ tp->tv_usec = counter_usec_elapsed % 1000000;
+
+ } else {
+ /* We're just using GetTickCount(). */
+ tp->tv_sec = (time_t) (ticks / 1000);
+ tp->tv_usec = (ticks % 1000) * 1000;
+ }
+ adjust_monotonic_time(base, tp);
+
+ return 0;
+}
+#endif
+
+#if defined(HAVE_FALLBACK_MONOTONIC)
+/* =====
+ And if none of the other options work, let's just use gettimeofday(), and
+ ratchet it forward so that it acts like a monotonic timer, whether it
+ wants to or not.
+ */
+
+int
+evutil_configure_monotonic_time_(struct evutil_monotonic_timer *base,
+ int precise)
+{
+ memset(base, 0, sizeof(*base));
+ return 0;
+}
+
+int
+evutil_gettime_monotonic_(struct evutil_monotonic_timer *base,
+ struct timeval *tp)
+{
+ if (evutil_gettimeofday(tp, NULL) < 0)
+ return -1;
+ adjust_monotonic_time(base, tp);
+ return 0;
+
+}
+#endif
diff --git a/ht-internal.h b/ht-internal.h
index 46738251..8842f4cd 100644
--- a/ht-internal.h
+++ b/ht-internal.h
@@ -1,12 +1,12 @@
-/* Based on work Copyright 2002 Christopher Clark */
+/* Copyright 2002 Christopher Clark */
/* Copyright 2005-2012 Nick Mathewson */
/* Copyright 2009-2012 Niels Provos and Nick Mathewson */
/* See license at end. */
/* Based on ideas by Christopher Clark and interfaces from Niels Provos. */
-#ifndef _EVENT_HT_H
-#define _EVENT_HT_H
+#ifndef HT_INTERNAL_H_INCLUDED_
+#define HT_INTERNAL_H_INCLUDED_
#define HT_HEAD(name, type) \
struct name { \
@@ -25,16 +25,16 @@
#define HT_INITIALIZER() \
{ NULL, 0, 0, 0, -1 }
-#ifdef HT_CACHE_HASH_VALUES
+#ifdef HT_NO_CACHE_HASH_VALUES
#define HT_ENTRY(type) \
struct { \
struct type *hte_next; \
- unsigned hte_hash; \
}
#else
#define HT_ENTRY(type) \
struct { \
struct type *hte_next; \
+ unsigned hte_hash; \
}
#endif
@@ -45,6 +45,10 @@
#define HT_SIZE(head) \
((head)->hth_n_entries)
+/* Return memory usage for a hashtable (not counting the entries themselves) */
+#define HT_MEM_USAGE(head) \
+ (sizeof(*head) + (head)->hth_table_length * sizeof(void*))
+
#define HT_FIND(name, head, elm) name##_HT_FIND((head), (elm))
#define HT_INSERT(name, head, elm) name##_HT_INSERT((head), (elm))
#define HT_REPLACE(name, head, elm) name##_HT_REPLACE((head), (elm))
@@ -56,7 +60,7 @@
#define HT_INIT(name, head) name##_HT_INIT(head)
/* Helper: */
static inline unsigned
-ht_improve_hash(unsigned h)
+ht_improve_hash_(unsigned h)
{
/* Aim to protect against poor hash functions by adding logic here
* - logic taken from java 1.4 hashtable source */
@@ -70,7 +74,7 @@ ht_improve_hash(unsigned h)
#if 0
/** Basic string hash function, from Java standard String.hashCode(). */
static inline unsigned
-ht_string_hash(const char *s)
+ht_string_hash_(const char *s)
{
unsigned h = 0;
int m = 1;
@@ -84,7 +88,7 @@ ht_string_hash(const char *s)
/** Basic string hash function, from Python's str.__hash__() */
static inline unsigned
-ht_string_hash(const char *s)
+ht_string_hash_(const char *s)
{
unsigned h;
const unsigned char *cp = (const unsigned char *)s;
@@ -97,25 +101,25 @@ ht_string_hash(const char *s)
return h;
}
-#ifdef HT_CACHE_HASH_VALUES
-#define _HT_SET_HASH(elm, field, hashfn) \
+#ifndef HT_NO_CACHE_HASH_VALUES
+#define HT_SET_HASH_(elm, field, hashfn) \
do { (elm)->field.hte_hash = hashfn(elm); } while (0)
-#define _HT_SET_HASHVAL(elm, field, val) \
+#define HT_SET_HASHVAL_(elm, field, val) \
do { (elm)->field.hte_hash = (val); } while (0)
-#define _HT_ELT_HASH(elm, field, hashfn) \
+#define HT_ELT_HASH_(elm, field, hashfn) \
((elm)->field.hte_hash)
#else
-#define _HT_SET_HASH(elm, field, hashfn) \
+#define HT_SET_HASH_(elm, field, hashfn) \
((void)0)
-#define _HT_ELT_HASH(elm, field, hashfn) \
+#define HT_ELT_HASH_(elm, field, hashfn) \
(hashfn(elm))
-#define _HT_SET_HASHVAL(elm, field, val) \
+#define HT_SET_HASHVAL_(elm, field, val) \
((void)0)
#endif
/* Helper: alias for the bucket containing 'elm'. */
-#define _HT_BUCKET(head, field, elm, hashfn) \
- ((head)->hth_table[_HT_ELT_HASH(elm,field,hashfn) % head->hth_table_length])
+#define HT_BUCKET_(head, field, elm, hashfn) \
+ ((head)->hth_table[HT_ELT_HASH_(elm,field,hashfn) % head->hth_table_length])
#define HT_FOREACH(x, name, head) \
for ((x) = HT_START(name, head); \
@@ -125,7 +129,7 @@ ht_string_hash(const char *s)
#define HT_PROTOTYPE(name, type, field, hashfn, eqfn) \
int name##_HT_GROW(struct name *ht, unsigned min_capacity); \
void name##_HT_CLEAR(struct name *ht); \
- int _##name##_HT_REP_IS_BAD(const struct name *ht); \
+ int name##_HT_REP_IS_BAD_(const struct name *ht); \
static inline void \
name##_HT_INIT(struct name *head) { \
head->hth_table_length = 0; \
@@ -137,12 +141,12 @@ ht_string_hash(const char *s)
/* Helper: returns a pointer to the right location in the table \
* 'head' to find or insert the element 'elm'. */ \
static inline struct type ** \
- _##name##_HT_FIND_P(struct name *head, struct type *elm) \
+ name##_HT_FIND_P_(struct name *head, struct type *elm) \
{ \
struct type **p; \
if (!head->hth_table) \
return NULL; \
- p = &_HT_BUCKET(head, field, elm, hashfn); \
+ p = &HT_BUCKET_(head, field, elm, hashfn); \
while (*p) { \
if (eqfn(*p, elm)) \
return p; \
@@ -157,8 +161,8 @@ ht_string_hash(const char *s)
{ \
struct type **p; \
struct name *h = (struct name *) head; \
- _HT_SET_HASH(elm, field, hashfn); \
- p = _##name##_HT_FIND_P(h, elm); \
+ HT_SET_HASH_(elm, field, hashfn); \
+ p = name##_HT_FIND_P_(h, elm); \
return p ? *p : NULL; \
} \
/* Insert the element 'elm' into the table 'head'. Do not call this \
@@ -170,8 +174,8 @@ ht_string_hash(const char *s)
if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
name##_HT_GROW(head, head->hth_n_entries+1); \
++head->hth_n_entries; \
- _HT_SET_HASH(elm, field, hashfn); \
- p = &_HT_BUCKET(head, field, elm, hashfn); \
+ HT_SET_HASH_(elm, field, hashfn); \
+ p = &HT_BUCKET_(head, field, elm, hashfn); \
elm->field.hte_next = *p; \
*p = elm; \
} \
@@ -184,8 +188,8 @@ ht_string_hash(const char *s)
struct type **p, *r; \
if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
name##_HT_GROW(head, head->hth_n_entries+1); \
- _HT_SET_HASH(elm, field, hashfn); \
- p = _##name##_HT_FIND_P(head, elm); \
+ HT_SET_HASH_(elm, field, hashfn); \
+ p = name##_HT_FIND_P_(head, elm); \
r = *p; \
*p = elm; \
if (r && (r!=elm)) { \
@@ -203,8 +207,8 @@ ht_string_hash(const char *s)
name##_HT_REMOVE(struct name *head, struct type *elm) \
{ \
struct type **p, *r; \
- _HT_SET_HASH(elm, field, hashfn); \
- p = _##name##_HT_FIND_P(head,elm); \
+ HT_SET_HASH_(elm, field, hashfn); \
+ p = name##_HT_FIND_P_(head,elm); \
if (!p || !*p) \
return NULL; \
r = *p; \
@@ -265,7 +269,7 @@ ht_string_hash(const char *s)
if ((*elm)->field.hte_next) { \
return &(*elm)->field.hte_next; \
} else { \
- unsigned b = (_HT_ELT_HASH(*elm, field, hashfn) % head->hth_table_length)+1; \
+ unsigned b = (HT_ELT_HASH_(*elm, field, hashfn) % head->hth_table_length)+1; \
while (b < head->hth_table_length) { \
if (head->hth_table[b]) \
return &head->hth_table[b]; \
@@ -277,7 +281,7 @@ ht_string_hash(const char *s)
static inline struct type ** \
name##_HT_NEXT_RMV(struct name *head, struct type **elm) \
{ \
- unsigned h = _HT_ELT_HASH(*elm, field, hashfn); \
+ unsigned h = HT_ELT_HASH_(*elm, field, hashfn); \
*elm = (*elm)->field.hte_next; \
--head->hth_n_entries; \
if (*elm) { \
@@ -334,7 +338,7 @@ ht_string_hash(const char *s)
elm = head->hth_table[b]; \
while (elm) { \
next = elm->field.hte_next; \
- b2 = _HT_ELT_HASH(elm, field, hashfn) % new_len; \
+ b2 = HT_ELT_HASH_(elm, field, hashfn) % new_len; \
elm->field.hte_next = new_table[b2]; \
new_table[b2] = elm; \
elm = next; \
@@ -352,7 +356,7 @@ ht_string_hash(const char *s)
for (b=0; b < head->hth_table_length; ++b) { \
struct type *e, **pE; \
for (pE = &new_table[b], e = *pE; e != NULL; e = *pE) { \
- b2 = _HT_ELT_HASH(e, field, hashfn) % new_len; \
+ b2 = HT_ELT_HASH_(e, field, hashfn) % new_len; \
if (b2 == b) { \
pE = &e->field.hte_next; \
} else { \
@@ -382,7 +386,7 @@ ht_string_hash(const char *s)
/* Debugging helper: return false iff the representation of 'head' is \
* internally consistent. */ \
int \
- _##name##_HT_REP_IS_BAD(const struct name *head) \
+ name##_HT_REP_IS_BAD_(const struct name *head) \
{ \
unsigned n, i; \
struct type *elm; \
@@ -404,9 +408,9 @@ ht_string_hash(const char *s)
return 5; \
for (n = i = 0; i < head->hth_table_length; ++i) { \
for (elm = head->hth_table[i]; elm; elm = elm->field.hte_next) { \
- if (_HT_ELT_HASH(elm, field, hashfn) != hashfn(elm)) \
+ if (HT_ELT_HASH_(elm, field, hashfn) != hashfn(elm)) \
return 1000 + i; \
- if ((_HT_ELT_HASH(elm, field, hashfn) % head->hth_table_length) != i) \
+ if ((HT_ELT_HASH_(elm, field, hashfn) % head->hth_table_length) != i) \
return 10000 + i; \
++n; \
} \
@@ -419,24 +423,24 @@ ht_string_hash(const char *s)
/** Implements an over-optimized "find and insert if absent" block;
* not meant for direct usage by typical code, or usage outside the critical
* path.*/
-#define _HT_FIND_OR_INSERT(name, field, hashfn, head, eltype, elm, var, y, n) \
+#define HT_FIND_OR_INSERT_(name, field, hashfn, head, eltype, elm, var, y, n) \
{ \
- struct name *_##var##_head = head; \
- struct eltype **var; \
- if (!_##var##_head->hth_table || \
- _##var##_head->hth_n_entries >= _##var##_head->hth_load_limit) \
- name##_HT_GROW(_##var##_head, _##var##_head->hth_n_entries+1); \
- _HT_SET_HASH((elm), field, hashfn); \
- var = _##name##_HT_FIND_P(_##var##_head, (elm)); \
+ struct name *var##_head_ = head; \
+ struct eltype **var; \
+ if (!var##_head_->hth_table || \
+ var##_head_->hth_n_entries >= var##_head_->hth_load_limit) \
+ name##_HT_GROW(var##_head_, var##_head_->hth_n_entries+1); \
+ HT_SET_HASH_((elm), field, hashfn); \
+ var = name##_HT_FIND_P_(var##_head_, (elm)); \
if (*var) { \
y; \
} else { \
n; \
} \
}
-#define _HT_FOI_INSERT(field, head, elm, newent, var) \
+#define HT_FOI_INSERT_(field, head, elm, newent, var) \
{ \
- _HT_SET_HASHVAL(newent, field, (elm)->field.hte_hash); \
+ HT_SET_HASHVAL_(newent, field, (elm)->field.hte_hash); \
newent->field.hte_next = NULL; \
*var = newent; \
++((head)->hth_n_entries); \
@@ -444,7 +448,7 @@ ht_string_hash(const char *s)
/*
* Copyright 2005, Nick Mathewson. Implementation logic is adapted from code
- * by Cristopher Clark, retrofit to allow drop-in memory management, and to
+ * by Christopher Clark, retrofit to allow drop-in memory management, and to
* use the same interface as Niels Provos's tree.h. This is probably still
* a derived work, so the original license below still applies.
*
diff --git a/http-internal.h b/http-internal.h
index 5f66673e..a83160c8 100644
--- a/http-internal.h
+++ b/http-internal.h
@@ -7,8 +7,8 @@
* need to know about these.
*/
-#ifndef _HTTP_INTERNAL_H_
-#define _HTTP_INTERNAL_H_
+#ifndef HTTP_INTERNAL_H_INCLUDED_
+#define HTTP_INTERNAL_H_INCLUDED_
#include "event2/event_struct.h"
#include "util-internal.h"
@@ -29,20 +29,12 @@ enum message_read_status {
DATA_TOO_LONG = -3
};
-enum evhttp_connection_error {
- EVCON_HTTP_TIMEOUT,
- EVCON_HTTP_EOF,
- EVCON_HTTP_INVALID_HEADER,
- EVCON_HTTP_BUFFER_ERROR,
- EVCON_HTTP_REQUEST_CANCEL
-};
-
struct evbuffer;
struct addrinfo;
struct evhttp_request;
/* Indicates an unknown request method. */
-#define _EVHTTP_REQ_UNKNOWN (1<<15)
+#define EVHTTP_REQ_UNKNOWN_ (1<<15)
enum evhttp_connection_state {
EVCON_DISCONNECTED, /**< not currently connected not trying either*/
@@ -82,10 +74,14 @@ struct evhttp_connection {
#define EVHTTP_CON_INCOMING 0x0001 /* only one request on it ever */
#define EVHTTP_CON_OUTGOING 0x0002 /* multiple requests possible */
#define EVHTTP_CON_CLOSEDETECT 0x0004 /* detecting if persistent close */
+#define EVHTTP_CON_AUTOFREE 0x0008 /* set when we want to auto free the connection */
- int timeout; /* timeout in seconds for events */
+ struct timeval timeout; /* timeout for events */
int retry_cnt; /* retry count */
int retry_max; /* maximum number of retries */
+ struct timeval initial_retry_timeout; /* Timeout for low long to wait
+ * after first failing attempt
+ * before retry */
enum evhttp_connection_state state;
@@ -100,10 +96,18 @@ struct evhttp_connection {
void (*closecb)(struct evhttp_connection *, void *);
void *closecb_arg;
- struct deferred_cb read_more_deferred_cb;
+ struct event_callback read_more_deferred_cb;
struct event_base *base;
struct evdns_base *dns_base;
+ int ai_family;
+
+ /* Saved conn_addr, to extract IP address from it.
+ *
+ * Because some servers may reset/close connection without waiting clients,
+ * in that case we can't extract IP address even in close_cb.
+ * So we need to save it, just after we connected to remote server. */
+ struct sockaddr_storage *conn_address;
};
/* A callback for an http server */
@@ -152,10 +156,11 @@ struct evhttp {
/* NULL if this server is not a vhost */
char *vhost_pattern;
- int timeout;
+ struct timeval timeout;
size_t default_max_headers_size;
ev_uint64_t default_max_body_size;
+ const char *default_content_type;
/* Bitmask of all HTTP methods that we accept and pass to user
* callbacks. */
@@ -165,6 +170,8 @@ struct evhttp {
don't match. */
void (*gencb)(struct evhttp_request *req, void *);
void *gencbarg;
+ struct bufferevent* (*bevcb)(struct event_base *, void *);
+ void *bevcbarg;
struct event_base *base;
};
@@ -172,24 +179,28 @@ struct evhttp {
/* XXX most of these functions could be static. */
/* resets the connection; can be reused for more requests */
-void evhttp_connection_reset(struct evhttp_connection *);
+void evhttp_connection_reset_(struct evhttp_connection *);
/* connects if necessary */
-int evhttp_connection_connect(struct evhttp_connection *);
+int evhttp_connection_connect_(struct evhttp_connection *);
+enum evhttp_request_error;
/* notifies the current request that it failed; resets connection */
-void evhttp_connection_fail(struct evhttp_connection *,
- enum evhttp_connection_error error);
+void evhttp_connection_fail_(struct evhttp_connection *,
+ enum evhttp_request_error error);
enum message_read_status;
-enum message_read_status evhttp_parse_firstline(struct evhttp_request *, struct evbuffer*);
-enum message_read_status evhttp_parse_headers(struct evhttp_request *, struct evbuffer*);
+enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*);
+enum message_read_status evhttp_parse_headers_(struct evhttp_request *, struct evbuffer*);
-void evhttp_start_read(struct evhttp_connection *);
+void evhttp_start_read_(struct evhttp_connection *);
/* response sending HTML the data in the buffer */
-void evhttp_response_code(struct evhttp_request *, int, const char *);
-void evhttp_send_page(struct evhttp_request *, struct evbuffer *);
+void evhttp_response_code_(struct evhttp_request *, int, const char *);
+void evhttp_send_page_(struct evhttp_request *, struct evbuffer *);
+
+int evhttp_decode_uri_internal(const char *uri, size_t length,
+ char *ret, int decode_plus);
#endif /* _HTTP_H */
diff --git a/http.c b/http.c
index 377597ea..dd8c9cff 100644
--- a/http.c
+++ b/http.c
@@ -26,22 +26,23 @@
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#ifdef _EVENT_HAVE_SYS_PARAM_H
+#ifdef EVENT__HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_IOCCOM_H
#include <sys/ioccom.h>
#endif
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
@@ -53,17 +54,17 @@
#include <sys/queue.h>
-#ifdef _EVENT_HAVE_NETINET_IN_H
+#ifdef EVENT__HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
-#ifdef _EVENT_HAVE_ARPA_INET_H
+#ifdef EVENT__HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
-#ifdef _EVENT_HAVE_NETDB_H
+#ifdef EVENT__HAVE_NETDB_H
#include <netdb.h>
#endif
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#endif
@@ -71,15 +72,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <syslog.h>
#endif
#include <signal.h>
#include <time.h>
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
-#ifdef _EVENT_HAVE_FCNTL_H
+#ifdef EVENT__HAVE_FCNTL_H
#include <fcntl.h>
#endif
@@ -91,7 +92,6 @@
#include "event2/event.h"
#include "event2/buffer.h"
#include "event2/bufferevent.h"
-#include "event2/bufferevent_compat.h"
#include "event2/http_struct.h"
#include "event2/http_compat.h"
#include "event2/util.h"
@@ -102,7 +102,7 @@
#include "mm-internal.h"
#include "bufferevent-internal.h"
-#ifndef _EVENT_HAVE_GETNAMEINFO
+#ifndef EVENT__HAVE_GETNAMEINFO
#define NI_MAXSERV 32
#define NI_MAXHOST 1025
@@ -193,12 +193,10 @@ static void evhttp_make_header(struct evhttp_connection *, struct evhttp_request
static void evhttp_read_cb(struct bufferevent *, void *);
static void evhttp_write_cb(struct bufferevent *, void *);
static void evhttp_error_cb(struct bufferevent *bufev, short what, void *arg);
-static int evhttp_decode_uri_internal(const char *uri, size_t length,
- char *ret, int decode_plus);
static int evhttp_find_vhost(struct evhttp *http, struct evhttp **outhttp,
const char *hostname);
-#ifndef _EVENT_HAVE_STRSEP
+#ifndef EVENT__HAVE_STRSEP
/* strsep replacement for platforms that lack it. Only works if
* del is one character long. */
static char *
@@ -352,23 +350,6 @@ evhttp_response_needs_body(struct evhttp_request *req)
req->type != EVHTTP_REQ_HEAD);
}
-/** Helper: adds the event 'ev' with the timeout 'timeout', or with
- * default_timeout if timeout is -1.
- */
-static int
-evhttp_add_event(struct event *ev, int timeout, int default_timeout)
-{
- if (timeout != 0) {
- struct timeval tv;
-
- evutil_timerclear(&tv);
- tv.tv_sec = timeout != -1 ? timeout : default_timeout;
- return event_add(ev, &tv);
- } else {
- return event_add(ev, NULL);
- }
-}
-
/** Helper: called after we've added some data to an evcon's bufferevent's
* output buffer. Sets the evconn's writing-is-done callback, and puts
* the bufferevent into writing mode.
@@ -496,12 +477,12 @@ evhttp_maybe_add_date_header(struct evkeyvalq *headers)
{
if (evhttp_find_header(headers, "Date") == NULL) {
char date[50];
-#ifndef WIN32
+#ifndef _WIN32
struct tm cur;
#endif
struct tm *cur_p;
time_t t = time(NULL);
-#ifdef WIN32
+#ifdef _WIN32
cur_p = gmtime(&t);
#else
gmtime_r(&t, &cur);
@@ -571,9 +552,11 @@ evhttp_make_header_response(struct evhttp_connection *evcon,
/* Potentially add headers for unidentified content. */
if (evhttp_response_needs_body(req)) {
if (evhttp_find_header(req->output_headers,
- "Content-Type") == NULL) {
+ "Content-Type") == NULL
+ && evcon->http_server->default_content_type) {
evhttp_add_header(req->output_headers,
- "Content-Type", "text/html; charset=ISO-8859-1");
+ "Content-Type",
+ evcon->http_server->default_content_type);
}
}
@@ -644,11 +627,11 @@ evhttp_connection_set_max_body_size(struct evhttp_connection* evcon,
static int
evhttp_connection_incoming_fail(struct evhttp_request *req,
- enum evhttp_connection_error error)
+ enum evhttp_request_error error)
{
switch (error) {
- case EVCON_HTTP_TIMEOUT:
- case EVCON_HTTP_EOF:
+ case EVREQ_HTTP_TIMEOUT:
+ case EVREQ_HTTP_EOF:
/*
* these are cases in which we probably should just
* close the connection and not send a reply. this
@@ -666,9 +649,10 @@ evhttp_connection_incoming_fail(struct evhttp_request *req,
req->evcon = NULL;
}
return (-1);
- case EVCON_HTTP_INVALID_HEADER:
- case EVCON_HTTP_BUFFER_ERROR:
- case EVCON_HTTP_REQUEST_CANCEL:
+ case EVREQ_HTTP_INVALID_HEADER:
+ case EVREQ_HTTP_BUFFER_ERROR:
+ case EVREQ_HTTP_REQUEST_CANCEL:
+ case EVREQ_HTTP_DATA_TOO_LONG:
default: /* xxx: probably should just error on default */
/* the callback looks at the uri to determine errors */
if (req->uri) {
@@ -695,12 +679,15 @@ evhttp_connection_incoming_fail(struct evhttp_request *req,
* retry any pending requests, and inform the user. If it's incoming,
* delegates to evhttp_connection_incoming_fail(). */
void
-evhttp_connection_fail(struct evhttp_connection *evcon,
- enum evhttp_connection_error error)
+evhttp_connection_fail_(struct evhttp_connection *evcon,
+ enum evhttp_request_error error)
{
+ const int errsave = EVUTIL_SOCKET_ERROR();
struct evhttp_request* req = TAILQ_FIRST(&evcon->requests);
void (*cb)(struct evhttp_request *, void *);
void *cb_arg;
+ void (*error_cb)(enum evhttp_request_error, void *);
+ void *error_cb_arg;
EVUTIL_ASSERT(req != NULL);
bufferevent_disable(evcon->bufev, EV_READ|EV_WRITE);
@@ -719,8 +706,10 @@ evhttp_connection_fail(struct evhttp_connection *evcon,
return;
}
+ error_cb = req->error_cb;
+ error_cb_arg = req->cb_arg;
/* when the request was canceled, the callback is not executed */
- if (error != EVCON_HTTP_REQUEST_CANCEL) {
+ if (error != EVREQ_HTTP_REQUEST_CANCEL) {
/* save the callback for later; the cb might free our object */
cb = req->cb;
cb_arg = req->cb_arg;
@@ -737,13 +726,21 @@ evhttp_connection_fail(struct evhttp_connection *evcon,
evhttp_request_free(req);
/* reset the connection */
- evhttp_connection_reset(evcon);
+ evhttp_connection_reset_(evcon);
/* We are trying the next request that was queued on us */
if (TAILQ_FIRST(&evcon->requests) != NULL)
- evhttp_connection_connect(evcon);
+ evhttp_connection_connect_(evcon);
+
+ /* The call to evhttp_connection_reset_ overwrote errno.
+ * Let's restore the original errno, so that the user's
+ * callback can have a better idea of what the error was.
+ */
+ EVUTIL_SET_SOCKET_ERROR(errsave);
/* inform the user */
+ if (error_cb != NULL)
+ error_cb(error, error_cb_arg);
if (cb != NULL)
(*cb)(NULL, cb_arg);
}
@@ -772,6 +769,7 @@ evhttp_connection_done(struct evhttp_connection *evcon)
{
struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
int con_outgoing = evcon->flags & EVHTTP_CON_OUTGOING;
+ int free_evcon = 0;
if (con_outgoing) {
/* idle or close the connection */
@@ -787,7 +785,7 @@ evhttp_connection_done(struct evhttp_connection *evcon)
/* check if we got asked to close the connection */
if (need_close)
- evhttp_connection_reset(evcon);
+ evhttp_connection_reset_(evcon);
if (TAILQ_FIRST(&evcon->requests) != NULL) {
/*
@@ -795,7 +793,7 @@ evhttp_connection_done(struct evhttp_connection *evcon)
* and deal with the next request.
*/
if (!evhttp_connected(evcon))
- evhttp_connection_connect(evcon);
+ evhttp_connection_connect_(evcon);
else
evhttp_request_dispatch(evcon);
} else if (!need_close) {
@@ -804,6 +802,12 @@ evhttp_connection_done(struct evhttp_connection *evcon)
* need to detect if the other side closes it.
*/
evhttp_connection_start_detectclose(evcon);
+ } else if ((evcon->flags & EVHTTP_CON_AUTOFREE)) {
+ /*
+ * If we have no more requests that need completion
+ * and we're not waiting for the connection to close
+ */
+ free_evcon = 1;
}
} else {
/*
@@ -822,6 +826,16 @@ evhttp_connection_done(struct evhttp_connection *evcon)
if (con_outgoing && ((req->flags & EVHTTP_USER_OWNED) == 0)) {
evhttp_request_free(req);
}
+
+ /* If this was the last request of an outgoing connection and we're
+ * not waiting to receive a connection close event and we want to
+ * automatically free the connection. We check to ensure our request
+ * list is empty one last time just in case our callback added a
+ * new request.
+ */
+ if (free_evcon && TAILQ_FIRST(&evcon->requests) == NULL) {
+ evhttp_connection_free(evcon);
+ }
}
/*
@@ -934,10 +948,10 @@ evhttp_read_trailer(struct evhttp_connection *evcon, struct evhttp_request *req)
{
struct evbuffer *buf = bufferevent_get_input(evcon->bufev);
- switch (evhttp_parse_headers(req, buf)) {
+ switch (evhttp_parse_headers_(req, buf)) {
case DATA_CORRUPTED:
case DATA_TOO_LONG:
- evhttp_connection_fail(evcon, EVCON_HTTP_INVALID_HEADER);
+ evhttp_connection_fail_(evcon, EVREQ_HTTP_DATA_TOO_LONG);
break;
case ALL_DATA_READ:
bufferevent_disable(evcon->bufev, EV_READ);
@@ -964,10 +978,10 @@ evhttp_read_body(struct evhttp_connection *evcon, struct evhttp_request *req)
evhttp_read_trailer(evcon, req);
return;
case DATA_CORRUPTED:
- case DATA_TOO_LONG:/*separate error for this? XXX */
+ case DATA_TOO_LONG:
/* corrupted data */
- evhttp_connection_fail(evcon,
- EVCON_HTTP_INVALID_HEADER);
+ evhttp_connection_fail_(evcon,
+ EVREQ_HTTP_DATA_TOO_LONG);
return;
case REQUEST_CANCELED:
/* request canceled */
@@ -980,7 +994,7 @@ evhttp_read_body(struct evhttp_connection *evcon, struct evhttp_request *req)
} else if (req->ntoread < 0) {
/* Read until connection close. */
if ((size_t)(req->body_size + evbuffer_get_length(buf)) < req->body_size) {
- evhttp_connection_fail(evcon, EVCON_HTTP_INVALID_HEADER);
+ evhttp_connection_fail_(evcon, EVREQ_HTTP_INVALID_HEADER);
return;
}
@@ -1005,8 +1019,8 @@ evhttp_read_body(struct evhttp_connection *evcon, struct evhttp_request *req)
/* XXX: The above casted comparison must checked for overflow */
/* failed body length test */
event_debug(("Request body is too long"));
- evhttp_connection_fail(evcon,
- EVCON_HTTP_INVALID_HEADER);
+ evhttp_connection_fail_(evcon,
+ EVREQ_HTTP_DATA_TOO_LONG);
return;
}
@@ -1034,7 +1048,7 @@ evhttp_read_body(struct evhttp_connection *evcon, struct evhttp_request *req)
}
#define get_deferred_queue(evcon) \
- (event_base_get_deferred_cb_queue((evcon)->base))
+ ((evcon)->base)
/*
* Gets called when more data becomes available
@@ -1047,7 +1061,7 @@ evhttp_read_cb(struct bufferevent *bufev, void *arg)
struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
/* Cancel if it's pending. */
- event_deferred_cb_cancel(get_deferred_queue(evcon),
+ event_deferred_cb_cancel_(get_deferred_queue(evcon),
&evcon->read_more_deferred_cb);
switch (evcon->state) {
@@ -1083,7 +1097,7 @@ evhttp_read_cb(struct bufferevent *bufev, void *arg)
__func__, EV_SIZE_ARG(total_len)));
#endif
- evhttp_connection_reset(evcon);
+ evhttp_connection_reset_(evcon);
}
break;
case EVCON_DISCONNECTED:
@@ -1096,7 +1110,7 @@ evhttp_read_cb(struct bufferevent *bufev, void *arg)
}
static void
-evhttp_deferred_read_cb(struct deferred_cb *cb, void *data)
+evhttp_deferred_read_cb(struct event_callback *cb, void *data)
{
struct evhttp_connection *evcon = data;
evhttp_read_cb(evcon->bufev, evcon);
@@ -1114,7 +1128,7 @@ evhttp_write_connectioncb(struct evhttp_connection *evcon, void *arg)
/* We are done writing our header and are now expecting the response */
req->kind = EVHTTP_RESPONSE;
- evhttp_start_read(evcon);
+ evhttp_start_read_(evcon);
}
/*
@@ -1135,7 +1149,7 @@ evhttp_connection_free(struct evhttp_connection *evcon)
/* remove all requests that might be queued on this
* connection. for server connections, this should be empty.
* because it gets dequeued either in evhttp_connection_done or
- * evhttp_connection_fail.
+ * evhttp_connection_fail_.
*/
while ((req = TAILQ_FIRST(&evcon->requests)) != NULL) {
TAILQ_REMOVE(&evcon->requests, req, next);
@@ -1155,12 +1169,14 @@ evhttp_connection_free(struct evhttp_connection *evcon)
if (evcon->bufev != NULL)
bufferevent_free(evcon->bufev);
- event_deferred_cb_cancel(get_deferred_queue(evcon),
+ event_deferred_cb_cancel_(get_deferred_queue(evcon),
&evcon->read_more_deferred_cb);
if (evcon->fd != -1) {
shutdown(evcon->fd, EVUTIL_SHUT_WR);
- evutil_closesocket(evcon->fd);
+ if (!(bufferevent_get_options_(evcon->bufev) & BEV_OPT_CLOSE_ON_FREE)) {
+ evutil_closesocket(evcon->fd);
+ }
}
if (evcon->bind_address != NULL)
@@ -1169,10 +1185,18 @@ evhttp_connection_free(struct evhttp_connection *evcon)
if (evcon->address != NULL)
mm_free(evcon->address);
+ if (evcon->conn_address != NULL)
+ mm_free(evcon->conn_address);
+
mm_free(evcon);
}
void
+evhttp_connection_free_on_completion(struct evhttp_connection *evcon) {
+ evcon->flags |= EVHTTP_CON_AUTOFREE;
+}
+
+void
evhttp_connection_set_local_address(struct evhttp_connection *evcon,
const char *address)
{
@@ -1217,7 +1241,7 @@ evhttp_request_dispatch(struct evhttp_connection* evcon)
/* Reset our connection state: disables reading/writing, closes our fd (if
* any), clears out buffers, and puts us in state DISCONNECTED. */
void
-evhttp_connection_reset(struct evhttp_connection *evcon)
+evhttp_connection_reset_(struct evhttp_connection *evcon)
{
struct evbuffer *tmp;
@@ -1230,9 +1254,9 @@ evhttp_connection_reset(struct evhttp_connection *evcon)
bufferevent is connecting, then you can't actually stop the
bufferevent from trying to connect with bufferevent_disable(). The
connect will never trigger, since we close the fd, but the timeout
- might. That caused an assertion failure in evhttp_connection_fail.
+ might. That caused an assertion failure in evhttp_connection_fail_.
*/
- bufferevent_disable_hard(evcon->bufev, EV_READ|EV_WRITE);
+ bufferevent_disable_hard_(evcon->bufev, EV_READ|EV_WRITE);
if (evcon->fd != -1) {
/* inform interested parties about connection close */
@@ -1241,6 +1265,7 @@ evhttp_connection_reset(struct evhttp_connection *evcon)
shutdown(evcon->fd, EVUTIL_SHUT_WR);
evutil_closesocket(evcon->fd);
+ bufferevent_setfd(evcon->bufev, -1);
evcon->fd = -1;
}
@@ -1275,7 +1300,7 @@ evhttp_connection_retry(evutil_socket_t fd, short what, void *arg)
struct evhttp_connection *evcon = arg;
evcon->state = EVCON_DISCONNECTED;
- evhttp_connection_connect(evcon);
+ evhttp_connection_connect_(evcon);
}
static void
@@ -1283,16 +1308,28 @@ evhttp_connection_cb_cleanup(struct evhttp_connection *evcon)
{
struct evcon_requestq requests;
+ evhttp_connection_reset_(evcon);
if (evcon->retry_max < 0 || evcon->retry_cnt < evcon->retry_max) {
+ struct timeval tv_retry = evcon->initial_retry_timeout;
+ int i;
evtimer_assign(&evcon->retry_ev, evcon->base, evhttp_connection_retry, evcon);
/* XXXX handle failure from evhttp_add_event */
- evhttp_add_event(&evcon->retry_ev,
- MIN(3600, 2 << evcon->retry_cnt),
- HTTP_CONNECT_TIMEOUT);
+ for (i=0; i < evcon->retry_cnt; ++i) {
+ tv_retry.tv_usec *= 2;
+ if (tv_retry.tv_usec > 1000000) {
+ tv_retry.tv_usec -= 1000000;
+ tv_retry.tv_sec += 1;
+ }
+ tv_retry.tv_sec *= 2;
+ if (tv_retry.tv_sec > 3600) {
+ tv_retry.tv_sec = 3600;
+ tv_retry.tv_usec = 0;
+ }
+ }
+ event_add(&evcon->retry_ev, &tv_retry);
evcon->retry_cnt++;
return;
}
- evhttp_connection_reset(evcon);
/*
* User callback can do evhttp_make_request() on the same
@@ -1325,6 +1362,9 @@ evhttp_error_cb(struct bufferevent *bufev, short what, void *arg)
struct evhttp_connection *evcon = arg;
struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
+ if (evcon->fd == -1)
+ evcon->fd = bufferevent_getfd(bufev);
+
switch (evcon->state) {
case EVCON_CONNECTING:
if (what & BEV_EVENT_TIMEOUT) {
@@ -1367,16 +1407,28 @@ evhttp_error_cb(struct bufferevent *bufev, short what, void *arg)
* disconnected.
*/
EVUTIL_ASSERT(evcon->state == EVCON_IDLE);
- evhttp_connection_reset(evcon);
+ evhttp_connection_reset_(evcon);
+
+ /*
+ * If we have no more requests that need completion
+ * and we want to auto-free the connection when all
+ * requests have been completed.
+ */
+ if (TAILQ_FIRST(&evcon->requests) == NULL
+ && (evcon->flags & EVHTTP_CON_OUTGOING)
+ && (evcon->flags & EVHTTP_CON_AUTOFREE)) {
+ evhttp_connection_free(evcon);
+ }
return;
}
if (what & BEV_EVENT_TIMEOUT) {
- evhttp_connection_fail(evcon, EVCON_HTTP_TIMEOUT);
+ evhttp_connection_fail_(evcon, EVREQ_HTTP_TIMEOUT);
} else if (what & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
- evhttp_connection_fail(evcon, EVCON_HTTP_EOF);
+ evhttp_connection_fail_(evcon, EVREQ_HTTP_EOF);
+ } else if (what == BEV_EVENT_CONNECTED) {
} else {
- evhttp_connection_fail(evcon, EVCON_HTTP_BUFFER_ERROR);
+ evhttp_connection_fail_(evcon, EVREQ_HTTP_BUFFER_ERROR);
}
}
@@ -1389,13 +1441,17 @@ evhttp_connection_cb(struct bufferevent *bufev, short what, void *arg)
struct evhttp_connection *evcon = arg;
int error;
ev_socklen_t errsz = sizeof(error);
+ socklen_t conn_address_len = sizeof(*evcon->conn_address);
+
+ if (evcon->fd == -1)
+ evcon->fd = bufferevent_getfd(bufev);
if (!(what & BEV_EVENT_CONNECTED)) {
/* some operating systems return ECONNREFUSED immediately
* when connecting to a local address. the cleanup is going
* to reschedule this function call.
*/
-#ifndef WIN32
+#ifndef _WIN32
if (errno == ECONNREFUSED)
goto cleanup;
#endif
@@ -1403,6 +1459,12 @@ evhttp_connection_cb(struct bufferevent *bufev, short what, void *arg)
return;
}
+ if (evcon->fd == -1) {
+ event_debug(("%s: bufferevent_getfd returned -1",
+ __func__));
+ goto cleanup;
+ }
+
/* Check if the connection completed */
if (getsockopt(evcon->fd, SOL_SOCKET, SO_ERROR, (void*)&error,
&errsz) == -1) {
@@ -1430,6 +1492,14 @@ evhttp_connection_cb(struct bufferevent *bufev, short what, void *arg)
evcon->retry_cnt = 0;
evcon->state = EVCON_IDLE;
+ if (!evcon->conn_address) {
+ evcon->conn_address = mm_malloc(sizeof(*evcon->conn_address));
+ }
+ if (getpeername(evcon->fd, (struct sockaddr *)evcon->conn_address, &conn_address_len)) {
+ mm_free(evcon->conn_address);
+ evcon->conn_address = NULL;
+ }
+
/* reset the bufferevent cbs */
bufferevent_setcb(evcon->bufev,
evhttp_read_cb,
@@ -1437,14 +1507,12 @@ evhttp_connection_cb(struct bufferevent *bufev, short what, void *arg)
evhttp_error_cb,
evcon);
- if (evcon->timeout == -1)
- bufferevent_settimeout(evcon->bufev,
- HTTP_READ_TIMEOUT, HTTP_WRITE_TIMEOUT);
- else {
- struct timeval tv;
- tv.tv_sec = evcon->timeout;
- tv.tv_usec = 0;
- bufferevent_set_timeouts(evcon->bufev, &tv, &tv);
+ if (!evutil_timerisset(&evcon->timeout)) {
+ const struct timeval read_tv = { HTTP_READ_TIMEOUT, 0 };
+ const struct timeval write_tv = { HTTP_WRITE_TIMEOUT, 0 };
+ bufferevent_set_timeouts(evcon->bufev, &read_tv, &write_tv);
+ } else {
+ bufferevent_set_timeouts(evcon->bufev, &evcon->timeout, &evcon->timeout);
}
/* try to start requests that have queued up on this connection */
@@ -1528,6 +1596,8 @@ evhttp_parse_request_line(struct evhttp_request *req, char *line)
char *version;
const char *hostname;
const char *scheme;
+ size_t method_len;
+ enum evhttp_cmd_type type;
/* Parse the request line */
method = strsep(&line, " ");
@@ -1540,30 +1610,122 @@ evhttp_parse_request_line(struct evhttp_request *req, char *line)
if (line != NULL)
return (-1);
+ method_len = (uri - method) - 1;
+ type = EVHTTP_REQ_UNKNOWN_;
+
/* First line */
- if (strcmp(method, "GET") == 0) {
- req->type = EVHTTP_REQ_GET;
- } else if (strcmp(method, "POST") == 0) {
- req->type = EVHTTP_REQ_POST;
- } else if (strcmp(method, "HEAD") == 0) {
- req->type = EVHTTP_REQ_HEAD;
- } else if (strcmp(method, "PUT") == 0) {
- req->type = EVHTTP_REQ_PUT;
- } else if (strcmp(method, "DELETE") == 0) {
- req->type = EVHTTP_REQ_DELETE;
- } else if (strcmp(method, "OPTIONS") == 0) {
- req->type = EVHTTP_REQ_OPTIONS;
- } else if (strcmp(method, "TRACE") == 0) {
- req->type = EVHTTP_REQ_TRACE;
- } else if (strcmp(method, "PATCH") == 0) {
- req->type = EVHTTP_REQ_PATCH;
- } else {
- req->type = _EVHTTP_REQ_UNKNOWN;
- event_debug(("%s: bad method %s on request %p from %s",
+ switch (method_len) {
+ case 3:
+ /* The length of the method string is 3, meaning it can only be one of two methods: GET or PUT */
+
+ /* Since both GET and PUT share the same character 'T' at the end,
+ * if the string doesn't have 'T', we can immediately determine this
+ * is an invalid HTTP method */
+
+ if (method[2] != 'T') {
+ break;
+ }
+
+ switch (*method) {
+ case 'G':
+ /* This first byte is 'G', so make sure the next byte is
+ * 'E', if it isn't then this isn't a valid method */
+
+ if (method[1] == 'E') {
+ type = EVHTTP_REQ_GET;
+ }
+
+ break;
+ case 'P':
+ /* First byte is P, check second byte for 'U', if not,
+ * we know it's an invalid method */
+ if (method[1] == 'U') {
+ type = EVHTTP_REQ_PUT;
+ }
+ break;
+ default:
+ break;
+ }
+ break;
+ case 4:
+ /* The method length is 4 bytes, leaving only the methods "POST" and "HEAD" */
+ switch (*method) {
+ case 'P':
+ if (method[3] == 'T' && method[2] == 'S' && method[1] == 'O') {
+ type = EVHTTP_REQ_POST;
+ }
+ break;
+ case 'H':
+ if (method[3] == 'D' && method[2] == 'A' && method[1] == 'E') {
+ type = EVHTTP_REQ_HEAD;
+ }
+ break;
+ default:
+ break;
+ }
+ break;
+ case 5:
+ /* Method length is 5 bytes, which can only encompass PATCH and TRACE */
+ switch (*method) {
+ case 'P':
+ if (method[4] == 'H' && method[3] == 'C' && method[2] == 'T' && method[1] == 'A') {
+ type = EVHTTP_REQ_PATCH;
+ }
+ break;
+ case 'T':
+ if (method[4] == 'E' && method[3] == 'C' && method[2] == 'A' && method[1] == 'R') {
+ type = EVHTTP_REQ_TRACE;
+ }
+
+ break;
+ default:
+ break;
+ }
+ break;
+ case 6:
+ /* Method length is 6, only valid method 6 bytes in length is DELEte */
+
+ /* If the first byte isn't 'D' then it's invalid */
+ if (*method != 'D') {
+ break;
+ }
+
+ if (method[5] == 'E' && method[4] == 'T' && method[3] == 'E' && method[2] == 'L' && method[1] == 'E') {
+ type = EVHTTP_REQ_DELETE;
+ }
+
+ break;
+ case 7:
+ /* Method length is 7, only valid methods are "OPTIONS" and "CONNECT" */
+ switch (*method) {
+ case 'O':
+ if (method[6] == 'S' && method[5] == 'N' && method[4] == 'O' &&
+ method[3] == 'I' && method[2] == 'T' && method[1] == 'P') {
+ type = EVHTTP_REQ_OPTIONS;
+ }
+
+ break;
+ case 'C':
+ if (method[6] == 'T' && method[5] == 'C' && method[4] == 'E' &&
+ method[3] == 'N' && method[2] == 'N' && method[1] == 'O') {
+ type = EVHTTP_REQ_CONNECT;
+ }
+
+ break;
+ default:
+ break;
+ }
+ break;
+ } /* switch */
+
+ if ((int)type == EVHTTP_REQ_UNKNOWN_) {
+ event_debug(("%s: bad method %s on request %p from %s",
__func__, method, req, req->remote_host));
- /* No error yet; we'll give a better error later when
- * we see that req->type is unsupported. */
+ /* No error yet; we'll give a better error later when
+ * we see that req->type is unsupported. */
}
+
+ req->type = type;
if (evhttp_parse_http_version(version, req) < 0)
return (-1);
@@ -1719,7 +1881,7 @@ evhttp_add_header_internal(struct evkeyvalq *headers,
*/
enum message_read_status
-evhttp_parse_firstline(struct evhttp_request *req, struct evbuffer *buffer)
+evhttp_parse_firstline_(struct evhttp_request *req, struct evbuffer *buffer)
{
char *line;
enum message_read_status status = ALL_DATA_READ;
@@ -1761,7 +1923,7 @@ evhttp_parse_firstline(struct evhttp_request *req, struct evbuffer *buffer)
}
static int
-evhttp_append_to_last_header(struct evkeyvalq *headers, const char *line)
+evhttp_append_to_last_header(struct evkeyvalq *headers, char *line)
{
struct evkeyval *header = TAILQ_LAST(headers, evkeyvalq);
char *newval;
@@ -1771,20 +1933,27 @@ evhttp_append_to_last_header(struct evkeyvalq *headers, const char *line)
return (-1);
old_len = strlen(header->value);
+
+ /* Strip space from start and end of line. */
+ while (*line == ' ' || *line == '\t')
+ ++line;
+ evutil_rtrim_lws_(line);
+
line_len = strlen(line);
- newval = mm_realloc(header->value, old_len + line_len + 1);
+ newval = mm_realloc(header->value, old_len + line_len + 2);
if (newval == NULL)
return (-1);
- memcpy(newval + old_len, line, line_len + 1);
+ newval[old_len] = ' ';
+ memcpy(newval + old_len + 1, line, line_len + 1);
header->value = newval;
return (0);
}
enum message_read_status
-evhttp_parse_headers(struct evhttp_request *req, struct evbuffer* buffer)
+evhttp_parse_headers_(struct evhttp_request *req, struct evbuffer* buffer)
{
enum message_read_status errcode = DATA_CORRUPTED;
char *line;
@@ -1825,6 +1994,7 @@ evhttp_parse_headers(struct evhttp_request *req, struct evbuffer* buffer)
goto error;
svalue += strspn(svalue, " ");
+ evutil_rtrim_lws_(svalue);
if (evhttp_add_header(headers, skey, svalue) == -1)
goto error;
@@ -1924,8 +2094,8 @@ evhttp_get_body(struct evhttp_connection *evcon, struct evhttp_request *req)
req->ntoread = -1;
} else {
if (evhttp_get_body_length(req) == -1) {
- evhttp_connection_fail(evcon,
- EVCON_HTTP_INVALID_HEADER);
+ evhttp_connection_fail_(evcon,
+ EVREQ_HTTP_INVALID_HEADER);
return;
}
if (req->kind == EVHTTP_REQUEST && req->ntoread < 1) {
@@ -1976,12 +2146,12 @@ evhttp_read_firstline(struct evhttp_connection *evcon,
{
enum message_read_status res;
- res = evhttp_parse_firstline(req, bufferevent_get_input(evcon->bufev));
+ res = evhttp_parse_firstline_(req, bufferevent_get_input(evcon->bufev));
if (res == DATA_CORRUPTED || res == DATA_TOO_LONG) {
/* Error while reading, terminate */
event_debug(("%s: bad header lines on "EV_SOCK_FMT"\n",
__func__, EV_SOCK_ARG(evcon->fd)));
- evhttp_connection_fail(evcon, EVCON_HTTP_INVALID_HEADER);
+ evhttp_connection_fail_(evcon, EVREQ_HTTP_INVALID_HEADER);
return;
} else if (res == MORE_DATA_EXPECTED) {
/* Need more header lines */
@@ -1999,12 +2169,12 @@ evhttp_read_header(struct evhttp_connection *evcon,
enum message_read_status res;
evutil_socket_t fd = evcon->fd;
- res = evhttp_parse_headers(req, bufferevent_get_input(evcon->bufev));
+ res = evhttp_parse_headers_(req, bufferevent_get_input(evcon->bufev));
if (res == DATA_CORRUPTED || res == DATA_TOO_LONG) {
/* Error while reading, terminate */
event_debug(("%s: bad header lines on "EV_SOCK_FMT"\n",
__func__, EV_SOCK_ARG(fd)));
- evhttp_connection_fail(evcon, EVCON_HTTP_INVALID_HEADER);
+ evhttp_connection_fail_(evcon, EVREQ_HTTP_INVALID_HEADER);
return;
} else if (res == MORE_DATA_EXPECTED) {
/* Need more header lines */
@@ -2014,6 +2184,14 @@ evhttp_read_header(struct evhttp_connection *evcon,
/* Disable reading for now */
bufferevent_disable(evcon->bufev, EV_READ);
+ /* Callback can shut down connection with negative return value */
+ if (req->header_cb != NULL) {
+ if ((*req->header_cb)(req, req->cb_arg) < 0) {
+ evhttp_connection_fail_(evcon, EVREQ_HTTP_EOF);
+ return;
+ }
+ }
+
/* Done reading headers, do the real work */
switch (req->kind) {
case EVHTTP_REQUEST:
@@ -2026,7 +2204,7 @@ evhttp_read_header(struct evhttp_connection *evcon,
case EVHTTP_RESPONSE:
/* Start over if we got a 100 Continue response. */
if (req->response_code == 100) {
- evhttp_start_read(evcon);
+ evhttp_start_read_(evcon);
return;
}
if (!evhttp_response_needs_body(req)) {
@@ -2046,7 +2224,7 @@ evhttp_read_header(struct evhttp_connection *evcon,
default:
event_warnx("%s: bad header on "EV_SOCK_FMT, __func__,
EV_SOCK_ARG(fd));
- evhttp_connection_fail(evcon, EVCON_HTTP_INVALID_HEADER);
+ evhttp_connection_fail_(evcon, EVREQ_HTTP_INVALID_HEADER);
break;
}
/* request may have been freed above */
@@ -2069,7 +2247,7 @@ evhttp_connection_new(const char *address, unsigned short port)
}
struct evhttp_connection *
-evhttp_connection_base_new(struct event_base *base, struct evdns_base *dnsbase,
+evhttp_connection_base_bufferevent_new(struct event_base *base, struct evdns_base *dnsbase, struct bufferevent* bev,
const char *address, unsigned short port)
{
struct evhttp_connection *evcon = NULL;
@@ -2087,7 +2265,7 @@ evhttp_connection_base_new(struct event_base *base, struct evdns_base *dnsbase,
evcon->max_headers_size = EV_SIZE_MAX;
evcon->max_body_size = EV_SIZE_MAX;
- evcon->timeout = -1;
+ evutil_timerclear(&evcon->timeout);
evcon->retry_cnt = evcon->retry_max = 0;
if ((evcon->address = mm_strdup(address)) == NULL) {
@@ -2095,27 +2273,35 @@ evhttp_connection_base_new(struct event_base *base, struct evdns_base *dnsbase,
goto error;
}
- if ((evcon->bufev = bufferevent_new(-1,
- evhttp_read_cb,
- evhttp_write_cb,
- evhttp_error_cb, evcon)) == NULL) {
- event_warn("%s: bufferevent_new failed", __func__);
- goto error;
+ if (bev == NULL) {
+ if (!(bev = bufferevent_socket_new(base, -1, 0))) {
+ event_warn("%s: bufferevent_socket_new failed", __func__);
+ goto error;
+ }
}
+ bufferevent_setcb(bev, evhttp_read_cb, evhttp_write_cb, evhttp_error_cb, evcon);
+ evcon->bufev = bev;
+
evcon->state = EVCON_DISCONNECTED;
TAILQ_INIT(&evcon->requests);
+ evcon->initial_retry_timeout.tv_sec = 2;
+ evcon->initial_retry_timeout.tv_usec = 0;
+
if (base != NULL) {
evcon->base = base;
- bufferevent_base_set(base, evcon->bufev);
+ if (bufferevent_get_base(bev) != base)
+ bufferevent_base_set(base, evcon->bufev);
}
-
- event_deferred_cb_init(&evcon->read_more_deferred_cb,
+ event_deferred_cb_init_(
+ &evcon->read_more_deferred_cb,
+ bufferevent_get_priority(bev),
evhttp_deferred_read_cb, evcon);
evcon->dns_base = dnsbase;
+ evcon->ai_family = AF_UNSPEC;
return (evcon);
@@ -2125,12 +2311,30 @@ evhttp_connection_base_new(struct event_base *base, struct evdns_base *dnsbase,
return (NULL);
}
-struct bufferevent *
-evhttp_connection_get_bufferevent(struct evhttp_connection *evcon)
+struct bufferevent* evhttp_connection_get_bufferevent(struct evhttp_connection *evcon)
{
return evcon->bufev;
}
+struct evhttp *
+evhttp_connection_get_server(struct evhttp_connection *evcon)
+{
+ return evcon->http_server;
+}
+
+struct evhttp_connection *
+evhttp_connection_base_new(struct event_base *base, struct evdns_base *dnsbase,
+ const char *address, unsigned short port)
+{
+ return evhttp_connection_base_bufferevent_new(base, dnsbase, NULL, address, port);
+}
+
+void evhttp_connection_set_family(struct evhttp_connection *evcon,
+ int family)
+{
+ evcon->ai_family = family;
+}
+
void
evhttp_connection_set_base(struct evhttp_connection *evcon,
struct event_base *base)
@@ -2145,14 +2349,41 @@ void
evhttp_connection_set_timeout(struct evhttp_connection *evcon,
int timeout_in_secs)
{
- evcon->timeout = timeout_in_secs;
+ if (timeout_in_secs == -1)
+ evhttp_connection_set_timeout_tv(evcon, NULL);
+ else {
+ struct timeval tv;
+ tv.tv_sec = timeout_in_secs;
+ tv.tv_usec = 0;
+ evhttp_connection_set_timeout_tv(evcon, &tv);
+ }
+}
- if (evcon->timeout == -1)
- bufferevent_settimeout(evcon->bufev,
- HTTP_READ_TIMEOUT, HTTP_WRITE_TIMEOUT);
- else
- bufferevent_settimeout(evcon->bufev,
- evcon->timeout, evcon->timeout);
+void
+evhttp_connection_set_timeout_tv(struct evhttp_connection *evcon,
+ const struct timeval* tv)
+{
+ if (tv) {
+ evcon->timeout = *tv;
+ bufferevent_set_timeouts(evcon->bufev, &evcon->timeout, &evcon->timeout);
+ } else {
+ const struct timeval read_tv = { HTTP_READ_TIMEOUT, 0 };
+ const struct timeval write_tv = { HTTP_WRITE_TIMEOUT, 0 };
+ evutil_timerclear(&evcon->timeout);
+ bufferevent_set_timeouts(evcon->bufev, &read_tv, &write_tv);
+ }
+}
+
+void
+evhttp_connection_set_initial_retry_tv(struct evhttp_connection *evcon,
+ const struct timeval *tv)
+{
+ if (tv) {
+ evcon->initial_retry_timeout = *tv;
+ } else {
+ evutil_timerclear(&evcon->initial_retry_timeout);
+ evcon->initial_retry_timeout.tv_sec = 2;
+ }
}
void
@@ -2178,43 +2409,58 @@ evhttp_connection_get_peer(struct evhttp_connection *evcon,
*port = evcon->port;
}
+const struct sockaddr*
+evhttp_connection_get_addr(struct evhttp_connection *evcon)
+{
+ return (struct sockaddr *)evcon->conn_address;
+}
+
int
-evhttp_connection_connect(struct evhttp_connection *evcon)
+evhttp_connection_connect_(struct evhttp_connection *evcon)
{
int old_state = evcon->state;
if (evcon->state == EVCON_CONNECTING)
return (0);
- evhttp_connection_reset(evcon);
+ evhttp_connection_reset_(evcon);
EVUTIL_ASSERT(!(evcon->flags & EVHTTP_CON_INCOMING));
evcon->flags |= EVHTTP_CON_OUTGOING;
- evcon->fd = bind_socket(
- evcon->bind_address, evcon->bind_port, 0 /*reuse*/);
- if (evcon->fd == -1) {
- event_debug(("%s: failed to bind to \"%s\"",
- __func__, evcon->bind_address));
- return (-1);
+ if (evcon->bind_address || evcon->bind_port) {
+ evcon->fd = bind_socket(
+ evcon->bind_address, evcon->bind_port, 0 /*reuse*/);
+ if (evcon->fd == -1) {
+ event_debug(("%s: failed to bind to \"%s\"",
+ __func__, evcon->bind_address));
+ return (-1);
+ }
+
+ bufferevent_setfd(evcon->bufev, evcon->fd);
+ } else {
+ bufferevent_setfd(evcon->bufev, -1);
}
/* Set up a callback for successful connection setup */
- bufferevent_setfd(evcon->bufev, evcon->fd);
bufferevent_setcb(evcon->bufev,
NULL /* evhttp_read_cb */,
NULL /* evhttp_write_cb */,
evhttp_connection_cb,
evcon);
- bufferevent_settimeout(evcon->bufev, 0,
- evcon->timeout != -1 ? evcon->timeout : HTTP_CONNECT_TIMEOUT);
+ if (!evutil_timerisset(&evcon->timeout)) {
+ const struct timeval conn_tv = { HTTP_CONNECT_TIMEOUT, 0 };
+ bufferevent_set_timeouts(evcon->bufev, NULL, &conn_tv);
+ } else {
+ bufferevent_set_timeouts(evcon->bufev, NULL, &evcon->timeout);
+ }
/* make sure that we get a write callback */
bufferevent_enable(evcon->bufev, EV_WRITE);
evcon->state = EVCON_CONNECTING;
if (bufferevent_socket_connect_hostname(evcon->bufev, evcon->dns_base,
- AF_UNSPEC, evcon->address, evcon->port) < 0) {
+ evcon->ai_family, evcon->address, evcon->port) < 0) {
evcon->state = old_state;
event_sock_warn(evcon->fd, "%s: connection to \"%s\" failed",
__func__, evcon->address);
@@ -2261,17 +2507,17 @@ evhttp_make_request(struct evhttp_connection *evcon,
req->evcon = evcon;
EVUTIL_ASSERT(!(req->flags & EVHTTP_REQ_OWN_CONNECTION));
- TAILQ_INSERT_TAIL(&evcon->requests, req, next);
+ TAILQ_INSERT_TAIL(&evcon->requests, req, next);
/* If the connection object is not connected; make it so */
if (!evhttp_connected(evcon)) {
- int res = evhttp_connection_connect(evcon);
- /* evhttp_connection_fail(), which is called through
- * evhttp_connection_connect(), assumes that req lies in
- * evcon->requests. Thus, enqueue the request in advance and r
- * it in the error case. */
- if (res != 0)
- TAILQ_REMOVE(&evcon->requests, req, next);
+ int res = evhttp_connection_connect_(evcon);
+ /* evhttp_connection_fail_(), which is called through
+ * evhttp_connection_connect_(), assumes that req lies in
+ * evcon->requests. Thus, enqueue the request in advance and
+ * remove it in the error case. */
+ if (res != 0)
+ TAILQ_REMOVE(&evcon->requests, req, next);
return res;
}
@@ -2297,8 +2543,8 @@ evhttp_cancel_request(struct evhttp_request *req)
/* it's currently being worked on, so reset
* the connection.
*/
- evhttp_connection_fail(evcon,
- EVCON_HTTP_REQUEST_CANCEL);
+ evhttp_connection_fail_(evcon,
+ EVREQ_HTTP_REQUEST_CANCEL);
/* connection fail freed the request */
return;
@@ -2319,7 +2565,7 @@ evhttp_cancel_request(struct evhttp_request *req)
*/
void
-evhttp_start_read(struct evhttp_connection *evcon)
+evhttp_start_read_(struct evhttp_connection *evcon)
{
/* Set up an event to read the headers */
bufferevent_disable(evcon->bufev, EV_WRITE);
@@ -2335,7 +2581,7 @@ evhttp_start_read(struct evhttp_connection *evcon)
/* If there's still data pending, process it next time through the
* loop. Don't do it now; that could get recusive. */
if (evbuffer_get_length(bufferevent_get_input(evcon->bufev))) {
- event_deferred_cb_schedule(get_deferred_queue(evcon),
+ event_deferred_cb_schedule_(get_deferred_queue(evcon),
&evcon->read_more_deferred_cb);
}
}
@@ -2347,6 +2593,10 @@ evhttp_send_done(struct evhttp_connection *evcon, void *arg)
struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
TAILQ_REMOVE(&evcon->requests, req, next);
+ if (req->on_complete_cb != NULL) {
+ req->on_complete_cb(req, req->on_complete_cb_arg);
+ }
+
need_close =
(REQ_VERSION_BEFORE(req, 1, 1) &&
!evhttp_is_connection_keepalive(req->input_headers))||
@@ -2391,11 +2641,11 @@ evhttp_send_error(struct evhttp_request *req, int error, const char *reason)
reason = evhttp_response_phrase_internal(error);
}
- evhttp_response_code(req, error, reason);
+ evhttp_response_code_(req, error, reason);
evbuffer_add_printf(buf, ERR_FORMAT, error, reason, reason);
- evhttp_send_page(req, buf);
+ evhttp_send_page_(req, buf);
evbuffer_free(buf);
#undef ERR_FORMAT
@@ -2432,7 +2682,7 @@ void
evhttp_send_reply(struct evhttp_request *req, int code, const char *reason,
struct evbuffer *databuf)
{
- evhttp_response_code(req, code, reason);
+ evhttp_response_code_(req, code, reason);
evhttp_send(req, databuf);
}
@@ -2441,7 +2691,7 @@ void
evhttp_send_reply_start(struct evhttp_request *req, int code,
const char *reason)
{
- evhttp_response_code(req, code, reason);
+ evhttp_response_code_(req, code, reason);
if (evhttp_find_header(req->output_headers, "Content-Length") == NULL &&
REQ_VERSION_ATLEAST(req, 1, 1) &&
evhttp_response_needs_body(req)) {
@@ -2461,7 +2711,8 @@ evhttp_send_reply_start(struct evhttp_request *req, int code,
}
void
-evhttp_send_reply_chunk(struct evhttp_request *req, struct evbuffer *databuf)
+evhttp_send_reply_chunk_with_cb(struct evhttp_request *req, struct evbuffer *databuf,
+ void (*cb)(struct evhttp_connection *, void *), void *arg)
{
struct evhttp_connection *evcon = req->evcon;
struct evbuffer *output;
@@ -2483,10 +2734,15 @@ evhttp_send_reply_chunk(struct evhttp_request *req, struct evbuffer *databuf)
if (req->chunked) {
evbuffer_add(output, "\r\n", 2);
}
- evhttp_write_buffer(evcon, NULL, NULL);
+ evhttp_write_buffer(evcon, cb, arg);
}
void
+evhttp_send_reply_chunk(struct evhttp_request *req, struct evbuffer *databuf)
+{
+ evhttp_send_reply_chunk_with_cb(req, databuf, NULL, NULL);
+}
+void
evhttp_send_reply_end(struct evhttp_request *req)
{
struct evhttp_connection *evcon = req->evcon;
@@ -2607,7 +2863,7 @@ evhttp_response_phrase_internal(int code)
}
void
-evhttp_response_code(struct evhttp_request *req, int code, const char *reason)
+evhttp_response_code_(struct evhttp_request *req, int code, const char *reason)
{
req->kind = EVHTTP_RESPONSE;
req->response_code = code;
@@ -2623,7 +2879,7 @@ evhttp_response_code(struct evhttp_request *req, int code, const char *reason)
}
void
-evhttp_send_page(struct evhttp_request *req, struct evbuffer *databuf)
+evhttp_send_page_(struct evhttp_request *req, struct evbuffer *databuf)
{
if (!req->major || !req->minor) {
req->major = 1;
@@ -2631,7 +2887,7 @@ evhttp_send_page(struct evhttp_request *req, struct evbuffer *databuf)
}
if (req->kind != EVHTTP_RESPONSE)
- evhttp_response_code(req, 200, "OK");
+ evhttp_response_code_(req, 200, "OK");
evhttp_clear_headers(req->output_headers);
evhttp_add_header(req->output_headers, "Content-Type", "text/html");
@@ -2715,7 +2971,7 @@ evhttp_encode_uri(const char *str)
* a ?. -1 is deprecated.
* @return the number of bytes written to 'ret'.
*/
-static int
+int
evhttp_decode_uri_internal(
const char *uri, size_t length, char *ret, int decode_plus_ctl)
{
@@ -2731,8 +2987,8 @@ evhttp_decode_uri_internal(
decode_plus = 1;
} else if (c == '+' && decode_plus) {
c = ' ';
- } else if (c == '%' && EVUTIL_ISXDIGIT(uri[i+1]) &&
- EVUTIL_ISXDIGIT(uri[i+2])) {
+ } else if ((i + 2) < length && c == '%' &&
+ EVUTIL_ISXDIGIT_(uri[i+1]) && EVUTIL_ISXDIGIT_(uri[i+2])) {
char tmp[3];
tmp[0] = uri[i+1];
tmp[1] = uri[i+2];
@@ -2920,7 +3176,7 @@ prefix_suffix_match(const char *pattern, const char *name, int ignorecase)
default:
if (c != *name) {
if (!ignorecase ||
- EVUTIL_TOLOWER(c) != EVUTIL_TOLOWER(*name))
+ EVUTIL_TOLOWER_(c) != EVUTIL_TOLOWER_(*name))
return (0);
}
++name;
@@ -3061,13 +3317,13 @@ evhttp_handle_request(struct evhttp_request *req, void *arg)
return;
}
- evhttp_response_code(req, HTTP_NOTFOUND, "Not Found");
+ evhttp_response_code_(req, HTTP_NOTFOUND, "Not Found");
evbuffer_add_printf(buf, ERR_FORMAT, escaped_html);
mm_free(escaped_html);
- evhttp_send_page(req, buf);
+ evhttp_send_page_(req, buf);
evbuffer_free(buf);
#undef ERR_FORMAT
@@ -3129,6 +3385,16 @@ evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd)
return (0);
}
+void
+evhttp_foreach_bound_socket(struct evhttp *http,
+ evhttp_bound_socket_foreach_fn *function,
+ void *argument)
+{
+ struct evhttp_bound_socket *bound;
+
+ TAILQ_FOREACH(bound, &http->sockets, next)
+ function(bound, argument);
+}
struct evhttp_bound_socket *
evhttp_accept_socket_with_handle(struct evhttp *http, evutil_socket_t fd)
@@ -3199,9 +3465,10 @@ evhttp_new_object(void)
return (NULL);
}
- http->timeout = -1;
+ evutil_timerclear(&http->timeout);
evhttp_set_max_headers_size(http, EV_SIZE_MAX);
evhttp_set_max_body_size(http, EV_SIZE_MAX);
+ evhttp_set_default_content_type(http, "text/html; charset=ISO-8859-1");
evhttp_set_allowed_methods(http,
EVHTTP_REQ_GET |
EVHTTP_REQ_POST |
@@ -3370,7 +3637,24 @@ evhttp_remove_server_alias(struct evhttp *http, const char *alias)
void
evhttp_set_timeout(struct evhttp* http, int timeout_in_secs)
{
- http->timeout = timeout_in_secs;
+ if (timeout_in_secs == -1) {
+ evhttp_set_timeout_tv(http, NULL);
+ } else {
+ struct timeval tv;
+ tv.tv_sec = timeout_in_secs;
+ tv.tv_usec = 0;
+ evhttp_set_timeout_tv(http, &tv);
+ }
+}
+
+void
+evhttp_set_timeout_tv(struct evhttp* http, const struct timeval* tv)
+{
+ if (tv) {
+ http->timeout = *tv;
+ } else {
+ evutil_timerclear(&http->timeout);
+ }
}
void
@@ -3392,6 +3676,12 @@ evhttp_set_max_body_size(struct evhttp* http, ev_ssize_t max_body_size)
}
void
+evhttp_set_default_content_type(struct evhttp *http,
+ const char *content_type) {
+ http->default_content_type = content_type;
+}
+
+void
evhttp_set_allowed_methods(struct evhttp* http, ev_uint16_t methods)
{
http->allowed_methods = methods;
@@ -3454,6 +3744,14 @@ evhttp_set_gencb(struct evhttp *http,
http->gencbarg = cbarg;
}
+void
+evhttp_set_bevcb(struct evhttp *http,
+ struct bufferevent* (*cb)(struct event_base *, void *), void *cbarg)
+{
+ http->bevcb = cb;
+ http->bevcbarg = cbarg;
+}
+
/*
* Request related functions
*/
@@ -3573,6 +3871,28 @@ evhttp_request_set_chunked_cb(struct evhttp_request *req,
req->chunk_cb = cb;
}
+void
+evhttp_request_set_header_cb(struct evhttp_request *req,
+ int (*cb)(struct evhttp_request *, void *))
+{
+ req->header_cb = cb;
+}
+
+void
+evhttp_request_set_error_cb(struct evhttp_request *req,
+ void (*cb)(enum evhttp_request_error, void *))
+{
+ req->error_cb = cb;
+}
+
+void
+evhttp_request_set_on_complete_cb(struct evhttp_request *req,
+ void (*cb)(struct evhttp_request *, void *), void *cb_arg)
+{
+ req->on_complete_cb = cb;
+ req->on_complete_cb_arg = cb_arg;
+}
+
/*
* Allows for inspection of the request URI
*/
@@ -3611,7 +3931,7 @@ evhttp_request_get_host(struct evhttp_request *req)
to be consistent with uri_elems case above. */
if (host) {
p = host + strlen(host) - 1;
- while (p > host && EVUTIL_ISDIGIT(*p))
+ while (p > host && EVUTIL_ISDIGIT_(*p))
--p;
if (p > host && *p == ':') {
len = p - host;
@@ -3641,6 +3961,12 @@ evhttp_request_get_response_code(const struct evhttp_request *req)
return req->response_code;
}
+const char *
+evhttp_request_get_response_code_line(const struct evhttp_request *req)
+{
+ return req->response_code_line;
+}
+
/** Returns the input headers */
struct evkeyvalq *evhttp_request_get_input_headers(struct evhttp_request *req)
{
@@ -3678,6 +4004,7 @@ evhttp_get_request_connection(
{
struct evhttp_connection *evcon;
char *hostname = NULL, *portname = NULL;
+ struct bufferevent* bev = NULL;
name_from_addr(sa, salen, &hostname, &portname);
if (hostname == NULL || portname == NULL) {
@@ -3690,8 +4017,11 @@ evhttp_get_request_connection(
__func__, hostname, portname, EV_SOCK_ARG(fd)));
/* we need a connection object to put the http request on */
- evcon = evhttp_connection_base_new(
- http->base, NULL, hostname, atoi(portname));
+ if (http->bevcb != NULL) {
+ bev = (*http->bevcb)(http->base, http->bevcbarg);
+ }
+ evcon = evhttp_connection_base_bufferevent_new(
+ http->base, NULL, bev, hostname, atoi(portname));
mm_free(hostname);
mm_free(portname);
if (evcon == NULL)
@@ -3740,7 +4070,7 @@ evhttp_associate_new_request_with_connection(struct evhttp_connection *evcon)
req->kind = EVHTTP_REQUEST;
- evhttp_start_read(evcon);
+ evhttp_start_read_(evcon);
return (0);
}
@@ -3760,8 +4090,8 @@ evhttp_get_request(struct evhttp *http, evutil_socket_t fd,
}
/* the timeout can be used by the server to close idle connections */
- if (http->timeout != -1)
- evhttp_connection_set_timeout(evcon, http->timeout);
+ if (evutil_timerisset(&http->timeout))
+ evhttp_connection_set_timeout_tv(evcon, &http->timeout);
/*
* if we want to accept more than one request on a connection,
@@ -3788,7 +4118,7 @@ name_from_addr(struct sockaddr *sa, ev_socklen_t salen,
char strport[NI_MAXSERV];
int ni_result;
-#ifdef _EVENT_HAVE_GETNAMEINFO
+#ifdef EVENT__HAVE_GETNAMEINFO
ni_result = getnameinfo(sa, salen,
ntop, sizeof(ntop), strport, sizeof(strport),
NI_NUMERICHOST|NI_NUMERICSERV);
@@ -3826,17 +4156,13 @@ bind_socket_ai(struct evutil_addrinfo *ai, int reuse)
int serrno;
/* Create listen socket */
- fd = socket(ai ? ai->ai_family : AF_INET, SOCK_STREAM, 0);
+ fd = evutil_socket_(ai ? ai->ai_family : AF_INET,
+ SOCK_STREAM|EVUTIL_SOCK_NONBLOCK|EVUTIL_SOCK_CLOEXEC, 0);
if (fd == -1) {
event_sock_warn(-1, "socket");
return (-1);
}
- if (evutil_make_socket_nonblocking(fd) < 0)
- goto out;
- if (evutil_make_socket_closeonexec(fd) < 0)
- goto out;
-
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on))<0)
goto out;
if (reuse) {
@@ -3946,10 +4272,10 @@ scheme_ok(const char *s, const char *eos)
EVUTIL_ASSERT(eos >= s);
if (s == eos)
return 0;
- if (!EVUTIL_ISALPHA(*s))
+ if (!EVUTIL_ISALPHA_(*s))
return 0;
while (++s < eos) {
- if (! EVUTIL_ISALNUM(*s) &&
+ if (! EVUTIL_ISALNUM_(*s) &&
*s != '+' && *s != '-' && *s != '.')
return 0;
}
@@ -3968,8 +4294,8 @@ userinfo_ok(const char *s, const char *eos)
*s == ':')
++s;
else if (*s == '%' && s+2 < eos &&
- EVUTIL_ISXDIGIT(s[1]) &&
- EVUTIL_ISXDIGIT(s[2]))
+ EVUTIL_ISXDIGIT_(s[1]) &&
+ EVUTIL_ISXDIGIT_(s[2]))
s += 3;
else
return 0;
@@ -3985,8 +4311,8 @@ regname_ok(const char *s, const char *eos)
strchr(SUBDELIMS, *s))
++s;
else if (*s == '%' &&
- EVUTIL_ISXDIGIT(s[1]) &&
- EVUTIL_ISXDIGIT(s[2]))
+ EVUTIL_ISXDIGIT_(s[1]) &&
+ EVUTIL_ISXDIGIT_(s[2]))
s += 3;
else
return 0;
@@ -3999,11 +4325,13 @@ parse_port(const char *s, const char *eos)
{
int portnum = 0;
while (s < eos) {
- if (! EVUTIL_ISDIGIT(*s))
+ if (! EVUTIL_ISDIGIT_(*s))
return -1;
portnum = (portnum * 10) + (*s - '0');
if (portnum < 0)
return -1;
+ if (portnum > 65535)
+ return -1;
++s;
}
return portnum;
@@ -4021,10 +4349,10 @@ bracket_addr_ok(const char *s, const char *eos)
*/
s += 2; /* skip [v */
--eos;
- if (!EVUTIL_ISXDIGIT(*s)) /*require at least one*/
+ if (!EVUTIL_ISXDIGIT_(*s)) /*require at least one*/
return 0;
while (s < eos && *s != '.') {
- if (EVUTIL_ISXDIGIT(*s))
+ if (EVUTIL_ISXDIGIT_(*s))
++s;
else
return 0;
@@ -4084,7 +4412,7 @@ parse_authority(struct evhttp_uri *uri, char *s, char *eos)
cp = s;
}
/* Optionally, we end with ":port" */
- for (port=eos-1; port >= cp && EVUTIL_ISDIGIT(*port); --port)
+ for (port=eos-1; port >= cp && EVUTIL_ISDIGIT_(*port); --port)
;
if (port >= cp && *port == ':') {
if (port+1 == eos) /* Leave port unspecified; the RFC allows a
@@ -4168,8 +4496,8 @@ end_of_path(char *cp, enum uri_part part, unsigned flags)
strchr(SUBDELIMS, *cp) ||
*cp == ':' || *cp == '@' || *cp == '/')
++cp;
- else if (*cp == '%' && EVUTIL_ISXDIGIT(cp[1]) &&
- EVUTIL_ISXDIGIT(cp[2]))
+ else if (*cp == '%' && EVUTIL_ISXDIGIT_(cp[1]) &&
+ EVUTIL_ISXDIGIT_(cp[2]))
cp += 3;
else if (*cp == '?' && part != PART_PATH)
++cp;
@@ -4329,20 +4657,20 @@ err:
void
evhttp_uri_free(struct evhttp_uri *uri)
{
-#define _URI_FREE_STR(f) \
+#define URI_FREE_STR_(f) \
if (uri->f) { \
mm_free(uri->f); \
}
- _URI_FREE_STR(scheme);
- _URI_FREE_STR(userinfo);
- _URI_FREE_STR(host);
- _URI_FREE_STR(path);
- _URI_FREE_STR(query);
- _URI_FREE_STR(fragment);
+ URI_FREE_STR_(scheme);
+ URI_FREE_STR_(userinfo);
+ URI_FREE_STR_(host);
+ URI_FREE_STR_(path);
+ URI_FREE_STR_(query);
+ URI_FREE_STR_(fragment);
mm_free(uri);
-#undef _URI_FREE_STR
+#undef URI_FREE_STR_
}
char *
@@ -4352,7 +4680,7 @@ evhttp_uri_join(struct evhttp_uri *uri, char *buf, size_t limit)
size_t joined_size = 0;
char *output = NULL;
-#define _URI_ADD(f) evbuffer_add(tmp, uri->f, strlen(uri->f))
+#define URI_ADD_(f) evbuffer_add(tmp, uri->f, strlen(uri->f))
if (!uri || !buf || !limit)
return NULL;
@@ -4362,14 +4690,14 @@ evhttp_uri_join(struct evhttp_uri *uri, char *buf, size_t limit)
return NULL;
if (uri->scheme) {
- _URI_ADD(scheme);
+ URI_ADD_(scheme);
evbuffer_add(tmp, ":", 1);
}
if (uri->host) {
evbuffer_add(tmp, "//", 2);
if (uri->userinfo)
evbuffer_add_printf(tmp,"%s@", uri->userinfo);
- _URI_ADD(host);
+ URI_ADD_(host);
if (uri->port >= 0)
evbuffer_add_printf(tmp,":%d", uri->port);
@@ -4378,16 +4706,16 @@ evhttp_uri_join(struct evhttp_uri *uri, char *buf, size_t limit)
}
if (uri->path)
- _URI_ADD(path);
+ URI_ADD_(path);
if (uri->query) {
evbuffer_add(tmp, "?", 1);
- _URI_ADD(query);
+ URI_ADD_(query);
}
if (uri->fragment) {
evbuffer_add(tmp, "#", 1);
- _URI_ADD(fragment);
+ URI_ADD_(fragment);
}
evbuffer_add(tmp, "\0", 1); /* NUL */
@@ -4406,7 +4734,7 @@ err:
evbuffer_free(tmp);
return output;
-#undef _URI_ADD
+#undef URI_ADD_
}
const char *
@@ -4445,7 +4773,7 @@ evhttp_uri_get_fragment(const struct evhttp_uri *uri)
return uri->fragment;
}
-#define _URI_SET_STR(f) do { \
+#define URI_SET_STR_(f) do { \
if (uri->f) \
mm_free(uri->f); \
if (f) { \
@@ -4464,7 +4792,7 @@ evhttp_uri_set_scheme(struct evhttp_uri *uri, const char *scheme)
if (scheme && !scheme_ok(scheme, scheme+strlen(scheme)))
return -1;
- _URI_SET_STR(scheme);
+ URI_SET_STR_(scheme);
return 0;
}
int
@@ -4472,7 +4800,7 @@ evhttp_uri_set_userinfo(struct evhttp_uri *uri, const char *userinfo)
{
if (userinfo && !userinfo_ok(userinfo, userinfo+strlen(userinfo)))
return -1;
- _URI_SET_STR(userinfo);
+ URI_SET_STR_(userinfo);
return 0;
}
int
@@ -4488,7 +4816,7 @@ evhttp_uri_set_host(struct evhttp_uri *uri, const char *host)
}
}
- _URI_SET_STR(host);
+ URI_SET_STR_(host);
return 0;
}
int
@@ -4508,7 +4836,7 @@ evhttp_uri_set_path(struct evhttp_uri *uri, const char *path)
if (path && end_of_cpath(path, PART_PATH, uri->flags) != path+strlen(path))
return -1;
- _URI_SET_STR(path);
+ URI_SET_STR_(path);
return 0;
}
int
@@ -4516,7 +4844,7 @@ evhttp_uri_set_query(struct evhttp_uri *uri, const char *query)
{
if (query && end_of_cpath(query, PART_QUERY, uri->flags) != query+strlen(query))
return -1;
- _URI_SET_STR(query);
+ URI_SET_STR_(query);
return 0;
}
int
@@ -4524,6 +4852,6 @@ evhttp_uri_set_fragment(struct evhttp_uri *uri, const char *fragment)
{
if (fragment && end_of_cpath(fragment, PART_FRAGMENT, uri->flags) != fragment+strlen(fragment))
return -1;
- _URI_SET_STR(fragment);
+ URI_SET_STR_(fragment);
return 0;
}
diff --git a/include/Makefile.am b/include/Makefile.am
deleted file mode 100644
index 5153db27..00000000
--- a/include/Makefile.am
+++ /dev/null
@@ -1,47 +0,0 @@
-# include/Makefile.am for libevent
-# Copyright 2000-2007 Niels Provos
-# Copyright 2007-2012 Niels Provos and Nick Mathewson
-#
-# See LICENSE for copying information.
-
-AUTOMAKE_OPTIONS = foreign
-
-EVENT2_EXPORT = \
- event2/buffer.h \
- event2/buffer_compat.h \
- event2/bufferevent.h \
- event2/bufferevent_compat.h \
- event2/bufferevent_ssl.h \
- event2/bufferevent_struct.h \
- event2/dns.h \
- event2/dns_compat.h \
- event2/dns_struct.h \
- event2/event.h \
- event2/event_compat.h \
- event2/event_struct.h \
- event2/http.h \
- event2/http_compat.h \
- event2/http_struct.h \
- event2/keyvalq_struct.h \
- event2/listener.h \
- event2/rpc.h \
- event2/rpc_compat.h \
- event2/rpc_struct.h \
- event2/tag.h \
- event2/tag_compat.h \
- event2/thread.h \
- event2/util.h
-
-EXTRA_SRC = $(EVENT2_EXPORT)
-
-## Without the nobase_ prefixing, Automake would strip "event2/" from
-## the source header filename to derive the installed header filename.
-## With nobase_ the installed path is $(includedir)/event2/ev*.h.
-
-if INSTALL_LIBEVENT
-nobase_include_HEADERS = $(EVENT2_EXPORT)
-nobase_nodist_include_HEADERS = ./event2/event-config.h
-else
-noinst_HEADERS = $(EVENT2_EXPORT)
-nodist_noinst_HEADERS = ./event2/event-config.h
-endif
diff --git a/evdns.h b/include/evdns.h
index 513f9b70..8672db03 100644
--- a/evdns.h
+++ b/include/evdns.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVDNS_H_
-#define _EVDNS_H_
+#ifndef EVENT1_EVDNS_H_INCLUDED_
+#define EVENT1_EVDNS_H_INCLUDED_
/** @file evdns.h
@@ -42,4 +42,4 @@
#include <event2/dns_compat.h>
#include <event2/dns_struct.h>
-#endif /* _EVDNS_H_ */
+#endif /* EVENT1_EVDNS_H_INCLUDED_ */
diff --git a/event.h b/include/event.h
index a3785b98..3a1b0c2c 100644
--- a/event.h
+++ b/include/event.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT_H_
-#define _EVENT_H_
+#ifndef EVENT1_EVENT_H_INCLUDED_
+#define EVENT1_EVENT_H_INCLUDED_
/** @file event.h
@@ -42,13 +42,13 @@ extern "C" {
#endif
#include <event2/event-config.h>
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
-#ifdef _EVENT_HAVE_STDINT_H
+#ifdef EVENT__HAVE_STDINT_H
#include <stdint.h>
#endif
#include <stdarg.h>
@@ -56,7 +56,7 @@ extern "C" {
/* For int types. */
#include <evutil.h>
-#ifdef WIN32
+#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
@@ -82,4 +82,4 @@ typedef unsigned short u_short;
}
#endif
-#endif /* _EVENT_H_ */
+#endif /* EVENT1_EVENT_H_INCLUDED_ */
diff --git a/include/event2/buffer.h b/include/event2/buffer.h
index 5c896558..03a18984 100644
--- a/include/event2/buffer.h
+++ b/include/event2/buffer.h
@@ -23,8 +23,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_BUFFER_H_
-#define _EVENT2_BUFFER_H_
+#ifndef EVENT2_BUFFER_H_INCLUDED_
+#define EVENT2_BUFFER_H_INCLUDED_
/** @file event2/buffer.h
@@ -72,16 +72,18 @@
evbuffer_peek() if you don't require that the memory to be contiguous.
*/
+#include <event2/visibility.h>
+
#ifdef __cplusplus
extern "C" {
#endif
#include <event2/event-config.h>
#include <stdarg.h>
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_SYS_UIO_H
+#ifdef EVENT__HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#include <event2/util.h>
@@ -93,7 +95,7 @@ extern "C" {
@see event2/event.h for more information
*/
struct evbuffer
-#ifdef _EVENT_IN_DOXYGEN
+#ifdef EVENT_IN_DOXYGEN_
{}
#endif
;
@@ -103,17 +105,23 @@ struct evbuffer
Used when repeatedly searching through a buffer. Calling any function
that modifies or re-packs the buffer contents may invalidate all
- evbuffer_ptrs for that buffer. Do not modify these values except with
- evbuffer_ptr_set.
+ evbuffer_ptrs for that buffer. Do not modify or contruct these values
+ except with evbuffer_ptr_set.
+
+ An evbuffer_ptr can represent any position from the start of a buffer up
+ to a position immediately after the end of a buffer.
+
+ @see evbuffer_ptr_set()
*/
struct evbuffer_ptr {
ev_ssize_t pos;
- /* Do not alter the values of fields. */
+ /* Do not alter or rely on the values of fields: they are for internal
+ * use */
struct {
void *chain;
size_t pos_in_chain;
- } _internal;
+ } internal_;
};
/** Describes a single extent of memory inside an evbuffer. Used for
@@ -121,10 +129,10 @@ struct evbuffer_ptr {
@see evbuffer_reserve_space, evbuffer_commit_space, evbuffer_peek
*/
-#ifdef _EVENT_HAVE_SYS_UIO_H
+#ifdef EVENT__HAVE_SYS_UIO_H
#define evbuffer_iovec iovec
/* Internal use -- defined only if we are using the native struct iovec */
-#define _EVBUFFER_IOVEC_IS_NATIVE
+#define EVBUFFER_IOVEC_IS_NATIVE_
#else
struct evbuffer_iovec {
/** The start of the extent of memory. */
@@ -140,12 +148,14 @@ struct evbuffer_iovec {
@return a pointer to a newly allocated evbuffer struct, or NULL if an error
occurred
*/
+EVENT2_EXPORT_SYMBOL
struct evbuffer *evbuffer_new(void);
/**
Deallocate storage for an evbuffer.
@param buf pointer to the evbuffer to be freed
*/
+EVENT2_EXPORT_SYMBOL
void evbuffer_free(struct evbuffer *buf);
/**
@@ -160,18 +170,21 @@ void evbuffer_free(struct evbuffer *buf);
@param lock A lock object, or NULL if we should allocate our own.
@return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_enable_locking(struct evbuffer *buf, void *lock);
/**
Acquire the lock on an evbuffer. Has no effect if locking was not enabled
with evbuffer_enable_locking.
*/
+EVENT2_EXPORT_SYMBOL
void evbuffer_lock(struct evbuffer *buf);
/**
Release the lock on an evbuffer. Has no effect if locking was not enabled
with evbuffer_enable_locking.
*/
+EVENT2_EXPORT_SYMBOL
void evbuffer_unlock(struct evbuffer *buf);
@@ -199,6 +212,7 @@ void evbuffer_unlock(struct evbuffer *buf);
* @param flags One or more EVBUFFER_FLAG_* options
* @return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_set_flags(struct evbuffer *buf, ev_uint64_t flags);
/** Change the flags that are set for an evbuffer by removing some.
*
@@ -207,6 +221,7 @@ int evbuffer_set_flags(struct evbuffer *buf, ev_uint64_t flags);
* @param flags One or more EVBUFFER_FLAG_* options
* @return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags);
/**
@@ -215,6 +230,7 @@ int evbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags);
@param buf pointer to the evbuffer
@return the number of bytes stored in the evbuffer
*/
+EVENT2_EXPORT_SYMBOL
size_t evbuffer_get_length(const struct evbuffer *buf);
/**
@@ -229,6 +245,7 @@ size_t evbuffer_get_length(const struct evbuffer *buf);
@return 0 if no data is available, otherwise the number of available bytes
in the first buffer chain.
*/
+EVENT2_EXPORT_SYMBOL
size_t evbuffer_get_contiguous_space(const struct evbuffer *buf);
/**
@@ -241,6 +258,7 @@ size_t evbuffer_get_contiguous_space(const struct evbuffer *buf);
@param datlen the new minimum length requirement
@return 0 if successful, or -1 if an error occurred
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_expand(struct evbuffer *buf, size_t datlen);
/**
@@ -276,6 +294,7 @@ int evbuffer_expand(struct evbuffer *buf, size_t datlen);
@return the number of provided extents, or -1 on error.
@see evbuffer_commit_space()
*/
+EVENT2_EXPORT_SYMBOL
int
evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
struct evbuffer_iovec *vec, int n_vec);
@@ -301,6 +320,7 @@ evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
@return 0 on success, -1 on error
@see evbuffer_reserve_space()
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_commit_space(struct evbuffer *buf,
struct evbuffer_iovec *vec, int n_vecs);
@@ -312,6 +332,7 @@ int evbuffer_commit_space(struct evbuffer *buf,
@param datlen the number of bytes to be copied from the data buffer
@return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen);
@@ -326,6 +347,7 @@ int evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen);
@param datlen the maximum size of the destination buffer
@return the number of bytes read, or -1 if we can't drain the buffer.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);
/**
@@ -339,9 +361,25 @@ int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);
@param datlen the maximum size of the destination buffer
@return the number of bytes read, or -1 if we can't drain the buffer.
*/
+EVENT2_EXPORT_SYMBOL
ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen);
/**
+ Read data from the middle of an evbuffer, and leave the buffer unchanged.
+
+ If more bytes are requested than are available in the evbuffer, we
+ only extract as many bytes as were available.
+
+ @param buf the evbuffer to be read from
+ @param pos the position to start reading from
+ @param data_out the destination buffer to store the result
+ @param datlen the maximum size of the destination buffer
+ @return the number of bytes read, or -1 if we can't drain the buffer.
+ */
+EVENT2_EXPORT_SYMBOL
+ev_ssize_t evbuffer_copyout_from(struct evbuffer *buf, const struct evbuffer_ptr *pos, void *data_out, size_t datlen);
+
+/**
Read data from an evbuffer into another evbuffer, draining
the bytes from the source buffer. This function avoids copy
operations to the extent possible.
@@ -354,6 +392,7 @@ ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen)
@param datlen the maximum numbers of bytes to transfer
@return the number of bytes read
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst,
size_t datlen);
@@ -376,7 +415,9 @@ enum evbuffer_eol_style {
/** An EOL is a CR followed by an LF. */
EVBUFFER_EOL_CRLF_STRICT,
/** An EOL is a LF. */
- EVBUFFER_EOL_LF
+ EVBUFFER_EOL_LF,
+ /** An EOL is a NUL character (that is, a single byte with value 0) */
+ EVBUFFER_EOL_NUL
};
/**
@@ -393,6 +434,7 @@ enum evbuffer_eol_style {
* @param eol_style the style of line-ending to use.
* @return pointer to a single line, or NULL if an error occurred
*/
+EVENT2_EXPORT_SYMBOL
char *evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
enum evbuffer_eol_style eol_style);
@@ -408,9 +450,27 @@ char *evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
@see evbuffer_remove_buffer()
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf);
/**
+ Copy data from one evbuffer into another evbuffer.
+
+ This is a non-destructive add. The data from one buffer is copied
+ into the other buffer. However, no unnecessary memory copies occur.
+
+ Note that buffers already containing buffer references can't be added
+ to other buffers.
+
+ @param outbuf the output buffer
+ @param inbuf the input buffer
+ @return 0 if successful, or -1 if an error occurred
+ */
+EVENT2_EXPORT_SYMBOL
+int evbuffer_add_buffer_reference(struct evbuffer *outbuf,
+ struct evbuffer *inbuf);
+
+/**
A cleanup function for a piece of memory added to an evbuffer by
reference.
@@ -434,6 +494,7 @@ typedef void (*evbuffer_ref_cleanup_cb)(const void *data,
@param cleanupfn_arg optional argument to the cleanup callback
@return 0 if successful, or -1 if an error occurred
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_add_reference(struct evbuffer *outbuf,
const void *data, size_t datlen,
evbuffer_ref_cleanup_cb cleanupfn, void *cleanupfn_arg);
@@ -452,17 +513,140 @@ int evbuffer_add_reference(struct evbuffer *outbuf,
The results of using evbuffer_remove() or evbuffer_pullup() on
evbuffers whose data was added using this function are undefined.
+ For more fine-grained control, use evbuffer_add_file_segment.
+
@param outbuf the output buffer
@param fd the file descriptor
@param offset the offset from which to read data
- @param length how much data to read
+ @param length how much data to read, or -1 to read as much as possible.
+ (-1 requires that 'fd' support fstat.)
@return 0 if successful, or -1 if an error occurred
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_add_file(struct evbuffer *outbuf, int fd, ev_off_t offset,
ev_off_t length);
/**
+ An evbuffer_file_segment holds a reference to a range of a file --
+ possibly the whole file! -- for use in writing from an evbuffer to a
+ socket. It could be implemented with mmap, sendfile, splice, or (if all
+ else fails) by just pulling all the data into RAM. A single
+ evbuffer_file_segment can be added more than once, and to more than one
+ evbuffer.
+ */
+struct evbuffer_file_segment;
+
+/**
+ Flag for creating evbuffer_file_segment: If this flag is set, then when
+ the evbuffer_file_segment is freed and no longer in use by any
+ evbuffer, the underlying fd is closed.
+ */
+#define EVBUF_FS_CLOSE_ON_FREE 0x01
+/**
+ Flag for creating evbuffer_file_segment: Disable memory-map based
+ implementations.
+ */
+#define EVBUF_FS_DISABLE_MMAP 0x02
+/**
+ Flag for creating evbuffer_file_segment: Disable direct fd-to-fd
+ implementations (including sendfile and splice).
+
+ You might want to use this option if data needs to be taken from the
+ evbuffer by any means other than writing it to the network: the sendfile
+ backend is fast, but it only works for sending files directly to the
+ network.
+ */
+#define EVBUF_FS_DISABLE_SENDFILE 0x04
+/**
+ Flag for creating evbuffer_file_segment: Do not allocate a lock for this
+ segment. If this option is set, then neither the segment nor any
+ evbuffer it is added to may ever be accessed from more than one thread
+ at a time.
+ */
+#define EVBUF_FS_DISABLE_LOCKING 0x08
+
+/**
+ A cleanup function for a evbuffer_file_segment added to an evbuffer
+ for reference.
+ */
+typedef void (*evbuffer_file_segment_cleanup_cb)(
+ struct evbuffer_file_segment const* seg, int flags, void* arg);
+
+/**
+ Create and return a new evbuffer_file_segment for reading data from a
+ file and sending it out via an evbuffer.
+
+ This function avoids unnecessary data copies between userland and
+ kernel. Where available, it uses sendfile or splice.
+
+ The file descriptor must not be closed so long as any evbuffer is using
+ this segment.
+
+ The results of using evbuffer_remove() or evbuffer_pullup() or any other
+ function that reads bytes from an evbuffer on any evbuffer containing
+ the newly returned segment are undefined, unless you pass the
+ EVBUF_FS_DISABLE_SENDFILE flag to this function.
+
+ @param fd an open file to read from.
+ @param offset an index within the file at which to start reading
+ @param length how much data to read, or -1 to read as much as possible.
+ (-1 requires that 'fd' support fstat.)
+ @param flags any number of the EVBUF_FS_* flags
+ @return a new evbuffer_file_segment, or NULL on failure.
+ **/
+EVENT2_EXPORT_SYMBOL
+struct evbuffer_file_segment *evbuffer_file_segment_new(
+ int fd, ev_off_t offset, ev_off_t length, unsigned flags);
+
+/**
+ Free an evbuffer_file_segment
+
+ It is safe to call this function even if the segment has been added to
+ one or more evbuffers. The evbuffer_file_segment will not be freed
+ until no more references to it exist.
+ */
+EVENT2_EXPORT_SYMBOL
+void evbuffer_file_segment_free(struct evbuffer_file_segment *seg);
+
+/**
+ Add cleanup callback and argument for the callback to an
+ evbuffer_file_segment.
+
+ The cleanup callback will be invoked when no more references to the
+ evbuffer_file_segment exist.
+ **/
+EVENT2_EXPORT_SYMBOL
+void evbuffer_file_segment_add_cleanup_cb(struct evbuffer_file_segment *seg,
+ evbuffer_file_segment_cleanup_cb cb, void* arg);
+
+/**
+ Insert some or all of an evbuffer_file_segment at the end of an evbuffer
+
+ Note that the offset and length parameters of this function have a
+ different meaning from those provided to evbuffer_file_segment_new: When
+ you create the segment, the offset is the offset _within the file_, and
+ the length is the length _of the segment_, whereas when you add a
+ segment to an evbuffer, the offset is _within the segment_ and the
+ length is the length of the _part of the segment you want to use.
+
+ In other words, if you have a 10 KiB file, and you create an
+ evbuffer_file_segment for it with offset 20 and length 1000, it will
+ refer to bytes 20..1019 inclusive. If you then pass this segment to
+ evbuffer_add_file_segment and specify an offset of 20 and a length of
+ 50, you will be adding bytes 40..99 inclusive.
+
+ @param buf the evbuffer to append to
+ @param seg the segment to add
+ @param offset the offset within the segment to start from
+ @param length the amount of data to add, or -1 to add it all.
+ @return 0 on success, -1 on failure.
+ */
+EVENT2_EXPORT_SYMBOL
+int evbuffer_add_file_segment(struct evbuffer *buf,
+ struct evbuffer_file_segment *seg, ev_off_t offset, ev_off_t length);
+
+/**
Append a formatted string to the end of an evbuffer.
The string is formated as printf.
@@ -474,6 +658,7 @@ int evbuffer_add_file(struct evbuffer *outbuf, int fd, ev_off_t offset,
@see evutil_printf(), evbuffer_add_vprintf()
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
#ifdef __GNUC__
__attribute__((format(printf, 2, 3)))
@@ -488,6 +673,7 @@ int evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
@param ap a varargs va_list argument array that will be passed to vprintf(3)
@return The number of bytes added if successful, or -1 if an error occurred.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
#ifdef __GNUC__
__attribute__((format(printf, 2, 0)))
@@ -502,6 +688,7 @@ int evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
@param len the number of bytes to drain from the beginning of the buffer
@return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_drain(struct evbuffer *buf, size_t len);
@@ -515,6 +702,7 @@ int evbuffer_drain(struct evbuffer *buf, size_t len);
@return the number of bytes written, or -1 if an error occurred
@see evbuffer_read()
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd);
/**
@@ -529,6 +717,7 @@ int evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd);
@return the number of bytes written, or -1 if an error occurred
@see evbuffer_read()
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd,
ev_ssize_t howmuch);
@@ -541,6 +730,7 @@ int evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd,
@return the number of bytes read, or -1 if an error occurred
@see evbuffer_write()
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_read(struct evbuffer *buffer, evutil_socket_t fd, int howmuch);
/**
@@ -554,6 +744,7 @@ int evbuffer_read(struct evbuffer *buffer, evutil_socket_t fd, int howmuch);
first occurrence of the string in the buffer after 'start'. The 'pos'
field of the result is -1 if the string was not found.
*/
+EVENT2_EXPORT_SYMBOL
struct evbuffer_ptr evbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start);
/**
@@ -570,6 +761,7 @@ struct evbuffer_ptr evbuffer_search(struct evbuffer *buffer, const char *what, s
first occurrence of the string in the buffer after 'start'. The 'pos'
field of the result is -1 if the string was not found.
*/
+EVENT2_EXPORT_SYMBOL
struct evbuffer_ptr evbuffer_search_range(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start, const struct evbuffer_ptr *end);
/**
@@ -587,15 +779,25 @@ enum evbuffer_ptr_how {
/**
Sets the search pointer in the buffer to position.
- If evbuffer_ptr is not initialized. This function can only be called
+ There are two ways to use this function: you can call
+ evbuffer_ptr_set(buf, &pos, N, EVBUFFER_PTR_SET)
+ to move 'pos' to a position 'N' bytes after the start of the buffer, or
+ evbuffer_ptr_set(buf, &pos, N, EVBUFFER_PTR_SET)
+ to move 'pos' forward by 'N' bytes.
+
+ If evbuffer_ptr is not initialized, this function can only be called
with EVBUFFER_PTR_SET.
+ An evbuffer_ptr can represent any position from the start of the buffer to
+ a position immediately after the end of the buffer.
+
@param buffer the evbuffer to be search
@param ptr a pointer to a struct evbuffer_ptr
@param position the position at which to start the next search
@param how determines how the pointer should be manipulated.
@returns 0 on success or -1 otherwise
*/
+EVENT2_EXPORT_SYMBOL
int
evbuffer_ptr_set(struct evbuffer *buffer, struct evbuffer_ptr *ptr,
size_t position, enum evbuffer_ptr_how how);
@@ -614,6 +816,7 @@ evbuffer_ptr_set(struct evbuffer *buffer, struct evbuffer_ptr *ptr,
first occurrence EOL in the buffer after 'start'. The 'pos'
field of the result is -1 if the string was not found.
*/
+EVENT2_EXPORT_SYMBOL
struct evbuffer_ptr evbuffer_search_eol(struct evbuffer *buffer,
struct evbuffer_ptr *start, size_t *eol_len_out,
enum evbuffer_eol_style eol_style);
@@ -646,6 +849,7 @@ struct evbuffer_ptr evbuffer_search_eol(struct evbuffer *buffer,
than n_vec if we would need more to return all the data that was
requested.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
struct evbuffer_ptr *start_at,
struct evbuffer_iovec *vec_out, int n_vec);
@@ -698,6 +902,7 @@ struct evbuffer_cb_entry;
@param cbarg an argument to be provided to the callback function
@return a handle to the callback on success, or NULL on failure.
*/
+EVENT2_EXPORT_SYMBOL
struct evbuffer_cb_entry *evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
/** Remove a callback from an evbuffer, given a handle returned from
@@ -708,6 +913,7 @@ struct evbuffer_cb_entry *evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_f
@return 0 if a callback was removed, or -1 if no matching callback was
found.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_remove_cb_entry(struct evbuffer *buffer,
struct evbuffer_cb_entry *ent);
@@ -717,6 +923,7 @@ int evbuffer_remove_cb_entry(struct evbuffer *buffer,
@return 0 if a callback was removed, or -1 if no matching callback was
found.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
/** If this flag is not set, then a callback is temporarily disabled, and
@@ -733,6 +940,7 @@ int evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg
@param flags EVBUFFER_CB_ENABLED to re-enable the callback.
@return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_cb_set_flags(struct evbuffer *buffer,
struct evbuffer_cb_entry *cb, ev_uint32_t flags);
@@ -743,6 +951,7 @@ int evbuffer_cb_set_flags(struct evbuffer *buffer,
@param flags EVBUFFER_CB_ENABLED to disable the callback.
@return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_cb_clear_flags(struct evbuffer *buffer,
struct evbuffer_cb_entry *cb, ev_uint32_t flags);
@@ -756,6 +965,7 @@ int evbuffer_cb_clear_flags(struct evbuffer *buffer,
@param the buffer that the callback is watching.
@param cb the callback we want to suspend.
*/
+EVENT2_EXPORT_SYMBOL
void evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
/** Stop postponing a callback that we postponed with evbuffer_cb_suspend.
@@ -765,6 +975,7 @@ void evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
@param the buffer that the callback is watching.
@param cb the callback we want to stop suspending.
*/
+EVENT2_EXPORT_SYMBOL
void evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
#endif
@@ -774,9 +985,11 @@ void evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb
@param buf the evbuffer to make contiguous
@param size the number of bytes to make contiguous, or -1 to make the
entire buffer contiguous.
- @return a pointer to the contiguous memory array
+ @return a pointer to the contiguous memory array, or NULL if param size
+ requested more data than is present in the buffer.
*/
+EVENT2_EXPORT_SYMBOL
unsigned char *evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size);
/**
@@ -788,6 +1001,7 @@ unsigned char *evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size);
@return 0 if successful, or -1 otherwise
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_prepend(struct evbuffer *buf, const void *data, size_t size);
/**
@@ -798,6 +1012,7 @@ int evbuffer_prepend(struct evbuffer *buf, const void *data, size_t size);
@param src the evbuffer to prepend; it will be emptied as a result
@return 0 if successful, or -1 otherwise
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_prepend_buffer(struct evbuffer *dst, struct evbuffer* src);
/**
@@ -814,6 +1029,7 @@ int evbuffer_prepend_buffer(struct evbuffer *dst, struct evbuffer* src);
we freeze the back.
@return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_freeze(struct evbuffer *buf, int at_front);
/**
Re-enable calls that modify an evbuffer.
@@ -823,6 +1039,7 @@ int evbuffer_freeze(struct evbuffer *buf, int at_front);
we unfreeze the back.
@return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_unfreeze(struct evbuffer *buf, int at_front);
struct event_base;
@@ -833,10 +1050,27 @@ struct event_base;
This can be used to serialize all the callbacks to a single thread
of execution.
*/
+EVENT2_EXPORT_SYMBOL
int evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base);
+/**
+ Append data from 1 or more iovec's to an evbuffer
+
+ Calculates the number of bytes needed for an iovec structure and guarantees
+ all data will fit into a single chain. Can be used in lieu of functionality
+ which calls evbuffer_add() constantly before being used to increase
+ performance.
+
+ @param buffer the destination buffer
+ @param vec the source iovec
+ @param n_vec the number of iovec structures.
+ @return the number of bytes successfully written to the output buffer.
+*/
+EVENT2_EXPORT_SYMBOL
+size_t evbuffer_add_iovec(struct evbuffer * buffer, struct evbuffer_iovec * vec, int n_vec);
+
#ifdef __cplusplus
}
#endif
-#endif /* _EVENT2_BUFFER_H_ */
+#endif /* EVENT2_BUFFER_H_INCLUDED_ */
diff --git a/include/event2/buffer_compat.h b/include/event2/buffer_compat.h
index a713ee32..c0bdd9d2 100644
--- a/include/event2/buffer_compat.h
+++ b/include/event2/buffer_compat.h
@@ -24,8 +24,10 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_BUFFER_COMPAT_H_
-#define _EVENT2_BUFFER_COMPAT_H_
+#ifndef EVENT2_BUFFER_COMPAT_H_INCLUDED_
+#define EVENT2_BUFFER_COMPAT_H_INCLUDED_
+
+#include <event2/visibility.h>
/** @file event2/buffer_compat.h
@@ -45,6 +47,7 @@
@return pointer to a single line, or NULL if an error occurred
*/
+EVENT2_EXPORT_SYMBOL
char *evbuffer_readline(struct evbuffer *buffer);
/** Type definition for a callback that is invoked whenever data is added or
@@ -88,6 +91,7 @@ typedef void (*evbuffer_cb)(struct evbuffer *buffer, size_t old_len, size_t new_
or NULL to remove all callbacks.
@param cbarg an argument to be provided to the callback function
*/
+EVENT2_EXPORT_SYMBOL
void evbuffer_setcb(struct evbuffer *buffer, evbuffer_cb cb, void *cbarg);
@@ -99,6 +103,7 @@ void evbuffer_setcb(struct evbuffer *buffer, evbuffer_cb cb, void *cbarg);
@param len the length of the search string
@return a pointer to the beginning of the search string, or NULL if the search failed.
*/
+EVENT2_EXPORT_SYMBOL
unsigned char *evbuffer_find(struct evbuffer *buffer, const unsigned char *what, size_t len);
/** deprecated in favor of calling the functions directly */
diff --git a/include/event2/bufferevent.h b/include/event2/bufferevent.h
index 0cc6cb1c..fe8a74d3 100644
--- a/include/event2/bufferevent.h
+++ b/include/event2/bufferevent.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_BUFFEREVENT_H_
-#define _EVENT2_BUFFEREVENT_H_
+#ifndef EVENT2_BUFFEREVENT_H_INCLUDED_
+#define EVENT2_BUFFEREVENT_H_INCLUDED_
/**
@file event2/bufferevent.h
@@ -74,15 +74,17 @@
</dl>
*/
+#include <event2/visibility.h>
+
#ifdef __cplusplus
extern "C" {
#endif
#include <event2/event-config.h>
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -109,7 +111,7 @@ extern "C" {
@see event2/bufferevent.h
*/
struct bufferevent
-#ifdef _EVENT_IN_DOXYGEN
+#ifdef EVENT_IN_DOXYGEN_
{}
#endif
;
@@ -138,6 +140,9 @@ typedef void (*bufferevent_data_cb)(struct bufferevent *bev, void *ctx);
The event callback is triggered if either an EOF condition or another
unrecoverable error was encountered.
+ For bufferevents with deferred callbacks, this is a bitwise OR of all errors
+ that have happened on the bufferevent since the last callback invocation.
+
@param bev the bufferevent for which the error condition was reached
@param what a conjunction of flags: BEV_EVENT_READING or BEV_EVENT_WRITING
to indicate if the error was encountered on the read or write path,
@@ -181,6 +186,7 @@ enum bufferevent_options {
error occurred
@see bufferevent_free()
*/
+EVENT2_EXPORT_SYMBOL
struct bufferevent *bufferevent_socket_new(struct event_base *base, evutil_socket_t fd, int options);
/**
@@ -202,6 +208,7 @@ struct bufferevent *bufferevent_socket_new(struct event_base *base, evutil_socke
@param socklen The length of the address
@return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_socket_connect(struct bufferevent *, struct sockaddr *, int);
struct evdns_base;
@@ -231,6 +238,7 @@ struct evdns_base;
may block while it waits for a DNS response. This is probably not
what you want.
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_socket_connect_hostname(struct bufferevent *,
struct evdns_base *, int, const char *, int);
@@ -242,6 +250,7 @@ int bufferevent_socket_connect_hostname(struct bufferevent *,
@return DNS error code.
@see evutil_gai_strerror()
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_socket_get_dns_error(struct bufferevent *bev);
/**
@@ -255,11 +264,13 @@ int bufferevent_socket_get_dns_error(struct bufferevent *bev);
@return 0 if successful, or -1 if an error occurred
@see bufferevent_new()
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
/**
Return the event_base used by a bufferevent
*/
+EVENT2_EXPORT_SYMBOL
struct event_base *bufferevent_get_base(struct bufferevent *bev);
/**
@@ -271,14 +282,26 @@ struct event_base *bufferevent_get_base(struct bufferevent *bev);
@param pri the priority to be assigned
@return 0 if successful, or -1 if an error occurred
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_priority_set(struct bufferevent *bufev, int pri);
+/**
+ Return the priority of a bufferevent.
+
+ Only supported for socket bufferevents
+ */
+EVENT2_EXPORT_SYMBOL
+int bufferevent_get_priority(const struct bufferevent *bufev);
/**
Deallocate the storage associated with a bufferevent structure.
+ If there is pending data to write on the bufferevent, it probably won't be
+ flushed before the bufferevent is freed.
+
@param bufev the bufferevent structure to be freed.
*/
+EVENT2_EXPORT_SYMBOL
void bufferevent_free(struct bufferevent *bufev);
@@ -296,29 +319,54 @@ void bufferevent_free(struct bufferevent *bufev);
(readcb, writecb, and errorcb)
@see bufferevent_new()
*/
+EVENT2_EXPORT_SYMBOL
void bufferevent_setcb(struct bufferevent *bufev,
bufferevent_data_cb readcb, bufferevent_data_cb writecb,
bufferevent_event_cb eventcb, void *cbarg);
/**
+ Retrieves the callbacks for a bufferevent.
+
+ @param bufev the bufferevent to examine.
+ @param readcb_ptr if readcb_ptr is nonnull, *readcb_ptr is set to the current
+ read callback for the bufferevent.
+ @param writecb_ptr if writecb_ptr is nonnull, *writecb_ptr is set to the
+ current write callback for the bufferevent.
+ @param eventcb_ptr if eventcb_ptr is nonnull, *eventcb_ptr is set to the
+ current event callback for the bufferevent.
+ @param cbarg_ptr if cbarg_ptr is nonnull, *cbarg_ptr is set to the current
+ callback argument for the bufferevent.
+ @see buffervent_setcb()
+*/
+EVENT2_EXPORT_SYMBOL
+void bufferevent_getcb(struct bufferevent *bufev,
+ bufferevent_data_cb *readcb_ptr,
+ bufferevent_data_cb *writecb_ptr,
+ bufferevent_event_cb *eventcb_ptr,
+ void **cbarg_ptr);
+
+/**
Changes the file descriptor on which the bufferevent operates.
Not supported for all bufferevent types.
@param bufev the bufferevent object for which to change the file descriptor
@param fd the file descriptor to operate on
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_setfd(struct bufferevent *bufev, evutil_socket_t fd);
/**
Returns the file descriptor associated with a bufferevent, or -1 if
no file descriptor is associated with the bufferevent.
*/
+EVENT2_EXPORT_SYMBOL
evutil_socket_t bufferevent_getfd(struct bufferevent *bufev);
/**
Returns the underlying bufferevent associated with a bufferevent (if
the bufferevent is a wrapper), or NULL if there is no underlying bufferevent.
*/
+EVENT2_EXPORT_SYMBOL
struct bufferevent *bufferevent_get_underlying(struct bufferevent *bufev);
/**
@@ -334,6 +382,7 @@ struct bufferevent *bufferevent_get_underlying(struct bufferevent *bufev);
@return 0 if successful, or -1 if an error occurred
@see bufferevent_write_buffer()
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_write(struct bufferevent *bufev,
const void *data, size_t size);
@@ -347,6 +396,7 @@ int bufferevent_write(struct bufferevent *bufev,
@return 0 if successful, or -1 if an error occurred
@see bufferevent_write()
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
@@ -360,6 +410,7 @@ int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
@param size the size of the data buffer, in bytes
@return the amount of data read, in bytes.
*/
+EVENT2_EXPORT_SYMBOL
size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
/**
@@ -370,6 +421,7 @@ size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
@param buf the evbuffer to which to add data
@return 0 if successful, or -1 if an error occurred.
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf);
/**
@@ -381,6 +433,7 @@ int bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf);
@return the evbuffer object for the input buffer
*/
+EVENT2_EXPORT_SYMBOL
struct evbuffer *bufferevent_get_input(struct bufferevent *bufev);
/**
@@ -395,6 +448,7 @@ struct evbuffer *bufferevent_get_input(struct bufferevent *bufev);
@return the evbuffer object for the output buffer
*/
+EVENT2_EXPORT_SYMBOL
struct evbuffer *bufferevent_get_output(struct bufferevent *bufev);
/**
@@ -405,6 +459,7 @@ struct evbuffer *bufferevent_get_output(struct bufferevent *bufev);
@return 0 if successful, or -1 if an error occurred
@see bufferevent_disable()
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_enable(struct bufferevent *bufev, short event);
/**
@@ -415,6 +470,7 @@ int bufferevent_enable(struct bufferevent *bufev, short event);
@return 0 if successful, or -1 if an error occurred
@see bufferevent_enable()
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_disable(struct bufferevent *bufev, short event);
/**
@@ -423,6 +479,7 @@ int bufferevent_disable(struct bufferevent *bufev, short event);
@param bufev the bufferevent to inspect
@return A combination of EV_READ | EV_WRITE
*/
+EVENT2_EXPORT_SYMBOL
short bufferevent_get_enabled(struct bufferevent *bufev);
/**
@@ -451,6 +508,7 @@ short bufferevent_get_enabled(struct bufferevent *bufev);
@param timeout_read the read timeout, or NULL
@param timeout_write the write timeout, or NULL
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_set_timeouts(struct bufferevent *bufev,
const struct timeval *timeout_read, const struct timeval *timeout_write);
@@ -472,19 +530,36 @@ int bufferevent_set_timeouts(struct bufferevent *bufev,
@param highmark the high watermark to set
*/
+EVENT2_EXPORT_SYMBOL
void bufferevent_setwatermark(struct bufferevent *bufev, short events,
size_t lowmark, size_t highmark);
/**
+ Retrieves the watermarks for read or write events.
+ Returns non-zero if events contains not only EV_READ or EV_WRITE.
+ Returns zero if events equal EV_READ or EV_WRITE
+
+ @param bufev the bufferevent to be examined
+ @param events EV_READ or EV_WRITE
+ @param lowmark receives the lower watermark if not NULL
+ @param highmark receives the high watermark if not NULL
+*/
+EVENT2_EXPORT_SYMBOL
+int bufferevent_getwatermark(struct bufferevent *bufev, short events,
+ size_t *lowmark, size_t *highmark);
+
+/**
Acquire the lock on a bufferevent. Has no effect if locking was not
enabled with BEV_OPT_THREADSAFE.
*/
+EVENT2_EXPORT_SYMBOL
void bufferevent_lock(struct bufferevent *bufev);
/**
Release the lock on a bufferevent. Has no effect if locking was not
enabled with BEV_OPT_THREADSAFE.
*/
+EVENT2_EXPORT_SYMBOL
void bufferevent_unlock(struct bufferevent *bufev);
/**
@@ -510,11 +585,55 @@ enum bufferevent_flush_mode {
@param mode either BEV_NORMAL or BEV_FLUSH or BEV_FINISHED
@return -1 on failure, 0 if no data was produces, 1 if data was produced
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_flush(struct bufferevent *bufev,
short iotype,
enum bufferevent_flush_mode mode);
/**
+ Flags for bufferevent_trigger(_event) that modify when and how to trigger
+ the callback.
+*/
+enum bufferevent_trigger_options {
+ /** trigger the callback regardless of the watermarks */
+ BEV_TRIG_IGNORE_WATERMARKS = (1<<16),
+
+ /** defer even if the callbacks are not */
+ BEV_TRIG_DEFER_CALLBACKS = BEV_OPT_DEFER_CALLBACKS
+
+ /* (Note: for internal reasons, these need to be disjoint from
+ * bufferevent_options, except when they mean the same thing. */
+};
+
+/**
+ Triggers bufferevent data callbacks.
+
+ The function will honor watermarks unless options contain
+ BEV_TRIG_IGNORE_WATERMARKS. If the options contain BEV_OPT_DEFER_CALLBACKS,
+ the callbacks are deferred.
+
+ @param bufev the bufferevent object
+ @param iotype either EV_READ or EV_WRITE or both.
+ @param options
+ */
+EVENT2_EXPORT_SYMBOL
+void bufferevent_trigger(struct bufferevent *bufev, short iotype,
+ int options);
+
+/**
+ Triggers the bufferevent event callback.
+
+ If the options contain BEV_OPT_DEFER_CALLBACKS, the callbacks are deferred.
+
+ @param bufev the bufferevent object
+ @param what the flags to pass onto the event callback
+ @param options
+ */
+EVENT2_EXPORT_SYMBOL
+void bufferevent_trigger_event(struct bufferevent *bufev, short what,
+ int options);
+
+/**
@name Filtering support
@{
@@ -569,6 +688,7 @@ typedef enum bufferevent_filter_result (*bufferevent_filter_cb)(
this bufferevent is freed.
@param ctx A context pointer to pass to the filter functions.
*/
+EVENT2_EXPORT_SYMBOL
struct bufferevent *
bufferevent_filter_new(struct bufferevent *underlying,
bufferevent_filter_cb input_filter,
@@ -588,6 +708,7 @@ bufferevent_filter_new(struct bufferevent *underlying,
@param pair A pointer to an array to hold the two new bufferevent objects.
@return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_pair_new(struct event_base *base, int options,
struct bufferevent *pair[2]);
@@ -595,6 +716,7 @@ int bufferevent_pair_new(struct event_base *base, int options,
Given one bufferevent returned by bufferevent_pair_new(), returns the
other one if it still exists. Otherwise returns NULL.
*/
+EVENT2_EXPORT_SYMBOL
struct bufferevent *bufferevent_pair_get_partner(struct bufferevent *bev);
/**
@@ -628,6 +750,7 @@ struct bufferevent_rate_limit_group;
Note that all rate-limits hare are currently best-effort: future versions
of Libevent may implement them more tightly.
*/
+EVENT2_EXPORT_SYMBOL
struct ev_token_bucket_cfg *ev_token_bucket_cfg_new(
size_t read_rate, size_t read_burst,
size_t write_rate, size_t write_burst,
@@ -638,6 +761,7 @@ struct ev_token_bucket_cfg *ev_token_bucket_cfg_new(
Note: 'cfg' is not currently reference-counted; it is not safe to free it
until no bufferevent is using it.
*/
+EVENT2_EXPORT_SYMBOL
void ev_token_bucket_cfg_free(struct ev_token_bucket_cfg *cfg);
/**
@@ -651,6 +775,7 @@ void ev_token_bucket_cfg_free(struct ev_token_bucket_cfg *cfg);
Return 0 on sucess, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_set_rate_limit(struct bufferevent *bev,
struct ev_token_bucket_cfg *cfg);
@@ -671,6 +796,7 @@ int bufferevent_set_rate_limit(struct bufferevent *bev,
They are: socket-based bufferevents (normal and IOCP-based), and SSL-based
bufferevents.
*/
+EVENT2_EXPORT_SYMBOL
struct bufferevent_rate_limit_group *bufferevent_rate_limit_group_new(
struct event_base *base,
const struct ev_token_bucket_cfg *cfg);
@@ -679,6 +805,7 @@ struct bufferevent_rate_limit_group *bufferevent_rate_limit_group_new(
Return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_rate_limit_group_set_cfg(
struct bufferevent_rate_limit_group *,
const struct ev_token_bucket_cfg *);
@@ -699,6 +826,7 @@ int bufferevent_rate_limit_group_set_cfg(
Returns 0 on success, -1 on faulre.
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_rate_limit_group_set_min_share(
struct bufferevent_rate_limit_group *, size_t);
@@ -706,6 +834,7 @@ int bufferevent_rate_limit_group_set_min_share(
Free a rate-limiting group. The group must have no members when
this function is called.
*/
+EVENT2_EXPORT_SYMBOL
void bufferevent_rate_limit_group_free(struct bufferevent_rate_limit_group *);
/**
@@ -718,13 +847,43 @@ void bufferevent_rate_limit_group_free(struct bufferevent_rate_limit_group *);
Return 0 on success and -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_add_to_rate_limit_group(struct bufferevent *bev,
struct bufferevent_rate_limit_group *g);
/** Remove 'bev' from its current rate-limit group (if any). */
+EVENT2_EXPORT_SYMBOL
int bufferevent_remove_from_rate_limit_group(struct bufferevent *bev);
/**
+ Set the size limit for single read operation.
+
+ Set to 0 for a reasonable default.
+
+ Return 0 on success and -1 on failure.
+ */
+EVENT2_EXPORT_SYMBOL
+int bufferevent_set_max_single_read(struct bufferevent *bev, size_t size);
+
+/**
+ Set the size limit for single write operation.
+
+ Set to 0 for a reasonable default.
+
+ Return 0 on success and -1 on failure.
+ */
+EVENT2_EXPORT_SYMBOL
+int bufferevent_set_max_single_write(struct bufferevent *bev, size_t size);
+
+/** Get the current size limit for single read operation. */
+EVENT2_EXPORT_SYMBOL
+ev_ssize_t bufferevent_get_max_single_read(struct bufferevent *bev);
+
+/** Get the current size limit for single write operation. */
+EVENT2_EXPORT_SYMBOL
+ev_ssize_t bufferevent_get_max_single_write(struct bufferevent *bev);
+
+/**
@name Rate limit inspection
Return the current read or write bucket size for a bufferevent.
@@ -735,13 +894,20 @@ int bufferevent_remove_from_rate_limit_group(struct bufferevent *bev);
@{
*/
+EVENT2_EXPORT_SYMBOL
ev_ssize_t bufferevent_get_read_limit(struct bufferevent *bev);
+EVENT2_EXPORT_SYMBOL
ev_ssize_t bufferevent_get_write_limit(struct bufferevent *bev);
/*@}*/
+EVENT2_EXPORT_SYMBOL
ev_ssize_t bufferevent_get_max_to_read(struct bufferevent *bev);
+EVENT2_EXPORT_SYMBOL
ev_ssize_t bufferevent_get_max_to_write(struct bufferevent *bev);
+EVENT2_EXPORT_SYMBOL
+const struct ev_token_bucket_cfg *bufferevent_get_token_bucket_cfg(const struct bufferevent * bev);
+
/**
@name Group Rate limit inspection
@@ -751,8 +917,10 @@ ev_ssize_t bufferevent_get_max_to_write(struct bufferevent *bev);
@{
*/
+EVENT2_EXPORT_SYMBOL
ev_ssize_t bufferevent_rate_limit_group_get_read_limit(
struct bufferevent_rate_limit_group *);
+EVENT2_EXPORT_SYMBOL
ev_ssize_t bufferevent_rate_limit_group_get_write_limit(
struct bufferevent_rate_limit_group *);
/*@}*/
@@ -771,7 +939,9 @@ ev_ssize_t bufferevent_rate_limit_group_get_write_limit(
@{
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_decrement_read_limit(struct bufferevent *bev, ev_ssize_t decr);
+EVENT2_EXPORT_SYMBOL
int bufferevent_decrement_write_limit(struct bufferevent *bev, ev_ssize_t decr);
/*@}*/
@@ -788,8 +958,10 @@ int bufferevent_decrement_write_limit(struct bufferevent *bev, ev_ssize_t decr);
@{
*/
+EVENT2_EXPORT_SYMBOL
int bufferevent_rate_limit_group_decrement_read(
struct bufferevent_rate_limit_group *, ev_ssize_t);
+EVENT2_EXPORT_SYMBOL
int bufferevent_rate_limit_group_decrement_write(
struct bufferevent_rate_limit_group *, ev_ssize_t);
/*@}*/
@@ -801,6 +973,7 @@ int bufferevent_rate_limit_group_decrement_write(
* Set the variable pointed to by total_read_out to the total number of bytes
* ever read on grp, and the variable pointed to by total_written_out to the
* total number of bytes ever written on grp. */
+EVENT2_EXPORT_SYMBOL
void bufferevent_rate_limit_group_get_totals(
struct bufferevent_rate_limit_group *grp,
ev_uint64_t *total_read_out, ev_uint64_t *total_written_out);
@@ -810,6 +983,7 @@ void bufferevent_rate_limit_group_get_totals(
*
* Reset the number of bytes read or written on grp as given by
* bufferevent_rate_limit_group_reset_totals(). */
+EVENT2_EXPORT_SYMBOL
void
bufferevent_rate_limit_group_reset_totals(
struct bufferevent_rate_limit_group *grp);
@@ -818,4 +992,4 @@ bufferevent_rate_limit_group_reset_totals(
}
#endif
-#endif /* _EVENT2_BUFFEREVENT_H_ */
+#endif /* EVENT2_BUFFEREVENT_H_INCLUDED_ */
diff --git a/include/event2/bufferevent_compat.h b/include/event2/bufferevent_compat.h
index 3256fd16..65482042 100644
--- a/include/event2/bufferevent_compat.h
+++ b/include/event2/bufferevent_compat.h
@@ -25,8 +25,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_BUFFEREVENT_COMPAT_H_
-#define _EVENT2_BUFFEREVENT_COMPAT_H_
+#ifndef EVENT2_BUFFEREVENT_COMPAT_H_INCLUDED_
+#define EVENT2_BUFFEREVENT_COMPAT_H_INCLUDED_
#define evbuffercb bufferevent_data_cb
#define everrorcb bufferevent_event_cb
diff --git a/include/event2/bufferevent_ssl.h b/include/event2/bufferevent_ssl.h
index c612e99e..bf39b844 100644
--- a/include/event2/bufferevent_ssl.h
+++ b/include/event2/bufferevent_ssl.h
@@ -23,14 +23,14 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_BUFFEREVENT_SSL_H_
-#define _EVENT2_BUFFEREVENT_SSL_H_
+#ifndef EVENT2_BUFFEREVENT_SSL_H_INCLUDED_
+#define EVENT2_BUFFEREVENT_SSL_H_INCLUDED_
/** @file event2/bufferevent_ssl.h
OpenSSL support for bufferevents.
*/
-
+#include <event2/visibility.h>
#include <event2/event-config.h>
#include <event2/bufferevent.h>
#include <event2/util.h>
@@ -52,7 +52,7 @@ enum bufferevent_ssl_state {
BUFFEREVENT_SSL_ACCEPTING = 2
};
-#if defined(_EVENT_HAVE_OPENSSL) || defined(_EVENT_IN_DOXYGEN)
+#if defined(EVENT__HAVE_OPENSSL) || defined(EVENT_IN_DOXYGEN_)
/**
Create a new SSL bufferevent to send its data over another bufferevent.
@@ -64,6 +64,7 @@ enum bufferevent_ssl_state {
@param options One or more bufferevent_options
@return A new bufferevent on success, or NULL on failure
*/
+EVENT2_EXPORT_SYMBOL
struct bufferevent *
bufferevent_openssl_filter_new(struct event_base *base,
struct bufferevent *underlying,
@@ -81,6 +82,7 @@ bufferevent_openssl_filter_new(struct event_base *base,
@param options One or more bufferevent_options
@return A new bufferevent on success, or NULL on failure.
*/
+EVENT2_EXPORT_SYMBOL
struct bufferevent *
bufferevent_openssl_socket_new(struct event_base *base,
evutil_socket_t fd,
@@ -88,14 +90,39 @@ bufferevent_openssl_socket_new(struct event_base *base,
enum bufferevent_ssl_state state,
int options);
+/** Control how to report dirty SSL shutdowns.
+
+ If the peer (or the network, or an attacker) closes the TCP
+ connection before closing the SSL channel, and the protocol is SSL >= v3,
+ this is a "dirty" shutdown. If allow_dirty_shutdown is 0 (default),
+ this is reported as BEV_EVENT_ERROR.
+
+ If instead allow_dirty_shutdown=1, a dirty shutdown is reported as
+ BEV_EVENT_EOF.
+
+ (Note that if the protocol is < SSLv3, you will always receive
+ BEV_EVENT_EOF, since SSL 2 and earlier cannot distinguish a secure
+ connection close from a dirty one. This is one reason (among many)
+ not to use SSL 2.)
+*/
+
+EVENT2_EXPORT_SYMBOL
+int bufferevent_openssl_get_allow_dirty_shutdown(struct bufferevent *bev);
+EVENT2_EXPORT_SYMBOL
+void bufferevent_openssl_set_allow_dirty_shutdown(struct bufferevent *bev,
+ int allow_dirty_shutdown);
+
/** Return the underlying openssl SSL * object for an SSL bufferevent. */
+EVENT2_EXPORT_SYMBOL
struct ssl_st *
bufferevent_openssl_get_ssl(struct bufferevent *bufev);
/** Tells a bufferevent to begin SSL renegotiation. */
+EVENT2_EXPORT_SYMBOL
int bufferevent_ssl_renegotiate(struct bufferevent *bev);
/** Return the most recent OpenSSL error reported on an SSL bufferevent. */
+EVENT2_EXPORT_SYMBOL
unsigned long bufferevent_get_openssl_error(struct bufferevent *bev);
#endif
@@ -104,4 +131,4 @@ unsigned long bufferevent_get_openssl_error(struct bufferevent *bev);
}
#endif
-#endif /* _EVENT2_BUFFEREVENT_SSL_H_ */
+#endif /* EVENT2_BUFFEREVENT_SSL_H_INCLUDED_ */
diff --git a/include/event2/bufferevent_struct.h b/include/event2/bufferevent_struct.h
index 02835c16..e84c082c 100644
--- a/include/event2/bufferevent_struct.h
+++ b/include/event2/bufferevent_struct.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_BUFFEREVENT_STRUCT_H_
-#define _EVENT2_BUFFEREVENT_STRUCT_H_
+#ifndef EVENT2_BUFFEREVENT_STRUCT_H_INCLUDED_
+#define EVENT2_BUFFEREVENT_STRUCT_H_INCLUDED_
/** @file event2/bufferevent_struct.h
@@ -42,10 +42,10 @@ extern "C" {
#endif
#include <event2/event-config.h>
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -113,4 +113,4 @@ struct bufferevent {
}
#endif
-#endif /* _EVENT2_BUFFEREVENT_STRUCT_H_ */
+#endif /* EVENT2_BUFFEREVENT_STRUCT_H_INCLUDED_ */
diff --git a/include/event2/dns.h b/include/event2/dns.h
index 6677d4e2..f052af68 100644
--- a/include/event2/dns.h
+++ b/include/event2/dns.h
@@ -132,8 +132,10 @@
* with the next probe.
*/
-#ifndef _EVENT2_DNS_H_
-#define _EVENT2_DNS_H_
+#ifndef EVENT2_DNS_H_INCLUDED_
+#define EVENT2_DNS_H_INCLUDED_
+
+#include <event2/visibility.h>
#ifdef __cplusplus
extern "C" {
@@ -201,6 +203,12 @@ typedef void (*evdns_callback_type) (int result, char type, int count, int ttl,
struct evdns_base;
struct event_base;
+/** Flag for evdns_base_new: process resolv.conf. */
+#define EVDNS_BASE_INITIALIZE_NAMESERVERS 1
+/** Flag for evdns_base_new: Do not prevent the libevent event loop from
+ * exiting when we have no active dns requests. */
+#define EVDNS_BASE_DISABLE_WHEN_INACTIVE 0x8000
+
/**
Initialize the asynchronous DNS library.
@@ -209,10 +217,12 @@ struct event_base;
evdns_config_windows_nameservers() on Windows.
@param event_base the event base to associate the dns client with
- @param initialize_nameservers 1 if resolve.conf processing should occur
+ @param flags any of EVDNS_BASE_INITIALIZE_NAMESERVERS|
+ EVDNS_BASE_DISABLE_WHEN_INACTIVE
@return evdns_base object if successful, or NULL if an error occurred.
@see evdns_base_free()
*/
+EVENT2_EXPORT_SYMBOL
struct evdns_base * evdns_base_new(struct event_base *event_base, int initialize_nameservers);
@@ -228,14 +238,25 @@ struct evdns_base * evdns_base_new(struct event_base *event_base, int initialize
active requests will return DNS_ERR_SHUTDOWN.
@see evdns_base_new()
*/
+EVENT2_EXPORT_SYMBOL
void evdns_base_free(struct evdns_base *base, int fail_requests);
/**
+ Remove all hosts entries that have been loaded into the event_base via
+ evdns_base_load_hosts or via event_base_resolv_conf_parse.
+
+ @param evdns_base the evdns base to remove outdated host addresses from
+ */
+EVENT2_EXPORT_SYMBOL
+void evdns_base_clear_host_addresses(struct evdns_base *base);
+
+/**
Convert a DNS error code to a string.
@param err the DNS error code
@return a string containing an explanation of the error code
*/
+EVENT2_EXPORT_SYMBOL
const char *evdns_err_to_string(int err);
@@ -250,6 +271,7 @@ const char *evdns_err_to_string(int err);
@return 0 if successful, or -1 if an error occurred
@see evdns_base_nameserver_ip_add()
*/
+EVENT2_EXPORT_SYMBOL
int evdns_base_nameserver_add(struct evdns_base *base,
unsigned long int address);
@@ -265,6 +287,7 @@ int evdns_base_nameserver_add(struct evdns_base *base,
@return the number of configured nameservers
@see evdns_base_nameserver_add()
*/
+EVENT2_EXPORT_SYMBOL
int evdns_base_count_nameservers(struct evdns_base *base);
/**
@@ -276,6 +299,7 @@ int evdns_base_count_nameservers(struct evdns_base *base);
@return 0 if successful, or -1 if an error occurred
@see evdns_base_resume()
*/
+EVENT2_EXPORT_SYMBOL
int evdns_base_clear_nameservers_and_suspend(struct evdns_base *base);
@@ -289,6 +313,7 @@ int evdns_base_clear_nameservers_and_suspend(struct evdns_base *base);
@return 0 if successful, or -1 if an error occurred
@see evdns_base_clear_nameservers_and_suspend()
*/
+EVENT2_EXPORT_SYMBOL
int evdns_base_resume(struct evdns_base *base);
/**
@@ -308,12 +333,14 @@ int evdns_base_resume(struct evdns_base *base);
@return 0 if successful, or -1 if an error occurred
@see evdns_base_nameserver_add()
*/
+EVENT2_EXPORT_SYMBOL
int evdns_base_nameserver_ip_add(struct evdns_base *base,
const char *ip_as_string);
/**
Add a nameserver by sockaddr.
**/
+EVENT2_EXPORT_SYMBOL
int
evdns_base_nameserver_sockaddr_add(struct evdns_base *base,
const struct sockaddr *sa, ev_socklen_t len, unsigned flags);
@@ -331,6 +358,7 @@ struct evdns_request;
@return an evdns_request object if successful, or NULL if an error occurred.
@see evdns_resolve_ipv6(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6(), evdns_cancel_request()
*/
+EVENT2_EXPORT_SYMBOL
struct evdns_request *evdns_base_resolve_ipv4(struct evdns_base *base, const char *name, int flags, evdns_callback_type callback, void *ptr);
/**
@@ -344,6 +372,7 @@ struct evdns_request *evdns_base_resolve_ipv4(struct evdns_base *base, const cha
@return an evdns_request object if successful, or NULL if an error occurred.
@see evdns_resolve_ipv4(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6(), evdns_cancel_request()
*/
+EVENT2_EXPORT_SYMBOL
struct evdns_request *evdns_base_resolve_ipv6(struct evdns_base *base, const char *name, int flags, evdns_callback_type callback, void *ptr);
struct in_addr;
@@ -360,6 +389,7 @@ struct in6_addr;
@return an evdns_request object if successful, or NULL if an error occurred.
@see evdns_resolve_reverse_ipv6(), evdns_cancel_request()
*/
+EVENT2_EXPORT_SYMBOL
struct evdns_request *evdns_base_resolve_reverse(struct evdns_base *base, const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr);
@@ -374,6 +404,7 @@ struct evdns_request *evdns_base_resolve_reverse(struct evdns_base *base, const
@return an evdns_request object if successful, or NULL if an error occurred.
@see evdns_resolve_reverse_ipv6(), evdns_cancel_request()
*/
+EVENT2_EXPORT_SYMBOL
struct evdns_request *evdns_base_resolve_reverse_ipv6(struct evdns_base *base, const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr);
/**
@@ -383,6 +414,7 @@ struct evdns_request *evdns_base_resolve_reverse_ipv6(struct evdns_base *base, c
@param req the evdns_request that was returned by calling a resolve function
@see evdns_base_resolve_ipv4(), evdns_base_resolve_ipv6, evdns_base_resolve_reverse
*/
+EVENT2_EXPORT_SYMBOL
void evdns_cancel_request(struct evdns_base *base, struct evdns_request *req);
/**
@@ -401,6 +433,7 @@ void evdns_cancel_request(struct evdns_base *base, struct evdns_request *req);
@param val the value to be set
@return 0 if successful, or -1 if an error occurred
*/
+EVENT2_EXPORT_SYMBOL
int evdns_base_set_option(struct evdns_base *base, const char *option, const char *val);
@@ -426,6 +459,7 @@ int evdns_base_set_option(struct evdns_base *base, const char *option, const cha
occurred (see above)
@see resolv.conf(3), evdns_config_windows_nameservers()
*/
+EVENT2_EXPORT_SYMBOL
int evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *const filename);
/**
@@ -436,8 +470,12 @@ int evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char
Note that only evdns_getaddrinfo uses the /etc/hosts entries.
+ This function does not replace previously loaded hosts entries; to do that,
+ call evdns_base_clear_host_addresses first.
+
Return 0 on success, negative on failure.
*/
+EVENT2_EXPORT_SYMBOL
int evdns_base_load_hosts(struct evdns_base *base, const char *hosts_fname);
/**
@@ -450,7 +488,8 @@ int evdns_base_load_hosts(struct evdns_base *base, const char *hosts_fname);
@return 0 if successful, or -1 if an error occurred
@see evdns_resolv_conf_parse()
*/
-#ifdef WIN32
+#ifdef _WIN32
+EVENT2_EXPORT_SYMBOL
int evdns_base_config_windows_nameservers(struct evdns_base *);
#define EVDNS_BASE_CONFIG_WINDOWS_NAMESERVERS_IMPLEMENTED
#endif
@@ -459,6 +498,7 @@ int evdns_base_config_windows_nameservers(struct evdns_base *);
/**
Clear the list of search domains.
*/
+EVENT2_EXPORT_SYMBOL
void evdns_base_search_clear(struct evdns_base *base);
@@ -467,6 +507,7 @@ void evdns_base_search_clear(struct evdns_base *base);
@param domain the domain to be added to the search list
*/
+EVENT2_EXPORT_SYMBOL
void evdns_base_search_add(struct evdns_base *base, const char *domain);
@@ -478,6 +519,7 @@ void evdns_base_search_add(struct evdns_base *base, const char *domain);
@param ndots the new ndots parameter
*/
+EVENT2_EXPORT_SYMBOL
void evdns_base_search_ndots_set(struct evdns_base *base, const int ndots);
/**
@@ -496,6 +538,7 @@ typedef void (*evdns_debug_log_fn_type)(int is_warning, const char *msg);
@param fn the callback to be invoked when a log message is generated
*/
+EVENT2_EXPORT_SYMBOL
void evdns_set_log_fn(evdns_debug_log_fn_type fn);
/**
@@ -508,6 +551,7 @@ void evdns_set_log_fn(evdns_debug_log_fn_type fn);
NOTE: This function has no effect in Libevent 2.0.4-alpha and later,
since Libevent now provides its own secure RNG.
*/
+EVENT2_EXPORT_SYMBOL
void evdns_set_transaction_id_fn(ev_uint16_t (*fn)(void));
/**
@@ -519,6 +563,7 @@ void evdns_set_transaction_id_fn(ev_uint16_t (*fn)(void));
NOTE: This function has no effect in Libevent 2.0.4-alpha and later,
since Libevent now provides its own secure RNG.
*/
+EVENT2_EXPORT_SYMBOL
void evdns_set_random_bytes_fn(void (*fn)(char *, size_t));
/*
@@ -572,36 +617,47 @@ typedef void (*evdns_request_callback_fn_type)(struct evdns_server_request *, vo
@param user_data Data to pass to the callback.
@return an evdns_server_port structure for this server port.
*/
+EVENT2_EXPORT_SYMBOL
struct evdns_server_port *evdns_add_server_port_with_base(struct event_base *base, evutil_socket_t socket, int flags, evdns_request_callback_fn_type callback, void *user_data);
/** Close down a DNS server port, and free associated structures. */
+EVENT2_EXPORT_SYMBOL
void evdns_close_server_port(struct evdns_server_port *port);
/** Sets some flags in a reply we're building.
Allows setting of the AA or RD flags
*/
+EVENT2_EXPORT_SYMBOL
void evdns_server_request_set_flags(struct evdns_server_request *req, int flags);
/* Functions to add an answer to an in-progress DNS reply.
*/
+EVENT2_EXPORT_SYMBOL
int evdns_server_request_add_reply(struct evdns_server_request *req, int section, const char *name, int type, int dns_class, int ttl, int datalen, int is_name, const char *data);
+EVENT2_EXPORT_SYMBOL
int evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl);
+EVENT2_EXPORT_SYMBOL
int evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl);
+EVENT2_EXPORT_SYMBOL
int evdns_server_request_add_ptr_reply(struct evdns_server_request *req, struct in_addr *in, const char *inaddr_name, const char *hostname, int ttl);
+EVENT2_EXPORT_SYMBOL
int evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl);
/**
Send back a response to a DNS request, and free the request structure.
*/
+EVENT2_EXPORT_SYMBOL
int evdns_server_request_respond(struct evdns_server_request *req, int err);
/**
Free a DNS request without sending back a reply.
*/
+EVENT2_EXPORT_SYMBOL
int evdns_server_request_drop(struct evdns_server_request *req);
struct sockaddr;
/**
Get the address that made a DNS request.
*/
-int evdns_server_request_get_requesting_addr(struct evdns_server_request *_req, struct sockaddr *sa, int addr_len);
+EVENT2_EXPORT_SYMBOL
+int evdns_server_request_get_requesting_addr(struct evdns_server_request *req, struct sockaddr *sa, int addr_len);
/** Callback for evdns_getaddrinfo. */
typedef void (*evdns_getaddrinfo_cb)(int result, struct evutil_addrinfo *res, void *arg);
@@ -625,6 +681,7 @@ struct evdns_getaddrinfo_request;
* - For ai_socktype, we only handle SOCKTYPE_STREAM, SOCKTYPE_UDP, and 0.
* - For ai_protocol, we only handle IPPROTO_TCP, IPPROTO_UDP, and 0.
*/
+EVENT2_EXPORT_SYMBOL
struct evdns_getaddrinfo_request *evdns_getaddrinfo(
struct evdns_base *dns_base,
const char *nodename, const char *servname,
@@ -634,10 +691,11 @@ struct evdns_getaddrinfo_request *evdns_getaddrinfo(
/* Cancel an in-progress evdns_getaddrinfo. This MUST NOT be called after the
* getaddrinfo's callback has been invoked. The resolves will be canceled,
* and the callback will be invoked with the error EVUTIL_EAI_CANCEL. */
+EVENT2_EXPORT_SYMBOL
void evdns_getaddrinfo_cancel(struct evdns_getaddrinfo_request *req);
#ifdef __cplusplus
}
#endif
-#endif /* !_EVENT2_DNS_H_ */
+#endif /* !EVENT2_DNS_H_INCLUDED_ */
diff --git a/include/event2/dns_compat.h b/include/event2/dns_compat.h
index 7c1c7761..965fd654 100644
--- a/include/event2/dns_compat.h
+++ b/include/event2/dns_compat.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_DNS_COMPAT_H_
-#define _EVENT2_DNS_COMPAT_H_
+#ifndef EVENT2_DNS_COMPAT_H_INCLUDED_
+#define EVENT2_DNS_COMPAT_H_INCLUDED_
/** @file event2/dns_compat.h
@@ -40,10 +40,10 @@ extern "C" {
#endif
#include <event2/event-config.h>
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -324,7 +324,7 @@ void evdns_search_ndots_set(const int ndots);
*/
struct evdns_server_port *evdns_add_server_port(evutil_socket_t socket, int flags, evdns_request_callback_fn_type callback, void *user_data);
-#ifdef WIN32
+#ifdef _WIN32
int evdns_config_windows_nameservers(void);
#define EVDNS_CONFIG_WINDOWS_NAMESERVERS_IMPLEMENTED
#endif
@@ -333,4 +333,4 @@ int evdns_config_windows_nameservers(void);
}
#endif
-#endif /* _EVENT2_EVENT_COMPAT_H_ */
+#endif /* EVENT2_EVENT_COMPAT_H_INCLUDED_ */
diff --git a/include/event2/dns_struct.h b/include/event2/dns_struct.h
index 5a02a0bd..593a8a70 100644
--- a/include/event2/dns_struct.h
+++ b/include/event2/dns_struct.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_DNS_STRUCT_H_
-#define _EVENT2_DNS_STRUCT_H_
+#ifndef EVENT2_DNS_STRUCT_H_INCLUDED_
+#define EVENT2_DNS_STRUCT_H_INCLUDED_
/** @file event2/dns_struct.h
@@ -39,10 +39,10 @@ extern "C" {
#endif
#include <event2/event-config.h>
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -76,5 +76,5 @@ struct evdns_server_question {
}
#endif
-#endif /* _EVENT2_DNS_STRUCT_H_ */
+#endif /* EVENT2_DNS_STRUCT_H_INCLUDED_ */
diff --git a/include/event2/event.h b/include/event2/event.h
index 10bcd97c..6e2b884d 100644
--- a/include/event2/event.h
+++ b/include/event2/event.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_EVENT_H_
-#define _EVENT2_EVENT_H_
+#ifndef EVENT2_EVENT_H_INCLUDED_
+#define EVENT2_EVENT_H_INCLUDED_
/**
@mainpage
@@ -39,7 +39,7 @@
to signals or regular timeouts.
Libevent is meant to replace the event loop found in event driven network
- servers. An application just needs to call event_dispatch() and then add or
+ servers. An application just needs to call event_base_dispatch() and then add or
remove events dynamically without having to change the event loop.
@@ -54,7 +54,7 @@
@section usage Standard usage
- Every program that uses Libevent must inclurde the <event2/event.h>
+ Every program that uses Libevent must include the <event2/event.h>
header, and pass the -levent flag to the linker. (You can instead link
-levent_core if you only want the main event and buffered IO-based code,
and don't want to link any protocol code.)
@@ -90,7 +90,7 @@
remain allocated as long as it is active, so it should generally be
allocated on the heap.
- @section loop Dispaching evets.
+ @section loop Dispatching events.
Finally, you call event_base_dispatch() to loop and dispatch events.
You can also use event_base_loop() for more fine-grained control.
@@ -124,9 +124,11 @@
@section timers Timers
Libevent can also be used to create timers that invoke a callback after a
- certain amount of time has expired. The evtimer_new() function returns
+ certain amount of time has expired. The evtimer_new() macro returns
an event struct to use as a timer. To activate the timer, call
evtimer_add(). Timers can be deactivated by calling evtimer_del().
+ (These macros are thin wrappers around event_new(), event_add(),
+ and event_del(); you can also use those instead.)
@section evdns Asynchronous DNS resolution
@@ -180,15 +182,17 @@
Core functions for waiting for and receiving events, and using event bases.
*/
+#include <event2/visibility.h>
+
#ifdef __cplusplus
extern "C" {
#endif
#include <event2/event-config.h>
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -211,7 +215,7 @@ extern "C" {
* event_base_new_with_config()
*/
struct event_base
-#ifdef _EVENT_IN_DOXYGEN
+#ifdef EVENT_IN_DOXYGEN_
{/*Empty body so that doxygen will generate documentation here.*/}
#endif
;
@@ -270,7 +274,7 @@ struct event_base
* event_priority_set()
*/
struct event
-#ifdef _EVENT_IN_DOXYGEN
+#ifdef EVENT_IN_DOXYGEN_
{/*Empty body so that doxygen will generate documentation here.*/}
#endif
;
@@ -289,7 +293,7 @@ struct event
* event_config_set_flag(), event_config_set_num_cpus_hint()
*/
struct event_config
-#ifdef _EVENT_IN_DOXYGEN
+#ifdef EVENT_IN_DOXYGEN_
{/*Empty body so that doxygen will generate documentation here.*/}
#endif
;
@@ -314,6 +318,7 @@ struct event_config
*
* @see event_debug_unassign()
*/
+EVENT2_EXPORT_SYMBOL
void event_enable_debug_mode(void);
/**
@@ -325,6 +330,7 @@ void event_enable_debug_mode(void);
*
* @see event_enable_debug_mode()
*/
+EVENT2_EXPORT_SYMBOL
void event_debug_unassign(struct event *);
/**
@@ -334,6 +340,7 @@ void event_debug_unassign(struct event *);
*
* @see event_base_free(), event_base_new_with_config()
*/
+EVENT2_EXPORT_SYMBOL
struct event_base *event_base_new(void);
/**
@@ -346,6 +353,7 @@ struct event_base *event_base_new(void);
@return 0 if successful, or -1 if some events could not be re-added.
@see event_base_new()
*/
+EVENT2_EXPORT_SYMBOL
int event_reinit(struct event_base *base);
/**
@@ -361,6 +369,7 @@ int event_reinit(struct event_base *base);
no events were pending or active.
@see event_base_loop()
*/
+EVENT2_EXPORT_SYMBOL
int event_base_dispatch(struct event_base *);
/**
@@ -369,6 +378,7 @@ int event_base_dispatch(struct event_base *);
@param eb the event_base structure returned by event_base_new()
@return a string identifying the kernel event mechanism (kqueue, epoll, etc.)
*/
+EVENT2_EXPORT_SYMBOL
const char *event_base_get_method(const struct event_base *);
/**
@@ -383,9 +393,61 @@ const char *event_base_get_method(const struct event_base *);
The end of the array is indicated by a NULL pointer. If an
error is encountered NULL is returned.
*/
+EVENT2_EXPORT_SYMBOL
const char **event_get_supported_methods(void);
/**
+ @name event type flag
+
+ Flags to pass to event_base_get_num_events() to specify the kinds of events
+ we want to aggregate counts for
+*/
+/**@{*/
+/** count the number of active events, which have been triggered.*/
+#define EVENT_BASE_COUNT_ACTIVE 1U
+/** count the number of virtual events, which is used to represent an internal
+ * condition, other than a pending event, that keeps the loop from exiting. */
+#define EVENT_BASE_COUNT_VIRTUAL 2U
+/** count the number of events which have been added to event base, including
+ * internal events. */
+#define EVENT_BASE_COUNT_ADDED 4U
+/**@}*/
+
+/**
+ Gets the number of events in event_base, as specified in the flags.
+
+ Since event base has some internal events added to make some of its
+ functionalities work, EVENT_BASE_COUNT_ADDED may return more than the
+ number of events you added using event_add().
+
+ If you pass EVENT_BASE_COUNT_ACTIVE and EVENT_BASE_COUNT_ADDED together, an
+ active event will be counted twice. However, this might not be the case in
+ future libevent versions. The return value is an indication of the work
+ load, but the user shouldn't rely on the exact value as this may change in
+ the future.
+
+ @param eb the event_base structure returned by event_base_new()
+ @param flags a bitwise combination of the kinds of events to aggregate
+ counts for
+ @return the number of events specified in the flags
+*/
+EVENT2_EXPORT_SYMBOL
+int event_base_get_num_events(struct event_base *, unsigned int);
+
+/**
+ Get the maximum number of events in a given event_base as specified in the
+ flags.
+
+ @param eb the event_base structure returned by event_base_new()
+ @param flags a bitwise combination of the kinds of events to aggregate
+ counts for
+ @param clear option used to reset the maximum count.
+ @return the number of events specified in the flags
+ */
+EVENT2_EXPORT_SYMBOL
+int event_base_get_max_events(struct event_base *, unsigned int, int);
+
+/**
Allocates a new event configuration object.
The event configuration object can be used to change the behavior of
@@ -395,6 +457,7 @@ const char **event_get_supported_methods(void);
NULL if an error is encountered.
@see event_base_new_with_config(), event_config_free(), event_config
*/
+EVENT2_EXPORT_SYMBOL
struct event_config *event_config_new(void);
/**
@@ -402,6 +465,7 @@ struct event_config *event_config_new(void);
@param cfg the event configuration object to be freed.
*/
+EVENT2_EXPORT_SYMBOL
void event_config_free(struct event_config *cfg);
/**
@@ -416,6 +480,7 @@ void event_config_free(struct event_config *cfg);
@param method the name of the event method to avoid
@return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int event_config_avoid_method(struct event_config *cfg, const char *method);
/**
@@ -437,7 +502,14 @@ enum event_method_feature {
EV_FEATURE_O1 = 0x02,
/** Require an event method that allows file descriptors as well as
* sockets. */
- EV_FEATURE_FDS = 0x04
+ EV_FEATURE_FDS = 0x04,
+ /** Require an event method that allows you to use EV_CLOSED to detect
+ * connection close without the necessity of reading all the pending data.
+ *
+ * Methods that do support EV_CLOSED may not be able to provide support on
+ * all kernel versions.
+ **/
+ EV_FEATURE_EARLY_CLOSE = 0x08
};
/**
@@ -450,7 +522,11 @@ enum event_method_feature {
*/
enum event_base_config_flag {
/** Do not allocate a lock for the event base, even if we have
- locking set up. */
+ locking set up.
+
+ Setting this option will make it unsafe and nonfunctional to call
+ functions on the base concurrently from multiple threads.
+ */
EVENT_BASE_FLAG_NOLOCK = 0x01,
/** Do not check the EVENT_* environment variables when configuring
an event_base */
@@ -475,13 +551,20 @@ enum event_base_config_flag {
if you have any fds cloned by dup() or its variants. Doing so
will produce strange and hard-to-diagnose bugs.
- This flag can also be activated by settnig the
+ This flag can also be activated by setting the
EVENT_EPOLL_USE_CHANGELIST environment variable.
This flag has no effect if you wind up using a backend other than
epoll.
*/
- EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST = 0x10
+ EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST = 0x10,
+
+ /** Ordinarily, Libevent implements its time and timeout code using
+ the fastest monotonic timer that we have. If this flag is set,
+ however, we use less efficient more precise timer, assuming one is
+ present.
+ */
+ EVENT_BASE_FLAG_PRECISE_TIMER = 0x20
};
/**
@@ -491,6 +574,7 @@ enum event_base_config_flag {
@see event_method_feature
*/
+EVENT2_EXPORT_SYMBOL
int event_base_get_features(const struct event_base *base);
/**
@@ -515,6 +599,7 @@ int event_base_get_features(const struct event_base *base);
@return 0 on success, -1 on failure.
@see event_method_feature, event_base_new_with_config()
*/
+EVENT2_EXPORT_SYMBOL
int event_config_require_features(struct event_config *cfg, int feature);
/**
@@ -523,6 +608,7 @@ int event_config_require_features(struct event_config *cfg, int feature);
*
* @see event_base_config_flags, event_base_new_with_config()
**/
+EVENT2_EXPORT_SYMBOL
int event_config_set_flag(struct event_config *cfg, int flag);
/**
@@ -534,9 +620,43 @@ int event_config_set_flag(struct event_config *cfg, int flag);
* @param cpus the number of cpus
* @return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int event_config_set_num_cpus_hint(struct event_config *cfg, int cpus);
/**
+ * Record an interval and/or a number of callbacks after which the event base
+ * should check for new events. By default, the event base will run as many
+ * events are as activated at the higest activated priority before checking
+ * for new events. If you configure it by setting max_interval, it will check
+ * the time after each callback, and not allow more than max_interval to
+ * elapse before checking for new events. If you configure it by setting
+ * max_callbacks to a value >= 0, it will run no more than max_callbacks
+ * callbacks before checking for new events.
+ *
+ * This option can decrease the latency of high-priority events, and
+ * avoid priority inversions where multiple low-priority events keep us from
+ * polling for high-priority events, but at the expense of slightly decreasing
+ * the throughput. Use it with caution!
+ *
+ * @param cfg The event_base configuration object.
+ * @param max_interval An interval after which Libevent should stop running
+ * callbacks and check for more events, or NULL if there should be
+ * no such interval.
+ * @param max_callbacks A number of callbacks after which Libevent should
+ * stop running callbacks and check for more events, or -1 if there
+ * should be no such limit.
+ * @param min_priority A priority below which max_interval and max_callbacks
+ * should not be enforced. If this is set to 0, they are enforced
+ * for events of every priority; if it's set to 1, they're enforced
+ * for events of priority 1 and above, and so on.
+ * @return 0 on success, -1 on failure.
+ **/
+EVENT2_EXPORT_SYMBOL
+int event_config_set_max_dispatch_interval(struct event_config *cfg,
+ const struct timeval *max_interval, int max_callbacks,
+ int min_priority);
+
+/**
Initialize the event API.
Use event_base_new_with_config() to initialize a new event base, taking
@@ -548,6 +668,7 @@ int event_config_set_num_cpus_hint(struct event_config *cfg, int cpus);
or NULL if no event base can be created with the requested event_config.
@see event_base_new(), event_base_free(), event_init(), event_assign()
*/
+EVENT2_EXPORT_SYMBOL
struct event_base *event_base_new_with_config(const struct event_config *);
/**
@@ -556,10 +677,23 @@ struct event_base *event_base_new_with_config(const struct event_config *);
Note that this function will not close any fds or free any memory passed
to event_new as the argument to callback.
+ If there are any pending finalizer callbacks, this function will invoke
+ them.
+
@param eb an event_base to be freed
*/
+EVENT2_EXPORT_SYMBOL
void event_base_free(struct event_base *);
+/**
+ As event_free, but do not run finalizers.
+
+ THIS IS AN EXPERIMENTAL API. IT MIGHT CHANGE BEFORE THE LIBEVENT 2.1 SERIES
+ BECOMES STABLE.
+ */
+EVENT2_EXPORT_SYMBOL
+void event_base_free_nofinalize(struct event_base *);
+
/** @name Log severities
*/
/**@{*/
@@ -586,12 +720,13 @@ typedef void (*event_log_cb)(int severity, const char *msg);
Redirect Libevent's log messages.
@param cb a function taking two arguments: an integer severity between
- _EVENT_LOG_DEBUG and _EVENT_LOG_ERR, and a string. If cb is NULL,
+ EVENT_LOG_DEBUG and EVENT_LOG_ERR, and a string. If cb is NULL,
then the default log is used.
NOTE: The function you provide *must not* call any other libevent
functionality. Doing so can produce undefined behavior.
*/
+EVENT2_EXPORT_SYMBOL
void event_set_log_callback(event_log_cb cb);
/**
@@ -610,11 +745,32 @@ typedef void (*event_fatal_cb)(int err);
something is wrong with your program, or with Libevent: any subsequent calls
to Libevent may result in undefined behavior.
- Libevent will (almost) always log an _EVENT_LOG_ERR message before calling
+ Libevent will (almost) always log an EVENT_LOG_ERR message before calling
this function; look at the last log message to see why Libevent has died.
*/
+EVENT2_EXPORT_SYMBOL
void event_set_fatal_callback(event_fatal_cb cb);
+#define EVENT_DBG_ALL 0xffffffffu
+#define EVENT_DBG_NONE 0
+
+/**
+ Turn on debugging logs and have them sent to the default log handler.
+
+ This is a global setting; if you are going to call it, you must call this
+ before any calls that create an event-base. You must call it before any
+ multithreaded use of Libevent.
+
+ Debug logs are verbose.
+
+ @param which Controls which debug messages are turned on. This option is
+ unused for now; for forward compatibility, you must pass in the constant
+ "EVENT_DBG_ALL" to turn debugging logs on, or "EVENT_DBG_NONE" to turn
+ debugging logs off.
+ */
+EVENT2_EXPORT_SYMBOL
+void event_enable_debug_logging(ev_uint32_t which);
+
/**
Associate a different event base with an event.
@@ -624,6 +780,7 @@ void event_set_fatal_callback(event_fatal_cb cb);
@param ev the event
@return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int event_base_set(struct event_base *, struct event *);
/** @name Loop flags
@@ -637,6 +794,11 @@ int event_base_set(struct event_base *, struct event *);
/** Do not block: see which events are ready now, run the callbacks
* of the highest-priority ones, then exit. */
#define EVLOOP_NONBLOCK 0x02
+/** Do not exit the loop because we have no pending events. Instead, keep
+ * running until event_base_loopexit() or event_base_loopbreak() makes us
+ * stop.
+ */
+#define EVLOOP_NO_EXIT_ON_EMPTY 0x04
/**@}*/
/**
@@ -657,6 +819,7 @@ int event_base_set(struct event_base *, struct event *);
@see event_base_loopexit(), event_base_dispatch(), EVLOOP_ONCE,
EVLOOP_NONBLOCK
*/
+EVENT2_EXPORT_SYMBOL
int event_base_loop(struct event_base *, int);
/**
@@ -674,6 +837,7 @@ int event_base_loop(struct event_base *, int);
@return 0 if successful, or -1 if an error occurred
@see event_base_loopbreak()
*/
+EVENT2_EXPORT_SYMBOL
int event_base_loopexit(struct event_base *, const struct timeval *);
/**
@@ -683,16 +847,37 @@ int event_base_loopexit(struct event_base *, const struct timeval *);
event_base_loopbreak() is typically invoked from this event's callback.
This behavior is analogous to the "break;" statement.
- Subsequent invocations of event_loop() will proceed normally.
+ Subsequent invocations of event_base_loop() will proceed normally.
@param eb the event_base structure returned by event_init()
@return 0 if successful, or -1 if an error occurred
@see event_base_loopexit()
*/
+EVENT2_EXPORT_SYMBOL
int event_base_loopbreak(struct event_base *);
/**
- Checks if the event loop was told to exit by event_loopexit().
+ Tell the active event_base_loop() to scan for new events immediately.
+
+ Calling this function makes the currently active event_base_loop()
+ start the loop over again (scanning for new events) after the current
+ event callback finishes. If the event loop is not running, this
+ function has no effect.
+
+ event_base_loopbreak() is typically invoked from this event's callback.
+ This behavior is analogous to the "continue;" statement.
+
+ Subsequent invocations of event loop will proceed normally.
+
+ @param eb the event_base structure returned by event_init()
+ @return 0 if successful, or -1 if an error occurred
+ @see event_base_loopbreak()
+ */
+EVENT2_EXPORT_SYMBOL
+int event_base_loopcontinue(struct event_base *);
+
+/**
+ Checks if the event loop was told to exit by event_base_loopexit().
This function will return true for an event_base at every point after
event_loopexit() is called, until the event loop is next entered.
@@ -703,13 +888,14 @@ int event_base_loopbreak(struct event_base *);
@see event_base_loopexit()
@see event_base_got_break()
*/
+EVENT2_EXPORT_SYMBOL
int event_base_got_exit(struct event_base *);
/**
- Checks if the event loop was told to abort immediately by event_loopbreak().
+ Checks if the event loop was told to abort immediately by event_base_loopbreak().
This function will return true for an event_base at every point after
- event_loopbreak() is called, until the event loop is next entered.
+ event_base_loopbreak() is called, until the event loop is next entered.
@param eb the event_base structure returned by event_init()
@return true if event_base_loopbreak() was called on this event base,
@@ -717,6 +903,7 @@ int event_base_got_exit(struct event_base *);
@see event_base_loopbreak()
@see event_base_got_exit()
*/
+EVENT2_EXPORT_SYMBOL
int event_base_got_break(struct event_base *);
/**
@@ -743,7 +930,28 @@ int event_base_got_break(struct event_base *);
*/
#define EV_PERSIST 0x10
/** Select edge-triggered behavior, if supported by the backend. */
-#define EV_ET 0x20
+#define EV_ET 0x20
+/**
+ * If this option is provided, then event_del() will not block in one thread
+ * while waiting for the event callback to complete in another thread.
+ *
+ * To use this option safely, you may need to use event_finalize() or
+ * event_free_finalize() in order to safely tear down an event in a
+ * multithreaded application. See those functions for more information.
+ *
+ * THIS IS AN EXPERIMENTAL API. IT MIGHT CHANGE BEFORE THE LIBEVENT 2.1 SERIES
+ * BECOMES STABLE.
+ **/
+#define EV_FINALIZE 0x40
+/**
+ * Detects connection close events. You can use this to detect when a
+ * connection has been closed, without having to read all the pending data
+ * from a connection.
+ *
+ * Not all backends support EV_CLOSED. To detect or require it, use the
+ * feature flag EV_FEATURE_EARLY_CLOSE.
+ **/
+#define EV_CLOSED 0x80
/**@}*/
/**
@@ -790,6 +998,31 @@ int event_base_got_break(struct event_base *);
typedef void (*event_callback_fn)(evutil_socket_t, short, void *);
/**
+ Return a value used to specify that the event itself must be used as the callback argument.
+
+ The function event_new() takes a callback argument which is passed
+ to the event's callback function. To specify that the argument to be
+ passed to the callback function is the event that event_new() returns,
+ pass in the return value of event_self_cbarg() as the callback argument
+ for event_new().
+
+ For example:
+ <pre>
+ struct event *ev = event_new(base, sock, events, callback, %event_self_cbarg());
+ </pre>
+
+ For consistency with event_new(), it is possible to pass the return value
+ of this function as the callback argument for event_assign() &ndash; this
+ achieves the same result as passing the event in directly.
+
+ @return a value to be passed as the callback argument to event_new() or
+ event_assign().
+ @see event_new(), event_assign()
+ */
+EVENT2_EXPORT_SYMBOL
+void *event_self_cbarg(void);
+
+/**
Allocate and asssign a new event structure, ready to be added.
The function event_new() returns a new event that can be used in
@@ -837,6 +1070,7 @@ typedef void (*event_callback_fn)(evutil_socket_t, short, void *);
event_free().
@see event_free(), event_add(), event_del(), event_assign()
*/
+EVENT2_EXPORT_SYMBOL
struct event *event_new(struct event_base *, evutil_socket_t, short, event_callback_fn, void *);
@@ -878,6 +1112,7 @@ struct event *event_new(struct event_base *, evutil_socket_t, short, event_callb
@see event_new(), event_add(), event_del(), event_base_once(),
event_get_struct_event_size()
*/
+EVENT2_EXPORT_SYMBOL
int event_assign(struct event *, struct event_base *, evutil_socket_t, short, event_callback_fn, void *);
/**
@@ -886,18 +1121,69 @@ int event_assign(struct event *, struct event_base *, evutil_socket_t, short, ev
If the event is pending or active, first make it non-pending and
non-active.
*/
+EVENT2_EXPORT_SYMBOL
void event_free(struct event *);
/**
+ * Callback type for event_finalize and event_free_finalize().
+ *
+ * THIS IS AN EXPERIMENTAL API. IT MIGHT CHANGE BEFORE THE LIBEVENT 2.1 SERIES
+ * BECOMES STABLE.
+ *
+ **/
+typedef void (*event_finalize_callback_fn)(struct event *, void *);
+/**
+ @name Finalization functions
+
+ These functions are used to safely tear down an event in a multithreaded
+ application. If you construct your events with EV_FINALIZE to avoid
+ deadlocks, you will need a way to remove an event in the certainty that
+ it will definitely not be running its callback when you deallocate it
+ and its callback argument.
+
+ To do this, call one of event_finalize() or event_free_finalize with
+ 0 for its first argument, the event to tear down as its second argument,
+ and a callback function as its third argument. The callback will be
+ invoked as part of the event loop, with the event's priority.
+
+ After you call a finalizer function, event_add() and event_active() will
+ no longer work on the event, and event_del() will produce a no-op. You
+ must not try to change the event's fields with event_assign() or
+ event_set() while the finalize callback is in progress. Once the
+ callback has been invoked, you should treat the event structure as
+ containing uninitialized memory.
+
+ The event_free_finalize() function frees the event after it's finalized;
+ event_finalize() does not.
+
+ A finalizer callback must not make events pending or active. It must not
+ add events, activate events, or attempt to "resucitate" the event being
+ finalized in any way.
+
+ THIS IS AN EXPERIMENTAL API. IT MIGHT CHANGE BEFORE THE LIBEVENT 2.1 SERIES
+ BECOMES STABLE.
+
+ @return 0 on succes, -1 on failure.
+ */
+/**@{*/
+EVENT2_EXPORT_SYMBOL
+int event_finalize(unsigned, struct event *, event_finalize_callback_fn);
+EVENT2_EXPORT_SYMBOL
+int event_free_finalize(unsigned, struct event *, event_finalize_callback_fn);
+/**@}*/
+
+/**
Schedule a one-time event
- The function event_base_once() is similar to event_set(). However, it
+ The function event_base_once() is similar to event_new(). However, it
schedules a callback to be called exactly once, and does not require the
caller to prepare an event structure.
- Note that in Libevent 2.0 and earlier, if the event is never triggered,
- the internal memory used to hold it will never be freed. This may be
- fixed in a later version of Libevent.
+ Note that in Libevent 2.0 and earlier, if the event is never triggered, the
+ internal memory used to hold it will never be freed. In Libevent 2.1,
+ the internal memory will get freed by event_base_free() if the event
+ is never triggered. The 'arg' value, however, will not get freed in either
+ case--you'll need to free that on your own if you want it to go away.
@param base an event_base
@param fd a file descriptor to monitor, or -1 for no fd.
@@ -910,13 +1196,14 @@ void event_free(struct event *);
EV_TIMEOUT event succees immediately.
@return 0 if successful, or -1 if an error occurred
*/
+EVENT2_EXPORT_SYMBOL
int event_base_once(struct event_base *, evutil_socket_t, short, event_callback_fn, void *, const struct timeval *);
/**
Add an event to the set of pending events.
- The function event_add() schedules the execution of the ev event when the
- event specified in event_assign()/event_new() occurs, or when the time
+ The function event_add() schedules the execution of the event 'ev' when the
+ condition specified by event_assign() or event_new() occurs, or when the time
specified in timeout has elapesed. If atimeout is NULL, no timeout
occurs and the function will only be
called if a matching event occurs. The event in the
@@ -925,18 +1212,30 @@ int event_base_once(struct event_base *, evutil_socket_t, short, event_callback_
in calls to event_assign() until it is no longer pending.
If the event in the ev argument already has a scheduled timeout, calling
- event_add() replaces the old timeout with the new one, or clears the old
- timeout if the timeout argument is NULL.
+ event_add() replaces the old timeout with the new one if tv is non-NULL.
- @param ev an event struct initialized via event_set()
+ @param ev an event struct initialized via event_assign() or event_new()
@param timeout the maximum amount of time to wait for the event, or NULL
to wait forever
@return 0 if successful, or -1 if an error occurred
@see event_del(), event_assign(), event_new()
*/
+EVENT2_EXPORT_SYMBOL
int event_add(struct event *ev, const struct timeval *timeout);
/**
+ Remove a timer from a pending event without removing the event itself.
+
+ If the event has a scheduled timeout, this function unschedules it but
+ leaves the event otherwise pending.
+
+ @param ev an event struct initialized via event_assign() or event_new()
+ @return 0 on success, or -1 if an error occurrect.
+*/
+EVENT2_EXPORT_SYMBOL
+int event_remove_timer(struct event *ev);
+
+/**
Remove an event from the set of monitored events.
The function event_del() will cancel the event in the argument ev. If the
@@ -947,8 +1246,29 @@ int event_add(struct event *ev, const struct timeval *timeout);
@return 0 if successful, or -1 if an error occurred
@see event_add()
*/
+EVENT2_EXPORT_SYMBOL
int event_del(struct event *);
+/**
+ As event_del(), but never blocks while the event's callback is running
+ in another thread, even if the event was constructed without the
+ EV_FINALIZE flag.
+
+ THIS IS AN EXPERIMENTAL API. IT MIGHT CHANGE BEFORE THE LIBEVENT 2.1 SERIES
+ BECOMES STABLE.
+ */
+EVENT2_EXPORT_SYMBOL
+int event_del_noblock(struct event *ev);
+/**
+ As event_del(), but always blocks while the event's callback is running
+ in another thread, even if the event was constructed with the
+ EV_FINALIZE flag.
+
+ THIS IS AN EXPERIMENTAL API. IT MIGHT CHANGE BEFORE THE LIBEVENT 2.1 SERIES
+ BECOMES STABLE.
+ */
+EVENT2_EXPORT_SYMBOL
+int event_del_block(struct event *ev);
/**
Make an event active.
@@ -964,6 +1284,7 @@ int event_del(struct event *);
@param res a set of flags to pass to the event's callback.
@param ncalls an obsolete argument: this is ignored.
**/
+EVENT2_EXPORT_SYMBOL
void event_active(struct event *ev, int res, short ncalls);
/**
@@ -979,8 +1300,17 @@ void event_active(struct event *ev, int res, short ncalls);
@return true if the event is pending on any of the events in 'what', (that
is to say, it has been added), or 0 if the event is not added.
*/
+EVENT2_EXPORT_SYMBOL
int event_pending(const struct event *ev, short events, struct timeval *tv);
+/**
+ If called from within the callback for an event, returns that event.
+
+ The behavior of this function is not defined when called from outside the
+ callback function for an event.
+ */
+EVENT2_EXPORT_SYMBOL
+struct event *event_base_get_running_event(struct event_base *base);
/**
Test if an event structure might be initialized.
@@ -997,6 +1327,7 @@ int event_pending(const struct event *ev, short events, struct timeval *tv);
@return 1 if the structure might be initialized, or 0 if it has not been
initialized
*/
+EVENT2_EXPORT_SYMBOL
int event_initialized(const struct event *ev);
/**
@@ -1008,35 +1339,48 @@ int event_initialized(const struct event *ev);
Get the socket or signal assigned to an event, or -1 if the event has
no socket.
*/
+EVENT2_EXPORT_SYMBOL
evutil_socket_t event_get_fd(const struct event *ev);
/**
Get the event_base associated with an event.
*/
+EVENT2_EXPORT_SYMBOL
struct event_base *event_get_base(const struct event *ev);
/**
Return the events (EV_READ, EV_WRITE, etc) assigned to an event.
*/
+EVENT2_EXPORT_SYMBOL
short event_get_events(const struct event *ev);
/**
Return the callback assigned to an event.
*/
+EVENT2_EXPORT_SYMBOL
event_callback_fn event_get_callback(const struct event *ev);
/**
Return the callback argument assigned to an event.
*/
+EVENT2_EXPORT_SYMBOL
void *event_get_callback_arg(const struct event *ev);
/**
+ Return the priority of an event.
+ @see event_priority_init(), event_get_priority()
+*/
+EVENT2_EXPORT_SYMBOL
+int event_get_priority(const struct event *ev);
+
+/**
Extract _all_ of arguments given to construct a given event. The
event_base is copied into *base_out, the fd is copied into *fd_out, and so
on.
If any of the "_out" arguments is NULL, it will be ignored.
*/
+EVENT2_EXPORT_SYMBOL
void event_get_assignment(const struct event *event,
struct event_base **base_out, evutil_socket_t *fd_out, short *events_out,
event_callback_fn *callback_out, void **arg_out);
@@ -1054,6 +1398,7 @@ void event_get_assignment(const struct event *event,
We might do this to help ensure ABI-compatibility between different
versions of Libevent.
*/
+EVENT2_EXPORT_SYMBOL
size_t event_get_struct_event_size(void);
/**
@@ -1065,6 +1410,7 @@ size_t event_get_struct_event_size(void);
@return a string containing the version number of Libevent
*/
+EVENT2_EXPORT_SYMBOL
const char *event_get_version(void);
/**
@@ -1078,13 +1424,14 @@ const char *event_get_version(void);
the version number. The low-order byte is unused. For example, version
2.0.1-alpha has a numeric representation of 0x02000100
*/
+EVENT2_EXPORT_SYMBOL
ev_uint32_t event_get_version_number(void);
/** As event_get_version, but gives the version of Libevent's headers. */
-#define LIBEVENT_VERSION _EVENT_VERSION
+#define LIBEVENT_VERSION EVENT__VERSION
/** As event_get_version_number, but gives the version number of Libevent's
* headers. */
-#define LIBEVENT_VERSION_NUMBER _EVENT_NUMERIC_VERSION
+#define LIBEVENT_VERSION_NUMBER EVENT__NUMERIC_VERSION
/** Largest number of priorities that Libevent can support. */
#define EVENT_MAX_PRIORITIES 256
@@ -1115,16 +1462,28 @@ ev_uint32_t event_get_version_number(void);
@return 0 if successful, or -1 if an error occurred
@see event_priority_set()
*/
+EVENT2_EXPORT_SYMBOL
int event_base_priority_init(struct event_base *, int);
/**
+ Get the number of different event priorities.
+
+ @param eb the event_base structure returned by event_base_new()
+ @return Number of different event priorities
+ @see event_base_priority_init()
+*/
+EVENT2_EXPORT_SYMBOL
+int event_base_get_npriorities(struct event_base *eb);
+
+/**
Assign a priority to an event.
@param ev an event struct
@param priority the new priority to be assigned
@return 0 if successful, or -1 if an error occurred
- @see event_priority_init()
+ @see event_priority_init(), event_get_priority()
*/
+EVENT2_EXPORT_SYMBOL
int event_priority_set(struct event *, int);
/**
@@ -1146,10 +1505,11 @@ int event_priority_set(struct event *, int);
(This optimization probably will not be worthwhile until you have thousands
or tens of thousands of events with the same timeout.)
*/
+EVENT2_EXPORT_SYMBOL
const struct timeval *event_base_init_common_timeout(struct event_base *base,
const struct timeval *duration);
-#if !defined(_EVENT_DISABLE_MM_REPLACEMENT) || defined(_EVENT_IN_DOXYGEN)
+#if !defined(EVENT__DISABLE_MM_REPLACEMENT) || defined(EVENT_IN_DOXYGEN_)
/**
Override the functions that Libevent uses for memory management.
@@ -1172,6 +1532,7 @@ const struct timeval *event_base_init_common_timeout(struct event_base *base,
@param realloc_fn A replacement for realloc
@param free_fn A replacement for free.
**/
+EVENT2_EXPORT_SYMBOL
void event_set_mem_functions(
void *(*malloc_fn)(size_t sz),
void *(*realloc_fn)(void *ptr, size_t sz),
@@ -1181,8 +1542,79 @@ void event_set_mem_functions(
#define EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
#endif
+/**
+ Writes a human-readable description of all inserted and/or active
+ events to a provided stdio stream.
+
+ This is intended for debugging; its format is not guaranteed to be the same
+ between libevent versions.
+
+ @param base An event_base on which to scan the events.
+ @param output A stdio file to write on.
+ */
+EVENT2_EXPORT_SYMBOL
void event_base_dump_events(struct event_base *, FILE *);
+
+/**
+ Activates all pending events for the given fd and event mask.
+
+ This function activates pending events only. Events which have not been
+ added will not become active.
+
+ @param base the event_base on which to activate the events.
+ @param fd An fd to active events on.
+ @param events One or more of EV_{READ,WRITE}.
+ */
+EVENT2_EXPORT_SYMBOL
+void event_base_active_by_fd(struct event_base *base, evutil_socket_t fd, short events);
+
+/**
+ Activates all pending signals with a given signal number
+
+ This function activates pending events only. Events which have not been
+ added will not become active.
+
+ @param base the event_base on which to activate the events.
+ @param fd The signal to active events on.
+ */
+EVENT2_EXPORT_SYMBOL
+void event_base_active_by_signal(struct event_base *base, int sig);
+
+/**
+ * Callback for iterating events in an event base via event_base_foreach_event
+ */
+typedef int (*event_base_foreach_event_cb)(const struct event_base *, const struct event *, void *);
+
+/**
+ Iterate over all added or active events events in an event loop, and invoke
+ a given callback on each one.
+
+ The callback must not call any function that modifies the event base, that
+ modifies any event in the event base, or that adds or removes any event to
+ the event base. Doing so is unsupported and will lead to undefined
+ behavior -- likely, to crashes.
+
+ event_base_foreach_event() holds a lock on the event_base() for the whole
+ time it's running: slow callbacks are not advisable.
+
+ Note that Libevent adds some events of its own to make pieces of its
+ functionality work. You must not assume that the only events you'll
+ encounter will be the ones you added yourself.
+
+ The callback function must return 0 to continue iteration, or some other
+ integer to stop iterating.
+
+ @param base An event_base on which to scan the events.
+ @param fn A callback function to receive the events.
+ @param arg An argument passed to the callback function.
+ @return 0 if we iterated over every event, or the value returned by the
+ callback function if the loop exited early.
+*/
+EVENT2_EXPORT_SYMBOL
+int event_base_foreach_event(struct event_base *base, event_base_foreach_event_cb fn, void *arg);
+
+
/** Sets 'tv' to the current time (as returned by gettimeofday()),
looking at the cached value in 'base' if possible, and calling
gettimeofday() or clock_gettime() as appropriate if there is no
@@ -1194,11 +1626,44 @@ void event_base_dump_events(struct event_base *, FILE *);
Returns 0 on success, negative on failure.
*/
+EVENT2_EXPORT_SYMBOL
int event_base_gettimeofday_cached(struct event_base *base,
struct timeval *tv);
+/** Update cached_tv in the 'base' to the current time
+ *
+ * You can use this function is useful for selectively increasing
+ * the accuracy of the cached time value in 'base' during callbacks
+ * that take a long time to execute.
+ *
+ * This function has no effect if the base is currently not in its
+ * event loop, or if timeval caching is disabled via
+ * EVENT_BASE_FLAG_NO_CACHE_TIME.
+ *
+ * @return 0 on success, -1 on failure
+ */
+EVENT2_EXPORT_SYMBOL
+int event_base_update_cache_time(struct event_base *base);
+
+/** Release up all globally-allocated resources allocated by Libevent.
+
+ This function does not free developer-controlled resources like
+ event_bases, events, bufferevents, listeners, and so on. It only releases
+ resources like global locks that there is no other way to free.
+
+ It is not actually necessary to call this function before exit: every
+ resource that it frees would be released anyway on exit. It mainly exists
+ so that resource-leak debugging tools don't see Libevent as holding
+ resources at exit.
+
+ You should only call this function when no other Libevent functions will
+ be invoked -- e.g., when cleanly exiting a program.
+ */
+EVENT2_EXPORT_SYMBOL
+void libevent_global_shutdown(void);
+
#ifdef __cplusplus
}
#endif
-#endif /* _EVENT2_EVENT_H_ */
+#endif /* EVENT2_EVENT_H_INCLUDED_ */
diff --git a/include/event2/event_compat.h b/include/event2/event_compat.h
index 14a10e9e..5110175a 100644
--- a/include/event2/event_compat.h
+++ b/include/event2/event_compat.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_EVENT_COMPAT_H_
-#define _EVENT2_EVENT_COMPAT_H_
+#ifndef EVENT2_EVENT_COMPAT_H_INCLUDED_
+#define EVENT2_EVENT_COMPAT_H_INCLUDED_
/** @file event2/event_compat.h
@@ -41,16 +41,17 @@
@deprecated All functions in this file are by definition deprecated.
*/
+#include <event2/visibility.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <event2/event-config.h>
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -70,6 +71,7 @@ extern "C" {
@see event_base_set(), event_base_new()
*/
+EVENT2_EXPORT_SYMBOL
struct event_base *event_init(void);
/**
@@ -83,6 +85,7 @@ struct event_base *event_init(void);
@see event_base_dispatch(), event_init()
*/
+EVENT2_EXPORT_SYMBOL
int event_dispatch(void);
/**
@@ -96,6 +99,7 @@ int event_dispatch(void);
@see event_base_loop(), event_init()
*/
+EVENT2_EXPORT_SYMBOL
int event_loop(int);
@@ -111,6 +115,7 @@ int event_loop(int);
@see event_init, event_base_loopexit()
*/
+EVENT2_EXPORT_SYMBOL
int event_loopexit(const struct timeval *);
@@ -126,6 +131,7 @@ int event_loopexit(const struct timeval *);
@see event_base_loopbreak(), event_init()
*/
+EVENT2_EXPORT_SYMBOL
int event_loopbreak(void);
/**
@@ -137,6 +143,7 @@ int event_loopbreak(void);
@see event_base_once()
*/
+EVENT2_EXPORT_SYMBOL
int event_once(evutil_socket_t , short,
void (*)(evutil_socket_t, short, void *), void *, const struct timeval *);
@@ -150,6 +157,7 @@ int event_once(evutil_socket_t , short,
@see event_base_get_method()
*/
+EVENT2_EXPORT_SYMBOL
const char *event_get_method(void);
@@ -162,6 +170,7 @@ const char *event_get_method(void);
@see event_base_priority_init()
*/
+EVENT2_EXPORT_SYMBOL
int event_priority_init(int);
/**
@@ -171,6 +180,7 @@ int event_priority_init(int);
a subsequent call to event_base_set() to be safe under most circumstances.
Use event_assign() or event_new() instead.
*/
+EVENT2_EXPORT_SYMBOL
void event_set(struct event *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *);
#define evtimer_set(ev, cb, arg) event_set((ev), -1, 0, (cb), (arg))
@@ -217,4 +227,4 @@ void event_set(struct event *, evutil_socket_t, short, void (*)(evutil_socket_t,
}
#endif
-#endif /* _EVENT2_EVENT_COMPAT_H_ */
+#endif /* EVENT2_EVENT_COMPAT_H_INCLUDED_ */
diff --git a/include/event2/event_struct.h b/include/event2/event_struct.h
index 2ed4272c..1c8b71b6 100644
--- a/include/event2/event_struct.h
+++ b/include/event2/event_struct.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_EVENT_STRUCT_H_
-#define _EVENT2_EVENT_STRUCT_H_
+#ifndef EVENT2_EVENT_STRUCT_H_INCLUDED_
+#define EVENT2_EVENT_STRUCT_H_INCLUDED_
/** @file event2/event_struct.h
@@ -41,10 +41,10 @@ extern "C" {
#endif
#include <event2/event-config.h>
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -54,19 +54,20 @@ extern "C" {
/* For evkeyvalq */
#include <event2/keyvalq_struct.h>
-#define EVLIST_TIMEOUT 0x01
-#define EVLIST_INSERTED 0x02
-#define EVLIST_SIGNAL 0x04
-#define EVLIST_ACTIVE 0x08
-#define EVLIST_INTERNAL 0x10
-#define EVLIST_INIT 0x80
+#define EVLIST_TIMEOUT 0x01
+#define EVLIST_INSERTED 0x02
+#define EVLIST_SIGNAL 0x04
+#define EVLIST_ACTIVE 0x08
+#define EVLIST_INTERNAL 0x10
+#define EVLIST_ACTIVE_LATER 0x20
+#define EVLIST_FINALIZING 0x40
+#define EVLIST_INIT 0x80
-/* EVLIST_X_ Private space: 0x1000-0xf000 */
-#define EVLIST_ALL (0xf000 | 0x9f)
+#define EVLIST_ALL 0xff
/* Fix so that people don't have to run with <sys/queue.h> */
#ifndef TAILQ_ENTRY
-#define _EVENT_DEFINED_TQENTRY
+#define EVENT_DEFINED_TQENTRY_
#define TAILQ_ENTRY(type) \
struct { \
struct type *tqe_next; /* next element */ \
@@ -75,7 +76,7 @@ struct { \
#endif /* !TAILQ_ENTRY */
#ifndef TAILQ_HEAD
-#define _EVENT_DEFINED_TQHEAD
+#define EVENT_DEFINED_TQHEAD_
#define TAILQ_HEAD(name, type) \
struct name { \
struct type *tqh_first; \
@@ -83,10 +84,45 @@ struct name { \
}
#endif
+/* Fix so that people don't have to run with <sys/queue.h> */
+#ifndef LIST_ENTRY
+#define EVENT_DEFINED_LISTENTRY_
+#define LIST_ENTRY(type) \
+struct { \
+ struct type *le_next; /* next element */ \
+ struct type **le_prev; /* address of previous next element */ \
+}
+#endif /* !LIST_ENTRY */
+
+#ifndef LIST_HEAD
+#define EVENT_DEFINED_LISTHEAD_
+#define LIST_HEAD(name, type) \
+struct name { \
+ struct type *lh_first; /* first element */ \
+ }
+#endif /* !LIST_HEAD */
+
+struct event;
+
+struct event_callback {
+ TAILQ_ENTRY(event_callback) evcb_active_next;
+ short evcb_flags;
+ ev_uint8_t evcb_pri; /* smaller numbers are higher priority */
+ ev_uint8_t evcb_closure;
+ /* allows us to adopt for different types of events */
+ union {
+ void (*evcb_callback)(evutil_socket_t, short, void *);
+ void (*evcb_selfcb)(struct event_callback *, void *);
+ void (*evcb_evfinalize)(struct event *, void *);
+ void (*evcb_cbfinalize)(struct event_callback *, void *);
+ } evcb_cb_union;
+ void *evcb_arg;
+};
+
struct event_base;
struct event {
- TAILQ_ENTRY(event) ev_active_next;
- TAILQ_ENTRY(event) ev_next;
+ struct event_callback ev_evcallback;
+
/* for managing timeouts */
union {
TAILQ_ENTRY(event) ev_next_with_common_timeout;
@@ -99,43 +135,46 @@ struct event {
union {
/* used for io events */
struct {
- TAILQ_ENTRY(event) ev_io_next;
+ LIST_ENTRY (event) ev_io_next;
struct timeval ev_timeout;
} ev_io;
/* used by signal events */
struct {
- TAILQ_ENTRY(event) ev_signal_next;
+ LIST_ENTRY (event) ev_signal_next;
short ev_ncalls;
/* Allows deletes in callback */
short *ev_pncalls;
} ev_signal;
- } _ev;
+ } ev_;
short ev_events;
short ev_res; /* result passed to event callback */
- short ev_flags;
- ev_uint8_t ev_pri; /* smaller numbers are higher priority */
- ev_uint8_t ev_closure;
struct timeval ev_timeout;
-
- /* allows us to adopt for different types of events */
- void (*ev_callback)(evutil_socket_t, short, void *arg);
- void *ev_arg;
};
TAILQ_HEAD (event_list, event);
-#ifdef _EVENT_DEFINED_TQENTRY
+#ifdef EVENT_DEFINED_TQENTRY_
#undef TAILQ_ENTRY
#endif
-#ifdef _EVENT_DEFINED_TQHEAD
+#ifdef EVENT_DEFINED_TQHEAD_
#undef TAILQ_HEAD
#endif
+LIST_HEAD (event_dlist, event);
+
+#ifdef EVENT_DEFINED_LISTENTRY_
+#undef LIST_ENTRY
+#endif
+
+#ifdef EVENT_DEFINED_LISTHEAD_
+#undef LIST_HEAD
+#endif
+
#ifdef __cplusplus
}
#endif
-#endif /* _EVENT2_EVENT_STRUCT_H_ */
+#endif /* EVENT2_EVENT_STRUCT_H_INCLUDED_ */
diff --git a/include/event2/http.h b/include/event2/http.h
index c6ee1dbe..4284d5fc 100644
--- a/include/event2/http.h
+++ b/include/event2/http.h
@@ -24,11 +24,12 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_HTTP_H_
-#define _EVENT2_HTTP_H_
+#ifndef EVENT2_HTTP_H_INCLUDED_
+#define EVENT2_HTTP_H_INCLUDED_
/* For int types. */
#include <event2/util.h>
+#include <event2/visibility.h>
#ifdef __cplusplus
extern "C" {
@@ -37,6 +38,8 @@ extern "C" {
/* In case we haven't included the right headers yet. */
struct evbuffer;
struct event_base;
+struct bufferevent;
+struct evhttp_connection;
/** @file event2/http.h
*
@@ -69,6 +72,7 @@ struct evhttp_request;
struct evkeyvalq;
struct evhttp_bound_socket;
struct evconnlistener;
+struct evdns_base;
/**
* Create a new HTTP server.
@@ -77,6 +81,7 @@ struct evconnlistener;
* @return a pointer to a newly initialized evhttp server structure
* @see evhttp_free()
*/
+EVENT2_EXPORT_SYMBOL
struct evhttp *evhttp_new(struct event_base *base);
/**
@@ -91,6 +96,7 @@ struct evhttp *evhttp_new(struct event_base *base);
* @return 0 on success, -1 on failure.
* @see evhttp_accept_socket()
*/
+EVENT2_EXPORT_SYMBOL
int evhttp_bind_socket(struct evhttp *http, const char *address, ev_uint16_t port);
/**
@@ -104,6 +110,7 @@ int evhttp_bind_socket(struct evhttp *http, const char *address, ev_uint16_t por
* @return Handle for the socket on success, NULL on failure.
* @see evhttp_bind_socket(), evhttp_del_accept_socket()
*/
+EVENT2_EXPORT_SYMBOL
struct evhttp_bound_socket *evhttp_bind_socket_with_handle(struct evhttp *http, const char *address, ev_uint16_t port);
/**
@@ -122,6 +129,7 @@ struct evhttp_bound_socket *evhttp_bind_socket_with_handle(struct evhttp *http,
* @return 0 on success, -1 on failure.
* @see evhttp_bind_socket()
*/
+EVENT2_EXPORT_SYMBOL
int evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd);
/**
@@ -134,6 +142,7 @@ int evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd);
* @return Handle for the socket on success, NULL on failure.
* @see evhttp_accept_socket(), evhttp_del_accept_socket()
*/
+EVENT2_EXPORT_SYMBOL
struct evhttp_bound_socket *evhttp_accept_socket_with_handle(struct evhttp *http, evutil_socket_t fd);
/**
@@ -141,13 +150,29 @@ struct evhttp_bound_socket *evhttp_accept_socket_with_handle(struct evhttp *http
* returns an evhttp_bound_socket. The listener will be freed when the bound
* socket is freed.
*/
+EVENT2_EXPORT_SYMBOL
struct evhttp_bound_socket *evhttp_bind_listener(struct evhttp *http, struct evconnlistener *listener);
/**
* Return the listener used to implement a bound socket.
*/
+EVENT2_EXPORT_SYMBOL
struct evconnlistener *evhttp_bound_socket_get_listener(struct evhttp_bound_socket *bound);
+typedef void evhttp_bound_socket_foreach_fn(struct evhttp_bound_socket *, void *);
+/**
+ * Applies the function specified in the first argument to all
+ * evhttp_bound_sockets associated with "http". The user must not
+ * attempt to free or remove any connections, sockets or listeners
+ * in the callback "function".
+ *
+ * @param http pointer to an evhttp object
+ * @param function function to apply to every bound socket
+ * @param argument pointer value passed to function for every socket iterated
+ */
+EVENT2_EXPORT_SYMBOL
+void evhttp_foreach_bound_socket(struct evhttp *http, evhttp_bound_socket_foreach_fn *function, void *argument);
+
/**
* Makes an HTTP server stop accepting connections on the specified socket
*
@@ -165,6 +190,7 @@ struct evconnlistener *evhttp_bound_socket_get_listener(struct evhttp_bound_sock
* @param bound_socket a handle returned by evhttp_{bind,accept}_socket_with_handle
* @see evhttp_bind_socket_with_handle(), evhttp_accept_socket_with_handle()
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_del_accept_socket(struct evhttp *http, struct evhttp_bound_socket *bound_socket);
/**
@@ -174,6 +200,7 @@ void evhttp_del_accept_socket(struct evhttp *http, struct evhttp_bound_socket *b
* @return the file descriptor used by the bound socket
* @see evhttp_bind_socket_with_handle(), evhttp_accept_socket_with_handle()
*/
+EVENT2_EXPORT_SYMBOL
evutil_socket_t evhttp_bound_socket_get_fd(struct evhttp_bound_socket *bound_socket);
/**
@@ -184,14 +211,29 @@ evutil_socket_t evhttp_bound_socket_get_fd(struct evhttp_bound_socket *bound_soc
* @param http the evhttp server object to be freed
* @see evhttp_start()
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_free(struct evhttp* http);
/** XXX Document. */
+EVENT2_EXPORT_SYMBOL
void evhttp_set_max_headers_size(struct evhttp* http, ev_ssize_t max_headers_size);
/** XXX Document. */
+EVENT2_EXPORT_SYMBOL
void evhttp_set_max_body_size(struct evhttp* http, ev_ssize_t max_body_size);
/**
+ Set the value to use for the Content-Type header when none was provided. If
+ the content type string is NULL, the Content-Type header will not be
+ automatically added.
+
+ @param http the http server on which to set the default content type
+ @param content_type the value for the Content-Type header
+*/
+EVENT2_EXPORT_SYMBOL
+void evhttp_set_default_content_type(struct evhttp *http,
+ const char *content_type);
+
+/**
Sets the what HTTP methods are supported in requests accepted by this
server, and passed to user callbacks.
@@ -202,6 +244,7 @@ void evhttp_set_max_body_size(struct evhttp* http, ev_ssize_t max_body_size);
@param http the http server on which to set the methods
@param methods bit mask constructed from evhttp_cmd_type values
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_set_allowed_methods(struct evhttp* http, ev_uint16_t methods);
/**
@@ -213,10 +256,12 @@ void evhttp_set_allowed_methods(struct evhttp* http, ev_uint16_t methods);
@param cb_arg an additional context argument for the callback
@return 0 on success, -1 if the callback existed already, -2 on failure
*/
+EVENT2_EXPORT_SYMBOL
int evhttp_set_cb(struct evhttp *http, const char *path,
void (*cb)(struct evhttp_request *, void *), void *cb_arg);
/** Removes the callback for a specified URI */
+EVENT2_EXPORT_SYMBOL
int evhttp_del_cb(struct evhttp *, const char *);
/**
@@ -230,10 +275,29 @@ int evhttp_del_cb(struct evhttp *, const char *);
@param cb the callback to invoke for any unmatched requests
@param arg an context argument for the callback
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_set_gencb(struct evhttp *http,
void (*cb)(struct evhttp_request *, void *), void *arg);
/**
+ Set a callback used to create new bufferevents for connections
+ to a given evhttp object.
+
+ You can use this to override the default bufferevent type -- for example,
+ to make this evhttp object use SSL bufferevents rather than unencrypted
+ ones.
+
+ New bufferevents must be allocated with no fd set on them.
+
+ @param http the evhttp server object for which to set the callback
+ @param cb the callback to invoke for incoming connections
+ @param arg an context argument for the callback
+ */
+EVENT2_EXPORT_SYMBOL
+void evhttp_set_bevcb(struct evhttp *http,
+ struct bufferevent *(*cb)(struct event_base *, void *), void *arg);
+
+/**
Adds a virtual host to the http server.
A virtual host is a newly initialized evhttp object that has request
@@ -255,6 +319,7 @@ void evhttp_set_gencb(struct evhttp *http,
@return 0 on success, -1 on failure
@see evhttp_remove_virtual_host()
*/
+EVENT2_EXPORT_SYMBOL
int evhttp_add_virtual_host(struct evhttp* http, const char *pattern,
struct evhttp* vhost);
@@ -266,6 +331,7 @@ int evhttp_add_virtual_host(struct evhttp* http, const char *pattern,
@return 0 on success, -1 on failure
@see evhttp_add_virtual_host()
*/
+EVENT2_EXPORT_SYMBOL
int evhttp_remove_virtual_host(struct evhttp* http, struct evhttp* vhost);
/**
@@ -276,6 +342,7 @@ int evhttp_remove_virtual_host(struct evhttp* http, struct evhttp* vhost);
@param alias the alias to add
@see evhttp_add_remove_alias()
*/
+EVENT2_EXPORT_SYMBOL
int evhttp_add_server_alias(struct evhttp *http, const char *alias);
/**
@@ -285,6 +352,7 @@ int evhttp_add_server_alias(struct evhttp *http, const char *alias);
@param alias the alias to remove
@see evhttp_add_server_alias()
*/
+EVENT2_EXPORT_SYMBOL
int evhttp_remove_server_alias(struct evhttp *http, const char *alias);
/**
@@ -293,8 +361,18 @@ int evhttp_remove_server_alias(struct evhttp *http, const char *alias);
* @param http an evhttp object
* @param timeout_in_secs the timeout, in seconds
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_set_timeout(struct evhttp *http, int timeout_in_secs);
+/**
+ * Set the timeout for an HTTP request.
+ *
+ * @param http an evhttp object
+ * @param tv the timeout, or NULL
+ */
+EVENT2_EXPORT_SYMBOL
+void evhttp_set_timeout_tv(struct evhttp *http, const struct timeval* tv);
+
/* Request/Response functionality */
/**
@@ -305,6 +383,7 @@ void evhttp_set_timeout(struct evhttp *http, int timeout_in_secs);
* @param reason a brief explanation of the error. If this is NULL, we'll
* just use the standard meaning of the error code.
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_send_error(struct evhttp_request *req, int error,
const char *reason);
@@ -321,6 +400,7 @@ void evhttp_send_error(struct evhttp_request *req, int error,
* @param reason a brief message to send with the response code
* @param databuf the body of the response
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_send_reply(struct evhttp_request *req, int code,
const char *reason, struct evbuffer *databuf);
@@ -340,6 +420,7 @@ void evhttp_send_reply(struct evhttp_request *req, int code,
@param code the HTTP response code to send
@param reason a brief message to send with the response code
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_send_reply_start(struct evhttp_request *req, int code,
const char *reason);
@@ -354,13 +435,33 @@ void evhttp_send_reply_start(struct evhttp_request *req, int code,
@param req a request object
@param databuf the data chunk to send as part of the reply.
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_send_reply_chunk(struct evhttp_request *req,
struct evbuffer *databuf);
+
+/**
+ Send another data chunk as part of an ongoing chunked reply.
+
+ The reply chunk consists of the data in databuf. After calling
+ evhttp_send_reply_chunk() databuf will be empty, but the buffer is
+ still owned by the caller and needs to be deallocated by the caller
+ if necessary.
+
+ @param req a request object
+ @param databuf the data chunk to send as part of the reply.
+ @param cb callback funcion
+ @param call back's argument.
+*/
+EVENT2_EXPORT_SYMBOL
+void evhttp_send_reply_chunk_with_cb(struct evhttp_request *, struct evbuffer *,
+ void (*cb)(struct evhttp_connection *, void *), void *arg);
+
/**
Complete a chunked reply, freeing the request as appropriate.
@param req a request object
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_send_reply_end(struct evhttp_request *req);
/*
@@ -390,10 +491,42 @@ enum evhttp_cmd_type {
enum evhttp_request_kind { EVHTTP_REQUEST, EVHTTP_RESPONSE };
/**
+ * Create and return a connection object that can be used to for making HTTP
+ * requests. The connection object tries to resolve address and establish the
+ * connection when it is given an http request object.
+ *
+ * @param base the event_base to use for handling the connection
+ * @param dnsbase the dns_base to use for resolving host names; if not
+ * specified host name resolution will block.
+ * @param bev a bufferevent to use for connecting to the server; if NULL, a
+ * socket-based bufferevent will be created. This buffrevent will be freed
+ * when the connection closes. It must have no fd set on it.
+ * @param address the address to which to connect
+ * @param port the port to connect to
+ * @return an evhttp_connection object that can be used for making requests
+ */
+EVENT2_EXPORT_SYMBOL
+struct evhttp_connection *evhttp_connection_base_bufferevent_new(
+ struct event_base *base, struct evdns_base *dnsbase, struct bufferevent* bev, const char *address, unsigned short port);
+
+/**
+ * Return the bufferevent that an evhttp_connection is using.
+ */
+EVENT2_EXPORT_SYMBOL
+struct bufferevent* evhttp_connection_get_bufferevent(struct evhttp_connection *evcon);
+
+/**
+ * Return the HTTP server associated with this connection, or NULL.
+ */
+EVENT2_EXPORT_SYMBOL
+struct evhttp *evhttp_connection_get_server(struct evhttp_connection *evcon);
+
+/**
* Creates a new request object that needs to be filled in with the request
* parameters. The callback is executed when the request completed or an
* error occurred.
*/
+EVENT2_EXPORT_SYMBOL
struct evhttp_request *evhttp_request_new(
void (*cb)(struct evhttp_request *, void *), void *arg);
@@ -404,18 +537,86 @@ struct evhttp_request *evhttp_request_new(
* response. May drain the input buffer; it will be drained
* automatically on return.
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_request_set_chunked_cb(struct evhttp_request *,
void (*cb)(struct evhttp_request *, void *));
+/**
+ * Register callback for additional parsing of request headers.
+ * @param cb will be called after receiving and parsing the full header.
+ * It allows analyzing the header and possibly closing the connection
+ * by returning a value < 0.
+ */
+EVENT2_EXPORT_SYMBOL
+void evhttp_request_set_header_cb(struct evhttp_request *,
+ int (*cb)(struct evhttp_request *, void *));
+
+/**
+ * The different error types supported by evhttp
+ *
+ * @see evhttp_request_set_error_cb()
+ */
+enum evhttp_request_error {
+ /**
+ * Timeout reached, also @see evhttp_connection_set_timeout()
+ */
+ EVREQ_HTTP_TIMEOUT,
+ /**
+ * EOF reached
+ */
+ EVREQ_HTTP_EOF,
+ /**
+ * Error while reading header, or invalid header
+ */
+ EVREQ_HTTP_INVALID_HEADER,
+ /**
+ * Error encountered while reading or writing
+ */
+ EVREQ_HTTP_BUFFER_ERROR,
+ /**
+ * The evhttp_cancel_request() called on this request.
+ */
+ EVREQ_HTTP_REQUEST_CANCEL,
+ /**
+ * Body is greater then evhttp_connection_set_max_body_size()
+ */
+ EVREQ_HTTP_DATA_TOO_LONG
+};
+/**
+ * Set a callback for errors
+ * @see evhttp_request_error for error types.
+ *
+ * On error, both the error callback and the regular callback will be called,
+ * error callback is called before the regular callback.
+ **/
+EVENT2_EXPORT_SYMBOL
+void evhttp_request_set_error_cb(struct evhttp_request *,
+ void (*)(enum evhttp_request_error, void *));
+
+/**
+ * Set a callback to be called on request completion of evhttp_send_* function.
+ *
+ * The callback function will be called on the completion of the request after
+ * the output data has been written and before the evhttp_request object
+ * is destroyed. This can be useful for tracking resources associated with a
+ * request (ex: timing metrics).
+ *
+ * @param req a request object
+ * @param cb callback function that will be called on request completion
+ * @param cb_arg an additional context argument for the callback
+ */
+EVENT2_EXPORT_SYMBOL
+void evhttp_request_set_on_complete_cb(struct evhttp_request *req,
+ void (*cb)(struct evhttp_request *, void *), void *cb_arg);
+
/** Frees the request object and removes associated events. */
+EVENT2_EXPORT_SYMBOL
void evhttp_request_free(struct evhttp_request *req);
-struct evdns_base;
-
/**
- * A connection object that can be used to for making HTTP requests. The
- * connection object tries to resolve address and establish the connection
- * when it is given an http request object.
+ * Create and return a connection object that can be used to for making HTTP
+ * requests. The connection object tries to resolve address and establish the
+ * connection when it is given an http request object.
*
* @param base the event_base to use for handling the connection
* @param dnsbase the dns_base to use for resolving host names; if not
@@ -424,24 +625,27 @@ struct evdns_base;
* @param port the port to connect to
* @return an evhttp_connection object that can be used for making requests
*/
+EVENT2_EXPORT_SYMBOL
struct evhttp_connection *evhttp_connection_base_new(
struct event_base *base, struct evdns_base *dnsbase,
const char *address, unsigned short port);
/**
- * Return the bufferevent that an evhttp_connection is using.
+ * Set family hint for DNS requests.
*/
-struct bufferevent *evhttp_connection_get_bufferevent(
- struct evhttp_connection *evcon);
+void evhttp_connection_set_family(struct evhttp_connection *evcon,
+ int family);
/** Takes ownership of the request object
*
* Can be used in a request callback to keep onto the request until
* evhttp_request_free() is explicitly called by the user.
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_request_own(struct evhttp_request *req);
/** Returns 1 if the request is owned by the user */
+EVENT2_EXPORT_SYMBOL
int evhttp_request_is_owned(struct evhttp_request *req);
/**
@@ -450,46 +654,89 @@ int evhttp_request_is_owned(struct evhttp_request *req);
* The user needs to either free the request explicitly or call
* evhttp_send_reply_end().
*/
+EVENT2_EXPORT_SYMBOL
struct evhttp_connection *evhttp_request_get_connection(struct evhttp_request *req);
/**
* Returns the underlying event_base for this connection
*/
+EVENT2_EXPORT_SYMBOL
struct event_base *evhttp_connection_get_base(struct evhttp_connection *req);
+EVENT2_EXPORT_SYMBOL
void evhttp_connection_set_max_headers_size(struct evhttp_connection *evcon,
ev_ssize_t new_max_headers_size);
+EVENT2_EXPORT_SYMBOL
void evhttp_connection_set_max_body_size(struct evhttp_connection* evcon,
ev_ssize_t new_max_body_size);
/** Frees an http connection */
+EVENT2_EXPORT_SYMBOL
void evhttp_connection_free(struct evhttp_connection *evcon);
+/** Disowns a given connection object
+ *
+ * Can be used to tell libevent to free the connection object after
+ * the last request has completed or failed.
+ */
+EVENT2_EXPORT_SYMBOL
+void evhttp_connection_free_on_completion(struct evhttp_connection *evcon);
+
/** sets the ip address from which http connections are made */
+EVENT2_EXPORT_SYMBOL
void evhttp_connection_set_local_address(struct evhttp_connection *evcon,
const char *address);
/** sets the local port from which http connections are made */
+EVENT2_EXPORT_SYMBOL
void evhttp_connection_set_local_port(struct evhttp_connection *evcon,
ev_uint16_t port);
-/** Sets the timeout for events related to this connection */
+/** Sets the timeout in seconds for events related to this connection */
+EVENT2_EXPORT_SYMBOL
void evhttp_connection_set_timeout(struct evhttp_connection *evcon,
int timeout_in_secs);
+/** Sets the timeout for events related to this connection. Takes a struct
+ * timeval. */
+EVENT2_EXPORT_SYMBOL
+void evhttp_connection_set_timeout_tv(struct evhttp_connection *evcon,
+ const struct timeval *tv);
+
+/** Sets the delay before retrying requests on this connection. This is only
+ * used if evhttp_connection_set_retries is used to make the number of retries
+ * at least one. Each retry after the first is twice as long as the one before
+ * it. */
+EVENT2_EXPORT_SYMBOL
+void evhttp_connection_set_initial_retry_tv(struct evhttp_connection *evcon,
+ const struct timeval *tv);
+
/** Sets the retry limit for this connection - -1 repeats indefinitely */
+EVENT2_EXPORT_SYMBOL
void evhttp_connection_set_retries(struct evhttp_connection *evcon,
int retry_max);
/** Set a callback for connection close. */
+EVENT2_EXPORT_SYMBOL
void evhttp_connection_set_closecb(struct evhttp_connection *evcon,
void (*)(struct evhttp_connection *, void *), void *);
/** Get the remote address and port associated with this connection. */
+EVENT2_EXPORT_SYMBOL
void evhttp_connection_get_peer(struct evhttp_connection *evcon,
char **address, ev_uint16_t *port);
+/** Get the remote address associated with this connection.
+ * extracted from getpeername().
+ *
+ * @return NULL if getpeername() return non success,
+ * or connection is not connected,
+ * otherwise it return pointer to struct sockaddr_storage */
+EVENT2_EXPORT_SYMBOL
+const struct sockaddr*
+evhttp_connection_get_addr(struct evhttp_connection *evcon);
+
/**
Make an HTTP request over the specified connection.
@@ -503,6 +750,7 @@ void evhttp_connection_get_peer(struct evhttp_connection *evcon,
@return 0 on success, -1 on failure
@see evhttp_cancel_request()
*/
+EVENT2_EXPORT_SYMBOL
int evhttp_make_request(struct evhttp_connection *evcon,
struct evhttp_request *req,
enum evhttp_cmd_type type, const char *uri);
@@ -520,6 +768,7 @@ int evhttp_make_request(struct evhttp_connection *evcon,
@param req the evhttp_request to cancel; req becomes invalid after this call.
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_cancel_request(struct evhttp_request *req);
/**
@@ -528,26 +777,37 @@ void evhttp_cancel_request(struct evhttp_request *req);
struct evhttp_uri;
/** Returns the request URI */
+EVENT2_EXPORT_SYMBOL
const char *evhttp_request_get_uri(const struct evhttp_request *req);
/** Returns the request URI (parsed) */
+EVENT2_EXPORT_SYMBOL
const struct evhttp_uri *evhttp_request_get_evhttp_uri(const struct evhttp_request *req);
/** Returns the request command */
+EVENT2_EXPORT_SYMBOL
enum evhttp_cmd_type evhttp_request_get_command(const struct evhttp_request *req);
+EVENT2_EXPORT_SYMBOL
int evhttp_request_get_response_code(const struct evhttp_request *req);
+EVENT2_EXPORT_SYMBOL
+const char * evhttp_request_get_response_code_line(const struct evhttp_request *req);
/** Returns the input headers */
+EVENT2_EXPORT_SYMBOL
struct evkeyvalq *evhttp_request_get_input_headers(struct evhttp_request *req);
/** Returns the output headers */
+EVENT2_EXPORT_SYMBOL
struct evkeyvalq *evhttp_request_get_output_headers(struct evhttp_request *req);
/** Returns the input buffer */
+EVENT2_EXPORT_SYMBOL
struct evbuffer *evhttp_request_get_input_buffer(struct evhttp_request *req);
/** Returns the output buffer */
+EVENT2_EXPORT_SYMBOL
struct evbuffer *evhttp_request_get_output_buffer(struct evhttp_request *req);
/** Returns the host associated with the request. If a client sends an absolute
URI, the host part of that is preferred. Otherwise, the input headers are
searched for a Host: header. NULL is returned if no absolute URI or Host:
header is provided. */
+EVENT2_EXPORT_SYMBOL
const char *evhttp_request_get_host(struct evhttp_request *req);
/* Interfaces for dealing with HTTP headers */
@@ -558,9 +818,10 @@ const char *evhttp_request_get_host(struct evhttp_request *req);
@param headers the evkeyvalq object in which to find the header
@param key the name of the header to find
@returns a pointer to the value for the header or NULL if the header
- count not be found.
+ could not be found.
@see evhttp_add_header(), evhttp_remove_header()
*/
+EVENT2_EXPORT_SYMBOL
const char *evhttp_find_header(const struct evkeyvalq *headers,
const char *key);
@@ -572,6 +833,7 @@ const char *evhttp_find_header(const struct evkeyvalq *headers,
@returns 0 if the header was removed, -1 otherwise.
@see evhttp_find_header(), evhttp_add_header()
*/
+EVENT2_EXPORT_SYMBOL
int evhttp_remove_header(struct evkeyvalq *headers, const char *key);
/**
@@ -583,6 +845,7 @@ int evhttp_remove_header(struct evkeyvalq *headers, const char *key);
@returns 0 on success, -1 otherwise.
@see evhttp_find_header(), evhttp_clear_headers()
*/
+EVENT2_EXPORT_SYMBOL
int evhttp_add_header(struct evkeyvalq *headers, const char *key, const char *value);
/**
@@ -590,6 +853,7 @@ int evhttp_add_header(struct evkeyvalq *headers, const char *key, const char *va
@param headers the evkeyvalq object from which to remove all headers
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_clear_headers(struct evkeyvalq *headers);
/* Miscellaneous utility functions */
@@ -606,6 +870,7 @@ void evhttp_clear_headers(struct evkeyvalq *headers);
@param str an unencoded string
@return a newly allocated URI-encoded string or NULL on failure
*/
+EVENT2_EXPORT_SYMBOL
char *evhttp_encode_uri(const char *str);
/**
@@ -622,6 +887,7 @@ char *evhttp_encode_uri(const char *str);
as +, not %20.
@return a newly allocate URI-encoded string, or NULL on failure.
*/
+EVENT2_EXPORT_SYMBOL
char *evhttp_uriencode(const char *str, ev_ssize_t size, int space_to_plus);
/**
@@ -638,6 +904,7 @@ char *evhttp_uriencode(const char *str, ev_ssize_t size, int space_to_plus);
@param uri an encoded URI
@return a newly allocated unencoded URI or NULL on failure
*/
+EVENT2_EXPORT_SYMBOL
char *evhttp_decode_uri(const char *uri);
/**
@@ -650,11 +917,12 @@ char *evhttp_decode_uri(const char *uri);
The returned string must be freed by the caller.
@param uri a URI-encode encoded URI
- @param decode_plus determines whether we convert '+' to sapce.
+ @param decode_plus determines whether we convert '+' to space.
@param size_out if size_out is not NULL, *size_out is set to the size of the
returned string
@return a newly allocated unencoded URI or NULL on failure
*/
+EVENT2_EXPORT_SYMBOL
char *evhttp_uridecode(const char *uri, int decode_plus,
size_t *size_out);
@@ -677,6 +945,7 @@ char *evhttp_uridecode(const char *uri, int decode_plus,
@param headers the head of the evkeyval queue
@return 0 on success, -1 on failure
*/
+EVENT2_EXPORT_SYMBOL
int evhttp_parse_query(const char *uri, struct evkeyvalq *headers);
/**
@@ -696,6 +965,7 @@ int evhttp_parse_query(const char *uri, struct evkeyvalq *headers);
@param headers the head of the evkeyval queue
@return 0 on success, -1 on failure
*/
+EVENT2_EXPORT_SYMBOL
int evhttp_parse_query_str(const char *uri, struct evkeyvalq *headers);
/**
@@ -709,26 +979,31 @@ int evhttp_parse_query_str(const char *uri, struct evkeyvalq *headers);
* @param html an unescaped HTML string
* @return an escaped HTML string or NULL on error
*/
+EVENT2_EXPORT_SYMBOL
char *evhttp_htmlescape(const char *html);
/**
* Return a new empty evhttp_uri with no fields set.
*/
+EVENT2_EXPORT_SYMBOL
struct evhttp_uri *evhttp_uri_new(void);
/**
* Changes the flags set on a given URI. See EVHTTP_URI_* for
* a list of flags.
**/
+EVENT2_EXPORT_SYMBOL
void evhttp_uri_set_flags(struct evhttp_uri *uri, unsigned flags);
/** Return the scheme of an evhttp_uri, or NULL if there is no scheme has
* been set and the evhttp_uri contains a Relative-Ref. */
+EVENT2_EXPORT_SYMBOL
const char *evhttp_uri_get_scheme(const struct evhttp_uri *uri);
/**
* Return the userinfo part of an evhttp_uri, or NULL if it has no userinfo
* set.
*/
+EVENT2_EXPORT_SYMBOL
const char *evhttp_uri_get_userinfo(const struct evhttp_uri *uri);
/**
* Return the host part of an evhttp_uri, or NULL if it has no host set.
@@ -742,40 +1017,52 @@ const char *evhttp_uri_get_userinfo(const struct evhttp_uri *uri);
* "mailto:user@example.com" has a host of NULL, but "file:///etc/motd"
* has a host of "".
*/
+EVENT2_EXPORT_SYMBOL
const char *evhttp_uri_get_host(const struct evhttp_uri *uri);
/** Return the port part of an evhttp_uri, or -1 if there is no port set. */
+EVENT2_EXPORT_SYMBOL
int evhttp_uri_get_port(const struct evhttp_uri *uri);
/** Return the path part of an evhttp_uri, or NULL if it has no path set */
+EVENT2_EXPORT_SYMBOL
const char *evhttp_uri_get_path(const struct evhttp_uri *uri);
/** Return the query part of an evhttp_uri (excluding the leading "?"), or
* NULL if it has no query set */
+EVENT2_EXPORT_SYMBOL
const char *evhttp_uri_get_query(const struct evhttp_uri *uri);
/** Return the fragment part of an evhttp_uri (excluding the leading "#"),
* or NULL if it has no fragment set */
+EVENT2_EXPORT_SYMBOL
const char *evhttp_uri_get_fragment(const struct evhttp_uri *uri);
/** Set the scheme of an evhttp_uri, or clear the scheme if scheme==NULL.
* Returns 0 on success, -1 if scheme is not well-formed. */
+EVENT2_EXPORT_SYMBOL
int evhttp_uri_set_scheme(struct evhttp_uri *uri, const char *scheme);
/** Set the userinfo of an evhttp_uri, or clear the userinfo if userinfo==NULL.
* Returns 0 on success, -1 if userinfo is not well-formed. */
+EVENT2_EXPORT_SYMBOL
int evhttp_uri_set_userinfo(struct evhttp_uri *uri, const char *userinfo);
/** Set the host of an evhttp_uri, or clear the host if host==NULL.
* Returns 0 on success, -1 if host is not well-formed. */
+EVENT2_EXPORT_SYMBOL
int evhttp_uri_set_host(struct evhttp_uri *uri, const char *host);
/** Set the port of an evhttp_uri, or clear the port if port==-1.
* Returns 0 on success, -1 if port is not well-formed. */
+EVENT2_EXPORT_SYMBOL
int evhttp_uri_set_port(struct evhttp_uri *uri, int port);
/** Set the path of an evhttp_uri, or clear the path if path==NULL.
* Returns 0 on success, -1 if path is not well-formed. */
+EVENT2_EXPORT_SYMBOL
int evhttp_uri_set_path(struct evhttp_uri *uri, const char *path);
/** Set the query of an evhttp_uri, or clear the query if query==NULL.
* The query should not include a leading "?".
* Returns 0 on success, -1 if query is not well-formed. */
+EVENT2_EXPORT_SYMBOL
int evhttp_uri_set_query(struct evhttp_uri *uri, const char *query);
/** Set the fragment of an evhttp_uri, or clear the fragment if fragment==NULL.
* The fragment should not include a leading "#".
* Returns 0 on success, -1 if fragment is not well-formed. */
+EVENT2_EXPORT_SYMBOL
int evhttp_uri_set_fragment(struct evhttp_uri *uri, const char *fragment);
/**
@@ -812,6 +1099,7 @@ int evhttp_uri_set_fragment(struct evhttp_uri *uri, const char *fragment);
* @return uri container to hold parsed data, or NULL if there is error
* @see evhttp_uri_free()
*/
+EVENT2_EXPORT_SYMBOL
struct evhttp_uri *evhttp_uri_parse_with_flags(const char *source_uri,
unsigned flags);
@@ -830,6 +1118,7 @@ struct evhttp_uri *evhttp_uri_parse_with_flags(const char *source_uri,
#define EVHTTP_URI_NONCONFORMANT 0x01
/** Alias for evhttp_uri_parse_with_flags(source_uri, 0) */
+EVENT2_EXPORT_SYMBOL
struct evhttp_uri *evhttp_uri_parse(const char *source_uri);
/**
@@ -839,6 +1128,7 @@ struct evhttp_uri *evhttp_uri_parse(const char *source_uri);
* @param uri container with parsed data
* @see evhttp_uri_parse()
*/
+EVENT2_EXPORT_SYMBOL
void evhttp_uri_free(struct evhttp_uri *uri);
/**
@@ -854,10 +1144,11 @@ void evhttp_uri_free(struct evhttp_uri *uri);
* @return an joined uri as string or NULL on error
* @see evhttp_uri_parse()
*/
+EVENT2_EXPORT_SYMBOL
char *evhttp_uri_join(struct evhttp_uri *uri, char *buf, size_t limit);
#ifdef __cplusplus
}
#endif
-#endif /* _EVENT2_HTTP_H_ */
+#endif /* EVENT2_HTTP_H_INCLUDED_ */
diff --git a/include/event2/http_compat.h b/include/event2/http_compat.h
index 9e902a4b..0d9af17f 100644
--- a/include/event2/http_compat.h
+++ b/include/event2/http_compat.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_HTTP_COMPAT_H_
-#define _EVENT2_HTTP_COMPAT_H_
+#ifndef EVENT2_HTTP_COMPAT_H_INCLUDED_
+#define EVENT2_HTTP_COMPAT_H_INCLUDED_
/** @file event2/http_compat.h
@@ -39,10 +39,10 @@ extern "C" {
#endif
#include <event2/event-config.h>
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -87,4 +87,4 @@ void evhttp_connection_set_base(struct evhttp_connection *evcon,
}
#endif
-#endif /* _EVENT2_EVENT_COMPAT_H_ */
+#endif /* EVENT2_EVENT_COMPAT_H_INCLUDED_ */
diff --git a/include/event2/http_struct.h b/include/event2/http_struct.h
index 41a92a9f..4bf5b1ff 100644
--- a/include/event2/http_struct.h
+++ b/include/event2/http_struct.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_HTTP_STRUCT_H_
-#define _EVENT2_HTTP_STRUCT_H_
+#ifndef EVENT2_HTTP_STRUCT_H_INCLUDED_
+#define EVENT2_HTTP_STRUCT_H_INCLUDED_
/** @file event2/http_struct.h
@@ -39,10 +39,10 @@ extern "C" {
#endif
#include <event2/event-config.h>
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -120,11 +120,33 @@ struct {
* the regular callback.
*/
void (*chunk_cb)(struct evhttp_request *, void *);
+
+ /*
+ * Callback added for forked-daapd so they can collect ICY
+ * (shoutcast) metadata from the http header. If return
+ * int is negative the connection will be closed.
+ */
+ int (*header_cb)(struct evhttp_request *, void *);
+
+ /*
+ * Error callback - called when error is occured.
+ * @see evhttp_request_error for error types.
+ *
+ * @see evhttp_request_set_error_cb()
+ */
+ void (*error_cb)(enum evhttp_request_error, void *);
+
+ /*
+ * Send complete callback - called when the request is actually
+ * sent and completed.
+ */
+ void (*on_complete_cb)(struct evhttp_request *, void *);
+ void *on_complete_cb_arg;
};
#ifdef __cplusplus
}
#endif
-#endif /* _EVENT2_HTTP_STRUCT_H_ */
+#endif /* EVENT2_HTTP_STRUCT_H_INCLUDED_ */
diff --git a/include/event2/keyvalq_struct.h b/include/event2/keyvalq_struct.h
index ab8040bf..bffa54b3 100644
--- a/include/event2/keyvalq_struct.h
+++ b/include/event2/keyvalq_struct.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_EVENT_KEYVALQ_STRUCT_H_
-#define _EVENT2_EVENT_KEYVALQ_STRUCT_H_
+#ifndef EVENT2_KEYVALQ_STRUCT_H_INCLUDED_
+#define EVENT2_KEYVALQ_STRUCT_H_INCLUDED_
#ifdef __cplusplus
extern "C" {
@@ -34,7 +34,7 @@ extern "C" {
/* Fix so that people don't have to run with <sys/queue.h> */
/* XXXX This code is duplicated with event_struct.h */
#ifndef TAILQ_ENTRY
-#define _EVENT_DEFINED_TQENTRY
+#define EVENT_DEFINED_TQENTRY_
#define TAILQ_ENTRY(type) \
struct { \
struct type *tqe_next; /* next element */ \
@@ -43,7 +43,7 @@ struct { \
#endif /* !TAILQ_ENTRY */
#ifndef TAILQ_HEAD
-#define _EVENT_DEFINED_TQHEAD
+#define EVENT_DEFINED_TQHEAD_
#define TAILQ_HEAD(name, type) \
struct name { \
struct type *tqh_first; \
@@ -65,11 +65,11 @@ struct evkeyval {
TAILQ_HEAD (evkeyvalq, evkeyval);
/* XXXX This code is duplicated with event_struct.h */
-#ifdef _EVENT_DEFINED_TQENTRY
+#ifdef EVENT_DEFINED_TQENTRY_
#undef TAILQ_ENTRY
#endif
-#ifdef _EVENT_DEFINED_TQHEAD
+#ifdef EVENT_DEFINED_TQHEAD_
#undef TAILQ_HEAD
#endif
diff --git a/include/event2/listener.h b/include/event2/listener.h
index 2dc64be4..84b4da05 100644
--- a/include/event2/listener.h
+++ b/include/event2/listener.h
@@ -24,8 +24,10 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_LISTENER_H_
-#define _EVENT2_LISTENER_H_
+#ifndef EVENT2_LISTENER_H_INCLUDED_
+#define EVENT2_LISTENER_H_INCLUDED_
+
+#include <event2/visibility.h>
#ifdef __cplusplus
extern "C" {
@@ -69,6 +71,32 @@ typedef void (*evconnlistener_errorcb)(struct evconnlistener *, void *);
/** Flag: Indicates that the listener should be locked so it's safe to use
* from multiple threadcs at once. */
#define LEV_OPT_THREADSAFE (1u<<4)
+/** Flag: Indicates that the listener should be created in disabled
+ * state. Use evconnlistener_enable() to enable it later. */
+#define LEV_OPT_DISABLED (1u<<5)
+/** Flag: Indicates that the listener should defer accept() until data is
+ * available, if possible. Ignored on platforms that do not support this.
+ *
+ * This option can help performance for protocols where the client transmits
+ * immediately after connecting. Do not use this option if your protocol
+ * _doesn't_ start out with the client transmitting data, since in that case
+ * this option will sometimes cause the kernel to never tell you about the
+ * connection.
+ *
+ * This option is only supported by evconnlistener_new_bind(): it can't
+ * work with evconnlistener_new_fd(), since the listener needs to be told
+ * to use the option before it is actually bound.
+ */
+#define LEV_OPT_DEFERRED_ACCEPT (1u<<6)
+/** Flag: Indicates that we ask to allow multiple servers (processes or
+ * threads) to bind to the same port if they each set the option.
+ *
+ * SO_REUSEPORT is what most people would expect SO_REUSEADDR to be, however
+ * SO_REUSEPORT does not imply SO_REUSEADDR.
+ *
+ * This is only available on Linux and kernel 3.9+
+ */
+#define LEV_OPT_REUSEABLE_PORT (1u<<7)
/**
Allocate a new evconnlistener object to listen for incoming TCP connections
@@ -87,6 +115,7 @@ typedef void (*evconnlistener_errorcb)(struct evconnlistener *, void *);
file descriptor, and it should already be bound to an appropriate
port and address.
*/
+EVENT2_EXPORT_SYMBOL
struct evconnlistener *evconnlistener_new(struct event_base *base,
evconnlistener_cb cb, void *ptr, unsigned flags, int backlog,
evutil_socket_t fd);
@@ -105,34 +134,42 @@ struct evconnlistener *evconnlistener_new(struct event_base *base,
@param addr The address to listen for connections on.
@param socklen The length of the address.
*/
+EVENT2_EXPORT_SYMBOL
struct evconnlistener *evconnlistener_new_bind(struct event_base *base,
evconnlistener_cb cb, void *ptr, unsigned flags, int backlog,
const struct sockaddr *sa, int socklen);
/**
Disable and deallocate an evconnlistener.
*/
+EVENT2_EXPORT_SYMBOL
void evconnlistener_free(struct evconnlistener *lev);
/**
Re-enable an evconnlistener that has been disabled.
*/
+EVENT2_EXPORT_SYMBOL
int evconnlistener_enable(struct evconnlistener *lev);
/**
Stop listening for connections on an evconnlistener.
*/
+EVENT2_EXPORT_SYMBOL
int evconnlistener_disable(struct evconnlistener *lev);
/** Return an evconnlistener's associated event_base. */
+EVENT2_EXPORT_SYMBOL
struct event_base *evconnlistener_get_base(struct evconnlistener *lev);
/** Return the socket that an evconnlistner is listening on. */
+EVENT2_EXPORT_SYMBOL
evutil_socket_t evconnlistener_get_fd(struct evconnlistener *lev);
/** Change the callback on the listener to cb and its user_data to arg.
*/
+EVENT2_EXPORT_SYMBOL
void evconnlistener_set_cb(struct evconnlistener *lev,
evconnlistener_cb cb, void *arg);
/** Set an evconnlistener's error callback. */
+EVENT2_EXPORT_SYMBOL
void evconnlistener_set_error_cb(struct evconnlistener *lev,
evconnlistener_errorcb errorcb);
diff --git a/include/event2/rpc.h b/include/event2/rpc.h
index 5272edc2..dd43df26 100644
--- a/include/event2/rpc.h
+++ b/include/event2/rpc.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_RPC_H_
-#define _EVENT2_RPC_H_
+#ifndef EVENT2_RPC_H_INCLUDED_
+#define EVENT2_RPC_H_INCLUDED_
#ifdef __cplusplus
extern "C" {
@@ -76,7 +76,7 @@ extern "C" {
#define EVTAG_HAS(msg, member) \
((msg)->member##_set == 1)
-#ifndef _EVENT2_RPC_COMPAT_H_
+#ifndef EVENT2_RPC_COMPAT_H_INCLUDED_
/**
Assigns a value to the member in the message.
@@ -119,7 +119,7 @@ extern "C" {
#define EVTAG_GET_WITH_LEN(msg, member, pvalue, plen) \
(*(msg)->base->member##_get)((msg), (pvalue), (plen))
-#endif /* _EVENT2_RPC_COMPAT_H_ */
+#endif /* EVENT2_RPC_COMPAT_H_INCLUDED_ */
/**
Adds a value to an array.
@@ -272,8 +272,8 @@ void *evrpc_get_reply(struct evrpc_req_generic *req);
* @param rpc_req the rpc request structure provided to the server callback
*/
#define EVRPC_REQUEST_DONE(rpc_req) do { \
- struct evrpc_req_generic *_req = (struct evrpc_req_generic *)(rpc_req); \
- evrpc_request_done(_req); \
+ struct evrpc_req_generic *req_ = (struct evrpc_req_generic *)(rpc_req); \
+ evrpc_request_done(req_); \
} while (0)
@@ -454,7 +454,7 @@ enum EVRPC_HOOK_TYPE {
EVRPC_OUTPUT /**< apply the function to an output hook */
};
-#ifndef WIN32
+#ifndef _WIN32
/** Deprecated alias for EVRPC_INPUT. Not available on windows, where it
* conflicts with platform headers. */
#define INPUT EVRPC_INPUT
@@ -593,4 +593,4 @@ void evrpc_request_set_cb(struct evrpc_request_wrapper *ctx,
}
#endif
-#endif /* _EVENT2_RPC_H_ */
+#endif /* EVENT2_RPC_H_INCLUDED_ */
diff --git a/include/event2/rpc_compat.h b/include/event2/rpc_compat.h
index a4e24bb6..8d8334d2 100644
--- a/include/event2/rpc_compat.h
+++ b/include/event2/rpc_compat.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_RPC_COMPAT_H_
-#define _EVENT2_RPC_COMPAT_H_
+#ifndef EVENT2_RPC_COMPAT_H_INCLUDED_
+#define EVENT2_RPC_COMPAT_H_INCLUDED_
/** @file event2/rpc_compat.h
@@ -58,4 +58,4 @@ extern "C" {
}
#endif
-#endif /* _EVENT2_EVENT_COMPAT_H_ */
+#endif /* EVENT2_EVENT_COMPAT_H_INCLUDED_ */
diff --git a/include/event2/rpc_struct.h b/include/event2/rpc_struct.h
index c339ae58..8f691f49 100644
--- a/include/event2/rpc_struct.h
+++ b/include/event2/rpc_struct.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_RPC_STRUCT_H_
-#define _EVENT2_RPC_STRUCT_H_
+#ifndef EVENT2_RPC_STRUCT_H_INCLUDED_
+#define EVENT2_RPC_STRUCT_H_INCLUDED_
#ifdef __cplusplus
extern "C" {
@@ -97,4 +97,4 @@ struct evrpc {
}
#endif
-#endif /* _EVENT2_RPC_STRUCT_H_ */
+#endif /* EVENT2_RPC_STRUCT_H_INCLUDED_ */
diff --git a/include/event2/tag.h b/include/event2/tag.h
index 9e51779e..2f73bfc0 100644
--- a/include/event2/tag.h
+++ b/include/event2/tag.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_TAG_H_
-#define _EVENT2_TAG_H_
+#ifndef EVENT2_TAG_H_INCLUDED_
+#define EVENT2_TAG_H_INCLUDED_
/** @file event2/tag.h
@@ -33,15 +33,17 @@
*/
+#include <event2/visibility.h>
+
#ifdef __cplusplus
extern "C" {
#endif
#include <event2/event-config.h>
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
@@ -56,6 +58,7 @@ struct evbuffer;
* known ones - and we can just ignore the end of an event buffer.
*/
+EVENT2_EXPORT_SYMBOL
void evtag_init(void);
/**
@@ -65,10 +68,13 @@ void evtag_init(void);
@param ptag a pointer in which the tag id is being stored
@returns -1 on failure or the number of bytes in the remaining payload.
*/
+EVENT2_EXPORT_SYMBOL
int evtag_unmarshal_header(struct evbuffer *evbuf, ev_uint32_t *ptag);
+EVENT2_EXPORT_SYMBOL
void evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag, const void *data,
ev_uint32_t len);
+EVENT2_EXPORT_SYMBOL
void evtag_marshal_buffer(struct evbuffer *evbuf, ev_uint32_t tag,
struct evbuffer *data);
@@ -82,38 +88,54 @@ void evtag_marshal_buffer(struct evbuffer *evbuf, ev_uint32_t tag,
@param evbuf evbuffer to store the encoded number
@param number a 32-bit integer
*/
+EVENT2_EXPORT_SYMBOL
void evtag_encode_int(struct evbuffer *evbuf, ev_uint32_t number);
+EVENT2_EXPORT_SYMBOL
void evtag_encode_int64(struct evbuffer *evbuf, ev_uint64_t number);
+EVENT2_EXPORT_SYMBOL
void evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag,
ev_uint32_t integer);
+EVENT2_EXPORT_SYMBOL
void evtag_marshal_int64(struct evbuffer *evbuf, ev_uint32_t tag,
ev_uint64_t integer);
+EVENT2_EXPORT_SYMBOL
void evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag,
const char *string);
+EVENT2_EXPORT_SYMBOL
void evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag,
struct timeval *tv);
+EVENT2_EXPORT_SYMBOL
int evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag,
struct evbuffer *dst);
+EVENT2_EXPORT_SYMBOL
int evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag);
+EVENT2_EXPORT_SYMBOL
int evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength);
+EVENT2_EXPORT_SYMBOL
int evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength);
+EVENT2_EXPORT_SYMBOL
int evtag_consume(struct evbuffer *evbuf);
+EVENT2_EXPORT_SYMBOL
int evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag,
ev_uint32_t *pinteger);
+EVENT2_EXPORT_SYMBOL
int evtag_unmarshal_int64(struct evbuffer *evbuf, ev_uint32_t need_tag,
ev_uint64_t *pinteger);
+EVENT2_EXPORT_SYMBOL
int evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag,
void *data, size_t len);
+EVENT2_EXPORT_SYMBOL
int evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag,
char **pstring);
+EVENT2_EXPORT_SYMBOL
int evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag,
struct timeval *ptv);
@@ -121,4 +143,4 @@ int evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag,
}
#endif
-#endif /* _EVENT2_TAG_H_ */
+#endif /* EVENT2_TAG_H_INCLUDED_ */
diff --git a/include/event2/tag_compat.h b/include/event2/tag_compat.h
index 73c19330..a276c0d3 100644
--- a/include/event2/tag_compat.h
+++ b/include/event2/tag_compat.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_TAG_COMPAT_H_
-#define _EVENT2_TAG_COMPAT_H_
+#ifndef EVENT2_TAG_COMPAT_H_INCLUDED_
+#define EVENT2_TAG_COMPAT_H_INCLUDED_
/** @file event2/tag_compat.h
@@ -46,4 +46,4 @@
#define encode_int64(evbuf, number) evtag_encode_int64((evbuf), (number))
/**@}*/
-#endif /* _EVENT2_TAG_H_ */
+#endif /* EVENT2_TAG_H_INCLUDED_ */
diff --git a/include/event2/thread.h b/include/event2/thread.h
index 0b638f0f..b5199863 100644
--- a/include/event2/thread.h
+++ b/include/event2/thread.h
@@ -23,8 +23,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_THREAD_H_
-#define _EVENT2_THREAD_H_
+#ifndef EVENT2_THREAD_H_INCLUDED_
+#define EVENT2_THREAD_H_INCLUDED_
/** @file event2/thread.h
@@ -46,6 +46,8 @@
*/
+#include <event2/visibility.h>
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -69,7 +71,7 @@ extern "C" {
#define EVTHREAD_TRY 0x10
/**@}*/
-#if !defined(_EVENT_DISABLE_THREAD_SUPPORT) || defined(_EVENT_IN_DOXYGEN)
+#if !defined(EVENT__DISABLE_THREAD_SUPPORT) || defined(EVENT_IN_DOXYGEN_)
#define EVTHREAD_LOCK_API_VERSION 1
@@ -124,6 +126,7 @@ struct evthread_lock_callbacks {
* probably shouldn't call this function; instead, use
* evthread_use_windows_threads() or evthread_use_posix_threads() if you can.
*/
+EVENT2_EXPORT_SYMBOL
int evthread_set_lock_callbacks(const struct evthread_lock_callbacks *);
#define EVTHREAD_CONDITION_API_VERSION 1
@@ -174,6 +177,7 @@ struct evthread_condition_callbacks {
* probably shouldn't call this function; instead, use
* evthread_use_windows_threads() or evthread_use_pthreads() if you can.
*/
+EVENT2_EXPORT_SYMBOL
int evthread_set_condition_callbacks(
const struct evthread_condition_callbacks *);
@@ -184,14 +188,16 @@ int evthread_set_condition_callbacks(
@param id_fn the identify function Libevent should invoke to
determine the identity of a thread.
*/
+EVENT2_EXPORT_SYMBOL
void evthread_set_id_callback(
unsigned long (*id_fn)(void));
-#if (defined(WIN32) && !defined(_EVENT_DISABLE_THREAD_SUPPORT)) || defined(_EVENT_IN_DOXYGEN)
+#if (defined(_WIN32) && !defined(EVENT__DISABLE_THREAD_SUPPORT)) || defined(EVENT_IN_DOXYGEN_)
/** Sets up Libevent for use with Windows builtin locking and thread ID
functions. Unavailable if Libevent is not built for Windows.
@return 0 on success, -1 on failure. */
+EVENT2_EXPORT_SYMBOL
int evthread_use_windows_threads(void);
/**
Defined if Libevent was built with support for evthread_use_windows_threads()
@@ -199,12 +205,13 @@ int evthread_use_windows_threads(void);
#define EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED 1
#endif
-#if defined(_EVENT_HAVE_PTHREADS) || defined(_EVENT_IN_DOXYGEN)
+#if defined(EVENT__HAVE_PTHREADS) || defined(EVENT_IN_DOXYGEN_)
/** Sets up Libevent for use with Pthreads locking and thread ID functions.
Unavailable if Libevent is not build for use with pthreads. Requires
libraries to link against Libevent_pthreads as well as Libevent.
@return 0 on success, -1 on failure. */
+EVENT2_EXPORT_SYMBOL
int evthread_use_pthreads(void);
/** Defined if Libevent was built with support for evthread_use_pthreads() */
#define EVTHREAD_USE_PTHREADS_IMPLEMENTED 1
@@ -217,20 +224,30 @@ int evthread_use_pthreads(void);
* If you're going to call this function, you must do so before any locks are
* allocated.
**/
+EVENT2_EXPORT_SYMBOL
+void evthread_enable_lock_debugging(void);
+
+/* Old (misspelled) version: This is deprecated; use
+ * evthread_enable_log_debugging instead. */
+EVENT2_EXPORT_SYMBOL
void evthread_enable_lock_debuging(void);
-#endif /* _EVENT_DISABLE_THREAD_SUPPORT */
+#endif /* EVENT__DISABLE_THREAD_SUPPORT */
struct event_base;
/** Make sure it's safe to tell an event base to wake up from another thread
or a signal handler.
+ You shouldn't need to call this by hand; configuring the base with thread
+ support should be necessary and sufficient.
+
@return 0 on success, -1 on failure.
*/
+EVENT2_EXPORT_SYMBOL
int evthread_make_base_notifiable(struct event_base *base);
#ifdef __cplusplus
}
#endif
-#endif /* _EVENT2_THREAD_H_ */
+#endif /* EVENT2_THREAD_H_INCLUDED_ */
diff --git a/include/event2/util.h b/include/event2/util.h
index dde91131..62b94773 100644
--- a/include/event2/util.h
+++ b/include/event2/util.h
@@ -23,8 +23,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT2_UTIL_H_
-#define _EVENT2_UTIL_H_
+#ifndef EVENT2_UTIL_H_INCLUDED_
+#define EVENT2_UTIL_H_INCLUDED_
/** @file event2/util.h
@@ -32,46 +32,51 @@
related socket manipulations.
*/
+#include <event2/visibility.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <event2/event-config.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
-#ifdef _EVENT_HAVE_STDINT_H
+#ifdef EVENT__HAVE_STDINT_H
#include <stdint.h>
-#elif defined(_EVENT_HAVE_INTTYPES_H)
+#elif defined(EVENT__HAVE_INTTYPES_H)
#include <inttypes.h>
#endif
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
-#ifdef _EVENT_HAVE_STDDEF_H
+#ifdef EVENT__HAVE_STDDEF_H
#include <stddef.h>
#endif
#ifdef _MSC_VER
#include <BaseTsd.h>
#endif
#include <stdarg.h>
-#ifdef _EVENT_HAVE_NETDB_H
+#ifdef EVENT__HAVE_NETDB_H
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include <netdb.h>
#endif
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
+#ifdef EVENT__HAVE_GETADDRINFO
+/* for EAI_* definitions. */
+#include <ws2tcpip.h>
+#endif
#else
#include <sys/socket.h>
#endif
/* Some openbsd autoconf versions get the name of this macro wrong. */
-#if defined(_EVENT_SIZEOF_VOID__) && !defined(_EVENT_SIZEOF_VOID_P)
-#define _EVENT_SIZEOF_VOID_P _EVENT_SIZEOF_VOID__
+#if defined(EVENT__SIZEOF_VOID__) && !defined(EVENT__SIZEOF_VOID_P)
+#define EVENT__SIZEOF_VOID_P EVENT__SIZEOF_VOID__
#endif
/**
@@ -103,67 +108,67 @@ extern "C" {
*
* @{
*/
-#ifdef _EVENT_HAVE_UINT64_T
+#ifdef EVENT__HAVE_UINT64_T
#define ev_uint64_t uint64_t
#define ev_int64_t int64_t
-#elif defined(WIN32)
+#elif defined(_WIN32)
#define ev_uint64_t unsigned __int64
#define ev_int64_t signed __int64
-#elif _EVENT_SIZEOF_LONG_LONG == 8
+#elif EVENT__SIZEOF_LONG_LONG == 8
#define ev_uint64_t unsigned long long
#define ev_int64_t long long
-#elif _EVENT_SIZEOF_LONG == 8
+#elif EVENT__SIZEOF_LONG == 8
#define ev_uint64_t unsigned long
#define ev_int64_t long
-#elif defined(_EVENT_IN_DOXYGEN)
+#elif defined(EVENT_IN_DOXYGEN_)
#define ev_uint64_t ...
#define ev_int64_t ...
#else
#error "No way to define ev_uint64_t"
#endif
-#ifdef _EVENT_HAVE_UINT32_T
+#ifdef EVENT__HAVE_UINT32_T
#define ev_uint32_t uint32_t
#define ev_int32_t int32_t
-#elif defined(WIN32)
+#elif defined(_WIN32)
#define ev_uint32_t unsigned int
#define ev_int32_t signed int
-#elif _EVENT_SIZEOF_LONG == 4
+#elif EVENT__SIZEOF_LONG == 4
#define ev_uint32_t unsigned long
#define ev_int32_t signed long
-#elif _EVENT_SIZEOF_INT == 4
+#elif EVENT__SIZEOF_INT == 4
#define ev_uint32_t unsigned int
#define ev_int32_t signed int
-#elif defined(_EVENT_IN_DOXYGEN)
+#elif defined(EVENT_IN_DOXYGEN_)
#define ev_uint32_t ...
#define ev_int32_t ...
#else
#error "No way to define ev_uint32_t"
#endif
-#ifdef _EVENT_HAVE_UINT16_T
+#ifdef EVENT__HAVE_UINT16_T
#define ev_uint16_t uint16_t
#define ev_int16_t int16_t
-#elif defined(WIN32)
+#elif defined(_WIN32)
#define ev_uint16_t unsigned short
#define ev_int16_t signed short
-#elif _EVENT_SIZEOF_INT == 2
+#elif EVENT__SIZEOF_INT == 2
#define ev_uint16_t unsigned int
#define ev_int16_t signed int
-#elif _EVENT_SIZEOF_SHORT == 2
+#elif EVENT__SIZEOF_SHORT == 2
#define ev_uint16_t unsigned short
#define ev_int16_t signed short
-#elif defined(_EVENT_IN_DOXYGEN)
+#elif defined(EVENT_IN_DOXYGEN_)
#define ev_uint16_t ...
#define ev_int16_t ...
#else
#error "No way to define ev_uint16_t"
#endif
-#ifdef _EVENT_HAVE_UINT8_T
+#ifdef EVENT__HAVE_UINT8_T
#define ev_uint8_t uint8_t
#define ev_int8_t int8_t
-#elif defined(_EVENT_IN_DOXYGEN)
+#elif defined(EVENT_IN_DOXYGEN_)
#define ev_uint8_t ...
#define ev_int8_t ...
#else
@@ -171,30 +176,43 @@ extern "C" {
#define ev_int8_t signed char
#endif
-#ifdef _EVENT_HAVE_UINTPTR_T
+#ifdef EVENT__HAVE_UINTPTR_T
#define ev_uintptr_t uintptr_t
#define ev_intptr_t intptr_t
-#elif _EVENT_SIZEOF_VOID_P <= 4
+#elif EVENT__SIZEOF_VOID_P <= 4
#define ev_uintptr_t ev_uint32_t
#define ev_intptr_t ev_int32_t
-#elif _EVENT_SIZEOF_VOID_P <= 8
+#elif EVENT__SIZEOF_VOID_P <= 8
#define ev_uintptr_t ev_uint64_t
#define ev_intptr_t ev_int64_t
-#elif defined(_EVENT_IN_DOXYGEN)
+#elif defined(EVENT_IN_DOXYGEN_)
#define ev_uintptr_t ...
#define ev_intptr_t ...
#else
#error "No way to define ev_uintptr_t"
#endif
-#ifdef _EVENT_ssize_t
-#define ev_ssize_t _EVENT_ssize_t
+#ifdef EVENT__ssize_t
+#define ev_ssize_t EVENT__ssize_t
#else
#define ev_ssize_t ssize_t
#endif
-#ifdef WIN32
+/* Note that we define ev_off_t based on the compile-time size of off_t that
+ * we used to build Libevent, and not based on the current size of off_t.
+ * (For example, we don't define ev_off_t to off_t.). We do this because
+ * some systems let you build your software with different off_t sizes
+ * at runtime, and so putting in any dependency on off_t would risk API
+ * mismatch.
+ */
+#ifdef _WIN32
+#define ev_off_t ev_int64_t
+#elif EVENT__SIZEOF_OFF_T == 8
#define ev_off_t ev_int64_t
+#elif EVENT__SIZEOF_OFF_T == 4
+#define ev_off_t ev_int32_t
+#elif defined(EVENT_IN_DOXYGEN_)
+#define ev_off_t ...
#else
#define ev_off_t off_t
#endif
@@ -234,13 +252,13 @@ extern "C" {
@{
*/
-#if _EVENT_SIZEOF_SIZE_T == 8
+#if EVENT__SIZEOF_SIZE_T == 8
#define EV_SIZE_MAX EV_UINT64_MAX
#define EV_SSIZE_MAX EV_INT64_MAX
-#elif _EVENT_SIZEOF_SIZE_T == 4
+#elif EVENT__SIZEOF_SIZE_T == 4
#define EV_SIZE_MAX EV_UINT32_MAX
#define EV_SSIZE_MAX EV_INT32_MAX
-#elif defined(_EVENT_IN_DOXYGEN)
+#elif defined(EVENT_IN_DOXYGEN_)
#define EV_SIZE_MAX ...
#define EV_SSIZE_MAX ...
#else
@@ -250,16 +268,16 @@ extern "C" {
#define EV_SSIZE_MIN ((-EV_SSIZE_MAX) - 1)
/**@}*/
-#ifdef WIN32
+#ifdef _WIN32
#define ev_socklen_t int
-#elif defined(_EVENT_socklen_t)
-#define ev_socklen_t _EVENT_socklen_t
+#elif defined(EVENT__socklen_t)
+#define ev_socklen_t EVENT__socklen_t
#else
#define ev_socklen_t socklen_t
#endif
-#ifdef _EVENT_HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
-#if !defined(_EVENT_HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY) \
+#ifdef EVENT__HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
+#if !defined(EVENT__HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY) \
&& !defined(ss_family)
#define ss_family __ss_family
#endif
@@ -268,7 +286,7 @@ extern "C" {
/**
* A type wide enough to hold the output of "socket()" or "accept()". On
* Windows, this is an intptr_t; elsewhere, it is an int. */
-#ifdef WIN32
+#ifdef _WIN32
#define evutil_socket_t intptr_t
#else
#define evutil_socket_t int
@@ -285,12 +303,14 @@ extern "C" {
Parameters and return values are as for socketpair()
*/
+EVENT2_EXPORT_SYMBOL
int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]);
/** Do platform-specific operations as needed to make a socket nonblocking.
@param sock The socket to make nonblocking
@return 0 on success, -1 on failure
*/
+EVENT2_EXPORT_SYMBOL
int evutil_make_socket_nonblocking(evutil_socket_t sock);
/** Do platform-specific operations to make a listener socket reusable.
@@ -304,14 +324,29 @@ int evutil_make_socket_nonblocking(evutil_socket_t sock);
@param sock The socket to make reusable
@return 0 on success, -1 on failure
*/
+EVENT2_EXPORT_SYMBOL
int evutil_make_listen_socket_reuseable(evutil_socket_t sock);
+/** Do platform-specific operations to make a listener port reusable.
+
+ Specifically, we want to make sure that multiple programs which also
+ set the same socket option will be able to bind, listen at the same time.
+
+ This is a feature available only to Linux 3.9+
+
+ @param sock The socket to make reusable
+ @return 0 on success, -1 on failure
+ */
+EVENT2_EXPORT_SYMBOL
+int evutil_make_listen_socket_reuseable_port(evutil_socket_t sock);
+
/** Do platform-specific operations as needed to close a socket upon a
successful execution of one of the exec*() functions.
@param sock The socket to be closed
@return 0 on success, -1 on failure
*/
+EVENT2_EXPORT_SYMBOL
int evutil_make_socket_closeonexec(evutil_socket_t sock);
/** Do the platform-specific call needed to close a socket returned from
@@ -320,21 +355,37 @@ int evutil_make_socket_closeonexec(evutil_socket_t sock);
@param sock The socket to be closed
@return 0 on success, -1 on failure
*/
+EVENT2_EXPORT_SYMBOL
int evutil_closesocket(evutil_socket_t sock);
#define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
-
-#ifdef WIN32
+/** Do platform-specific operations, if possible, to make a tcp listener
+ * socket defer accept()s until there is data to read.
+ *
+ * Not all platforms support this. You don't want to do this for every
+ * listener socket: only the ones that implement a protocol where the
+ * client transmits before the server needs to respond.
+ *
+ * @param sock The listening socket to to make deferred
+ * @return 0 on success (whether the operation is supported or not),
+ * -1 on failure
+*/
+EVENT2_EXPORT_SYMBOL
+int evutil_make_tcp_listen_socket_deferred(evutil_socket_t sock);
+
+#ifdef _WIN32
/** Return the most recent socket error. Not idempotent on all platforms. */
#define EVUTIL_SOCKET_ERROR() WSAGetLastError()
/** Replace the most recent socket error with errcode */
#define EVUTIL_SET_SOCKET_ERROR(errcode) \
do { WSASetLastError(errcode); } while (0)
/** Return the most recent socket error to occur on sock. */
+EVENT2_EXPORT_SYMBOL
int evutil_socket_geterror(evutil_socket_t sock);
/** Convert a socket error to a string. */
+EVENT2_EXPORT_SYMBOL
const char *evutil_socket_error_to_string(int errcode);
-#elif defined(_EVENT_IN_DOXYGEN)
+#elif defined(EVENT_IN_DOXYGEN_)
/**
@name Socket error functions
@@ -375,7 +426,7 @@ const char *evutil_socket_error_to_string(int errcode);
*
* @{
*/
-#ifdef _EVENT_HAVE_TIMERADD
+#ifdef EVENT__HAVE_TIMERADD
#define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
#define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
#else
@@ -397,9 +448,9 @@ const char *evutil_socket_error_to_string(int errcode);
(vvp)->tv_usec += 1000000; \
} \
} while (0)
-#endif /* !_EVENT_HAVE_HAVE_TIMERADD */
+#endif /* !EVENT__HAVE_TIMERADD */
-#ifdef _EVENT_HAVE_TIMERCLEAR
+#ifdef EVENT__HAVE_TIMERCLEAR
#define evutil_timerclear(tvp) timerclear(tvp)
#else
#define evutil_timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
@@ -413,7 +464,7 @@ const char *evutil_socket_error_to_string(int errcode);
((tvp)->tv_usec cmp (uvp)->tv_usec) : \
((tvp)->tv_sec cmp (uvp)->tv_sec))
-#ifdef _EVENT_HAVE_TIMERISSET
+#ifdef EVENT__HAVE_TIMERISSET
#define evutil_timerisset(tvp) timerisset(tvp)
#else
#define evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
@@ -428,19 +479,22 @@ const char *evutil_socket_error_to_string(int errcode);
/* big-int related functions */
/** Parse a 64-bit value from a string. Arguments are as for strtol. */
+EVENT2_EXPORT_SYMBOL
ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
/** Replacement for gettimeofday on platforms that lack it. */
-#ifdef _EVENT_HAVE_GETTIMEOFDAY
+#ifdef EVENT__HAVE_GETTIMEOFDAY
#define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
#else
struct timezone;
+EVENT2_EXPORT_SYMBOL
int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
#endif
/** Replacement for snprintf to get consistent behavior on platforms for
which the return value of snprintf does not conform to C99.
*/
+EVENT2_EXPORT_SYMBOL
int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
#ifdef __GNUC__
__attribute__((format(printf, 3, 4)))
@@ -449,6 +503,7 @@ int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
/** Replacement for vsnprintf to get consistent behavior on platforms for
which the return value of snprintf does not conform to C99.
*/
+EVENT2_EXPORT_SYMBOL
int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap)
#ifdef __GNUC__
__attribute__((format(printf, 3, 0)))
@@ -456,8 +511,10 @@ int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap)
;
/** Replacement for inet_ntop for platforms which lack it. */
+EVENT2_EXPORT_SYMBOL
const char *evutil_inet_ntop(int af, const void *src, char *dst, size_t len);
/** Replacement for inet_pton for platforms which lack it. */
+EVENT2_EXPORT_SYMBOL
int evutil_inet_pton(int af, const char *src, void *dst);
struct sockaddr;
@@ -481,6 +538,7 @@ struct sockaddr;
or if out is not large enough to hold the result. Otherwise returns
0 on success.
*/
+EVENT2_EXPORT_SYMBOL
int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen);
/** Compare two sockaddrs; return 0 if they are equal, or less than 0 if sa1
@@ -488,21 +546,24 @@ int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outle
* true, consider the port as well as the address. Only implemented for
* AF_INET and AF_INET6 addresses. The ordering is not guaranteed to remain
* the same between Libevent versions. */
+EVENT2_EXPORT_SYMBOL
int evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
int include_port);
/** As strcasecmp, but always compares the characters in locale-independent
ASCII. That's useful if you're handling data in ASCII-based protocols.
*/
+EVENT2_EXPORT_SYMBOL
int evutil_ascii_strcasecmp(const char *str1, const char *str2);
/** As strncasecmp, but always compares the characters in locale-independent
ASCII. That's useful if you're handling data in ASCII-based protocols.
*/
+EVENT2_EXPORT_SYMBOL
int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n);
/* Here we define evutil_addrinfo to the native addrinfo type, or redefine it
* if this system has no getaddrinfo(). */
-#ifdef _EVENT_HAVE_STRUCT_ADDRINFO
+#ifdef EVENT__HAVE_STRUCT_ADDRINFO
#define evutil_addrinfo addrinfo
#else
/** A definition of struct addrinfo for systems that lack it.
@@ -528,32 +589,32 @@ struct evutil_addrinfo {
@{
*/
-#ifdef EAI_ADDRFAMILY
+#if defined(EAI_ADDRFAMILY) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY
#else
#define EVUTIL_EAI_ADDRFAMILY -901
#endif
-#ifdef EAI_AGAIN
+#if defined(EAI_AGAIN) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_EAI_AGAIN EAI_AGAIN
#else
#define EVUTIL_EAI_AGAIN -902
#endif
-#ifdef EAI_BADFLAGS
+#if defined(EAI_BADFLAGS) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS
#else
#define EVUTIL_EAI_BADFLAGS -903
#endif
-#ifdef EAI_FAIL
+#if defined(EAI_FAIL) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_EAI_FAIL EAI_FAIL
#else
#define EVUTIL_EAI_FAIL -904
#endif
-#ifdef EAI_FAMILY
+#if defined(EAI_FAMILY) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_EAI_FAMILY EAI_FAMILY
#else
#define EVUTIL_EAI_FAMILY -905
#endif
-#ifdef EAI_MEMORY
+#if defined(EAI_MEMORY) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_EAI_MEMORY EAI_MEMORY
#else
#define EVUTIL_EAI_MEMORY -906
@@ -561,27 +622,27 @@ struct evutil_addrinfo {
/* This test is a bit complicated, since some MS SDKs decide to
* remove NODATA or redefine it to be the same as NONAME, in a
* fun interpretation of RFC 2553 and RFC 3493. */
-#if defined(EAI_NODATA) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME)
+#if defined(EAI_NODATA) && defined(EVENT__HAVE_GETADDRINFO) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME)
#define EVUTIL_EAI_NODATA EAI_NODATA
#else
#define EVUTIL_EAI_NODATA -907
#endif
-#ifdef EAI_NONAME
+#if defined(EAI_NONAME) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_EAI_NONAME EAI_NONAME
#else
#define EVUTIL_EAI_NONAME -908
#endif
-#ifdef EAI_SERVICE
+#if defined(EAI_SERVICE) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_EAI_SERVICE EAI_SERVICE
#else
#define EVUTIL_EAI_SERVICE -909
#endif
-#ifdef EAI_SOCKTYPE
+#if defined(EAI_SOCKTYPE) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE
#else
#define EVUTIL_EAI_SOCKTYPE -910
#endif
-#ifdef EAI_SYSTEM
+#if defined(EAI_SYSTEM) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_EAI_SYSTEM EAI_SYSTEM
#else
#define EVUTIL_EAI_SYSTEM -911
@@ -589,37 +650,37 @@ struct evutil_addrinfo {
#define EVUTIL_EAI_CANCEL -90001
-#ifdef AI_PASSIVE
+#if defined(AI_PASSIVE) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_AI_PASSIVE AI_PASSIVE
#else
#define EVUTIL_AI_PASSIVE 0x1000
#endif
-#ifdef AI_CANONNAME
+#if defined(AI_CANONNAME) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_AI_CANONNAME AI_CANONNAME
#else
#define EVUTIL_AI_CANONNAME 0x2000
#endif
-#ifdef AI_NUMERICHOST
+#if defined(AI_NUMERICHOST) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST
#else
#define EVUTIL_AI_NUMERICHOST 0x4000
#endif
-#ifdef AI_NUMERICSERV
+#if defined(AI_NUMERICSERV) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV
#else
#define EVUTIL_AI_NUMERICSERV 0x8000
#endif
-#ifdef AI_V4MAPPED
+#if defined(AI_V4MAPPED) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_AI_V4MAPPED AI_V4MAPPED
#else
#define EVUTIL_AI_V4MAPPED 0x10000
#endif
-#ifdef AI_ALL
+#if defined(AI_ALL) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_AI_ALL AI_ALL
#else
#define EVUTIL_AI_ALL 0x20000
#endif
-#ifdef AI_ADDRCONFIG
+#if defined(AI_ADDRCONFIG) && defined(EVENT__HAVE_GETADDRINFO)
#define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG
#else
#define EVUTIL_AI_ADDRCONFIG 0x40000
@@ -638,12 +699,15 @@ struct evutil_addrinfo;
*
* For a nonblocking variant, see evdns_getaddrinfo.
*/
+EVENT2_EXPORT_SYMBOL
int evutil_getaddrinfo(const char *nodename, const char *servname,
const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res);
/** Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo. */
+EVENT2_EXPORT_SYMBOL
void evutil_freeaddrinfo(struct evutil_addrinfo *ai);
+EVENT2_EXPORT_SYMBOL
const char *evutil_gai_strerror(int err);
/** Generate n bytes of secure pseudorandom data, and store them in buf.
@@ -655,6 +719,7 @@ const char *evutil_gai_strerror(int err);
* provides only rudimentary prediction- and backtracking-resistance. Don't
* use this for serious cryptographic applications.
*/
+EVENT2_EXPORT_SYMBOL
void evutil_secure_rng_get_bytes(void *buf, size_t n);
/**
@@ -673,6 +738,7 @@ void evutil_secure_rng_get_bytes(void *buf, size_t n);
* whatever), and you want to make sure that seeding happens before your
* program loses the ability to do it.
*/
+EVENT2_EXPORT_SYMBOL
int evutil_secure_rng_init(void);
/**
@@ -688,6 +754,7 @@ int evutil_secure_rng_init(void);
*
* This API is unstable, and might change in a future libevent version.
*/
+EVENT2_EXPORT_SYMBOL
int evutil_secure_rng_set_urandom_device_file(char *fname);
/** Seed the random number generator with extra random bytes.
@@ -704,10 +771,11 @@ int evutil_secure_rng_set_urandom_device_file(char *fname);
@param dat a buffer full of a strong source of random numbers
@param datlen the number of bytes to read from datlen
*/
+EVENT2_EXPORT_SYMBOL
void evutil_secure_rng_add_bytes(const char *dat, size_t datlen);
#ifdef __cplusplus
}
#endif
-#endif /* _EVUTIL_H_ */
+#endif /* EVENT1_EVUTIL_H_INCLUDED_ */
diff --git a/include/event2/visibility.h b/include/event2/visibility.h
new file mode 100644
index 00000000..5e7163a1
--- /dev/null
+++ b/include/event2/visibility.h
@@ -0,0 +1,55 @@
+/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+ * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef EVENT2_VISIBILITY_H_INCLUDED_
+#define EVENT2_VISIBILITY_H_INCLUDED_
+
+#include <event2/event-config.h>
+
+#if defined(event_EXPORTS) || defined(event_extra_EXPORTS) || defined(event_core_EXPORTS)
+
+#if defined (__SUNPRO_C) && (__SUNPRO_C >= 0x550)
+#define EVENT2_EXPORT_SYMBOL __global
+#elif defined __GNUC__
+#define EVENT2_EXPORT_SYMBOL __attribute__ ((visibility("default")))
+#elif defined(_MSC_VER)
+#define EVENT2_EXPORT_SYMBOL extern __declspec(dllexport)
+#else
+/* unknown compiler */
+#define EVENT2_EXPORT_SYMBOL
+#endif
+
+#else
+
+#if defined(EVENT__NEED_DLLIMPORT) && defined(_MSC_VER) && !defined(EVENT_BUILDING_REGRESS_TEST)
+#define EVENT2_EXPORT_SYMBOL extern __declspec(dllimport)
+#else
+#define EVENT2_EXPORT_SYMBOL
+#endif
+
+#endif
+
+#endif /* EVENT2_VISIBILITY_H_INCLUDED_ */
diff --git a/evhttp.h b/include/evhttp.h
index 5ce15e65..549bc9b1 100644
--- a/evhttp.h
+++ b/include/evhttp.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVHTTP_H_
-#define _EVHTTP_H_
+#ifndef EVENT1_EVHTTP_H_INCLUDED_
+#define EVENT1_EVHTTP_H_INCLUDED_
/** @file evhttp.h
@@ -42,4 +42,4 @@
#include <event2/http_struct.h>
#include <event2/http_compat.h>
-#endif /* _EVHTTP_H_ */
+#endif /* EVENT1_EVHTTP_H_INCLUDED_ */
diff --git a/evrpc.h b/include/evrpc.h
index 5b88262d..7e986f7d 100644
--- a/evrpc.h
+++ b/include/evrpc.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVRPC_H_
-#define _EVRPC_H_
+#ifndef EVENT1_EVRPC_H_INCLUDED_
+#define EVENT1_EVRPC_H_INCLUDED_
/** @file evrpc.h
@@ -42,4 +42,4 @@
#include <event2/rpc_struct.h>
#include <event2/rpc_compat.h>
-#endif /* _EVRPC_H_ */
+#endif /* EVENT1_EVRPC_H_INCLUDED_ */
diff --git a/evutil.h b/include/evutil.h
index 02bca89e..12c137d7 100644
--- a/evutil.h
+++ b/include/evutil.h
@@ -23,8 +23,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVUTIL_H_
-#define _EVUTIL_H_
+#ifndef EVENT1_EVUTIL_H_INCLUDED_
+#define EVENT1_EVUTIL_H_INCLUDED_
/** @file evutil.h
@@ -36,4 +36,4 @@
#include <event2/util.h>
-#endif /* _EVUTIL_H_ */
+#endif /* EVENT1_EVUTIL_H_INCLUDED_ */
diff --git a/include/include.am b/include/include.am
new file mode 100644
index 00000000..9aad2dba
--- /dev/null
+++ b/include/include.am
@@ -0,0 +1,46 @@
+# include/Makefile.am for libevent
+# Copyright 2000-2007 Niels Provos
+# Copyright 2007-2012 Niels Provos and Nick Mathewson
+#
+# See LICENSE for copying information.
+
+include_event2dir = $(includedir)/event2
+
+EVENT2_EXPORT = \
+ include/event2/buffer.h \
+ include/event2/buffer_compat.h \
+ include/event2/bufferevent.h \
+ include/event2/bufferevent_compat.h \
+ include/event2/bufferevent_ssl.h \
+ include/event2/bufferevent_struct.h \
+ include/event2/dns.h \
+ include/event2/dns_compat.h \
+ include/event2/dns_struct.h \
+ include/event2/event.h \
+ include/event2/event_compat.h \
+ include/event2/event_struct.h \
+ include/event2/http.h \
+ include/event2/http_compat.h \
+ include/event2/http_struct.h \
+ include/event2/keyvalq_struct.h \
+ include/event2/listener.h \
+ include/event2/rpc.h \
+ include/event2/rpc_compat.h \
+ include/event2/rpc_struct.h \
+ include/event2/tag.h \
+ include/event2/tag_compat.h \
+ include/event2/thread.h \
+ include/event2/util.h \
+ include/event2/visibility.h
+
+## Without the nobase_ prefixing, Automake would strip "include/event2/" from
+## the source header filename to derive the installed header filename.
+## With nobase_ the installed path is $(includedir)/include/event2/ev*.h.
+
+if INSTALL_LIBEVENT
+include_event2_HEADERS = $(EVENT2_EXPORT)
+nodist_include_event2_HEADERS = include/event2/event-config.h
+else
+noinst_HEADERS += $(EVENT2_EXPORT)
+nodist_noinst_HEADERS = include/event2/event-config.h
+endif
diff --git a/iocp-internal.h b/iocp-internal.h
index ae79cbc7..93dbe2b1 100644
--- a/iocp-internal.h
+++ b/iocp-internal.h
@@ -24,8 +24,8 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT_IOCP_INTERNAL_H
-#define _EVENT_IOCP_INTERNAL_H
+#ifndef IOCP_INTERNAL_H_INCLUDED_
+#define IOCP_INTERNAL_H_INCLUDED_
#ifdef __cplusplus
extern "C" {
@@ -39,7 +39,7 @@ typedef void (*iocp_callback)(struct event_overlapped *, ev_uintptr_t, ev_ssize_
/* This whole file is actually win32 only. We wrap the structures in a win32
* ifdef so that we can test-compile code that uses these interfaces on
* non-win32 platforms. */
-#ifdef WIN32
+#ifdef _WIN32
/**
Internal use only. Wraps an OVERLAPPED that we're using for libevent
@@ -92,7 +92,7 @@ struct event_iocp_port {
HANDLE *shutdownSemaphore;
};
-const struct win32_extension_fns *event_get_win32_extension_fns(void);
+const struct win32_extension_fns *event_get_win32_extension_fns_(void);
#else
/* Dummy definition so we can test-compile more things on unix. */
struct event_overlapped {
@@ -106,61 +106,61 @@ struct event_overlapped {
@param cb The callback that should be invoked once the IO operation has
finished.
*/
-void event_overlapped_init(struct event_overlapped *, iocp_callback cb);
+void event_overlapped_init_(struct event_overlapped *, iocp_callback cb);
/** Allocate and return a new evbuffer that supports overlapped IO on a given
socket. The socket must be associated with an IO completion port using
- event_iocp_port_associate.
+ event_iocp_port_associate_.
*/
-struct evbuffer *evbuffer_overlapped_new(evutil_socket_t fd);
+struct evbuffer *evbuffer_overlapped_new_(evutil_socket_t fd);
/** XXXX Document (nickm) */
-evutil_socket_t _evbuffer_overlapped_get_fd(struct evbuffer *buf);
+evutil_socket_t evbuffer_overlapped_get_fd_(struct evbuffer *buf);
-void _evbuffer_overlapped_set_fd(struct evbuffer *buf, evutil_socket_t fd);
+void evbuffer_overlapped_set_fd_(struct evbuffer *buf, evutil_socket_t fd);
/** Start reading data onto the end of an overlapped evbuffer.
An evbuffer can only have one read pending at a time. While the read
is in progress, no other data may be added to the end of the buffer.
- The buffer must be created with event_overlapped_init().
- evbuffer_commit_read() must be called in the completion callback.
+ The buffer must be created with event_overlapped_init_().
+ evbuffer_commit_read_() must be called in the completion callback.
@param buf The buffer to read onto
@param n The number of bytes to try to read.
@param ol Overlapped object with associated completion callback.
@return 0 on success, -1 on error.
*/
-int evbuffer_launch_read(struct evbuffer *buf, size_t n, struct event_overlapped *ol);
+int evbuffer_launch_read_(struct evbuffer *buf, size_t n, struct event_overlapped *ol);
/** Start writing data from the start of an evbuffer.
An evbuffer can only have one write pending at a time. While the write is
in progress, no other data may be removed from the front of the buffer.
- The buffer must be created with event_overlapped_init().
- evbuffer_commit_write() must be called in the completion callback.
+ The buffer must be created with event_overlapped_init_().
+ evbuffer_commit_write_() must be called in the completion callback.
@param buf The buffer to read onto
@param n The number of bytes to try to read.
@param ol Overlapped object with associated completion callback.
@return 0 on success, -1 on error.
*/
-int evbuffer_launch_write(struct evbuffer *buf, ev_ssize_t n, struct event_overlapped *ol);
+int evbuffer_launch_write_(struct evbuffer *buf, ev_ssize_t n, struct event_overlapped *ol);
/** XXX document */
-void evbuffer_commit_read(struct evbuffer *, ev_ssize_t);
-void evbuffer_commit_write(struct evbuffer *, ev_ssize_t);
+void evbuffer_commit_read_(struct evbuffer *, ev_ssize_t);
+void evbuffer_commit_write_(struct evbuffer *, ev_ssize_t);
/** Create an IOCP, and launch its worker threads. Internal use only.
This interface is unstable, and will change.
*/
-struct event_iocp_port *event_iocp_port_launch(int n_cpus);
+struct event_iocp_port *event_iocp_port_launch_(int n_cpus);
/** Associate a file descriptor with an iocp, such that overlapped IO on the
fd will happen on one of the iocp's worker threads.
*/
-int event_iocp_port_associate(struct event_iocp_port *port, evutil_socket_t fd,
+int event_iocp_port_associate_(struct event_iocp_port *port, evutil_socket_t fd,
ev_uintptr_t key);
/** Tell all threads serving an iocp to stop. Wait for up to waitMsec for all
@@ -169,29 +169,29 @@ int event_iocp_port_associate(struct event_iocp_port *port, evutil_socket_t fd,
0. Otherwise, return -1. If you get a -1 return value, it is safe to call
this function again.
*/
-int event_iocp_shutdown(struct event_iocp_port *port, long waitMsec);
+int event_iocp_shutdown_(struct event_iocp_port *port, long waitMsec);
/* FIXME document. */
-int event_iocp_activate_overlapped(struct event_iocp_port *port,
+int event_iocp_activate_overlapped_(struct event_iocp_port *port,
struct event_overlapped *o,
ev_uintptr_t key, ev_uint32_t n_bytes);
struct event_base;
/* FIXME document. */
-struct event_iocp_port *event_base_get_iocp(struct event_base *base);
+struct event_iocp_port *event_base_get_iocp_(struct event_base *base);
/* FIXME document. */
-int event_base_start_iocp(struct event_base *base, int n_cpus);
-void event_base_stop_iocp(struct event_base *base);
+int event_base_start_iocp_(struct event_base *base, int n_cpus);
+void event_base_stop_iocp_(struct event_base *base);
/* FIXME document. */
-struct bufferevent *bufferevent_async_new(struct event_base *base,
+struct bufferevent *bufferevent_async_new_(struct event_base *base,
evutil_socket_t fd, int options);
/* FIXME document. */
-void bufferevent_async_set_connected(struct bufferevent *bev);
-int bufferevent_async_can_connect(struct bufferevent *bev);
-int bufferevent_async_connect(struct bufferevent *bev, evutil_socket_t fd,
+void bufferevent_async_set_connected_(struct bufferevent *bev);
+int bufferevent_async_can_connect_(struct bufferevent *bev);
+int bufferevent_async_connect_(struct bufferevent *bev, evutil_socket_t fd,
const struct sockaddr *sa, int socklen);
#ifdef __cplusplus
diff --git a/ipv6-internal.h b/ipv6-internal.h
index e3737118..0c207377 100644
--- a/ipv6-internal.h
+++ b/ipv6-internal.h
@@ -27,14 +27,16 @@
/* Internal use only: Fake IPv6 structures and values on platforms that
* do not have them */
-#ifndef _EVENT_IPV6_INTERNAL_H
-#define _EVENT_IPV6_INTERNAL_H
+#ifndef IPV6_INTERNAL_H_INCLUDED_
+#define IPV6_INTERNAL_H_INCLUDED_
+
+#include "event2/event-config.h"
+#include "evconfig-private.h"
#include <sys/types.h>
-#ifdef _EVENT_HAVE_SYS_SOCKET_H
+#ifdef EVENT__HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
-#include "event2/event-config.h"
#include "event2/util.h"
#ifdef __cplusplus
@@ -47,17 +49,17 @@ extern "C" {
* properly.
*/
-#ifndef _EVENT_HAVE_STRUCT_IN6_ADDR
+#ifndef EVENT__HAVE_STRUCT_IN6_ADDR
struct in6_addr {
ev_uint8_t s6_addr[16];
};
#endif
-#ifndef _EVENT_HAVE_SA_FAMILY_T
+#ifndef EVENT__HAVE_SA_FAMILY_T
typedef int sa_family_t;
#endif
-#ifndef _EVENT_HAVE_STRUCT_SOCKADDR_IN6
+#ifndef EVENT__HAVE_STRUCT_SOCKADDR_IN6
struct sockaddr_in6 {
/* This will fail if we find a struct sockaddr that doesn't have
* sa_family as the first element. */
diff --git a/kqueue-internal.h b/kqueue-internal.h
new file mode 100644
index 00000000..02c5a360
--- /dev/null
+++ b/kqueue-internal.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2012 Niels Provos and Nick Mathewson
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef KQUEUE_INTERNAL_H_INCLUDED_
+#define KQUEUE_INTERNAL_H_INCLUDED_
+
+/** Notification function, used to tell an event base to wake up from another
+ * thread. Only works when event_kq_add_notify_event_() has previously been
+ * called successfully on that base. */
+int event_kq_notify_base_(struct event_base *base);
+
+/** Prepare a kqueue-using event base to receive notifications via an internal
+ * EVFILT_USER event. Return 0 on sucess, -1 on failure.
+ */
+int event_kq_add_notify_event_(struct event_base *base);
+
+#endif
diff --git a/kqueue.c b/kqueue.c
index 51984a4e..7780e1f1 100644
--- a/kqueue.c
+++ b/kqueue.c
@@ -27,11 +27,12 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#define _GNU_SOURCE
+#ifdef EVENT__HAVE_KQUEUE
#include <sys/types.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
@@ -42,14 +43,14 @@
#include <string.h>
#include <unistd.h>
#include <errno.h>
-#ifdef _EVENT_HAVE_INTTYPES_H
+#ifdef EVENT__HAVE_INTTYPES_H
#include <inttypes.h>
#endif
/* Some platforms apparently define the udata field of struct kevent as
* intptr_t, whereas others define it as void*. There doesn't seem to be an
* easy way to tell them apart via autoconf, so we need to use OS macros. */
-#if defined(_EVENT_HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__)
+#if defined(EVENT__HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__)
#define PTR_TO_UDATA(x) ((intptr_t)(x))
#define INT_TO_UDATA(x) ((intptr_t)(x))
#else
@@ -64,6 +65,8 @@
#include "evthread-internal.h"
#include "changelist-internal.h"
+#include "kqueue-internal.h"
+
#define NEVENT 64
struct kqop {
@@ -73,6 +76,7 @@ struct kqop {
struct kevent *events;
int events_size;
int kq;
+ int notify_event_added;
pid_t pid;
};
@@ -87,8 +91,8 @@ static void kq_dealloc(struct event_base *);
const struct eventop kqops = {
"kqueue",
kq_init,
- event_changelist_add,
- event_changelist_del,
+ event_changelist_add_,
+ event_changelist_del_,
kq_dispatch,
kq_dealloc,
1 /* need reinit */,
@@ -165,12 +169,6 @@ err:
return (NULL);
}
-static void
-kq_sighandler(int sig)
-{
- /* Do nothing here */
-}
-
#define ADD_UDATA 0x30303
static void
@@ -272,7 +270,7 @@ kq_dispatch(struct event_base *base, struct timeval *tv)
if (n_changes < 0)
return -1;
- event_changelist_remove_all(&base->changelist, base);
+ event_changelist_remove_all_(&base->changelist, base);
/* steal the changes array in case some broken code tries to call
* dispatch twice at once. */
@@ -334,6 +332,23 @@ kq_dispatch(struct event_base *base, struct timeval *tv)
* on FreeBSD. */
case EINVAL:
continue;
+#if defined(__FreeBSD__)
+ /*
+ * This currently occurs if an FD is closed
+ * before the EV_DELETE makes it out via kevent().
+ * The FreeBSD capabilities code sees the blank
+ * capability set and rejects the request to
+ * modify an event.
+ *
+ * To be strictly correct - when an FD is closed,
+ * all the registered events are also removed.
+ * Queuing EV_DELETE to a closed FD is wrong.
+ * The event(s) should just be deleted from
+ * the pending changelist.
+ */
+ case ENOTCAPABLE:
+ continue;
+#endif
/* Can occur on a delete if the fd is closed. */
case EBADF:
@@ -374,15 +389,19 @@ kq_dispatch(struct event_base *base, struct timeval *tv)
which |= EV_WRITE;
} else if (events[i].filter == EVFILT_SIGNAL) {
which |= EV_SIGNAL;
+#ifdef EVFILT_USER
+ } else if (events[i].filter == EVFILT_USER) {
+ base->is_notify_pending = 0;
+#endif
}
if (!which)
continue;
if (events[i].filter == EVFILT_SIGNAL) {
- evmap_signal_active(base, events[i].ident, 1);
+ evmap_signal_active_(base, events[i].ident, 1);
} else {
- evmap_io_active(base, events[i].ident, which | EV_ET);
+ evmap_io_active_(base, events[i].ident, which | EV_ET);
}
}
@@ -412,7 +431,7 @@ static void
kq_dealloc(struct event_base *base)
{
struct kqop *kqop = base->evbase;
- evsig_dealloc(base);
+ evsig_dealloc_(base);
kqop_free(kqop);
}
@@ -438,9 +457,13 @@ kq_sig_add(struct event_base *base, int nsignal, short old, short events, void *
if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
return (-1);
- /* XXXX The manpage suggest we could use SIG_IGN instead of a
- * do-nothing handler */
- if (_evsig_set_handler(base, nsignal, kq_sighandler) == -1)
+ /* We can set the handler for most signals to SIG_IGN and
+ * still have them reported to us in the queue. However,
+ * if the handler for SIGCHLD is SIG_IGN, the system reaps
+ * zombie processes for us, and we don't get any notification.
+ * This appears to be the only signal with this quirk. */
+ if (evsig_set_handler_(base, nsignal,
+ nsignal == SIGCHLD ? SIG_DFL : SIG_IGN) == -1)
return (-1);
return (0);
@@ -468,8 +491,76 @@ kq_sig_del(struct event_base *base, int nsignal, short old, short events, void *
if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1)
return (-1);
- if (_evsig_restore_handler(base, nsignal) == -1)
+ if (evsig_restore_handler_(base, nsignal) == -1)
return (-1);
return (0);
}
+
+
+/* OSX 10.6 and FreeBSD 8.1 add support for EVFILT_USER, which we can use
+ * to wake up the event loop from another thread. */
+
+/* Magic number we use for our filter ID. */
+#define NOTIFY_IDENT 42
+
+int
+event_kq_add_notify_event_(struct event_base *base)
+{
+ struct kqop *kqop = base->evbase;
+#if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
+ struct kevent kev;
+ struct timespec timeout = { 0, 0 };
+#endif
+
+ if (kqop->notify_event_added)
+ return 0;
+
+#if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
+ memset(&kev, 0, sizeof(kev));
+ kev.ident = NOTIFY_IDENT;
+ kev.filter = EVFILT_USER;
+ kev.flags = EV_ADD | EV_CLEAR;
+
+ if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) {
+ event_warn("kevent: adding EVFILT_USER event");
+ return -1;
+ }
+
+ kqop->notify_event_added = 1;
+
+ return 0;
+#else
+ return -1;
+#endif
+}
+
+int
+event_kq_notify_base_(struct event_base *base)
+{
+ struct kqop *kqop = base->evbase;
+#if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
+ struct kevent kev;
+ struct timespec timeout = { 0, 0 };
+#endif
+ if (! kqop->notify_event_added)
+ return -1;
+
+#if defined(EVFILT_USER) && defined(NOTE_TRIGGER)
+ memset(&kev, 0, sizeof(kev));
+ kev.ident = NOTIFY_IDENT;
+ kev.filter = EVFILT_USER;
+ kev.fflags = NOTE_TRIGGER;
+
+ if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) {
+ event_warn("kevent: triggering EVFILT_USER event");
+ return -1;
+ }
+
+ return 0;
+#else
+ return -1;
+#endif
+}
+
+#endif /* EVENT__HAVE_KQUEUE */
diff --git a/libevent_openssl.pc.in b/libevent_openssl.pc.in
index 9624cf24..a65d1e06 100644
--- a/libevent_openssl.pc.in
+++ b/libevent_openssl.pc.in
@@ -11,6 +11,6 @@ Version: @VERSION@
Requires: libevent
Conflicts:
Libs: -L${libdir} -levent_openssl
-Libs.private: @LIBS@ -lssl -lcrypto
-Cflags: -I${includedir}
+Libs.private: @LIBS@ @OPENSSL_LIBS@
+Cflags: -I${includedir} @OPENSSL_INCS@
diff --git a/listener.c b/listener.c
index 2b67b525..172d2927 100644
--- a/listener.c
+++ b/listener.c
@@ -24,11 +24,12 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <sys/types.h>
-
#include "event2/event-config.h"
+#include "evconfig-private.h"
+
+#include <sys/types.h>
-#ifdef WIN32
+#ifdef _WIN32
#ifndef _WIN32_WINNT
/* Minimum required for InitializeCriticalSectionAndSpinCount */
#define _WIN32_WINNT 0x0403
@@ -38,13 +39,13 @@
#include <mswsock.h>
#endif
#include <errno.h>
-#ifdef _EVENT_HAVE_SYS_SOCKET_H
+#ifdef EVENT__HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
-#ifdef _EVENT_HAVE_FCNTL_H
+#ifdef EVENT__HAVE_FCNTL_H
#include <fcntl.h>
#endif
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
@@ -56,7 +57,7 @@
#include "util-internal.h"
#include "log-internal.h"
#include "evthread-internal.h"
-#ifdef WIN32
+#ifdef _WIN32
#include "iocp-internal.h"
#include "defer-internal.h"
#include "event-internal.h"
@@ -79,6 +80,7 @@ struct evconnlistener {
void *user_data;
unsigned flags;
short refcnt;
+ int accept4_flags;
unsigned enabled : 1;
};
@@ -87,7 +89,7 @@ struct evconnlistener_event {
struct event listener;
};
-#ifdef WIN32
+#ifdef _WIN32
struct evconnlistener_iocp {
struct evconnlistener base;
evutil_socket_t fd;
@@ -157,10 +159,10 @@ evconnlistener_new(struct event_base *base,
{
struct evconnlistener_event *lev;
-#ifdef WIN32
- if (base && event_base_get_iocp(base)) {
+#ifdef _WIN32
+ if (base && event_base_get_iocp_(base)) {
const struct win32_extension_fns *ext =
- event_get_win32_extension_fns();
+ event_get_win32_extension_fns_();
if (ext->AcceptEx && ext->GetAcceptExSockaddrs)
return evconnlistener_new_async(base, cb, ptr, flags,
backlog, fd);
@@ -185,6 +187,12 @@ evconnlistener_new(struct event_base *base,
lev->base.flags = flags;
lev->base.refcnt = 1;
+ lev->base.accept4_flags = 0;
+ if (!(flags & LEV_OPT_LEAVE_SOCKETS_BLOCKING))
+ lev->base.accept4_flags |= EVUTIL_SOCK_NONBLOCK;
+ if (flags & LEV_OPT_CLOSE_ON_EXEC)
+ lev->base.accept4_flags |= EVUTIL_SOCK_CLOEXEC;
+
if (flags & LEV_OPT_THREADSAFE) {
EVTHREAD_ALLOC_LOCK(lev->base.lock, EVTHREAD_LOCKTYPE_RECURSIVE);
}
@@ -192,7 +200,8 @@ evconnlistener_new(struct event_base *base,
event_assign(&lev->listener, base, fd, EV_READ|EV_PERSIST,
listener_read_cb, lev);
- evconnlistener_enable(&lev->base);
+ if (!(flags & LEV_OPT_DISABLED))
+ evconnlistener_enable(&lev->base);
return &lev->base;
}
@@ -206,51 +215,49 @@ evconnlistener_new_bind(struct event_base *base, evconnlistener_cb cb,
evutil_socket_t fd;
int on = 1;
int family = sa ? sa->sa_family : AF_UNSPEC;
+ int socktype = SOCK_STREAM | EVUTIL_SOCK_NONBLOCK;
if (backlog == 0)
return NULL;
- fd = socket(family, SOCK_STREAM, 0);
+ if (flags & LEV_OPT_CLOSE_ON_EXEC)
+ socktype |= EVUTIL_SOCK_CLOEXEC;
+
+ fd = evutil_socket_(family, socktype, 0);
if (fd == -1)
return NULL;
- if (evutil_make_socket_nonblocking(fd) < 0) {
- evutil_closesocket(fd);
- return NULL;
- }
+ if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&on, sizeof(on))<0)
+ goto err;
- if (flags & LEV_OPT_CLOSE_ON_EXEC) {
- if (evutil_make_socket_closeonexec(fd) < 0) {
- evutil_closesocket(fd);
- return NULL;
- }
+ if (flags & LEV_OPT_REUSEABLE) {
+ if (evutil_make_listen_socket_reuseable(fd) < 0)
+ goto err;
}
- if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&on, sizeof(on))<0) {
- evutil_closesocket(fd);
- return NULL;
+ if (flags & LEV_OPT_REUSEABLE_PORT) {
+ if (evutil_make_listen_socket_reuseable_port(fd) < 0)
+ goto err;
}
- if (flags & LEV_OPT_REUSEABLE) {
- if (evutil_make_listen_socket_reuseable(fd) < 0) {
- evutil_closesocket(fd);
- return NULL;
- }
+
+ if (flags & LEV_OPT_DEFERRED_ACCEPT) {
+ if (evutil_make_tcp_listen_socket_deferred(fd) < 0)
+ goto err;
}
if (sa) {
- if (bind(fd, sa, socklen)<0) {
- evutil_closesocket(fd);
- return NULL;
- }
+ if (bind(fd, sa, socklen)<0)
+ goto err;
}
listener = evconnlistener_new(base, cb, ptr, flags, backlog, fd);
- if (!listener) {
- evutil_closesocket(fd);
- return NULL;
- }
+ if (!listener)
+ goto err;
return listener;
+err:
+ evutil_closesocket(fd);
+ return NULL;
}
void
@@ -388,12 +395,8 @@ listener_read_cb(evutil_socket_t fd, short what, void *p)
LOCK(lev);
while (1) {
struct sockaddr_storage ss;
-#ifdef WIN32
- int socklen = sizeof(ss);
-#else
- socklen_t socklen = sizeof(ss);
-#endif
- evutil_socket_t new_fd = accept(fd, (struct sockaddr*)&ss, &socklen);
+ ev_socklen_t socklen = sizeof(ss);
+ evutil_socket_t new_fd = evutil_accept4_(fd, (struct sockaddr*)&ss, &socklen, lev->accept4_flags);
if (new_fd < 0)
break;
if (socklen == 0) {
@@ -403,9 +406,6 @@ listener_read_cb(evutil_socket_t fd, short what, void *p)
continue;
}
- if (!(lev->flags & LEV_OPT_LEAVE_SOCKETS_BLOCKING))
- evutil_make_socket_nonblocking(new_fd);
-
if (lev->cb == NULL) {
evutil_closesocket(new_fd);
UNLOCK(lev);
@@ -443,13 +443,13 @@ listener_read_cb(evutil_socket_t fd, short what, void *p)
}
}
-#ifdef WIN32
+#ifdef _WIN32
struct accepting_socket {
CRITICAL_SECTION lock;
struct event_overlapped overlapped;
SOCKET s;
int error;
- struct deferred_cb deferred;
+ struct event_callback deferred;
struct evconnlistener_iocp *lev;
ev_uint8_t buflen;
ev_uint8_t family;
@@ -459,7 +459,7 @@ struct accepting_socket {
static void accepted_socket_cb(struct event_overlapped *o, ev_uintptr_t key,
ev_ssize_t n, int ok);
-static void accepted_socket_invoke_user_cb(struct deferred_cb *cb, void *arg);
+static void accepted_socket_invoke_user_cb(struct event_callback *cb, void *arg);
static void
iocp_listener_event_add(struct evconnlistener_iocp *lev)
@@ -468,7 +468,7 @@ iocp_listener_event_add(struct evconnlistener_iocp *lev)
return;
lev->event_added = 1;
- event_base_add_virtual(lev->event_base);
+ event_base_add_virtual_(lev->event_base);
}
static void
@@ -478,7 +478,7 @@ iocp_listener_event_del(struct evconnlistener_iocp *lev)
return;
lev->event_added = 0;
- event_base_del_virtual(lev->event_base);
+ event_base_del_virtual_(lev->event_base);
}
static struct accepting_socket *
@@ -500,14 +500,15 @@ new_accepting_socket(struct evconnlistener_iocp *lev, int family)
if (!res)
return NULL;
- event_overlapped_init(&res->overlapped, accepted_socket_cb);
+ event_overlapped_init_(&res->overlapped, accepted_socket_cb);
res->s = INVALID_SOCKET;
res->lev = lev;
res->buflen = buflen;
res->family = family;
- event_deferred_cb_init(&res->deferred,
- accepted_socket_invoke_user_cb, res);
+ event_deferred_cb_init_(&res->deferred,
+ event_base_get_npriorities(lev->event_base) / 2,
+ accepted_socket_invoke_user_cb, res);
InitializeCriticalSectionAndSpinCount(&res->lock, 1000);
@@ -530,7 +531,7 @@ static int
start_accepting(struct accepting_socket *as)
{
/* requires lock */
- const struct win32_extension_fns *ext = event_get_win32_extension_fns();
+ const struct win32_extension_fns *ext = event_get_win32_extension_fns_();
DWORD pending = 0;
SOCKET s = socket(as->family, SOCK_STREAM, 0);
int error = 0;
@@ -551,7 +552,7 @@ start_accepting(struct accepting_socket *as)
if (!(as->lev->base.flags & LEV_OPT_LEAVE_SOCKETS_BLOCKING))
evutil_make_socket_nonblocking(s);
- if (event_iocp_port_associate(as->lev->port, s, 1) < 0) {
+ if (event_iocp_port_associate_(as->lev->port, s, 1) < 0) {
closesocket(s);
return -1;
}
@@ -574,8 +575,8 @@ start_accepting(struct accepting_socket *as)
report_err:
as->error = error;
- event_deferred_cb_schedule(
- event_base_get_deferred_cb_queue(as->lev->event_base),
+ event_deferred_cb_schedule_(
+ as->lev->event_base,
&as->deferred);
return 0;
}
@@ -590,13 +591,13 @@ stop_accepting(struct accepting_socket *as)
}
static void
-accepted_socket_invoke_user_cb(struct deferred_cb *dcb, void *arg)
+accepted_socket_invoke_user_cb(struct event_callback *dcb, void *arg)
{
struct accepting_socket *as = arg;
struct sockaddr *sa_local=NULL, *sa_remote=NULL;
int socklen_local=0, socklen_remote=0;
- const struct win32_extension_fns *ext = event_get_win32_extension_fns();
+ const struct win32_extension_fns *ext = event_get_win32_extension_fns_();
struct evconnlistener *lev = &as->lev->base;
evutil_socket_t sock=-1;
void *data;
@@ -666,8 +667,8 @@ accepted_socket_cb(struct event_overlapped *o, ev_uintptr_t key, ev_ssize_t n, i
EnterCriticalSection(&as->lock);
if (ok) {
/* XXXX Don't do this if some EV_MT flag is set. */
- event_deferred_cb_schedule(
- event_base_get_deferred_cb_queue(as->lev->event_base),
+ event_deferred_cb_schedule_(
+ as->lev->event_base,
&as->deferred);
LeaveCriticalSection(&as->lock);
} else if (as->free_on_cb) {
@@ -691,8 +692,8 @@ accepted_socket_cb(struct event_overlapped *o, ev_uintptr_t key, ev_ssize_t n, i
} else {
as->error = WSAGetLastError();
}
- event_deferred_cb_schedule(
- event_base_get_deferred_cb_queue(as->lev->event_base),
+ event_deferred_cb_schedule_(
+ as->lev->event_base,
&as->deferred);
LeaveCriticalSection(&as->lock);
}
@@ -808,7 +809,7 @@ evconnlistener_new_async(struct event_base *base,
flags |= LEV_OPT_THREADSAFE;
- if (!base || !event_base_get_iocp(base))
+ if (!base || !event_base_get_iocp_(base))
goto err;
/* XXXX duplicate code */
@@ -835,12 +836,12 @@ evconnlistener_new_async(struct event_base *base,
lev->base.refcnt = 1;
lev->base.enabled = 1;
- lev->port = event_base_get_iocp(base);
+ lev->port = event_base_get_iocp_(base);
lev->fd = fd;
lev->event_base = base;
- if (event_iocp_port_associate(lev->port, fd, 1) < 0)
+ if (event_iocp_port_associate_(lev->port, fd, 1) < 0)
goto err_free_lev;
EVTHREAD_ALLOC_LOCK(lev->base.lock, EVTHREAD_LOCKTYPE_RECURSIVE);
diff --git a/log-internal.h b/log-internal.h
index 211b3e1d..330478a9 100644
--- a/log-internal.h
+++ b/log-internal.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _LOG_H_
-#define _LOG_H_
+#ifndef LOG_INTERNAL_H_INCLUDED_
+#define LOG_INTERNAL_H_INCLUDED_
#include "event2/util.h"
@@ -37,7 +37,24 @@
#define EV_NORETURN
#endif
-#define _EVENT_ERR_ABORT ((int)0xdeaddead)
+#define EVENT_ERR_ABORT_ ((int)0xdeaddead)
+
+#define USE_GLOBAL_FOR_DEBUG_LOGGING
+
+#if !defined(EVENT__DISABLE_DEBUG_MODE) || defined(USE_DEBUG)
+#define EVENT_DEBUG_LOGGING_ENABLED
+#endif
+
+#ifdef EVENT_DEBUG_LOGGING_ENABLED
+#ifdef USE_GLOBAL_FOR_DEBUG_LOGGING
+extern ev_uint32_t event_debug_logging_mask_;
+#define event_debug_get_logging_mask_() (event_debug_logging_mask_)
+#else
+ev_uint32_t event_debug_get_logging_mask_(void);
+#endif
+#else
+#define event_debug_get_logging_mask_() (0)
+#endif
void event_err(int eval, const char *fmt, ...) EV_CHECK_FMT(2,3) EV_NORETURN;
void event_warn(const char *fmt, ...) EV_CHECK_FMT(1,2);
@@ -46,12 +63,19 @@ void event_sock_warn(evutil_socket_t sock, const char *fmt, ...) EV_CHECK_FMT(2,
void event_errx(int eval, const char *fmt, ...) EV_CHECK_FMT(2,3) EV_NORETURN;
void event_warnx(const char *fmt, ...) EV_CHECK_FMT(1,2);
void event_msgx(const char *fmt, ...) EV_CHECK_FMT(1,2);
-void _event_debugx(const char *fmt, ...) EV_CHECK_FMT(1,2);
+void event_debugx_(const char *fmt, ...) EV_CHECK_FMT(1,2);
+
+void event_logv_(int severity, const char *errstr, const char *fmt, va_list ap)
+ EV_CHECK_FMT(3,0);
-#ifdef USE_DEBUG
-#define event_debug(x) _event_debugx x
+#ifdef EVENT_DEBUG_LOGGING_ENABLED
+#define event_debug(x) do { \
+ if (event_debug_get_logging_mask_()) { \
+ event_debugx_ x; \
+ } \
+ } while (0)
#else
-#define event_debug(x) do {;} while (0)
+#define event_debug(x) ((void)0)
#endif
#undef EV_CHECK_FMT
diff --git a/log.c b/log.c
index 046e81f9..e8ae9fdc 100644
--- a/log.c
+++ b/log.c
@@ -38,8 +38,9 @@
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
@@ -56,13 +57,38 @@
#include "log-internal.h"
-static void _warn_helper(int severity, const char *errstr, const char *fmt,
- va_list ap);
static void event_log(int severity, const char *msg);
static void event_exit(int errcode) EV_NORETURN;
static event_fatal_cb fatal_fn = NULL;
+#ifdef EVENT_DEBUG_LOGGING_ENABLED
+#ifdef USE_DEBUG
+#define DEFAULT_MASK EVENT_DBG_ALL
+#else
+#define DEFAULT_MASK 0
+#endif
+
+#ifdef USE_GLOBAL_FOR_DEBUG_LOGGING
+ev_uint32_t event_debug_logging_mask_ = DEFAULT_MASK;
+#else
+static ev_uint32_t event_debug_logging_mask_ = DEFAULT_MASK;
+ev_uint32_t
+event_debug_get_logging_mask_(void)
+{
+ return event_debug_logging_mask_;
+}
+#endif
+#endif /* EVENT_DEBUG_LOGGING_ENABLED */
+
+void
+event_enable_debug_logging(ev_uint32_t which)
+{
+#ifdef EVENT_DEBUG_LOGGING_ENABLED
+ event_debug_logging_mask_ = which;
+#endif
+}
+
void
event_set_fatal_callback(event_fatal_cb cb)
{
@@ -75,7 +101,7 @@ event_exit(int errcode)
if (fatal_fn) {
fatal_fn(errcode);
exit(errcode); /* should never be reached */
- } else if (errcode == _EVENT_ERR_ABORT)
+ } else if (errcode == EVENT_ERR_ABORT_)
abort();
else
exit(errcode);
@@ -87,7 +113,7 @@ event_err(int eval, const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
- _warn_helper(_EVENT_LOG_ERR, strerror(errno), fmt, ap);
+ event_logv_(EVENT_LOG_ERR, strerror(errno), fmt, ap);
va_end(ap);
event_exit(eval);
}
@@ -98,7 +124,7 @@ event_warn(const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
- _warn_helper(_EVENT_LOG_WARN, strerror(errno), fmt, ap);
+ event_logv_(EVENT_LOG_WARN, strerror(errno), fmt, ap);
va_end(ap);
}
@@ -109,7 +135,7 @@ event_sock_err(int eval, evutil_socket_t sock, const char *fmt, ...)
int err = evutil_socket_geterror(sock);
va_start(ap, fmt);
- _warn_helper(_EVENT_LOG_ERR, evutil_socket_error_to_string(err), fmt, ap);
+ event_logv_(EVENT_LOG_ERR, evutil_socket_error_to_string(err), fmt, ap);
va_end(ap);
event_exit(eval);
}
@@ -121,7 +147,7 @@ event_sock_warn(evutil_socket_t sock, const char *fmt, ...)
int err = evutil_socket_geterror(sock);
va_start(ap, fmt);
- _warn_helper(_EVENT_LOG_WARN, evutil_socket_error_to_string(err), fmt, ap);
+ event_logv_(EVENT_LOG_WARN, evutil_socket_error_to_string(err), fmt, ap);
va_end(ap);
}
@@ -131,7 +157,7 @@ event_errx(int eval, const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
- _warn_helper(_EVENT_LOG_ERR, NULL, fmt, ap);
+ event_logv_(EVENT_LOG_ERR, NULL, fmt, ap);
va_end(ap);
event_exit(eval);
}
@@ -142,7 +168,7 @@ event_warnx(const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
- _warn_helper(_EVENT_LOG_WARN, NULL, fmt, ap);
+ event_logv_(EVENT_LOG_WARN, NULL, fmt, ap);
va_end(ap);
}
@@ -152,26 +178,29 @@ event_msgx(const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
- _warn_helper(_EVENT_LOG_MSG, NULL, fmt, ap);
+ event_logv_(EVENT_LOG_MSG, NULL, fmt, ap);
va_end(ap);
}
void
-_event_debugx(const char *fmt, ...)
+event_debugx_(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
- _warn_helper(_EVENT_LOG_DEBUG, NULL, fmt, ap);
+ event_logv_(EVENT_LOG_DEBUG, NULL, fmt, ap);
va_end(ap);
}
-static void
-_warn_helper(int severity, const char *errstr, const char *fmt, va_list ap)
+void
+event_logv_(int severity, const char *errstr, const char *fmt, va_list ap)
{
char buf[1024];
size_t len;
+ if (severity == EVENT_LOG_DEBUG && !event_debug_get_logging_mask_())
+ return;
+
if (fmt != NULL)
evutil_vsnprintf(buf, sizeof(buf), fmt, ap);
else
@@ -203,16 +232,16 @@ event_log(int severity, const char *msg)
else {
const char *severity_str;
switch (severity) {
- case _EVENT_LOG_DEBUG:
+ case EVENT_LOG_DEBUG:
severity_str = "debug";
break;
- case _EVENT_LOG_MSG:
+ case EVENT_LOG_MSG:
severity_str = "msg";
break;
- case _EVENT_LOG_WARN:
+ case EVENT_LOG_WARN:
severity_str = "warn";
break;
- case _EVENT_LOG_ERR:
+ case EVENT_LOG_ERR:
severity_str = "err";
break;
default:
diff --git a/m4/libevent_openssl.m4 b/m4/libevent_openssl.m4
new file mode 100644
index 00000000..3cb064a0
--- /dev/null
+++ b/m4/libevent_openssl.m4
@@ -0,0 +1,47 @@
+dnl ######################################################################
+dnl OpenSSL support
+AC_DEFUN([LIBEVENT_OPENSSL], [
+AC_REQUIRE([NTP_PKG_CONFIG])dnl
+
+case "$enable_openssl" in
+ yes)
+ have_openssl=no
+ case "$PKG_CONFIG" in
+ '')
+ ;;
+ *)
+ OPENSSL_LIBS=`$PKG_CONFIG --libs openssl 2>/dev/null`
+ case "$OPENSSL_LIBS" in
+ '') ;;
+ *) OPENSSL_LIBS="$OPENSSL_LIBS $EV_LIB_GDI $EV_LIB_WS32 $OPENSSL_LIBADD"
+ have_openssl=yes
+ ;;
+ esac
+ OPENSSL_INCS=`$PKG_CONFIG --cflags openssl 2>/dev/null`
+ ;;
+ esac
+ case "$have_openssl" in
+ yes) ;;
+ *)
+ save_LIBS="$LIBS"
+ LIBS=""
+ OPENSSL_LIBS=""
+ AC_SEARCH_LIBS([SSL_new], [ssl],
+ [have_openssl=yes
+ OPENSSL_LIBS="$LIBS -lcrypto $EV_LIB_GDI $EV_LIB_WS32 $OPENSSL_LIBADD"],
+ [have_openssl=no],
+ [-lcrypto $EV_LIB_GDI $EV_LIB_WS32 $OPENSSL_LIBADD])
+ LIBS="$save_LIBS"
+ ;;
+ esac
+ AC_SUBST(OPENSSL_INCS)
+ AC_SUBST(OPENSSL_LIBS)
+ case "$have_openssl" in
+ yes) AC_DEFINE(HAVE_OPENSSL, 1, [Define if the system has openssl]) ;;
+ esac
+ ;;
+esac
+
+# check if we have and should use openssl
+AM_CONDITIONAL(OPENSSL, [test "$enable_openssl" != "no" && test "$have_openssl" = "yes"])
+])
diff --git a/m4/ntp_pkg_config.m4 b/m4/ntp_pkg_config.m4
new file mode 100644
index 00000000..1bce8a6e
--- /dev/null
+++ b/m4/ntp_pkg_config.m4
@@ -0,0 +1,27 @@
+dnl NTP_PKG_CONFIG -*- Autoconf -*-
+dnl
+dnl Look for pkg-config, which must be at least
+dnl $ntp_pkgconfig_min_version.
+dnl
+AC_DEFUN([NTP_PKG_CONFIG], [
+
+dnl lower the minimum version if you find an earlier one works
+ntp_pkgconfig_min_version='0.15.0'
+AC_PATH_TOOL([PKG_CONFIG], [pkg-config])
+AS_UNSET([ac_cv_path_PKG_CONFIG])
+AS_UNSET([ac_cv_path_ac_pt_PKG_CONFIG])
+
+case "$PKG_CONFIG" in
+ /*)
+ AC_MSG_CHECKING([if pkg-config is at least version $ntp_pkgconfig_min_version])
+ if $PKG_CONFIG --atleast-pkgconfig-version $ntp_pkgconfig_min_version; then
+ AC_MSG_RESULT([yes])
+ else
+ AC_MSG_RESULT([no])
+ PKG_CONFIG=""
+ fi
+ ;;
+esac
+
+]) dnl NTP_PKG_CONFIG
+
diff --git a/make-event-config.sed b/make-event-config.sed
index b343e3b7..e31018a2 100644
--- a/make-event-config.sed
+++ b/make-event-config.sed
@@ -11,13 +11,13 @@
* Do not rely on macros in this file existing in later versions.\
*/\
\
-#ifndef _EVENT2_EVENT_CONFIG_H_\
-#define _EVENT2_EVENT_CONFIG_H_\
+#ifndef EVENT2_EVENT_CONFIG_H_INCLUDED_\
+#define EVENT2_EVENT_CONFIG_H_INCLUDED_\
$a\
\
#endif /* event2/event-config.h */
-s/#define /#define _EVENT_/
-s/#undef /#undef _EVENT_/
-s/#ifndef /#ifndef _EVENT_/
+s/#\( *\)define /#\1define EVENT__/
+s/#\( *\)undef /#\1undef EVENT__/
+s/#\( *\)if\(n*\)def /#\1if\2def EVENT__/
diff --git a/make_epoll_table.py b/make_epoll_table.py
new file mode 100755
index 00000000..1b15a91a
--- /dev/null
+++ b/make_epoll_table.py
@@ -0,0 +1,63 @@
+#!/usr/bin/python2
+
+def get(old,wc,rc,cc):
+ if ('xxx' in (rc, wc, cc)):
+ return "0",255
+
+ if ('add' in (rc, wc, cc)):
+ events = []
+ if rc == 'add' or (rc != 'del' and 'r' in old):
+ events.append("EPOLLIN")
+ if wc == 'add' or (wc != 'del' and 'w' in old):
+ events.append("EPOLLOUT")
+ if cc == 'add' or (cc != 'del' and 'c' in old):
+ events.append("EPOLLRDHUP")
+
+ if old == "0":
+ op = "EPOLL_CTL_ADD"
+ else:
+ op = "EPOLL_CTL_MOD"
+ return "|".join(events), op
+
+ if ('del' in (rc, wc, cc)):
+ delevents = []
+ modevents = []
+ op = "EPOLL_CTL_DEL"
+
+ if 'r' in old:
+ modevents.append("EPOLLIN")
+ if 'w' in old:
+ modevents.append("EPOLLOUT")
+ if 'c' in old:
+ modevents.append("EPOLLRDHUP")
+
+ for item, event in [(rc,"EPOLLIN"),
+ (wc,"EPOLLOUT"),
+ (cc,"EPOLLRDHUP")]:
+ if item == 'del':
+ delevents.append(event)
+ if event in modevents:
+ modevents.remove(event)
+
+ if modevents:
+ return "|".join(modevents), "EPOLL_CTL_MOD"
+ else:
+ return "|".join(delevents), "EPOLL_CTL_DEL"
+
+ return 0, 0
+
+
+def fmt(op, ev, old, wc, rc, cc):
+ entry = "{ %s, %s },"%(op, ev)
+ print "\t/* old=%3s, write:%3s, read:%3s, close:%3s */\n\t%s" % (
+ old, wc, rc, cc, entry)
+ return len(entry)
+
+for old in ('0','r','w','rw','c','cr','cw','crw'):
+ for wc in ('0', 'add', 'del', 'xxx'):
+ for rc in ('0', 'add', 'del', 'xxx'):
+ for cc in ('0', 'add', 'del', 'xxx'):
+
+ op,ev = get(old,wc,rc,cc)
+
+ fmt(op, ev, old, wc, rc, cc)
diff --git a/minheap-internal.h b/minheap-internal.h
index 6d8e80a2..b3b6f1fd 100644
--- a/minheap-internal.h
+++ b/minheap-internal.h
@@ -25,10 +25,11 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _MIN_HEAP_H_
-#define _MIN_HEAP_H_
+#ifndef MINHEAP_INTERNAL_H_INCLUDED_
+#define MINHEAP_INTERNAL_H_INCLUDED_
#include "event2/event-config.h"
+#include "evconfig-private.h"
#include "event2/event.h"
#include "event2/event_struct.h"
#include "event2/util.h"
@@ -41,42 +42,41 @@ typedef struct min_heap
unsigned n, a;
} min_heap_t;
-static inline void min_heap_ctor(min_heap_t* s);
-static inline void min_heap_dtor(min_heap_t* s);
-static inline void min_heap_elem_init(struct event* e);
-static inline int min_heap_elt_is_top(const struct event *e);
-static inline int min_heap_elem_greater(struct event *a, struct event *b);
-static inline int min_heap_empty(min_heap_t* s);
-static inline unsigned min_heap_size(min_heap_t* s);
-static inline struct event* min_heap_top(min_heap_t* s);
-static inline int min_heap_reserve(min_heap_t* s, unsigned n);
-static inline int min_heap_push(min_heap_t* s, struct event* e);
-static inline struct event* min_heap_pop(min_heap_t* s);
-static inline int min_heap_erase(min_heap_t* s, struct event* e);
+static inline void min_heap_ctor_(min_heap_t* s);
+static inline void min_heap_dtor_(min_heap_t* s);
+static inline void min_heap_elem_init_(struct event* e);
+static inline int min_heap_elt_is_top_(const struct event *e);
+static inline int min_heap_empty_(min_heap_t* s);
+static inline unsigned min_heap_size_(min_heap_t* s);
+static inline struct event* min_heap_top_(min_heap_t* s);
+static inline int min_heap_reserve_(min_heap_t* s, unsigned n);
+static inline int min_heap_push_(min_heap_t* s, struct event* e);
+static inline struct event* min_heap_pop_(min_heap_t* s);
+static inline int min_heap_adjust_(min_heap_t *s, struct event* e);
+static inline int min_heap_erase_(min_heap_t* s, struct event* e);
static inline void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e);
+static inline void min_heap_shift_up_unconditional_(min_heap_t* s, unsigned hole_index, struct event* e);
static inline void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e);
-int min_heap_elem_greater(struct event *a, struct event *b)
-{
- return evutil_timercmp(&a->ev_timeout, &b->ev_timeout, >);
-}
+#define min_heap_elem_greater(a, b) \
+ (evutil_timercmp(&(a)->ev_timeout, &(b)->ev_timeout, >))
-void min_heap_ctor(min_heap_t* s) { s->p = 0; s->n = 0; s->a = 0; }
-void min_heap_dtor(min_heap_t* s) { if (s->p) mm_free(s->p); }
-void min_heap_elem_init(struct event* e) { e->ev_timeout_pos.min_heap_idx = -1; }
-int min_heap_empty(min_heap_t* s) { return 0u == s->n; }
-unsigned min_heap_size(min_heap_t* s) { return s->n; }
-struct event* min_heap_top(min_heap_t* s) { return s->n ? *s->p : 0; }
+void min_heap_ctor_(min_heap_t* s) { s->p = 0; s->n = 0; s->a = 0; }
+void min_heap_dtor_(min_heap_t* s) { if (s->p) mm_free(s->p); }
+void min_heap_elem_init_(struct event* e) { e->ev_timeout_pos.min_heap_idx = -1; }
+int min_heap_empty_(min_heap_t* s) { return 0u == s->n; }
+unsigned min_heap_size_(min_heap_t* s) { return s->n; }
+struct event* min_heap_top_(min_heap_t* s) { return s->n ? *s->p : 0; }
-int min_heap_push(min_heap_t* s, struct event* e)
+int min_heap_push_(min_heap_t* s, struct event* e)
{
- if (min_heap_reserve(s, s->n + 1))
+ if (min_heap_reserve_(s, s->n + 1))
return -1;
min_heap_shift_up_(s, s->n++, e);
return 0;
}
-struct event* min_heap_pop(min_heap_t* s)
+struct event* min_heap_pop_(min_heap_t* s)
{
if (s->n)
{
@@ -88,12 +88,12 @@ struct event* min_heap_pop(min_heap_t* s)
return 0;
}
-int min_heap_elt_is_top(const struct event *e)
+int min_heap_elt_is_top_(const struct event *e)
{
return e->ev_timeout_pos.min_heap_idx == 0;
}
-int min_heap_erase(min_heap_t* s, struct event* e)
+int min_heap_erase_(min_heap_t* s, struct event* e)
{
if (-1 != e->ev_timeout_pos.min_heap_idx)
{
@@ -105,7 +105,7 @@ int min_heap_erase(min_heap_t* s, struct event* e)
to be less than the parent, it can't need to shift both up and
down. */
if (e->ev_timeout_pos.min_heap_idx > 0 && min_heap_elem_greater(s->p[parent], last))
- min_heap_shift_up_(s, e->ev_timeout_pos.min_heap_idx, last);
+ min_heap_shift_up_unconditional_(s, e->ev_timeout_pos.min_heap_idx, last);
else
min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, last);
e->ev_timeout_pos.min_heap_idx = -1;
@@ -114,7 +114,23 @@ int min_heap_erase(min_heap_t* s, struct event* e)
return -1;
}
-int min_heap_reserve(min_heap_t* s, unsigned n)
+int min_heap_adjust_(min_heap_t *s, struct event *e)
+{
+ if (-1 == e->ev_timeout_pos.min_heap_idx) {
+ return min_heap_push_(s, e);
+ } else {
+ unsigned parent = (e->ev_timeout_pos.min_heap_idx - 1) / 2;
+ /* The position of e has changed; we shift it up or down
+ * as needed. We can't need to do both. */
+ if (e->ev_timeout_pos.min_heap_idx > 0 && min_heap_elem_greater(s->p[parent], e))
+ min_heap_shift_up_unconditional_(s, e->ev_timeout_pos.min_heap_idx, e);
+ else
+ min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, e);
+ return 0;
+ }
+}
+
+int min_heap_reserve_(min_heap_t* s, unsigned n)
{
if (s->a < n)
{
@@ -130,6 +146,18 @@ int min_heap_reserve(min_heap_t* s, unsigned n)
return 0;
}
+void min_heap_shift_up_unconditional_(min_heap_t* s, unsigned hole_index, struct event* e)
+{
+ unsigned parent = (hole_index - 1) / 2;
+ do
+ {
+ (s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;
+ hole_index = parent;
+ parent = (hole_index - 1) / 2;
+ } while (hole_index && min_heap_elem_greater(s->p[parent], e));
+ (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
+}
+
void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e)
{
unsigned parent = (hole_index - 1) / 2;
@@ -157,4 +185,4 @@ void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e)
(s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
}
-#endif /* _MIN_HEAP_H_ */
+#endif /* MINHEAP_INTERNAL_H_INCLUDED_ */
diff --git a/mm-internal.h b/mm-internal.h
index b375155d..4ba6fce4 100644
--- a/mm-internal.h
+++ b/mm-internal.h
@@ -23,8 +23,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT_MM_INTERNAL_H
-#define _EVENT_MM_INTERNAL_H
+#ifndef MM_INTERNAL_H_INCLUDED_
+#define MM_INTERNAL_H_INCLUDED_
#include <sys/types.h>
@@ -32,13 +32,39 @@
extern "C" {
#endif
-#ifndef _EVENT_DISABLE_MM_REPLACEMENT
+#ifndef EVENT__DISABLE_MM_REPLACEMENT
/* Internal use only: Memory allocation functions. We give them nice short
* mm_names for our own use, but make sure that the symbols have longer names
* so they don't conflict with other libraries (like, say, libmm). */
+
+/** Allocate uninitialized memory.
+ *
+ * @return On success, return a pointer to sz newly allocated bytes.
+ * On failure, set errno to ENOMEM and return NULL.
+ * If the argument sz is 0, simply return NULL.
+ */
void *event_mm_malloc_(size_t sz);
+
+/** Allocate memory initialized to zero.
+ *
+ * @return On success, return a pointer to (count * size) newly allocated
+ * bytes, initialized to zero.
+ * On failure, or if the product would result in an integer overflow,
+ * set errno to ENOMEM and return NULL.
+ * If either arguments are 0, simply return NULL.
+ */
void *event_mm_calloc_(size_t count, size_t size);
-char *event_mm_strdup_(const char *s);
+
+/** Duplicate a string.
+ *
+ * @return On success, return a pointer to a newly allocated duplicate
+ * of a string.
+ * Set errno to ENOMEM and return NULL if a memory allocation error
+ * occurs (or would occur) in the process.
+ * If the argument str is NULL, set errno to EINVAL and return NULL.
+ */
+char *event_mm_strdup_(const char *str);
+
void *event_mm_realloc_(void *p, size_t sz);
void event_mm_free_(void *p);
#define mm_malloc(sz) event_mm_malloc_(sz)
diff --git a/poll.c b/poll.c
index 04d311bb..51475934 100644
--- a/poll.c
+++ b/poll.c
@@ -27,9 +27,12 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
+
+#ifdef EVENT__HAVE_POLL
#include <sys/types.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
@@ -48,6 +51,7 @@
#include "evmap-internal.h"
#include "event2/thread.h"
#include "evthread-internal.h"
+#include "time-internal.h"
struct pollidx {
int idxplus1;
@@ -63,8 +67,8 @@ struct pollop {
};
static void *poll_init(struct event_base *);
-static int poll_add(struct event_base *, int, short old, short events, void *_idx);
-static int poll_del(struct event_base *, int, short old, short events, void *_idx);
+static int poll_add(struct event_base *, int, short old, short events, void *idx);
+static int poll_del(struct event_base *, int, short old, short events, void *idx);
static int poll_dispatch(struct event_base *, struct timeval *);
static void poll_dealloc(struct event_base *);
@@ -88,7 +92,9 @@ poll_init(struct event_base *base)
if (!(pollop = mm_calloc(1, sizeof(struct pollop))))
return (NULL);
- evsig_init(base);
+ evsig_init_(base);
+
+ evutil_weakrand_seed_(&base->weakrand_seed, 0);
return (pollop);
}
@@ -127,7 +133,7 @@ poll_dispatch(struct event_base *base, struct timeval *tv)
nfds = pop->nfds;
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
if (base->th_base_lock) {
/* If we're using this backend in a multithreaded setting,
* then we need to work on a copy of event_set, so that we can
@@ -155,7 +161,7 @@ poll_dispatch(struct event_base *base, struct timeval *tv)
#endif
if (tv != NULL) {
- msec = evutil_tv_to_msec(tv);
+ msec = evutil_tv_to_msec_(tv);
if (msec < 0 || msec > INT_MAX)
msec = INT_MAX;
}
@@ -180,7 +186,7 @@ poll_dispatch(struct event_base *base, struct timeval *tv)
if (res == 0 || nfds == 0)
return (0);
- i = random() % nfds;
+ i = evutil_weakrand_range_(&base->weakrand_seed, nfds);
for (j = 0; j < nfds; j++) {
int what;
if (++i == nfds)
@@ -201,18 +207,18 @@ poll_dispatch(struct event_base *base, struct timeval *tv)
if (res == 0)
continue;
- evmap_io_active(base, event_set[i].fd, res);
+ evmap_io_active_(base, event_set[i].fd, res);
}
return (0);
}
static int
-poll_add(struct event_base *base, int fd, short old, short events, void *_idx)
+poll_add(struct event_base *base, int fd, short old, short events, void *idx_)
{
struct pollop *pop = base->evbase;
struct pollfd *pfd = NULL;
- struct pollidx *idx = _idx;
+ struct pollidx *idx = idx_;
int i;
EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
@@ -269,11 +275,11 @@ poll_add(struct event_base *base, int fd, short old, short events, void *_idx)
*/
static int
-poll_del(struct event_base *base, int fd, short old, short events, void *_idx)
+poll_del(struct event_base *base, int fd, short old, short events, void *idx_)
{
struct pollop *pop = base->evbase;
struct pollfd *pfd = NULL;
- struct pollidx *idx = _idx;
+ struct pollidx *idx = idx_;
int i;
EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
@@ -307,7 +313,7 @@ poll_del(struct event_base *base, int fd, short old, short events, void *_idx)
*/
memcpy(&pop->event_set[i], &pop->event_set[pop->nfds],
sizeof(struct pollfd));
- idx = evmap_io_get_fdinfo(&base->io, pop->event_set[i].fd);
+ idx = evmap_io_get_fdinfo_(&base->io, pop->event_set[i].fd);
EVUTIL_ASSERT(idx);
EVUTIL_ASSERT(idx->idxplus1 == pop->nfds + 1);
idx->idxplus1 = i + 1;
@@ -322,7 +328,7 @@ poll_dealloc(struct event_base *base)
{
struct pollop *pop = base->evbase;
- evsig_dealloc(base);
+ evsig_dealloc_(base);
if (pop->event_set)
mm_free(pop->event_set);
if (pop->event_set_copy)
@@ -331,3 +337,5 @@ poll_dealloc(struct event_base *base)
memset(pop, 0, sizeof(struct pollop));
mm_free(pop);
}
+
+#endif /* EVENT__HAVE_POLL */
diff --git a/ratelim-internal.h b/ratelim-internal.h
index 4e3f8c4c..6cc1cdde 100644
--- a/ratelim-internal.h
+++ b/ratelim-internal.h
@@ -23,8 +23,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _RATELIM_INTERNAL_H_
-#define _RATELIM_INTERNAL_H_
+#ifndef RATELIM_INTERNAL_H_INCLUDED_
+#define RATELIM_INTERNAL_H_INCLUDED_
#ifdef __cplusplus
extern "C" {
@@ -65,13 +65,13 @@ struct ev_token_bucket_cfg {
/** The current tick is 'current_tick': add bytes to 'bucket' as specified in
* 'cfg'. */
-int ev_token_bucket_update(struct ev_token_bucket *bucket,
+int ev_token_bucket_update_(struct ev_token_bucket *bucket,
const struct ev_token_bucket_cfg *cfg,
ev_uint32_t current_tick);
/** In which tick does 'tv' fall according to 'cfg'? Note that ticks can
* overflow easily; your code needs to handle this. */
-ev_uint32_t ev_token_bucket_get_tick(const struct timeval *tv,
+ev_uint32_t ev_token_bucket_get_tick_(const struct timeval *tv,
const struct ev_token_bucket_cfg *cfg);
/** Adjust 'bucket' to respect 'cfg', and note that it was last updated in
@@ -79,12 +79,12 @@ ev_uint32_t ev_token_bucket_get_tick(const struct timeval *tv,
* configuration of 'bucket'; otherwise, we are setting it up for the first
* time.
*/
-int ev_token_bucket_init(struct ev_token_bucket *bucket,
+int ev_token_bucket_init_(struct ev_token_bucket *bucket,
const struct ev_token_bucket_cfg *cfg,
ev_uint32_t current_tick,
int reinitialize);
-int bufferevent_remove_from_rate_limit_group_internal(struct bufferevent *bev,
+int bufferevent_remove_from_rate_limit_group_internal_(struct bufferevent *bev,
int unsuspend);
/** Decrease the read limit of 'b' by 'n' bytes */
diff --git a/sample/Makefile.am b/sample/Makefile.am
deleted file mode 100644
index c926f4e6..00000000
--- a/sample/Makefile.am
+++ /dev/null
@@ -1,29 +0,0 @@
-# sample/Makefile.am for libevent
-# Copyright 2000-2007 Niels Provos
-# Copyright 2007-2012 Niels Provos and Nick Mathewson
-#
-# See LICENSE for copying information.
-
-AUTOMAKE_OPTIONS = foreign no-dependencies
-
-LDADD = $(LIBEVENT_GC_SECTIONS) ../libevent.la
-AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/compat -I$(top_srcdir)/include -I../include
-
-noinst_PROGRAMS = event-test time-test signal-test dns-example hello-world http-server
-
-event_test_SOURCES = event-test.c
-time_test_SOURCES = time-test.c
-signal_test_SOURCES = signal-test.c
-dns_example_SOURCES = dns-example.c
-hello_world_SOURCES = hello-world.c
-http_server_SOURCES = http-server.c
-
-if OPENSSL
-noinst_PROGRAMS += le-proxy
-le_proxy_SOURCES = le-proxy.c
-le_proxy_LDADD = $(LDADD) ../libevent_openssl.la -lssl -lcrypto ${OPENSSL_LIBADD}
-endif
-
-verify:
-
-DISTCLEANFILES = *~
diff --git a/sample/dns-example.c b/sample/dns-example.c
index f1390335..15e48ce4 100644
--- a/sample/dns-example.c
+++ b/sample/dns-example.c
@@ -12,7 +12,7 @@
#include <sys/types.h>
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
@@ -26,7 +26,7 @@
#include <event2/dns_struct.h>
#include <event2/util.h>
-#ifdef _EVENT_HAVE_NETINET_IN6_H
+#ifdef EVENT__HAVE_NETINET_IN6_H
#include <netinet/in6.h>
#endif
@@ -171,7 +171,7 @@ main(int c, char **v) {
++idx;
}
-#ifdef WIN32
+#ifdef _WIN32
{
WSADATA WSAData;
WSAStartup(0x101, &WSAData);
@@ -179,7 +179,7 @@ main(int c, char **v) {
#endif
event_base = event_base_new();
- evdns_base = evdns_base_new(event_base, 0);
+ evdns_base = evdns_base_new(event_base, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
evdns_set_log_fn(logfn);
if (servertest) {
@@ -202,7 +202,7 @@ main(int c, char **v) {
}
if (idx < c) {
int res;
-#ifdef WIN32
+#ifdef _WIN32
if (resolv_conf == NULL)
res = evdns_base_config_windows_nameservers(evdns_base);
else
diff --git a/sample/event-test.c b/sample/event-read-fifo.c
index a1483e63..27b0b530 100644
--- a/sample/event-test.c
+++ b/sample/event-read-fifo.c
@@ -1,21 +1,23 @@
/*
- * XXX This sample code was once meant to show how to use the basic Libevent
- * interfaces, but it never worked on non-Unix platforms, and some of the
- * interfaces have changed since it was first written. It should probably
- * be removed or replaced with something better.
+ * This sample code shows how to use Libevent to read from a named pipe.
+ * XXX This code could make better use of the Libevent interfaces.
*
- * Compile with:
- * cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent
+ * XXX This does not work on Windows; ignore everything inside the _WIN32 block.
+ *
+ * On UNIX, compile with:
+ * cc -I/usr/local/include -o event-read-fifo event-read-fifo.c \
+ * -L/usr/local/lib -levent
*/
#include <event2/event-config.h>
#include <sys/types.h>
#include <sys/stat.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/queue.h>
#include <unistd.h>
#include <sys/time.h>
+#include <signal.h>
#else
#include <winsock2.h>
#include <windows.h>
@@ -26,7 +28,7 @@
#include <string.h>
#include <errno.h>
-#include <event.h>
+#include <event2/event.h>
static void
fifo_read(evutil_socket_t fd, short event, void *arg)
@@ -34,16 +36,13 @@ fifo_read(evutil_socket_t fd, short event, void *arg)
char buf[255];
int len;
struct event *ev = arg;
-#ifdef WIN32
+#ifdef _WIN32
DWORD dwBytesRead;
#endif
- /* Reschedule this event */
- event_add(ev, NULL);
-
fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n",
(int)fd, event, arg);
-#ifdef WIN32
+#ifdef _WIN32
len = ReadFile((HANDLE)fd, buf, sizeof(buf) - 1, &dwBytesRead, NULL);
/* Check for end of file. */
@@ -57,11 +56,13 @@ fifo_read(evutil_socket_t fd, short event, void *arg)
#else
len = read(fd, buf, sizeof(buf) - 1);
- if (len == -1) {
- perror("read");
- return;
- } else if (len == 0) {
- fprintf(stderr, "Connection closed\n");
+ if (len <= 0) {
+ if (len == -1)
+ perror("read");
+ else if (len == 0)
+ fprintf(stderr, "Connection closed\n");
+ event_del(ev);
+ event_base_loopbreak(event_get_base(ev));
return;
}
@@ -70,11 +71,22 @@ fifo_read(evutil_socket_t fd, short event, void *arg)
fprintf(stdout, "Read: %s\n", buf);
}
+/* On Unix, cleanup event.fifo if SIGINT is received. */
+#ifndef _WIN32
+static void
+signal_cb(evutil_socket_t fd, short event, void *arg)
+{
+ struct event_base *base = arg;
+ event_base_loopbreak(base);
+}
+#endif
+
int
main(int argc, char **argv)
{
- struct event evfifo;
-#ifdef WIN32
+ struct event *evfifo;
+ struct event_base* base;
+#ifdef _WIN32
HANDLE socket;
/* Open a file. */
socket = CreateFileA("test.txt", /* open File */
@@ -89,6 +101,7 @@ main(int argc, char **argv)
return 1;
#else
+ struct event *signal_int;
struct stat st;
const char *fifo = "event.fifo";
int socket;
@@ -107,12 +120,7 @@ main(int argc, char **argv)
exit(1);
}
- /* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */
-#ifdef __linux
- socket = open(fifo, O_RDWR | O_NONBLOCK, 0);
-#else
socket = open(fifo, O_RDONLY | O_NONBLOCK, 0);
-#endif
if (socket == -1) {
perror("open");
@@ -122,22 +130,33 @@ main(int argc, char **argv)
fprintf(stderr, "Write data to %s\n", fifo);
#endif
/* Initalize the event library */
- event_init();
+ base = event_base_new();
/* Initalize one event */
-#ifdef WIN32
- event_set(&evfifo, (evutil_socket_t)socket, EV_READ, fifo_read, &evfifo);
+#ifdef _WIN32
+ evfifo = event_new(base, (evutil_socket_t)socket, EV_READ|EV_PERSIST, fifo_read,
+ event_self_cbarg());
#else
- event_set(&evfifo, socket, EV_READ, fifo_read, &evfifo);
+ /* catch SIGINT so that event.fifo can be cleaned up */
+ signal_int = evsignal_new(base, SIGINT, signal_cb, base);
+ event_add(signal_int, NULL);
+
+ evfifo = event_new(base, socket, EV_READ|EV_PERSIST, fifo_read,
+ event_self_cbarg());
#endif
/* Add it to the active events, without a timeout */
- event_add(&evfifo, NULL);
+ event_add(evfifo, NULL);
- event_dispatch();
-#ifdef WIN32
+ event_base_dispatch(base);
+ event_base_free(base);
+#ifdef _WIN32
CloseHandle(socket);
+#else
+ close(socket);
+ unlink(fifo);
#endif
+ libevent_global_shutdown();
return (0);
}
diff --git a/sample/hello-world.c b/sample/hello-world.c
index d40af529..d3cf058a 100644
--- a/sample/hello-world.c
+++ b/sample/hello-world.c
@@ -11,7 +11,7 @@
#include <errno.h>
#include <stdio.h>
#include <signal.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <netinet/in.h>
# ifdef _XOPEN_SOURCE_EXTENDED
# include <arpa/inet.h>
@@ -43,7 +43,7 @@ main(int argc, char **argv)
struct event *signal_event;
struct sockaddr_in sin;
-#ifdef WIN32
+#ifdef _WIN32
WSADATA wsa_data;
WSAStartup(0x0201, &wsa_data);
#endif
diff --git a/sample/hostcheck.c b/sample/hostcheck.c
new file mode 100644
index 00000000..50709369
--- /dev/null
+++ b/sample/hostcheck.c
@@ -0,0 +1,217 @@
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+
+/* This file is an amalgamation of hostcheck.c and most of rawstr.c
+ from cURL. The contents of the COPYING file mentioned above are:
+
+COPYRIGHT AND PERMISSION NOTICE
+
+Copyright (c) 1996 - 2013, Daniel Stenberg, <daniel@haxx.se>.
+
+All rights reserved.
+
+Permission to use, copy, modify, and distribute this software for any purpose
+with or without fee is hereby granted, provided that the above copyright
+notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN
+NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
+OR OTHER DEALINGS IN THE SOFTWARE.
+
+Except as contained in this notice, the name of a copyright holder shall not
+be used in advertising or otherwise to promote the sale, use or other dealings
+in this Software without prior written authorization of the copyright holder.
+*/
+
+#include "hostcheck.h"
+#include <string.h>
+
+/* Portable, consistent toupper (remember EBCDIC). Do not use toupper() because
+ its behavior is altered by the current locale. */
+static char Curl_raw_toupper(char in)
+{
+ switch (in) {
+ case 'a':
+ return 'A';
+ case 'b':
+ return 'B';
+ case 'c':
+ return 'C';
+ case 'd':
+ return 'D';
+ case 'e':
+ return 'E';
+ case 'f':
+ return 'F';
+ case 'g':
+ return 'G';
+ case 'h':
+ return 'H';
+ case 'i':
+ return 'I';
+ case 'j':
+ return 'J';
+ case 'k':
+ return 'K';
+ case 'l':
+ return 'L';
+ case 'm':
+ return 'M';
+ case 'n':
+ return 'N';
+ case 'o':
+ return 'O';
+ case 'p':
+ return 'P';
+ case 'q':
+ return 'Q';
+ case 'r':
+ return 'R';
+ case 's':
+ return 'S';
+ case 't':
+ return 'T';
+ case 'u':
+ return 'U';
+ case 'v':
+ return 'V';
+ case 'w':
+ return 'W';
+ case 'x':
+ return 'X';
+ case 'y':
+ return 'Y';
+ case 'z':
+ return 'Z';
+ }
+ return in;
+}
+
+/*
+ * Curl_raw_equal() is for doing "raw" case insensitive strings. This is meant
+ * to be locale independent and only compare strings we know are safe for
+ * this. See http://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for
+ * some further explanation to why this function is necessary.
+ *
+ * The function is capable of comparing a-z case insensitively even for
+ * non-ascii.
+ */
+
+static int Curl_raw_equal(const char *first, const char *second)
+{
+ while(*first && *second) {
+ if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
+ /* get out of the loop as soon as they don't match */
+ break;
+ first++;
+ second++;
+ }
+ /* we do the comparison here (possibly again), just to make sure that if the
+ loop above is skipped because one of the strings reached zero, we must not
+ return this as a successful match */
+ return (Curl_raw_toupper(*first) == Curl_raw_toupper(*second));
+}
+
+static int Curl_raw_nequal(const char *first, const char *second, size_t max)
+{
+ while(*first && *second && max) {
+ if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) {
+ break;
+ }
+ max--;
+ first++;
+ second++;
+ }
+ if(0 == max)
+ return 1; /* they are equal this far */
+
+ return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
+}
+
+/*
+ * Match a hostname against a wildcard pattern.
+ * E.g.
+ * "foo.host.com" matches "*.host.com".
+ *
+ * We use the matching rule described in RFC6125, section 6.4.3.
+ * http://tools.ietf.org/html/rfc6125#section-6.4.3
+ */
+
+static int hostmatch(const char *hostname, const char *pattern)
+{
+ const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
+ int wildcard_enabled;
+ size_t prefixlen, suffixlen;
+ pattern_wildcard = strchr(pattern, '*');
+ if(pattern_wildcard == NULL)
+ return Curl_raw_equal(pattern, hostname) ?
+ CURL_HOST_MATCH : CURL_HOST_NOMATCH;
+
+ /* We require at least 2 dots in pattern to avoid too wide wildcard
+ match. */
+ wildcard_enabled = 1;
+ pattern_label_end = strchr(pattern, '.');
+ if(pattern_label_end == NULL || strchr(pattern_label_end+1, '.') == NULL ||
+ pattern_wildcard > pattern_label_end ||
+ Curl_raw_nequal(pattern, "xn--", 4)) {
+ wildcard_enabled = 0;
+ }
+ if(!wildcard_enabled)
+ return Curl_raw_equal(pattern, hostname) ?
+ CURL_HOST_MATCH : CURL_HOST_NOMATCH;
+
+ hostname_label_end = strchr(hostname, '.');
+ if(hostname_label_end == NULL ||
+ !Curl_raw_equal(pattern_label_end, hostname_label_end))
+ return CURL_HOST_NOMATCH;
+
+ /* The wildcard must match at least one character, so the left-most
+ label of the hostname is at least as large as the left-most label
+ of the pattern. */
+ if(hostname_label_end - hostname < pattern_label_end - pattern)
+ return CURL_HOST_NOMATCH;
+
+ prefixlen = pattern_wildcard - pattern;
+ suffixlen = pattern_label_end - (pattern_wildcard+1);
+ return Curl_raw_nequal(pattern, hostname, prefixlen) &&
+ Curl_raw_nequal(pattern_wildcard+1, hostname_label_end - suffixlen,
+ suffixlen) ?
+ CURL_HOST_MATCH : CURL_HOST_NOMATCH;
+}
+
+int Curl_cert_hostcheck(const char *match_pattern, const char *hostname)
+{
+ if(!match_pattern || !*match_pattern ||
+ !hostname || !*hostname) /* sanity check */
+ return 0;
+
+ if(Curl_raw_equal(hostname, match_pattern)) /* trivial case */
+ return 1;
+
+ if(hostmatch(hostname,match_pattern) == CURL_HOST_MATCH)
+ return 1;
+ return 0;
+}
diff --git a/sample/hostcheck.h b/sample/hostcheck.h
new file mode 100644
index 00000000..f40bc434
--- /dev/null
+++ b/sample/hostcheck.h
@@ -0,0 +1,30 @@
+#ifndef HEADER_CURL_HOSTCHECK_H
+#define HEADER_CURL_HOSTCHECK_H
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+
+#define CURL_HOST_NOMATCH 0
+#define CURL_HOST_MATCH 1
+int Curl_cert_hostcheck(const char *match_pattern, const char *hostname);
+
+#endif /* HEADER_CURL_HOSTCHECK_H */
+
diff --git a/sample/http-server.c b/sample/http-server.c
index a63f3e91..cbb9c914 100644
--- a/sample/http-server.c
+++ b/sample/http-server.c
@@ -6,6 +6,9 @@
*/
+/* Compatibility for possible missing IPv6 declarations */
+#include "../util-internal.h"
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -13,7 +16,7 @@
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
@@ -37,23 +40,30 @@
#include <event2/util.h>
#include <event2/keyvalq_struct.h>
-#ifdef _EVENT_HAVE_NETINET_IN_H
+#ifdef EVENT__HAVE_NETINET_IN_H
#include <netinet/in.h>
# ifdef _XOPEN_SOURCE_EXTENDED
# include <arpa/inet.h>
# endif
#endif
-/* Compatibility for possible missing IPv6 declarations */
-#include "../util-internal.h"
-
-#ifdef WIN32
+#ifdef _WIN32
+#ifndef stat
#define stat _stat
+#endif
+#ifndef fstat
#define fstat _fstat
+#endif
+#ifndef open
#define open _open
+#endif
+#ifndef close
#define close _close
+#endif
+#ifndef O_RDONLY
#define O_RDONLY _O_RDONLY
#endif
+#endif
char uri_root[512];
@@ -206,7 +216,7 @@ send_document_cb(struct evhttp_request *req, void *arg)
if (S_ISDIR(st.st_mode)) {
/* If it's a directory, read the comments and make a little
* index page */
-#ifdef WIN32
+#ifdef _WIN32
HANDLE d;
WIN32_FIND_DATAA ent;
char *pattern;
@@ -220,7 +230,7 @@ send_document_cb(struct evhttp_request *req, void *arg)
if (!strlen(path) || path[strlen(path)-1] != '/')
trailing_slash = "/";
-#ifdef WIN32
+#ifdef _WIN32
dirlen = strlen(whole_path);
pattern = malloc(dirlen+3);
memcpy(pattern, whole_path, dirlen);
@@ -236,18 +246,21 @@ send_document_cb(struct evhttp_request *req, void *arg)
goto err;
#endif
- evbuffer_add_printf(evb, "<html>\n <head>\n"
+ evbuffer_add_printf(evb,
+ "<!DOCTYPE html>\n"
+ "<html>\n <head>\n"
+ " <meta charset='utf-8'>\n"
" <title>%s</title>\n"
- " <base href='%s%s%s'>\n"
+ " <base href='%s%s'>\n"
" </head>\n"
" <body>\n"
" <h1>%s</h1>\n"
" <ul>\n",
decoded_path, /* XXX html-escape this. */
- uri_root, path, /* XXX html-escape this? */
+ path, /* XXX html-escape this? */
trailing_slash,
decoded_path /* XXX html-escape this */);
-#ifdef WIN32
+#ifdef _WIN32
do {
const char *name = ent.cFileName;
#else
@@ -257,13 +270,13 @@ send_document_cb(struct evhttp_request *req, void *arg)
evbuffer_add_printf(evb,
" <li><a href=\"%s\">%s</a>\n",
name, name);/* XXX escape this */
-#ifdef WIN32
+#ifdef _WIN32
} while (FindNextFileA(d, &ent));
#else
}
#endif
evbuffer_add_printf(evb, "</ul></body></html>\n");
-#ifdef WIN32
+#ifdef _WIN32
FindClose(d);
#else
closedir(d);
@@ -321,7 +334,7 @@ main(int argc, char **argv)
struct evhttp_bound_socket *handle;
unsigned short port = 0;
-#ifdef WIN32
+#ifdef _WIN32
WSADATA WSAData;
WSAStartup(0x101, &WSAData);
#else
diff --git a/sample/https-client.c b/sample/https-client.c
new file mode 100644
index 00000000..fbd5de8c
--- /dev/null
+++ b/sample/https-client.c
@@ -0,0 +1,439 @@
+/*
+ This is an example of how to hook up evhttp with bufferevent_ssl
+
+ It just GETs an https URL given on the command-line and prints the response
+ body to stdout.
+
+ Actually, it also accepts plain http URLs to make it easy to compare http vs
+ https code paths.
+
+ Loosely based on le-proxy.c.
+ */
+
+// Get rid of OSX 10.7 and greater deprecation warnings.
+#if defined(__APPLE__) && defined(__clang__)
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+#endif
+
+#include <stdio.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#ifdef _WIN32
+#include <winsock2.h>
+#include <ws2tcpip.h>
+
+#define snprintf _snprintf
+#define strcasecmp _stricmp
+#else
+#include <sys/socket.h>
+#include <netinet/in.h>
+#endif
+
+#include <event2/bufferevent_ssl.h>
+#include <event2/bufferevent.h>
+#include <event2/buffer.h>
+#include <event2/listener.h>
+#include <event2/util.h>
+#include <event2/http.h>
+
+#include <openssl/ssl.h>
+#include <openssl/err.h>
+#include <openssl/rand.h>
+
+#include "openssl_hostname_validation.h"
+
+static struct event_base *base;
+static int ignore_cert = 0;
+
+static void
+http_request_done(struct evhttp_request *req, void *ctx)
+{
+ char buffer[256];
+ int nread;
+
+ if (req == NULL) {
+ /* If req is NULL, it means an error occurred, but
+ * sadly we are mostly left guessing what the error
+ * might have been. We'll do our best... */
+ struct bufferevent *bev = (struct bufferevent *) ctx;
+ unsigned long oslerr;
+ int printed_err = 0;
+ int errcode = EVUTIL_SOCKET_ERROR();
+ fprintf(stderr, "some request failed - no idea which one though!\n");
+ /* Print out the OpenSSL error queue that libevent
+ * squirreled away for us, if any. */
+ while ((oslerr = bufferevent_get_openssl_error(bev))) {
+ ERR_error_string_n(oslerr, buffer, sizeof(buffer));
+ fprintf(stderr, "%s\n", buffer);
+ printed_err = 1;
+ }
+ /* If the OpenSSL error queue was empty, maybe it was a
+ * socket error; let's try printing that. */
+ if (! printed_err)
+ fprintf(stderr, "socket error = %s (%d)\n",
+ evutil_socket_error_to_string(errcode),
+ errcode);
+ return;
+ }
+
+ fprintf(stderr, "Response line: %d %s\n",
+ evhttp_request_get_response_code(req),
+ evhttp_request_get_response_code_line(req));
+
+ while ((nread = evbuffer_remove(evhttp_request_get_input_buffer(req),
+ buffer, sizeof(buffer)))
+ > 0) {
+ /* These are just arbitrary chunks of 256 bytes.
+ * They are not lines, so we can't treat them as such. */
+ fwrite(buffer, nread, 1, stdout);
+ }
+}
+
+static void
+syntax(void)
+{
+ fputs("Syntax:\n", stderr);
+ fputs(" https-client -url <https-url> [-data data-file.bin] [-ignore-cert] [-retries num]\n", stderr);
+ fputs("Example:\n", stderr);
+ fputs(" https-client -url https://ip.appspot.com/\n", stderr);
+
+ exit(1);
+}
+
+static void
+die(const char *msg)
+{
+ fputs(msg, stderr);
+ exit(1);
+}
+
+static void
+die_openssl(const char *func)
+{
+ fprintf (stderr, "%s failed:\n", func);
+
+ /* This is the OpenSSL function that prints the contents of the
+ * error stack to the specified file handle. */
+ ERR_print_errors_fp (stderr);
+
+ exit(1);
+}
+
+/* See http://archives.seul.org/libevent/users/Jan-2013/msg00039.html */
+static int cert_verify_callback(X509_STORE_CTX *x509_ctx, void *arg)
+{
+ char cert_str[256];
+ const char *host = (const char *) arg;
+ const char *res_str = "X509_verify_cert failed";
+ HostnameValidationResult res = Error;
+
+ /* This is the function that OpenSSL would call if we hadn't called
+ * SSL_CTX_set_cert_verify_callback(). Therefore, we are "wrapping"
+ * the default functionality, rather than replacing it. */
+ int ok_so_far = 0;
+
+ X509 *server_cert = NULL;
+
+ if (ignore_cert) {
+ return 1;
+ }
+
+ ok_so_far = X509_verify_cert(x509_ctx);
+
+ server_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
+
+ if (ok_so_far) {
+ res = validate_hostname(host, server_cert);
+
+ switch (res) {
+ case MatchFound:
+ res_str = "MatchFound";
+ break;
+ case MatchNotFound:
+ res_str = "MatchNotFound";
+ break;
+ case NoSANPresent:
+ res_str = "NoSANPresent";
+ break;
+ case MalformedCertificate:
+ res_str = "MalformedCertificate";
+ break;
+ case Error:
+ res_str = "Error";
+ break;
+ default:
+ res_str = "WTF!";
+ break;
+ }
+ }
+
+ X509_NAME_oneline(X509_get_subject_name (server_cert),
+ cert_str, sizeof (cert_str));
+
+ if (res == MatchFound) {
+ printf("https server '%s' has this certificate, "
+ "which looks good to me:\n%s\n",
+ host, cert_str);
+ return 1;
+ } else {
+ printf("Got '%s' for hostname '%s' and certificate:\n%s\n",
+ res_str, host, cert_str);
+ return 0;
+ }
+}
+
+int
+main(int argc, char **argv)
+{
+ int r;
+
+ struct evhttp_uri *http_uri;
+ const char *url = NULL, *data_file = NULL;
+ const char *scheme, *host, *path, *query;
+ char uri[256];
+ int port;
+ int retries = 0;
+
+ SSL_CTX *ssl_ctx;
+ SSL *ssl;
+ struct bufferevent *bev;
+ struct evhttp_connection *evcon;
+ struct evhttp_request *req;
+ struct evkeyvalq *output_headers;
+ struct evbuffer * output_buffer;
+
+ int i;
+
+ for (i = 1; i < argc; i++) {
+ if (!strcmp("-url", argv[i])) {
+ if (i < argc - 1) {
+ url = argv[i + 1];
+ } else {
+ syntax();
+ }
+ } else if (!strcmp("-ignore-cert", argv[i])) {
+ ignore_cert = 1;
+ } else if (!strcmp("-data", argv[i])) {
+ if (i < argc - 1) {
+ data_file = argv[i + 1];
+ } else {
+ syntax();
+ }
+ } else if (!strcmp("-retries", argv[i])) {
+ if (i < argc - 1) {
+ retries = atoi(argv[i + 1]);
+ } else {
+ syntax();
+ }
+ } else if (!strcmp("-help", argv[i])) {
+ syntax();
+ }
+ }
+
+ if (!url) {
+ syntax();
+ }
+
+#ifdef _WIN32
+ {
+ WORD wVersionRequested;
+ WSADATA wsaData;
+ int err;
+
+ wVersionRequested = MAKEWORD(2, 2);
+
+ err = WSAStartup(wVersionRequested, &wsaData);
+ if (err != 0) {
+ printf("WSAStartup failed with error: %d\n", err);
+ return 1;
+ }
+ }
+#endif // _WIN32
+
+ http_uri = evhttp_uri_parse(url);
+ if (http_uri == NULL) {
+ die("malformed url");
+ }
+
+ scheme = evhttp_uri_get_scheme(http_uri);
+ if (scheme == NULL || (strcasecmp(scheme, "https") != 0 &&
+ strcasecmp(scheme, "http") != 0)) {
+ die("url must be http or https");
+ }
+
+ host = evhttp_uri_get_host(http_uri);
+ if (host == NULL) {
+ die("url must have a host");
+ }
+
+ port = evhttp_uri_get_port(http_uri);
+ if (port == -1) {
+ port = (strcasecmp(scheme, "http") == 0) ? 80 : 443;
+ }
+
+ path = evhttp_uri_get_path(http_uri);
+ if (path == NULL) {
+ path = "/";
+ }
+
+ query = evhttp_uri_get_query(http_uri);
+ if (query == NULL) {
+ snprintf(uri, sizeof(uri) - 1, "%s", path);
+ } else {
+ snprintf(uri, sizeof(uri) - 1, "%s?%s", path, query);
+ }
+ uri[sizeof(uri) - 1] = '\0';
+
+ // Initialize OpenSSL
+ SSL_library_init();
+ ERR_load_crypto_strings();
+ SSL_load_error_strings();
+ OpenSSL_add_all_algorithms();
+
+ /* This isn't strictly necessary... OpenSSL performs RAND_poll
+ * automatically on first use of random number generator. */
+ r = RAND_poll();
+ if (r == 0) {
+ die_openssl("RAND_poll");
+ }
+
+ /* Create a new OpenSSL context */
+ ssl_ctx = SSL_CTX_new(SSLv23_method());
+ if (!ssl_ctx)
+ die_openssl("SSL_CTX_new");
+
+ #ifndef _WIN32
+ /* TODO: Add certificate loading on Windows as well */
+
+ /* Attempt to use the system's trusted root certificates.
+ * (This path is only valid for Debian-based systems.) */
+ if (1 != SSL_CTX_load_verify_locations(ssl_ctx,
+ "/etc/ssl/certs/ca-certificates.crt",
+ NULL))
+ die_openssl("SSL_CTX_load_verify_locations");
+ /* Ask OpenSSL to verify the server certificate. Note that this
+ * does NOT include verifying that the hostname is correct.
+ * So, by itself, this means anyone with any legitimate
+ * CA-issued certificate for any website, can impersonate any
+ * other website in the world. This is not good. See "The
+ * Most Dangerous Code in the World" article at
+ * https://crypto.stanford.edu/~dabo/pubs/abstracts/ssl-client-bugs.html
+ */
+ SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
+ /* This is how we solve the problem mentioned in the previous
+ * comment. We "wrap" OpenSSL's validation routine in our
+ * own routine, which also validates the hostname by calling
+ * the code provided by iSECPartners. Note that even though
+ * the "Everything You've Always Wanted to Know About
+ * Certificate Validation With OpenSSL (But Were Afraid to
+ * Ask)" paper from iSECPartners says very explicitly not to
+ * call SSL_CTX_set_cert_verify_callback (at the bottom of
+ * page 2), what we're doing here is safe because our
+ * cert_verify_callback() calls X509_verify_cert(), which is
+ * OpenSSL's built-in routine which would have been called if
+ * we hadn't set the callback. Therefore, we're just
+ * "wrapping" OpenSSL's routine, not replacing it. */
+ SSL_CTX_set_cert_verify_callback (ssl_ctx, cert_verify_callback,
+ (void *) host);
+ #endif // not _WIN32
+
+ // Create event base
+ base = event_base_new();
+ if (!base) {
+ perror("event_base_new()");
+ return 1;
+ }
+
+ // Create OpenSSL bufferevent and stack evhttp on top of it
+ ssl = SSL_new(ssl_ctx);
+ if (ssl == NULL) {
+ die_openssl("SSL_new()");
+ }
+
+ #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
+ // Set hostname for SNI extension
+ SSL_set_tlsext_host_name(ssl, host);
+ #endif
+
+ if (strcasecmp(scheme, "http") == 0) {
+ bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
+ } else {
+ bev = bufferevent_openssl_socket_new(base, -1, ssl,
+ BUFFEREVENT_SSL_CONNECTING,
+ BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
+ }
+
+ if (bev == NULL) {
+ fprintf(stderr, "bufferevent_openssl_socket_new() failed\n");
+ return 1;
+ }
+
+ bufferevent_openssl_set_allow_dirty_shutdown(bev, 1);
+
+ // For simplicity, we let DNS resolution block. Everything else should be
+ // asynchronous though.
+ evcon = evhttp_connection_base_bufferevent_new(base, NULL, bev,
+ host, port);
+ if (evcon == NULL) {
+ fprintf(stderr, "evhttp_connection_base_bufferevent_new() failed\n");
+ return 1;
+ }
+
+ if (retries > 0) {
+ evhttp_connection_set_retries(evcon, retries);
+ }
+
+ // Fire off the request
+ req = evhttp_request_new(http_request_done, bev);
+ if (req == NULL) {
+ fprintf(stderr, "evhttp_request_new() failed\n");
+ return 1;
+ }
+
+ output_headers = evhttp_request_get_output_headers(req);
+ evhttp_add_header(output_headers, "Host", host);
+ evhttp_add_header(output_headers, "Connection", "close");
+
+ if (data_file) {
+ /* NOTE: In production code, you'd probably want to use
+ * evbuffer_add_file() or evbuffer_add_file_segment(), to
+ * avoid needless copying. */
+ FILE * f = fopen(data_file, "rb");
+ char buf[1024];
+ size_t s;
+ size_t bytes = 0;
+
+ if (!f) {
+ syntax();
+ }
+
+ output_buffer = evhttp_request_get_output_buffer(req);
+ while ((s = fread(buf, 1, sizeof(buf), f)) > 0) {
+ evbuffer_add(output_buffer, buf, s);
+ bytes += s;
+ }
+ evutil_snprintf(buf, sizeof(buf)-1, "%lu", (unsigned long)bytes);
+ evhttp_add_header(output_headers, "Content-Length", buf);
+ fclose(f);
+ }
+
+ r = evhttp_make_request(evcon, req, data_file ? EVHTTP_REQ_POST : EVHTTP_REQ_GET, uri);
+ if (r != 0) {
+ fprintf(stderr, "evhttp_make_request() failed\n");
+ return 1;
+ }
+
+ event_base_dispatch(base);
+
+ evhttp_connection_free(evcon);
+ event_base_free(base);
+
+#ifdef _WIN32
+ WSACleanup();
+#endif
+
+ return 0;
+}
diff --git a/sample/include.am b/sample/include.am
new file mode 100644
index 00000000..75f87c70
--- /dev/null
+++ b/sample/include.am
@@ -0,0 +1,53 @@
+# sample/include.am for libevent
+# Copyright 2000-2007 Niels Provos
+# Copyright 2007-2012 Niels Provos and Nick Mathewson
+#
+# See LICENSE for copying information.
+
+SAMPLES = \
+ sample/dns-example \
+ sample/event-read-fifo \
+ sample/hello-world \
+ sample/http-server \
+ sample/signal-test \
+ sample/time-test
+
+if OPENSSL
+SAMPLES += sample/le-proxy
+sample_le_proxy_SOURCES = sample/le-proxy.c
+sample_le_proxy_LDADD = libevent.la libevent_openssl.la ${OPENSSL_LIBS} ${OPENSSL_LIBADD}
+sample_le_proxy_INCLUDES = $(OPENSSL_INCS)
+
+SAMPLES += sample/https-client
+sample_https_client_SOURCES = \
+ sample/https-client.c \
+ sample/hostcheck.c \
+ sample/openssl_hostname_validation.c
+sample_https_client_LDADD = libevent.la libevent_openssl.la ${OPENSSL_LIBS} ${OPENSSL_LIBADD}
+sample_https_client_INCLUDES = $(OPENSSL_INCS)
+noinst_HEADERS += \
+ sample/hostcheck.h \
+ sample/openssl_hostname_validation.h
+endif
+
+if BUILD_SAMPLES
+noinst_PROGRAMS += $(SAMPLES)
+endif
+
+$(SAMPLES) : libevent.la
+
+sample_event_read_fifo_SOURCES = sample/event-read-fifo.c
+sample_event_read_fifo_LDADD = $(LIBEVENT_GC_SECTIONS) libevent.la
+sample_time_test_SOURCES = sample/time-test.c
+sample_time_test_LDADD = $(LIBEVENT_GC_SECTIONS) libevent.la
+sample_signal_test_SOURCES = sample/signal-test.c
+sample_signal_test_LDADD = $(LIBEVENT_GC_SECTIONS) libevent.la
+sample_dns_example_SOURCES = sample/dns-example.c
+sample_dns_example_LDADD = $(LIBEVENT_GC_SECTIONS) libevent.la
+sample_hello_world_SOURCES = sample/hello-world.c
+sample_hello_world_LDADD = $(LIBEVENT_GC_SECTIONS) libevent.la
+sample_http_server_SOURCES = sample/http-server.c
+sample_http_server_LDADD = $(LIBEVENT_GC_SECTIONS) libevent.la
+
+
+
diff --git a/sample/le-proxy.c b/sample/le-proxy.c
index 446bfb56..30e0a5f6 100644
--- a/sample/le-proxy.c
+++ b/sample/le-proxy.c
@@ -5,13 +5,18 @@
XXX It's a little ugly and should probably be cleaned up.
*/
+// Get rid of OSX 10.7 and greater deprecation warnings.
+#if defined(__APPLE__) && defined(__clang__)
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+#endif
+
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
@@ -269,6 +274,11 @@ main(int argc, char **argv)
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_CLOSE_ON_EXEC|LEV_OPT_REUSEABLE,
-1, (struct sockaddr*)&listen_on_addr, socklen);
+ if (! listener) {
+ fprintf(stderr, "Couldn't open listener.\n");
+ event_base_free(base);
+ return 1;
+ }
event_base_dispatch(base);
evconnlistener_free(listener);
diff --git a/sample/openssl_hostname_validation.c b/sample/openssl_hostname_validation.c
new file mode 100644
index 00000000..b5adc67b
--- /dev/null
+++ b/sample/openssl_hostname_validation.c
@@ -0,0 +1,172 @@
+/* Obtained from: https://github.com/iSECPartners/ssl-conservatory */
+
+/*
+Copyright (C) 2012, iSEC Partners.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+ */
+
+/*
+ * Helper functions to perform basic hostname validation using OpenSSL.
+ *
+ * Please read "everything-you-wanted-to-know-about-openssl.pdf" before
+ * attempting to use this code. This whitepaper describes how the code works,
+ * how it should be used, and what its limitations are.
+ *
+ * Author: Alban Diquet
+ * License: See LICENSE
+ *
+ */
+
+// Get rid of OSX 10.7 and greater deprecation warnings.
+#if defined(__APPLE__) && defined(__clang__)
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+#endif
+
+#include <openssl/x509v3.h>
+#include <openssl/ssl.h>
+
+#include "openssl_hostname_validation.h"
+#include "hostcheck.h"
+
+#define HOSTNAME_MAX_SIZE 255
+
+/**
+* Tries to find a match for hostname in the certificate's Common Name field.
+*
+* Returns MatchFound if a match was found.
+* Returns MatchNotFound if no matches were found.
+* Returns MalformedCertificate if the Common Name had a NUL character embedded in it.
+* Returns Error if the Common Name could not be extracted.
+*/
+static HostnameValidationResult matches_common_name(const char *hostname, const X509 *server_cert) {
+ int common_name_loc = -1;
+ X509_NAME_ENTRY *common_name_entry = NULL;
+ ASN1_STRING *common_name_asn1 = NULL;
+ char *common_name_str = NULL;
+
+ // Find the position of the CN field in the Subject field of the certificate
+ common_name_loc = X509_NAME_get_index_by_NID(X509_get_subject_name((X509 *) server_cert), NID_commonName, -1);
+ if (common_name_loc < 0) {
+ return Error;
+ }
+
+ // Extract the CN field
+ common_name_entry = X509_NAME_get_entry(X509_get_subject_name((X509 *) server_cert), common_name_loc);
+ if (common_name_entry == NULL) {
+ return Error;
+ }
+
+ // Convert the CN field to a C string
+ common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry);
+ if (common_name_asn1 == NULL) {
+ return Error;
+ }
+ common_name_str = (char *) ASN1_STRING_data(common_name_asn1);
+
+ // Make sure there isn't an embedded NUL character in the CN
+ if ((size_t)ASN1_STRING_length(common_name_asn1) != strlen(common_name_str)) {
+ return MalformedCertificate;
+ }
+
+ // Compare expected hostname with the CN
+ if (Curl_cert_hostcheck(common_name_str, hostname) == CURL_HOST_MATCH) {
+ return MatchFound;
+ }
+ else {
+ return MatchNotFound;
+ }
+}
+
+
+/**
+* Tries to find a match for hostname in the certificate's Subject Alternative Name extension.
+*
+* Returns MatchFound if a match was found.
+* Returns MatchNotFound if no matches were found.
+* Returns MalformedCertificate if any of the hostnames had a NUL character embedded in it.
+* Returns NoSANPresent if the SAN extension was not present in the certificate.
+*/
+static HostnameValidationResult matches_subject_alternative_name(const char *hostname, const X509 *server_cert) {
+ HostnameValidationResult result = MatchNotFound;
+ int i;
+ int san_names_nb = -1;
+ STACK_OF(GENERAL_NAME) *san_names = NULL;
+
+ // Try to extract the names within the SAN extension from the certificate
+ san_names = X509_get_ext_d2i((X509 *) server_cert, NID_subject_alt_name, NULL, NULL);
+ if (san_names == NULL) {
+ return NoSANPresent;
+ }
+ san_names_nb = sk_GENERAL_NAME_num(san_names);
+
+ // Check each name within the extension
+ for (i=0; i<san_names_nb; i++) {
+ const GENERAL_NAME *current_name = sk_GENERAL_NAME_value(san_names, i);
+
+ if (current_name->type == GEN_DNS) {
+ // Current name is a DNS name, let's check it
+ char *dns_name = (char *) ASN1_STRING_data(current_name->d.dNSName);
+
+ // Make sure there isn't an embedded NUL character in the DNS name
+ if ((size_t)ASN1_STRING_length(current_name->d.dNSName) != strlen(dns_name)) {
+ result = MalformedCertificate;
+ break;
+ }
+ else { // Compare expected hostname with the DNS name
+ if (Curl_cert_hostcheck(dns_name, hostname)
+ == CURL_HOST_MATCH) {
+ result = MatchFound;
+ break;
+ }
+ }
+ }
+ }
+ sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
+
+ return result;
+}
+
+
+/**
+* Validates the server's identity by looking for the expected hostname in the
+* server's certificate. As described in RFC 6125, it first tries to find a match
+* in the Subject Alternative Name extension. If the extension is not present in
+* the certificate, it checks the Common Name instead.
+*
+* Returns MatchFound if a match was found.
+* Returns MatchNotFound if no matches were found.
+* Returns MalformedCertificate if any of the hostnames had a NUL character embedded in it.
+* Returns Error if there was an error.
+*/
+HostnameValidationResult validate_hostname(const char *hostname, const X509 *server_cert) {
+ HostnameValidationResult result;
+
+ if((hostname == NULL) || (server_cert == NULL))
+ return Error;
+
+ // First try the Subject Alternative Names extension
+ result = matches_subject_alternative_name(hostname, server_cert);
+ if (result == NoSANPresent) {
+ // Extension was not found: try the Common Name
+ result = matches_common_name(hostname, server_cert);
+ }
+
+ return result;
+}
diff --git a/sample/openssl_hostname_validation.h b/sample/openssl_hostname_validation.h
new file mode 100644
index 00000000..54aa1c43
--- /dev/null
+++ b/sample/openssl_hostname_validation.h
@@ -0,0 +1,56 @@
+/* Obtained from: https://github.com/iSECPartners/ssl-conservatory */
+
+/*
+Copyright (C) 2012, iSEC Partners.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+ */
+
+/*
+ * Helper functions to perform basic hostname validation using OpenSSL.
+ *
+ * Please read "everything-you-wanted-to-know-about-openssl.pdf" before
+ * attempting to use this code. This whitepaper describes how the code works,
+ * how it should be used, and what its limitations are.
+ *
+ * Author: Alban Diquet
+ * License: See LICENSE
+ *
+ */
+
+typedef enum {
+ MatchFound,
+ MatchNotFound,
+ NoSANPresent,
+ MalformedCertificate,
+ Error
+} HostnameValidationResult;
+
+/**
+* Validates the server's identity by looking for the expected hostname in the
+* server's certificate. As described in RFC 6125, it first tries to find a match
+* in the Subject Alternative Name extension. If the extension is not present in
+* the certificate, it checks the Common Name instead.
+*
+* Returns MatchFound if a match was found.
+* Returns MatchNotFound if no matches were found.
+* Returns MalformedCertificate if any of the hostnames had a NUL character embedded in it.
+* Returns Error if there was an error.
+*/
+HostnameValidationResult validate_hostname(const char *hostname, const X509 *server_cert);
diff --git a/sample/signal-test.c b/sample/signal-test.c
index f969e234..a61642f3 100644
--- a/sample/signal-test.c
+++ b/sample/signal-test.c
@@ -9,7 +9,7 @@
#include <event2/event-config.h>
#include <sys/stat.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/queue.h>
#include <unistd.h>
#include <sys/time.h>
@@ -24,10 +24,10 @@
#include <string.h>
#include <errno.h>
-#include <event.h>
+#include <event2/event.h>
-#ifdef _EVENT___func__
-#define __func__ _EVENT___func__
+#ifdef EVENT____func__
+#define __func__ EVENT____func__
#endif
int called = 0;
@@ -37,7 +37,7 @@ signal_cb(evutil_socket_t fd, short event, void *arg)
{
struct event *signal = arg;
- printf("%s: got signal %d\n", __func__, EVENT_SIGNAL(signal));
+ printf("%s: got signal %d\n", __func__, event_get_signal(signal));
if (called >= 2)
event_del(signal);
@@ -48,9 +48,9 @@ signal_cb(evutil_socket_t fd, short event, void *arg)
int
main(int argc, char **argv)
{
- struct event signal_int;
+ struct event *signal_int;
struct event_base* base;
-#ifdef WIN32
+#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
@@ -63,10 +63,9 @@ main(int argc, char **argv)
base = event_base_new();
/* Initalize one event */
- event_assign(&signal_int, base, SIGINT, EV_SIGNAL|EV_PERSIST, signal_cb,
- &signal_int);
+ signal_int = evsignal_new(base, SIGINT, signal_cb, event_self_cbarg());
- event_add(&signal_int, NULL);
+ event_add(signal_int, NULL);
event_base_dispatch(base);
event_base_free(base);
diff --git a/sample/time-test.c b/sample/time-test.c
index 77d4e206..c94c18a5 100644
--- a/sample/time-test.c
+++ b/sample/time-test.c
@@ -13,12 +13,12 @@
#include <event2/event-config.h>
#include <sys/stat.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/queue.h>
#include <unistd.h>
#endif
#include <time.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <fcntl.h>
@@ -31,7 +31,7 @@
#include <event2/event_struct.h>
#include <event2/util.h>
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#endif
@@ -71,7 +71,7 @@ main(int argc, char **argv)
struct event_base *base;
int flags;
-#ifdef WIN32
+#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
diff --git a/select.c b/select.c
index afba6d34..8ae53cc1 100644
--- a/select.c
+++ b/select.c
@@ -27,12 +27,21 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
+
+#ifdef EVENT__HAVE_SELECT
+
+#ifdef __APPLE__
+/* Apple wants us to define this if we might ever pass more than
+ * FD_SETSIZE bits to select(). */
+#define _DARWIN_UNLIMITED_SELECT
+#endif
#include <sys/types.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
-#ifdef _EVENT_HAVE_SYS_SELECT_H
+#ifdef EVENT__HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <sys/queue.h>
@@ -50,7 +59,7 @@
#include "log-internal.h"
#include "evmap-internal.h"
-#ifndef _EVENT_HAVE_FD_MASK
+#ifndef EVENT__HAVE_FD_MASK
/* This type is mandatory, but Android doesn't define it. */
typedef unsigned long fd_mask;
#endif
@@ -110,7 +119,9 @@ select_init(struct event_base *base)
return (NULL);
}
- evsig_init(base);
+ evsig_init_(base);
+
+ evutil_weakrand_seed_(&base->weakrand_seed, 0);
return (sop);
}
@@ -177,7 +188,7 @@ select_dispatch(struct event_base *base, struct timeval *tv)
event_debug(("%s: select reports %d", __func__, res));
check_selectop(sop);
- i = random() % nfds;
+ i = evutil_weakrand_range_(&base->weakrand_seed, nfds);
for (j = 0; j < nfds; ++j) {
if (++i >= nfds)
i = 0;
@@ -190,7 +201,7 @@ select_dispatch(struct event_base *base, struct timeval *tv)
if (res == 0)
continue;
- evmap_io_active(base, i, res);
+ evmap_io_active_(base, i, res);
}
check_selectop(sop);
@@ -327,7 +338,9 @@ select_free_selectop(struct selectop *sop)
static void
select_dealloc(struct event_base *base)
{
- evsig_dealloc(base);
+ evsig_dealloc_(base);
select_free_selectop(base->evbase);
}
+
+#endif /* EVENT__HAVE_SELECT */
diff --git a/signal.c b/signal.c
index 423f12a4..3f462950 100644
--- a/signal.c
+++ b/signal.c
@@ -27,30 +27,31 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#ifdef WIN32
+#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
#endif
#include <sys/types.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
-#ifdef _EVENT_HAVE_SYS_SOCKET_H
+#ifdef EVENT__HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
-#ifdef _EVENT_HAVE_FCNTL_H
+#ifdef EVENT__HAVE_FCNTL_H
#include <fcntl.h>
#endif
@@ -81,7 +82,7 @@
on Linux do a reasonable thing using signalfd.
*/
-#ifndef WIN32
+#ifndef _WIN32
/* Windows wants us to call our signal handlers as __cdecl. Nobody else
* expects you to do anything crazy like this. */
#define __cdecl
@@ -100,7 +101,7 @@ static const struct eventop evsigops = {
0, 0, 0
};
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
/* Lock for evsig_base and evsig_base_n_signals_added fields. */
static void *evsig_base_lock = NULL;
#endif
@@ -116,12 +117,12 @@ static void __cdecl evsig_handler(int sig);
#define EVSIGBASE_UNLOCK() EVLOCK_UNLOCK(evsig_base_lock, 0)
void
-evsig_set_base(struct event_base *base)
+evsig_set_base_(struct event_base *base)
{
EVSIGBASE_LOCK();
evsig_base = base;
evsig_base_n_signals_added = base->sig.ev_n_signals_added;
- evsig_base_fd = base->sig.ev_signal_pair[0];
+ evsig_base_fd = base->sig.ev_signal_pair[1];
EVSIGBASE_UNLOCK();
}
@@ -140,7 +141,11 @@ evsig_cb(evutil_socket_t fd, short what, void *arg)
memset(&ncaught, 0, sizeof(ncaught));
while (1) {
+#ifdef _WIN32
n = recv(fd, signals, sizeof(signals), 0);
+#else
+ n = read(fd, signals, sizeof(signals));
+#endif
if (n == -1) {
int err = evutil_socket_geterror(fd);
if (! EVUTIL_ERR_RW_RETRIABLE(err))
@@ -160,22 +165,21 @@ evsig_cb(evutil_socket_t fd, short what, void *arg)
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
for (i = 0; i < NSIG; ++i) {
if (ncaught[i])
- evmap_signal_active(base, i, ncaught[i]);
+ evmap_signal_active_(base, i, ncaught[i]);
}
EVBASE_RELEASE_LOCK(base, th_base_lock);
}
int
-evsig_init(struct event_base *base)
+evsig_init_(struct event_base *base)
{
/*
* Our signal handler is going to write to one end of the socket
* pair to wake up our event loop. The event loop then scans for
* signals that got delivered.
*/
- if (evutil_socketpair(
- AF_UNIX, SOCK_STREAM, 0, base->sig.ev_signal_pair) == -1) {
-#ifdef WIN32
+ if (evutil_make_internal_pipe_(base->sig.ev_signal_pair) == -1) {
+#ifdef _WIN32
/* Make this nonfatal on win32, where sometimes people
have localhost firewalled. */
event_sock_warn(-1, "%s: socketpair", __func__);
@@ -185,15 +189,13 @@ evsig_init(struct event_base *base)
return -1;
}
- evutil_make_socket_closeonexec(base->sig.ev_signal_pair[0]);
- evutil_make_socket_closeonexec(base->sig.ev_signal_pair[1]);
+ if (base->sig.sh_old) {
+ mm_free(base->sig.sh_old);
+ }
base->sig.sh_old = NULL;
base->sig.sh_old_max = 0;
- evutil_make_socket_nonblocking(base->sig.ev_signal_pair[0]);
- evutil_make_socket_nonblocking(base->sig.ev_signal_pair[1]);
-
- event_assign(&base->sig.ev_signal, base, base->sig.ev_signal_pair[1],
+ event_assign(&base->sig.ev_signal, base, base->sig.ev_signal_pair[0],
EV_READ | EV_PERSIST, evsig_cb, base);
base->sig.ev_signal.ev_flags |= EVLIST_INTERNAL;
@@ -207,10 +209,10 @@ evsig_init(struct event_base *base)
/* Helper: set the signal handler for evsignal to handler in base, so that
* we can restore the original handler when we clear the current one. */
int
-_evsig_set_handler(struct event_base *base,
+evsig_set_handler_(struct event_base *base,
int evsignal, void (__cdecl *handler)(int))
{
-#ifdef _EVENT_HAVE_SIGACTION
+#ifdef EVENT__HAVE_SIGACTION
struct sigaction sa;
#else
ev_sighandler_t sh;
@@ -247,7 +249,7 @@ _evsig_set_handler(struct event_base *base,
}
/* save previous handler and setup new handler */
-#ifdef _EVENT_HAVE_SIGACTION
+#ifdef EVENT__HAVE_SIGACTION
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handler;
sa.sa_flags |= SA_RESTART;
@@ -293,17 +295,17 @@ evsig_add(struct event_base *base, evutil_socket_t evsignal, short old, short ev
}
evsig_base = base;
evsig_base_n_signals_added = ++sig->ev_n_signals_added;
- evsig_base_fd = base->sig.ev_signal_pair[0];
+ evsig_base_fd = base->sig.ev_signal_pair[1];
EVSIGBASE_UNLOCK();
event_debug(("%s: %d: changing signal handler", __func__, (int)evsignal));
- if (_evsig_set_handler(base, (int)evsignal, evsig_handler) == -1) {
+ if (evsig_set_handler_(base, (int)evsignal, evsig_handler) == -1) {
goto err;
}
if (!sig->ev_signal_added) {
- if (event_add(&sig->ev_signal, NULL))
+ if (event_add_nolock_(&sig->ev_signal, NULL, 0))
goto err;
sig->ev_signal_added = 1;
}
@@ -319,20 +321,26 @@ err:
}
int
-_evsig_restore_handler(struct event_base *base, int evsignal)
+evsig_restore_handler_(struct event_base *base, int evsignal)
{
int ret = 0;
struct evsig_info *sig = &base->sig;
-#ifdef _EVENT_HAVE_SIGACTION
+#ifdef EVENT__HAVE_SIGACTION
struct sigaction *sh;
#else
ev_sighandler_t *sh;
#endif
+ if (evsignal >= sig->sh_old_max) {
+ /* Can't actually restore. */
+ /* XXXX.*/
+ return 0;
+ }
+
/* restore previous handler */
sh = sig->sh_old[evsignal];
sig->sh_old[evsignal] = NULL;
-#ifdef _EVENT_HAVE_SIGACTION
+#ifdef EVENT__HAVE_SIGACTION
if (sigaction(evsignal, sh, NULL) == -1) {
event_warn("sigaction");
ret = -1;
@@ -362,14 +370,14 @@ evsig_del(struct event_base *base, evutil_socket_t evsignal, short old, short ev
--base->sig.ev_n_signals_added;
EVSIGBASE_UNLOCK();
- return (_evsig_restore_handler(base, (int)evsignal));
+ return (evsig_restore_handler_(base, (int)evsignal));
}
static void __cdecl
evsig_handler(int sig)
{
int save_errno = errno;
-#ifdef WIN32
+#ifdef _WIN32
int socket_errno = EVUTIL_SOCKET_ERROR();
#endif
ev_uint8_t msg;
@@ -381,34 +389,41 @@ evsig_handler(int sig)
return;
}
-#ifndef _EVENT_HAVE_SIGACTION
+#ifndef EVENT__HAVE_SIGACTION
signal(sig, evsig_handler);
#endif
/* Wake up our notification mechanism */
msg = sig;
+#ifdef _WIN32
send(evsig_base_fd, (char*)&msg, 1, 0);
+#else
+ {
+ int r = write(evsig_base_fd, (char*)&msg, 1);
+ (void)r; /* Suppress 'unused return value' and 'unused var' */
+ }
+#endif
errno = save_errno;
-#ifdef WIN32
+#ifdef _WIN32
EVUTIL_SET_SOCKET_ERROR(socket_errno);
#endif
}
void
-evsig_dealloc(struct event_base *base)
+evsig_dealloc_(struct event_base *base)
{
int i = 0;
if (base->sig.ev_signal_added) {
event_del(&base->sig.ev_signal);
base->sig.ev_signal_added = 0;
}
- /* debug event is created in evsig_init/event_assign even when
+ /* debug event is created in evsig_init_/event_assign even when
* ev_signal_added == 0, so unassign is required */
event_debug_unassign(&base->sig.ev_signal);
for (i = 0; i < NSIG; ++i) {
if (i < base->sig.sh_old_max && base->sig.sh_old[i] != NULL)
- _evsig_restore_handler(base, i);
+ evsig_restore_handler_(base, i);
}
EVSIGBASE_LOCK();
if (base == evsig_base) {
@@ -435,11 +450,30 @@ evsig_dealloc(struct event_base *base)
}
}
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+static void
+evsig_free_globals_locks(void)
+{
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
+ if (evsig_base_lock != NULL) {
+ EVTHREAD_FREE_LOCK(evsig_base_lock, 0);
+ evsig_base_lock = NULL;
+ }
+#endif
+ return;
+}
+
+void
+evsig_free_globals_(void)
+{
+ evsig_free_globals_locks();
+}
+
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
int
evsig_global_setup_locks_(const int enable_locks)
{
EVTHREAD_SETUP_GLOBAL_LOCK(evsig_base_lock, 0);
return 0;
}
+
#endif
diff --git a/strlcpy-internal.h b/strlcpy-internal.h
index 1968c003..cfc27ec6 100644
--- a/strlcpy-internal.h
+++ b/strlcpy-internal.h
@@ -1,16 +1,17 @@
-#ifndef _STRLCPY_INTERNAL_H_
-#define _STRLCPY_INTERNAL_H_
+#ifndef STRLCPY_INTERNAL_H_INCLUDED_
+#define STRLCPY_INTERNAL_H_INCLUDED_
#ifdef __cplusplus
extern "C" {
#endif
#include "event2/event-config.h"
+#include "evconfig-private.h"
-#ifndef _EVENT_HAVE_STRLCPY
+#ifndef EVENT__HAVE_STRLCPY
#include <string.h>
-size_t _event_strlcpy(char *dst, const char *src, size_t siz);
-#define strlcpy _event_strlcpy
+size_t event_strlcpy_(char *dst, const char *src, size_t siz);
+#define strlcpy event_strlcpy_
#endif
#ifdef __cplusplus
diff --git a/strlcpy.c b/strlcpy.c
index 11053c33..3876475f 100644
--- a/strlcpy.c
+++ b/strlcpy.c
@@ -31,11 +31,12 @@
static char *rcsid = "$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $";
#endif /* LIBC_SCCS and not lint */
-#include <sys/types.h>
-
#include "event2/event-config.h"
+#include "evconfig-private.h"
+
+#include <sys/types.h>
-#ifndef _EVENT_HAVE_STRLCPY
+#ifndef EVENT__HAVE_STRLCPY
#include "strlcpy-internal.h"
/*
@@ -44,7 +45,7 @@ static char *rcsid = "$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp
* Returns strlen(src); if retval >= siz, truncation occurred.
*/
size_t
-_event_strlcpy(dst, src, siz)
+event_strlcpy_(dst, src, siz)
char *dst;
const char *src;
size_t siz;
diff --git a/test/Makefile.am b/test/Makefile.am
deleted file mode 100644
index 0253a491..00000000
--- a/test/Makefile.am
+++ /dev/null
@@ -1,105 +0,0 @@
-# test/Makefile.am for libevent
-# Copyright 2000-2007 Niels Provos
-# Copyright 2007-2012 Niels Provos and Nick Mathewson
-#
-# See LICENSE for copying information.
-
-AUTOMAKE_OPTIONS = foreign
-
-AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/compat -I$(top_srcdir)/include -I../include -DTINYTEST_LOCAL
-
-EXTRA_DIST = regress.rpc regress.gen.h regress.gen.c rpcgen_wrapper.sh test.sh
-
-noinst_PROGRAMS = test-init test-eof test-weof test-time \
- bench bench_cascade bench_http bench_httpclient test-ratelim \
- test-changelist
-if BUILD_REGRESS
-noinst_PROGRAMS += regress
-endif
-EXTRA_PROGRAMS = regress
-noinst_HEADERS = tinytest.h tinytest_macros.h regress.h tinytest_local.h
-
-# We need to copy this file, since automake doesn't want us to use top_srcdir
-# in TESTS.
-TESTS = test-script.sh
-
-test-script.sh: test.sh
- cp $< $@
-
-DISTCLEANFILES = test-script.sh
-
-BUILT_SOURCES =
-if BUILD_REGRESS
-BUILT_SOURCES += regress.gen.c regress.gen.h
-endif
-
-test_init_SOURCES = test-init.c
-test_init_LDADD = ../libevent_core.la
-test_eof_SOURCES = test-eof.c
-test_eof_LDADD = ../libevent_core.la
-test_changelist_SOURCES = test-changelist.c
-test_changelist_LDADD = ../libevent_core.la
-test_weof_SOURCES = test-weof.c
-test_weof_LDADD = ../libevent_core.la
-test_time_SOURCES = test-time.c
-test_time_LDADD = ../libevent_core.la
-test_ratelim_SOURCES = test-ratelim.c
-test_ratelim_LDADD = ../libevent_core.la -lm
-
-regress_SOURCES = regress.c regress_buffer.c regress_http.c regress_dns.c \
- regress_testutils.c regress_testutils.h \
- regress_rpc.c regress.gen.c regress.gen.h regress_et.c \
- regress_bufferevent.c regress_listener.c \
- regress_util.c tinytest.c regress_main.c regress_minheap.c \
- $(regress_thread_SOURCES) $(regress_zlib_SOURCES)
-if PTHREADS
-regress_thread_SOURCES = regress_thread.c
-PTHREAD_LIBS += ../libevent_pthreads.la
-endif
-if BUILD_WIN32
-regress_thread_SOURCES = regress_thread.c
-endif
-if ZLIB_REGRESS
-regress_zlib_SOURCES = regress_zlib.c
-endif
-if BUILD_WIN32
-regress_SOURCES += regress_iocp.c
-endif
-
-regress_LDADD = $(LIBEVENT_GC_SECTIONS) ../libevent.la $(PTHREAD_LIBS) $(ZLIB_LIBS)
-regress_CPPFLAGS = $(AM_CPPFLAGS) $(PTHREAD_CFLAGS) $(ZLIB_CFLAGS)
-regress_LDFLAGS = $(PTHREAD_CFLAGS)
-
-if OPENSSL
-regress_SOURCES += regress_ssl.c
-regress_LDADD += ../libevent_openssl.la -lssl -lcrypto ${OPENSSL_LIBADD}
-endif
-
-bench_SOURCES = bench.c
-bench_LDADD = $(LIBEVENT_GC_SECTIONS) ../libevent.la
-bench_cascade_SOURCES = bench_cascade.c
-bench_cascade_LDADD = $(LIBEVENT_GC_SECTIONS) ../libevent.la
-bench_http_SOURCES = bench_http.c
-bench_http_LDADD = $(LIBEVENT_GC_SECTIONS) ../libevent.la
-bench_httpclient_SOURCES = bench_httpclient.c
-bench_httpclient_LDADD = $(LIBEVENT_GC_SECTIONS) ../libevent_core.la
-
-regress.gen.c regress.gen.h: rpcgen-attempted
-
-rpcgen-attempted: $(srcdir)/regress.rpc $(srcdir)/../event_rpcgen.py $(srcdir)/rpcgen_wrapper.sh
- date -u > $@
- if $(srcdir)/rpcgen_wrapper.sh $(srcdir); then \
- echo "rpcgen okay"; \
- else \
- echo "No Python installed; stubbing out RPC test." >&2; \
- echo " "> regress.gen.c; \
- echo "#define NO_PYTHON_EXISTS" > regress.gen.h; \
- fi
-
-CLEANFILES = rpcgen-attempted
-
-DISTCLEANFILES += *~
-
-verify: check
-
-bench test-init test-eof test-weof test-time test-changelist: ../libevent.la
diff --git a/test/Makefile.nmake b/test/Makefile.nmake
index a899941f..30c3eb79 100644
--- a/test/Makefile.nmake
+++ b/test/Makefile.nmake
@@ -1,5 +1,16 @@
+# WATCH OUT! This makefile is a work in progress. -*- makefile -*-
-CFLAGS=/I.. /I../WIN32-Code /I../include /I../compat /DWIN32 /DHAVE_CONFIG_H /DTINYTEST_LOCAL
+!IFDEF OPENSSL_DIR
+SSL_CFLAGS=/I$(OPENSSL_DIR)\include /DEVENT__HAVE_OPENSSL
+SSL_OBJS=regress_ssl.obj
+SSL_LIBS=..\libevent_openssl.lib $(OPENSSL_DIR)\lib\libeay32.lib $(OPENSSL_DIR)\lib\ssleay32.lib gdi32.lib User32.lib
+!ELSE
+SSL_CFLAGS=
+SSL_OBJS=
+SSL_LIBS=
+!ENDIF
+
+CFLAGS=/I.. /I../WIN32-Code /I../WIN32-Code/nmake /I../include /I../compat /DHAVE_CONFIG_H /DTINYTEST_LOCAL $(SSL_CFLAGS)
CFLAGS=$(CFLAGS) /Ox /W3 /wd4996 /nologo
@@ -9,15 +20,17 @@ REGRESS_OBJS=regress.obj regress_buffer.obj regress_http.obj regress_dns.obj \
regress_et.obj regress_bufferevent.obj \
regress_listener.obj regress_util.obj tinytest.obj \
regress_main.obj regress_minheap.obj regress_iocp.obj \
- regress_thread.obj
+ regress_thread.obj regress_finalize.obj $(SSL_OBJS)
-OTHER_OBJS=test-init.obj test-eof.obj test-weof.obj test-time.obj \
+OTHER_OBJS=test-init.obj test-eof.obj test-closed.obj test-weof.obj test-time.obj \
bench.obj bench_cascade.obj bench_http.obj bench_httpclient.obj \
- test-changelist.obj
+ test-changelist.obj \
+ print-winsock-errors.obj
PROGRAMS=regress.exe \
- test-init.exe test-eof.exe test-weof.exe test-time.exe \
- test-changelist.exe
+ test-init.exe test-eof.exe test-closed.exe test-weof.exe test-time.exe \
+ test-changelist.exe \
+ print-winsock-errors.exe
# Disabled for now:
# bench.exe bench_cascade.exe bench_http.exe bench_httpclient.exe
@@ -28,12 +41,14 @@ LIBS=..\libevent.lib ws2_32.lib shell32.lib advapi32.lib
all: $(PROGRAMS)
regress.exe: $(REGRESS_OBJS)
- $(CC) $(CFLAGS) $(LIBS) $(REGRESS_OBJS)
+ $(CC) $(CFLAGS) $(LIBS) $(SSL_LIBS) $(REGRESS_OBJS)
test-init.exe: test-init.obj
$(CC) $(CFLAGS) $(LIBS) test-init.obj
test-eof.exe: test-eof.obj
$(CC) $(CFLAGS) $(LIBS) test-eof.obj
+test-closed.exe: test-closed.obj
+ $(CC) $(CFLAGS) $(LIBS) test-closed.obj
test-changelist.exe: test-changelist.obj
$(CC) $(CFLAGS) $(LIBS) test-changelist.obj
test-weof.exe: test-weof.obj
@@ -41,6 +56,9 @@ test-weof.exe: test-weof.obj
test-time.exe: test-time.obj
$(CC) $(CFLAGS) $(LIBS) test-time.obj
+print-winsock-errors.exe: print-winsock-errors.obj
+ $(CC) $(CFLAGS) $(LIBS) print-winsock-errors.obj
+
bench.exe: bench.obj
$(CC) $(CFLAGS) $(LIBS) bench.obj
bench_cascade.exe: bench_cascade.obj
@@ -58,4 +76,4 @@ regress.gen.c regress.gen.h: regress.rpc ../event_rpcgen.py
clean:
-del $(REGRESS_OBJS)
-del $(OTHER_OBJS)
- -del regress.exe
+ -del $(PROGRAMS)
diff --git a/test/bench.c b/test/bench.c
index 9ae02ea8..922a743a 100644
--- a/test/bench.c
+++ b/test/bench.c
@@ -37,10 +37,10 @@
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
-#ifdef WIN32
+#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
@@ -52,15 +52,19 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
+#ifdef _WIN32
+#include <getopt.h>
+#endif
+
#include <event.h>
#include <evutil.h>
-static int count, writes, fired;
+static int count, writes, fired, failures;
static evutil_socket_t *pipes;
static int num_pipes, num_active, num_writes;
static struct event *events;
@@ -71,12 +75,19 @@ read_cb(evutil_socket_t fd, short which, void *arg)
{
ev_intptr_t idx = (ev_intptr_t) arg, widx = idx + 1;
u_char ch;
+ ev_ssize_t n;
- count += recv(fd, (char*)&ch, sizeof(ch), 0);
+ n = recv(fd, (char*)&ch, sizeof(ch), 0);
+ if (n >= 0)
+ count += n;
+ else
+ failures++;
if (writes) {
if (widx >= num_pipes)
widx -= num_pipes;
- send(pipes[2 * widx + 1], "e", 1, 0);
+ n = send(pipes[2 * widx + 1], "e", 1, 0);
+ if (n != 1)
+ failures++;
writes--;
fired++;
}
@@ -102,7 +113,7 @@ run_once(void)
space = num_pipes / num_active;
space = space * 2;
for (i = 0; i < num_active; i++, fired++)
- send(pipes[i * space + 1], "e", 1, 0);
+ (void) send(pipes[i * space + 1], "e", 1, 0);
count = 0;
writes = num_writes;
@@ -125,14 +136,14 @@ run_once(void)
int
main(int argc, char **argv)
{
-#ifndef WIN32
+#ifdef HAVE_SETRLIMIT
struct rlimit rl;
#endif
int i, c;
struct timeval *tv;
evutil_socket_t *cp;
-#ifdef WIN32
+#ifdef _WIN32
WSADATA WSAData;
WSAStartup(0x101, &WSAData);
#endif
@@ -156,7 +167,7 @@ main(int argc, char **argv)
}
}
-#ifndef WIN32
+#ifdef HAVE_SETRLIMIT
rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
perror("setrlimit");
diff --git a/test/bench_cascade.c b/test/bench_cascade.c
index c9c45c7c..2d85cc1f 100644
--- a/test/bench_cascade.c
+++ b/test/bench_cascade.c
@@ -29,10 +29,10 @@
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
-#ifdef WIN32
+#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
@@ -44,11 +44,11 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
-
+#include <getopt.h>
#include <event.h>
#include <evutil.h>
@@ -69,7 +69,7 @@ read_cb(evutil_socket_t fd, short which, void *arg)
char ch;
evutil_socket_t sock = (evutil_socket_t)(ev_intptr_t)arg;
- recv(fd, &ch, sizeof(ch), 0);
+ (void) recv(fd, &ch, sizeof(ch), 0);
if (sock >= 0) {
if (send(sock, "e", 1, 0) < 0)
perror("send");
@@ -84,8 +84,8 @@ run_once(int num_pipes)
evutil_socket_t *cp;
static struct timeval ts, te, tv_timeout;
- events = calloc(num_pipes, sizeof(struct event));
- pipes = calloc(num_pipes * 2, sizeof(evutil_socket_t));
+ events = (struct event *)calloc(num_pipes, sizeof(struct event));
+ pipes = (evutil_socket_t *)calloc(num_pipes * 2, sizeof(evutil_socket_t));
if (events == NULL || pipes == NULL) {
perror("malloc");
@@ -126,8 +126,8 @@ run_once(int num_pipes)
for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
event_del(&events[i]);
- close(cp[0]);
- close(cp[1]);
+ evutil_closesocket(cp[0]);
+ evutil_closesocket(cp[1]);
}
free(pipes);
@@ -139,13 +139,18 @@ run_once(int num_pipes)
int
main(int argc, char **argv)
{
-#ifndef WIN32
+#ifdef HAVE_SETRLIMIT
struct rlimit rl;
#endif
int i, c;
struct timeval *tv;
int num_pipes = 100;
+#ifdef _WIN32
+ WSADATA WSAData;
+ WSAStartup(0x101, &WSAData);
+#endif
+
while ((c = getopt(argc, argv, "n:")) != -1) {
switch (c) {
case 'n':
@@ -157,7 +162,7 @@ main(int argc, char **argv)
}
}
-#ifndef WIN32
+#ifdef HAVE_SETRLIMIT
rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
perror("setrlimit");
@@ -175,5 +180,9 @@ main(int argc, char **argv)
tv->tv_sec * 1000000L + tv->tv_usec);
}
+#ifdef _WIN32
+ WSACleanup();
+#endif
+
exit(0);
}
diff --git a/test/bench_http.c b/test/bench_http.c
index f6fd81b2..6d0d9717 100644
--- a/test/bench_http.c
+++ b/test/bench_http.c
@@ -27,7 +27,7 @@
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
@@ -93,7 +93,7 @@ main(int argc, char **argv)
unsigned short port = 8080;
char *endptr = NULL;
-#ifdef WIN32
+#ifdef _WIN32
WSADATA WSAData;
WSAStartup(0x101, &WSAData);
#else
@@ -135,7 +135,7 @@ main(int argc, char **argv)
exit(1);
}
break;
-#ifdef WIN32
+#ifdef _WIN32
case 'i':
use_iocp = 1;
evthread_use_windows_threads();
@@ -169,10 +169,8 @@ main(int argc, char **argv)
evhttp_set_cb(http, "/ind", http_basic_cb, NULL);
fprintf(stderr, "/ind - basic content (memory copy)\n");
-#ifdef _EVENT2_EVENT_H_
evhttp_set_cb(http, "/ref", http_ref_cb, NULL);
fprintf(stderr, "/ref - basic content (reference)\n");
-#endif
fprintf(stderr, "Serving %d bytes on port %d using %s\n",
(int)content_len, port,
@@ -180,12 +178,18 @@ main(int argc, char **argv)
evhttp_bind_socket(http, "0.0.0.0", port);
+#ifdef _WIN32
if (use_iocp) {
struct timeval tv={99999999,0};
event_base_loopexit(base, &tv);
}
+#endif
event_base_dispatch(base);
+#ifdef _WIN32
+ WSACleanup();
+#endif
+
/* NOTREACHED */
return (0);
}
diff --git a/test/bench_httpclient.c b/test/bench_httpclient.c
index 581b23da..22f174da 100644
--- a/test/bench_httpclient.c
+++ b/test/bench_httpclient.c
@@ -25,8 +25,11 @@
*
*/
+/* for EVUTIL_ERR_CONNECT_RETRIABLE macro */
+#include "util-internal.h"
+
#include <sys/types.h>
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
@@ -44,9 +47,6 @@
#include "event2/buffer.h"
#include "event2/util.h"
-/* for EVUTIL_ERR_CONNECT_RETRIABLE macro */
-#include "util-internal.h"
-
const char *resource = NULL;
struct event_base *base = NULL;
@@ -113,14 +113,18 @@ errorcb(struct bufferevent *b, short what, void *arg)
static void
frob_socket(evutil_socket_t sock)
{
+#ifdef HAVE_SO_LINGER
struct linger l;
+#endif
int one = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&one, sizeof(one))<0)
perror("setsockopt(SO_REUSEADDR)");
+#ifdef HAVE_SO_LINGER
l.l_onoff = 1;
l.l_linger = 0;
if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (void*)&l, sizeof(l))<0)
perror("setsockopt(SO_LINGER)");
+#endif
}
static int
@@ -141,12 +145,15 @@ launch_request(void)
sin.sin_port = htons(8080);
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return -1;
- if (evutil_make_socket_nonblocking(sock) < 0)
+ if (evutil_make_socket_nonblocking(sock) < 0) {
+ evutil_closesocket(sock);
return -1;
+ }
frob_socket(sock);
if (connect(sock, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
int e = errno;
if (! EVUTIL_ERR_CONNECT_RETRIABLE(e)) {
+ evutil_closesocket(sock);
return -1;
}
}
@@ -191,7 +198,7 @@ main(int argc, char **argv)
evutil_gettimeofday(&end, NULL);
evutil_timersub(&end, &start, &total);
- usec = total_time.tv_sec * 1000000 + total_time.tv_usec;
+ usec = total_time.tv_sec * (long long)1000000 + total_time.tv_usec;
if (!total_n_handled) {
puts("Nothing worked. You probably did something dumb.");
@@ -202,7 +209,7 @@ main(int argc, char **argv)
throughput = total_n_handled /
(total.tv_sec+ ((double)total.tv_usec)/1000000.0);
-#ifdef WIN32
+#ifdef _WIN32
#define I64_FMT "%I64d"
#define I64_TYP __int64
#else
diff --git a/test/check-dumpevents.py b/test/check-dumpevents.py
new file mode 100755
index 00000000..16fe9bc9
--- /dev/null
+++ b/test/check-dumpevents.py
@@ -0,0 +1,54 @@
+#!/usr/bin/python2
+#
+# Post-process the output of test-dumpevents and check it for correctness.
+#
+
+import math
+import re
+import sys
+
+text = sys.stdin.readlines()
+
+try:
+ expect_inserted_pos = text.index("Inserted:\n")
+ expect_active_pos = text.index("Active:\n")
+ got_inserted_pos = text.index("Inserted events:\n")
+ got_active_pos = text.index("Active events:\n")
+except ValueError:
+ print >>sys.stderr, "Missing expected dividing line in dumpevents output"
+ sys.exit(1)
+
+if not (expect_inserted_pos < expect_active_pos <
+ got_inserted_pos < got_active_pos):
+ print >>sys.stderr, "Sections out of order in dumpevents output"
+ sys.exit(1)
+
+now,T= text[1].split()
+T = float(T)
+
+want_inserted = set(text[expect_inserted_pos+1:expect_active_pos])
+want_active = set(text[expect_active_pos+1:got_inserted_pos-1])
+got_inserted = set(text[got_inserted_pos+1:got_active_pos])
+got_active = set(text[got_active_pos+1:])
+
+pat = re.compile(r'Timeout=([0-9\.]+)')
+def replace_time(m):
+ t = float(m.group(1))
+ if .9 < abs(t-T) < 1.1:
+ return "Timeout=T+1"
+ elif 2.4 < abs(t-T) < 2.6:
+ return "Timeout=T+2.5"
+ else:
+ return m.group(0)
+
+cleaned_inserted = set( pat.sub(replace_time, s) for s in got_inserted
+ if "Internal" not in s)
+
+if cleaned_inserted != want_inserted:
+ print >>sys.stderr, "Inserted event lists were not as expected!"
+ sys.exit(1)
+
+if set(got_active) != set(want_active):
+ print >>sys.stderr, "Active event lists were not as expected!"
+ sys.exit(1)
+
diff --git a/test/include.am b/test/include.am
new file mode 100644
index 00000000..4cd49ef6
--- /dev/null
+++ b/test/include.am
@@ -0,0 +1,146 @@
+# test/Makefile.am for libevent
+# Copyright 2000-2007 Niels Provos
+# Copyright 2007-2012 Niels Provos and Nick Mathewson
+#
+# See LICENSE for copying information.
+
+regress_CPPFLAGS = -DTINYTEST_LOCAL
+
+EXTRA_DIST+= \
+ test/check-dumpevents.py \
+ test/regress.gen.c \
+ test/regress.gen.h \
+ test/regress.rpc \
+ test/rpcgen_wrapper.sh \
+ test/test.sh
+
+TESTPROGRAMS = \
+ test/bench \
+ test/bench_cascade \
+ test/bench_http \
+ test/bench_httpclient \
+ test/test-changelist \
+ test/test-dumpevents \
+ test/test-eof \
+ test/test-closed \
+ test/test-fdleak \
+ test/test-init \
+ test/test-ratelim \
+ test/test-time \
+ test/test-weof \
+ test/regress
+
+if BUILD_REGRESS
+noinst_PROGRAMS += $(TESTPROGRAMS)
+EXTRA_PROGRAMS+= test/regress
+endif
+
+noinst_HEADERS+= \
+ test/regress.h \
+ test/regress_thread.h \
+ test/tinytest.h \
+ test/tinytest_local.h \
+ test/tinytest_macros.h
+
+# We need to copy this file, since automake doesn't want us to use top_srcdir
+# in TESTS.
+TESTS = test/test-script.sh
+
+test/test-script.sh: test/test.sh
+ cp $(top_srcdir)/test/test.sh $@
+
+DISTCLEANFILES += test/test-script.sh
+DISTCLEANFILES += test/regress.gen.c test/regress.gen.h
+
+if BUILD_REGRESS
+BUILT_SOURCES += test/regress.gen.c test/regress.gen.h
+endif
+
+test_test_init_SOURCES = test/test-init.c
+test_test_init_LDADD = libevent_core.la
+test_test_dumpevents_SOURCES = test/test-dumpevents.c
+test_test_dumpevents_LDADD = libevent_core.la
+test_test_eof_SOURCES = test/test-eof.c
+test_test_eof_LDADD = libevent_core.la
+test_test_closed_SOURCES = test/test-closed.c
+test_test_closed_LDADD = libevent_core.la
+test_test_changelist_SOURCES = test/test-changelist.c
+test_test_changelist_LDADD = libevent_core.la
+test_test_weof_SOURCES = test/test-weof.c
+test_test_weof_LDADD = libevent_core.la
+test_test_time_SOURCES = test/test-time.c
+test_test_time_LDADD = libevent_core.la
+test_test_ratelim_SOURCES = test/test-ratelim.c
+test_test_ratelim_LDADD = libevent_core.la -lm
+test_test_fdleak_SOURCES = test/test-fdleak.c
+test_test_fdleak_LDADD = libevent_core.la
+
+test_regress_SOURCES = \
+ test/regress.c \
+ test/regress.gen.c \
+ test/regress.gen.h \
+ test/regress_buffer.c \
+ test/regress_bufferevent.c \
+ test/regress_dns.c \
+ test/regress_et.c \
+ test/regress_finalize.c \
+ test/regress_http.c \
+ test/regress_listener.c \
+ test/regress_main.c \
+ test/regress_minheap.c \
+ test/regress_rpc.c \
+ test/regress_testutils.c \
+ test/regress_testutils.h \
+ test/regress_util.c \
+ test/tinytest.c \
+ $(regress_thread_SOURCES) \
+ $(regress_zlib_SOURCES)
+
+if PTHREADS
+regress_thread_SOURCES = test/regress_thread.c
+PTHREAD_LIBS += libevent_pthreads.la
+endif
+if BUILD_WIN32
+regress_thread_SOURCES = test/regress_thread.c
+endif
+if ZLIB_REGRESS
+regress_zlib_SOURCES = test/regress_zlib.c
+endif
+if BUILD_WIN32
+test_regress_SOURCES += test/regress_iocp.c
+endif
+
+test_regress_LDADD = $(LIBEVENT_GC_SECTIONS) libevent.la $(PTHREAD_LIBS) $(ZLIB_LIBS)
+test_regress_CPPFLAGS = $(AM_CPPFLAGS) $(PTHREAD_CFLAGS) $(ZLIB_CFLAGS) -Itest
+test_regress_LDFLAGS = $(PTHREAD_CFLAGS)
+
+if OPENSSL
+test_regress_SOURCES += test/regress_ssl.c
+test_regress_CPPFLAGS += $(OPENSSL_INCS)
+test_regress_LDADD += libevent_openssl.la $(OPENSSL_LIBS) ${OPENSSL_LIBADD}
+endif
+
+test_bench_SOURCES = test/bench.c
+test_bench_LDADD = $(LIBEVENT_GC_SECTIONS) libevent.la
+test_bench_cascade_SOURCES = test/bench_cascade.c
+test_bench_cascade_LDADD = $(LIBEVENT_GC_SECTIONS) libevent.la
+test_bench_http_SOURCES = test/bench_http.c
+test_bench_http_LDADD = $(LIBEVENT_GC_SECTIONS) libevent.la
+test_bench_httpclient_SOURCES = test/bench_httpclient.c
+test_bench_httpclient_LDADD = $(LIBEVENT_GC_SECTIONS) libevent_core.la
+
+test/regress.gen.c test/regress.gen.h: test/rpcgen-attempted
+
+test/rpcgen-attempted: test/regress.rpc event_rpcgen.py test/rpcgen_wrapper.sh
+ $(AM_V_GEN)date -u > $@
+ $(AM_V_at)if $(srcdir)/test/rpcgen_wrapper.sh $(srcdir)/test; then \
+ true; \
+ else \
+ echo "No Python installed; stubbing out RPC test." >&2; \
+ echo " "> test/regress.gen.c; \
+ echo "#define NO_PYTHON_EXISTS" > test/regress.gen.h; \
+ fi
+
+CLEANFILES += test/rpcgen-attempted
+
+$(TESTPROGRAMS) : libevent.la
diff --git a/test/print-winsock-errors.c b/test/print-winsock-errors.c
new file mode 100644
index 00000000..ab6e610e
--- /dev/null
+++ b/test/print-winsock-errors.c
@@ -0,0 +1,84 @@
+#include <winsock2.h>
+#include <windows.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "event2/event.h"
+#include "event2/util.h"
+#include "event2/thread.h"
+
+#define E(x) printf (#x " -> \"%s\"\n", evutil_socket_error_to_string (x));
+
+int main (int argc, char **argv)
+{
+ int i, j;
+ const char *s1, *s2;
+
+ evthread_use_windows_threads ();
+
+ s1 = evutil_socket_error_to_string (WSAEINTR);
+
+ for (i = 0; i < 3; i++) {
+ printf ("\niteration %d:\n\n", i);
+ E(WSAEINTR);
+ E(WSAEACCES);
+ E(WSAEFAULT);
+ E(WSAEINVAL);
+ E(WSAEMFILE);
+ E(WSAEWOULDBLOCK);
+ E(WSAEINPROGRESS);
+ E(WSAEALREADY);
+ E(WSAENOTSOCK);
+ E(WSAEDESTADDRREQ);
+ E(WSAEMSGSIZE);
+ E(WSAEPROTOTYPE);
+ E(WSAENOPROTOOPT);
+ E(WSAEPROTONOSUPPORT);
+ E(WSAESOCKTNOSUPPORT);
+ E(WSAEOPNOTSUPP);
+ E(WSAEPFNOSUPPORT);
+ E(WSAEAFNOSUPPORT);
+ E(WSAEADDRINUSE);
+ E(WSAEADDRNOTAVAIL);
+ E(WSAENETDOWN);
+ E(WSAENETUNREACH);
+ E(WSAENETRESET);
+ E(WSAECONNABORTED);
+ E(WSAECONNRESET);
+ E(WSAENOBUFS);
+ E(WSAEISCONN);
+ E(WSAENOTCONN);
+ E(WSAESHUTDOWN);
+ E(WSAETIMEDOUT);
+ E(WSAECONNREFUSED);
+ E(WSAEHOSTDOWN);
+ E(WSAEHOSTUNREACH);
+ E(WSAEPROCLIM);
+ E(WSASYSNOTREADY);
+ E(WSAVERNOTSUPPORTED);
+ E(WSANOTINITIALISED);
+ E(WSAEDISCON);
+ E(WSATYPE_NOT_FOUND);
+ E(WSAHOST_NOT_FOUND);
+ E(WSATRY_AGAIN);
+ E(WSANO_RECOVERY);
+ E(WSANO_DATA);
+ E(0xdeadbeef); /* test the case where no message is available */
+
+ /* fill up the hash table a bit to make sure it grows properly */
+ for (j = 0; j < 50; j++) {
+ int err;
+ evutil_secure_rng_get_bytes(&err, sizeof(err));
+ evutil_socket_error_to_string(err);
+ }
+ }
+
+ s2 = evutil_socket_error_to_string (WSAEINTR);
+ if (s1 != s2)
+ printf ("caching failed!\n");
+
+ libevent_global_shutdown ();
+
+ return EXIT_SUCCESS;
+}
diff --git a/test/regress.c b/test/regress.c
index 5935f9be..399ba2fb 100644
--- a/test/regress.c
+++ b/test/regress.c
@@ -24,8 +24,9 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "util-internal.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#endif
@@ -34,11 +35,11 @@
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
@@ -63,12 +64,12 @@
#include "event2/util.h"
#include "event-internal.h"
#include "evthread-internal.h"
-#include "util-internal.h"
#include "log-internal.h"
+#include "time-internal.h"
#include "regress.h"
-#ifndef WIN32
+#ifndef _WIN32
#include "regress.gen.h"
#endif
@@ -87,13 +88,12 @@ static struct timeval tcalled;
#define TEST1 "this is a test"
-#define SECONDS 1
#ifndef SHUT_WR
#define SHUT_WR 1
#endif
-#ifdef WIN32
+#ifdef _WIN32
#define write(fd,buf,len) send((fd),(buf),(int)(len),0)
#define read(fd,buf,len) recv((fd),(buf),(int)(len),0)
#endif
@@ -233,21 +233,7 @@ multiple_read_cb(evutil_socket_t fd, short event, void *arg)
static void
timeout_cb(evutil_socket_t fd, short event, void *arg)
{
- struct timeval tv;
- int diff;
-
evutil_gettimeofday(&tcalled, NULL);
- if (evutil_timercmp(&tcalled, &tset, >))
- evutil_timersub(&tcalled, &tset, &tv);
- else
- evutil_timersub(&tset, &tcalled, &tv);
-
- diff = tv.tv_sec*1000 + tv.tv_usec/1000 - SECONDS * 1000;
- if (diff < 0)
- diff = -diff;
-
- if (diff < 100)
- test_ok = 1;
}
struct both {
@@ -414,7 +400,7 @@ test_simpleclose(void *ptr)
short got_read_on_close = 0, got_write_on_close = 0;
char buf[1024];
memset(buf, 99, sizeof(buf));
-#ifdef WIN32
+#ifdef _WIN32
#define LOCAL_SOCKETPAIR_AF AF_INET
#else
#define LOCAL_SOCKETPAIR_AF AF_UNIX
@@ -583,14 +569,18 @@ test_simpletimeout(void)
setup_test("Simple timeout: ");
- tv.tv_usec = 0;
- tv.tv_sec = SECONDS;
+ tv.tv_usec = 200*1000;
+ tv.tv_sec = 0;
+ evutil_timerclear(&tcalled);
evtimer_set(&ev, timeout_cb, NULL);
evtimer_add(&ev, &tv);
evutil_gettimeofday(&tset, NULL);
event_dispatch();
+ test_timeval_diff_eq(&tset, &tcalled, 200);
+ test_ok = 1;
+end:
cleanup_test();
}
@@ -635,15 +625,12 @@ test_persistent_timeout_jump(void *ptr)
int count = 0;
struct timeval msec100 = { 0, 100 * 1000 };
struct timeval msec50 = { 0, 50 * 1000 };
+ struct timeval msec300 = { 0, 300 * 1000 };
event_assign(&ev, data->base, -1, EV_PERSIST, periodic_timeout_cb, &count);
event_add(&ev, &msec100);
/* Wait for a bit */
-#ifdef _WIN32
- Sleep(1000);
-#else
- sleep(1);
-#endif
+ evutil_usleep_(&msec300);
event_base_loopexit(data->base, &msec50);
event_base_dispatch(data->base);
tt_int_op(count, ==, 1);
@@ -702,11 +689,11 @@ test_persistent_active_timeout(void *ptr)
tv_exit.tv_usec = 600 * 1000;
event_base_loopexit(base, &tv_exit);
- event_base_assert_ok(base);
+ event_base_assert_ok_(base);
evutil_gettimeofday(&start, NULL);
event_base_dispatch(base);
- event_base_assert_ok(base);
+ event_base_assert_ok_(base);
tt_int_op(res.n, ==, 3);
tt_int_op(res.events[0], ==, EV_READ);
@@ -732,7 +719,7 @@ common_timeout_cb(evutil_socket_t fd, short event, void *arg)
struct common_timeout_info *ti = arg;
++ti->count;
evutil_gettimeofday(&ti->called_at, NULL);
- if (ti->count >= 6)
+ if (ti->count >= 4)
event_del(&ti->ev);
}
@@ -745,22 +732,30 @@ test_common_timeout(void *ptr)
int i;
struct common_timeout_info info[100];
- struct timeval now;
+ struct timeval start;
struct timeval tmp_100_ms = { 0, 100*1000 };
struct timeval tmp_200_ms = { 0, 200*1000 };
+ struct timeval tmp_5_sec = { 5, 0 };
+ struct timeval tmp_5M_usec = { 0, 5*1000*1000 };
- const struct timeval *ms_100, *ms_200;
+ const struct timeval *ms_100, *ms_200, *sec_5;
ms_100 = event_base_init_common_timeout(base, &tmp_100_ms);
ms_200 = event_base_init_common_timeout(base, &tmp_200_ms);
+ sec_5 = event_base_init_common_timeout(base, &tmp_5_sec);
tt_assert(ms_100);
tt_assert(ms_200);
+ tt_assert(sec_5);
tt_ptr_op(event_base_init_common_timeout(base, &tmp_200_ms),
==, ms_200);
+ tt_ptr_op(event_base_init_common_timeout(base, ms_200), ==, ms_200);
+ tt_ptr_op(event_base_init_common_timeout(base, &tmp_5M_usec), ==, sec_5);
tt_int_op(ms_100->tv_sec, ==, 0);
tt_int_op(ms_200->tv_sec, ==, 0);
+ tt_int_op(sec_5->tv_sec, ==, 5);
tt_int_op(ms_100->tv_usec, ==, 100000|0x50000000);
tt_int_op(ms_200->tv_usec, ==, 200000|0x50100000);
+ tt_int_op(sec_5->tv_usec, ==, 0|0x50200000);
memset(info, 0, sizeof(info));
@@ -769,30 +764,40 @@ test_common_timeout(void *ptr)
event_assign(&info[i].ev, base, -1, EV_TIMEOUT|EV_PERSIST,
common_timeout_cb, &info[i]);
if (i % 2) {
- event_add(&info[i].ev, ms_100);
+ if ((i%20)==1) {
+ /* Glass-box test: Make sure we survive the
+ * transition to non-common timeouts. It's
+ * a little tricky. */
+ event_add(&info[i].ev, ms_200);
+ event_add(&info[i].ev, &tmp_100_ms);
+ } else if ((i%20)==3) {
+ /* Check heap-to-common too. */
+ event_add(&info[i].ev, &tmp_200_ms);
+ event_add(&info[i].ev, ms_100);
+ } else if ((i%20)==5) {
+ /* Also check common-to-common. */
+ event_add(&info[i].ev, ms_200);
+ event_add(&info[i].ev, ms_100);
+ } else {
+ event_add(&info[i].ev, ms_100);
+ }
} else {
event_add(&info[i].ev, ms_200);
}
}
- event_base_assert_ok(base);
+ event_base_assert_ok_(base);
+ evutil_gettimeofday(&start, NULL);
event_base_dispatch(base);
- evutil_gettimeofday(&now, NULL);
- event_base_assert_ok(base);
+ event_base_assert_ok_(base);
for (i=0; i<10; ++i) {
- struct timeval tmp;
- int ms_diff;
- tt_int_op(info[i].count, ==, 6);
- evutil_timersub(&now, &info[i].called_at, &tmp);
- ms_diff = tmp.tv_usec/1000 + tmp.tv_sec*1000;
+ tt_int_op(info[i].count, ==, 4);
if (i % 2) {
- tt_int_op(ms_diff, >, 500);
- tt_int_op(ms_diff, <, 700);
+ test_timeval_diff_eq(&start, &info[i].called_at, 400);
} else {
- tt_int_op(ms_diff, >, -100);
- tt_int_op(ms_diff, <, 100);
+ test_timeval_diff_eq(&start, &info[i].called_at, 800);
}
}
@@ -811,7 +816,7 @@ end:
data->base = NULL;
}
-#ifndef WIN32
+#ifndef _WIN32
static void signal_cb(evutil_socket_t fd, short event, void *arg);
#define current_base event_global_current_base_
@@ -853,18 +858,18 @@ test_fork(void)
evsignal_set(&sig_ev, SIGCHLD, child_signal_cb, &got_sigchld);
evsignal_add(&sig_ev, NULL);
- event_base_assert_ok(current_base);
+ event_base_assert_ok_(current_base);
TT_BLATHER(("Before fork"));
if ((pid = regress_fork()) == 0) {
/* in the child */
TT_BLATHER(("In child, before reinit"));
- event_base_assert_ok(current_base);
+ event_base_assert_ok_(current_base);
if (event_reinit(current_base) == -1) {
fprintf(stdout, "FAILED (reinit)\n");
exit(1);
}
TT_BLATHER(("After reinit"));
- event_base_assert_ok(current_base);
+ event_base_assert_ok_(current_base);
TT_BLATHER(("After assert-ok"));
evsignal_del(&sig_ev);
@@ -882,7 +887,10 @@ test_fork(void)
}
/* wait for the child to read the data */
- sleep(1);
+ {
+ const struct timeval tv = { 0, 100000 };
+ evutil_usleep_(&tv);
+ }
if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
tt_fail_perror("write");
@@ -936,20 +944,22 @@ signal_cb(evutil_socket_t fd, short event, void *arg)
}
static void
-test_simplesignal(void)
+test_simplesignal_impl(int find_reorder)
{
struct event ev;
struct itimerval itv;
- setup_test("Simple signal: ");
evsignal_set(&ev, SIGALRM, signal_cb, &ev);
evsignal_add(&ev, NULL);
/* find bugs in which operations are re-ordered */
- evsignal_del(&ev);
- evsignal_add(&ev, NULL);
+ if (find_reorder) {
+ evsignal_del(&ev);
+ evsignal_add(&ev, NULL);
+ }
memset(&itv, 0, sizeof(itv));
- itv.it_value.tv_sec = 1;
+ itv.it_value.tv_sec = 0;
+ itv.it_value.tv_usec = 100000;
if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
goto skip_simplesignal;
@@ -962,6 +972,20 @@ test_simplesignal(void)
}
static void
+test_simplestsignal(void)
+{
+ setup_test("Simplest one signal: ");
+ test_simplesignal_impl(0);
+}
+
+static void
+test_simplesignal(void)
+{
+ setup_test("Simple signal: ");
+ test_simplesignal_impl(1);
+}
+
+static void
test_multiplesignal(void)
{
struct event ev_one, ev_two;
@@ -976,7 +1000,8 @@ test_multiplesignal(void)
evsignal_add(&ev_two, NULL);
memset(&itv, 0, sizeof(itv));
- itv.it_value.tv_sec = 1;
+ itv.it_value.tv_sec = 0;
+ itv.it_value.tv_usec = 100000;
if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
goto skip_simplesignal;
@@ -1139,12 +1164,12 @@ test_signal_restore(void)
{
struct event ev;
struct event_base *base = event_init();
-#ifdef _EVENT_HAVE_SIGACTION
+#ifdef EVENT__HAVE_SIGACTION
struct sigaction sa;
#endif
test_ok = 0;
-#ifdef _EVENT_HAVE_SIGACTION
+#ifdef EVENT__HAVE_SIGACTION
sa.sa_handler = signal_cb_sa;
sa.sa_flags = 0x0;
sigemptyset(&sa.sa_mask);
@@ -1265,6 +1290,311 @@ end:
}
static void
+event_selfarg_cb(evutil_socket_t fd, short event, void *arg)
+{
+ struct event *ev = arg;
+ struct event_base *base = event_get_base(ev);
+ event_base_assert_ok_(base);
+ event_base_loopexit(base, NULL);
+ tt_want(ev == event_base_get_running_event(base));
+}
+
+static void
+test_event_new_selfarg(void *ptr)
+{
+ struct basic_test_data *data = ptr;
+ struct event_base *base = data->base;
+ struct event *ev = event_new(base, -1, EV_READ, event_selfarg_cb,
+ event_self_cbarg());
+
+ event_active(ev, EV_READ, 1);
+ event_base_dispatch(base);
+
+ event_free(ev);
+}
+
+static void
+test_event_assign_selfarg(void *ptr)
+{
+ struct basic_test_data *data = ptr;
+ struct event_base *base = data->base;
+ struct event ev;
+
+ event_assign(&ev, base, -1, EV_READ, event_selfarg_cb,
+ event_self_cbarg());
+ event_active(&ev, EV_READ, 1);
+ event_base_dispatch(base);
+}
+
+static void
+test_event_base_get_num_events(void *ptr)
+{
+ struct basic_test_data *data = ptr;
+ struct event_base *base = data->base;
+ struct event ev;
+ int event_count_active;
+ int event_count_virtual;
+ int event_count_added;
+ int event_count_active_virtual;
+ int event_count_active_added;
+ int event_count_virtual_added;
+ int event_count_active_added_virtual;
+
+ struct timeval qsec = {0, 100000};
+
+ event_assign(&ev, base, -1, EV_READ, event_selfarg_cb,
+ event_self_cbarg());
+
+ event_add(&ev, &qsec);
+ event_count_active = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE);
+ event_count_virtual = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_VIRTUAL);
+ event_count_added = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ADDED);
+ event_count_active_virtual = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
+ event_count_active_added = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
+ event_count_virtual_added = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
+ event_count_active_added_virtual = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE|
+ EVENT_BASE_COUNT_ADDED|
+ EVENT_BASE_COUNT_VIRTUAL);
+ tt_int_op(event_count_active, ==, 0);
+ tt_int_op(event_count_virtual, ==, 0);
+ /* libevent itself adds a timeout event, so the event_count is 2 here */
+ tt_int_op(event_count_added, ==, 2);
+ tt_int_op(event_count_active_virtual, ==, 0);
+ tt_int_op(event_count_active_added, ==, 2);
+ tt_int_op(event_count_virtual_added, ==, 2);
+ tt_int_op(event_count_active_added_virtual, ==, 2);
+
+ event_active(&ev, EV_READ, 1);
+ event_count_active = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE);
+ event_count_virtual = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_VIRTUAL);
+ event_count_added = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ADDED);
+ event_count_active_virtual = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
+ event_count_active_added = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
+ event_count_virtual_added = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
+ event_count_active_added_virtual = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE|
+ EVENT_BASE_COUNT_ADDED|
+ EVENT_BASE_COUNT_VIRTUAL);
+ tt_int_op(event_count_active, ==, 1);
+ tt_int_op(event_count_virtual, ==, 0);
+ tt_int_op(event_count_added, ==, 3);
+ tt_int_op(event_count_active_virtual, ==, 1);
+ tt_int_op(event_count_active_added, ==, 4);
+ tt_int_op(event_count_virtual_added, ==, 3);
+ tt_int_op(event_count_active_added_virtual, ==, 4);
+
+ event_base_loop(base, 0);
+ event_count_active = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE);
+ event_count_virtual = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_VIRTUAL);
+ event_count_added = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ADDED);
+ event_count_active_virtual = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
+ event_count_active_added = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
+ event_count_virtual_added = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
+ event_count_active_added_virtual = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE|
+ EVENT_BASE_COUNT_ADDED|
+ EVENT_BASE_COUNT_VIRTUAL);
+ tt_int_op(event_count_active, ==, 0);
+ tt_int_op(event_count_virtual, ==, 0);
+ tt_int_op(event_count_added, ==, 0);
+ tt_int_op(event_count_active_virtual, ==, 0);
+ tt_int_op(event_count_active_added, ==, 0);
+ tt_int_op(event_count_virtual_added, ==, 0);
+ tt_int_op(event_count_active_added_virtual, ==, 0);
+
+ event_base_add_virtual_(base);
+ event_count_active = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE);
+ event_count_virtual = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_VIRTUAL);
+ event_count_added = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ADDED);
+ event_count_active_virtual = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
+ event_count_active_added = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
+ event_count_virtual_added = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
+ event_count_active_added_virtual = event_base_get_num_events(base,
+ EVENT_BASE_COUNT_ACTIVE|
+ EVENT_BASE_COUNT_ADDED|
+ EVENT_BASE_COUNT_VIRTUAL);
+ tt_int_op(event_count_active, ==, 0);
+ tt_int_op(event_count_virtual, ==, 1);
+ tt_int_op(event_count_added, ==, 0);
+ tt_int_op(event_count_active_virtual, ==, 1);
+ tt_int_op(event_count_active_added, ==, 0);
+ tt_int_op(event_count_virtual_added, ==, 1);
+ tt_int_op(event_count_active_added_virtual, ==, 1);
+
+end:
+ ;
+}
+
+static void
+test_event_base_get_max_events(void *ptr)
+{
+ struct basic_test_data *data = ptr;
+ struct event_base *base = data->base;
+ struct event ev;
+ struct event ev2;
+ int event_count_active;
+ int event_count_virtual;
+ int event_count_added;
+ int event_count_active_virtual;
+ int event_count_active_added;
+ int event_count_virtual_added;
+ int event_count_active_added_virtual;
+
+ struct timeval qsec = {0, 100000};
+
+ event_assign(&ev, base, -1, EV_READ, event_selfarg_cb,
+ event_self_cbarg());
+ event_assign(&ev2, base, -1, EV_READ, event_selfarg_cb,
+ event_self_cbarg());
+
+ event_add(&ev, &qsec);
+ event_add(&ev2, &qsec);
+ event_del(&ev2);
+
+ event_count_active = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE, 0);
+ event_count_virtual = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_VIRTUAL, 0);
+ event_count_added = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ADDED, 0);
+ event_count_active_virtual = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
+ event_count_active_added = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
+ event_count_virtual_added = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
+ event_count_active_added_virtual = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE |
+ EVENT_BASE_COUNT_ADDED |
+ EVENT_BASE_COUNT_VIRTUAL, 0);
+
+ tt_int_op(event_count_active, ==, 0);
+ tt_int_op(event_count_virtual, ==, 0);
+ /* libevent itself adds a timeout event, so the event_count is 4 here */
+ tt_int_op(event_count_added, ==, 4);
+ tt_int_op(event_count_active_virtual, ==, 0);
+ tt_int_op(event_count_active_added, ==, 4);
+ tt_int_op(event_count_virtual_added, ==, 4);
+ tt_int_op(event_count_active_added_virtual, ==, 4);
+
+ event_active(&ev, EV_READ, 1);
+ event_count_active = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE, 0);
+ event_count_virtual = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_VIRTUAL, 0);
+ event_count_added = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ADDED, 0);
+ event_count_active_virtual = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
+ event_count_active_added = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
+ event_count_virtual_added = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
+ event_count_active_added_virtual = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE |
+ EVENT_BASE_COUNT_ADDED |
+ EVENT_BASE_COUNT_VIRTUAL, 0);
+
+ tt_int_op(event_count_active, ==, 1);
+ tt_int_op(event_count_virtual, ==, 0);
+ tt_int_op(event_count_added, ==, 4);
+ tt_int_op(event_count_active_virtual, ==, 1);
+ tt_int_op(event_count_active_added, ==, 5);
+ tt_int_op(event_count_virtual_added, ==, 4);
+ tt_int_op(event_count_active_added_virtual, ==, 5);
+
+ event_base_loop(base, 0);
+ event_count_active = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE, 1);
+ event_count_virtual = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_VIRTUAL, 1);
+ event_count_added = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ADDED, 1);
+ event_count_active_virtual = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
+ event_count_active_added = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
+ event_count_virtual_added = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
+ event_count_active_added_virtual = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE |
+ EVENT_BASE_COUNT_ADDED |
+ EVENT_BASE_COUNT_VIRTUAL, 1);
+
+ tt_int_op(event_count_active, ==, 1);
+ tt_int_op(event_count_virtual, ==, 0);
+ tt_int_op(event_count_added, ==, 4);
+ tt_int_op(event_count_active_virtual, ==, 0);
+ tt_int_op(event_count_active_added, ==, 0);
+ tt_int_op(event_count_virtual_added, ==, 0);
+ tt_int_op(event_count_active_added_virtual, ==, 0);
+
+ event_count_active = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE, 0);
+ event_count_virtual = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_VIRTUAL, 0);
+ event_count_added = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ADDED, 0);
+ tt_int_op(event_count_active, ==, 0);
+ tt_int_op(event_count_virtual, ==, 0);
+ tt_int_op(event_count_added, ==, 0);
+
+ event_base_add_virtual_(base);
+ event_count_active = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE, 0);
+ event_count_virtual = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_VIRTUAL, 0);
+ event_count_added = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ADDED, 0);
+ event_count_active_virtual = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
+ event_count_active_added = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
+ event_count_virtual_added = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
+ event_count_active_added_virtual = event_base_get_max_events(base,
+ EVENT_BASE_COUNT_ACTIVE |
+ EVENT_BASE_COUNT_ADDED |
+ EVENT_BASE_COUNT_VIRTUAL, 0);
+
+ tt_int_op(event_count_active, ==, 0);
+ tt_int_op(event_count_virtual, ==, 1);
+ tt_int_op(event_count_added, ==, 0);
+ tt_int_op(event_count_active_virtual, ==, 1);
+ tt_int_op(event_count_active_added, ==, 0);
+ tt_int_op(event_count_virtual_added, ==, 1);
+ tt_int_op(event_count_active_added_virtual, ==, 1);
+
+end:
+ ;
+}
+
+static void
test_bad_assign(void *ptr)
{
struct event ev;
@@ -1311,6 +1641,168 @@ end:
;
}
+static int n_write_a_byte_cb=0;
+static int n_read_and_drain_cb=0;
+static int n_activate_other_event_cb=0;
+static void
+write_a_byte_cb(evutil_socket_t fd, short what, void *arg)
+{
+ char buf[] = "x";
+ if (write(fd, buf, 1) == 1)
+ ++n_write_a_byte_cb;
+}
+static void
+read_and_drain_cb(evutil_socket_t fd, short what, void *arg)
+{
+ char buf[128];
+ int n;
+ ++n_read_and_drain_cb;
+ while ((n = read(fd, buf, sizeof(buf))) > 0)
+ ;
+}
+
+static void
+activate_other_event_cb(evutil_socket_t fd, short what, void *other_)
+{
+ struct event *ev_activate = other_;
+ ++n_activate_other_event_cb;
+ event_active_later_(ev_activate, EV_READ);
+}
+
+static void
+test_active_later(void *ptr)
+{
+ struct basic_test_data *data = ptr;
+ struct event *ev1 = NULL, *ev2 = NULL;
+ struct event ev3, ev4;
+ struct timeval qsec = {0, 100000};
+ ev1 = event_new(data->base, data->pair[0], EV_READ|EV_PERSIST, read_and_drain_cb, NULL);
+ ev2 = event_new(data->base, data->pair[1], EV_WRITE|EV_PERSIST, write_a_byte_cb, NULL);
+ event_assign(&ev3, data->base, -1, 0, activate_other_event_cb, &ev4);
+ event_assign(&ev4, data->base, -1, 0, activate_other_event_cb, &ev3);
+ event_add(ev1, NULL);
+ event_add(ev2, NULL);
+ event_active_later_(&ev3, EV_READ);
+
+ event_base_loopexit(data->base, &qsec);
+
+ event_base_loop(data->base, 0);
+
+ TT_BLATHER(("%d write calls, %d read calls, %d activate-other calls.",
+ n_write_a_byte_cb, n_read_and_drain_cb, n_activate_other_event_cb));
+ event_del(&ev3);
+ event_del(&ev4);
+
+ tt_int_op(n_write_a_byte_cb, ==, n_activate_other_event_cb);
+ tt_int_op(n_write_a_byte_cb, >, 100);
+ tt_int_op(n_read_and_drain_cb, >, 100);
+ tt_int_op(n_activate_other_event_cb, >, 100);
+
+ event_active_later_(&ev4, EV_READ);
+ event_active(&ev4, EV_READ, 1); /* This should make the event
+ active immediately. */
+ tt_assert((ev4.ev_flags & EVLIST_ACTIVE) != 0);
+ tt_assert((ev4.ev_flags & EVLIST_ACTIVE_LATER) == 0);
+
+ /* Now leave this one around, so that event_free sees it and removes
+ * it. */
+ event_active_later_(&ev3, EV_READ);
+ event_base_assert_ok_(data->base);
+
+end:
+ if (ev1)
+ event_free(ev1);
+ if (ev2)
+ event_free(ev2);
+
+ event_base_free(data->base);
+ data->base = NULL;
+}
+
+
+static void incr_arg_cb(evutil_socket_t fd, short what, void *arg)
+{
+ int *intptr = arg;
+ (void) fd; (void) what;
+ ++*intptr;
+}
+static void remove_timers_cb(evutil_socket_t fd, short what, void *arg)
+{
+ struct event **ep = arg;
+ (void) fd; (void) what;
+ event_remove_timer(ep[0]);
+ event_remove_timer(ep[1]);
+}
+static void send_a_byte_cb(evutil_socket_t fd, short what, void *arg)
+{
+ evutil_socket_t *sockp = arg;
+ (void) fd; (void) what;
+ (void) write(*sockp, "A", 1);
+}
+struct read_not_timeout_param
+{
+ struct event **ev;
+ int events;
+ int count;
+};
+static void read_not_timeout_cb(evutil_socket_t fd, short what, void *arg)
+{
+ struct read_not_timeout_param *rntp = arg;
+ char c;
+ ev_ssize_t n;
+ (void) fd; (void) what;
+ n = read(fd, &c, 1);
+ tt_int_op(n, ==, 1);
+ rntp->events |= what;
+ ++rntp->count;
+ if(2 == rntp->count) event_del(rntp->ev[0]);
+end:
+ ;
+}
+
+static void
+test_event_remove_timeout(void *ptr)
+{
+ struct basic_test_data *data = ptr;
+ struct event_base *base = data->base;
+ struct event *ev[5];
+ int ev1_fired=0;
+ struct timeval ms25 = { 0, 25*1000 },
+ ms40 = { 0, 40*1000 },
+ ms75 = { 0, 75*1000 },
+ ms125 = { 0, 125*1000 };
+ struct read_not_timeout_param rntp = { ev, 0, 0 };
+
+ event_base_assert_ok_(base);
+
+ ev[0] = event_new(base, data->pair[0], EV_READ|EV_PERSIST,
+ read_not_timeout_cb, &rntp);
+ ev[1] = evtimer_new(base, incr_arg_cb, &ev1_fired);
+ ev[2] = evtimer_new(base, remove_timers_cb, ev);
+ ev[3] = evtimer_new(base, send_a_byte_cb, &data->pair[1]);
+ ev[4] = evtimer_new(base, send_a_byte_cb, &data->pair[1]);
+ tt_assert(base);
+ event_add(ev[2], &ms25); /* remove timers */
+ event_add(ev[4], &ms40); /* write to test if timer re-activates */
+ event_add(ev[0], &ms75); /* read */
+ event_add(ev[1], &ms75); /* timer */
+ event_add(ev[3], &ms125); /* timeout. */
+ event_base_assert_ok_(base);
+
+ event_base_dispatch(base);
+
+ tt_int_op(ev1_fired, ==, 0);
+ tt_int_op(rntp.events, ==, EV_READ);
+
+ event_base_assert_ok_(base);
+end:
+ event_free(ev[0]);
+ event_free(ev[1]);
+ event_free(ev[2]);
+ event_free(ev[3]);
+ event_free(ev[4]);
+}
+
static void
test_event_base_new(void *ptr)
{
@@ -1365,23 +1857,22 @@ test_loopexit(void)
evtimer_set(&ev, timeout_cb, NULL);
evtimer_add(&ev, &tv);
- tv.tv_usec = 0;
- tv.tv_sec = 1;
+ tv.tv_usec = 300*1000;
+ tv.tv_sec = 0;
event_loopexit(&tv);
evutil_gettimeofday(&tv_start, NULL);
event_dispatch();
evutil_gettimeofday(&tv_end, NULL);
- evutil_timersub(&tv_end, &tv_start, &tv_end);
evtimer_del(&ev);
tt_assert(event_base_got_exit(global_base));
tt_assert(!event_base_got_break(global_base));
- if (tv.tv_sec < 2)
- test_ok = 1;
+ test_timeval_diff_eq(&tv_start, &tv_end, 300);
+ test_ok = 1;
end:
cleanup_test();
}
@@ -1389,28 +1880,32 @@ end:
static void
test_loopexit_multiple(void)
{
- struct timeval tv;
+ struct timeval tv, tv_start, tv_end;
struct event_base *base;
setup_test("Loop Multiple exit: ");
base = event_base_new();
- tv.tv_usec = 0;
- tv.tv_sec = 1;
+ tv.tv_usec = 200*1000;
+ tv.tv_sec = 0;
event_base_loopexit(base, &tv);
tv.tv_usec = 0;
- tv.tv_sec = 2;
+ tv.tv_sec = 3;
event_base_loopexit(base, &tv);
+ evutil_gettimeofday(&tv_start, NULL);
event_base_dispatch(base);
+ evutil_gettimeofday(&tv_end, NULL);
tt_assert(event_base_got_exit(base));
tt_assert(!event_base_got_break(base));
event_base_free(base);
+ test_timeval_diff_eq(&tv_start, &tv_end, 200);
+
test_ok = 1;
end:
@@ -1463,14 +1958,20 @@ re_add_read_cb(evutil_socket_t fd, short event, void *arg)
{
char buf[256];
struct event *ev_other = arg;
+ ev_ssize_t n_read;
+
readd_test_event_last_added = ev_other;
- if (read(fd, buf, sizeof(buf)) < 0) {
+ n_read = read(fd, buf, sizeof(buf));
+
+ if (n_read < 0) {
tt_fail_perror("read");
+ event_base_loopbreak(event_get_base(ev_other));
+ return;
+ } else {
+ event_add(ev_other, NULL);
+ ++test_ok;
}
-
- event_add(ev_other, NULL);
- ++test_ok;
}
static void
@@ -1736,7 +2237,7 @@ test_want_only_once(void)
/* Setup the loop termination */
evutil_timerclear(&tv);
- tv.tv_sec = 1;
+ tv.tv_usec = 300*1000;
event_loopexit(&tv);
event_set(&ev, pair[1], EV_READ, read_once_cb, &ev);
@@ -1800,7 +2301,7 @@ evtag_fuzz(void *ptr)
for (j = 0; j < 100; j++) {
for (i = 0; i < (int)sizeof(buffer); i++)
- buffer[i] = rand();
+ buffer[i] = test_weakrand();
evbuffer_drain(tmp, -1);
evbuffer_add(tmp, buffer, sizeof(buffer));
@@ -1974,10 +2475,10 @@ end:
event_config_free(cfg);
}
-#ifdef _EVENT_HAVE_SETENV
+#ifdef EVENT__HAVE_SETENV
#define SETENV_OK
-#elif !defined(_EVENT_HAVE_SETENV) && defined(_EVENT_HAVE_PUTENV)
-static void setenv(const char *k, const char *v, int _o)
+#elif !defined(EVENT__HAVE_SETENV) && defined(EVENT__HAVE_PUTENV)
+static void setenv(const char *k, const char *v, int o_)
{
char b[256];
evutil_snprintf(b, sizeof(b), "%s=%s",k,v);
@@ -1986,9 +2487,9 @@ static void setenv(const char *k, const char *v, int _o)
#define SETENV_OK
#endif
-#ifdef _EVENT_HAVE_UNSETENV
+#ifdef EVENT__HAVE_UNSETENV
#define UNSETENV_OK
-#elif !defined(_EVENT_HAVE_UNSETENV) && defined(_EVENT_HAVE_PUTENV)
+#elif !defined(EVENT__HAVE_UNSETENV) && defined(EVENT__HAVE_PUTENV)
static void unsetenv(const char *k)
{
char b[256];
@@ -2005,7 +2506,7 @@ methodname_to_envvar(const char *mname, char *buf, size_t buflen)
char *cp;
evutil_snprintf(buf, buflen, "EVENT_NO%s", mname);
for (cp = buf; *cp; ++cp) {
- *cp = EVUTIL_TOUPPER(*cp);
+ *cp = EVUTIL_TOUPPER_(*cp);
}
}
#endif
@@ -2026,7 +2527,7 @@ test_base_environ(void *arg)
setenv("EVENT_NOWAFFLES", "1", 1);
unsetenv("EVENT_NOWAFFLES");
if (getenv("EVENT_NOWAFFLES") != NULL) {
-#ifndef _EVENT_HAVE_UNSETENV
+#ifndef EVENT__HAVE_UNSETENV
TT_DECLARE("NOTE", ("Can't fake unsetenv; skipping test"));
#else
TT_DECLARE("NOTE", ("unsetenv doesn't work; skipping test"));
@@ -2109,6 +2610,15 @@ end:
}
static void
+immediate_called_twice_cb(evutil_socket_t fd, short event, void *arg)
+{
+ tt_int_op(event, ==, EV_TIMEOUT);
+ called += 1000;
+end:
+ ;
+}
+
+static void
test_event_once(void *ptr)
{
struct basic_test_data *data = ptr;
@@ -2126,6 +2636,14 @@ test_event_once(void *ptr)
tt_int_op(r, ==, 0);
r = event_base_once(data->base, -1, 0, NULL, NULL, NULL);
tt_int_op(r, <, 0);
+ r = event_base_once(data->base, -1, EV_TIMEOUT,
+ immediate_called_twice_cb, NULL, NULL);
+ tt_int_op(r, ==, 0);
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ r = event_base_once(data->base, -1, EV_TIMEOUT,
+ immediate_called_twice_cb, NULL, &tv);
+ tt_int_op(r, ==, 0);
if (write(data->pair[1], TEST1, strlen(TEST1)+1) < 0) {
tt_fail_perror("write");
@@ -2135,7 +2653,32 @@ test_event_once(void *ptr)
event_base_dispatch(data->base);
- tt_int_op(called, ==, 101);
+ tt_int_op(called, ==, 2101);
+end:
+ ;
+}
+
+static void
+test_event_once_never(void *ptr)
+{
+ struct basic_test_data *data = ptr;
+ struct timeval tv;
+
+ /* Have one trigger in 10 seconds (don't worry, because) */
+ tv.tv_sec = 10;
+ tv.tv_usec = 0;
+ called = 0;
+ event_base_once(data->base, -1, EV_TIMEOUT,
+ timeout_called_once_cb, NULL, &tv);
+
+ /* But shut down the base in 75 msec. */
+ tv.tv_sec = 0;
+ tv.tv_usec = 75*1000;
+ event_base_loopexit(data->base, &tv);
+
+ event_base_dispatch(data->base);
+
+ tt_int_op(called, ==, 0);
end:
;
}
@@ -2145,7 +2688,7 @@ test_event_pending(void *ptr)
{
struct basic_test_data *data = ptr;
struct event *r=NULL, *w=NULL, *t=NULL;
- struct timeval tv, now, tv2, diff;
+ struct timeval tv, now, tv2;
tv.tv_sec = 0;
tv.tv_usec = 500 * 1000;
@@ -2172,10 +2715,8 @@ test_event_pending(void *ptr)
tt_assert( event_pending(t, EV_TIMEOUT, &tv2));
tt_assert(evutil_timercmp(&tv2, &now, >));
- evutil_timeradd(&now, &tv, &tv);
- evutil_timersub(&tv2, &tv, &diff);
- tt_int_op(diff.tv_sec, ==, 0);
- tt_int_op(labs(diff.tv_usec), <, 1000);
+
+ test_timeval_diff_eq(&now, &tv2, 500);
end:
if (r) {
@@ -2192,7 +2733,7 @@ end:
}
}
-#ifndef WIN32
+#ifndef _WIN32
/* You can't do this test on windows, since dup2 doesn't work on sockets */
static void
@@ -2263,17 +2804,17 @@ end:
}
#endif
-#ifdef _EVENT_DISABLE_MM_REPLACEMENT
+#ifdef EVENT__DISABLE_MM_REPLACEMENT
static void
test_mm_functions(void *arg)
{
- _tinytest_set_test_skipped();
+ tinytest_set_test_skipped_();
}
#else
static int
-check_dummy_mem_ok(void *_mem)
+check_dummy_mem_ok(void *mem_)
{
- char *mem = _mem;
+ char *mem = mem_;
mem -= 16;
return !memcmp(mem, "{[<guardedram>]}", 16);
}
@@ -2287,22 +2828,22 @@ dummy_malloc(size_t len)
}
static void *
-dummy_realloc(void *_mem, size_t len)
+dummy_realloc(void *mem_, size_t len)
{
- char *mem = _mem;
+ char *mem = mem_;
if (!mem)
return dummy_malloc(len);
- tt_want(check_dummy_mem_ok(_mem));
+ tt_want(check_dummy_mem_ok(mem_));
mem -= 16;
mem = realloc(mem, len+16);
return mem+16;
}
static void
-dummy_free(void *_mem)
+dummy_free(void *mem_)
{
- char *mem = _mem;
- tt_want(check_dummy_mem_ok(_mem));
+ char *mem = mem_;
+ tt_want(check_dummy_mem_ok(mem_));
mem -= 16;
free(mem);
}
@@ -2350,7 +2891,6 @@ test_many_events(void *arg)
int called[MANY];
int i;
int loopflags = EVLOOP_NONBLOCK, evflags=0;
- const int is_evport = !strcmp(event_base_get_method(base),"evport");
if (one_at_a_time) {
loopflags |= EVLOOP_ONCE;
evflags = EV_PERSIST;
@@ -2359,10 +2899,6 @@ test_many_events(void *arg)
memset(sock, 0xff, sizeof(sock));
memset(ev, 0, sizeof(ev));
memset(called, 0, sizeof(called));
- if (is_evport && one_at_a_time) {
- TT_DECLARE("NOTE", ("evport can't pass this in 2.0; skipping\n"));
- tt_skip();
- }
for (i = 0; i < MANY; ++i) {
/* We need an event that will hit the backend, and that will
@@ -2405,6 +2941,292 @@ end:
;
}
+static void
+test_get_assignment(void *arg)
+{
+ struct basic_test_data *data = arg;
+ struct event_base *base = data->base;
+ struct event *ev1 = NULL;
+ const char *str = "foo";
+
+ struct event_base *b;
+ evutil_socket_t s;
+ short what;
+ event_callback_fn cb;
+ void *cb_arg;
+
+ ev1 = event_new(base, data->pair[1], EV_READ, dummy_read_cb, (void*)str);
+ event_get_assignment(ev1, &b, &s, &what, &cb, &cb_arg);
+
+ tt_ptr_op(b, ==, base);
+ tt_int_op(s, ==, data->pair[1]);
+ tt_int_op(what, ==, EV_READ);
+ tt_ptr_op(cb, ==, dummy_read_cb);
+ tt_ptr_op(cb_arg, ==, str);
+
+ /* Now make sure this doesn't crash. */
+ event_get_assignment(ev1, NULL, NULL, NULL, NULL, NULL);
+
+end:
+ if (ev1)
+ event_free(ev1);
+}
+
+struct foreach_helper {
+ int count;
+ const struct event *ev;
+};
+
+static int
+foreach_count_cb(const struct event_base *base, const struct event *ev, void *arg)
+{
+ struct foreach_helper *h = event_get_callback_arg(ev);
+ struct timeval *tv = arg;
+ if (event_get_callback(ev) != timeout_cb)
+ return 0;
+ tt_ptr_op(event_get_base(ev), ==, base);
+ tt_int_op(tv->tv_sec, ==, 10);
+ h->ev = ev;
+ h->count++;
+ return 0;
+end:
+ return -1;
+}
+
+static int
+foreach_find_cb(const struct event_base *base, const struct event *ev, void *arg)
+{
+ const struct event **ev_out = arg;
+ struct foreach_helper *h = event_get_callback_arg(ev);
+ if (event_get_callback(ev) != timeout_cb)
+ return 0;
+ if (h->count == 99) {
+ *ev_out = ev;
+ return 101;
+ }
+ return 0;
+}
+
+static void
+test_event_foreach(void *arg)
+{
+ struct basic_test_data *data = arg;
+ struct event_base *base = data->base;
+ struct event *ev[5];
+ struct foreach_helper visited[5];
+ int i;
+ struct timeval ten_sec = {10,0};
+ const struct event *ev_found = NULL;
+
+ for (i = 0; i < 5; ++i) {
+ visited[i].count = 0;
+ visited[i].ev = NULL;
+ ev[i] = event_new(base, -1, 0, timeout_cb, &visited[i]);
+ }
+
+ tt_int_op(-1, ==, event_base_foreach_event(NULL, foreach_count_cb, NULL));
+ tt_int_op(-1, ==, event_base_foreach_event(base, NULL, NULL));
+
+ event_add(ev[0], &ten_sec);
+ event_add(ev[1], &ten_sec);
+ event_active(ev[1], EV_TIMEOUT, 1);
+ event_active(ev[2], EV_TIMEOUT, 1);
+ event_add(ev[3], &ten_sec);
+ /* Don't touch ev[4]. */
+
+ tt_int_op(0, ==, event_base_foreach_event(base, foreach_count_cb,
+ &ten_sec));
+ tt_int_op(1, ==, visited[0].count);
+ tt_int_op(1, ==, visited[1].count);
+ tt_int_op(1, ==, visited[2].count);
+ tt_int_op(1, ==, visited[3].count);
+ tt_ptr_op(ev[0], ==, visited[0].ev);
+ tt_ptr_op(ev[1], ==, visited[1].ev);
+ tt_ptr_op(ev[2], ==, visited[2].ev);
+ tt_ptr_op(ev[3], ==, visited[3].ev);
+
+ visited[2].count = 99;
+ tt_int_op(101, ==, event_base_foreach_event(base, foreach_find_cb,
+ &ev_found));
+ tt_ptr_op(ev_found, ==, ev[2]);
+
+end:
+ for (i=0; i<5; ++i) {
+ event_free(ev[i]);
+ }
+}
+
+static struct event_base *cached_time_base = NULL;
+static int cached_time_reset = 0;
+static int cached_time_sleep = 0;
+static void
+cache_time_cb(evutil_socket_t fd, short what, void *arg)
+{
+ struct timeval *tv = arg;
+ tt_int_op(0, ==, event_base_gettimeofday_cached(cached_time_base, tv));
+ if (cached_time_sleep) {
+ struct timeval delay = { 0, 30*1000 };
+ evutil_usleep_(&delay);
+ }
+ if (cached_time_reset) {
+ event_base_update_cache_time(cached_time_base);
+ }
+end:
+ ;
+}
+
+static void
+test_gettimeofday_cached(void *arg)
+{
+ struct basic_test_data *data = arg;
+ struct event_config *cfg = NULL;
+ struct event_base *base = NULL;
+ struct timeval tv1, tv2, tv3, now;
+ struct event *ev1=NULL, *ev2=NULL, *ev3=NULL;
+ int cached_time_disable = strstr(data->setup_data, "disable") != NULL;
+
+ cfg = event_config_new();
+ if (cached_time_disable) {
+ event_config_set_flag(cfg, EVENT_BASE_FLAG_NO_CACHE_TIME);
+ }
+ cached_time_base = base = event_base_new_with_config(cfg);
+ tt_assert(base);
+
+ /* Try gettimeofday_cached outside of an event loop. */
+ evutil_gettimeofday(&now, NULL);
+ tt_int_op(0, ==, event_base_gettimeofday_cached(NULL, &tv1));
+ tt_int_op(0, ==, event_base_gettimeofday_cached(base, &tv2));
+ tt_int_op(timeval_msec_diff(&tv1, &tv2), <, 10);
+ tt_int_op(timeval_msec_diff(&tv1, &now), <, 10);
+
+ cached_time_reset = strstr(data->setup_data, "reset") != NULL;
+ cached_time_sleep = strstr(data->setup_data, "sleep") != NULL;
+
+ ev1 = event_new(base, -1, 0, cache_time_cb, &tv1);
+ ev2 = event_new(base, -1, 0, cache_time_cb, &tv2);
+ ev3 = event_new(base, -1, 0, cache_time_cb, &tv3);
+
+ event_active(ev1, EV_TIMEOUT, 1);
+ event_active(ev2, EV_TIMEOUT, 1);
+ event_active(ev3, EV_TIMEOUT, 1);
+
+ event_base_dispatch(base);
+
+ if (cached_time_reset && cached_time_sleep) {
+ tt_int_op(labs(timeval_msec_diff(&tv1,&tv2)), >, 10);
+ tt_int_op(labs(timeval_msec_diff(&tv2,&tv3)), >, 10);
+ } else if (cached_time_disable && cached_time_sleep) {
+ tt_int_op(labs(timeval_msec_diff(&tv1,&tv2)), >, 10);
+ tt_int_op(labs(timeval_msec_diff(&tv2,&tv3)), >, 10);
+ } else if (! cached_time_disable) {
+ tt_assert(evutil_timercmp(&tv1, &tv2, ==));
+ tt_assert(evutil_timercmp(&tv2, &tv3, ==));
+ }
+
+end:
+ if (ev1)
+ event_free(ev1);
+ if (ev2)
+ event_free(ev2);
+ if (ev3)
+ event_free(ev3);
+ if (base)
+ event_base_free(base);
+ if (cfg)
+ event_config_free(cfg);
+}
+
+static void
+tabf_cb(evutil_socket_t fd, short what, void *arg)
+{
+ int *ptr = arg;
+ *ptr = what;
+ *ptr += 0x10000;
+}
+
+static void
+test_active_by_fd(void *arg)
+{
+ struct basic_test_data *data = arg;
+ struct event_base *base = data->base;
+ struct event *ev1 = NULL, *ev2 = NULL, *ev3 = NULL, *ev4 = NULL;
+ int e1,e2,e3,e4;
+#ifndef _WIN32
+ struct event *evsig = NULL;
+ int es;
+#endif
+ struct timeval tenmin = { 600, 0 };
+
+ /* Ensure no crash on nonexistent FD. */
+ event_base_active_by_fd(base, 1000, EV_READ);
+
+ /* Ensure no crash on bogus FD. */
+ event_base_active_by_fd(base, -1, EV_READ);
+
+ /* Ensure no crash on nonexistent/bogus signal. */
+ event_base_active_by_signal(base, 1000);
+ event_base_active_by_signal(base, -1);
+
+ event_base_assert_ok_(base);
+
+ e1 = e2 = e3 = e4 = 0;
+ ev1 = event_new(base, data->pair[0], EV_READ, tabf_cb, &e1);
+ ev2 = event_new(base, data->pair[0], EV_WRITE, tabf_cb, &e2);
+ ev3 = event_new(base, data->pair[1], EV_READ, tabf_cb, &e3);
+ ev4 = event_new(base, data->pair[1], EV_READ, tabf_cb, &e4);
+ tt_assert(ev1);
+ tt_assert(ev2);
+ tt_assert(ev3);
+ tt_assert(ev4);
+#ifndef _WIN32
+ evsig = event_new(base, SIGHUP, EV_SIGNAL, tabf_cb, &es);
+ tt_assert(evsig);
+ event_add(evsig, &tenmin);
+#endif
+
+ event_add(ev1, &tenmin);
+ event_add(ev2, NULL);
+ event_add(ev3, NULL);
+ event_add(ev4, &tenmin);
+
+
+ event_base_assert_ok_(base);
+
+ /* Trigger 2, 3, 4 */
+ event_base_active_by_fd(base, data->pair[0], EV_WRITE);
+ event_base_active_by_fd(base, data->pair[1], EV_READ);
+#ifndef _WIN32
+ event_base_active_by_signal(base, SIGHUP);
+#endif
+
+ event_base_assert_ok_(base);
+
+ event_base_loop(base, EVLOOP_ONCE);
+
+ tt_int_op(e1, ==, 0);
+ tt_int_op(e2, ==, EV_WRITE | 0x10000);
+ tt_int_op(e3, ==, EV_READ | 0x10000);
+ /* Mask out EV_WRITE here, since it could be genuinely writeable. */
+ tt_int_op((e4 & ~EV_WRITE), ==, EV_READ | 0x10000);
+#ifndef _WIN32
+ tt_int_op(es, ==, EV_SIGNAL | 0x10000);
+#endif
+
+end:
+ if (ev1)
+ event_free(ev1);
+ if (ev2)
+ event_free(ev2);
+ if (ev3)
+ event_free(ev3);
+ if (ev4)
+ event_free(ev4);
+#ifndef _WIN32
+ if (evsig)
+ event_free(evsig);
+#endif
+}
+
struct testcase_t main_testcases[] = {
/* Some converted-over tests */
{ "methods", test_methods, TT_FORK, NULL, NULL },
@@ -2416,10 +3238,17 @@ struct testcase_t main_testcases[] = {
BASIC(free_active_base, TT_FORK|TT_NEED_SOCKETPAIR),
BASIC(manipulate_active_events, TT_FORK|TT_NEED_BASE),
+ BASIC(event_new_selfarg, TT_FORK|TT_NEED_BASE),
+ BASIC(event_assign_selfarg, TT_FORK|TT_NEED_BASE),
+ BASIC(event_base_get_num_events, TT_FORK|TT_NEED_BASE),
+ BASIC(event_base_get_max_events, TT_FORK|TT_NEED_BASE),
BASIC(bad_assign, TT_FORK|TT_NEED_BASE|TT_NO_LOGS),
BASIC(bad_reentrant, TT_FORK|TT_NEED_BASE|TT_NO_LOGS),
+ BASIC(active_later, TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR),
+ BASIC(event_remove_timeout, TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR),
+ /* These are still using the old API */
LEGACY(persistent_timeout, TT_FORK|TT_NEED_BASE),
{ "persistent_timeout_jump", test_persistent_timeout_jump, TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
{ "persistent_active_timeout", test_persistent_active_timeout,
@@ -2446,9 +3275,10 @@ struct testcase_t main_testcases[] = {
LEGACY(multiple_events_for_same_fd, TT_ISOLATED),
LEGACY(want_only_once, TT_ISOLATED),
{ "event_once", test_event_once, TT_ISOLATED, &basic_setup, NULL },
+ { "event_once_never", test_event_once_never, TT_ISOLATED, &basic_setup, NULL },
{ "event_pending", test_event_pending, TT_ISOLATED, &basic_setup,
NULL },
-#ifndef WIN32
+#ifndef _WIN32
{ "dup_fd", test_dup_fd, TT_ISOLATED, &basic_setup, NULL },
#endif
{ "mm_functions", test_mm_functions, TT_FORK, NULL, NULL },
@@ -2456,8 +3286,18 @@ struct testcase_t main_testcases[] = {
{ "many_events_slow_add", test_many_events, TT_ISOLATED, &basic_setup, (void*)1 },
{ "struct_event_size", test_struct_event_size, 0, NULL, NULL },
+ BASIC(get_assignment, TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR),
+
+ BASIC(event_foreach, TT_FORK|TT_NEED_BASE),
+ { "gettimeofday_cached", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"" },
+ { "gettimeofday_cached_sleep", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"sleep" },
+ { "gettimeofday_cached_reset", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"sleep reset" },
+ { "gettimeofday_cached_disabled", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"sleep disable" },
+ { "gettimeofday_cached_disabled_nosleep", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"disable" },
+
+ BASIC(active_by_fd, TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR),
-#ifndef WIN32
+#ifndef _WIN32
LEGACY(fork, TT_ISOLATED),
#endif
END_OF_TESTCASES
@@ -2473,7 +3313,8 @@ struct testcase_t evtag_testcases[] = {
};
struct testcase_t signal_testcases[] = {
-#ifndef WIN32
+#ifndef _WIN32
+ LEGACY(simplestsignal, TT_ISOLATED),
LEGACY(simplesignal, TT_ISOLATED),
LEGACY(multiplesignal, TT_ISOLATED),
LEGACY(immediatesignal, TT_ISOLATED),
diff --git a/test/regress.h b/test/regress.h
index 78dc3d01..2e9f427b 100644
--- a/test/regress.h
+++ b/test/regress.h
@@ -24,8 +24,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _REGRESS_H_
-#define _REGRESS_H_
+#ifndef REGRESS_H_INCLUDED_
+#define REGRESS_H_INCLUDED_
#ifdef __cplusplus
extern "C" {
@@ -37,6 +37,7 @@ extern "C" {
extern struct testcase_t main_testcases[];
extern struct testcase_t evtag_testcases[];
extern struct testcase_t evbuffer_testcases[];
+extern struct testcase_t finalize_testcases[];
extern struct testcase_t bufferevent_testcases[];
extern struct testcase_t bufferevent_iocp_testcases[];
extern struct testcase_t util_testcases[];
@@ -52,6 +53,10 @@ extern struct testcase_t listener_testcases[];
extern struct testcase_t listener_iocp_testcases[];
extern struct testcase_t thread_testcases[];
+extern struct evutil_weakrand_state test_weakrand_state;
+
+#define test_weakrand() (evutil_weakrand_(&test_weakrand_state))
+
void regress_threads(void *);
void test_bufferevent_zlib(void *);
@@ -62,7 +67,7 @@ extern int called;
extern struct event_base *global_base;
extern int in_legacy_test_wrapper;
-int regress_make_tmpfile(const void *data, size_t datalen);
+int regress_make_tmpfile(const void *data, size_t datalen, char **filename_out);
struct basic_test_data {
struct event_base *base;
@@ -78,6 +83,8 @@ extern const struct testcase_setup_t basic_setup;
extern const struct testcase_setup_t legacy_setup;
void run_legacy_test_fn(void *ptr);
+extern int libevent_tests_running_in_debug_mode;
+
/* A couple of flags that basic/legacy_setup can support. */
#define TT_NEED_SOCKETPAIR TT_FIRST_USER_FLAG
#define TT_NEED_BASE (TT_FIRST_USER_FLAG<<1)
@@ -102,11 +109,11 @@ void run_legacy_test_fn(void *ptr);
struct evutil_addrinfo;
struct evutil_addrinfo *ai_find_by_family(struct evutil_addrinfo *ai, int f);
struct evutil_addrinfo *ai_find_by_protocol(struct evutil_addrinfo *ai, int p);
-int _test_ai_eq(const struct evutil_addrinfo *ai, const char *sockaddr_port,
+int test_ai_eq_(const struct evutil_addrinfo *ai, const char *sockaddr_port,
int socktype, int protocol, int line);
#define test_ai_eq(ai, str, s, p) do { \
- if (_test_ai_eq((ai), (str), (s), (p), __LINE__)<0) \
+ if (test_ai_eq_((ai), (str), (s), (p), __LINE__)<0) \
goto end; \
} while (0)
@@ -126,4 +133,4 @@ pid_t regress_fork(void);
}
#endif
-#endif /* _REGRESS_H_ */
+#endif /* REGRESS_H_INCLUDED_ */
diff --git a/test/regress_buffer.c b/test/regress_buffer.c
index d7cc5385..4d6a5de2 100644
--- a/test/regress_buffer.c
+++ b/test/regress_buffer.c
@@ -24,8 +24,9 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "util-internal.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#endif
@@ -34,11 +35,11 @@
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
@@ -56,6 +57,7 @@
#include "event2/buffer_compat.h"
#include "event2/util.h"
+#include "defer-internal.h"
#include "evbuffer-internal.h"
#include "log-internal.h"
@@ -64,7 +66,7 @@
/* Validates that an evbuffer is good. Returns false if it isn't, true if it
* is*/
static int
-_evbuffer_validate(struct evbuffer *buf)
+evbuffer_validate_(struct evbuffer *buf)
{
struct evbuffer_chain *chain;
size_t sum = 0;
@@ -163,7 +165,7 @@ evbuffer_get_waste(struct evbuffer *buf, size_t *allocatedp, size_t *wastedp, si
}
#define evbuffer_validate(buf) \
- TT_STMT_BEGIN if (!_evbuffer_validate(buf)) TT_DIE(("Buffer format invalid")); TT_STMT_END
+ TT_STMT_BEGIN if (!evbuffer_validate_(buf)) TT_DIE(("Buffer format invalid")); TT_STMT_END
static void
test_evbuffer(void *ptr)
@@ -620,89 +622,322 @@ test_evbuffer_reference(void *ptr)
evbuffer_free(src);
}
-int _evbuffer_testing_use_sendfile(void);
-int _evbuffer_testing_use_mmap(void);
-int _evbuffer_testing_use_linear_file_access(void);
+static struct event_base *addfile_test_event_base = NULL;
+static int addfile_test_done_writing = 0;
+static int addfile_test_total_written = 0;
+static int addfile_test_total_read = 0;
+
+static void
+addfile_test_writecb(evutil_socket_t fd, short what, void *arg)
+{
+ struct evbuffer *b = arg;
+ int r;
+ evbuffer_validate(b);
+ while (evbuffer_get_length(b)) {
+ r = evbuffer_write(b, fd);
+ if (r > 0) {
+ addfile_test_total_written += r;
+ TT_BLATHER(("Wrote %d/%d bytes", r, addfile_test_total_written));
+ } else {
+ int e = evutil_socket_geterror(fd);
+ if (EVUTIL_ERR_RW_RETRIABLE(e))
+ return;
+ tt_fail_perror("write");
+ event_base_loopexit(addfile_test_event_base,NULL);
+ }
+ evbuffer_validate(b);
+ }
+ addfile_test_done_writing = 1;
+ return;
+end:
+ event_base_loopexit(addfile_test_event_base,NULL);
+}
+
+static void
+addfile_test_readcb(evutil_socket_t fd, short what, void *arg)
+{
+ struct evbuffer *b = arg;
+ int e, r = 0;
+ do {
+ r = evbuffer_read(b, fd, 1024);
+ if (r > 0) {
+ addfile_test_total_read += r;
+ TT_BLATHER(("Read %d/%d bytes", r, addfile_test_total_read));
+ }
+ } while (r > 0);
+ if (r < 0) {
+ e = evutil_socket_geterror(fd);
+ if (! EVUTIL_ERR_RW_RETRIABLE(e)) {
+ tt_fail_perror("read");
+ event_base_loopexit(addfile_test_event_base,NULL);
+ }
+ }
+ if (addfile_test_done_writing &&
+ addfile_test_total_read >= addfile_test_total_written) {
+ event_base_loopexit(addfile_test_event_base,NULL);
+ }
+}
static void
test_evbuffer_add_file(void *ptr)
{
- const char *impl = ptr;
- struct evbuffer *src = evbuffer_new();
- const char *data = "this is what we add as file system data.";
- size_t datalen;
+ struct basic_test_data *testdata = ptr;
+ const char *impl = testdata->setup_data;
+ struct evbuffer *src = evbuffer_new(), *dest = evbuffer_new();
+ char *tmpfilename = NULL;
+ char *data = NULL;
+ const char *expect_data;
+ size_t datalen, expect_len;
const char *compare;
int fd = -1;
+ int want_ismapping = -1, want_cansendfile = -1;
+ unsigned flags = 0;
+ int use_segment = 1, use_bigfile = 0, map_from_offset = 0,
+ view_from_offset = 0;
+ struct evbuffer_file_segment *seg = NULL;
+ ev_off_t starting_offset = 0, mapping_len = -1;
+ ev_off_t segment_offset = 0, segment_len = -1;
+ struct event *rev=NULL, *wev=NULL;
+ struct event_base *base = testdata->base;
evutil_socket_t pair[2] = {-1, -1};
- int r=0, n_written=0;
-
- /* Add a test for a big file. XXXX */
+ struct evutil_weakrand_state seed = { 123456789U };
+ /* This test is highly parameterized based on substrings of its
+ * argument. The strings are: */
tt_assert(impl);
- if (!strcmp(impl, "sendfile")) {
- if (!_evbuffer_testing_use_sendfile())
- tt_skip();
- TT_BLATHER(("Using sendfile-based implementaion"));
- } else if (!strcmp(impl, "mmap")) {
- if (!_evbuffer_testing_use_mmap())
- tt_skip();
- TT_BLATHER(("Using mmap-based implementaion"));
- } else if (!strcmp(impl, "linear")) {
- if (!_evbuffer_testing_use_linear_file_access())
- tt_skip();
- TT_BLATHER(("Using read-based implementaion"));
+ if (strstr(impl, "nosegment")) {
+ /* If nosegment is set, use the older evbuffer_add_file
+ * interface */
+ use_segment = 0;
+ }
+ if (strstr(impl, "bigfile")) {
+ /* If bigfile is set, use a 512K file. Else use a smaller
+ * one. */
+ use_bigfile = 1;
+ }
+ if (strstr(impl, "map_offset")) {
+ /* If map_offset is set, we build the file segment starting
+ * from a point other than byte 0 and ending somewhere other
+ * than the last byte. Otherwise we map the whole thing */
+ map_from_offset = 1;
+ }
+ if (strstr(impl, "offset_in_segment")) {
+ /* If offset_in_segment is set, we add a subsection of the
+ * file semgment starting from a point other than byte 0 of
+ * the segment. */
+ view_from_offset = 1;
+ }
+ if (strstr(impl, "sendfile")) {
+ /* If sendfile is set, we try to use a sendfile/splice style
+ * backend. */
+ flags = EVBUF_FS_DISABLE_MMAP;
+ want_cansendfile = 1;
+ want_ismapping = 0;
+ } else if (strstr(impl, "mmap")) {
+ /* If sendfile is set, we try to use a mmap/CreateFileMapping
+ * style backend. */
+ flags = EVBUF_FS_DISABLE_SENDFILE;
+ want_ismapping = 1;
+ want_cansendfile = 0;
+ } else if (strstr(impl, "linear")) {
+ /* If linear is set, we try to use a read-the-whole-thing
+ * backend. */
+ flags = EVBUF_FS_DISABLE_SENDFILE|EVBUF_FS_DISABLE_MMAP;
+ want_ismapping = 0;
+ want_cansendfile = 0;
+ } else if (strstr(impl, "default")) {
+ /* The caller doesn't care which backend we use. */
+ ;
} else {
+ /* The caller must choose a backend. */
TT_DIE(("Didn't recognize the implementation"));
}
+ if (use_bigfile) {
+ unsigned int i;
+ datalen = 1024*512;
+ data = malloc(1024*512);
+ tt_assert(data);
+ for (i = 0; i < datalen; ++i)
+ data[i] = (char)evutil_weakrand_(&seed);
+ } else {
+ data = strdup("here is a relatively small string.");
+ tt_assert(data);
+ datalen = strlen(data);
+ }
+
+ fd = regress_make_tmpfile(data, datalen, &tmpfilename);
+
+ if (map_from_offset) {
+ starting_offset = datalen/4 + 1;
+ mapping_len = datalen / 2 - 1;
+ expect_data = data + starting_offset;
+ expect_len = mapping_len;
+ } else {
+ expect_data = data;
+ expect_len = datalen;
+ }
+ if (view_from_offset) {
+ tt_assert(use_segment); /* Can't do this with add_file*/
+ segment_offset = expect_len / 3;
+ segment_len = expect_len / 2;
+ expect_data = expect_data + segment_offset;
+ expect_len = segment_len;
+ }
+
+ if (use_segment) {
+ seg = evbuffer_file_segment_new(fd, starting_offset,
+ mapping_len, flags);
+ tt_assert(seg);
+ if (want_ismapping >= 0) {
+ if (seg->is_mapping != (unsigned)want_ismapping)
+ tt_skip();
+ }
+ if (want_cansendfile >= 0) {
+ if (seg->can_sendfile != (unsigned)want_cansendfile)
+ tt_skip();
+ }
+ }
+
/* Say that it drains to a fd so that we can use sendfile. */
evbuffer_set_flags(src, EVBUFFER_FLAG_DRAINS_TO_FD);
-#if defined(_EVENT_HAVE_SENDFILE) && defined(__sun__) && defined(__svr4__)
+#if defined(EVENT__HAVE_SENDFILE) && defined(__sun__) && defined(__svr4__)
/* We need to use a pair of AF_INET sockets, since Solaris
doesn't support sendfile() over AF_UNIX. */
- if (evutil_ersatz_socketpair(AF_INET, SOCK_STREAM, 0, pair) == -1)
+ if (evutil_ersatz_socketpair_(AF_INET, SOCK_STREAM, 0, pair) == -1)
tt_abort_msg("ersatz_socketpair failed");
#else
if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
tt_abort_msg("socketpair failed");
#endif
-
- datalen = strlen(data);
- fd = regress_make_tmpfile(data, datalen);
+ evutil_make_socket_nonblocking(pair[0]);
+ evutil_make_socket_nonblocking(pair[1]);
tt_assert(fd != -1);
- tt_assert(evbuffer_add_file(src, fd, 0, datalen) != -1);
+ if (use_segment) {
+ tt_assert(evbuffer_add_file_segment(src, seg,
+ segment_offset, segment_len)!=-1);
+ } else {
+ tt_assert(evbuffer_add_file(src, fd, starting_offset,
+ mapping_len) != -1);
+ }
evbuffer_validate(src);
- while (evbuffer_get_length(src) &&
- (r = evbuffer_write(src, pair[0])) > 0) {
- evbuffer_validate(src);
- n_written += r;
- }
- tt_int_op(r, !=, -1);
- tt_int_op(n_written, ==, datalen);
+ addfile_test_event_base = base;
+ wev = event_new(base, pair[0], EV_WRITE|EV_PERSIST,
+ addfile_test_writecb, src);
+ rev = event_new(base, pair[1], EV_READ|EV_PERSIST,
+ addfile_test_readcb, dest);
+
+ event_add(wev, NULL);
+ event_add(rev, NULL);
+ event_base_dispatch(base);
evbuffer_validate(src);
- tt_int_op(evbuffer_read(src, pair[1], (int)strlen(data)), ==, datalen);
- evbuffer_validate(src);
- compare = (char *)evbuffer_pullup(src, datalen);
+ evbuffer_validate(dest);
+
+ tt_assert(addfile_test_done_writing);
+ tt_int_op(addfile_test_total_written, ==, expect_len);
+ tt_int_op(addfile_test_total_read, ==, expect_len);
+
+ compare = (char *)evbuffer_pullup(dest, expect_len);
tt_assert(compare != NULL);
- if (memcmp(compare, data, datalen))
+ if (memcmp(compare, expect_data, expect_len)) {
tt_abort_msg("Data from add_file differs.");
+ }
- evbuffer_validate(src);
+ evbuffer_validate(dest);
end:
+ if (data)
+ free(data);
+ if (seg)
+ evbuffer_file_segment_free(seg);
+ if (src)
+ evbuffer_free(src);
+ if (dest)
+ evbuffer_free(dest);
if (pair[0] >= 0)
evutil_closesocket(pair[0]);
if (pair[1] >= 0)
evutil_closesocket(pair[1]);
- evbuffer_free(src);
+ if (wev)
+ event_free(wev);
+ if (rev)
+ event_free(rev);
+ if (tmpfilename) {
+ unlink(tmpfilename);
+ free(tmpfilename);
+ }
+}
+
+static int file_segment_cleanup_cb_called_count = 0;
+static struct evbuffer_file_segment const* file_segment_cleanup_cb_called_with = NULL;
+static int file_segment_cleanup_cb_called_with_flags = 0;
+static void* file_segment_cleanup_cb_called_with_arg = NULL;
+static void
+file_segment_cleanup_cp(struct evbuffer_file_segment const* seg, int flags, void* arg)
+{
+ ++file_segment_cleanup_cb_called_count;
+ file_segment_cleanup_cb_called_with = seg;
+ file_segment_cleanup_cb_called_with_flags = flags;
+ file_segment_cleanup_cb_called_with_arg = arg;
+}
+
+static void
+test_evbuffer_file_segment_add_cleanup_cb(void* ptr)
+{
+ char *tmpfilename = NULL;
+ int fd = -1;
+ struct evbuffer *evb = NULL;
+ struct evbuffer_file_segment *seg = NULL, *segptr;
+ char const* arg = "token";
+
+ fd = regress_make_tmpfile("file_segment_test_file", 22, &tmpfilename);
+ tt_int_op(fd, >=, 0);
+
+ evb = evbuffer_new();
+ tt_assert(evb);
+
+ segptr = seg = evbuffer_file_segment_new(fd, 0, -1, 0);
+ tt_assert(seg);
+
+ evbuffer_file_segment_add_cleanup_cb(
+ seg, &file_segment_cleanup_cp, (void*)arg);
+
+ tt_assert(fd != -1);
+
+ tt_assert(evbuffer_add_file_segment(evb, seg, 0, -1)!=-1);
+
+ evbuffer_validate(evb);
+
+ tt_int_op(file_segment_cleanup_cb_called_count, ==, 0);
+ evbuffer_file_segment_free(seg);
+ seg = NULL; /* Prevent double-free. */
+
+ tt_int_op(file_segment_cleanup_cb_called_count, ==, 0);
+ evbuffer_free(evb);
+ evb = NULL; /* pevent double-free */
+
+ tt_int_op(file_segment_cleanup_cb_called_count, ==, 1);
+ tt_assert(file_segment_cleanup_cb_called_with == segptr);
+ tt_assert(file_segment_cleanup_cb_called_with_flags == 0);
+ tt_assert(file_segment_cleanup_cb_called_with_arg == (void*)arg);
+
+end:
+ if (evb)
+ evbuffer_free(evb);
+ if (seg)
+ evbuffer_file_segment_free(seg);
+ if (tmpfilename) {
+ unlink(tmpfilename);
+ free(tmpfilename);
+ }
}
-#ifndef _EVENT_DISABLE_MM_REPLACEMENT
+#ifndef EVENT__DISABLE_MM_REPLACEMENT
static void *
failing_malloc(size_t how_much)
{
@@ -855,6 +1090,32 @@ test_evbuffer_readln(void *ptr)
free(cp);
evbuffer_validate(evb);
+ /* Test NUL */
+ tt_int_op(evbuffer_get_length(evb), ==, 0);
+ {
+ char x[] =
+ "NUL\n\0\0"
+ "The all-zeros character which may serve\0"
+ "to accomplish time fill\0and media fill";
+ /* Add all but the final NUL of x. */
+ evbuffer_add(evb, x, sizeof(x)-1);
+ }
+ cp = evbuffer_readln(evb, &sz, EVBUFFER_EOL_NUL);
+ tt_line_eq("NUL\n");
+ free(cp);
+ cp = evbuffer_readln(evb, &sz, EVBUFFER_EOL_NUL);
+ tt_line_eq("");
+ free(cp);
+ cp = evbuffer_readln(evb, &sz, EVBUFFER_EOL_NUL);
+ tt_line_eq("The all-zeros character which may serve");
+ free(cp);
+ cp = evbuffer_readln(evb, &sz, EVBUFFER_EOL_NUL);
+ tt_line_eq("to accomplish time fill");
+ free(cp);
+ cp = evbuffer_readln(evb, &sz, EVBUFFER_EOL_NUL);
+ tt_ptr_op(cp, ==, NULL);
+ evbuffer_drain(evb, -1);
+
/* Test CRLF_STRICT - across boundaries*/
s = " and a bad crlf\nand a good one\r";
evbuffer_add(evb_tmp, s, strlen(s));
@@ -907,7 +1168,7 @@ test_evbuffer_readln(void *ptr)
evbuffer_validate(evb);
/* the next call to readline should fail */
-#ifndef _EVENT_DISABLE_MM_REPLACEMENT
+#ifndef EVENT__DISABLE_MM_REPLACEMENT
event_set_mem_functions(failing_malloc, realloc, free);
cp = evbuffer_readln(evb, &sz, EVBUFFER_EOL_LF);
tt_assert(cp == NULL);
@@ -974,6 +1235,12 @@ test_evbuffer_search_eol(void *ptr)
tt_int_op(ptr2.pos, ==, 11);
tt_int_op(eol_len, ==, 1);
+ tt_assert(evbuffer_ptr_set(buf, &ptr1, evbuffer_get_length(buf), EVBUFFER_PTR_SET) == 0);
+ eol_len = -1;
+ ptr2 = evbuffer_search_eol(buf, &ptr1, &eol_len, EVBUFFER_EOL_LF);
+ tt_int_op(ptr2.pos, ==, -1);
+ tt_int_op(eol_len, ==, 0);
+
end:
evbuffer_free(buf);
}
@@ -1078,6 +1345,15 @@ test_evbuffer_ptr_set(void *ptr)
tt_assert(buf);
+ tt_int_op(evbuffer_get_length(buf), ==, 0);
+
+ tt_assert(evbuffer_ptr_set(buf, &pos, 0, EVBUFFER_PTR_SET) == 0);
+ tt_assert(pos.pos == 0);
+ tt_assert(evbuffer_ptr_set(buf, &pos, 1, EVBUFFER_PTR_ADD) == -1);
+ tt_assert(pos.pos == -1);
+ tt_assert(evbuffer_ptr_set(buf, &pos, 1, EVBUFFER_PTR_SET) == -1);
+ tt_assert(pos.pos == -1);
+
/* create some chains */
evbuffer_reserve_space(buf, 5000, v, 1);
v[0].iov_len = 5000;
@@ -1110,6 +1386,8 @@ test_evbuffer_ptr_set(void *ptr)
tt_assert(pos.pos == 10000);
tt_assert(evbuffer_ptr_set(buf, &pos, 1000, EVBUFFER_PTR_ADD) == 0);
tt_assert(pos.pos == 11000);
+ tt_assert(evbuffer_ptr_set(buf, &pos, 1000, EVBUFFER_PTR_ADD) == 0);
+ tt_assert(pos.pos == 12000);
tt_assert(evbuffer_ptr_set(buf, &pos, 1000, EVBUFFER_PTR_ADD) == -1);
tt_assert(pos.pos == -1);
@@ -1128,6 +1406,18 @@ test_evbuffer_search(void *ptr)
tt_assert(buf);
tt_assert(tmp);
+ pos = evbuffer_search(buf, "x", 1, NULL);
+ tt_int_op(pos.pos, ==, -1);
+ tt_assert(evbuffer_ptr_set(buf, &pos, 0, EVBUFFER_PTR_SET) == 0);
+ pos = evbuffer_search(buf, "x", 1, &pos);
+ tt_int_op(pos.pos, ==, -1);
+ tt_assert(evbuffer_ptr_set(buf, &pos, 0, EVBUFFER_PTR_SET) == 0);
+ pos = evbuffer_search_range(buf, "x", 1, &pos, &pos);
+ tt_int_op(pos.pos, ==, -1);
+ tt_assert(evbuffer_ptr_set(buf, &pos, 0, EVBUFFER_PTR_SET) == 0);
+ pos = evbuffer_search_range(buf, "x", 1, &pos, NULL);
+ tt_int_op(pos.pos, ==, -1);
+
/* set up our chains */
evbuffer_add_printf(tmp, "hello"); /* 5 chars */
evbuffer_add_buffer(buf, tmp);
@@ -1171,6 +1461,20 @@ test_evbuffer_search(void *ptr)
pos = evbuffer_search_range(buf, "ack", 3, NULL, &end);
tt_int_op(pos.pos, ==, -1);
+ /* Set "end" after the last byte in the buffer. */
+ tt_assert(evbuffer_ptr_set(buf, &end, 17, EVBUFFER_PTR_SET) == 0);
+
+ pos = evbuffer_search_range(buf, "attack", 6, NULL, &end);
+ tt_int_op(pos.pos, ==, 11);
+ tt_assert(evbuffer_ptr_set(buf, &pos, 11, EVBUFFER_PTR_SET) == 0);
+ pos = evbuffer_search_range(buf, "attack", 6, &pos, &end);
+ tt_int_op(pos.pos, ==, 11);
+ tt_assert(evbuffer_ptr_set(buf, &pos, 17, EVBUFFER_PTR_SET) == 0);
+ pos = evbuffer_search_range(buf, "attack", 6, &pos, &end);
+ tt_int_op(pos.pos, ==, -1);
+ tt_assert(evbuffer_ptr_set(buf, &pos, 17, EVBUFFER_PTR_SET) == 0);
+ pos = evbuffer_search_range(buf, "attack", 6, &pos, NULL);
+ tt_int_op(pos.pos, ==, -1);
end:
if (buf)
@@ -1234,9 +1538,9 @@ test_evbuffer_callbacks(void *ptr)
tt_assert(!evbuffer_remove_cb(buf, log_change_callback, buf_out2));
evbuffer_validate(buf);
- tt_str_op(evbuffer_pullup(buf_out1, -1), ==,
+ tt_str_op((const char *) evbuffer_pullup(buf_out1, -1), ==,
"0->36; 36->26; 26->31; 31->38; ");
- tt_str_op(evbuffer_pullup(buf_out2, -1), ==,
+ tt_str_op((const char *) evbuffer_pullup(buf_out2, -1), ==,
"0->36; 31->38; 38->0; 0->1; ");
evbuffer_drain(buf_out1, evbuffer_get_length(buf_out1));
evbuffer_drain(buf_out2, evbuffer_get_length(buf_out2));
@@ -1252,7 +1556,7 @@ test_evbuffer_callbacks(void *ptr)
tt_uint_op(evbuffer_get_length(buf_out2), ==, 0);
evbuffer_setcb(buf, NULL, NULL);
evbuffer_add_printf(buf, "This will not.");
- tt_str_op(evbuffer_pullup(buf, -1), ==, "This will not.");
+ tt_str_op((const char *) evbuffer_pullup(buf, -1), ==, "This will not.");
evbuffer_validate(buf);
evbuffer_drain(buf, evbuffer_get_length(buf));
evbuffer_validate(buf);
@@ -1360,7 +1664,7 @@ test_evbuffer_add_reference(void *ptr)
evbuffer_add(buf1, "You shake and shake the ", 24);
evbuffer_add_reference(buf1, "ketchup bottle", 14, ref_done_cb,
(void*)3333);
- evbuffer_add(buf1, ". Nothing comes and then a lot'll.", 42);
+ evbuffer_add(buf1, ". Nothing comes and then a lot'll.", 35);
evbuffer_free(buf1);
buf1 = NULL;
tt_int_op(ref_done_cb_called_count, ==, 3);
@@ -1373,6 +1677,111 @@ end:
evbuffer_free(buf2);
}
+static void
+test_evbuffer_multicast(void *ptr)
+{
+ const char chunk1[] = "If you have found the answer to such a problem";
+ const char chunk2[] = "you ought to write it up for publication";
+ /* -- Knuth's "Notes on the Exercises" from TAOCP */
+ char tmp[16];
+ size_t len1 = strlen(chunk1), len2=strlen(chunk2);
+
+ struct evbuffer *buf1 = NULL, *buf2 = NULL;
+
+ buf1 = evbuffer_new();
+ tt_assert(buf1);
+
+ evbuffer_add(buf1, chunk1, len1);
+ evbuffer_add(buf1, ", ", 2);
+ evbuffer_add(buf1, chunk2, len2);
+ tt_int_op(evbuffer_get_length(buf1), ==, len1+len2+2);
+
+ buf2 = evbuffer_new();
+ tt_assert(buf2);
+
+ tt_int_op(evbuffer_add_buffer_reference(buf2, buf1), ==, 0);
+ /* nested references are not allowed */
+ tt_int_op(evbuffer_add_buffer_reference(buf2, buf2), ==, -1);
+ tt_int_op(evbuffer_add_buffer_reference(buf1, buf2), ==, -1);
+
+ /* both buffers contain the same amount of data */
+ tt_int_op(evbuffer_get_length(buf1), ==, evbuffer_get_length(buf1));
+
+ /* Make sure we can drain a little from the first buffer. */
+ tt_int_op(evbuffer_remove(buf1, tmp, 6), ==, 6);
+ tt_int_op(memcmp(tmp, "If you", 6), ==, 0);
+ tt_int_op(evbuffer_remove(buf1, tmp, 5), ==, 5);
+ tt_int_op(memcmp(tmp, " have", 5), ==, 0);
+
+ /* Make sure that prepending does not meddle with immutable data */
+ tt_int_op(evbuffer_prepend(buf1, "I have ", 7), ==, 0);
+ tt_int_op(memcmp(chunk1, "If you", 6), ==, 0);
+ evbuffer_validate(buf1);
+
+ /* Make sure we can drain a little from the second buffer. */
+ tt_int_op(evbuffer_remove(buf2, tmp, 6), ==, 6);
+ tt_int_op(memcmp(tmp, "If you", 6), ==, 0);
+ tt_int_op(evbuffer_remove(buf2, tmp, 5), ==, 5);
+ tt_int_op(memcmp(tmp, " have", 5), ==, 0);
+
+ /* Make sure that prepending does not meddle with immutable data */
+ tt_int_op(evbuffer_prepend(buf2, "I have ", 7), ==, 0);
+ tt_int_op(memcmp(chunk1, "If you", 6), ==, 0);
+ evbuffer_validate(buf2);
+
+ /* Make sure the data can be read from the second buffer when the first is freed */
+ evbuffer_free(buf1);
+ buf1 = NULL;
+
+ tt_int_op(evbuffer_remove(buf2, tmp, 6), ==, 6);
+ tt_int_op(memcmp(tmp, "I have", 6), ==, 0);
+
+ tt_int_op(evbuffer_remove(buf2, tmp, 6), ==, 6);
+ tt_int_op(memcmp(tmp, " foun", 6), ==, 0);
+
+end:
+ if (buf1)
+ evbuffer_free(buf1);
+ if (buf2)
+ evbuffer_free(buf2);
+}
+
+static void
+test_evbuffer_multicast_drain(void *ptr)
+{
+ const char chunk1[] = "If you have found the answer to such a problem";
+ const char chunk2[] = "you ought to write it up for publication";
+ /* -- Knuth's "Notes on the Exercises" from TAOCP */
+ size_t len1 = strlen(chunk1), len2=strlen(chunk2);
+
+ struct evbuffer *buf1 = NULL, *buf2 = NULL;
+
+ buf1 = evbuffer_new();
+ tt_assert(buf1);
+
+ evbuffer_add(buf1, chunk1, len1);
+ evbuffer_add(buf1, ", ", 2);
+ evbuffer_add(buf1, chunk2, len2);
+ tt_int_op(evbuffer_get_length(buf1), ==, len1+len2+2);
+
+ buf2 = evbuffer_new();
+ tt_assert(buf2);
+
+ tt_int_op(evbuffer_add_buffer_reference(buf2, buf1), ==, 0);
+ tt_int_op(evbuffer_get_length(buf2), ==, len1+len2+2);
+ tt_int_op(evbuffer_drain(buf1, evbuffer_get_length(buf1)), ==, 0);
+ tt_int_op(evbuffer_get_length(buf2), ==, len1+len2+2);
+ tt_int_op(evbuffer_drain(buf2, evbuffer_get_length(buf2)), ==, 0);
+ evbuffer_validate(buf1);
+ evbuffer_validate(buf2);
+
+end:
+ if (buf1)
+ evbuffer_free(buf1);
+ if (buf2)
+ evbuffer_free(buf2);
+}
+
/* Some cases that we didn't get in test_evbuffer() above, for more coverage. */
static void
test_evbuffer_prepend(void *ptr)
@@ -1403,6 +1812,7 @@ test_evbuffer_prepend(void *ptr)
evbuffer_prepend(buf1, "It is no longer true to say ", 28);
evbuffer_validate(buf1);
n = evbuffer_remove(buf1, tmp, sizeof(tmp)-1);
+ tt_int_op(n, >=, 0);
tmp[n]='\0';
tt_str_op(tmp,==,"It is no longer true to say it has 29 characters");
@@ -1421,6 +1831,7 @@ test_evbuffer_prepend(void *ptr)
evbuffer_validate(buf2);
evbuffer_validate(buf1);
n = evbuffer_remove(buf2, tmp, sizeof(tmp)-1);
+ tt_int_op(n, >=, 0);
tmp[n]='\0';
tt_str_op(tmp,==,"Here is string 1000. Here is string 999. ");
@@ -1433,6 +1844,42 @@ end:
}
static void
+test_evbuffer_peek_first_gt(void *info)
+{
+ struct evbuffer *buf = NULL, *tmp_buf = NULL;
+ struct evbuffer_ptr ptr;
+ struct evbuffer_iovec v[2];
+
+ buf = evbuffer_new();
+ tmp_buf = evbuffer_new();
+ evbuffer_add_printf(tmp_buf, "Contents of chunk 100\n");
+ evbuffer_add_buffer(buf, tmp_buf);
+ evbuffer_add_printf(tmp_buf, "Contents of chunk 1\n");
+ evbuffer_add_buffer(buf, tmp_buf);
+
+ evbuffer_ptr_set(buf, &ptr, 0, EVBUFFER_PTR_SET);
+
+ /** The only case that matters*/
+ tt_int_op(evbuffer_peek(buf, -1, &ptr, NULL, 0), ==, 2);
+ /** Just in case */
+ tt_int_op(evbuffer_peek(buf, -1, &ptr, v, 2), ==, 2);
+
+ evbuffer_ptr_set(buf, &ptr, 20, EVBUFFER_PTR_ADD);
+ tt_int_op(evbuffer_peek(buf, -1, &ptr, NULL, 0), ==, 2);
+ tt_int_op(evbuffer_peek(buf, -1, &ptr, v, 2), ==, 2);
+ tt_int_op(evbuffer_peek(buf, 2, &ptr, NULL, 0), ==, 1);
+ tt_int_op(evbuffer_peek(buf, 2, &ptr, v, 2), ==, 1);
+ tt_int_op(evbuffer_peek(buf, 3, &ptr, NULL, 0), ==, 2);
+ tt_int_op(evbuffer_peek(buf, 3, &ptr, v, 2), ==, 2);
+
+end:
+ if (buf)
+ evbuffer_free(buf);
+ if (tmp_buf)
+ evbuffer_free(tmp_buf);
+}
+
+static void
test_evbuffer_peek(void *info)
{
struct evbuffer *buf = NULL, *tmp_buf = NULL;
@@ -1529,6 +1976,13 @@ test_evbuffer_peek(void *info)
tt_iov_eq(&v[0], "Contents of chunk [2]\n");
tt_iov_eq(&v[1], "Contents of chunk [3]\n"); /*more than we asked for*/
+ /* peek at the end of the buffer */
+ memset(v, 0, sizeof(v));
+ tt_assert(evbuffer_ptr_set(buf, &ptr, evbuffer_get_length(buf), EVBUFFER_PTR_SET) == 0);
+ i = evbuffer_peek(buf, 44, &ptr, v, 20);
+ tt_int_op(i, ==, 0);
+ tt_assert(v[0].iov_base == NULL);
+
end:
if (buf)
evbuffer_free(buf);
@@ -1586,7 +2040,7 @@ test_evbuffer_freeze(void *ptr)
FREEZE_EQ(r, 0, -1);
r = evbuffer_reserve_space(buf, 10, v, 1);
FREEZE_EQ(r, 1, -1);
- if (r == 0) {
+ if (r == 1) {
memset(v[0].iov_base, 'X', 10);
v[0].iov_len = 10;
}
@@ -1627,6 +2081,133 @@ end:
evbuffer_free(tmp_buf);
}
+static void
+test_evbuffer_add_iovec(void * ptr)
+{
+ struct evbuffer * buf = NULL;
+ struct evbuffer_iovec vec[4];
+ const char * data[] = {
+ "Guilt resembles a sword with two edges.",
+ "On the one hand, it cuts for Justice, imposing practical morality upon those who fear it.",
+ "Conscience does not always adhere to rational judgment.",
+ "Guilt is always a self-imposed burden, but it is not always rightly imposed."
+ /* -- R.A. Salvatore, _Sojurn_ */
+ };
+ size_t expected_length = 0;
+ size_t returned_length = 0;
+ int i;
+
+ buf = evbuffer_new();
+
+ tt_assert(buf);
+
+ for (i = 0; i < 4; i++) {
+ vec[i].iov_len = strlen(data[i]);
+ vec[i].iov_base = (char*) data[i];
+ expected_length += vec[i].iov_len;
+ }
+
+ returned_length = evbuffer_add_iovec(buf, vec, 4);
+
+ tt_int_op(returned_length, ==, evbuffer_get_length(buf));
+ tt_int_op(evbuffer_get_length(buf), ==, expected_length);
+
+ for (i = 0; i < 4; i++) {
+ char charbuf[1024];
+
+ memset(charbuf, 0, 1024);
+ evbuffer_remove(buf, charbuf, strlen(data[i]));
+ tt_assert(strcmp(charbuf, data[i]) == 0);
+ }
+
+ tt_assert(evbuffer_get_length(buf) == 0);
+end:
+ if (buf) {
+ evbuffer_free(buf);
+ }
+}
+
+static void
+test_evbuffer_copyout(void *dummy)
+{
+ const char string[] =
+ "Still they skirmish to and fro, men my messmates on the snow "
+ "When we headed off the aurochs turn for turn; "
+ "When the rich Allobrogenses never kept amanuenses, "
+ "And our only plots were piled in lakes at Berne.";
+ /* -- Kipling, "In The Neolithic Age" */
+ char tmp[1024];
+ struct evbuffer_ptr ptr;
+ struct evbuffer *buf;
+
+ (void)dummy;
+
+ buf = evbuffer_new();
+ tt_assert(buf);
+
+ tt_int_op(strlen(string), ==, 206);
+
+ /* Ensure separate chains */
+ evbuffer_add_reference(buf, string, 80, no_cleanup, NULL);
+ evbuffer_add_reference(buf, string+80, 80, no_cleanup, NULL);
+ evbuffer_add(buf, string+160, strlen(string)-160);
+
+ tt_int_op(206, ==, evbuffer_get_length(buf));
+
+ /* First, let's test plain old copyout. */
+
+ /* Copy a little from the beginning. */
+ tt_int_op(10, ==, evbuffer_copyout(buf, tmp, 10));
+ tt_int_op(0, ==, memcmp(tmp, "Still they", 10));
+
+ /* Now copy more than a little from the beginning */
+ memset(tmp, 0, sizeof(tmp));
+ tt_int_op(100, ==, evbuffer_copyout(buf, tmp, 100));
+ tt_int_op(0, ==, memcmp(tmp, string, 100));
+
+ /* Copy too much; ensure truncation. */
+ memset(tmp, 0, sizeof(tmp));
+ tt_int_op(206, ==, evbuffer_copyout(buf, tmp, 230));
+ tt_int_op(0, ==, memcmp(tmp, string, 206));
+
+ /* That was supposed to be nondestructive, btw */
+ tt_int_op(206, ==, evbuffer_get_length(buf));
+
+ /* Now it's time to test copyout_from! First, let's start in the
+ * first chain. */
+ evbuffer_ptr_set(buf, &ptr, 15, EVBUFFER_PTR_SET);
+ memset(tmp, 0, sizeof(tmp));
+ tt_int_op(10, ==, evbuffer_copyout_from(buf, &ptr, tmp, 10));
+ tt_int_op(0, ==, memcmp(tmp, "mish to an", 10));
+
+ /* Right up to the end of the first chain */
+ memset(tmp, 0, sizeof(tmp));
+ tt_int_op(65, ==, evbuffer_copyout_from(buf, &ptr, tmp, 65));
+ tt_int_op(0, ==, memcmp(tmp, string+15, 65));
+
+ /* Span into the second chain */
+ memset(tmp, 0, sizeof(tmp));
+ tt_int_op(90, ==, evbuffer_copyout_from(buf, &ptr, tmp, 90));
+ tt_int_op(0, ==, memcmp(tmp, string+15, 90));
+
+ /* Span into the third chain */
+ memset(tmp, 0, sizeof(tmp));
+ tt_int_op(160, ==, evbuffer_copyout_from(buf, &ptr, tmp, 160));
+ tt_int_op(0, ==, memcmp(tmp, string+15, 160));
+
+ /* Overrun */
+ memset(tmp, 0, sizeof(tmp));
+ tt_int_op(206-15, ==, evbuffer_copyout_from(buf, &ptr, tmp, 999));
+ tt_int_op(0, ==, memcmp(tmp, string+15, 206-15));
+
+ /* That was supposed to be nondestructive, too */
+ tt_int_op(206, ==, evbuffer_get_length(buf));
+
+end:
+ if (buf)
+ evbuffer_free(buf);
+}
+
static void *
setup_passthrough(const struct testcase_t *testcase)
{
@@ -1661,17 +2242,40 @@ struct testcase_t evbuffer_testcases[] = {
{ "search", test_evbuffer_search, 0, NULL, NULL },
{ "callbacks", test_evbuffer_callbacks, 0, NULL, NULL },
{ "add_reference", test_evbuffer_add_reference, 0, NULL, NULL },
+ { "multicast", test_evbuffer_multicast, 0, NULL, NULL },
+ { "multicast_drain", test_evbuffer_multicast_drain, 0, NULL, NULL },
{ "prepend", test_evbuffer_prepend, TT_FORK, NULL, NULL },
{ "peek", test_evbuffer_peek, 0, NULL, NULL },
+ { "peek_first_gt", test_evbuffer_peek_first_gt, 0, NULL, NULL },
{ "freeze_start", test_evbuffer_freeze, 0, &nil_setup, (void*)"start" },
{ "freeze_end", test_evbuffer_freeze, 0, &nil_setup, (void*)"end" },
- /* TODO: need a temp file implementation for Windows */
- { "add_file_sendfile", test_evbuffer_add_file, TT_FORK, &nil_setup,
- (void*)"sendfile" },
- { "add_file_mmap", test_evbuffer_add_file, TT_FORK, &nil_setup,
- (void*)"mmap" },
- { "add_file_linear", test_evbuffer_add_file, TT_FORK, &nil_setup,
- (void*)"linear" },
+ { "add_iovec", test_evbuffer_add_iovec, 0, NULL, NULL},
+ { "copyout", test_evbuffer_copyout, 0, NULL, NULL},
+ { "file_segment_add_cleanup_cb", test_evbuffer_file_segment_add_cleanup_cb, 0, NULL, NULL },
+
+#define ADDFILE_TEST(name, parameters) \
+ { name, test_evbuffer_add_file, TT_FORK|TT_NEED_BASE, \
+ &basic_setup, (void*)(parameters) }
+
+#define ADDFILE_TEST_GROUP(name, parameters) \
+ ADDFILE_TEST(name "_sendfile", "sendfile " parameters), \
+ ADDFILE_TEST(name "_mmap", "mmap " parameters), \
+ ADDFILE_TEST(name "_linear", "linear " parameters)
+
+ ADDFILE_TEST_GROUP("add_file", ""),
+ ADDFILE_TEST("add_file_nosegment", "default nosegment"),
+
+ ADDFILE_TEST_GROUP("add_big_file", "bigfile"),
+ ADDFILE_TEST("add_big_file_nosegment", "default nosegment bigfile"),
+
+ ADDFILE_TEST_GROUP("add_file_offset", "bigfile map_offset"),
+ ADDFILE_TEST("add_file_offset_nosegment",
+ "default nosegment bigfile map_offset"),
+
+ ADDFILE_TEST_GROUP("add_file_offset2", "bigfile offset_in_segment"),
+
+ ADDFILE_TEST_GROUP("add_file_offset3",
+ "bigfile offset_in_segment map_offset"),
END_OF_TESTCASES
};
diff --git a/test/regress_bufferevent.c b/test/regress_bufferevent.c
index 19ee4e3a..9cc453fe 100644
--- a/test/regress_bufferevent.c
+++ b/test/regress_bufferevent.c
@@ -24,11 +24,12 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "util-internal.h"
/* The old tests here need assertions to work. */
#undef NDEBUG
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#endif
@@ -37,11 +38,11 @@
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
@@ -57,7 +58,7 @@
#include <errno.h>
#include <assert.h>
-#ifdef _EVENT_HAVE_ARPA_INET_H
+#ifdef EVENT__HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
@@ -75,7 +76,7 @@
#include "bufferevent-internal.h"
#include "util-internal.h"
-#ifdef WIN32
+#ifdef _WIN32
#include "iocp-internal.h"
#endif
@@ -132,7 +133,7 @@ test_bufferevent_impl(int use_pair)
tt_assert(0 == bufferevent_pair_new(NULL, 0, pair));
bev1 = pair[0];
bev2 = pair[1];
- bufferevent_setcb(bev1, readcb, writecb, errorcb, NULL);
+ bufferevent_setcb(bev1, readcb, writecb, errorcb, bev1);
bufferevent_setcb(bev2, readcb, writecb, errorcb, NULL);
tt_int_op(bufferevent_getfd(bev1), ==, -1);
tt_ptr_op(bufferevent_get_underlying(bev1), ==, NULL);
@@ -147,6 +148,18 @@ test_bufferevent_impl(int use_pair)
tt_ptr_op(bufferevent_pair_get_partner(bev2), ==, NULL);
}
+ {
+ /* Test getcb. */
+ bufferevent_data_cb r, w;
+ bufferevent_event_cb e;
+ void *a;
+ bufferevent_getcb(bev1, &r, &w, &e, &a);
+ tt_ptr_op(r, ==, readcb);
+ tt_ptr_op(w, ==, writecb);
+ tt_ptr_op(e, ==, errorcb);
+ tt_ptr_op(a, ==, use_pair ? bev1 : NULL);
+ }
+
bufferevent_disable(bev1, EV_READ);
bufferevent_enable(bev2, EV_READ);
@@ -230,6 +243,7 @@ test_bufferevent_watermarks_impl(int use_pair)
{
struct bufferevent *bev1 = NULL, *bev2 = NULL;
char buffer[65000];
+ size_t low, high;
int i;
test_ok = 0;
@@ -249,16 +263,35 @@ test_bufferevent_watermarks_impl(int use_pair)
bufferevent_disable(bev1, EV_READ);
bufferevent_enable(bev2, EV_READ);
+ /* By default, low watermarks are set to 0 */
+ bufferevent_getwatermark(bev1, EV_READ, &low, NULL);
+ tt_int_op(low, ==, 0);
+ bufferevent_getwatermark(bev2, EV_WRITE, &low, NULL);
+ tt_int_op(low, ==, 0);
+
for (i = 0; i < (int)sizeof(buffer); i++)
buffer[i] = (char)i;
/* limit the reading on the receiving bufferevent */
bufferevent_setwatermark(bev2, EV_READ, 10, 20);
+ bufferevent_getwatermark(bev2, EV_READ, &low, &high);
+ tt_int_op(low, ==, 10);
+ tt_int_op(high, ==, 20);
+
/* Tell the sending bufferevent not to notify us till it's down to
100 bytes. */
bufferevent_setwatermark(bev1, EV_WRITE, 100, 2000);
+ bufferevent_getwatermark(bev1, EV_WRITE, &low, &high);
+ tt_int_op(low, ==, 100);
+ tt_int_op(high, ==, 2000);
+
+ {
+ int r = bufferevent_getwatermark(bev1, EV_WRITE | EV_READ, &low, &high);
+ tt_int_op(r, !=, 0);
+ }
+
bufferevent_write(bev1, buffer, sizeof(buffer));
event_dispatch();
@@ -407,6 +440,7 @@ sender_writecb(struct bufferevent *bev, void *ctx)
{
if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
bufferevent_disable(bev,EV_READ|EV_WRITE);
+ TT_BLATHER(("Flushed %d: freeing it.", (int)bufferevent_getfd(bev)));
bufferevent_free(bev);
}
}
@@ -418,6 +452,7 @@ sender_errorcb(struct bufferevent *bev, short what, void *ctx)
}
static int bufferevent_connect_test_flags = 0;
+static int bufferevent_trigger_test_flags = 0;
static int n_strings_read = 0;
static int n_reads_invoked = 0;
@@ -449,16 +484,20 @@ reader_eventcb(struct bufferevent *bev, short what, void *ctx)
return;
}
if (what & BEV_EVENT_CONNECTED) {
+ TT_BLATHER(("connected on %d", (int)bufferevent_getfd(bev)));
bufferevent_enable(bev, EV_READ);
}
if (what & BEV_EVENT_EOF) {
char buf[512];
size_t n;
n = bufferevent_read(bev, buf, sizeof(buf)-1);
+ tt_int_op(n, >=, 0);
buf[n] = '\0';
tt_str_op(buf, ==, TEST_STR);
if (++n_strings_read == 2)
event_base_loopexit(base, NULL);
+ TT_BLATHER(("EOF on %d: %d strings read.",
+ (int)bufferevent_getfd(bev), n_strings_read));
}
end:
;
@@ -467,6 +506,7 @@ end:
static void
reader_readcb(struct bufferevent *bev, void *ctx)
{
+ TT_BLATHER(("Read invoked on %d.", (int)bufferevent_getfd(bev)));
n_reads_invoked++;
}
@@ -493,11 +533,11 @@ test_bufferevent_connect(void *arg)
be_flags |= BEV_OPT_THREADSAFE;
}
bufferevent_connect_test_flags = be_flags;
-#ifdef WIN32
+#ifdef _WIN32
if (!strcmp((char*)data->setup_data, "unset_connectex")) {
struct win32_extension_fns *ext =
(struct win32_extension_fns *)
- event_get_win32_extension_fns();
+ event_get_win32_extension_fns_();
ext->ConnectEx = NULL;
}
#endif
@@ -581,7 +621,7 @@ close_socket_cb(evutil_socket_t fd, short what, void *arg)
static void
test_bufferevent_connect_fail(void *arg)
{
- struct basic_test_data *data = arg;
+ struct basic_test_data *data = (struct basic_test_data *)arg;
struct bufferevent *bev=NULL;
struct sockaddr_in localhost;
struct sockaddr *sa = (struct sockaddr*)&localhost;
@@ -754,8 +794,8 @@ test_bufferevent_timeouts(void *arg)
bufferevent_set_timeouts(bev2, &tv_r, &tv_w);
bufferevent_enable(bev2, EV_WRITE);
- tv_r.tv_sec = 1;
- tv_r.tv_usec = 0;
+ tv_r.tv_sec = 0;
+ tv_r.tv_usec = 350000;
event_base_loopexit(data->base, &tv_r);
event_base_dispatch(data->base);
@@ -779,6 +819,132 @@ end:
bufferevent_free(bev2);
}
+static void
+trigger_failure_cb(evutil_socket_t fd, short what, void *ctx)
+{
+ TT_FAIL(("The triggered callback did not fire or the machine is really slow (try increasing timeout)."));
+}
+
+static void
+trigger_eventcb(struct bufferevent *bev, short what, void *ctx)
+{
+ struct event_base *base = ctx;
+ if (what == ~0) {
+ TT_BLATHER(("Event successfully triggered."));
+ event_base_loopexit(base, NULL);
+ return;
+ }
+ reader_eventcb(bev, what, ctx);
+}
+
+static void
+trigger_readcb_triggered(struct bufferevent *bev, void *ctx)
+{
+ TT_BLATHER(("Read successfully triggered."));
+ n_reads_invoked++;
+ bufferevent_trigger_event(bev, ~0, bufferevent_trigger_test_flags);
+}
+
+static void
+trigger_readcb(struct bufferevent *bev, void *ctx)
+{
+ struct timeval timeout = { 30, 0 };
+ struct event_base *base = ctx;
+ size_t low, high, len;
+ int expected_reads;
+
+ TT_BLATHER(("Read invoked on %d.", (int)bufferevent_getfd(bev)));
+ expected_reads = ++n_reads_invoked;
+
+ bufferevent_setcb(bev, trigger_readcb_triggered, NULL, trigger_eventcb, ctx);
+
+ bufferevent_getwatermark(bev, EV_READ, &low, &high);
+ len = evbuffer_get_length(bufferevent_get_input(bev));
+
+ bufferevent_setwatermark(bev, EV_READ, len + 1, 0);
+ bufferevent_trigger(bev, EV_READ, bufferevent_trigger_test_flags);
+ /* no callback expected */
+ tt_int_op(n_reads_invoked, ==, expected_reads);
+
+ if ((bufferevent_trigger_test_flags & BEV_TRIG_DEFER_CALLBACKS) ||
+ (bufferevent_connect_test_flags & BEV_OPT_DEFER_CALLBACKS)) {
+ /* will be deferred */
+ } else {
+ expected_reads++;
+ }
+
+ event_base_once(base, -1, EV_TIMEOUT, trigger_failure_cb, NULL, &timeout);
+
+ bufferevent_trigger(bev, EV_READ,
+ bufferevent_trigger_test_flags | BEV_TRIG_IGNORE_WATERMARKS);
+ tt_int_op(n_reads_invoked, ==, expected_reads);
+
+ bufferevent_setwatermark(bev, EV_READ, low, high);
+end:
+ ;
+}
+
+static void
+test_bufferevent_trigger(void *arg)
+{
+ struct basic_test_data *data = arg;
+ struct evconnlistener *lev=NULL;
+ struct bufferevent *bev=NULL;
+ struct sockaddr_in localhost;
+ struct sockaddr_storage ss;
+ struct sockaddr *sa;
+ ev_socklen_t slen;
+
+ int be_flags=BEV_OPT_CLOSE_ON_FREE;
+ int trig_flags=0;
+
+ if (strstr((char*)data->setup_data, "defer")) {
+ be_flags |= BEV_OPT_DEFER_CALLBACKS;
+ }
+ bufferevent_connect_test_flags = be_flags;
+
+ if (strstr((char*)data->setup_data, "postpone")) {
+ trig_flags |= BEV_TRIG_DEFER_CALLBACKS;
+ }
+ bufferevent_trigger_test_flags = trig_flags;
+
+ memset(&localhost, 0, sizeof(localhost));
+
+ localhost.sin_port = 0; /* pick-a-port */
+ localhost.sin_addr.s_addr = htonl(0x7f000001L);
+ localhost.sin_family = AF_INET;
+ sa = (struct sockaddr *)&localhost;
+ lev = evconnlistener_new_bind(data->base, listen_cb, data->base,
+ LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
+ 16, sa, sizeof(localhost));
+ tt_assert(lev);
+
+ sa = (struct sockaddr *)&ss;
+ slen = sizeof(ss);
+ if (regress_get_listener_addr(lev, sa, &slen) < 0) {
+ tt_abort_perror("getsockname");
+ }
+
+ tt_assert(!evconnlistener_enable(lev));
+ bev = bufferevent_socket_new(data->base, -1, be_flags);
+ tt_assert(bev);
+ bufferevent_setcb(bev, trigger_readcb, NULL, trigger_eventcb, data->base);
+
+ bufferevent_enable(bev, EV_READ);
+
+ tt_want(!bufferevent_socket_connect(bev, sa, sizeof(localhost)));
+
+ event_base_dispatch(data->base);
+
+ tt_int_op(n_reads_invoked, ==, 2);
+end:
+ if (lev)
+ evconnlistener_free(lev);
+
+ if (bev)
+ bufferevent_free(bev);
+}
+
struct testcase_t bufferevent_testcases[] = {
LEGACY(bufferevent, TT_ISOLATED),
@@ -809,7 +975,17 @@ struct testcase_t bufferevent_testcases[] = {
TT_FORK|TT_NEED_BASE, &basic_setup, (void*)"filter" },
{ "bufferevent_timeout_filter_pair", test_bufferevent_timeouts,
TT_FORK|TT_NEED_BASE, &basic_setup, (void*)"filter pair" },
-#ifdef _EVENT_HAVE_LIBZ
+ { "bufferevent_trigger", test_bufferevent_trigger, TT_FORK|TT_NEED_BASE,
+ &basic_setup, (void*)"" },
+ { "bufferevent_trigger_defer", test_bufferevent_trigger,
+ TT_FORK|TT_NEED_BASE, &basic_setup, (void*)"defer" },
+ { "bufferevent_trigger_postpone", test_bufferevent_trigger,
+ TT_FORK|TT_NEED_BASE|TT_NEED_THREADS, &basic_setup,
+ (void*)"postpone" },
+ { "bufferevent_trigger_defer_postpone", test_bufferevent_trigger,
+ TT_FORK|TT_NEED_BASE|TT_NEED_THREADS, &basic_setup,
+ (void*)"defer postpone" },
+#ifdef EVENT__HAVE_LIBZ
LEGACY(bufferevent_zlib, TT_ISOLATED),
#else
{ "bufferevent_zlib", NULL, TT_SKIP, NULL, NULL },
diff --git a/test/regress_dns.c b/test/regress_dns.c
index 4d90d671..e03c143c 100644
--- a/test/regress_dns.c
+++ b/test/regress_dns.c
@@ -24,8 +24,9 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "../util-internal.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
@@ -35,18 +36,18 @@
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/socket.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
-#ifdef _EVENT_HAVE_NETINET_IN6_H
+#ifdef EVENT__HAVE_NETINET_IN6_H
#include <netinet/in6.h>
#endif
#ifdef HAVE_NETDB_H
@@ -71,8 +72,6 @@
#include "regress.h"
#include "regress_testutils.h"
-#include "../util-internal.h"
-
static int dns_ok = 0;
static int dns_got_cancel = 0;
static int dns_err = 0;
@@ -99,7 +98,7 @@ dns_gethostbyname_cb(int result, char type, int count, int ttl,
switch (type) {
case DNS_IPv6_AAAA: {
-#if defined(_EVENT_HAVE_STRUCT_IN6_ADDR) && defined(_EVENT_HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
+#if defined(EVENT__HAVE_STRUCT_IN6_ADDR) && defined(EVENT__HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
struct in6_addr *in6_addrs = addresses;
char buf[INET6_ADDRSTRLEN+1];
int i;
@@ -320,7 +319,7 @@ dns_server_gethostbyname_cb(int result, char type, int count, int ttl,
break;
}
case DNS_IPv6_AAAA: {
-#if defined (_EVENT_HAVE_STRUCT_IN6_ADDR) && defined(_EVENT_HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
+#if defined (EVENT__HAVE_STRUCT_IN6_ADDR) && defined(EVENT__HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
struct in6_addr *in6_addrs = addresses;
char buf[INET6_ADDRSTRLEN+1];
if (memcmp(&in6_addrs[0].s6_addr, "abcdefghijklmnop", 16)
@@ -444,6 +443,7 @@ end:
static int n_replies_left;
static struct event_base *exit_base;
+static struct evdns_server_port *exit_port;
struct generic_dns_callback_result {
int result;
@@ -484,8 +484,14 @@ generic_dns_callback(int result, char type, int count, int ttl, void *addresses,
res->addrs = res->addrs_buf;
}
- if (--n_replies_left == 0)
- event_base_loopexit(exit_base, NULL);
+ --n_replies_left;
+ if (n_replies_left == 0) {
+ if (exit_port) {
+ evdns_close_server_port(exit_port);
+ exit_port = NULL;
+ } else
+ event_base_loopexit(exit_base, NULL);
+ }
}
static struct regress_dns_server_table search_table[] = {
@@ -655,7 +661,7 @@ fail_server_cb(struct evdns_server_request *req, void *data)
event_base_loopexit(exit_base, NULL);
}
- evutil_inet_pton(AF_INET, "16.32.64.128", &in);
+ tt_assert(evutil_inet_pton(AF_INET, "16.32.64.128", &in));
evdns_server_request_add_a_reply(req, question, 1, &in.s_addr,
100);
tt_assert(! evdns_server_request_respond(req, 0))
@@ -665,7 +671,7 @@ end:
}
static void
-dns_retry_test(void *arg)
+dns_retry_test_impl(void *arg, int flags)
{
struct basic_test_data *data = arg;
struct event_base *base = data->base;
@@ -682,11 +688,11 @@ dns_retry_test(void *arg)
tt_assert(port);
evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
- dns = evdns_base_new(base, 0);
+ dns = evdns_base_new(base, flags);
tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
- tt_assert(! evdns_base_set_option(dns, "timeout", "0.3"));
+ tt_assert(! evdns_base_set_option(dns, "timeout", "0.2"));
tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "10"));
- tt_assert(! evdns_base_set_option(dns, "initial-probe-timeout", "0.5"));
+ tt_assert(! evdns_base_set_option(dns, "initial-probe-timeout", "0.1"));
evdns_base_resolve_ipv4(dns, "host.example.com", 0,
generic_dns_callback, &r1);
@@ -705,8 +711,8 @@ dns_retry_test(void *arg)
/* Now try again, but this time have the server get treated as
* failed, so we can send it a test probe. */
drop_count = 4;
- tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "3"));
- tt_assert(! evdns_base_set_option(dns, "attempts:", "4"));
+ tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "2"));
+ tt_assert(! evdns_base_set_option(dns, "attempts:", "3"));
memset(&r1, 0, sizeof(r1));
evdns_base_resolve_ipv4(dns, "host.example.com", 0,
@@ -737,6 +743,16 @@ end:
if (port)
evdns_close_server_port(port);
}
+static void
+dns_retry_test(void *arg)
+{
+ dns_retry_test_impl(arg, 0);
+}
+static void
+dns_retry_disable_when_inactive_test(void *arg)
+{
+ dns_retry_test_impl(arg, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
+}
static struct regress_dns_server_table internal_error_table[] = {
/* Error 4 (NOTIMPL) makes us reissue the request to another server
@@ -754,7 +770,7 @@ static struct regress_dns_server_table reissue_table[] = {
};
static void
-dns_reissue_test(void *arg)
+dns_reissue_test_impl(void *arg, int flags)
{
struct basic_test_data *data = arg;
struct event_base *base = data->base;
@@ -773,7 +789,7 @@ dns_reissue_test(void *arg)
evutil_snprintf(buf1, sizeof(buf1), "127.0.0.1:%d", (int)portnum1);
evutil_snprintf(buf2, sizeof(buf2), "127.0.0.1:%d", (int)portnum2);
- dns = evdns_base_new(base, 0);
+ dns = evdns_base_new(base, flags);
tt_assert(!evdns_base_nameserver_ip_add(dns, buf1));
tt_assert(! evdns_base_set_option(dns, "timeout:", "0.3"));
tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "2"));
@@ -806,6 +822,16 @@ end:
if (port2)
evdns_close_server_port(port2);
}
+static void
+dns_reissue_test(void *arg)
+{
+ dns_reissue_test_impl(arg, 0);
+}
+static void
+dns_reissue_disable_when_inactive_test(void *arg)
+{
+ dns_reissue_test_impl(arg, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
+}
#if 0
static void
@@ -820,21 +846,29 @@ dumb_bytes_fn(char *p, size_t n)
#endif
static void
-dns_inflight_test(void *arg)
+dns_inflight_test_impl(void *arg, int flags)
{
struct basic_test_data *data = arg;
struct event_base *base = data->base;
struct evdns_base *dns = NULL;
+ struct evdns_server_port *dns_port = NULL;
ev_uint16_t portnum = 0;
char buf[64];
+ int disable_when_inactive = flags & EVDNS_BASE_DISABLE_WHEN_INACTIVE;
struct generic_dns_callback_result r[20];
int i;
- tt_assert(regress_dnsserver(base, &portnum, reissue_table));
+ dns_port = regress_get_dnsserver(base, &portnum, NULL,
+ regress_dns_server_cb, reissue_table);
+ tt_assert(dns_port);
+ if (disable_when_inactive) {
+ exit_port = dns_port;
+ }
+
evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
- dns = evdns_base_new(base, 0);
+ dns = evdns_base_new(base, flags);
tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
tt_assert(! evdns_base_set_option(dns, "max-inflight:", "3"));
tt_assert(! evdns_base_set_option(dns, "randomize-case:", "0"));
@@ -856,7 +890,65 @@ dns_inflight_test(void *arg)
end:
if (dns)
evdns_base_free(dns, 0);
+ if (exit_port) {
+ evdns_close_server_port(exit_port);
+ exit_port = NULL;
+ } else if (! disable_when_inactive) {
+ evdns_close_server_port(dns_port);
+ }
+}
+
+static void
+dns_inflight_test(void *arg)
+{
+ dns_inflight_test_impl(arg, 0);
+}
+
+static void
+dns_disable_when_inactive_test(void *arg)
+{
+ dns_inflight_test_impl(arg, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
+}
+
+static void
+dns_disable_when_inactive_no_ns_test(void *arg)
+{
+ struct basic_test_data *data = arg;
+ struct event_base *base = data->base, *inactive_base;
+ struct evdns_base *dns = NULL;
+ ev_uint16_t portnum = 0;
+ char buf[64];
+ struct generic_dns_callback_result r;
+
+ inactive_base = event_base_new();
+ tt_assert(inactive_base);
+
+ /** Create dns server with inactive base, to avoid replying to clients */
+ tt_assert(regress_dnsserver(inactive_base, &portnum, search_table));
+ evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
+
+ dns = evdns_base_new(base, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
+ tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
+ tt_assert(! evdns_base_set_option(dns, "timeout:", "0.1"));
+
+ evdns_base_resolve_ipv4(dns, "foof.example.com", 0, generic_dns_callback, &r);
+ n_replies_left = 1;
+ exit_base = base;
+
+ event_base_dispatch(base);
+
+ tt_int_op(n_replies_left, ==, 0);
+
+ tt_int_op(r.result, ==, DNS_ERR_TIMEOUT);
+ tt_int_op(r.count, ==, 0);
+ tt_ptr_op(r.addrs, ==, NULL);
+
+end:
+ if (dns)
+ evdns_base_free(dns, 0);
regress_clean_dnsserver();
+ if (inactive_base)
+ event_base_free(inactive_base);
}
/* === Test for bufferevent_socket_connect_hostname */
@@ -883,6 +975,8 @@ be_getaddrinfo_server_cb(struct evdns_server_request *req, void *data)
struct in6_addr ans6;
memset(&ans6, 0, sizeof(ans6));
+ TT_BLATHER(("Got question about %s, type=%d", qname, qtype));
+
if (qtype == EVDNS_TYPE_A &&
qclass == EVDNS_CLASS_INET &&
!evutil_ascii_strcasecmp(qname, "nobodaddy.example.com")) {
@@ -983,10 +1077,13 @@ be_getaddrinfo_server_cb(struct evdns_server_request *req, void *data)
TT_GRIPE(("Got weird request for %s",qname));
}
}
- if (added_any)
+ if (added_any) {
+ TT_BLATHER(("answering"));
evdns_server_request_respond(req, 0);
- else
+ } else {
+ TT_BLATHER(("saying nexist."));
evdns_server_request_respond(req, 3);
+ }
}
/* Implements a listener for connect_hostname test. */
@@ -1207,15 +1304,19 @@ test_getaddrinfo_async(void *arg)
struct evdns_server_port *port = NULL;
ev_uint16_t dns_port = 0;
int n_dns_questions = 0;
+ struct evdns_base *dns_base;
+
+ memset(a_out, 0, sizeof(a_out));
+ memset(&local_outcome, 0, sizeof(local_outcome));
- struct evdns_base *dns_base = evdns_base_new(data->base, 0);
+ dns_base = evdns_base_new(data->base, 0);
tt_assert(dns_base);
/* for localhost */
evdns_base_load_hosts(dns_base, NULL);
- memset(a_out, 0, sizeof(a_out));
- memset(&local_outcome, 0, sizeof(local_outcome));
+ tt_assert(! evdns_base_set_option(dns_base, "timeout", "0.3"));
+ tt_assert(! evdns_base_set_option(dns_base, "getaddrinfo-allow-skew", "0.2"));
n_gai_results_pending = 10000; /* don't think about exiting yet. */
@@ -1555,7 +1656,8 @@ test_getaddrinfo_async(void *arg)
end:
if (local_outcome.ai)
evutil_freeaddrinfo(local_outcome.ai);
- for (i=0;i<10;++i) {
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
+ for (i=0;i<(int)ARRAY_SIZE(a_out);++i) {
if (a_out[i].ai)
evutil_freeaddrinfo(a_out[i].ai);
}
@@ -1687,7 +1789,8 @@ testleak_setup(const struct testcase_t *testcase)
allocated_chunks = 0;
event_set_mem_functions(cnt_malloc, cnt_realloc, cnt_free);
- event_enable_debug_mode();
+ if (!libevent_tests_running_in_debug_mode)
+ event_enable_debug_mode();
/* not mm_calloc: we don't want to mess with the count. */
env = calloc(1, sizeof(struct testleak_env_t));
@@ -1705,7 +1808,7 @@ testleak_cleanup(const struct testcase_t *testcase, void *env_)
int ok = 0;
struct testleak_env_t *env = env_;
tt_assert(env);
-#ifdef _EVENT_DISABLE_DEBUG_MODE
+#ifdef EVENT__DISABLE_DEBUG_MODE
tt_int_op(allocated_chunks, ==, 0);
#else
/* FIXME: that's `1' because of event_debug_map_HT_GROW */
@@ -1748,6 +1851,57 @@ test_dbg_leak_cancel(void *env_)
}
static void
+dbg_leak_resume(void *env_, int cancel, int send_err_shutdown)
+{
+ /* cancel, loop, free/dns, free/base */
+ struct testleak_env_t *env = env_;
+ if (cancel) {
+ evdns_cancel_request(env->dns_base, env->req);
+ tt_assert(!evdns_base_resume(env->dns_base));
+ } else {
+ /* TODO: No nameservers, request can't be processed, must be errored */
+ tt_assert(!evdns_base_resume(env->dns_base));
+ }
+
+ /**
+ * Because we don't cancel request,
+ * and want our callback to recieve DNS_ERR_SHUTDOWN,
+ * we use deferred callback, and there was
+ * - one extra malloc(),
+ * @see reply_schedule_callback()
+ * - and one missing free
+ * @see request_finished() (req->handle->pending_cb = 1)
+ * than we don't need to count in testleak_cleanup()
+ *
+ * So just decrement allocated_chunks to 2,
+ * like we already take care about it.
+ */
+ if (!cancel && send_err_shutdown) {
+ allocated_chunks -= 2;
+ }
+
+ event_base_loop(env->base, EVLOOP_NONBLOCK);
+
+end:
+ evdns_base_free(env->dns_base, send_err_shutdown);
+ env->dns_base = 0;
+
+ event_base_free(env->base);
+ env->base = 0;
+}
+
+#define IMPL_DBG_LEAK_RESUME(name, cancel, send_err_shutdown) \
+ static void \
+ test_dbg_leak_##name##_(void *env_) \
+ { \
+ dbg_leak_resume(env_, cancel, send_err_shutdown); \
+ }
+IMPL_DBG_LEAK_RESUME(resume, 0, 0)
+IMPL_DBG_LEAK_RESUME(cancel_and_resume, 1, 0)
+IMPL_DBG_LEAK_RESUME(resume_send_err, 0, 1)
+IMPL_DBG_LEAK_RESUME(cancel_and_resume_send_err, 1, 1)
+
+static void
test_dbg_leak_shutdown(void *env_)
{
/* free/dns, loop, free/base */
@@ -1816,6 +1970,8 @@ end:
evdns_base_free(dns_base, 1);
if (server)
evdns_close_server_port(server);
+ if (base)
+ event_base_free(base);
if (fd >= 0)
evutil_closesocket(fd);
}
@@ -1827,18 +1983,26 @@ end:
struct testcase_t dns_testcases[] = {
DNS_LEGACY(server, TT_FORK|TT_NEED_BASE),
- DNS_LEGACY(gethostbyname, TT_FORK|TT_NEED_BASE|TT_NEED_DNS),
- DNS_LEGACY(gethostbyname6, TT_FORK|TT_NEED_BASE|TT_NEED_DNS),
- DNS_LEGACY(gethostbyaddr, TT_FORK|TT_NEED_BASE|TT_NEED_DNS),
- { "resolve_reverse", dns_resolve_reverse, TT_FORK, NULL, NULL },
+ DNS_LEGACY(gethostbyname, TT_FORK|TT_NEED_BASE|TT_NEED_DNS|TT_OFF_BY_DEFAULT),
+ DNS_LEGACY(gethostbyname6, TT_FORK|TT_NEED_BASE|TT_NEED_DNS|TT_OFF_BY_DEFAULT),
+ DNS_LEGACY(gethostbyaddr, TT_FORK|TT_NEED_BASE|TT_NEED_DNS|TT_OFF_BY_DEFAULT),
+ { "resolve_reverse", dns_resolve_reverse, TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL },
{ "search", dns_search_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
{ "search_cancel", dns_search_cancel_test,
TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
- { "retry", dns_retry_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
- { "reissue", dns_reissue_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
+ { "retry", dns_retry_test, TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
+ { "retry_disable_when_inactive", dns_retry_disable_when_inactive_test,
+ TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
+ { "reissue", dns_reissue_test, TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
+ { "reissue_disable_when_inactive", dns_reissue_disable_when_inactive_test,
+ TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
{ "inflight", dns_inflight_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
{ "bufferevent_connect_hostname", test_bufferevent_connect_hostname,
TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
+ { "disable_when_inactive", dns_disable_when_inactive_test,
+ TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
+ { "disable_when_inactive_no_ns", dns_disable_when_inactive_no_ns_test,
+ TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
{ "getaddrinfo_async", test_getaddrinfo_async,
TT_FORK|TT_NEED_BASE, &basic_setup, (char*)"" },
@@ -1848,6 +2012,14 @@ struct testcase_t dns_testcases[] = {
#ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
{ "leak_shutdown", test_dbg_leak_shutdown, TT_FORK, &testleak_funcs, NULL },
{ "leak_cancel", test_dbg_leak_cancel, TT_FORK, &testleak_funcs, NULL },
+
+ { "leak_resume", test_dbg_leak_resume_, TT_FORK, &testleak_funcs, NULL },
+ { "leak_cancel_and_resume", test_dbg_leak_cancel_and_resume_,
+ TT_FORK, &testleak_funcs, NULL },
+ { "leak_resume_send_err", test_dbg_leak_resume_send_err_,
+ TT_FORK, &testleak_funcs, NULL },
+ { "leak_cancel_and_resume_send_err", test_dbg_leak_cancel_and_resume_send_err_,
+ TT_FORK, &testleak_funcs, NULL },
#endif
END_OF_TESTCASES
diff --git a/test/regress_et.c b/test/regress_et.c
index 3ddd9edd..229a78e2 100644
--- a/test/regress_et.c
+++ b/test/regress_et.c
@@ -26,19 +26,19 @@
#include "../util-internal.h"
#include "event2/event-config.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_SOCKET_H
+#ifdef EVENT__HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/time.h>
#include <unistd.h>
#endif
@@ -71,7 +71,7 @@ read_cb(evutil_socket_t fd, short event, void *arg)
#define SHUT_WR 1
#endif
-#ifdef WIN32
+#ifdef _WIN32
#define LOCAL_SOCKETPAIR_AF AF_INET
#else
#define LOCAL_SOCKETPAIR_AF AF_UNIX
@@ -93,7 +93,7 @@ test_edgetriggered(void *et)
* problem.
*/
#ifdef __linux__
- if (evutil_ersatz_socketpair(AF_INET, SOCK_STREAM, 0, pair) == -1) {
+ if (evutil_ersatz_socketpair_(AF_INET, SOCK_STREAM, 0, pair) == -1) {
tt_abort_perror("socketpair");
}
#else
@@ -162,12 +162,13 @@ test_edgetriggered_mix_error(void *data_)
struct event_base *base = NULL;
struct event *ev_et=NULL, *ev_lt=NULL;
-#ifdef _EVENT_DISABLE_DEBUG_MODE
+#ifdef EVENT__DISABLE_DEBUG_MODE
if (1)
tt_skip();
#endif
- event_enable_debug_mode();
+ if (!libevent_tests_running_in_debug_mode)
+ event_enable_debug_mode();
base = event_base_new();
diff --git a/test/regress_finalize.c b/test/regress_finalize.c
new file mode 100644
index 00000000..552210fe
--- /dev/null
+++ b/test/regress_finalize.c
@@ -0,0 +1,347 @@
+/*
+ * Copyright (c) 2013 Niels Provos and Nick Mathewson
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "event2/event-config.h"
+#include "evconfig-private.h"
+#include "tinytest.h"
+#include "tinytest_macros.h"
+#include <stdlib.h>
+
+#include "event2/event.h"
+#include "event2/util.h"
+#include "event-internal.h"
+#include "defer-internal.h"
+
+#include "regress.h"
+#include "regress_thread.h"
+
+static void
+timer_callback(evutil_socket_t fd, short what, void *arg)
+{
+ int *int_arg = arg;
+ *int_arg += 1;
+ (void)fd;
+ (void)what;
+}
+static void
+simple_callback(struct event_callback *evcb, void *arg)
+{
+ int *int_arg = arg;
+ *int_arg += 1;
+ (void)evcb;
+}
+static void
+event_finalize_callback_1(struct event *ev, void *arg)
+{
+ int *int_arg = arg;
+ *int_arg += 100;
+ (void)ev;
+}
+static void
+callback_finalize_callback_1(struct event_callback *evcb, void *arg)
+{
+ int *int_arg = arg;
+ *int_arg += 100;
+ (void)evcb;
+}
+
+
+static void
+test_fin_cb_invoked(void *arg)
+{
+ struct basic_test_data *data = arg;
+ struct event_base *base = data->base;
+
+ struct event *ev;
+ struct event ev2;
+ struct event_callback evcb;
+ int cb_called = 0;
+ int ev_called = 0;
+
+ const struct timeval ten_sec = {10,0};
+
+ event_deferred_cb_init_(&evcb, 0, simple_callback, &cb_called);
+ ev = evtimer_new(base, timer_callback, &ev_called);
+ /* Just finalize them; don't bother adding. */
+ event_free_finalize(0, ev, event_finalize_callback_1);
+ event_callback_finalize_(base, 0, &evcb, callback_finalize_callback_1);
+
+ event_base_dispatch(base);
+
+ tt_int_op(cb_called, ==, 100);
+ tt_int_op(ev_called, ==, 100);
+
+ ev_called = cb_called = 0;
+ event_base_assert_ok_(base);
+
+ /* Now try it when they're active. (actually, don't finalize: make
+ * sure activation can happen! */
+ ev = evtimer_new(base, timer_callback, &ev_called);
+ event_deferred_cb_init_(&evcb, 0, simple_callback, &cb_called);
+
+ event_active(ev, EV_TIMEOUT, 1);
+ event_callback_activate_(base, &evcb);
+
+ event_base_dispatch(base);
+ tt_int_op(cb_called, ==, 1);
+ tt_int_op(ev_called, ==, 1);
+
+ ev_called = cb_called = 0;
+ event_base_assert_ok_(base);
+
+ /* Great, it worked. Now activate and finalize and make sure only
+ * finalizing happens. */
+ event_active(ev, EV_TIMEOUT, 1);
+ event_callback_activate_(base, &evcb);
+ event_free_finalize(0, ev, event_finalize_callback_1);
+ event_callback_finalize_(base, 0, &evcb, callback_finalize_callback_1);
+
+ event_base_dispatch(base);
+ tt_int_op(cb_called, ==, 100);
+ tt_int_op(ev_called, ==, 100);
+
+ ev_called = 0;
+
+ event_base_assert_ok_(base);
+
+ /* Okay, now add but don't have it become active, and make sure *that*
+ * works. */
+ ev = evtimer_new(base, timer_callback, &ev_called);
+ event_add(ev, &ten_sec);
+ event_free_finalize(0, ev, event_finalize_callback_1);
+
+ event_base_dispatch(base);
+ tt_int_op(ev_called, ==, 100);
+
+ ev_called = 0;
+ event_base_assert_ok_(base);
+
+ /* Now try adding and deleting after finalizing. */
+ ev = evtimer_new(base, timer_callback, &ev_called);
+ evtimer_assign(&ev2, base, timer_callback, &ev_called);
+ event_add(ev, &ten_sec);
+ event_free_finalize(0, ev, event_finalize_callback_1);
+ event_finalize(0, &ev2, event_finalize_callback_1);
+
+ event_add(&ev2, &ten_sec);
+ event_del(ev);
+ event_active(&ev2, EV_TIMEOUT, 1);
+
+ event_base_dispatch(base);
+ tt_int_op(ev_called, ==, 200);
+
+ event_base_assert_ok_(base);
+
+end:
+ ;
+}
+
+#ifndef EVENT__DISABLE_MM_REPLACEMENT
+static void *
+tfff_malloc(size_t n)
+{
+ return malloc(n);
+}
+static void *tfff_p1=NULL, *tfff_p2=NULL;
+static int tfff_p1_freed=0, tfff_p2_freed=0;
+static void
+tfff_free(void *p)
+{
+ if (! p)
+ return;
+ if (p == tfff_p1)
+ ++tfff_p1_freed;
+ if (p == tfff_p2)
+ ++tfff_p2_freed;
+ free(p);
+}
+static void *
+tfff_realloc(void *p, size_t sz)
+{
+ return realloc(p,sz);
+}
+#endif
+
+static void
+test_fin_free_finalize(void *arg)
+{
+#ifdef EVENT__DISABLE_MM_REPLACEMENT
+ tinytest_set_test_skipped_();
+#else
+ struct event_base *base = NULL;
+ struct event *ev, *ev2;
+ int ev_called = 0;
+ int ev2_called = 0;
+
+ (void)arg;
+
+ event_set_mem_functions(tfff_malloc, tfff_realloc, tfff_free);
+
+ base = event_base_new();
+ tt_assert(base);
+
+ ev = evtimer_new(base, timer_callback, &ev_called);
+ ev2 = evtimer_new(base, timer_callback, &ev2_called);
+ tfff_p1 = ev;
+ tfff_p2 = ev2;
+ event_free_finalize(0, ev, event_finalize_callback_1);
+ event_finalize(0, ev2, event_finalize_callback_1);
+
+ event_base_dispatch(base);
+
+ tt_int_op(ev_called, ==, 100);
+ tt_int_op(ev2_called, ==, 100);
+
+ event_base_assert_ok_(base);
+ tt_int_op(tfff_p1_freed, ==, 1);
+ tt_int_op(tfff_p2_freed, ==, 0);
+
+ event_free(ev2);
+
+end:
+ if (base)
+ event_base_free(base);
+#endif
+}
+
+/* For test_fin_within_cb */
+struct event_and_count {
+ struct event *ev;
+ struct event *ev2;
+ int count;
+};
+static void
+event_finalize_callback_2(struct event *ev, void *arg)
+{
+ struct event_and_count *evc = arg;
+ evc->count += 100;
+ event_free(ev);
+}
+static void
+timer_callback_2(evutil_socket_t fd, short what, void *arg)
+{
+ struct event_and_count *evc = arg;
+ event_finalize(0, evc->ev, event_finalize_callback_2);
+ event_finalize(0, evc->ev2, event_finalize_callback_2);
+ ++ evc->count;
+ (void)fd;
+ (void)what;
+}
+
+static void
+test_fin_within_cb(void *arg)
+{
+ struct basic_test_data *data = arg;
+ struct event_base *base = data->base;
+
+ struct event_and_count evc1, evc2;
+ evc1.count = evc2.count = 0;
+ evc2.ev2 = evc1.ev = evtimer_new(base, timer_callback_2, &evc1);
+ evc1.ev2 = evc2.ev = evtimer_new(base, timer_callback_2, &evc2);
+
+ /* Activate both. The first one will have its callback run, which
+ * will finalize both of them, preventing the second one's callback
+ * from running. */
+ event_active(evc1.ev, EV_TIMEOUT, 1);
+ event_active(evc2.ev, EV_TIMEOUT, 1);
+
+ event_base_dispatch(base);
+ tt_int_op(evc1.count, ==, 101);
+ tt_int_op(evc2.count, ==, 100);
+
+ event_base_assert_ok_(base);
+ /* Now try with EV_PERSIST events. */
+ evc1.count = evc2.count = 0;
+ evc2.ev2 = evc1.ev = event_new(base, -1, EV_PERSIST, timer_callback_2, &evc1);
+ evc1.ev2 = evc2.ev = event_new(base, -1, EV_PERSIST, timer_callback_2, &evc2);
+
+ event_active(evc1.ev, EV_TIMEOUT, 1);
+ event_active(evc2.ev, EV_TIMEOUT, 1);
+
+ event_base_dispatch(base);
+ tt_int_op(evc1.count, ==, 101);
+ tt_int_op(evc2.count, ==, 100);
+
+ event_base_assert_ok_(base);
+end:
+ ;
+}
+
+#if 0
+static void
+timer_callback_3(evutil_socket_t *fd, short what, void *arg)
+{
+ (void)fd;
+ (void)what;
+
+}
+static void
+test_fin_many(void *arg)
+{
+ struct basic_test_data *data = arg;
+ struct event_base *base = data->base;
+
+ struct event *ev1, *ev2;
+ struct event_callback evcb1, evcb2;
+ int ev1_count = 0, ev2_count = 0;
+ int evcb1_count = 0, evcb2_count = 0;
+ struct event_callback *array[4];
+
+ int n;
+
+ /* First attempt: call finalize_many with no events running */
+ ev1 = evtimer_new(base, timer_callback, &ev1_count);
+ ev1 = evtimer_new(base, timer_callback, &ev2_count);
+ event_deferred_cb_init_(&evcb1, 0, simple_callback, &evcb1_called);
+ event_deferred_cb_init_(&evcb2, 0, simple_callback, &evcb2_called);
+ array[0] = &ev1->ev_evcallback;
+ array[1] = &ev2->ev_evcallback;
+ array[2] = &evcb1;
+ array[3] = &evcb2;
+
+
+
+ n = event_callback_finalize_many(base, 4, array,
+ callback_finalize_callback_1);
+
+}
+#endif
+
+
+#define TEST(name, flags) \
+ { #name, test_fin_##name, (flags), &basic_setup, NULL }
+
+struct testcase_t finalize_testcases[] = {
+
+ TEST(cb_invoked, TT_FORK|TT_NEED_BASE),
+ TEST(free_finalize, TT_FORK),
+ TEST(within_cb, TT_FORK|TT_NEED_BASE),
+// TEST(many, TT_FORK|TT_NEED_BASE),
+
+
+ END_OF_TESTCASES
+};
+
diff --git a/test/regress_http.c b/test/regress_http.c
index 9ece455b..35f6dd76 100644
--- a/test/regress_http.c
+++ b/test/regress_http.c
@@ -24,8 +24,9 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "util-internal.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
@@ -35,11 +36,11 @@
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/socket.h>
#include <signal.h>
#include <unistd.h>
@@ -59,7 +60,6 @@
#include "event2/bufferevent.h"
#include "event2/util.h"
#include "log-internal.h"
-#include "util-internal.h"
#include "http-internal.h"
#include "regress.h"
#include "regress_testutils.h"
@@ -70,6 +70,19 @@ static struct event_base *exit_base;
static char const BASIC_REQUEST_BODY[] = "This is funny";
+#define IMPL_HTTP_REQUEST_ERROR_CB(name, expecting_error) \
+ static void \
+ http_request_error_cb_with_##name##_(enum evhttp_request_error error, \
+ void *arg) \
+ { \
+ if (error != expecting_error) { \
+ fprintf(stderr, "FAILED\n"); \
+ exit(1); \
+ } \
+ test_ok = 1; \
+ }
+IMPL_HTTP_REQUEST_ERROR_CB(cancel, EVREQ_HTTP_REQUEST_CANCEL)
+
static void http_basic_cb(struct evhttp_request *req, void *arg);
static void http_chunked_cb(struct evhttp_request *req, void *arg);
static void http_post_cb(struct evhttp_request *req, void *arg);
@@ -79,15 +92,25 @@ static void http_delay_cb(struct evhttp_request *req, void *arg);
static void http_large_delay_cb(struct evhttp_request *req, void *arg);
static void http_badreq_cb(struct evhttp_request *req, void *arg);
static void http_dispatcher_cb(struct evhttp_request *req, void *arg);
+static void http_on_complete_cb(struct evhttp_request *req, void *arg);
+
static int
-http_bind(struct evhttp *myhttp, ev_uint16_t *pport)
+http_bind(struct evhttp *myhttp, ev_uint16_t *pport, int ipv6)
{
int port;
struct evhttp_bound_socket *sock;
- sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", *pport);
- if (sock == NULL)
- event_errx(1, "Could not start web server");
+ if (ipv6)
+ sock = evhttp_bind_socket_with_handle(myhttp, "::1", *pport);
+ else
+ sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", *pport);
+
+ if (sock == NULL) {
+ if (ipv6)
+ return -1;
+ else
+ event_errx(1, "Could not start web server");
+ }
port = regress_get_socket_port(evhttp_bound_socket_get_fd(sock));
if (port < 0)
@@ -98,14 +121,14 @@ http_bind(struct evhttp *myhttp, ev_uint16_t *pport)
}
static struct evhttp *
-http_setup(ev_uint16_t *pport, struct event_base *base)
+http_setup(ev_uint16_t *pport, struct event_base *base, int ipv6)
{
struct evhttp *myhttp;
/* Try a few different ports */
myhttp = evhttp_new(base);
- if (http_bind(myhttp, pport) < 0)
+ if (http_bind(myhttp, pport, ipv6) < 0)
return NULL;
/* Register a callback for certain types of requests */
@@ -118,6 +141,7 @@ http_setup(ev_uint16_t *pport, struct event_base *base)
evhttp_set_cb(myhttp, "/delay", http_delay_cb, base);
evhttp_set_cb(myhttp, "/largedelay", http_large_delay_cb, base);
evhttp_set_cb(myhttp, "/badrequest", http_badreq_cb, base);
+ evhttp_set_cb(myhttp, "/oncomplete", http_on_complete_cb, base);
evhttp_set_cb(myhttp, "/", http_dispatcher_cb, base);
return (myhttp);
}
@@ -154,7 +178,7 @@ http_connect(const char *address, u_short port)
evutil_make_socket_nonblocking(fd);
if (connect(fd, sa, slen) == -1) {
-#ifdef WIN32
+#ifdef _WIN32
int tmp_err = WSAGetLastError();
if (tmp_err != WSAEINPROGRESS && tmp_err != WSAEINVAL &&
tmp_err != WSAEWOULDBLOCK)
@@ -212,11 +236,11 @@ http_readcb(struct bufferevent *bev, void *arg)
enum message_read_status done;
/* req->kind = EVHTTP_RESPONSE; */
- done = evhttp_parse_firstline(req, bufferevent_get_input(bev));
+ done = evhttp_parse_firstline_(req, bufferevent_get_input(bev));
if (done != ALL_DATA_READ)
goto out;
- done = evhttp_parse_headers(req, bufferevent_get_input(bev));
+ done = evhttp_parse_headers_(req, bufferevent_get_input(bev));
if (done != ALL_DATA_READ)
goto out;
@@ -256,25 +280,41 @@ http_errorcb(struct bufferevent *bev, short what, void *arg)
event_base_loopexit(arg, NULL);
}
+static int found_multi = 0;
+static int found_multi2 = 0;
+
static void
http_basic_cb(struct evhttp_request *req, void *arg)
{
struct evbuffer *evb = evbuffer_new();
+ struct evhttp_connection *evcon;
int empty = evhttp_find_header(evhttp_request_get_input_headers(req), "Empty") != NULL;
event_debug(("%s: called\n", __func__));
evbuffer_add_printf(evb, BASIC_REQUEST_BODY);
+ evcon = evhttp_request_get_connection(req);
+ tt_assert(evhttp_connection_get_server(evcon) == http);
+
/* For multi-line headers test */
{
const char *multi =
- evhttp_find_header(evhttp_request_get_input_headers(req),"X-multi");
+ evhttp_find_header(evhttp_request_get_input_headers(req),"X-Multi");
if (multi) {
+ found_multi = !strcmp(multi,"aaaaaaaa a END");
if (strcmp("END", multi + strlen(multi) - 3) == 0)
test_ok++;
if (evhttp_find_header(evhttp_request_get_input_headers(req), "X-Last"))
test_ok++;
}
}
+ {
+ const char *multi2 =
+ evhttp_find_header(evhttp_request_get_input_headers(req),"X-Multi-Extra-WS");
+ if (multi2) {
+ found_multi2 = !strcmp(multi2,"libevent 2.1");
+ }
+ }
+
/* injecting a bad content-length */
if (evhttp_find_header(evhttp_request_get_input_headers(req), "X-Negative"))
@@ -285,6 +325,7 @@ http_basic_cb(struct evhttp_request *req, void *arg)
evhttp_send_reply(req, HTTP_OK, "Everything is fine",
!empty ? evb : NULL);
+end:
evbuffer_free(evb);
}
@@ -358,17 +399,17 @@ http_basic_test(void *arg)
{
struct basic_test_data *data = arg;
struct timeval tv;
- struct bufferevent *bev;
+ struct bufferevent *bev = NULL;
evutil_socket_t fd;
const char *http_request;
ev_uint16_t port = 0, port2 = 0;
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
/* bind to a second socket */
- if (http_bind(http, &port2) == -1) {
+ if (http_bind(http, &port2, 0) == -1) {
fprintf(stdout, "FAILED (bind)\n");
exit(1);
}
@@ -443,9 +484,11 @@ http_basic_test(void *arg)
evhttp_free(http);
end:
- ;
+ if (bev)
+ bufferevent_free(bev);
}
+
static void
http_delay_reply(evutil_socket_t fd, short what, void *arg)
{
@@ -487,7 +530,7 @@ http_badreq_errorcb(struct bufferevent *bev, short what, void *arg)
}
#ifndef SHUT_WR
-#ifdef WIN32
+#ifdef _WIN32
#define SHUT_WR SD_SEND
#else
#define SHUT_WR 1
@@ -512,11 +555,11 @@ http_badreq_readcb(struct bufferevent *bev, void *arg)
enum message_read_status done;
/* req->kind = EVHTTP_RESPONSE; */
- done = evhttp_parse_firstline(req, bufferevent_get_input(bev));
+ done = evhttp_parse_firstline_(req, bufferevent_get_input(bev));
if (done != ALL_DATA_READ)
goto out;
- done = evhttp_parse_headers(req, bufferevent_get_input(bev));
+ done = evhttp_parse_headers_(req, bufferevent_get_input(bev));
if (done != ALL_DATA_READ)
goto out;
@@ -546,21 +589,22 @@ http_bad_request_test(void *arg)
struct basic_test_data *data = arg;
struct timeval tv;
struct bufferevent *bev = NULL;
- evutil_socket_t fd;
+ evutil_socket_t fd = -1;
const char *http_request;
ev_uint16_t port=0, port2=0;
test_ok = 0;
exit_base = data->base;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
/* bind to a second socket */
- if (http_bind(http, &port2) == -1)
+ if (http_bind(http, &port2, 0) == -1)
TT_DIE(("Bind socket failed"));
/* NULL request test */
fd = http_connect("127.0.0.1", port);
+ tt_int_op(fd, >=, 0);
/* Stupid thing to send a request */
bev = bufferevent_socket_new(data->base, fd, 0);
@@ -619,6 +663,8 @@ end:
evhttp_free(http);
if (bev)
bufferevent_free(bev);
+ if (fd >= 0)
+ evutil_closesocket(fd);
}
static struct evhttp_connection *delayed_client;
@@ -628,10 +674,10 @@ http_large_delay_cb(struct evhttp_request *req, void *arg)
{
struct timeval tv;
evutil_timerclear(&tv);
- tv.tv_sec = 3;
+ tv.tv_usec = 500000;
event_base_once(arg, -1, EV_TIMEOUT, http_delay_reply, req, &tv);
- evhttp_connection_fail(delayed_client, EVCON_HTTP_EOF);
+ evhttp_connection_fail_(delayed_client, EVREQ_HTTP_EOF);
}
/*
@@ -665,15 +711,17 @@ http_delete_test(void *arg)
{
struct basic_test_data *data = arg;
struct bufferevent *bev;
- evutil_socket_t fd;
+ evutil_socket_t fd = -1;
const char *http_request;
ev_uint16_t port = 0;
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
+ tt_assert(http);
fd = http_connect("127.0.0.1", port);
+ tt_int_op(fd, >=, 0);
/* Stupid thing to send a request */
bev = bufferevent_socket_new(data->base, fd, 0);
@@ -692,12 +740,95 @@ http_delete_test(void *arg)
bufferevent_free(bev);
evutil_closesocket(fd);
+ fd = -1;
evhttp_free(http);
tt_int_op(test_ok, ==, 2);
end:
- ;
+ if (fd >= 0)
+ evutil_closesocket(fd);
+}
+
+static void
+http_sent_cb(struct evhttp_request *req, void *arg)
+{
+ ev_uintptr_t val = (ev_uintptr_t)arg;
+ struct evbuffer *b;
+
+ if (val != 0xDEADBEEF) {
+ fprintf(stdout, "FAILED on_complete_cb argument\n");
+ exit(1);
+ }
+
+ b = evhttp_request_get_output_buffer(req);
+ if (evbuffer_get_length(b) != 0) {
+ fprintf(stdout, "FAILED on_complete_cb output buffer not written\n");
+ exit(1);
+ }
+
+ event_debug(("%s: called\n", __func__));
+
+ ++test_ok;
+}
+
+static void
+http_on_complete_cb(struct evhttp_request *req, void *arg)
+{
+ struct evbuffer *evb = evbuffer_new();
+
+ evhttp_request_set_on_complete_cb(req, http_sent_cb, (void *)0xDEADBEEF);
+
+ event_debug(("%s: called\n", __func__));
+ evbuffer_add_printf(evb, BASIC_REQUEST_BODY);
+
+ /* allow sending of an empty reply */
+ evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
+
+ evbuffer_free(evb);
+
+ ++test_ok;
+}
+
+static void
+http_on_complete_test(void *arg)
+{
+ struct basic_test_data *data = arg;
+ struct bufferevent *bev;
+ evutil_socket_t fd = -1;
+ const char *http_request;
+ ev_uint16_t port = 0;
+
+ test_ok = 0;
+
+ http = http_setup(&port, data->base, 0);
+
+ fd = http_connect("127.0.0.1", port);
+ tt_int_op(fd, >=, 0);
+
+ /* Stupid thing to send a request */
+ bev = bufferevent_socket_new(data->base, fd, 0);
+ bufferevent_setcb(bev, http_readcb, http_writecb,
+ http_errorcb, data->base);
+
+ http_request =
+ "GET /oncomplete HTTP/1.1\r\n"
+ "Host: somehost\r\n"
+ "Connection: close\r\n"
+ "\r\n";
+
+ bufferevent_write(bev, http_request, strlen(http_request));
+
+ event_base_dispatch(data->base);
+
+ bufferevent_free(bev);
+
+ evhttp_free(http);
+
+ tt_int_op(test_ok, ==, 4);
+ end:
+ if (fd >= 0)
+ evutil_closesocket(fd);
}
static void
@@ -724,7 +855,7 @@ http_allowed_methods_test(void *arg)
{
struct basic_test_data *data = arg;
struct bufferevent *bev1, *bev2, *bev3;
- evutil_socket_t fd1, fd2, fd3;
+ evutil_socket_t fd1=-1, fd2=-1, fd3=-1;
const char *http_request;
char *result1=NULL, *result2=NULL, *result3=NULL;
ev_uint16_t port = 0;
@@ -732,9 +863,10 @@ http_allowed_methods_test(void *arg)
exit_base = data->base;
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
fd1 = http_connect("127.0.0.1", port);
+ tt_int_op(fd1, >=, 0);
/* GET is out; PATCH is in. */
evhttp_set_allowed_methods(http, EVHTTP_REQ_PATCH);
@@ -756,6 +888,7 @@ http_allowed_methods_test(void *arg)
event_base_dispatch(data->base);
fd2 = http_connect("127.0.0.1", port);
+ tt_int_op(fd2, >=, 0);
bev2 = bufferevent_socket_new(data->base, fd2, 0);
bufferevent_enable(bev2, EV_READ|EV_WRITE);
@@ -773,6 +906,7 @@ http_allowed_methods_test(void *arg)
event_base_dispatch(data->base);
fd3 = http_connect("127.0.0.1", port);
+ tt_int_op(fd3, >=, 0);
bev3 = bufferevent_socket_new(data->base, fd3, 0);
bufferevent_enable(bev3, EV_READ|EV_WRITE);
@@ -792,9 +926,6 @@ http_allowed_methods_test(void *arg)
bufferevent_free(bev1);
bufferevent_free(bev2);
bufferevent_free(bev3);
- evutil_closesocket(fd1);
- evutil_closesocket(fd2);
- evutil_closesocket(fd3);
evhttp_free(http);
@@ -817,13 +948,20 @@ http_allowed_methods_test(void *arg)
free(result2);
if (result3)
free(result3);
+ if (fd1 >= 0)
+ evutil_closesocket(fd1);
+ if (fd2 >= 0)
+ evutil_closesocket(fd2);
+ if (fd3 >= 0)
+ evutil_closesocket(fd3);
}
static void http_request_done(struct evhttp_request *, void *);
static void http_request_empty_done(struct evhttp_request *, void *);
static void
-_http_connection_test(struct basic_test_data *data, int persistent)
+http_connection_test_(struct basic_test_data *data, int persistent,
+ const char *address, struct evdns_base *dnsbase, int ipv6, int family)
{
ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
@@ -831,19 +969,26 @@ _http_connection_test(struct basic_test_data *data, int persistent)
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, ipv6);
+ if (!http && ipv6) {
+ tt_skip();
+ }
+ tt_assert(http);
- evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
+ evcon = evhttp_connection_base_new(data->base, dnsbase, address, port);
tt_assert(evcon);
+ evhttp_connection_set_family(evcon, family);
tt_assert(evhttp_connection_get_base(evcon) == data->base);
exit_base = data->base;
+
+ tt_assert(evhttp_connection_get_server(evcon) == NULL);
+
/*
* At this point, we want to schedule a request to the HTTP
* server using our make request method.
*/
-
req = evhttp_request_new(http_request_done, (void*) BASIC_REQUEST_BODY);
/* Add the information that we care about */
@@ -906,12 +1051,12 @@ _http_connection_test(struct basic_test_data *data, int persistent)
static void
http_connection_test(void *arg)
{
- _http_connection_test(arg, 0);
+ http_connection_test_(arg, 0, "127.0.0.1", NULL, 0, AF_UNSPEC);
}
static void
http_persist_connection_test(void *arg)
{
- _http_connection_test(arg, 1);
+ http_connection_test_(arg, 1, "127.0.0.1", NULL, 0, AF_UNSPEC);
}
static struct regress_dns_server_table search_table[] = {
@@ -943,7 +1088,7 @@ http_connection_async_test(void *arg)
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, dns_base, "127.0.0.1", port);
tt_assert(evcon);
@@ -1015,6 +1160,63 @@ http_connection_async_test(void *arg)
}
static void
+http_autofree_connection_test(void *arg)
+{
+ struct basic_test_data *data = arg;
+ ev_uint16_t port = 0;
+ struct evhttp_connection *evcon = NULL;
+ struct evhttp_request *req[2] = { NULL };
+
+ test_ok = 0;
+ http = http_setup(&port, data->base, 0);
+
+ evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
+ tt_assert(evcon);
+
+ /*
+ * At this point, we want to schedule two request to the HTTP
+ * server using our make request method.
+ */
+ req[0] = evhttp_request_new(http_request_empty_done, data->base);
+ req[1] = evhttp_request_new(http_request_empty_done, data->base);
+
+ /* Add the information that we care about */
+ evhttp_add_header(evhttp_request_get_output_headers(req[0]), "Host", "somehost");
+ evhttp_add_header(evhttp_request_get_output_headers(req[0]), "Connection", "close");
+ evhttp_add_header(evhttp_request_get_output_headers(req[0]), "Empty", "itis");
+ evhttp_add_header(evhttp_request_get_output_headers(req[1]), "Host", "somehost");
+ evhttp_add_header(evhttp_request_get_output_headers(req[1]), "Connection", "close");
+ evhttp_add_header(evhttp_request_get_output_headers(req[1]), "Empty", "itis");
+
+ /* We give ownership of the request to the connection */
+ if (evhttp_make_request(evcon, req[0], EVHTTP_REQ_GET, "/test") == -1) {
+ tt_abort_msg("couldn't make request");
+ }
+ if (evhttp_make_request(evcon, req[1], EVHTTP_REQ_GET, "/test") == -1) {
+ tt_abort_msg("couldn't make request");
+ }
+
+ /*
+ * Tell libevent to free the connection when the request completes
+ * We then set the evcon pointer to NULL since we don't want to free it
+ * when this function ends.
+ */
+ evhttp_connection_free_on_completion(evcon);
+ evcon = NULL;
+
+ event_base_dispatch(data->base);
+
+ /* at this point, the http server should have no connection */
+ tt_assert(TAILQ_FIRST(&http->connections) == NULL);
+
+ end:
+ if (evcon)
+ evhttp_connection_free(evcon);
+ if (http)
+ evhttp_free(http);
+}
+
+static void
http_request_never_call(struct evhttp_request *req, void *arg)
{
fprintf(stdout, "FAILED\n");
@@ -1052,7 +1254,7 @@ http_cancel_test(void *arg)
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);
@@ -1063,6 +1265,7 @@ http_cancel_test(void *arg)
*/
req = evhttp_request_new(http_request_never_call, NULL);
+ evhttp_request_set_error_cb(req, http_request_error_cb_with_cancel_);
/* Add the information that we care about */
evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
@@ -1079,7 +1282,7 @@ http_cancel_test(void *arg)
event_base_dispatch(data->base);
- tt_int_op(test_ok, ==, 2);
+ tt_int_op(test_ok, ==, 3);
/* try to make another request over the same connection */
test_ok = 0;
@@ -1174,7 +1377,7 @@ http_virtual_host_test(void *arg)
exit_base = data->base;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
/* virtual host */
second = evhttp_new(NULL);
@@ -1415,7 +1618,7 @@ http_dispatcher_test(void *arg)
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);
@@ -1465,7 +1668,7 @@ http_post_test(void *arg)
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);
@@ -1605,7 +1808,7 @@ http_put_test(void *arg)
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);
@@ -1725,15 +1928,16 @@ http_failure_test(void *arg)
{
struct basic_test_data *data = arg;
struct bufferevent *bev;
- evutil_socket_t fd;
+ evutil_socket_t fd = -1;
const char *http_request;
ev_uint16_t port = 0;
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
fd = http_connect("127.0.0.1", port);
+ tt_int_op(fd, >=, 0);
/* Stupid thing to send a request */
bev = bufferevent_socket_new(data->base, fd, 0);
@@ -1747,13 +1951,13 @@ http_failure_test(void *arg)
event_base_dispatch(data->base);
bufferevent_free(bev);
- evutil_closesocket(fd);
evhttp_free(http);
tt_int_op(test_ok, ==, 2);
end:
- ;
+ if (fd >= 0)
+ evutil_closesocket(fd);
}
static void
@@ -1767,7 +1971,7 @@ close_detect_done(struct evhttp_request *req, void *arg)
end:
evutil_timerclear(&tv);
- tv.tv_sec = 3;
+ tv.tv_usec = 150000;
event_base_loopexit(arg, &tv);
}
@@ -1801,9 +2005,10 @@ close_detect_cb(struct evhttp_request *req, void *arg)
}
evutil_timerclear(&tv);
- tv.tv_sec = 3; /* longer than the http time out */
+ tv.tv_sec = 0; /* longer than the http time out */
+ tv.tv_usec = 600000; /* longer than the http time out */
- /* launch a new request on the persistent connection in 3 seconds */
+ /* launch a new request on the persistent connection in .3 seconds */
event_base_once(base, -1, EV_TIMEOUT, close_detect_launch, evcon, &tv);
end:
;
@@ -1811,21 +2016,26 @@ close_detect_cb(struct evhttp_request *req, void *arg)
static void
-_http_close_detection(struct basic_test_data *data, int with_delay)
+http_close_detection_(struct basic_test_data *data, int with_delay)
{
ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
+ const struct timeval sec_tenth = { 0, 100000 };
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
- /* 2 second timeout */
- evhttp_set_timeout(http, 1);
+ /* .1 second timeout */
+ evhttp_set_timeout_tv(http, &sec_tenth);
evcon = evhttp_connection_base_new(data->base, NULL,
"127.0.0.1", port);
tt_assert(evcon);
+ evhttp_connection_set_timeout_tv(evcon, &sec_tenth);
+
+
+ tt_assert(evcon);
delayed_client = evcon;
/*
@@ -1858,12 +2068,12 @@ _http_close_detection(struct basic_test_data *data, int with_delay)
static void
http_close_detection_test(void *arg)
{
- _http_close_detection(arg, 0);
+ http_close_detection_(arg, 0);
}
static void
http_close_detection_delay_test(void *arg)
{
- _http_close_detection(arg, 1);
+ http_close_detection_(arg, 1);
}
static void
@@ -2375,6 +2585,7 @@ http_uriencode_test(void *ptr)
{
char *s=NULL, *s2=NULL;
size_t sz;
+ int bytes_decoded;
#define ENC(from,want,plus) do { \
s = evhttp_uriencode((from), -1, (plus)); \
@@ -2431,6 +2642,15 @@ http_uriencode_test(void *ptr)
free(s);
s = NULL;
+ /* Now try decoding just part of string. */
+ s = malloc(6 + 1 /* NUL byte */);
+ bytes_decoded = evhttp_decode_uri_internal("hello%20%20", 6, s, 0);
+ tt_assert(s);
+ tt_int_op(bytes_decoded,==,6);
+ tt_str_op(s,==,"hello%");
+ free(s);
+ s = NULL;
+
/* Now try out some decoding cases that we don't generate with
* encode_uri: Make sure that malformed stuff doesn't crash... */
DEC("%%xhello th+ere \xff",
@@ -2481,9 +2701,11 @@ http_base_test(void *ptr)
test_ok = 0;
base = event_base_new();
- http = http_setup(&port, base);
+ tt_assert(base);
+ http = http_setup(&port, base, 0);
fd = http_connect("127.0.0.1", port);
+ tt_int_op(fd, >=, 0);
/* Stupid thing to send a request */
bev = bufferevent_socket_new(base, fd, 0);
@@ -2551,7 +2773,7 @@ http_incomplete_writecb(struct bufferevent *bev, void *arg)
}
static void
-_http_incomplete_test(struct basic_test_data *data, int use_timeout)
+http_incomplete_test_(struct basic_test_data *data, int use_timeout)
{
struct bufferevent *bev;
evutil_socket_t fd;
@@ -2563,10 +2785,11 @@ _http_incomplete_test(struct basic_test_data *data, int use_timeout)
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
evhttp_set_timeout(http, 1);
fd = http_connect("127.0.0.1", port);
+ tt_int_op(fd, >=, 0);
/* Stupid thing to send a request */
bev = bufferevent_socket_new(data->base, fd, 0);
@@ -2590,6 +2813,7 @@ _http_incomplete_test(struct basic_test_data *data, int use_timeout)
bufferevent_free(bev);
if (use_timeout) {
evutil_closesocket(fd);
+ fd = -1;
}
evhttp_free(http);
@@ -2603,17 +2827,18 @@ _http_incomplete_test(struct basic_test_data *data, int use_timeout)
tt_int_op(test_ok, ==, 2);
end:
- ;
+ if (fd >= 0)
+ evutil_closesocket(fd);
}
static void
http_incomplete_test(void *arg)
{
- _http_incomplete_test(arg, 0);
+ http_incomplete_test_(arg, 0);
}
static void
http_incomplete_timeout_test(void *arg)
{
- _http_incomplete_test(arg, 1);
+ http_incomplete_test_(arg, 1);
}
/*
@@ -2629,22 +2854,24 @@ http_chunked_readcb(struct bufferevent *bev, void *arg)
static void
http_chunked_errorcb(struct bufferevent *bev, short what, void *arg)
{
+ struct evhttp_request *req = NULL;
+
if (!test_ok)
goto out;
test_ok = -1;
if ((what & BEV_EVENT_EOF) != 0) {
- struct evhttp_request *req = evhttp_request_new(NULL, NULL);
const char *header;
enum message_read_status done;
+ req = evhttp_request_new(NULL, NULL);
/* req->kind = EVHTTP_RESPONSE; */
- done = evhttp_parse_firstline(req, bufferevent_get_input(bev));
+ done = evhttp_parse_firstline_(req, bufferevent_get_input(bev));
if (done != ALL_DATA_READ)
goto out;
- done = evhttp_parse_headers(req, bufferevent_get_input(bev));
+ done = evhttp_parse_headers_(req, bufferevent_get_input(bev));
if (done != ALL_DATA_READ)
goto out;
@@ -2713,11 +2940,12 @@ http_chunked_errorcb(struct bufferevent *bev, short what, void *arg)
free((void *)header);
test_ok = 2;
-
- evhttp_request_free(req);
}
out:
+ if (req)
+ evhttp_request_free(req);
+
event_base_loopexit(arg, NULL);
}
@@ -2777,7 +3005,7 @@ http_chunk_out_test(void *arg)
exit_base = data->base;
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
fd = http_connect("127.0.0.1", port);
@@ -2849,7 +3077,7 @@ http_stream_out_test(void *arg)
test_ok = 0;
exit_base = data->base;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);
@@ -2908,7 +3136,7 @@ http_stream_in_done(struct evhttp_request *req, void *arg)
* Makes a request and reads the response in chunks.
*/
static void
-_http_stream_in_test(struct basic_test_data *data, char const *url,
+http_stream_in_test_(struct basic_test_data *data, char const *url,
size_t expected_len, char const *expected)
{
struct evhttp_connection *evcon;
@@ -2917,7 +3145,7 @@ _http_stream_in_test(struct basic_test_data *data, char const *url,
ev_uint16_t port = 0;
exit_base = data->base;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL,"127.0.0.1", port);
tt_assert(evcon);
@@ -2956,10 +3184,10 @@ _http_stream_in_test(struct basic_test_data *data, char const *url,
static void
http_stream_in_test(void *arg)
{
- _http_stream_in_test(arg, "/chunked", 13 + 18 + 8,
+ http_stream_in_test_(arg, "/chunked", 13 + 18 + 8,
"This is funnybut not hilarious.bwv 1052");
- _http_stream_in_test(arg, "/test", strlen(BASIC_REQUEST_BODY),
+ http_stream_in_test_(arg, "/test", strlen(BASIC_REQUEST_BODY),
BASIC_REQUEST_BODY);
}
@@ -2988,7 +3216,7 @@ http_stream_in_cancel_test(void *arg)
struct evhttp_request *req = NULL;
ev_uint16_t port = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);
@@ -3017,7 +3245,7 @@ http_connection_fail_done(struct evhttp_request *req, void *arg)
struct event_base *base = evhttp_connection_get_base(evcon);
/* An ENETUNREACH error results in an unrecoverable
- * evhttp_connection error (see evhttp_connection_fail()). The
+ * evhttp_connection error (see evhttp_connection_fail_()). The
* connection will be reset, and the user will be notified with a NULL
* req parameter. */
tt_assert(!req);
@@ -3044,7 +3272,7 @@ http_connection_fail_test(void *arg)
test_ok = 0;
/* auto detect a port */
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
evhttp_free(http);
http = NULL;
@@ -3094,7 +3322,7 @@ static void
http_make_web_server(evutil_socket_t fd, short what, void *arg)
{
ev_uint16_t port = *(ev_uint16_t*)arg;
- http = http_setup(&port, http_make_web_server_base);
+ http = http_setup(&port, http_make_web_server_base, 0);
}
static void
@@ -3110,7 +3338,7 @@ http_connection_retry_test(void *arg)
test_ok = 0;
/* auto detect a port */
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
evhttp_free(http);
http = NULL;
@@ -3150,7 +3378,12 @@ http_connection_retry_test(void *arg)
*/
test_ok = 0;
- evhttp_connection_set_timeout(evcon, 1);
+ {
+ const struct timeval tv_timeout = { 0, 500000 };
+ const struct timeval tv_retry = { 0, 500000 };
+ evhttp_connection_set_timeout_tv(evcon, &tv_timeout);
+ evhttp_connection_set_initial_retry_tv(evcon, &tv_retry);
+ }
evhttp_connection_set_retries(evcon, 1);
req = evhttp_request_new(http_connection_retry_done, data->base);
@@ -3167,9 +3400,9 @@ http_connection_retry_test(void *arg)
evutil_gettimeofday(&tv_start, NULL);
event_base_dispatch(data->base);
evutil_gettimeofday(&tv_end, NULL);
- evutil_timersub(&tv_end, &tv_start, &tv_end);
- tt_int_op(tv_end.tv_sec, >, 1);
- tt_int_op(tv_end.tv_sec, <, 6);
+
+ /* fails fast, .5 sec to wait to retry, fails fast again. */
+ test_timeval_diff_leq(&tv_start, &tv_end, 500, 200);
tt_assert(test_ok == 1);
@@ -3193,22 +3426,19 @@ http_connection_retry_test(void *arg)
tt_abort_msg("Couldn't make request");
}
- /* start up a web server one second after the connection tried
+ /* start up a web server .2 seconds after the connection tried
* to send a request
*/
evutil_timerclear(&tv);
- tv.tv_sec = 1;
+ tv.tv_usec = 200000;
http_make_web_server_base = data->base;
event_base_once(data->base, -1, EV_TIMEOUT, http_make_web_server, &port, &tv);
evutil_gettimeofday(&tv_start, NULL);
event_base_dispatch(data->base);
evutil_gettimeofday(&tv_end, NULL);
-
- evutil_timersub(&tv_end, &tv_start, &tv_end);
-
- tt_int_op(tv_end.tv_sec, >, 1);
- tt_int_op(tv_end.tv_sec, <, 6);
+ /* We'll wait twice as long as we did last time. */
+ test_timeval_diff_leq(&tv_start, &tv_end, 1000, 400);
tt_int_op(test_ok, ==, 1);
@@ -3260,12 +3490,17 @@ http_multi_line_header_test(void *arg)
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
+
+ tt_ptr_op(http, !=, NULL);
fd = http_connect("127.0.0.1", port);
+ tt_int_op(fd, !=, -1);
+
/* Stupid thing to send a request */
bev = bufferevent_socket_new(data->base, fd, 0);
+ tt_ptr_op(bev, !=, NULL);
bufferevent_setcb(bev, http_readcb, http_writecb,
http_errorcb, data->base);
@@ -3273,6 +3508,8 @@ http_multi_line_header_test(void *arg)
"GET /test HTTP/1.1\r\n"
"Host: somehost\r\n"
"Connection: close\r\n"
+ "X-Multi-Extra-WS: libevent \r\n"
+ "\t\t\t2.1 \r\n"
"X-Multi: aaaaaaaa\r\n"
" a\r\n"
"\tEND\r\n"
@@ -3280,9 +3517,12 @@ http_multi_line_header_test(void *arg)
"\r\n";
bufferevent_write(bev, http_start_request, strlen(http_start_request));
+ found_multi = found_multi2 = 0;
event_base_dispatch(data->base);
+ tt_int_op(found_multi, ==, 1);
+ tt_int_op(found_multi2, ==, 1);
tt_int_op(test_ok, ==, 4);
end:
if (bev)
@@ -3315,7 +3555,7 @@ http_negative_content_length_test(void *arg)
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);
@@ -3374,7 +3614,7 @@ http_data_length_constraints_test(void *arg)
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);
@@ -3532,7 +3772,7 @@ http_terminate_chunked_test(void *arg)
test_ok = 0;
- http = http_setup(&port, data->base);
+ http = http_setup(&port, data->base, 0);
evhttp_del_cb(http, "/test");
tt_assert(evhttp_set_cb(http, "/test",
terminate_chunked_cb, &terminate_state) == 0);
@@ -3573,6 +3813,122 @@ http_terminate_chunked_test(void *arg)
evhttp_free(http);
}
+static struct regress_dns_server_table ipv6_search_table[] = {
+ { "localhost", "AAAA", "::1", 0 },
+ { NULL, NULL, NULL, 0 }
+};
+
+static void
+http_ipv6_for_domain_test_impl(void *arg, int family)
+{
+ struct basic_test_data *data = arg;
+ struct evdns_base *dns_base = NULL;
+ ev_uint16_t portnum = 0;
+ char address[64];
+
+ tt_assert(regress_dnsserver(data->base, &portnum, ipv6_search_table));
+
+ dns_base = evdns_base_new(data->base, 0/* init name servers */);
+ tt_assert(dns_base);
+
+ /* Add ourself as the only nameserver, and make sure we really are
+ * the only nameserver. */
+ evutil_snprintf(address, sizeof(address), "127.0.0.1:%d", portnum);
+ evdns_base_nameserver_ip_add(dns_base, address);
+
+ http_connection_test_(arg, 0 /* not persistent */, "localhost", dns_base,
+ 1 /* ipv6 */, family);
+
+ end:
+ if (dns_base)
+ evdns_base_free(dns_base, 0);
+ regress_clean_dnsserver();
+}
+static void
+http_ipv6_for_domain_test(void *arg)
+{
+ http_ipv6_for_domain_test_impl(arg, AF_UNSPEC);
+}
+
+static void
+http_request_get_addr_on_close(struct evhttp_connection *evcon, void *arg)
+{
+ const struct sockaddr *storage;
+ char addrbuf[128];
+ char local[] = "127.0.0.1:";
+
+ test_ok = 0;
+ tt_assert(evcon);
+
+ storage = evhttp_connection_get_addr(evcon);
+ tt_assert(storage);
+
+ evutil_format_sockaddr_port_((struct sockaddr *)storage, addrbuf, sizeof(addrbuf));
+ tt_assert(!strncmp(addrbuf, local, sizeof(local) - 1));
+
+ test_ok = 1;
+ return;
+
+end:
+ test_ok = 0;
+}
+
+static void
+http_get_addr_test(void *arg)
+{
+ struct basic_test_data *data = arg;
+ ev_uint16_t port = 0;
+ struct evhttp_connection *evcon = NULL;
+ struct evhttp_request *req = NULL;
+
+ test_ok = 0;
+ exit_base = data->base;
+
+ http = http_setup(&port, data->base, 0);
+
+ evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
+ tt_assert(evcon);
+ evhttp_connection_set_closecb(evcon, http_request_get_addr_on_close, arg);
+
+ /*
+ * At this point, we want to schedule a request to the HTTP
+ * server using our make request method.
+ */
+
+ req = evhttp_request_new(http_request_done, (void *)BASIC_REQUEST_BODY);
+
+ /* We give ownership of the request to the connection */
+ if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
+ tt_abort_msg("Couldn't make request");
+ }
+
+ event_base_dispatch(data->base);
+
+ http_request_get_addr_on_close(evcon, NULL);
+
+ end:
+ if (evcon)
+ evhttp_connection_free(evcon);
+ if (http)
+ evhttp_free(http);
+}
+
+static void
+http_set_family_test(void *arg)
+{
+ http_connection_test_(arg, 0, "127.0.0.1", NULL, 0, AF_UNSPEC);
+}
+static void
+http_set_family_ipv4_test(void *arg)
+{
+ http_connection_test_(arg, 0, "127.0.0.1", NULL, 0, AF_INET);
+}
+static void
+http_set_family_ipv6_test(void *arg)
+{
+ http_ipv6_for_domain_test_impl(arg, AF_INET6);
+}
+
#define HTTP_LEGACY(name) \
{ #name, run_legacy_test_fn, TT_ISOLATED|TT_LEGACY, &legacy_setup, \
http_##name##_test }
@@ -3598,6 +3954,7 @@ struct testcase_t http_testcases[] = {
HTTP(failure),
HTTP(connection),
HTTP(persist_connection),
+ HTTP(autofree_connection),
HTTP(connection_async),
HTTP(close_detection),
HTTP(close_detection_delay),
@@ -3605,6 +3962,7 @@ struct testcase_t http_testcases[] = {
HTTP(incomplete),
HTTP(incomplete_timeout),
HTTP(terminate_chunked),
+ HTTP(on_complete),
HTTP(highport),
HTTP(dispatcher),
@@ -3617,9 +3975,17 @@ struct testcase_t http_testcases[] = {
HTTP(stream_in_cancel),
HTTP(connection_fail),
- HTTP(connection_retry),
+ { "connection_retry", http_connection_retry_test, TT_ISOLATED|TT_OFF_BY_DEFAULT, &basic_setup, NULL },
+
HTTP(data_length_constraints),
+ HTTP(ipv6_for_domain),
+ HTTP(get_addr),
+
+ HTTP(set_family),
+ HTTP(set_family_ipv4),
+ HTTP(set_family_ipv6),
+
END_OF_TESTCASES
};
diff --git a/test/regress_iocp.c b/test/regress_iocp.c
index 916d3223..17b38524 100644
--- a/test/regress_iocp.c
+++ b/test/regress_iocp.c
@@ -167,27 +167,27 @@ test_iocp_port(void *ptr)
tt_assert(o1.lock);
tt_assert(o2.lock);
- event_overlapped_init(&o1.eo, dummy_cb);
- event_overlapped_init(&o2.eo, dummy_cb);
+ event_overlapped_init_(&o1.eo, dummy_cb);
+ event_overlapped_init_(&o2.eo, dummy_cb);
- port = event_iocp_port_launch(0);
+ port = event_iocp_port_launch_(0);
tt_assert(port);
- tt_assert(!event_iocp_activate_overlapped(port, &o1.eo, 10, 100));
- tt_assert(!event_iocp_activate_overlapped(port, &o2.eo, 20, 200));
+ tt_assert(!event_iocp_activate_overlapped_(port, &o1.eo, 10, 100));
+ tt_assert(!event_iocp_activate_overlapped_(port, &o2.eo, 20, 200));
- tt_assert(!event_iocp_activate_overlapped(port, &o1.eo, 11, 101));
- tt_assert(!event_iocp_activate_overlapped(port, &o2.eo, 21, 201));
+ tt_assert(!event_iocp_activate_overlapped_(port, &o1.eo, 11, 101));
+ tt_assert(!event_iocp_activate_overlapped_(port, &o2.eo, 21, 201));
- tt_assert(!event_iocp_activate_overlapped(port, &o1.eo, 12, 102));
- tt_assert(!event_iocp_activate_overlapped(port, &o2.eo, 22, 202));
+ tt_assert(!event_iocp_activate_overlapped_(port, &o1.eo, 12, 102));
+ tt_assert(!event_iocp_activate_overlapped_(port, &o2.eo, 22, 202));
- tt_assert(!event_iocp_activate_overlapped(port, &o1.eo, 13, 103));
- tt_assert(!event_iocp_activate_overlapped(port, &o2.eo, 23, 203));
+ tt_assert(!event_iocp_activate_overlapped_(port, &o1.eo, 13, 103));
+ tt_assert(!event_iocp_activate_overlapped_(port, &o2.eo, 23, 203));
tt_int_op(count_wait_for(8, 2000), ==, 0);
- tt_want(!event_iocp_shutdown(port, 2000));
+ tt_want(!event_iocp_shutdown_(port, 2000));
tt_int_op(o1.call_count, ==, 4);
tt_int_op(o2.call_count, ==, 4);
@@ -215,7 +215,7 @@ read_complete(struct event_overlapped *eo, uintptr_t key,
ev_ssize_t nbytes, int ok)
{
tt_assert(ok);
- evbuffer_commit_read(rbuf, nbytes);
+ evbuffer_commit_read_(rbuf, nbytes);
count_incr();
end:
;
@@ -226,7 +226,7 @@ write_complete(struct event_overlapped *eo, uintptr_t key,
ev_ssize_t nbytes, int ok)
{
tt_assert(ok);
- evbuffer_commit_write(wbuf, nbytes);
+ evbuffer_commit_write_(wbuf, nbytes);
count_incr();
end:
;
@@ -244,24 +244,24 @@ test_iocp_evbuffer(void *ptr)
int i;
count_init();
- event_overlapped_init(&rol, read_complete);
- event_overlapped_init(&wol, write_complete);
+ event_overlapped_init_(&rol, read_complete);
+ event_overlapped_init_(&wol, write_complete);
for (i = 0; i < (int)sizeof(junk); ++i)
junk[i] = (char)(i);
- rbuf = evbuffer_overlapped_new(data->pair[0]);
- wbuf = evbuffer_overlapped_new(data->pair[1]);
+ rbuf = evbuffer_overlapped_new_(data->pair[0]);
+ wbuf = evbuffer_overlapped_new_(data->pair[1]);
evbuffer_enable_locking(rbuf, NULL);
evbuffer_enable_locking(wbuf, NULL);
- port = event_iocp_port_launch(0);
+ port = event_iocp_port_launch_(0);
tt_assert(port);
tt_assert(rbuf);
tt_assert(wbuf);
- tt_assert(!event_iocp_port_associate(port, data->pair[0], 100));
- tt_assert(!event_iocp_port_associate(port, data->pair[1], 100));
+ tt_assert(!event_iocp_port_associate_(port, data->pair[0], 100));
+ tt_assert(!event_iocp_port_associate_(port, data->pair[1], 100));
for (i=0;i<10;++i)
evbuffer_add(wbuf, junk, sizeof(junk));
@@ -269,13 +269,13 @@ test_iocp_evbuffer(void *ptr)
buf = evbuffer_new();
tt_assert(buf != NULL);
evbuffer_add(rbuf, junk, sizeof(junk));
- tt_assert(!evbuffer_launch_read(rbuf, 2048, &rol));
+ tt_assert(!evbuffer_launch_read_(rbuf, 2048, &rol));
evbuffer_add_buffer(buf, rbuf);
tt_int_op(evbuffer_get_length(buf), ==, sizeof(junk));
for (chain = buf->first; chain; chain = chain->next)
tt_int_op(chain->flags & EVBUFFER_MEM_PINNED_ANY, ==, 0);
tt_assert(!evbuffer_get_length(rbuf));
- tt_assert(!evbuffer_launch_write(wbuf, 512, &wol));
+ tt_assert(!evbuffer_launch_write_(wbuf, 512, &wol));
tt_int_op(count_wait_for(2, 2000), ==, 0);
@@ -283,7 +283,7 @@ test_iocp_evbuffer(void *ptr)
/* FIXME Actually test some stuff here. */
- tt_want(!event_iocp_shutdown(port, 2000));
+ tt_want(!event_iocp_shutdown_(port, 2000));
end:
count_free();
evbuffer_free(rbuf);
@@ -310,13 +310,13 @@ test_iocp_bufferevent_async(void *ptr)
char buf[128];
size_t n;
- event_base_start_iocp(data->base, 0);
- port = event_base_get_iocp(data->base);
+ event_base_start_iocp_(data->base, 0);
+ port = event_base_get_iocp_(data->base);
tt_assert(port);
- bea1 = bufferevent_async_new(data->base, data->pair[0],
+ bea1 = bufferevent_async_new_(data->base, data->pair[0],
BEV_OPT_DEFER_CALLBACKS);
- bea2 = bufferevent_async_new(data->base, data->pair[1],
+ bea2 = bufferevent_async_new_(data->base, data->pair[1],
BEV_OPT_DEFER_CALLBACKS);
tt_assert(bea1);
tt_assert(bea2);
diff --git a/test/regress_listener.c b/test/regress_listener.c
index 8938546d..4db102df 100644
--- a/test/regress_listener.c
+++ b/test/regress_listener.c
@@ -23,15 +23,16 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "util-internal.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#endif
#include <sys/types.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
# ifdef _XOPEN_SOURCE_EXTENDED
@@ -49,7 +50,6 @@
#include "regress.h"
#include "tinytest.h"
#include "tinytest_macros.h"
-#include "util-internal.h"
static void
acceptcb(struct evconnlistener *listener, evutil_socket_t fd,
@@ -115,11 +115,11 @@ regress_pick_a_port(void *arg)
tt_ptr_op(evconnlistener_get_base(listener2), ==, base);
fd1 = fd2 = fd3 = -1;
- evutil_socket_connect(&fd1, (struct sockaddr*)&ss1, slen1);
- evutil_socket_connect(&fd2, (struct sockaddr*)&ss1, slen1);
- evutil_socket_connect(&fd3, (struct sockaddr*)&ss2, slen2);
+ evutil_socket_connect_(&fd1, (struct sockaddr*)&ss1, slen1);
+ evutil_socket_connect_(&fd2, (struct sockaddr*)&ss1, slen1);
+ evutil_socket_connect_(&fd3, (struct sockaddr*)&ss2, slen2);
-#ifdef WIN32
+#ifdef _WIN32
Sleep(100); /* XXXX this is a stupid stopgap. */
#endif
event_base_dispatch(base);
diff --git a/test/regress_main.c b/test/regress_main.c
index 1649e9df..3198ced1 100644
--- a/test/regress_main.c
+++ b/test/regress_main.c
@@ -24,8 +24,9 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "util-internal.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#include <io.h>
@@ -42,14 +43,14 @@
#include "event2/event-config.h"
-#ifdef _EVENT___func__
-#define __func__ _EVENT___func__
+#ifdef EVENT____func__
+#define __func__ EVENT____func__
#endif
#if 0
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
@@ -58,11 +59,11 @@
#endif
#include <sys/types.h>
-#ifdef _EVENT_HAVE_SYS_STAT_H
+#ifdef EVENT__HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
@@ -89,6 +90,8 @@
#include "../iocp-internal.h"
#include "../event-internal.h"
+struct evutil_weakrand_state test_weakrand_state;
+
long
timeval_msec_diff(const struct timeval *start, const struct timeval *end)
{
@@ -96,7 +99,6 @@ timeval_msec_diff(const struct timeval *start, const struct timeval *end)
ms *= 1000;
ms += ((end->tv_usec - start->tv_usec)+500) / 1000;
return ms;
-
}
/* ============================================================ */
@@ -118,15 +120,17 @@ static void dnslogcb(int w, const char *m)
TT_BLATHER(("%s", m));
}
-/* creates a temporary file with the data in it */
+/* creates a temporary file with the data in it. If *filename_out gets set,
+ * the caller should try to unlink it. */
int
-regress_make_tmpfile(const void *data, size_t datalen)
+regress_make_tmpfile(const void *data, size_t datalen, char **filename_out)
{
-#ifndef WIN32
+#ifndef _WIN32
char tmpfilename[32];
int fd;
+ *filename_out = NULL;
strcpy(tmpfilename, "/tmp/eventtmp.XXXXXX");
-#ifdef _EVENT_HAVE_UMASK
+#ifdef EVENT__HAVE_UMASK
umask(0077);
#endif
fd = mkstemp(tmpfilename);
@@ -162,6 +166,7 @@ regress_make_tmpfile(const void *data, size_t datalen)
if (tries == 0)
return (-1);
written = 0;
+ *filename_out = strdup(tmpfilename);
WriteFile(h, data, (DWORD)datalen, &written, NULL);
/* Closing the fd returned by this function will indeed close h. */
return _open_osfhandle((intptr_t)h,_O_RDONLY);
@@ -192,7 +197,7 @@ basic_test_setup(const struct testcase_t *testcase)
evutil_socket_t spair[2] = { -1, -1 };
struct basic_test_data *data = NULL;
-#ifndef WIN32
+#ifndef _WIN32
if (testcase->flags & TT_ENABLE_IOCP_FLAG)
return (void*)TT_SKIP;
#endif
@@ -236,7 +241,7 @@ basic_test_setup(const struct testcase_t *testcase)
exit(1);
}
if (testcase->flags & TT_ENABLE_IOCP_FLAG) {
- if (event_base_start_iocp(base, 0)<0) {
+ if (event_base_start_iocp_(base, 0)<0) {
event_base_free(base);
return (void*)TT_SKIP;
}
@@ -282,11 +287,14 @@ basic_test_cleanup(const struct testcase_t *testcase, void *ptr)
if (testcase->flags & TT_NEED_BASE) {
if (data->base) {
- event_base_assert_ok(data->base);
+ event_base_assert_ok_(data->base);
event_base_free(data->base);
}
}
+ if (testcase->flags & TT_FORK)
+ libevent_global_shutdown();
+
free(data);
return 1;
@@ -354,7 +362,7 @@ const struct testcase_setup_t legacy_setup = {
/* ============================================================ */
-#if (!defined(_EVENT_HAVE_PTHREADS) && !defined(WIN32)) || defined(_EVENT_DISABLE_THREAD_SUPPORT)
+#if (!defined(EVENT__HAVE_PTHREADS) && !defined(_WIN32)) || defined(EVENT__DISABLE_THREAD_SUPPORT)
struct testcase_t thread_testcases[] = {
{ "basic", NULL, TT_SKIP, NULL, NULL },
END_OF_TESTCASES
@@ -365,6 +373,7 @@ struct testgroup_t testgroups[] = {
{ "main/", main_testcases },
{ "heap/", minheap_testcases },
{ "et/", edgetriggered_testcases },
+ { "finalize/", finalize_testcases },
{ "evbuffer/", evbuffer_testcases },
{ "signal/", signal_testcases },
{ "util/", util_testcases },
@@ -375,21 +384,44 @@ struct testgroup_t testgroups[] = {
{ "rpc/", rpc_testcases },
{ "thread/", thread_testcases },
{ "listener/", listener_testcases },
-#ifdef WIN32
+#ifdef _WIN32
{ "iocp/", iocp_testcases },
{ "iocp/bufferevent/", bufferevent_iocp_testcases },
{ "iocp/listener/", listener_iocp_testcases },
#endif
-#ifdef _EVENT_HAVE_OPENSSL
+#ifdef EVENT__HAVE_OPENSSL
{ "ssl/", ssl_testcases },
#endif
END_OF_GROUPS
};
+const char *alltests[] = { "+..", NULL };
+const char *livenettests[] = {
+ "+util/getaddrinfo_live",
+ "+dns/gethostby..",
+ "+dns/resolve_reverse",
+ NULL
+};
+const char *finetimetests[] = {
+ "+util/monotonic_res_precise",
+ "+util/monotonic_res_fallback",
+ "+thread/deferred_cb_skew",
+ "+http/connection_retry",
+ NULL
+};
+struct testlist_alias_t testaliases[] = {
+ { "all", alltests },
+ { "live_net", livenettests },
+ { "fine_timing", finetimetests },
+ END_OF_ALIASES
+};
+
+int libevent_tests_running_in_debug_mode = 0;
+
int
main(int argc, const char **argv)
{
-#ifdef WIN32
+#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
@@ -398,23 +430,37 @@ main(int argc, const char **argv)
(void) WSAStartup(wVersionRequested, &wsaData);
#endif
-#ifndef WIN32
+#ifndef _WIN32
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
return 1;
#endif
-#ifdef WIN32
+#ifdef _WIN32
tinytest_skip(testgroups, "http/connection_retry");
#endif
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
if (!getenv("EVENT_NO_DEBUG_LOCKS"))
- evthread_enable_lock_debuging();
+ evthread_enable_lock_debugging();
#endif
+ if (getenv("EVENT_DEBUG_MODE")) {
+ event_enable_debug_mode();
+ libevent_tests_running_in_debug_mode = 1;
+ }
+ if (getenv("EVENT_DEBUG_LOGGING_ALL")) {
+ event_enable_debug_logging(EVENT_DBG_ALL);
+ }
+
+ tinytest_set_aliases(testaliases);
+
+ evutil_weakrand_seed_(&test_weakrand_state, 0);
+
if (tinytest_main(argc,argv,testgroups))
return 1;
+ libevent_global_shutdown();
+
return 0;
}
diff --git a/test/regress_minheap.c b/test/regress_minheap.c
index 049d22a0..05db32e2 100644
--- a/test/regress_minheap.c
+++ b/test/regress_minheap.c
@@ -23,19 +23,20 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "../minheap-internal.h"
#include <stdlib.h>
#include "event2/event_struct.h"
#include "tinytest.h"
#include "tinytest_macros.h"
-#include "../minheap-internal.h"
+#include "regress.h"
static void
set_random_timeout(struct event *ev)
{
- ev->ev_timeout.tv_sec = rand();
- ev->ev_timeout.tv_usec = rand() & 0xfffff;
+ ev->ev_timeout.tv_sec = test_weakrand();
+ ev->ev_timeout.tv_usec = test_weakrand() & 0xfffff;
ev->ev_timeout_pos.min_heap_idx = -1;
}
@@ -58,38 +59,38 @@ test_heap_randomized(void *ptr)
struct event *e, *last_e;
int i;
- min_heap_ctor(&heap);
+ min_heap_ctor_(&heap);
for (i = 0; i < 1024; ++i) {
inserted[i] = malloc(sizeof(struct event));
set_random_timeout(inserted[i]);
- min_heap_push(&heap, inserted[i]);
+ min_heap_push_(&heap, inserted[i]);
}
check_heap(&heap);
- tt_assert(min_heap_size(&heap) == 1024);
+ tt_assert(min_heap_size_(&heap) == 1024);
for (i = 0; i < 512; ++i) {
- min_heap_erase(&heap, inserted[i]);
+ min_heap_erase_(&heap, inserted[i]);
if (0 == (i % 32))
check_heap(&heap);
}
- tt_assert(min_heap_size(&heap) == 512);
+ tt_assert(min_heap_size_(&heap) == 512);
- last_e = min_heap_pop(&heap);
+ last_e = min_heap_pop_(&heap);
while (1) {
- e = min_heap_pop(&heap);
+ e = min_heap_pop_(&heap);
if (!e)
break;
tt_want(evutil_timercmp(&last_e->ev_timeout,
&e->ev_timeout, <=));
}
- tt_assert(min_heap_size(&heap) == 0);
+ tt_assert(min_heap_size_(&heap) == 0);
end:
for (i = 0; i < 1024; ++i)
free(inserted[i]);
- min_heap_dtor(&heap);
+ min_heap_dtor_(&heap);
}
struct testcase_t minheap_testcases[] = {
diff --git a/test/regress_rpc.c b/test/regress_rpc.c
index 7604ca44..01a058cb 100644
--- a/test/regress_rpc.c
+++ b/test/regress_rpc.c
@@ -28,7 +28,7 @@
/* The old tests here need assertions to work. */
#undef NDEBUG
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#endif
@@ -37,11 +37,11 @@
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/socket.h>
#include <signal.h>
#include <unistd.h>
@@ -463,12 +463,14 @@ rpc_basic_client(void)
!= NULL);
pool = rpc_pool_with_connection(port);
+ tt_assert(pool);
assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_add_meta, NULL));
assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_remove_header, (void*)"output"));
/* set up the basic message */
msg = msg_new();
+ tt_assert(msg);
EVTAG_ASSIGN(msg, from_name, "niels");
EVTAG_ASSIGN(msg, to_name, "tester");
@@ -539,9 +541,11 @@ rpc_basic_queued_client(void)
rpc_setup(&http, &port, &base);
pool = rpc_pool_with_connection(port);
+ tt_assert(pool);
/* set up the basic message */
msg = msg_new();
+ tt_assert(msg);
EVTAG_ASSIGN(msg, from_name, "niels");
EVTAG_ASSIGN(msg, to_name, "tester");
@@ -592,7 +596,7 @@ done:
/* we just pause the rpc and continue it in the next callback */
-struct _rpc_hook_ctx {
+struct rpc_hook_ctx_ {
void *vbase;
void *ctx;
};
@@ -602,7 +606,7 @@ static int hook_pause_cb_called=0;
static void
rpc_hook_pause_cb(evutil_socket_t fd, short what, void *arg)
{
- struct _rpc_hook_ctx *ctx = arg;
+ struct rpc_hook_ctx_ *ctx = arg;
++hook_pause_cb_called;
evrpc_resume_request(ctx->vbase, ctx->ctx, EVRPC_CONTINUE);
free(arg);
@@ -612,7 +616,7 @@ static int
rpc_hook_pause(void *ctx, struct evhttp_request *req, struct evbuffer *evbuf,
void *arg)
{
- struct _rpc_hook_ctx *tmp = malloc(sizeof(*tmp));
+ struct rpc_hook_ctx_ *tmp = malloc(sizeof(*tmp));
struct timeval tv;
assert(tmp != NULL);
@@ -640,12 +644,13 @@ rpc_basic_client_with_pause(void)
assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_pause, base));
pool = rpc_pool_with_connection(port);
-
+ tt_assert(pool);
assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_pause, pool));
assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_pause, pool));
/* set up the basic message */
msg = msg_new();
+ tt_assert(msg);
EVTAG_ASSIGN(msg, from_name, "niels");
EVTAG_ASSIGN(msg, to_name, "tester");
@@ -688,12 +693,14 @@ rpc_client_timeout(void)
rpc_setup(&http, &port, &base);
pool = rpc_pool_with_connection(port);
+ tt_assert(pool);
- /* set the timeout to 5 seconds */
- evrpc_pool_set_timeout(pool, 5);
+ /* set the timeout to 1 second. */
+ evrpc_pool_set_timeout(pool, 1);
/* set up the basic message */
msg = msg_new();
+ tt_assert(msg);
EVTAG_ASSIGN(msg, from_name, "niels");
EVTAG_ASSIGN(msg, to_name, "tester");
diff --git a/test/regress_ssl.c b/test/regress_ssl.c
index 1ce5f6aa..bf9b46b1 100644
--- a/test/regress_ssl.c
+++ b/test/regress_ssl.c
@@ -24,12 +24,17 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifdef WIN32
+// Get rid of OSX 10.7 and greater deprecation warnings.
+#if defined(__APPLE__) && defined(__clang__)
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+#endif
+
+#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#endif
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -159,7 +164,7 @@ init_ssl(void)
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
if (SSLeay() != OPENSSL_VERSION_NUMBER) {
- TT_DECLARE("WARN", ("Version mismatch for openssl: compiled with %lx but running with %lx", (unsigned long)OPENSSL_VERSION_NUMBER, (unsigned long)SSLeay()));
+ TT_DECLARE("WARN", ("Version mismatch for openssl: compiled with %lx but running with %lx", (unsigned long)OPENSSL_VERSION_NUMBER, (unsigned long) SSLeay()));
}
}
@@ -189,6 +194,7 @@ respond_to_number(struct bufferevent *bev, void *ctx)
n = atoi(line);
if (n <= 0)
TT_FAIL(("Bad number: %s", line));
+ free(line);
TT_BLATHER(("The number was %d", n));
if (n == 1001) {
++test_is_done;
diff --git a/test/regress_testutils.c b/test/regress_testutils.c
index c70c9a17..1f004603 100644
--- a/test/regress_testutils.c
+++ b/test/regress_testutils.c
@@ -23,8 +23,9 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "../util-internal.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
@@ -34,18 +35,18 @@
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/socket.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
-#ifdef _EVENT_HAVE_NETINET_IN6_H
+#ifdef EVENT__HAVE_NETINET_IN6_H
#include <netinet/in6.h>
#endif
#ifdef HAVE_NETDB_H
@@ -68,8 +69,6 @@
#include "regress.h"
#include "regress_testutils.h"
-#include "../util-internal.h"
-
/* globals */
static struct evdns_server_port *dns_port;
evutil_socket_t dns_sock = -1;
diff --git a/test/regress_testutils.h b/test/regress_testutils.h
index 1f09427d..f90b9c99 100644
--- a/test/regress_testutils.h
+++ b/test/regress_testutils.h
@@ -24,8 +24,8 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _TESTUTILS_H
-#define _TESTUTILS_H
+#ifndef REGRESS_TESTUTILS_H_INCLUDED_
+#define REGRESS_TESTUTILS_H_INCLUDED_
#include "event2/dns.h"
@@ -62,5 +62,5 @@ struct sockaddr;
int regress_get_listener_addr(struct evconnlistener *lev,
struct sockaddr *sa, ev_socklen_t *socklen);
-#endif /* _TESTUTILS_H */
+#endif /* REGRESS_TESTUTILS_H_INCLUDED_ */
diff --git a/test/regress_thread.c b/test/regress_thread.c
index 1acf6824..131d6def 100644
--- a/test/regress_thread.c
+++ b/test/regress_thread.c
@@ -23,6 +23,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "util-internal.h"
/* The old tests here need assertions to work. */
#undef NDEBUG
@@ -33,53 +34,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
-#ifdef _EVENT_HAVE_SYS_WAIT_H
+#ifdef EVENT__HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
-#ifdef _EVENT_HAVE_PTHREADS
+#ifdef EVENT__HAVE_PTHREADS
#include <pthread.h>
-#elif defined(WIN32)
+#elif defined(_WIN32)
#include <process.h>
#endif
#include <assert.h>
-#ifdef _EVENT_HAVE_UNISTD_H
+#ifdef EVENT__HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <time.h>
#include "sys/queue.h"
-#include "event2/util.h"
#include "event2/event.h"
#include "event2/event_struct.h"
#include "event2/thread.h"
+#include "event2/util.h"
#include "evthread-internal.h"
#include "event-internal.h"
#include "defer-internal.h"
#include "regress.h"
#include "tinytest_macros.h"
-
-#ifdef _EVENT_HAVE_PTHREADS
-#define THREAD_T pthread_t
-#define THREAD_FN void *
-#define THREAD_RETURN() return (NULL)
-#define THREAD_START(threadvar, fn, arg) \
- pthread_create(&(threadvar), NULL, fn, arg)
-#define THREAD_JOIN(th) pthread_join(th, NULL)
-#else
-#define THREAD_T HANDLE
-#define THREAD_FN unsigned __stdcall
-#define THREAD_RETURN() return (0)
-#define THREAD_START(threadvar, fn, arg) do { \
- uintptr_t threadhandle = _beginthreadex(NULL,0,fn,(arg),0,NULL); \
- (threadvar) = (HANDLE) threadhandle; \
- } while (0)
-#define THREAD_JOIN(th) WaitForSingleObject(th, INFINITE)
-#endif
+#include "time-internal.h"
+#include "regress_thread.h"
struct cond_wait {
void *lock;
@@ -156,7 +141,7 @@ basic_thread(void *arg)
}
static int notification_fd_used = 0;
-#ifndef WIN32
+#ifndef _WIN32
static int got_sigchld = 0;
static void
sigchld_cb(evutil_socket_t fd, short event, void *arg)
@@ -199,7 +184,7 @@ thread_basic(void *arg)
tt_abort_msg("Couldn't make base notifiable!");
}
-#ifndef WIN32
+#ifndef _WIN32
if (data->setup_data && !strcmp(data->setup_data, "forking")) {
pid_t pid;
int status;
@@ -397,31 +382,35 @@ thread_conditions_simple(void *arg)
tt_int_op(n_signal, ==, 1);
end:
- ;
+ EVTHREAD_FREE_LOCK(cond.lock, EVTHREAD_LOCKTYPE_RECURSIVE);
+ EVTHREAD_FREE_COND(cond.cond);
}
#define CB_COUNT 128
#define QUEUE_THREAD_COUNT 8
-#ifdef WIN32
-#define SLEEP_MS(ms) Sleep(ms)
-#else
-#define SLEEP_MS(ms) usleep((ms) * 1000)
-#endif
+static void
+SLEEP_MS(int ms)
+{
+ struct timeval tv;
+ tv.tv_sec = ms/1000;
+ tv.tv_usec = (ms%1000)*1000;
+ evutil_usleep_(&tv);
+}
struct deferred_test_data {
- struct deferred_cb cbs[CB_COUNT];
- struct deferred_cb_queue *queue;
+ struct event_callback cbs[CB_COUNT];
+ struct event_base *queue;
};
-static time_t timer_start = 0;
-static time_t timer_end = 0;
+static struct timeval timer_start = {0,0};
+static struct timeval timer_end = {0,0};
static unsigned callback_count = 0;
static THREAD_T load_threads[QUEUE_THREAD_COUNT];
static struct deferred_test_data deferred_data[QUEUE_THREAD_COUNT];
static void
-deferred_callback(struct deferred_cb *cb, void *arg)
+deferred_callback(struct event_callback *cb, void *arg)
{
SLEEP_MS(1);
callback_count += 1;
@@ -434,8 +423,9 @@ load_deferred_queue(void *arg)
size_t i;
for (i = 0; i < CB_COUNT; ++i) {
- event_deferred_cb_init(&data->cbs[i], deferred_callback, NULL);
- event_deferred_cb_schedule(data->queue, &data->cbs[i]);
+ event_deferred_cb_init_(&data->cbs[i], 0, deferred_callback,
+ NULL);
+ event_deferred_cb_schedule_(data->queue, &data->cbs[i]);
SLEEP_MS(1);
}
@@ -445,7 +435,7 @@ load_deferred_queue(void *arg)
static void
timer_callback(evutil_socket_t fd, short what, void *arg)
{
- timer_end = time(NULL);
+ evutil_gettimeofday(&timer_end, NULL);
}
static void
@@ -462,35 +452,116 @@ start_threads_callback(evutil_socket_t fd, short what, void *arg)
static void
thread_deferred_cb_skew(void *arg)
{
- struct basic_test_data *data = arg;
- struct timeval tv_timer = {4, 0};
- struct deferred_cb_queue *queue;
- time_t elapsed;
+ struct timeval tv_timer = {1, 0};
+ struct event_base *base = NULL;
+ struct event_config *cfg = NULL;
+ struct timeval elapsed;
+ int elapsed_usec;
int i;
- queue = event_base_get_deferred_cb_queue(data->base);
- tt_assert(queue);
+ cfg = event_config_new();
+ tt_assert(cfg);
+ event_config_set_max_dispatch_interval(cfg, NULL, 16, 0);
+
+ base = event_base_new_with_config(cfg);
+ tt_assert(base);
for (i = 0; i < QUEUE_THREAD_COUNT; ++i)
- deferred_data[i].queue = queue;
+ deferred_data[i].queue = base;
- timer_start = time(NULL);
- event_base_once(data->base, -1, EV_TIMEOUT, timer_callback, NULL,
+ evutil_gettimeofday(&timer_start, NULL);
+ event_base_once(base, -1, EV_TIMEOUT, timer_callback, NULL,
&tv_timer);
- event_base_once(data->base, -1, EV_TIMEOUT, start_threads_callback,
+ event_base_once(base, -1, EV_TIMEOUT, start_threads_callback,
NULL, NULL);
- event_base_dispatch(data->base);
+ event_base_dispatch(base);
- elapsed = timer_end - timer_start;
+ evutil_timersub(&timer_end, &timer_start, &elapsed);
TT_BLATHER(("callback count, %u", callback_count));
- TT_BLATHER(("elapsed time, %u", (unsigned)elapsed));
+ elapsed_usec =
+ (unsigned)(elapsed.tv_sec*1000000 + elapsed.tv_usec);
+ TT_BLATHER(("elapsed time, %u usec", elapsed_usec));
+
/* XXX be more intelligent here. just make sure skew is
- * within 2 seconds for now. */
- tt_assert(elapsed >= 4 && elapsed <= 6);
+ * within .4 seconds for now. */
+ tt_assert(elapsed_usec >= 600000 && elapsed_usec <= 1400000);
end:
for (i = 0; i < QUEUE_THREAD_COUNT; ++i)
THREAD_JOIN(load_threads[i]);
+ if (base)
+ event_base_free(base);
+ if (cfg)
+ event_config_free(cfg);
+}
+
+static struct event time_events[5];
+static struct timeval times[5];
+static struct event_base *exit_base = NULL;
+static void
+note_time_cb(evutil_socket_t fd, short what, void *arg)
+{
+ evutil_gettimeofday(arg, NULL);
+ if (arg == &times[4]) {
+ event_base_loopbreak(exit_base);
+ }
+}
+static THREAD_FN
+register_events_subthread(void *arg)
+{
+ struct timeval tv = {0,0};
+ SLEEP_MS(100);
+ event_active(&time_events[0], EV_TIMEOUT, 1);
+ SLEEP_MS(100);
+ event_active(&time_events[1], EV_TIMEOUT, 1);
+ SLEEP_MS(100);
+ tv.tv_usec = 100*1000;
+ event_add(&time_events[2], &tv);
+ tv.tv_usec = 150*1000;
+ event_add(&time_events[3], &tv);
+ SLEEP_MS(200);
+ event_active(&time_events[4], EV_TIMEOUT, 1);
+
+ THREAD_RETURN();
+}
+
+static void
+thread_no_events(void *arg)
+{
+ THREAD_T thread;
+ struct basic_test_data *data = arg;
+ struct timeval starttime, endtime;
+ int i;
+ exit_base = data->base;
+
+ memset(times,0,sizeof(times));
+ for (i=0;i<5;++i) {
+ event_assign(&time_events[i], data->base,
+ -1, 0, note_time_cb, &times[i]);
+ }
+
+ evutil_gettimeofday(&starttime, NULL);
+ THREAD_START(thread, register_events_subthread, data->base);
+ event_base_loop(data->base, EVLOOP_NO_EXIT_ON_EMPTY);
+ evutil_gettimeofday(&endtime, NULL);
+ tt_assert(event_base_got_break(data->base));
+ THREAD_JOIN(thread);
+ for (i=0; i<5; ++i) {
+ struct timeval diff;
+ double sec;
+ evutil_timersub(&times[i], &starttime, &diff);
+ sec = diff.tv_sec + diff.tv_usec/1.0e6;
+ TT_BLATHER(("event %d at %.4f seconds", i, sec));
+ }
+ test_timeval_diff_eq(&starttime, &times[0], 100);
+ test_timeval_diff_eq(&starttime, &times[1], 200);
+ test_timeval_diff_eq(&starttime, &times[2], 400);
+ test_timeval_diff_eq(&starttime, &times[3], 450);
+ test_timeval_diff_eq(&starttime, &times[4], 500);
+ test_timeval_diff_eq(&starttime, &endtime, 500);
+
+end:
+ ;
}
#define TEST(name) \
@@ -500,12 +571,15 @@ end:
struct testcase_t thread_testcases[] = {
{ "basic", thread_basic, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE,
&basic_setup, NULL },
-#ifndef WIN32
+#ifndef _WIN32
{ "forking", thread_basic, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE,
&basic_setup, (char*)"forking" },
#endif
TEST(conditions_simple),
- TEST(deferred_cb_skew),
+ { "deferred_cb_skew", thread_deferred_cb_skew,
+ TT_FORK|TT_NEED_THREADS|TT_OFF_BY_DEFAULT,
+ &basic_setup, NULL },
+ TEST(no_events),
END_OF_TESTCASES
};
diff --git a/test/regress_thread.h b/test/regress_thread.h
new file mode 100644
index 00000000..831b51e5
--- /dev/null
+++ b/test/regress_thread.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef REGRESS_THREAD_H_INCLUDED_
+#define REGRESS_THREAD_H_INCLUDED_
+
+#ifdef EVENT__HAVE_PTHREADS
+#define THREAD_T pthread_t
+#define THREAD_FN void *
+#define THREAD_RETURN() return (NULL)
+#define THREAD_START(threadvar, fn, arg) \
+ pthread_create(&(threadvar), NULL, fn, arg)
+#define THREAD_JOIN(th) pthread_join(th, NULL)
+#else
+#define THREAD_T HANDLE
+#define THREAD_FN unsigned __stdcall
+#define THREAD_RETURN() return (0)
+#define THREAD_START(threadvar, fn, arg) do { \
+ uintptr_t threadhandle = _beginthreadex(NULL,0,fn,(arg),0,NULL); \
+ (threadvar) = (HANDLE) threadhandle; \
+ } while (0)
+#define THREAD_JOIN(th) WaitForSingleObject(th, INFINITE)
+#endif
+
+#endif
diff --git a/test/regress_util.c b/test/regress_util.c
index 9805df4e..60f085bf 100644
--- a/test/regress_util.c
+++ b/test/regress_util.c
@@ -23,7 +23,9 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifdef WIN32
+#include "../util-internal.h"
+
+#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
@@ -33,16 +35,16 @@
#include <sys/types.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
-#ifdef _EVENT_HAVE_NETINET_IN6_H
+#ifdef EVENT__HAVE_NETINET_IN6_H
#include <netinet/in6.h>
#endif
-#ifdef _EVENT_HAVE_SYS_WAIT_H
+#ifdef EVENT__HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include <signal.h>
@@ -53,9 +55,10 @@
#include "event2/event.h"
#include "event2/util.h"
#include "../ipv6-internal.h"
-#include "../util-internal.h"
#include "../log-internal.h"
#include "../strlcpy-internal.h"
+#include "../mm-internal.h"
+#include "../time-internal.h"
#include "regress.h"
@@ -176,10 +179,10 @@ regress_ipv6_parse(void *ptr)
for (j = 0; j < 4; ++j) {
/* Can't use s6_addr32 here; some don't have it. */
ev_uint32_t u =
- (in6.s6_addr[j*4 ] << 24) |
- (in6.s6_addr[j*4+1] << 16) |
- (in6.s6_addr[j*4+2] << 8) |
- (in6.s6_addr[j*4+3]);
+ ((ev_uint32_t)in6.s6_addr[j*4 ] << 24) |
+ ((ev_uint32_t)in6.s6_addr[j*4+1] << 16) |
+ ((ev_uint32_t)in6.s6_addr[j*4+2] << 8) |
+ ((ev_uint32_t)in6.s6_addr[j*4+3]);
if (u != ent->res[j]) {
TT_FAIL(("%s did not parse as expected.", ent->addr));
continue;
@@ -245,7 +248,7 @@ regress_sockaddr_port_parse(void *ptr)
if (ent->safamily == AF_INET) {
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
-#ifdef _EVENT_HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
+#ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
sin.sin_len = sizeof(sin);
#endif
sin.sin_family = AF_INET;
@@ -261,7 +264,7 @@ regress_sockaddr_port_parse(void *ptr)
} else {
struct sockaddr_in6 sin6;
memset(&sin6, 0, sizeof(sin6));
-#ifdef _EVENT_HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
+#ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
sin6.sin6_len = sizeof(sin6);
#endif
sin6.sin6_family = AF_INET6;
@@ -292,7 +295,7 @@ regress_sockaddr_port_format(void *ptr)
r = evutil_parse_sockaddr_port("192.168.1.1:80",
(struct sockaddr*)&ss, &len);
tt_int_op(r,==,0);
- cp = evutil_format_sockaddr_port(
+ cp = evutil_format_sockaddr_port_(
(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
tt_ptr_op(cp,==,cbuf);
tt_str_op(cp,==,"192.168.1.1:80");
@@ -301,13 +304,13 @@ regress_sockaddr_port_format(void *ptr)
r = evutil_parse_sockaddr_port("[ff00::8010]:999",
(struct sockaddr*)&ss, &len);
tt_int_op(r,==,0);
- cp = evutil_format_sockaddr_port(
+ cp = evutil_format_sockaddr_port_(
(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
tt_ptr_op(cp,==,cbuf);
tt_str_op(cp,==,"[ff00::8010]:999");
ss.ss_family=99;
- cp = evutil_format_sockaddr_port(
+ cp = evutil_format_sockaddr_port_(
(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
tt_ptr_op(cp,==,cbuf);
tt_str_op(cp,==,"<addr with socktype 99>");
@@ -352,7 +355,7 @@ test_evutil_sockaddr_predicates(void *ptr)
}
/* sockaddr_is_loopback */
- if (ent->is_loopback != evutil_sockaddr_is_loopback((struct sockaddr*)&ss)) {
+ if (ent->is_loopback != evutil_sockaddr_is_loopback_((struct sockaddr*)&ss)) {
TT_FAIL(("evutil_sockaddr_loopback(%s) not as expected",
ent->parse));
}
@@ -431,6 +434,43 @@ end:
;
}
+static void
+test_evutil_rtrim(void *ptr)
+{
+#define TEST_TRIM(s, result) \
+ do { \
+ if (cp) mm_free(cp); \
+ cp = mm_strdup(s); \
+ tt_assert(cp); \
+ evutil_rtrim_lws_(cp); \
+ tt_str_op(cp, ==, result); \
+ } while(0)
+
+ char *cp = NULL;
+ (void) ptr;
+
+ TEST_TRIM("", "");
+ TEST_TRIM("a", "a");
+ TEST_TRIM("abcdef ghi", "abcdef ghi");
+
+ TEST_TRIM(" ", "");
+ TEST_TRIM(" ", "");
+ TEST_TRIM("a ", "a");
+ TEST_TRIM("abcdef gH ", "abcdef gH");
+
+ TEST_TRIM("\t\t", "");
+ TEST_TRIM(" \t", "");
+ TEST_TRIM("\t", "");
+ TEST_TRIM("a \t", "a");
+ TEST_TRIM("a\t ", "a");
+ TEST_TRIM("a\t", "a");
+ TEST_TRIM("abcdef gH \t ", "abcdef gH");
+
+end:
+ if (cp)
+ mm_free(cp);
+}
+
static int logsev = 0;
static char *logmsg = NULL;
@@ -459,7 +499,7 @@ fatalfn(int exitcode)
exit(exitcode);
}
-#ifndef WIN32
+#ifndef _WIN32
#define CAN_CHECK_ERR
static void
check_error_logging(void (*fn)(void), int wantexitcode,
@@ -499,7 +539,7 @@ static void
sock_err_fn(void)
{
evutil_socket_t fd = socket(AF_INET, SOCK_STREAM, 0);
-#ifdef WIN32
+#ifdef _WIN32
EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK);
#else
errno = EAGAIN;
@@ -532,22 +572,22 @@ test_evutil_log(void *ptr)
* module didn't enforce the requirement that a fatal callback
* actually exit. Now, it exits no matter what, so if we wan to
* reinstate these tests, we'll need to fork for each one. */
- check_error_logging(errx_fn, 2, _EVENT_LOG_ERR,
+ check_error_logging(errx_fn, 2, EVENT_LOG_ERR,
"Fatal error; too many kumquats (5)");
RESET();
#endif
event_warnx("Far too many %s (%d)", "wombats", 99);
- LOGEQ(_EVENT_LOG_WARN, "Far too many wombats (99)");
+ LOGEQ(EVENT_LOG_WARN, "Far too many wombats (99)");
RESET();
event_msgx("Connecting lime to coconut");
- LOGEQ(_EVENT_LOG_MSG, "Connecting lime to coconut");
+ LOGEQ(EVENT_LOG_MSG, "Connecting lime to coconut");
RESET();
event_debug(("A millisecond passed! We should log that!"));
#ifdef USE_DEBUG
- LOGEQ(_EVENT_LOG_DEBUG, "A millisecond passed! We should log that!");
+ LOGEQ(EVENT_LOG_DEBUG, "A millisecond passed! We should log that!");
#else
tt_int_op(logsev,==,0);
tt_ptr_op(logmsg,==,NULL);
@@ -559,19 +599,19 @@ test_evutil_log(void *ptr)
event_warn("Couldn't open %s", "/bad/file");
evutil_snprintf(buf, sizeof(buf),
"Couldn't open /bad/file: %s",strerror(ENOENT));
- LOGEQ(_EVENT_LOG_WARN,buf);
+ LOGEQ(EVENT_LOG_WARN,buf);
RESET();
#ifdef CAN_CHECK_ERR
evutil_snprintf(buf, sizeof(buf),
"Couldn't open /very/bad/file: %s",strerror(ENOENT));
- check_error_logging(err_fn, 5, _EVENT_LOG_ERR, buf);
+ check_error_logging(err_fn, 5, EVENT_LOG_ERR, buf);
RESET();
#endif
/* Try with a socket errno. */
fd = socket(AF_INET, SOCK_STREAM, 0);
-#ifdef WIN32
+#ifdef _WIN32
evutil_snprintf(buf, sizeof(buf),
"Unhappy socket: %s",
evutil_socket_error_to_string(WSAEWOULDBLOCK));
@@ -582,11 +622,11 @@ test_evutil_log(void *ptr)
errno = EAGAIN;
#endif
event_sock_warn(fd, "Unhappy socket");
- LOGEQ(_EVENT_LOG_WARN, buf);
+ LOGEQ(EVENT_LOG_WARN, buf);
RESET();
#ifdef CAN_CHECK_ERR
- check_error_logging(sock_err_fn, 20, _EVENT_LOG_ERR, buf);
+ check_error_logging(sock_err_fn, 20, EVENT_LOG_ERR, buf);
RESET();
#endif
@@ -686,46 +726,48 @@ test_evutil_integers(void *arg)
tt_assert(u64 > 0);
tt_assert(i64 > 0);
u64++;
- i64++;
+/* i64++; */
tt_assert(u64 == 0);
- tt_assert(i64 == EV_INT64_MIN);
- tt_assert(i64 < 0);
+/* tt_assert(i64 == EV_INT64_MIN); */
+/* tt_assert(i64 < 0); */
u32 = EV_UINT32_MAX;
i32 = EV_INT32_MAX;
tt_assert(u32 > 0);
tt_assert(i32 > 0);
u32++;
- i32++;
+/* i32++; */
tt_assert(u32 == 0);
- tt_assert(i32 == EV_INT32_MIN);
- tt_assert(i32 < 0);
+/* tt_assert(i32 == EV_INT32_MIN); */
+/* tt_assert(i32 < 0); */
u16 = EV_UINT16_MAX;
i16 = EV_INT16_MAX;
tt_assert(u16 > 0);
tt_assert(i16 > 0);
u16++;
- i16++;
+/* i16++; */
tt_assert(u16 == 0);
- tt_assert(i16 == EV_INT16_MIN);
- tt_assert(i16 < 0);
+/* tt_assert(i16 == EV_INT16_MIN); */
+/* tt_assert(i16 < 0); */
u8 = EV_UINT8_MAX;
i8 = EV_INT8_MAX;
tt_assert(u8 > 0);
tt_assert(i8 > 0);
u8++;
- i8++;
+/* i8++;*/
tt_assert(u8 == 0);
- tt_assert(i8 == EV_INT8_MIN);
- tt_assert(i8 < 0);
+/* tt_assert(i8 == EV_INT8_MIN); */
+/* tt_assert(i8 < 0); */
+/*
ssize = EV_SSIZE_MAX;
tt_assert(ssize > 0);
ssize++;
tt_assert(ssize < 0);
tt_assert(ssize == EV_SSIZE_MIN);
+*/
ptr = &ssize;
iptr = (ev_intptr_t)ptr;
@@ -765,7 +807,7 @@ ai_find_by_protocol(struct evutil_addrinfo *ai, int protocol)
int
-_test_ai_eq(const struct evutil_addrinfo *ai, const char *sockaddr_port,
+test_ai_eq_(const struct evutil_addrinfo *ai, const char *sockaddr_port,
int socktype, int protocol, int line)
{
struct sockaddr_storage ss;
@@ -827,6 +869,7 @@ test_evutil_rand(void *arg)
char buf2[32];
int counts[256];
int i, j, k, n=0;
+ struct evutil_weakrand_state seed = { 12346789U };
memset(buf2, 0, sizeof(buf2));
memset(counts, 0, sizeof(counts));
@@ -834,8 +877,8 @@ test_evutil_rand(void *arg)
for (k=0;k<32;++k) {
/* Try a few different start and end points; try to catch
* the various misaligned cases of arc4random_buf */
- int startpoint = _evutil_weakrand() % 4;
- int endpoint = 32 - (_evutil_weakrand() % 4);
+ int startpoint = evutil_weakrand_(&seed) % 4;
+ int endpoint = 32 - (evutil_weakrand_(&seed) % 4);
memset(buf2, 0, sizeof(buf2));
@@ -866,6 +909,13 @@ test_evutil_rand(void *arg)
}
}
+ evutil_weakrand_seed_(&seed, 0);
+ for (i = 0; i < 10000; ++i) {
+ ev_int32_t r = evutil_weakrand_range_(&seed, 9999);
+ tt_int_op(0, <=, r);
+ tt_int_op(r, <, 9999);
+ }
+
/* for (i=0;i<256;++i) { printf("%3d %2d\n", i, counts[i]); } */
end:
;
@@ -876,11 +926,6 @@ test_evutil_getaddrinfo(void *arg)
{
struct evutil_addrinfo *ai = NULL, *a;
struct evutil_addrinfo hints;
-
- struct sockaddr_in6 *sin6;
- struct sockaddr_in *sin;
- char buf[128];
- const char *cp;
int r;
/* Try using it as a pton. */
@@ -1007,6 +1052,23 @@ test_evutil_getaddrinfo(void *arg)
ai = NULL;
}
+end:
+ if (ai)
+ evutil_freeaddrinfo(ai);
+}
+
+static void
+test_evutil_getaddrinfo_live(void *arg)
+{
+ struct evutil_addrinfo *ai = NULL;
+ struct evutil_addrinfo hints;
+
+ struct sockaddr_in6 *sin6;
+ struct sockaddr_in *sin;
+ char buf[128];
+ const char *cp;
+ int r;
+
/* Now do some actual lookups. */
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_INET;
@@ -1055,13 +1117,13 @@ end:
evutil_freeaddrinfo(ai);
}
-#ifdef WIN32
+#ifdef _WIN32
static void
test_evutil_loadsyslib(void *arg)
{
- HANDLE h=NULL;
+ HMODULE h=NULL;
- h = evutil_load_windows_system_library(TEXT("kernel32.dll"));
+ h = evutil_load_windows_system_library_(TEXT("kernel32.dll"));
tt_assert(h);
end:
@@ -1071,6 +1133,251 @@ end:
}
#endif
+/** Test mm_malloc(). */
+static void
+test_event_malloc(void *arg)
+{
+ void *p = NULL;
+ (void)arg;
+
+ /* mm_malloc(0) should simply return NULL. */
+#ifndef EVENT__DISABLE_MM_REPLACEMENT
+ errno = 0;
+ p = mm_malloc(0);
+ tt_assert(p == NULL);
+ tt_int_op(errno, ==, 0);
+#endif
+
+ /* Trivial case. */
+ errno = 0;
+ p = mm_malloc(8);
+ tt_assert(p != NULL);
+ tt_int_op(errno, ==, 0);
+ mm_free(p);
+
+ end:
+ errno = 0;
+ return;
+}
+
+static void
+test_event_calloc(void *arg)
+{
+ void *p = NULL;
+ (void)arg;
+
+#ifndef EVENT__DISABLE_MM_REPLACEMENT
+ /* mm_calloc() should simply return NULL
+ * if either argument is zero. */
+ errno = 0;
+ p = mm_calloc(0, 0);
+ tt_assert(p == NULL);
+ tt_int_op(errno, ==, 0);
+ errno = 0;
+ p = mm_calloc(0, 1);
+ tt_assert(p == NULL);
+ tt_int_op(errno, ==, 0);
+ errno = 0;
+ p = mm_calloc(1, 0);
+ tt_assert(p == NULL);
+ tt_int_op(errno, ==, 0);
+#endif
+
+ /* Trivial case. */
+ errno = 0;
+ p = mm_calloc(8, 8);
+ tt_assert(p != NULL);
+ tt_int_op(errno, ==, 0);
+ mm_free(p);
+ p = NULL;
+
+ /* mm_calloc() should set errno = ENOMEM and return NULL
+ * in case of potential overflow. */
+ errno = 0;
+ p = mm_calloc(EV_SIZE_MAX/2, EV_SIZE_MAX/2 + 8);
+ tt_assert(p == NULL);
+ tt_int_op(errno, ==, ENOMEM);
+
+ end:
+ errno = 0;
+ if (p)
+ mm_free(p);
+
+ return;
+}
+
+static void
+test_event_strdup(void *arg)
+{
+ void *p = NULL;
+ (void)arg;
+
+#ifndef EVENT__DISABLE_MM_REPLACEMENT
+ /* mm_strdup(NULL) should set errno = EINVAL and return NULL. */
+ errno = 0;
+ p = mm_strdup(NULL);
+ tt_assert(p == NULL);
+ tt_int_op(errno, ==, EINVAL);
+#endif
+
+ /* Trivial cases. */
+
+ errno = 0;
+ p = mm_strdup("");
+ tt_assert(p != NULL);
+ tt_int_op(errno, ==, 0);
+ tt_str_op(p, ==, "");
+ mm_free(p);
+
+ errno = 0;
+ p = mm_strdup("foo");
+ tt_assert(p != NULL);
+ tt_int_op(errno, ==, 0);
+ tt_str_op(p, ==, "foo");
+ mm_free(p);
+
+ /* XXX
+ * mm_strdup(str) where str is a string of length EV_SIZE_MAX
+ * should set errno = ENOMEM and return NULL. */
+
+ end:
+ errno = 0;
+ return;
+}
+
+static void
+test_evutil_usleep(void *arg)
+{
+ struct timeval tv1, tv2, tv3, diff1, diff2;
+ const struct timeval quarter_sec = {0, 250*1000};
+ const struct timeval tenth_sec = {0, 100*1000};
+ long usec1, usec2;
+
+ evutil_gettimeofday(&tv1, NULL);
+ evutil_usleep_(&quarter_sec);
+ evutil_gettimeofday(&tv2, NULL);
+ evutil_usleep_(&tenth_sec);
+ evutil_gettimeofday(&tv3, NULL);
+
+ evutil_timersub(&tv2, &tv1, &diff1);
+ evutil_timersub(&tv3, &tv2, &diff2);
+ usec1 = diff1.tv_sec * 1000000 + diff1.tv_usec;
+ usec2 = diff2.tv_sec * 1000000 + diff2.tv_usec;
+
+ tt_int_op(usec1, >, 200000);
+ tt_int_op(usec1, <, 300000);
+ tt_int_op(usec2, >, 80000);
+ tt_int_op(usec2, <, 120000);
+
+end:
+ ;
+}
+
+static void
+test_evutil_monotonic_res(void *data_)
+{
+ /* Basic santity-test for monotonic timers. What we'd really like
+ * to do is make sure that they can't go backwards even when the
+ * system clock goes backwards. But we haven't got a good way to
+ * move the system clock backwards.
+ */
+ struct basic_test_data *data = data_;
+ struct evutil_monotonic_timer timer;
+ const int precise = strstr(data->setup_data, "precise") != NULL;
+ const int fallback = strstr(data->setup_data, "fallback") != NULL;
+ struct timeval tv[10], delay;
+ int total_diff = 0;
+
+ int flags = 0, wantres, acceptdiff, i;
+ if (precise)
+ flags |= EV_MONOT_PRECISE;
+ if (fallback)
+ flags |= EV_MONOT_FALLBACK;
+ if (precise || fallback) {
+#ifdef _WIN32
+ wantres = 10*1000;
+ acceptdiff = 1000;
+#else
+ wantres = 1000;
+ acceptdiff = 300;
+#endif
+ } else {
+ wantres = 40*1000;
+ acceptdiff = 20*1000;
+ }
+
+ TT_BLATHER(("Precise = %d", precise));
+ TT_BLATHER(("Fallback = %d", fallback));
+
+ /* First, make sure we match up with usleep. */
+
+ delay.tv_sec = 0;
+ delay.tv_usec = wantres;
+
+ tt_int_op(evutil_configure_monotonic_time_(&timer, flags), ==, 0);
+
+ for (i = 0; i < 10; ++i) {
+ evutil_gettime_monotonic_(&timer, &tv[i]);
+ evutil_usleep_(&delay);
+ }
+
+ for (i = 0; i < 9; ++i) {
+ struct timeval diff;
+ tt_assert(evutil_timercmp(&tv[i], &tv[i+1], <));
+ evutil_timersub(&tv[i+1], &tv[i], &diff);
+ tt_int_op(diff.tv_sec, ==, 0);
+ total_diff += diff.tv_usec;
+ TT_BLATHER(("Difference = %d", (int)diff.tv_usec));
+ }
+ tt_int_op(abs(total_diff/9 - wantres), <, acceptdiff);
+
+end:
+ ;
+}
+
+static void
+test_evutil_monotonic_prc(void *data_)
+{
+ struct basic_test_data *data = data_;
+ struct evutil_monotonic_timer timer;
+ const int precise = strstr(data->setup_data, "precise") != NULL;
+ const int fallback = strstr(data->setup_data, "fallback") != NULL;
+ struct timeval tv[10];
+ int total_diff = 0;
+ int i, maxstep = 25*1000,flags=0;
+ if (precise)
+ maxstep = 500;
+ if (precise)
+ flags |= EV_MONOT_PRECISE;
+ if (fallback)
+ flags |= EV_MONOT_FALLBACK;
+ tt_int_op(evutil_configure_monotonic_time_(&timer, flags), ==, 0);
+
+ /* find out what precision we actually see. */
+
+ evutil_gettime_monotonic_(&timer, &tv[0]);
+ for (i = 1; i < 10; ++i) {
+ do {
+ evutil_gettime_monotonic_(&timer, &tv[i]);
+ } while (evutil_timercmp(&tv[i-1], &tv[i], ==));
+ }
+
+ total_diff = 0;
+ for (i = 0; i < 9; ++i) {
+ struct timeval diff;
+ tt_assert(evutil_timercmp(&tv[i], &tv[i+1], <));
+ evutil_timersub(&tv[i+1], &tv[i], &diff);
+ tt_int_op(diff.tv_sec, ==, 0);
+ total_diff += diff.tv_usec;
+ TT_BLATHER(("Step difference = %d", (int)diff.tv_usec));
+ }
+ TT_BLATHER(("Average step difference = %d", total_diff / 9));
+ tt_int_op(total_diff/9, <, maxstep);
+
+end:
+ ;
+}
+
struct testcase_t util_testcases[] = {
{ "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL },
{ "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL },
@@ -1080,15 +1387,27 @@ struct testcase_t util_testcases[] = {
{ "evutil_snprintf", test_evutil_snprintf, 0, NULL, NULL },
{ "evutil_strtoll", test_evutil_strtoll, 0, NULL, NULL },
{ "evutil_casecmp", test_evutil_casecmp, 0, NULL, NULL },
+ { "evutil_rtrim", test_evutil_rtrim, 0, NULL, NULL },
{ "strlcpy", test_evutil_strlcpy, 0, NULL, NULL },
{ "log", test_evutil_log, TT_FORK, NULL, NULL },
{ "upcast", test_evutil_upcast, 0, NULL, NULL },
{ "integers", test_evutil_integers, 0, NULL, NULL },
{ "rand", test_evutil_rand, TT_FORK, NULL, NULL },
{ "getaddrinfo", test_evutil_getaddrinfo, TT_FORK, NULL, NULL },
-#ifdef WIN32
+ { "getaddrinfo_live", test_evutil_getaddrinfo_live, TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL },
+#ifdef _WIN32
{ "loadsyslib", test_evutil_loadsyslib, TT_FORK, NULL, NULL },
#endif
+ { "mm_malloc", test_event_malloc, 0, NULL, NULL },
+ { "mm_calloc", test_event_calloc, 0, NULL, NULL },
+ { "mm_strdup", test_event_strdup, 0, NULL, NULL },
+ { "usleep", test_evutil_usleep, 0, NULL, NULL },
+ { "monotonic_res", test_evutil_monotonic_res, 0, &basic_setup, (void*)"" },
+ { "monotonic_res_precise", test_evutil_monotonic_res, TT_OFF_BY_DEFAULT, &basic_setup, (void*)"precise" },
+ { "monotonic_res_fallback", test_evutil_monotonic_res, TT_OFF_BY_DEFAULT, &basic_setup, (void*)"fallback" },
+ { "monotonic_prc", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"" },
+ { "monotonic_prc_precise", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"precise" },
+ { "monotonic_prc_fallback", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"fallback" },
END_OF_TESTCASES,
};
diff --git a/test/regress_zlib.c b/test/regress_zlib.c
index 641fb5cf..84066769 100644
--- a/test/regress_zlib.c
+++ b/test/regress_zlib.c
@@ -27,7 +27,7 @@
/* The old tests here need assertions to work. */
#undef NDEBUG
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#endif
@@ -35,7 +35,7 @@
#include "event2/event-config.h"
#include <sys/types.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
@@ -56,6 +56,7 @@
#include "event2/bufferevent.h"
#include "regress.h"
+#include "mm-internal.h"
/* zlib 1.2.4 and 1.2.5 do some "clever" things with macros. Instead of
saying "(defined(FOO) ? FOO : 0)" they like to say "FOO-0", on the theory
@@ -95,6 +96,7 @@ zlib_deflate_free(void *ctx)
z_streamp p = ctx;
assert(deflateEnd(p) == Z_OK);
+ mm_free(p);
}
static void
@@ -103,6 +105,7 @@ zlib_inflate_free(void *ctx)
z_streamp p = ctx;
assert(inflateEnd(p) == Z_OK);
+ mm_free(p);
}
static int
@@ -275,7 +278,7 @@ test_bufferevent_zlib(void *arg)
{
struct bufferevent *bev1=NULL, *bev2=NULL;
char buffer[8333];
- z_stream z_input, z_output;
+ z_stream *z_input, *z_output;
int i, r;
evutil_socket_t pair[2] = {-1, -1};
(void)arg;
@@ -293,18 +296,18 @@ test_bufferevent_zlib(void *arg)
bev1 = bufferevent_socket_new(NULL, pair[0], 0);
bev2 = bufferevent_socket_new(NULL, pair[1], 0);
- memset(&z_output, 0, sizeof(z_output));
- r = deflateInit(&z_output, Z_DEFAULT_COMPRESSION);
+ z_output = mm_calloc(sizeof(*z_output), 1);
+ r = deflateInit(z_output, Z_DEFAULT_COMPRESSION);
tt_int_op(r, ==, Z_OK);
- memset(&z_input, 0, sizeof(z_input));
- r = inflateInit(&z_input);
+ z_input = mm_calloc(sizeof(*z_input), 1);
+ r = inflateInit(z_input);
tt_int_op(r, ==, Z_OK);
/* initialize filters */
bev1 = bufferevent_filter_new(bev1, NULL, zlib_output_filter,
- BEV_OPT_CLOSE_ON_FREE, zlib_deflate_free, &z_output);
+ BEV_OPT_CLOSE_ON_FREE, zlib_deflate_free, z_output);
bev2 = bufferevent_filter_new(bev2, zlib_input_filter,
- NULL, BEV_OPT_CLOSE_ON_FREE, zlib_inflate_free, &z_input);
+ NULL, BEV_OPT_CLOSE_ON_FREE, zlib_inflate_free, z_input);
bufferevent_setcb(bev1, readcb, writecb, errorcb, NULL);
bufferevent_setcb(bev2, readcb, writecb, errorcb, NULL);
diff --git a/test/rpcgen_wrapper.sh b/test/rpcgen_wrapper.sh
index d2101b05..aaa03031 100755
--- a/test/rpcgen_wrapper.sh
+++ b/test/rpcgen_wrapper.sh
@@ -12,12 +12,12 @@
# working Python a requirement for make distcheck of a git tree.
exit_updated() {
- echo "Updated ${srcdir}\regress.gen.c and ${srcdir}\regress.gen.h"
+# echo "Updated ${srcdir}/regress.gen.c and ${srcdir}/regress.gen.h"
exit 0
}
exit_reuse() {
- echo "event_rpcgen.py failed, ${srcdir}\regress.gen.\[ch\] will be reused." >&2
+ echo "event_rpcgen.py failed, ${srcdir}/regress.gen.\[ch\] will be reused." >&2
exit 0
}
@@ -36,7 +36,10 @@ fi
srcdir=$1
srcdir=${srcdir:-.}
-${PYTHON2} ${srcdir}/../event_rpcgen.py ${srcdir}/regress.rpc
+
+${PYTHON2} ${srcdir}/../event_rpcgen.py --quiet ${srcdir}/regress.rpc \
+ test/regress.gen.h test/regress.gen.c
+
case "$?" in
0)
exit_updated
diff --git a/test/test-changelist.c b/test/test-changelist.c
index 2d88f346..6e2466d5 100644
--- a/test/test-changelist.c
+++ b/test/test-changelist.c
@@ -26,7 +26,7 @@
#include "event2/event-config.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#else
@@ -34,11 +34,11 @@
#endif
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
-#ifdef _EVENT_HAVE_SYS_SOCKET_H
+#ifdef EVENT__HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <fcntl.h>
@@ -52,7 +52,7 @@
#include <time.h>
struct cpu_usage_timer {
-#ifdef WIN32
+#ifdef _WIN32
HANDLE thread;
FILETIME usertimeBegin;
FILETIME kerneltimeBegin;
@@ -64,7 +64,7 @@ struct cpu_usage_timer {
static void
start_cpu_usage_timer(struct cpu_usage_timer *timer)
{
-#ifdef WIN32
+#ifdef _WIN32
int r;
FILETIME createtime, exittime;
timer->thread = GetCurrentThread();
@@ -77,7 +77,7 @@ start_cpu_usage_timer(struct cpu_usage_timer *timer)
evutil_gettimeofday(&timer->timeBegin, NULL);
}
-#ifdef WIN32
+#ifdef _WIN32
static ev_int64_t
filetime_to_100nsec(const FILETIME *ft)
{
@@ -104,7 +104,7 @@ static void
get_cpu_usage(struct cpu_usage_timer *timer, double *secElapsedOut,
double *secUsedOut, double *usageOut)
{
-#ifdef WIN32
+#ifdef _WIN32
double usertime_seconds, kerneltime_seconds;
FILETIME createtime, exittime, usertimeEnd, kerneltimeEnd;
int r;
@@ -114,7 +114,7 @@ get_cpu_usage(struct cpu_usage_timer *timer, double *secElapsedOut,
struct timeval timeEnd, timeDiff;
double secondsPassed, secondsUsed;
-#ifdef WIN32
+#ifdef _WIN32
r = GetThreadTimes(timer->thread, &createtime, &exittime,
&usertimeEnd, &kerneltimeEnd);
if (r==0) printf("GetThreadTimes failed.");
@@ -171,7 +171,7 @@ main(int argc, char **argv)
double usage, secPassed, secUsed;
-#ifdef WIN32
+#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
@@ -183,15 +183,16 @@ main(int argc, char **argv)
return (1);
/* Initalize the event library */
- base = event_base_new();
+ if (!(base = event_base_new()))
+ return (1);
/* Initalize a timeout to terminate the test */
timeout = evtimer_new(base,timeout_cb,&timeout);
/* and watch for writability on one end of the pipe */
ev = event_new(base,pair[1],EV_WRITE | EV_PERSIST, write_cb, &ev);
- tv.tv_sec = 5;
- tv.tv_usec = 0;
+ tv.tv_sec = 1;
+ tv.tv_usec = 500*1000;
evtimer_add(timeout, &tv);
diff --git a/test/test-closed.c b/test/test-closed.c
new file mode 100644
index 00000000..5b04f354
--- /dev/null
+++ b/test/test-closed.c
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
+ * Copyright (c) 2007-2013 Niels Provos and Nick Mathewson
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "event2/event-config.h"
+
+#ifdef _WIN32
+#include <winsock2.h>
+#else
+#include <unistd.h>
+#endif
+#include <sys/types.h>
+#include <sys/stat.h>
+#ifdef EVENT__HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+#ifdef EVENT__HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include <event.h>
+#include <evutil.h>
+
+#ifdef EVENT____func__
+#define __func__ EVENT____func__
+#endif
+
+struct timeval timeout = {3, 0};
+
+static void
+closed_cb(evutil_socket_t fd, short event, void *arg)
+{
+ if (EV_TIMEOUT & event) {
+ printf("%s: Timeout!\n", __func__);
+ exit(1);
+ }
+
+ if (EV_CLOSED & event) {
+ printf("%s: detected socket close with success\n", __func__);
+ return;
+ }
+
+ printf("%s: unable to detect socket close\n", __func__);
+ exit(1);
+}
+
+#ifndef SHUT_WR
+#define SHUT_WR 1
+#endif
+
+int
+main(int argc, char **argv)
+{
+ struct event_base *base;
+ struct event_config *cfg;
+ struct event *ev;
+ const char *test = "test string";
+ evutil_socket_t pair[2];
+
+ /* Initialize the library and check if the backend
+ supports EV_FEATURE_EARLY_CLOSE
+ */
+ cfg = event_config_new();
+ event_config_require_features(cfg, EV_FEATURE_EARLY_CLOSE);
+ base = event_base_new_with_config(cfg);
+ event_config_free(cfg);
+ if (!base) {
+ /* Backend doesn't support EV_FEATURE_EARLY_CLOSE */
+ return 0;
+ }
+
+ /* Create a pair of sockets */
+ if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
+ return (1);
+
+ /* Send some data on socket 0 and immediately close it */
+ if (send(pair[0], test, (int)strlen(test)+1, 0) < 0)
+ return (1);
+ shutdown(pair[0], SHUT_WR);
+
+ /* Dispatch */
+ ev = event_new(base, pair[1], EV_CLOSED | EV_TIMEOUT, closed_cb, event_self_cbarg());
+ event_add(ev, &timeout);
+ event_base_dispatch(base);
+
+ /* Finalize library */
+ event_base_free(base);
+ return 0;
+}
+
diff --git a/test/test-dumpevents.c b/test/test-dumpevents.c
new file mode 100644
index 00000000..1c272d4c
--- /dev/null
+++ b/test/test-dumpevents.c
@@ -0,0 +1,179 @@
+/*
+ * Copyright (c) 2012 Niels Provos and Nick Mathewson
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "util-internal.h"
+#include "event2/event-config.h"
+
+#ifdef _WIN32
+#include <winsock2.h>
+#include <windows.h>
+#else
+#include <unistd.h>
+#endif
+
+#include <stdio.h>
+#include <event2/event.h>
+#include <signal.h>
+
+static void
+sock_perror(const char *s)
+{
+#ifdef _WIN32
+ const char *err = evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR());
+ fprintf(stderr, "%s: %s\n", s, err);
+#else
+ perror(s);
+#endif
+}
+
+static void
+callback1(evutil_socket_t fd, short events, void *arg)
+{
+}
+static void
+callback2(evutil_socket_t fd, short events, void *arg)
+{
+}
+
+/* Testing code for event_base_dump_events().
+
+ Notes that just because we have code to exercise this function,
+ doesn't mean that *ANYTHING* about the output format is guaranteed to
+ remain in the future.
+ */
+int
+main(int argc, char **argv)
+{
+#define N_EVENTS 13
+ int i;
+ struct event *ev[N_EVENTS];
+ evutil_socket_t pair1[2];
+ evutil_socket_t pair2[2];
+ struct timeval tv_onesec = {1,0};
+ struct timeval tv_two5sec = {2,500*1000};
+ const struct timeval *tv_onesec_common;
+ const struct timeval *tv_two5sec_common;
+ struct event_base *base;
+ struct timeval now;
+
+#ifdef _WIN32
+ WORD wVersionRequested;
+ WSADATA wsaData;
+
+ wVersionRequested = MAKEWORD(2, 2);
+
+ WSAStartup(wVersionRequested, &wsaData);
+#endif
+
+#ifdef _WIN32
+#define LOCAL_SOCKETPAIR_AF AF_INET
+#else
+#define LOCAL_SOCKETPAIR_AF AF_UNIX
+#endif
+
+ if (evutil_make_internal_pipe_(pair1) < 0 ||
+ evutil_make_internal_pipe_(pair2) < 0) {
+ sock_perror("evutil_make_internal_pipe_");
+ return 1;
+ }
+
+ if (!(base = event_base_new())) {
+ fprintf(stderr,"Couldn't make event_base\n");
+ return 2;
+ }
+
+ tv_onesec_common = event_base_init_common_timeout(base, &tv_onesec);
+ tv_two5sec_common = event_base_init_common_timeout(base, &tv_two5sec);
+
+ ev[0] = event_new(base, pair1[0], EV_WRITE, callback1, NULL);
+ ev[1] = event_new(base, pair1[1], EV_READ|EV_PERSIST, callback1, NULL);
+ ev[2] = event_new(base, pair2[0], EV_WRITE|EV_PERSIST, callback2, NULL);
+ ev[3] = event_new(base, pair2[1], EV_READ, callback2, NULL);
+
+ /* For timers */
+ ev[4] = evtimer_new(base, callback1, NULL);
+ ev[5] = evtimer_new(base, callback1, NULL);
+ ev[6] = evtimer_new(base, callback1, NULL);
+ ev[7] = event_new(base, -1, EV_PERSIST, callback2, NULL);
+ ev[8] = event_new(base, -1, EV_PERSIST, callback2, NULL);
+ ev[9] = event_new(base, -1, EV_PERSIST, callback2, NULL);
+
+ /* To activate */
+ ev[10] = event_new(base, -1, 0, callback1, NULL);
+ ev[11] = event_new(base, -1, 0, callback2, NULL);
+
+ /* Signals */
+ ev[12] = evsignal_new(base, SIGINT, callback2, NULL);
+
+ event_add(ev[0], NULL);
+ event_add(ev[1], &tv_onesec);
+ event_add(ev[2], tv_onesec_common);
+ event_add(ev[3], tv_two5sec_common);
+
+ event_add(ev[4], tv_onesec_common);
+ event_add(ev[5], tv_onesec_common);
+ event_add(ev[6], &tv_onesec);
+ event_add(ev[7], tv_two5sec_common);
+ event_add(ev[8], tv_onesec_common);
+ event_add(ev[9], &tv_two5sec);
+
+ event_active(ev[10], EV_READ, 1);
+ event_active(ev[11], EV_READ|EV_WRITE|EV_TIMEOUT, 1);
+ event_active(ev[1], EV_READ, 1);
+
+ event_add(ev[12], NULL);
+
+ evutil_gettimeofday(&now,NULL);
+ puts("=====expected");
+ printf("Now= %ld.%06d\n",(long)now.tv_sec,(int)now.tv_usec);
+ puts("Inserted:");
+ printf(" %p [fd %ld] Write\n",ev[0],(long)pair1[0]);
+ printf(" %p [fd %ld] Read Persist Timeout=T+1\n",ev[1],(long)pair1[1]);
+ printf(" %p [fd %ld] Write Persist Timeout=T+1\n",ev[2],(long)pair2[0]);
+ printf(" %p [fd %ld] Read Timeout=T+2.5\n",ev[3],(long)pair2[1]);
+ printf(" %p [fd -1] Timeout=T+1\n",ev[4]);
+ printf(" %p [fd -1] Timeout=T+1\n",ev[5]);
+ printf(" %p [fd -1] Timeout=T+1\n",ev[6]);
+ printf(" %p [fd -1] Persist Timeout=T+2.5\n",ev[7]);
+ printf(" %p [fd -1] Persist Timeout=T+1\n",ev[8]);
+ printf(" %p [fd -1] Persist Timeout=T+2.5\n",ev[9]);
+ printf(" %p [sig %d] Signal Persist\n", ev[12], (int)SIGINT);
+
+ puts("Active:");
+ printf(" %p [fd -1, priority=0] Read active\n", ev[10]);
+ printf(" %p [fd -1, priority=0] Read Write Timeout active\n", ev[11]);
+ printf(" %p [fd %ld, priority=0] Read active\n", ev[1], (long)pair1[1]);
+
+ puts("======received");
+ event_base_dump_events(base, stdout);
+
+ for (i = 0; i < N_EVENTS; ++i) {
+ event_free(ev[i]);
+ }
+ event_base_free(base);
+
+ return 0;
+}
+
diff --git a/test/test-eof.c b/test/test-eof.c
index 741a2d81..a9ca5343 100644
--- a/test/test-eof.c
+++ b/test/test-eof.c
@@ -26,17 +26,17 @@
*/
#include "event2/event-config.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#else
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
-#ifdef _EVENT_HAVE_SYS_SOCKET_H
+#ifdef EVENT__HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <fcntl.h>
@@ -48,8 +48,8 @@
#include <event.h>
#include <evutil.h>
-#ifdef _EVENT___func__
-#define __func__ _EVENT___func__
+#ifdef EVENT____func__
+#define __func__ EVENT____func__
#endif
int test_okay = 1;
@@ -92,7 +92,7 @@ main(int argc, char **argv)
const char *test = "test string";
evutil_socket_t pair[2];
-#ifdef WIN32
+#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
diff --git a/test/test-fdleak.c b/test/test-fdleak.c
new file mode 100644
index 00000000..4c4eba25
--- /dev/null
+++ b/test/test-fdleak.c
@@ -0,0 +1,249 @@
+/*
+ * Copyright (c) 2012 Ross Lagerwall <rosslagerwall@gmail.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "event2/event-config.h"
+
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#endif
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#ifdef EVENT__HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+#ifdef EVENT__HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>
+#endif
+#ifdef EVENT__HAVE_NETINET_IN_H
+#include <netinet/in.h>
+#endif
+
+#include "event2/event.h"
+#include "event2/bufferevent.h"
+#include "event2/buffer.h"
+#include "event2/listener.h"
+
+/* Number of requests to make. Setting this too high might result in the machine
+ running out of ephemeral ports */
+#ifdef _WIN32
+#define MAX_REQUESTS 1000
+#else
+#define MAX_REQUESTS 4000
+#endif
+
+/* Provide storage for the address, both for the server & the clients */
+static struct sockaddr_in saddr;
+
+/* Number of sucessful requests so far */
+static int num_requests;
+
+static void start_client(struct event_base *base);
+
+static void
+my_perror(const char *s)
+{
+ fprintf(stderr, "%s: %s",
+ s, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR()));
+}
+
+/*
+===============================================
+Server functions
+===============================================
+*/
+
+/* Read a byte from the client and write it back */
+static void
+server_read_cb(struct bufferevent *bev, void *ctx)
+{
+ while (evbuffer_get_length(bufferevent_get_input(bev))) {
+ unsigned char tmp;
+ bufferevent_read(bev, &tmp, 1);
+ bufferevent_write(bev, &tmp, 1);
+ }
+}
+
+/* Wait for an EOF and then free the bufferevent */
+static void
+server_event_cb(struct bufferevent *bev, short events, void *ctx)
+{
+ if (events & BEV_EVENT_ERROR) {
+ my_perror("Error from bufferevent");
+ exit(1);
+ } else if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
+ bufferevent_free(bev);
+ }
+}
+
+/* Accept a client socket and set it up to for reading & writing */
+static void
+listener_accept_cb(struct evconnlistener *listener, evutil_socket_t sock,
+ struct sockaddr *addr, int len, void *ptr)
+{
+ struct event_base *base = evconnlistener_get_base(listener);
+ struct bufferevent *bev = bufferevent_socket_new(base, sock,
+ BEV_OPT_CLOSE_ON_FREE);
+
+ bufferevent_setcb(bev, server_read_cb, NULL, server_event_cb, NULL);
+ bufferevent_enable(bev, EV_READ|EV_WRITE);
+}
+
+/* Start the server listening on a random port and start the first client. */
+static void
+start_loop(void)
+{
+ struct event_base *base;
+ struct evconnlistener *listener;
+ struct sockaddr_storage ss;
+ ev_socklen_t socklen = sizeof(ss);
+ evutil_socket_t fd;
+
+ base = event_base_new();
+ if (base == NULL) {
+ puts("Could not open event base!");
+ exit(1);
+ }
+
+ listener = evconnlistener_new_bind(base, listener_accept_cb, NULL,
+ LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
+ -1, (struct sockaddr *)&saddr, sizeof(saddr));
+ if (listener == NULL) {
+ my_perror("Could not create listener!");
+ exit(1);
+ }
+ fd = evconnlistener_get_fd(listener);
+ if (fd < 0) {
+ puts("Couldn't get fd from listener");
+ exit(1);
+ }
+ if (getsockname(fd, (struct sockaddr *)&ss, &socklen) < 0) {
+ my_perror("getsockname()");
+ exit(1);
+ }
+ memcpy(&saddr, &ss, sizeof(saddr));
+ if (saddr.sin_family != AF_INET) {
+ puts("AF mismatch from getsockname().");
+ exit(1);
+ }
+
+ start_client(base);
+
+ event_base_dispatch(base);
+}
+
+/*
+===============================================
+Client functions
+===============================================
+*/
+
+/* Check that the server sends back the same byte that the client sent.
+ If MAX_REQUESTS have been reached, exit. Otherwise, start another client. */
+static void
+client_read_cb(struct bufferevent *bev, void *ctx)
+{
+ unsigned char tmp;
+ struct event_base *base = bufferevent_get_base(bev);
+
+ bufferevent_read(bev, &tmp, 1);
+ if (tmp != 'A') {
+ puts("Incorrect data received!");
+ exit(2);
+ }
+ bufferevent_free(bev);
+
+ num_requests++;
+ if (num_requests == MAX_REQUESTS) {
+ event_base_loopbreak(base);
+ } else {
+ start_client(base);
+ }
+}
+
+/* Send a byte to the server. */
+static void
+client_event_cb(struct bufferevent *bev, short events, void *ctx)
+{
+ if (events & BEV_EVENT_CONNECTED) {
+ unsigned char tmp = 'A';
+ bufferevent_write(bev, &tmp, 1);
+ } else if (events & BEV_EVENT_ERROR) {
+ puts("Client socket got error!");
+ exit(2);
+ }
+
+ bufferevent_enable(bev, EV_READ);
+}
+
+/* Open a client socket to connect to localhost on sin */
+static void
+start_client(struct event_base *base)
+{
+ struct bufferevent *bev = bufferevent_socket_new(base, -1,
+ BEV_OPT_CLOSE_ON_FREE);
+ bufferevent_setcb(bev, client_read_cb, NULL, client_event_cb, NULL);
+
+ if (bufferevent_socket_connect(bev, (struct sockaddr *)&saddr,
+ sizeof(saddr)) < 0) {
+ my_perror("Could not connect!");
+ bufferevent_free(bev);
+ exit(2);
+ }
+}
+
+int
+main(int argc, char **argv)
+{
+#ifdef EVENT__HAVE_SETRLIMIT
+ /* Set the fd limit to a low value so that any fd leak is caught without
+ making many requests. */
+ struct rlimit rl;
+ rl.rlim_cur = rl.rlim_max = 20;
+ if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
+ my_perror("setrlimit");
+ exit(3);
+ }
+#endif
+
+#ifdef _WIN32
+ WSADATA WSAData;
+ WSAStartup(0x101, &WSAData);
+#endif
+
+ /* Set up an address, used by both client & server. */
+ memset(&saddr, 0, sizeof(saddr));
+ saddr.sin_family = AF_INET;
+ saddr.sin_addr.s_addr = htonl(0x7f000001);
+ saddr.sin_port = 0; /* Tell the implementation to pick a port. */
+
+ start_loop();
+
+ return 0;
+}
+
+/* XXX why does this test cause so much latency sometimes (OSX 10.5)? */
diff --git a/test/test-init.c b/test/test-init.c
index fc18f4d1..92fbc6b1 100644
--- a/test/test-init.c
+++ b/test/test-init.c
@@ -28,17 +28,17 @@
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
-#ifdef _EVENT_HAVE_SYS_SOCKET_H
+#ifdef EVENT__HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <unistd.h>
#endif
#include <errno.h>
@@ -48,7 +48,7 @@
int
main(int argc, char **argv)
{
-#ifdef WIN32
+#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
diff --git a/test/test-ratelim.c b/test/test-ratelim.c
index 815657ec..17babfdc 100644
--- a/test/test-ratelim.c
+++ b/test/test-ratelim.c
@@ -23,6 +23,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "../util-internal.h"
#include <stdio.h>
#include <stdlib.h>
@@ -30,7 +31,7 @@
#include <assert.h>
#include <math.h>
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
@@ -49,7 +50,7 @@
#include "event2/listener.h"
#include "event2/thread.h"
-#include "../util-internal.h"
+static struct evutil_weakrand_state weakrand_state;
static int cfg_verbose = 0;
static int cfg_help = 0;
@@ -60,6 +61,7 @@ static int cfg_connlimit = 0;
static int cfg_grouplimit = 0;
static int cfg_tick_msec = 1000;
static int cfg_min_share = -1;
+static int cfg_group_drain = 0;
static int cfg_connlimit_tolerance = -1;
static int cfg_grouplimit_tolerance = -1;
@@ -79,21 +81,40 @@ static double seconds_per_tick = 0.0;
struct client_state {
size_t queued;
ev_uint64_t received;
+
};
+static const struct timeval *ms100_common=NULL;
+
+/* info from check_bucket_levels_cb */
+static int total_n_bev_checks = 0;
+static ev_int64_t total_rbucket_level=0;
+static ev_int64_t total_wbucket_level=0;
+static ev_int64_t total_max_to_read=0;
+static ev_int64_t total_max_to_write=0;
+static ev_int64_t max_bucket_level=EV_INT64_MIN;
+static ev_int64_t min_bucket_level=EV_INT64_MAX;
+
+/* from check_group_bucket_levels_cb */
+static int total_n_group_bev_checks = 0;
+static ev_int64_t total_group_rbucket_level = 0;
+static ev_int64_t total_group_wbucket_level = 0;
static int n_echo_conns_open = 0;
+/* Info on the open connections */
+struct bufferevent **bevs;
+struct client_state *states;
+struct bufferevent_rate_limit_group *group = NULL;
+
+static void check_bucket_levels_cb(evutil_socket_t fd, short events, void *arg);
+
static void
loud_writecb(struct bufferevent *bev, void *ctx)
{
struct client_state *cs = ctx;
struct evbuffer *output = bufferevent_get_output(bev);
char buf[1024];
-#ifdef WIN32
- int r = rand() % 256;
-#else
- int r = random() % 256;
-#endif
+ int r = evutil_weakrand_(&weakrand_state);
memset(buf, r, sizeof(buf));
while (evbuffer_get_length(output) < 8192) {
evbuffer_add(output, buf, sizeof(buf));
@@ -159,14 +180,70 @@ echo_listenercb(struct evconnlistener *listener, evutil_socket_t newsock,
bev = bufferevent_socket_new(base, newsock, flags);
bufferevent_setcb(bev, echo_readcb, echo_writecb, echo_eventcb, NULL);
- if (conn_bucket_cfg)
+ if (conn_bucket_cfg) {
+ struct event *check_event =
+ event_new(base, -1, EV_PERSIST, check_bucket_levels_cb, bev);
bufferevent_set_rate_limit(bev, conn_bucket_cfg);
+
+ assert(bufferevent_get_token_bucket_cfg(bev) != NULL);
+ event_add(check_event, ms100_common);
+ }
if (ratelim_group)
bufferevent_add_to_rate_limit_group(bev, ratelim_group);
++n_echo_conns_open;
bufferevent_enable(bev, EV_READ|EV_WRITE);
}
+/* Called periodically to check up on how full the buckets are */
+static void
+check_bucket_levels_cb(evutil_socket_t fd, short events, void *arg)
+{
+ struct bufferevent *bev = arg;
+
+ ev_ssize_t r = bufferevent_get_read_limit(bev);
+ ev_ssize_t w = bufferevent_get_write_limit(bev);
+ ev_ssize_t rm = bufferevent_get_max_to_read(bev);
+ ev_ssize_t wm = bufferevent_get_max_to_write(bev);
+ /* XXXX check that no value is above the cofigured burst
+ * limit */
+ total_rbucket_level += r;
+ total_wbucket_level += w;
+ total_max_to_read += rm;
+ total_max_to_write += wm;
+#define B(x) \
+ if ((x) > max_bucket_level) \
+ max_bucket_level = (x); \
+ if ((x) < min_bucket_level) \
+ min_bucket_level = (x)
+ B(r);
+ B(w);
+#undef B
+
+ total_n_bev_checks++;
+ if (total_n_bev_checks >= .8 * ((double)cfg_duration / cfg_tick_msec) * cfg_n_connections) {
+ event_free(event_base_get_running_event(bufferevent_get_base(bev)));
+ }
+}
+
+static void
+check_group_bucket_levels_cb(evutil_socket_t fd, short events, void *arg)
+{
+ if (ratelim_group) {
+ ev_ssize_t r = bufferevent_rate_limit_group_get_read_limit(ratelim_group);
+ ev_ssize_t w = bufferevent_rate_limit_group_get_write_limit(ratelim_group);
+ total_group_rbucket_level += r;
+ total_group_wbucket_level += w;
+ }
+ ++total_n_group_bev_checks;
+}
+
+static void
+group_drain_cb(evutil_socket_t fd, short events, void *arg)
+{
+ bufferevent_rate_limit_group_decrement_read(ratelim_group, cfg_group_drain);
+ bufferevent_rate_limit_group_decrement_write(ratelim_group, cfg_group_drain);
+}
+
static int
test_ratelimiting(void)
{
@@ -177,10 +254,6 @@ test_ratelimiting(void)
struct sockaddr_storage ss;
ev_socklen_t slen;
- struct bufferevent **bevs;
- struct client_state *states;
- struct bufferevent_rate_limit_group *group = NULL;
-
int i;
struct timeval tv;
@@ -191,6 +264,8 @@ test_ratelimiting(void)
double expected_total_persec = -1.0, expected_avg_persec = -1.0;
int ok = 1;
struct event_config *base_cfg;
+ struct event *periodic_level_check;
+ struct event *group_drain_event=NULL;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
@@ -211,10 +286,18 @@ test_ratelimiting(void)
base = event_base_new_with_config(base_cfg);
event_config_free(base_cfg);
+ if (! base) {
+ fprintf(stderr, "Couldn't create event_base");
+ return 1;
+ }
listener = evconnlistener_new_bind(base, echo_listenercb, base,
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1,
(struct sockaddr *)&sin, sizeof(sin));
+ if (! listener) {
+ fprintf(stderr, "Couldn't create listener");
+ return 1;
+ }
slen = sizeof(ss);
if (getsockname(evconnlistener_get_fd(listener), (struct sockaddr *)&ss,
@@ -238,7 +321,7 @@ test_ratelimiting(void)
&cfg_tick);
group = ratelim_group = bufferevent_rate_limit_group_new(
base, group_bucket_cfg);
- expected_total_persec = cfg_grouplimit;
+ expected_total_persec = cfg_grouplimit - (cfg_group_drain / seconds_per_tick);
expected_avg_persec = cfg_grouplimit / cfg_n_connections;
if (cfg_connlimit > 0 && expected_avg_persec > cfg_connlimit)
expected_avg_persec = cfg_connlimit;
@@ -274,9 +357,24 @@ test_ratelimiting(void)
event_base_loopexit(base, &tv);
+ tv.tv_sec = 0;
+ tv.tv_usec = 100*1000;
+ ms100_common = event_base_init_common_timeout(base, &tv);
+
+ periodic_level_check = event_new(base, -1, EV_PERSIST, check_group_bucket_levels_cb, NULL);
+ event_add(periodic_level_check, ms100_common);
+
+ if (cfg_group_drain && ratelim_group) {
+ group_drain_event = event_new(base, -1, EV_PERSIST, group_drain_cb, NULL);
+ event_add(group_drain_event, &cfg_tick);
+ }
+
event_base_dispatch(base);
ratelim_group = NULL; /* So no more responders get added */
+ event_free(periodic_level_check);
+ if (group_drain_event)
+ event_del(group_drain_event);
for (i = 0; i < cfg_n_connections; ++i) {
bufferevent_free(bevs[i]);
@@ -298,6 +396,27 @@ test_ratelimiting(void)
if (group)
bufferevent_rate_limit_group_free(group);
+ if (total_n_bev_checks) {
+ printf("Average read bucket level: %f\n",
+ (double)total_rbucket_level/total_n_bev_checks);
+ printf("Average write bucket level: %f\n",
+ (double)total_wbucket_level/total_n_bev_checks);
+ printf("Highest read bucket level: %f\n",
+ (double)max_bucket_level);
+ printf("Highest write bucket level: %f\n",
+ (double)min_bucket_level);
+ printf("Average max-to-read: %f\n",
+ ((double)total_max_to_read)/total_n_bev_checks);
+ printf("Average max-to-write: %f\n",
+ ((double)total_max_to_write)/total_n_bev_checks);
+ }
+ if (total_n_group_bev_checks) {
+ printf("Average group read bucket level: %f\n",
+ ((double)total_group_rbucket_level)/total_n_group_bev_checks);
+ printf("Average group write bucket level: %f\n",
+ ((double)total_group_wbucket_level)/total_n_group_bev_checks);
+ }
+
total_received = 0;
total_persec = 0.0;
total_sq_persec = 0.0;
@@ -359,6 +478,7 @@ static struct option {
{ "-d", &cfg_duration, 1, 0 },
{ "-c", &cfg_connlimit, 0, 0 },
{ "-g", &cfg_grouplimit, 0, 0 },
+ { "-G", &cfg_group_drain, -100000, 0 },
{ "-t", &cfg_tick_msec, 10, 0 },
{ "--min-share", &cfg_min_share, 0, 0 },
{ "--check-connlimit", &cfg_connlimit_tolerance, 0, 0 },
@@ -413,6 +533,7 @@ usage(void)
" (default: None.)\n"
" -g INT: Group-rate limit applied to sum of all usage in bytes per second\n"
" (default: None.)\n"
+" -G INT: drain INT bytes from the group limit every tick. (default: 0)\n"
" -t INT: Granularity of timing, in milliseconds (default: 1000 msec)\n");
}
@@ -422,14 +543,16 @@ main(int argc, char **argv)
int i,j;
double ratio;
-#ifdef WIN32
+#ifdef _WIN32
WORD wVersionRequested = MAKEWORD(2,2);
WSADATA wsaData;
(void) WSAStartup(wVersionRequested, &wsaData);
#endif
-#ifndef WIN32
+ evutil_weakrand_seed_(&weakrand_state, 0);
+
+#ifndef _WIN32
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
return 1;
#endif
@@ -463,15 +586,15 @@ main(int argc, char **argv)
{
struct timeval tv;
evutil_gettimeofday(&tv, NULL);
-#ifdef WIN32
+#ifdef _WIN32
srand(tv.tv_usec);
#else
srandom(tv.tv_usec);
#endif
}
-#ifndef _EVENT_DISABLE_THREAD_SUPPORT
- evthread_enable_lock_debuging();
+#ifndef EVENT__DISABLE_THREAD_SUPPORT
+ evthread_enable_lock_debugging();
#endif
return test_ratelimiting();
diff --git a/test/test-ratelim.sh b/test/test-ratelim.sh
index 229cba54..b5e0ca62 100755
--- a/test/test-ratelim.sh
+++ b/test/test-ratelim.sh
@@ -66,6 +66,17 @@ run_tests () {
announce FAILED ;
FAILED=yes
fi
+
+ announce_n " Connection limit and group limit with independent drain:"
+ if $TEST_DIR/test-ratelim -c 1000 -g 35000 -n 30 -t 100 -G 500 --check-grouplimit 1000 --check-connlimit 50 --check-stddev 50 >>"$TEST_OUTPUT_FILE"
+ then
+ announce OKAY ;
+ else
+ announce FAILED ;
+ FAILED=yes
+ fi
+
+
}
announce "Running rate-limiting tests:"
diff --git a/test/test-time.c b/test/test-time.c
index ca47205d..fd4de7b6 100644
--- a/test/test-time.c
+++ b/test/test-time.c
@@ -32,7 +32,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#ifndef WIN32
+#ifndef _WIN32
#include <unistd.h>
#include <sys/time.h>
#endif
@@ -41,6 +41,7 @@
#include "event2/event.h"
#include "event2/event_compat.h"
#include "event2/event_struct.h"
+#include "util-internal.h"
int called = 0;
@@ -48,14 +49,12 @@ int called = 0;
struct event *ev[NEVENT];
+struct evutil_weakrand_state weakrand_state;
+
static int
rand_int(int n)
{
-#ifdef WIN32
- return (int)(rand() % n);
-#else
- return (int)(random() % n);
-#endif
+ return evutil_weakrand_(&weakrand_state) % n;
}
static void
@@ -84,7 +83,7 @@ main(int argc, char **argv)
{
struct timeval tv;
int i;
-#ifdef WIN32
+#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
@@ -93,6 +92,8 @@ main(int argc, char **argv)
(void) WSAStartup(wVersionRequested, &wsaData);
#endif
+ evutil_weakrand_seed_(&weakrand_state, 0);
+
/* Initalize the event library */
event_init();
diff --git a/test/test-weof.c b/test/test-weof.c
index 1ac89eda..c379f287 100644
--- a/test/test-weof.c
+++ b/test/test-weof.c
@@ -26,17 +26,17 @@
*/
#include "event2/event-config.h"
-#ifdef WIN32
+#ifdef _WIN32
#include <winsock2.h>
#else
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
-#ifdef _EVENT_HAVE_SYS_SOCKET_H
+#ifdef EVENT__HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <fcntl.h>
@@ -51,8 +51,8 @@
#include "event2/event_compat.h"
#include "event2/util.h"
-#ifdef _EVENT___func__
-#define __func__ _EVENT___func__
+#ifdef EVENT____func__
+#define __func__ EVENT____func__
#endif
evutil_socket_t pair[2];
@@ -85,7 +85,7 @@ main(int argc, char **argv)
{
struct event ev;
-#ifdef WIN32
+#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
@@ -94,7 +94,7 @@ main(int argc, char **argv)
(void) WSAStartup(wVersionRequested, &wsaData);
#endif
-#ifndef WIN32
+#ifndef _WIN32
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
return (1);
#endif
diff --git a/test/test.sh b/test/test.sh
index 08a4cddb..2b083ac7 100755
--- a/test/test.sh
+++ b/test/test.sh
@@ -1,11 +1,10 @@
#!/bin/sh
+BACKENDS="EVPORT KQUEUE EPOLL DEVPOLL POLL SELECT WIN32"
+TESTS="test-eof test-closed test-weof test-time test-changelist test-fdleak"
FAILED=no
-
-if test "x$TEST_OUTPUT_FILE" = "x"
-then
- TEST_OUTPUT_FILE=/dev/null
-fi
+TEST_OUTPUT_FILE=${TEST_OUTPUT_FILE:-/dev/null}
+REGRESS_ARGS=${REGRESS_ARGS:-}
# /bin/echo is a little more likely to support -n than sh's builtin echo,
# printf is even more likely
@@ -27,22 +26,30 @@ then
fi
TEST_DIR=.
+TEST_SRC_DIR=.
-T=`echo "$0" | sed -e 's/test.sh$//'`
+T=`echo "$0" | sed -e 's/test.sh$//' | sed -e 's/test-script.sh//' `
if test -x "$T/test-init"
then
TEST_DIR="$T"
+elif test -x "./test/test-init"
+then
+ TEST_DIR="./test"
+fi
+if test -f "$T/check-dumpevents.py"
+then
+ TEST_SRC_DIR="$T"
+elif test -f "./test/check-dumpevents.py"
+then
+ TEST_SRC_DIR="./test"
fi
setup () {
- EVENT_NOKQUEUE=yes; export EVENT_NOKQUEUE
- EVENT_NODEVPOLL=yes; export EVENT_NODEVPOLL
- EVENT_NOPOLL=yes; export EVENT_NOPOLL
- EVENT_NOSELECT=yes; export EVENT_NOSELECT
- EVENT_NOEPOLL=yes; export EVENT_NOEPOLL
+ for i in $BACKENDS; do
+ eval "EVENT_NO$i=yes; export EVENT_NO$i"
+ done
unset EVENT_EPOLL_USE_CHANGELIST
- EVENT_NOEVPORT=yes; export EVENT_NOEVPORT
- EVENT_NOWIN32=yes; export EVENT_NOWIN32
+ unset EVENT_PRECISE_TIMER
}
announce () {
@@ -64,46 +71,39 @@ run_tests () {
announce Skipping test
return
fi
-
- announce_n " test-eof: "
- if $TEST_DIR/test-eof >>"$TEST_OUTPUT_FILE" ;
- then
- announce OKAY ;
- else
- announce FAILED ;
- FAILED=yes
- fi
- announce_n " test-weof: "
- if $TEST_DIR/test-weof >>"$TEST_OUTPUT_FILE" ;
- then
- announce OKAY ;
+ for i in $TESTS; do
+ announce_n " $i: "
+ if $TEST_DIR/$i >>"$TEST_OUTPUT_FILE" ;
+ then
+ announce OKAY ;
+ else
+ announce FAILED ;
+ FAILED=yes
+ fi
+ done
+ announce_n " test-dumpevents: "
+ if python2 -c 'import sys; assert(sys.version_info >= (2, 4))' 2>/dev/null && test -f $TEST_SRC_DIR/check-dumpevents.py; then
+ if $TEST_DIR/test-dumpevents | python2 $TEST_SRC_DIR/check-dumpevents.py >> "$TEST_OUTPUT_FILE" ;
+ then
+ announce OKAY ;
+ else
+ announce FAILED ;
+ fi
else
- announce FAILED ;
- FAILED=yes
- fi
- announce_n " test-time: "
- if $TEST_DIR/test-time >>"$TEST_OUTPUT_FILE" ;
- then
- announce OKAY ;
- else
- announce FAILED ;
- FAILED=yes
- fi
- announce_n " test-changelist: "
- if $TEST_DIR/test-changelist >>"$TEST_OUTPUT_FILE" ;
- then
- announce OKAY ;
- else
- announce FAILED ;
- FAILED=yes
+ # no python
+ if $TEST_DIR/test-dumpevents >/dev/null; then
+ announce "OKAY (output not checked)" ;
+ else
+ announce "FAILED (output not checked)" ;
+ fi
fi
test -x $TEST_DIR/regress || return
announce_n " regress: "
if test "$TEST_OUTPUT_FILE" = "/dev/null" ;
then
- $TEST_DIR/regress --quiet
+ $TEST_DIR/regress --quiet $REGRESS_ARGS
else
- $TEST_DIR/regress >>"$TEST_OUTPUT_FILE"
+ $TEST_DIR/regress $REGRESS_ARGS >>"$TEST_OUTPUT_FILE"
fi
if test "$?" = "0" ;
then
@@ -114,49 +114,30 @@ run_tests () {
fi
}
-announce "Running tests:"
-
-# Need to do this by hand?
-setup
-unset EVENT_NOEVPORT
-announce "EVPORT"
-run_tests
-
-setup
-unset EVENT_NOKQUEUE
-announce "KQUEUE"
-run_tests
-
-setup
-unset EVENT_NOEPOLL
-announce "EPOLL"
-run_tests
-
-setup
-unset EVENT_NOEPOLL
-EVENT_EPOLL_USE_CHANGELIST=yes; export EVENT_EPOLL_USE_CHANGELIST
-announce "EPOLL (changelist)"
-run_tests
-
-setup
-unset EVENT_NODEVPOLL
-announce "DEVPOLL"
-run_tests
-
-setup
-unset EVENT_NOPOLL
-announce "POLL"
-run_tests
+do_test() {
+ setup
+ announce "$1 $2"
+ unset EVENT_NO$1
+ if test "$2" = "(changelist)" ; then
+ EVENT_EPOLL_USE_CHANGELIST=yes; export EVENT_EPOLL_USE_CHANGELIST
+ elif test "$2" = "(timerfd)" ; then
+ EVENT_PRECISE_TIMER=1; export EVENT_PRECISE_TIMER
+ elif test "$2" = "(timerfd+changelist)" ; then
+ EVENT_EPOLL_USE_CHANGELIST=yes; export EVENT_EPOLL_USE_CHANGELIST
+ EVENT_PRECISE_TIMER=1; export EVENT_PRECISE_TIMER
+ fi
+
+ run_tests
+}
-setup
-unset EVENT_NOSELECT
-announce "SELECT"
-run_tests
+announce "Running tests:"
-setup
-unset EVENT_NOWIN32
-announce "WIN32"
-run_tests
+do_test EPOLL "(timerfd)"
+do_test EPOLL "(changelist)"
+do_test EPOLL "(timerfd+changelist)"
+for i in $BACKENDS; do
+ do_test $i
+done
if test "$FAILED" = "yes"; then
exit 1
diff --git a/test/tinytest.c b/test/tinytest.c
index 065bd623..3a8e3310 100644
--- a/test/tinytest.c
+++ b/test/tinytest.c
@@ -22,17 +22,18 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#ifdef TINYTEST_LOCAL
+#include "tinytest_local.h"
+#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
-#ifdef TINYTEST_LOCAL
-#include "tinytest_local.h"
-#endif
+#ifndef NO_FORKING
-#ifdef WIN32
+#ifdef _WIN32
#include <windows.h>
#else
#include <sys/types.h>
@@ -49,6 +50,8 @@
#endif
#endif
+#endif /* !NO_FORKING */
+
#ifndef __GNUC__
#define __attribute__(x)
#endif
@@ -68,22 +71,25 @@ static int opt_nofork = 0; /**< Suppress calls to fork() for debugging. */
static int opt_verbosity = 1; /**< -==quiet,0==terse,1==normal,2==verbose */
const char *verbosity_flag = "";
+const struct testlist_alias_t *cfg_aliases=NULL;
+
enum outcome { SKIP=2, OK=1, FAIL=0 };
static enum outcome cur_test_outcome = 0;
const char *cur_test_prefix = NULL; /**< prefix of the current test group */
/** Name of the current test, if we haven't logged is yet. Used for --quiet */
const char *cur_test_name = NULL;
-#ifdef WIN32
+#ifdef _WIN32
/* Copy of argv[0] for win32. */
static char commandname[MAX_PATH+1];
#endif
static void usage(struct testgroup_t *groups, int list_groups)
__attribute__((noreturn));
+static int process_test_option(struct testgroup_t *groups, const char *test);
static enum outcome
-_testcase_run_bare(const struct testcase_t *testcase)
+testcase_run_bare_(const struct testcase_t *testcase)
{
void *env = NULL;
int outcome;
@@ -109,11 +115,13 @@ _testcase_run_bare(const struct testcase_t *testcase)
#define MAGIC_EXITCODE 42
+#ifndef NO_FORKING
+
static enum outcome
-_testcase_run_forked(const struct testgroup_t *group,
+testcase_run_forked_(const struct testgroup_t *group,
const struct testcase_t *testcase)
{
-#ifdef WIN32
+#ifdef _WIN32
/* Fork? On Win32? How primitive! We'll do what the smart kids do:
we'll invoke our own exe (whose name we recall from the command
line) with a command line that tells it to run just the test we
@@ -129,7 +137,7 @@ _testcase_run_forked(const struct testgroup_t *group,
DWORD exitcode;
if (!in_tinytest_main) {
- printf("\nERROR. On Windows, _testcase_run_forked must be"
+ printf("\nERROR. On Windows, testcase_run_forked_ must be"
" called from within tinytest_main.\n");
abort();
}
@@ -178,7 +186,7 @@ _testcase_run_forked(const struct testgroup_t *group,
int test_r, write_r;
char b[1];
close(outcome_pipe[0]);
- test_r = _testcase_run_bare(testcase);
+ test_r = testcase_run_bare_(testcase);
assert(0<=(int)test_r && (int)test_r<=2);
b[0] = "NYS"[test_r];
write_r = (int)write(outcome_pipe[1], b, 1);
@@ -209,16 +217,19 @@ _testcase_run_forked(const struct testgroup_t *group,
#endif
}
+#endif /* !NO_FORKING */
+
int
testcase_run_one(const struct testgroup_t *group,
const struct testcase_t *testcase)
{
enum outcome outcome;
- if (testcase->flags & TT_SKIP) {
+ if (testcase->flags & (TT_SKIP|TT_OFF_BY_DEFAULT)) {
if (opt_verbosity>0)
- printf("%s%s: SKIPPED\n",
- group->prefix, testcase->name);
+ printf("%s%s: %s\n",
+ group->prefix, testcase->name,
+ (testcase->flags & TT_SKIP) ? "SKIPPED" : "DISABLED");
++n_skipped;
return SKIP;
}
@@ -231,10 +242,14 @@ testcase_run_one(const struct testgroup_t *group,
cur_test_name = testcase->name;
}
+#ifndef NO_FORKING
if ((testcase->flags & TT_FORK) && !(opt_forked||opt_nofork)) {
- outcome = _testcase_run_forked(group, testcase);
+ outcome = testcase_run_forked_(group, testcase);
} else {
- outcome = _testcase_run_bare(testcase);
+#else
+ {
+#endif
+ outcome = testcase_run_bare_(testcase);
}
if (outcome == OK) {
@@ -260,7 +275,7 @@ testcase_run_one(const struct testgroup_t *group,
}
int
-_tinytest_set_flag(struct testgroup_t *groups, const char *arg, unsigned long flag)
+tinytest_set_flag_(struct testgroup_t *groups, const char *arg, int set, unsigned long flag)
{
int i, j;
size_t length = LONGEST_TEST_NAME;
@@ -270,12 +285,23 @@ _tinytest_set_flag(struct testgroup_t *groups, const char *arg, unsigned long fl
length = strstr(arg,"..")-arg;
for (i=0; groups[i].prefix; ++i) {
for (j=0; groups[i].cases[j].name; ++j) {
+ struct testcase_t *testcase = &groups[i].cases[j];
snprintf(fullname, sizeof(fullname), "%s%s",
- groups[i].prefix, groups[i].cases[j].name);
- if (!flag) /* Hack! */
- printf(" %s\n", fullname);
+ groups[i].prefix, testcase->name);
+ if (!flag) { /* Hack! */
+ printf(" %s", fullname);
+ if (testcase->flags & TT_OFF_BY_DEFAULT)
+ puts(" (Off by default)");
+ else if (testcase->flags & TT_SKIP)
+ puts(" (DISABLED)");
+ else
+ puts("");
+ }
if (!strncmp(fullname, arg, length)) {
- groups[i].cases[j].flags |= flag;
+ if (set)
+ testcase->flags |= flag;
+ else
+ testcase->flags &= ~flag;
++found;
}
}
@@ -288,21 +314,75 @@ usage(struct testgroup_t *groups, int list_groups)
{
puts("Options are: [--verbose|--quiet|--terse] [--no-fork]");
puts(" Specify tests by name, or using a prefix ending with '..'");
- puts(" To skip a test, list give its name prefixed with a colon.");
+ puts(" To skip a test, prefix its name with a colon.");
+ puts(" To enable a disabled test, prefix its name with a plus.");
puts(" Use --list-tests for a list of tests.");
if (list_groups) {
puts("Known tests are:");
- _tinytest_set_flag(groups, "..", 0);
+ tinytest_set_flag_(groups, "..", 1, 0);
}
exit(0);
}
+static int
+process_test_alias(struct testgroup_t *groups, const char *test)
+{
+ int i, j, n, r;
+ for (i=0; cfg_aliases && cfg_aliases[i].name; ++i) {
+ if (!strcmp(cfg_aliases[i].name, test)) {
+ n = 0;
+ for (j = 0; cfg_aliases[i].tests[j]; ++j) {
+ r = process_test_option(groups, cfg_aliases[i].tests[j]);
+ if (r<0)
+ return -1;
+ n += r;
+ }
+ return n;
+ }
+ }
+ printf("No such test alias as @%s!",test);
+ return -1;
+}
+
+static int
+process_test_option(struct testgroup_t *groups, const char *test)
+{
+ int flag = TT_ENABLED_;
+ int n = 0;
+ if (test[0] == '@') {
+ return process_test_alias(groups, test + 1);
+ } else if (test[0] == ':') {
+ ++test;
+ flag = TT_SKIP;
+ } else if (test[0] == '+') {
+ ++test;
+ ++n;
+ if (!tinytest_set_flag_(groups, test, 0, TT_OFF_BY_DEFAULT)) {
+ printf("No such test as %s!\n", test);
+ return -1;
+ }
+ } else {
+ ++n;
+ }
+ if (!tinytest_set_flag_(groups, test, 1, flag)) {
+ printf("No such test as %s!\n", test);
+ return -1;
+ }
+ return n;
+}
+
+void
+tinytest_set_aliases(const struct testlist_alias_t *aliases)
+{
+ cfg_aliases = aliases;
+}
+
int
tinytest_main(int c, const char **v, struct testgroup_t *groups)
{
int i, j, n=0;
-#ifdef WIN32
+#ifdef _WIN32
const char *sp = strrchr(v[0], '.');
const char *extension = "";
if (!sp || stricmp(sp, ".exe"))
@@ -334,29 +414,23 @@ tinytest_main(int c, const char **v, struct testgroup_t *groups)
return -1;
}
} else {
- const char *test = v[i];
- int flag = _TT_ENABLED;
- if (test[0] == ':') {
- ++test;
- flag = TT_SKIP;
- } else {
- ++n;
- }
- if (!_tinytest_set_flag(groups, test, flag)) {
- printf("No such test as %s!\n", v[i]);
+ int r = process_test_option(groups, v[i]);
+ if (r<0)
return -1;
- }
+ n += r;
}
}
if (!n)
- _tinytest_set_flag(groups, "..", _TT_ENABLED);
+ tinytest_set_flag_(groups, "..", 1, TT_ENABLED_);
+#ifdef _IONBF
setvbuf(stdout, NULL, _IONBF, 0);
+#endif
++in_tinytest_main;
for (i=0; groups[i].prefix; ++i)
for (j=0; groups[i].cases[j].name; ++j)
- if (groups[i].cases[j].flags & _TT_ENABLED)
+ if (groups[i].cases[j].flags & TT_ENABLED_)
testcase_run_one(&groups[i],
&groups[i].cases[j]);
@@ -375,13 +449,13 @@ tinytest_main(int c, const char **v, struct testgroup_t *groups)
}
int
-_tinytest_get_verbosity(void)
+tinytest_get_verbosity_(void)
{
return opt_verbosity;
}
void
-_tinytest_set_test_failed(void)
+tinytest_set_test_failed_(void)
{
if (opt_verbosity <= 0 && cur_test_name) {
if (opt_verbosity==0) puts("");
@@ -392,9 +466,28 @@ _tinytest_set_test_failed(void)
}
void
-_tinytest_set_test_skipped(void)
+tinytest_set_test_skipped_(void)
{
if (cur_test_outcome==OK)
cur_test_outcome = SKIP;
}
+char *
+tinytest_format_hex_(const void *val_, unsigned long len)
+{
+ const unsigned char *val = val_;
+ char *result, *cp;
+ size_t i;
+
+ if (!val)
+ return strdup("null");
+ if (!(result = malloc(len*2+1)))
+ return strdup("<allocation failure>");
+ cp = result;
+ for (i=0;i<len;++i) {
+ *cp++ = "0123456789ABCDEF"[val[i] >> 4];
+ *cp++ = "0123456789ABCDEF"[val[i] & 0x0f];
+ }
+ *cp = 0;
+ return result;
+}
diff --git a/test/tinytest.h b/test/tinytest.h
index 7ce7858e..ed07b26b 100644
--- a/test/tinytest.h
+++ b/test/tinytest.h
@@ -23,17 +23,19 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _TINYTEST_H
-#define _TINYTEST_H
+#ifndef TINYTEST_H_INCLUDED_
+#define TINYTEST_H_INCLUDED_
/** Flag for a test that needs to run in a subprocess. */
#define TT_FORK (1<<0)
/** Runtime flag for a test we've decided to skip. */
#define TT_SKIP (1<<1)
/** Internal runtime flag for a test we've decided to run. */
-#define _TT_ENABLED (1<<2)
+#define TT_ENABLED_ (1<<2)
+/** Flag for a test that's off by default. */
+#define TT_OFF_BY_DEFAULT (1<<3)
/** If you add your own flags, make them start at this point. */
-#define TT_FIRST_USER_FLAG (1<<3)
+#define TT_FIRST_USER_FLAG (1<<4)
typedef void (*testcase_fn)(void *);
@@ -64,22 +66,33 @@ struct testgroup_t {
};
#define END_OF_GROUPS { NULL, NULL}
+struct testlist_alias_t {
+ const char *name;
+ const char **tests;
+};
+#define END_OF_ALIASES { NULL, NULL }
+
/** Implementation: called from a test to indicate failure, before logging. */
-void _tinytest_set_test_failed(void);
+void tinytest_set_test_failed_(void);
/** Implementation: called from a test to indicate that we're skipping. */
-void _tinytest_set_test_skipped(void);
+void tinytest_set_test_skipped_(void);
/** Implementation: return 0 for quiet, 1 for normal, 2 for loud. */
-int _tinytest_get_verbosity(void);
+int tinytest_get_verbosity_(void);
/** Implementation: Set a flag on tests matching a name; returns number
* of tests that matched. */
-int _tinytest_set_flag(struct testgroup_t *, const char *, unsigned long);
+int tinytest_set_flag_(struct testgroup_t *, const char *, int set, unsigned long);
+/** Implementation: Put a chunk of memory into hex. */
+char *tinytest_format_hex_(const void *, unsigned long);
/** Set all tests in 'groups' matching the name 'named' to be skipped. */
#define tinytest_skip(groups, named) \
- _tinytest_set_flag(groups, named, TT_SKIP)
+ tinytest_set_flag_(groups, named, 1, TT_SKIP)
/** Run a single testcase in a single group. */
int testcase_run_one(const struct testgroup_t *,const struct testcase_t *);
+
+void tinytest_set_aliases(const struct testlist_alias_t *aliases);
+
/** Run a set of testcases from an END_OF_GROUPS-terminated array of groups,
as selected from the command line. */
int tinytest_main(int argc, const char **argv, struct testgroup_t *groups);
diff --git a/test/tinytest_demo.c b/test/tinytest_demo.c
index 2a999bee..bdd0e600 100644
--- a/test/tinytest_demo.c
+++ b/test/tinytest_demo.c
@@ -35,11 +35,15 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <time.h>
+#ifndef _WIN32
+#include <unistd.h>
+#endif
/* ============================================================ */
/* First, let's see if strcmp is working. (All your test cases should be
- * functions declared to take a single void * as) an argument. */
+ * functions declared to take a single void * as an argument.) */
void
test_strcmp(void *data)
{
@@ -148,6 +152,9 @@ test_memcpy(void *ptr)
memcpy(db->buffer2, db->buffer1, sizeof(db->buffer1));
tt_str_op(db->buffer1, ==, db->buffer2);
+ /* This one works if there's an internal NUL. */
+ tt_mem_op(db->buffer1, <, db->buffer2, sizeof(db->buffer1));
+
/* Now we've allocated memory that's referenced by a local variable.
The end block of the function will clean it up. */
mem = strdup("Hello world.");
@@ -162,6 +169,27 @@ test_memcpy(void *ptr)
free(mem);
}
+void
+test_timeout(void *ptr)
+{
+ time_t t1, t2;
+ (void)ptr;
+ t1 = time(NULL);
+#ifdef _WIN32
+ Sleep(5000);
+#else
+ sleep(5);
+#endif
+ t2 = time(NULL);
+
+ tt_int_op(t2-t1, >=, 4);
+
+ tt_int_op(t2-t1, <=, 6);
+
+ end:
+ ;
+}
+
/* ============================================================ */
/* Now we need to make sure that our tests get invoked. First, you take
@@ -178,6 +206,10 @@ struct testcase_t demo_tests[] = {
its environment. */
{ "memcpy", test_memcpy, TT_FORK, &data_buffer_setup },
+ /* This flag is off-by-default, since it takes a while to run. You
+ * can enable it manually by passing +demo/timeout at the command line.*/
+ { "timeout", test_timeout, TT_OFF_BY_DEFAULT },
+
/* The array has to end with END_OF_TESTCASES. */
END_OF_TESTCASES
};
@@ -192,6 +224,18 @@ struct testgroup_t groups[] = {
END_OF_GROUPS
};
+/* We can also define test aliases. These can be used for types of tests that
+ * cut across groups. */
+const char *alltests[] = { "+..", NULL };
+const char *slowtests[] = { "+demo/timeout", NULL };
+struct testlist_alias_t aliases[] = {
+
+ { "ALL", alltests },
+ { "SLOW", slowtests },
+
+ END_OF_ALIASES
+};
+
int
main(int c, const char **v)
@@ -211,5 +255,6 @@ main(int c, const char **v)
"tinytest-demo" and "tinytest-demo .." mean the same thing.
*/
+ tinytest_set_aliases(aliases);
return tinytest_main(c, v, groups);
}
diff --git a/test/tinytest_local.h b/test/tinytest_local.h
index 1a7f75ef..87ec2fa6 100644
--- a/test/tinytest_local.h
+++ b/test/tinytest_local.h
@@ -1,10 +1,10 @@
-#ifdef WIN32
+#include "util-internal.h"
+#ifdef _WIN32
#include <winsock2.h>
#endif
#include "event2/util.h"
-#include "util-internal.h"
#ifdef snprintf
#undef snprintf
diff --git a/test/tinytest_macros.h b/test/tinytest_macros.h
index 1a07ad14..c3728d1f 100644
--- a/test/tinytest_macros.h
+++ b/test/tinytest_macros.h
@@ -23,8 +23,8 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _TINYTEST_MACROS_H
-#define _TINYTEST_MACROS_H
+#ifndef TINYTEST_MACROS_H_INCLUDED_
+#define TINYTEST_MACROS_H_INCLUDED_
/* Helpers for defining statement-like macros */
#define TT_STMT_BEGIN do {
@@ -51,19 +51,19 @@
/* Announce a non-failure if we're verbose. */
#define TT_BLATHER(args) \
TT_STMT_BEGIN \
- if (_tinytest_get_verbosity()>1) TT_DECLARE(" OK", args); \
+ if (tinytest_get_verbosity_()>1) TT_DECLARE(" OK", args); \
TT_STMT_END
#define TT_DIE(args) \
TT_STMT_BEGIN \
- _tinytest_set_test_failed(); \
+ tinytest_set_test_failed_(); \
TT_GRIPE(args); \
TT_EXIT_TEST_FUNCTION; \
TT_STMT_END
#define TT_FAIL(args) \
TT_STMT_BEGIN \
- _tinytest_set_test_failed(); \
+ tinytest_set_test_failed_(); \
TT_GRIPE(args); \
TT_STMT_END
@@ -74,7 +74,7 @@
#define tt_abort() TT_DIE(("%s", "(Failed.)"))
/* Fail but do not abort the current test for the reason in msg. */
-#define tt_fail_printf(msg) TT_FAIL(msg)
+#define tt_failprint_f(msg) TT_FAIL(msg)
#define tt_fail_perror(op) TT_FAIL(("%s: %s [%d]",(op),strerror(errno), errno))
#define tt_fail_msg(msg) TT_FAIL(("%s", msg))
#define tt_fail() TT_FAIL(("%s", "(Failed.)"))
@@ -82,14 +82,14 @@
/* End the current test, and indicate we are skipping it. */
#define tt_skip() \
TT_STMT_BEGIN \
- _tinytest_set_test_skipped(); \
+ tinytest_set_test_skipped_(); \
TT_EXIT_TEST_FUNCTION; \
TT_STMT_END
-#define _tt_want(b, msg, fail) \
+#define tt_want_(b, msg, fail) \
TT_STMT_BEGIN \
if (!(b)) { \
- _tinytest_set_test_failed(); \
+ tinytest_set_test_failed_(); \
TT_GRIPE(("%s",msg)); \
fail; \
} else { \
@@ -99,11 +99,11 @@
/* Assert b, but do not stop the test if b fails. Log msg on failure. */
#define tt_want_msg(b, msg) \
- _tt_want(b, msg, );
+ tt_want_(b, msg, );
/* Assert b and stop the test if b fails. Log msg on failure. */
#define tt_assert_msg(b, msg) \
- _tt_want(b, msg, TT_EXIT_TEST_FUNCTION);
+ tt_want_(b, msg, TT_EXIT_TEST_FUNCTION);
/* Assert b, but do not stop the test if b fails. */
#define tt_want(b) tt_want_msg( (b), "want("#b")")
@@ -113,28 +113,28 @@
#define tt_assert_test_fmt_type(a,b,str_test,type,test,printf_type,printf_fmt, \
setup_block,cleanup_block,die_on_fail) \
TT_STMT_BEGIN \
- type _val1 = (type)(a); \
- type _val2 = (type)(b); \
- int _tt_status = (test); \
- if (!_tt_status || _tinytest_get_verbosity()>1) { \
- printf_type _print; \
- printf_type _print1; \
- printf_type _print2; \
- type _value = _val1; \
+ type val1_ = (a); \
+ type val2_ = (b); \
+ int tt_status_ = (test); \
+ if (!tt_status_ || tinytest_get_verbosity_()>1) { \
+ printf_type print_; \
+ printf_type print1_; \
+ printf_type print2_; \
+ type value_ = val1_; \
setup_block; \
- _print1 = _print; \
- _value = _val2; \
+ print1_ = print_; \
+ value_ = val2_; \
setup_block; \
- _print2 = _print; \
- TT_DECLARE(_tt_status?" OK":"FAIL", \
+ print2_ = print_; \
+ TT_DECLARE(tt_status_?" OK":"FAIL", \
("assert(%s): "printf_fmt" vs "printf_fmt, \
- str_test, _print1, _print2)); \
- _print = _print1; \
+ str_test, print1_, print2_)); \
+ print_ = print1_; \
cleanup_block; \
- _print = _print2; \
+ print_ = print2_; \
cleanup_block; \
- if (!_tt_status) { \
- _tinytest_set_test_failed(); \
+ if (!tt_status_) { \
+ tinytest_set_test_failed_(); \
die_on_fail ; \
} \
} \
@@ -142,43 +142,58 @@
#define tt_assert_test_type(a,b,str_test,type,test,fmt,die_on_fail) \
tt_assert_test_fmt_type(a,b,str_test,type,test,type,fmt, \
- {_print=_value;},{},die_on_fail)
+ {print_=value_;},{},die_on_fail)
+
+#define tt_assert_test_type_opt(a,b,str_test,type,test,fmt,die_on_fail) \
+ tt_assert_test_fmt_type(a,b,str_test,type,test,type,fmt, \
+ {print_=value_?value_:"<NULL>";},{},die_on_fail)
/* Helper: assert that a op b, when cast to type. Format the values with
* printf format fmt on failure. */
#define tt_assert_op_type(a,op,b,type,fmt) \
- tt_assert_test_type(a,b,#a" "#op" "#b,type,(_val1 op _val2),fmt, \
+ tt_assert_test_type(a,b,#a" "#op" "#b,type,(val1_ op val2_),fmt, \
TT_EXIT_TEST_FUNCTION)
#define tt_int_op(a,op,b) \
- tt_assert_test_type(a,b,#a" "#op" "#b,long,(_val1 op _val2), \
+ tt_assert_test_type(a,b,#a" "#op" "#b,long,(val1_ op val2_), \
"%ld",TT_EXIT_TEST_FUNCTION)
#define tt_uint_op(a,op,b) \
tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long, \
- (_val1 op _val2),"%lu",TT_EXIT_TEST_FUNCTION)
+ (val1_ op val2_),"%lu",TT_EXIT_TEST_FUNCTION)
#define tt_ptr_op(a,op,b) \
- tt_assert_test_type(a,b,#a" "#op" "#b,void*, \
- (_val1 op _val2),"%p",TT_EXIT_TEST_FUNCTION)
+ tt_assert_test_type(a,b,#a" "#op" "#b,const void*, \
+ (val1_ op val2_),"%p",TT_EXIT_TEST_FUNCTION)
#define tt_str_op(a,op,b) \
- tt_assert_test_type(a,b,#a" "#op" "#b,const char *, \
- (strcmp(_val1,_val2) op 0),"<%s>",TT_EXIT_TEST_FUNCTION)
+ tt_assert_test_type_opt(a,b,#a" "#op" "#b,const char *, \
+ (val1_ && val2_ && strcmp(val1_,val2_) op 0),"<%s>", \
+ TT_EXIT_TEST_FUNCTION)
+
+#define tt_mem_op(expr1, op, expr2, len) \
+ tt_assert_test_fmt_type(expr1,expr2,#expr1" "#op" "#expr2, \
+ const void *, \
+ (val1_ && val2_ && memcmp(val1_, val2_, len) op 0), \
+ char *, "%s", \
+ { print_ = tinytest_format_hex_(value_, (len)); }, \
+ { if (print_) free(print_); }, \
+ TT_EXIT_TEST_FUNCTION \
+ );
#define tt_want_int_op(a,op,b) \
- tt_assert_test_type(a,b,#a" "#op" "#b,long,(_val1 op _val2),"%ld",(void)0)
+ tt_assert_test_type(a,b,#a" "#op" "#b,long,(val1_ op val2_),"%ld",(void)0)
#define tt_want_uint_op(a,op,b) \
tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long, \
- (_val1 op _val2),"%lu",(void)0)
+ (val1_ op val2_),"%lu",(void)0)
#define tt_want_ptr_op(a,op,b) \
- tt_assert_test_type(a,b,#a" "#op" "#b,void*, \
- (_val1 op _val2),"%p",(void)0)
+ tt_assert_test_type(a,b,#a" "#op" "#b,const void*, \
+ (val1_ op val2_),"%p",(void)0)
#define tt_want_str_op(a,op,b) \
tt_assert_test_type(a,b,#a" "#op" "#b,const char *, \
- (strcmp(_val1,_val2) op 0),"<%s>",(void)0)
+ (strcmp(val1_,val2_) op 0),"<%s>",(void)0)
#endif
diff --git a/time-internal.h b/time-internal.h
new file mode 100644
index 00000000..daf20f47
--- /dev/null
+++ b/time-internal.h
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
+ * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef TIME_INTERNAL_H_INCLUDED_
+#define TIME_INTERNAL_H_INCLUDED_
+
+#include "event2/event-config.h"
+#include "evconfig-private.h"
+
+#ifdef EVENT__HAVE_MACH_MACH_TIME_H
+/* For mach_timebase_info */
+#include <mach/mach_time.h>
+#endif
+
+#include <time.h>
+
+#include "event2/util.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if defined(EVENT__HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
+#define HAVE_POSIX_MONOTONIC
+#elif defined(EVENT__HAVE_MACH_ABSOLUTE_TIME)
+#define HAVE_MACH_MONOTONIC
+#elif defined(_WIN32)
+#define HAVE_WIN32_MONOTONIC
+#else
+#define HAVE_FALLBACK_MONOTONIC
+#endif
+
+long evutil_tv_to_msec_(const struct timeval *tv);
+void evutil_usleep_(const struct timeval *tv);
+
+#ifdef _WIN32
+typedef ULONGLONG (WINAPI *ev_GetTickCount_func)(void);
+#endif
+
+struct evutil_monotonic_timer {
+
+#ifdef HAVE_MACH_MONOTONIC
+ struct mach_timebase_info mach_timebase_units;
+#endif
+
+#ifdef HAVE_POSIX_MONOTONIC
+ int monotonic_clock;
+#endif
+
+#ifdef HAVE_WIN32_MONOTONIC
+ ev_GetTickCount_func GetTickCount64_fn;
+ ev_GetTickCount_func GetTickCount_fn;
+ ev_uint64_t last_tick_count;
+ ev_uint64_t adjust_tick_count;
+
+ ev_uint64_t first_tick;
+ ev_uint64_t first_counter;
+ double usec_per_count;
+ int use_performance_counter;
+#endif
+
+ struct timeval adjust_monotonic_clock;
+ struct timeval last_time;
+};
+
+#define EV_MONOT_PRECISE 1
+#define EV_MONOT_FALLBACK 2
+
+int evutil_configure_monotonic_time_(struct evutil_monotonic_timer *mt,
+ int flags);
+int evutil_gettime_monotonic_(struct evutil_monotonic_timer *mt, struct timeval *tv);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* EVENT_INTERNAL_H_INCLUDED_ */
diff --git a/util-internal.h b/util-internal.h
index 01ebc171..4fdedeaf 100644
--- a/util-internal.h
+++ b/util-internal.h
@@ -23,21 +23,27 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _EVENT_UTIL_INTERNAL_H
-#define _EVENT_UTIL_INTERNAL_H
+#ifndef UTIL_INTERNAL_H_INCLUDED_
+#define UTIL_INTERNAL_H_INCLUDED_
#include "event2/event-config.h"
+#include "evconfig-private.h"
+
#include <errno.h>
/* For EVUTIL_ASSERT */
#include "log-internal.h"
#include <stdio.h>
#include <stdlib.h>
-#ifdef _EVENT_HAVE_SYS_SOCKET_H
+#ifdef EVENT__HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
+#ifdef EVENT__HAVE_SYS_EVENTFD_H
+#include <sys/eventfd.h>
+#endif
#include "event2/util.h"
+#include "time-internal.h"
#include "ipv6-internal.h"
#ifdef __cplusplus
@@ -45,21 +51,21 @@ extern "C" {
#endif
/* If we need magic to say "inline", get it for free internally. */
-#ifdef _EVENT_inline
-#define inline _EVENT_inline
+#ifdef EVENT__inline
+#define inline EVENT__inline
#endif
-#ifdef _EVENT___func__
-#define __func__ _EVENT___func__
+#ifdef EVENT____func__
+#define __func__ EVENT____func__
#endif
/* A good no-op to use in macro definitions. */
-#define _EVUTIL_NIL_STMT ((void)0)
+#define EVUTIL_NIL_STMT_ ((void)0)
/* A no-op that tricks the compiler into thinking a condition is used while
* definitely not making any code for it. Used to compile out asserts while
* avoiding "unused variable" warnings. The "!" forces the compiler to
* do the sizeof() on an int, in case "condition" is a bitfield value.
*/
-#define _EVUTIL_NIL_CONDITION(condition) do { \
+#define EVUTIL_NIL_CONDITION_(condition) do { \
(void)sizeof(!(condition)); \
} while(0)
@@ -71,23 +77,35 @@ extern "C" {
data," and we need to look at the error code. Second, Windows defines
a different set of error codes for sockets. */
-#ifndef WIN32
+#ifndef _WIN32
+
+#if EAGAIN == EWOULDBLOCK
+#define EVUTIL_ERR_IS_EAGAIN(e) \
+ ((e) == EAGAIN)
+#else
+#define EVUTIL_ERR_IS_EAGAIN(e) \
+ ((e) == EAGAIN || (e) == EWOULDBLOCK)
+#endif
/* True iff e is an error that means a read/write operation can be retried. */
#define EVUTIL_ERR_RW_RETRIABLE(e) \
- ((e) == EINTR || (e) == EAGAIN)
+ ((e) == EINTR || EVUTIL_ERR_IS_EAGAIN(e))
/* True iff e is an error that means an connect can be retried. */
#define EVUTIL_ERR_CONNECT_RETRIABLE(e) \
((e) == EINTR || (e) == EINPROGRESS)
/* True iff e is an error that means a accept can be retried. */
#define EVUTIL_ERR_ACCEPT_RETRIABLE(e) \
- ((e) == EINTR || (e) == EAGAIN || (e) == ECONNABORTED)
+ ((e) == EINTR || EVUTIL_ERR_IS_EAGAIN(e) || (e) == ECONNABORTED)
/* True iff e is an error that means the connection was refused */
#define EVUTIL_ERR_CONNECT_REFUSED(e) \
((e) == ECONNREFUSED)
#else
+/* Win32 */
+
+#define EVUTIL_ERR_IS_EAGAIN(e) \
+ ((e) == WSAEWOULDBLOCK || (e) == EAGAIN)
#define EVUTIL_ERR_RW_RETRIABLE(e) \
((e) == WSAEWOULDBLOCK || \
@@ -107,10 +125,6 @@ extern "C" {
#endif
-#ifdef _EVENT_socklen_t
-#define socklen_t _EVENT_socklen_t
-#endif
-
/* Arguments for shutdown() */
#ifdef SHUT_RD
#define EVUTIL_SHUT_RD SHUT_RD
@@ -128,20 +142,98 @@ extern "C" {
#define EVUTIL_SHUT_BOTH 2
#endif
+/* Helper: Verify that all the elements in 'dlist' are internally consistent.
+ * Checks for circular lists and bad prev/next pointers.
+ *
+ * Example usage:
+ * EVUTIL_ASSERT_LIST_OK(eventlist, event, ev_next);
+ */
+#define EVUTIL_ASSERT_LIST_OK(dlist, type, field) do { \
+ struct type *elm1, *elm2, **nextp; \
+ if (LIST_EMPTY((dlist))) \
+ break; \
+ \
+ /* Check list for circularity using Floyd's */ \
+ /* 'Tortoise and Hare' algorithm */ \
+ elm1 = LIST_FIRST((dlist)); \
+ elm2 = LIST_NEXT(elm1, field); \
+ while (elm1 && elm2) { \
+ EVUTIL_ASSERT(elm1 != elm2); \
+ elm1 = LIST_NEXT(elm1, field); \
+ elm2 = LIST_NEXT(elm2, field); \
+ if (!elm2) \
+ break; \
+ EVUTIL_ASSERT(elm1 != elm2); \
+ elm2 = LIST_NEXT(elm2, field); \
+ } \
+ \
+ /* Now check next and prev pointers for consistency. */ \
+ nextp = &LIST_FIRST((dlist)); \
+ elm1 = LIST_FIRST((dlist)); \
+ while (elm1) { \
+ EVUTIL_ASSERT(*nextp == elm1); \
+ EVUTIL_ASSERT(nextp == elm1->field.le_prev); \
+ nextp = &LIST_NEXT(elm1, field); \
+ elm1 = *nextp; \
+ } \
+ } while (0)
+
+/* Helper: Verify that all the elements in a TAILQ are internally consistent.
+ * Checks for circular lists and bad prev/next pointers.
+ *
+ * Example usage:
+ * EVUTIL_ASSERT_TAILQ_OK(activelist, event, ev_active_next);
+ */
+#define EVUTIL_ASSERT_TAILQ_OK(tailq, type, field) do { \
+ struct type *elm1, *elm2, **nextp; \
+ if (TAILQ_EMPTY((tailq))) \
+ break; \
+ \
+ /* Check list for circularity using Floyd's */ \
+ /* 'Tortoise and Hare' algorithm */ \
+ elm1 = TAILQ_FIRST((tailq)); \
+ elm2 = TAILQ_NEXT(elm1, field); \
+ while (elm1 && elm2) { \
+ EVUTIL_ASSERT(elm1 != elm2); \
+ elm1 = TAILQ_NEXT(elm1, field); \
+ elm2 = TAILQ_NEXT(elm2, field); \
+ if (!elm2) \
+ break; \
+ EVUTIL_ASSERT(elm1 != elm2); \
+ elm2 = TAILQ_NEXT(elm2, field); \
+ } \
+ \
+ /* Now check next and prev pointers for consistency. */ \
+ nextp = &TAILQ_FIRST((tailq)); \
+ elm1 = TAILQ_FIRST((tailq)); \
+ while (elm1) { \
+ EVUTIL_ASSERT(*nextp == elm1); \
+ EVUTIL_ASSERT(nextp == elm1->field.tqe_prev); \
+ nextp = &TAILQ_NEXT(elm1, field); \
+ elm1 = *nextp; \
+ } \
+ EVUTIL_ASSERT(nextp == (tailq)->tqh_last); \
+ } while (0)
+
/* Locale-independent replacements for some ctypes functions. Use these
* when you care about ASCII's notion of character types, because you are about
* to send those types onto the wire.
*/
-int EVUTIL_ISALPHA(char c);
-int EVUTIL_ISALNUM(char c);
-int EVUTIL_ISSPACE(char c);
-int EVUTIL_ISDIGIT(char c);
-int EVUTIL_ISXDIGIT(char c);
-int EVUTIL_ISPRINT(char c);
-int EVUTIL_ISLOWER(char c);
-int EVUTIL_ISUPPER(char c);
-char EVUTIL_TOUPPER(char c);
-char EVUTIL_TOLOWER(char c);
+int EVUTIL_ISALPHA_(char c);
+int EVUTIL_ISALNUM_(char c);
+int EVUTIL_ISSPACE_(char c);
+int EVUTIL_ISDIGIT_(char c);
+int EVUTIL_ISXDIGIT_(char c);
+int EVUTIL_ISPRINT_(char c);
+int EVUTIL_ISLOWER_(char c);
+int EVUTIL_ISUPPER_(char c);
+char EVUTIL_TOUPPER_(char c);
+char EVUTIL_TOLOWER_(char c);
+
+/** Remove all trailing horizontal whitespace (space or tab) from the end of a
+ * string */
+void evutil_rtrim_lws_(char *);
+
/** Helper macro. If we know that a given pointer points to a field in a
structure, return a pointer to the structure itself. Used to implement
@@ -164,23 +256,49 @@ char EVUTIL_TOLOWER(char c);
/* As open(pathname, flags, mode), except that the file is always opened with
* the close-on-exec flag set. (And the mode argument is mandatory.)
*/
-int evutil_open_closeonexec(const char *pathname, int flags, unsigned mode);
+int evutil_open_closeonexec_(const char *pathname, int flags, unsigned mode);
-int evutil_read_file(const char *filename, char **content_out, size_t *len_out,
+int evutil_read_file_(const char *filename, char **content_out, size_t *len_out,
int is_binary);
-int evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen);
+int evutil_socket_connect_(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen);
-int evutil_socket_finished_connecting(evutil_socket_t fd);
+int evutil_socket_finished_connecting_(evutil_socket_t fd);
-int evutil_ersatz_socketpair(int, int , int, evutil_socket_t[]);
+int evutil_ersatz_socketpair_(int, int , int, evutil_socket_t[]);
-int evutil_resolve(int family, const char *hostname, struct sockaddr *sa,
+int evutil_resolve_(int family, const char *hostname, struct sockaddr *sa,
ev_socklen_t *socklen, int port);
-const char *evutil_getenv(const char *name);
+const char *evutil_getenv_(const char *name);
-long _evutil_weakrand(void);
+/* Structure to hold the state of our weak random number generator.
+ */
+struct evutil_weakrand_state {
+ ev_uint32_t seed;
+};
+
+#define EVUTIL_WEAKRAND_MAX EV_INT32_MAX
+
+/* Initialize the state of a week random number generator based on 'seed'. If
+ * the seed is 0, construct a new seed based on not-very-strong platform
+ * entropy, like the PID and the time of day.
+ *
+ * This function, and the other evutil_weakrand* functions, are meant for
+ * speed, not security or statistical strength. If you need a RNG which an
+ * attacker can't predict, or which passes strong statistical tests, use the
+ * evutil_secure_rng* functions instead.
+ */
+ev_uint32_t evutil_weakrand_seed_(struct evutil_weakrand_state *state, ev_uint32_t seed);
+/* Return a pseudorandom value between 0 and EVUTIL_WEAKRAND_MAX inclusive.
+ * Updates the state in 'seed' as needed -- this value must be protected by a
+ * lock.
+ */
+ev_int32_t evutil_weakrand_(struct evutil_weakrand_state *seed);
+/* Return a pseudorandom value x such that 0 <= x < top. top must be no more
+ * than EVUTIL_WEAKRAND_MAX. Updates the state in 'seed' as needed -- this
+ * value must be proteced by a lock */
+ev_int32_t evutil_weakrand_range_(struct evutil_weakrand_state *seed, ev_int32_t top);
/* Evaluates to the same boolean value as 'p', and hints to the compiler that
* we expect this value to be false. */
@@ -192,13 +310,13 @@ long _evutil_weakrand(void);
/* Replacement for assert() that calls event_errx on failure. */
#ifdef NDEBUG
-#define EVUTIL_ASSERT(cond) _EVUTIL_NIL_CONDITION(cond)
+#define EVUTIL_ASSERT(cond) EVUTIL_NIL_CONDITION_(cond)
#define EVUTIL_FAILURE_CHECK(cond) 0
#else
#define EVUTIL_ASSERT(cond) \
do { \
if (EVUTIL_UNLIKELY(!(cond))) { \
- event_errx(_EVENT_ERR_ABORT, \
+ event_errx(EVENT_ERR_ABORT_, \
"%s:%d: Assertion %s failed in %s", \
__FILE__,__LINE__,#cond,__func__); \
/* In case a user-supplied handler tries to */ \
@@ -212,7 +330,7 @@ long _evutil_weakrand(void);
#define EVUTIL_FAILURE_CHECK(cond) EVUTIL_UNLIKELY(cond)
#endif
-#ifndef _EVENT_HAVE_STRUCT_SOCKADDR_STORAGE
+#ifndef EVENT__HAVE_STRUCT_SOCKADDR_STORAGE
/* Replacement for sockaddr storage that we can use internally on platforms
* that lack it. It is not space-efficient, but neither is sockaddr_storage.
*/
@@ -228,7 +346,7 @@ struct sockaddr_storage {
#endif
/* Internal addrinfo error code. This one is returned from only from
- * evutil_getaddrinfo_common, when we are sure that we'll have to hit a DNS
+ * evutil_getaddrinfo_common_, when we are sure that we'll have to hit a DNS
* server. */
#define EVUTIL_EAI_NEED_RESOLVE -90002
@@ -240,24 +358,24 @@ typedef struct evdns_getaddrinfo_request* (*evdns_getaddrinfo_fn)(
const struct evutil_addrinfo *hints_in,
void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
-void evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo_fn fn);
+void evutil_set_evdns_getaddrinfo_fn_(evdns_getaddrinfo_fn fn);
-struct evutil_addrinfo *evutil_new_addrinfo(struct sockaddr *sa,
+struct evutil_addrinfo *evutil_new_addrinfo_(struct sockaddr *sa,
ev_socklen_t socklen, const struct evutil_addrinfo *hints);
-struct evutil_addrinfo *evutil_addrinfo_append(struct evutil_addrinfo *first,
+struct evutil_addrinfo *evutil_addrinfo_append_(struct evutil_addrinfo *first,
struct evutil_addrinfo *append);
-void evutil_adjust_hints_for_addrconfig(struct evutil_addrinfo *hints);
-int evutil_getaddrinfo_common(const char *nodename, const char *servname,
+void evutil_adjust_hints_for_addrconfig_(struct evutil_addrinfo *hints);
+int evutil_getaddrinfo_common_(const char *nodename, const char *servname,
struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum);
-int evutil_getaddrinfo_async(struct evdns_base *dns_base,
+int evutil_getaddrinfo_async_(struct evdns_base *dns_base,
const char *nodename, const char *servname,
const struct evutil_addrinfo *hints_in,
void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
/** Return true iff sa is a looback address. (That is, it is 127.0.0.1/8, or
* ::1). */
-int evutil_sockaddr_is_loopback(const struct sockaddr *sa);
+int evutil_sockaddr_is_loopback_(const struct sockaddr *sa);
/**
@@ -265,14 +383,16 @@ int evutil_sockaddr_is_loopback(const struct sockaddr *sa);
Returns a pointer to out. Always writes something into out, so it's safe
to use the output of this function without checking it for NULL.
*/
-const char *evutil_format_sockaddr_port(const struct sockaddr *sa, char *out, size_t outlen);
+const char *evutil_format_sockaddr_port_(const struct sockaddr *sa, char *out, size_t outlen);
+
+int evutil_hex_char_to_int_(char c);
-long evutil_tv_to_msec(const struct timeval *tv);
-int evutil_hex_char_to_int(char c);
+void evutil_free_secure_rng_globals_(void);
+void evutil_free_globals_(void);
-#ifdef WIN32
-HANDLE evutil_load_windows_system_library(const TCHAR *library_name);
+#ifdef _WIN32
+HMODULE evutil_load_windows_system_library_(const TCHAR *library_name);
#endif
#ifndef EV_SIZE_FMT
@@ -307,7 +427,7 @@ HANDLE evutil_load_windows_system_library(const TCHAR *library_name);
#endif
#ifndef EV_SIZE_FMT
-#if (_EVENT_SIZEOF_SIZE_T <= _EVENT_SIZEOF_LONG)
+#if (EVENT__SIZEOF_SIZE_T <= EVENT__SIZEOF_LONG)
#define EV_SIZE_FMT "%lu"
#define EV_SSIZE_FMT "%ld"
#define EV_SIZE_ARG(x) ((unsigned long)(x))
@@ -320,6 +440,36 @@ HANDLE evutil_load_windows_system_library(const TCHAR *library_name);
#endif
#endif
+evutil_socket_t evutil_socket_(int domain, int type, int protocol);
+evutil_socket_t evutil_accept4_(evutil_socket_t sockfd, struct sockaddr *addr,
+ ev_socklen_t *addrlen, int flags);
+
+ /* used by one of the test programs.. */
+EVENT2_EXPORT_SYMBOL
+int evutil_make_internal_pipe_(evutil_socket_t fd[2]);
+evutil_socket_t evutil_eventfd_(unsigned initval, int flags);
+
+#ifdef SOCK_NONBLOCK
+#define EVUTIL_SOCK_NONBLOCK SOCK_NONBLOCK
+#else
+#define EVUTIL_SOCK_NONBLOCK 0x4000000
+#endif
+#ifdef SOCK_CLOEXEC
+#define EVUTIL_SOCK_CLOEXEC SOCK_CLOEXEC
+#else
+#define EVUTIL_SOCK_CLOEXEC 0x80000000
+#endif
+#ifdef EFD_NONBLOCK
+#define EVUTIL_EFD_NONBLOCK EFD_NONBLOCK
+#else
+#define EVUTIL_EFD_NONBLOCK 0x4000
+#endif
+#ifdef EFD_CLOEXEC
+#define EVUTIL_EFD_CLOEXEC EFD_CLOEXEC
+#else
+#define EVUTIL_EFD_CLOEXEC 0x8000
+#endif
+
void evutil_memclear_(void *mem, size_t len);
#ifdef __cplusplus
diff --git a/whatsnew-2.0.txt b/whatsnew-2.0.txt
index be0c71bc..3561fcb9 100644
--- a/whatsnew-2.0.txt
+++ b/whatsnew-2.0.txt
@@ -358,7 +358,7 @@ What's New In Libevent 2.0 so far:
any means provided to create an evbuffer except via evbuffer_new which
heap-allocated the buffer.
- If you need access to the whole bufer as a linear chunk of memory, the
+ If you need access to the whole buffer as a linear chunk of memory, the
EVBUFFER_DATA() function still works. Watch out, though: it needs to copy
the buffer's contents in a linear chunk before you can use it.
diff --git a/whatsnew-2.1.txt b/whatsnew-2.1.txt
new file mode 100644
index 00000000..0be54ae1
--- /dev/null
+++ b/whatsnew-2.1.txt
@@ -0,0 +1,690 @@
+ What's new in Libevent 2.1
+ Nick Mathewson
+
+0. Before we start
+
+0.1. About this document
+
+ This document describes the key differences between Libevent 2.0 and
+ Libevent 2.1, from a user's point of view. It's a work in progress.
+
+ For better documentation about libevent, see the links at
+ http://libevent.org/
+
+ Libevent 2.1 would not be possible without the generous help of
+ numerous volunteers. For a list of who did what in Libevent 2.1,
+ please see the ChangeLog!
+
+ NOTE: I am very sure that I missed some thing on this list. Caveat
+ haxxor.
+
+0.2. Where to get help
+
+ Try looking at the other documentation too. All of the header files
+ have documentation in the doxygen format; this gets turned into nice
+ HTML and linked to from the libevent.org website.
+
+ There is a work-in-progress book with reference manual at
+ http://www.wangafu.net/~nickm/libevent-book/ .
+
+ You can ask questions on the #libevent IRC channel at irc.oftc.net or
+ on the mailing list at libevent-users@freehaven.net. The mailing list
+ is subscribers-only, so you will need to subscribe before you post.
+
+0.3. Compatibility
+
+ Our source-compatibility policy is that correct code (that is to say,
+ code that uses public interfaces of Libevent and relies only on their
+ documented behavior) should have forward source compatibility: any
+ such code that worked with a previous version of Libevent should work
+ with this version too.
+
+ We don't try to do binary compatibility except within stable release
+ series, so binaries linked against any version of Libevent 2.0 will
+ probably need to be recompiled against Libevent 2.1.4-alpha if you
+ want to use it. It is probable that we'll break binary compatibility
+ again before Libevent 2.1 is stable.
+
+1. New APIs and features
+
+1.1. New ways to build libevent
+
+ We now provide an --enable-gcc-hardening configure option to turn on
+ GCC features designed for increased code security.
+
+ There is also an --enable-silent-rules configure option to make
+ compilation run more quietly with automake 1.11 or later.
+
+ You no longer need to use the --enable-gcc-warnings option to turn on
+ all of the GCC warnings that Libevent uses. The only change from
+ using that option now is to turn warnings into errors.
+
+ For IDE users, files that are not supposed to be built are now
+ surrounded with appropriate #ifdef lines to keep your IDE from getting
+ upset.
+
+ There is now an alternative cmake-based build process; cmake users
+ should see the relevant sections in the README.
+
+
+1.2. New functions for events and the event loop
+
+ If you're running Libevent with multiple event priorities, you might
+ want to make sure that Libevent checks for new events frequently, so
+ that time-consuming or numerous low-priority events don't keep it from
+ checking for new high-priority events. You can now use the
+ event_config_set_max_dispatch_interval() interface to ensure that the
+ loop checks for new events either every N microseconds, every M
+ callbacks, or both.
+
+ When configuring an event base, you can now choose whether you want
+ timers to be more efficient, or more precise. (This only has effect
+ on Linux for now.) Timers are efficient by default: to select more
+ precise timers, use the EVENT_BASE_FLAG_PRECISE_TIMER flag when
+ constructing the event_config, or set the EVENT_PRECISE_TIMER
+ environment variable to a non-empty string.
+
+ There is an EVLOOP_NO_EXIT_ON_EMPTY flag that tells event_base_loop()
+ to keep looping even when there are no pending events. (Ordinarily,
+ event_base_loop() will exit as soon as no events are pending.)
+
+ Past versions of Libevent have been annoying to use with some
+ memory-leak-checking tools, because Libevent allocated some global
+ singletons but provided no means to free them. There is now a
+ function, libevent_global_shutdown(), that you can use to free all
+ globally held resources before exiting, so that your leak-check tools
+ don't complain. (Note: this function doesn't free non-global things
+ like events, bufferevents, and so on; and it doesn't free anything
+ that wouldn't otherwise get cleaned up by the operating system when
+ your process exit()s. If you aren't using a leak-checking tool, there
+ is not much reason to call libevent_global_shutdown().)
+
+ There is a new event_base_get_npriorities() function to return the
+ number of priorities set in the event base.
+
+ Libevent 2.0 added an event_new() function to construct a new struct
+ event on the heap. Unfortunately, with event_new(), there was no
+ equivalent for:
+
+ struct event ev;
+ event_assign(&ev, base, fd, EV_READ, callback, &ev);
+
+ In other words, there was no easy way for event_new() to set up an
+ event so that the event itself would be its callback argument.
+ Libevent 2.1 lets you do this by passing "event_self_cbarg()" as the
+ callback argument:
+
+ struct event *evp;
+ evp = event_new(base, fd, EV_READ, callback,
+ event_self_cbarg());
+
+ There's also a new event_base_get_running_event() function you can
+ call from within a Libevent callback to get a pointer to the current
+ event. This should never be strictly necessary, but it's sometimes
+ convenient.
+
+ The event_base_once() function used to leak some memory if the event
+ that it added was never actually triggered. Now, its memory is
+ tracked in the event_base and freed when the event_base is freed.
+ Note however that Libevent doesn't know how to free any information
+ passed as the callback argument to event_base_once is still something
+ you'll might need a way to de-allocate yourself.
+
+ There is an event_get_priority() function to return an event's
+ priority.
+
+ By analogy to event_base_loopbreak(), there is now an
+ event_base_loopcontinue() that tells Libevent to stop processing
+ active event callbacks, and re-scan for new events right away.
+
+ There's a function, event_base_foreach_event(), that can iterate over
+ every event currently pending or active on an event base, and invoke a
+ user-supplied callback on each. The callback must not alter the events
+ or add or remove anything to the event base.
+
+ We now have an event_remove_timer() function to remove the timeout on
+ an event while leaving its socket and/or signal triggers unchanged.
+ (If we were designing the API from scratch, this would be the behavior
+ of "event_add(ev, NULL)" on an already-added event with a timeout. But
+ that's a no-op in past versions of Libevent, and we don't want to
+ break compatibility.)
+
+ You can use the new event_base_get_num_events() function to find the
+ number of events active or pending on an event_base. To find the
+ largest number of events that there have been since the last call, use
+ event_base_get_max_events().
+
+ You can now activate all the events waiting for a given fd or signal
+ using the event_base_active_by_fd() and event_base_active_by_signal()
+ APIs.
+
+ On backends that support it (currently epoll), there is now an
+ EV_CLOSED flag that programs can use to detect when a socket has
+ closed without having to read all the bytes until receiving an EOF.
+
+1.3. Event finalization
+
+ [NOTE: This is an experimental feature in Libevent 2.1.3-alpha. Though
+ it seems solid so far, its API might change between now and the first
+ release candidate for Libevent 2.1.]
+
+1.3.1. Why event finalization?
+
+ Libevent 2.1 now supports an API for safely "finalizing" events that
+ might be running in multiple threads, and provides a way to slightly
+ change the semantics of event_del() to prevent deadlocks in
+ multithreaded programs.
+
+ To motivate this feature, consider the following code, in the context
+ of a mulithreaded Libevent application:
+
+ struct connection *conn = event_get_callback_arg(ev);
+ event_del(ev);
+ connection_free(conn);
+
+ Suppose that the event's callback might be running in another thread,
+ and using the value of "conn" concurrently. We wouldn't want to
+ execute the connection_free() call until "conn" is no longer in use.
+ How can we make this code safe?
+
+ Libevent 2.0 answered that question by saying that the event_del()
+ call should block if the event's callback is running in another
+ thread. That way, we can be sure that event_del() has canceled the
+ callback (if the callback hadn't started running yet), or has waited
+ for the callback to finish.
+
+ But now suppose that the data structure is protected by a lock, and we
+ have the following code:
+
+ void check_disable(struct connection *connection) {
+ lock(connection);
+ if (should_stop_reading(connection))
+ event_del(connection->read_event);
+ unlock(connection);
+ }
+
+ What happens when we call check_disable() from a callback and from
+ another thread? Let's say that the other thread gets the lock
+ first. If it decides to call event_del(), it will wait for the
+ callback to finish. But meanwhile, the callback will be waiting for
+ the lock on the connection. Since each threads is waiting for the
+ other one to release a resource, the program will deadlock.
+
+ This bug showed up in multithreaded bufferevent programs in 2.1,
+ particularly when freeing bufferevents. (For more information, see
+ the "Deadlock when calling bufferevent_free from an other thread"
+ thread on libevent-users starting on 6 August 2012 and running through
+ February of 2013. You might also like to read my earlier writeup at
+ http://archives.seul.org/libevent/users/Feb-2012/msg00053.html and
+ the ensuing discussion.)
+
+1.3.2. The EV_FINALIZE flag and avoiding deadlock
+
+ To prevent the deadlock condition described above, Libevent
+ 2.1.3-alpha adds a new flag, "EV_FINALIZE". You can pass it to
+ event_new() and event_assign() along with EV_READ, EV_WRITE, and the
+ other event flags.
+
+ When an event is constructed with the EV_FINALIZE flag, event_del()
+ will not block on that event, even when the event's callback is
+ running in another thread. By using EV_FINALIZE, you are therefore
+ promising not to use the "event_del(ev); free(event_get_callback_arg(ev));"
+ pattern, but rather to use one of the finalization functions below to
+ clean up the event.
+
+ EV_FINALIZE has no effect on a single-threaded program, or on a
+ program where events are only used from one thread.
+
+
+ There are also two new variants of event_del() that you can use for
+ more fine-grained control:
+ event_del_noblock(ev)
+ event_del_block(ev)
+ The event_del_noblock() function will never block, even if the event
+ callback is running in another thread and doesn't have the EV_FINALIZE
+ flag. The event_del_block() function will _always_ block if the event
+ callback is running in another thread, even if the event _does_ have
+ the EV_FINALIZE flag.
+
+ [A future version of Libevent may have a way to make the EV_FINALIZE
+ flag the default.]
+
+1.3.3. Safely finalizing events
+
+ To safely tear down an event that may be running, Libevent 2.1.3-alpha
+ introduces event_finalize() and event_free_finalize(). You call them
+ on an event, and provide a finalizer callback to be run on the event
+ and its callback argument once the event is definitely no longer
+ running.
+
+ With event_free_finalize(), the event is also freed once the finalizer
+ callback has been invoked.
+
+ A finalized event cannot be re-added or activated. The finalizer
+ callback must not add events, activate events, or attempt to
+ "resucitate" the event being finalized in any way.
+
+ If any finalizer callbacks are pending as the event_base is being
+ freed, they will be invoked. You can override this behavior with the
+ new function event_base_free_nofinalize().
+
+1.4. New debugging features
+
+ You can now turn on debug logs at runtime using a new function,
+ event_enable_debug_logging().
+
+ The event_enable_lock_debugging() function is now spelled correctly.
+ You can still use the old "event_enable_lock_debuging" name, though,
+ so your old programs shouldnt' break.
+
+ There's also been some work done to try to make the debugging logs
+ more generally useful.
+
+1.5. New evbuffer functions
+
+ In Libevent 2.0, we introduced evbuffer_add_file() to add an entire
+ file's contents to an evbuffer, and then send them using sendfile() or
+ mmap() as appropriate. This API had some drawbacks, however.
+ Notably, it created one mapping or fd for every instance of the same
+ file added to any evbuffer. Also, adding a file to an evbuffer could
+ make that buffer unusable with SSL bufferevents, filtering
+ bufferevents, and any code that tried to read the contents of the
+ evbuffer.
+
+ Libevent 2.1 adds a new evbuffer_file_segment API to solve these
+ problems. Now, you can use evbuffer_file_segment_new() to construct a
+ file-segment object, and evbuffer_add_file_segment() to insert it (or
+ part of it) into an evbuffer. These segments avoid creating redundant
+ maps or fds. Better still, the code is smart enough (when the OS
+ supports sendfile) to map the file when that's necessary, and use
+ sendfile() otherwise.
+
+ File segments can receive callback functions that are invoked when the
+ file segments are freed.
+
+ The evbuffer_ptr interface has been extended so that an evbuffer_ptr
+ can now yield a point just after the end of the buffer. This makes
+ many algorithms simpler to implement.
+
+ There's a new evbuffer_add_buffer() interface that you can use to add
+ one buffer to another nondestructively. When you say
+ evbuffer_add_buffer_reference(outbuf, inbuf), outbuf now contains a
+ reference to the contents of inbuf.
+
+ To aid in adding data in bulk while minimizing evbuffer calls, there
+ is an evbuffer_add_iovec() function.
+
+ There's a new evbuffer_copyout_from() variant function to enable
+ copying data nondestructively from the middle of a buffer.
+
+ evbuffer_readln() now supports an EVBUFFER_EOL_NUL argument to fetch
+ NUL-terminated strings from buffers.
+
+1.6. New functions and features: bufferevents
+
+ You can now use the bufferevent_getcb() function to find out a
+ bufferevent's callbacks. Previously, there was no supported way to do
+ that.
+
+ The largest chunk readable or writeable in a single bufferevent
+ callback is no longer hardcoded; it's now configurable with
+ the new functions bufferevent_set_max_single_read() and
+ bufferevent_set_max_single_write().
+
+ For consistency, OpenSSL bufferevents now make sure to always set one
+ of BEV_EVENT_READING or BEV_EVENT_WRITING when invoking an event
+ callback.
+
+ Calling bufferevent_set_timeouts(bev, NULL, NULL) now removes the
+ timeouts from socket and ssl bufferevents correctly.
+
+ You can find the priority at which a bufferevent runs with
+ bufferevent_get_priority().
+
+ The function bufferevent_get_token_bucket_cfg() can retrieve the
+ rate-limit settings for a bufferevent; bufferevent_getwatermark() can
+ return a bufferevent's current watermark settings.
+
+ You can manually trigger a bufferevent's callbacks via
+ bufferevent_trigger() and bufferevent_trigger_event().
+
+1.7. New functions and features: evdns
+
+ The previous evdns interface used an "open a test UDP socket" trick in
+ order to detect IPv6 support. This was a hack, since it would
+ sometimes badly confuse people's firewall software, even though no
+ packets were sent. The current evdns interface-detection code uses
+ the appropriate OS functions to see which interfaces are configured.
+
+ The evdns_base_new() function now has multiple possible values for its
+ second (flags) argument. Using 1 and 0 have their old meanings, though the
+ 1 flag now has a symbolic name of EVDNS_BASE_INITIALIZE_NAMESERVERS.
+ A second flag is now supported too: the EVDNS_BASE_DISABLE_WHEN_INACTIVE
+ flag, which tells the evdns_base that it should not prevent Libevent from
+ exiting while it has no DNS requests in progress.
+
+ There is a new evdns_base_clear_host_addresses() function to remove
+ all the /etc/hosts addresses registered with an evdns instance.
+
+1.8. New functions and features: evconnlistener
+
+ Libevent 2.1 adds the following evconnlistener flags:
+
+ LEV_OPT_DEFERRED_ACCEPT -- Tells the OS that it doesn't need to
+ report sockets as having arrived until the initiator has sent some
+ data too. This can greatly improve performance with protocols like
+ HTTP where the client always speaks first. On operating systems
+ that don't support this functionality, this option has no effect.
+
+ LEV_OPT_DISABLED -- Creates an evconnlistener in the disabled (not
+ listening) state.
+
+ Libevent 2.1 changes the behavior of the LEV_OPT_CLOSE_ON_EXEC
+ flag. Previously, it would apply to the listener sockets, but not to
+ the accepted sockets themselves. That's almost never what you want.
+ Now, it applies both to the listener and the accepted sockets.
+
+1.9. New functions and features: evhttp
+
+ **********************************************************************
+ NOTE: The evhttp module will eventually be deprecated in favor of Mark
+ Ellzey's libevhtp library. Don't worry -- this won't happen until
+ libevhtp provides every feature that evhttp does, and provides a
+ compatible interface that applications can use to migrate.
+ **********************************************************************
+
+ Previously, you could only set evhttp timeouts in increments of one
+ second. Now, you can use evhttp_set_timeout_tv() and
+ evhttp_connection_set_timeout_tv() to configure
+ microsecond-granularity timeouts.
+
+ There are a new pair of functions: evhttp_set_bevcb() and
+ evhttp_connection_base_bufferevent_new(), that you can use to
+ configure which bufferevents will be used for incoming and outgoing
+ http connections respectively. These functions, combined with SSL
+ bufferevents, should enable HTTPS support.
+
+ There's a new evhttp_foreach_bound_socket() function to iterate over
+ every listener on an evhttp object.
+
+ Whitespace between lines in headers is now folded into a single space;
+ whitespace at the end of a header is now removed.
+
+ The socket errno value is now preserved when invoking an http error
+ callback.
+
+ There's a new kind of request callback for errors; you can set it with
+ evhttp_request_set_error_cb(). It gets called when there's a request error,
+ and actually reports the error code and lets you figure out which request
+ failed.
+
+ You can navigate from an evhttp_connection back to its evhttp with the
+ new evhttp_connection_get_server() function.
+
+ You can override the default HTTP Content-Type with the new
+ evhttp_set_default_content_type() function
+
+ There's a new evhttp_connection_get_addr() API to return the peer
+ address of an evhttp_connection.
+
+ The new evhttp_send_reply_chunk_with_cb() is a variant of
+ evhttp_send_reply_chunk() with a callback to be invoked when the
+ chunk is sent.
+
+ The evhttp_request_set_header_cb() facility adds a callback to be
+ invoked while parsing headers.
+
+ The evhttp_request_set_on_complete_cb() facility adds a callback to be
+ invoked on request completion.
+
+1.10. New functions and features: evutil
+
+ There's a function "evutil_secure_rng_set_urandom_device_file()" that
+ you can use to override the default file that Libevent uses to seed
+ its (sort-of) secure RNG.
+
+2. Cross-platform performance improvements
+
+2.1. Better data structures
+
+ We replaced several users of the sys/queue.h "TAILQ" data structure
+ with the "LIST" data structure. Because this data type doesn't
+ require FIFO access, it requires fewer pointer checks and
+ manipulations to keep it in line.
+
+ All previous versions of Libevent have kept every pending (added)
+ event in an "eventqueue" data structure. Starting in Libevent 2.0,
+ however, this structure became redundant: every pending timeout event
+ is stored in the timeout heap or in one of the common_timeout queues,
+ and every pending fd or signal event is stored in an evmap. Libevent
+ 2.1 removes this data structure, and thereby saves all of the code
+ that we'd been using to keep it updated.
+
+2.2. Faster activations and timeouts
+
+ It's a common pattern in older code to use event_base_once() with a
+ 0-second timeout to ensure that a callback will get run 'as soon as
+ possible' in the current iteration of the Libevent loop. We optimize
+ this case by calling event_active() directly, and bypassing the
+ timeout pool. (People who are using this pattern should also consider
+ using event_active() themselves.)
+
+ Libevent 2.0 would wake up a polling event loop whenever the first
+ timeout in the event loop was adjusted--whether it had become earlier
+ or later. We now only notify the event loop when a change causes the
+ expiration time to become _sooner_ than it would have been otherwise.
+
+ The timeout heap code is now optimized to perform fewer comparisons
+ and shifts when changing or removing a timeout.
+
+ Instead of checking for a wall-clock time jump every time we call
+ clock_gettime(), we now check only every 5 seconds. This should save
+ a huge number of gettimeofday() calls.
+
+2.3. Microoptimizations
+
+ Internal event list maintainance no longer use the antipattern where
+ we have one function with multiple totally independent behaviors
+ depending on an argument:
+ #define OP1 1
+ #define OP2 2
+ #define OP3 3
+ void func(int operation, struct event *ev) {
+ switch (op) {
+ ...
+ }
+ }
+ Instead, these functions are now split into separate functions for
+ each operation:
+ void func_op1(struct event *ev) { ... }
+ void func_op2(struct event *ev) { ... }
+ void func_op3(struct event *ev) { ... }
+
+ This produces better code generation and inlining decisions on some
+ compilers, and makes the code easier to read and check.
+
+2.4. Evbuffer performance improvements
+
+ The EVBUFFER_EOL_CRLF line-ending type is now much faster, thanks to
+ smart optimizations.
+
+2.5. HTTP performance improvements
+
+ o Performance tweak to evhttp_parse_request_line. (aee1a97 Mark Ellzey)
+ o Add missing break to evhttp_parse_request_line (0fcc536)
+
+2.6. Coarse timers by default on Linux
+
+ Due to limitations of the epoll interface, Libevent programs using epoll
+ have not previously been able to wait for timeouts with accuracy smaller
+ than 1 millisecond. But Libevent had been using CLOCK_MONOTONIC for
+ timekeeping on Linux, which is needlessly expensive: CLOCK_MONOTONIC_COARSE
+ has approximately the resolution corresponding to epoll, and is much faster
+ to invoke than CLOCK_MONOTONIC.
+
+ To disable coarse timers, and get a more plausible precision, use the
+ new EVENT_BASE_FLAG_PRECISE_TIMER flag when setting up your event base.
+
+3. Backend/OS-specific improvements
+
+3.1. Linux-specific improvements
+
+ The logic for deciding which arguements to use with epoll_ctl() is now
+ a table-driven lookup, rather than the previous pile of cascading
+ branches. This should minimize epoll_ctl() calls and make the epoll
+ code run a little faster on change-heavy loads.
+
+ Libevent now takes advantage of Linux's support for enhanced APIs
+ (e.g., SOCK_CLOEXEC, SOCK_NONBLOCK, accept4, pipe2) that allow us to
+ simultaneously create a socket, make it nonblocking, and make it
+ close-on-exec. This should save syscalls throughout our codebase, and
+ avoid race-conditions if an exec() occurs after a socket is socket is
+ created but before we can make it close-on-execute on it.
+
+3.2. Windows-specific improvements
+
+ We now use GetSystemTimeAsFileTime to implement gettimeofday. It's
+ significantly faster and more accurate than our old ftime()-based approach.
+
+3.3. Improvements in the solaris evport backend.
+
+ The evport backend has been updated to use many of the infrastructure
+ improvements from Libevent 2.0. Notably, it keeps track of per-fd
+ information using the evmap infrastructure, and removes a number of
+ linear scans over recently-added events. This last change makes it
+ efficient to receive many more events per evport_getn() call, thereby
+ reducing evport overhead in general.
+
+3.4. OSX backend improvements
+
+ The OSX select backend doesn't like to have more than a certain number
+ of fds set unless an "unlimited select" option has been set.
+ Therefore, we now set it.
+
+3.5. Monotonic clocks on even more platforms
+
+ Libevent previously used a monotonic clock for its internal timekeeping
+ only on platforms supporting the POSIX clock_gettime() interface. Now,
+ Libevent has support for monotonic clocks on OSX and Windows too, and a
+ fallback implementation for systems without monotonic clocks that will at
+ least keep time running forwards.
+
+ Using monotonic timers makes Libevent more resilient to changes in the
+ system time, as can happen in small amounts due to clock adjustments from
+ NTP, or in large amounts due to users who move their system clocks all over
+ the timeline in order to keep nagware from nagging them.
+
+3.6. Faster cross-thread notification on kqueue
+
+ When a thread other than the one in which the main event loop is
+ running needs to wake the thread running the main event loop, Libevent
+ usually writes to a socketpair in order to force the main event loop
+ to wake up. On Linux, we've been able to use eventfd() instead. Now
+ on BSD and OSX systems (any anywhere else that has kqueue with the
+ EVFILT_USER extension), we can use EVFILT_USER to wake up the main
+ thread from kqueue. This should be a tiny bit faster than the
+ previous approach.
+
+4. Infrastructure improvements
+
+4.1. Faster tests
+
+ I've spent some time to try to make the unit tests run faster in
+ Libevent 2.1. Nearly all of this was a matter of searching slow tests
+ for unreasonably long timeouts, and cutting them down to reasonably
+ long delays, though on one or two cases I actually had to parallelize
+ an operation or improve an algorithm.
+
+ On my desktop, a full "make verify" run of Libevent 2.0.18-stable
+ requires about 218 seconds. Libevent 2.1.1-alpha cuts this down to
+ about 78 seconds.
+
+ Faster unit tests are great, since they let programmers test their
+ changes without losing their train of thought.
+
+4.2. Finicky tests are now off-by-default
+
+ The Tinytest unit testing framework now supports optional tests, and
+ Libevent uses them. By default, Libevent's unit testing framework
+ does not run tests that require a working network, and does not run
+ tests that tend to fail on heavily loaded systems because of timing
+ issues. To re-enable all tests, run ./test/regress using the "@all"
+ alias.
+
+4.3. Modernized use of autotools
+
+ Our autotools-based build system has been updated to build without
+ warnings on recent autoconf/automake versions.
+
+ Libevent's autotools makefiles are no longer recursive. This allows
+ make to use the maximum possible parallelism to do the minimally
+ necessary amount of work. See Peter Miller's "Recursive Make
+ Considered Harmful" at http://miller.emu.id.au/pmiller/books/rmch/ for
+ more information here.
+
+ We now use the "quiet build" option to suppress distracting messages
+ about which commandlines are running. You can get them back with
+ "make V=1".
+
+4.4. Portability
+
+ Libevent now uses large-file support internally on platforms where it
+ matters. You shouldn't need to set _LARGEFILE or OFFSET_BITS or
+ anything magic before including the Libevent headers, either, since
+ Libevent now sets the size of ev_off_t to the size of off_t that it
+ received at compile time, not to some (possibly different) size based
+ on current macro definitions when your program is building.
+
+ We now also use the Autoconf AC_USE_SYSTEM_EXTENSIONS mechanism to
+ enable per-system macros needed to enable not-on-by-default features.
+ Unlike the rest of the autoconf macros, we output these to an
+ internal-use-only evconfig-private.h header, since their names need to
+ survive unmangled. This lets us build correctly on more platforms,
+ and avoid inconsistencies when some files define _GNU_SOURCE and
+ others don't.
+
+ Libevent now tries to detect OpenSSL via pkg-config.
+
+4.5. Standards conformance
+
+ Previous Libevent versions had no consistent convention for internal
+ vs external identifiers, and used identifiers starting with the "_"
+ character throughout the codebase. That's no good, since the C
+ standard says that identifiers beginning with _ are reserved. I'm not
+ aware of having any collisions with system identifiers, but it's best
+ to fix these things before they cause trouble.
+
+ We now avoid all use of the _identifiers in the Libevent source code.
+ These changes were made *mainly* through the use of automated scripts,
+ so there shouldn't be any mistakes, but you never know.
+
+ As an exception, the names _EVENT_LOG_DEBUG, _EVENT_LOG_MSG_,
+ _EVENT_LOG_WARN, and _EVENT_LOG_ERR are still exposed in event.h: they
+ are now deprecated, but to support older code, they will need to stay
+ around for a while. New code should use EVENT_LOG_DEBUG,
+ EVENT_LOG_MSG, EVENT_LOG_WARN, and EVENT_LOG_ERR instead.
+
+4.6. Event and callback refactoring
+
+ As a simplification and optimization to Libevent's "deferred callback"
+ logic (introduced in 2.0 to avoid callback recursion), Libevent now
+ treats all of its deferrable callback types using the same logic it
+ uses for active events. Now deferred events no longer cause priority
+ inversion, no longer require special code to cancel them, and so on.
+
+ Regular events and deferred callbacks now both descend from an
+ internal light-weight event_callback supertype, and both support
+ priorities and take part in the other anti-priority-inversion
+ mechanisms in Libevent.
+
+ To avoid starvation from callback recursion (which was the reason we
+ introduced "deferred callbacks" in the first place) the implementation
+ now allows an event callback to be scheduled as "active later":
+ instead of running in the current iteration of the event loop, it runs
+ in the next one.
+
+5. Testing
+
+ Libevent's test coverage level is more or less unchanged since before:
+ we still have over 80% line coverage in our tests on Linux and OSX.
+ There are some under-tested modules, though: we need to fix those.
diff --git a/win32select.c b/win32select.c
index 1743b01e..a50a2df7 100644
--- a/win32select.c
+++ b/win32select.c
@@ -25,6 +25,10 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "event2/event-config.h"
+#include "evconfig-private.h"
+
+#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
@@ -38,7 +42,6 @@
#include <errno.h>
#include "event2/util.h"
-#include "event2/event-config.h"
#include "util-internal.h"
#include "log-internal.h"
#include "event2/event.h"
@@ -46,6 +49,7 @@
#include "evmap-internal.h"
#include "event2/thread.h"
#include "evthread-internal.h"
+#include "time-internal.h"
#define XFREE(ptr) do { if (ptr) mm_free(ptr); } while (0)
@@ -77,8 +81,8 @@ struct win32op {
};
static void *win32_init(struct event_base *);
-static int win32_add(struct event_base *, evutil_socket_t, short old, short events, void *_idx);
-static int win32_del(struct event_base *, evutil_socket_t, short old, short events, void *_idx);
+static int win32_add(struct event_base *, evutil_socket_t, short old, short events, void *idx_);
+static int win32_del(struct event_base *, evutil_socket_t, short old, short events, void *idx_);
static int win32_dispatch(struct event_base *base, struct timeval *);
static void win32_dealloc(struct event_base *);
@@ -160,7 +164,7 @@ do_fd_clear(struct event_base *base,
SOCKET s2;
s2 = set->fd_array[i] = set->fd_array[set->fd_count];
- ent2 = evmap_io_get_fdinfo(&base->io, s2);
+ ent2 = evmap_io_get_fdinfo_(&base->io, s2);
if (!ent2) /* This indicates a bug. */
return (0);
@@ -174,7 +178,7 @@ do_fd_clear(struct event_base *base,
#define NEVENT 32
void *
-win32_init(struct event_base *_base)
+win32_init(struct event_base *base)
{
struct win32op *winop;
size_t size;
@@ -196,9 +200,11 @@ win32_init(struct event_base *_base)
winop->readset_out->fd_count = winop->writeset_out->fd_count
= winop->exset_out->fd_count = 0;
- if (evsig_init(_base) < 0)
+ if (evsig_init_(base) < 0)
winop->signals_are_broken = 1;
+ evutil_weakrand_seed_(&base->weakrand_seed, 0);
+
return (winop);
err:
XFREE(winop->readset_in);
@@ -212,10 +218,10 @@ win32_init(struct event_base *_base)
int
win32_add(struct event_base *base, evutil_socket_t fd,
- short old, short events, void *_idx)
+ short old, short events, void *idx_)
{
struct win32op *win32op = base->evbase;
- struct idx_info *idx = _idx;
+ struct idx_info *idx = idx_;
if ((events & EV_SIGNAL) && win32op->signals_are_broken)
return (-1);
@@ -237,10 +243,10 @@ win32_add(struct event_base *base, evutil_socket_t fd,
int
win32_del(struct event_base *base, evutil_socket_t fd, short old, short events,
- void *_idx)
+ void *idx_)
{
struct win32op *win32op = base->evbase;
- struct idx_info *idx = _idx;
+ struct idx_info *idx = idx_;
event_debug(("%s: Removing event for "EV_SOCK_FMT,
__func__, EV_SOCK_ARG(fd)));
@@ -299,7 +305,7 @@ win32_dispatch(struct event_base *base, struct timeval *tv)
win32op->readset_out->fd_count : win32op->writeset_out->fd_count;
if (!fd_count) {
- long msec = tv ? evutil_tv_to_msec(tv) : LONG_MAX;
+ long msec = tv ? evutil_tv_to_msec_(tv) : LONG_MAX;
/* Sleep's DWORD argument is unsigned long */
if (msec < 0)
msec = LONG_MAX;
@@ -324,42 +330,45 @@ win32_dispatch(struct event_base *base, struct timeval *tv)
}
if (win32op->readset_out->fd_count) {
- i = rand() % win32op->readset_out->fd_count;
+ i = evutil_weakrand_range_(&base->weakrand_seed,
+ win32op->readset_out->fd_count);
for (j=0; j<win32op->readset_out->fd_count; ++j) {
if (++i >= win32op->readset_out->fd_count)
i = 0;
s = win32op->readset_out->fd_array[i];
- evmap_io_active(base, s, EV_READ);
+ evmap_io_active_(base, s, EV_READ);
}
}
if (win32op->exset_out->fd_count) {
- i = rand() % win32op->exset_out->fd_count;
+ i = evutil_weakrand_range_(&base->weakrand_seed,
+ win32op->exset_out->fd_count);
for (j=0; j<win32op->exset_out->fd_count; ++j) {
if (++i >= win32op->exset_out->fd_count)
i = 0;
s = win32op->exset_out->fd_array[i];
- evmap_io_active(base, s, EV_WRITE);
+ evmap_io_active_(base, s, EV_WRITE);
}
}
if (win32op->writeset_out->fd_count) {
SOCKET s;
- i = rand() % win32op->writeset_out->fd_count;
+ i = evutil_weakrand_range_(&base->weakrand_seed,
+ win32op->writeset_out->fd_count);
for (j=0; j<win32op->writeset_out->fd_count; ++j) {
if (++i >= win32op->writeset_out->fd_count)
i = 0;
s = win32op->writeset_out->fd_array[i];
- evmap_io_active(base, s, EV_WRITE);
+ evmap_io_active_(base, s, EV_WRITE);
}
}
return (0);
}
void
-win32_dealloc(struct event_base *_base)
+win32_dealloc(struct event_base *base)
{
- struct win32op *win32op = _base->evbase;
+ struct win32op *win32op = base->evbase;
- evsig_dealloc(_base);
+ evsig_dealloc_(base);
if (win32op->readset_in)
mm_free(win32op->readset_in);
if (win32op->writeset_in)
@@ -375,3 +384,5 @@ win32_dealloc(struct event_base *_base)
memset(win32op, 0, sizeof(*win32op));
mm_free(win32op);
}
+
+#endif