server: import the duel server (nanahira/ygopro server at 52c868ce)
The server tree of https://code.moenext.com/nanahira/ygopro.git, branch server, commit 52c868ce071ea0f2fcb0cf39bf2fb4151d12b90c, stock: gframe/, premake5.lua, .ci/, LICENSE, and gframe/ygopro.rc + .ico (upstream keeps them in resource/ and copies them in before building). No game assets, no scripts, no ocgcore submodule: the core is ../core. The third-party sources upstream's Windows CI downloads are vendored here at the positions its premake expects, each with its premake fragment from upstream's premake/ folder inside it: lua 5.4.8, sqlite 3.53.3, libevent 2.1.13-stable (tests, samples and autotools helpers left out; the files its Windows prebuild copies into include/ left out), zlib 1.3.2 (contrib and examples left out), xz 5.8.3 (src/liblzma + src/common only), and irrlicht 1.9 from mycard at 20f86d25: include/ plus the source/Irrlicht headers and the 11 .cpp files the server's zip reader project compiles. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
26f9baf9b4
commit
da27435cdb
1032 files changed
+746904
No files matched your search
@@ -0,0 +1,6 @@
|
||||
export ASSET_BRANCH_NAME="master"
|
||||
|
||||
# if $CI_COMMIT_REF_NAME includes develop or pre, then we use the develop branch
|
||||
if [[ "$CI_COMMIT_REF_NAME" == *"develop"* || "$CI_COMMIT_REF_NAME" == *".pre"* ]]; then
|
||||
export ASSET_BRANCH_NAME="develop"
|
||||
fi
|
||||
@@ -0,0 +1,18 @@
|
||||
#!/bin/bash
|
||||
set -x
|
||||
set -o errexit
|
||||
source .ci/asset-branch
|
||||
# ygopro-database
|
||||
apt update && apt -y install wget git libarchive-tools sqlite3
|
||||
git clone --depth=1 -b master https://code.moenext.com/nanahira/ygopro-database
|
||||
cp -rf ./ygopro-database/locales/$TARGET_LOCALE/strings.conf .
|
||||
cp -rf ./ygopro-database/locales/$TARGET_LOCALE/servers.conf .
|
||||
rm -f cards.cdb
|
||||
sqlite3 ./ygopro-database/locales/$TARGET_LOCALE/cards.cdb .dump | sqlite3 cards.cdb
|
||||
# ygopro-images
|
||||
mkdir pics
|
||||
if [[ "$ASSET_BRANCH_NAME" == "develop" ]]; then
|
||||
echo "This is a pre-release, skipping download."
|
||||
else
|
||||
wget -O - https://cdn02.moecube.com:444/images/ygopro-images-${TARGET_LOCALE}.zip | bsdtar -C pics -xf -
|
||||
fi
|
||||
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
libs_to_configure=(
|
||||
"ogg"
|
||||
# "opus"
|
||||
)
|
||||
|
||||
cd miniaudio/external
|
||||
for lib in "${libs_to_configure[@]}"; do
|
||||
cd $lib
|
||||
chmod +x configure || true
|
||||
./configure
|
||||
cd ..
|
||||
done
|
||||
@@ -0,0 +1,41 @@
|
||||
#!/bin/sh
|
||||
set -x
|
||||
set -o errexit
|
||||
|
||||
SCRIPT_DIR="$(CDPATH= cd "$(dirname "$0")" && pwd)"
|
||||
PROJECT_DIR="$(CDPATH= cd "$SCRIPT_DIR/.." && pwd)"
|
||||
|
||||
cd "$PROJECT_DIR/event"
|
||||
chmod +x configure build-aux/* || true
|
||||
|
||||
if [ -n "${NDK_DIR:-}" ]; then
|
||||
ANDROID_API_LEVEL="${ANDROID_API_LEVEL:-26}"
|
||||
case "$NDK_DIR" in
|
||||
/*)
|
||||
NDK_DIR_PATH="$NDK_DIR"
|
||||
;;
|
||||
*)
|
||||
NDK_DIR_PATH="$PROJECT_DIR/$NDK_DIR"
|
||||
;;
|
||||
esac
|
||||
NDK_DIR_ABS="$(cd "$NDK_DIR_PATH" && pwd)"
|
||||
NDK_PREBUILT_DIR="$(find "$NDK_DIR_ABS/toolchains/llvm/prebuilt" -mindepth 1 -maxdepth 1 -type d | sort | head -n 1)"
|
||||
if [ -z "$NDK_PREBUILT_DIR" ]; then
|
||||
echo "Android NDK toolchain not found under $NDK_DIR_ABS/toolchains/llvm/prebuilt" >&2
|
||||
exit 1
|
||||
fi
|
||||
NDK_BIN_DIR="$NDK_PREBUILT_DIR/bin"
|
||||
export CC="$NDK_BIN_DIR/clang --target=aarch64-linux-android$ANDROID_API_LEVEL"
|
||||
export CXX="$NDK_BIN_DIR/clang++ --target=aarch64-linux-android$ANDROID_API_LEVEL"
|
||||
export AR="$NDK_BIN_DIR/llvm-ar"
|
||||
export RANLIB="$NDK_BIN_DIR/llvm-ranlib"
|
||||
./configure \
|
||||
--host=aarch64-linux-android \
|
||||
--disable-openssl \
|
||||
--enable-static=yes \
|
||||
--enable-shared=no
|
||||
else
|
||||
./configure --disable-openssl --enable-static=yes --enable-shared=no
|
||||
fi
|
||||
|
||||
sed -f make-event-config.sed < config.h > ./include/event2/event-config.h
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
set -x
|
||||
set -o errexit
|
||||
|
||||
TARGET_PLATFORM=$(arch)
|
||||
TARGET_YGOPRO_BINARY_PATH=./ygopro-platforms/ygopro-platform-$TARGET_PLATFORM
|
||||
|
||||
./.ci/configure-audio.sh
|
||||
|
||||
rm -rf sqlite3/VERSION sqlite3/version
|
||||
|
||||
./premake5 gmake --cc=clang
|
||||
|
||||
cd build
|
||||
make config=release -j$(sysctl -n hw.ncpu)
|
||||
cd ..
|
||||
|
||||
mkdir ygopro-platforms
|
||||
mv bin/release/YGOPro $TARGET_YGOPRO_BINARY_PATH
|
||||
|
||||
#if [[ $TARGET_PLATFORM == "x86" ]]; then
|
||||
# install_name_tool -change /usr/local/lib/libirrklang.dylib @executable_path/../Frameworks/libirrklang.dylib $TARGET_YGOPRO_BINARY_PATH
|
||||
#fi
|
||||
|
||||
strip $TARGET_YGOPRO_BINARY_PATH
|
||||
@@ -0,0 +1,327 @@
|
||||
#!/bin/bash
|
||||
set -o errexit
|
||||
set -o nounset
|
||||
set -o pipefail
|
||||
set -x
|
||||
|
||||
_prepare_pack_dirs() {
|
||||
mkdir -p dist replay
|
||||
}
|
||||
|
||||
_apply_locale_overlay() {
|
||||
cp -rf "locales/$TARGET_LOCALE/"* .
|
||||
}
|
||||
|
||||
_collect_existing_files() {
|
||||
local existing=()
|
||||
local path
|
||||
for path in "$@"; do
|
||||
if [[ -e "$path" ]]; then
|
||||
existing+=("$path")
|
||||
fi
|
||||
done
|
||||
|
||||
printf '%s\n' "${existing[@]}"
|
||||
}
|
||||
|
||||
_append_files() {
|
||||
local array_name="$1"
|
||||
shift
|
||||
local value
|
||||
for value in "$@"; do
|
||||
eval "$array_name+=(\"\$value\")"
|
||||
done
|
||||
}
|
||||
|
||||
_remove_files() {
|
||||
local array_name="$1"
|
||||
shift
|
||||
local removals=("$@")
|
||||
local filtered=()
|
||||
local value
|
||||
local removal
|
||||
local skip
|
||||
|
||||
eval "local current=(\"\${${array_name}[@]}\")"
|
||||
for value in "${current[@]}"; do
|
||||
skip=0
|
||||
for removal in "${removals[@]}"; do
|
||||
if [[ "$value" == "$removal" ]]; then
|
||||
skip=1
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [[ "$skip" -eq 0 ]]; then
|
||||
filtered+=("$value")
|
||||
fi
|
||||
done
|
||||
|
||||
eval "$array_name=(\"\${filtered[@]}\")"
|
||||
}
|
||||
|
||||
_pack_tar_archive() {
|
||||
local archive_name="$1"
|
||||
shift
|
||||
|
||||
local files
|
||||
_prepare_pack_dirs
|
||||
mapfile -t files < <(_collect_existing_files "$@")
|
||||
tar -acf "dist/${archive_name}" --exclude='.git*' "${files[@]}"
|
||||
}
|
||||
|
||||
_get_cpu_count() {
|
||||
local cpu_count
|
||||
cpu_count=$(nproc 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)
|
||||
if [[ ! "$cpu_count" =~ ^[0-9]+$ || "$cpu_count" -lt 1 ]]; then
|
||||
cpu_count=1
|
||||
fi
|
||||
|
||||
echo "$cpu_count"
|
||||
}
|
||||
|
||||
_get_memory_limit_kib() {
|
||||
local mem_total_kib
|
||||
mem_total_kib=$(awk '/MemTotal:/ {print $2}' /proc/meminfo)
|
||||
|
||||
local limit_kib="$mem_total_kib"
|
||||
local cgroup_limit
|
||||
local cgroup_limit_kib
|
||||
if [[ -r /sys/fs/cgroup/memory.max ]]; then
|
||||
cgroup_limit=$(cat /sys/fs/cgroup/memory.max)
|
||||
if [[ "$cgroup_limit" != "max" && "$cgroup_limit" =~ ^[0-9]+$ ]]; then
|
||||
cgroup_limit_kib=$((cgroup_limit / 1024))
|
||||
if (( cgroup_limit_kib > 0 && cgroup_limit_kib < limit_kib )); then
|
||||
limit_kib="$cgroup_limit_kib"
|
||||
fi
|
||||
fi
|
||||
elif [[ -r /sys/fs/cgroup/memory/memory.limit_in_bytes ]]; then
|
||||
cgroup_limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes)
|
||||
if [[ "$cgroup_limit" =~ ^[0-9]+$ ]]; then
|
||||
cgroup_limit_kib=$((cgroup_limit / 1024))
|
||||
if (( cgroup_limit_kib > 0 && cgroup_limit_kib < limit_kib )); then
|
||||
limit_kib="$cgroup_limit_kib"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "$limit_kib"
|
||||
}
|
||||
|
||||
_parse_7z_dict_mib() {
|
||||
local value="${1,,}"
|
||||
local number
|
||||
local unit
|
||||
if [[ ! "$value" =~ ^([0-9]+)([kmg]?)b?$ ]]; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
number="${BASH_REMATCH[1]}"
|
||||
unit="${BASH_REMATCH[2]}"
|
||||
case "$unit" in
|
||||
k)
|
||||
echo $(((number + 1023) / 1024))
|
||||
;;
|
||||
g)
|
||||
echo $((number * 1024))
|
||||
;;
|
||||
*)
|
||||
echo "$number"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
_estimate_7z_memory_kib() {
|
||||
local threads="$1"
|
||||
local dict_mib="$2"
|
||||
|
||||
echo $(((128 + threads * dict_mib * 12) * 1024))
|
||||
}
|
||||
|
||||
_choose_7z_settings() {
|
||||
local cpu_count
|
||||
local memory_limit_kib
|
||||
local memory_budget_kib
|
||||
local max_threads
|
||||
local threads
|
||||
local dict_mib
|
||||
local estimated_kib
|
||||
local dict_candidates=(64 32 16 8)
|
||||
|
||||
cpu_count=$(_get_cpu_count)
|
||||
max_threads=$((cpu_count / 2))
|
||||
if (( max_threads < 1 )); then
|
||||
max_threads=1
|
||||
fi
|
||||
|
||||
threads="$max_threads"
|
||||
if [[ -n "${PACK_7Z_THREADS:-}" ]]; then
|
||||
if [[ ! "$PACK_7Z_THREADS" =~ ^[0-9]+$ || "$PACK_7Z_THREADS" -lt 1 ]]; then
|
||||
echo "Invalid PACK_7Z_THREADS: $PACK_7Z_THREADS" >&2
|
||||
exit 1
|
||||
fi
|
||||
threads="$PACK_7Z_THREADS"
|
||||
if (( threads > max_threads )); then
|
||||
echo "Capping PACK_7Z_THREADS=${threads} to half of available CPUs: ${max_threads}" >&2
|
||||
threads="$max_threads"
|
||||
fi
|
||||
fi
|
||||
|
||||
memory_limit_kib=$(_get_memory_limit_kib)
|
||||
memory_budget_kib=$((memory_limit_kib / 2))
|
||||
|
||||
if [[ -n "${PACK_7Z_DICT_SIZE:-}" ]]; then
|
||||
dict_mib=$(_parse_7z_dict_mib "$PACK_7Z_DICT_SIZE") || {
|
||||
echo "Invalid PACK_7Z_DICT_SIZE: $PACK_7Z_DICT_SIZE" >&2
|
||||
exit 1
|
||||
}
|
||||
while (( threads > 1 )); do
|
||||
estimated_kib=$(_estimate_7z_memory_kib "$threads" "$dict_mib")
|
||||
if (( estimated_kib <= memory_budget_kib )); then
|
||||
break
|
||||
fi
|
||||
threads=$((threads - 1))
|
||||
done
|
||||
|
||||
estimated_kib=$(_estimate_7z_memory_kib "$threads" "$dict_mib")
|
||||
if (( estimated_kib > memory_budget_kib )); then
|
||||
echo "PACK_7Z_DICT_SIZE=${PACK_7Z_DICT_SIZE} does not fit in half of available memory" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
printf '%s %sm\n' "$threads" "$dict_mib"
|
||||
return
|
||||
fi
|
||||
|
||||
while true; do
|
||||
for dict_mib in "${dict_candidates[@]}"; do
|
||||
estimated_kib=$(_estimate_7z_memory_kib "$threads" "$dict_mib")
|
||||
if (( estimated_kib <= memory_budget_kib )); then
|
||||
printf '%s %sm\n' "$threads" "$dict_mib"
|
||||
return
|
||||
fi
|
||||
done
|
||||
|
||||
if (( threads <= 1 )); then
|
||||
printf '1 8m\n'
|
||||
return
|
||||
fi
|
||||
threads=$((threads - 1))
|
||||
done
|
||||
}
|
||||
|
||||
_pack_7z_archive() {
|
||||
local archive_name="$1"
|
||||
shift
|
||||
|
||||
local files
|
||||
local dict_size
|
||||
local threads
|
||||
|
||||
_prepare_pack_dirs
|
||||
mapfile -t files < <(_collect_existing_files "$@")
|
||||
read -r threads dict_size < <(_choose_7z_settings)
|
||||
echo "Running 7z with -mx9, -md=${dict_size}, -mmt=${threads}"
|
||||
7z a -m0=lzma2 -mx9 "-md=${dict_size}" "-mmt=${threads}" -xr!.git* "dist/${archive_name}" "${files[@]}"
|
||||
}
|
||||
|
||||
_append_base_files() {
|
||||
local array_name="$1"
|
||||
_append_files "$array_name" \
|
||||
strings.conf system.conf servers.conf \
|
||||
cards.cdb textures locales fonts pack skin
|
||||
}
|
||||
|
||||
_append_dlc_files() {
|
||||
local array_name="$1"
|
||||
_append_base_files "$array_name"
|
||||
_append_files "$array_name" sound
|
||||
}
|
||||
|
||||
_append_release_files() {
|
||||
local array_name="$1"
|
||||
_append_dlc_files "$array_name"
|
||||
_append_files "$array_name" \
|
||||
LICENSE README.md lflist.conf \
|
||||
script deck single pics replay bot.conf
|
||||
}
|
||||
|
||||
_append_win_locale_updater() {
|
||||
local array_name="$1"
|
||||
local archive_kind="$2"
|
||||
if [[ "$TARGET_LOCALE" != "zh-CN" ]]; then
|
||||
return
|
||||
fi
|
||||
|
||||
if [[ "$archive_kind" == "7z" || "$ARCHIVE_SUFFIX" != "zst" ]]; then
|
||||
_append_files "$array_name" update-koishipro
|
||||
fi
|
||||
}
|
||||
|
||||
_append_platform_files() {
|
||||
local array_name="$1"
|
||||
local package_kind="$2"
|
||||
local target_platform="$3"
|
||||
|
||||
case "$target_platform" in
|
||||
darwin)
|
||||
_append_files "$array_name" ygopro.app
|
||||
if [[ "$package_kind" == "release" ]]; then
|
||||
_append_files "$array_name" windbot bot
|
||||
fi
|
||||
;;
|
||||
linux|linuxarm)
|
||||
_append_files "$array_name" ygopro
|
||||
if [[ "$package_kind" == "release" ]]; then
|
||||
_append_files "$array_name" windbot bot
|
||||
fi
|
||||
;;
|
||||
win32)
|
||||
_append_files "$array_name" ygopro.exe vcomp140.dll
|
||||
if [[ "$package_kind" == "release" ]]; then
|
||||
_append_files "$array_name" Bot.exe WindBot
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
echo "Unsupported platform: $target_platform" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
_pack_release() {
|
||||
local default_platform="$1"
|
||||
local target_platform="${TARGET_PLATFORM:-$default_platform}"
|
||||
local archive_name="KoishiPro-${CI_COMMIT_REF_NAME}-${target_platform}-${TARGET_LOCALE}.tar.${ARCHIVE_SUFFIX}"
|
||||
local files=()
|
||||
|
||||
_append_release_files files
|
||||
_append_platform_files files release "$target_platform"
|
||||
_append_win_locale_updater files tar
|
||||
|
||||
_pack_tar_archive "$archive_name" "${files[@]}"
|
||||
}
|
||||
|
||||
_pack_dlc() {
|
||||
local default_platform="$1"
|
||||
local target_platform="${TARGET_PLATFORM:-$default_platform}"
|
||||
local archive_name="KoishiPro-dlc-${CI_COMMIT_REF_NAME}-${target_platform}-${TARGET_LOCALE}.tar.${ARCHIVE_SUFFIX}"
|
||||
local files=()
|
||||
|
||||
_apply_locale_overlay
|
||||
_append_dlc_files files
|
||||
_append_platform_files files dlc "$target_platform"
|
||||
|
||||
_pack_tar_archive "$archive_name" "${files[@]}"
|
||||
}
|
||||
|
||||
_pack_7z() {
|
||||
local target_platform="${TARGET_PLATFORM:-win32}"
|
||||
local archive_name="KoishiPro-${CI_COMMIT_REF_NAME}-${target_platform}-${TARGET_LOCALE}.7z"
|
||||
local files=()
|
||||
|
||||
_append_release_files files
|
||||
_append_platform_files files release "$target_platform"
|
||||
_append_win_locale_updater files 7z
|
||||
|
||||
_pack_7z_archive "$archive_name" "${files[@]}"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
set -o errexit
|
||||
set -x
|
||||
|
||||
source .ci/pack-common
|
||||
|
||||
_pack_dlc linux
|
||||
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
set -o errexit
|
||||
set -x
|
||||
|
||||
source .ci/pack-common
|
||||
|
||||
_pack_release linux
|
||||
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
set -o errexit
|
||||
set -x
|
||||
|
||||
source .ci/pack-common
|
||||
|
||||
_pack_dlc darwin
|
||||
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
set -o errexit
|
||||
set -x
|
||||
|
||||
source .ci/pack-common
|
||||
|
||||
_pack_release darwin
|
||||
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
set -o errexit
|
||||
set -x
|
||||
|
||||
source .ci/pack-common
|
||||
|
||||
_pack_7z
|
||||
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
set -o errexit
|
||||
set -x
|
||||
|
||||
source .ci/pack-common
|
||||
|
||||
_pack_dlc win32
|
||||
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
set -o errexit
|
||||
set -x
|
||||
|
||||
source .ci/pack-common
|
||||
|
||||
_pack_release win32
|
||||
@@ -0,0 +1,6 @@
|
||||
#!/bin/bash
|
||||
set -x
|
||||
set -o errexit
|
||||
|
||||
source .ci/prepare-repo
|
||||
prepare_repo "https://code.moenext.com/mycard/irrlicht-1.9.git" "irrlicht"
|
||||
@@ -0,0 +1,34 @@
|
||||
#!/bin/bash
|
||||
set -x
|
||||
set -o errexit
|
||||
|
||||
MINIAUDIO_REPO="https://github.com/mackron/miniaudio"
|
||||
MINIAUDIO_REF="0.11.25"
|
||||
|
||||
if [ ! -d "miniaudio/.git" ]; then
|
||||
rm -rf miniaudio
|
||||
git clone --depth=1 -b "$MINIAUDIO_REF" "$MINIAUDIO_REPO" miniaudio
|
||||
else
|
||||
git -C miniaudio remote set-url origin "$MINIAUDIO_REPO"
|
||||
git -C miniaudio fetch --depth=1 origin "refs/tags/$MINIAUDIO_REF"
|
||||
git -C miniaudio checkout -f FETCH_HEAD
|
||||
fi
|
||||
|
||||
cp -rf miniaudio/extras/miniaudio_split/miniaudio.* miniaudio/
|
||||
|
||||
mkdir -p miniaudio/external
|
||||
|
||||
install_external() {
|
||||
dir="$1"
|
||||
url="$2"
|
||||
if [ ! -d "miniaudio/external/$dir" ]; then
|
||||
mkdir -p "miniaudio/external/$dir"
|
||||
# Download the external library and strip one level of directories from the archive
|
||||
wget -O - "$url" | tar --strip-components=1 -C "miniaudio/external/$dir" -zxf -
|
||||
fi
|
||||
}
|
||||
|
||||
install_external "ogg" "https://mat-cacher.moenext.com/https://github.com/xiph/ogg/releases/download/v1.3.6/libogg-1.3.6.tar.gz"
|
||||
install_external "opus" "https://mat-cacher.moenext.com/https://github.com/xiph/opus/releases/download/v1.5.2/opus-1.5.2.tar.gz"
|
||||
install_external "opusfile" "https://mat-cacher.moenext.com/https://github.com/xiph/opusfile/releases/download/v0.12/opusfile-0.12.tar.gz"
|
||||
install_external "vorbis" "https://mat-cacher.moenext.com/https://github.com/xiph/vorbis/releases/download/v1.3.7/libvorbis-1.3.7.tar.gz"
|
||||
@@ -0,0 +1,20 @@
|
||||
source .ci/asset-branch
|
||||
BRANCH_NAME="$ASSET_BRANCH_NAME"
|
||||
|
||||
prepare_repo() {
|
||||
REPO_URL="$1"
|
||||
TARGET_DIR_NAME="$2"
|
||||
|
||||
if [ ! -d "$TARGET_DIR_NAME" ]; then
|
||||
echo "Cloning repository $REPO_URL branch $BRANCH_NAME into $TARGET_DIR_NAME"
|
||||
git clone --depth=1 --branch "$BRANCH_NAME" "$REPO_URL" "$TARGET_DIR_NAME"
|
||||
else
|
||||
echo "Repository $REPO_URL already exists in $TARGET_DIR_NAME, updating to $BRANCH_NAME..."
|
||||
cd "$TARGET_DIR_NAME"
|
||||
git remote set-url origin "$REPO_URL"
|
||||
git fetch origin "$BRANCH_NAME"
|
||||
git checkout "$BRANCH_NAME"
|
||||
git reset --hard origin/"$BRANCH_NAME"
|
||||
cd ..
|
||||
fi
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
set -x
|
||||
set -o errexit
|
||||
|
||||
echo "$MACOS_CERTIFICATE" | base64 --decode --output cert.p12
|
||||
|
||||
security create-keychain -p "$MACOS_KEYCHAIN_PASSWORD" "$MACOS_KEYCHAIN_NAME" || true
|
||||
security list-keychains -s login.keychain "$MACOS_KEYCHAIN_NAME"
|
||||
security default-keychain -s "$MACOS_KEYCHAIN_NAME"
|
||||
security unlock-keychain -p "$MACOS_KEYCHAIN_PASSWORD" "$MACOS_KEYCHAIN_NAME"
|
||||
security import cert.p12 -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
|
||||
security set-key-partition-list -S apple-tool:,apple: -s -k "$MACOS_KEYCHAIN_PASSWORD" "$MACOS_KEYCHAIN_NAME";
|
||||
|
||||
CERT_IDENTITY_NAME=$(security find-identity -v -p codesigning $MACOS_KEYCHAIN_NAME | head -1 | grep -o '".*"' | tr -d '"')
|
||||
/usr/bin/codesign --deep --sign "$CERT_IDENTITY_NAME" ygopro.app
|
||||
|
||||
security lock-keychain "$MACOS_KEYCHAIN_NAME"
|
||||
@@ -0,0 +1,47 @@
|
||||
#!/bin/bash
|
||||
set -o errexit
|
||||
|
||||
apt update; apt -y install curl jq
|
||||
|
||||
apiRoot=https://api.moecube.com
|
||||
|
||||
loginInfo=$(curl -sL -X POST $apiRoot/accounts/signin -d account=$MYCARD_USERNAME -d password=$MYCARD_PASSWORD)
|
||||
token=$(echo $loginInfo | jq '.token' | sed 's/"//g')
|
||||
header="Authorization: $token"
|
||||
echo "Login succeeded."
|
||||
|
||||
appName="koishipro"
|
||||
|
||||
handleErrorMessage() {
|
||||
rawJsonInput="$1"
|
||||
successInfo=$(echo "$rawJsonInput" | jq '.success')
|
||||
|
||||
if [[ "$successInfo" != "true" ]]; then
|
||||
echo "$rawJsonInput"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
runForDepot() {
|
||||
platform=$1
|
||||
locale=$2
|
||||
archivePath="./dist/KoishiPro-dlc-$appVersion-$platform-$locale.tar.zst"
|
||||
suffix="?platform=$platform&locale=$locale&arch=generic"
|
||||
echo "Uploading $archivePath"
|
||||
result=$(curl -H "$header" -X POST "$apiRoot/release/api/build/$appName/${appVersion}${suffix}" -F file=@$archivePath)
|
||||
handleErrorMessage "$result"
|
||||
echo "$result" | jq .
|
||||
}
|
||||
|
||||
source .ci/asset-branch
|
||||
|
||||
if [[ "$ASSET_BRANCH_NAME" == "develop" ]]; then
|
||||
echo "This is a pre-release, skipping upload."
|
||||
else
|
||||
runForDepot win32 zh-CN
|
||||
runForDepot linux zh-CN
|
||||
runForDepot darwin zh-CN
|
||||
runForDepot win32 en-US
|
||||
runForDepot linux en-US
|
||||
runForDepot darwin en-US
|
||||
fi
|
||||
+339
@@ -0,0 +1,339 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Lesser General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program is interactive, make it output a short notice like this
|
||||
when it starts in an interactive mode:
|
||||
|
||||
Gnomovision version 69, Copyright (C) year name of author
|
||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, the commands you use may
|
||||
be called something other than `show w' and `show c'; they could even be
|
||||
mouse-clicks or menu items--whatever suits your program.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1989
|
||||
Ty Coon, President of Vice
|
||||
|
||||
This General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may
|
||||
consider it more useful to permit linking proprietary applications with the
|
||||
library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License.
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -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-warnings 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.
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,257 @@
|
||||
# Doxyfile 1.5.1
|
||||
|
||||
# This file describes the settings to be used by the documentation system
|
||||
# doxygen (www.doxygen.org) for a project
|
||||
#
|
||||
# All text after a hash (#) is considered a comment and will be ignored
|
||||
# The format is:
|
||||
# TAG = value [value, ...]
|
||||
# For lists items can also be appended using:
|
||||
# TAG += value [value, ...]
|
||||
# Values that contain spaces should be placed between quotes (" ")
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# Project related configuration options
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
# The PROJECT_NAME tag is a single word (or a sequence of words surrounded
|
||||
# by quotes) that should identify the project.
|
||||
|
||||
PROJECT_NAME = $(PROJECT)-$(VERSION)
|
||||
|
||||
# Place all output under 'doxygen/'
|
||||
|
||||
OUTPUT_DIRECTORY = $(DOCDIR)
|
||||
|
||||
# If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen
|
||||
# will interpret the first line (until the first dot) of a JavaDoc-style
|
||||
# comment as the brief description. If set to NO, the JavaDoc
|
||||
# comments will behave just like the Qt-style comments (thus requiring an
|
||||
# explicit @brief command for a brief description.
|
||||
|
||||
JAVADOC_AUTOBRIEF = YES
|
||||
|
||||
# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C
|
||||
# sources only. Doxygen will then generate output that is more tailored for C.
|
||||
# For instance, some of the names that are used will be different. The list
|
||||
# of all members will be omitted, etc.
|
||||
|
||||
OPTIMIZE_OUTPUT_FOR_C = YES
|
||||
|
||||
# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the
|
||||
# brief documentation of file, namespace and class members alphabetically
|
||||
# by member name. If set to NO (the default) the members will appear in
|
||||
# declaration order.
|
||||
|
||||
SORT_BRIEF_DOCS = YES
|
||||
|
||||
# If the FULL_PATH_NAMES tag is set to YES then the STRIP_FROM_PATH tag
|
||||
# can be used to strip a user-defined part of the path. Stripping is
|
||||
# only done if one of the specified strings matches the left-hand part of
|
||||
# the path. The tag can be used to show relative paths in the file list.
|
||||
# If left blank the directory from which doxygen is run is used as the
|
||||
# path to strip.
|
||||
|
||||
STRIP_FROM_PATH = include/
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options related to the input files
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
# The INPUT tag can be used to specify the files and/or directories that contain
|
||||
# documented source files. You may enter file names like "myfile.cpp" or
|
||||
# directories like "/usr/src/myproject". Separate the files or directories
|
||||
# with spaces.
|
||||
|
||||
INPUT = \
|
||||
$(SRCDIR)/include/event2/buffer.h \
|
||||
$(SRCDIR)/include/event2/buffer_compat.h \
|
||||
$(SRCDIR)/include/event2/bufferevent.h \
|
||||
$(SRCDIR)/include/event2/bufferevent_compat.h \
|
||||
$(SRCDIR)/include/event2/bufferevent_ssl.h \
|
||||
$(SRCDIR)/include/event2/dns.h \
|
||||
$(SRCDIR)/include/event2/dns_compat.h \
|
||||
$(SRCDIR)/include/event2/event.h \
|
||||
$(SRCDIR)/include/event2/event_compat.h \
|
||||
$(SRCDIR)/include/event2/http.h \
|
||||
$(SRCDIR)/include/event2/http_compat.h \
|
||||
$(SRCDIR)/include/event2/listener.h \
|
||||
$(SRCDIR)/include/event2/rpc.h \
|
||||
$(SRCDIR)/include/event2/rpc_compat.h \
|
||||
$(SRCDIR)/include/event2/tag.h \
|
||||
$(SRCDIR)/include/event2/tag_compat.h \
|
||||
$(SRCDIR)/include/event2/thread.h \
|
||||
$(SRCDIR)/include/event2/util.h
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options related to the HTML output
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
# If the GENERATE_HTML tag is set to YES (the default) Doxygen will
|
||||
# generate HTML output.
|
||||
|
||||
GENERATE_HTML = $(GENERATE_HTML)
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options related to the LaTeX output
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
# If the GENERATE_LATEX tag is set to YES (the default) Doxygen will
|
||||
# generate Latex output.
|
||||
|
||||
GENERATE_LATEX = $(GENERATE_LATEX)
|
||||
|
||||
# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put.
|
||||
# If a relative path is entered the value of OUTPUT_DIRECTORY will be
|
||||
# put in front of it. If left blank `latex' will be used as the default path.
|
||||
|
||||
LATEX_OUTPUT = latex
|
||||
|
||||
# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be
|
||||
# invoked. If left blank `latex' will be used as the default command name.
|
||||
|
||||
LATEX_CMD_NAME = latex
|
||||
|
||||
# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to
|
||||
# generate index for LaTeX. If left blank `makeindex' will be used as the
|
||||
# default command name.
|
||||
|
||||
MAKEINDEX_CMD_NAME = makeindex
|
||||
|
||||
# If the COMPACT_LATEX tag is set to YES Doxygen generates more compact
|
||||
# LaTeX documents. This may be useful for small projects and may help to
|
||||
# save some trees in general.
|
||||
|
||||
COMPACT_LATEX = NO
|
||||
|
||||
# The PAPER_TYPE tag can be used to set the paper type that is used
|
||||
# by the printer. Possible values are: a4, a4wide, letter, legal and
|
||||
# executive. If left blank a4wide will be used.
|
||||
|
||||
PAPER_TYPE = a4wide
|
||||
|
||||
# The EXTRA_PACKAGES tag can be to specify one or more names of LaTeX
|
||||
# packages that should be included in the LaTeX output.
|
||||
|
||||
EXTRA_PACKAGES =
|
||||
|
||||
# The LATEX_HEADER tag can be used to specify a personal LaTeX header for
|
||||
# the generated latex document. The header should contain everything until
|
||||
# the first chapter. If it is left blank doxygen will generate a
|
||||
# standard header. Notice: only use this tag if you know what you are doing!
|
||||
|
||||
LATEX_HEADER =
|
||||
|
||||
# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated
|
||||
# is prepared for conversion to pdf (using ps2pdf). The pdf file will
|
||||
# contain links (just like the HTML output) instead of page references
|
||||
# This makes the output suitable for online browsing using a pdf viewer.
|
||||
|
||||
PDF_HYPERLINKS = NO
|
||||
|
||||
# If the USE_PDFLATEX tag is set to YES, pdflatex will be used instead of
|
||||
# plain latex in the generated Makefile. Set this option to YES to get a
|
||||
# higher quality PDF documentation.
|
||||
|
||||
USE_PDFLATEX = YES
|
||||
|
||||
# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \\batchmode.
|
||||
# command to the generated LaTeX files. This will instruct LaTeX to keep
|
||||
# running if errors occur, instead of asking the user for help.
|
||||
# This option is also used when generating formulas in HTML.
|
||||
|
||||
LATEX_BATCHMODE = NO
|
||||
|
||||
# If LATEX_HIDE_INDICES is set to YES then doxygen will not
|
||||
# include the index chapters (such as File Index, Compound Index, etc.)
|
||||
# in the output.
|
||||
|
||||
LATEX_HIDE_INDICES = NO
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# configuration options related to the man page output
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
# If the GENERATE_MAN tag is set to YES (the default) Doxygen will
|
||||
# generate man pages
|
||||
|
||||
GENERATE_MAN = $(GENERATE_MAN)
|
||||
|
||||
# The MAN_EXTENSION tag determines the extension that is added to
|
||||
# the generated man pages (default is the subroutine's section .3)
|
||||
|
||||
MAN_EXTENSION = .3
|
||||
|
||||
# If the MAN_LINKS tag is set to YES and Doxygen generates man output,
|
||||
# then it will generate one additional man file for each entity
|
||||
# documented in the real man page(s). These additional files
|
||||
# only source the real man page, but without them the man command
|
||||
# would be unable to find the correct page. The default is NO.
|
||||
|
||||
MAN_LINKS = YES
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
# Configuration options related to the preprocessor
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
# If the ENABLE_PREPROCESSING tag is set to YES (the default) Doxygen will
|
||||
# evaluate all C-preprocessor directives found in the sources and include
|
||||
# files.
|
||||
|
||||
ENABLE_PREPROCESSING = YES
|
||||
|
||||
# If the MACRO_EXPANSION tag is set to YES Doxygen will expand all macro
|
||||
# names in the source code. If set to NO (the default) only conditional
|
||||
# compilation will be performed. Macro expansion can be done in a controlled
|
||||
# way by setting EXPAND_ONLY_PREDEF to YES.
|
||||
|
||||
MACRO_EXPANSION = NO
|
||||
|
||||
# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES
|
||||
# then the macro expansion is limited to the macros specified with the
|
||||
# PREDEFINED and EXPAND_AS_DEFINED tags.
|
||||
|
||||
EXPAND_ONLY_PREDEF = NO
|
||||
|
||||
# If the SEARCH_INCLUDES tag is set to YES (the default) the includes files
|
||||
# in the INCLUDE_PATH (see below) will be search if a #include is found.
|
||||
|
||||
SEARCH_INCLUDES = YES
|
||||
|
||||
# The INCLUDE_PATH tag can be used to specify one or more directories that
|
||||
# contain include files that are not input files but should be processed by
|
||||
# the preprocessor.
|
||||
|
||||
INCLUDE_PATH =
|
||||
|
||||
# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard
|
||||
# patterns (like *.h and *.hpp) to filter out the header-files in the
|
||||
# directories. If left blank, the patterns specified with FILE_PATTERNS will
|
||||
# be used.
|
||||
|
||||
INCLUDE_FILE_PATTERNS =
|
||||
|
||||
# The PREDEFINED tag can be used to specify one or more macro names that
|
||||
# are defined before the preprocessor is started (similar to the -D option of
|
||||
# gcc). The argument of the tag is a list of macros of the form: name
|
||||
# or name=definition (no spaces). If the definition and the = are
|
||||
# omitted =1 is assumed. To prevent a macro definition from being
|
||||
# undefined via #undef or recursively expanded use the := operator
|
||||
# instead of the = operator.
|
||||
|
||||
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.
|
||||
# The macro definition that is found in the sources will be used.
|
||||
# Use the PREDEFINED tag if you want to use a different macro definition.
|
||||
|
||||
EXPAND_AS_DEFINED =
|
||||
|
||||
# If the SKIP_FUNCTION_MACROS tag is set to YES (the default) then
|
||||
# doxygen's preprocessor will remove all function-like macros that are alone
|
||||
# on a line, have an all uppercase name, and do not end with a semicolon. Such
|
||||
# function macros are typically used for boiler-plate code, and will confuse
|
||||
# the parser if not removed.
|
||||
|
||||
SKIP_FUNCTION_MACROS = YES
|
||||
@@ -0,0 +1,99 @@
|
||||
Libevent is available for use under the following license, commonly known
|
||||
as the 3-clause (or "modified") BSD license:
|
||||
|
||||
==============================
|
||||
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.
|
||||
==============================
|
||||
|
||||
Portions of Libevent are based on works by others, also made available by
|
||||
them under the three-clause BSD license above. The copyright notices are
|
||||
available in the corresponding source files; the license is as above. Here's
|
||||
a list:
|
||||
|
||||
log.c:
|
||||
Copyright (c) 2000 Dug Song <dugsong@monkey.org>
|
||||
Copyright (c) 1993 The Regents of the University of California.
|
||||
|
||||
strlcpy.c:
|
||||
Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
|
||||
win32select.c:
|
||||
Copyright (c) 2003 Michael A. Davis <mike@datanerds.net>
|
||||
|
||||
evport.c:
|
||||
Copyright (c) 2007 Sun Microsystems
|
||||
|
||||
ht-internal.h:
|
||||
Copyright (c) 2002 Christopher Clark
|
||||
|
||||
minheap-internal.h:
|
||||
Copyright (c) 2006 Maxim Yegorushkin <maxim.yegorushkin@gmail.com>
|
||||
|
||||
==============================
|
||||
|
||||
The arc4module is available under the following, sometimes called the
|
||||
"OpenBSD" license:
|
||||
|
||||
Copyright (c) 1996, David Mazieres <dm@uun.org>
|
||||
Copyright (c) 2008, Damien Miller <djm@openbsd.org>
|
||||
|
||||
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" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
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.
|
||||
@@ -0,0 +1,345 @@
|
||||
# Makefile.am for libevent
|
||||
# Copyright 2000-2007 Niels Provos
|
||||
# Copyright 2007-2012 Niels Provos and Nick Mathewson
|
||||
#
|
||||
# See LICENSE for copying information.
|
||||
|
||||
# 'foreign' means that we're not enforcing GNU package rules strictly.
|
||||
# '1.13' means that we need automake 1.13 or later (and we do).
|
||||
AUTOMAKE_OPTIONS = foreign 1.13 subdir-objects
|
||||
|
||||
ACLOCAL_AMFLAGS = -I m4
|
||||
|
||||
# This is the "Release" of the Libevent ABI. It takes precedence over
|
||||
# the VERSION_INFO, so that two versions of Libevent with the same
|
||||
# "Release" are never binary-compatible.
|
||||
#
|
||||
# This number incremented once for the 2.0 release candidate, and
|
||||
# will increment for each series until we revise our interfaces enough
|
||||
# that we can seriously expect ABI compatibility between series.
|
||||
#
|
||||
RELEASE = -release 2.1
|
||||
|
||||
# This is the version info for the libevent binary API. It has three
|
||||
# numbers:
|
||||
# Current -- the number of the binary API that we're implementing
|
||||
# Revision -- which iteration of the implementation of the binary
|
||||
# API are we supplying?
|
||||
# Age -- How many previous binary API versions do we also
|
||||
# support?
|
||||
#
|
||||
# To increment a VERSION_INFO (current:revision:age):
|
||||
# If the ABI didn't change:
|
||||
# Return (current:revision+1:age)
|
||||
# If the ABI changed, but it's backward-compatible:
|
||||
# Return (current+1:0:age+1)
|
||||
# If the ABI changed and it isn't backward-compatible:
|
||||
# Return (current+1:0:0)
|
||||
#
|
||||
# Once an RC is out, DO NOT MAKE ANY ABI-BREAKING CHANGES IN THAT SERIES
|
||||
# UNLESS YOU REALLY REALLY HAVE TO.
|
||||
VERSION_INFO = 7:2:0
|
||||
|
||||
# History: RELEASE VERSION_INFO
|
||||
# 2.0.1-alpha -- 2.0 1:0:0
|
||||
# 2.0.2-alpha -- 2:0:0
|
||||
# 2.0.3-alpha -- 2:0:0 (should have incremented; didn't.)
|
||||
# 2.0.4-alpha -- 3:0:0
|
||||
# 2.0.5-beta -- 4:0:0
|
||||
# 2.0.6-rc -- 2.0 2:0:0
|
||||
# 2.0.7-rc -- 2.0 3:0:1
|
||||
# 2.0.8-rc -- 2.0 4:0:2
|
||||
# 2.0.9-rc -- 2.0 5:0:0 (ABI changed slightly)
|
||||
# 2.0.10-stable-- 2.0 5:1:0 (No ABI change)
|
||||
# 2.0.11-stable-- 2.0 6:0:1 (ABI changed, backward-compatible)
|
||||
# 2.0.12-stable-- 2.0 6:1:1 (No ABI change)
|
||||
# 2.0.13-stable-- 2.0 6:2:1 (No ABI change)
|
||||
# 2.0.14-stable-- 2.0 6:3:1 (No ABI change)
|
||||
# 2.0.15-stable-- 2.0 6:3:1 (Forgot to update :( )
|
||||
# 2.0.16-stable-- 2.0 6:4:1 (No ABI change)
|
||||
# 2.0.17-stable-- 2.0 6:5:1 (No ABI change)
|
||||
# 2.0.18-stable-- 2.0 6:6:1 (No ABI change)
|
||||
# 2.0.19-stable-- 2.0 6:7:1 (No ABI change)
|
||||
# 2.0.20-stable-- 2.0 6:8:1 (No ABI change)
|
||||
# 2.0.21-stable-- 2.0 6:9:1 (No ABI change)
|
||||
#
|
||||
# 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)
|
||||
# 2.1.5-beta -- 2.1 5:0:0 (ABI changed slightly)
|
||||
# 2.1.6-beta -- 2.1 6:0:0 (ABI changed slightly)
|
||||
# 2.1.7-beta -- 2.1 6:1:0 (ABI changed slightly)
|
||||
# 2.1.8-stable-- 2.1 6:2:0 (No ABI change)
|
||||
# 2.1.9-beta-- 2.1 6:3:0 (No ABI change)
|
||||
# 2.1.10-stable-- 2.1 6:4:0 (No ABI change, WRONG)
|
||||
# 2.1.11-stable-- 2.1 7:0:0 (ABI changed)
|
||||
# 2.1.12-stable-- 2.1 7:1:0 (No ABI change)
|
||||
# 2.1.13-stable-- 2.1 7:2:0 (No ABI change)
|
||||
|
||||
# ABI version history for this package effectively restarts every time
|
||||
# we change RELEASE. Version 1.4.x had RELEASE of 1.4.
|
||||
#
|
||||
# Ideally, we would not be using RELEASE at all; instead we could just
|
||||
# use the VERSION_INFO field to label our backward-incompatible ABI
|
||||
# changes, and those would be few and far between. Unfortunately,
|
||||
# Libevent still exposes far too many volatile structures in its
|
||||
# headers, so we pretty much have to assume that most development
|
||||
# series will break ABI compatibility. For now, it's simplest just to
|
||||
# keep incrementing the RELEASE between series and resetting VERSION_INFO.
|
||||
#
|
||||
# Eventually, when we get to the point where the structures in the
|
||||
# headers are all non-changing (or not there at all!), we can shift to
|
||||
# a more normal worldview where backward-incompatible ABI changes are
|
||||
# nice and rare. For the next couple of years, though, 'struct event'
|
||||
# is user-visible, and so we can pretty much guarantee that release
|
||||
# series won't be binary-compatible.
|
||||
|
||||
if INSTALL_LIBEVENT
|
||||
dist_bin_SCRIPTS = event_rpcgen.py
|
||||
endif
|
||||
|
||||
pkgconfigdir=$(libdir)/pkgconfig
|
||||
LIBEVENT_PKGCONFIG=libevent.pc libevent_core.pc libevent_extra.pc
|
||||
|
||||
# These sources are conditionally added by configure.ac or conditionally
|
||||
# included from other files.
|
||||
PLATFORM_DEPENDENT_SRC = \
|
||||
arc4random.c \
|
||||
epoll_sub.c
|
||||
|
||||
CMAKE_FILES = \
|
||||
cmake/AddCompilerFlags.cmake \
|
||||
cmake/AddEventLibrary.cmake \
|
||||
cmake/CheckConstExists.cmake \
|
||||
cmake/CheckFileOffsetBits.c \
|
||||
cmake/CheckFileOffsetBits.cmake \
|
||||
cmake/CheckFunctionKeywords.cmake \
|
||||
cmake/CheckPrototypeDefinition.c.in \
|
||||
cmake/CheckPrototypeDefinition.cmake \
|
||||
cmake/CheckWorkingKqueue.cmake \
|
||||
cmake/CodeCoverage.cmake \
|
||||
cmake/COPYING-CMAKE-SCRIPTS \
|
||||
cmake/Copyright.txt \
|
||||
cmake/LibeventConfig.cmake.in \
|
||||
cmake/LibeventConfigVersion.cmake.in \
|
||||
cmake/Macros.cmake \
|
||||
cmake/Uninstall.cmake.in \
|
||||
cmake/UseDoxygen.cmake \
|
||||
cmake/VersionViaGit.cmake \
|
||||
event-config.h.cmake \
|
||||
evconfig-private.h.cmake \
|
||||
CMakeLists.txt
|
||||
|
||||
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 \
|
||||
whatsnew-2.0.txt \
|
||||
whatsnew-2.1.txt \
|
||||
README.md \
|
||||
$(CMAKE_FILES) \
|
||||
$(PLATFORM_DEPENDENT_SRC)
|
||||
|
||||
LIBEVENT_LIBS_LA = libevent.la libevent_core.la libevent_extra.la
|
||||
if PTHREADS
|
||||
LIBEVENT_LIBS_LA += libevent_pthreads.la
|
||||
LIBEVENT_PKGCONFIG += libevent_pthreads.pc
|
||||
endif
|
||||
if OPENSSL
|
||||
LIBEVENT_LIBS_LA += libevent_openssl.la
|
||||
LIBEVENT_PKGCONFIG += libevent_openssl.pc
|
||||
endif
|
||||
|
||||
if INSTALL_LIBEVENT
|
||||
lib_LTLIBRARIES = $(LIBEVENT_LIBS_LA)
|
||||
pkgconfig_DATA = $(LIBEVENT_PKGCONFIG)
|
||||
else
|
||||
noinst_LTLIBRARIES = $(LIBEVENT_LIBS_LA)
|
||||
endif
|
||||
|
||||
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_CORE_LIBS = -liphlpapi
|
||||
SYS_LIBS = -lws2_32 -lshell32 -ladvapi32
|
||||
SYS_SRC = win32select.c buffer_iocp.c event_iocp.c \
|
||||
bufferevent_async.c
|
||||
SYS_INCLUDES = -IWIN32-Code -IWIN32-Code/nmake
|
||||
|
||||
if THREADS
|
||||
SYS_SRC += evthread_win32.c
|
||||
endif
|
||||
|
||||
else
|
||||
|
||||
SYS_CORE_LIBS =
|
||||
SYS_LIBS =
|
||||
SYS_SRC =
|
||||
SYS_INCLUDES =
|
||||
|
||||
endif
|
||||
|
||||
if STRLCPY_IMPL
|
||||
SYS_SRC += strlcpy.c
|
||||
endif
|
||||
if SELECT_BACKEND
|
||||
SYS_SRC += select.c
|
||||
endif
|
||||
if POLL_BACKEND
|
||||
SYS_SRC += poll.c
|
||||
endif
|
||||
if DEVPOLL_BACKEND
|
||||
SYS_SRC += devpoll.c
|
||||
endif
|
||||
if KQUEUE_BACKEND
|
||||
SYS_SRC += kqueue.c
|
||||
endif
|
||||
if EPOLL_BACKEND
|
||||
SYS_SRC += epoll.c
|
||||
endif
|
||||
if EVPORT_BACKEND
|
||||
SYS_SRC += evport.c
|
||||
endif
|
||||
if SIGNAL_SUPPORT
|
||||
SYS_SRC += signal.c
|
||||
endif
|
||||
|
||||
BUILT_SOURCES += include/event2/event-config.h
|
||||
|
||||
include/event2/event-config.h: config.h make-event-config.sed
|
||||
$(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
|
||||
MAYBE_CORE = libevent_core.la
|
||||
else
|
||||
NO_UNDEFINED =
|
||||
MAYBE_CORE =
|
||||
endif
|
||||
|
||||
AM_CFLAGS = $(LIBEVENT_CFLAGS)
|
||||
AM_CPPFLAGS = -I$(srcdir)/compat -I./include -I$(srcdir)/include $(SYS_INCLUDES) $(LIBEVENT_CPPFLAGS)
|
||||
AM_LDFLAGS = $(LIBEVENT_LDFLAGS)
|
||||
|
||||
GENERIC_LDFLAGS = -version-info $(VERSION_INFO) $(RELEASE) $(NO_UNDEFINED) $(AM_LDFLAGS)
|
||||
|
||||
libevent_la_SOURCES = $(CORE_SRC) $(EXTRAS_SRC)
|
||||
libevent_la_LIBADD = @LTLIBOBJS@ $(SYS_LIBS) $(SYS_CORE_LIBS)
|
||||
libevent_la_LDFLAGS = $(GENERIC_LDFLAGS)
|
||||
|
||||
libevent_core_la_SOURCES = $(CORE_SRC)
|
||||
libevent_core_la_LIBADD = @LTLIBOBJS@ $(SYS_LIBS) $(SYS_CORE_LIBS)
|
||||
libevent_core_la_LDFLAGS = $(GENERIC_LDFLAGS)
|
||||
|
||||
if PTHREADS
|
||||
libevent_pthreads_la_SOURCES = evthread_pthread.c
|
||||
libevent_pthreads_la_LIBADD = $(MAYBE_CORE)
|
||||
libevent_pthreads_la_LDFLAGS = $(GENERIC_LDFLAGS)
|
||||
endif
|
||||
|
||||
libevent_extra_la_SOURCES = $(EXTRAS_SRC)
|
||||
libevent_extra_la_LIBADD = $(MAYBE_CORE) $(SYS_LIBS)
|
||||
libevent_extra_la_LDFLAGS = $(GENERIC_LDFLAGS)
|
||||
|
||||
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 += \
|
||||
WIN32-Code/nmake/evconfig-private.h \
|
||||
WIN32-Code/nmake/event2/event-config.h \
|
||||
WIN32-Code/getopt.h \
|
||||
WIN32-Code/getopt.c \
|
||||
WIN32-Code/getopt_long.c \
|
||||
WIN32-Code/tree.h \
|
||||
bufferevent-internal.h \
|
||||
changelist-internal.h \
|
||||
compat/sys/queue.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 \
|
||||
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 \
|
||||
openssl-compat.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)
|
||||
else
|
||||
noinst_HEADERS += $(EVENT1_HDRS)
|
||||
endif
|
||||
|
||||
verify: check
|
||||
|
||||
include doxygen.am
|
||||
|
||||
DISTCLEANFILES += *~ libevent.pc libevent_core.pc libevent_extra.pc ./include/event2/event-config.h
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,82 @@
|
||||
# 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 /IWIN32-Code/nmake /Iinclude /Icompat /DHAVE_CONFIG_H /I. $(SSL_CFLAGS)
|
||||
|
||||
# For optimization and warnings
|
||||
CFLAGS=$(CFLAGS) /Ox /W3 /wd4996 /nologo
|
||||
|
||||
# XXXX have a debug mode
|
||||
|
||||
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 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
|
||||
|
||||
!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
|
||||
|
||||
static_libs: $(STATIC_LIBS)
|
||||
|
||||
libevent_core.lib: $(CORE_OBJS) $(WIN_OBJS)
|
||||
lib $(LIBFLAGS) $(CORE_OBJS) $(WIN_OBJS) /out:libevent_core.lib
|
||||
|
||||
libevent_extras.lib: $(EXTRA_OBJS)
|
||||
lib $(LIBFLAGS) $(EXTRA_OBJS) /out:libevent_extras.lib
|
||||
|
||||
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 ..
|
||||
@@ -0,0 +1,487 @@
|
||||
<p align="center">
|
||||
<img src="https://strcpy.net/libevent3.png" alt="libevent logo"/>
|
||||
</p>
|
||||
|
||||
|
||||
|
||||
[](https://ci.appveyor.com/project/libevent/libevent)
|
||||
[](https://travis-ci.org/libevent/libevent)
|
||||
[](https://coveralls.io/github/libevent/libevent)
|
||||
[](https://gitter.im/libevent/libevent?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
|
||||
|
||||
|
||||
# 0. BUILDING AND INSTALLATION (Briefly)
|
||||
|
||||
## Autoconf
|
||||
|
||||
$ ./configure
|
||||
$ make
|
||||
$ make verify # (optional)
|
||||
$ sudo make install
|
||||
|
||||
## CMake (General)
|
||||
|
||||
|
||||
The following Libevent specific CMake variables are as follows (the values being
|
||||
the default).
|
||||
|
||||
```
|
||||
# Type of the library to build (SHARED or STATIC)
|
||||
# Default is: SHARED for MSVC, otherwise BOTH
|
||||
EVENT__LIBRARY_TYPE:STRING=DEFAULT
|
||||
|
||||
# Installation directory for CMake files
|
||||
EVENT_INSTALL_CMAKE_DIR:PATH=lib/cmake/libevent
|
||||
|
||||
# Enable running gcov to get a test coverage report (only works with
|
||||
# GCC/CLang). Make sure to enable -DCMAKE_BUILD_TYPE=Debug as well.
|
||||
EVENT__COVERAGE:BOOL=OFF
|
||||
|
||||
# Defines if Libevent should build without the benchmark executables
|
||||
EVENT__DISABLE_BENCHMARK:BOOL=OFF
|
||||
|
||||
# Define if Libevent should build without support for a debug mode
|
||||
EVENT__DISABLE_DEBUG_MODE:BOOL=OFF
|
||||
|
||||
# Define if Libevent should not allow replacing the mm functions
|
||||
EVENT__DISABLE_MM_REPLACEMENT:BOOL=OFF
|
||||
|
||||
# Define if Libevent should build without support for OpenSSL encryption
|
||||
EVENT__DISABLE_OPENSSL:BOOL=OFF
|
||||
|
||||
# Disable the regress tests
|
||||
EVENT__DISABLE_REGRESS:BOOL=OFF
|
||||
|
||||
# Disable sample files
|
||||
EVENT__DISABLE_SAMPLES:BOOL=OFF
|
||||
|
||||
# If tests should be compiled or not
|
||||
EVENT__DISABLE_TESTS:BOOL=OFF
|
||||
|
||||
# Define if Libevent should not be compiled with thread support
|
||||
EVENT__DISABLE_THREAD_SUPPORT:BOOL=OFF
|
||||
|
||||
# Enables verbose debugging
|
||||
EVENT__ENABLE_VERBOSE_DEBUG:BOOL=OFF
|
||||
|
||||
# When cross compiling, 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
|
||||
EVENT__FORCE_KQUEUE_CHECK:BOOL=OFF
|
||||
```
|
||||
|
||||
__More variables can be found by running `cmake -LAH <sourcedir_path>`__
|
||||
|
||||
|
||||
## CMake (Windows)
|
||||
|
||||
Install CMake: <https://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 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
|
||||
environment 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 <https://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.
|
||||
|
||||
# 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 https://github.com/libevent/libevent.git
|
||||
```
|
||||
|
||||
You can browse the git repository online at:
|
||||
|
||||
<https://github.com/libevent/libevent>
|
||||
|
||||
To report bugs, issues, or ask for new features:
|
||||
|
||||
__Patches__: https://github.com/libevent/libevent/pulls
|
||||
> OK, those are not really _patches_. You fork, modify, and hit the "Create Pull Request" button.
|
||||
> You can still submit normal git patches via the mailing list.
|
||||
|
||||
__Bugs, Features [RFC], and Issues__: https://github.com/libevent/libevent/issues
|
||||
> Or you can do it via the mailing list.
|
||||
|
||||
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
|
||||
* 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
|
||||
* Andrey Skriabin
|
||||
* basavesh.as
|
||||
* billsegall
|
||||
* Bill Vaughan
|
||||
* Christopher Wiley
|
||||
* David Paschich
|
||||
* Ed Schouten
|
||||
* Eduardo Panisset
|
||||
* Jan Heylen
|
||||
* jer-gentoo
|
||||
* Joakim Söderberg
|
||||
* kirillDanshin
|
||||
* lzmths
|
||||
* Marcus Sundberg
|
||||
* Mark Mentovai
|
||||
* Mattes D
|
||||
* Matyas Dolak
|
||||
* Neeraj Badlani
|
||||
* Nick Mathewson
|
||||
* Rainer Keller
|
||||
* Seungmo Koo
|
||||
* Thomas Bernard
|
||||
* Xiao Bao Clark
|
||||
* zeliard
|
||||
* Zonr Chang
|
||||
* Kurt Roeckx
|
||||
* Seven
|
||||
* Simone Basso
|
||||
* Vlad Shcherban
|
||||
* Tim Hentenaar
|
||||
* Breaker
|
||||
* johnsonlee
|
||||
* Philip Prindeville
|
||||
* Vis Virial
|
||||
* Andreas Gustafsson
|
||||
* Andrey Okoshkin
|
||||
* an-tao
|
||||
* baixiangcpp
|
||||
* Bernard Spil
|
||||
* Bogdan Harjoc
|
||||
* Carlo Marcelo Arenas Belón
|
||||
* David Benjamin
|
||||
* David Disseldorp
|
||||
* Dmitry Alimov
|
||||
* Dominic Chen
|
||||
* dpayne
|
||||
* ejurgensen
|
||||
* Fredrik Strupe
|
||||
* Gonçalo Ribeiro
|
||||
* James Synge
|
||||
* Jan Beich
|
||||
* Jesse Fang
|
||||
* Jiri Luznicky
|
||||
* José Luis Millán
|
||||
* Kiyoshi Aman
|
||||
* Leo Zhang
|
||||
* lightningkay
|
||||
* Luke Dashjr
|
||||
* Marcin Szewczyk
|
||||
* Maximilian Brunner
|
||||
* Maya Rashish
|
||||
* Murat Demirten
|
||||
* Nathan French
|
||||
* Nikolay Edigaryev
|
||||
* Philip Herron
|
||||
* Redfoxmoon
|
||||
* stenn
|
||||
* SuckShit
|
||||
* The Gitter Badger
|
||||
* tim-le
|
||||
* Vincent JARDIN
|
||||
* Xiang Zhang
|
||||
* Xiaozhou Liu
|
||||
* yongqing.jiao
|
||||
* Enji Cooper
|
||||
* linxiaohui
|
||||
* Seong-Joong Kim
|
||||
* Tobias Stoeckmann
|
||||
* Yury Korzhetsky
|
||||
* zhuizhuhaomeng
|
||||
* Pierce Lopez
|
||||
* yuangongji
|
||||
* Keith Smiley
|
||||
* jeremyerb
|
||||
* Fabrice Fontaine
|
||||
* wenyg
|
||||
* Aleksandr-Melnikov
|
||||
* ayuseleznev
|
||||
* chenguolong
|
||||
* Dimo Markov
|
||||
* dota17
|
||||
* fanquake
|
||||
* Jan Kasiak
|
||||
* Kamil Rytarowski
|
||||
* Mario Emmenlauer
|
||||
* Michael Davidsaver
|
||||
* mohuang
|
||||
* Nick Grifka
|
||||
* Nicolas J. Bouliane
|
||||
* Paul Osborne
|
||||
* Philip Homburg
|
||||
* Wataru Ashihara
|
||||
* William A Rowe Jr
|
||||
* yangyongsheng
|
||||
|
||||
|
||||
If we have forgotten your name, please contact us.
|
||||
@@ -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 */
|
||||
}
|
||||
|
||||
@@ -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__ */
|
||||
@@ -0,0 +1,234 @@
|
||||
|
||||
/*
|
||||
* 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, match = -1;
|
||||
size_t current_argv_len;
|
||||
|
||||
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) == 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);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#if !defined(EVENT_EVCONFIG__PRIVATE_H_) && !defined(__MINGW32__)
|
||||
#define EVENT_EVCONFIG__PRIVATE_H_
|
||||
|
||||
/* Nothing to see here. Move along. */
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,350 @@
|
||||
/* event2/event-config.h
|
||||
*
|
||||
* This file was generated by autoconf when libevent was built, and post-
|
||||
* processed by Libevent so that its macros would have a uniform prefix.
|
||||
*
|
||||
* DO NOT EDIT THIS FILE.
|
||||
*
|
||||
* Do not rely on macros in this file existing in later versions.
|
||||
*/
|
||||
#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 */
|
||||
|
||||
/* Define if libevent should not be compiled with 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 to 1 if you have the <arpa/inet.h> header file. */
|
||||
/* #undef EVENT__HAVE_ARPA_INET_H */
|
||||
|
||||
/* Define to 1 if you have the `clock_gettime' function. */
|
||||
/* #undef EVENT__HAVE_CLOCK_GETTIME */
|
||||
|
||||
/* Define if /dev/poll is available */
|
||||
/* #undef EVENT__HAVE_DEVPOLL */
|
||||
|
||||
/* Define to 1 if you have the <dlfcn.h> header file. */
|
||||
/* #undef EVENT__HAVE_DLFCN_H */
|
||||
|
||||
/* Define if your system supports the epoll system calls */
|
||||
/* #undef EVENT__HAVE_EPOLL */
|
||||
|
||||
/* Define to 1 if you have the `epoll_ctl' function. */
|
||||
/* #undef EVENT__HAVE_EPOLL_CTL */
|
||||
|
||||
/* Define to 1 if you have the `eventfd' function. */
|
||||
/* #undef EVENT__HAVE_EVENTFD */
|
||||
|
||||
/* Define if your system supports event ports */
|
||||
/* #undef EVENT__HAVE_EVENT_PORTS */
|
||||
|
||||
/* Define to 1 if you have the `fcntl' function. */
|
||||
/* #undef EVENT__HAVE_FCNTL */
|
||||
|
||||
/* Define to 1 if you have the <fcntl.h> header file. */
|
||||
#define EVENT__HAVE_FCNTL_H 1
|
||||
|
||||
/* Define to 1 if you have the `getaddrinfo' function. */
|
||||
#define EVENT__HAVE_GETADDRINFO 1
|
||||
|
||||
/* Define to 1 if you have the `getnameinfo' function. */
|
||||
#define EVENT__HAVE_GETNAMEINFO 1
|
||||
|
||||
/* Define to 1 if you have the `getprotobynumber' function. */
|
||||
#define EVENT__HAVE_GETPROTOBYNUMBER 1
|
||||
|
||||
/* Define to 1 if you have the `getservbyname' function. */
|
||||
#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_ntop' function. */
|
||||
/* #undef EVENT__HAVE_INET_NTOP */
|
||||
|
||||
/* Define to 1 if you have the `inet_pton' function. */
|
||||
/* #undef EVENT__HAVE_INET_PTON */
|
||||
|
||||
/* Define to 1 if you have the <inttypes.h> header file. */
|
||||
/* #define EVENT__HAVE_INTTYPES_H 1 */
|
||||
|
||||
/* Define to 1 if you have the `kqueue' function. */
|
||||
/* #undef EVENT__HAVE_KQUEUE */
|
||||
|
||||
/* Define if the system has zlib */
|
||||
/* #undef EVENT__HAVE_LIBZ */
|
||||
|
||||
/* Define to 1 if you have the <memory.h> header file. */
|
||||
#define EVENT__HAVE_MEMORY_H 1
|
||||
|
||||
/* Define to 1 if you have the `mmap' function. */
|
||||
/* #undef EVENT__HAVE_MMAP */
|
||||
|
||||
/* Define to 1 if you have the <netinet/in6.h> header file. */
|
||||
/* #undef EVENT__HAVE_NETINET_IN6_H */
|
||||
|
||||
/* Define to 1 if you have the <netinet/in.h> header file. */
|
||||
/* #undef EVENT__HAVE_NETINET_IN_H */
|
||||
|
||||
/* Define to 1 if you have the `pipe' function. */
|
||||
/* #undef EVENT__HAVE_PIPE */
|
||||
|
||||
/* Define to 1 if you have the `poll' function. */
|
||||
/* #undef EVENT__HAVE_POLL */
|
||||
|
||||
/* Define to 1 if you have the <poll.h> header file. */
|
||||
/* #undef EVENT__HAVE_POLL_H */
|
||||
|
||||
/* Define to 1 if you have the `port_create' function. */
|
||||
/* #undef EVENT__HAVE_PORT_CREATE */
|
||||
|
||||
/* Define to 1 if you have the <port.h> header file. */
|
||||
/* #undef EVENT__HAVE_PORT_H */
|
||||
|
||||
/* Define if you have POSIX threads libraries and header files. */
|
||||
/* #undef EVENT__HAVE_PTHREAD */
|
||||
|
||||
/* Define if we have pthreads on this system */
|
||||
/* #undef EVENT__HAVE_PTHREADS */
|
||||
|
||||
/* Define to 1 if the system has the type `sa_family_t'. */
|
||||
/* #undef EVENT__HAVE_SA_FAMILY_T */
|
||||
|
||||
/* Define to 1 if you have the `select' function. */
|
||||
/* #undef EVENT__HAVE_SELECT */
|
||||
|
||||
/* Define to 1 if you have the `sendfile' function. */
|
||||
/* #undef EVENT__HAVE_SENDFILE */
|
||||
|
||||
/* Define if F_SETFD is defined in <fcntl.h> */
|
||||
/* #undef EVENT__HAVE_SETFD */
|
||||
|
||||
/* Define to 1 if you have the `sigaction' function. */
|
||||
/* #undef EVENT__HAVE_SIGACTION */
|
||||
|
||||
/* Define to 1 if you have the `signal' function. */
|
||||
#define EVENT__HAVE_SIGNAL 1
|
||||
|
||||
/* Define to 1 if you have the `splice' function. */
|
||||
/* #undef EVENT__HAVE_SPLICE */
|
||||
|
||||
/* Define to 1 if you have the <stdarg.h> header file. */
|
||||
#define EVENT__HAVE_STDARG_H 1
|
||||
|
||||
/* Define to 1 if you have the <stddef.h> header file. */
|
||||
#define EVENT__HAVE_STDDEF_H 1
|
||||
|
||||
/* Define to 1 if you have the <stdint.h> header file. */
|
||||
/* #define EVENT__HAVE_STDINT_H 1 */
|
||||
|
||||
/* Define to 1 if you have the <stdlib.h> header file. */
|
||||
#define EVENT__HAVE_STDLIB_H 1
|
||||
|
||||
/* Define to 1 if you have the <strings.h> header file. */
|
||||
#define EVENT__HAVE_STRINGS_H 1
|
||||
|
||||
/* Define to 1 if you have the <string.h> header file. */
|
||||
#define EVENT__HAVE_STRING_H 1
|
||||
|
||||
/* Define to 1 if you have the `strlcpy' function. */
|
||||
/* #undef EVENT__HAVE_STRLCPY */
|
||||
|
||||
/* Define to 1 if you have the `strsep' function. */
|
||||
/* #undef EVENT__HAVE_STRSEP */
|
||||
|
||||
/* Define to 1 if you have the `strtok_r' function. */
|
||||
/* #undef EVENT__HAVE_STRTOK_R */
|
||||
|
||||
/* Define to 1 if you have the `strtoll' function. */
|
||||
/* #define EVENT__HAVE_STRTOLL 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 to 1 if `s6_addr16' is member of `struct in6_addr'. */
|
||||
#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 to 1 if the system has the type `struct sockaddr_in6'. */
|
||||
#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 */
|
||||
|
||||
/* Define to 1 if `sin_len' is member of `struct sockaddr_in'. */
|
||||
/* #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 to 1 if you have the <sys/epoll.h> header file. */
|
||||
/* #undef EVENT__HAVE_SYS_EPOLL_H */
|
||||
|
||||
/* Define to 1 if you have the <sys/eventfd.h> header file. */
|
||||
/* #undef EVENT__HAVE_SYS_EVENTFD_H */
|
||||
|
||||
/* Define to 1 if you have the <sys/event.h> header file. */
|
||||
/* #undef EVENT__HAVE_SYS_EVENT_H */
|
||||
|
||||
/* Define to 1 if you have the <sys/ioctl.h> header file. */
|
||||
/* #undef EVENT__HAVE_SYS_IOCTL_H */
|
||||
|
||||
/* Define to 1 if you have the <sys/mman.h> header file. */
|
||||
/* #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 to 1 if you have the <sys/queue.h> header file. */
|
||||
/* #undef EVENT__HAVE_SYS_QUEUE_H */
|
||||
|
||||
/* Define to 1 if you have the <sys/select.h> header file. */
|
||||
/* #undef EVENT__HAVE_SYS_SELECT_H */
|
||||
|
||||
/* Define to 1 if you have the <sys/sendfile.h> header file. */
|
||||
/* #undef EVENT__HAVE_SYS_SENDFILE_H */
|
||||
|
||||
/* Define to 1 if you have the <sys/socket.h> header file. */
|
||||
/* #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 to 1 if you have the <sys/time.h> header file. */
|
||||
/* #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 to 1 if you have the <sys/uio.h> header file. */
|
||||
/* #undef EVENT__HAVE_SYS_UIO_H */
|
||||
|
||||
/* Define if TAILQ_FOREACH is defined in <sys/queue.h> */
|
||||
/* #undef EVENT__HAVE_TAILQFOREACH */
|
||||
|
||||
/* Define if timeradd is defined in <sys/time.h> */
|
||||
/* #undef EVENT__HAVE_TIMERADD */
|
||||
|
||||
/* Define if timerclear is defined in <sys/time.h> */
|
||||
#define EVENT__HAVE_TIMERCLEAR 1
|
||||
|
||||
/* Define if timercmp is defined in <sys/time.h> */
|
||||
#define EVENT__HAVE_TIMERCMP 1
|
||||
|
||||
/* Define if timerisset is defined in <sys/time.h> */
|
||||
#define EVENT__HAVE_TIMERISSET 1
|
||||
|
||||
/* Define to 1 if the system has the type `uint16_t'. */
|
||||
/* #define EVENT__HAVE_UINT16_T 1 */
|
||||
|
||||
/* Define to 1 if the system has the type `uint32_t'. */
|
||||
/* #define EVENT__HAVE_UINT32_T 1 */
|
||||
|
||||
/* Define to 1 if the system has the type `uint64_t'. */
|
||||
/* #define EVENT__HAVE_UINT64_T 1 */
|
||||
|
||||
/* Define to 1 if the system has the type `uint8_t'. */
|
||||
/* #define EVENT__HAVE_UINT8_T 1 */
|
||||
|
||||
/* Define to 1 if you have the <unistd.h> header file. */
|
||||
/* #define EVENT__HAVE_UNISTD_H 1 */
|
||||
|
||||
/* Define to 1 if you have the `vasprintf' function. */
|
||||
/* #undef EVENT__HAVE_VASPRINTF */
|
||||
|
||||
/* Define if kqueue works correctly with pipes */
|
||||
/* #undef EVENT__HAVE_WORKING_KQUEUE */
|
||||
|
||||
/* Numeric representation of the version */
|
||||
#define EVENT__NUMERIC_VERSION 0x02010d00
|
||||
|
||||
/* 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 to the version of this package. */
|
||||
#define EVENT__PACKAGE_VERSION ""
|
||||
|
||||
/* Define to necessary symbol if this constant uses a non-standard name on
|
||||
your system. */
|
||||
/* #undef EVENT__PTHREAD_CREATE_JOINABLE */
|
||||
|
||||
/* The size of a `int', as computed by sizeof. */
|
||||
#define EVENT__SIZEOF_INT 4
|
||||
|
||||
/* The size of a `long', as computed by sizeof. */
|
||||
#define EVENT__SIZEOF_LONG 4
|
||||
|
||||
/* The size of a `long long', as computed by sizeof. */
|
||||
#define EVENT__SIZEOF_LONG_LONG 8
|
||||
|
||||
/* The size of a `short', as computed by sizeof. */
|
||||
#define EVENT__SIZEOF_SHORT 2
|
||||
|
||||
/* The size of `size_t', as computed by sizeof. */
|
||||
#ifdef _WIN64
|
||||
#define EVENT__SIZEOF_SIZE_T 8
|
||||
#else
|
||||
#define EVENT__SIZEOF_SIZE_T 4
|
||||
#endif
|
||||
|
||||
/* The size of `void *', as computed by sizeof. */
|
||||
#ifdef _WIN64
|
||||
#define EVENT__SIZEOF_VOID_P 8
|
||||
#else
|
||||
#define EVENT__SIZEOF_VOID_P 4
|
||||
#endif
|
||||
|
||||
/* The size of `time_t`, as computed by sizeof. */
|
||||
#ifdef _WIN64
|
||||
#define EVENT__SIZEOF_TIME_T 8
|
||||
#else
|
||||
#define EVENT__SIZEOF_TIME_T 4
|
||||
#endif
|
||||
|
||||
/* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
|
||||
#define EVENT__TIME_WITH_SYS_TIME 1
|
||||
|
||||
/* Version number of package */
|
||||
#define EVENT__VERSION "2.1.13-stable"
|
||||
|
||||
/* 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. */
|
||||
#define EVENT__inline __inline
|
||||
|
||||
/* Define to `unsigned' if <sys/types.h> does not define. */
|
||||
/* #undef EVENT__size_t */
|
||||
|
||||
/* Define to unsigned int if you dont have it */
|
||||
#define EVENT__socklen_t unsigned int
|
||||
|
||||
/* Define to `int' if <sys/types.h> does not define. */
|
||||
#define EVENT__ssize_t SSIZE_T
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,677 @@
|
||||
/* $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_ */
|
||||
Vendored
+1328
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,546 @@
|
||||
/* Portable arc4random.c based on arc4random.c from OpenBSD.
|
||||
* Portable version by Chris Davis, adapted for Libevent by Nick Mathewson
|
||||
* Copyright (c) 2010 Chris Davis, Niels Provos, and Nick Mathewson
|
||||
* Copyright (c) 2010-2012 Niels Provos and Nick Mathewson
|
||||
*
|
||||
* Note that in Libevent, this file isn't compiled directly. Instead,
|
||||
* it's included from evutil_rand.c
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 1996, David Mazieres <dm@uun.org>
|
||||
* Copyright (c) 2008, Damien Miller <djm@openbsd.org>
|
||||
*
|
||||
* 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" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Arc4 random number generator for OpenBSD.
|
||||
*
|
||||
* This code is derived from section 17.1 of Applied Cryptography,
|
||||
* second edition, which describes a stream cipher allegedly
|
||||
* compatible with RSA Labs "RC4" cipher (the actual description of
|
||||
* which is a trade secret). The same algorithm is used as a stream
|
||||
* cipher called "arcfour" in Tatu Ylonen's ssh package.
|
||||
*
|
||||
* Here the stream cipher has been modified always to include the time
|
||||
* when initializing the state. That makes it impossible to
|
||||
* regenerate the same random sequence twice, so this can't be used
|
||||
* for encryption, but will generate good random numbers.
|
||||
*
|
||||
* RC4 is a registered trademark of RSA Laboratories.
|
||||
*/
|
||||
|
||||
#ifndef ARC4RANDOM_EXPORT
|
||||
#define ARC4RANDOM_EXPORT
|
||||
#endif
|
||||
|
||||
#ifndef ARC4RANDOM_UINT32
|
||||
#define ARC4RANDOM_UINT32 uint32_t
|
||||
#endif
|
||||
|
||||
#ifndef ARC4RANDOM_NO_INCLUDES
|
||||
#include "evconfig-private.h"
|
||||
#ifdef _WIN32
|
||||
#include <wincrypt.h>
|
||||
#include <process.h>
|
||||
#include <winerror.h>
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/time.h>
|
||||
#ifdef EVENT__HAVE_SYS_SYSCTL_H
|
||||
#include <sys/sysctl.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_SYS_RANDOM_H
|
||||
#include <sys/random.h>
|
||||
#endif
|
||||
#endif
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
/* Add platform entropy 32 bytes (256 bits) at a time. */
|
||||
#define ADD_ENTROPY 32
|
||||
|
||||
/* Re-seed from the platform RNG after generating this many bytes. */
|
||||
#define BYTES_BEFORE_RESEED 1600000
|
||||
|
||||
struct arc4_stream {
|
||||
unsigned char i;
|
||||
unsigned char j;
|
||||
unsigned char s[256];
|
||||
};
|
||||
|
||||
#ifdef _WIN32
|
||||
#define getpid _getpid
|
||||
#define pid_t int
|
||||
#endif
|
||||
|
||||
static int rs_initialized;
|
||||
static struct arc4_stream rs;
|
||||
static pid_t arc4_stir_pid;
|
||||
static int arc4_count;
|
||||
|
||||
static inline unsigned char arc4_getbyte(void);
|
||||
|
||||
static inline void
|
||||
arc4_init(void)
|
||||
{
|
||||
int n;
|
||||
|
||||
for (n = 0; n < 256; n++)
|
||||
rs.s[n] = n;
|
||||
rs.i = 0;
|
||||
rs.j = 0;
|
||||
}
|
||||
|
||||
static inline void
|
||||
arc4_addrandom(const unsigned char *dat, int datlen)
|
||||
{
|
||||
int n;
|
||||
unsigned char si;
|
||||
|
||||
rs.i--;
|
||||
for (n = 0; n < 256; n++) {
|
||||
rs.i = (rs.i + 1);
|
||||
si = rs.s[rs.i];
|
||||
rs.j = (rs.j + si + dat[n % datlen]);
|
||||
rs.s[rs.i] = rs.s[rs.j];
|
||||
rs.s[rs.j] = si;
|
||||
}
|
||||
rs.j = rs.i;
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
static ssize_t
|
||||
read_all(int fd, unsigned char *buf, size_t count)
|
||||
{
|
||||
size_t numread = 0;
|
||||
ssize_t result;
|
||||
|
||||
while (numread < count) {
|
||||
result = read(fd, buf+numread, count-numread);
|
||||
if (result<0)
|
||||
return -1;
|
||||
else if (result == 0)
|
||||
break;
|
||||
numread += result;
|
||||
}
|
||||
|
||||
return (ssize_t)numread;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#define TRY_SEED_WIN32
|
||||
static int
|
||||
arc4_seed_win32(void)
|
||||
{
|
||||
/* This is adapted from Tor's crypto_seed_rng() */
|
||||
static int provider_set = 0;
|
||||
static HCRYPTPROV provider;
|
||||
unsigned char buf[ADD_ENTROPY];
|
||||
|
||||
if (!provider_set) {
|
||||
if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
|
||||
CRYPT_VERIFYCONTEXT)) {
|
||||
if (GetLastError() != (DWORD)NTE_BAD_KEYSET)
|
||||
return -1;
|
||||
}
|
||||
provider_set = 1;
|
||||
}
|
||||
if (!CryptGenRandom(provider, sizeof(buf), buf))
|
||||
return -1;
|
||||
arc4_addrandom(buf, sizeof(buf));
|
||||
evutil_memclear_(buf, sizeof(buf));
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(EVENT__HAVE_GETRANDOM)
|
||||
#define TRY_SEED_GETRANDOM
|
||||
static int
|
||||
arc4_seed_getrandom(void)
|
||||
{
|
||||
unsigned char buf[ADD_ENTROPY];
|
||||
size_t len, n;
|
||||
unsigned i;
|
||||
int any_set;
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
for (len = 0; len < sizeof(buf); len += n) {
|
||||
n = sizeof(buf) - len;
|
||||
|
||||
if (0 == getrandom(&buf[len], n, 0))
|
||||
return -1;
|
||||
}
|
||||
/* make sure that the buffer actually got set. */
|
||||
for (i=0,any_set=0; i<sizeof(buf); ++i) {
|
||||
any_set |= buf[i];
|
||||
}
|
||||
if (!any_set)
|
||||
return -1;
|
||||
|
||||
arc4_addrandom(buf, sizeof(buf));
|
||||
evutil_memclear_(buf, sizeof(buf));
|
||||
return 0;
|
||||
}
|
||||
#endif /* EVENT__HAVE_GETRANDOM */
|
||||
|
||||
#if defined(EVENT__HAVE_SYS_SYSCTL_H) && defined(EVENT__HAVE_SYSCTL)
|
||||
#if EVENT__HAVE_DECL_CTL_KERN && EVENT__HAVE_DECL_KERN_ARND
|
||||
#define TRY_SEED_SYSCTL_BSD
|
||||
static int
|
||||
arc4_seed_sysctl_bsd(void)
|
||||
{
|
||||
/* Based on code from William Ahern and from OpenBSD, this function
|
||||
* tries to use the KERN_ARND syscall to get entropy from the kernel.
|
||||
* This can work even if /dev/urandom is inaccessible for some reason
|
||||
* (e.g., we're running in a chroot). */
|
||||
int mib[] = { CTL_KERN, KERN_ARND };
|
||||
unsigned char buf[ADD_ENTROPY];
|
||||
size_t len, n;
|
||||
int i, any_set;
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
len = sizeof(buf);
|
||||
if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
|
||||
for (len = 0; len < sizeof(buf); len += sizeof(unsigned)) {
|
||||
n = sizeof(unsigned);
|
||||
if (n + len > sizeof(buf))
|
||||
n = len - sizeof(buf);
|
||||
if (sysctl(mib, 2, &buf[len], &n, NULL, 0) == -1)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* make sure that the buffer actually got set. */
|
||||
for (i=any_set=0; i<sizeof(buf); ++i) {
|
||||
any_set |= buf[i];
|
||||
}
|
||||
if (!any_set)
|
||||
return -1;
|
||||
|
||||
arc4_addrandom(buf, sizeof(buf));
|
||||
evutil_memclear_(buf, sizeof(buf));
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#endif /* defined(EVENT__HAVE_SYS_SYSCTL_H) */
|
||||
|
||||
#ifdef __linux__
|
||||
#define TRY_SEED_PROC_SYS_KERNEL_RANDOM_UUID
|
||||
static int
|
||||
arc4_seed_proc_sys_kernel_random_uuid(void)
|
||||
{
|
||||
/* Occasionally, somebody will make /proc/sys accessible in a chroot,
|
||||
* but not /dev/urandom. Let's try /proc/sys/kernel/random/uuid.
|
||||
* Its format is stupid, so we need to decode it from hex.
|
||||
*/
|
||||
int fd;
|
||||
char buf[128];
|
||||
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);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
n = read(fd, buf, sizeof(buf));
|
||||
close(fd);
|
||||
if (n<=0)
|
||||
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 (nybbles & 1) {
|
||||
entropy[nybbles/2] |= nyb;
|
||||
} else {
|
||||
entropy[nybbles/2] |= nyb<<4;
|
||||
}
|
||||
++nybbles;
|
||||
}
|
||||
}
|
||||
if (nybbles < 2)
|
||||
return -1;
|
||||
arc4_addrandom(entropy, nybbles/2);
|
||||
bytes += nybbles/2;
|
||||
}
|
||||
evutil_memclear_(entropy, sizeof(entropy));
|
||||
evutil_memclear_(buf, sizeof(buf));
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
#define TRY_SEED_URANDOM
|
||||
static char *arc4random_urandom_filename = NULL;
|
||||
|
||||
static int arc4_seed_urandom_helper_(const char *fname)
|
||||
{
|
||||
unsigned char buf[ADD_ENTROPY];
|
||||
int fd;
|
||||
size_t n;
|
||||
|
||||
fd = evutil_open_closeonexec_(fname, O_RDONLY, 0);
|
||||
if (fd<0)
|
||||
return -1;
|
||||
n = read_all(fd, buf, sizeof(buf));
|
||||
close(fd);
|
||||
if (n != sizeof(buf))
|
||||
return -1;
|
||||
arc4_addrandom(buf, sizeof(buf));
|
||||
evutil_memclear_(buf, sizeof(buf));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
arc4_seed_urandom(void)
|
||||
{
|
||||
/* This is adapted from Tor's crypto_seed_rng() */
|
||||
static const char *filenames[] = {
|
||||
"/dev/srandom", "/dev/urandom", "/dev/random", NULL
|
||||
};
|
||||
int i;
|
||||
if (arc4random_urandom_filename)
|
||||
return arc4_seed_urandom_helper_(arc4random_urandom_filename);
|
||||
|
||||
for (i = 0; filenames[i]; ++i) {
|
||||
if (arc4_seed_urandom_helper_(filenames[i]) == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int
|
||||
arc4_seed(void)
|
||||
{
|
||||
int ok = 0;
|
||||
/* We try every method that might work, and don't give up even if one
|
||||
* does seem to work. There's no real harm in over-seeding, and if
|
||||
* one of these sources turns out to be broken, that would be bad. */
|
||||
#ifdef TRY_SEED_WIN32
|
||||
if (0 == arc4_seed_win32())
|
||||
ok = 1;
|
||||
#endif
|
||||
#ifdef TRY_SEED_GETRANDOM
|
||||
if (0 == arc4_seed_getrandom())
|
||||
ok = 1;
|
||||
#endif
|
||||
#ifdef TRY_SEED_URANDOM
|
||||
if (0 == arc4_seed_urandom())
|
||||
ok = 1;
|
||||
#endif
|
||||
#ifdef TRY_SEED_PROC_SYS_KERNEL_RANDOM_UUID
|
||||
if (arc4random_urandom_filename == NULL &&
|
||||
0 == arc4_seed_proc_sys_kernel_random_uuid())
|
||||
ok = 1;
|
||||
#endif
|
||||
#ifdef TRY_SEED_SYSCTL_BSD
|
||||
if (0 == arc4_seed_sysctl_bsd())
|
||||
ok = 1;
|
||||
#endif
|
||||
return ok ? 0 : -1;
|
||||
}
|
||||
|
||||
static int
|
||||
arc4_stir(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!rs_initialized) {
|
||||
arc4_init();
|
||||
rs_initialized = 1;
|
||||
}
|
||||
|
||||
if (0 != arc4_seed())
|
||||
return -1;
|
||||
|
||||
/*
|
||||
* Discard early keystream, as per recommendations in
|
||||
* "Weaknesses in the Key Scheduling Algorithm of RC4" by
|
||||
* Scott Fluhrer, Itsik Mantin, and Adi Shamir.
|
||||
* http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
|
||||
*
|
||||
* Ilya Mironov's "(Not So) Random Shuffles of RC4" suggests that
|
||||
* we drop at least 2*256 bytes, with 12*256 as a conservative
|
||||
* value.
|
||||
*
|
||||
* RFC4345 says to drop 6*256.
|
||||
*
|
||||
* At least some versions of this code drop 4*256, in a mistaken
|
||||
* belief that "words" in the Fluhrer/Mantin/Shamir paper refers
|
||||
* to processor words.
|
||||
*
|
||||
* We add another sect to the cargo cult, and choose 12*256.
|
||||
*/
|
||||
for (i = 0; i < 12*256; i++)
|
||||
(void)arc4_getbyte();
|
||||
|
||||
arc4_count = BYTES_BEFORE_RESEED;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
arc4_stir_if_needed(void)
|
||||
{
|
||||
pid_t pid = getpid();
|
||||
|
||||
if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != pid)
|
||||
{
|
||||
arc4_stir_pid = pid;
|
||||
arc4_stir();
|
||||
}
|
||||
}
|
||||
|
||||
static inline unsigned char
|
||||
arc4_getbyte(void)
|
||||
{
|
||||
unsigned char si, sj;
|
||||
|
||||
rs.i = (rs.i + 1);
|
||||
si = rs.s[rs.i];
|
||||
rs.j = (rs.j + si);
|
||||
sj = rs.s[rs.j];
|
||||
rs.s[rs.i] = sj;
|
||||
rs.s[rs.j] = si;
|
||||
return (rs.s[(si + sj) & 0xff]);
|
||||
}
|
||||
|
||||
static inline unsigned int
|
||||
arc4_getword(void)
|
||||
{
|
||||
unsigned int val;
|
||||
|
||||
val = arc4_getbyte() << 24;
|
||||
val |= arc4_getbyte() << 16;
|
||||
val |= arc4_getbyte() << 8;
|
||||
val |= arc4_getbyte();
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
#ifndef ARC4RANDOM_NOSTIR
|
||||
ARC4RANDOM_EXPORT int
|
||||
arc4random_stir(void)
|
||||
{
|
||||
int val;
|
||||
ARC4_LOCK_();
|
||||
val = arc4_stir();
|
||||
ARC4_UNLOCK_();
|
||||
return val;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef ARC4RANDOM_NOADDRANDOM
|
||||
ARC4RANDOM_EXPORT void
|
||||
arc4random_addrandom(const unsigned char *dat, int datlen)
|
||||
{
|
||||
int j;
|
||||
ARC4_LOCK_();
|
||||
if (!rs_initialized)
|
||||
arc4_stir();
|
||||
for (j = 0; j < datlen; j += 256) {
|
||||
/* arc4_addrandom() ignores all but the first 256 bytes of
|
||||
* its input. We want to make sure to look at ALL the
|
||||
* data in 'dat', just in case the user is doing something
|
||||
* crazy like passing us all the files in /var/log. */
|
||||
arc4_addrandom(dat + j, datlen - j);
|
||||
}
|
||||
ARC4_UNLOCK_();
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef ARC4RANDOM_NORANDOM
|
||||
ARC4RANDOM_EXPORT ARC4RANDOM_UINT32
|
||||
arc4random(void)
|
||||
{
|
||||
ARC4RANDOM_UINT32 val;
|
||||
ARC4_LOCK_();
|
||||
arc4_count -= 4;
|
||||
arc4_stir_if_needed();
|
||||
val = arc4_getword();
|
||||
ARC4_UNLOCK_();
|
||||
return val;
|
||||
}
|
||||
#endif
|
||||
|
||||
ARC4RANDOM_EXPORT void
|
||||
arc4random_buf(void *buf_, size_t n)
|
||||
{
|
||||
unsigned char *buf = buf_;
|
||||
ARC4_LOCK_();
|
||||
arc4_stir_if_needed();
|
||||
while (n--) {
|
||||
if (--arc4_count <= 0)
|
||||
arc4_stir();
|
||||
buf[n] = arc4_getbyte();
|
||||
}
|
||||
ARC4_UNLOCK_();
|
||||
}
|
||||
|
||||
#ifndef ARC4RANDOM_NOUNIFORM
|
||||
/*
|
||||
* Calculate a uniformly distributed random number less than upper_bound
|
||||
* avoiding "modulo bias".
|
||||
*
|
||||
* Uniformity is achieved by generating new random numbers until the one
|
||||
* returned is outside the range [0, 2**32 % upper_bound). This
|
||||
* guarantees the selected random number will be inside
|
||||
* [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
|
||||
* after reduction modulo upper_bound.
|
||||
*/
|
||||
ARC4RANDOM_EXPORT unsigned int
|
||||
arc4random_uniform(unsigned int upper_bound)
|
||||
{
|
||||
ARC4RANDOM_UINT32 r, min;
|
||||
|
||||
if (upper_bound < 2)
|
||||
return 0;
|
||||
|
||||
#if (UINT_MAX > 0xffffffffUL)
|
||||
min = 0x100000000UL % upper_bound;
|
||||
#else
|
||||
/* Calculate (2**32 % upper_bound) avoiding 64-bit math */
|
||||
if (upper_bound > 0x80000000)
|
||||
min = 1 + ~upper_bound; /* 2**32 - upper_bound */
|
||||
else {
|
||||
/* (2**32 - (x * 2)) % x == 2**32 % x when x <= 2**31 */
|
||||
min = ((0xffffffff - (upper_bound * 2)) + 1) % upper_bound;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* This could theoretically loop forever but each retry has
|
||||
* p > 0.5 (worst case, usually far better) of selecting a
|
||||
* number inside the range we need, so it should rarely need
|
||||
* to re-roll.
|
||||
*/
|
||||
for (;;) {
|
||||
r = arc4random();
|
||||
if (r >= min)
|
||||
break;
|
||||
}
|
||||
|
||||
return r % upper_bound;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/bin/sh
|
||||
|
||||
MAKE=make
|
||||
if command -v gmake >/dev/null 2>/dev/null; then
|
||||
MAKE=gmake
|
||||
fi
|
||||
$MAKE maintainer-clean >/dev/null 2>/dev/null
|
||||
|
||||
if [ -x "`which autoreconf 2>/dev/null`" ] ; then
|
||||
exec autoreconf -ivf
|
||||
fi
|
||||
|
||||
LIBTOOLIZE=libtoolize
|
||||
SYSNAME=`uname`
|
||||
if [ "x$SYSNAME" = "xDarwin" ] ; then
|
||||
LIBTOOLIZE=glibtoolize
|
||||
fi
|
||||
aclocal -I m4 && \
|
||||
autoheader && \
|
||||
$LIBTOOLIZE && \
|
||||
autoconf && \
|
||||
automake --add-missing --force-missing --copy
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,327 @@
|
||||
/*
|
||||
* Copyright (c) 2009-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.
|
||||
*/
|
||||
|
||||
/**
|
||||
@file buffer_iocp.c
|
||||
|
||||
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 "util-internal.h"
|
||||
#include "evthread-internal.h"
|
||||
#include "evbuffer-internal.h"
|
||||
#include "iocp-internal.h"
|
||||
#include "mm-internal.h"
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <winerror.h>
|
||||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define MAX_WSABUFS 16
|
||||
|
||||
/** An evbuffer that can handle overlapped IO. */
|
||||
struct evbuffer_overlapped {
|
||||
struct evbuffer buffer;
|
||||
/** The socket that we're doing overlapped IO on. */
|
||||
evutil_socket_t fd;
|
||||
|
||||
/** pending I/O type */
|
||||
unsigned read_in_progress : 1;
|
||||
unsigned write_in_progress : 1;
|
||||
|
||||
/** The first pinned chain in the buffer. */
|
||||
struct evbuffer_chain *first_pinned;
|
||||
|
||||
/** How many chains are pinned; how many of the fields in buffers
|
||||
* are we using. */
|
||||
int n_buffers;
|
||||
WSABUF buffers[MAX_WSABUFS];
|
||||
};
|
||||
|
||||
/** Given an evbuffer, return the correponding evbuffer structure, or NULL if
|
||||
* the evbuffer isn't overlapped. */
|
||||
static inline struct evbuffer_overlapped *
|
||||
upcast_evbuffer(struct evbuffer *buf)
|
||||
{
|
||||
if (!buf || !buf->is_overlapped)
|
||||
return NULL;
|
||||
return EVUTIL_UPCAST(buf, struct evbuffer_overlapped, buffer);
|
||||
}
|
||||
|
||||
/** Unpin all the chains noted as pinned in 'eo'. */
|
||||
static void
|
||||
pin_release(struct evbuffer_overlapped *eo, unsigned flag)
|
||||
{
|
||||
int i;
|
||||
struct evbuffer_chain *next, *chain = eo->first_pinned;
|
||||
|
||||
for (i = 0; i < eo->n_buffers; ++i) {
|
||||
EVUTIL_ASSERT(chain);
|
||||
next = chain->next;
|
||||
evbuffer_chain_unpin_(chain, flag);
|
||||
chain = next;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
evbuffer_commit_read_(struct evbuffer *evbuf, ev_ssize_t nBytes)
|
||||
{
|
||||
struct evbuffer_overlapped *buf = upcast_evbuffer(evbuf);
|
||||
struct evbuffer_chain **chainp;
|
||||
size_t remaining, len;
|
||||
unsigned i;
|
||||
|
||||
EVBUFFER_LOCK(evbuf);
|
||||
EVUTIL_ASSERT(buf->read_in_progress && !buf->write_in_progress);
|
||||
EVUTIL_ASSERT(nBytes >= 0); /* XXXX Can this be false? */
|
||||
|
||||
evbuffer_unfreeze(evbuf, 0);
|
||||
|
||||
chainp = evbuf->last_with_datap;
|
||||
if (!((*chainp)->flags & EVBUFFER_MEM_PINNED_R))
|
||||
chainp = &(*chainp)->next;
|
||||
remaining = nBytes;
|
||||
for (i = 0; remaining > 0 && i < (unsigned)buf->n_buffers; ++i) {
|
||||
EVUTIL_ASSERT(*chainp);
|
||||
len = buf->buffers[i].len;
|
||||
if (remaining < len)
|
||||
len = remaining;
|
||||
(*chainp)->off += len;
|
||||
evbuf->last_with_datap = chainp;
|
||||
remaining -= len;
|
||||
chainp = &(*chainp)->next;
|
||||
}
|
||||
|
||||
pin_release(buf, EVBUFFER_MEM_PINNED_R);
|
||||
|
||||
buf->read_in_progress = 0;
|
||||
|
||||
evbuf->total_len += nBytes;
|
||||
evbuf->n_add_for_cb += nBytes;
|
||||
|
||||
evbuffer_invoke_callbacks_(evbuf);
|
||||
|
||||
evbuffer_decref_and_unlock_(evbuf);
|
||||
}
|
||||
|
||||
void
|
||||
evbuffer_commit_write_(struct evbuffer *evbuf, ev_ssize_t nBytes)
|
||||
{
|
||||
struct evbuffer_overlapped *buf = upcast_evbuffer(evbuf);
|
||||
|
||||
EVBUFFER_LOCK(evbuf);
|
||||
EVUTIL_ASSERT(buf->write_in_progress && !buf->read_in_progress);
|
||||
evbuffer_unfreeze(evbuf, 1);
|
||||
evbuffer_drain(evbuf, nBytes);
|
||||
pin_release(buf,EVBUFFER_MEM_PINNED_W);
|
||||
buf->write_in_progress = 0;
|
||||
evbuffer_decref_and_unlock_(evbuf);
|
||||
}
|
||||
|
||||
struct evbuffer *
|
||||
evbuffer_overlapped_new_(evutil_socket_t fd)
|
||||
{
|
||||
struct evbuffer_overlapped *evo;
|
||||
|
||||
evo = mm_calloc(1, sizeof(struct evbuffer_overlapped));
|
||||
if (!evo)
|
||||
return NULL;
|
||||
|
||||
LIST_INIT(&evo->buffer.callbacks);
|
||||
evo->buffer.refcnt = 1;
|
||||
evo->buffer.last_with_datap = &evo->buffer.first;
|
||||
|
||||
evo->buffer.is_overlapped = 1;
|
||||
evo->fd = fd;
|
||||
|
||||
return &evo->buffer;
|
||||
}
|
||||
|
||||
int
|
||||
evbuffer_launch_write_(struct evbuffer *buf, ev_ssize_t at_most,
|
||||
struct event_overlapped *ol)
|
||||
{
|
||||
struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf);
|
||||
int r = -1;
|
||||
int i;
|
||||
struct evbuffer_chain *chain;
|
||||
DWORD bytesSent;
|
||||
|
||||
if (!buf) {
|
||||
/* No buffer, or it isn't overlapped */
|
||||
return -1;
|
||||
}
|
||||
|
||||
EVBUFFER_LOCK(buf);
|
||||
EVUTIL_ASSERT(!buf_o->read_in_progress);
|
||||
if (buf->freeze_start || buf_o->write_in_progress)
|
||||
goto done;
|
||||
if (!buf->total_len) {
|
||||
/* Nothing to write */
|
||||
r = 0;
|
||||
goto done;
|
||||
} else if (at_most < 0 || (size_t)at_most > buf->total_len) {
|
||||
at_most = buf->total_len;
|
||||
}
|
||||
evbuffer_freeze(buf, 1);
|
||||
|
||||
buf_o->first_pinned = NULL;
|
||||
buf_o->n_buffers = 0;
|
||||
memset(buf_o->buffers, 0, sizeof(buf_o->buffers));
|
||||
|
||||
chain = buf_o->first_pinned = buf->first;
|
||||
|
||||
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);
|
||||
|
||||
if ((size_t)at_most > chain->off) {
|
||||
/* XXXX Cast is safe for now, since win32 has no
|
||||
mmaped chains. But later, we need to have this
|
||||
add more WSAbufs if chain->off is greater than
|
||||
ULONG_MAX */
|
||||
b->len = (unsigned long)chain->off;
|
||||
at_most -= chain->off;
|
||||
} else {
|
||||
b->len = (unsigned long)at_most;
|
||||
++i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
buf_o->n_buffers = i;
|
||||
evbuffer_incref_(buf);
|
||||
if (WSASend(buf_o->fd, buf_o->buffers, i, &bytesSent, 0,
|
||||
&ol->overlapped, NULL)) {
|
||||
int error = WSAGetLastError();
|
||||
if (error != WSA_IO_PENDING) {
|
||||
/* An actual error. */
|
||||
pin_release(buf_o, EVBUFFER_MEM_PINNED_W);
|
||||
evbuffer_unfreeze(buf, 1);
|
||||
evbuffer_free(buf); /* decref */
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
|
||||
buf_o->write_in_progress = 1;
|
||||
r = 0;
|
||||
done:
|
||||
EVBUFFER_UNLOCK(buf);
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
evbuffer_launch_read_(struct evbuffer *buf, size_t at_most,
|
||||
struct event_overlapped *ol)
|
||||
{
|
||||
struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf);
|
||||
int r = -1, i;
|
||||
int nvecs;
|
||||
int npin=0;
|
||||
struct evbuffer_chain *chain=NULL, **chainp;
|
||||
DWORD bytesRead;
|
||||
DWORD flags = 0;
|
||||
struct evbuffer_iovec vecs[MAX_WSABUFS];
|
||||
|
||||
if (!buf_o)
|
||||
return -1;
|
||||
EVBUFFER_LOCK(buf);
|
||||
EVUTIL_ASSERT(!buf_o->write_in_progress);
|
||||
if (buf->freeze_end || buf_o->read_in_progress)
|
||||
goto done;
|
||||
|
||||
buf_o->first_pinned = NULL;
|
||||
buf_o->n_buffers = 0;
|
||||
memset(buf_o->buffers, 0, sizeof(buf_o->buffers));
|
||||
|
||||
if (evbuffer_expand_fast_(buf, at_most, MAX_WSABUFS) == -1)
|
||||
goto done;
|
||||
evbuffer_freeze(buf, 0);
|
||||
|
||||
nvecs = evbuffer_read_setup_vecs_(buf, at_most,
|
||||
vecs, MAX_WSABUFS, &chainp, 1);
|
||||
for (i=0;i<nvecs;++i) {
|
||||
WSABUF_FROM_EVBUFFER_IOV(
|
||||
&buf_o->buffers[i],
|
||||
&vecs[i]);
|
||||
}
|
||||
|
||||
buf_o->n_buffers = nvecs;
|
||||
buf_o->first_pinned = chain = *chainp;
|
||||
|
||||
npin=0;
|
||||
for ( ; chain; chain = chain->next) {
|
||||
evbuffer_chain_pin_(chain, EVBUFFER_MEM_PINNED_R);
|
||||
++npin;
|
||||
}
|
||||
EVUTIL_ASSERT(npin == nvecs);
|
||||
|
||||
evbuffer_incref_(buf);
|
||||
if (WSARecv(buf_o->fd, buf_o->buffers, nvecs, &bytesRead, &flags,
|
||||
&ol->overlapped, NULL)) {
|
||||
int error = WSAGetLastError();
|
||||
if (error != WSA_IO_PENDING) {
|
||||
/* An actual error. */
|
||||
pin_release(buf_o, EVBUFFER_MEM_PINNED_R);
|
||||
evbuffer_unfreeze(buf, 0);
|
||||
evbuffer_free(buf); /* decref */
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
|
||||
buf_o->read_in_progress = 1;
|
||||
r = 0;
|
||||
done:
|
||||
EVBUFFER_UNLOCK(buf);
|
||||
return r;
|
||||
}
|
||||
|
||||
evutil_socket_t
|
||||
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)
|
||||
{
|
||||
struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf);
|
||||
EVBUFFER_LOCK(buf);
|
||||
/* XXX is this right?, should it cancel current I/O operations? */
|
||||
if (buf_o)
|
||||
buf_o->fd = fd;
|
||||
EVBUFFER_UNLOCK(buf);
|
||||
}
|
||||
@@ -0,0 +1,519 @@
|
||||
/*
|
||||
* Copyright (c) 2008-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 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 "util-internal.h"
|
||||
#include "defer-internal.h"
|
||||
#include "evthread-internal.h"
|
||||
#include "event2/thread.h"
|
||||
#include "ratelim-internal.h"
|
||||
#include "event2/bufferevent_struct.h"
|
||||
|
||||
#include "ipv6-internal.h"
|
||||
#ifdef _WIN32
|
||||
#include <ws2tcpip.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_NETINET_IN_H
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_NETINET_IN6_H
|
||||
#include <netinet/in6.h>
|
||||
#endif
|
||||
|
||||
/* These flags are reasons that we might be declining to actually enable
|
||||
reading or writing on a bufferevent.
|
||||
*/
|
||||
|
||||
/* On a all bufferevents, for reading: used when we have read up to the
|
||||
watermark value.
|
||||
|
||||
On a filtering bufferevent, for writing: used when the underlying
|
||||
bufferevent's write buffer has been filled up to its watermark
|
||||
value.
|
||||
*/
|
||||
#define BEV_SUSPEND_WM 0x01
|
||||
/* On a base bufferevent: when we have emptied a bandwidth buckets */
|
||||
#define BEV_SUSPEND_BW 0x02
|
||||
/* On a base bufferevent: when we have emptied the group's bandwidth bucket. */
|
||||
#define BEV_SUSPEND_BW_GROUP 0x04
|
||||
/* On a socket bufferevent: can't do any operations while we're waiting for
|
||||
* name lookup to finish. */
|
||||
#define BEV_SUSPEND_LOOKUP 0x08
|
||||
/* On a base bufferevent, for reading: used when a filter has choked this
|
||||
* (underlying) bufferevent because it has stopped reading from it. */
|
||||
#define BEV_SUSPEND_FILT_READ 0x10
|
||||
|
||||
typedef ev_uint16_t bufferevent_suspend_flags;
|
||||
|
||||
struct bufferevent_rate_limit_group {
|
||||
/** List of all members in the group */
|
||||
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;
|
||||
|
||||
/** True iff we don't want to read from any member of the group.until
|
||||
* the token bucket refills. */
|
||||
unsigned read_suspended : 1;
|
||||
/** True iff we don't want to write from any member of the group.until
|
||||
* the token bucket refills. */
|
||||
unsigned write_suspended : 1;
|
||||
/** True iff we were unable to suspend one of the bufferevents in the
|
||||
* group for reading the last time we tried, and we should try
|
||||
* again. */
|
||||
unsigned pending_unsuspend_read : 1;
|
||||
/** True iff we were unable to suspend one of the bufferevents in the
|
||||
* group for writing the last time we tried, and we should try
|
||||
* again. */
|
||||
unsigned pending_unsuspend_write : 1;
|
||||
|
||||
/*@{*/
|
||||
/** Total number of bytes read or written in this group since last
|
||||
* reset. */
|
||||
ev_uint64_t total_read;
|
||||
ev_uint64_t total_written;
|
||||
/*@}*/
|
||||
|
||||
/** The number of bufferevents in the group. */
|
||||
int n_members;
|
||||
|
||||
/** The smallest number of bytes that any member of the group should
|
||||
* be limited to read or write at a time. */
|
||||
ev_ssize_t min_share;
|
||||
ev_ssize_t configured_min_share;
|
||||
|
||||
/** 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. */
|
||||
void *lock;
|
||||
};
|
||||
|
||||
/** Fields for rate-limiting a single bufferevent. */
|
||||
struct bufferevent_rate_limit {
|
||||
/* Linked-list elements for storing this bufferevent_private in a
|
||||
* group.
|
||||
*
|
||||
* Note that this field is supposed to be protected by the group
|
||||
* lock */
|
||||
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;
|
||||
|
||||
/* This bufferevent's current limits. */
|
||||
struct ev_token_bucket limit;
|
||||
/* Pointer to the rate-limit configuration for this bufferevent.
|
||||
* Can be shared. XXX reference-count this? */
|
||||
struct ev_token_bucket_cfg *cfg;
|
||||
|
||||
/* Timeout event used when one this bufferevent's buckets are
|
||||
* empty. */
|
||||
struct event refill_bucket_event;
|
||||
};
|
||||
|
||||
/** Parts of the bufferevent structure that are shared among all bufferevent
|
||||
* types, but not exposed in bufferevent_struct.h. */
|
||||
struct bufferevent_private {
|
||||
/** The underlying bufferevent structure. */
|
||||
struct bufferevent bev;
|
||||
|
||||
/** Evbuffer callback to enforce watermarks on input. */
|
||||
struct evbuffer_cb_entry *read_watermarks_cb;
|
||||
|
||||
/** If set, we should free the lock when we free the bufferevent. */
|
||||
unsigned own_lock : 1;
|
||||
|
||||
/** Flag: set if we have deferred callbacks and a read callback is
|
||||
* pending. */
|
||||
unsigned readcb_pending : 1;
|
||||
/** Flag: set if we have deferred callbacks and a write callback is
|
||||
* pending. */
|
||||
unsigned writecb_pending : 1;
|
||||
/** Flag: set if we are currently busy connecting. */
|
||||
unsigned connecting : 1;
|
||||
/** Flag: set if a connect failed prematurely; this is a hack for
|
||||
* getting around the bufferevent abstraction. */
|
||||
unsigned connection_refused : 1;
|
||||
/** Set to the events pending if we have deferred callbacks and
|
||||
* an events callback is pending. */
|
||||
short eventcb_pending;
|
||||
|
||||
/** If set, read is suspended until one or more conditions are over.
|
||||
* The actual value here is a bitfield of those conditions; see the
|
||||
* BEV_SUSPEND_* flags above. */
|
||||
bufferevent_suspend_flags read_suspended;
|
||||
|
||||
/** If set, writing is suspended until one or more conditions are over.
|
||||
* The actual value here is a bitfield of those conditions; see the
|
||||
* BEV_SUSPEND_* flags above. */
|
||||
bufferevent_suspend_flags write_suspended;
|
||||
|
||||
/** Set to the current socket errno if we have deferred callbacks and
|
||||
* an events callback is pending. */
|
||||
int errno_pending;
|
||||
|
||||
/** The DNS error code for bufferevent_socket_connect_hostname */
|
||||
int dns_error;
|
||||
|
||||
/** Used to implement deferred callbacks */
|
||||
struct event_callback deferred;
|
||||
|
||||
/** The options this bufferevent was constructed with */
|
||||
enum bufferevent_options options;
|
||||
|
||||
/** Current reference count for this bufferevent. */
|
||||
int refcnt;
|
||||
|
||||
/** Lock for this bufferevent. Shared by the inbuf and the outbuf.
|
||||
* 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;
|
||||
|
||||
/* 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, or
|
||||
* after resolving (to avoid extra dns requests during retrying, since UDP
|
||||
* is slow) */
|
||||
/* NOTE: it might be nice to use fewer bytes here, but we need to store
|
||||
* sockaddr_un sometimes in order to make AF_UNIX sockets work as expected
|
||||
* with http.c. */
|
||||
struct sockaddr_storage conn_address;
|
||||
|
||||
struct evdns_getaddrinfo_request *dns_request;
|
||||
};
|
||||
|
||||
/** Possible operations for a control callback. */
|
||||
enum bufferevent_ctrl_op {
|
||||
BEV_CTRL_SET_FD,
|
||||
BEV_CTRL_GET_FD,
|
||||
BEV_CTRL_GET_UNDERLYING,
|
||||
BEV_CTRL_CANCEL_ALL
|
||||
};
|
||||
|
||||
/** Possible data types for a control callback */
|
||||
union bufferevent_ctrl_data {
|
||||
void *ptr;
|
||||
evutil_socket_t fd;
|
||||
};
|
||||
|
||||
/**
|
||||
Implementation table for a bufferevent: holds function pointers and other
|
||||
information to make the various bufferevent types work.
|
||||
*/
|
||||
struct bufferevent_ops {
|
||||
/** The name of the bufferevent's type. */
|
||||
const char *type;
|
||||
/** At what offset into the implementation type will we find a
|
||||
bufferevent structure?
|
||||
|
||||
Example: if the type is implemented as
|
||||
struct bufferevent_x {
|
||||
int extra_data;
|
||||
struct bufferevent bev;
|
||||
}
|
||||
then mem_offset should be offsetof(struct bufferevent_x, bev)
|
||||
*/
|
||||
off_t mem_offset;
|
||||
|
||||
/** Enables one or more of EV_READ|EV_WRITE on a bufferevent. Does
|
||||
not need to adjust the 'enabled' field. Returns 0 on success, -1
|
||||
on failure.
|
||||
*/
|
||||
int (*enable)(struct bufferevent *, short);
|
||||
|
||||
/** Disables one or more of EV_READ|EV_WRITE on a bufferevent. Does
|
||||
not need to adjust the 'enabled' field. Returns 0 on success, -1
|
||||
on failure.
|
||||
*/
|
||||
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. Called when the bufferevent is
|
||||
finalized.
|
||||
*/
|
||||
void (*destruct)(struct bufferevent *);
|
||||
|
||||
/** Called when the timeouts on the bufferevent have changed.*/
|
||||
int (*adj_timeouts)(struct bufferevent *);
|
||||
|
||||
/** Called to flush data. */
|
||||
int (*flush)(struct bufferevent *, short, enum bufferevent_flush_mode);
|
||||
|
||||
/** Called to access miscellaneous fields. */
|
||||
int (*ctrl)(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
|
||||
|
||||
};
|
||||
|
||||
extern const struct bufferevent_ops bufferevent_ops_socket;
|
||||
extern const struct bufferevent_ops bufferevent_ops_filter;
|
||||
extern const struct bufferevent_ops bufferevent_ops_pair;
|
||||
|
||||
#define BEV_IS_SOCKET(bevp) ((bevp)->be_ops == &bufferevent_ops_socket)
|
||||
#define BEV_IS_FILTER(bevp) ((bevp)->be_ops == &bufferevent_ops_filter)
|
||||
#define BEV_IS_PAIR(bevp) ((bevp)->be_ops == &bufferevent_ops_pair)
|
||||
|
||||
#if defined(EVENT__HAVE_OPENSSL)
|
||||
extern const struct bufferevent_ops bufferevent_ops_openssl;
|
||||
#define BEV_IS_OPENSSL(bevp) ((bevp)->be_ops == &bufferevent_ops_openssl)
|
||||
#else
|
||||
#define BEV_IS_OPENSSL(bevp) 0
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
extern const struct bufferevent_ops bufferevent_ops_async;
|
||||
#define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async)
|
||||
#else
|
||||
#define BEV_IS_ASYNC(bevp) 0
|
||||
#endif
|
||||
|
||||
/** Initialize the shared parts of a bufferevent. */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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. */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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. */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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);
|
||||
/** 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);
|
||||
|
||||
#define bufferevent_wm_suspend_read(b) \
|
||||
bufferevent_suspend_read_((b), BEV_SUSPEND_WM)
|
||||
#define bufferevent_wm_unsuspend_read(b) \
|
||||
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.
|
||||
|
||||
@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()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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. */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int bufferevent_enable_locking_(struct bufferevent *bufev, void *lock);
|
||||
/** Internal: backwards compat macro for the now public function
|
||||
* Increment the reference count on bufev. */
|
||||
#define bufferevent_incref_(bufev) bufferevent_incref(bufev)
|
||||
/** Internal: Lock bufev and increase its reference count.
|
||||
* unlocking it otherwise. */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void bufferevent_incref_and_lock_(struct bufferevent *bufev);
|
||||
/** Internal: backwards compat macro for the now public function
|
||||
* Decrement the reference count on bufev. Returns 1 if it freed
|
||||
* the bufferevent.*/
|
||||
#define bufferevent_decref_(bufev) bufferevent_decref(bufev)
|
||||
|
||||
/** Internal: Drop the reference count on bufev, freeing as necessary, and
|
||||
* unlocking it otherwise. Returns 1 if it freed the bufferevent. */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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. Ignores watermarks. */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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. Ignores watermarks. */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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.
|
||||
* See bufferevent_trigger_event for meaning of "options". */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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. */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int bufferevent_add_event_(struct event *ev, const struct timeval *tv);
|
||||
|
||||
/* =========
|
||||
* These next functions implement timeouts for bufferevents that aren't doing
|
||||
* anything else with ev_read and ev_write, to handle timeouts.
|
||||
* ========= */
|
||||
/** 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. */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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. */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int bufferevent_generic_adj_timeouts_(struct bufferevent *bev);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int bufferevent_generic_adj_existing_timeouts_(struct bufferevent *bev);
|
||||
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
enum bufferevent_options bufferevent_get_options_(struct bufferevent *bev);
|
||||
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
const struct sockaddr*
|
||||
bufferevent_socket_get_conn_address_(struct bufferevent *bev);
|
||||
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void
|
||||
bufferevent_socket_set_conn_address_fd_(struct bufferevent *bev, evutil_socket_t fd);
|
||||
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int
|
||||
bufferevent_socket_set_conn_address_(struct bufferevent *bev, struct sockaddr *addr, size_t addrlen);
|
||||
|
||||
|
||||
/** Internal use: We have just successfully read data into an inbuf, so
|
||||
* reset the read timeout (if any). */
|
||||
#define BEV_RESET_GENERIC_READ_TIMEOUT(bev) \
|
||||
do { \
|
||||
if (evutil_timerisset(&(bev)->timeout_read)) \
|
||||
event_add(&(bev)->ev_read, &(bev)->timeout_read); \
|
||||
} while (0)
|
||||
/** Internal use: We have just successfully written data from an inbuf, so
|
||||
* reset the read timeout (if any). */
|
||||
#define BEV_RESET_GENERIC_WRITE_TIMEOUT(bev) \
|
||||
do { \
|
||||
if (evutil_timerisset(&(bev)->timeout_write)) \
|
||||
event_add(&(bev)->ev_write, &(bev)->timeout_write); \
|
||||
} while (0)
|
||||
#define BEV_DEL_GENERIC_READ_TIMEOUT(bev) \
|
||||
event_del(&(bev)->ev_read)
|
||||
#define BEV_DEL_GENERIC_WRITE_TIMEOUT(bev) \
|
||||
event_del(&(bev)->ev_write)
|
||||
|
||||
|
||||
/** Internal: Given a bufferevent, return its corresponding
|
||||
* 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_
|
||||
#else
|
||||
/** Internal: Grab the lock (if any) on a bufferevent */
|
||||
#define BEV_LOCK(b) do { \
|
||||
struct bufferevent_private *locking = BEV_UPCAST(b); \
|
||||
EVLOCK_LOCK(locking->lock, 0); \
|
||||
} while (0)
|
||||
|
||||
/** Internal: Release the lock (if any) on a bufferevent */
|
||||
#define BEV_UNLOCK(b) do { \
|
||||
struct bufferevent_private *locking = BEV_UPCAST(b); \
|
||||
EVLOCK_UNLOCK(locking->lock, 0); \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
|
||||
/* ==== For rate-limiting. */
|
||||
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int bufferevent_decrement_write_buckets_(struct bufferevent_private *bev,
|
||||
ev_ssize_t bytes);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int bufferevent_decrement_read_buckets_(struct bufferevent_private *bev,
|
||||
ev_ssize_t bytes);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
ev_ssize_t bufferevent_get_read_max_(struct bufferevent_private *bev);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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_INCLUDED_ */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,706 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
|
||||
*
|
||||
* 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. 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 EVENT__HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifdef EVENT__HAVE_STDARG_H
|
||||
#include <stdarg.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <winerror.h>
|
||||
#include <ws2tcpip.h>
|
||||
#endif
|
||||
|
||||
#include <sys/queue.h>
|
||||
|
||||
#include "event2/util.h"
|
||||
#include "event2/bufferevent.h"
|
||||
#include "event2/buffer.h"
|
||||
#include "event2/bufferevent_struct.h"
|
||||
#include "event2/event.h"
|
||||
#include "event2/util.h"
|
||||
#include "event-internal.h"
|
||||
#include "log-internal.h"
|
||||
#include "mm-internal.h"
|
||||
#include "bufferevent-internal.h"
|
||||
#include "util-internal.h"
|
||||
#include "iocp-internal.h"
|
||||
|
||||
#ifndef SO_UPDATE_CONNECT_CONTEXT
|
||||
/* Mingw is sometimes missing this */
|
||||
#define SO_UPDATE_CONNECT_CONTEXT 0x7010
|
||||
#endif
|
||||
|
||||
/* prototypes */
|
||||
static int be_async_enable(struct bufferevent *, short);
|
||||
static int be_async_disable(struct bufferevent *, short);
|
||||
static void be_async_destruct(struct bufferevent *);
|
||||
static int be_async_flush(struct bufferevent *, short, enum bufferevent_flush_mode);
|
||||
static int be_async_ctrl(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
|
||||
|
||||
struct bufferevent_async {
|
||||
struct bufferevent_private bev;
|
||||
struct event_overlapped connect_overlapped;
|
||||
struct event_overlapped read_overlapped;
|
||||
struct event_overlapped write_overlapped;
|
||||
size_t read_in_progress;
|
||||
size_t write_in_progress;
|
||||
unsigned ok : 1;
|
||||
unsigned read_added : 1;
|
||||
unsigned write_added : 1;
|
||||
};
|
||||
|
||||
const struct bufferevent_ops bufferevent_ops_async = {
|
||||
"socket_async",
|
||||
evutil_offsetof(struct bufferevent_async, bev.bev),
|
||||
be_async_enable,
|
||||
be_async_disable,
|
||||
NULL, /* Unlink */
|
||||
be_async_destruct,
|
||||
bufferevent_generic_adj_timeouts_,
|
||||
be_async_flush,
|
||||
be_async_ctrl,
|
||||
};
|
||||
|
||||
static inline void
|
||||
be_async_run_eventcb(struct bufferevent *bev, short what, int options)
|
||||
{ bufferevent_run_eventcb_(bev, what, options|BEV_TRIG_DEFER_CALLBACKS); }
|
||||
|
||||
static inline void
|
||||
be_async_trigger_nolock(struct bufferevent *bev, short what, int options)
|
||||
{ bufferevent_trigger_nolock_(bev, what, options|BEV_TRIG_DEFER_CALLBACKS); }
|
||||
|
||||
static inline int
|
||||
fatal_error(int err)
|
||||
{
|
||||
switch (err) {
|
||||
/* We may have already associated this fd with a port.
|
||||
* Let's hope it's this port, and that the error code
|
||||
* for doing this neer changes. */
|
||||
case ERROR_INVALID_PARAMETER:
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static inline struct bufferevent_async *
|
||||
upcast(struct bufferevent *bev)
|
||||
{
|
||||
struct bufferevent_async *bev_a;
|
||||
if (!BEV_IS_ASYNC(bev))
|
||||
return NULL;
|
||||
bev_a = EVUTIL_UPCAST(bev, struct bufferevent_async, bev.bev);
|
||||
return bev_a;
|
||||
}
|
||||
|
||||
static inline struct bufferevent_async *
|
||||
upcast_connect(struct event_overlapped *eo)
|
||||
{
|
||||
struct bufferevent_async *bev_a;
|
||||
bev_a = EVUTIL_UPCAST(eo, struct bufferevent_async, connect_overlapped);
|
||||
EVUTIL_ASSERT(BEV_IS_ASYNC(&bev_a->bev.bev));
|
||||
return bev_a;
|
||||
}
|
||||
|
||||
static inline struct bufferevent_async *
|
||||
upcast_read(struct event_overlapped *eo)
|
||||
{
|
||||
struct bufferevent_async *bev_a;
|
||||
bev_a = EVUTIL_UPCAST(eo, struct bufferevent_async, read_overlapped);
|
||||
EVUTIL_ASSERT(BEV_IS_ASYNC(&bev_a->bev.bev));
|
||||
return bev_a;
|
||||
}
|
||||
|
||||
static inline struct bufferevent_async *
|
||||
upcast_write(struct event_overlapped *eo)
|
||||
{
|
||||
struct bufferevent_async *bev_a;
|
||||
bev_a = EVUTIL_UPCAST(eo, struct bufferevent_async, write_overlapped);
|
||||
EVUTIL_ASSERT(BEV_IS_ASYNC(&bev_a->bev.bev));
|
||||
return bev_a;
|
||||
}
|
||||
|
||||
static void
|
||||
bev_async_del_write(struct bufferevent_async *beva)
|
||||
{
|
||||
struct bufferevent *bev = &beva->bev.bev;
|
||||
|
||||
if (beva->write_added) {
|
||||
beva->write_added = 0;
|
||||
event_base_del_virtual_(bev->ev_base);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bev_async_del_read(struct bufferevent_async *beva)
|
||||
{
|
||||
struct bufferevent *bev = &beva->bev.bev;
|
||||
|
||||
if (beva->read_added) {
|
||||
beva->read_added = 0;
|
||||
event_base_del_virtual_(bev->ev_base);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bev_async_add_write(struct bufferevent_async *beva)
|
||||
{
|
||||
struct bufferevent *bev = &beva->bev.bev;
|
||||
|
||||
if (!beva->write_added) {
|
||||
beva->write_added = 1;
|
||||
event_base_add_virtual_(bev->ev_base);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bev_async_add_read(struct bufferevent_async *beva)
|
||||
{
|
||||
struct bufferevent *bev = &beva->bev.bev;
|
||||
|
||||
if (!beva->read_added) {
|
||||
beva->read_added = 1;
|
||||
event_base_add_virtual_(bev->ev_base);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bev_async_consider_writing(struct bufferevent_async *beva)
|
||||
{
|
||||
size_t at_most;
|
||||
int limit;
|
||||
struct bufferevent *bev = &beva->bev.bev;
|
||||
|
||||
/* Don't write if there's a write in progress, or we do not
|
||||
* want to write, or when there's nothing left to write. */
|
||||
if (beva->write_in_progress || beva->bev.connecting)
|
||||
return;
|
||||
if (!beva->ok || !(bev->enabled&EV_WRITE) ||
|
||||
!evbuffer_get_length(bev->output)) {
|
||||
bev_async_del_write(beva);
|
||||
return;
|
||||
}
|
||||
|
||||
at_most = evbuffer_get_length(bev->output);
|
||||
|
||||
/* 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);
|
||||
if (at_most >= (size_t)limit && limit >= 0)
|
||||
at_most = limit;
|
||||
|
||||
if (beva->bev.write_suspended) {
|
||||
bev_async_del_write(beva);
|
||||
return;
|
||||
}
|
||||
|
||||
/* XXXX doesn't respect low-water mark very well. */
|
||||
bufferevent_incref_(bev);
|
||||
if (evbuffer_launch_write_(bev->output, at_most,
|
||||
&beva->write_overlapped)) {
|
||||
bufferevent_decref_(bev);
|
||||
beva->ok = 0;
|
||||
be_async_run_eventcb(bev, BEV_EVENT_ERROR, 0);
|
||||
} else {
|
||||
beva->write_in_progress = at_most;
|
||||
bufferevent_decrement_write_buckets_(&beva->bev, at_most);
|
||||
bev_async_add_write(beva);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bev_async_consider_reading(struct bufferevent_async *beva)
|
||||
{
|
||||
size_t cur_size;
|
||||
size_t read_high;
|
||||
size_t at_most;
|
||||
int limit;
|
||||
struct bufferevent *bev = &beva->bev.bev;
|
||||
|
||||
/* Don't read if there is a read in progress, or we do not
|
||||
* want to read. */
|
||||
if (beva->read_in_progress || beva->bev.connecting)
|
||||
return;
|
||||
if (!beva->ok || !(bev->enabled&EV_READ)) {
|
||||
bev_async_del_read(beva);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Don't read if we're full */
|
||||
cur_size = evbuffer_get_length(bev->input);
|
||||
read_high = bev->wm_read.high;
|
||||
if (read_high) {
|
||||
if (cur_size >= read_high) {
|
||||
bev_async_del_read(beva);
|
||||
return;
|
||||
}
|
||||
at_most = read_high - cur_size;
|
||||
} else {
|
||||
at_most = 16384; /* FIXME totally magic. */
|
||||
}
|
||||
|
||||
/* XXXX This over-commits. */
|
||||
/* 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;
|
||||
|
||||
if (beva->bev.read_suspended) {
|
||||
bev_async_del_read(beva);
|
||||
return;
|
||||
}
|
||||
|
||||
bufferevent_incref_(bev);
|
||||
if (evbuffer_launch_read_(bev->input, at_most, &beva->read_overlapped)) {
|
||||
beva->ok = 0;
|
||||
be_async_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);
|
||||
bev_async_add_read(beva);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static void
|
||||
be_async_outbuf_callback(struct evbuffer *buf,
|
||||
const struct evbuffer_cb_info *cbinfo,
|
||||
void *arg)
|
||||
{
|
||||
struct bufferevent *bev = arg;
|
||||
struct bufferevent_async *bev_async = upcast(bev);
|
||||
|
||||
/* If we added data to the outbuf and were not writing before,
|
||||
* we may want to write now. */
|
||||
|
||||
bufferevent_incref_and_lock_(bev);
|
||||
|
||||
if (cbinfo->n_added)
|
||||
bev_async_consider_writing(bev_async);
|
||||
|
||||
bufferevent_decref_and_unlock_(bev);
|
||||
}
|
||||
|
||||
static void
|
||||
be_async_inbuf_callback(struct evbuffer *buf,
|
||||
const struct evbuffer_cb_info *cbinfo,
|
||||
void *arg)
|
||||
{
|
||||
struct bufferevent *bev = arg;
|
||||
struct bufferevent_async *bev_async = upcast(bev);
|
||||
|
||||
/* If we drained data from the inbuf and were not reading before,
|
||||
* we may want to read now */
|
||||
|
||||
bufferevent_incref_and_lock_(bev);
|
||||
|
||||
if (cbinfo->n_deleted)
|
||||
bev_async_consider_reading(bev_async);
|
||||
|
||||
bufferevent_decref_and_unlock_(bev);
|
||||
}
|
||||
|
||||
static int
|
||||
be_async_enable(struct bufferevent *buf, short what)
|
||||
{
|
||||
struct bufferevent_async *bev_async = upcast(buf);
|
||||
|
||||
if (!bev_async->ok)
|
||||
return -1;
|
||||
|
||||
if (bev_async->bev.connecting) {
|
||||
/* Don't launch anything during connection attempts. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (what & EV_READ)
|
||||
BEV_RESET_GENERIC_READ_TIMEOUT(buf);
|
||||
if (what & EV_WRITE)
|
||||
BEV_RESET_GENERIC_WRITE_TIMEOUT(buf);
|
||||
|
||||
/* If we newly enable reading or writing, and we aren't reading or
|
||||
writing already, consider launching a new read or write. */
|
||||
|
||||
if (what & EV_READ)
|
||||
bev_async_consider_reading(bev_async);
|
||||
if (what & EV_WRITE)
|
||||
bev_async_consider_writing(bev_async);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
be_async_disable(struct bufferevent *bev, short what)
|
||||
{
|
||||
struct bufferevent_async *bev_async = upcast(bev);
|
||||
/* XXXX If we disable reading or writing, we may want to consider
|
||||
* canceling any in-progress read or write operation, though it might
|
||||
* not work. */
|
||||
|
||||
if (what & EV_READ) {
|
||||
BEV_DEL_GENERIC_READ_TIMEOUT(bev);
|
||||
bev_async_del_read(bev_async);
|
||||
}
|
||||
if (what & EV_WRITE) {
|
||||
BEV_DEL_GENERIC_WRITE_TIMEOUT(bev);
|
||||
bev_async_del_write(bev_async);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
be_async_destruct(struct bufferevent *bev)
|
||||
{
|
||||
struct bufferevent_async *bev_async = upcast(bev);
|
||||
struct bufferevent_private *bev_p = BEV_UPCAST(bev);
|
||||
evutil_socket_t fd;
|
||||
|
||||
EVUTIL_ASSERT(!upcast(bev)->write_in_progress &&
|
||||
!upcast(bev)->read_in_progress);
|
||||
|
||||
bev_async_del_read(bev_async);
|
||||
bev_async_del_write(bev_async);
|
||||
|
||||
fd = evbuffer_overlapped_get_fd_(bev->input);
|
||||
if (fd != (evutil_socket_t)EVUTIL_INVALID_SOCKET &&
|
||||
(bev_p->options & BEV_OPT_CLOSE_ON_FREE)) {
|
||||
evutil_closesocket(fd);
|
||||
evbuffer_overlapped_set_fd_(bev->input, EVUTIL_INVALID_SOCKET);
|
||||
}
|
||||
}
|
||||
|
||||
/* GetQueuedCompletionStatus doesn't reliably yield WSA error codes, so
|
||||
* we use WSAGetOverlappedResult to translate. */
|
||||
static void
|
||||
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);
|
||||
WSAGetOverlappedResult(fd, &eo->overlapped, &bytes, FALSE, &flags);
|
||||
}
|
||||
|
||||
static int
|
||||
be_async_flush(struct bufferevent *bev, short what,
|
||||
enum bufferevent_flush_mode mode)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
connect_complete(struct event_overlapped *eo, ev_uintptr_t key,
|
||||
ev_ssize_t nbytes, int ok)
|
||||
{
|
||||
struct bufferevent_async *bev_a = upcast_connect(eo);
|
||||
struct bufferevent *bev = &bev_a->bev.bev;
|
||||
evutil_socket_t sock;
|
||||
|
||||
BEV_LOCK(bev);
|
||||
|
||||
EVUTIL_ASSERT(bev_a->bev.connecting);
|
||||
bev_a->bev.connecting = 0;
|
||||
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);
|
||||
else
|
||||
bev_async_set_wsa_error(bev, eo);
|
||||
|
||||
be_async_run_eventcb(bev, ok ? BEV_EVENT_CONNECTED : BEV_EVENT_ERROR, 0);
|
||||
|
||||
event_base_del_virtual_(bev->ev_base);
|
||||
|
||||
bufferevent_decref_and_unlock_(bev);
|
||||
}
|
||||
|
||||
static void
|
||||
read_complete(struct event_overlapped *eo, ev_uintptr_t key,
|
||||
ev_ssize_t nbytes, int ok)
|
||||
{
|
||||
struct bufferevent_async *bev_a = upcast_read(eo);
|
||||
struct bufferevent *bev = &bev_a->bev.bev;
|
||||
short what = BEV_EVENT_READING;
|
||||
ev_ssize_t amount_unread;
|
||||
BEV_LOCK(bev);
|
||||
EVUTIL_ASSERT(bev_a->read_in_progress);
|
||||
|
||||
amount_unread = bev_a->read_in_progress - 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);
|
||||
|
||||
if (!ok)
|
||||
bev_async_set_wsa_error(bev, eo);
|
||||
|
||||
if (bev_a->ok) {
|
||||
if (ok && nbytes) {
|
||||
BEV_RESET_GENERIC_READ_TIMEOUT(bev);
|
||||
be_async_trigger_nolock(bev, EV_READ, 0);
|
||||
bev_async_consider_reading(bev_a);
|
||||
} else if (!ok) {
|
||||
what |= BEV_EVENT_ERROR;
|
||||
bev_a->ok = 0;
|
||||
be_async_run_eventcb(bev, what, 0);
|
||||
} else if (!nbytes) {
|
||||
what |= BEV_EVENT_EOF;
|
||||
bev_a->ok = 0;
|
||||
be_async_run_eventcb(bev, what, 0);
|
||||
}
|
||||
}
|
||||
|
||||
bufferevent_decref_and_unlock_(bev);
|
||||
}
|
||||
|
||||
static void
|
||||
write_complete(struct event_overlapped *eo, ev_uintptr_t key,
|
||||
ev_ssize_t nbytes, int ok)
|
||||
{
|
||||
struct bufferevent_async *bev_a = upcast_write(eo);
|
||||
struct bufferevent *bev = &bev_a->bev.bev;
|
||||
short what = BEV_EVENT_WRITING;
|
||||
ev_ssize_t amount_unwritten;
|
||||
|
||||
BEV_LOCK(bev);
|
||||
EVUTIL_ASSERT(bev_a->write_in_progress);
|
||||
|
||||
amount_unwritten = bev_a->write_in_progress - nbytes;
|
||||
evbuffer_commit_write_(bev->output, nbytes);
|
||||
bev_a->write_in_progress = 0;
|
||||
|
||||
if (amount_unwritten)
|
||||
bufferevent_decrement_write_buckets_(&bev_a->bev,
|
||||
-amount_unwritten);
|
||||
|
||||
|
||||
if (!ok)
|
||||
bev_async_set_wsa_error(bev, eo);
|
||||
|
||||
if (bev_a->ok) {
|
||||
if (ok && nbytes) {
|
||||
BEV_RESET_GENERIC_WRITE_TIMEOUT(bev);
|
||||
be_async_trigger_nolock(bev, EV_WRITE, 0);
|
||||
bev_async_consider_writing(bev_a);
|
||||
} else if (!ok) {
|
||||
what |= BEV_EVENT_ERROR;
|
||||
bev_a->ok = 0;
|
||||
be_async_run_eventcb(bev, what, 0);
|
||||
} else if (!nbytes) {
|
||||
what |= BEV_EVENT_EOF;
|
||||
bev_a->ok = 0;
|
||||
be_async_run_eventcb(bev, what, 0);
|
||||
}
|
||||
}
|
||||
|
||||
bufferevent_decref_and_unlock_(bev);
|
||||
}
|
||||
|
||||
struct bufferevent *
|
||||
bufferevent_async_new_(struct event_base *base,
|
||||
evutil_socket_t fd, int options)
|
||||
{
|
||||
struct bufferevent_async *bev_a;
|
||||
struct bufferevent *bev;
|
||||
struct event_iocp_port *iocp;
|
||||
|
||||
options |= BEV_OPT_THREADSAFE;
|
||||
|
||||
if (!(iocp = event_base_get_iocp_(base)))
|
||||
return NULL;
|
||||
|
||||
if (fd >= 0 && event_iocp_port_associate_(iocp, fd, 1)<0) {
|
||||
if (fatal_error(GetLastError()))
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!(bev_a = mm_calloc(1, sizeof(struct bufferevent_async))))
|
||||
return NULL;
|
||||
|
||||
bev = &bev_a->bev.bev;
|
||||
if (!(bev->input = evbuffer_overlapped_new_(fd))) {
|
||||
mm_free(bev_a);
|
||||
return NULL;
|
||||
}
|
||||
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,
|
||||
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);
|
||||
|
||||
bufferevent_init_generic_timeout_cbs_(bev);
|
||||
|
||||
bev_a->ok = fd >= 0;
|
||||
|
||||
return bev;
|
||||
err:
|
||||
bufferevent_free(&bev_a->bev.bev);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
bufferevent_async_set_connected_(struct bufferevent *bev)
|
||||
{
|
||||
struct bufferevent_async *bev_async = upcast(bev);
|
||||
bev_async->ok = 1;
|
||||
/* Now's a good time to consider reading/writing */
|
||||
be_async_enable(bev, bev->enabled);
|
||||
}
|
||||
|
||||
int
|
||||
bufferevent_async_can_connect_(struct bufferevent *bev)
|
||||
{
|
||||
const struct win32_extension_fns *ext =
|
||||
event_get_win32_extension_fns_();
|
||||
|
||||
if (BEV_IS_ASYNC(bev) &&
|
||||
event_base_get_iocp_(bev->ev_base) &&
|
||||
ext && ext->ConnectEx)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
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_();
|
||||
|
||||
EVUTIL_ASSERT(ext && ext->ConnectEx && fd >= 0 && sa != NULL);
|
||||
|
||||
/* ConnectEx() requires that the socket be bound to an address
|
||||
* with bind() before using, otherwise it will fail. We attempt
|
||||
* to issue a bind() here, taking into account that the error
|
||||
* code is set to WSAEINVAL when the socket is already bound. */
|
||||
memset(&ss, 0, sizeof(ss));
|
||||
if (sa->sa_family == AF_INET) {
|
||||
struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
|
||||
sin->sin_family = AF_INET;
|
||||
sin->sin_addr.s_addr = INADDR_ANY;
|
||||
} else if (sa->sa_family == AF_INET6) {
|
||||
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
|
||||
sin6->sin6_family = AF_INET6;
|
||||
sin6->sin6_addr = in6addr_any;
|
||||
} else {
|
||||
/* Well, the user will have to bind() */
|
||||
return -1;
|
||||
}
|
||||
if (bind(fd, (struct sockaddr *)&ss, sizeof(ss)) < 0 &&
|
||||
WSAGetLastError() != WSAEINVAL)
|
||||
return -1;
|
||||
|
||||
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);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int
|
||||
be_async_ctrl(struct bufferevent *bev, enum bufferevent_ctrl_op op,
|
||||
union bufferevent_ctrl_data *data)
|
||||
{
|
||||
switch (op) {
|
||||
case BEV_CTRL_GET_FD:
|
||||
data->fd = evbuffer_overlapped_get_fd_(bev->input);
|
||||
return 0;
|
||||
case BEV_CTRL_SET_FD: {
|
||||
struct bufferevent_async *bev_a = upcast(bev);
|
||||
struct event_iocp_port *iocp;
|
||||
|
||||
if (data->fd == evbuffer_overlapped_get_fd_(bev->input))
|
||||
return 0;
|
||||
if (!(iocp = event_base_get_iocp_(bev->ev_base)))
|
||||
return -1;
|
||||
if (event_iocp_port_associate_(iocp, data->fd, 1) < 0) {
|
||||
if (fatal_error(GetLastError()))
|
||||
return -1;
|
||||
}
|
||||
evbuffer_overlapped_set_fd_(bev->input, data->fd);
|
||||
evbuffer_overlapped_set_fd_(bev->output, data->fd);
|
||||
bev_a->ok = data->fd >= 0;
|
||||
return 0;
|
||||
}
|
||||
case BEV_CTRL_CANCEL_ALL: {
|
||||
struct bufferevent_async *bev_a = upcast(bev);
|
||||
evutil_socket_t fd = evbuffer_overlapped_get_fd_(bev->input);
|
||||
if (fd != (evutil_socket_t)EVUTIL_INVALID_SOCKET &&
|
||||
(bev_a->bev.options & BEV_OPT_CLOSE_ON_FREE)) {
|
||||
closesocket(fd);
|
||||
evbuffer_overlapped_set_fd_(bev->input, EVUTIL_INVALID_SOCKET);
|
||||
}
|
||||
bev_a->ok = 0;
|
||||
return 0;
|
||||
}
|
||||
case BEV_CTRL_GET_UNDERLYING:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,625 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
|
||||
* Copyright (c) 2002-2006 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.
|
||||
* 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 "evconfig-private.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "event2/event-config.h"
|
||||
|
||||
#ifdef EVENT__HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifdef EVENT__HAVE_STDARG_H
|
||||
#include <stdarg.h>
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
|
||||
#include "event2/util.h"
|
||||
#include "event2/bufferevent.h"
|
||||
#include "event2/buffer.h"
|
||||
#include "event2/bufferevent_struct.h"
|
||||
#include "event2/event.h"
|
||||
#include "log-internal.h"
|
||||
#include "mm-internal.h"
|
||||
#include "bufferevent-internal.h"
|
||||
#include "util-internal.h"
|
||||
|
||||
/* 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 *);
|
||||
static void be_filter_writecb(struct bufferevent *, void *);
|
||||
static void be_filter_eventcb(struct bufferevent *, short, void *);
|
||||
static int be_filter_flush(struct bufferevent *bufev,
|
||||
short iotype, enum bufferevent_flush_mode mode);
|
||||
static int be_filter_ctrl(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
|
||||
|
||||
static void bufferevent_filtered_inbuf_cb(struct evbuffer *buf,
|
||||
const struct evbuffer_cb_info *cbinfo, void *arg);
|
||||
|
||||
static void bufferevent_filtered_outbuf_cb(struct evbuffer *buf,
|
||||
const struct evbuffer_cb_info *info, void *arg);
|
||||
|
||||
struct bufferevent_filtered {
|
||||
struct bufferevent_private bev;
|
||||
|
||||
/** The bufferevent that we read/write filtered data from/to. */
|
||||
struct bufferevent *underlying;
|
||||
/** A callback on our inbuf to notice somebory removes data */
|
||||
struct evbuffer_cb_entry *inbuf_cb;
|
||||
/** A callback on our outbuf to notice when somebody adds data */
|
||||
struct evbuffer_cb_entry *outbuf_cb;
|
||||
/** True iff we have received an EOF callback from the underlying
|
||||
* bufferevent. */
|
||||
unsigned got_eof;
|
||||
|
||||
/** Function to free context when we're done. */
|
||||
void (*free_context)(void *);
|
||||
/** Input filter */
|
||||
bufferevent_filter_cb process_in;
|
||||
/** Output filter */
|
||||
bufferevent_filter_cb process_out;
|
||||
/** User-supplied argument to the filters. */
|
||||
void *context;
|
||||
};
|
||||
|
||||
const struct bufferevent_ops bufferevent_ops_filter = {
|
||||
"filter",
|
||||
evutil_offsetof(struct bufferevent_filtered, bev.bev),
|
||||
be_filter_enable,
|
||||
be_filter_disable,
|
||||
be_filter_unlink,
|
||||
be_filter_destruct,
|
||||
bufferevent_generic_adj_timeouts_,
|
||||
be_filter_flush,
|
||||
be_filter_ctrl,
|
||||
};
|
||||
|
||||
/* Given a bufferevent that's really the bev filter of a bufferevent_filtered,
|
||||
* return that bufferevent_filtered. Returns NULL otherwise.*/
|
||||
static inline struct bufferevent_filtered *
|
||||
upcast(struct bufferevent *bev)
|
||||
{
|
||||
struct bufferevent_filtered *bev_f;
|
||||
if (!BEV_IS_FILTER(bev))
|
||||
return NULL;
|
||||
bev_f = (void*)( ((char*)bev) -
|
||||
evutil_offsetof(struct bufferevent_filtered, bev.bev));
|
||||
EVUTIL_ASSERT(BEV_IS_FILTER(&bev_f->bev.bev));
|
||||
return bev_f;
|
||||
}
|
||||
|
||||
#define downcast(bev_f) (&(bev_f)->bev.bev)
|
||||
|
||||
/** Return 1 iff bevf's underlying bufferevent's output buffer is at or
|
||||
* over its high watermark such that we should not write to it in a given
|
||||
* flush mode. */
|
||||
static int
|
||||
be_underlying_writebuf_full(struct bufferevent_filtered *bevf,
|
||||
enum bufferevent_flush_mode state)
|
||||
{
|
||||
struct bufferevent *u = bevf->underlying;
|
||||
return state == BEV_NORMAL &&
|
||||
u->wm_write.high &&
|
||||
evbuffer_get_length(u->output) >= u->wm_write.high;
|
||||
}
|
||||
|
||||
/** Return 1 if our input buffer is at or over its high watermark such that we
|
||||
* should not write to it in a given flush mode. */
|
||||
static int
|
||||
be_readbuf_full(struct bufferevent_filtered *bevf,
|
||||
enum bufferevent_flush_mode state)
|
||||
{
|
||||
struct bufferevent *bufev = downcast(bevf);
|
||||
return state == BEV_NORMAL &&
|
||||
bufev->wm_read.high &&
|
||||
evbuffer_get_length(bufev->input) >= bufev->wm_read.high;
|
||||
}
|
||||
|
||||
|
||||
/* Filter to use when we're created with a NULL filter. */
|
||||
static enum bufferevent_filter_result
|
||||
be_null_filter(struct evbuffer *src, struct evbuffer *dst, ev_ssize_t lim,
|
||||
enum bufferevent_flush_mode state, void *ctx)
|
||||
{
|
||||
(void)state;
|
||||
if (evbuffer_remove_buffer(src, dst, lim) >= 0)
|
||||
return BEV_OK;
|
||||
else
|
||||
return BEV_ERROR;
|
||||
}
|
||||
|
||||
struct bufferevent *
|
||||
bufferevent_filter_new(struct bufferevent *underlying,
|
||||
bufferevent_filter_cb input_filter,
|
||||
bufferevent_filter_cb output_filter,
|
||||
int options,
|
||||
void (*free_context)(void *),
|
||||
void *ctx)
|
||||
{
|
||||
struct bufferevent_filtered *bufev_f;
|
||||
int tmp_options = options & ~BEV_OPT_THREADSAFE;
|
||||
|
||||
if (!underlying)
|
||||
return NULL;
|
||||
|
||||
if (!input_filter)
|
||||
input_filter = be_null_filter;
|
||||
if (!output_filter)
|
||||
output_filter = be_null_filter;
|
||||
|
||||
bufev_f = mm_calloc(1, sizeof(struct bufferevent_filtered));
|
||||
if (!bufev_f)
|
||||
return NULL;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
bufev_f->underlying = underlying;
|
||||
|
||||
bufev_f->process_in = input_filter;
|
||||
bufev_f->process_out = output_filter;
|
||||
bufev_f->free_context = free_context;
|
||||
bufev_f->context = ctx;
|
||||
|
||||
bufferevent_setcb(bufev_f->underlying,
|
||||
be_filter_readcb, be_filter_writecb, be_filter_eventcb, bufev_f);
|
||||
|
||||
bufev_f->inbuf_cb = evbuffer_add_cb(downcast(bufev_f)->input,
|
||||
bufferevent_filtered_inbuf_cb, bufev_f);
|
||||
evbuffer_cb_clear_flags(downcast(bufev_f)->input, bufev_f->inbuf_cb,
|
||||
EVBUFFER_CB_ENABLED);
|
||||
|
||||
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_enable(underlying, EV_READ|EV_WRITE);
|
||||
bufferevent_suspend_read_(underlying, BEV_SUSPEND_FILT_READ);
|
||||
|
||||
return downcast(bufev_f);
|
||||
}
|
||||
|
||||
static void
|
||||
be_filter_unlink(struct bufferevent *bev)
|
||||
{
|
||||
struct bufferevent_filtered *bevf = upcast(bev);
|
||||
EVUTIL_ASSERT(bevf);
|
||||
|
||||
if (bevf->bev.options & BEV_OPT_CLOSE_ON_FREE) {
|
||||
/* 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.
|
||||
*/
|
||||
if (BEV_UPCAST(bevf->underlying)->refcnt < 2) {
|
||||
event_warnx("BEV_OPT_CLOSE_ON_FREE set on an "
|
||||
"bufferevent with too few references");
|
||||
} else {
|
||||
bufferevent_free(bevf->underlying);
|
||||
}
|
||||
} else {
|
||||
if (bevf->underlying) {
|
||||
if (bevf->underlying->errorcb == be_filter_eventcb)
|
||||
bufferevent_setcb(bevf->underlying,
|
||||
NULL, NULL, NULL, NULL);
|
||||
bufferevent_unsuspend_read_(bevf->underlying,
|
||||
BEV_SUSPEND_FILT_READ);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (bevf->inbuf_cb)
|
||||
evbuffer_remove_cb_entry(bev->input, bevf->inbuf_cb);
|
||||
|
||||
if (bevf->outbuf_cb)
|
||||
evbuffer_remove_cb_entry(bev->output, bevf->outbuf_cb);
|
||||
}
|
||||
|
||||
static int
|
||||
be_filter_enable(struct bufferevent *bev, short event)
|
||||
{
|
||||
struct bufferevent_filtered *bevf = upcast(bev);
|
||||
if (event & EV_WRITE)
|
||||
BEV_RESET_GENERIC_WRITE_TIMEOUT(bev);
|
||||
|
||||
if (event & EV_READ) {
|
||||
BEV_RESET_GENERIC_READ_TIMEOUT(bev);
|
||||
bufferevent_unsuspend_read_(bevf->underlying,
|
||||
BEV_SUSPEND_FILT_READ);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
be_filter_disable(struct bufferevent *bev, short event)
|
||||
{
|
||||
struct bufferevent_filtered *bevf = upcast(bev);
|
||||
if (event & EV_WRITE)
|
||||
BEV_DEL_GENERIC_WRITE_TIMEOUT(bev);
|
||||
if (event & EV_READ) {
|
||||
BEV_DEL_GENERIC_READ_TIMEOUT(bev);
|
||||
bufferevent_suspend_read_(bevf->underlying,
|
||||
BEV_SUSPEND_FILT_READ);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static enum bufferevent_filter_result
|
||||
be_filter_process_input(struct bufferevent_filtered *bevf,
|
||||
enum bufferevent_flush_mode state,
|
||||
int *processed_out)
|
||||
{
|
||||
enum bufferevent_filter_result res;
|
||||
struct bufferevent *bev = downcast(bevf);
|
||||
|
||||
if (state == BEV_NORMAL) {
|
||||
/* If we're in 'normal' mode, don't urge data on the filter
|
||||
* unless we're reading data and under our high-water mark.*/
|
||||
if (!(bev->enabled & EV_READ) ||
|
||||
be_readbuf_full(bevf, state))
|
||||
return BEV_OK;
|
||||
}
|
||||
|
||||
do {
|
||||
ev_ssize_t limit = -1;
|
||||
if (state == BEV_NORMAL && bev->wm_read.high)
|
||||
limit = bev->wm_read.high -
|
||||
evbuffer_get_length(bev->input);
|
||||
|
||||
res = bevf->process_in(bevf->underlying->input,
|
||||
bev->input, limit, state, bevf->context);
|
||||
|
||||
if (res == BEV_OK)
|
||||
*processed_out = 1;
|
||||
} while (res == BEV_OK &&
|
||||
(bev->enabled & EV_READ) &&
|
||||
evbuffer_get_length(bevf->underlying->input) &&
|
||||
!be_readbuf_full(bevf, state));
|
||||
|
||||
if (*processed_out)
|
||||
BEV_RESET_GENERIC_READ_TIMEOUT(bev);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static enum bufferevent_filter_result
|
||||
be_filter_process_output(struct bufferevent_filtered *bevf,
|
||||
enum bufferevent_flush_mode state,
|
||||
int *processed_out)
|
||||
{
|
||||
/* Requires references and lock: might call writecb */
|
||||
enum bufferevent_filter_result res = BEV_OK;
|
||||
struct bufferevent *bufev = downcast(bevf);
|
||||
int again = 0;
|
||||
|
||||
if (state == BEV_NORMAL) {
|
||||
/* If we're in 'normal' mode, don't urge data on the
|
||||
* filter unless we're writing data, and the underlying
|
||||
* bufferevent is accepting data, and we have data to
|
||||
* give the filter. If we're in 'flush' or 'finish',
|
||||
* call the filter no matter what. */
|
||||
if (!(bufev->enabled & EV_WRITE) ||
|
||||
be_underlying_writebuf_full(bevf, state) ||
|
||||
!evbuffer_get_length(bufev->output))
|
||||
return BEV_OK;
|
||||
}
|
||||
|
||||
/* disable the callback that calls this function
|
||||
when the user adds to the output buffer. */
|
||||
evbuffer_cb_clear_flags(bufev->output, bevf->outbuf_cb,
|
||||
EVBUFFER_CB_ENABLED);
|
||||
|
||||
do {
|
||||
int processed = 0;
|
||||
again = 0;
|
||||
|
||||
do {
|
||||
ev_ssize_t limit = -1;
|
||||
if (state == BEV_NORMAL &&
|
||||
bevf->underlying->wm_write.high)
|
||||
limit = bevf->underlying->wm_write.high -
|
||||
evbuffer_get_length(bevf->underlying->output);
|
||||
|
||||
res = bevf->process_out(downcast(bevf)->output,
|
||||
bevf->underlying->output,
|
||||
limit,
|
||||
state,
|
||||
bevf->context);
|
||||
|
||||
if (res == BEV_OK)
|
||||
processed = *processed_out = 1;
|
||||
} while (/* Stop if the filter wasn't successful...*/
|
||||
res == BEV_OK &&
|
||||
/* Or if we aren't writing any more. */
|
||||
(bufev->enabled & EV_WRITE) &&
|
||||
/* Of if we have nothing more to write and we are
|
||||
* not flushing. */
|
||||
evbuffer_get_length(bufev->output) &&
|
||||
/* Or if we have filled the underlying output buffer. */
|
||||
!be_underlying_writebuf_full(bevf,state));
|
||||
|
||||
if (processed) {
|
||||
/* call the write callback.*/
|
||||
bufferevent_trigger_nolock_(bufev, EV_WRITE, 0);
|
||||
|
||||
if (res == BEV_OK &&
|
||||
(bufev->enabled & EV_WRITE) &&
|
||||
evbuffer_get_length(bufev->output) &&
|
||||
!be_underlying_writebuf_full(bevf, state)) {
|
||||
again = 1;
|
||||
}
|
||||
}
|
||||
} while (again);
|
||||
|
||||
/* reenable the outbuf_cb */
|
||||
evbuffer_cb_set_flags(bufev->output,bevf->outbuf_cb,
|
||||
EVBUFFER_CB_ENABLED);
|
||||
|
||||
if (*processed_out)
|
||||
BEV_RESET_GENERIC_WRITE_TIMEOUT(bufev);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Called when the size of our outbuf changes. */
|
||||
static void
|
||||
bufferevent_filtered_outbuf_cb(struct evbuffer *buf,
|
||||
const struct evbuffer_cb_info *cbinfo, void *arg)
|
||||
{
|
||||
struct bufferevent_filtered *bevf = arg;
|
||||
struct bufferevent *bev = downcast(bevf);
|
||||
|
||||
if (cbinfo->n_added) {
|
||||
int processed_any = 0;
|
||||
/* Somebody added more data to the output buffer. Try to
|
||||
* process it, if we should. */
|
||||
bufferevent_incref_and_lock_(bev);
|
||||
be_filter_process_output(bevf, BEV_NORMAL, &processed_any);
|
||||
bufferevent_decref_and_unlock_(bev);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
be_filter_read_nolock_(struct bufferevent *underlying, void *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;
|
||||
|
||||
// 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) {
|
||||
|
||||
if (bevf->got_eof)
|
||||
state = BEV_FINISHED;
|
||||
else
|
||||
state = BEV_NORMAL;
|
||||
|
||||
/* 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);
|
||||
if (evbuffer_get_length(underlying->input) > 0 &&
|
||||
be_readbuf_full(bevf, state)) {
|
||||
/* data left in underlying buffer and filter input buffer
|
||||
* hit its read high watermark.
|
||||
* Schedule callback to avoid data gets stuck in underlying
|
||||
* input buffer.
|
||||
*/
|
||||
evbuffer_cb_set_flags(bufev->input, bevf->inbuf_cb,
|
||||
EVBUFFER_CB_ENABLED);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Called when the size of our inbuf changes. */
|
||||
static void
|
||||
bufferevent_filtered_inbuf_cb(struct evbuffer *buf,
|
||||
const struct evbuffer_cb_info *cbinfo, void *arg)
|
||||
{
|
||||
struct bufferevent_filtered *bevf = arg;
|
||||
enum bufferevent_flush_mode state;
|
||||
struct bufferevent *bev = downcast(bevf);
|
||||
|
||||
BEV_LOCK(bev);
|
||||
|
||||
if (bevf->got_eof)
|
||||
state = BEV_FINISHED;
|
||||
else
|
||||
state = BEV_NORMAL;
|
||||
|
||||
|
||||
if (!be_readbuf_full(bevf, state)) {
|
||||
/* opportunity to read data which was left in underlying
|
||||
* input buffer because filter input buffer hit read
|
||||
* high watermark.
|
||||
*/
|
||||
evbuffer_cb_clear_flags(bev->input, bevf->inbuf_cb,
|
||||
EVBUFFER_CB_ENABLED);
|
||||
if (evbuffer_get_length(bevf->underlying->input) > 0)
|
||||
be_filter_read_nolock_(bevf->underlying, bevf);
|
||||
}
|
||||
|
||||
BEV_UNLOCK(bev);
|
||||
}
|
||||
|
||||
/* Called when the underlying socket has read. */
|
||||
static void
|
||||
be_filter_readcb(struct bufferevent *underlying, void *me_)
|
||||
{
|
||||
struct bufferevent_filtered *bevf = me_;
|
||||
struct bufferevent *bev = downcast(bevf);
|
||||
|
||||
BEV_LOCK(bev);
|
||||
|
||||
be_filter_read_nolock_(underlying, me_);
|
||||
|
||||
BEV_UNLOCK(bev);
|
||||
}
|
||||
|
||||
/* Called when the underlying socket has drained enough that we can write to
|
||||
it. */
|
||||
static void
|
||||
be_filter_writecb(struct bufferevent *underlying, void *me_)
|
||||
{
|
||||
struct bufferevent_filtered *bevf = me_;
|
||||
struct bufferevent *bev = downcast(bevf);
|
||||
struct bufferevent_private *bufev_private = BEV_UPCAST(bev);
|
||||
int processed_any = 0;
|
||||
|
||||
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_)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
BEV_UNLOCK(bev);
|
||||
}
|
||||
|
||||
static int
|
||||
be_filter_flush(struct bufferevent *bufev,
|
||||
short iotype, enum bufferevent_flush_mode mode)
|
||||
{
|
||||
struct bufferevent_filtered *bevf = upcast(bufev);
|
||||
int processed_any = 0;
|
||||
EVUTIL_ASSERT(bevf);
|
||||
|
||||
bufferevent_incref_and_lock_(bufev);
|
||||
|
||||
if (iotype & EV_READ) {
|
||||
be_filter_process_input(bevf, mode, &processed_any);
|
||||
}
|
||||
if (iotype & EV_WRITE) {
|
||||
be_filter_process_output(bevf, mode, &processed_any);
|
||||
}
|
||||
/* XXX check the return value? */
|
||||
/* XXX does this want to recursively call lower-level flushes? */
|
||||
bufferevent_flush(bevf->underlying, iotype, mode);
|
||||
|
||||
bufferevent_decref_and_unlock_(bufev);
|
||||
|
||||
return processed_any;
|
||||
}
|
||||
|
||||
static int
|
||||
be_filter_ctrl(struct bufferevent *bev, enum bufferevent_ctrl_op op,
|
||||
union bufferevent_ctrl_data *data)
|
||||
{
|
||||
struct bufferevent_filtered *bevf;
|
||||
switch (op) {
|
||||
case BEV_CTRL_GET_UNDERLYING:
|
||||
bevf = upcast(bev);
|
||||
data->ptr = bevf->underlying;
|
||||
return 0;
|
||||
case BEV_CTRL_SET_FD:
|
||||
case BEV_CTRL_GET_FD:
|
||||
bevf = upcast(bev);
|
||||
|
||||
if (bevf->underlying &&
|
||||
bevf->underlying->be_ops &&
|
||||
bevf->underlying->be_ops->ctrl) {
|
||||
return (bevf->underlying->be_ops->ctrl)(bevf->underlying, op, data);
|
||||
}
|
||||
EVUTIL_FALLTHROUGH;
|
||||
|
||||
case BEV_CTRL_CANCEL_ALL:
|
||||
EVUTIL_FALLTHROUGH;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,365 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2012 Niels Provos, 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 <sys/types.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
|
||||
#include "event2/util.h"
|
||||
#include "event2/buffer.h"
|
||||
#include "event2/bufferevent.h"
|
||||
#include "event2/bufferevent_struct.h"
|
||||
#include "event2/event.h"
|
||||
#include "defer-internal.h"
|
||||
#include "bufferevent-internal.h"
|
||||
#include "mm-internal.h"
|
||||
#include "util-internal.h"
|
||||
|
||||
struct bufferevent_pair {
|
||||
struct bufferevent_private bev;
|
||||
struct bufferevent_pair *partner;
|
||||
/* For ->destruct() lock checking */
|
||||
struct bufferevent_pair *unlinked_partner;
|
||||
};
|
||||
|
||||
|
||||
/* Given a bufferevent that's really a bev part of a bufferevent_pair,
|
||||
* return that bufferevent_filtered. Returns NULL otherwise.*/
|
||||
static inline struct bufferevent_pair *
|
||||
upcast(struct bufferevent *bev)
|
||||
{
|
||||
struct bufferevent_pair *bev_p;
|
||||
if (!BEV_IS_PAIR(bev))
|
||||
return NULL;
|
||||
bev_p = EVUTIL_UPCAST(bev, struct bufferevent_pair, bev.bev);
|
||||
EVUTIL_ASSERT(BEV_IS_PAIR(&bev_p->bev.bev));
|
||||
return bev_p;
|
||||
}
|
||||
|
||||
#define downcast(bev_pair) (&(bev_pair)->bev.bev)
|
||||
|
||||
static inline void
|
||||
incref_and_lock(struct bufferevent *b)
|
||||
{
|
||||
struct bufferevent_pair *bevp;
|
||||
bufferevent_incref_and_lock_(b);
|
||||
bevp = upcast(b);
|
||||
if (bevp->partner)
|
||||
bufferevent_incref_and_lock_(downcast(bevp->partner));
|
||||
}
|
||||
|
||||
static inline void
|
||||
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);
|
||||
}
|
||||
|
||||
/* XXX Handle close */
|
||||
|
||||
static void be_pair_outbuf_cb(struct evbuffer *,
|
||||
const struct evbuffer_cb_info *, void *);
|
||||
|
||||
static struct bufferevent_pair *
|
||||
bufferevent_pair_elt_new(struct event_base *base,
|
||||
int options)
|
||||
{
|
||||
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,
|
||||
options)) {
|
||||
mm_free(bufev);
|
||||
return NULL;
|
||||
}
|
||||
if (!evbuffer_add_cb(bufev->bev.bev.output, be_pair_outbuf_cb, bufev)) {
|
||||
bufferevent_free(downcast(bufev));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bufferevent_init_generic_timeout_cbs_(&bufev->bev.bev);
|
||||
|
||||
return bufev;
|
||||
}
|
||||
|
||||
int
|
||||
bufferevent_pair_new(struct event_base *base, int options,
|
||||
struct bufferevent *pair[2])
|
||||
{
|
||||
struct bufferevent_pair *bufev1 = NULL, *bufev2 = NULL;
|
||||
int tmp_options;
|
||||
|
||||
options |= BEV_OPT_DEFER_CALLBACKS;
|
||||
tmp_options = options & ~BEV_OPT_THREADSAFE;
|
||||
|
||||
bufev1 = bufferevent_pair_elt_new(base, options);
|
||||
if (!bufev1)
|
||||
return -1;
|
||||
bufev2 = bufferevent_pair_elt_new(base, tmp_options);
|
||||
if (!bufev2) {
|
||||
bufferevent_free(downcast(bufev1));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (options & BEV_OPT_THREADSAFE) {
|
||||
/*XXXX check return */
|
||||
bufferevent_enable_locking_(downcast(bufev2), bufev1->bev.lock);
|
||||
}
|
||||
|
||||
bufev1->partner = bufev2;
|
||||
bufev2->partner = bufev1;
|
||||
|
||||
evbuffer_freeze(downcast(bufev1)->input, 0);
|
||||
evbuffer_freeze(downcast(bufev1)->output, 1);
|
||||
evbuffer_freeze(downcast(bufev2)->input, 0);
|
||||
evbuffer_freeze(downcast(bufev2)->output, 1);
|
||||
|
||||
pair[0] = downcast(bufev1);
|
||||
pair[1] = downcast(bufev2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
be_pair_transfer(struct bufferevent *src, struct bufferevent *dst,
|
||||
int ignore_wm)
|
||||
{
|
||||
size_t dst_size;
|
||||
size_t n;
|
||||
|
||||
evbuffer_unfreeze(src->output, 1);
|
||||
evbuffer_unfreeze(dst->input, 0);
|
||||
|
||||
if (dst->wm_read.high) {
|
||||
dst_size = evbuffer_get_length(dst->input);
|
||||
if (dst_size < dst->wm_read.high) {
|
||||
n = dst->wm_read.high - dst_size;
|
||||
evbuffer_remove_buffer(src->output, dst->input, n);
|
||||
} else {
|
||||
if (!ignore_wm)
|
||||
goto done;
|
||||
n = evbuffer_get_length(src->output);
|
||||
evbuffer_add_buffer(dst->input, src->output);
|
||||
}
|
||||
} else {
|
||||
n = evbuffer_get_length(src->output);
|
||||
evbuffer_add_buffer(dst->input, src->output);
|
||||
}
|
||||
|
||||
if (n) {
|
||||
BEV_RESET_GENERIC_READ_TIMEOUT(dst);
|
||||
|
||||
if (evbuffer_get_length(dst->output))
|
||||
BEV_RESET_GENERIC_WRITE_TIMEOUT(dst);
|
||||
else
|
||||
BEV_DEL_GENERIC_WRITE_TIMEOUT(dst);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
static inline int
|
||||
be_pair_wants_to_talk(struct bufferevent_pair *src,
|
||||
struct bufferevent_pair *dst)
|
||||
{
|
||||
return (downcast(src)->enabled & EV_WRITE) &&
|
||||
(downcast(dst)->enabled & EV_READ) &&
|
||||
!dst->bev.read_suspended &&
|
||||
evbuffer_get_length(downcast(src)->output);
|
||||
}
|
||||
|
||||
static void
|
||||
be_pair_outbuf_cb(struct evbuffer *outbuf,
|
||||
const struct evbuffer_cb_info *info, void *arg)
|
||||
{
|
||||
struct bufferevent_pair *bev_pair = arg;
|
||||
struct bufferevent_pair *partner = bev_pair->partner;
|
||||
|
||||
incref_and_lock(downcast(bev_pair));
|
||||
|
||||
if (info->n_added > info->n_deleted && partner) {
|
||||
/* We got more data. If the other side's reading, then
|
||||
hand it over. */
|
||||
if (be_pair_wants_to_talk(bev_pair, partner)) {
|
||||
be_pair_transfer(downcast(bev_pair), downcast(partner), 0);
|
||||
}
|
||||
}
|
||||
|
||||
decref_and_unlock(downcast(bev_pair));
|
||||
}
|
||||
|
||||
static int
|
||||
be_pair_enable(struct bufferevent *bufev, short events)
|
||||
{
|
||||
struct bufferevent_pair *bev_p = upcast(bufev);
|
||||
struct bufferevent_pair *partner = bev_p->partner;
|
||||
|
||||
incref_and_lock(bufev);
|
||||
|
||||
if (events & EV_READ) {
|
||||
BEV_RESET_GENERIC_READ_TIMEOUT(bufev);
|
||||
}
|
||||
if ((events & EV_WRITE) && evbuffer_get_length(bufev->output))
|
||||
BEV_RESET_GENERIC_WRITE_TIMEOUT(bufev);
|
||||
|
||||
/* We're starting to read! Does the other side have anything to write?*/
|
||||
if ((events & EV_READ) && partner &&
|
||||
be_pair_wants_to_talk(partner, bev_p)) {
|
||||
be_pair_transfer(downcast(partner), bufev, 0);
|
||||
}
|
||||
/* We're starting to write! Does the other side want to read? */
|
||||
if ((events & EV_WRITE) && partner &&
|
||||
be_pair_wants_to_talk(bev_p, partner)) {
|
||||
be_pair_transfer(bufev, downcast(partner), 0);
|
||||
}
|
||||
decref_and_unlock(bufev);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
be_pair_disable(struct bufferevent *bev, short events)
|
||||
{
|
||||
if (events & EV_READ) {
|
||||
BEV_DEL_GENERIC_READ_TIMEOUT(bev);
|
||||
}
|
||||
if (events & EV_WRITE) {
|
||||
BEV_DEL_GENERIC_WRITE_TIMEOUT(bev);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
be_pair_unlink(struct bufferevent *bev)
|
||||
{
|
||||
struct bufferevent_pair *bev_p = upcast(bev);
|
||||
|
||||
if (bev_p->partner) {
|
||||
bev_p->unlinked_partner = bev_p->partner;
|
||||
bev_p->partner->partner = NULL;
|
||||
bev_p->partner = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Free *shared* lock in the latest be (since we share it between two of them). */
|
||||
static void
|
||||
be_pair_destruct(struct bufferevent *bev)
|
||||
{
|
||||
struct bufferevent_pair *bev_p = upcast(bev);
|
||||
|
||||
/* Transfer ownership of the lock into partner, otherwise we will use
|
||||
* already free'd lock during freeing second bev, see next example:
|
||||
*
|
||||
* bev1->own_lock = 1
|
||||
* bev2->own_lock = 0
|
||||
* bev2->lock = bev1->lock
|
||||
*
|
||||
* bufferevent_free(bev1) # refcnt == 0 -> unlink
|
||||
* bufferevent_free(bev2) # refcnt == 0 -> unlink
|
||||
*
|
||||
* event_base_free() -> finilizers -> EVTHREAD_FREE_LOCK(bev1->lock)
|
||||
* -> BEV_LOCK(bev2->lock) <-- already freed
|
||||
*
|
||||
* Where bev1 == pair[0], bev2 == pair[1].
|
||||
*/
|
||||
if (bev_p->unlinked_partner && bev_p->bev.own_lock) {
|
||||
bev_p->unlinked_partner->bev.own_lock = 1;
|
||||
bev_p->bev.own_lock = 0;
|
||||
}
|
||||
bev_p->unlinked_partner = NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
be_pair_flush(struct bufferevent *bev, short iotype,
|
||||
enum bufferevent_flush_mode mode)
|
||||
{
|
||||
struct bufferevent_pair *bev_p = upcast(bev);
|
||||
struct bufferevent *partner;
|
||||
|
||||
if (!bev_p->partner)
|
||||
return -1;
|
||||
|
||||
if (mode == BEV_NORMAL)
|
||||
return 0;
|
||||
|
||||
incref_and_lock(bev);
|
||||
|
||||
partner = downcast(bev_p->partner);
|
||||
|
||||
if ((iotype & EV_READ) != 0)
|
||||
be_pair_transfer(partner, bev, 1);
|
||||
|
||||
if ((iotype & EV_WRITE) != 0)
|
||||
be_pair_transfer(bev, partner, 1);
|
||||
|
||||
if (mode == BEV_FINISHED) {
|
||||
short what = BEV_EVENT_EOF;
|
||||
if (iotype & EV_READ)
|
||||
what |= BEV_EVENT_WRITING;
|
||||
if (iotype & EV_WRITE)
|
||||
what |= BEV_EVENT_READING;
|
||||
bufferevent_run_eventcb_(partner, what, 0);
|
||||
}
|
||||
decref_and_unlock(bev);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct bufferevent *
|
||||
bufferevent_pair_get_partner(struct bufferevent *bev)
|
||||
{
|
||||
struct bufferevent_pair *bev_p;
|
||||
struct bufferevent *partner = NULL;
|
||||
bev_p = upcast(bev);
|
||||
if (! bev_p)
|
||||
return NULL;
|
||||
|
||||
incref_and_lock(bev);
|
||||
if (bev_p->partner)
|
||||
partner = downcast(bev_p->partner);
|
||||
decref_and_unlock(bev);
|
||||
return partner;
|
||||
}
|
||||
|
||||
const struct bufferevent_ops bufferevent_ops_pair = {
|
||||
"pair_elt",
|
||||
evutil_offsetof(struct bufferevent_pair, bev.bev),
|
||||
be_pair_enable,
|
||||
be_pair_disable,
|
||||
be_pair_unlink,
|
||||
be_pair_destruct,
|
||||
bufferevent_generic_adj_timeouts_,
|
||||
be_pair_flush,
|
||||
NULL, /* ctrl */
|
||||
};
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,714 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
|
||||
* Copyright (c) 2002-2006 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.
|
||||
* 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 <sys/types.h>
|
||||
|
||||
#ifdef EVENT__HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifdef EVENT__HAVE_STDARG_H
|
||||
#include <stdarg.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#endif
|
||||
|
||||
#ifdef EVENT__HAVE_SYS_SOCKET_H
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_NETINET_IN_H
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_NETINET_IN6_H
|
||||
#include <netinet/in6.h>
|
||||
#endif
|
||||
|
||||
#include "event2/util.h"
|
||||
#include "event2/bufferevent.h"
|
||||
#include "event2/buffer.h"
|
||||
#include "event2/bufferevent_struct.h"
|
||||
#include "event2/bufferevent_compat.h"
|
||||
#include "event2/event.h"
|
||||
#include "log-internal.h"
|
||||
#include "mm-internal.h"
|
||||
#include "bufferevent-internal.h"
|
||||
#include "util-internal.h"
|
||||
#ifdef _WIN32
|
||||
#include "iocp-internal.h"
|
||||
#endif
|
||||
|
||||
/* prototypes */
|
||||
static int be_socket_enable(struct bufferevent *, short);
|
||||
static int be_socket_disable(struct bufferevent *, short);
|
||||
static void be_socket_destruct(struct bufferevent *);
|
||||
static int be_socket_flush(struct bufferevent *, short, enum bufferevent_flush_mode);
|
||||
static int be_socket_ctrl(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
|
||||
|
||||
static void be_socket_setfd(struct bufferevent *, evutil_socket_t);
|
||||
|
||||
const struct bufferevent_ops bufferevent_ops_socket = {
|
||||
"socket",
|
||||
evutil_offsetof(struct bufferevent_private, bev),
|
||||
be_socket_enable,
|
||||
be_socket_disable,
|
||||
NULL, /* unlink */
|
||||
be_socket_destruct,
|
||||
bufferevent_generic_adj_existing_timeouts_,
|
||||
be_socket_flush,
|
||||
be_socket_ctrl,
|
||||
};
|
||||
|
||||
const struct sockaddr*
|
||||
bufferevent_socket_get_conn_address_(struct bufferevent *bev)
|
||||
{
|
||||
struct bufferevent_private *bev_p = BEV_UPCAST(bev);
|
||||
return (struct sockaddr *)&bev_p->conn_address;
|
||||
}
|
||||
|
||||
void
|
||||
bufferevent_socket_set_conn_address_fd_(struct bufferevent *bev,
|
||||
evutil_socket_t fd)
|
||||
{
|
||||
struct bufferevent_private *bev_p = BEV_UPCAST(bev);
|
||||
|
||||
socklen_t len = sizeof(bev_p->conn_address);
|
||||
|
||||
struct sockaddr *addr = (struct sockaddr *)&bev_p->conn_address;
|
||||
if (addr->sa_family != AF_UNSPEC)
|
||||
getpeername(fd, addr, &len);
|
||||
}
|
||||
|
||||
int
|
||||
bufferevent_socket_set_conn_address_(struct bufferevent *bev,
|
||||
struct sockaddr *addr, size_t addrlen)
|
||||
{
|
||||
struct bufferevent_private *bev_p = BEV_UPCAST(bev);
|
||||
if (addrlen <= sizeof(bev_p->conn_address)) {
|
||||
memcpy(&bev_p->conn_address, addr, addrlen);
|
||||
return 0;
|
||||
} else {
|
||||
return EVUTIL_EAI_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bufferevent_socket_outbuf_cb(struct evbuffer *buf,
|
||||
const struct evbuffer_cb_info *cbinfo,
|
||||
void *arg)
|
||||
{
|
||||
struct bufferevent *bufev = arg;
|
||||
struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
|
||||
|
||||
if (cbinfo->n_added &&
|
||||
(bufev->enabled & EV_WRITE) &&
|
||||
!event_pending(&bufev->ev_write, EV_WRITE, NULL) &&
|
||||
!bufev_p->write_suspended) {
|
||||
/* Somebody added data to the buffer, and we would like to
|
||||
* write, and we were not writing. So, start writing. */
|
||||
if (bufferevent_add_event_(&bufev->ev_write, &bufev->timeout_write) == -1) {
|
||||
/* Should we log this? */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bufferevent_readcb(evutil_socket_t fd, short event, void *arg)
|
||||
{
|
||||
struct bufferevent *bufev = arg;
|
||||
struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
|
||||
struct evbuffer *input;
|
||||
int res = 0;
|
||||
short what = BEV_EVENT_READING;
|
||||
ev_ssize_t howmuch = -1, readmax=-1;
|
||||
|
||||
bufferevent_incref_and_lock_(bufev);
|
||||
|
||||
if (event == EV_TIMEOUT) {
|
||||
/* Note that we only check for event==EV_TIMEOUT. If
|
||||
* event==EV_TIMEOUT|EV_READ, we can safely ignore the
|
||||
* timeout, since a read has occurred */
|
||||
what |= BEV_EVENT_TIMEOUT;
|
||||
goto error;
|
||||
}
|
||||
|
||||
input = bufev->input;
|
||||
|
||||
/*
|
||||
* If we have a high watermark configured then we don't want to
|
||||
* read more data than would make us reach the watermark.
|
||||
*/
|
||||
if (bufev->wm_read.high != 0) {
|
||||
howmuch = bufev->wm_read.high - evbuffer_get_length(input);
|
||||
/* we somehow lowered the watermark, stop reading */
|
||||
if (howmuch <= 0) {
|
||||
bufferevent_wm_suspend_read(bufev);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
readmax = bufferevent_get_read_max_(bufev_p);
|
||||
if (howmuch < 0 || howmuch > readmax) /* The use of -1 for "unlimited"
|
||||
* uglifies this code. XXXX */
|
||||
howmuch = readmax;
|
||||
if (bufev_p->read_suspended)
|
||||
goto done;
|
||||
|
||||
evbuffer_unfreeze(input, 0);
|
||||
res = evbuffer_read(input, fd, (int)howmuch); /* XXXX evbuffer_read would do better to take and return ev_ssize_t */
|
||||
evbuffer_freeze(input, 0);
|
||||
|
||||
if (res == -1) {
|
||||
int err = evutil_socket_geterror(fd);
|
||||
if (EVUTIL_ERR_RW_RETRIABLE(err))
|
||||
goto reschedule;
|
||||
if (EVUTIL_ERR_CONNECT_REFUSED(err)) {
|
||||
bufev_p->connection_refused = 1;
|
||||
goto done;
|
||||
}
|
||||
/* error case */
|
||||
what |= BEV_EVENT_ERROR;
|
||||
} else if (res == 0) {
|
||||
/* eof case */
|
||||
what |= BEV_EVENT_EOF;
|
||||
}
|
||||
|
||||
if (res <= 0)
|
||||
goto error;
|
||||
|
||||
bufferevent_decrement_read_buckets_(bufev_p, res);
|
||||
|
||||
/* Invoke the user callback - must always be called last */
|
||||
bufferevent_trigger_nolock_(bufev, EV_READ, 0);
|
||||
|
||||
goto done;
|
||||
|
||||
reschedule:
|
||||
goto done;
|
||||
|
||||
error:
|
||||
bufferevent_disable(bufev, EV_READ);
|
||||
bufferevent_run_eventcb_(bufev, what, 0);
|
||||
|
||||
done:
|
||||
bufferevent_decref_and_unlock_(bufev);
|
||||
}
|
||||
|
||||
static void
|
||||
bufferevent_writecb(evutil_socket_t fd, short event, void *arg)
|
||||
{
|
||||
struct bufferevent *bufev = arg;
|
||||
struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
|
||||
int res = 0;
|
||||
short what = BEV_EVENT_WRITING;
|
||||
int connected = 0;
|
||||
ev_ssize_t atmost = -1;
|
||||
|
||||
bufferevent_incref_and_lock_(bufev);
|
||||
|
||||
if (event == EV_TIMEOUT) {
|
||||
/* Note that we only check for event==EV_TIMEOUT. If
|
||||
* event==EV_TIMEOUT|EV_WRITE, we can safely ignore the
|
||||
* timeout, since a read has occurred */
|
||||
what |= BEV_EVENT_TIMEOUT;
|
||||
goto error;
|
||||
}
|
||||
if (bufev_p->connecting) {
|
||||
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) {
|
||||
bufev_p->connection_refused = 0;
|
||||
c = -1;
|
||||
}
|
||||
|
||||
if (c == 0)
|
||||
goto done;
|
||||
|
||||
bufev_p->connecting = 0;
|
||||
if (c < 0) {
|
||||
event_del(&bufev->ev_write);
|
||||
event_del(&bufev->ev_read);
|
||||
bufferevent_run_eventcb_(bufev, BEV_EVENT_ERROR, 0);
|
||||
goto done;
|
||||
} else {
|
||||
connected = 1;
|
||||
bufferevent_socket_set_conn_address_fd_(bufev, fd);
|
||||
#ifdef _WIN32
|
||||
if (BEV_IS_ASYNC(bufev)) {
|
||||
event_del(&bufev->ev_write);
|
||||
bufferevent_async_set_connected_(bufev);
|
||||
bufferevent_run_eventcb_(bufev,
|
||||
BEV_EVENT_CONNECTED, 0);
|
||||
goto done;
|
||||
}
|
||||
#endif
|
||||
bufferevent_run_eventcb_(bufev,
|
||||
BEV_EVENT_CONNECTED, 0);
|
||||
if (!(bufev->enabled & EV_WRITE) ||
|
||||
bufev_p->write_suspended) {
|
||||
event_del(&bufev->ev_write);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
atmost = bufferevent_get_write_max_(bufev_p);
|
||||
|
||||
if (bufev_p->write_suspended)
|
||||
goto done;
|
||||
|
||||
if (evbuffer_get_length(bufev->output)) {
|
||||
evbuffer_unfreeze(bufev->output, 1);
|
||||
res = evbuffer_write_atmost(bufev->output, fd, atmost);
|
||||
evbuffer_freeze(bufev->output, 1);
|
||||
if (res == -1) {
|
||||
int err = evutil_socket_geterror(fd);
|
||||
if (EVUTIL_ERR_RW_RETRIABLE(err))
|
||||
goto reschedule;
|
||||
what |= BEV_EVENT_ERROR;
|
||||
} else if (res == 0) {
|
||||
/* eof case
|
||||
XXXX Actually, a 0 on write doesn't indicate
|
||||
an EOF. An ECONNRESET might be more typical.
|
||||
*/
|
||||
what |= BEV_EVENT_EOF;
|
||||
}
|
||||
if (res <= 0)
|
||||
goto error;
|
||||
|
||||
bufferevent_decrement_write_buckets_(bufev_p, res);
|
||||
}
|
||||
|
||||
if (evbuffer_get_length(bufev->output) == 0) {
|
||||
event_del(&bufev->ev_write);
|
||||
}
|
||||
|
||||
/*
|
||||
* Invoke the user callback if our buffer is drained or below the
|
||||
* low watermark.
|
||||
*/
|
||||
if (res || !connected) {
|
||||
bufferevent_trigger_nolock_(bufev, EV_WRITE, 0);
|
||||
}
|
||||
|
||||
goto done;
|
||||
|
||||
reschedule:
|
||||
if (evbuffer_get_length(bufev->output) == 0) {
|
||||
event_del(&bufev->ev_write);
|
||||
}
|
||||
goto done;
|
||||
|
||||
error:
|
||||
bufferevent_disable(bufev, EV_WRITE);
|
||||
bufferevent_run_eventcb_(bufev, what, 0);
|
||||
|
||||
done:
|
||||
bufferevent_decref_and_unlock_(bufev);
|
||||
}
|
||||
|
||||
struct bufferevent *
|
||||
bufferevent_socket_new(struct event_base *base, evutil_socket_t fd,
|
||||
int options)
|
||||
{
|
||||
struct bufferevent_private *bufev_p;
|
||||
struct bufferevent *bufev;
|
||||
|
||||
#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,
|
||||
options) < 0) {
|
||||
mm_free(bufev_p);
|
||||
return NULL;
|
||||
}
|
||||
bufev = &bufev_p->bev;
|
||||
evbuffer_set_flags(bufev->output, EVBUFFER_FLAG_DRAINS_TO_FD);
|
||||
|
||||
event_assign(&bufev->ev_read, bufev->ev_base, fd,
|
||||
EV_READ|EV_PERSIST|EV_FINALIZE, bufferevent_readcb, bufev);
|
||||
event_assign(&bufev->ev_write, bufev->ev_base, fd,
|
||||
EV_WRITE|EV_PERSIST|EV_FINALIZE, bufferevent_writecb, bufev);
|
||||
|
||||
evbuffer_add_cb(bufev->output, bufferevent_socket_outbuf_cb, bufev);
|
||||
|
||||
evbuffer_freeze(bufev->input, 0);
|
||||
evbuffer_freeze(bufev->output, 1);
|
||||
|
||||
return bufev;
|
||||
}
|
||||
|
||||
int
|
||||
bufferevent_socket_connect(struct bufferevent *bev,
|
||||
const struct sockaddr *sa, int socklen)
|
||||
{
|
||||
struct bufferevent_private *bufev_p = BEV_UPCAST(bev);
|
||||
|
||||
evutil_socket_t fd;
|
||||
int r = 0;
|
||||
int result=-1;
|
||||
int ownfd = 0;
|
||||
|
||||
bufferevent_incref_and_lock_(bev);
|
||||
|
||||
fd = bufferevent_getfd(bev);
|
||||
if (fd < 0) {
|
||||
if (!sa)
|
||||
goto done;
|
||||
fd = evutil_socket_(sa->sa_family,
|
||||
SOCK_STREAM|EVUTIL_SOCK_NONBLOCK, 0);
|
||||
if (fd < 0)
|
||||
goto freesock;
|
||||
ownfd = 1;
|
||||
}
|
||||
if (sa) {
|
||||
#ifdef _WIN32
|
||||
if (bufferevent_async_can_connect_(bev)) {
|
||||
bufferevent_setfd(bev, fd);
|
||||
r = bufferevent_async_connect_(bev, fd, sa, socklen);
|
||||
if (r < 0)
|
||||
goto freesock;
|
||||
bufev_p->connecting = 1;
|
||||
result = 0;
|
||||
goto done;
|
||||
} else
|
||||
#endif
|
||||
r = evutil_socket_connect_(&fd, sa, socklen);
|
||||
if (r < 0)
|
||||
goto freesock;
|
||||
}
|
||||
#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|EV_FINALIZE, bufferevent_writecb, bev);
|
||||
}
|
||||
#endif
|
||||
bufferevent_setfd(bev, fd);
|
||||
if (r == 0) {
|
||||
if (! be_socket_enable(bev, EV_WRITE)) {
|
||||
bufev_p->connecting = 1;
|
||||
result = 0;
|
||||
goto done;
|
||||
}
|
||||
} else if (r == 1) {
|
||||
/* The connect succeeded already. How very BSD of it. */
|
||||
result = 0;
|
||||
bufev_p->connecting = 1;
|
||||
bufferevent_trigger_nolock_(bev, EV_WRITE, BEV_OPT_DEFER_CALLBACKS);
|
||||
} else {
|
||||
/* The connect failed already. How very BSD of it. */
|
||||
result = 0;
|
||||
bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, BEV_OPT_DEFER_CALLBACKS);
|
||||
bufferevent_disable(bev, EV_WRITE|EV_READ);
|
||||
}
|
||||
|
||||
goto done;
|
||||
|
||||
freesock:
|
||||
if (ownfd)
|
||||
evutil_closesocket(fd);
|
||||
done:
|
||||
bufferevent_decref_and_unlock_(bev);
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
bufferevent_connect_getaddrinfo_cb(int result, struct evutil_addrinfo *ai,
|
||||
void *arg)
|
||||
{
|
||||
struct bufferevent *bev = arg;
|
||||
struct bufferevent_private *bev_p = BEV_UPCAST(bev);
|
||||
int r;
|
||||
BEV_LOCK(bev);
|
||||
|
||||
bufferevent_unsuspend_write_(bev, BEV_SUSPEND_LOOKUP);
|
||||
bufferevent_unsuspend_read_(bev, BEV_SUSPEND_LOOKUP);
|
||||
|
||||
bev_p->dns_request = NULL;
|
||||
|
||||
if (result == EVUTIL_EAI_CANCEL) {
|
||||
bev_p->dns_error = result;
|
||||
bufferevent_decref_and_unlock_(bev);
|
||||
return;
|
||||
}
|
||||
if (result == 0) {
|
||||
/* XXX use the other addrinfos? */
|
||||
result = bufferevent_socket_set_conn_address_(
|
||||
bev, ai->ai_addr, (int)ai->ai_addrlen);
|
||||
}
|
||||
if (result != 0) {
|
||||
bev_p->dns_error = result;
|
||||
bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, 0);
|
||||
bufferevent_decref_and_unlock_(bev);
|
||||
if (ai)
|
||||
evutil_freeaddrinfo(ai);
|
||||
return;
|
||||
}
|
||||
|
||||
r = bufferevent_socket_connect(bev, ai->ai_addr, (int)ai->ai_addrlen);
|
||||
if (r < 0)
|
||||
bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, 0);
|
||||
bufferevent_decref_and_unlock_(bev);
|
||||
evutil_freeaddrinfo(ai);
|
||||
}
|
||||
|
||||
int
|
||||
bufferevent_socket_connect_hostname(struct bufferevent *bev,
|
||||
struct evdns_base *evdns_base, int family, const char *hostname, int port)
|
||||
{
|
||||
char portbuf[10];
|
||||
struct evutil_addrinfo hint;
|
||||
struct bufferevent_private *bev_p = BEV_UPCAST(bev);
|
||||
|
||||
if (family != AF_INET && family != AF_INET6 && family != AF_UNSPEC)
|
||||
return -1;
|
||||
if (port < 1 || port > 65535)
|
||||
return -1;
|
||||
|
||||
memset(&hint, 0, sizeof(hint));
|
||||
hint.ai_family = family;
|
||||
hint.ai_protocol = IPPROTO_TCP;
|
||||
hint.ai_socktype = SOCK_STREAM;
|
||||
|
||||
evutil_snprintf(portbuf, sizeof(portbuf), "%d", port);
|
||||
|
||||
BEV_LOCK(bev);
|
||||
bev_p->dns_error = 0;
|
||||
|
||||
bufferevent_suspend_write_(bev, BEV_SUSPEND_LOOKUP);
|
||||
bufferevent_suspend_read_(bev, BEV_SUSPEND_LOOKUP);
|
||||
|
||||
bufferevent_incref_(bev);
|
||||
bev_p->dns_request = evutil_getaddrinfo_async_(evdns_base, hostname,
|
||||
portbuf, &hint, bufferevent_connect_getaddrinfo_cb, bev);
|
||||
BEV_UNLOCK(bev);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
bufferevent_socket_get_dns_error(struct bufferevent *bev)
|
||||
{
|
||||
int rv;
|
||||
struct bufferevent_private *bev_p = BEV_UPCAST(bev);
|
||||
|
||||
BEV_LOCK(bev);
|
||||
rv = bev_p->dns_error;
|
||||
BEV_UNLOCK(bev);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a new buffered event object.
|
||||
*
|
||||
* The read callback is invoked whenever we read new data.
|
||||
* The write callback is invoked whenever the output buffer is drained.
|
||||
* The error callback is invoked on a write/read error or on EOF.
|
||||
*
|
||||
* Both read and write callbacks maybe NULL. The error callback is not
|
||||
* allowed to be NULL and have to be provided always.
|
||||
*/
|
||||
|
||||
struct bufferevent *
|
||||
bufferevent_new(evutil_socket_t fd,
|
||||
bufferevent_data_cb readcb, bufferevent_data_cb writecb,
|
||||
bufferevent_event_cb eventcb, void *cbarg)
|
||||
{
|
||||
struct bufferevent *bufev;
|
||||
|
||||
if (!(bufev = bufferevent_socket_new(NULL, fd, 0)))
|
||||
return NULL;
|
||||
|
||||
bufferevent_setcb(bufev, readcb, writecb, eventcb, cbarg);
|
||||
|
||||
return bufev;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
be_socket_enable(struct bufferevent *bufev, short event)
|
||||
{
|
||||
if (event & EV_READ &&
|
||||
bufferevent_add_event_(&bufev->ev_read, &bufev->timeout_read) == -1)
|
||||
return -1;
|
||||
if (event & EV_WRITE &&
|
||||
bufferevent_add_event_(&bufev->ev_write, &bufev->timeout_write) == -1)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
be_socket_disable(struct bufferevent *bufev, short event)
|
||||
{
|
||||
struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
|
||||
if (event & EV_READ) {
|
||||
if (event_del(&bufev->ev_read) == -1)
|
||||
return -1;
|
||||
}
|
||||
/* Don't actually disable the write if we are trying to connect. */
|
||||
if ((event & EV_WRITE) && ! bufev_p->connecting) {
|
||||
if (event_del(&bufev->ev_write) == -1)
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
be_socket_destruct(struct bufferevent *bufev)
|
||||
{
|
||||
struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
|
||||
evutil_socket_t fd;
|
||||
EVUTIL_ASSERT(BEV_IS_SOCKET(bufev));
|
||||
|
||||
fd = event_get_fd(&bufev->ev_read);
|
||||
|
||||
if ((bufev_p->options & BEV_OPT_CLOSE_ON_FREE) && fd >= 0)
|
||||
EVUTIL_CLOSESOCKET(fd);
|
||||
|
||||
evutil_getaddrinfo_cancel_async_(bufev_p->dns_request);
|
||||
}
|
||||
|
||||
static int
|
||||
be_socket_flush(struct bufferevent *bev, short iotype,
|
||||
enum bufferevent_flush_mode mode)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
be_socket_setfd(struct bufferevent *bufev, evutil_socket_t fd)
|
||||
{
|
||||
struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
|
||||
|
||||
BEV_LOCK(bufev);
|
||||
EVUTIL_ASSERT(BEV_IS_SOCKET(bufev));
|
||||
|
||||
event_del(&bufev->ev_read);
|
||||
event_del(&bufev->ev_write);
|
||||
|
||||
evbuffer_unfreeze(bufev->input, 0);
|
||||
evbuffer_unfreeze(bufev->output, 1);
|
||||
|
||||
event_assign(&bufev->ev_read, bufev->ev_base, fd,
|
||||
EV_READ|EV_PERSIST|EV_FINALIZE, bufferevent_readcb, bufev);
|
||||
event_assign(&bufev->ev_write, bufev->ev_base, fd,
|
||||
EV_WRITE|EV_PERSIST|EV_FINALIZE, bufferevent_writecb, bufev);
|
||||
|
||||
if (fd >= 0)
|
||||
bufferevent_enable(bufev, bufev->enabled);
|
||||
|
||||
evutil_getaddrinfo_cancel_async_(bufev_p->dns_request);
|
||||
|
||||
BEV_UNLOCK(bufev);
|
||||
}
|
||||
|
||||
/* XXXX Should non-socket bufferevents support this? */
|
||||
int
|
||||
bufferevent_priority_set(struct bufferevent *bufev, int priority)
|
||||
{
|
||||
int r = -1;
|
||||
struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
|
||||
|
||||
BEV_LOCK(bufev);
|
||||
if (BEV_IS_ASYNC(bufev) || BEV_IS_FILTER(bufev) || BEV_IS_PAIR(bufev))
|
||||
goto done;
|
||||
|
||||
if (event_priority_set(&bufev->ev_read, priority) == -1)
|
||||
goto done;
|
||||
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);
|
||||
return r;
|
||||
}
|
||||
|
||||
/* XXXX Should non-socket bufferevents support this? */
|
||||
int
|
||||
bufferevent_base_set(struct event_base *base, struct bufferevent *bufev)
|
||||
{
|
||||
int res = -1;
|
||||
|
||||
BEV_LOCK(bufev);
|
||||
if (!BEV_IS_SOCKET(bufev))
|
||||
goto done;
|
||||
|
||||
bufev->ev_base = base;
|
||||
|
||||
res = event_base_set(base, &bufev->ev_read);
|
||||
if (res == -1)
|
||||
goto done;
|
||||
|
||||
res = event_base_set(base, &bufev->ev_write);
|
||||
done:
|
||||
BEV_UNLOCK(bufev);
|
||||
return res;
|
||||
}
|
||||
|
||||
static int
|
||||
be_socket_ctrl(struct bufferevent *bev, enum bufferevent_ctrl_op op,
|
||||
union bufferevent_ctrl_data *data)
|
||||
{
|
||||
switch (op) {
|
||||
case BEV_CTRL_SET_FD:
|
||||
be_socket_setfd(bev, data->fd);
|
||||
return 0;
|
||||
case BEV_CTRL_GET_FD:
|
||||
data->fd = event_get_fd(&bev->ev_read);
|
||||
return 0;
|
||||
case BEV_CTRL_GET_UNDERLYING:
|
||||
case BEV_CTRL_CANCEL_ALL:
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright (c) 2009-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 CHANGELIST_INTERNAL_H_INCLUDED_
|
||||
#define CHANGELIST_INTERNAL_H_INCLUDED_
|
||||
|
||||
/*
|
||||
A "changelist" is a list of all the fd status changes that should be made
|
||||
between calls to the backend's dispatch function. There are a few reasons
|
||||
that a backend would want to queue changes like this rather than processing
|
||||
them immediately.
|
||||
|
||||
1) Sometimes applications will add and delete the same event more than
|
||||
once between calls to dispatch. Processing these changes immediately
|
||||
is needless, and potentially expensive (especially if we're on a system
|
||||
that makes one syscall per changed event).
|
||||
|
||||
2) Sometimes we can coalesce multiple changes on the same fd into a single
|
||||
syscall if we know about them in advance. For example, epoll can do an
|
||||
add and a delete at the same time, but only if we have found out about
|
||||
both of them before we tell epoll.
|
||||
|
||||
3) Sometimes adding an event that we immediately delete can cause
|
||||
unintended consequences: in kqueue, this makes pending events get
|
||||
reported spuriously.
|
||||
*/
|
||||
|
||||
#include "event2/util.h"
|
||||
|
||||
/** Represents a */
|
||||
struct event_change {
|
||||
/** The fd or signal whose events are to be changed */
|
||||
evutil_socket_t fd;
|
||||
/* The events that were enabled on the fd before any of these changes
|
||||
were made. May include EV_READ or EV_WRITE. */
|
||||
short old_events;
|
||||
|
||||
/* The changes that we want to make in reading and writing on this fd.
|
||||
* If this is a signal, then read_change has EV_CHANGE_SIGNAL set,
|
||||
* 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. */
|
||||
|
||||
/* If set, add the event. */
|
||||
#define EV_CHANGE_ADD 0x01
|
||||
/* If set, delete the event. Exclusive with EV_CHANGE_ADD */
|
||||
#define EV_CHANGE_DEL 0x02
|
||||
/* If set, this event refers a signal, not an fd. */
|
||||
#define EV_CHANGE_SIGNAL EV_SIGNAL
|
||||
/* Set for persistent events. Currently not used. */
|
||||
#define EV_CHANGE_PERSIST EV_PERSIST
|
||||
/* Set for adding edge-triggered events. */
|
||||
#define EV_CHANGE_ET EV_ET
|
||||
|
||||
/* The value of fdinfo_size that a backend should use if it is letting
|
||||
* changelist handle its add and delete functions. */
|
||||
#define EVENT_CHANGELIST_FDINFO_SIZE sizeof(int)
|
||||
|
||||
/** Set up the data fields in a 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,
|
||||
struct event_base *base);
|
||||
/** Free all memory held in a 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,
|
||||
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,
|
||||
void *p);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,488 @@
|
||||
/* $OpenBSD: queue.h,v 1.16 2000/09/07 19:47:59 art Exp $ */
|
||||
/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1991, 1993
|
||||
* 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 name of the University 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 REGENTS 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.
|
||||
*
|
||||
* @(#)queue.h 8.5 (Berkeley) 8/20/94
|
||||
*/
|
||||
|
||||
#ifndef SYS_QUEUE_H__
|
||||
#define SYS_QUEUE_H__
|
||||
|
||||
/*
|
||||
* This file defines five types of data structures: singly-linked lists,
|
||||
* lists, simple queues, tail queues, and circular queues.
|
||||
*
|
||||
*
|
||||
* A singly-linked list is headed by a single forward pointer. The elements
|
||||
* are singly linked for minimum space and pointer manipulation overhead at
|
||||
* the expense of O(n) removal for arbitrary elements. New elements can be
|
||||
* added to the list after an existing element or at the head of the list.
|
||||
* Elements being removed from the head of the list should use the explicit
|
||||
* macro for this purpose for optimum efficiency. A singly-linked list may
|
||||
* only be traversed in the forward direction. Singly-linked lists are ideal
|
||||
* for applications with large datasets and few or no removals or for
|
||||
* implementing a LIFO queue.
|
||||
*
|
||||
* A list is headed by a single forward pointer (or an array of forward
|
||||
* pointers for a hash table header). The elements are doubly linked
|
||||
* so that an arbitrary element can be removed without a need to
|
||||
* traverse the list. New elements can be added to the list before
|
||||
* or after an existing element or at the head of the list. A list
|
||||
* may only be traversed in the forward direction.
|
||||
*
|
||||
* A simple queue is headed by a pair of pointers, one the head of the
|
||||
* list and the other to the tail of the list. The elements are singly
|
||||
* linked to save space, so elements can only be removed from the
|
||||
* head of the list. New elements can be added to the list before or after
|
||||
* an existing element, at the head of the list, or at the end of the
|
||||
* list. A simple queue may only be traversed in the forward direction.
|
||||
*
|
||||
* A tail queue is headed by a pair of pointers, one to the head of the
|
||||
* list and the other to the tail of the list. The elements are doubly
|
||||
* linked so that an arbitrary element can be removed without a need to
|
||||
* traverse the list. New elements can be added to the list before or
|
||||
* after an existing element, at the head of the list, or at the end of
|
||||
* the list. A tail queue may be traversed in either direction.
|
||||
*
|
||||
* A circle queue is headed by a pair of pointers, one to the head of the
|
||||
* list and the other to the tail of the list. The elements are doubly
|
||||
* linked so that an arbitrary element can be removed without a need to
|
||||
* traverse the list. New elements can be added to the list before or after
|
||||
* an existing element, at the head of the list, or at the end of the list.
|
||||
* A circle queue may be traversed in either direction, but has a more
|
||||
* complex end of list detection.
|
||||
*
|
||||
* For details on the use of these macros, see the queue(3) manual page.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Singly-linked List definitions.
|
||||
*/
|
||||
#define SLIST_HEAD(name, type) \
|
||||
struct name { \
|
||||
struct type *slh_first; /* first element */ \
|
||||
}
|
||||
|
||||
#define SLIST_HEAD_INITIALIZER(head) \
|
||||
{ NULL }
|
||||
|
||||
#ifndef _WIN32
|
||||
#define SLIST_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *sle_next; /* next element */ \
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Singly-linked List access methods.
|
||||
*/
|
||||
#define SLIST_FIRST(head) ((head)->slh_first)
|
||||
#define SLIST_END(head) NULL
|
||||
#define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
|
||||
#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
|
||||
|
||||
#define SLIST_FOREACH(var, head, field) \
|
||||
for((var) = SLIST_FIRST(head); \
|
||||
(var) != SLIST_END(head); \
|
||||
(var) = SLIST_NEXT(var, field))
|
||||
|
||||
/*
|
||||
* Singly-linked List functions.
|
||||
*/
|
||||
#define SLIST_INIT(head) { \
|
||||
SLIST_FIRST(head) = SLIST_END(head); \
|
||||
}
|
||||
|
||||
#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
|
||||
(elm)->field.sle_next = (slistelm)->field.sle_next; \
|
||||
(slistelm)->field.sle_next = (elm); \
|
||||
} while (0)
|
||||
|
||||
#define SLIST_INSERT_HEAD(head, elm, field) do { \
|
||||
(elm)->field.sle_next = (head)->slh_first; \
|
||||
(head)->slh_first = (elm); \
|
||||
} while (0)
|
||||
|
||||
#define SLIST_REMOVE_HEAD(head, field) do { \
|
||||
(head)->slh_first = (head)->slh_first->field.sle_next; \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* List definitions.
|
||||
*/
|
||||
#define LIST_HEAD(name, type) \
|
||||
struct name { \
|
||||
struct type *lh_first; /* first element */ \
|
||||
}
|
||||
|
||||
#define LIST_HEAD_INITIALIZER(head) \
|
||||
{ NULL }
|
||||
|
||||
#define LIST_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *le_next; /* next element */ \
|
||||
struct type **le_prev; /* address of previous next element */ \
|
||||
}
|
||||
|
||||
/*
|
||||
* List access methods
|
||||
*/
|
||||
#define LIST_FIRST(head) ((head)->lh_first)
|
||||
#define LIST_END(head) NULL
|
||||
#define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
|
||||
#define LIST_NEXT(elm, field) ((elm)->field.le_next)
|
||||
|
||||
#define LIST_FOREACH(var, head, field) \
|
||||
for((var) = LIST_FIRST(head); \
|
||||
(var)!= LIST_END(head); \
|
||||
(var) = LIST_NEXT(var, field))
|
||||
|
||||
/*
|
||||
* List functions.
|
||||
*/
|
||||
#define LIST_INIT(head) do { \
|
||||
LIST_FIRST(head) = LIST_END(head); \
|
||||
} while (0)
|
||||
|
||||
#define LIST_INSERT_AFTER(listelm, elm, field) do { \
|
||||
if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
|
||||
(listelm)->field.le_next->field.le_prev = \
|
||||
&(elm)->field.le_next; \
|
||||
(listelm)->field.le_next = (elm); \
|
||||
(elm)->field.le_prev = &(listelm)->field.le_next; \
|
||||
} while (0)
|
||||
|
||||
#define LIST_INSERT_BEFORE(listelm, elm, field) do { \
|
||||
(elm)->field.le_prev = (listelm)->field.le_prev; \
|
||||
(elm)->field.le_next = (listelm); \
|
||||
*(listelm)->field.le_prev = (elm); \
|
||||
(listelm)->field.le_prev = &(elm)->field.le_next; \
|
||||
} while (0)
|
||||
|
||||
#define LIST_INSERT_HEAD(head, elm, field) do { \
|
||||
if (((elm)->field.le_next = (head)->lh_first) != NULL) \
|
||||
(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
|
||||
(head)->lh_first = (elm); \
|
||||
(elm)->field.le_prev = &(head)->lh_first; \
|
||||
} while (0)
|
||||
|
||||
#define LIST_REMOVE(elm, field) do { \
|
||||
if ((elm)->field.le_next != NULL) \
|
||||
(elm)->field.le_next->field.le_prev = \
|
||||
(elm)->field.le_prev; \
|
||||
*(elm)->field.le_prev = (elm)->field.le_next; \
|
||||
} while (0)
|
||||
|
||||
#define LIST_REPLACE(elm, elm2, field) do { \
|
||||
if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
|
||||
(elm2)->field.le_next->field.le_prev = \
|
||||
&(elm2)->field.le_next; \
|
||||
(elm2)->field.le_prev = (elm)->field.le_prev; \
|
||||
*(elm2)->field.le_prev = (elm2); \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* Simple queue definitions.
|
||||
*/
|
||||
#define SIMPLEQ_HEAD(name, type) \
|
||||
struct name { \
|
||||
struct type *sqh_first; /* first element */ \
|
||||
struct type **sqh_last; /* addr of last next element */ \
|
||||
}
|
||||
|
||||
#define SIMPLEQ_HEAD_INITIALIZER(head) \
|
||||
{ NULL, &(head).sqh_first }
|
||||
|
||||
#define SIMPLEQ_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *sqe_next; /* next element */ \
|
||||
}
|
||||
|
||||
/*
|
||||
* Simple queue access methods.
|
||||
*/
|
||||
#define SIMPLEQ_FIRST(head) ((head)->sqh_first)
|
||||
#define SIMPLEQ_END(head) NULL
|
||||
#define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
|
||||
#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
|
||||
|
||||
#define SIMPLEQ_FOREACH(var, head, field) \
|
||||
for((var) = SIMPLEQ_FIRST(head); \
|
||||
(var) != SIMPLEQ_END(head); \
|
||||
(var) = SIMPLEQ_NEXT(var, field))
|
||||
|
||||
/*
|
||||
* Simple queue functions.
|
||||
*/
|
||||
#define SIMPLEQ_INIT(head) do { \
|
||||
(head)->sqh_first = NULL; \
|
||||
(head)->sqh_last = &(head)->sqh_first; \
|
||||
} while (0)
|
||||
|
||||
#define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
|
||||
if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
|
||||
(head)->sqh_last = &(elm)->field.sqe_next; \
|
||||
(head)->sqh_first = (elm); \
|
||||
} while (0)
|
||||
|
||||
#define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
|
||||
(elm)->field.sqe_next = NULL; \
|
||||
*(head)->sqh_last = (elm); \
|
||||
(head)->sqh_last = &(elm)->field.sqe_next; \
|
||||
} while (0)
|
||||
|
||||
#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
|
||||
if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
|
||||
(head)->sqh_last = &(elm)->field.sqe_next; \
|
||||
(listelm)->field.sqe_next = (elm); \
|
||||
} while (0)
|
||||
|
||||
#define SIMPLEQ_REMOVE_HEAD(head, elm, field) do { \
|
||||
if (((head)->sqh_first = (elm)->field.sqe_next) == NULL) \
|
||||
(head)->sqh_last = &(head)->sqh_first; \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* Tail queue definitions.
|
||||
*/
|
||||
#define TAILQ_HEAD(name, type) \
|
||||
struct name { \
|
||||
struct type *tqh_first; /* first element */ \
|
||||
struct type **tqh_last; /* addr of last next element */ \
|
||||
}
|
||||
|
||||
#define TAILQ_HEAD_INITIALIZER(head) \
|
||||
{ NULL, &(head).tqh_first }
|
||||
|
||||
#define TAILQ_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *tqe_next; /* next element */ \
|
||||
struct type **tqe_prev; /* address of previous next element */ \
|
||||
}
|
||||
|
||||
/*
|
||||
* tail queue access methods
|
||||
*/
|
||||
#define TAILQ_FIRST(head) ((head)->tqh_first)
|
||||
#define TAILQ_END(head) NULL
|
||||
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
|
||||
#define TAILQ_LAST(head, headname) \
|
||||
(*(((struct headname *)((head)->tqh_last))->tqh_last))
|
||||
/* XXX */
|
||||
#define TAILQ_PREV(elm, headname, field) \
|
||||
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
|
||||
#define TAILQ_EMPTY(head) \
|
||||
(TAILQ_FIRST(head) == TAILQ_END(head))
|
||||
|
||||
#define TAILQ_FOREACH(var, head, field) \
|
||||
for((var) = TAILQ_FIRST(head); \
|
||||
(var) != TAILQ_END(head); \
|
||||
(var) = TAILQ_NEXT(var, field))
|
||||
|
||||
#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
|
||||
for((var) = TAILQ_LAST(head, headname); \
|
||||
(var) != TAILQ_END(head); \
|
||||
(var) = TAILQ_PREV(var, headname, field))
|
||||
|
||||
/*
|
||||
* Tail queue functions.
|
||||
*/
|
||||
#define TAILQ_INIT(head) do { \
|
||||
(head)->tqh_first = NULL; \
|
||||
(head)->tqh_last = &(head)->tqh_first; \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_INSERT_HEAD(head, elm, field) do { \
|
||||
if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
|
||||
(head)->tqh_first->field.tqe_prev = \
|
||||
&(elm)->field.tqe_next; \
|
||||
else \
|
||||
(head)->tqh_last = &(elm)->field.tqe_next; \
|
||||
(head)->tqh_first = (elm); \
|
||||
(elm)->field.tqe_prev = &(head)->tqh_first; \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_INSERT_TAIL(head, elm, field) do { \
|
||||
(elm)->field.tqe_next = NULL; \
|
||||
(elm)->field.tqe_prev = (head)->tqh_last; \
|
||||
*(head)->tqh_last = (elm); \
|
||||
(head)->tqh_last = &(elm)->field.tqe_next; \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
|
||||
if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
|
||||
(elm)->field.tqe_next->field.tqe_prev = \
|
||||
&(elm)->field.tqe_next; \
|
||||
else \
|
||||
(head)->tqh_last = &(elm)->field.tqe_next; \
|
||||
(listelm)->field.tqe_next = (elm); \
|
||||
(elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
|
||||
(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
|
||||
(elm)->field.tqe_next = (listelm); \
|
||||
*(listelm)->field.tqe_prev = (elm); \
|
||||
(listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_REMOVE(head, elm, field) do { \
|
||||
if (((elm)->field.tqe_next) != NULL) \
|
||||
(elm)->field.tqe_next->field.tqe_prev = \
|
||||
(elm)->field.tqe_prev; \
|
||||
else \
|
||||
(head)->tqh_last = (elm)->field.tqe_prev; \
|
||||
*(elm)->field.tqe_prev = (elm)->field.tqe_next; \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_REPLACE(head, elm, elm2, field) do { \
|
||||
if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
|
||||
(elm2)->field.tqe_next->field.tqe_prev = \
|
||||
&(elm2)->field.tqe_next; \
|
||||
else \
|
||||
(head)->tqh_last = &(elm2)->field.tqe_next; \
|
||||
(elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
|
||||
*(elm2)->field.tqe_prev = (elm2); \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* Circular queue definitions.
|
||||
*/
|
||||
#define CIRCLEQ_HEAD(name, type) \
|
||||
struct name { \
|
||||
struct type *cqh_first; /* first element */ \
|
||||
struct type *cqh_last; /* last element */ \
|
||||
}
|
||||
|
||||
#define CIRCLEQ_HEAD_INITIALIZER(head) \
|
||||
{ CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
|
||||
|
||||
#define CIRCLEQ_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *cqe_next; /* next element */ \
|
||||
struct type *cqe_prev; /* previous element */ \
|
||||
}
|
||||
|
||||
/*
|
||||
* Circular queue access methods
|
||||
*/
|
||||
#define CIRCLEQ_FIRST(head) ((head)->cqh_first)
|
||||
#define CIRCLEQ_LAST(head) ((head)->cqh_last)
|
||||
#define CIRCLEQ_END(head) ((void *)(head))
|
||||
#define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
|
||||
#define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
|
||||
#define CIRCLEQ_EMPTY(head) \
|
||||
(CIRCLEQ_FIRST(head) == CIRCLEQ_END(head))
|
||||
|
||||
#define CIRCLEQ_FOREACH(var, head, field) \
|
||||
for((var) = CIRCLEQ_FIRST(head); \
|
||||
(var) != CIRCLEQ_END(head); \
|
||||
(var) = CIRCLEQ_NEXT(var, field))
|
||||
|
||||
#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \
|
||||
for((var) = CIRCLEQ_LAST(head); \
|
||||
(var) != CIRCLEQ_END(head); \
|
||||
(var) = CIRCLEQ_PREV(var, field))
|
||||
|
||||
/*
|
||||
* Circular queue functions.
|
||||
*/
|
||||
#define CIRCLEQ_INIT(head) do { \
|
||||
(head)->cqh_first = CIRCLEQ_END(head); \
|
||||
(head)->cqh_last = CIRCLEQ_END(head); \
|
||||
} while (0)
|
||||
|
||||
#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
|
||||
(elm)->field.cqe_next = (listelm)->field.cqe_next; \
|
||||
(elm)->field.cqe_prev = (listelm); \
|
||||
if ((listelm)->field.cqe_next == CIRCLEQ_END(head)) \
|
||||
(head)->cqh_last = (elm); \
|
||||
else \
|
||||
(listelm)->field.cqe_next->field.cqe_prev = (elm); \
|
||||
(listelm)->field.cqe_next = (elm); \
|
||||
} while (0)
|
||||
|
||||
#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
|
||||
(elm)->field.cqe_next = (listelm); \
|
||||
(elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
|
||||
if ((listelm)->field.cqe_prev == CIRCLEQ_END(head)) \
|
||||
(head)->cqh_first = (elm); \
|
||||
else \
|
||||
(listelm)->field.cqe_prev->field.cqe_next = (elm); \
|
||||
(listelm)->field.cqe_prev = (elm); \
|
||||
} while (0)
|
||||
|
||||
#define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
|
||||
(elm)->field.cqe_next = (head)->cqh_first; \
|
||||
(elm)->field.cqe_prev = CIRCLEQ_END(head); \
|
||||
if ((head)->cqh_last == CIRCLEQ_END(head)) \
|
||||
(head)->cqh_last = (elm); \
|
||||
else \
|
||||
(head)->cqh_first->field.cqe_prev = (elm); \
|
||||
(head)->cqh_first = (elm); \
|
||||
} while (0)
|
||||
|
||||
#define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
|
||||
(elm)->field.cqe_next = CIRCLEQ_END(head); \
|
||||
(elm)->field.cqe_prev = (head)->cqh_last; \
|
||||
if ((head)->cqh_first == CIRCLEQ_END(head)) \
|
||||
(head)->cqh_first = (elm); \
|
||||
else \
|
||||
(head)->cqh_last->field.cqe_next = (elm); \
|
||||
(head)->cqh_last = (elm); \
|
||||
} while (0)
|
||||
|
||||
#define CIRCLEQ_REMOVE(head, elm, field) do { \
|
||||
if ((elm)->field.cqe_next == CIRCLEQ_END(head)) \
|
||||
(head)->cqh_last = (elm)->field.cqe_prev; \
|
||||
else \
|
||||
(elm)->field.cqe_next->field.cqe_prev = \
|
||||
(elm)->field.cqe_prev; \
|
||||
if ((elm)->field.cqe_prev == CIRCLEQ_END(head)) \
|
||||
(head)->cqh_first = (elm)->field.cqe_next; \
|
||||
else \
|
||||
(elm)->field.cqe_prev->field.cqe_next = \
|
||||
(elm)->field.cqe_next; \
|
||||
} while (0)
|
||||
|
||||
#define CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
|
||||
if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
|
||||
CIRCLEQ_END(head)) \
|
||||
(head).cqh_last = (elm2); \
|
||||
else \
|
||||
(elm2)->field.cqe_next->field.cqe_prev = (elm2); \
|
||||
if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
|
||||
CIRCLEQ_END(head)) \
|
||||
(head).cqh_first = (elm2); \
|
||||
else \
|
||||
(elm2)->field.cqe_prev->field.cqe_next = (elm2); \
|
||||
} while (0)
|
||||
|
||||
#endif /* !SYS_QUEUE_H__ */
|
||||
@@ -0,0 +1,624 @@
|
||||
/* config.h.in. Generated from configure.ac by autoheader. */
|
||||
|
||||
/* Define if libevent should build without support for a debug mode */
|
||||
#undef DISABLE_DEBUG_MODE
|
||||
|
||||
/* Define if libevent should not allow replacing the mm functions */
|
||||
#undef DISABLE_MM_REPLACEMENT
|
||||
|
||||
/* Define if libevent should not be compiled with thread support */
|
||||
#undef DISABLE_THREAD_SUPPORT
|
||||
|
||||
/* Define to 1 if you have the 'accept4' function. */
|
||||
#undef HAVE_ACCEPT4
|
||||
|
||||
/* Define to 1 if you have the <afunix.h> header file. */
|
||||
#undef HAVE_AFUNIX_H
|
||||
|
||||
/* Define to 1 if you have the 'arc4random' function. */
|
||||
#undef HAVE_ARC4RANDOM
|
||||
|
||||
/* Define to 1 if you have the 'arc4random_addrandom' function. */
|
||||
#undef HAVE_ARC4RANDOM_ADDRANDOM
|
||||
|
||||
/* Define to 1 if you have the 'arc4random_buf' function. */
|
||||
#undef HAVE_ARC4RANDOM_BUF
|
||||
|
||||
/* Define to 1 if you have the <arpa/inet.h> header file. */
|
||||
#undef HAVE_ARPA_INET_H
|
||||
|
||||
/* Define to 1 if you have the 'clock_gettime' function. */
|
||||
#undef HAVE_CLOCK_GETTIME
|
||||
|
||||
/* Define to 1 if you have the declaration of 'CTL_KERN', and to 0 if you
|
||||
don't. */
|
||||
#undef HAVE_DECL_CTL_KERN
|
||||
|
||||
/* Define to 1 if you have the declaration of 'KERN_ARND', and to 0 if you
|
||||
don't. */
|
||||
#undef HAVE_DECL_KERN_ARND
|
||||
|
||||
/* Define if /dev/poll is available */
|
||||
#undef HAVE_DEVPOLL
|
||||
|
||||
/* Define to 1 if you have the <dlfcn.h> header file. */
|
||||
#undef HAVE_DLFCN_H
|
||||
|
||||
/* Define if your system supports the epoll system calls */
|
||||
#undef HAVE_EPOLL
|
||||
|
||||
/* Define to 1 if you have the 'epoll_create1' function. */
|
||||
#undef HAVE_EPOLL_CREATE1
|
||||
|
||||
/* Define to 1 if you have the 'epoll_ctl' function. */
|
||||
#undef HAVE_EPOLL_CTL
|
||||
|
||||
/* Define to 1 if you have the <errno.h> header file. */
|
||||
#undef HAVE_ERRNO_H
|
||||
|
||||
/* Define to 1 if you have the 'eventfd' function. */
|
||||
#undef HAVE_EVENTFD
|
||||
|
||||
/* Define if your system supports event ports */
|
||||
#undef HAVE_EVENT_PORTS
|
||||
|
||||
/* Define to 1 if you have the 'fcntl' function. */
|
||||
#undef HAVE_FCNTL
|
||||
|
||||
/* Define to 1 if you have the <fcntl.h> header file. */
|
||||
#undef HAVE_FCNTL_H
|
||||
|
||||
/* Define to 1 if the system has the type 'fd_mask'. */
|
||||
#undef HAVE_FD_MASK
|
||||
|
||||
/* Do we have getaddrinfo()? */
|
||||
#undef HAVE_GETADDRINFO
|
||||
|
||||
/* Define to 1 if you have the 'getegid' function. */
|
||||
#undef HAVE_GETEGID
|
||||
|
||||
/* Define to 1 if you have the 'geteuid' function. */
|
||||
#undef HAVE_GETEUID
|
||||
|
||||
/* Define this if you have any gethostbyname_r() */
|
||||
#undef HAVE_GETHOSTBYNAME_R
|
||||
|
||||
/* Define this if gethostbyname_r takes 3 arguments */
|
||||
#undef HAVE_GETHOSTBYNAME_R_3_ARG
|
||||
|
||||
/* Define this if gethostbyname_r takes 5 arguments */
|
||||
#undef HAVE_GETHOSTBYNAME_R_5_ARG
|
||||
|
||||
/* Define this if gethostbyname_r takes 6 arguments */
|
||||
#undef HAVE_GETHOSTBYNAME_R_6_ARG
|
||||
|
||||
/* Define to 1 if you have the 'getifaddrs' function. */
|
||||
#undef HAVE_GETIFADDRS
|
||||
|
||||
/* Define to 1 if you have the 'getnameinfo' function. */
|
||||
#undef HAVE_GETNAMEINFO
|
||||
|
||||
/* Define to 1 if you have the 'getprotobynumber' function. */
|
||||
#undef HAVE_GETPROTOBYNUMBER
|
||||
|
||||
/* Define to 1 if you have the 'getrandom' function. */
|
||||
#undef HAVE_GETRANDOM
|
||||
|
||||
/* Define to 1 if you have the 'getservbyname' function. */
|
||||
#undef HAVE_GETSERVBYNAME
|
||||
|
||||
/* Define to 1 if you have the 'gettimeofday' function. */
|
||||
#undef HAVE_GETTIMEOFDAY
|
||||
|
||||
/* Define to 1 if you have the <ifaddrs.h> header file. */
|
||||
#undef HAVE_IFADDRS_H
|
||||
|
||||
/* Define to 1 if you have the 'inet_ntop' function. */
|
||||
#undef HAVE_INET_NTOP
|
||||
|
||||
/* Define to 1 if you have the 'inet_pton' function. */
|
||||
#undef HAVE_INET_PTON
|
||||
|
||||
/* Define to 1 if you have the <inttypes.h> header file. */
|
||||
#undef HAVE_INTTYPES_H
|
||||
|
||||
/* Define to 1 if you have the 'issetugid' function. */
|
||||
#undef HAVE_ISSETUGID
|
||||
|
||||
/* Define to 1 if you have the 'kqueue' function. */
|
||||
#undef HAVE_KQUEUE
|
||||
|
||||
/* Define to 1 if you have the 'ws2_32' library (-lws2_32). */
|
||||
#undef HAVE_LIBWS2_32
|
||||
|
||||
/* Define if the system has zlib */
|
||||
#undef HAVE_LIBZ
|
||||
|
||||
/* Define to 1 if you have the 'mach_absolute_time' function. */
|
||||
#undef HAVE_MACH_ABSOLUTE_TIME
|
||||
|
||||
/* Define to 1 if you have the <mach/mach.h> header file. */
|
||||
#undef HAVE_MACH_MACH_H
|
||||
|
||||
/* Define to 1 if you have the <mach/mach_time.h> header file. */
|
||||
#undef HAVE_MACH_MACH_TIME_H
|
||||
|
||||
/* Define to 1 if you have the <minix/config.h> header file. */
|
||||
#undef HAVE_MINIX_CONFIG_H
|
||||
|
||||
/* Define to 1 if you have the 'mmap' function. */
|
||||
#undef HAVE_MMAP
|
||||
|
||||
/* Define to 1 if you have the 'nanosleep' function. */
|
||||
#undef HAVE_NANOSLEEP
|
||||
|
||||
/* Define to 1 if you have the <netdb.h> header file. */
|
||||
#undef HAVE_NETDB_H
|
||||
|
||||
/* Define to 1 if you have the <netinet/in6.h> header file. */
|
||||
#undef HAVE_NETINET_IN6_H
|
||||
|
||||
/* Define to 1 if you have the <netinet/in.h> header file. */
|
||||
#undef HAVE_NETINET_IN_H
|
||||
|
||||
/* Define to 1 if you have the <netinet/tcp.h> header file. */
|
||||
#undef HAVE_NETINET_TCP_H
|
||||
|
||||
/* Define if the system has openssl */
|
||||
#undef HAVE_OPENSSL
|
||||
|
||||
/* Define to 1 if you have the <openssl/ssl.h> header file. */
|
||||
#undef HAVE_OPENSSL_SSL_H
|
||||
|
||||
/* Define to 1 if you have the 'pipe' function. */
|
||||
#undef HAVE_PIPE
|
||||
|
||||
/* Define to 1 if you have the 'pipe2' function. */
|
||||
#undef HAVE_PIPE2
|
||||
|
||||
/* Define to 1 if you have the 'poll' function. */
|
||||
#undef HAVE_POLL
|
||||
|
||||
/* Define to 1 if you have the <poll.h> header file. */
|
||||
#undef HAVE_POLL_H
|
||||
|
||||
/* Define to 1 if you have the 'port_create' function. */
|
||||
#undef HAVE_PORT_CREATE
|
||||
|
||||
/* Define to 1 if you have the <port.h> header file. */
|
||||
#undef HAVE_PORT_H
|
||||
|
||||
/* Define if you have POSIX threads libraries and header files. */
|
||||
#undef HAVE_PTHREAD
|
||||
|
||||
/* Define if we have pthreads on this system */
|
||||
#undef HAVE_PTHREADS
|
||||
|
||||
/* Define to 1 if you have the 'putenv' function. */
|
||||
#undef HAVE_PUTENV
|
||||
|
||||
/* Define to 1 if the system has the type 'sa_family_t'. */
|
||||
#undef HAVE_SA_FAMILY_T
|
||||
|
||||
/* Define to 1 if you have the 'select' function. */
|
||||
#undef HAVE_SELECT
|
||||
|
||||
/* Define to 1 if you have the 'sendfile' function. */
|
||||
#undef HAVE_SENDFILE
|
||||
|
||||
/* Define to 1 if you have the 'setenv' function. */
|
||||
#undef HAVE_SETENV
|
||||
|
||||
/* Define if F_SETFD is defined in <fcntl.h> */
|
||||
#undef HAVE_SETFD
|
||||
|
||||
/* Define to 1 if you have the 'setrlimit' function. */
|
||||
#undef HAVE_SETRLIMIT
|
||||
|
||||
/* Define to 1 if you have the 'sigaction' function. */
|
||||
#undef HAVE_SIGACTION
|
||||
|
||||
/* Define to 1 if you have the 'signal' function. */
|
||||
#undef HAVE_SIGNAL
|
||||
|
||||
/* Define to 1 if you have the 'splice' function. */
|
||||
#undef HAVE_SPLICE
|
||||
|
||||
/* Define to 1 if you have the <stdarg.h> header file. */
|
||||
#undef HAVE_STDARG_H
|
||||
|
||||
/* Define to 1 if you have the <stddef.h> header file. */
|
||||
#undef HAVE_STDDEF_H
|
||||
|
||||
/* Define to 1 if you have the <stdint.h> header file. */
|
||||
#undef HAVE_STDINT_H
|
||||
|
||||
/* Define to 1 if you have the <stdio.h> header file. */
|
||||
#undef HAVE_STDIO_H
|
||||
|
||||
/* Define to 1 if you have the <stdlib.h> header file. */
|
||||
#undef HAVE_STDLIB_H
|
||||
|
||||
/* Define to 1 if you have the <strings.h> header file. */
|
||||
#undef HAVE_STRINGS_H
|
||||
|
||||
/* Define to 1 if you have the <string.h> header file. */
|
||||
#undef HAVE_STRING_H
|
||||
|
||||
/* Define to 1 if you have the 'strlcpy' function. */
|
||||
#undef HAVE_STRLCPY
|
||||
|
||||
/* Define to 1 if you have the 'strsep' function. */
|
||||
#undef HAVE_STRSEP
|
||||
|
||||
/* Define to 1 if you have the 'strtok_r' function. */
|
||||
#undef HAVE_STRTOK_R
|
||||
|
||||
/* Define to 1 if you have the 'strtoll' function. */
|
||||
#undef HAVE_STRTOLL
|
||||
|
||||
/* Define to 1 if the system has the type 'struct addrinfo'. */
|
||||
#undef HAVE_STRUCT_ADDRINFO
|
||||
|
||||
/* Define to 1 if the system has the type 'struct in6_addr'. */
|
||||
#undef HAVE_STRUCT_IN6_ADDR
|
||||
|
||||
/* Define to 1 if 's6_addr16' is a member of 'struct in6_addr'. */
|
||||
#undef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
|
||||
|
||||
/* Define to 1 if 's6_addr32' is a member of 'struct in6_addr'. */
|
||||
#undef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
|
||||
|
||||
/* Define to 1 if the system has the type 'struct linger'. */
|
||||
#undef HAVE_STRUCT_LINGER
|
||||
|
||||
/* Define to 1 if the system has the type 'struct sockaddr_in6'. */
|
||||
#undef HAVE_STRUCT_SOCKADDR_IN6
|
||||
|
||||
/* Define to 1 if 'sin6_len' is a member of 'struct sockaddr_in6'. */
|
||||
#undef HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
|
||||
|
||||
/* Define to 1 if 'sin_len' is a member of 'struct sockaddr_in'. */
|
||||
#undef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
|
||||
|
||||
/* Define to 1 if the system has the type 'struct sockaddr_storage'. */
|
||||
#undef HAVE_STRUCT_SOCKADDR_STORAGE
|
||||
|
||||
/* Define to 1 if 'ss_family' is a member of 'struct sockaddr_storage'. */
|
||||
#undef HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY
|
||||
|
||||
/* Define to 1 if '__ss_family' is a member of 'struct sockaddr_storage'. */
|
||||
#undef HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
|
||||
|
||||
/* Define to 1 if the system has the type 'struct sockaddr_un'. */
|
||||
#undef HAVE_STRUCT_SOCKADDR_UN
|
||||
|
||||
/* Define to 1 if you have the 'sysctl' function. */
|
||||
#undef HAVE_SYSCTL
|
||||
|
||||
/* Define to 1 if you have the <sys/devpoll.h> header file. */
|
||||
#undef HAVE_SYS_DEVPOLL_H
|
||||
|
||||
/* Define to 1 if you have the <sys/epoll.h> header file. */
|
||||
#undef HAVE_SYS_EPOLL_H
|
||||
|
||||
/* Define to 1 if you have the <sys/eventfd.h> header file. */
|
||||
#undef HAVE_SYS_EVENTFD_H
|
||||
|
||||
/* Define to 1 if you have the <sys/event.h> header file. */
|
||||
#undef HAVE_SYS_EVENT_H
|
||||
|
||||
/* Define to 1 if you have the <sys/ioctl.h> header file. */
|
||||
#undef HAVE_SYS_IOCTL_H
|
||||
|
||||
/* Define to 1 if you have the <sys/mman.h> header file. */
|
||||
#undef HAVE_SYS_MMAN_H
|
||||
|
||||
/* Define to 1 if you have the <sys/param.h> header file. */
|
||||
#undef HAVE_SYS_PARAM_H
|
||||
|
||||
/* Define to 1 if you have the <sys/queue.h> header file. */
|
||||
#undef HAVE_SYS_QUEUE_H
|
||||
|
||||
/* Define to 1 if you have the <sys/random.h> header file. */
|
||||
#undef HAVE_SYS_RANDOM_H
|
||||
|
||||
/* Define to 1 if you have the <sys/resource.h> header file. */
|
||||
#undef HAVE_SYS_RESOURCE_H
|
||||
|
||||
/* Define to 1 if you have the <sys/select.h> header file. */
|
||||
#undef HAVE_SYS_SELECT_H
|
||||
|
||||
/* Define to 1 if you have the <sys/sendfile.h> header file. */
|
||||
#undef HAVE_SYS_SENDFILE_H
|
||||
|
||||
/* Define to 1 if you have the <sys/socket.h> header file. */
|
||||
#undef HAVE_SYS_SOCKET_H
|
||||
|
||||
/* Define to 1 if you have the <sys/stat.h> header file. */
|
||||
#undef HAVE_SYS_STAT_H
|
||||
|
||||
/* Define to 1 if you have the <sys/sysctl.h> header file. */
|
||||
#undef HAVE_SYS_SYSCTL_H
|
||||
|
||||
/* Define to 1 if you have the <sys/timerfd.h> header file. */
|
||||
#undef HAVE_SYS_TIMERFD_H
|
||||
|
||||
/* Define to 1 if you have the <sys/time.h> header file. */
|
||||
#undef HAVE_SYS_TIME_H
|
||||
|
||||
/* Define to 1 if you have the <sys/types.h> header file. */
|
||||
#undef HAVE_SYS_TYPES_H
|
||||
|
||||
/* Define to 1 if you have the <sys/uio.h> header file. */
|
||||
#undef HAVE_SYS_UIO_H
|
||||
|
||||
/* Define to 1 if you have the <sys/un.h> header file. */
|
||||
#undef HAVE_SYS_UN_H
|
||||
|
||||
/* Define to 1 if you have the <sys/wait.h> header file. */
|
||||
#undef HAVE_SYS_WAIT_H
|
||||
|
||||
/* Define if TAILQ_FOREACH is defined in <sys/queue.h> */
|
||||
#undef HAVE_TAILQFOREACH
|
||||
|
||||
/* Define if timeradd is defined in <sys/time.h> */
|
||||
#undef HAVE_TIMERADD
|
||||
|
||||
/* Define if timerclear is defined in <sys/time.h> */
|
||||
#undef HAVE_TIMERCLEAR
|
||||
|
||||
/* Define if timercmp is defined in <sys/time.h> */
|
||||
#undef HAVE_TIMERCMP
|
||||
|
||||
/* Define to 1 if you have the 'timerfd_create' function. */
|
||||
#undef HAVE_TIMERFD_CREATE
|
||||
|
||||
/* Define if timerisset is defined in <sys/time.h> */
|
||||
#undef HAVE_TIMERISSET
|
||||
|
||||
/* Define to 1 if the system has the type 'uint16_t'. */
|
||||
#undef HAVE_UINT16_T
|
||||
|
||||
/* Define to 1 if the system has the type 'uint32_t'. */
|
||||
#undef HAVE_UINT32_T
|
||||
|
||||
/* Define to 1 if the system has the type 'uint64_t'. */
|
||||
#undef HAVE_UINT64_T
|
||||
|
||||
/* Define to 1 if the system has the type 'uint8_t'. */
|
||||
#undef HAVE_UINT8_T
|
||||
|
||||
/* Define to 1 if the system has the type 'uintptr_t'. */
|
||||
#undef HAVE_UINTPTR_T
|
||||
|
||||
/* Define to 1 if you have the 'umask' function. */
|
||||
#undef HAVE_UMASK
|
||||
|
||||
/* Define to 1 if you have the <unistd.h> header file. */
|
||||
#undef HAVE_UNISTD_H
|
||||
|
||||
/* Define to 1 if you have the 'unsetenv' function. */
|
||||
#undef HAVE_UNSETENV
|
||||
|
||||
/* Define to 1 if you have the 'usleep' function. */
|
||||
#undef HAVE_USLEEP
|
||||
|
||||
/* Define to 1 if you have the 'vasprintf' function. */
|
||||
#undef HAVE_VASPRINTF
|
||||
|
||||
/* Define to 1 if you have the <wchar.h> header file. */
|
||||
#undef HAVE_WCHAR_H
|
||||
|
||||
/* Define if kqueue works correctly with pipes */
|
||||
#undef HAVE_WORKING_KQUEUE
|
||||
|
||||
/* Define to 1 if you have the <zlib.h> header file. */
|
||||
#undef HAVE_ZLIB_H
|
||||
|
||||
/* Define to 1 if you have the '_gmtime64' function. */
|
||||
#undef HAVE__GMTIME64
|
||||
|
||||
/* Define to 1 if you have the '_gmtime64_s' function. */
|
||||
#undef HAVE__GMTIME64_S
|
||||
|
||||
/* Define to 1 if compiler have __FUNCTION__ */
|
||||
#undef HAVE___FUNCTION__
|
||||
|
||||
/* Define to 1 if compiler have __func__ */
|
||||
#undef HAVE___func__
|
||||
|
||||
/* Define to the sub-directory where libtool stores uninstalled libraries. */
|
||||
#undef LT_OBJDIR
|
||||
|
||||
/* Numeric representation of the version */
|
||||
#undef NUMERIC_VERSION
|
||||
|
||||
/* Name of package */
|
||||
#undef PACKAGE
|
||||
|
||||
/* Define to the address where bug reports for this package should be sent. */
|
||||
#undef PACKAGE_BUGREPORT
|
||||
|
||||
/* Define to the full name of this package. */
|
||||
#undef PACKAGE_NAME
|
||||
|
||||
/* Define to the full name and version of this package. */
|
||||
#undef PACKAGE_STRING
|
||||
|
||||
/* Define to the one symbol short name of this package. */
|
||||
#undef PACKAGE_TARNAME
|
||||
|
||||
/* Define to the home page for this package. */
|
||||
#undef PACKAGE_URL
|
||||
|
||||
/* Define to the version of this package. */
|
||||
#undef PACKAGE_VERSION
|
||||
|
||||
/* Define to necessary symbol if this constant uses a non-standard name on
|
||||
your system. */
|
||||
#undef PTHREAD_CREATE_JOINABLE
|
||||
|
||||
/* The size of 'int', as computed by sizeof. */
|
||||
#undef SIZEOF_INT
|
||||
|
||||
/* The size of 'long', as computed by sizeof. */
|
||||
#undef SIZEOF_LONG
|
||||
|
||||
/* The size of 'long long', as computed by sizeof. */
|
||||
#undef SIZEOF_LONG_LONG
|
||||
|
||||
/* The size of 'off_t', as computed by sizeof. */
|
||||
#undef SIZEOF_OFF_T
|
||||
|
||||
/* The size of 'pthread_t', as computed by sizeof. */
|
||||
#undef SIZEOF_PTHREAD_T
|
||||
|
||||
/* The size of 'short', as computed by sizeof. */
|
||||
#undef SIZEOF_SHORT
|
||||
|
||||
/* The size of 'size_t', as computed by sizeof. */
|
||||
#undef SIZEOF_SIZE_T
|
||||
|
||||
/* The size of 'time_t', as computed by sizeof. */
|
||||
#undef SIZEOF_TIME_T
|
||||
|
||||
/* The size of 'void *', as computed by sizeof. */
|
||||
#undef SIZEOF_VOID_P
|
||||
|
||||
/* Define to 1 if all of the C89 standard headers exist (not just the ones
|
||||
required in a freestanding environment). This macro is provided for
|
||||
backward compatibility; new code need not use it. */
|
||||
#undef STDC_HEADERS
|
||||
|
||||
/* Define to 1 if you can safely include both <sys/time.h> and <time.h>. This
|
||||
macro is obsolete. */
|
||||
#undef TIME_WITH_SYS_TIME
|
||||
|
||||
/* Enable extensions on AIX, Interix, z/OS. */
|
||||
#ifndef _ALL_SOURCE
|
||||
# undef _ALL_SOURCE
|
||||
#endif
|
||||
/* Enable general extensions on macOS. */
|
||||
#ifndef _DARWIN_C_SOURCE
|
||||
# undef _DARWIN_C_SOURCE
|
||||
#endif
|
||||
/* Enable general extensions on Solaris. */
|
||||
#ifndef __EXTENSIONS__
|
||||
# undef __EXTENSIONS__
|
||||
#endif
|
||||
/* Enable GNU extensions on systems that have them. */
|
||||
#ifndef _GNU_SOURCE
|
||||
# undef _GNU_SOURCE
|
||||
#endif
|
||||
/* Enable X/Open compliant socket functions that do not require linking
|
||||
with -lxnet on HP-UX 11.11. */
|
||||
#ifndef _HPUX_ALT_XOPEN_SOCKET_API
|
||||
# undef _HPUX_ALT_XOPEN_SOCKET_API
|
||||
#endif
|
||||
/* Identify the host operating system as Minix.
|
||||
This macro does not affect the system headers' behavior.
|
||||
A future release of Autoconf may stop defining this macro. */
|
||||
#ifndef _MINIX
|
||||
# undef _MINIX
|
||||
#endif
|
||||
/* Enable general extensions on NetBSD.
|
||||
Enable NetBSD compatibility extensions on Minix. */
|
||||
#ifndef _NETBSD_SOURCE
|
||||
# undef _NETBSD_SOURCE
|
||||
#endif
|
||||
/* Enable OpenBSD compatibility extensions on NetBSD.
|
||||
Oddly enough, this does nothing on OpenBSD. */
|
||||
#ifndef _OPENBSD_SOURCE
|
||||
# undef _OPENBSD_SOURCE
|
||||
#endif
|
||||
/* Define to 1 if needed for POSIX-compatible behavior. */
|
||||
#ifndef _POSIX_SOURCE
|
||||
# undef _POSIX_SOURCE
|
||||
#endif
|
||||
/* Define to 2 if needed for POSIX-compatible behavior. */
|
||||
#ifndef _POSIX_1_SOURCE
|
||||
# undef _POSIX_1_SOURCE
|
||||
#endif
|
||||
/* Enable POSIX-compatible threading on Solaris. */
|
||||
#ifndef _POSIX_PTHREAD_SEMANTICS
|
||||
# undef _POSIX_PTHREAD_SEMANTICS
|
||||
#endif
|
||||
/* Enable extensions specified by ISO/IEC TS 18661-5:2014. */
|
||||
#ifndef __STDC_WANT_IEC_60559_ATTRIBS_EXT__
|
||||
# undef __STDC_WANT_IEC_60559_ATTRIBS_EXT__
|
||||
#endif
|
||||
/* Enable extensions specified by ISO/IEC TS 18661-1:2014. */
|
||||
#ifndef __STDC_WANT_IEC_60559_BFP_EXT__
|
||||
# undef __STDC_WANT_IEC_60559_BFP_EXT__
|
||||
#endif
|
||||
/* Enable extensions specified by ISO/IEC TS 18661-2:2015. */
|
||||
#ifndef __STDC_WANT_IEC_60559_DFP_EXT__
|
||||
# undef __STDC_WANT_IEC_60559_DFP_EXT__
|
||||
#endif
|
||||
/* Enable extensions specified by C23 Annex F. */
|
||||
#ifndef __STDC_WANT_IEC_60559_EXT__
|
||||
# undef __STDC_WANT_IEC_60559_EXT__
|
||||
#endif
|
||||
/* Enable extensions specified by ISO/IEC TS 18661-4:2015. */
|
||||
#ifndef __STDC_WANT_IEC_60559_FUNCS_EXT__
|
||||
# undef __STDC_WANT_IEC_60559_FUNCS_EXT__
|
||||
#endif
|
||||
/* Enable extensions specified by C23 Annex H and ISO/IEC TS 18661-3:2015. */
|
||||
#ifndef __STDC_WANT_IEC_60559_TYPES_EXT__
|
||||
# undef __STDC_WANT_IEC_60559_TYPES_EXT__
|
||||
#endif
|
||||
/* Enable extensions specified by ISO/IEC TR 24731-2:2010. */
|
||||
#ifndef __STDC_WANT_LIB_EXT2__
|
||||
# undef __STDC_WANT_LIB_EXT2__
|
||||
#endif
|
||||
/* Enable extensions specified by ISO/IEC 24747:2009. */
|
||||
#ifndef __STDC_WANT_MATH_SPEC_FUNCS__
|
||||
# undef __STDC_WANT_MATH_SPEC_FUNCS__
|
||||
#endif
|
||||
/* Enable extensions on HP NonStop. */
|
||||
#ifndef _TANDEM_SOURCE
|
||||
# undef _TANDEM_SOURCE
|
||||
#endif
|
||||
/* Enable X/Open extensions. Define to 500 only if necessary
|
||||
to make mbstate_t available. */
|
||||
#ifndef _XOPEN_SOURCE
|
||||
# undef _XOPEN_SOURCE
|
||||
#endif
|
||||
|
||||
|
||||
/* Version number of package */
|
||||
#undef VERSION
|
||||
|
||||
/* Number of bits in a file offset, on hosts where this is settable. */
|
||||
#undef _FILE_OFFSET_BITS
|
||||
|
||||
/* Define to 1 on platforms where this makes off_t a 64-bit type. */
|
||||
#undef _LARGE_FILES
|
||||
|
||||
/* Number of bits in time_t, on hosts where this is settable. */
|
||||
#undef _TIME_BITS
|
||||
|
||||
/* Define to 1 on platforms where this makes time_t a 64-bit type. */
|
||||
#undef __MINGW_USE_VC2005_COMPAT
|
||||
|
||||
/* Define to empty if 'const' does not conform to ANSI C. */
|
||||
#undef 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
|
||||
#undef inline
|
||||
#endif
|
||||
|
||||
/* Define as a signed integer type capable of holding a process identifier. */
|
||||
#undef pid_t
|
||||
|
||||
/* Define as 'unsigned int' if <stddef.h> doesn't define. */
|
||||
#undef size_t
|
||||
|
||||
/* Define to unsigned int if you dont have it */
|
||||
#undef socklen_t
|
||||
|
||||
/* Define to 'int' if <sys/types.h> does not define. */
|
||||
#undef ssize_t
|
||||
Vendored
+23852
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2009-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 DEFER_INTERNAL_H_INCLUDED_
|
||||
#define DEFER_INTERNAL_H_INCLUDED_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "event2/event-config.h"
|
||||
#include "evconfig-private.h"
|
||||
|
||||
#include <sys/queue.h>
|
||||
|
||||
struct event_callback;
|
||||
typedef void (*deferred_cb_fn)(struct event_callback *, void *);
|
||||
|
||||
/**
|
||||
Initialize an empty, non-pending event_callback.
|
||||
|
||||
@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.
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void event_deferred_cb_init_(struct event_callback *, ev_uint8_t, deferred_cb_fn, void *);
|
||||
/**
|
||||
Change the priority of a non-pending event_callback.
|
||||
*/
|
||||
void event_deferred_cb_set_priority_(struct event_callback *, ev_uint8_t);
|
||||
/**
|
||||
Cancel a struct event_callback if it is currently scheduled in an event_base.
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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.
|
||||
|
||||
Return true if it was not previously scheduled.
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int event_deferred_cb_schedule_(struct event_base *, struct event_callback *);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* EVENT_INTERNAL_H_INCLUDED_ */
|
||||
|
||||
@@ -0,0 +1,311 @@
|
||||
/*
|
||||
* Copyright 2000-2009 Niels Provos <provos@citi.umich.edu>
|
||||
* Copyright 2009-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 EVENT__HAVE_DEVPOLL
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/resource.h>
|
||||
#ifdef EVENT__HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
#include <sys/queue.h>
|
||||
#include <sys/devpoll.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "event2/event.h"
|
||||
#include "event2/event_struct.h"
|
||||
#include "event2/thread.h"
|
||||
#include "event-internal.h"
|
||||
#include "evsignal-internal.h"
|
||||
#include "log-internal.h"
|
||||
#include "evmap-internal.h"
|
||||
#include "evthread-internal.h"
|
||||
|
||||
struct devpollop {
|
||||
struct pollfd *events;
|
||||
int nevents;
|
||||
int dpfd;
|
||||
struct pollfd *changes;
|
||||
int nchanges;
|
||||
};
|
||||
|
||||
static void *devpoll_init(struct event_base *);
|
||||
static int devpoll_add(struct event_base *, int fd, short old, short events, void *);
|
||||
static int devpoll_del(struct event_base *, int fd, short old, short events, void *);
|
||||
static int devpoll_dispatch(struct event_base *, struct timeval *);
|
||||
static void devpoll_dealloc(struct event_base *);
|
||||
|
||||
const struct eventop devpollops = {
|
||||
"devpoll",
|
||||
devpoll_init,
|
||||
devpoll_add,
|
||||
devpoll_del,
|
||||
devpoll_dispatch,
|
||||
devpoll_dealloc,
|
||||
1, /* need reinit */
|
||||
EV_FEATURE_FDS|EV_FEATURE_O1,
|
||||
0
|
||||
};
|
||||
|
||||
#define NEVENT 32000
|
||||
|
||||
static int
|
||||
devpoll_commit(struct devpollop *devpollop)
|
||||
{
|
||||
/*
|
||||
* Due to a bug in Solaris, we have to use pwrite with an offset of 0.
|
||||
* Write is limited to 2GB of data, until it will fail.
|
||||
*/
|
||||
if (pwrite(devpollop->dpfd, devpollop->changes,
|
||||
sizeof(struct pollfd) * devpollop->nchanges, 0) == -1)
|
||||
return (-1);
|
||||
|
||||
devpollop->nchanges = 0;
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
devpoll_queue(struct devpollop *devpollop, int fd, int events) {
|
||||
struct pollfd *pfd;
|
||||
|
||||
if (devpollop->nchanges >= devpollop->nevents) {
|
||||
/*
|
||||
* Change buffer is full, must commit it to /dev/poll before
|
||||
* adding more
|
||||
*/
|
||||
if (devpoll_commit(devpollop) != 0)
|
||||
return (-1);
|
||||
}
|
||||
|
||||
pfd = &devpollop->changes[devpollop->nchanges++];
|
||||
pfd->fd = fd;
|
||||
pfd->events = events;
|
||||
pfd->revents = 0;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void *
|
||||
devpoll_init(struct event_base *base)
|
||||
{
|
||||
int dpfd, nfiles = NEVENT;
|
||||
struct rlimit rl;
|
||||
struct devpollop *devpollop;
|
||||
|
||||
if (!(devpollop = mm_calloc(1, sizeof(struct devpollop))))
|
||||
return (NULL);
|
||||
|
||||
if (getrlimit(RLIMIT_NOFILE, &rl) == 0 &&
|
||||
rl.rlim_cur != RLIM_INFINITY)
|
||||
nfiles = rl.rlim_cur;
|
||||
|
||||
/* Initialize the kernel queue */
|
||||
if ((dpfd = evutil_open_closeonexec_("/dev/poll", O_RDWR, 0)) == -1) {
|
||||
event_warn("open: /dev/poll");
|
||||
mm_free(devpollop);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
devpollop->dpfd = dpfd;
|
||||
|
||||
/* Initialize fields */
|
||||
/* FIXME: allocating 'nfiles' worth of space here can be
|
||||
* expensive and unnecessary. See how epoll.c does it instead. */
|
||||
devpollop->events = mm_calloc(nfiles, sizeof(struct pollfd));
|
||||
if (devpollop->events == NULL) {
|
||||
mm_free(devpollop);
|
||||
close(dpfd);
|
||||
return (NULL);
|
||||
}
|
||||
devpollop->nevents = nfiles;
|
||||
|
||||
devpollop->changes = mm_calloc(nfiles, sizeof(struct pollfd));
|
||||
if (devpollop->changes == NULL) {
|
||||
mm_free(devpollop->events);
|
||||
mm_free(devpollop);
|
||||
close(dpfd);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
evsig_init_(base);
|
||||
|
||||
return (devpollop);
|
||||
}
|
||||
|
||||
static int
|
||||
devpoll_dispatch(struct event_base *base, struct timeval *tv)
|
||||
{
|
||||
struct devpollop *devpollop = base->evbase;
|
||||
struct pollfd *events = devpollop->events;
|
||||
struct dvpoll dvp;
|
||||
int i, res, timeout = -1;
|
||||
|
||||
if (devpollop->nchanges)
|
||||
devpoll_commit(devpollop);
|
||||
|
||||
if (tv != NULL)
|
||||
timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000;
|
||||
|
||||
dvp.dp_fds = devpollop->events;
|
||||
dvp.dp_nfds = devpollop->nevents;
|
||||
dvp.dp_timeout = timeout;
|
||||
|
||||
EVBASE_RELEASE_LOCK(base, th_base_lock);
|
||||
|
||||
res = ioctl(devpollop->dpfd, DP_POLL, &dvp);
|
||||
|
||||
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
|
||||
|
||||
if (res == -1) {
|
||||
if (errno != EINTR) {
|
||||
event_warn("ioctl: DP_POLL");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
event_debug(("%s: devpoll_wait reports %d", __func__, res));
|
||||
|
||||
for (i = 0; i < res; i++) {
|
||||
int which = 0;
|
||||
int what = events[i].revents;
|
||||
|
||||
if (what & POLLHUP)
|
||||
what |= POLLIN | POLLOUT;
|
||||
else if (what & POLLERR)
|
||||
what |= POLLIN | POLLOUT;
|
||||
|
||||
if (what & POLLIN)
|
||||
which |= EV_READ;
|
||||
if (what & POLLOUT)
|
||||
which |= EV_WRITE;
|
||||
|
||||
if (!which)
|
||||
continue;
|
||||
|
||||
/* XXX(niels): not sure if this works for devpoll */
|
||||
evmap_io_active_(base, events[i].fd, which);
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
devpoll_add(struct event_base *base, int fd, short old, short events, void *p)
|
||||
{
|
||||
struct devpollop *devpollop = base->evbase;
|
||||
int res;
|
||||
(void)p;
|
||||
|
||||
/*
|
||||
* It's not necessary to OR the existing read/write events that we
|
||||
* are currently interested in with the new event we are adding.
|
||||
* The /dev/poll driver ORs any new events with the existing events
|
||||
* that it has cached for the fd.
|
||||
*/
|
||||
|
||||
res = 0;
|
||||
if (events & EV_READ)
|
||||
res |= POLLIN;
|
||||
if (events & EV_WRITE)
|
||||
res |= POLLOUT;
|
||||
|
||||
if (devpoll_queue(devpollop, fd, res) != 0)
|
||||
return (-1);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
devpoll_del(struct event_base *base, int fd, short old, short events, void *p)
|
||||
{
|
||||
struct devpollop *devpollop = base->evbase;
|
||||
int res;
|
||||
(void)p;
|
||||
|
||||
res = 0;
|
||||
if (events & EV_READ)
|
||||
res |= POLLIN;
|
||||
if (events & EV_WRITE)
|
||||
res |= POLLOUT;
|
||||
|
||||
/*
|
||||
* The only way to remove an fd from the /dev/poll monitored set is
|
||||
* to use POLLREMOVE by itself. This removes ALL events for the fd
|
||||
* provided so if we care about two events and are only removing one
|
||||
* we must re-add the other event after POLLREMOVE.
|
||||
*/
|
||||
|
||||
if (devpoll_queue(devpollop, fd, POLLREMOVE) != 0)
|
||||
return (-1);
|
||||
|
||||
if ((res & (POLLIN|POLLOUT)) != (POLLIN|POLLOUT)) {
|
||||
/*
|
||||
* We're not deleting all events, so we must resubmit the
|
||||
* event that we are still interested in if one exists.
|
||||
*/
|
||||
|
||||
if ((res & POLLIN) && (old & EV_WRITE)) {
|
||||
/* Deleting read, still care about write */
|
||||
devpoll_queue(devpollop, fd, POLLOUT);
|
||||
} else if ((res & POLLOUT) && (old & EV_READ)) {
|
||||
/* Deleting write, still care about read */
|
||||
devpoll_queue(devpollop, fd, POLLIN);
|
||||
}
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void
|
||||
devpoll_dealloc(struct event_base *base)
|
||||
{
|
||||
struct devpollop *devpollop = base->evbase;
|
||||
|
||||
evsig_dealloc_(base);
|
||||
if (devpollop->events)
|
||||
mm_free(devpollop->events);
|
||||
if (devpollop->changes)
|
||||
mm_free(devpollop->changes);
|
||||
if (devpollop->dpfd >= 0)
|
||||
close(devpollop->dpfd);
|
||||
|
||||
memset(devpollop, 0, sizeof(struct devpollop));
|
||||
mm_free(devpollop);
|
||||
}
|
||||
|
||||
#endif /* EVENT__HAVE_DEVPOLL */
|
||||
@@ -0,0 +1,55 @@
|
||||
# Doxygen documentation will not be generated with default configuration,
|
||||
# unless '--enable-doxygen-doc' is configured.
|
||||
# The following targets are all about doxygen:
|
||||
# make # 'make doxygen' would be auto executed
|
||||
# make doxygen # generating doxygen documentation
|
||||
# make doxygen-doc # same as 'make doxygen'
|
||||
# make clean # clean docs generated by doxygen
|
||||
# make install # install doxygen documentation
|
||||
# make uninstall # uninstall doxygen documentation
|
||||
|
||||
if ENABLE_DOXYGEN
|
||||
|
||||
# Add all needed rules defined in ax_prog_doxygen.m4
|
||||
@DX_RULES@
|
||||
|
||||
# Use 'make clean' to clean docs generated by doxygen.
|
||||
clean-local:
|
||||
-rm -rf $(DX_CLEANFILES)
|
||||
|
||||
# integrate doxygen with automake targets
|
||||
man3_MANS = @DX_DOCDIR@/man/man3/*
|
||||
$(man3_MANS): doxygen-doc
|
||||
|
||||
# Docs will be installed. It may be one or more docs supported
|
||||
# by doxygen, but does not include 'man'.
|
||||
docdirs = $(DX_INSTALL_DOCS)
|
||||
|
||||
# Rules for installing docs generated by doxygen into $(htmldir),
|
||||
# The typical value of $(htmldir) is '/usr/local/share/doc/$(PACKAGE)'
|
||||
install-data-local:
|
||||
@if ! test -d "$(DESTDIR)$(htmldir)"; then \
|
||||
echo "$(mkinstalldirs) '$(DESTDIR)$(htmldir)'"; \
|
||||
$(mkinstalldirs) '$(DESTDIR)$(htmldir)'; \
|
||||
fi
|
||||
@for d in $(docdirs); do \
|
||||
echo "cp -pR $$d '$(DESTDIR)$(htmldir)/'"; \
|
||||
cp -pR $$d '$(DESTDIR)$(htmldir)/'; \
|
||||
done
|
||||
|
||||
# Rules for uninstalling docs generated by doxygen from $(htmldir)
|
||||
uninstall-local:
|
||||
@for d in $(docdirs); do \
|
||||
d=`basename $$d`; \
|
||||
echo "test ! -d '$(DESTDIR)$(htmldir)/'$$d || \
|
||||
{ find '$(DESTDIR)$(htmldir)/'$$d -type d ! -perm -200 -exec chmod u+w '{}' ';' && \
|
||||
rm -rf '$(DESTDIR)$(htmldir)/'$$d; }"; \
|
||||
test ! -d '$(DESTDIR)$(htmldir)/'$$d || \
|
||||
{ find '$(DESTDIR)$(htmldir)/'$$d -type d ! -perm -200 -exec chmod u+w '{}' ';' && \
|
||||
rm -rf '$(DESTDIR)$(htmldir)/'$$d; }; \
|
||||
done
|
||||
rmdir "$(DESTDIR)$(htmldir)/" || true
|
||||
|
||||
doxygen: doxygen-doc
|
||||
|
||||
endif ENABLE_DOXYGEN
|
||||
@@ -0,0 +1,545 @@
|
||||
/*
|
||||
* Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
|
||||
* Copyright 2007-2012 Niels Provos, 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 EVENT__HAVE_EPOLL
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/resource.h>
|
||||
#ifdef EVENT__HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
#include <sys/queue.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <signal.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.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"
|
||||
#include "event2/thread.h"
|
||||
#include "evthread-internal.h"
|
||||
#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 *);
|
||||
static int epoll_dispatch(struct event_base *, struct timeval *);
|
||||
static void epoll_dealloc(struct event_base *);
|
||||
|
||||
static const struct eventop epollops_changelist = {
|
||||
"epoll (with changelist)",
|
||||
epoll_init,
|
||||
event_changelist_add_,
|
||||
event_changelist_del_,
|
||||
epoll_dispatch,
|
||||
epoll_dealloc,
|
||||
1, /* need reinit */
|
||||
EV_FEATURE_ET|EV_FEATURE_O1| EARLY_CLOSE_IF_HAVE_RDHUP,
|
||||
EVENT_CHANGELIST_FDINFO_SIZE
|
||||
};
|
||||
|
||||
|
||||
static int epoll_nochangelist_add(struct event_base *base, evutil_socket_t fd,
|
||||
short old, short events, void *p);
|
||||
static int epoll_nochangelist_del(struct event_base *base, evutil_socket_t fd,
|
||||
short old, short events, void *p);
|
||||
|
||||
const struct eventop epollops = {
|
||||
"epoll",
|
||||
epoll_init,
|
||||
epoll_nochangelist_add,
|
||||
epoll_nochangelist_del,
|
||||
epoll_dispatch,
|
||||
epoll_dealloc,
|
||||
1, /* need reinit */
|
||||
EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_EARLY_CLOSE,
|
||||
0
|
||||
};
|
||||
|
||||
#define INITIAL_NEVENT 32
|
||||
#define MAX_NEVENT 4096
|
||||
|
||||
/* On Linux kernels at least up to 2.6.24.4, epoll can't handle timeout
|
||||
* values bigger than (LONG_MAX - 999ULL)/HZ. HZ in the wild can be
|
||||
* as big as 1000, and LONG_MAX can be as small as (1<<31)-1, so the
|
||||
* largest number of msec we can support here is 2147482. Let's
|
||||
* round that down by 47 seconds.
|
||||
*/
|
||||
#define MAX_EPOLL_TIMEOUT_MSEC (35*60*1000)
|
||||
|
||||
static void *
|
||||
epoll_init(struct event_base *base)
|
||||
{
|
||||
int epfd = -1;
|
||||
struct epollop *epollop;
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
if (!(epollop = mm_calloc(1, sizeof(struct epollop)))) {
|
||||
close(epfd);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
epollop->epfd = epfd;
|
||||
|
||||
/* Initialize fields */
|
||||
epollop->events = mm_calloc(INITIAL_NEVENT, sizeof(struct epoll_event));
|
||||
if (epollop->events == NULL) {
|
||||
mm_free(epollop);
|
||||
close(epfd);
|
||||
return (NULL);
|
||||
}
|
||||
epollop->nevents = INITIAL_NEVENT;
|
||||
|
||||
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)) {
|
||||
|
||||
base->evsel = &epollops_changelist;
|
||||
}
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
static const char *
|
||||
change_to_string(int change)
|
||||
{
|
||||
change &= (EV_CHANGE_ADD|EV_CHANGE_DEL);
|
||||
if (change == EV_CHANGE_ADD) {
|
||||
return "add";
|
||||
} else if (change == EV_CHANGE_DEL) {
|
||||
return "del";
|
||||
} else if (change == 0) {
|
||||
return "none";
|
||||
} else {
|
||||
return "???";
|
||||
}
|
||||
}
|
||||
|
||||
static const char *
|
||||
epoll_op_to_string(int op)
|
||||
{
|
||||
return op == EPOLL_CTL_ADD?"ADD":
|
||||
op == EPOLL_CTL_DEL?"DEL":
|
||||
op == EPOLL_CTL_MOD?"MOD":
|
||||
"???";
|
||||
}
|
||||
|
||||
#define PRINT_CHANGES(op, events, ch, status) \
|
||||
"Epoll %s(%d) on fd %d " status ". " \
|
||||
"Old events were %d; " \
|
||||
"read change was %d (%s); " \
|
||||
"write change was %d (%s); " \
|
||||
"close change was %d (%s)", \
|
||||
epoll_op_to_string(op), \
|
||||
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)
|
||||
|
||||
static int
|
||||
epoll_apply_one_change(struct event_base *base,
|
||||
struct epollop *epollop,
|
||||
const struct event_change *ch)
|
||||
{
|
||||
struct epoll_event epev;
|
||||
int op, events = 0;
|
||||
int idx;
|
||||
|
||||
idx = EPOLL_OP_TABLE_INDEX(ch);
|
||||
op = epoll_op_table[idx].op;
|
||||
events = epoll_op_table[idx].events;
|
||||
|
||||
if (!events) {
|
||||
EVUTIL_ASSERT(op == 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((ch->read_change|ch->write_change|ch->close_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((PRINT_CHANGES(op, epev.events, ch, "okay")));
|
||||
return 0;
|
||||
}
|
||||
|
||||
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_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;
|
||||
}
|
||||
}
|
||||
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,
|
||||
ch->fd,
|
||||
strerror(errno)));
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
event_warn(PRINT_CHANGES(op, epev.events, ch, "failed"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int
|
||||
epoll_apply_changes(struct event_base *base)
|
||||
{
|
||||
struct event_changelist *changelist = &base->changelist;
|
||||
struct epollop *epollop = base->evbase;
|
||||
struct event_change *ch;
|
||||
|
||||
int r = 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < changelist->n_changes; ++i) {
|
||||
ch = &changelist->changes[i];
|
||||
if (epoll_apply_one_change(base, epollop, ch) < 0)
|
||||
r = -1;
|
||||
}
|
||||
|
||||
return (r);
|
||||
}
|
||||
|
||||
static int
|
||||
epoll_nochangelist_add(struct event_base *base, evutil_socket_t fd,
|
||||
short old, short events, void *p)
|
||||
{
|
||||
struct event_change ch;
|
||||
ch.fd = fd;
|
||||
ch.old_events = old;
|
||||
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);
|
||||
}
|
||||
|
||||
static int
|
||||
epoll_nochangelist_del(struct event_base *base, evutil_socket_t fd,
|
||||
short old, short events, void *p)
|
||||
{
|
||||
struct event_change ch;
|
||||
ch.fd = fd;
|
||||
ch.old_events = old;
|
||||
ch.read_change = ch.write_change = ch.close_change = 0;
|
||||
if (events & EV_WRITE)
|
||||
ch.write_change = EV_CHANGE_DEL |
|
||||
(events & EV_ET);
|
||||
if (events & EV_READ)
|
||||
ch.read_change = EV_CHANGE_DEL |
|
||||
(events & EV_ET);
|
||||
if (events & EV_CLOSED)
|
||||
ch.close_change = EV_CHANGE_DEL |
|
||||
(events & EV_ET);
|
||||
|
||||
return epoll_apply_one_change(base, base->evbase, &ch);
|
||||
}
|
||||
|
||||
static int
|
||||
epoll_dispatch(struct event_base *base, struct timeval *tv)
|
||||
{
|
||||
struct epollop *epollop = base->evbase;
|
||||
struct epoll_event *events = epollop->events;
|
||||
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);
|
||||
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. */
|
||||
timeout = MAX_EPOLL_TIMEOUT_MSEC;
|
||||
}
|
||||
}
|
||||
|
||||
epoll_apply_changes(base);
|
||||
event_changelist_remove_all_(&base->changelist, base);
|
||||
|
||||
EVBASE_RELEASE_LOCK(base, th_base_lock);
|
||||
|
||||
res = epoll_wait(epollop->epfd, events, epollop->nevents, timeout);
|
||||
|
||||
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
|
||||
|
||||
if (res == -1) {
|
||||
if (errno != EINTR) {
|
||||
event_warn("epoll_wait");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
event_debug(("%s: epoll_wait reports %d", __func__, res));
|
||||
EVUTIL_ASSERT(res <= epollop->nevents);
|
||||
|
||||
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 & EPOLLERR) {
|
||||
ev = EV_READ | EV_WRITE;
|
||||
} else if ((what & EPOLLHUP) && !(what & EPOLLRDHUP)) {
|
||||
ev = EV_READ | EV_WRITE;
|
||||
} else {
|
||||
if (what & EPOLLIN)
|
||||
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);
|
||||
}
|
||||
|
||||
if (res == epollop->nevents && epollop->nevents < MAX_NEVENT) {
|
||||
/* We used all of the event space this time. We should
|
||||
be ready for more events next time. */
|
||||
int new_nevents = epollop->nevents * 2;
|
||||
struct epoll_event *new_events;
|
||||
|
||||
new_events = mm_realloc(epollop->events,
|
||||
new_nevents * sizeof(struct epoll_event));
|
||||
if (new_events) {
|
||||
epollop->events = new_events;
|
||||
epollop->nevents = new_nevents;
|
||||
}
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
epoll_dealloc(struct event_base *base)
|
||||
{
|
||||
struct epollop *epollop = base->evbase;
|
||||
|
||||
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 */
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2003-2009 Niels Provos <provos@citi.umich.edu>
|
||||
* Copyright 2009-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 "evconfig-private.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
int
|
||||
epoll_create(int size)
|
||||
{
|
||||
#if !defined(__NR_epoll_create) && defined(__NR_epoll_create1)
|
||||
if (size <= 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
return (syscall(__NR_epoll_create1, 0));
|
||||
#else
|
||||
return (syscall(__NR_epoll_create, size));
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
|
||||
{
|
||||
|
||||
return (syscall(__NR_epoll_ctl, epfd, op, fd, event));
|
||||
}
|
||||
|
||||
int
|
||||
epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)
|
||||
{
|
||||
#if !defined(__NR_epoll_wait) && defined(__NR_epoll_pwait)
|
||||
return (syscall(__NR_epoll_pwait, epfd, events, maxevents, timeout, NULL, 0));
|
||||
#else
|
||||
return (syscall(__NR_epoll_wait, epfd, events, maxevents, timeout));
|
||||
#endif
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,351 @@
|
||||
/*
|
||||
* 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 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"
|
||||
|
||||
/* Experimental cb flag: "never deferred." Implementation note:
|
||||
* these callbacks may get an inaccurate view of n_del/n_added in their
|
||||
* arguments. */
|
||||
#define EVBUFFER_CB_NODEFER 2
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
#include <sys/queue.h>
|
||||
|
||||
/* 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
|
||||
#define MIN_BUFFER_SIZE 512
|
||||
#else
|
||||
#define MIN_BUFFER_SIZE 1024
|
||||
#endif
|
||||
|
||||
/** A single evbuffer callback for an evbuffer. This function will be invoked
|
||||
* when bytes are added to or removed from the evbuffer. */
|
||||
struct evbuffer_cb_entry {
|
||||
/** Structures to implement a doubly-linked queue of callbacks */
|
||||
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. */
|
||||
union {
|
||||
evbuffer_cb_func cb_func;
|
||||
evbuffer_cb cb_obsolete;
|
||||
} cb;
|
||||
/** Argument to pass to cb. */
|
||||
void *cbarg;
|
||||
/** Currently set flags on this callback. */
|
||||
ev_uint32_t flags;
|
||||
};
|
||||
|
||||
struct bufferevent;
|
||||
struct evbuffer_chain;
|
||||
struct evbuffer {
|
||||
/** The first chain in this buffer's linked list of chains. */
|
||||
struct evbuffer_chain *first;
|
||||
/** The last chain in this buffer's linked list of chains. */
|
||||
struct evbuffer_chain *last;
|
||||
|
||||
/** Pointer to the next pointer pointing at the 'last_with_data' chain.
|
||||
*
|
||||
* To unpack:
|
||||
*
|
||||
* The last_with_data chain is the last chain that has any data in it.
|
||||
* If all chains in the buffer are empty, it is the first chain.
|
||||
* If the buffer has no chains, it is NULL.
|
||||
*
|
||||
* The last_with_datap pointer points at _whatever 'next' pointer_
|
||||
* pointing at the last_with_data chain. If the last_with_data chain
|
||||
* is the first chain, or it is NULL, then the last_with_datap pointer
|
||||
* is &buf->first.
|
||||
*/
|
||||
struct evbuffer_chain **last_with_datap;
|
||||
|
||||
/** Total amount of bytes stored in all chains.*/
|
||||
size_t total_len;
|
||||
|
||||
/** Number of bytes we have added to the buffer since we last tried to
|
||||
* invoke callbacks. */
|
||||
size_t n_add_for_cb;
|
||||
/** Number of bytes we have removed from the buffer since we last
|
||||
* tried to invoke callbacks. */
|
||||
size_t n_del_for_cb;
|
||||
|
||||
#ifndef EVENT__DISABLE_THREAD_SUPPORT
|
||||
/** A lock used to mediate access to this buffer. */
|
||||
void *lock;
|
||||
#endif
|
||||
/** True iff we should free the lock field when we free this
|
||||
* evbuffer. */
|
||||
unsigned own_lock : 1;
|
||||
/** True iff we should not allow changes to the front of the buffer
|
||||
* (drains or prepends). */
|
||||
unsigned freeze_start : 1;
|
||||
/** True iff we should not allow changes to the end of the buffer
|
||||
* (appends) */
|
||||
unsigned freeze_end : 1;
|
||||
/** True iff this evbuffer's callbacks are not invoked immediately
|
||||
* upon a change in the buffer, but instead are deferred to be invoked
|
||||
* from the event_base's loop. Useful for preventing enormous stack
|
||||
* overflows when we have mutually recursive callbacks, and for
|
||||
* serializing callbacks in a single thread. */
|
||||
unsigned deferred_cbs : 1;
|
||||
#ifdef _WIN32
|
||||
/** True iff this buffer is set up for overlapped IO. */
|
||||
unsigned is_overlapped : 1;
|
||||
#endif
|
||||
/** Zero or more EVBUFFER_FLAG_* bits */
|
||||
ev_uint32_t flags;
|
||||
|
||||
/** Used to implement deferred callbacks. */
|
||||
struct event_base *cb_queue;
|
||||
|
||||
/** A reference count on this evbuffer. When the reference count
|
||||
* reaches 0, the buffer is destroyed. Manipulated with
|
||||
* evbuffer_incref and evbuffer_decref_and_unlock and
|
||||
* evbuffer_free. */
|
||||
int refcnt;
|
||||
|
||||
/** A struct event_callback handle to make all of this buffer's callbacks
|
||||
* invoked from the event loop. */
|
||||
struct event_callback deferred;
|
||||
|
||||
/** A doubly-linked-list of callback functions */
|
||||
LIST_HEAD(evbuffer_cb_queue, evbuffer_cb_entry) callbacks;
|
||||
|
||||
/** The parent bufferevent object this evbuffer belongs to.
|
||||
* NULL if the evbuffer stands alone. */
|
||||
struct bufferevent *parent;
|
||||
};
|
||||
|
||||
#if EVENT__SIZEOF_OFF_T < EVENT__SIZEOF_SIZE_T
|
||||
typedef ev_ssize_t ev_misalign_t;
|
||||
#define EVBUFFER_CHAIN_MAX ((size_t)EV_SSIZE_MAX)
|
||||
#else
|
||||
typedef ev_off_t ev_misalign_t;
|
||||
#if EVENT__SIZEOF_OFF_T > EVENT__SIZEOF_SIZE_T
|
||||
#define EVBUFFER_CHAIN_MAX EV_SIZE_MAX
|
||||
#else
|
||||
#define EVBUFFER_CHAIN_MAX ((size_t)EV_SSIZE_MAX)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/** A single item in an evbuffer. */
|
||||
struct evbuffer_chain {
|
||||
/** points to next buffer in the chain */
|
||||
struct evbuffer_chain *next;
|
||||
|
||||
/** total allocation available in the buffer field. */
|
||||
size_t buffer_len;
|
||||
|
||||
/** unused space at the beginning of buffer or an offset into a
|
||||
* file for sendfile buffers. */
|
||||
ev_misalign_t misalign;
|
||||
|
||||
/** Offset into buffer + misalign at which to start writing.
|
||||
* In other words, the total number of bytes actually stored
|
||||
* in buffer. */
|
||||
size_t off;
|
||||
|
||||
/** Set if special handling is required for this chain */
|
||||
unsigned flags;
|
||||
#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
|
||||
* memmoved, until the chain is un-pinned. */
|
||||
#define EVBUFFER_MEM_PINNED_R 0x0010
|
||||
#define EVBUFFER_MEM_PINNED_W 0x0020
|
||||
#define EVBUFFER_MEM_PINNED_ANY (EVBUFFER_MEM_PINNED_R|EVBUFFER_MEM_PINNED_W)
|
||||
/** 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.
|
||||
* For mmap, this can be a read-only buffer and
|
||||
* EVBUFFER_IMMUTABLE will be set in flags. For sendfile, it
|
||||
* may point to NULL.
|
||||
*/
|
||||
unsigned char *buffer;
|
||||
};
|
||||
|
||||
/** 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)
|
||||
|
||||
/** Assert that we are holding the lock on an evbuffer */
|
||||
#define ASSERT_EVBUFFER_LOCKED(buffer) \
|
||||
EVLOCK_ASSERT_LOCKED((buffer)->lock)
|
||||
|
||||
#define EVBUFFER_LOCK(buffer) \
|
||||
do { \
|
||||
EVLOCK_LOCK((buffer)->lock, 0); \
|
||||
} while (0)
|
||||
#define EVBUFFER_UNLOCK(buffer) \
|
||||
do { \
|
||||
EVLOCK_UNLOCK((buffer)->lock, 0); \
|
||||
} while (0)
|
||||
#define EVBUFFER_LOCK2(buffer1, buffer2) \
|
||||
do { \
|
||||
EVLOCK_LOCK2((buffer1)->lock, (buffer2)->lock, 0, 0); \
|
||||
} while (0)
|
||||
#define EVBUFFER_UNLOCK2(buffer1, buffer2) \
|
||||
do { \
|
||||
EVLOCK_UNLOCK2((buffer1)->lock, (buffer2)->lock, 0, 0); \
|
||||
} while (0)
|
||||
|
||||
/** Increase the reference count of buf by one. */
|
||||
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);
|
||||
/** 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);
|
||||
/** Unpin a single buffer chain using a given 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);
|
||||
|
||||
/** 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);
|
||||
|
||||
/** Helper: prepares for a readv/WSARecv call by expanding the buffer to
|
||||
* hold enough memory to read 'howmuch' bytes in possibly noncontiguous memory.
|
||||
* Sets up the one or two iovecs in 'vecs' to point to the free memory and its
|
||||
* 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,
|
||||
struct evbuffer_iovec *vecs, int n_vecs, struct evbuffer_chain ***chainp,
|
||||
int exact);
|
||||
|
||||
/* Helper macro: copies an evbuffer_iovec in ei to a win32 WSABUF in i. */
|
||||
#define WSABUF_FROM_EVBUFFER_IOV(i,ei) do { \
|
||||
(i)->buf = (ei)->iov_base; \
|
||||
(i)->len = (unsigned long)(ei)->iov_len; \
|
||||
} while (0)
|
||||
/* XXXX the cast above is safe for now, but not if we allow mmaps on win64.
|
||||
* 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_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_INCLUDED_ */
|
||||
@@ -0,0 +1,40 @@
|
||||
|
||||
#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
|
||||
|
||||
/* Enable POSIX.2 extensions on QNX for getopt */
|
||||
#ifdef __QNX__
|
||||
#cmakedefine __EXT_POSIX2 1
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,55 @@
|
||||
/* 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
|
||||
|
||||
/* Enable POSIX.2 extensions on QNX for getopt */
|
||||
#ifdef __QNX__
|
||||
# ifndef __EXT_POSIX2
|
||||
# define __EXT_POSIX2
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,513 @@
|
||||
/* 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@"
|
||||
|
||||
#define EVENT__VERSION_MAJOR @EVENT_VERSION_MAJOR@
|
||||
#define EVENT__VERSION_MINOR @EVENT_VERSION_MINOR@
|
||||
#define EVENT__VERSION_PATCH @EVENT_VERSION_PATCH@
|
||||
|
||||
/* 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 1
|
||||
|
||||
/* Define if libevent should not allow replacing the mm functions */
|
||||
#cmakedefine EVENT__DISABLE_MM_REPLACEMENT 1
|
||||
|
||||
/* Define if libevent should not be compiled with thread support */
|
||||
#cmakedefine EVENT__DISABLE_THREAD_SUPPORT 1
|
||||
|
||||
/* 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 to 1 if you have the `arc4random_addrandom' function. */
|
||||
#cmakedefine EVENT__HAVE_ARC4RANDOM_ADDRANDOM 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'. */
|
||||
#define EVENT__HAVE_DECL_CTL_KERN @EVENT__HAVE_DECL_CTL_KERN@
|
||||
|
||||
/* Define to 1 if you have the declaration of `KERN_ARND'. */
|
||||
#define EVENT__HAVE_DECL_KERN_ARND @EVENT__HAVE_DECL_KERN_ARND@
|
||||
|
||||
/* Define to 1 if you have `getrandom' function. */
|
||||
#cmakedefine EVENT__HAVE_GETRANDOM 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 <mach/mach.h> header file. */
|
||||
#cmakedefine EVENT__HAVE_MACH_MACH_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 <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 to 1 if you have the <sys/un.h> header file. */
|
||||
#cmakedefine EVENT__HAVE_SYS_UN_H 1
|
||||
|
||||
/* Define to 1 if you have the <afunix.h> header file. */
|
||||
#cmakedefine EVENT__HAVE_AFUNIX_H 1
|
||||
|
||||
/* Define if the system has openssl */
|
||||
#cmakedefine EVENT__HAVE_OPENSSL 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 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 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 <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 you have the `_gmtime64_s' function. */
|
||||
#cmakedefine EVENT__HAVE__GMTIME64_S 1
|
||||
|
||||
/* Define to 1 if you have the `_gmtime64' function. */
|
||||
#cmakedefine EVENT__HAVE__GMTIME64 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_un'. */
|
||||
#cmakedefine EVENT__HAVE_STRUCT_SOCKADDR_UN 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 the system has the type `struct linger'. */
|
||||
#cmakedefine EVENT__HAVE_STRUCT_LINGER 1
|
||||
|
||||
/* Define to 1 if you have the `sysctl' function. */
|
||||
#cmakedefine EVENT__HAVE_SYSCTL 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/random.h> header file. */
|
||||
#cmakedefine EVENT__HAVE_SYS_RANDOM_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 1
|
||||
|
||||
/* 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 to 1 if you have the <errno.h> header file. */
|
||||
#cmakedefine EVENT__HAVE_ERRNO_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
|
||||
|
||||
#ifdef __USE_UNUSED_DEFINITIONS__
|
||||
/* Define to necessary symbol if this constant uses a non-standard name on your system. */
|
||||
/* XXX: Hello, this isn't even used, nor is it defined anywhere... - Ellzey */
|
||||
#define EVENT__PTHREAD_CREATE_JOINABLE ${EVENT__PTHREAD_CREATE_JOINABLE}
|
||||
#endif
|
||||
|
||||
/* The size of `pthread_t', as computed by sizeof. */
|
||||
#define EVENT__SIZEOF_PTHREAD_T @EVENT__SIZEOF_PTHREAD_T@
|
||||
|
||||
/* The size of a `int', as computed by sizeof. */
|
||||
#define EVENT__SIZEOF_INT @EVENT__SIZEOF_INT@
|
||||
|
||||
/* The size of a `long', as computed by sizeof. */
|
||||
#define EVENT__SIZEOF_LONG @EVENT__SIZEOF_LONG@
|
||||
|
||||
/* The size of a `long long', as computed by sizeof. */
|
||||
#define EVENT__SIZEOF_LONG_LONG @EVENT__SIZEOF_LONG_LONG@
|
||||
|
||||
/* The size of `off_t', as computed by sizeof. */
|
||||
#define EVENT__SIZEOF_OFF_T @EVENT__SIZEOF_OFF_T@
|
||||
|
||||
#define EVENT__SIZEOF_SSIZE_T @EVENT__SIZEOF_SSIZE_T@
|
||||
|
||||
|
||||
/* The size of a `short', as computed by sizeof. */
|
||||
#define EVENT__SIZEOF_SHORT @EVENT__SIZEOF_SHORT@
|
||||
|
||||
/* The size of `size_t', as computed by sizeof. */
|
||||
#define EVENT__SIZEOF_SIZE_T @EVENT__SIZEOF_SIZE_T@
|
||||
|
||||
/* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
|
||||
#cmakedefine EVENT__TIME_WITH_SYS_TIME 1
|
||||
|
||||
/* The size of `socklen_t', as computed by sizeof. */
|
||||
#define EVENT__SIZEOF_SOCKLEN_T @EVENT__SIZEOF_SOCKLEN_T@
|
||||
|
||||
/* The size of 'void *', as computer by sizeof */
|
||||
#define EVENT__SIZEOF_VOID_P @EVENT__SIZEOF_VOID_P@
|
||||
|
||||
/* 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
|
||||
/* why not c++?
|
||||
*
|
||||
* and are we really expected to use EVENT__inline everywhere,
|
||||
* shouldn't we just do:
|
||||
* ifdef EVENT__inline
|
||||
* define inline EVENT__inline
|
||||
*
|
||||
* - Ellzey
|
||||
*/
|
||||
|
||||
#define EVENT__inline @EVENT__inline@
|
||||
#endif
|
||||
|
||||
#cmakedefine EVENT__HAVE___func__ 1
|
||||
#cmakedefine EVENT__HAVE___FUNCTION__ 1
|
||||
|
||||
/* Define to `unsigned' if <sys/types.h> does not define. */
|
||||
#define EVENT__size_t @EVENT__size_t@
|
||||
|
||||
/* Define to unsigned int if you dont have it */
|
||||
#define EVENT__socklen_t @EVENT__socklen_t@
|
||||
|
||||
/* Define to `int' if <sys/types.h> does not define. */
|
||||
#define EVENT__ssize_t @EVENT__ssize_t@
|
||||
|
||||
#endif /* \EVENT2_EVENT_CONFIG_H_INCLUDED_ */
|
||||
@@ -0,0 +1,488 @@
|
||||
/*
|
||||
* 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 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"
|
||||
#include "minheap-internal.h"
|
||||
#include "evsignal-internal.h"
|
||||
#include "mm-internal.h"
|
||||
#include "defer-internal.h"
|
||||
|
||||
/* 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
|
||||
|
||||
/* used only by signals */
|
||||
#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
|
||||
|
||||
@{
|
||||
*/
|
||||
/** 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 {
|
||||
/** The name of this backend. */
|
||||
const char *name;
|
||||
/** Function to set up an event_base to use this backend. It should
|
||||
* create a new structure holding whatever information is needed to
|
||||
* run the backend, and return it. The returned pointer will get
|
||||
* stored by event_init into the event_base.evbase field. On failure,
|
||||
* this function should return NULL. */
|
||||
void *(*init)(struct event_base *);
|
||||
/** Enable reading/writing on a given fd or signal. 'events' will be
|
||||
* the events that we're trying to enable: one or more of EV_READ,
|
||||
* EV_WRITE, EV_SIGNAL, and EV_ET. 'old' will be those events that
|
||||
* were enabled on this fd previously. 'fdinfo' will be a structure
|
||||
* associated with the fd by the evmap; its size is defined by the
|
||||
* fdinfo field below. It will be set to 0 the first time the fd is
|
||||
* added. The function should return 0 on success and -1 on error.
|
||||
*/
|
||||
int (*add)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);
|
||||
/** As "add", except 'events' contains the events we mean to disable. */
|
||||
int (*del)(struct event_base *, evutil_socket_t fd, short old, short events, void *fdinfo);
|
||||
/** Function to implement the core of an event loop. It must see which
|
||||
added events are ready, and cause event_active to be called for each
|
||||
active event (usually via event_io_active or such). It should
|
||||
return 0 on success and -1 on error.
|
||||
*/
|
||||
int (*dispatch)(struct event_base *, struct timeval *);
|
||||
/** Function to clean up and free our data from the event_base. */
|
||||
void (*dealloc)(struct event_base *);
|
||||
/** Flag: set if we need to reinitialize the event base after we fork.
|
||||
*/
|
||||
int need_reinit;
|
||||
/** Bit-array of supported event_method_features that this backend can
|
||||
* provide. */
|
||||
enum event_method_feature features;
|
||||
/** Length of the extra information we should record for each fd that
|
||||
has one or more active events. This information is recorded
|
||||
as part of the evmap entry for each fd, and passed as an argument
|
||||
to the add and del functions above.
|
||||
*/
|
||||
size_t fdinfo_len;
|
||||
};
|
||||
|
||||
#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.
|
||||
*/
|
||||
#define EVMAP_USE_HT
|
||||
#endif
|
||||
|
||||
/* #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);
|
||||
#else
|
||||
#define event_io_map event_signal_map
|
||||
#endif
|
||||
|
||||
/* Used to map signal numbers to a list of events. If EVMAP_USE_HT is not
|
||||
defined, this structure is also used as event_io_map, which maps fds to a
|
||||
list of events.
|
||||
*/
|
||||
struct event_signal_map {
|
||||
/* An array of evmap_io * or of evmap_signal *; empty entries are
|
||||
* set to NULL. */
|
||||
void **entries;
|
||||
/* The number of entries available in entries */
|
||||
int nentries;
|
||||
};
|
||||
|
||||
/* A list of events waiting on a given 'common' timeout value. Ordinarily,
|
||||
* events waiting for a timeout wait on a minheap. Sometimes, however, a
|
||||
* queue can be faster.
|
||||
**/
|
||||
struct common_timeout_list {
|
||||
/* List of events currently waiting in the queue. */
|
||||
struct event_list events;
|
||||
/* 'magic' timeval used to indicate the duration of events in this
|
||||
* queue. */
|
||||
struct timeval duration;
|
||||
/* Event that triggers whenever one of the events in the queue is
|
||||
* ready to activate */
|
||||
struct event timeout_event;
|
||||
/* The event_base that this timeout list is part of */
|
||||
struct event_base *base;
|
||||
};
|
||||
|
||||
/** Mask used to get the real tv_usec value from a common timeout. */
|
||||
#define COMMON_TIMEOUT_MICROSECONDS_MASK 0x000fffff
|
||||
|
||||
struct event_change;
|
||||
|
||||
/* List of 'changes' since the last call to eventop.dispatch. Only maintained
|
||||
* if the backend is using changesets. */
|
||||
struct event_changelist {
|
||||
struct event_change *changes;
|
||||
int n_changes;
|
||||
int changes_size;
|
||||
};
|
||||
|
||||
#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_)
|
||||
#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. */
|
||||
const struct eventop *evsel;
|
||||
/** Pointer to backend-specific data. */
|
||||
void *evbase;
|
||||
|
||||
/** List of changes to tell backend about at next dispatch. Only used
|
||||
* by the O(1) backends. */
|
||||
struct event_changelist changelist;
|
||||
|
||||
/** Function pointers used to describe the backend that this event_base
|
||||
* uses for signals */
|
||||
const struct eventop *evsigsel;
|
||||
/** Data to implement the common signal handler code. */
|
||||
struct evsig_info sig;
|
||||
|
||||
/** 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. */
|
||||
int event_gotterm;
|
||||
/** Set if we should terminate the loop immediately */
|
||||
int event_break;
|
||||
/** Set if we should start a new instance of the loop immediately. */
|
||||
int event_continue;
|
||||
|
||||
/** The currently running priority of events */
|
||||
int event_running_priority;
|
||||
|
||||
/** Set if we're running the event_base_loop function, to prevent
|
||||
* 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 event_callbacks (ones
|
||||
* that have triggered, and whose callbacks need to be called). Low
|
||||
* priority numbers are more important, and stall higher ones.
|
||||
*/
|
||||
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 */
|
||||
|
||||
/** An array of common_timeout_list* for all of the common timeout
|
||||
* values we know. */
|
||||
struct common_timeout_list **common_timeout_queues;
|
||||
/** The number of entries used in common_timeout_queues */
|
||||
int n_common_timeouts;
|
||||
/** The total size of common_timeout_queues. */
|
||||
int n_common_timeouts_allocated;
|
||||
|
||||
/** 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;
|
||||
|
||||
/** Priority queue of events with timeouts. */
|
||||
struct min_heap timeheap;
|
||||
|
||||
/** Stored timeval: used to avoid calling gettimeofday/clock_gettime
|
||||
* too often. */
|
||||
struct timeval tv_cache;
|
||||
|
||||
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;
|
||||
|
||||
#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;
|
||||
/** 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
|
||||
/** IOCP support structure, if IOCP is enabled. */
|
||||
struct event_iocp_port *iocp;
|
||||
#endif
|
||||
|
||||
/** 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. */
|
||||
int is_notify_pending;
|
||||
/** A socketpair used by some th_notify functions to wake up the main
|
||||
* thread. */
|
||||
evutil_socket_t th_notify_fd[2];
|
||||
/** An event used by some th_notify functions to wake up the main
|
||||
* thread. */
|
||||
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 {
|
||||
TAILQ_ENTRY(event_config_entry) next;
|
||||
|
||||
const char *avoid_method;
|
||||
};
|
||||
|
||||
/** Internal structure: describes the configuration we want for an event_base
|
||||
* that we're about to allocate. */
|
||||
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> */
|
||||
#ifndef LIST_END
|
||||
#define LIST_END(head) NULL
|
||||
#endif
|
||||
|
||||
#ifndef TAILQ_FIRST
|
||||
#define TAILQ_FIRST(head) ((head)->tqh_first)
|
||||
#endif
|
||||
#ifndef TAILQ_END
|
||||
#define TAILQ_END(head) NULL
|
||||
#endif
|
||||
#ifndef TAILQ_NEXT
|
||||
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
|
||||
#endif
|
||||
|
||||
#ifndef TAILQ_FOREACH
|
||||
#define TAILQ_FOREACH(var, head, field) \
|
||||
for ((var) = TAILQ_FIRST(head); \
|
||||
(var) != TAILQ_END(head); \
|
||||
(var) = TAILQ_NEXT(var, field))
|
||||
#endif
|
||||
|
||||
#ifndef TAILQ_INSERT_BEFORE
|
||||
#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
|
||||
(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
|
||||
(elm)->field.tqe_next = (listelm); \
|
||||
*(listelm)->field.tqe_prev = (elm); \
|
||||
(listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
#define N_ACTIVE_CALLBACKS(base) \
|
||||
((base)->event_count_active)
|
||||
|
||||
int evsig_set_handler_(struct event_base *base, int evsignal,
|
||||
void (*fn)(int));
|
||||
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 proceed 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);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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 *));
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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 *));
|
||||
|
||||
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void event_active_later_(struct event *ev, int res);
|
||||
void event_active_later_nolock_(struct event *ev, int res);
|
||||
int 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. */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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
|
||||
be expensive.
|
||||
|
||||
Returns on success; aborts on failure.
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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);
|
||||
|
||||
/* Cleanup function to reset debug mode during shutdown.
|
||||
*
|
||||
* Calling this function doesn't mean it'll be possible to re-enable
|
||||
* debug mode if any events were added.
|
||||
*/
|
||||
void event_disable_debug_mode(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* EVENT_INTERNAL_H_INCLUDED_ */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,294 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2012 Niels Provos, 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 "evconfig-private.h"
|
||||
|
||||
#ifndef _WIN32_WINNT
|
||||
/* Minimum required for InitializeCriticalSectionAndSpinCount */
|
||||
#define _WIN32_WINNT 0x0403
|
||||
#endif
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#include <process.h>
|
||||
#include <stdio.h>
|
||||
#include <mswsock.h>
|
||||
|
||||
#include "event2/util.h"
|
||||
#include "util-internal.h"
|
||||
#include "iocp-internal.h"
|
||||
#include "log-internal.h"
|
||||
#include "mm-internal.h"
|
||||
#include "event-internal.h"
|
||||
#include "evthread-internal.h"
|
||||
|
||||
#define NOTIFICATION_KEY ((ULONG_PTR)-1)
|
||||
|
||||
void
|
||||
event_overlapped_init_(struct event_overlapped *o, iocp_callback cb)
|
||||
{
|
||||
memset(o, 0, sizeof(struct event_overlapped));
|
||||
o->cb = cb;
|
||||
}
|
||||
|
||||
static void
|
||||
handle_entry(OVERLAPPED *o, ULONG_PTR completion_key, DWORD nBytes, int ok)
|
||||
{
|
||||
struct event_overlapped *eo =
|
||||
EVUTIL_UPCAST(o, struct event_overlapped, overlapped);
|
||||
eo->cb(eo, completion_key, nBytes, ok);
|
||||
}
|
||||
|
||||
static void
|
||||
loop(void *port_)
|
||||
{
|
||||
struct event_iocp_port *port = port_;
|
||||
long ms = port->ms;
|
||||
HANDLE p = port->port;
|
||||
|
||||
if (ms <= 0)
|
||||
ms = INFINITE;
|
||||
|
||||
while (1) {
|
||||
OVERLAPPED *overlapped=NULL;
|
||||
ULONG_PTR key=0;
|
||||
DWORD bytes=0;
|
||||
int ok = GetQueuedCompletionStatus(p, &bytes, &key,
|
||||
&overlapped, ms);
|
||||
EnterCriticalSection(&port->lock);
|
||||
if (port->shutdown) {
|
||||
if (--port->n_live_threads == 0)
|
||||
ReleaseSemaphore(port->shutdownSemaphore, 1,
|
||||
NULL);
|
||||
LeaveCriticalSection(&port->lock);
|
||||
return;
|
||||
}
|
||||
LeaveCriticalSection(&port->lock);
|
||||
|
||||
if (key != NOTIFICATION_KEY && overlapped)
|
||||
handle_entry(overlapped, key, bytes, ok);
|
||||
else if (!overlapped)
|
||||
break;
|
||||
}
|
||||
event_warnx("GetQueuedCompletionStatus exited with no event.");
|
||||
EnterCriticalSection(&port->lock);
|
||||
if (--port->n_live_threads == 0)
|
||||
ReleaseSemaphore(port->shutdownSemaphore, 1, NULL);
|
||||
LeaveCriticalSection(&port->lock);
|
||||
}
|
||||
|
||||
int
|
||||
event_iocp_port_associate_(struct event_iocp_port *port, evutil_socket_t fd,
|
||||
ev_uintptr_t key)
|
||||
{
|
||||
HANDLE h;
|
||||
h = CreateIoCompletionPort((HANDLE)fd, port->port, key, port->n_threads);
|
||||
if (!h)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void *
|
||||
get_extension_function(SOCKET s, const GUID *which_fn)
|
||||
{
|
||||
void *ptr = NULL;
|
||||
DWORD bytes=0;
|
||||
WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,
|
||||
(GUID*)which_fn, sizeof(*which_fn),
|
||||
&ptr, sizeof(ptr),
|
||||
&bytes, NULL, NULL);
|
||||
|
||||
/* No need to detect errors here: if ptr is set, then we have a good
|
||||
function pointer. Otherwise, we should behave as if we had no
|
||||
function pointer.
|
||||
*/
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/* Mingw doesn't have these in its mswsock.h. The values are copied from
|
||||
wine.h. Perhaps if we copy them exactly, the cargo will come again.
|
||||
*/
|
||||
#ifndef WSAID_ACCEPTEX
|
||||
#define WSAID_ACCEPTEX \
|
||||
{0xb5367df1,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
|
||||
#endif
|
||||
#ifndef WSAID_CONNECTEX
|
||||
#define WSAID_CONNECTEX \
|
||||
{0x25a207b9,0xddf3,0x4660,{0x8e,0xe9,0x76,0xe5,0x8c,0x74,0x06,0x3e}}
|
||||
#endif
|
||||
#ifndef WSAID_GETACCEPTEXSOCKADDRS
|
||||
#define WSAID_GETACCEPTEXSOCKADDRS \
|
||||
{0xb5367df2,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
|
||||
#endif
|
||||
|
||||
static int extension_fns_initialized = 0;
|
||||
|
||||
static void
|
||||
init_extension_functions(struct win32_extension_fns *ext)
|
||||
{
|
||||
const GUID acceptex = WSAID_ACCEPTEX;
|
||||
const GUID connectex = WSAID_CONNECTEX;
|
||||
const GUID getacceptexsockaddrs = WSAID_GETACCEPTEXSOCKADDRS;
|
||||
SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (s == EVUTIL_INVALID_SOCKET)
|
||||
return;
|
||||
ext->AcceptEx = get_extension_function(s, &acceptex);
|
||||
ext->ConnectEx = get_extension_function(s, &connectex);
|
||||
ext->GetAcceptExSockaddrs = get_extension_function(s,
|
||||
&getacceptexsockaddrs);
|
||||
closesocket(s);
|
||||
|
||||
extension_fns_initialized = 1;
|
||||
}
|
||||
|
||||
static struct win32_extension_fns the_extension_fns;
|
||||
|
||||
const struct win32_extension_fns *
|
||||
event_get_win32_extension_fns_(void)
|
||||
{
|
||||
return &the_extension_fns;
|
||||
}
|
||||
|
||||
#define N_CPUS_DEFAULT 2
|
||||
|
||||
struct event_iocp_port *
|
||||
event_iocp_port_launch_(int n_cpus)
|
||||
{
|
||||
struct event_iocp_port *port;
|
||||
int i;
|
||||
|
||||
if (!extension_fns_initialized)
|
||||
init_extension_functions(&the_extension_fns);
|
||||
|
||||
if (!(port = mm_calloc(1, sizeof(struct event_iocp_port))))
|
||||
return NULL;
|
||||
|
||||
if (n_cpus <= 0)
|
||||
n_cpus = N_CPUS_DEFAULT;
|
||||
port->n_threads = n_cpus * 2;
|
||||
port->threads = mm_calloc(port->n_threads, sizeof(HANDLE));
|
||||
if (!port->threads)
|
||||
goto err;
|
||||
|
||||
port->port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0,
|
||||
n_cpus);
|
||||
port->ms = -1;
|
||||
if (!port->port)
|
||||
goto err;
|
||||
|
||||
port->shutdownSemaphore = CreateSemaphore(NULL, 0, 1, NULL);
|
||||
if (!port->shutdownSemaphore)
|
||||
goto err;
|
||||
|
||||
for (i=0; i<port->n_threads; ++i) {
|
||||
ev_uintptr_t th = _beginthread(loop, 0, port);
|
||||
if (th == (ev_uintptr_t)-1)
|
||||
goto err;
|
||||
port->threads[i] = (HANDLE)th;
|
||||
++port->n_live_threads;
|
||||
}
|
||||
|
||||
InitializeCriticalSectionAndSpinCount(&port->lock, 1000);
|
||||
|
||||
return port;
|
||||
err:
|
||||
if (port->port)
|
||||
CloseHandle(port->port);
|
||||
if (port->threads)
|
||||
mm_free(port->threads);
|
||||
if (port->shutdownSemaphore)
|
||||
CloseHandle(port->shutdownSemaphore);
|
||||
mm_free(port);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
event_iocp_port_unlock_and_free_(struct event_iocp_port *port)
|
||||
{
|
||||
DeleteCriticalSection(&port->lock);
|
||||
CloseHandle(port->port);
|
||||
CloseHandle(port->shutdownSemaphore);
|
||||
mm_free(port->threads);
|
||||
mm_free(port);
|
||||
}
|
||||
|
||||
static int
|
||||
event_iocp_notify_all(struct event_iocp_port *port)
|
||||
{
|
||||
int i, r, ok=1;
|
||||
for (i=0; i<port->n_threads; ++i) {
|
||||
r = PostQueuedCompletionStatus(port->port, 0, NOTIFICATION_KEY,
|
||||
NULL);
|
||||
if (!r)
|
||||
ok = 0;
|
||||
}
|
||||
return ok ? 0 : -1;
|
||||
}
|
||||
|
||||
int
|
||||
event_iocp_shutdown_(struct event_iocp_port *port, long waitMsec)
|
||||
{
|
||||
DWORD ms = INFINITE;
|
||||
int n;
|
||||
|
||||
EnterCriticalSection(&port->lock);
|
||||
port->shutdown = 1;
|
||||
LeaveCriticalSection(&port->lock);
|
||||
event_iocp_notify_all(port);
|
||||
|
||||
if (waitMsec >= 0)
|
||||
ms = waitMsec;
|
||||
|
||||
WaitForSingleObject(port->shutdownSemaphore, ms);
|
||||
EnterCriticalSection(&port->lock);
|
||||
n = port->n_live_threads;
|
||||
LeaveCriticalSection(&port->lock);
|
||||
if (n == 0) {
|
||||
event_iocp_port_unlock_and_free_(port);
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
event_iocp_activate_overlapped_(
|
||||
struct event_iocp_port *port, struct event_overlapped *o,
|
||||
ev_uintptr_t key, ev_uint32_t n)
|
||||
{
|
||||
BOOL r;
|
||||
|
||||
r = PostQueuedCompletionStatus(port->port, n, key, &o->overlapped);
|
||||
return (r==0) ? -1 : 0;
|
||||
}
|
||||
|
||||
struct event_iocp_port *
|
||||
event_base_get_iocp_(struct event_base *base)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return base->iocp;
|
||||
#else
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,610 @@
|
||||
/*
|
||||
* Copyright (c) 2003-2009 Niels Provos <provos@citi.umich.edu>
|
||||
* Copyright (c) 2009-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 EVENT__HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_SYS_PARAM_H
|
||||
#include <sys/param.h>
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#undef WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
|
||||
#ifdef EVENT__HAVE_SYS_IOCTL_H
|
||||
#include <sys/ioctl.h>
|
||||
#endif
|
||||
#include <sys/queue.h>
|
||||
#ifdef EVENT__HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifndef _WIN32
|
||||
#include <syslog.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <limits.h>
|
||||
|
||||
#include "event2/event.h"
|
||||
#include "event2/tag.h"
|
||||
#include "event2/buffer.h"
|
||||
#include "log-internal.h"
|
||||
#include "mm-internal.h"
|
||||
#include "util-internal.h"
|
||||
|
||||
/*
|
||||
Here's our wire format:
|
||||
|
||||
Stream = TaggedData*
|
||||
|
||||
TaggedData = Tag Length Data
|
||||
where the integer value of 'Length' is the length of 'data'.
|
||||
|
||||
Tag = HByte* LByte
|
||||
where HByte is a byte with the high bit set, and LByte is a byte
|
||||
with the high bit clear. The integer value of the tag is taken
|
||||
by concatenating the lower 7 bits from all the tags. So for example,
|
||||
the tag 0x66 is encoded as [66], whereas the tag 0x166 is encoded as
|
||||
[82 66]
|
||||
|
||||
Length = Integer
|
||||
|
||||
Integer = NNibbles Nibble* Padding?
|
||||
where NNibbles is a 4-bit value encoding the number of nibbles-1,
|
||||
and each Nibble is 4 bits worth of encoded integer, in big-endian
|
||||
order. If the total encoded integer size is an odd number of nibbles,
|
||||
a final padding nibble with value 0 is appended.
|
||||
*/
|
||||
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evtag_decode_int64(ev_uint64_t *pnumber, struct evbuffer *evbuf);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evtag_encode_tag(struct evbuffer *evbuf, ev_uint32_t tag);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evtag_decode_tag(ev_uint32_t *ptag, struct evbuffer *evbuf);
|
||||
|
||||
void
|
||||
evtag_init(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
* We encode integers by nibbles; the first nibble contains the number
|
||||
* of significant nibbles - 1; this allows us to encode up to 64-bit
|
||||
* integers. This function is byte-order independent.
|
||||
*
|
||||
* @param number a 32-bit unsigned integer to encode
|
||||
* @param data a pointer to where the data should be written. Must
|
||||
* have at least 5 bytes free.
|
||||
* @return the number of bytes written into data.
|
||||
*/
|
||||
|
||||
#define ENCODE_INT_INTERNAL(data, number) do { \
|
||||
int off = 1, nibbles = 0; \
|
||||
\
|
||||
memset(data, 0, sizeof(number)+1); \
|
||||
while (number) { \
|
||||
if (off & 0x1) \
|
||||
data[off/2] = (data[off/2] & 0xf0) | (number & 0x0f); \
|
||||
else \
|
||||
data[off/2] = (data[off/2] & 0x0f) | \
|
||||
((number & 0x0f) << 4); \
|
||||
number >>= 4; \
|
||||
off++; \
|
||||
} \
|
||||
\
|
||||
if (off > 2) \
|
||||
nibbles = off - 2; \
|
||||
\
|
||||
/* Off - 1 is the number of encoded nibbles */ \
|
||||
data[0] = (data[0] & 0x0f) | ((nibbles & 0x0f) << 4); \
|
||||
\
|
||||
return ((off + 1) / 2); \
|
||||
} while (0)
|
||||
|
||||
static inline int
|
||||
encode_int_internal(ev_uint8_t *data, ev_uint32_t number)
|
||||
{
|
||||
ENCODE_INT_INTERNAL(data, number);
|
||||
}
|
||||
|
||||
static inline int
|
||||
encode_int64_internal(ev_uint8_t *data, ev_uint64_t number)
|
||||
{
|
||||
ENCODE_INT_INTERNAL(data, number);
|
||||
}
|
||||
|
||||
void
|
||||
evtag_encode_int(struct evbuffer *evbuf, ev_uint32_t number)
|
||||
{
|
||||
ev_uint8_t data[5];
|
||||
int len = encode_int_internal(data, number);
|
||||
evbuffer_add(evbuf, data, len);
|
||||
}
|
||||
|
||||
void
|
||||
evtag_encode_int64(struct evbuffer *evbuf, ev_uint64_t number)
|
||||
{
|
||||
ev_uint8_t data[9];
|
||||
int len = encode_int64_internal(data, number);
|
||||
evbuffer_add(evbuf, data, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Support variable length encoding of tags; we use the high bit in each
|
||||
* octet as a continuation signal.
|
||||
*/
|
||||
|
||||
int
|
||||
evtag_encode_tag(struct evbuffer *evbuf, ev_uint32_t tag)
|
||||
{
|
||||
int bytes = 0;
|
||||
ev_uint8_t data[5];
|
||||
|
||||
memset(data, 0, sizeof(data));
|
||||
do {
|
||||
ev_uint8_t lower = tag & 0x7f;
|
||||
tag >>= 7;
|
||||
|
||||
if (tag)
|
||||
lower |= 0x80;
|
||||
|
||||
data[bytes++] = lower;
|
||||
} while (tag);
|
||||
|
||||
if (evbuf != NULL)
|
||||
evbuffer_add(evbuf, data, bytes);
|
||||
|
||||
return (bytes);
|
||||
}
|
||||
|
||||
static int
|
||||
decode_tag_internal(ev_uint32_t *ptag, struct evbuffer *evbuf, int dodrain)
|
||||
{
|
||||
ev_uint32_t number = 0;
|
||||
size_t len = evbuffer_get_length(evbuf);
|
||||
ev_uint8_t *data;
|
||||
size_t count = 0;
|
||||
int shift = 0, done = 0;
|
||||
|
||||
/*
|
||||
* the encoding of a number is at most one byte more than its
|
||||
* storage size. however, it may also be much smaller.
|
||||
*/
|
||||
size_t pullup_len = len < sizeof(number) + 1 ? len : sizeof(number) + 1;
|
||||
data = evbuffer_pullup(
|
||||
evbuf, pullup_len);
|
||||
if (!data)
|
||||
return (-1);
|
||||
|
||||
while (count++ < pullup_len) {
|
||||
ev_uint8_t lower = *data++;
|
||||
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)) {
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!done)
|
||||
return (-1);
|
||||
|
||||
if (dodrain)
|
||||
evbuffer_drain(evbuf, count);
|
||||
|
||||
if (ptag != NULL)
|
||||
*ptag = number;
|
||||
|
||||
return count > INT_MAX ? INT_MAX : (int)(count);
|
||||
}
|
||||
|
||||
int
|
||||
evtag_decode_tag(ev_uint32_t *ptag, struct evbuffer *evbuf)
|
||||
{
|
||||
return (decode_tag_internal(ptag, evbuf, 1 /* dodrain */));
|
||||
}
|
||||
|
||||
/*
|
||||
* Marshal a data type, the general format is as follows:
|
||||
*
|
||||
* tag number: one byte; length: var bytes; payload: var bytes
|
||||
*/
|
||||
|
||||
void
|
||||
evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag,
|
||||
const void *data, ev_uint32_t len)
|
||||
{
|
||||
evtag_encode_tag(evbuf, tag);
|
||||
evtag_encode_int(evbuf, len);
|
||||
evbuffer_add(evbuf, (void *)data, len);
|
||||
}
|
||||
|
||||
void
|
||||
evtag_marshal_buffer(struct evbuffer *evbuf, ev_uint32_t tag,
|
||||
struct evbuffer *data)
|
||||
{
|
||||
evtag_encode_tag(evbuf, tag);
|
||||
/* XXX support more than UINT32_MAX data */
|
||||
evtag_encode_int(evbuf, (ev_uint32_t)evbuffer_get_length(data));
|
||||
evbuffer_add_buffer(evbuf, data);
|
||||
}
|
||||
|
||||
/* Marshaling for integers */
|
||||
void
|
||||
evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag, ev_uint32_t integer)
|
||||
{
|
||||
ev_uint8_t data[5];
|
||||
int len = encode_int_internal(data, integer);
|
||||
|
||||
evtag_encode_tag(evbuf, tag);
|
||||
evtag_encode_int(evbuf, len);
|
||||
evbuffer_add(evbuf, data, len);
|
||||
}
|
||||
|
||||
void
|
||||
evtag_marshal_int64(struct evbuffer *evbuf, ev_uint32_t tag,
|
||||
ev_uint64_t integer)
|
||||
{
|
||||
ev_uint8_t data[9];
|
||||
int len = encode_int64_internal(data, integer);
|
||||
|
||||
evtag_encode_tag(evbuf, tag);
|
||||
evtag_encode_int(evbuf, len);
|
||||
evbuffer_add(evbuf, data, len);
|
||||
}
|
||||
|
||||
void
|
||||
evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag, const char *string)
|
||||
{
|
||||
/* TODO support strings longer than UINT32_MAX ? */
|
||||
evtag_marshal(buf, tag, string, (ev_uint32_t)strlen(string));
|
||||
}
|
||||
|
||||
void
|
||||
evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag, struct timeval *tv)
|
||||
{
|
||||
ev_uint8_t data[10];
|
||||
int len = encode_int_internal(data, tv->tv_sec);
|
||||
len += encode_int_internal(data + len, tv->tv_usec);
|
||||
evtag_marshal(evbuf, tag, data, len);
|
||||
}
|
||||
|
||||
#define DECODE_INT_INTERNAL(number, maxnibbles, pnumber, evbuf, offset) \
|
||||
do { \
|
||||
ev_uint8_t *data; \
|
||||
ev_ssize_t len = evbuffer_get_length(evbuf) - offset; \
|
||||
int nibbles = 0; \
|
||||
\
|
||||
if (len <= 0) \
|
||||
return (-1); \
|
||||
\
|
||||
/* XXX(niels): faster? */ \
|
||||
data = evbuffer_pullup(evbuf, offset + 1) + offset; \
|
||||
if (!data) \
|
||||
return (-1); \
|
||||
\
|
||||
nibbles = ((data[0] & 0xf0) >> 4) + 1; \
|
||||
if (nibbles > maxnibbles || (nibbles >> 1) + 1 > len) \
|
||||
return (-1); \
|
||||
len = (nibbles >> 1) + 1; \
|
||||
\
|
||||
data = evbuffer_pullup(evbuf, offset + len) + offset; \
|
||||
if (!data) \
|
||||
return (-1); \
|
||||
\
|
||||
while (nibbles > 0) { \
|
||||
number <<= 4; \
|
||||
if (nibbles & 0x1) \
|
||||
number |= data[nibbles >> 1] & 0x0f; \
|
||||
else \
|
||||
number |= (data[nibbles >> 1] & 0xf0) >> 4; \
|
||||
nibbles--; \
|
||||
} \
|
||||
\
|
||||
*pnumber = number; \
|
||||
\
|
||||
return (int)(len); \
|
||||
} while (0)
|
||||
|
||||
/* Internal: decode an integer from an evbuffer, without draining it.
|
||||
* Only integers up to 32-bits are supported.
|
||||
*
|
||||
* @param evbuf the buffer to read from
|
||||
* @param offset an index into the buffer at which we should start reading.
|
||||
* @param pnumber a pointer to receive the integer.
|
||||
* @return The length of the number as encoded, or -1 on error.
|
||||
*/
|
||||
|
||||
static int
|
||||
decode_int_internal(ev_uint32_t *pnumber, struct evbuffer *evbuf, int offset)
|
||||
{
|
||||
ev_uint32_t number = 0;
|
||||
DECODE_INT_INTERNAL(number, 8, pnumber, evbuf, offset);
|
||||
}
|
||||
|
||||
static int
|
||||
decode_int64_internal(ev_uint64_t *pnumber, struct evbuffer *evbuf, int offset)
|
||||
{
|
||||
ev_uint64_t number = 0;
|
||||
DECODE_INT_INTERNAL(number, 16, pnumber, evbuf, offset);
|
||||
}
|
||||
|
||||
int
|
||||
evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf)
|
||||
{
|
||||
int res = decode_int_internal(pnumber, evbuf, 0);
|
||||
if (res != -1)
|
||||
evbuffer_drain(evbuf, res);
|
||||
|
||||
return (res == -1 ? -1 : 0);
|
||||
}
|
||||
|
||||
int
|
||||
evtag_decode_int64(ev_uint64_t *pnumber, struct evbuffer *evbuf)
|
||||
{
|
||||
int res = decode_int64_internal(pnumber, evbuf, 0);
|
||||
if (res != -1)
|
||||
evbuffer_drain(evbuf, res);
|
||||
|
||||
return (res == -1 ? -1 : 0);
|
||||
}
|
||||
|
||||
int
|
||||
evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag)
|
||||
{
|
||||
return (decode_tag_internal(ptag, evbuf, 0 /* dodrain */));
|
||||
}
|
||||
|
||||
int
|
||||
evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength)
|
||||
{
|
||||
int res, len;
|
||||
|
||||
len = decode_tag_internal(NULL, evbuf, 0 /* dodrain */);
|
||||
if (len == -1)
|
||||
return (-1);
|
||||
|
||||
res = decode_int_internal(plength, evbuf, len);
|
||||
if (res == -1)
|
||||
return (-1);
|
||||
|
||||
*plength += res + len;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength)
|
||||
{
|
||||
int res, len;
|
||||
|
||||
len = decode_tag_internal(NULL, evbuf, 0 /* dodrain */);
|
||||
if (len == -1)
|
||||
return (-1);
|
||||
|
||||
res = decode_int_internal(plength, evbuf, len);
|
||||
if (res == -1)
|
||||
return (-1);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* just unmarshals the header and returns the length of the remaining data */
|
||||
|
||||
int
|
||||
evtag_unmarshal_header(struct evbuffer *evbuf, ev_uint32_t *ptag)
|
||||
{
|
||||
ev_uint32_t len;
|
||||
|
||||
if (decode_tag_internal(ptag, evbuf, 1 /* dodrain */) == -1)
|
||||
return (-1);
|
||||
if (evtag_decode_int(&len, evbuf) == -1 || len > INT_MAX)
|
||||
return (-1);
|
||||
|
||||
if (evbuffer_get_length(evbuf) < len)
|
||||
return (-1);
|
||||
|
||||
return (len);
|
||||
}
|
||||
|
||||
int
|
||||
evtag_consume(struct evbuffer *evbuf)
|
||||
{
|
||||
int len;
|
||||
if ((len = evtag_unmarshal_header(evbuf, NULL)) == -1)
|
||||
return (-1);
|
||||
evbuffer_drain(evbuf, len);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Reads the data type from an event buffer */
|
||||
|
||||
int
|
||||
evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag, struct evbuffer *dst)
|
||||
{
|
||||
int len;
|
||||
|
||||
if ((len = evtag_unmarshal_header(src, ptag)) == -1)
|
||||
return (-1);
|
||||
|
||||
if (evbuffer_add(dst, evbuffer_pullup(src, len), len) == -1)
|
||||
return (-1);
|
||||
|
||||
evbuffer_drain(src, len);
|
||||
|
||||
return (len);
|
||||
}
|
||||
|
||||
/* Marshaling for integers */
|
||||
|
||||
int
|
||||
evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag,
|
||||
ev_uint32_t *pinteger)
|
||||
{
|
||||
ev_uint32_t tag;
|
||||
ev_uint32_t len;
|
||||
int result;
|
||||
|
||||
if (decode_tag_internal(&tag, evbuf, 1 /* dodrain */) == -1)
|
||||
return (-1);
|
||||
if (need_tag != tag)
|
||||
return (-1);
|
||||
if (evtag_decode_int(&len, evbuf) == -1)
|
||||
return (-1);
|
||||
|
||||
if (evbuffer_get_length(evbuf) < len)
|
||||
return (-1);
|
||||
|
||||
result = decode_int_internal(pinteger, evbuf, 0);
|
||||
evbuffer_drain(evbuf, len);
|
||||
if (result < 0 || (size_t)result > len) /* XXX Should this be != rather than > ?*/
|
||||
return (-1);
|
||||
else
|
||||
return result;
|
||||
}
|
||||
|
||||
int
|
||||
evtag_unmarshal_int64(struct evbuffer *evbuf, ev_uint32_t need_tag,
|
||||
ev_uint64_t *pinteger)
|
||||
{
|
||||
ev_uint32_t tag;
|
||||
ev_uint32_t len;
|
||||
int result;
|
||||
|
||||
if (decode_tag_internal(&tag, evbuf, 1 /* dodrain */) == -1)
|
||||
return (-1);
|
||||
if (need_tag != tag)
|
||||
return (-1);
|
||||
if (evtag_decode_int(&len, evbuf) == -1)
|
||||
return (-1);
|
||||
|
||||
if (evbuffer_get_length(evbuf) < len)
|
||||
return (-1);
|
||||
|
||||
result = decode_int64_internal(pinteger, evbuf, 0);
|
||||
evbuffer_drain(evbuf, len);
|
||||
if (result < 0 || (size_t)result > len) /* XXX Should this be != rather than > ?*/
|
||||
return (-1);
|
||||
else
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Unmarshal a fixed length tag */
|
||||
|
||||
int
|
||||
evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag, void *data,
|
||||
size_t len)
|
||||
{
|
||||
ev_uint32_t tag;
|
||||
int tag_len;
|
||||
|
||||
/* Now unmarshal a tag and check that it matches the tag we want */
|
||||
if ((tag_len = evtag_unmarshal_header(src, &tag)) < 0 ||
|
||||
tag != need_tag)
|
||||
return (-1);
|
||||
|
||||
if ((size_t)tag_len != len)
|
||||
return (-1);
|
||||
|
||||
evbuffer_remove(src, data, len);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag,
|
||||
char **pstring)
|
||||
{
|
||||
ev_uint32_t tag;
|
||||
int tag_len;
|
||||
|
||||
if ((tag_len = evtag_unmarshal_header(evbuf, &tag)) == -1 ||
|
||||
tag != need_tag)
|
||||
return (-1);
|
||||
|
||||
*pstring = mm_malloc(tag_len + 1);
|
||||
if (*pstring == NULL) {
|
||||
event_warn("%s: malloc", __func__);
|
||||
return -1;
|
||||
}
|
||||
evbuffer_remove(evbuf, *pstring, tag_len);
|
||||
(*pstring)[tag_len] = '\0';
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag,
|
||||
struct timeval *ptv)
|
||||
{
|
||||
ev_uint32_t tag;
|
||||
ev_uint32_t integer;
|
||||
int len, offset, offset2;
|
||||
int result = -1;
|
||||
|
||||
if ((len = evtag_unmarshal_header(evbuf, &tag)) == -1)
|
||||
return (-1);
|
||||
if (tag != need_tag)
|
||||
goto done;
|
||||
if ((offset = decode_int_internal(&integer, evbuf, 0)) == -1)
|
||||
goto done;
|
||||
ptv->tv_sec = integer;
|
||||
if ((offset2 = decode_int_internal(&integer, evbuf, offset)) == -1)
|
||||
goto done;
|
||||
ptv->tv_usec = integer;
|
||||
if (offset + offset2 > len) /* XXX Should this be != instead of > ? */
|
||||
goto done;
|
||||
|
||||
result = 0;
|
||||
done:
|
||||
evbuffer_drain(evbuf, len);
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* 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 EVMAP_INTERNAL_H_INCLUDED_
|
||||
#define EVMAP_INTERNAL_H_INCLUDED_
|
||||
|
||||
/** @file evmap-internal.h
|
||||
*
|
||||
* An event_map is a utility structure to map each fd or signal to zero or
|
||||
* more events. Functions to manipulate event_maps should only be used from
|
||||
* inside libevent. They generally need to hold the lock on the corresponding
|
||||
* event_base.
|
||||
**/
|
||||
|
||||
struct event_base;
|
||||
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);
|
||||
|
||||
/** 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);
|
||||
|
||||
/** 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
|
||||
underlying eventops about the fd if its state has changed.
|
||||
|
||||
Requires that ev is not already added.
|
||||
|
||||
@param base the event_base to operate on.
|
||||
@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);
|
||||
/** 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.
|
||||
|
||||
@param base the event_base to operate on.
|
||||
@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);
|
||||
/** 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);
|
||||
|
||||
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
|
||||
/* 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_INTERNAL_H_INCLUDED_ */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,451 @@
|
||||
/*
|
||||
* Submitted by David Pacheco (dp.spambait@gmail.com)
|
||||
*
|
||||
* Copyright 2006-2007 Niels Provos
|
||||
* Copyright 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 SUN MICROSYSTEMS, INC. ``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 SUN MICROSYSTEMS, INC. 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2007 Sun Microsystems. All rights reserved.
|
||||
* Use is subject to license terms.
|
||||
*/
|
||||
|
||||
/*
|
||||
* evport.c: event backend using Solaris 10 event ports. See port_create(3C).
|
||||
* This implementation is loosely modeled after the one used for select(2) (in
|
||||
* select.c).
|
||||
*
|
||||
* The outstanding events are tracked in a data structure called evport_data.
|
||||
* Each entry in the ed_fds array corresponds to a file descriptor, and contains
|
||||
* pointers to the read and write events that correspond to that fd. (That is,
|
||||
* when the file is readable, the "read" event should handle it, etc.)
|
||||
*
|
||||
* evport_add and evport_del update this data structure. evport_dispatch uses it
|
||||
* to determine where to callback when an event occurs (which it gets from
|
||||
* port_getn).
|
||||
*
|
||||
* Helper functions are used: grow() grows the file descriptor array as
|
||||
* necessary when large fd's come in. reassociate() takes care of maintaining
|
||||
* the proper file-descriptor/event-port associations.
|
||||
*
|
||||
* As in the select(2) implementation, signals are handled by evsignal.
|
||||
*/
|
||||
|
||||
#include "event2/event-config.h"
|
||||
#include "evconfig-private.h"
|
||||
|
||||
#ifdef EVENT__HAVE_EVENT_PORTS
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/queue.h>
|
||||
#include <errno.h>
|
||||
#include <poll.h>
|
||||
#include <port.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "event2/thread.h"
|
||||
|
||||
#include "evthread-internal.h"
|
||||
#include "event-internal.h"
|
||||
#include "log-internal.h"
|
||||
#include "evsignal-internal.h"
|
||||
#include "evmap-internal.h"
|
||||
|
||||
#define INITIAL_EVENTS_PER_GETN 8
|
||||
#define MAX_EVENTS_PER_GETN 4096
|
||||
|
||||
/*
|
||||
* Per-file-descriptor information about what events we're subscribed to. These
|
||||
* fields are NULL if no event is subscribed to either of them.
|
||||
*/
|
||||
|
||||
struct fd_info {
|
||||
/* 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)
|
||||
#define FDI_HAS_WRITE(fdi) ((fdi)->fdi_what & EV_WRITE)
|
||||
#define FDI_HAS_EVENTS(fdi) (FDI_HAS_READ(fdi) || FDI_HAS_WRITE(fdi))
|
||||
#define FDI_TO_SYSEVENTS(fdi) (FDI_HAS_READ(fdi) ? POLLIN : 0) | \
|
||||
(FDI_HAS_WRITE(fdi) ? POLLOUT : 0)
|
||||
|
||||
struct evport_data {
|
||||
int ed_port; /* event port for system events */
|
||||
/* 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;
|
||||
/* storage space for incoming events. */
|
||||
port_event_t *ed_pevtlist;
|
||||
|
||||
};
|
||||
|
||||
static void* evport_init(struct event_base *);
|
||||
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",
|
||||
evport_init,
|
||||
evport_add,
|
||||
evport_del,
|
||||
evport_dispatch,
|
||||
evport_dealloc,
|
||||
1, /* need reinit */
|
||||
0, /* features */
|
||||
sizeof(struct fd_info), /* fdinfo length */
|
||||
};
|
||||
|
||||
/*
|
||||
* Initialize the event port implementation.
|
||||
*/
|
||||
|
||||
static void*
|
||||
evport_init(struct event_base *base)
|
||||
{
|
||||
struct evport_data *evpd;
|
||||
|
||||
if (!(evpd = mm_calloc(1, sizeof(struct evport_data))))
|
||||
return (NULL);
|
||||
|
||||
if ((evpd->ed_port = port_create()) == -1) {
|
||||
mm_free(evpd);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (grow(evpd, INITIAL_EVENTS_PER_GETN) < 0) {
|
||||
close(evpd->ed_port);
|
||||
mm_free(evpd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
evpd->ed_npending = 0;
|
||||
|
||||
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
|
||||
* checks all file descriptors, this function can be expensive when the maximum
|
||||
* file descriptor ever used is rather large.
|
||||
*/
|
||||
|
||||
static void
|
||||
check_evportop(struct evport_data *evpd)
|
||||
{
|
||||
EVUTIL_ASSERT(evpd);
|
||||
EVUTIL_ASSERT(evpd->ed_port > 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Verifies very basic integrity of a given port_event.
|
||||
*/
|
||||
static void
|
||||
check_event(port_event_t* pevt)
|
||||
{
|
||||
/*
|
||||
* We've only registered for PORT_SOURCE_FD events. The only
|
||||
* other thing we can legitimately receive is PORT_SOURCE_ALERT,
|
||||
* but since we're not using port_alert either, we can assume
|
||||
* PORT_SOURCE_FD.
|
||||
*/
|
||||
EVUTIL_ASSERT(pevt->portev_source == PORT_SOURCE_FD);
|
||||
}
|
||||
|
||||
#else
|
||||
#define check_evportop(epop)
|
||||
#define check_event(pevt)
|
||||
#endif /* CHECK_INVARIANTS */
|
||||
|
||||
/*
|
||||
* (Re)associates the given file descriptor with the event port. The OS events
|
||||
* are specified (implicitly) from the fd_info struct.
|
||||
*/
|
||||
static int
|
||||
reassociate(struct evport_data *epdp, struct fd_info *fdip, int fd)
|
||||
{
|
||||
int sysevents = FDI_TO_SYSEVENTS(fdip);
|
||||
|
||||
if (sysevents != 0) {
|
||||
if (port_associate(epdp->ed_port, PORT_SOURCE_FD,
|
||||
fd, sysevents, fdip) == -1) {
|
||||
event_warn("port_associate");
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
check_evportop(epdp);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Main event loop - polls port_getn for some number of events, and processes
|
||||
* them.
|
||||
*/
|
||||
|
||||
static int
|
||||
evport_dispatch(struct event_base *base, struct timeval *tv)
|
||||
{
|
||||
int i, res;
|
||||
struct evport_data *epdp = base->evbase;
|
||||
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 (ed_maxevents)) in
|
||||
* nevents.
|
||||
*/
|
||||
int nevents = 1;
|
||||
|
||||
/*
|
||||
* We have to convert a struct timeval to a struct timespec
|
||||
* (only difference is nanoseconds vs. microseconds). If no time-based
|
||||
* events are active, we should wait for I/O (and tv == NULL).
|
||||
*/
|
||||
struct timespec ts;
|
||||
struct timespec *ts_p = NULL;
|
||||
if (tv != NULL) {
|
||||
ts.tv_sec = tv->tv_sec;
|
||||
ts.tv_nsec = tv->tv_usec * 1000;
|
||||
ts_p = &ts;
|
||||
}
|
||||
|
||||
/*
|
||||
* Before doing anything else, we need to reassociate the events we hit
|
||||
* last time which need reassociation. See comment at the end of the
|
||||
* loop below.
|
||||
*/
|
||||
for (i = 0; i < epdp->ed_npending; ++i) {
|
||||
struct fd_info *fdi = NULL;
|
||||
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)) {
|
||||
reassociate(epdp, fdi, fd);
|
||||
/* 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, epdp->ed_maxevents,
|
||||
(unsigned int *) &nevents, ts_p);
|
||||
|
||||
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
|
||||
|
||||
if (res == -1) {
|
||||
if (errno == EINTR || errno == EAGAIN) {
|
||||
return (0);
|
||||
} else if (errno == ETIME) {
|
||||
if (nevents == 0)
|
||||
return (0);
|
||||
} else {
|
||||
event_warn("port_getn");
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
event_debug(("%s: port_getn reports %d events", __func__, nevents));
|
||||
|
||||
for (i = 0; i < nevents; ++i) {
|
||||
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
|
||||
* (because we have to pass this to the callback)
|
||||
*/
|
||||
res = 0;
|
||||
if (pevt->portev_events & (POLLERR|POLLHUP)) {
|
||||
res = EV_READ | EV_WRITE;
|
||||
} else {
|
||||
if (pevt->portev_events & POLLIN)
|
||||
res |= EV_READ;
|
||||
if (pevt->portev_events & POLLOUT)
|
||||
res |= EV_WRITE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check for the error situations or a hangup situation
|
||||
*/
|
||||
if (pevt->portev_events & (POLLERR|POLLHUP|POLLNVAL))
|
||||
res |= EV_READ|EV_WRITE;
|
||||
|
||||
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);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Adds the given event (so that you will be notified when it happens via
|
||||
* the callback function).
|
||||
*/
|
||||
|
||||
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 = p;
|
||||
|
||||
check_evportop(evpd);
|
||||
|
||||
fdi->fdi_what |= events;
|
||||
|
||||
return reassociate(evpd, fdi, fd);
|
||||
}
|
||||
|
||||
/*
|
||||
* Removes the given event from the list of events to wait for.
|
||||
*/
|
||||
|
||||
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 = p;
|
||||
int associated = ! fdi->pending_idx_plus_1;
|
||||
|
||||
check_evportop(evpd);
|
||||
|
||||
fdi->fdi_what &= ~(events &(EV_READ|EV_WRITE));
|
||||
|
||||
if (associated) {
|
||||
if (!FDI_HAS_EVENTS(fdi) &&
|
||||
port_dissociate(evpd->ed_port, PORT_SOURCE_FD, fd) == -1) {
|
||||
/*
|
||||
* Ignore EBADFD error the fd could have been closed
|
||||
* before event_del() was called.
|
||||
*/
|
||||
if (errno != EBADFD) {
|
||||
event_warn("port_dissociate");
|
||||
return (-1);
|
||||
}
|
||||
} else {
|
||||
if (FDI_HAS_EVENTS(fdi)) {
|
||||
return (reassociate(evpd, fdi, fd));
|
||||
}
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
evport_dealloc(struct event_base *base)
|
||||
{
|
||||
struct evport_data *evpd = base->evbase;
|
||||
|
||||
evsig_dealloc_(base);
|
||||
|
||||
close(evpd->ed_port);
|
||||
|
||||
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 */
|
||||
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
* Copyright (c) 2006-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 EVRPC_INTERNAL_H_INCLUDED_
|
||||
#define EVRPC_INTERNAL_H_INCLUDED_
|
||||
|
||||
#include "event2/http.h"
|
||||
#include "http-internal.h"
|
||||
|
||||
struct evrpc;
|
||||
struct evrpc_request_wrapper;
|
||||
|
||||
#define EVRPC_URI_PREFIX "/.rpc."
|
||||
|
||||
struct evrpc_hook {
|
||||
TAILQ_ENTRY(evrpc_hook) next;
|
||||
|
||||
/* returns EVRPC_TERMINATE; if the rpc should be aborted.
|
||||
* a hook is is allowed to rewrite the evbuffer
|
||||
*/
|
||||
int (*process)(void *, struct evhttp_request *,
|
||||
struct evbuffer *, void *);
|
||||
void *process_arg;
|
||||
};
|
||||
|
||||
TAILQ_HEAD(evrpc_hook_list, evrpc_hook);
|
||||
|
||||
/*
|
||||
* this is shared between the base and the pool, so that we can reuse
|
||||
* the hook adding functions; we alias both evrpc_pool and evrpc_base
|
||||
* to this common structure.
|
||||
*/
|
||||
|
||||
struct evrpc_hook_ctx;
|
||||
TAILQ_HEAD(evrpc_pause_list, evrpc_hook_ctx);
|
||||
|
||||
struct evrpc_hooks_ {
|
||||
/* hooks for processing outbound and inbound rpcs */
|
||||
struct evrpc_hook_list in_hooks;
|
||||
struct evrpc_hook_list out_hooks;
|
||||
|
||||
struct evrpc_pause_list pause_requests;
|
||||
};
|
||||
|
||||
#define input_hooks common.in_hooks
|
||||
#define output_hooks common.out_hooks
|
||||
#define paused_requests common.pause_requests
|
||||
|
||||
struct evrpc_base {
|
||||
struct evrpc_hooks_ common;
|
||||
|
||||
/* the HTTP server under which we register our RPC calls */
|
||||
struct evhttp* http_server;
|
||||
|
||||
/* a list of all RPCs registered with us */
|
||||
TAILQ_HEAD(evrpc_list, evrpc) registered_rpcs;
|
||||
};
|
||||
|
||||
struct evrpc_req_generic;
|
||||
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 event_base *base;
|
||||
|
||||
struct evconq connections;
|
||||
|
||||
int timeout;
|
||||
|
||||
TAILQ_HEAD(evrpc_requestq, evrpc_request_wrapper) (requests);
|
||||
};
|
||||
|
||||
struct evrpc_hook_ctx {
|
||||
TAILQ_ENTRY(evrpc_hook_ctx) next;
|
||||
|
||||
void *ctx;
|
||||
void (*cb)(void *, enum EVRPC_HOOK_RESULT);
|
||||
};
|
||||
|
||||
struct evrpc_meta {
|
||||
TAILQ_ENTRY(evrpc_meta) next;
|
||||
char *key;
|
||||
|
||||
void *data;
|
||||
size_t data_size;
|
||||
};
|
||||
|
||||
TAILQ_HEAD(evrpc_meta_list, evrpc_meta);
|
||||
|
||||
struct evrpc_hook_meta {
|
||||
struct evrpc_meta_list meta_data;
|
||||
struct evhttp_connection *evcon;
|
||||
};
|
||||
|
||||
/* allows association of meta data with a request */
|
||||
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);
|
||||
|
||||
/* frees the meta data associated with a request */
|
||||
static void evrpc_hook_context_free_(struct evrpc_hook_meta *ctx);
|
||||
|
||||
/* the server side of an rpc */
|
||||
|
||||
/* We alias the RPC specific structs to this voided one */
|
||||
struct evrpc_req_generic {
|
||||
/*
|
||||
* allows association of meta data via hooks - needs to be
|
||||
* synchronized with evrpc_request_wrapper
|
||||
*/
|
||||
struct evrpc_hook_meta *hook_meta;
|
||||
|
||||
/* the unmarshaled request object */
|
||||
void *request;
|
||||
|
||||
/* the empty reply object that needs to be filled in */
|
||||
void *reply;
|
||||
|
||||
/*
|
||||
* the static structure for this rpc; that can be used to
|
||||
* automatically unmarshal and marshal the http buffers.
|
||||
*/
|
||||
struct evrpc *rpc;
|
||||
|
||||
/*
|
||||
* the http request structure on which we need to answer.
|
||||
*/
|
||||
struct evhttp_request* http_req;
|
||||
|
||||
/*
|
||||
* Temporary data store for marshaled data
|
||||
*/
|
||||
struct evbuffer* rpc_data;
|
||||
};
|
||||
|
||||
/* the client side of an rpc request */
|
||||
struct evrpc_request_wrapper {
|
||||
/*
|
||||
* allows association of meta data via hooks - needs to be
|
||||
* synchronized with evrpc_req_generic.
|
||||
*/
|
||||
struct evrpc_hook_meta *hook_meta;
|
||||
|
||||
TAILQ_ENTRY(evrpc_request_wrapper) next;
|
||||
|
||||
/* pool on which this rpc request is being made */
|
||||
struct evrpc_pool *pool;
|
||||
|
||||
/* connection on which the request is being sent */
|
||||
struct evhttp_connection *evcon;
|
||||
|
||||
/* the actual request */
|
||||
struct evhttp_request *req;
|
||||
|
||||
/* event for implementing request timeouts */
|
||||
struct event ev_timeout;
|
||||
|
||||
/* the name of the rpc */
|
||||
char *name;
|
||||
|
||||
/* callback */
|
||||
void (*cb)(struct evrpc_status*, void *request, void *reply, void *arg);
|
||||
void *cb_arg;
|
||||
|
||||
void *request;
|
||||
void *reply;
|
||||
|
||||
/* unmarshals the buffer into the proper request structure */
|
||||
void (*request_marshal)(struct evbuffer *, void *);
|
||||
|
||||
/* removes all stored state in the reply */
|
||||
void (*reply_clear)(void *);
|
||||
|
||||
/* marshals the reply into a buffer */
|
||||
int (*reply_unmarshal)(void *, struct evbuffer*);
|
||||
};
|
||||
|
||||
#endif /* EVRPC_INTERNAL_H_INCLUDED_ */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
|
||||
* Copyright 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 EVSIGNAL_INTERNAL_H_INCLUDED_
|
||||
#define EVSIGNAL_INTERNAL_H_INCLUDED_
|
||||
|
||||
#ifndef evutil_socket_t
|
||||
#include "event2/util.h"
|
||||
#endif
|
||||
#include <signal.h>
|
||||
|
||||
typedef void (*ev_sighandler_t)(int);
|
||||
|
||||
/* Data structure for the default signal-handling implementation in signal.c
|
||||
*/
|
||||
struct evsig_info {
|
||||
/* Event watching ev_signal_pair[1] */
|
||||
struct event ev_signal;
|
||||
/* Socketpair used to send notifications from the signal handler */
|
||||
evutil_socket_t ev_signal_pair[2];
|
||||
/* True iff we've added the ev_signal event yet. */
|
||||
int ev_signal_added;
|
||||
/* Count of the number of signals we're currently watching. */
|
||||
int ev_n_signals_added;
|
||||
|
||||
/* Array of previous signal handler objects before Libevent started
|
||||
* messing with them. Used to restore old signal handlers. */
|
||||
#ifdef EVENT__HAVE_SIGACTION
|
||||
struct sigaction **sh_old;
|
||||
#else
|
||||
ev_sighandler_t **sh_old;
|
||||
#endif
|
||||
/* Size of sh_old. */
|
||||
int sh_old_max;
|
||||
};
|
||||
int evsig_init_(struct event_base *);
|
||||
void evsig_dealloc_(struct event_base *);
|
||||
|
||||
void evsig_set_base_(struct event_base *base);
|
||||
void evsig_free_globals_(void);
|
||||
|
||||
#endif /* EVSIGNAL_INTERNAL_H_INCLUDED_ */
|
||||
@@ -0,0 +1,406 @@
|
||||
/*
|
||||
* Copyright (c) 2008-2012 Niels Provos, 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 EVTHREAD_INTERNAL_H_INCLUDED_
|
||||
#define EVTHREAD_INTERNAL_H_INCLUDED_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "event2/event-config.h"
|
||||
#include "evconfig-private.h"
|
||||
|
||||
#include "event2/thread.h"
|
||||
#include "util-internal.h"
|
||||
|
||||
struct event_base;
|
||||
|
||||
#if !defined(_WIN32) && !defined(__CYGWIN__)
|
||||
/* 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.
|
||||
*/
|
||||
#define EVTHREAD_EXPOSE_STRUCTS
|
||||
#endif
|
||||
|
||||
#if ! defined(EVENT__DISABLE_THREAD_SUPPORT) && defined(EVTHREAD_EXPOSE_STRUCTS)
|
||||
/* Global function pointers to lock-related functions. NULL if locking isn't
|
||||
enabled. */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
extern struct evthread_lock_callbacks evthread_lock_fns_;
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
extern struct evthread_condition_callbacks evthread_cond_fns_;
|
||||
extern unsigned long (*evthread_id_fn_)(void);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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)
|
||||
|
||||
/** 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_())
|
||||
|
||||
/** 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 && \
|
||||
(base)->running_loop && \
|
||||
(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)
|
||||
|
||||
/** 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)); \
|
||||
} while (0)
|
||||
|
||||
/** Acquire a lock. */
|
||||
#define EVLOCK_LOCK(lockvar,mode) \
|
||||
do { \
|
||||
if (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); \
|
||||
} while (0)
|
||||
|
||||
/** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
|
||||
#define EVLOCK_SORTLOCKS_(lockvar1, lockvar2) \
|
||||
do { \
|
||||
if (lockvar1 && lockvar2 && lockvar1 > lockvar2) { \
|
||||
void *tmp = lockvar1; \
|
||||
lockvar1 = lockvar2; \
|
||||
lockvar2 = tmp; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/** Lock an event_base, if it is set up for locking. Acquires the lock
|
||||
in the base structure whose field is named 'lockvar'. */
|
||||
#define EVBASE_ACQUIRE_LOCK(base, lockvar) do { \
|
||||
EVLOCK_LOCK((base)->lockvar, 0); \
|
||||
} while (0)
|
||||
|
||||
/** Unlock an event_base, if it is set up for locking. */
|
||||
#define EVBASE_RELEASE_LOCK(base, lockvar) do { \
|
||||
EVLOCK_UNLOCK((base)->lockvar, 0); \
|
||||
} while (0)
|
||||
|
||||
/** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
|
||||
* 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)); \
|
||||
} \
|
||||
} 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)
|
||||
{
|
||||
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;
|
||||
* of course we count as having the lock. */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/** 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; \
|
||||
} while (0)
|
||||
/** Deallocate and free a condition variable in condvar */
|
||||
#define EVTHREAD_FREE_COND(cond) \
|
||||
do { \
|
||||
if (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 )
|
||||
/** Signal all threads waiting on cond */
|
||||
#define EVTHREAD_COND_BROADCAST(cond) \
|
||||
( (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 )
|
||||
/** 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 )
|
||||
|
||||
/** 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);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evthreadimpl_is_lock_debugging_enabled_(void);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void *evthreadimpl_lock_alloc_(unsigned locktype);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void evthreadimpl_lock_free_(void *lock, unsigned locktype);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evthreadimpl_lock_lock_(unsigned mode, void *lock);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evthreadimpl_lock_unlock_(unsigned mode, void *lock);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void *evthreadimpl_cond_alloc_(unsigned condtype);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void evthreadimpl_cond_free_(void *cond);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evthreadimpl_cond_signal_(void *cond, int broadcast);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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_())
|
||||
#define EVBASE_NEED_NOTIFY(base) \
|
||||
((base)->running_loop && \
|
||||
((base)->th_owner_id != evthreadimpl_get_id_()))
|
||||
|
||||
#define EVTHREAD_ALLOC_LOCK(lockvar, 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)); \
|
||||
} while (0)
|
||||
|
||||
/** Acquire a lock. */
|
||||
#define EVLOCK_LOCK(lockvar,mode) \
|
||||
do { \
|
||||
if (lockvar) \
|
||||
evthreadimpl_lock_lock_(mode, lockvar); \
|
||||
} while (0)
|
||||
|
||||
/** Release a lock */
|
||||
#define EVLOCK_UNLOCK(lockvar,mode) \
|
||||
do { \
|
||||
if (lockvar) \
|
||||
evthreadimpl_lock_unlock_(mode, lockvar); \
|
||||
} while (0)
|
||||
|
||||
/** Lock an event_base, if it is set up for locking. Acquires the lock
|
||||
in the base structure whose field is named 'lockvar'. */
|
||||
#define EVBASE_ACQUIRE_LOCK(base, lockvar) do { \
|
||||
EVLOCK_LOCK((base)->lockvar, 0); \
|
||||
} while (0)
|
||||
|
||||
/** Unlock an event_base, if it is set up for locking. */
|
||||
#define EVBASE_RELEASE_LOCK(base, lockvar) do { \
|
||||
EVLOCK_UNLOCK((base)->lockvar, 0); \
|
||||
} while (0)
|
||||
|
||||
/** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
|
||||
* 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)); \
|
||||
} \
|
||||
} 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)
|
||||
{
|
||||
if (lock) {
|
||||
int r = evthreadimpl_lock_lock_(EVTHREAD_TRY, lock);
|
||||
return !r;
|
||||
} else {
|
||||
/* Locking is disabled either globally or for this thing;
|
||||
* of course we count as having the lock. */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/** Allocate a new condition variable and store it in the void *, condvar */
|
||||
#define EVTHREAD_ALLOC_COND(condvar) \
|
||||
do { \
|
||||
(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)); \
|
||||
} while (0)
|
||||
/** Signal one thread waiting on cond */
|
||||
#define EVTHREAD_COND_SIGNAL(cond) \
|
||||
( (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 )
|
||||
/** 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 )
|
||||
/** 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 )
|
||||
|
||||
#define EVTHREAD_LOCKING_ENABLED() \
|
||||
(evthreadimpl_locking_enabled_())
|
||||
|
||||
#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 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 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_LOCKING_ENABLED() 0
|
||||
|
||||
#endif
|
||||
|
||||
/* This code is shared between both lock impls */
|
||||
#if ! defined(EVENT__DISABLE_THREAD_SUPPORT)
|
||||
/** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
|
||||
#define EVLOCK_SORTLOCKS_(lockvar1, lockvar2) \
|
||||
do { \
|
||||
if (lockvar1 && lockvar2 && lockvar1 > lockvar2) { \
|
||||
void *tmp = lockvar1; \
|
||||
lockvar1 = lockvar2; \
|
||||
lockvar2 = tmp; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/** Acquire both lock1 and lock2. Always allocates locks in the same order,
|
||||
* 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); \
|
||||
} 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); \
|
||||
} while (0)
|
||||
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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);
|
||||
|
||||
#define EVTHREAD_SETUP_GLOBAL_LOCK(lockvar, locktype) \
|
||||
do { \
|
||||
lockvar = evthread_setup_global_lock_(lockvar, \
|
||||
(locktype), enable_locks); \
|
||||
if (!lockvar) { \
|
||||
event_warn("Couldn't allocate %s", #lockvar); \
|
||||
return -1; \
|
||||
} \
|
||||
} while (0);
|
||||
|
||||
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);
|
||||
|
||||
/** Return current evthread_lock_callbacks */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
struct evthread_lock_callbacks *evthread_get_lock_callbacks(void);
|
||||
/** Return current evthread_condition_callbacks */
|
||||
struct evthread_condition_callbacks *evthread_get_condition_callbacks(void);
|
||||
/** Disable locking for internal usage (like global shutdown) */
|
||||
void evthreadimpl_disable_lock_debugging_(void);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* EVTHREAD_INTERNAL_H_INCLUDED_ */
|
||||
@@ -0,0 +1,509 @@
|
||||
/*
|
||||
* Copyright (c) 2008-2012 Niels Provos, 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"
|
||||
|
||||
#ifndef EVENT__DISABLE_THREAD_SUPPORT
|
||||
|
||||
#include "event2/thread.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "log-internal.h"
|
||||
#include "mm-internal.h"
|
||||
#include "util-internal.h"
|
||||
#include "evthread-internal.h"
|
||||
|
||||
#ifdef EVTHREAD_EXPOSE_STRUCTS
|
||||
#define GLOBAL
|
||||
#else
|
||||
#define GLOBAL static
|
||||
#endif
|
||||
|
||||
#ifndef EVENT__DISABLE_DEBUG_MODE
|
||||
extern int event_debug_created_threadable_ctx_;
|
||||
extern int event_debug_mode_on_;
|
||||
#endif
|
||||
|
||||
/* globals */
|
||||
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_ = {
|
||||
0, NULL, NULL, NULL, NULL
|
||||
};
|
||||
|
||||
/* Used for debugging */
|
||||
static struct evthread_lock_callbacks original_lock_fns_ = {
|
||||
0, 0, NULL, NULL, NULL, NULL
|
||||
};
|
||||
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;
|
||||
}
|
||||
|
||||
struct evthread_lock_callbacks *evthread_get_lock_callbacks(void)
|
||||
{
|
||||
return evthread_lock_debugging_enabled_
|
||||
? &original_lock_fns_ : &evthread_lock_fns_;
|
||||
}
|
||||
struct evthread_condition_callbacks *evthread_get_condition_callbacks(void)
|
||||
{
|
||||
return evthread_lock_debugging_enabled_
|
||||
? &original_cond_fns_ : &evthread_cond_fns_;
|
||||
}
|
||||
void evthreadimpl_disable_lock_debugging_(void)
|
||||
{
|
||||
evthread_lock_debugging_enabled_ = 0;
|
||||
}
|
||||
|
||||
int
|
||||
evthread_set_lock_callbacks(const struct evthread_lock_callbacks *cbs)
|
||||
{
|
||||
struct evthread_lock_callbacks *target = evthread_get_lock_callbacks();
|
||||
|
||||
#ifndef EVENT__DISABLE_DEBUG_MODE
|
||||
if (event_debug_mode_on_) {
|
||||
if (event_debug_created_threadable_ctx_) {
|
||||
event_errx(1, "evthread initialization must be called BEFORE anything else!");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
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_));
|
||||
return 0;
|
||||
}
|
||||
if (target->alloc) {
|
||||
/* Uh oh; we already had locking callbacks set up.*/
|
||||
if (target->lock_api_version == cbs->lock_api_version &&
|
||||
target->supported_locktypes == cbs->supported_locktypes &&
|
||||
target->alloc == cbs->alloc &&
|
||||
target->free == cbs->free &&
|
||||
target->lock == cbs->lock &&
|
||||
target->unlock == cbs->unlock) {
|
||||
/* no change -- allow this. */
|
||||
return 0;
|
||||
}
|
||||
event_warnx("Can't change lock callbacks once they have been "
|
||||
"initialized.");
|
||||
return -1;
|
||||
}
|
||||
if (cbs->alloc && cbs->free && cbs->lock && cbs->unlock) {
|
||||
memcpy(target, cbs, sizeof(evthread_lock_fns_));
|
||||
return event_global_setup_locks_(1);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
evthread_set_condition_callbacks(const struct evthread_condition_callbacks *cbs)
|
||||
{
|
||||
struct evthread_condition_callbacks *target = evthread_get_condition_callbacks();
|
||||
|
||||
#ifndef EVENT__DISABLE_DEBUG_MODE
|
||||
if (event_debug_mode_on_) {
|
||||
if (event_debug_created_threadable_ctx_) {
|
||||
event_errx(1, "evthread initialization must be called BEFORE anything else!");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
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_));
|
||||
return 0;
|
||||
}
|
||||
if (target->alloc_condition) {
|
||||
/* Uh oh; we already had condition callbacks set up.*/
|
||||
if (target->condition_api_version == cbs->condition_api_version &&
|
||||
target->alloc_condition == cbs->alloc_condition &&
|
||||
target->free_condition == cbs->free_condition &&
|
||||
target->signal_condition == cbs->signal_condition &&
|
||||
target->wait_condition == cbs->wait_condition) {
|
||||
/* no change -- allow this. */
|
||||
return 0;
|
||||
}
|
||||
event_warnx("Can't change condition callbacks once they "
|
||||
"have been initialized.");
|
||||
return -1;
|
||||
}
|
||||
if (cbs->alloc_condition && cbs->free_condition &&
|
||||
cbs->signal_condition && cbs->wait_condition) {
|
||||
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;
|
||||
}
|
||||
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
|
||||
* lock to protect count. */
|
||||
int count;
|
||||
void *lock;
|
||||
};
|
||||
|
||||
static void *
|
||||
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(
|
||||
locktype|EVTHREAD_LOCKTYPE_RECURSIVE))) {
|
||||
mm_free(result);
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
result->lock = NULL;
|
||||
}
|
||||
result->signature = DEBUG_LOCK_SIG;
|
||||
result->locktype = locktype;
|
||||
result->count = 0;
|
||||
result->held_by = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
debug_lock_free(void *lock_, unsigned locktype)
|
||||
{
|
||||
struct debug_lock *lock = lock_;
|
||||
EVUTIL_ASSERT(lock->count == 0);
|
||||
EVUTIL_ASSERT(locktype == lock->locktype);
|
||||
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_) {
|
||||
unsigned long me;
|
||||
me = evthread_id_fn_();
|
||||
if (lock->count > 1)
|
||||
EVUTIL_ASSERT(lock->held_by == me);
|
||||
lock->held_by = me;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
debug_lock_lock(unsigned mode, void *lock_)
|
||||
{
|
||||
struct debug_lock *lock = lock_;
|
||||
int res = 0;
|
||||
if (lock->locktype & EVTHREAD_LOCKTYPE_READWRITE)
|
||||
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 (!res) {
|
||||
evthread_debug_lock_mark_locked(mode, lock);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
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_) {
|
||||
unsigned long me;
|
||||
me = evthread_id_fn_();
|
||||
EVUTIL_ASSERT(lock->held_by == me);
|
||||
if (lock->count == 1)
|
||||
lock->held_by = 0;
|
||||
}
|
||||
--lock->count;
|
||||
EVUTIL_ASSERT(lock->count >= 0);
|
||||
}
|
||||
|
||||
static int
|
||||
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);
|
||||
return res;
|
||||
}
|
||||
|
||||
static int
|
||||
debug_cond_wait(void *cond_, void *lock_, const struct timeval *tv)
|
||||
{
|
||||
int r;
|
||||
struct debug_lock *lock = lock_;
|
||||
EVUTIL_ASSERT(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);
|
||||
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,
|
||||
debug_lock_alloc,
|
||||
debug_lock_free,
|
||||
debug_lock_lock,
|
||||
debug_lock_unlock
|
||||
};
|
||||
if (evthread_lock_debugging_enabled_)
|
||||
return;
|
||||
memcpy(&original_lock_fns_, &evthread_lock_fns_,
|
||||
sizeof(struct evthread_lock_callbacks));
|
||||
memcpy(&evthread_lock_fns_, &cbs,
|
||||
sizeof(struct evthread_lock_callbacks));
|
||||
|
||||
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;
|
||||
|
||||
/* XXX return value should get checked. */
|
||||
event_global_setup_locks_(0);
|
||||
}
|
||||
|
||||
int
|
||||
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 (lock->held_by != me)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void *
|
||||
evthread_debug_get_real_lock_(void *lock_)
|
||||
{
|
||||
struct debug_lock *lock = lock_;
|
||||
return lock->lock;
|
||||
}
|
||||
|
||||
void *
|
||||
evthread_setup_global_lock_(void *lock_, unsigned locktype, int enable_locks)
|
||||
{
|
||||
/* there are four cases here:
|
||||
1) we're turning on debugging; locking is not on.
|
||||
2) we're turning on debugging; locking is on.
|
||||
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) {
|
||||
/* Case 1: allocate a debug lock. */
|
||||
EVUTIL_ASSERT(lock_ == NULL);
|
||||
return debug_lock_alloc(locktype);
|
||||
} 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);
|
||||
return debug_lock_alloc(locktype);
|
||||
}
|
||||
lock = mm_malloc(sizeof(struct debug_lock));
|
||||
if (!lock) {
|
||||
original_lock_fns_.free(lock_, locktype);
|
||||
return NULL;
|
||||
}
|
||||
lock->lock = lock_;
|
||||
lock->locktype = locktype;
|
||||
lock->count = 0;
|
||||
lock->held_by = 0;
|
||||
return lock;
|
||||
} else if (enable_locks && ! evthread_lock_debugging_enabled_) {
|
||||
/* Case 3: allocate a regular lock */
|
||||
EVUTIL_ASSERT(lock_ == NULL);
|
||||
return evthread_lock_fns_.alloc(locktype);
|
||||
} else {
|
||||
/* Case 4: Fill in a debug lock with a real lock */
|
||||
struct debug_lock *lock = lock_ ? lock_ : debug_lock_alloc(locktype);
|
||||
EVUTIL_ASSERT(enable_locks &&
|
||||
evthread_lock_debugging_enabled_);
|
||||
EVUTIL_ASSERT(lock->locktype == locktype);
|
||||
if (!lock->lock) {
|
||||
lock->lock = original_lock_fns_.alloc(
|
||||
locktype|EVTHREAD_LOCKTYPE_RECURSIVE);
|
||||
if (!lock->lock) {
|
||||
lock->count = -200;
|
||||
mm_free(lock);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return lock;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#ifndef EVTHREAD_EXPOSE_STRUCTS
|
||||
unsigned long
|
||||
evthreadimpl_get_id_()
|
||||
{
|
||||
return evthread_id_fn_ ? evthread_id_fn_() : 1;
|
||||
}
|
||||
void *
|
||||
evthreadimpl_lock_alloc_(unsigned locktype)
|
||||
{
|
||||
#ifndef EVENT__DISABLE_DEBUG_MODE
|
||||
if (event_debug_mode_on_) {
|
||||
event_debug_created_threadable_ctx_ = 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
return evthread_lock_fns_.alloc ?
|
||||
evthread_lock_fns_.alloc(locktype) : NULL;
|
||||
}
|
||||
void
|
||||
evthreadimpl_lock_free_(void *lock, unsigned locktype)
|
||||
{
|
||||
if (evthread_lock_fns_.free)
|
||||
evthread_lock_fns_.free(lock, locktype);
|
||||
}
|
||||
int
|
||||
evthreadimpl_lock_lock_(unsigned mode, void *lock)
|
||||
{
|
||||
if (evthread_lock_fns_.lock)
|
||||
return evthread_lock_fns_.lock(mode, lock);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
int
|
||||
evthreadimpl_lock_unlock_(unsigned mode, void *lock)
|
||||
{
|
||||
if (evthread_lock_fns_.unlock)
|
||||
return evthread_lock_fns_.unlock(mode, lock);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
void *
|
||||
evthreadimpl_cond_alloc_(unsigned condtype)
|
||||
{
|
||||
#ifndef EVENT__DISABLE_DEBUG_MODE
|
||||
if (event_debug_mode_on_) {
|
||||
event_debug_created_threadable_ctx_ = 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
return evthread_cond_fns_.alloc_condition ?
|
||||
evthread_cond_fns_.alloc_condition(condtype) : NULL;
|
||||
}
|
||||
void
|
||||
evthreadimpl_cond_free_(void *cond)
|
||||
{
|
||||
if (evthread_cond_fns_.free_condition)
|
||||
evthread_cond_fns_.free_condition(cond);
|
||||
}
|
||||
int
|
||||
evthreadimpl_cond_signal_(void *cond, int 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return evthread_lock_debugging_enabled_;
|
||||
}
|
||||
|
||||
int
|
||||
evthreadimpl_locking_enabled_(void)
|
||||
{
|
||||
return evthread_lock_fns_.lock != NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
* Copyright 2009-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"
|
||||
|
||||
/* 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;
|
||||
#include "event2/thread.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "mm-internal.h"
|
||||
#include "evthread-internal.h"
|
||||
|
||||
static pthread_mutexattr_t attr_recursive;
|
||||
|
||||
static void *
|
||||
evthread_posix_lock_alloc(unsigned locktype)
|
||||
{
|
||||
pthread_mutexattr_t *attr = NULL;
|
||||
pthread_mutex_t *lock = mm_malloc(sizeof(pthread_mutex_t));
|
||||
if (!lock)
|
||||
return NULL;
|
||||
if (locktype & EVTHREAD_LOCKTYPE_RECURSIVE)
|
||||
attr = &attr_recursive;
|
||||
if (pthread_mutex_init(lock, attr)) {
|
||||
mm_free(lock);
|
||||
return NULL;
|
||||
}
|
||||
return lock;
|
||||
}
|
||||
|
||||
static void
|
||||
evthread_posix_lock_free(void *lock_, unsigned locktype)
|
||||
{
|
||||
pthread_mutex_t *lock = lock_;
|
||||
pthread_mutex_destroy(lock);
|
||||
mm_free(lock);
|
||||
}
|
||||
|
||||
static int
|
||||
evthread_posix_lock(unsigned mode, void *lock_)
|
||||
{
|
||||
pthread_mutex_t *lock = lock_;
|
||||
if (mode & EVTHREAD_TRY)
|
||||
return pthread_mutex_trylock(lock);
|
||||
else
|
||||
return pthread_mutex_lock(lock);
|
||||
}
|
||||
|
||||
static int
|
||||
evthread_posix_unlock(unsigned mode, void *lock_)
|
||||
{
|
||||
pthread_mutex_t *lock = lock_;
|
||||
return pthread_mutex_unlock(lock);
|
||||
}
|
||||
|
||||
static unsigned long
|
||||
evthread_posix_get_id(void)
|
||||
{
|
||||
union {
|
||||
pthread_t thr;
|
||||
#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
|
||||
memset(&r, 0, sizeof(r));
|
||||
#endif
|
||||
r.thr = pthread_self();
|
||||
return (unsigned long)r.id;
|
||||
}
|
||||
|
||||
static void *
|
||||
evthread_posix_cond_alloc(unsigned condflags)
|
||||
{
|
||||
pthread_cond_t *cond = mm_malloc(sizeof(pthread_cond_t));
|
||||
if (!cond)
|
||||
return NULL;
|
||||
if (pthread_cond_init(cond, NULL)) {
|
||||
mm_free(cond);
|
||||
return NULL;
|
||||
}
|
||||
return cond;
|
||||
}
|
||||
|
||||
static void
|
||||
evthread_posix_cond_free(void *cond_)
|
||||
{
|
||||
pthread_cond_t *cond = cond_;
|
||||
pthread_cond_destroy(cond);
|
||||
mm_free(cond);
|
||||
}
|
||||
|
||||
static int
|
||||
evthread_posix_cond_signal(void *cond_, int broadcast)
|
||||
{
|
||||
pthread_cond_t *cond = cond_;
|
||||
int r;
|
||||
if (broadcast)
|
||||
r = pthread_cond_broadcast(cond);
|
||||
else
|
||||
r = pthread_cond_signal(cond);
|
||||
return r ? -1 : 0;
|
||||
}
|
||||
|
||||
static int
|
||||
evthread_posix_cond_wait(void *cond_, void *lock_, const struct timeval *tv)
|
||||
{
|
||||
int r;
|
||||
pthread_cond_t *cond = cond_;
|
||||
pthread_mutex_t *lock = lock_;
|
||||
|
||||
if (tv) {
|
||||
struct timeval now, abstime;
|
||||
struct timespec ts;
|
||||
evutil_gettimeofday(&now, NULL);
|
||||
evutil_timeradd(&now, tv, &abstime);
|
||||
ts.tv_sec = abstime.tv_sec;
|
||||
ts.tv_nsec = abstime.tv_usec*1000;
|
||||
r = pthread_cond_timedwait(cond, lock, &ts);
|
||||
if (r == ETIMEDOUT)
|
||||
return 1;
|
||||
else if (r)
|
||||
return -1;
|
||||
else
|
||||
return 0;
|
||||
} else {
|
||||
r = pthread_cond_wait(cond, lock);
|
||||
return r ? -1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
evthread_use_pthreads(void)
|
||||
{
|
||||
struct evthread_lock_callbacks cbs = {
|
||||
EVTHREAD_LOCK_API_VERSION,
|
||||
EVTHREAD_LOCKTYPE_RECURSIVE,
|
||||
evthread_posix_lock_alloc,
|
||||
evthread_posix_lock_free,
|
||||
evthread_posix_lock,
|
||||
evthread_posix_unlock
|
||||
};
|
||||
struct evthread_condition_callbacks cond_cbs = {
|
||||
EVTHREAD_CONDITION_API_VERSION,
|
||||
evthread_posix_cond_alloc,
|
||||
evthread_posix_cond_free,
|
||||
evthread_posix_cond_signal,
|
||||
evthread_posix_cond_wait
|
||||
};
|
||||
/* Set ourselves up to get recursive locks. */
|
||||
if (pthread_mutexattr_init(&attr_recursive))
|
||||
return -1;
|
||||
if (pthread_mutexattr_settype(&attr_recursive, PTHREAD_MUTEX_RECURSIVE))
|
||||
return -1;
|
||||
|
||||
evthread_set_lock_callbacks(&cbs);
|
||||
evthread_set_condition_callbacks(&cond_cbs);
|
||||
evthread_set_id_callback(evthread_posix_get_id);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,341 @@
|
||||
/*
|
||||
* Copyright 2009-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
|
||||
#ifndef _WIN32_WINNT
|
||||
/* Minimum required for InitializeCriticalSectionAndSpinCount */
|
||||
#define _WIN32_WINNT 0x0403
|
||||
#endif
|
||||
#include <winsock2.h>
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#undef WIN32_LEAN_AND_MEAN
|
||||
#include <sys/locking.h>
|
||||
#endif
|
||||
|
||||
struct event_base;
|
||||
#include "event2/thread.h"
|
||||
|
||||
#include "mm-internal.h"
|
||||
#include "evthread-internal.h"
|
||||
#include "time-internal.h"
|
||||
|
||||
#define SPIN_COUNT 2000
|
||||
|
||||
static void *
|
||||
evthread_win32_lock_create(unsigned locktype)
|
||||
{
|
||||
CRITICAL_SECTION *lock = mm_malloc(sizeof(CRITICAL_SECTION));
|
||||
if (!lock)
|
||||
return NULL;
|
||||
if (InitializeCriticalSectionAndSpinCount(lock, SPIN_COUNT) == 0) {
|
||||
mm_free(lock);
|
||||
return NULL;
|
||||
}
|
||||
return lock;
|
||||
}
|
||||
|
||||
static void
|
||||
evthread_win32_lock_free(void *lock_, unsigned locktype)
|
||||
{
|
||||
CRITICAL_SECTION *lock = lock_;
|
||||
DeleteCriticalSection(lock);
|
||||
mm_free(lock);
|
||||
}
|
||||
|
||||
static int
|
||||
evthread_win32_lock(unsigned mode, void *lock_)
|
||||
{
|
||||
CRITICAL_SECTION *lock = lock_;
|
||||
if ((mode & EVTHREAD_TRY)) {
|
||||
return ! TryEnterCriticalSection(lock);
|
||||
} else {
|
||||
EnterCriticalSection(lock);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
evthread_win32_unlock(unsigned mode, void *lock_)
|
||||
{
|
||||
CRITICAL_SECTION *lock = lock_;
|
||||
LeaveCriticalSection(lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned long
|
||||
evthread_win32_get_id(void)
|
||||
{
|
||||
return (unsigned long) GetCurrentThreadId();
|
||||
}
|
||||
|
||||
#ifdef WIN32_HAVE_CONDITION_VARIABLES
|
||||
static void WINAPI (*InitializeConditionVariable_fn)(PCONDITION_VARIABLE)
|
||||
= NULL;
|
||||
static BOOL WINAPI (*SleepConditionVariableCS_fn)(
|
||||
PCONDITION_VARIABLE, PCRITICAL_SECTION, DWORD) = NULL;
|
||||
static void WINAPI (*WakeAllConditionVariable_fn)(PCONDITION_VARIABLE) = NULL;
|
||||
static void WINAPI (*WakeConditionVariable_fn)(PCONDITION_VARIABLE) = NULL;
|
||||
|
||||
static int
|
||||
evthread_win32_condvar_init(void)
|
||||
{
|
||||
HANDLE lib;
|
||||
|
||||
lib = GetModuleHandle(TEXT("kernel32.dll"));
|
||||
if (lib == NULL)
|
||||
return 0;
|
||||
|
||||
#define LOAD(name) \
|
||||
name##_fn = GetProcAddress(lib, #name)
|
||||
LOAD(InitializeConditionVariable);
|
||||
LOAD(SleepConditionVariableCS);
|
||||
LOAD(WakeAllConditionVariable);
|
||||
LOAD(WakeConditionVariable);
|
||||
|
||||
return InitializeConditionVariable_fn && SleepConditionVariableCS_fn &&
|
||||
WakeAllConditionVariable_fn && WakeConditionVariable_fn;
|
||||
}
|
||||
|
||||
/* XXXX Even if we can build this, we don't necessarily want to: the functions
|
||||
* in question didn't exist before Vista, so we'd better LoadProc them. */
|
||||
static void *
|
||||
evthread_win32_condvar_alloc(unsigned condflags)
|
||||
{
|
||||
CONDITION_VARIABLE *cond = mm_malloc(sizeof(CONDITION_VARIABLE));
|
||||
if (!cond)
|
||||
return NULL;
|
||||
InitializeConditionVariable_fn(cond);
|
||||
return cond;
|
||||
}
|
||||
|
||||
static void
|
||||
evthread_win32_condvar_free(void *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)
|
||||
{
|
||||
CONDITION_VARIABLE *cond = cond_;
|
||||
if (broadcast)
|
||||
WakeAllConditionVariable_fn(cond);
|
||||
else
|
||||
WakeConditionVariable_fn(cond);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
evthread_win32_condvar_wait(void *cond_, void *lock_, const struct timeval *tv)
|
||||
{
|
||||
CONDITION_VARIABLE *cond = cond_;
|
||||
CRITICAL_SECTION *lock = lock_;
|
||||
DWORD ms, err;
|
||||
BOOL result;
|
||||
|
||||
if (tv)
|
||||
ms = evutil_tv_to_msec_(tv);
|
||||
else
|
||||
ms = INFINITE;
|
||||
result = SleepConditionVariableCS_fn(cond, lock, ms);
|
||||
if (result) {
|
||||
if (GetLastError() == WAIT_TIMEOUT)
|
||||
return 1;
|
||||
else
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
struct evthread_win32_cond {
|
||||
HANDLE event;
|
||||
|
||||
CRITICAL_SECTION lock;
|
||||
int n_waiting;
|
||||
int n_to_wake;
|
||||
int generation;
|
||||
};
|
||||
|
||||
static void *
|
||||
evthread_win32_cond_alloc(unsigned flags)
|
||||
{
|
||||
struct evthread_win32_cond *cond;
|
||||
if (!(cond = mm_malloc(sizeof(struct evthread_win32_cond))))
|
||||
return NULL;
|
||||
if (InitializeCriticalSectionAndSpinCount(&cond->lock, SPIN_COUNT)==0) {
|
||||
mm_free(cond);
|
||||
return NULL;
|
||||
}
|
||||
if ((cond->event = CreateEvent(NULL,TRUE,FALSE,NULL)) == NULL) {
|
||||
DeleteCriticalSection(&cond->lock);
|
||||
mm_free(cond);
|
||||
return NULL;
|
||||
}
|
||||
cond->n_waiting = cond->n_to_wake = cond->generation = 0;
|
||||
return cond;
|
||||
}
|
||||
|
||||
static void
|
||||
evthread_win32_cond_free(void *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)
|
||||
{
|
||||
struct evthread_win32_cond *cond = cond_;
|
||||
EnterCriticalSection(&cond->lock);
|
||||
if (broadcast)
|
||||
cond->n_to_wake = cond->n_waiting;
|
||||
else
|
||||
++cond->n_to_wake;
|
||||
cond->generation++;
|
||||
SetEvent(cond->event);
|
||||
LeaveCriticalSection(&cond->lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
evthread_win32_cond_wait(void *cond_, void *lock_, const struct timeval *tv)
|
||||
{
|
||||
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);
|
||||
|
||||
EnterCriticalSection(&cond->lock);
|
||||
++cond->n_waiting;
|
||||
generation_at_start = cond->generation;
|
||||
LeaveCriticalSection(&cond->lock);
|
||||
|
||||
LeaveCriticalSection(lock);
|
||||
|
||||
startTime = GetTickCount();
|
||||
do {
|
||||
DWORD res;
|
||||
res = WaitForSingleObject(cond->event, ms);
|
||||
EnterCriticalSection(&cond->lock);
|
||||
if (cond->n_to_wake &&
|
||||
cond->generation != generation_at_start) {
|
||||
--cond->n_to_wake;
|
||||
--cond->n_waiting;
|
||||
result = 0;
|
||||
waiting = 0;
|
||||
goto out;
|
||||
} else if (res != WAIT_OBJECT_0) {
|
||||
result = (res==WAIT_TIMEOUT) ? 1 : -1;
|
||||
--cond->n_waiting;
|
||||
waiting = 0;
|
||||
goto out;
|
||||
} else if (ms != INFINITE) {
|
||||
endTime = GetTickCount();
|
||||
if (startTime + ms_orig <= endTime) {
|
||||
result = 1; /* Timeout */
|
||||
--cond->n_waiting;
|
||||
waiting = 0;
|
||||
goto out;
|
||||
} else {
|
||||
ms = startTime + ms_orig - endTime;
|
||||
}
|
||||
}
|
||||
/* If we make it here, we are still waiting. */
|
||||
if (cond->n_to_wake == 0) {
|
||||
/* There is nobody else who should wake up; reset
|
||||
* the event. */
|
||||
ResetEvent(cond->event);
|
||||
}
|
||||
out:
|
||||
LeaveCriticalSection(&cond->lock);
|
||||
} while (waiting);
|
||||
|
||||
EnterCriticalSection(lock);
|
||||
|
||||
EnterCriticalSection(&cond->lock);
|
||||
if (!cond->n_waiting)
|
||||
ResetEvent(cond->event);
|
||||
LeaveCriticalSection(&cond->lock);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int
|
||||
evthread_use_windows_threads(void)
|
||||
{
|
||||
struct evthread_lock_callbacks cbs = {
|
||||
EVTHREAD_LOCK_API_VERSION,
|
||||
EVTHREAD_LOCKTYPE_RECURSIVE,
|
||||
evthread_win32_lock_create,
|
||||
evthread_win32_lock_free,
|
||||
evthread_win32_lock,
|
||||
evthread_win32_unlock
|
||||
};
|
||||
|
||||
|
||||
struct evthread_condition_callbacks cond_cbs = {
|
||||
EVTHREAD_CONDITION_API_VERSION,
|
||||
evthread_win32_cond_alloc,
|
||||
evthread_win32_cond_free,
|
||||
evthread_win32_cond_signal,
|
||||
evthread_win32_cond_wait
|
||||
};
|
||||
#ifdef WIN32_HAVE_CONDITION_VARIABLES
|
||||
struct evthread_condition_callbacks condvar_cbs = {
|
||||
EVTHREAD_CONDITION_API_VERSION,
|
||||
evthread_win32_condvar_alloc,
|
||||
evthread_win32_condvar_free,
|
||||
evthread_win32_condvar_signal,
|
||||
evthread_win32_condvar_wait
|
||||
};
|
||||
#endif
|
||||
|
||||
evthread_set_lock_callbacks(&cbs);
|
||||
evthread_set_id_callback(evthread_win32_get_id);
|
||||
#ifdef WIN32_HAVE_CONDITION_VARIABLES
|
||||
if (evthread_win32_condvar_init()) {
|
||||
evthread_set_condition_callbacks(&condvar_cbs);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
evthread_set_condition_callbacks(&cond_cbs);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,206 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/* This file has our secure PRNG code. On platforms that have arc4random(),
|
||||
* we just use that. Otherwise, we include arc4random.c as a bunch of static
|
||||
* functions, and wrap it lightly. We don't expose the arc4random*() APIs
|
||||
* because A) they aren't in our namespace, and B) it's not nice to name your
|
||||
* APIs after their implementations. We keep them in a separate file
|
||||
* so that other people can rip it out and use it for whatever.
|
||||
*/
|
||||
|
||||
#include "event2/event-config.h"
|
||||
#include "evconfig-private.h"
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "util-internal.h"
|
||||
#include "evthread-internal.h"
|
||||
|
||||
#ifdef EVENT__HAVE_ARC4RANDOM
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
int
|
||||
evutil_secure_rng_set_urandom_device_file(char *fname)
|
||||
{
|
||||
(void) fname;
|
||||
return -1;
|
||||
}
|
||||
int
|
||||
evutil_secure_rng_init(void)
|
||||
{
|
||||
/* call arc4random() now to force it to self-initialize */
|
||||
(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__)
|
||||
arc4random_buf(buf, n);
|
||||
return;
|
||||
#else
|
||||
unsigned char *b = 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,
|
||||
* and fall back otherwise. (OSX does this using some linker
|
||||
* trickery.)
|
||||
*/
|
||||
{
|
||||
void (*tptr)(void *,size_t) =
|
||||
(void (*)(void*,size_t))arc4random_buf;
|
||||
if (tptr != NULL) {
|
||||
arc4random_buf(buf, n);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/* Make sure that we start out with b at a 4-byte alignment; plenty
|
||||
* of CPUs care about this for 32-bit access. */
|
||||
if (n >= 4 && ((ev_uintptr_t)b) & 3) {
|
||||
ev_uint32_t u = arc4random();
|
||||
int n_bytes = 4 - (((ev_uintptr_t)b) & 3);
|
||||
memcpy(b, &u, n_bytes);
|
||||
b += n_bytes;
|
||||
n -= n_bytes;
|
||||
}
|
||||
while (n >= 4) {
|
||||
*(ev_uint32_t*)b = arc4random();
|
||||
b += 4;
|
||||
n -= 4;
|
||||
}
|
||||
if (n) {
|
||||
ev_uint32_t u = arc4random();
|
||||
memcpy(b, &u, n);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#else /* !EVENT__HAVE_ARC4RANDOM { */
|
||||
|
||||
#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
|
||||
static void *arc4rand_lock;
|
||||
#endif
|
||||
|
||||
#define ARC4RANDOM_UINT32 ev_uint32_t
|
||||
#define ARC4RANDOM_NOSTIR
|
||||
#define ARC4RANDOM_NORANDOM
|
||||
#define ARC4RANDOM_NOUNIFORM
|
||||
|
||||
#include "./arc4random.c"
|
||||
|
||||
#ifndef EVENT__DISABLE_THREAD_SUPPORT
|
||||
int
|
||||
evutil_secure_rng_global_setup_locks_(const int enable_locks)
|
||||
{
|
||||
EVTHREAD_SETUP_GLOBAL_LOCK(arc4rand_lock, 0);
|
||||
return 0;
|
||||
}
|
||||
#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_();
|
||||
arc4random_urandom_filename = fname;
|
||||
ARC4_UNLOCK_();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
evutil_secure_rng_init(void)
|
||||
{
|
||||
int val;
|
||||
|
||||
ARC4_LOCK_();
|
||||
val = (!arc4_stir()) ? 0 : -1;
|
||||
ARC4_UNLOCK_();
|
||||
return val;
|
||||
}
|
||||
|
||||
static void
|
||||
ev_arc4random_buf(void *buf, size_t n)
|
||||
{
|
||||
arc4random_buf(buf, n);
|
||||
}
|
||||
|
||||
#endif /* } !EVENT__HAVE_ARC4RANDOM */
|
||||
|
||||
void
|
||||
evutil_secure_rng_get_bytes(void *buf, size_t n)
|
||||
{
|
||||
ev_arc4random_buf(buf, n);
|
||||
}
|
||||
|
||||
#if !defined(EVENT__HAVE_ARC4RANDOM) || defined(EVENT__HAVE_ARC4RANDOM_ADDRANDOM)
|
||||
void
|
||||
evutil_secure_rng_add_bytes(const char *buf, size_t n)
|
||||
{
|
||||
arc4random_addrandom((unsigned char*)buf,
|
||||
n>(size_t)INT_MAX ? INT_MAX : (int)n);
|
||||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
evutil_free_secure_rng_globals_(void)
|
||||
{
|
||||
evutil_free_secure_rng_globals_locks();
|
||||
}
|
||||
@@ -0,0 +1,621 @@
|
||||
/*
|
||||
* 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>
|
||||
|
||||
/** evutil_usleep_() */
|
||||
#if defined(_WIN32)
|
||||
#elif defined(EVENT__HAVE_NANOSLEEP)
|
||||
#elif defined(EVENT__HAVE_USLEEP)
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "event2/util.h"
|
||||
#include "util-internal.h"
|
||||
#include "log-internal.h"
|
||||
#include "mm-internal.h"
|
||||
|
||||
#ifndef EVENT__HAVE_GETTIMEOFDAY
|
||||
/* No gettimeofday; this must be windows. */
|
||||
|
||||
typedef void (WINAPI *GetSystemTimePreciseAsFileTime_fn_t) (LPFILETIME);
|
||||
|
||||
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;
|
||||
|
||||
static GetSystemTimePreciseAsFileTime_fn_t GetSystemTimePreciseAsFileTime_fn = NULL;
|
||||
static int check_precise = 1;
|
||||
|
||||
if (EVUTIL_UNLIKELY(check_precise)) {
|
||||
HMODULE h = evutil_load_windows_system_library_(TEXT("kernel32.dll"));
|
||||
if (h != NULL)
|
||||
GetSystemTimePreciseAsFileTime_fn =
|
||||
(GetSystemTimePreciseAsFileTime_fn_t)
|
||||
GetProcAddress(h, "GetSystemTimePreciseAsFileTime");
|
||||
check_precise = 0;
|
||||
}
|
||||
|
||||
if (GetSystemTimePreciseAsFileTime_fn != NULL)
|
||||
GetSystemTimePreciseAsFileTime_fn(&ft.ft_ft);
|
||||
else
|
||||
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)
|
||||
{
|
||||
__int64 usec;
|
||||
LARGE_INTEGER li;
|
||||
HANDLE timer;
|
||||
|
||||
usec = tv->tv_sec * 1000000LL + tv->tv_usec;
|
||||
if (!usec)
|
||||
return;
|
||||
|
||||
li.QuadPart = -10LL * usec;
|
||||
timer = CreateWaitableTimer(NULL, TRUE, NULL);
|
||||
if (!timer)
|
||||
return;
|
||||
|
||||
SetWaitableTimer(timer, &li, 0, NULL, NULL, 0);
|
||||
WaitForSingleObject(timer, INFINITE);
|
||||
CloseHandle(timer);
|
||||
}
|
||||
#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
|
||||
{
|
||||
struct timeval tv2 = *tv;
|
||||
select(0, NULL, NULL, NULL, &tv2);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
evutil_date_rfc1123(char *date, const size_t datelen, const struct tm *tm)
|
||||
{
|
||||
static const char *DAYS[] =
|
||||
{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
|
||||
static const char *MONTHS[] =
|
||||
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
|
||||
|
||||
time_t t = time(NULL);
|
||||
|
||||
#if defined(EVENT__HAVE__GMTIME64_S) || !defined(_WIN32)
|
||||
struct tm sys;
|
||||
#endif
|
||||
|
||||
/* If `tm` is null, set system's current time. */
|
||||
if (tm == NULL) {
|
||||
#if !defined(_WIN32)
|
||||
gmtime_r(&t, &sys);
|
||||
tm = &sys;
|
||||
/** detect _gmtime64()/_gmtime64_s() */
|
||||
#elif defined(EVENT__HAVE__GMTIME64_S)
|
||||
errno_t err;
|
||||
err = _gmtime64_s(&sys, &t);
|
||||
if (err) {
|
||||
event_errx(1, "Invalid argument to _gmtime64_s");
|
||||
} else {
|
||||
tm = &sys;
|
||||
}
|
||||
#elif defined(EVENT__HAVE__GMTIME64)
|
||||
tm = _gmtime64(&t);
|
||||
#else
|
||||
tm = gmtime(&t);
|
||||
#endif
|
||||
}
|
||||
|
||||
return evutil_snprintf(
|
||||
date, datelen, "%s, %02d %s %4d %02d:%02d:%02d GMT",
|
||||
DAYS[tm->tm_wday], tm->tm_mday, MONTHS[tm->tm_mon],
|
||||
1900 + tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec);
|
||||
}
|
||||
|
||||
/*
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
Allocate a new struct evutil_monotonic_timer
|
||||
*/
|
||||
struct evutil_monotonic_timer *
|
||||
evutil_monotonic_timer_new(void)
|
||||
{
|
||||
struct evutil_monotonic_timer *p = NULL;
|
||||
|
||||
p = mm_malloc(sizeof(*p));
|
||||
if (!p) goto done;
|
||||
|
||||
memset(p, 0, sizeof(*p));
|
||||
|
||||
done:
|
||||
return p;
|
||||
}
|
||||
|
||||
/*
|
||||
Free a struct evutil_monotonic_timer
|
||||
*/
|
||||
void
|
||||
evutil_monotonic_timer_free(struct evutil_monotonic_timer *timer)
|
||||
{
|
||||
if (timer) {
|
||||
mm_free(timer);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Set up a struct evutil_monotonic_timer for initial use
|
||||
*/
|
||||
int
|
||||
evutil_configure_monotonic_time(struct evutil_monotonic_timer *timer,
|
||||
int flags)
|
||||
{
|
||||
return evutil_configure_monotonic_time_(timer, flags);
|
||||
}
|
||||
|
||||
/*
|
||||
Query the current monotonic time
|
||||
*/
|
||||
int
|
||||
evutil_gettime_monotonic(struct evutil_monotonic_timer *timer,
|
||||
struct timeval *tp)
|
||||
{
|
||||
return evutil_gettime_monotonic_(timer, tp);
|
||||
}
|
||||
|
||||
|
||||
#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
|
||||
@@ -0,0 +1,487 @@
|
||||
/* 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 HT_INTERNAL_H_INCLUDED_
|
||||
#define HT_INTERNAL_H_INCLUDED_
|
||||
|
||||
#define HT_HEAD(name, type) \
|
||||
struct name { \
|
||||
/* The hash table itself. */ \
|
||||
struct type **hth_table; \
|
||||
/* How long is the hash table? */ \
|
||||
unsigned hth_table_length; \
|
||||
/* How many elements does the table contain? */ \
|
||||
unsigned hth_n_entries; \
|
||||
/* How many elements will we allow in the table before resizing it? */ \
|
||||
unsigned hth_load_limit; \
|
||||
/* Position of hth_table_length in the primes table. */ \
|
||||
int hth_prime_idx; \
|
||||
}
|
||||
|
||||
#define HT_INITIALIZER() \
|
||||
{ NULL, 0, 0, 0, -1 }
|
||||
|
||||
#ifdef HT_NO_CACHE_HASH_VALUES
|
||||
#define HT_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *hte_next; \
|
||||
}
|
||||
#else
|
||||
#define HT_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *hte_next; \
|
||||
unsigned hte_hash; \
|
||||
}
|
||||
#endif
|
||||
|
||||
#define HT_EMPTY(head) \
|
||||
((head)->hth_n_entries == 0)
|
||||
|
||||
/* How many elements in 'head'? */
|
||||
#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))
|
||||
#define HT_REMOVE(name, head, elm) name##_HT_REMOVE((head), (elm))
|
||||
#define HT_START(name, head) name##_HT_START(head)
|
||||
#define HT_NEXT(name, head, elm) name##_HT_NEXT((head), (elm))
|
||||
#define HT_NEXT_RMV(name, head, elm) name##_HT_NEXT_RMV((head), (elm))
|
||||
#define HT_CLEAR(name, head) name##_HT_CLEAR(head)
|
||||
#define HT_INIT(name, head) name##_HT_INIT(head)
|
||||
/* Helper: */
|
||||
static inline unsigned
|
||||
ht_improve_hash_(unsigned h)
|
||||
{
|
||||
/* Aim to protect against poor hash functions by adding logic here
|
||||
* - logic taken from java 1.4 hashtable source */
|
||||
h += ~(h << 9);
|
||||
h ^= ((h >> 14) | (h << 18)); /* >>> */
|
||||
h += (h << 4);
|
||||
h ^= ((h >> 10) | (h << 22)); /* >>> */
|
||||
return h;
|
||||
}
|
||||
|
||||
#if 0
|
||||
/** Basic string hash function, from Java standard String.hashCode(). */
|
||||
static inline unsigned
|
||||
ht_string_hash_(const char *s)
|
||||
{
|
||||
unsigned h = 0;
|
||||
int m = 1;
|
||||
while (*s) {
|
||||
h += ((signed char)*s++)*m;
|
||||
m = (m<<5)-1; /* m *= 31 */
|
||||
}
|
||||
return h;
|
||||
}
|
||||
#endif
|
||||
|
||||
/** Basic string hash function, from Python's str.__hash__() */
|
||||
static inline unsigned
|
||||
ht_string_hash_(const char *s)
|
||||
{
|
||||
unsigned h;
|
||||
const unsigned char *cp = (const unsigned char *)s;
|
||||
h = *cp << 7;
|
||||
while (*cp) {
|
||||
h = (1000003*h) ^ *cp++;
|
||||
}
|
||||
/* This conversion truncates the length of the string, but that's ok. */
|
||||
h ^= (unsigned)(cp-(const unsigned char*)s);
|
||||
return h;
|
||||
}
|
||||
|
||||
#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) \
|
||||
do { (elm)->field.hte_hash = (val); } while (0)
|
||||
#define HT_ELT_HASH_(elm, field, hashfn) \
|
||||
((elm)->field.hte_hash)
|
||||
#else
|
||||
#define HT_SET_HASH_(elm, field, hashfn) \
|
||||
((void)0)
|
||||
#define HT_ELT_HASH_(elm, field, hashfn) \
|
||||
(hashfn(elm))
|
||||
#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_FOREACH(x, name, head) \
|
||||
for ((x) = HT_START(name, head); \
|
||||
(x) != NULL; \
|
||||
(x) = HT_NEXT(name, head, x))
|
||||
|
||||
#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); \
|
||||
static inline void \
|
||||
name##_HT_INIT(struct name *head) { \
|
||||
head->hth_table_length = 0; \
|
||||
head->hth_table = NULL; \
|
||||
head->hth_n_entries = 0; \
|
||||
head->hth_load_limit = 0; \
|
||||
head->hth_prime_idx = -1; \
|
||||
} \
|
||||
/* 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) \
|
||||
{ \
|
||||
struct type **p; \
|
||||
if (!head->hth_table) \
|
||||
return NULL; \
|
||||
p = &HT_BUCKET_(head, field, elm, hashfn); \
|
||||
while (*p) { \
|
||||
if (eqfn(*p, elm)) \
|
||||
return p; \
|
||||
p = &(*p)->field.hte_next; \
|
||||
} \
|
||||
return p; \
|
||||
} \
|
||||
/* Return a pointer to the element in the table 'head' matching 'elm', \
|
||||
* or NULL if no such element exists */ \
|
||||
static inline struct type * \
|
||||
name##_HT_FIND(const struct name *head, struct type *elm) \
|
||||
{ \
|
||||
struct type **p; \
|
||||
struct name *h = (struct name *) head; \
|
||||
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 \
|
||||
* function if the table might already contain a matching element. */ \
|
||||
static inline void \
|
||||
name##_HT_INSERT(struct name *head, struct type *elm) \
|
||||
{ \
|
||||
struct type **p; \
|
||||
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); \
|
||||
elm->field.hte_next = *p; \
|
||||
*p = elm; \
|
||||
} \
|
||||
/* Insert the element 'elm' into the table 'head'. If there already \
|
||||
* a matching element in the table, replace that element and return \
|
||||
* it. */ \
|
||||
static inline struct type * \
|
||||
name##_HT_REPLACE(struct name *head, struct type *elm) \
|
||||
{ \
|
||||
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); \
|
||||
r = *p; \
|
||||
*p = elm; \
|
||||
if (r && (r!=elm)) { \
|
||||
elm->field.hte_next = r->field.hte_next; \
|
||||
r->field.hte_next = NULL; \
|
||||
return r; \
|
||||
} else { \
|
||||
++head->hth_n_entries; \
|
||||
return NULL; \
|
||||
} \
|
||||
} \
|
||||
/* Remove any element matching 'elm' from the table 'head'. If such \
|
||||
* an element is found, return it; otherwise return NULL. */ \
|
||||
static inline struct type * \
|
||||
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); \
|
||||
if (!p || !*p) \
|
||||
return NULL; \
|
||||
r = *p; \
|
||||
*p = r->field.hte_next; \
|
||||
r->field.hte_next = NULL; \
|
||||
--head->hth_n_entries; \
|
||||
return r; \
|
||||
} \
|
||||
/* Invoke the function 'fn' on every element of the table 'head', \
|
||||
* using 'data' as its second argument. If the function returns \
|
||||
* nonzero, remove the most recently examined element before invoking \
|
||||
* the function again. */ \
|
||||
static inline void \
|
||||
name##_HT_FOREACH_FN(struct name *head, \
|
||||
int (*fn)(struct type *, void *), \
|
||||
void *data) \
|
||||
{ \
|
||||
unsigned idx; \
|
||||
struct type **p, **nextp, *next; \
|
||||
if (!head->hth_table) \
|
||||
return; \
|
||||
for (idx=0; idx < head->hth_table_length; ++idx) { \
|
||||
p = &head->hth_table[idx]; \
|
||||
while (*p) { \
|
||||
nextp = &(*p)->field.hte_next; \
|
||||
next = *nextp; \
|
||||
if (fn(*p, data)) { \
|
||||
--head->hth_n_entries; \
|
||||
*p = next; \
|
||||
} else { \
|
||||
p = nextp; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
/* Return a pointer to the first element in the table 'head', under \
|
||||
* an arbitrary order. This order is stable under remove operations, \
|
||||
* but not under others. If the table is empty, return NULL. */ \
|
||||
static inline struct type ** \
|
||||
name##_HT_START(struct name *head) \
|
||||
{ \
|
||||
unsigned b = 0; \
|
||||
while (b < head->hth_table_length) { \
|
||||
if (head->hth_table[b]) \
|
||||
return &head->hth_table[b]; \
|
||||
++b; \
|
||||
} \
|
||||
return NULL; \
|
||||
} \
|
||||
/* Return the next element in 'head' after 'elm', under the arbitrary \
|
||||
* order used by HT_START. If there are no more elements, return \
|
||||
* NULL. If 'elm' is to be removed from the table, you must call \
|
||||
* this function for the next value before you remove it. \
|
||||
*/ \
|
||||
static inline struct type ** \
|
||||
name##_HT_NEXT(struct name *head, struct type **elm) \
|
||||
{ \
|
||||
if ((*elm)->field.hte_next) { \
|
||||
return &(*elm)->field.hte_next; \
|
||||
} else { \
|
||||
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]; \
|
||||
++b; \
|
||||
} \
|
||||
return NULL; \
|
||||
} \
|
||||
} \
|
||||
static inline struct type ** \
|
||||
name##_HT_NEXT_RMV(struct name *head, struct type **elm) \
|
||||
{ \
|
||||
unsigned h = HT_ELT_HASH_(*elm, field, hashfn); \
|
||||
*elm = (*elm)->field.hte_next; \
|
||||
--head->hth_n_entries; \
|
||||
if (*elm) { \
|
||||
return elm; \
|
||||
} else { \
|
||||
unsigned b = (h % head->hth_table_length)+1; \
|
||||
while (b < head->hth_table_length) { \
|
||||
if (head->hth_table[b]) \
|
||||
return &head->hth_table[b]; \
|
||||
++b; \
|
||||
} \
|
||||
return NULL; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define HT_GENERATE(name, type, field, hashfn, eqfn, load, mallocfn, \
|
||||
reallocfn, freefn) \
|
||||
static unsigned name##_PRIMES[] = { \
|
||||
53, 97, 193, 389, \
|
||||
769, 1543, 3079, 6151, \
|
||||
12289, 24593, 49157, 98317, \
|
||||
196613, 393241, 786433, 1572869, \
|
||||
3145739, 6291469, 12582917, 25165843, \
|
||||
50331653, 100663319, 201326611, 402653189, \
|
||||
805306457, 1610612741 \
|
||||
}; \
|
||||
static unsigned name##_N_PRIMES = \
|
||||
(unsigned)(sizeof(name##_PRIMES)/sizeof(name##_PRIMES[0])); \
|
||||
/* Expand the internal table of 'head' until it is large enough to \
|
||||
* hold 'size' elements. Return 0 on success, -1 on allocation \
|
||||
* failure. */ \
|
||||
int \
|
||||
name##_HT_GROW(struct name *head, unsigned size) \
|
||||
{ \
|
||||
unsigned new_len, new_load_limit; \
|
||||
int prime_idx; \
|
||||
struct type **new_table; \
|
||||
if (head->hth_prime_idx == (int)name##_N_PRIMES - 1) \
|
||||
return 0; \
|
||||
if (head->hth_load_limit > size) \
|
||||
return 0; \
|
||||
prime_idx = head->hth_prime_idx; \
|
||||
do { \
|
||||
new_len = name##_PRIMES[++prime_idx]; \
|
||||
new_load_limit = (unsigned)(load*new_len); \
|
||||
} while (new_load_limit <= size && \
|
||||
prime_idx < (int)name##_N_PRIMES); \
|
||||
if ((new_table = mallocfn(new_len*sizeof(struct type*)))) { \
|
||||
unsigned b; \
|
||||
memset(new_table, 0, new_len*sizeof(struct type*)); \
|
||||
for (b = 0; b < head->hth_table_length; ++b) { \
|
||||
struct type *elm, *next; \
|
||||
unsigned b2; \
|
||||
elm = head->hth_table[b]; \
|
||||
while (elm) { \
|
||||
next = elm->field.hte_next; \
|
||||
b2 = HT_ELT_HASH_(elm, field, hashfn) % new_len; \
|
||||
elm->field.hte_next = new_table[b2]; \
|
||||
new_table[b2] = elm; \
|
||||
elm = next; \
|
||||
} \
|
||||
} \
|
||||
if (head->hth_table) \
|
||||
freefn(head->hth_table); \
|
||||
head->hth_table = new_table; \
|
||||
} else { \
|
||||
unsigned b, b2; \
|
||||
new_table = reallocfn(head->hth_table, new_len*sizeof(struct type*)); \
|
||||
if (!new_table) return -1; \
|
||||
memset(new_table + head->hth_table_length, 0, \
|
||||
(new_len - head->hth_table_length)*sizeof(struct type*)); \
|
||||
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; \
|
||||
if (b2 == b) { \
|
||||
pE = &e->field.hte_next; \
|
||||
} else { \
|
||||
*pE = e->field.hte_next; \
|
||||
e->field.hte_next = new_table[b2]; \
|
||||
new_table[b2] = e; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
head->hth_table = new_table; \
|
||||
} \
|
||||
head->hth_table_length = new_len; \
|
||||
head->hth_prime_idx = prime_idx; \
|
||||
head->hth_load_limit = new_load_limit; \
|
||||
return 0; \
|
||||
} \
|
||||
/* Free all storage held by 'head'. Does not free 'head' itself, or \
|
||||
* individual elements. */ \
|
||||
void \
|
||||
name##_HT_CLEAR(struct name *head) \
|
||||
{ \
|
||||
if (head->hth_table) \
|
||||
freefn(head->hth_table); \
|
||||
name##_HT_INIT(head); \
|
||||
} \
|
||||
/* Debugging helper: return false iff the representation of 'head' is \
|
||||
* internally consistent. */ \
|
||||
int \
|
||||
name##_HT_REP_IS_BAD_(const struct name *head) \
|
||||
{ \
|
||||
unsigned n, i; \
|
||||
struct type *elm; \
|
||||
if (!head->hth_table_length) { \
|
||||
if (!head->hth_table && !head->hth_n_entries && \
|
||||
!head->hth_load_limit && head->hth_prime_idx == -1) \
|
||||
return 0; \
|
||||
else \
|
||||
return 1; \
|
||||
} \
|
||||
if (!head->hth_table || head->hth_prime_idx < 0 || \
|
||||
!head->hth_load_limit) \
|
||||
return 2; \
|
||||
if (head->hth_n_entries > head->hth_load_limit) \
|
||||
return 3; \
|
||||
if (head->hth_table_length != name##_PRIMES[head->hth_prime_idx]) \
|
||||
return 4; \
|
||||
if (head->hth_load_limit != (unsigned)(load*head->hth_table_length)) \
|
||||
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)) \
|
||||
return 1000 + i; \
|
||||
if ((HT_ELT_HASH_(elm, field, hashfn) % head->hth_table_length) != i) \
|
||||
return 10000 + i; \
|
||||
++n; \
|
||||
} \
|
||||
} \
|
||||
if (n != head->hth_n_entries) \
|
||||
return 6; \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
/** 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) \
|
||||
{ \
|
||||
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) \
|
||||
{ \
|
||||
HT_SET_HASHVAL_(newent, field, (elm)->field.hte_hash); \
|
||||
newent->field.hte_next = NULL; \
|
||||
*var = newent; \
|
||||
++((head)->hth_n_entries); \
|
||||
}
|
||||
|
||||
/*
|
||||
* Copyright 2005, Nick Mathewson. Implementation logic is adapted from code
|
||||
* 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.
|
||||
*
|
||||
* Copyright (c) 2002, Christopher Clark
|
||||
* 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 name of the original author; nor the names of any 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 OWNER
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
/*
|
||||
* Copyright 2001-2007 Niels Provos <provos@citi.umich.edu>
|
||||
* Copyright 2007-2012 Niels Provos and Nick Mathewson
|
||||
*
|
||||
* This header file contains definitions for dealing with HTTP requests
|
||||
* that are internal to libevent. As user of the library, you should not
|
||||
* need to know about these.
|
||||
*/
|
||||
|
||||
#ifndef HTTP_INTERNAL_H_INCLUDED_
|
||||
#define HTTP_INTERNAL_H_INCLUDED_
|
||||
|
||||
#include "event2/event_struct.h"
|
||||
#include "util-internal.h"
|
||||
#include "defer-internal.h"
|
||||
|
||||
#define HTTP_CONNECT_TIMEOUT 45
|
||||
#define HTTP_WRITE_TIMEOUT 50
|
||||
#define HTTP_READ_TIMEOUT 50
|
||||
|
||||
enum message_read_status {
|
||||
ALL_DATA_READ = 1,
|
||||
MORE_DATA_EXPECTED = 0,
|
||||
DATA_CORRUPTED = -1,
|
||||
REQUEST_CANCELED = -2,
|
||||
DATA_TOO_LONG = -3
|
||||
};
|
||||
|
||||
struct evbuffer;
|
||||
struct addrinfo;
|
||||
struct evhttp_request;
|
||||
|
||||
/* Indicates an unknown request method. */
|
||||
#define EVHTTP_REQ_UNKNOWN_ (1<<15)
|
||||
|
||||
enum evhttp_connection_state {
|
||||
EVCON_DISCONNECTED, /**< not currently connected not trying either*/
|
||||
EVCON_CONNECTING, /**< tries to currently connect */
|
||||
EVCON_IDLE, /**< connection is established */
|
||||
EVCON_READING_FIRSTLINE,/**< reading Request-Line (incoming conn) or
|
||||
**< Status-Line (outgoing conn) */
|
||||
EVCON_READING_HEADERS, /**< reading request/response headers */
|
||||
EVCON_READING_BODY, /**< reading request/response body */
|
||||
EVCON_READING_TRAILER, /**< reading request/response chunked trailer */
|
||||
EVCON_WRITING /**< writing request/response headers/body */
|
||||
};
|
||||
|
||||
struct event_base;
|
||||
|
||||
/* A client or server connection. */
|
||||
struct evhttp_connection {
|
||||
/* we use this tailq only if this connection was created for an http
|
||||
* server */
|
||||
TAILQ_ENTRY(evhttp_connection) next;
|
||||
|
||||
evutil_socket_t fd;
|
||||
struct bufferevent *bufev;
|
||||
|
||||
struct event retry_ev; /* for retrying connects */
|
||||
|
||||
char *bind_address; /* address to use for binding the src */
|
||||
ev_uint16_t bind_port; /* local port for binding the src */
|
||||
|
||||
char *address; /* address to connect to */
|
||||
ev_uint16_t port;
|
||||
|
||||
size_t max_headers_size;
|
||||
ev_uint64_t max_body_size;
|
||||
|
||||
int flags;
|
||||
#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 */
|
||||
/* set when we want to auto free the connection */
|
||||
#define EVHTTP_CON_AUTOFREE EVHTTP_CON_PUBLIC_FLAGS_END
|
||||
/* Installed when attempt to read HTTP error after write failed, see
|
||||
* EVHTTP_CON_READ_ON_WRITE_ERROR */
|
||||
#define EVHTTP_CON_READING_ERROR (EVHTTP_CON_AUTOFREE << 1)
|
||||
|
||||
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;
|
||||
|
||||
/* for server connections, the http server they are connected with */
|
||||
struct evhttp *http_server;
|
||||
|
||||
TAILQ_HEAD(evcon_requestq, evhttp_request) requests;
|
||||
|
||||
void (*cb)(struct evhttp_connection *, void *);
|
||||
void *cb_arg;
|
||||
|
||||
void (*closecb)(struct evhttp_connection *, void *);
|
||||
void *closecb_arg;
|
||||
|
||||
struct event_callback read_more_deferred_cb;
|
||||
|
||||
struct event_base *base;
|
||||
struct evdns_base *dns_base;
|
||||
int ai_family;
|
||||
};
|
||||
|
||||
/* A callback for an http server */
|
||||
struct evhttp_cb {
|
||||
TAILQ_ENTRY(evhttp_cb) next;
|
||||
|
||||
char *what;
|
||||
|
||||
void (*cb)(struct evhttp_request *req, void *);
|
||||
void *cbarg;
|
||||
};
|
||||
|
||||
/* both the http server as well as the rpc system need to queue connections */
|
||||
TAILQ_HEAD(evconq, evhttp_connection);
|
||||
|
||||
/* each bound socket is stored in one of these */
|
||||
struct evhttp_bound_socket {
|
||||
TAILQ_ENTRY(evhttp_bound_socket) next;
|
||||
|
||||
struct evconnlistener *listener;
|
||||
};
|
||||
|
||||
/* server alias list item. */
|
||||
struct evhttp_server_alias {
|
||||
TAILQ_ENTRY(evhttp_server_alias) next;
|
||||
|
||||
char *alias; /* the server alias. */
|
||||
};
|
||||
|
||||
struct evhttp {
|
||||
/* Next vhost, if this is a vhost. */
|
||||
TAILQ_ENTRY(evhttp) next_vhost;
|
||||
|
||||
/* All listeners for this host */
|
||||
TAILQ_HEAD(boundq, evhttp_bound_socket) sockets;
|
||||
|
||||
TAILQ_HEAD(httpcbq, evhttp_cb) callbacks;
|
||||
|
||||
/* All live connections on this host. */
|
||||
struct evconq connections;
|
||||
|
||||
TAILQ_HEAD(vhostsq, evhttp) virtualhosts;
|
||||
|
||||
TAILQ_HEAD(aliasq, evhttp_server_alias) aliases;
|
||||
|
||||
/* NULL if this server is not a vhost */
|
||||
char *vhost_pattern;
|
||||
|
||||
struct timeval timeout;
|
||||
|
||||
size_t default_max_headers_size;
|
||||
ev_uint64_t default_max_body_size;
|
||||
int flags;
|
||||
const char *default_content_type;
|
||||
|
||||
/* Bitmask of all HTTP methods that we accept and pass to user
|
||||
* callbacks. */
|
||||
ev_uint16_t allowed_methods;
|
||||
|
||||
/* Fallback callback if all the other callbacks for this connection
|
||||
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;
|
||||
};
|
||||
|
||||
/* XXX most of these functions could be static. */
|
||||
|
||||
/* resets the connection; can be reused for more requests */
|
||||
void evhttp_connection_reset_(struct evhttp_connection *);
|
||||
|
||||
/* connects if necessary */
|
||||
int evhttp_connection_connect_(struct evhttp_connection *);
|
||||
|
||||
enum evhttp_request_error;
|
||||
/* notifies the current request that it failed; resets connection */
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void evhttp_connection_fail_(struct evhttp_connection *,
|
||||
enum evhttp_request_error error);
|
||||
|
||||
enum evhttp_transfer_encoding_header_status {
|
||||
/* This Transfer-Encoding line was invalid.
|
||||
* (We consider all "chunked" encodings invalid except those at the end
|
||||
* of the sequence.) */
|
||||
TE_INVALID,
|
||||
/* This Transfer-Encoding line ended with the "chunked" encoding. */
|
||||
TE_ENDS_IN_CHUNKED,
|
||||
/* This Transfer-Encoding line was valid and did not contain "chunked" */
|
||||
TE_NO_CHUNKED,
|
||||
};
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evhttp_str_is_chunked_(const char *value, const char *eos);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
enum evhttp_transfer_encoding_header_status
|
||||
evhttp_check_transfer_encoding_(const char *value);
|
||||
|
||||
enum message_read_status;
|
||||
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
enum message_read_status evhttp_parse_firstline_(struct evhttp_request *, struct evbuffer*);
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
enum message_read_status evhttp_parse_headers_(struct evhttp_request *, struct evbuffer*);
|
||||
|
||||
void evhttp_start_read_(struct evhttp_connection *);
|
||||
void evhttp_start_write_(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 *);
|
||||
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evhttp_decode_uri_internal(const char *uri, size_t length,
|
||||
char *ret, int decode_plus);
|
||||
|
||||
#endif /* HTTP_INTERNAL_H_INCLUDED_ */
|
||||
+5292
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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 EVENT1_EVDNS_H_INCLUDED_
|
||||
#define EVENT1_EVDNS_H_INCLUDED_
|
||||
|
||||
/** @file evdns.h
|
||||
|
||||
A dns subsystem for Libevent.
|
||||
|
||||
The <evdns.h> header is deprecated in Libevent 2.0 and later; please
|
||||
use <event2/evdns.h> instead. Depending on what functionality you
|
||||
need, you may also want to include more of the other <event2/...>
|
||||
headers.
|
||||
*/
|
||||
|
||||
#include <event.h>
|
||||
#include <event2/dns.h>
|
||||
#include <event2/dns_compat.h>
|
||||
#include <event2/dns_struct.h>
|
||||
|
||||
#endif /* EVENT1_EVDNS_H_INCLUDED_ */
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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 EVENT1_EVENT_H_INCLUDED_
|
||||
#define EVENT1_EVENT_H_INCLUDED_
|
||||
|
||||
/** @file event.h
|
||||
|
||||
A library for writing event-driven network servers.
|
||||
|
||||
The <event.h> header is deprecated in Libevent 2.0 and later; please
|
||||
use <event2/event.h> instead. Depending on what functionality you
|
||||
need, you may also want to include more of the other event2/
|
||||
headers.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <event2/event-config.h>
|
||||
#ifdef EVENT__HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_STDINT_H
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
#include <stdarg.h>
|
||||
|
||||
/* For int types. */
|
||||
#include <evutil.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#undef WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
|
||||
#include <event2/event_struct.h>
|
||||
#include <event2/event.h>
|
||||
#include <event2/event_compat.h>
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/buffer_compat.h>
|
||||
#include <event2/bufferevent.h>
|
||||
#include <event2/bufferevent_struct.h>
|
||||
#include <event2/bufferevent_compat.h>
|
||||
#include <event2/tag.h>
|
||||
#include <event2/tag_compat.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* EVENT1_EVENT_H_INCLUDED_ */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* 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_BUFFER_COMPAT_H_INCLUDED_
|
||||
#define EVENT2_BUFFER_COMPAT_H_INCLUDED_
|
||||
|
||||
#include <event2/visibility.h>
|
||||
|
||||
/** @file event2/buffer_compat.h
|
||||
|
||||
Obsolete and deprecated versions of the functions in buffer.h: provided
|
||||
only for backward compatibility.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
Obsolete alias for evbuffer_readln(buffer, NULL, EVBUFFER_EOL_ANY).
|
||||
|
||||
@deprecated This function is deprecated because its behavior is not correct
|
||||
for almost any protocol, and also because it's wholly subsumed by
|
||||
evbuffer_readln().
|
||||
|
||||
@param buffer the evbuffer to read from
|
||||
@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
|
||||
removed from an evbuffer.
|
||||
|
||||
An evbuffer may have one or more callbacks set at a time. The order
|
||||
in which they are executed is undefined.
|
||||
|
||||
A callback function may add more callbacks, or remove itself from the
|
||||
list of callbacks, or add or remove data from the buffer. It may not
|
||||
remove another callback from the list.
|
||||
|
||||
If a callback adds or removes data from the buffer or from another
|
||||
buffer, this can cause a recursive invocation of your callback or
|
||||
other callbacks. If you ask for an infinite loop, you might just get
|
||||
one: watch out!
|
||||
|
||||
@param buffer the buffer whose size has changed
|
||||
@param old_len the previous length of the buffer
|
||||
@param new_len the current length of the buffer
|
||||
@param arg a pointer to user data
|
||||
*/
|
||||
typedef void (*evbuffer_cb)(struct evbuffer *buffer, size_t old_len, size_t new_len, void *arg);
|
||||
|
||||
/**
|
||||
Replace all callbacks on an evbuffer with a single new callback, or
|
||||
remove them.
|
||||
|
||||
Subsequent calls to evbuffer_setcb() replace callbacks set by previous
|
||||
calls. Setting the callback to NULL removes any previously set callback.
|
||||
|
||||
@deprecated This function is deprecated because it clears all previous
|
||||
callbacks set on the evbuffer, which can cause confusing behavior if
|
||||
multiple parts of the code all want to add their own callbacks on a
|
||||
buffer. Instead, use evbuffer_add(), evbuffer_del(), and
|
||||
evbuffer_setflags() to manage your own evbuffer callbacks without
|
||||
interfering with callbacks set by others.
|
||||
|
||||
@param buffer the evbuffer to be monitored
|
||||
@param cb the callback function to invoke when the evbuffer is modified,
|
||||
or NULL to remove all callbacks.
|
||||
@param cbarg an argument to be provided to the callback function
|
||||
@return 0 if successful, or -1 on error
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evbuffer_setcb(struct evbuffer *buffer, evbuffer_cb cb, void *cbarg);
|
||||
|
||||
|
||||
/**
|
||||
Find a string within an evbuffer.
|
||||
|
||||
@param buffer the evbuffer to be searched
|
||||
@param what the string to be searched for
|
||||
@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 */
|
||||
#define EVBUFFER_LENGTH(x) evbuffer_get_length(x)
|
||||
/** deprecated in favor of calling the functions directly */
|
||||
#define EVBUFFER_DATA(x) evbuffer_pullup((x), -1)
|
||||
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2012 Niels Provos, Nick Mathewson
|
||||
* Copyright (c) 2000-2007 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.
|
||||
* 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_BUFFEREVENT_COMPAT_H_INCLUDED_
|
||||
#define EVENT2_BUFFEREVENT_COMPAT_H_INCLUDED_
|
||||
|
||||
#include <event2/visibility.h>
|
||||
|
||||
#define evbuffercb bufferevent_data_cb
|
||||
#define everrorcb bufferevent_event_cb
|
||||
|
||||
/**
|
||||
Create a new bufferevent for an fd.
|
||||
|
||||
This function is deprecated. Use bufferevent_socket_new and
|
||||
bufferevent_set_callbacks instead.
|
||||
|
||||
Libevent provides an abstraction on top of the regular event callbacks.
|
||||
This abstraction is called a buffered event. A buffered event provides
|
||||
input and output buffers that get filled and drained automatically. The
|
||||
user of a buffered event no longer deals directly with the I/O, but
|
||||
instead is reading from input and writing to output buffers.
|
||||
|
||||
Once initialized, the bufferevent structure can be used repeatedly with
|
||||
bufferevent_enable() and bufferevent_disable().
|
||||
|
||||
When read enabled the bufferevent will try to read from the file descriptor
|
||||
and call the read callback. The write callback is executed whenever the
|
||||
output buffer is drained below the write low watermark, which is 0 by
|
||||
default.
|
||||
|
||||
If multiple bases are in use, bufferevent_base_set() must be called before
|
||||
enabling the bufferevent for the first time.
|
||||
|
||||
@deprecated This function is deprecated because it uses the current
|
||||
event base, and as such can be error prone for multithreaded programs.
|
||||
Use bufferevent_socket_new() instead.
|
||||
|
||||
@param fd the file descriptor from which data is read and written to.
|
||||
This file descriptor is not allowed to be a pipe(2).
|
||||
@param readcb callback to invoke when there is data to be read, or NULL if
|
||||
no callback is desired
|
||||
@param writecb callback to invoke when the file descriptor is ready for
|
||||
writing, or NULL if no callback is desired
|
||||
@param errorcb callback to invoke when there is an error on the file
|
||||
descriptor
|
||||
@param cbarg an argument that will be supplied to each of the callbacks
|
||||
(readcb, writecb, and errorcb)
|
||||
@return a pointer to a newly allocated bufferevent struct, or NULL if an
|
||||
error occurred
|
||||
@see bufferevent_base_set(), bufferevent_free()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
struct bufferevent *bufferevent_new(evutil_socket_t fd,
|
||||
evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
|
||||
|
||||
|
||||
/**
|
||||
Set the read and write timeout for a buffered event.
|
||||
|
||||
@param bufev the bufferevent to be modified
|
||||
@param timeout_read the read timeout
|
||||
@param timeout_write the write timeout
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void bufferevent_settimeout(struct bufferevent *bufev,
|
||||
int timeout_read, int timeout_write);
|
||||
|
||||
#define EVBUFFER_READ BEV_EVENT_READING
|
||||
#define EVBUFFER_WRITE BEV_EVENT_WRITING
|
||||
#define EVBUFFER_EOF BEV_EVENT_EOF
|
||||
#define EVBUFFER_ERROR BEV_EVENT_ERROR
|
||||
#define EVBUFFER_TIMEOUT BEV_EVENT_TIMEOUT
|
||||
|
||||
/** macro for getting access to the input buffer of a bufferevent */
|
||||
#define EVBUFFER_INPUT(x) bufferevent_get_input(x)
|
||||
/** macro for getting access to the output buffer of a bufferevent */
|
||||
#define EVBUFFER_OUTPUT(x) bufferevent_get_output(x)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Copyright (c) 2009-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_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>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* This is what openssl's SSL objects are underneath. */
|
||||
struct ssl_st;
|
||||
|
||||
/**
|
||||
The state of an SSL object to be used when creating a new
|
||||
SSL bufferevent.
|
||||
*/
|
||||
enum bufferevent_ssl_state {
|
||||
BUFFEREVENT_SSL_OPEN = 0,
|
||||
BUFFEREVENT_SSL_CONNECTING = 1,
|
||||
BUFFEREVENT_SSL_ACCEPTING = 2
|
||||
};
|
||||
|
||||
#if defined(EVENT__HAVE_OPENSSL) || defined(EVENT_IN_DOXYGEN_)
|
||||
/**
|
||||
Create a new SSL bufferevent to send its data over another bufferevent.
|
||||
|
||||
@param base An event_base to use to detect reading and writing. It
|
||||
must also be the base for the underlying bufferevent.
|
||||
@param underlying A socket to use for this SSL
|
||||
@param ssl A SSL* object from openssl.
|
||||
@param state The current state of the SSL connection
|
||||
@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,
|
||||
struct ssl_st *ssl,
|
||||
enum bufferevent_ssl_state state,
|
||||
int options);
|
||||
|
||||
/**
|
||||
Create a new SSL bufferevent to send its data over an SSL * on a socket.
|
||||
|
||||
@param base An event_base to use to detect reading and writing
|
||||
@param fd A socket to use for this SSL
|
||||
@param ssl A SSL* object from openssl.
|
||||
@param state The current state of the SSL connection
|
||||
@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,
|
||||
struct ssl_st *ssl,
|
||||
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
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* EVENT2_BUFFEREVENT_SSL_H_INCLUDED_ */
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* 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 EVENT2_BUFFEREVENT_STRUCT_H_INCLUDED_
|
||||
#define EVENT2_BUFFEREVENT_STRUCT_H_INCLUDED_
|
||||
|
||||
/** @file event2/bufferevent_struct.h
|
||||
|
||||
Data structures for bufferevents. Using these structures may hurt forward
|
||||
compatibility with later versions of Libevent: be careful!
|
||||
|
||||
@deprecated Use of bufferevent_struct.h is completely deprecated; these
|
||||
structures are only exposed for backward compatibility with programs
|
||||
written before Libevent 2.0 that used them.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <event2/event-config.h>
|
||||
#ifdef EVENT__HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
/* For int types. */
|
||||
#include <event2/util.h>
|
||||
/* For struct event */
|
||||
#include <event2/event_struct.h>
|
||||
|
||||
struct event_watermark {
|
||||
size_t low;
|
||||
size_t high;
|
||||
};
|
||||
|
||||
/**
|
||||
Shared implementation of a bufferevent.
|
||||
|
||||
This type is exposed only because it was exposed in previous versions,
|
||||
and some people's code may rely on manipulating it. Otherwise, you
|
||||
should really not rely on the layout, size, or contents of this structure:
|
||||
it is fairly volatile, and WILL change in future versions of the code.
|
||||
**/
|
||||
struct bufferevent {
|
||||
/** Event base for which this bufferevent was created. */
|
||||
struct event_base *ev_base;
|
||||
/** Pointer to a table of function pointers to set up how this
|
||||
bufferevent behaves. */
|
||||
const struct bufferevent_ops *be_ops;
|
||||
|
||||
/** A read event that triggers when a timeout has happened or a socket
|
||||
is ready to read data. Only used by some subtypes of
|
||||
bufferevent. */
|
||||
struct event ev_read;
|
||||
/** A write event that triggers when a timeout has happened or a socket
|
||||
is ready to write data. Only used by some subtypes of
|
||||
bufferevent. */
|
||||
struct event ev_write;
|
||||
|
||||
/** An input buffer. Only the bufferevent is allowed to add data to
|
||||
this buffer, though the user is allowed to drain it. */
|
||||
struct evbuffer *input;
|
||||
|
||||
/** An input buffer. Only the bufferevent is allowed to drain data
|
||||
from this buffer, though the user is allowed to add it. */
|
||||
struct evbuffer *output;
|
||||
|
||||
struct event_watermark wm_read;
|
||||
struct event_watermark wm_write;
|
||||
|
||||
bufferevent_data_cb readcb;
|
||||
bufferevent_data_cb writecb;
|
||||
/* This should be called 'eventcb', but renaming it would break
|
||||
* backward compatibility */
|
||||
bufferevent_event_cb errorcb;
|
||||
void *cbarg;
|
||||
|
||||
struct timeval timeout_read;
|
||||
struct timeval timeout_write;
|
||||
|
||||
/** Events that are currently enabled: currently EV_READ and EV_WRITE
|
||||
are supported. */
|
||||
short enabled;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* EVENT2_BUFFEREVENT_STRUCT_H_INCLUDED_ */
|
||||
@@ -0,0 +1,751 @@
|
||||
/*
|
||||
* Copyright (c) 2006-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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* The original DNS code is due to Adam Langley with heavy
|
||||
* modifications by Nick Mathewson. Adam put his DNS software in the
|
||||
* public domain. You can find his original copyright below. Please,
|
||||
* aware that the code as part of Libevent is governed by the 3-clause
|
||||
* BSD license above.
|
||||
*
|
||||
* This software is Public Domain. To view a copy of the public domain dedication,
|
||||
* visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
|
||||
* Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
|
||||
*
|
||||
* I ask and expect, but do not require, that all derivative works contain an
|
||||
* attribution similar to:
|
||||
* Parts developed by Adam Langley <agl@imperialviolet.org>
|
||||
*
|
||||
* You may wish to replace the word "Parts" with something else depending on
|
||||
* the amount of original code.
|
||||
*
|
||||
* (Derivative works does not include programs which link against, run or include
|
||||
* the source verbatim in their source distributions)
|
||||
*/
|
||||
|
||||
/** @file event2/dns.h
|
||||
*
|
||||
* Welcome, gentle reader
|
||||
*
|
||||
* Async DNS lookups are really a whole lot harder than they should be,
|
||||
* mostly stemming from the fact that the libc resolver has never been
|
||||
* very good at them. Before you use this library you should see if libc
|
||||
* can do the job for you with the modern async call getaddrinfo_a
|
||||
* (see http://www.imperialviolet.org/page25.html#e498). Otherwise,
|
||||
* please continue.
|
||||
*
|
||||
* The library keeps track of the state of nameservers and will avoid
|
||||
* them when they go down. Otherwise it will round robin between them.
|
||||
*
|
||||
* Quick start guide:
|
||||
* #include "evdns.h"
|
||||
* void callback(int result, char type, int count, int ttl,
|
||||
* void *addresses, void *arg);
|
||||
* evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
|
||||
* evdns_resolve("www.hostname.com", 0, callback, NULL);
|
||||
*
|
||||
* When the lookup is complete the callback function is called. The
|
||||
* first argument will be one of the DNS_ERR_* defines in evdns.h.
|
||||
* Hopefully it will be DNS_ERR_NONE, in which case type will be
|
||||
* DNS_IPv4_A, count will be the number of IP addresses, ttl is the time
|
||||
* which the data can be cached for (in seconds), addresses will point
|
||||
* to an array of uint32_t's and arg will be whatever you passed to
|
||||
* evdns_resolve.
|
||||
*
|
||||
* Searching:
|
||||
*
|
||||
* In order for this library to be a good replacement for glibc's resolver it
|
||||
* supports searching. This involves setting a list of default domains, in
|
||||
* which names will be queried for. The number of dots in the query name
|
||||
* determines the order in which this list is used.
|
||||
*
|
||||
* Searching appears to be a single lookup from the point of view of the API,
|
||||
* although many DNS queries may be generated from a single call to
|
||||
* evdns_resolve. Searching can also drastically slow down the resolution
|
||||
* of names.
|
||||
*
|
||||
* To disable searching:
|
||||
* 1. Never set it up. If you never call evdns_resolv_conf_parse or
|
||||
* evdns_search_add then no searching will occur.
|
||||
*
|
||||
* 2. If you do call evdns_resolv_conf_parse then don't pass
|
||||
* DNS_OPTION_SEARCH (or DNS_OPTIONS_ALL, which implies it).
|
||||
*
|
||||
* 3. When calling evdns_resolve, pass the DNS_QUERY_NO_SEARCH flag.
|
||||
*
|
||||
* The order of searches depends on the number of dots in the name. If the
|
||||
* number is greater than the ndots setting then the names is first tried
|
||||
* globally. Otherwise each search domain is appended in turn.
|
||||
*
|
||||
* The ndots setting can either be set from a resolv.conf, or by calling
|
||||
* evdns_search_ndots_set.
|
||||
*
|
||||
* For example, with ndots set to 1 (the default) and a search domain list of
|
||||
* ["myhome.net"]:
|
||||
* Query: www
|
||||
* Order: www.myhome.net, www.
|
||||
*
|
||||
* Query: www.abc
|
||||
* Order: www.abc., www.abc.myhome.net
|
||||
*
|
||||
* Internals:
|
||||
*
|
||||
* Requests are kept in two queues. The first is the inflight queue. In
|
||||
* this queue requests have an allocated transaction id and nameserver.
|
||||
* They will soon be transmitted if they haven't already been.
|
||||
*
|
||||
* The second is the waiting queue. The size of the inflight ring is
|
||||
* limited and all other requests wait in waiting queue for space. This
|
||||
* bounds the number of concurrent requests so that we don't flood the
|
||||
* nameserver. Several algorithms require a full walk of the inflight
|
||||
* queue and so bounding its size keeps thing going nicely under huge
|
||||
* (many thousands of requests) loads.
|
||||
*
|
||||
* If a nameserver loses too many requests it is considered down and we
|
||||
* try not to use it. After a while we send a probe to that nameserver
|
||||
* (a lookup for google.com) and, if it replies, we consider it working
|
||||
* again. If the nameserver fails a probe we wait longer to try again
|
||||
* with the next probe.
|
||||
*/
|
||||
|
||||
#ifndef EVENT2_DNS_H_INCLUDED_
|
||||
#define EVENT2_DNS_H_INCLUDED_
|
||||
|
||||
#include <event2/visibility.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* For integer types. */
|
||||
#include <event2/util.h>
|
||||
|
||||
/** Error codes 0-5 are as described in RFC 1035. */
|
||||
#define DNS_ERR_NONE 0
|
||||
/** The name server was unable to interpret the query */
|
||||
#define DNS_ERR_FORMAT 1
|
||||
/** The name server was unable to process this query due to a problem with the
|
||||
* name server */
|
||||
#define DNS_ERR_SERVERFAILED 2
|
||||
/** The domain name does not exist */
|
||||
#define DNS_ERR_NOTEXIST 3
|
||||
/** The name server does not support the requested kind of query */
|
||||
#define DNS_ERR_NOTIMPL 4
|
||||
/** The name server refuses to reform the specified operation for policy
|
||||
* reasons */
|
||||
#define DNS_ERR_REFUSED 5
|
||||
/** The reply was truncated or ill-formatted */
|
||||
#define DNS_ERR_TRUNCATED 65
|
||||
/** An unknown error occurred */
|
||||
#define DNS_ERR_UNKNOWN 66
|
||||
/** Communication with the server timed out */
|
||||
#define DNS_ERR_TIMEOUT 67
|
||||
/** The request was canceled because the DNS subsystem was shut down. */
|
||||
#define DNS_ERR_SHUTDOWN 68
|
||||
/** The request was canceled via a call to evdns_cancel_request */
|
||||
#define DNS_ERR_CANCEL 69
|
||||
/** There were no answers and no error condition in the DNS packet.
|
||||
* This can happen when you ask for an address that exists, but a record
|
||||
* type that doesn't. */
|
||||
#define DNS_ERR_NODATA 70
|
||||
|
||||
#define DNS_IPv4_A 1
|
||||
#define DNS_PTR 2
|
||||
#define DNS_IPv6_AAAA 3
|
||||
|
||||
#define DNS_QUERY_NO_SEARCH 1
|
||||
|
||||
/* Allow searching */
|
||||
#define DNS_OPTION_SEARCH 1
|
||||
/* Parse "nameserver" and add default if no such section */
|
||||
#define DNS_OPTION_NAMESERVERS 2
|
||||
/* Parse additional options like:
|
||||
* - timeout:
|
||||
* - getaddrinfo-allow-skew:
|
||||
* - max-timeouts:
|
||||
* - max-inflight:
|
||||
* - attempts:
|
||||
* - randomize-case:
|
||||
* - initial-probe-timeout:
|
||||
*/
|
||||
#define DNS_OPTION_MISC 4
|
||||
/* Load hosts file (i.e. "/etc/hosts") */
|
||||
#define DNS_OPTION_HOSTSFILE 8
|
||||
/**
|
||||
* All above:
|
||||
* - DNS_OPTION_SEARCH
|
||||
* - DNS_OPTION_NAMESERVERS
|
||||
* - DNS_OPTION_MISC
|
||||
* - DNS_OPTION_HOSTSFILE
|
||||
*/
|
||||
#define DNS_OPTIONS_ALL ( \
|
||||
DNS_OPTION_SEARCH | \
|
||||
DNS_OPTION_NAMESERVERS | \
|
||||
DNS_OPTION_MISC | \
|
||||
DNS_OPTION_HOSTSFILE | \
|
||||
0 \
|
||||
)
|
||||
/* Do not "default" nameserver (i.e. "127.0.0.1:53") if there is no nameservers
|
||||
* in resolv.conf, (iff DNS_OPTION_NAMESERVERS is set) */
|
||||
#define DNS_OPTION_NAMESERVERS_NO_DEFAULT 16
|
||||
|
||||
/* Obsolete name for DNS_QUERY_NO_SEARCH */
|
||||
#define DNS_NO_SEARCH DNS_QUERY_NO_SEARCH
|
||||
|
||||
/**
|
||||
* The callback that contains the results from a lookup.
|
||||
* - result is one of the DNS_ERR_* values (DNS_ERR_NONE for success)
|
||||
* - type is either DNS_IPv4_A or DNS_PTR or DNS_IPv6_AAAA
|
||||
* - count contains the number of addresses of form type
|
||||
* - ttl is the number of seconds the resolution may be cached for.
|
||||
* - addresses needs to be cast according to type. It will be an array of
|
||||
* 4-byte sequences for ipv4, or an array of 16-byte sequences for ipv6,
|
||||
* or a nul-terminated string for PTR.
|
||||
*/
|
||||
typedef void (*evdns_callback_type) (int result, char type, int count, int ttl, void *addresses, void *arg);
|
||||
|
||||
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
|
||||
/** Flag for evdns_base_new: If EVDNS_BASE_INITIALIZE_NAMESERVERS isset, do not
|
||||
* add default nameserver if there are no nameservers in resolv.conf
|
||||
* @see DNS_OPTION_NAMESERVERS_NO_DEFAULT */
|
||||
#define EVDNS_BASE_NAMESERVERS_NO_DEFAULT 0x10000
|
||||
|
||||
/**
|
||||
Initialize the asynchronous DNS library.
|
||||
|
||||
This function initializes support for non-blocking name resolution by
|
||||
calling evdns_resolv_conf_parse() on UNIX and
|
||||
evdns_config_windows_nameservers() on Windows.
|
||||
|
||||
@param event_base the event base to associate the dns client with
|
||||
@param flags any of EVDNS_BASE_INITIALIZE_NAMESERVERS|
|
||||
EVDNS_BASE_DISABLE_WHEN_INACTIVE|EVDNS_BASE_NAMESERVERS_NO_DEFAULT
|
||||
@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);
|
||||
|
||||
|
||||
/**
|
||||
Shut down the asynchronous DNS resolver and terminate all active requests.
|
||||
|
||||
If the 'fail_requests' option is enabled, all active requests will return
|
||||
an empty result with the error flag set to DNS_ERR_SHUTDOWN. Otherwise,
|
||||
the requests will be silently discarded.
|
||||
|
||||
@param evdns_base the evdns base to free
|
||||
@param fail_requests if zero, active requests will be aborted; if non-zero,
|
||||
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);
|
||||
|
||||
|
||||
/**
|
||||
Add a nameserver.
|
||||
|
||||
The address should be an IPv4 address in network byte order.
|
||||
The type of address is chosen so that it matches in_addr.s_addr.
|
||||
|
||||
@param base the evdns_base to which to add the name server
|
||||
@param address an IP address in network byte order
|
||||
@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);
|
||||
|
||||
/**
|
||||
Get the number of configured nameservers.
|
||||
|
||||
This returns the number of configured nameservers (not necessarily the
|
||||
number of running nameservers). This is useful for double-checking
|
||||
whether our calls to the various nameserver configuration functions
|
||||
have been successful.
|
||||
|
||||
@param base the evdns_base to which to apply this operation
|
||||
@return the number of configured nameservers
|
||||
@see evdns_base_nameserver_add()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_base_count_nameservers(struct evdns_base *base);
|
||||
|
||||
/**
|
||||
Remove all configured nameservers, and suspend all pending resolves.
|
||||
|
||||
Resolves will not necessarily be re-attempted until evdns_base_resume() is called.
|
||||
|
||||
@param base the evdns_base to which to apply this operation
|
||||
@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);
|
||||
|
||||
|
||||
/**
|
||||
Resume normal operation and continue any suspended resolve requests.
|
||||
|
||||
Re-attempt resolves left in limbo after an earlier call to
|
||||
evdns_base_clear_nameservers_and_suspend().
|
||||
|
||||
@param base the evdns_base to which to apply this operation
|
||||
@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);
|
||||
|
||||
/**
|
||||
Add a nameserver by string address.
|
||||
|
||||
This function parses a n IPv4 or IPv6 address from a string and adds it as a
|
||||
nameserver. It supports the following formats:
|
||||
- [IPv6Address]:port
|
||||
- [IPv6Address]
|
||||
- IPv6Address
|
||||
- IPv4Address:port
|
||||
- IPv4Address
|
||||
|
||||
If no port is specified, it defaults to 53.
|
||||
|
||||
@param base the evdns_base to which to apply this operation
|
||||
@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);
|
||||
|
||||
struct evdns_request;
|
||||
|
||||
/**
|
||||
Lookup an A record for a given name.
|
||||
|
||||
@param base the evdns_base to which to apply this operation
|
||||
@param name a DNS hostname
|
||||
@param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
|
||||
@param callback a callback function to invoke when the request is completed
|
||||
@param ptr an argument to pass to the callback function
|
||||
@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);
|
||||
|
||||
/**
|
||||
Lookup an AAAA record for a given name.
|
||||
|
||||
@param base the evdns_base to which to apply this operation
|
||||
@param name a DNS hostname
|
||||
@param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
|
||||
@param callback a callback function to invoke when the request is completed
|
||||
@param ptr an argument to pass to the callback function
|
||||
@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;
|
||||
struct in6_addr;
|
||||
|
||||
/**
|
||||
Lookup a PTR record for a given IP address.
|
||||
|
||||
@param base the evdns_base to which to apply this operation
|
||||
@param in an IPv4 address
|
||||
@param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
|
||||
@param callback a callback function to invoke when the request is completed
|
||||
@param ptr an argument to pass to the callback function
|
||||
@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);
|
||||
|
||||
|
||||
/**
|
||||
Lookup a PTR record for a given IPv6 address.
|
||||
|
||||
@param base the evdns_base to which to apply this operation
|
||||
@param in an IPv6 address
|
||||
@param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
|
||||
@param callback a callback function to invoke when the request is completed
|
||||
@param ptr an argument to pass to the callback function
|
||||
@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);
|
||||
|
||||
/**
|
||||
Cancels a pending DNS resolution request.
|
||||
|
||||
@param base the evdns_base that was used to make the request
|
||||
@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);
|
||||
|
||||
/**
|
||||
Set the value of a configuration option.
|
||||
|
||||
The currently available configuration options are:
|
||||
|
||||
ndots, timeout, max-timeouts, max-inflight, attempts, randomize-case,
|
||||
bind-to, initial-probe-timeout, getaddrinfo-allow-skew,
|
||||
so-rcvbuf, so-sndbuf.
|
||||
|
||||
In versions before Libevent 2.0.3-alpha, the option name needed to end with
|
||||
a colon.
|
||||
|
||||
@param base the evdns_base to which to apply this operation
|
||||
@param option the name of the configuration option to be modified
|
||||
@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);
|
||||
|
||||
|
||||
/**
|
||||
Parse a resolv.conf file.
|
||||
|
||||
The 'flags' parameter determines what information is parsed from the
|
||||
resolv.conf file. See the man page for resolv.conf for the format of this
|
||||
file.
|
||||
|
||||
The following directives are not parsed from the file: sortlist, rotate,
|
||||
no-check-names, inet6, debug.
|
||||
|
||||
If this function encounters an error, the possible return values are: 1 =
|
||||
failed to open file, 2 = failed to stat file, 3 = file too large, 4 = out of
|
||||
memory, 5 = short read from file, 6 = no nameservers listed in the file
|
||||
|
||||
@param base the evdns_base to which to apply this operation
|
||||
@param flags any of DNS_OPTION_NAMESERVERS|DNS_OPTION_SEARCH|DNS_OPTION_MISC|
|
||||
DNS_OPTION_HOSTSFILE|DNS_OPTIONS_ALL|DNS_OPTION_NAMESERVERS_NO_DEFAULT
|
||||
@param filename the path to the resolv.conf file
|
||||
@return 0 if successful, or various positive error codes if an error
|
||||
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);
|
||||
|
||||
/**
|
||||
Load an /etc/hosts-style file from 'hosts_fname' into 'base'.
|
||||
|
||||
If hosts_fname is NULL, add minimal entries for localhost, and nothing
|
||||
else.
|
||||
|
||||
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);
|
||||
|
||||
#if defined(EVENT_IN_DOXYGEN_) || defined(_WIN32)
|
||||
/**
|
||||
Obtain nameserver information using the Windows API.
|
||||
|
||||
Attempt to configure a set of nameservers based on platform settings on
|
||||
a win32 host. Preferentially tries to use GetNetworkParams; if that fails,
|
||||
looks in the registry.
|
||||
|
||||
@return 0 if successful, or -1 if an error occurred
|
||||
@see evdns_resolv_conf_parse()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_base_config_windows_nameservers(struct evdns_base *);
|
||||
#define EVDNS_BASE_CONFIG_WINDOWS_NAMESERVERS_IMPLEMENTED
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
Clear the list of search domains.
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void evdns_base_search_clear(struct evdns_base *base);
|
||||
|
||||
|
||||
/**
|
||||
Add a domain to the list of search domains
|
||||
|
||||
@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);
|
||||
|
||||
|
||||
/**
|
||||
Set the 'ndots' parameter for searches.
|
||||
|
||||
Sets the number of dots which, when found in a name, causes
|
||||
the first query to be without any search domain.
|
||||
|
||||
@param ndots the new ndots parameter
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void evdns_base_search_ndots_set(struct evdns_base *base, const int ndots);
|
||||
|
||||
/**
|
||||
A callback that is invoked when a log message is generated
|
||||
|
||||
@param is_warning indicates if the log message is a 'warning'
|
||||
@param msg the content of the log message
|
||||
*/
|
||||
typedef void (*evdns_debug_log_fn_type)(int is_warning, const char *msg);
|
||||
|
||||
|
||||
/**
|
||||
Set the callback function to handle DNS log messages. If this
|
||||
callback is not set, evdns log messages are handled with the regular
|
||||
Libevent logging system.
|
||||
|
||||
@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);
|
||||
|
||||
/**
|
||||
Set a callback that will be invoked to generate transaction IDs. By
|
||||
default, we pick transaction IDs based on the current clock time, which
|
||||
is bad for security.
|
||||
|
||||
@param fn the new callback, or NULL to use the default.
|
||||
|
||||
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));
|
||||
|
||||
/**
|
||||
Set a callback used to generate random bytes. By default, we use
|
||||
the same function as passed to evdns_set_transaction_id_fn to generate
|
||||
bytes two at a time. If a function is provided here, it's also used
|
||||
to generate transaction IDs.
|
||||
|
||||
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));
|
||||
|
||||
/*
|
||||
* Functions used to implement a DNS server.
|
||||
*/
|
||||
|
||||
struct evdns_server_request;
|
||||
struct evdns_server_question;
|
||||
|
||||
/**
|
||||
A callback to implement a DNS server. The callback function receives a DNS
|
||||
request. It should then optionally add a number of answers to the reply
|
||||
using the evdns_server_request_add_*_reply functions, before calling either
|
||||
evdns_server_request_respond to send the reply back, or
|
||||
evdns_server_request_drop to decline to answer the request.
|
||||
|
||||
@param req A newly received request
|
||||
@param user_data A pointer that was passed to
|
||||
evdns_add_server_port_with_base().
|
||||
*/
|
||||
typedef void (*evdns_request_callback_fn_type)(struct evdns_server_request *, void *);
|
||||
#define EVDNS_ANSWER_SECTION 0
|
||||
#define EVDNS_AUTHORITY_SECTION 1
|
||||
#define EVDNS_ADDITIONAL_SECTION 2
|
||||
|
||||
#define EVDNS_TYPE_A 1
|
||||
#define EVDNS_TYPE_NS 2
|
||||
#define EVDNS_TYPE_CNAME 5
|
||||
#define EVDNS_TYPE_SOA 6
|
||||
#define EVDNS_TYPE_PTR 12
|
||||
#define EVDNS_TYPE_MX 15
|
||||
#define EVDNS_TYPE_TXT 16
|
||||
#define EVDNS_TYPE_AAAA 28
|
||||
|
||||
#define EVDNS_QTYPE_AXFR 252
|
||||
#define EVDNS_QTYPE_ALL 255
|
||||
|
||||
#define EVDNS_CLASS_INET 1
|
||||
|
||||
/* flags that can be set in answers; as part of the err parameter */
|
||||
#define EVDNS_FLAGS_AA 0x400
|
||||
#define EVDNS_FLAGS_RD 0x080
|
||||
|
||||
/** Create a new DNS server port.
|
||||
|
||||
@param base The event base to handle events for the server port.
|
||||
@param socket A UDP socket to accept DNS requests.
|
||||
@param flags Always 0 for now.
|
||||
@param callback A function to invoke whenever we get a DNS request
|
||||
on the socket.
|
||||
@param user_data Data to pass to the callback.
|
||||
@return an evdns_server_port structure for this server port or NULL if
|
||||
an error occurred.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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);
|
||||
|
||||
struct evdns_base;
|
||||
struct evdns_getaddrinfo_request;
|
||||
/** Make a non-blocking getaddrinfo request using the dns_base in 'dns_base'.
|
||||
*
|
||||
* If we can answer the request immediately (with an error or not!), then we
|
||||
* invoke cb immediately and return NULL. Otherwise we return
|
||||
* an evdns_getaddrinfo_request and invoke cb later.
|
||||
*
|
||||
* When the callback is invoked, we pass as its first argument the error code
|
||||
* that getaddrinfo would return (or 0 for no error). As its second argument,
|
||||
* we pass the evutil_addrinfo structures we found (or NULL on error). We
|
||||
* pass 'arg' as the third argument.
|
||||
*
|
||||
* Limitations:
|
||||
*
|
||||
* - The AI_V4MAPPED and AI_ALL flags are not currently implemented.
|
||||
* - 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,
|
||||
const struct evutil_addrinfo *hints_in,
|
||||
evdns_getaddrinfo_cb cb, void *arg);
|
||||
|
||||
/* 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);
|
||||
|
||||
/**
|
||||
Retrieve the address of the 'idx'th configured nameserver.
|
||||
|
||||
@param base The evdns_base to examine.
|
||||
@param idx The index of the nameserver to get the address of.
|
||||
@param sa A location to receive the server's address.
|
||||
@param len The number of bytes available at sa.
|
||||
|
||||
@return the number of bytes written into sa on success. On failure, returns
|
||||
-1 if idx is greater than the number of configured nameservers, or a
|
||||
value greater than 'len' if len was not high enough.
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_base_get_nameserver_addr(struct evdns_base *base, int idx,
|
||||
struct sockaddr *sa, ev_socklen_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* !EVENT2_DNS_H_INCLUDED_ */
|
||||
@@ -0,0 +1,358 @@
|
||||
/*
|
||||
* Copyright (c) 2006-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 EVENT2_DNS_COMPAT_H_INCLUDED_
|
||||
#define EVENT2_DNS_COMPAT_H_INCLUDED_
|
||||
|
||||
/** @file event2/dns_compat.h
|
||||
|
||||
Potentially non-threadsafe versions of the functions in dns.h: provided
|
||||
only for backwards compatibility.
|
||||
|
||||
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <event2/event-config.h>
|
||||
#ifdef EVENT__HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
/* For int types. */
|
||||
#include <event2/util.h>
|
||||
#include <event2/visibility.h>
|
||||
|
||||
/**
|
||||
Initialize the asynchronous DNS library.
|
||||
|
||||
This function initializes support for non-blocking name resolution by
|
||||
calling evdns_resolv_conf_parse() on UNIX and
|
||||
evdns_config_windows_nameservers() on Windows.
|
||||
|
||||
@deprecated This function is deprecated because it always uses the current
|
||||
event base, and is easily confused by multiple calls to event_init(), and
|
||||
so is not safe for multithreaded use. Additionally, it allocates a global
|
||||
structure that only one thread can use. The replacement is
|
||||
evdns_base_new().
|
||||
|
||||
@return 0 if successful, or -1 if an error occurred
|
||||
@see evdns_shutdown()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_init(void);
|
||||
|
||||
struct evdns_base;
|
||||
/**
|
||||
Return the global evdns_base created by event_init() and used by the other
|
||||
deprecated functions.
|
||||
|
||||
@deprecated This function is deprecated because use of the global
|
||||
evdns_base is error-prone.
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
struct evdns_base *evdns_get_global_base(void);
|
||||
|
||||
/**
|
||||
Shut down the asynchronous DNS resolver and terminate all active requests.
|
||||
|
||||
If the 'fail_requests' option is enabled, all active requests will return
|
||||
an empty result with the error flag set to DNS_ERR_SHUTDOWN. Otherwise,
|
||||
the requests will be silently discarded.
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which evdns_base it applies to. The recommended
|
||||
function is evdns_base_shutdown().
|
||||
|
||||
@param fail_requests if zero, active requests will be aborted; if non-zero,
|
||||
active requests will return DNS_ERR_SHUTDOWN.
|
||||
@see evdns_init()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void evdns_shutdown(int fail_requests);
|
||||
|
||||
/**
|
||||
Add a nameserver.
|
||||
|
||||
The address should be an IPv4 address in network byte order.
|
||||
The type of address is chosen so that it matches in_addr.s_addr.
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which evdns_base it applies to. The recommended
|
||||
function is evdns_base_nameserver_add().
|
||||
|
||||
@param address an IP address in network byte order
|
||||
@return 0 if successful, or -1 if an error occurred
|
||||
@see evdns_nameserver_ip_add()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_nameserver_add(unsigned long int address);
|
||||
|
||||
/**
|
||||
Get the number of configured nameservers.
|
||||
|
||||
This returns the number of configured nameservers (not necessarily the
|
||||
number of running nameservers). This is useful for double-checking
|
||||
whether our calls to the various nameserver configuration functions
|
||||
have been successful.
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which evdns_base it applies to. The recommended
|
||||
function is evdns_base_count_nameservers().
|
||||
|
||||
@return the number of configured nameservers
|
||||
@see evdns_nameserver_add()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_count_nameservers(void);
|
||||
|
||||
/**
|
||||
Remove all configured nameservers, and suspend all pending resolves.
|
||||
|
||||
Resolves will not necessarily be re-attempted until evdns_resume() is called.
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which evdns_base it applies to. The recommended
|
||||
function is evdns_base_clear_nameservers_and_suspend().
|
||||
|
||||
@return 0 if successful, or -1 if an error occurred
|
||||
@see evdns_resume()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_clear_nameservers_and_suspend(void);
|
||||
|
||||
/**
|
||||
Resume normal operation and continue any suspended resolve requests.
|
||||
|
||||
Re-attempt resolves left in limbo after an earlier call to
|
||||
evdns_clear_nameservers_and_suspend().
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which evdns_base it applies to. The recommended
|
||||
function is evdns_base_resume().
|
||||
|
||||
@return 0 if successful, or -1 if an error occurred
|
||||
@see evdns_clear_nameservers_and_suspend()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_resume(void);
|
||||
|
||||
/**
|
||||
Add a nameserver.
|
||||
|
||||
This wraps the evdns_nameserver_add() function by parsing a string as an IP
|
||||
address and adds it as a nameserver.
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which evdns_base it applies to. The recommended
|
||||
function is evdns_base_nameserver_ip_add().
|
||||
|
||||
@return 0 if successful, or -1 if an error occurred
|
||||
@see evdns_nameserver_add()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_nameserver_ip_add(const char *ip_as_string);
|
||||
|
||||
/**
|
||||
Lookup an A record for a given name.
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which evdns_base it applies to. The recommended
|
||||
function is evdns_base_resolve_ipv4().
|
||||
|
||||
@param name a DNS hostname
|
||||
@param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
|
||||
@param callback a callback function to invoke when the request is completed
|
||||
@param ptr an argument to pass to the callback function
|
||||
@return 0 if successful, or -1 if an error occurred
|
||||
@see evdns_resolve_ipv6(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_resolve_ipv4(const char *name, int flags, evdns_callback_type callback, void *ptr);
|
||||
|
||||
/**
|
||||
Lookup an AAAA record for a given name.
|
||||
|
||||
@param name a DNS hostname
|
||||
@param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
|
||||
@param callback a callback function to invoke when the request is completed
|
||||
@param ptr an argument to pass to the callback function
|
||||
@return 0 if successful, or -1 if an error occurred
|
||||
@see evdns_resolve_ipv4(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_resolve_ipv6(const char *name, int flags, evdns_callback_type callback, void *ptr);
|
||||
|
||||
struct in_addr;
|
||||
struct in6_addr;
|
||||
|
||||
/**
|
||||
Lookup a PTR record for a given IP address.
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which evdns_base it applies to. The recommended
|
||||
function is evdns_base_resolve_reverse().
|
||||
|
||||
@param in an IPv4 address
|
||||
@param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
|
||||
@param callback a callback function to invoke when the request is completed
|
||||
@param ptr an argument to pass to the callback function
|
||||
@return 0 if successful, or -1 if an error occurred
|
||||
@see evdns_resolve_reverse_ipv6()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_resolve_reverse(const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr);
|
||||
|
||||
/**
|
||||
Lookup a PTR record for a given IPv6 address.
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which evdns_base it applies to. The recommended
|
||||
function is evdns_base_resolve_reverse_ipv6().
|
||||
|
||||
@param in an IPv6 address
|
||||
@param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
|
||||
@param callback a callback function to invoke when the request is completed
|
||||
@param ptr an argument to pass to the callback function
|
||||
@return 0 if successful, or -1 if an error occurred
|
||||
@see evdns_resolve_reverse_ipv6()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_resolve_reverse_ipv6(const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr);
|
||||
|
||||
/**
|
||||
Set the value of a configuration option.
|
||||
|
||||
The currently available configuration options are:
|
||||
|
||||
ndots, timeout, max-timeouts, max-inflight, and attempts
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which evdns_base it applies to. The recommended
|
||||
function is evdns_base_set_option().
|
||||
|
||||
@param option the name of the configuration option to be modified
|
||||
@param val the value to be set
|
||||
@param flags Ignored.
|
||||
@return 0 if successful, or -1 if an error occurred
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_set_option(const char *option, const char *val, int flags);
|
||||
|
||||
/**
|
||||
Parse a resolv.conf file.
|
||||
|
||||
The 'flags' parameter determines what information is parsed from the
|
||||
resolv.conf file. See the man page for resolv.conf for the format of this
|
||||
file.
|
||||
|
||||
The following directives are not parsed from the file: sortlist, rotate,
|
||||
no-check-names, inet6, debug.
|
||||
|
||||
If this function encounters an error, the possible return values are: 1 =
|
||||
failed to open file, 2 = failed to stat file, 3 = file too large, 4 = out of
|
||||
memory, 5 = short read from file, 6 = no nameservers listed in the file
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which evdns_base it applies to. The recommended
|
||||
function is evdns_base_resolv_conf_parse().
|
||||
|
||||
@param flags any of DNS_OPTION_NAMESERVERS|DNS_OPTION_SEARCH|DNS_OPTION_MISC|
|
||||
DNS_OPTIONS_ALL
|
||||
@param filename the path to the resolv.conf file
|
||||
@return 0 if successful, or various positive error codes if an error
|
||||
occurred (see above)
|
||||
@see resolv.conf(3), evdns_config_windows_nameservers()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_resolv_conf_parse(int flags, const char *const filename);
|
||||
|
||||
/**
|
||||
Clear the list of search domains.
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which evdns_base it applies to. The recommended
|
||||
function is evdns_base_search_clear().
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void evdns_search_clear(void);
|
||||
|
||||
/**
|
||||
Add a domain to the list of search domains
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which evdns_base it applies to. The recommended
|
||||
function is evdns_base_search_add().
|
||||
|
||||
@param domain the domain to be added to the search list
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void evdns_search_add(const char *domain);
|
||||
|
||||
/**
|
||||
Set the 'ndots' parameter for searches.
|
||||
|
||||
Sets the number of dots which, when found in a name, causes
|
||||
the first query to be without any search domain.
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which evdns_base it applies to. The recommended
|
||||
function is evdns_base_search_ndots_set().
|
||||
|
||||
@param ndots the new ndots parameter
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
void evdns_search_ndots_set(const int ndots);
|
||||
|
||||
/**
|
||||
As evdns_server_new_with_base.
|
||||
|
||||
@deprecated This function is deprecated because it does not allow the
|
||||
caller to specify which even_base it uses. The recommended
|
||||
function is evdns_add_server_port_with_base().
|
||||
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
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
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int evdns_config_windows_nameservers(void);
|
||||
#define EVDNS_CONFIG_WINDOWS_NAMESERVERS_IMPLEMENTED
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* EVENT2_EVENT_COMPAT_H_INCLUDED_ */
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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 EVENT2_DNS_STRUCT_H_INCLUDED_
|
||||
#define EVENT2_DNS_STRUCT_H_INCLUDED_
|
||||
|
||||
/** @file event2/dns_struct.h
|
||||
|
||||
Data structures for dns. Using these structures may hurt forward
|
||||
compatibility with later versions of Libevent: be careful!
|
||||
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <event2/event-config.h>
|
||||
#ifdef EVENT__HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
/* For int types. */
|
||||
#include <event2/util.h>
|
||||
|
||||
/*
|
||||
* Structures used to implement a DNS server.
|
||||
*/
|
||||
|
||||
struct evdns_server_request {
|
||||
int flags;
|
||||
int nquestions;
|
||||
struct evdns_server_question **questions;
|
||||
};
|
||||
struct evdns_server_question {
|
||||
int type;
|
||||
#ifdef __cplusplus
|
||||
int dns_question_class;
|
||||
#else
|
||||
/* You should refer to this field as "dns_question_class". The
|
||||
* name "class" works in C for backward compatibility, and will be
|
||||
* removed in a future version. (1.5 or later). */
|
||||
int class;
|
||||
#define dns_question_class class
|
||||
#endif
|
||||
char name[1];
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* EVENT2_DNS_STRUCT_H_INCLUDED_ */
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,230 @@
|
||||
/*
|
||||
* 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 EVENT2_EVENT_COMPAT_H_INCLUDED_
|
||||
#define EVENT2_EVENT_COMPAT_H_INCLUDED_
|
||||
|
||||
/** @file event2/event_compat.h
|
||||
|
||||
Potentially non-threadsafe versions of the functions in event.h: provided
|
||||
only for backwards compatibility.
|
||||
|
||||
In the oldest versions of Libevent, event_base was not a first-class
|
||||
structure. Instead, there was a single event base that every function
|
||||
manipulated. Later, when separate event bases were added, the old functions
|
||||
that didn't take an event_base argument needed to work by manipulating the
|
||||
"current" event base. This could lead to thread-safety issues, and obscure,
|
||||
hard-to-diagnose bugs.
|
||||
|
||||
@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
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
/* For int types. */
|
||||
#include <event2/util.h>
|
||||
|
||||
/**
|
||||
Initialize the event API.
|
||||
|
||||
The event API needs to be initialized with event_init() before it can be
|
||||
used. Sets the global current base that gets used for events that have no
|
||||
base associated with them.
|
||||
|
||||
@deprecated This function is deprecated because it replaces the "current"
|
||||
event_base, and is totally unsafe for multithreaded use. The replacement
|
||||
is event_base_new().
|
||||
|
||||
@see event_base_set(), event_base_new()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
struct event_base *event_init(void);
|
||||
|
||||
/**
|
||||
Loop to process events.
|
||||
|
||||
Like event_base_dispatch(), but uses the "current" base.
|
||||
|
||||
@deprecated This function is deprecated because it is easily confused by
|
||||
multiple calls to event_init(), and because it is not safe for
|
||||
multithreaded use. The replacement is event_base_dispatch().
|
||||
|
||||
@see event_base_dispatch(), event_init()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int event_dispatch(void);
|
||||
|
||||
/**
|
||||
Handle events.
|
||||
|
||||
This function behaves like event_base_loop(), but uses the "current" base
|
||||
|
||||
@deprecated This function is deprecated because it uses the event base from
|
||||
the last call to event_init, and is therefore not safe for multithreaded
|
||||
use. The replacement is event_base_loop().
|
||||
|
||||
@see event_base_loop(), event_init()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int event_loop(int);
|
||||
|
||||
|
||||
/**
|
||||
Exit the event loop after the specified time.
|
||||
|
||||
This function behaves like event_base_loopexit(), except that it uses the
|
||||
"current" base.
|
||||
|
||||
@deprecated This function is deprecated because it uses the event base from
|
||||
the last call to event_init, and is therefore not safe for multithreaded
|
||||
use. The replacement is event_base_loopexit().
|
||||
|
||||
@see event_init, event_base_loopexit()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int event_loopexit(const struct timeval *);
|
||||
|
||||
|
||||
/**
|
||||
Abort the active event_loop() immediately.
|
||||
|
||||
This function behaves like event_base_loopbreakt(), except that it uses the
|
||||
"current" base.
|
||||
|
||||
@deprecated This function is deprecated because it uses the event base from
|
||||
the last call to event_init, and is therefore not safe for multithreaded
|
||||
use. The replacement is event_base_loopbreak().
|
||||
|
||||
@see event_base_loopbreak(), event_init()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int event_loopbreak(void);
|
||||
|
||||
/**
|
||||
Schedule a one-time event to occur.
|
||||
|
||||
@deprecated This function is obsolete, and has been replaced by
|
||||
event_base_once(). Its use is deprecated because it relies on the
|
||||
"current" base configured by event_init().
|
||||
|
||||
@see event_base_once()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int event_once(evutil_socket_t , short,
|
||||
void (*)(evutil_socket_t, short, void *), void *, const struct timeval *);
|
||||
|
||||
|
||||
/**
|
||||
Get the kernel event notification mechanism used by Libevent.
|
||||
|
||||
@deprecated This function is obsolete, and has been replaced by
|
||||
event_base_get_method(). Its use is deprecated because it relies on the
|
||||
"current" base configured by event_init().
|
||||
|
||||
@see event_base_get_method()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
const char *event_get_method(void);
|
||||
|
||||
|
||||
/**
|
||||
Set the number of different event priorities.
|
||||
|
||||
@deprecated This function is deprecated because it is easily confused by
|
||||
multiple calls to event_init(), and because it is not safe for
|
||||
multithreaded use. The replacement is event_base_priority_init().
|
||||
|
||||
@see event_base_priority_init()
|
||||
*/
|
||||
EVENT2_EXPORT_SYMBOL
|
||||
int event_priority_init(int);
|
||||
|
||||
/**
|
||||
Prepare an event structure to be added.
|
||||
|
||||
@deprecated event_set() is not recommended for new code, because it requires
|
||||
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))
|
||||
#define evsignal_set(ev, x, cb, arg) \
|
||||
event_set((ev), (x), EV_SIGNAL|EV_PERSIST, (cb), (arg))
|
||||
|
||||
|
||||
/**
|
||||
@name timeout_* macros
|
||||
|
||||
@deprecated These macros are deprecated because their naming is inconsistent
|
||||
with the rest of Libevent. Use the evtimer_* macros instead.
|
||||
@{
|
||||
*/
|
||||
#define timeout_add(ev, tv) event_add((ev), (tv))
|
||||
#define timeout_set(ev, cb, arg) event_set((ev), -1, 0, (cb), (arg))
|
||||
#define timeout_del(ev) event_del(ev)
|
||||
#define timeout_pending(ev, tv) event_pending((ev), EV_TIMEOUT, (tv))
|
||||
#define timeout_initialized(ev) event_initialized(ev)
|
||||
/**@}*/
|
||||
|
||||
/**
|
||||
@name signal_* macros
|
||||
|
||||
@deprecated These macros are deprecated because their naming is inconsistent
|
||||
with the rest of Libevent. Use the evsignal_* macros instead.
|
||||
@{
|
||||
*/
|
||||
#define signal_add(ev, tv) event_add((ev), (tv))
|
||||
#define signal_set(ev, x, cb, arg) \
|
||||
event_set((ev), (x), EV_SIGNAL|EV_PERSIST, (cb), (arg))
|
||||
#define signal_del(ev) event_del(ev)
|
||||
#define signal_pending(ev, tv) event_pending((ev), EV_SIGNAL, (tv))
|
||||
#define signal_initialized(ev) event_initialized(ev)
|
||||
/**@}*/
|
||||
|
||||
#ifndef EVENT_FD
|
||||
/* These macros are obsolete; use event_get_fd and event_get_signal instead. */
|
||||
#define EVENT_FD(ev) ((int)event_get_fd(ev))
|
||||
#define EVENT_SIGNAL(ev) event_get_signal(ev)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* EVENT2_EVENT_COMPAT_H_INCLUDED_ */
|
||||
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* 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 EVENT2_EVENT_STRUCT_H_INCLUDED_
|
||||
#define EVENT2_EVENT_STRUCT_H_INCLUDED_
|
||||
|
||||
/** @file event2/event_struct.h
|
||||
|
||||
Structures used by event.h. Using these structures directly WILL harm
|
||||
forward compatibility: be careful.
|
||||
|
||||
No field declared in this file should be used directly in user code. Except
|
||||
for historical reasons, these fields would not be exposed at all.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <event2/event-config.h>
|
||||
#ifdef EVENT__HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
#ifdef EVENT__HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
/* For int types. */
|
||||
#include <event2/util.h>
|
||||
|
||||
/* 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_ACTIVE_LATER 0x20
|
||||
#define EVLIST_FINALIZING 0x40
|
||||
#define EVLIST_INIT 0x80
|
||||
|
||||
#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 TAILQ_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *tqe_next; /* next element */ \
|
||||
struct type **tqe_prev; /* address of previous next element */ \
|
||||
}
|
||||
#endif /* !TAILQ_ENTRY */
|
||||
|
||||
#ifndef TAILQ_HEAD
|
||||
#define EVENT_DEFINED_TQHEAD_
|
||||
#define TAILQ_HEAD(name, type) \
|
||||
struct name { \
|
||||
struct type *tqh_first; \
|
||||
struct type **tqh_last; \
|
||||
}
|
||||
#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 {
|
||||
struct event_callback ev_evcallback;
|
||||
|
||||
/* for managing timeouts */
|
||||
union {
|
||||
TAILQ_ENTRY(event) ev_next_with_common_timeout;
|
||||
int min_heap_idx;
|
||||
} ev_timeout_pos;
|
||||
evutil_socket_t ev_fd;
|
||||
|
||||
struct event_base *ev_base;
|
||||
|
||||
union {
|
||||
/* used for io events */
|
||||
struct {
|
||||
LIST_ENTRY (event) ev_io_next;
|
||||
struct timeval ev_timeout;
|
||||
} ev_io;
|
||||
|
||||
/* used by signal events */
|
||||
struct {
|
||||
LIST_ENTRY (event) ev_signal_next;
|
||||
short ev_ncalls;
|
||||
/* Allows deletes in callback */
|
||||
short *ev_pncalls;
|
||||
} ev_signal;
|
||||
} ev_;
|
||||
|
||||
short ev_events;
|
||||
short ev_res; /* result passed to event callback */
|
||||
struct timeval ev_timeout;
|
||||
};
|
||||
|
||||
TAILQ_HEAD (event_list, event);
|
||||
|
||||
#ifdef EVENT_DEFINED_TQENTRY_
|
||||
#undef TAILQ_ENTRY
|
||||
#endif
|
||||
|
||||
#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_INCLUDED_ */
|
||||
File diff suppressed because it is too large.
Load diff
Loaded 100 of 1032 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user