From fad6c713c26d6e4f2dd867db4d341c7abe0fcbda Mon Sep 17 00:00:00 2001 From: Yossi Gottlieb Date: Tue, 30 May 2023 22:23:45 +0300 Subject: [PATCH] Squashed 'deps/hiredis/' changes from f8de9a4bd..b6a052fe0 b6a052fe0 Helper for setting TCP_USER_TIMEOUT socket option (#1188) 3fa9b6944 Add RedisModule adapter (#1182) d13c091e9 Fix wincrypt symbols conflict 5d84c8cfd Add a test ensuring we don't clobber connection error. 3f95fcdae Don't attempt to set a timeout if we are in an error state. aacb84b8d Fix typo in makefile. 563b062e3 Accept -nan per the RESP3 spec recommendation. 04c1b5b02 Fix colliding option values 4ca8e73f6 Rework searching for openssl cd208812f Attempt to find the correct path for openssl. 011f7093c Allow specifying the keepalive interval e9243d4f7 Cmake static or shared (#1160) 1cbd5bc76 Write a version file for the CMake package (#1165) 6f5bae8c6 fix typo acd09461d CMakeLists.txt: respect BUILD_SHARED_LIBS 97fcf0fd1 Add sdevent adapter ccff093bc Bump dev version for the next release cycle. c14775b4e Prepare for v1.1.0 GA f0bdf8405 Add support for nan in RESP3 double (#1133) 991b0b0b3 Add an example that calls redisCommandArgv (#1140) a36686f84 CI updates (#1139) 8ad4985e9 fix flag reference 7583ebb1b Make freeing a NULL redisAsyncContext a no op. 2c53dea7f Update version in dev branch. f063370ed Prepare for v1.1.0-rc1 2b069573a CI fixes in preparation of release e1e9eb40d Add author information to release-drafter template. afc29ee1a Update for mingw cross compile ceb8a8815 fixed cpp build error with adapters/libhv.h 3b15a04b5 Fixup of PR734: Coverage of hiredis.c (#1124) c245df9fb CMake corrections for building on Windows (#1122) 9c338a598 Fix PUSH handler tests for Redis >= 7.0.5 6d5c3ee74 Install on windows fixes (#1117) 68b29e1ad Add timeout support to libhv adapter. (#1109) 722e3409c Additional include directory given by pkg-config (#1118) bd9ccb8c4 Use __attribute__ when building with clang on windows 5392adc26 set default SSL certificate directory 560e66486 Minor refactor d756f68a5 Add libhv example to our standard Makefile a66916719 Add adapters/libhv 855b48a81 Fix pkgconfig for hiredis_ssl 79ae5ffc6 Fix protocol error (#1106) 61b5b299f Use a windows specific keepalive function. (#1104) fce8abc1c Introduce .close method for redisContextFuncs cfb6ca881 Add REDIS_OPT_PREFER_UNSPEC (#1101) cc7c35ce6 Update documentation to explain redisConnectWithOptions. bc8d837b7 fix heap-buffer-overflow (#957) ca4a0e850 uvadapter: reduce number of uv_poll_start calls 35d398c90 Fix cmake config path on Linux. CMake config files were installed to `/usr/local/share/hiredis`, which is not recognizable by `find_package()`. I'm not sure why it was set that way. Given the commit introducing it is for Windows, I keep that behavior consistent there, but fix the rest. 10c78c6e1 Add possibility to prefer IPv6, IPv4 or unspecified 1abe0c828 fuzzer: No alloc in redisFormatCommand() when fail 329eaf9ba Fix heap-buffer-overflow issue in redisvFormatCommad eaae7321c Polling adapter requires sockcompat.h 0a5fa3dde Regression test for off-by-one parsing error 9e174e8f7 Add do while(0) protection for macros 4ad99c69a Rework asSleep to be a generic millisleep function. 75cb6c1ea Do store command timeout in the context for redisSetTimeout (#593) c57cad658 CMake: remove dict.c form hiredis_sources 8491a65a9 Add Github Actions CI workflow for hiredis: Arm, Arm64, 386, windows. (#943) 77e4f09ea Merge pull request #964 from afcidk/fix-createDoubleObject 9219f7e7c Merge pull request #901 from devnexen/illumos_test_fix 810cc6104 Merge pull request #905 from sundb/master df8b74d69 Merge pull request #1091 from redis/ssl-error-ub-fix 0ed6cdec3 Fix some undefined behaviour 507a6dcaa Merge pull request #1090 from Nordix/subscribe-oom-error b044eaa6a Copy error to redisAsyncContext when finding subscribe cb e0200b797 Merge pull request #1087 from redis/const-and-non-const-callback 6a3e96ad2 Maintain backward compatibiliy withour onConnect callback. e7afd998f Merge pull request #1079 from SukkaW/drop-macos-10.15-runner 17c8fe079 Merge pull request #931 from kristjanvalur/pr2 b808c0c20 Merge pull request #1083 from chayim/ck-drafter 367a82bf0 Merge pull request #1085 from stanhu/ssl-improve-options-setting 71119a71d Make it possible to set SSL verify mode dd7979ac1 Merge pull request #1084 from stanhu/sh-improve-ssl-docs c71116178 Improve example for SSL initialization in README.md 5c9b6b571 Release drafter a606ccf2a CI: use recommended `vmactions/freebsd-vm@v0` 0865c115b Merge pull request #1080 from Nordix/readme-corrections f6cee7142 Fix README typos 06be7ff31 Merge pull request #1050 from smmir-cent/fix-cmake-version 7dd833d54 CI: bump macos runner version f69fac769 Drop `const` on redisAsyncContext in redisConnectCallback Since the callback is now re-entrant, it can call apis such as redisAsyncDisconnect() 005d7edeb Support calling redisAsyncDisconnect from the onConnected callback, by deferring context deletion 6ed060920 Add async regression test for issue #931 eaa2a7ee7 Merge pull request #932 from kristjanvalur/pr3 2ccef30f3 Add regression test for issue #945 4b901d44a Initial async tests 31c91408e Polling adapter and example 8a15f4d65 Merge pull request #1057 from orgads/static-name 902dd047f Merge pull request #1054 from kristjanvalur/pr08 c78d0926b Merge pull request #1074 from michael-grunder/kristjanvalur-pr4 2b115d56c Whitespace 1343988ce Fix typos 47b57aa24 Add some documentation on connect/disconnect callbacks and command callbacks a890d9ce2 Merge pull request #1073 from michael-grunder/kristjanvalur-pr1 f246ee433 Whitespace, style 94c1985bd Use correct type for getsockopt() 5e002bc21 Support failed async connects on windows. 5d68ad2f4 Merge pull request #1072 from michael-grunder/fix-redis7-unit-tests f4b6ed289 Fix tests so they work for Redis 7.0 95a0c1283 Merge pull request #1058 from orgads/win64 eedb37a65 Fix warnings on Win64 47c3ecefc Merge pull request #1062 from yossigo/fix-push-notification-order e23d91c97 Merge pull request #1061 from yossigo/update-redis-apt 34211ad54 Merge pull request #1063 from redis/fix-windows-tests 9957af7e3 Whitelist hiredis repo path in cygwin b455b3381 Handle push notifications before or after reply. aed9ce446 Use official repository for redis package. d7683f35a Merge pull request #1047 from Nordix/unsubscribe-handling 7c44a9d7e Merge pull request #1045 from Nordix/sds-updates dd4bf9783 Use the same name for static and shared libraries ff57c18b9 Embed debug information in windows static lib, rather than create a .pdb file 8310ad4f5 fix cmake version 7123b87f6 Handle any pipelined unsubscribe in async b6fb548fc Ignore pubsub replies without a channel/pattern 00b82683b Handle overflows as errors instead of asserting 64062a1d4 Catch size_t overflows in sds.c 066c6de79 Use size_t/long to avoid truncation c6657ef65 Merge branch 'redis:master' into master 50cdcab49 Fix potential fault at createDoubleObject fd033e983 Remove semicolon after do-while in _EL_CLEANUP 664c415e7 Illumos test fixes, error message difference fot bad hostname test. git-subtree-dir: deps/hiredis git-subtree-split: b6a052fe0959dae69e16b9d74449faeb1b70dbe1 --- .github/release-drafter-config.yml | 49 +++ .github/workflows/build.yml | 74 ++--- .github/workflows/release-drafter.yml | 19 ++ .github/workflows/test.yml | 100 ++++++ CHANGELOG.md | 165 ++++++++++ CMakeLists.txt | 88 +++--- Makefile | 54 ++-- README.md | 186 ++++++++++-- adapters/libhv.h | 123 ++++++++ adapters/libsdevent.h | 177 +++++++++++ adapters/libuv.h | 8 + adapters/poll.h | 197 ++++++++++++ adapters/redismoduleapi.h | 144 +++++++++ async.c | 227 ++++++++++---- async.h | 5 + async_private.h | 2 +- examples/CMakeLists.txt | 12 + examples/example-libevent-ssl.c | 2 +- examples/example-libhv.c | 70 +++++ examples/example-libsdevent.c | 86 ++++++ examples/example-poll.c | 62 ++++ examples/example-redismoduleapi.c | 101 +++++++ examples/example-ssl.c | 7 +- examples/example.c | 51 ++++ fuzzing/format_command_fuzzer.c | 5 +- hiredis-config.cmake.in | 4 +- hiredis.c | 49 ++- hiredis.h | 68 +++-- hiredis.pc.in | 2 +- hiredis_ssl-config.cmake.in | 3 + hiredis_ssl.h | 36 ++- hiredis_ssl.pc.in | 3 +- net.c | 95 ++++-- net.h | 1 + read.c | 13 +- sds.c | 15 +- sds.h | 4 +- sockcompat.c | 36 ++- sockcompat.h | 3 + ssl.c | 38 ++- test.c | 418 ++++++++++++++++++++++++-- test.sh | 52 +++- 42 files changed, 2556 insertions(+), 298 deletions(-) create mode 100644 .github/release-drafter-config.yml create mode 100644 .github/workflows/release-drafter.yml create mode 100644 .github/workflows/test.yml create mode 100644 adapters/libhv.h create mode 100644 adapters/libsdevent.h create mode 100644 adapters/poll.h create mode 100644 adapters/redismoduleapi.h create mode 100644 examples/example-libhv.c create mode 100644 examples/example-libsdevent.c create mode 100644 examples/example-poll.c create mode 100644 examples/example-redismoduleapi.c diff --git a/.github/release-drafter-config.yml b/.github/release-drafter-config.yml new file mode 100644 index 000000000..81afa9f97 --- /dev/null +++ b/.github/release-drafter-config.yml @@ -0,0 +1,49 @@ +name-template: '$NEXT_MAJOR_VERSION' +tag-template: 'v$NEXT_MAJOR_VERSION' +autolabeler: + - label: 'maintenance' + files: + - '*.md' + - '.github/*' + - label: 'bug' + branch: + - '/bug-.+' + - label: 'maintenance' + branch: + - '/maintenance-.+' + - label: 'feature' + branch: + - '/feature-.+' +categories: + - title: 'Breaking Changes' + labels: + - 'breakingchange' + + - title: '๐Ÿงช Experimental Features' + labels: + - 'experimental' + - title: '๐Ÿš€ New Features' + labels: + - 'feature' + - 'enhancement' + - title: '๐Ÿ› Bug Fixes' + labels: + - 'fix' + - 'bugfix' + - 'bug' + - 'BUG' + - title: '๐Ÿงฐ Maintenance' + label: 'maintenance' +change-template: '- $TITLE @$AUTHOR (#$NUMBER)' +exclude-labels: + - 'skip-changelog' +template: | + ## Changes + + $CHANGES + + ## Contributors + We'd like to thank all the contributors who worked on this release! + + $CONTRIBUTORS + diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 362bc77b7..1a1ef5153 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,24 +6,21 @@ jobs: name: Ubuntu runs-on: ubuntu-latest steps: - - name: Checkout code - uses: actions/checkout@v2 - with: - repository: ${{ env.GITHUB_REPOSITORY }} - ref: ${{ env.GITHUB_HEAD_REF }} + - uses: actions/checkout@v3 - name: Install dependencies run: | - sudo add-apt-repository -y ppa:chris-lea/redis-server + curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg + echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list sudo apt-get update sudo apt-get install -y redis-server valgrind libevent-dev - + - name: Build using cmake - env: + env: EXTRA_CMAKE_OPTS: -DENABLE_EXAMPLES:BOOL=ON -DENABLE_SSL:BOOL=ON -DENABLE_SSL_TESTS:BOOL=ON -DENABLE_ASYNC_TESTS:BOOL=ON CFLAGS: -Werror CXXFLAGS: -Werror - run: mkdir build-ubuntu && cd build-ubuntu && cmake .. + run: mkdir build && cd build && cmake .. && make - name: Build using makefile run: USE_SSL=1 TEST_ASYNC=1 make @@ -45,11 +42,7 @@ jobs: runs-on: ubuntu-latest container: centos:7 steps: - - name: Checkout code - uses: actions/checkout@v2 - with: - repository: ${{ env.GITHUB_REPOSITORY }} - ref: ${{ env.GITHUB_HEAD_REF }} + - uses: actions/checkout@v3 - name: Install dependencies run: | @@ -58,11 +51,11 @@ jobs: yum -y install gcc gcc-c++ make openssl openssl-devel cmake3 valgrind libevent-devel - name: Build using cmake - env: + env: EXTRA_CMAKE_OPTS: -DENABLE_EXAMPLES:BOOL=ON -DENABLE_SSL:BOOL=ON -DENABLE_SSL_TESTS:BOOL=ON -DENABLE_ASYNC_TESTS:BOOL=ON CFLAGS: -Werror CXXFLAGS: -Werror - run: mkdir build-centos7 && cd build-centos7 && cmake3 .. + run: mkdir build && cd build && cmake3 .. && make - name: Build using Makefile run: USE_SSL=1 TEST_ASYNC=1 make @@ -85,25 +78,22 @@ jobs: runs-on: ubuntu-latest container: rockylinux:8 steps: - - name: Checkout code - uses: actions/checkout@v2 - with: - repository: ${{ env.GITHUB_REPOSITORY }} - ref: ${{ env.GITHUB_HEAD_REF }} + - uses: actions/checkout@v3 - name: Install dependencies run: | + dnf -y upgrade --refresh dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm dnf -y module install redis:remi-6.0 dnf -y group install "Development Tools" dnf -y install openssl-devel cmake valgrind libevent-devel - name: Build using cmake - env: + env: EXTRA_CMAKE_OPTS: -DENABLE_EXAMPLES:BOOL=ON -DENABLE_SSL:BOOL=ON -DENABLE_SSL_TESTS:BOOL=ON -DENABLE_ASYNC_TESTS:BOOL=ON CFLAGS: -Werror CXXFLAGS: -Werror - run: mkdir build-centos8 && cd build-centos8 && cmake .. + run: mkdir build && cd build && cmake .. && make - name: Build using Makefile run: USE_SSL=1 TEST_ASYNC=1 make @@ -122,17 +112,13 @@ jobs: run: $GITHUB_WORKSPACE/test.sh freebsd: - runs-on: macos-10.15 + runs-on: macos-12 name: FreeBSD steps: - - name: Checkout code - uses: actions/checkout@v2 - with: - repository: ${{ env.GITHUB_REPOSITORY }} - ref: ${{ env.GITHUB_HEAD_REF }} + - uses: actions/checkout@v3 - name: Build in FreeBSD - uses: vmactions/freebsd-vm@v0.1.5 + uses: vmactions/freebsd-vm@v0 with: prepare: pkg install -y gmake cmake run: | @@ -143,15 +129,12 @@ jobs: name: macOS runs-on: macos-latest steps: - - name: Checkout code - uses: actions/checkout@v2 - with: - repository: ${{ env.GITHUB_REPOSITORY }} - ref: ${{ env.GITHUB_HEAD_REF }} + - uses: actions/checkout@v3 - name: Install dependencies run: | - brew install openssl redis + brew install openssl redis@6.2 + brew link redis@6.2 --force - name: Build hiredis run: USE_SSL=1 make @@ -165,11 +148,7 @@ jobs: name: Windows runs-on: windows-latest steps: - - name: Checkout code - uses: actions/checkout@v2 - with: - repository: ${{ env.GITHUB_REPOSITORY }} - ref: ${{ env.GITHUB_HEAD_REF }} + - uses: actions/checkout@v3 - name: Install dependencies run: | @@ -186,20 +165,13 @@ jobs: run: | ./build/hiredis-test.exe - - name: Setup cygwin - uses: egor-tensin/setup-cygwin@v3 + - name: Install Cygwin Action + uses: cygwin/cygwin-install-action@v2 with: - platform: x64 packages: make git gcc-core - name: Build in cygwin env: HIREDIS_PATH: ${{ github.workspace }} run: | - build_hiredis() { - cd $(cygpath -u $HIREDIS_PATH) - git clean -xfd - make - } - build_hiredis - shell: C:\tools\cygwin\bin\bash.exe --login --norc -eo pipefail -o igncr '{0}' + make clean && make diff --git a/.github/workflows/release-drafter.yml b/.github/workflows/release-drafter.yml new file mode 100644 index 000000000..ec2d88bf6 --- /dev/null +++ b/.github/workflows/release-drafter.yml @@ -0,0 +1,19 @@ +name: Release Drafter + +on: + push: + # branches to consider in the event; optional, defaults to all + branches: + - master + +jobs: + update_release_draft: + runs-on: ubuntu-latest + steps: + # Drafts your next Release notes as Pull Requests are merged into "master" + - uses: release-drafter/release-drafter@v5 + with: + # (Optional) specify config name to use, relative to .github/. Default: release-drafter.yml + config-name: release-drafter-config.yml + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 000000000..7812af6f7 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,100 @@ +name: C/C++ CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + full-build: + name: Build all, plus default examples, run tests against redis + runs-on: ubuntu-latest + env: + # the docker image used by the test.sh + REDIS_DOCKER: redis:alpine + + steps: + - name: Install prerequisites + run: sudo apt-get update && sudo apt-get install -y libev-dev libevent-dev libglib2.0-dev libssl-dev valgrind + - uses: actions/checkout@v3 + - name: Run make + run: make all examples + - name: Run unittests + run: make check + - name: Run tests with valgrind + env: + TEST_PREFIX: valgrind --error-exitcode=100 + SKIPS_ARG: --skip-throughput + run: make check + + build-32-bit: + name: Build and test minimal 32 bit linux + runs-on: ubuntu-latest + steps: + - name: Install prerequisites + run: sudo apt-get update && sudo apt-get install gcc-multilib + - uses: actions/checkout@v3 + - name: Run make + run: make all + env: + PLATFORM_FLAGS: -m32 + - name: Run unittests + env: + REDIS_DOCKER: redis:alpine + run: make check + + build-arm: + name: Cross-compile and test arm linux with Qemu + runs-on: ubuntu-latest + strategy: + matrix: + include: + - name: arm + toolset: arm-linux-gnueabi + emulator: qemu-arm + - name: aarch64 + toolset: aarch64-linux-gnu + emulator: qemu-aarch64 + + steps: + - name: Install qemu + if: matrix.emulator + run: sudo apt-get install -y qemu-user + - name: Install platform toolset + if: matrix.toolset + run: sudo apt-get install -y gcc-${{matrix.toolset}} + - uses: actions/checkout@v3 + - name: Run make + run: make all + env: + CC: ${{matrix.toolset}}-gcc + AR: ${{matrix.toolset}}-ar + - name: Run unittests + env: + REDIS_DOCKER: redis:alpine + TEST_PREFIX: ${{matrix.emulator}} -L /usr/${{matrix.toolset}}/ + run: make check + + build-windows: + name: Build and test on windows 64 bit Intel + runs-on: windows-latest + steps: + - uses: microsoft/setup-msbuild@v1.0.2 + - uses: actions/checkout@v3 + - name: Run CMake (shared lib) + run: cmake -Wno-dev CMakeLists.txt + - name: Build hiredis (shared lib) + run: MSBuild hiredis.vcxproj /p:Configuration=Debug + - name: Run CMake (static lib) + run: cmake -Wno-dev CMakeLists.txt -DBUILD_SHARED_LIBS=OFF + - name: Build hiredis (static lib) + run: MSBuild hiredis.vcxproj /p:Configuration=Debug + - name: Build hiredis-test + run: MSBuild hiredis-test.vcxproj /p:Configuration=Debug + # use memurai, redis compatible server, since it is easy to install. Can't + # install official redis containers on the windows runner + - name: Install Memurai redis server + run: choco install -y memurai-developer.install + - name: Run tests + run: Debug\hiredis-test.exe diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a2bc314a..a2e065b2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,168 @@ +## [1.1.0](https://github.com/redis/hiredis/tree/v1.1.0) - (2022-11-15) + +Announcing Hiredis v1.1.0 GA with better SSL convenience, new async adapters and a great many bug fixes. + +**NOTE**: Hiredis can now return `nan` in addition to `-inf` and `inf` when returning a `REDIS_REPLY_DOUBLE`. + +## ๐Ÿ› Bug Fixes + +- Add support for nan in RESP3 double [@filipecosta90](https://github.com/filipecosta90) + ([\#1133](https://github.com/redis/hiredis/pull/1133)) + +## ๐Ÿงฐ Maintenance + +- Add an example that calls redisCommandArgv [@michael-grunder](https://github.com/michael-grunder) + ([\#1140](https://github.com/redis/hiredis/pull/1140)) +- fix flag reference [@pata00](https://github.com/pata00) ([\#1136](https://github.com/redis/hiredis/pull/1136)) +- Make freeing a NULL redisAsyncContext a no op. [@michael-grunder](https://github.com/michael-grunder) + ([\#1135](https://github.com/redis/hiredis/pull/1135)) +- CI updates ([@bjosv](https://github.com/redis/bjosv) ([\#1139](https://github.com/redis/hiredis/pull/1139)) + + +## Contributors +We'd like to thank all the contributors who worked on this release! + + + + + + +## [1.1.0-rc1](https://github.com/redis/hiredis/tree/v1.1.0-rc1) - (2022-11-06) + +Announcing Hiredis v1.1.0-rc1, with better SSL convenience, new async adapters, and a great many bug fixes. + +## ๐Ÿš€ New Features + +- Add possibility to prefer IPv6, IPv4 or unspecified [@zuiderkwast](https://github.com/zuiderkwast) + ([\#1096](https://github.com/redis/hiredis/pull/1096)) +- Add adapters/libhv [@ithewei](https://github.com/ithewei) ([\#904](https://github.com/redis/hiredis/pull/904)) +- Add timeout support to libhv adapter. [@michael-grunder](https://github.com/michael-grunder) ([\#1109](https://github.com/redis/hiredis/pull/1109)) +- set default SSL verification path [@adobeturchenko](https://github.com/adobeturchenko) ([\#928](https://github.com/redis/hiredis/pull/928)) +- Introduce .close method for redisContextFuncs [@pizhenwei](https://github.com/pizhenwei) ([\#1094](https://github.com/redis/hiredis/pull/1094)) +- Make it possible to set SSL verify mode [@stanhu](https://github.com/stanhu) ([\#1085](https://github.com/redis/hiredis/pull/1085)) +- Polling adapter and example [@kristjanvalur](https://github.com/kristjanvalur) ([\#932](https://github.com/redis/hiredis/pull/932)) +- Unsubscribe handling in async [@bjosv](https://github.com/bjosv) ([\#1047](https://github.com/redis/hiredis/pull/1047)) +- Add timeout support for libuv adapter [@MichaelSuen-thePointer](https://github.com/@MichaelSuenthePointer) ([\#1016](https://github.com/redis/hiredis/pull/1016)) + +## ๐Ÿ› Bug Fixes + +- Update for MinGW cross compile [@bit0fun](https://github.com/bit0fun) ([\#1127](https://github.com/redis/hiredis/pull/1127)) +- fixed CPP build error with adapters/libhv.h [@mtdxc](https://github.com/mtdxc) ([\#1125](https://github.com/redis/hiredis/pull/1125)) +- Fix protocol error + [@michael-grunder](https://github.com/michael-grunder), + [@mtuleika-appcast](https://github.com/mtuleika-appcast) ([\#1106](https://github.com/redis/hiredis/pull/1106)) +- Use a windows specific keepalive function. [@michael-grunder](https://github.com/michael-grunder) ([\#1104](https://github.com/redis/hiredis/pull/1104)) +- Fix CMake config path on Linux. [@xkszltl](https://github.com/xkszltl) ([\#989](https://github.com/redis/hiredis/pull/989)) +- Fix potential fault at createDoubleObject [@afcidk](https://github.com/afcidk) ([\#964](https://github.com/redis/hiredis/pull/964)) +- Fix some undefined behavior [@jengab](https://github.com/jengab) ([\#1091](https://github.com/redis/hiredis/pull/1091)) +- Copy OOM errors to redisAsyncContext when finding subscribe callback [@bjosv](https://github.com/bjosv) ([\#1090](https://github.com/redis/hiredis/pull/1090)) +- Maintain backward compatibility with our onConnect callback. [@michael-grunder](https://github.com/michael-grunder) ([\#1087](https://github.com/redis/hiredis/pull/1087)) +- Fix PUSH handler tests for Redis >= 7.0.5 [@michael-grunder](https://github.com/michael-grunder) ([\#1121](https://github.com/redis/hiredis/pull/1121)) +- fix heap-buffer-overflow [@zhangtaoXT5](https://github.com/zhangtaoXT5) ([\#957](https://github.com/redis/hiredis/pull/957)) +- Fix heap-buffer-overflow issue in redisvFormatCommad [@bjosv](https://github.com/bjosv) ([\#1097](https://github.com/redis/hiredis/pull/1097)) +- Polling adapter requires sockcompat.h [@michael-grunder](https://github.com/michael-grunder) ([\#1095](https://github.com/redis/hiredis/pull/1095)) +- Illumos test fixes, error message difference for bad hostname test. [@devnexen](https://github.com/devnexen) ([\#901](https://github.com/redis/hiredis/pull/901)) +- Remove semicolon after do-while in \_EL\_CLEANUP [@sundb](https://github.com/sundb) ([\#905](https://github.com/redis/hiredis/pull/905)) +- Stability: Support calling redisAsyncCommand and redisAsyncDisconnect from the onConnected callback [@kristjanvalur](https://github.com/kristjanvalur) + ([\#931](https://github.com/redis/hiredis/pull/931)) +- Fix async connect on Windows [@kristjanvalur](https://github.com/kristjanvalur) ([\#1073](https://github.com/redis/hiredis/pull/1073)) +- Fix tests so they work for Redis 7.0 [@michael-grunder](https://github.com/michael-grunder) ([\#1072](https://github.com/redis/hiredis/pull/1072)) +- Fix warnings on Win64 [@orgads](https://github.com/orgads) ([\#1058](https://github.com/redis/hiredis/pull/1058)) +- Handle push notifications before or after reply. [@yossigo](https://github.com/yossigo) ([\#1062](https://github.com/redis/hiredis/pull/1062)) +- Update hiredis sds with improvements found in redis [@bjosv](https://github.com/bjosv) ([\#1045](https://github.com/redis/hiredis/pull/1045)) +- Avoid incorrect call to the previous reply's callback [@bjosv](https://github.com/bjosv) ([\#1040](https://github.com/redis/hiredis/pull/1040)) +- fix building on AIX and SunOS [\#1031](https://github.com/redis/hiredis/pull/1031) ([@scddev](https://github.com/scddev)) +- Allow sending commands after sending an unsubscribe [@bjosv](https://github.com/bjosv) ([\#1036](https://github.com/redis/hiredis/pull/1036)) +- Correction for command timeout during pubsub [@bjosv](https://github.com/bjosv) ([\#1038](https://github.com/redis/hiredis/pull/1038)) +- Fix adapters/libevent.h compilation for 64-bit Windows [@pbtummillo](https://github.com/pbtummillo) ([\#937](https://github.com/redis/hiredis/pull/937)) +- Fix integer overflow when format command larger than 4GB [@sundb](https://github.com/sundb) ([\#1030](https://github.com/redis/hiredis/pull/1030)) +- Handle array response during subscribe in RESP3 [@bjosv](https://github.com/bjosv) ([\#1014](https://github.com/redis/hiredis/pull/1014)) +- Support PING while subscribing (RESP2) [@bjosv](https://github.com/bjosv) ([\#1027](https://github.com/redis/hiredis/pull/1027)) + +## ๐Ÿงฐ Maintenance + +- CI fixes in preparation of release [@michael-grunder](https://github.com/michael-grunder) ([\#1130](https://github.com/redis/hiredis/pull/1130)) +- Add do while(0) (protection for macros [@afcidk](https://github.com/afcidk) [\#959](https://github.com/redis/hiredis/pull/959)) +- Fixup of PR734: Coverage of hiredis.c [@bjosv](https://github.com/bjosv) ([\#1124](https://github.com/redis/hiredis/pull/1124)) +- CMake corrections for building on Windows [@bjosv](https://github.com/bjosv) ([\#1122](https://github.com/redis/hiredis/pull/1122)) +- Install on windows fixes [@bjosv](https://github.com/bjosv) ([\#1117](https://github.com/redis/hiredis/pull/1117)) +- Add libhv example to our standard Makefile [@michael-grunder](https://github.com/michael-grunder) ([\#1108](https://github.com/redis/hiredis/pull/1108)) +- Additional include directory given by pkg-config [@bjosv](https://github.com/bjosv) ([\#1118](https://github.com/redis/hiredis/pull/1118)) +- Use __attribute__ when building with Clang on Windows [@bjosv](https://github.com/bjosv) ([\#1115](https://github.com/redis/hiredis/pull/1115)) +- Minor refactor [@michael-grunder](https://github.com/michael-grunder) ([\#1110](https://github.com/redis/hiredis/pull/1110)) +- Fix pkgconfig result for hiredis_ssl [@bjosv](https://github.com/bjosv) ([\#1107](https://github.com/redis/hiredis/pull/1107)) +- Update documentation to explain redisConnectWithOptions. [@michael-grunder](https://github.com/michael-grunder) ([\#1099](https://github.com/redis/hiredis/pull/1099)) +- uvadapter: reduce number of uv_poll_start calls [@noxiouz](https://github.com/noxiouz) ([\#1098](https://github.com/redis/hiredis/pull/1098)) +- Regression test for off-by-one parsing error [@bugwz](https://github.com/bugwz) ([\#1092](https://github.com/redis/hiredis/pull/1092)) +- CMake: remove dict.c form hiredis_sources [@Lipraxde](https://github.com/Lipraxde) ([\#1055](https://github.com/redis/hiredis/pull/1055)) +- Do store command timeout in the context for redisSetTimeout [@catterer](https://github.com/catterer) ([\#593](https://github.com/redis/hiredis/pull/593), [\#1093](https://github.com/redis/hiredis/pull/1093)) +- Add GitHub Actions CI workflow for hiredis: Arm, Arm64, 386, windows. [@kristjanvalur](https://github.com/kristjanvalur) ([\#943](https://github.com/redis/hiredis/pull/943)) +- CI: bump macOS runner version [@SukkaW](https://github.com/SukkaW) ([\#1079](https://github.com/redis/hiredis/pull/1079)) +- Support for generating release notes [@chayim](https://github.com/chayim) ([\#1083](https://github.com/redis/hiredis/pull/1083)) +- Improve example for SSL initialization in README.md [@stanhu](https://github.com/stanhu) ([\#1084](https://github.com/redis/hiredis/pull/1084)) +- Fix README typos [@bjosv](https://github.com/bjosv) ([\#1080](https://github.com/redis/hiredis/pull/1080)) +- fix cmake version [@smmir-cent](https://github.com/@smmircent) ([\#1050](https://github.com/redis/hiredis/pull/1050)) +- Use the same name for static and shared libraries [@orgads](https://github.com/orgads) ([\#1057](https://github.com/redis/hiredis/pull/1057)) +- Embed debug information in windows static .lib file [@kristjanvalur](https://github.com/kristjanvalur) ([\#1054](https://github.com/redis/hiredis/pull/1054)) +- Improved async documentation [@kristjanvalur](https://github.com/kristjanvalur) ([\#1074](https://github.com/redis/hiredis/pull/1074)) +- Use official repository for redis package. [@yossigo](https://github.com/yossigo) ([\#1061](https://github.com/redis/hiredis/pull/1061)) +- Whitelist hiredis repo path in cygwin [@michael-grunder](https://github.com/michael-grunder) ([\#1063](https://github.com/redis/hiredis/pull/1063)) +- CentOS 8 is EOL, switch to RockyLinux [@michael-grunder](https://github.com/michael-grunder) ([\#1046](https://github.com/redis/hiredis/pull/1046)) +- CMakeLists.txt: allow building without a C++ compiler [@ffontaine](https://github.com/ffontaine) ([\#872](https://github.com/redis/hiredis/pull/872)) +- Makefile: move SSL options into a block and refine rules [@pizhenwei](https://github.com/pizhenwei) ([\#997](https://github.com/redis/hiredis/pull/997)) +- Update CMakeLists.txt for more portability [@EricDeng1001](https://github.com/EricDeng1001) ([\#1005](https://github.com/redis/hiredis/pull/1005)) +- FreeBSD build fixes + CI [@michael-grunder](https://github.com/michael-grunder) ([\#1026](https://github.com/redis/hiredis/pull/1026)) +- Add asynchronous test for pubsub using RESP3 [@bjosv](https://github.com/bjosv) ([\#1012](https://github.com/redis/hiredis/pull/1012)) +- Trigger CI failure when Valgrind issues are found [@bjosv](https://github.com/bjosv) ([\#1011](https://github.com/redis/hiredis/pull/1011)) +- Move to using make directly in Cygwin [@michael-grunder](https://github.com/michael-grunder) ([\#1020](https://github.com/redis/hiredis/pull/1020)) +- Add asynchronous API tests [@bjosv](https://github.com/bjosv) ([\#1010](https://github.com/redis/hiredis/pull/1010)) +- Correcting the build target `coverage` for enabled SSL [@bjosv](https://github.com/bjosv) ([\#1009](https://github.com/redis/hiredis/pull/1009)) +- GH Actions: Run SSL tests during CI [@bjosv](https://github.com/bjosv) ([\#1008](https://github.com/redis/hiredis/pull/1008)) +- GH: Actions - Add valgrind and CMake [@michael-grunder](https://github.com/michael-grunder) ([\#1004](https://github.com/redis/hiredis/pull/1004)) +- Add Centos8 tests in GH Actions [@michael-grunder](https://github.com/michael-grunder) ([\#1001](https://github.com/redis/hiredis/pull/1001)) +- We should run actions on PRs [@michael-grunder](https://github.com/michael-grunder) (([\#1000](https://github.com/redis/hiredis/pull/1000)) +- Add Cygwin test in GitHub actions [@michael-grunder](https://github.com/michael-grunder) ([\#999](https://github.com/redis/hiredis/pull/999)) +- Add Windows tests in GitHub actions [@michael-grunder](https://github.com/michael-grunder) ([\#996](https://github.com/redis/hiredis/pull/996)) +- Switch to GitHub actions [@michael-grunder](https://github.com/michael-grunder) ([\#995](https://github.com/redis/hiredis/pull/995)) +- Minor refactor of CVE-2021-32765 fix. [@michael-grunder](https://github.com/michael-grunder) ([\#993](https://github.com/redis/hiredis/pull/993)) +- Remove extra comma from CMake var. [@xkszltl](https://github.com/xkszltl) ([\#988](https://github.com/redis/hiredis/pull/988)) +- Add REDIS\_OPT\_PREFER\_UNSPEC [@michael-grunder](https://github.com/michael-grunder) ([\#1101](https://github.com/redis/hiredis/pull/1101)) + +## Contributors +We'd like to thank all the contributors who worked on this release! + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ## [1.0.2](https://github.com/redis/hiredis/tree/v1.0.2) - (2021-10-07) Announcing Hiredis v1.0.2, which fixes CVE-2021-32765 but returns the SONAME to the correct value of `1.0.0`. diff --git a/CMakeLists.txt b/CMakeLists.txt index fe6720b28..b7d6ee8d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,10 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 3.4.0) +CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0) +OPTION(BUILD_SHARED_LIBS "Build shared libraries" ON) OPTION(ENABLE_SSL "Build hiredis_ssl for SSL support" OFF) OPTION(DISABLE_TESTS "If tests should be compiled or not" OFF) OPTION(ENABLE_SSL_TESTS "Should we test SSL connections" OFF) +OPTION(ENABLE_EXAMPLES "Enable building hiredis examples" OFF) OPTION(ENABLE_ASYNC_TESTS "Should we run all asynchronous API tests" OFF) MACRO(getVersionBit name) @@ -24,15 +26,11 @@ INCLUDE(GNUInstallDirs) # Hiredis requires C99 SET(CMAKE_C_STANDARD 99) -SET(CMAKE_POSITION_INDEPENDENT_CODE ON) SET(CMAKE_DEBUG_POSTFIX d) -SET(ENABLE_EXAMPLES OFF CACHE BOOL "Enable building hiredis examples") - SET(hiredis_sources alloc.c async.c - dict.c hiredis.c net.c read.c @@ -42,34 +40,30 @@ SET(hiredis_sources SET(hiredis_sources ${hiredis_sources}) IF(WIN32) - ADD_COMPILE_DEFINITIONS(_CRT_SECURE_NO_WARNINGS WIN32_LEAN_AND_MEAN) + ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -DWIN32_LEAN_AND_MEAN) ENDIF() -ADD_LIBRARY(hiredis SHARED ${hiredis_sources}) -ADD_LIBRARY(hiredis_static STATIC ${hiredis_sources}) +ADD_LIBRARY(hiredis ${hiredis_sources}) ADD_LIBRARY(hiredis::hiredis ALIAS hiredis) -ADD_LIBRARY(hiredis::hiredis_static ALIAS hiredis_static) +set(hiredis_export_name hiredis CACHE STRING "Name of the exported target") +set_target_properties(hiredis PROPERTIES EXPORT_NAME ${hiredis_export_name}) SET_TARGET_PROPERTIES(hiredis PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE VERSION "${HIREDIS_SONAME}") -SET_TARGET_PROPERTIES(hiredis_static - PROPERTIES COMPILE_PDB_NAME hiredis_static) -SET_TARGET_PROPERTIES(hiredis_static - PROPERTIES COMPILE_PDB_NAME_DEBUG hiredis_static${CMAKE_DEBUG_POSTFIX}) -IF(WIN32 OR MINGW) +IF(MSVC) + SET_TARGET_PROPERTIES(hiredis + PROPERTIES COMPILE_FLAGS /Z7) +ENDIF() +IF(WIN32) TARGET_LINK_LIBRARIES(hiredis PUBLIC ws2_32 crypt32) - TARGET_LINK_LIBRARIES(hiredis_static PUBLIC ws2_32 crypt32) ELSEIF(CMAKE_SYSTEM_NAME MATCHES "FreeBSD") TARGET_LINK_LIBRARIES(hiredis PUBLIC m) - TARGET_LINK_LIBRARIES(hiredis_static PUBLIC m) ELSEIF(CMAKE_SYSTEM_NAME MATCHES "SunOS") TARGET_LINK_LIBRARIES(hiredis PUBLIC socket) - TARGET_LINK_LIBRARIES(hiredis_static PUBLIC socket) ENDIF() TARGET_INCLUDE_DIRECTORIES(hiredis PUBLIC $ $) -TARGET_INCLUDE_DIRECTORIES(hiredis_static PUBLIC $ $) CONFIGURE_FILE(hiredis.pc.in hiredis.pc @ONLY) @@ -99,26 +93,23 @@ set(CPACK_RPM_PACKAGE_AUTOREQPROV ON) include(CPack) -INSTALL(TARGETS hiredis hiredis_static +INSTALL(TARGETS hiredis EXPORT hiredis-targets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) -if (MSVC) +if (MSVC AND BUILD_SHARED_LIBS) INSTALL(FILES $ DESTINATION ${CMAKE_INSTALL_BINDIR} CONFIGURATIONS Debug RelWithDebInfo) - INSTALL(FILES $/$.pdb - DESTINATION ${CMAKE_INSTALL_LIBDIR} - CONFIGURATIONS Debug RelWithDebInfo) endif() # For NuGet packages INSTALL(FILES hiredis.targets DESTINATION build/native) -INSTALL(FILES hiredis.h read.h sds.h async.h alloc.h +INSTALL(FILES hiredis.h read.h sds.h async.h alloc.h sockcompat.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hiredis) INSTALL(DIRECTORY adapters @@ -131,9 +122,15 @@ export(EXPORT hiredis-targets FILE "${CMAKE_CURRENT_BINARY_DIR}/hiredis-targets.cmake" NAMESPACE hiredis::) -SET(CMAKE_CONF_INSTALL_DIR share/hiredis) +if(WIN32) + SET(CMAKE_CONF_INSTALL_DIR share/hiredis) +else() + SET(CMAKE_CONF_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/hiredis) +endif() SET(INCLUDE_INSTALL_DIR include) include(CMakePackageConfigHelpers) +write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/hiredis-config-version.cmake" + COMPATIBILITY SameMajorVersion) configure_package_config_file(hiredis-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/hiredis-config.cmake INSTALL_DESTINATION ${CMAKE_CONF_INSTALL_DIR} PATH_VARS INCLUDE_INSTALL_DIR) @@ -144,6 +141,7 @@ INSTALL(EXPORT hiredis-targets DESTINATION ${CMAKE_CONF_INSTALL_DIR}) INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hiredis-config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/hiredis-config-version.cmake DESTINATION ${CMAKE_CONF_INSTALL_DIR}) @@ -156,12 +154,10 @@ IF(ENABLE_SSL) FIND_PACKAGE(OpenSSL REQUIRED) SET(hiredis_ssl_sources ssl.c) - ADD_LIBRARY(hiredis_ssl SHARED - ${hiredis_ssl_sources}) - ADD_LIBRARY(hiredis_ssl_static STATIC - ${hiredis_ssl_sources}) + ADD_LIBRARY(hiredis_ssl ${hiredis_ssl_sources}) + ADD_LIBRARY(hiredis::hiredis_ssl ALIAS hiredis_ssl) - IF (APPLE) + IF (APPLE AND BUILD_SHARED_LIBS) SET_PROPERTY(TARGET hiredis_ssl PROPERTY LINK_FLAGS "-Wl,-undefined -Wl,dynamic_lookup") ENDIF() @@ -169,34 +165,26 @@ IF(ENABLE_SSL) PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE VERSION "${HIREDIS_SONAME}") - SET_TARGET_PROPERTIES(hiredis_ssl_static - PROPERTIES COMPILE_PDB_NAME hiredis_ssl_static) - SET_TARGET_PROPERTIES(hiredis_ssl_static - PROPERTIES COMPILE_PDB_NAME_DEBUG hiredis_ssl_static${CMAKE_DEBUG_POSTFIX}) - - TARGET_INCLUDE_DIRECTORIES(hiredis_ssl PRIVATE "${OPENSSL_INCLUDE_DIR}") - TARGET_INCLUDE_DIRECTORIES(hiredis_ssl_static PRIVATE "${OPENSSL_INCLUDE_DIR}") - - TARGET_LINK_LIBRARIES(hiredis_ssl PRIVATE ${OPENSSL_LIBRARIES}) - IF (WIN32 OR MINGW) + IF(MSVC) + SET_TARGET_PROPERTIES(hiredis_ssl + PROPERTIES COMPILE_FLAGS /Z7) + ENDIF() + TARGET_LINK_LIBRARIES(hiredis_ssl PRIVATE OpenSSL::SSL) + IF(WIN32) TARGET_LINK_LIBRARIES(hiredis_ssl PRIVATE hiredis) - TARGET_LINK_LIBRARIES(hiredis_ssl_static PUBLIC hiredis_static) ENDIF() CONFIGURE_FILE(hiredis_ssl.pc.in hiredis_ssl.pc @ONLY) - INSTALL(TARGETS hiredis_ssl hiredis_ssl_static + INSTALL(TARGETS hiredis_ssl EXPORT hiredis_ssl-targets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) - if (MSVC) + if (MSVC AND BUILD_SHARED_LIBS) INSTALL(FILES $ DESTINATION ${CMAKE_INSTALL_BINDIR} CONFIGURATIONS Debug RelWithDebInfo) - INSTALL(FILES $/$.pdb - DESTINATION ${CMAKE_INSTALL_LIBDIR} - CONFIGURATIONS Debug RelWithDebInfo) endif() INSTALL(FILES hiredis_ssl.h @@ -209,7 +197,11 @@ IF(ENABLE_SSL) FILE "${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl-targets.cmake" NAMESPACE hiredis::) - SET(CMAKE_CONF_INSTALL_DIR share/hiredis_ssl) + if(WIN32) + SET(CMAKE_CONF_INSTALL_DIR share/hiredis_ssl) + else() + SET(CMAKE_CONF_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/hiredis_ssl) + endif() configure_package_config_file(hiredis_ssl-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl-config.cmake INSTALL_DESTINATION ${CMAKE_CONF_INSTALL_DIR} PATH_VARS INCLUDE_INSTALL_DIR) @@ -241,5 +233,5 @@ ENDIF() # Add examples IF(ENABLE_EXAMPLES) - ADD_SUBDIRECTORY(examples) + ADD_SUBDIRECTORY(examples) ENDIF(ENABLE_EXAMPLES) diff --git a/Makefile b/Makefile index a2ad84c6b..f31293e90 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ # This file is released under the BSD license, see the COPYING file OBJ=alloc.o net.o hiredis.o sds.o async.o read.o sockcompat.o -EXAMPLES=hiredis-example hiredis-example-libevent hiredis-example-libev hiredis-example-glib hiredis-example-push +EXAMPLES=hiredis-example hiredis-example-libevent hiredis-example-libev hiredis-example-glib hiredis-example-push hiredis-example-poll TESTS=hiredis-test LIBNAME=libhiredis PKGCONFNAME=hiredis.pc @@ -41,7 +41,7 @@ CXX:=$(shell sh -c 'type $${CXX%% *} >/dev/null 2>/dev/null && echo $(CXX) || ec OPTIMIZATION?=-O3 WARNINGS=-Wall -W -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers DEBUG_FLAGS?= -g -ggdb -REAL_CFLAGS=$(OPTIMIZATION) -fPIC $(CPPFLAGS) $(CFLAGS) $(WARNINGS) $(DEBUG_FLAGS) +REAL_CFLAGS=$(OPTIMIZATION) -fPIC $(CPPFLAGS) $(CFLAGS) $(WARNINGS) $(DEBUG_FLAGS) $(PLATFORM_FLAGS) REAL_LDFLAGS=$(LDFLAGS) DYLIBSUFFIX=so @@ -50,7 +50,7 @@ DYLIB_MINOR_NAME=$(LIBNAME).$(DYLIBSUFFIX).$(HIREDIS_SONAME) DYLIB_MAJOR_NAME=$(LIBNAME).$(DYLIBSUFFIX).$(HIREDIS_MAJOR) DYLIBNAME=$(LIBNAME).$(DYLIBSUFFIX) -DYLIB_MAKE_CMD=$(CC) -shared -Wl,-soname,$(DYLIB_MINOR_NAME) +DYLIB_MAKE_CMD=$(CC) $(PLATFORM_FLAGS) -shared -Wl,-soname,$(DYLIB_MINOR_NAME) STLIBNAME=$(LIBNAME).$(STLIBSUFFIX) STLIB_MAKE_CMD=$(AR) rcs @@ -63,7 +63,7 @@ SSL_DYLIB_MINOR_NAME=$(SSL_LIBNAME).$(DYLIBSUFFIX).$(HIREDIS_SONAME) SSL_DYLIB_MAJOR_NAME=$(SSL_LIBNAME).$(DYLIBSUFFIX).$(HIREDIS_MAJOR) SSL_DYLIBNAME=$(SSL_LIBNAME).$(DYLIBSUFFIX) SSL_STLIBNAME=$(SSL_LIBNAME).$(STLIBSUFFIX) -SSL_DYLIB_MAKE_CMD=$(CC) -shared -Wl,-soname,$(SSL_DYLIB_MINOR_NAME) +SSL_DYLIB_MAKE_CMD=$(CC) $(PLATFORM_FLAGS) -shared -Wl,-soname,$(SSL_DYLIB_MINOR_NAME) USE_SSL?=0 ifeq ($(USE_SSL),1) @@ -92,18 +92,25 @@ ifeq ($(TEST_ASYNC),1) endif ifeq ($(USE_SSL),1) - ifeq ($(uname_S),Linux) - ifdef OPENSSL_PREFIX - CFLAGS+=-I$(OPENSSL_PREFIX)/include - SSL_LDFLAGS+=-L$(OPENSSL_PREFIX)/lib -lssl -lcrypto - else - SSL_LDFLAGS=-lssl -lcrypto + ifndef OPENSSL_PREFIX + ifeq ($(uname_S),Darwin) + SEARCH_PATH1=/opt/homebrew/opt/openssl + SEARCH_PATH2=/usr/local/opt/openssl + + ifneq ($(wildcard $(SEARCH_PATH1)),) + OPENSSL_PREFIX=$(SEARCH_PATH1) + else ifneq ($(wildcard $(SEARCH_PATH2)),) + OPENSSL_PREFIX=$(SEARCH_PATH2) + endif endif - else - OPENSSL_PREFIX?=/usr/local/opt/openssl - CFLAGS+=-I$(OPENSSL_PREFIX)/include - SSL_LDFLAGS+=-L$(OPENSSL_PREFIX)/lib -lssl -lcrypto endif + + ifdef OPENSSL_PREFIX + CFLAGS+=-I$(OPENSSL_PREFIX)/include + SSL_LDFLAGS+=-L$(OPENSSL_PREFIX)/lib + endif + + SSL_LDFLAGS+=-lssl -lcrypto endif ifeq ($(uname_S),FreeBSD) @@ -180,6 +187,9 @@ hiredis-example-libevent-ssl: examples/example-libevent-ssl.c adapters/libevent. hiredis-example-libev: examples/example-libev.c adapters/libev.h $(STLIBNAME) $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< -lev $(STLIBNAME) $(REAL_LDFLAGS) +hiredis-example-libhv: examples/example-libhv.c adapters/libhv.h $(STLIBNAME) + $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< -lhv $(STLIBNAME) $(REAL_LDFLAGS) + hiredis-example-glib: examples/example-glib.c adapters/glib.h $(STLIBNAME) $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< $(shell pkg-config --cflags --libs glib-2.0) $(STLIBNAME) $(REAL_LDFLAGS) @@ -192,6 +202,9 @@ hiredis-example-macosx: examples/example-macosx.c adapters/macosx.h $(STLIBNAME) hiredis-example-ssl: examples/example-ssl.c $(STLIBNAME) $(SSL_STLIBNAME) $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< $(STLIBNAME) $(SSL_STLIBNAME) $(REAL_LDFLAGS) $(SSL_LDFLAGS) +hiredis-example-poll: examples/example-poll.c adapters/poll.h $(STLIBNAME) + $(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< $(STLIBNAME) $(REAL_LDFLAGS) + ifndef AE_DIR hiredis-example-ae: @echo "Please specify AE_DIR (e.g. /src)" @@ -269,20 +282,22 @@ $(PKGCONFNAME): hiredis.h @echo prefix=$(PREFIX) > $@ @echo exec_prefix=\$${prefix} >> $@ @echo libdir=$(PREFIX)/$(LIBRARY_PATH) >> $@ - @echo includedir=$(PREFIX)/$(INCLUDE_PATH) >> $@ + @echo includedir=$(PREFIX)/include >> $@ + @echo pkgincludedir=$(PREFIX)/$(INCLUDE_PATH) >> $@ @echo >> $@ @echo Name: hiredis >> $@ @echo Description: Minimalistic C client library for Redis. >> $@ @echo Version: $(HIREDIS_MAJOR).$(HIREDIS_MINOR).$(HIREDIS_PATCH) >> $@ @echo Libs: -L\$${libdir} -lhiredis >> $@ - @echo Cflags: -I\$${includedir} -D_FILE_OFFSET_BITS=64 >> $@ + @echo Cflags: -I\$${pkgincludedir} -I\$${includedir} -D_FILE_OFFSET_BITS=64 >> $@ $(SSL_PKGCONFNAME): hiredis_ssl.h @echo "Generating $@ for pkgconfig..." @echo prefix=$(PREFIX) > $@ @echo exec_prefix=\$${prefix} >> $@ @echo libdir=$(PREFIX)/$(LIBRARY_PATH) >> $@ - @echo includedir=$(PREFIX)/$(INCLUDE_PATH) >> $@ + @echo includedir=$(PREFIX)/include >> $@ + @echo pkgincludedir=$(PREFIX)/$(INCLUDE_PATH) >> $@ @echo >> $@ @echo Name: hiredis_ssl >> $@ @echo Description: SSL Support for hiredis. >> $@ @@ -293,7 +308,7 @@ $(SSL_PKGCONFNAME): hiredis_ssl.h install: $(DYLIBNAME) $(STLIBNAME) $(PKGCONFNAME) $(SSL_INSTALL) mkdir -p $(INSTALL_INCLUDE_PATH) $(INSTALL_INCLUDE_PATH)/adapters $(INSTALL_LIBRARY_PATH) - $(INSTALL) hiredis.h async.h read.h sds.h alloc.h $(INSTALL_INCLUDE_PATH) + $(INSTALL) hiredis.h async.h read.h sds.h alloc.h sockcompat.h $(INSTALL_INCLUDE_PATH) $(INSTALL) adapters/*.h $(INSTALL_INCLUDE_PATH)/adapters $(INSTALL) $(DYLIBNAME) $(INSTALL_LIBRARY_PATH)/$(DYLIB_MINOR_NAME) cd $(INSTALL_LIBRARY_PATH) && ln -sf $(DYLIB_MINOR_NAME) $(DYLIBNAME) @@ -330,7 +345,8 @@ coverage: gcov make check mkdir -p tmp/lcov lcov -d . -c --exclude '/usr*' -o tmp/lcov/hiredis.info - genhtml --legend -o tmp/lcov/report tmp/lcov/hiredis.info + lcov -q -l tmp/lcov/hiredis.info + genhtml --legend -q -o tmp/lcov/report tmp/lcov/hiredis.info noopt: $(MAKE) OPTIMIZATION="" diff --git a/README.md b/README.md index ed66220c7..74364b411 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,13 @@ Redis version >= 1.2.0. The library comes with multiple APIs. There is the *synchronous API*, the *asynchronous API* and the *reply parsing API*. +## Upgrading to `1.1.0` + +Almost all users will simply need to recompile their applications against the newer version of hiredis. + +**NOTE**: Hiredis can now return `nan` in addition to `-inf` and `inf` in a `REDIS_REPLY_DOUBLE`. + Applications that deal with `RESP3` doubles should make sure to account for this. + ## Upgrading to `1.0.2` NOTE: v1.0.1 erroneously bumped SONAME, which is why it is skipped here. @@ -82,6 +89,7 @@ an error state. The field `errstr` will contain a string with a description of the error. More information on errors can be found in the **Errors** section. After trying to connect to Redis using `redisConnect` you should check the `err` field to see if establishing the connection was successful: + ```c redisContext *c = redisConnect("127.0.0.1", 6379); if (c == NULL || c->err) { @@ -94,8 +102,74 @@ if (c == NULL || c->err) { } ``` +One can also use `redisConnectWithOptions` which takes a `redisOptions` argument +that can be configured with endpoint information as well as many different flags +to change how the `redisContext` will be configured. + +```c +redisOptions opt = {0}; + +/* One can set the endpoint with one of our helper macros */ +if (tcp) { + REDIS_OPTIONS_SET_TCP(&opt, "localhost", 6379); +} else { + REDIS_OPTIONS_SET_UNIX(&opt, "/tmp/redis.sock"); +} + +/* And privdata can be specified with another helper */ +REDIS_OPTIONS_SET_PRIVDATA(&opt, myPrivData, myPrivDataDtor); + +/* Finally various options may be set via the `options` member, as described below */ +opt->options |= REDIS_OPT_PREFER_IPV4; +``` + +If a connection is lost, `int redisReconnect(redisContext *c)` can be used to restore the connection using the same endpoint and options as the given context. + +### Configurable redisOptions flags + +There are several flags you may set in the `redisOptions` struct to change default behavior. You can specify the flags via the `redisOptions->options` member. + +| Flag | Description | +| --- | --- | +| REDIS\_OPT\_NONBLOCK | Tells hiredis to make a non-blocking connection. | +| REDIS\_OPT\_REUSEADDR | Tells hiredis to set the [SO_REUSEADDR](https://man7.org/linux/man-pages/man7/socket.7.html) socket option | +| REDIS\_OPT\_PREFER\_IPV4
REDIS\_OPT\_PREFER_IPV6
REDIS\_OPT\_PREFER\_IP\_UNSPEC | Informs hiredis to either prefer IPv4 or IPv6 when invoking [getaddrinfo](https://man7.org/linux/man-pages/man3/gai_strerror.3.html). `REDIS_OPT_PREFER_IP_UNSPEC` will cause hiredis to specify `AF_UNSPEC` in the getaddrinfo call, which means both IPv4 and IPv6 addresses will be searched simultaneously.
Hiredis prefers IPv4 by default. | +| REDIS\_OPT\_NO\_PUSH\_AUTOFREE | Tells hiredis to not install the default RESP3 PUSH handler (which just intercepts and frees the replies). This is useful in situations where you want to process these messages in-band. | +| REDIS\_OPT\_NOAUTOFREEREPLIES | **ASYNC**: tells hiredis not to automatically invoke `freeReplyObject` after executing the reply callback. | +| REDIS\_OPT\_NOAUTOFREE | **ASYNC**: Tells hiredis not to automatically free the `redisAsyncContext` on connection/communication failure, but only if the user makes an explicit call to `redisAsyncDisconnect` or `redisAsyncFree` | + *Note: A `redisContext` is not thread-safe.* +### Other configuration using socket options + +The following socket options are applied directly to the underlying socket. +The values are not stored in the `redisContext`, so they are not automatically applied when reconnecting using `redisReconnect()`. +These functions return `REDIS_OK` on success. +On failure, `REDIS_ERR` is returned and the underlying connection is closed. + +To configure these for an asyncronous context (see *Asynchronous API* below), use `ac->c` to get the redisContext out of an asyncRedisContext. + +```C +int redisEnableKeepAlive(redisContext *c); +int redisEnableKeepAliveWithInterval(redisContext *c, int interval); +``` + +Enables TCP keepalive by setting the following socket options (with some variations depending on OS): + +* `SO_KEEPALIVE`; +* `TCP_KEEPALIVE` or `TCP_KEEPIDLE`, value configurable using the `interval` parameter, default 15 seconds; +* `TCP_KEEPINTVL` set to 1/3 of `interval`; +* `TCP_KEEPCNT` set to 3. + +```C +int redisSetTcpUserTimeout(redisContext *c, unsigned int timeout); +``` + +Set the `TCP_USER_TIMEOUT` Linux-specific socket option which is as described in the `tcp` man page: + +> When the value is greater than 0, it specifies the maximum amount of time in milliseconds that trans mitted data may remain unacknowledged before TCP will forcibly close the corresponding connection and return ETIMEDOUT to the application. +> If the option value is specified as 0, TCP will use the system default. + ### Sending commands There are several ways to issue commands to Redis. The first that will be introduced is @@ -250,7 +324,7 @@ following two execution paths: * Read from the socket until a single reply could be parsed The function `redisGetReply` is exported as part of the Hiredis API and can be used when a reply -is expected on the socket. To pipeline commands, the only things that needs to be done is +is expected on the socket. To pipeline commands, the only thing that needs to be done is filling up the output buffer. For this cause, two commands can be used that are identical to the `redisCommand` family, apart from not returning a reply: ```c @@ -320,23 +394,48 @@ Redis. It returns a pointer to the newly created `redisAsyncContext` struct. The should be checked after creation to see if there were errors creating the connection. Because the connection that will be created is non-blocking, the kernel is not able to instantly return if the specified host and port is able to accept a connection. +In case of error, it is the caller's responsibility to free the context using `redisAsyncFree()` *Note: A `redisAsyncContext` is not thread-safe.* +An application function creating a connection might look like this: + ```c -redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); -if (c->err) { - printf("Error: %s\n", c->errstr); - // handle error +void appConnect(myAppData *appData) +{ + redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); + if (c->err) { + printf("Error: %s\n", c->errstr); + // handle error + redisAsyncFree(c); + c = NULL; + } else { + appData->context = c; + appData->connecting = 1; + c->data = appData; /* store application pointer for the callbacks */ + redisAsyncSetConnectCallback(c, appOnConnect); + redisAsyncSetDisconnectCallback(c, appOnDisconnect); + } } + ``` -The asynchronous context can hold a disconnect callback function that is called when the -connection is disconnected (either because of an error or per user request). This function should + +The asynchronous context _should_ hold a *connect* callback function that is called when the connection +attempt completes, either successfully or with an error. +It _can_ also hold a *disconnect* callback function that is called when the +connection is disconnected (either because of an error or per user request). Both callbacks should have the following prototype: ```c void(const redisAsyncContext *c, int status); ``` + +On a *connect*, the `status` argument is set to `REDIS_OK` if the connection attempt succeeded. In this +case, the context is ready to accept commands. If it is called with `REDIS_ERR` then the +connection attempt failed. The `err` field in the context can be accessed to find out the cause of the error. +After a failed connection attempt, the context object is automatically freed by the library after calling +the connect callback. This may be a good point to create a new context and retry the connection. + On a disconnect, the `status` argument is set to `REDIS_OK` when disconnection was initiated by the user, or `REDIS_ERR` when the disconnection was caused by an error. When it is `REDIS_ERR`, the `err` field in the context can be accessed to find out the cause of the error. @@ -344,12 +443,46 @@ field in the context can be accessed to find out the cause of the error. The context object is always freed after the disconnect callback fired. When a reconnect is needed, the disconnect callback is a good point to do so. -Setting the disconnect callback can only be done once per context. For subsequent calls it will -return `REDIS_ERR`. The function to set the disconnect callback has the following prototype: +Setting the connect or disconnect callbacks can only be done once per context. For subsequent calls the +api will return `REDIS_ERR`. The function to set the callbacks have the following prototype: ```c +/* Alternatively you can use redisAsyncSetConnectCallbackNC which will be passed a non-const + redisAsyncContext* on invocation (e.g. allowing writes to the privdata member). */ +int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn); int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn); ``` -`ac->data` may be used to pass user data to this callback, the same can be done for redisConnectCallback. +`ac->data` may be used to pass user data to both callbacks. A typical implementation +might look something like this: +```c +void appOnConnect(redisAsyncContext *c, int status) +{ + myAppData *appData = (myAppData*)c->data; /* get my application specific context*/ + appData->connecting = 0; + if (status == REDIS_OK) { + appData->connected = 1; + } else { + appData->connected = 0; + appData->err = c->err; + appData->context = NULL; /* avoid stale pointer when callback returns */ + } + appAttemptReconnect(); +} + +void appOnDisconnect(redisAsyncContext *c, int status) +{ + myAppData *appData = (myAppData*)c->data; /* get my application specific context*/ + appData->connected = 0; + appData->err = c->err; + appData->context = NULL; /* avoid stale pointer when callback returns */ + if (status == REDIS_OK) { + appNotifyDisconnectCompleted(mydata); + } else { + appNotifyUnexpectedDisconnect(mydata); + appAttemptReconnect(); + } +} +``` + ### Sending commands and their callbacks In an asynchronous context, commands are automatically pipelined due to the nature of an event loop. @@ -382,6 +515,14 @@ valid for the duration of the callback. All pending callbacks are called with a `NULL` reply when the context encountered an error. +For every command issued, with the exception of **SUBSCRIBE** and **PSUBSCRIBE**, the callback is +called exactly once. Even if the context object id disconnected or deleted, every pending callback +will be called with a `NULL` reply. + +For **SUBSCRIBE** and **PSUBSCRIBE**, the callbacks may be called repeatedly until an `unsubscribe` +message arrives. This will be the last invocation of the callback. In case of error, the callbacks +may receive a final `NULL` reply instead. + ### Disconnecting An asynchronous connection can be terminated using: @@ -394,6 +535,15 @@ have been written to the socket, their respective replies have been read and the callbacks have been executed. After this, the disconnection callback is executed with the `REDIS_OK` status and the context object is freed. +The connection can be forcefully disconnected using +```c +void redisAsyncFree(redisAsyncContext *ac); +``` +In this case, nothing more is written to the socket, all pending callbacks are called with a `NULL` +reply and the disconnection callback is called with `REDIS_OK`, after which the context object +is freed. + + ### Hooking it up to event library *X* There are a few hooks that need to be set on the context object after it is created. @@ -497,8 +647,8 @@ unaffected so no additional dependencies are introduced. First, you'll need to make sure you include the SSL header file: ```c -#include "hiredis.h" -#include "hiredis_ssl.h" +#include +#include ``` You will also need to link against `libhiredis_ssl`, **in addition** to @@ -529,7 +679,7 @@ redisSSLContext *ssl_context; /* An error variable to indicate what went wrong, if the context fails to * initialize. */ -redisSSLContextError ssl_error; +redisSSLContextError ssl_error = REDIS_SSL_CTX_NONE; /* Initialize global OpenSSL state. * @@ -547,11 +697,11 @@ ssl_context = redisCreateSSLContext( "redis.mydomain.com", /* Server name to request (SNI), optional */ &ssl_error); -if(ssl_context == NULL || ssl_error != 0) { +if(ssl_context == NULL || ssl_error != REDIS_SSL_CTX_NONE) { /* Handle error and abort... */ - /* e.g. - printf("SSL error: %s\n", - (ssl_error != 0) ? + /* e.g. + printf("SSL error: %s\n", + (ssl_error != REDIS_SSL_CTX_NONE) ? redisSSLContextGetError(ssl_error) : "Unknown error"); // Abort */ @@ -633,7 +783,7 @@ If you have a unique use-case where you don't want hiredis to automatically inte redisSetPushCallback(context, NULL); ``` - _Note: With no handler configured, calls to `redisCommand` may generate more than one reply, so this strategy is only applicable when there's some kind of blocking`redisGetReply()` loop (e.g. `MONITOR` or `SUBSCRIBE` workloads)._ + _Note: With no handler configured, calls to `redisCommand` may generate more than one reply, so this strategy is only applicable when there's some kind of blocking `redisGetReply()` loop (e.g. `MONITOR` or `SUBSCRIBE` workloads)._ ## Allocator injection diff --git a/adapters/libhv.h b/adapters/libhv.h new file mode 100644 index 000000000..3b54c70f4 --- /dev/null +++ b/adapters/libhv.h @@ -0,0 +1,123 @@ +#ifndef __HIREDIS_LIBHV_H__ +#define __HIREDIS_LIBHV_H__ + +#include +#include "../hiredis.h" +#include "../async.h" + +typedef struct redisLibhvEvents { + hio_t *io; + htimer_t *timer; +} redisLibhvEvents; + +static void redisLibhvHandleEvents(hio_t* io) { + redisAsyncContext* context = (redisAsyncContext*)hevent_userdata(io); + int events = hio_events(io); + int revents = hio_revents(io); + if (context && (events & HV_READ) && (revents & HV_READ)) { + redisAsyncHandleRead(context); + } + if (context && (events & HV_WRITE) && (revents & HV_WRITE)) { + redisAsyncHandleWrite(context); + } +} + +static void redisLibhvAddRead(void *privdata) { + redisLibhvEvents* events = (redisLibhvEvents*)privdata; + hio_add(events->io, redisLibhvHandleEvents, HV_READ); +} + +static void redisLibhvDelRead(void *privdata) { + redisLibhvEvents* events = (redisLibhvEvents*)privdata; + hio_del(events->io, HV_READ); +} + +static void redisLibhvAddWrite(void *privdata) { + redisLibhvEvents* events = (redisLibhvEvents*)privdata; + hio_add(events->io, redisLibhvHandleEvents, HV_WRITE); +} + +static void redisLibhvDelWrite(void *privdata) { + redisLibhvEvents* events = (redisLibhvEvents*)privdata; + hio_del(events->io, HV_WRITE); +} + +static void redisLibhvCleanup(void *privdata) { + redisLibhvEvents* events = (redisLibhvEvents*)privdata; + + if (events->timer) + htimer_del(events->timer); + + hio_close(events->io); + hevent_set_userdata(events->io, NULL); + + hi_free(events); +} + +static void redisLibhvTimeout(htimer_t* timer) { + hio_t* io = (hio_t*)hevent_userdata(timer); + redisAsyncHandleTimeout((redisAsyncContext*)hevent_userdata(io)); +} + +static void redisLibhvSetTimeout(void *privdata, struct timeval tv) { + redisLibhvEvents* events; + uint32_t millis; + hloop_t* loop; + + events = (redisLibhvEvents*)privdata; + millis = tv.tv_sec * 1000 + tv.tv_usec / 1000; + + if (millis == 0) { + /* Libhv disallows zero'd timers so treat this as a delete or NO OP */ + if (events->timer) { + htimer_del(events->timer); + events->timer = NULL; + } + } else if (events->timer == NULL) { + /* Add new timer */ + loop = hevent_loop(events->io); + events->timer = htimer_add(loop, redisLibhvTimeout, millis, 1); + hevent_set_userdata(events->timer, events->io); + } else { + /* Update existing timer */ + htimer_reset(events->timer, millis); + } +} + +static int redisLibhvAttach(redisAsyncContext* ac, hloop_t* loop) { + redisContext *c = &(ac->c); + redisLibhvEvents *events; + hio_t* io = NULL; + + if (ac->ev.data != NULL) { + return REDIS_ERR; + } + + /* Create container struct to keep track of our io and any timer */ + events = (redisLibhvEvents*)hi_malloc(sizeof(*events)); + if (events == NULL) { + return REDIS_ERR; + } + + io = hio_get(loop, c->fd); + if (io == NULL) { + hi_free(events); + return REDIS_ERR; + } + + hevent_set_userdata(io, ac); + + events->io = io; + events->timer = NULL; + + ac->ev.addRead = redisLibhvAddRead; + ac->ev.delRead = redisLibhvDelRead; + ac->ev.addWrite = redisLibhvAddWrite; + ac->ev.delWrite = redisLibhvDelWrite; + ac->ev.cleanup = redisLibhvCleanup; + ac->ev.scheduleTimer = redisLibhvSetTimeout; + ac->ev.data = events; + + return REDIS_OK; +} +#endif diff --git a/adapters/libsdevent.h b/adapters/libsdevent.h new file mode 100644 index 000000000..1268ed9f9 --- /dev/null +++ b/adapters/libsdevent.h @@ -0,0 +1,177 @@ +#ifndef HIREDIS_LIBSDEVENT_H +#define HIREDIS_LIBSDEVENT_H +#include +#include "../hiredis.h" +#include "../async.h" + +#define REDIS_LIBSDEVENT_DELETED 0x01 +#define REDIS_LIBSDEVENT_ENTERED 0x02 + +typedef struct redisLibsdeventEvents { + redisAsyncContext *context; + struct sd_event *event; + struct sd_event_source *fdSource; + struct sd_event_source *timerSource; + int fd; + short flags; + short state; +} redisLibsdeventEvents; + +static void redisLibsdeventDestroy(redisLibsdeventEvents *e) { + if (e->fdSource) { + e->fdSource = sd_event_source_disable_unref(e->fdSource); + } + if (e->timerSource) { + e->timerSource = sd_event_source_disable_unref(e->timerSource); + } + sd_event_unref(e->event); + hi_free(e); +} + +static int redisLibsdeventTimeoutHandler(sd_event_source *s, uint64_t usec, void *userdata) { + ((void)s); + ((void)usec); + redisLibsdeventEvents *e = (redisLibsdeventEvents*)userdata; + redisAsyncHandleTimeout(e->context); + return 0; +} + +static int redisLibsdeventHandler(sd_event_source *s, int fd, uint32_t event, void *userdata) { + ((void)s); + ((void)fd); + redisLibsdeventEvents *e = (redisLibsdeventEvents*)userdata; + e->state |= REDIS_LIBSDEVENT_ENTERED; + +#define CHECK_DELETED() if (e->state & REDIS_LIBSDEVENT_DELETED) {\ + redisLibsdeventDestroy(e);\ + return 0; \ + } + + if ((event & EPOLLIN) && e->context && (e->state & REDIS_LIBSDEVENT_DELETED) == 0) { + redisAsyncHandleRead(e->context); + CHECK_DELETED(); + } + + if ((event & EPOLLOUT) && e->context && (e->state & REDIS_LIBSDEVENT_DELETED) == 0) { + redisAsyncHandleWrite(e->context); + CHECK_DELETED(); + } + + e->state &= ~REDIS_LIBSDEVENT_ENTERED; +#undef CHECK_DELETED + + return 0; +} + +static void redisLibsdeventAddRead(void *userdata) { + redisLibsdeventEvents *e = (redisLibsdeventEvents*)userdata; + + if (e->flags & EPOLLIN) { + return; + } + + e->flags |= EPOLLIN; + + if (e->flags & EPOLLOUT) { + sd_event_source_set_io_events(e->fdSource, e->flags); + } else { + sd_event_add_io(e->event, &e->fdSource, e->fd, e->flags, redisLibsdeventHandler, e); + } +} + +static void redisLibsdeventDelRead(void *userdata) { + redisLibsdeventEvents *e = (redisLibsdeventEvents*)userdata; + + e->flags &= ~EPOLLIN; + + if (e->flags) { + sd_event_source_set_io_events(e->fdSource, e->flags); + } else { + e->fdSource = sd_event_source_disable_unref(e->fdSource); + } +} + +static void redisLibsdeventAddWrite(void *userdata) { + redisLibsdeventEvents *e = (redisLibsdeventEvents*)userdata; + + if (e->flags & EPOLLOUT) { + return; + } + + e->flags |= EPOLLOUT; + + if (e->flags & EPOLLIN) { + sd_event_source_set_io_events(e->fdSource, e->flags); + } else { + sd_event_add_io(e->event, &e->fdSource, e->fd, e->flags, redisLibsdeventHandler, e); + } +} + +static void redisLibsdeventDelWrite(void *userdata) { + redisLibsdeventEvents *e = (redisLibsdeventEvents*)userdata; + + e->flags &= ~EPOLLOUT; + + if (e->flags) { + sd_event_source_set_io_events(e->fdSource, e->flags); + } else { + e->fdSource = sd_event_source_disable_unref(e->fdSource); + } +} + +static void redisLibsdeventCleanup(void *userdata) { + redisLibsdeventEvents *e = (redisLibsdeventEvents*)userdata; + + if (!e) { + return; + } + + if (e->state & REDIS_LIBSDEVENT_ENTERED) { + e->state |= REDIS_LIBSDEVENT_DELETED; + } else { + redisLibsdeventDestroy(e); + } +} + +static void redisLibsdeventSetTimeout(void *userdata, struct timeval tv) { + redisLibsdeventEvents *e = (redisLibsdeventEvents *)userdata; + + uint64_t usec = tv.tv_sec * 1000000 + tv.tv_usec; + if (!e->timerSource) { + sd_event_add_time_relative(e->event, &e->timerSource, CLOCK_MONOTONIC, usec, 1, redisLibsdeventTimeoutHandler, e); + } else { + sd_event_source_set_time_relative(e->timerSource, usec); + } +} + +static int redisLibsdeventAttach(redisAsyncContext *ac, struct sd_event *event) { + redisContext *c = &(ac->c); + redisLibsdeventEvents *e; + + /* Nothing should be attached when something is already attached */ + if (ac->ev.data != NULL) + return REDIS_ERR; + + /* Create container for context and r/w events */ + e = (redisLibsdeventEvents*)hi_calloc(1, sizeof(*e)); + if (e == NULL) + return REDIS_ERR; + + /* Initialize and increase event refcount */ + e->context = ac; + e->event = event; + e->fd = c->fd; + sd_event_ref(event); + + /* Register functions to start/stop listening for events */ + ac->ev.addRead = redisLibsdeventAddRead; + ac->ev.delRead = redisLibsdeventDelRead; + ac->ev.addWrite = redisLibsdeventAddWrite; + ac->ev.delWrite = redisLibsdeventDelWrite; + ac->ev.cleanup = redisLibsdeventCleanup; + ac->ev.scheduleTimer = redisLibsdeventSetTimeout; + ac->ev.data = e; + + return REDIS_OK; +} +#endif diff --git a/adapters/libuv.h b/adapters/libuv.h index df0a84578..268edab79 100644 --- a/adapters/libuv.h +++ b/adapters/libuv.h @@ -30,6 +30,10 @@ static void redisLibuvPoll(uv_poll_t* handle, int status, int events) { static void redisLibuvAddRead(void *privdata) { redisLibuvEvents* p = (redisLibuvEvents*)privdata; + if (p->events & UV_READABLE) { + return; + } + p->events |= UV_READABLE; uv_poll_start(&p->handle, p->events, redisLibuvPoll); @@ -52,6 +56,10 @@ static void redisLibuvDelRead(void *privdata) { static void redisLibuvAddWrite(void *privdata) { redisLibuvEvents* p = (redisLibuvEvents*)privdata; + if (p->events & UV_WRITABLE) { + return; + } + p->events |= UV_WRITABLE; uv_poll_start(&p->handle, p->events, redisLibuvPoll); diff --git a/adapters/poll.h b/adapters/poll.h new file mode 100644 index 000000000..f138650f9 --- /dev/null +++ b/adapters/poll.h @@ -0,0 +1,197 @@ + +#ifndef HIREDIS_POLL_H +#define HIREDIS_POLL_H + +#include "../async.h" +#include "../sockcompat.h" +#include // for memset +#include + +/* Values to return from redisPollTick */ +#define REDIS_POLL_HANDLED_READ 1 +#define REDIS_POLL_HANDLED_WRITE 2 +#define REDIS_POLL_HANDLED_TIMEOUT 4 + +/* An adapter to allow manual polling of the async context by checking the state + * of the underlying file descriptor. Useful in cases where there is no formal + * IO event loop but regular ticking can be used, such as in game engines. */ + +typedef struct redisPollEvents { + redisAsyncContext *context; + redisFD fd; + char reading, writing; + char in_tick; + char deleted; + double deadline; +} redisPollEvents; + +static double redisPollTimevalToDouble(struct timeval *tv) { + if (tv == NULL) + return 0.0; + return tv->tv_sec + tv->tv_usec / 1000000.00; +} + +static double redisPollGetNow(void) { +#ifndef _MSC_VER + struct timeval tv; + gettimeofday(&tv,NULL); + return redisPollTimevalToDouble(&tv); +#else + FILETIME ft; + ULARGE_INTEGER li; + GetSystemTimeAsFileTime(&ft); + li.HighPart = ft.dwHighDateTime; + li.LowPart = ft.dwLowDateTime; + return (double)li.QuadPart * 1e-7; +#endif +} + +/* Poll for io, handling any pending callbacks. The timeout argument can be + * positive to wait for a maximum given time for IO, zero to poll, or negative + * to wait forever */ +static int redisPollTick(redisAsyncContext *ac, double timeout) { + int reading, writing; + struct pollfd pfd; + int handled; + int ns; + int itimeout; + + redisPollEvents *e = (redisPollEvents*)ac->ev.data; + if (!e) + return 0; + + /* local flags, won't get changed during callbacks */ + reading = e->reading; + writing = e->writing; + if (!reading && !writing) + return 0; + + pfd.fd = e->fd; + pfd.events = 0; + if (reading) + pfd.events = POLLIN; + if (writing) + pfd.events |= POLLOUT; + + if (timeout >= 0.0) { + itimeout = (int)(timeout * 1000.0); + } else { + itimeout = -1; + } + + ns = poll(&pfd, 1, itimeout); + if (ns < 0) { + /* ignore the EINTR error */ + if (errno != EINTR) + return ns; + ns = 0; + } + + handled = 0; + e->in_tick = 1; + if (ns) { + if (reading && (pfd.revents & POLLIN)) { + redisAsyncHandleRead(ac); + handled |= REDIS_POLL_HANDLED_READ; + } + /* on Windows, connection failure is indicated with the Exception fdset. + * handle it the same as writable. */ + if (writing && (pfd.revents & (POLLOUT | POLLERR))) { + /* context Read callback may have caused context to be deleted, e.g. + by doing an redisAsyncDisconnect() */ + if (!e->deleted) { + redisAsyncHandleWrite(ac); + handled |= REDIS_POLL_HANDLED_WRITE; + } + } + } + + /* perform timeouts */ + if (!e->deleted && e->deadline != 0.0) { + double now = redisPollGetNow(); + if (now >= e->deadline) { + /* deadline has passed. disable timeout and perform callback */ + e->deadline = 0.0; + redisAsyncHandleTimeout(ac); + handled |= REDIS_POLL_HANDLED_TIMEOUT; + } + } + + /* do a delayed cleanup if required */ + if (e->deleted) + hi_free(e); + else + e->in_tick = 0; + + return handled; +} + +static void redisPollAddRead(void *data) { + redisPollEvents *e = (redisPollEvents*)data; + e->reading = 1; +} + +static void redisPollDelRead(void *data) { + redisPollEvents *e = (redisPollEvents*)data; + e->reading = 0; +} + +static void redisPollAddWrite(void *data) { + redisPollEvents *e = (redisPollEvents*)data; + e->writing = 1; +} + +static void redisPollDelWrite(void *data) { + redisPollEvents *e = (redisPollEvents*)data; + e->writing = 0; +} + +static void redisPollCleanup(void *data) { + redisPollEvents *e = (redisPollEvents*)data; + + /* if we are currently processing a tick, postpone deletion */ + if (e->in_tick) + e->deleted = 1; + else + hi_free(e); +} + +static void redisPollScheduleTimer(void *data, struct timeval tv) +{ + redisPollEvents *e = (redisPollEvents*)data; + double now = redisPollGetNow(); + e->deadline = now + redisPollTimevalToDouble(&tv); +} + +static int redisPollAttach(redisAsyncContext *ac) { + redisContext *c = &(ac->c); + redisPollEvents *e; + + /* Nothing should be attached when something is already attached */ + if (ac->ev.data != NULL) + return REDIS_ERR; + + /* Create container for context and r/w events */ + e = (redisPollEvents*)hi_malloc(sizeof(*e)); + if (e == NULL) + return REDIS_ERR; + memset(e, 0, sizeof(*e)); + + e->context = ac; + e->fd = c->fd; + e->reading = e->writing = 0; + e->in_tick = e->deleted = 0; + e->deadline = 0.0; + + /* Register functions to start/stop listening for events */ + ac->ev.addRead = redisPollAddRead; + ac->ev.delRead = redisPollDelRead; + ac->ev.addWrite = redisPollAddWrite; + ac->ev.delWrite = redisPollDelWrite; + ac->ev.scheduleTimer = redisPollScheduleTimer; + ac->ev.cleanup = redisPollCleanup; + ac->ev.data = e; + + return REDIS_OK; +} +#endif /* HIREDIS_POLL_H */ diff --git a/adapters/redismoduleapi.h b/adapters/redismoduleapi.h new file mode 100644 index 000000000..8a076fe46 --- /dev/null +++ b/adapters/redismoduleapi.h @@ -0,0 +1,144 @@ +#ifndef __HIREDIS_REDISMODULEAPI_H__ +#define __HIREDIS_REDISMODULEAPI_H__ + +#include "redismodule.h" + +#include "../async.h" +#include "../hiredis.h" + +#include + +typedef struct redisModuleEvents { + redisAsyncContext *context; + RedisModuleCtx *module_ctx; + int fd; + int reading, writing; + int timer_active; + RedisModuleTimerID timer_id; +} redisModuleEvents; + +static inline void redisModuleReadEvent(int fd, void *privdata, int mask) { + (void) fd; + (void) mask; + + redisModuleEvents *e = (redisModuleEvents*)privdata; + redisAsyncHandleRead(e->context); +} + +static inline void redisModuleWriteEvent(int fd, void *privdata, int mask) { + (void) fd; + (void) mask; + + redisModuleEvents *e = (redisModuleEvents*)privdata; + redisAsyncHandleWrite(e->context); +} + +static inline void redisModuleAddRead(void *privdata) { + redisModuleEvents *e = (redisModuleEvents*)privdata; + if (!e->reading) { + e->reading = 1; + RedisModule_EventLoopAdd(e->fd, REDISMODULE_EVENTLOOP_READABLE, redisModuleReadEvent, e); + } +} + +static inline void redisModuleDelRead(void *privdata) { + redisModuleEvents *e = (redisModuleEvents*)privdata; + if (e->reading) { + e->reading = 0; + RedisModule_EventLoopDel(e->fd, REDISMODULE_EVENTLOOP_READABLE); + } +} + +static inline void redisModuleAddWrite(void *privdata) { + redisModuleEvents *e = (redisModuleEvents*)privdata; + if (!e->writing) { + e->writing = 1; + RedisModule_EventLoopAdd(e->fd, REDISMODULE_EVENTLOOP_WRITABLE, redisModuleWriteEvent, e); + } +} + +static inline void redisModuleDelWrite(void *privdata) { + redisModuleEvents *e = (redisModuleEvents*)privdata; + if (e->writing) { + e->writing = 0; + RedisModule_EventLoopDel(e->fd, REDISMODULE_EVENTLOOP_WRITABLE); + } +} + +static inline void redisModuleStopTimer(void *privdata) { + redisModuleEvents *e = (redisModuleEvents*)privdata; + if (e->timer_active) { + RedisModule_StopTimer(e->module_ctx, e->timer_id, NULL); + } + e->timer_active = 0; +} + +static inline void redisModuleCleanup(void *privdata) { + redisModuleEvents *e = (redisModuleEvents*)privdata; + redisModuleDelRead(privdata); + redisModuleDelWrite(privdata); + redisModuleStopTimer(privdata); + hi_free(e); +} + +static inline void redisModuleTimeout(RedisModuleCtx *ctx, void *privdata) { + (void) ctx; + + redisModuleEvents *e = (redisModuleEvents*)privdata; + e->timer_active = 0; + redisAsyncHandleTimeout(e->context); +} + +static inline void redisModuleSetTimeout(void *privdata, struct timeval tv) { + redisModuleEvents* e = (redisModuleEvents*)privdata; + + redisModuleStopTimer(privdata); + + mstime_t millis = tv.tv_sec * 1000 + tv.tv_usec / 1000.0; + e->timer_id = RedisModule_CreateTimer(e->module_ctx, millis, redisModuleTimeout, e); + e->timer_active = 1; +} + +/* Check if Redis version is compatible with the adapter. */ +static inline int redisModuleCompatibilityCheck(void) { + if (!RedisModule_EventLoopAdd || + !RedisModule_EventLoopDel || + !RedisModule_CreateTimer || + !RedisModule_StopTimer) { + return REDIS_ERR; + } + return REDIS_OK; +} + +static inline int redisModuleAttach(redisAsyncContext *ac, RedisModuleCtx *module_ctx) { + redisContext *c = &(ac->c); + redisModuleEvents *e; + + /* Nothing should be attached when something is already attached */ + if (ac->ev.data != NULL) + return REDIS_ERR; + + /* Create container for context and r/w events */ + e = (redisModuleEvents*)hi_malloc(sizeof(*e)); + if (e == NULL) + return REDIS_ERR; + + e->context = ac; + e->module_ctx = module_ctx; + e->fd = c->fd; + e->reading = e->writing = 0; + e->timer_active = 0; + + /* Register functions to start/stop listening for events */ + ac->ev.addRead = redisModuleAddRead; + ac->ev.delRead = redisModuleDelRead; + ac->ev.addWrite = redisModuleAddWrite; + ac->ev.delWrite = redisModuleDelWrite; + ac->ev.cleanup = redisModuleCleanup; + ac->ev.scheduleTimer = redisModuleSetTimeout; + ac->ev.data = e; + + return REDIS_OK; +} + +#endif diff --git a/async.c b/async.c index 65551142b..f82f567f8 100644 --- a/async.c +++ b/async.c @@ -140,6 +140,7 @@ static redisAsyncContext *redisAsyncInitialize(redisContext *c) { ac->ev.scheduleTimer = NULL; ac->onConnect = NULL; + ac->onConnectNC = NULL; ac->onDisconnect = NULL; ac->replies.head = NULL; @@ -148,6 +149,7 @@ static redisAsyncContext *redisAsyncInitialize(redisContext *c) { ac->sub.replies.tail = NULL; ac->sub.channels = channels; ac->sub.patterns = patterns; + ac->sub.pending_unsubs = 0; return ac; oom: @@ -225,17 +227,34 @@ redisAsyncContext *redisAsyncConnectUnix(const char *path) { return redisAsyncConnectWithOptions(&options); } -int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn) { - if (ac->onConnect == NULL) { - ac->onConnect = fn; +static int +redisAsyncSetConnectCallbackImpl(redisAsyncContext *ac, redisConnectCallback *fn, + redisConnectCallbackNC *fn_nc) +{ + /* If either are already set, this is an error */ + if (ac->onConnect || ac->onConnectNC) + return REDIS_ERR; - /* The common way to detect an established connection is to wait for - * the first write event to be fired. This assumes the related event - * library functions are already set. */ - _EL_ADD_WRITE(ac); - return REDIS_OK; + if (fn) { + ac->onConnect = fn; + } else if (fn_nc) { + ac->onConnectNC = fn_nc; } - return REDIS_ERR; + + /* The common way to detect an established connection is to wait for + * the first write event to be fired. This assumes the related event + * library functions are already set. */ + _EL_ADD_WRITE(ac); + + return REDIS_OK; +} + +int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn) { + return redisAsyncSetConnectCallbackImpl(ac, fn, NULL); +} + +int redisAsyncSetConnectCallbackNC(redisAsyncContext *ac, redisConnectCallbackNC *fn) { + return redisAsyncSetConnectCallbackImpl(ac, NULL, fn); } int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn) { @@ -302,6 +321,43 @@ static void __redisRunPushCallback(redisAsyncContext *ac, redisReply *reply) { } } +static void __redisRunConnectCallback(redisAsyncContext *ac, int status) +{ + if (ac->onConnect == NULL && ac->onConnectNC == NULL) + return; + + if (!(ac->c.flags & REDIS_IN_CALLBACK)) { + ac->c.flags |= REDIS_IN_CALLBACK; + if (ac->onConnect) { + ac->onConnect(ac, status); + } else { + ac->onConnectNC(ac, status); + } + ac->c.flags &= ~REDIS_IN_CALLBACK; + } else { + /* already in callback */ + if (ac->onConnect) { + ac->onConnect(ac, status); + } else { + ac->onConnectNC(ac, status); + } + } +} + +static void __redisRunDisconnectCallback(redisAsyncContext *ac, int status) +{ + if (ac->onDisconnect) { + if (!(ac->c.flags & REDIS_IN_CALLBACK)) { + ac->c.flags |= REDIS_IN_CALLBACK; + ac->onDisconnect(ac, status); + ac->c.flags &= ~REDIS_IN_CALLBACK; + } else { + /* already in callback */ + ac->onDisconnect(ac, status); + } + } +} + /* Helper function to free the context. */ static void __redisAsyncFree(redisAsyncContext *ac) { redisContext *c = &(ac->c); @@ -337,12 +393,11 @@ static void __redisAsyncFree(redisAsyncContext *ac) { /* Execute disconnect callback. When redisAsyncFree() initiated destroying * this context, the status will always be REDIS_OK. */ - if (ac->onDisconnect && (c->flags & REDIS_CONNECTED)) { - if (c->flags & REDIS_FREEING) { - ac->onDisconnect(ac,REDIS_OK); - } else { - ac->onDisconnect(ac,(ac->err == 0) ? REDIS_OK : REDIS_ERR); - } + if (c->flags & REDIS_CONNECTED) { + int status = ac->err == 0 ? REDIS_OK : REDIS_ERR; + if (c->flags & REDIS_FREEING) + status = REDIS_OK; + __redisRunDisconnectCallback(ac, status); } if (ac->dataCleanup) { @@ -358,7 +413,11 @@ static void __redisAsyncFree(redisAsyncContext *ac) { * free'ing. To do so, a flag is set on the context which is picked up by * redisProcessCallbacks(). Otherwise, the context is immediately free'd. */ void redisAsyncFree(redisAsyncContext *ac) { + if (ac == NULL) + return; + redisContext *c = &(ac->c); + c->flags |= REDIS_FREEING; if (!(c->flags & REDIS_IN_CALLBACK)) __redisAsyncFree(ac); @@ -411,11 +470,11 @@ void redisAsyncDisconnect(redisAsyncContext *ac) { static int __redisGetSubscribeCallback(redisAsyncContext *ac, redisReply *reply, redisCallback *dstcb) { redisContext *c = &(ac->c); dict *callbacks; - redisCallback *cb; + redisCallback *cb = NULL; dictEntry *de; int pvariant; char *stype; - sds sname; + sds sname = NULL; /* Match reply with the expected format of a pushed message. * The type and number of elements (3 to 4) are specified at: @@ -432,42 +491,43 @@ static int __redisGetSubscribeCallback(redisAsyncContext *ac, redisReply *reply, callbacks = ac->sub.channels; /* Locate the right callback */ - assert(reply->element[1]->type == REDIS_REPLY_STRING); - sname = sdsnewlen(reply->element[1]->str,reply->element[1]->len); - if (sname == NULL) - goto oom; + if (reply->element[1]->type == REDIS_REPLY_STRING) { + sname = sdsnewlen(reply->element[1]->str,reply->element[1]->len); + if (sname == NULL) goto oom; - de = dictFind(callbacks,sname); - if (de != NULL) { - cb = dictGetEntryVal(de); - - /* If this is an subscribe reply decrease pending counter. */ - if (strcasecmp(stype+pvariant,"subscribe") == 0) { - cb->pending_subs -= 1; + if ((de = dictFind(callbacks,sname)) != NULL) { + cb = dictGetEntryVal(de); + memcpy(dstcb,cb,sizeof(*dstcb)); } + } - memcpy(dstcb,cb,sizeof(*dstcb)); + /* If this is an subscribe reply decrease pending counter. */ + if (strcasecmp(stype+pvariant,"subscribe") == 0) { + assert(cb != NULL); + cb->pending_subs -= 1; - /* If this is an unsubscribe message, remove it. */ - if (strcasecmp(stype+pvariant,"unsubscribe") == 0) { - if (cb->pending_subs == 0) - dictDelete(callbacks,sname); + } else if (strcasecmp(stype+pvariant,"unsubscribe") == 0) { + if (cb == NULL) + ac->sub.pending_unsubs -= 1; + else if (cb->pending_subs == 0) + dictDelete(callbacks,sname); - /* If this was the last unsubscribe message, revert to - * non-subscribe mode. */ - assert(reply->element[2]->type == REDIS_REPLY_INTEGER); + /* If this was the last unsubscribe message, revert to + * non-subscribe mode. */ + assert(reply->element[2]->type == REDIS_REPLY_INTEGER); - /* Unset subscribed flag only when no pipelined pending subscribe. */ - if (reply->element[2]->integer == 0 - && dictSize(ac->sub.channels) == 0 - && dictSize(ac->sub.patterns) == 0) { - c->flags &= ~REDIS_SUBSCRIBED; + /* Unset subscribed flag only when no pipelined pending subscribe + * or pending unsubscribe replies. */ + if (reply->element[2]->integer == 0 + && dictSize(ac->sub.channels) == 0 + && dictSize(ac->sub.patterns) == 0 + && ac->sub.pending_unsubs == 0) { + c->flags &= ~REDIS_SUBSCRIBED; - /* Move ongoing regular command callbacks. */ - redisCallback cb; - while (__redisShiftCallback(&ac->sub.replies,&cb) == REDIS_OK) { - __redisPushCallback(&ac->replies,&cb); - } + /* Move ongoing regular command callbacks. */ + redisCallback cb; + while (__redisShiftCallback(&ac->sub.replies,&cb) == REDIS_OK) { + __redisPushCallback(&ac->replies,&cb); } } } @@ -479,6 +539,7 @@ static int __redisGetSubscribeCallback(redisAsyncContext *ac, redisReply *reply, return REDIS_OK; oom: __redisSetError(&(ac->c), REDIS_ERR_OOM, "Out of memory"); + __redisAsyncCopyError(ac); return REDIS_ERR; } @@ -540,7 +601,7 @@ void redisProcessCallbacks(redisAsyncContext *ac) { /* Even if the context is subscribed, pending regular * callbacks will get a reply before pub/sub messages arrive. */ - redisCallback cb = {NULL, NULL, 0, NULL}; + redisCallback cb = {NULL, NULL, 0, 0, NULL}; if (__redisShiftCallback(&ac->replies,&cb) != REDIS_OK) { /* * A spontaneous reply in a not-subscribed context can be the error @@ -601,7 +662,7 @@ void redisProcessCallbacks(redisAsyncContext *ac) { } static void __redisAsyncHandleConnectFailure(redisAsyncContext *ac) { - if (ac->onConnect) ac->onConnect(ac, REDIS_ERR); + __redisRunConnectCallback(ac, REDIS_ERR); __redisAsyncDisconnect(ac); } @@ -626,8 +687,19 @@ static int __redisAsyncHandleConnect(redisAsyncContext *ac) { return REDIS_ERR; } - if (ac->onConnect) ac->onConnect(ac, REDIS_OK); + /* flag us as fully connect, but allow the callback + * to disconnect. For that reason, permit the function + * to delete the context here after callback return. + */ c->flags |= REDIS_CONNECTED; + __redisRunConnectCallback(ac, REDIS_OK); + if ((ac->c.flags & REDIS_DISCONNECTING)) { + redisAsyncDisconnect(ac); + return REDIS_ERR; + } else if ((ac->c.flags & REDIS_FREEING)) { + redisAsyncFree(ac); + return REDIS_ERR; + } return REDIS_OK; } else { return REDIS_OK; @@ -651,6 +723,8 @@ void redisAsyncRead(redisAsyncContext *ac) { */ void redisAsyncHandleRead(redisAsyncContext *ac) { redisContext *c = &(ac->c); + /* must not be called from a callback */ + assert(!(c->flags & REDIS_IN_CALLBACK)); if (!(c->flags & REDIS_CONNECTED)) { /* Abort connect was not successful. */ @@ -684,6 +758,8 @@ void redisAsyncWrite(redisAsyncContext *ac) { void redisAsyncHandleWrite(redisAsyncContext *ac) { redisContext *c = &(ac->c); + /* must not be called from a callback */ + assert(!(c->flags & REDIS_IN_CALLBACK)); if (!(c->flags & REDIS_CONNECTED)) { /* Abort connect was not successful. */ @@ -700,6 +776,8 @@ void redisAsyncHandleWrite(redisAsyncContext *ac) { void redisAsyncHandleTimeout(redisAsyncContext *ac) { redisContext *c = &(ac->c); redisCallback cb; + /* must not be called from a callback */ + assert(!(c->flags & REDIS_IN_CALLBACK)); if ((c->flags & REDIS_CONNECTED)) { if (ac->replies.head == NULL && ac->sub.replies.head == NULL) { @@ -719,8 +797,8 @@ void redisAsyncHandleTimeout(redisAsyncContext *ac) { __redisAsyncCopyError(ac); } - if (!(c->flags & REDIS_CONNECTED) && ac->onConnect) { - ac->onConnect(ac, REDIS_ERR); + if (!(c->flags & REDIS_CONNECTED)) { + __redisRunConnectCallback(ac, REDIS_ERR); } while (__redisShiftCallback(&ac->replies, &cb) == REDIS_OK) { @@ -757,6 +835,7 @@ static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void redisContext *c = &(ac->c); redisCallback cb; struct dict *cbdict; + dictIterator it; dictEntry *de; redisCallback *existcb; int pvariant, hasnext; @@ -773,6 +852,7 @@ static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void cb.fn = fn; cb.privdata = privdata; cb.pending_subs = 1; + cb.unsubscribe_sent = 0; /* Find out which command will be appended. */ p = nextArgument(cmd,&cstr,&clen); @@ -812,6 +892,51 @@ static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void * subscribed to one or more channels or patterns. */ if (!(c->flags & REDIS_SUBSCRIBED)) return REDIS_ERR; + if (pvariant) + cbdict = ac->sub.patterns; + else + cbdict = ac->sub.channels; + + if (hasnext) { + /* Send an unsubscribe with specific channels/patterns. + * Bookkeeping the number of expected replies */ + while ((p = nextArgument(p,&astr,&alen)) != NULL) { + sname = sdsnewlen(astr,alen); + if (sname == NULL) + goto oom; + + de = dictFind(cbdict,sname); + if (de != NULL) { + existcb = dictGetEntryVal(de); + if (existcb->unsubscribe_sent == 0) + existcb->unsubscribe_sent = 1; + else + /* Already sent, reply to be ignored */ + ac->sub.pending_unsubs += 1; + } else { + /* Not subscribed to, reply to be ignored */ + ac->sub.pending_unsubs += 1; + } + sdsfree(sname); + } + } else { + /* Send an unsubscribe without specific channels/patterns. + * Bookkeeping the number of expected replies */ + int no_subs = 1; + dictInitIterator(&it,cbdict); + while ((de = dictNext(&it)) != NULL) { + existcb = dictGetEntryVal(de); + if (existcb->unsubscribe_sent == 0) { + existcb->unsubscribe_sent = 1; + no_subs = 0; + } + } + /* Unsubscribing to all channels/patterns, where none is + * subscribed to, results in a single reply to be ignored. */ + if (no_subs == 1) + ac->sub.pending_unsubs += 1; + } + /* (P)UNSUBSCRIBE does not have its own response: every channel or * pattern that is unsubscribed will receive a message. This means we * should not append a callback function for this command. */ diff --git a/async.h b/async.h index 4c65203c1..4f94660b1 100644 --- a/async.h +++ b/async.h @@ -46,6 +46,7 @@ typedef struct redisCallback { struct redisCallback *next; /* simple singly linked list */ redisCallbackFn *fn; int pending_subs; + int unsubscribe_sent; void *privdata; } redisCallback; @@ -57,6 +58,7 @@ typedef struct redisCallbackList { /* Connection callback prototypes */ typedef void (redisDisconnectCallback)(const struct redisAsyncContext*, int status); typedef void (redisConnectCallback)(const struct redisAsyncContext*, int status); +typedef void (redisConnectCallbackNC)(struct redisAsyncContext *, int status); typedef void(redisTimerCallback)(void *timer, void *privdata); /* Context for an async connection to Redis */ @@ -92,6 +94,7 @@ typedef struct redisAsyncContext { /* Called when the first write event was received. */ redisConnectCallback *onConnect; + redisConnectCallbackNC *onConnectNC; /* Regular command callbacks */ redisCallbackList replies; @@ -105,6 +108,7 @@ typedef struct redisAsyncContext { redisCallbackList replies; struct dict *channels; struct dict *patterns; + int pending_unsubs; } sub; /* Any configured RESP3 PUSH handler */ @@ -119,6 +123,7 @@ redisAsyncContext *redisAsyncConnectBindWithReuse(const char *ip, int port, const char *source_addr); redisAsyncContext *redisAsyncConnectUnix(const char *path); int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn); +int redisAsyncSetConnectCallbackNC(redisAsyncContext *ac, redisConnectCallbackNC *fn); int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn); redisAsyncPushFn *redisAsyncSetPushCallback(redisAsyncContext *ac, redisAsyncPushFn *fn); diff --git a/async_private.h b/async_private.h index b9d23fffd..ea0558d42 100644 --- a/async_private.h +++ b/async_private.h @@ -51,7 +51,7 @@ #define _EL_CLEANUP(ctx) do { \ if ((ctx)->ev.cleanup) (ctx)->ev.cleanup((ctx)->ev.data); \ ctx->ev.cleanup = NULL; \ - } while(0); + } while(0) static inline void refreshTimeout(redisAsyncContext *ctx) { #define REDIS_TIMER_ISSET(tvp) \ diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 49cd8d440..214898b07 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -25,12 +25,24 @@ if (LIBEVENT) TARGET_LINK_LIBRARIES(example-libevent hiredis event) ENDIF() +FIND_PATH(LIBHV hv/hv.h) +IF (LIBHV) + ADD_EXECUTABLE(example-libhv example-libhv.c) + TARGET_LINK_LIBRARIES(example-libhv hiredis hv) +ENDIF() + FIND_PATH(LIBUV uv.h) IF (LIBUV) ADD_EXECUTABLE(example-libuv example-libuv.c) TARGET_LINK_LIBRARIES(example-libuv hiredis uv) ENDIF() +FIND_PATH(LIBSDEVENT systemd/sd-event.h) +IF (LIBSDEVENT) + ADD_EXECUTABLE(example-libsdevent example-libsdevent.c) + TARGET_LINK_LIBRARIES(example-libsdevent hiredis systemd) +ENDIF() + IF (APPLE) FIND_LIBRARY(CF CoreFoundation) ADD_EXECUTABLE(example-macosx example-macosx.c) diff --git a/examples/example-libevent-ssl.c b/examples/example-libevent-ssl.c index 7d99af1ba..d0998bab3 100644 --- a/examples/example-libevent-ssl.c +++ b/examples/example-libevent-ssl.c @@ -56,7 +56,7 @@ int main (int argc, char **argv) { const char *caCert = argc > 5 ? argv[6] : NULL; redisSSLContext *ssl; - redisSSLContextError ssl_error; + redisSSLContextError ssl_error = REDIS_SSL_CTX_NONE; redisInitOpenSSL(); diff --git a/examples/example-libhv.c b/examples/example-libhv.c new file mode 100644 index 000000000..ac68b0086 --- /dev/null +++ b/examples/example-libhv.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +#include +#include +#include + +void getCallback(redisAsyncContext *c, void *r, void *privdata) { + redisReply *reply = r; + if (reply == NULL) return; + printf("argv[%s]: %s\n", (char*)privdata, reply->str); + + /* Disconnect after receiving the reply to GET */ + redisAsyncDisconnect(c); +} + +void debugCallback(redisAsyncContext *c, void *r, void *privdata) { + (void)privdata; + redisReply *reply = r; + + if (reply == NULL) { + printf("`DEBUG SLEEP` error: %s\n", c->errstr ? c->errstr : "unknown error"); + return; + } + + redisAsyncDisconnect(c); +} + +void connectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Connected...\n"); +} + +void disconnectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Disconnected...\n"); +} + +int main (int argc, char **argv) { +#ifndef _WIN32 + signal(SIGPIPE, SIG_IGN); +#endif + + redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); + if (c->err) { + /* Let *c leak for now... */ + printf("Error: %s\n", c->errstr); + return 1; + } + + hloop_t* loop = hloop_new(HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS); + redisLibhvAttach(c, loop); + redisAsyncSetTimeout(c, (struct timeval){.tv_sec = 0, .tv_usec = 500000}); + redisAsyncSetConnectCallback(c,connectCallback); + redisAsyncSetDisconnectCallback(c,disconnectCallback); + redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); + redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); + redisAsyncCommand(c, debugCallback, NULL, "DEBUG SLEEP %d", 1); + hloop_run(loop); + hloop_free(&loop); + return 0; +} diff --git a/examples/example-libsdevent.c b/examples/example-libsdevent.c new file mode 100644 index 000000000..c3b902b4e --- /dev/null +++ b/examples/example-libsdevent.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include + +#include +#include +#include + +void debugCallback(redisAsyncContext *c, void *r, void *privdata) { + (void)privdata; + redisReply *reply = r; + if (reply == NULL) { + /* The DEBUG SLEEP command will almost always fail, because we have set a 1 second timeout */ + printf("`DEBUG SLEEP` error: %s\n", c->errstr ? c->errstr : "unknown error"); + return; + } + /* Disconnect after receiving the reply of DEBUG SLEEP (which will not)*/ + redisAsyncDisconnect(c); +} + +void getCallback(redisAsyncContext *c, void *r, void *privdata) { + redisReply *reply = r; + if (reply == NULL) { + printf("`GET key` error: %s\n", c->errstr ? c->errstr : "unknown error"); + return; + } + printf("`GET key` result: argv[%s]: %s\n", (char*)privdata, reply->str); + + /* start another request that demonstrate timeout */ + redisAsyncCommand(c, debugCallback, NULL, "DEBUG SLEEP %f", 1.5); +} + +void connectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("connect error: %s\n", c->errstr); + return; + } + printf("Connected...\n"); +} + +void disconnectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("disconnect because of error: %s\n", c->errstr); + return; + } + printf("Disconnected...\n"); +} + +int main (int argc, char **argv) { + signal(SIGPIPE, SIG_IGN); + + struct sd_event *event; + sd_event_default(&event); + + redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); + if (c->err) { + printf("Error: %s\n", c->errstr); + redisAsyncFree(c); + return 1; + } + + redisLibsdeventAttach(c,event); + redisAsyncSetConnectCallback(c,connectCallback); + redisAsyncSetDisconnectCallback(c,disconnectCallback); + redisAsyncSetTimeout(c, (struct timeval){ .tv_sec = 1, .tv_usec = 0}); + + /* + In this demo, we first `set key`, then `get key` to demonstrate the basic usage of libsdevent adapter. + Then in `getCallback`, we start a `debug sleep` command to create 1.5 second long request. + Because we have set a 1 second timeout to the connection, the command will always fail with a + timeout error, which is shown in the `debugCallback`. + */ + + redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); + redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); + + /* sd-event does not quit when there are no handlers registered. Manually exit after 1.5 seconds */ + sd_event_source *s; + sd_event_add_time_relative(event, &s, CLOCK_MONOTONIC, 1500000, 1, NULL, NULL); + + sd_event_loop(event); + sd_event_source_disable_unref(s); + sd_event_unref(event); + return 0; +} diff --git a/examples/example-poll.c b/examples/example-poll.c new file mode 100644 index 000000000..954673dac --- /dev/null +++ b/examples/example-poll.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include + +#include +#include + +/* Put in the global scope, so that loop can be explicitly stopped */ +static int exit_loop = 0; + +void getCallback(redisAsyncContext *c, void *r, void *privdata) { + redisReply *reply = r; + if (reply == NULL) return; + printf("argv[%s]: %s\n", (char*)privdata, reply->str); + + /* Disconnect after receiving the reply to GET */ + redisAsyncDisconnect(c); +} + +void connectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + exit_loop = 1; + return; + } + + printf("Connected...\n"); +} + +void disconnectCallback(const redisAsyncContext *c, int status) { + exit_loop = 1; + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + + printf("Disconnected...\n"); +} + +int main (int argc, char **argv) { + signal(SIGPIPE, SIG_IGN); + + redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); + if (c->err) { + /* Let *c leak for now... */ + printf("Error: %s\n", c->errstr); + return 1; + } + + redisPollAttach(c); + redisAsyncSetConnectCallback(c,connectCallback); + redisAsyncSetDisconnectCallback(c,disconnectCallback); + redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); + redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); + while (!exit_loop) + { + redisPollTick(c, 0.1); + } + return 0; +} diff --git a/examples/example-redismoduleapi.c b/examples/example-redismoduleapi.c new file mode 100644 index 000000000..7d12f8a06 --- /dev/null +++ b/examples/example-redismoduleapi.c @@ -0,0 +1,101 @@ +#include +#include +#include +#include + +#include +#include +#include + +void debugCallback(redisAsyncContext *c, void *r, void *privdata) { + (void)privdata; //unused + redisReply *reply = r; + if (reply == NULL) { + /* The DEBUG SLEEP command will almost always fail, because we have set a 1 second timeout */ + printf("`DEBUG SLEEP` error: %s\n", c->errstr ? c->errstr : "unknown error"); + return; + } + /* Disconnect after receiving the reply of DEBUG SLEEP (which will not)*/ + redisAsyncDisconnect(c); +} + +void getCallback(redisAsyncContext *c, void *r, void *privdata) { + redisReply *reply = r; + if (reply == NULL) { + if (c->errstr) { + printf("errstr: %s\n", c->errstr); + } + return; + } + printf("argv[%s]: %s\n", (char*)privdata, reply->str); + + /* start another request that demonstrate timeout */ + redisAsyncCommand(c, debugCallback, NULL, "DEBUG SLEEP %f", 1.5); +} + +void connectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Connected...\n"); +} + +void disconnectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Disconnected...\n"); +} + +/* + * This example requires Redis 7.0 or above. + * + * 1- Compile this file as a shared library. Directory of "redismodule.h" must + * be in the include path. + * gcc -fPIC -shared -I../../redis/src/ -I.. example-redismoduleapi.c -o example-redismoduleapi.so + * + * 2- Load module: + * redis-server --loadmodule ./example-redismoduleapi.so value + */ +int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { + + int ret = RedisModule_Init(ctx, "example-redismoduleapi", 1, REDISMODULE_APIVER_1); + if (ret != REDISMODULE_OK) { + printf("error module init \n"); + return REDISMODULE_ERR; + } + + if (redisModuleCompatibilityCheck() != REDIS_OK) { + printf("Redis 7.0 or above is required! \n"); + return REDISMODULE_ERR; + } + + redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); + if (c->err) { + /* Let *c leak for now... */ + printf("Error: %s\n", c->errstr); + return 1; + } + + size_t len; + const char *val = RedisModule_StringPtrLen(argv[argc-1], &len); + + RedisModuleCtx *module_ctx = RedisModule_GetDetachedThreadSafeContext(ctx); + redisModuleAttach(c, module_ctx); + redisAsyncSetConnectCallback(c,connectCallback); + redisAsyncSetDisconnectCallback(c,disconnectCallback); + redisAsyncSetTimeout(c, (struct timeval){ .tv_sec = 1, .tv_usec = 0}); + + /* + In this demo, we first `set key`, then `get key` to demonstrate the basic usage of the adapter. + Then in `getCallback`, we start a `debug sleep` command to create 1.5 second long request. + Because we have set a 1 second timeout to the connection, the command will always fail with a + timeout error, which is shown in the `debugCallback`. + */ + + redisAsyncCommand(c, NULL, NULL, "SET key %b", val, len); + redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); + return 0; +} diff --git a/examples/example-ssl.c b/examples/example-ssl.c index b8ca44281..6d1e32e75 100644 --- a/examples/example-ssl.c +++ b/examples/example-ssl.c @@ -12,7 +12,7 @@ int main(int argc, char **argv) { unsigned int j; redisSSLContext *ssl; - redisSSLContextError ssl_error; + redisSSLContextError ssl_error = REDIS_SSL_CTX_NONE; redisContext *c; redisReply *reply; if (argc < 4) { @@ -27,9 +27,8 @@ int main(int argc, char **argv) { redisInitOpenSSL(); ssl = redisCreateSSLContext(ca, NULL, cert, key, NULL, &ssl_error); - if (!ssl) { - printf("SSL Context error: %s\n", - redisSSLContextGetError(ssl_error)); + if (!ssl || ssl_error != REDIS_SSL_CTX_NONE) { + printf("SSL Context error: %s\n", redisSSLContextGetError(ssl_error)); exit(1); } diff --git a/examples/example.c b/examples/example.c index f1b8b4a85..c0a9bb734 100644 --- a/examples/example.c +++ b/examples/example.c @@ -7,6 +7,54 @@ #include /* For struct timeval */ #endif +static void example_argv_command(redisContext *c, size_t n) { + char **argv, tmp[42]; + size_t *argvlen; + redisReply *reply; + + /* We're allocating two additional elements for command and key */ + argv = malloc(sizeof(*argv) * (2 + n)); + argvlen = malloc(sizeof(*argvlen) * (2 + n)); + + /* First the command */ + argv[0] = (char*)"RPUSH"; + argvlen[0] = sizeof("RPUSH") - 1; + + /* Now our key */ + argv[1] = (char*)"argvlist"; + argvlen[1] = sizeof("argvlist") - 1; + + /* Now add the entries we wish to add to the list */ + for (size_t i = 2; i < (n + 2); i++) { + argvlen[i] = snprintf(tmp, sizeof(tmp), "argv-element-%zu", i - 2); + argv[i] = strdup(tmp); + } + + /* Execute the command using redisCommandArgv. We're sending the arguments with + * two explicit arrays. One for each argument's string, and the other for its + * length. */ + reply = redisCommandArgv(c, n + 2, (const char **)argv, (const size_t*)argvlen); + + if (reply == NULL || c->err) { + fprintf(stderr, "Error: Couldn't execute redisCommandArgv\n"); + exit(1); + } + + if (reply->type == REDIS_REPLY_INTEGER) { + printf("%s reply: %lld\n", argv[0], reply->integer); + } + + freeReplyObject(reply); + + /* Clean up */ + for (size_t i = 2; i < (n + 2); i++) { + free(argv[i]); + } + + free(argv); + free(argvlen); +} + int main(int argc, char **argv) { unsigned int j, isunix = 0; redisContext *c; @@ -87,6 +135,9 @@ int main(int argc, char **argv) { } freeReplyObject(reply); + /* See function for an example of redisCommandArgv */ + example_argv_command(c, 10); + /* Disconnects and frees the context */ redisFree(c); diff --git a/fuzzing/format_command_fuzzer.c b/fuzzing/format_command_fuzzer.c index 91adeac58..de125e08d 100644 --- a/fuzzing/format_command_fuzzer.c +++ b/fuzzing/format_command_fuzzer.c @@ -48,10 +48,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { memcpy(new_str, data, size); new_str[size] = '\0'; - redisFormatCommand(&cmd, new_str); - - if (cmd != NULL) + if (redisFormatCommand(&cmd, new_str) != -1) hi_free(cmd); + free(new_str); return 0; } diff --git a/hiredis-config.cmake.in b/hiredis-config.cmake.in index 98851dcee..033985852 100644 --- a/hiredis-config.cmake.in +++ b/hiredis-config.cmake.in @@ -2,11 +2,11 @@ set_and_check(hiredis_INCLUDEDIR "@PACKAGE_INCLUDE_INSTALL_DIR@") -IF (NOT TARGET hiredis::hiredis) +IF (NOT TARGET hiredis::@hiredis_export_name@) INCLUDE(${CMAKE_CURRENT_LIST_DIR}/hiredis-targets.cmake) ENDIF() -SET(hiredis_LIBRARIES hiredis::hiredis) +SET(hiredis_LIBRARIES hiredis::@hiredis_export_name@) SET(hiredis_INCLUDE_DIRS ${hiredis_INCLUDEDIR}) check_required_components(hiredis) diff --git a/hiredis.c b/hiredis.c index 91086f6f6..9d8a500c7 100644 --- a/hiredis.c +++ b/hiredis.c @@ -48,6 +48,7 @@ extern int redisContextUpdateConnectTimeout(redisContext *c, const struct timeva extern int redisContextUpdateCommandTimeout(redisContext *c, const struct timeval *timeout); static redisContextFuncs redisContextDefaultFuncs = { + .close = redisNetClose, .free_privctx = NULL, .async_read = redisAsyncRead, .async_write = redisAsyncWrite, @@ -221,6 +222,9 @@ static void *createIntegerObject(const redisReadTask *task, long long value) { static void *createDoubleObject(const redisReadTask *task, double value, char *str, size_t len) { redisReply *r, *parent; + if (len == SIZE_MAX) // Prevents hi_malloc(0) if len equals to SIZE_MAX + return NULL; + r = createReplyObject(REDIS_REPLY_DOUBLE); if (r == NULL) return NULL; @@ -399,6 +403,11 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) { /* Copy va_list before consuming with va_arg */ va_copy(_cpy,ap); + /* Make sure we have more characters otherwise strchr() accepts + * '\0' as an integer specifier. This is checked after above + * va_copy() to avoid UB in fmt_invalid's call to va_end(). */ + if (*_p == '\0') goto fmt_invalid; + /* Integer conversion (without modifiers) */ if (strchr(intfmts,*_p) != NULL) { va_arg(ap,int); @@ -477,6 +486,8 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) { touched = 1; c++; + if (*c == '\0') + break; } c++; } @@ -719,7 +730,10 @@ static redisContext *redisContextInit(void) { void redisFree(redisContext *c) { if (c == NULL) return; - redisNetClose(c); + + if (c->funcs && c->funcs->close) { + c->funcs->close(c); + } sdsfree(c->obuf); redisReaderFree(c->reader); @@ -733,7 +747,7 @@ void redisFree(redisContext *c) { if (c->privdata && c->free_privdata) c->free_privdata(c->privdata); - if (c->funcs->free_privctx) + if (c->funcs && c->funcs->free_privctx) c->funcs->free_privctx(c->privctx); memset(c, 0xff, sizeof(*c)); @@ -756,7 +770,9 @@ int redisReconnect(redisContext *c) { c->privctx = NULL; } - redisNetClose(c); + if (c->funcs && c->funcs->close) { + c->funcs->close(c); + } sdsfree(c->obuf); redisReaderFree(c->reader); @@ -806,6 +822,12 @@ redisContext *redisConnectWithOptions(const redisOptions *options) { if (options->options & REDIS_OPT_NOAUTOFREEREPLIES) { c->flags |= REDIS_NO_AUTO_FREE_REPLIES; } + if (options->options & REDIS_OPT_PREFER_IPV4) { + c->flags |= REDIS_PREFER_IPV4; + } + if (options->options & REDIS_OPT_PREFER_IPV6) { + c->flags |= REDIS_PREFER_IPV6; + } /* Set any user supplied RESP3 PUSH handler or use freeReplyObject * as a default unless specifically flagged that we don't want one. */ @@ -838,7 +860,9 @@ redisContext *redisConnectWithOptions(const redisOptions *options) { return NULL; } - if (options->command_timeout != NULL && (c->flags & REDIS_BLOCK) && c->fd != REDIS_INVALID_FD) { + if (c->err == 0 && c->fd != REDIS_INVALID_FD && + options->command_timeout != NULL && (c->flags & REDIS_BLOCK)) + { redisContextSetTimeout(c, *options->command_timeout); } @@ -920,11 +944,18 @@ int redisSetTimeout(redisContext *c, const struct timeval tv) { return REDIS_ERR; } +int redisEnableKeepAliveWithInterval(redisContext *c, int interval) { + return redisKeepAlive(c, interval); +} + /* Enable connection KeepAlive. */ int redisEnableKeepAlive(redisContext *c) { - if (redisKeepAlive(c, REDIS_KEEPALIVE_INTERVAL) != REDIS_OK) - return REDIS_ERR; - return REDIS_OK; + return redisKeepAlive(c, REDIS_KEEPALIVE_INTERVAL); +} + +/* Set the socket option TCP_USER_TIMEOUT. */ +int redisSetTcpUserTimeout(redisContext *c, unsigned int timeout) { + return redisContextSetTcpUserTimeout(c, timeout); } /* Set a user provided RESP3 PUSH handler and return any old one set. */ @@ -964,8 +995,8 @@ int redisBufferRead(redisContext *c) { * successfully written to the socket. When the buffer is empty after the * write operation, "done" is set to 1 (if given). * - * Returns REDIS_ERR if an error occurred trying to write and sets - * c->errstr to hold the appropriate error string. + * Returns REDIS_ERR if an unrecoverable error occurred in the underlying + * c->funcs->write function. */ int redisBufferWrite(redisContext *c, int *done) { diff --git a/hiredis.h b/hiredis.h index b378128b5..2291d3eba 100644 --- a/hiredis.h +++ b/hiredis.h @@ -46,9 +46,9 @@ typedef long long ssize_t; #include "alloc.h" /* for allocation wrappers */ #define HIREDIS_MAJOR 1 -#define HIREDIS_MINOR 0 -#define HIREDIS_PATCH 3 -#define HIREDIS_SONAME 1.0.3-dev +#define HIREDIS_MINOR 1 +#define HIREDIS_PATCH 1 +#define HIREDIS_SONAME 1.1.1-dev /* Connection type can be blocking or non-blocking and is set in the * least significant bit of the flags field in redisContext. */ @@ -92,6 +92,11 @@ typedef long long ssize_t; /* Flag that indicates the user does not want replies to be automatically freed */ #define REDIS_NO_AUTO_FREE_REPLIES 0x400 +/* Flags to prefer IPv6 or IPv4 when doing DNS lookup. (If both are set, + * AF_UNSPEC is used.) */ +#define REDIS_PREFER_IPV4 0x800 +#define REDIS_PREFER_IPV6 0x1000 + #define REDIS_KEEPALIVE_INTERVAL 15 /* seconds */ /* number of times we retry to connect in the case of EADDRNOTAVAIL and @@ -149,20 +154,17 @@ struct redisSsl; #define REDIS_OPT_NONBLOCK 0x01 #define REDIS_OPT_REUSEADDR 0x02 - -/** - * Don't automatically free the async object on a connection failure, - * or other implicit conditions. Only free on an explicit call to disconnect() or free() - */ -#define REDIS_OPT_NOAUTOFREE 0x04 - -/* Don't automatically intercept and free RESP3 PUSH replies. */ -#define REDIS_OPT_NO_PUSH_AUTOFREE 0x08 - -/** - * Don't automatically free replies - */ -#define REDIS_OPT_NOAUTOFREEREPLIES 0x10 +#define REDIS_OPT_NOAUTOFREE 0x04 /* Don't automatically free the async + * object on a connection failure, or + * other implicit conditions. Only free + * on an explicit call to disconnect() + * or free() */ +#define REDIS_OPT_NO_PUSH_AUTOFREE 0x08 /* Don't automatically intercept and + * free RESP3 PUSH replies. */ +#define REDIS_OPT_NOAUTOFREEREPLIES 0x10 /* Don't automatically free replies. */ +#define REDIS_OPT_PREFER_IPV4 0x20 /* Prefer IPv4 in DNS lookups. */ +#define REDIS_OPT_PREFER_IPV6 0x40 /* Prefer IPv6 in DNS lookups. */ +#define REDIS_OPT_PREFER_IP_UNSPEC (REDIS_OPT_PREFER_IPV4 | REDIS_OPT_PREFER_IPV6) /* In Unix systems a file descriptor is a regular signed int, with -1 * representing an invalid descriptor. In Windows it is a SOCKET @@ -220,27 +222,37 @@ typedef struct { /** * Helper macros to initialize options to their specified fields. */ -#define REDIS_OPTIONS_SET_TCP(opts, ip_, port_) \ - (opts)->type = REDIS_CONN_TCP; \ - (opts)->endpoint.tcp.ip = ip_; \ - (opts)->endpoint.tcp.port = port_; +#define REDIS_OPTIONS_SET_TCP(opts, ip_, port_) do { \ + (opts)->type = REDIS_CONN_TCP; \ + (opts)->endpoint.tcp.ip = ip_; \ + (opts)->endpoint.tcp.port = port_; \ + } while(0) -#define REDIS_OPTIONS_SET_UNIX(opts, path) \ - (opts)->type = REDIS_CONN_UNIX; \ - (opts)->endpoint.unix_socket = path; +#define REDIS_OPTIONS_SET_UNIX(opts, path) do { \ + (opts)->type = REDIS_CONN_UNIX; \ + (opts)->endpoint.unix_socket = path; \ + } while(0) -#define REDIS_OPTIONS_SET_PRIVDATA(opts, data, dtor) \ - (opts)->privdata = data; \ - (opts)->free_privdata = dtor; \ +#define REDIS_OPTIONS_SET_PRIVDATA(opts, data, dtor) do { \ + (opts)->privdata = data; \ + (opts)->free_privdata = dtor; \ + } while(0) typedef struct redisContextFuncs { + void (*close)(struct redisContext *); void (*free_privctx)(void *); void (*async_read)(struct redisAsyncContext *); void (*async_write)(struct redisAsyncContext *); + + /* Read/Write data to the underlying communication stream, returning the + * number of bytes read/written. In the event of an unrecoverable error + * these functions shall return a value < 0. In the event of a + * recoverable error, they should return 0. */ ssize_t (*read)(struct redisContext *, char *, size_t); ssize_t (*write)(struct redisContext *); } redisContextFuncs; + /* Context for a connection to Redis */ typedef struct redisContext { const redisContextFuncs *funcs; /* Function table */ @@ -310,6 +322,8 @@ int redisReconnect(redisContext *c); redisPushFn *redisSetPushCallback(redisContext *c, redisPushFn *fn); int redisSetTimeout(redisContext *c, const struct timeval tv); int redisEnableKeepAlive(redisContext *c); +int redisEnableKeepAliveWithInterval(redisContext *c, int interval); +int redisSetTcpUserTimeout(redisContext *c, unsigned int timeout); void redisFree(redisContext *c); redisFD redisFreeKeepFd(redisContext *c); int redisBufferRead(redisContext *c); diff --git a/hiredis.pc.in b/hiredis.pc.in index 91b773183..c7b8e0e36 100644 --- a/hiredis.pc.in +++ b/hiredis.pc.in @@ -9,4 +9,4 @@ Name: hiredis Description: Minimalistic C client library for Redis. Version: @PROJECT_VERSION@ Libs: -L${libdir} -lhiredis -Cflags: -I${pkgincludedir} -D_FILE_OFFSET_BITS=64 +Cflags: -I${pkgincludedir} -I${includedir} -D_FILE_OFFSET_BITS=64 diff --git a/hiredis_ssl-config.cmake.in b/hiredis_ssl-config.cmake.in index 9a283dfc2..eeb19d1d1 100644 --- a/hiredis_ssl-config.cmake.in +++ b/hiredis_ssl-config.cmake.in @@ -2,6 +2,9 @@ set_and_check(hiredis_ssl_INCLUDEDIR "@PACKAGE_INCLUDE_INSTALL_DIR@") +include(CMakeFindDependencyMacro) +find_dependency(OpenSSL) + IF (NOT TARGET hiredis::hiredis_ssl) INCLUDE(${CMAKE_CURRENT_LIST_DIR}/hiredis_ssl-targets.cmake) ENDIF() diff --git a/hiredis_ssl.h b/hiredis_ssl.h index e3d3e1cf5..5f92cca9b 100644 --- a/hiredis_ssl.h +++ b/hiredis_ssl.h @@ -56,11 +56,33 @@ typedef enum { REDIS_SSL_CTX_CERT_KEY_REQUIRED, /* Client cert and key must both be specified or skipped */ REDIS_SSL_CTX_CA_CERT_LOAD_FAILED, /* Failed to load CA Certificate or CA Path */ REDIS_SSL_CTX_CLIENT_CERT_LOAD_FAILED, /* Failed to load client certificate */ + REDIS_SSL_CTX_CLIENT_DEFAULT_CERT_FAILED, /* Failed to set client default certificate directory */ REDIS_SSL_CTX_PRIVATE_KEY_LOAD_FAILED, /* Failed to load private key */ - REDIS_SSL_CTX_OS_CERTSTORE_OPEN_FAILED, /* Failed to open system certifcate store */ + REDIS_SSL_CTX_OS_CERTSTORE_OPEN_FAILED, /* Failed to open system certificate store */ REDIS_SSL_CTX_OS_CERT_ADD_FAILED /* Failed to add CA certificates obtained from system to the SSL context */ } redisSSLContextError; +/* Constants that mirror OpenSSL's verify modes. By default, + * REDIS_SSL_VERIFY_PEER is used with redisCreateSSLContext(). + * Some Redis clients disable peer verification if there are no + * certificates specified. + */ +#define REDIS_SSL_VERIFY_NONE 0x00 +#define REDIS_SSL_VERIFY_PEER 0x01 +#define REDIS_SSL_VERIFY_FAIL_IF_NO_PEER_CERT 0x02 +#define REDIS_SSL_VERIFY_CLIENT_ONCE 0x04 +#define REDIS_SSL_VERIFY_POST_HANDSHAKE 0x08 + +/* Options to create an OpenSSL context. */ +typedef struct { + const char *cacert_filename; + const char *capath; + const char *cert_filename; + const char *private_key_filename; + const char *server_name; + int verify_mode; +} redisSSLOptions; + /** * Return the error message corresponding with the specified error code. */ @@ -101,6 +123,18 @@ redisSSLContext *redisCreateSSLContext(const char *cacert_filename, const char * const char *cert_filename, const char *private_key_filename, const char *server_name, redisSSLContextError *error); +/** + * Helper function to initialize an OpenSSL context that can be used + * to initiate SSL connections. This is a more extensible version of redisCreateSSLContext(). + * + * options contains a structure of SSL options to use. + * + * If error is non-null, it will be populated in case the context creation fails + * (returning a NULL). +*/ +redisSSLContext *redisCreateSSLContextWithOptions(redisSSLOptions *options, + redisSSLContextError *error); + /** * Free a previously created OpenSSL context. */ diff --git a/hiredis_ssl.pc.in b/hiredis_ssl.pc.in index 588a978a5..f7bdd99d4 100644 --- a/hiredis_ssl.pc.in +++ b/hiredis_ssl.pc.in @@ -1,6 +1,7 @@ prefix=@CMAKE_INSTALL_PREFIX@ +install_libdir=@CMAKE_INSTALL_LIBDIR@ exec_prefix=${prefix} -libdir=${exec_prefix}/lib +libdir=${exec_prefix}/${install_libdir} includedir=${prefix}/include pkgincludedir=${includedir}/hiredis diff --git a/net.c b/net.c index c6b0e5d8e..ccd7f166a 100644 --- a/net.c +++ b/net.c @@ -50,6 +50,8 @@ /* Defined in hiredis.c */ void __redisSetError(redisContext *c, int type, const char *str); +int redisContextUpdateCommandTimeout(redisContext *c, const struct timeval *timeout); + void redisNetClose(redisContext *c) { if (c && c->fd != REDIS_INVALID_FD) { close(c->fd); @@ -68,7 +70,7 @@ ssize_t redisNetRead(redisContext *c, char *buf, size_t bufcap) { __redisSetError(c, REDIS_ERR_TIMEOUT, "recv timeout"); return -1; } else { - __redisSetError(c, REDIS_ERR_IO, NULL); + __redisSetError(c, REDIS_ERR_IO, strerror(errno)); return -1; } } else if (nread == 0) { @@ -80,15 +82,19 @@ ssize_t redisNetRead(redisContext *c, char *buf, size_t bufcap) { } ssize_t redisNetWrite(redisContext *c) { - ssize_t nwritten = send(c->fd, c->obuf, sdslen(c->obuf), 0); + ssize_t nwritten; + + nwritten = send(c->fd, c->obuf, sdslen(c->obuf), 0); if (nwritten < 0) { if ((errno == EWOULDBLOCK && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) { - /* Try again later */ + /* Try again */ + return 0; } else { - __redisSetError(c, REDIS_ERR_IO, NULL); + __redisSetError(c, REDIS_ERR_IO, strerror(errno)); return -1; } } + return nwritten; } @@ -166,6 +172,7 @@ int redisKeepAlive(redisContext *c, int interval) { int val = 1; redisFD fd = c->fd; +#ifndef _WIN32 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1){ __redisSetError(c,REDIS_ERR_OTHER,strerror(errno)); return REDIS_ERR; @@ -199,7 +206,15 @@ int redisKeepAlive(redisContext *c, int interval) { } #endif #endif +#else + int res; + res = win32_redisKeepAlive(fd, interval * 1000); + if (res != 0) { + __redisSetError(c, REDIS_ERR_OTHER, strerror(res)); + return REDIS_ERR; + } +#endif return REDIS_OK; } @@ -213,6 +228,22 @@ int redisSetTcpNoDelay(redisContext *c) { return REDIS_OK; } +int redisContextSetTcpUserTimeout(redisContext *c, unsigned int timeout) { + int res; +#ifdef TCP_USER_TIMEOUT + res = setsockopt(c->fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &timeout, sizeof(timeout)); +#else + res = -1; + (void)timeout; +#endif + if (res == -1); { + __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(TCP_USER_TIMEOUT)"); + redisNetClose(c); + return REDIS_ERR; + } + return REDIS_OK; +} + #define __MAX_MSEC (((LONG_MAX) - 999) / 1000) static int redisContextTimeoutMsec(redisContext *c, long *result) @@ -223,6 +254,7 @@ static int redisContextTimeoutMsec(redisContext *c, long *result) /* Only use timeout when not NULL. */ if (timeout != NULL) { if (timeout->tv_usec > 1000000 || timeout->tv_sec > __MAX_MSEC) { + __redisSetError(c, REDIS_ERR_IO, "Invalid timeout specified"); *result = msec; return REDIS_ERR; } @@ -277,12 +309,28 @@ int redisCheckConnectDone(redisContext *c, int *completed) { *completed = 1; return REDIS_OK; } - switch (errno) { + int error = errno; + if (error == EINPROGRESS) { + /* must check error to see if connect failed. Get the socket error */ + int fail, so_error; + socklen_t optlen = sizeof(so_error); + fail = getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &so_error, &optlen); + if (fail == 0) { + if (so_error == 0) { + /* Socket is connected! */ + *completed = 1; + return REDIS_OK; + } + /* connection error; */ + errno = so_error; + error = so_error; + } + } + switch (error) { case EISCONN: *completed = 1; return REDIS_OK; case EALREADY: - case EINPROGRESS: case EWOULDBLOCK: *completed = 0; return REDIS_OK; @@ -317,6 +365,10 @@ int redisContextSetTimeout(redisContext *c, const struct timeval tv) { const void *to_ptr = &tv; size_t to_sz = sizeof(tv); + if (redisContextUpdateCommandTimeout(c, &tv) != REDIS_OK) { + __redisSetError(c, REDIS_ERR_OOM, "Out of memory"); + return REDIS_ERR; + } if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,to_ptr,to_sz) == -1) { __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_RCVTIMEO)"); return REDIS_ERR; @@ -400,7 +452,6 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port, } if (redisContextTimeoutMsec(c, &timeout_msec) != REDIS_OK) { - __redisSetError(c, REDIS_ERR_IO, "Invalid timeout specified"); goto error; } @@ -417,17 +468,25 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port, hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; - /* Try with IPv6 if no IPv4 address was found. We do it in this order since - * in a Redis client you can't afford to test if you have IPv6 connectivity - * as this would add latency to every connect. Otherwise a more sensible - * route could be: Use IPv6 if both addresses are available and there is IPv6 - * connectivity. */ - if ((rv = getaddrinfo(c->tcp.host,_port,&hints,&servinfo)) != 0) { - hints.ai_family = AF_INET6; - if ((rv = getaddrinfo(addr,_port,&hints,&servinfo)) != 0) { - __redisSetError(c,REDIS_ERR_OTHER,gai_strerror(rv)); - return REDIS_ERR; - } + /* DNS lookup. To use dual stack, set both flags to prefer both IPv4 and + * IPv6. By default, for historical reasons, we try IPv4 first and then we + * try IPv6 only if no IPv4 address was found. */ + if (c->flags & REDIS_PREFER_IPV6 && c->flags & REDIS_PREFER_IPV4) + hints.ai_family = AF_UNSPEC; + else if (c->flags & REDIS_PREFER_IPV6) + hints.ai_family = AF_INET6; + else + hints.ai_family = AF_INET; + + rv = getaddrinfo(c->tcp.host, _port, &hints, &servinfo); + if (rv != 0 && hints.ai_family != AF_UNSPEC) { + /* Try again with the other IP version. */ + hints.ai_family = (hints.ai_family == AF_INET) ? AF_INET6 : AF_INET; + rv = getaddrinfo(c->tcp.host, _port, &hints, &servinfo); + } + if (rv != 0) { + __redisSetError(c, REDIS_ERR_OTHER, gai_strerror(rv)); + return REDIS_ERR; } for (p = servinfo; p != NULL; p = p->ai_next) { addrretry: diff --git a/net.h b/net.h index 9f43283a5..e15d46264 100644 --- a/net.h +++ b/net.h @@ -52,5 +52,6 @@ int redisKeepAlive(redisContext *c, int interval); int redisCheckConnectDone(redisContext *c, int *completed); int redisSetTcpNoDelay(redisContext *c); +int redisContextSetTcpUserTimeout(redisContext *c, unsigned int timeout); #endif diff --git a/read.c b/read.c index de62b9ab0..9c8f86906 100644 --- a/read.c +++ b/read.c @@ -303,11 +303,14 @@ static int processLineItem(redisReader *r) { d = INFINITY; /* Positive infinite. */ } else if (len == 4 && strcasecmp(buf,"-inf") == 0) { d = -INFINITY; /* Negative infinite. */ + } else if ((len == 3 && strcasecmp(buf,"nan") == 0) || + (len == 4 && strcasecmp(buf, "-nan") == 0)) { + d = NAN; /* nan. */ } else { d = strtod((char*)buf,&eptr); /* RESP3 only allows "inf", "-inf", and finite values, while - * strtod() allows other variations on infinity, NaN, - * etc. We explicity handle our two allowed infinite cases + * strtod() allows other variations on infinity, + * etc. We explicity handle our two allowed infinite cases and NaN * above, so strtod() should only result in finite values. */ if (buf[0] == '\0' || eptr != &buf[len] || !isfinite(d)) { __redisReaderSetError(r,REDIS_ERR_PROTOCOL, @@ -374,7 +377,7 @@ static int processLineItem(redisReader *r) { if (r->fn && r->fn->createString) obj = r->fn->createString(cur,p,len); else - obj = (void*)(size_t)(cur->type); + obj = (void*)(uintptr_t)(cur->type); } if (obj == NULL) { @@ -439,7 +442,7 @@ static int processBulkItem(redisReader *r) { if (r->fn && r->fn->createString) obj = r->fn->createString(cur,s+2,len); else - obj = (void*)(long)cur->type; + obj = (void*)(uintptr_t)cur->type; success = 1; } } @@ -536,7 +539,7 @@ static int processAggregateItem(redisReader *r) { if (r->fn && r->fn->createArray) obj = r->fn->createArray(cur,elements); else - obj = (void*)(long)cur->type; + obj = (void*)(uintptr_t)cur->type; if (obj == NULL) { __redisReaderSetErrorOOM(r); diff --git a/sds.c b/sds.c index 35baa057e..a20ba1912 100644 --- a/sds.c +++ b/sds.c @@ -90,6 +90,7 @@ sds sdsnewlen(const void *init, size_t initlen) { int hdrlen = sdsHdrSize(type); unsigned char *fp; /* flags pointer. */ + if (hdrlen+initlen+1 <= initlen) return NULL; /* Catch size_t overflow */ sh = s_malloc(hdrlen+initlen+1); if (sh == NULL) return NULL; if (!init) @@ -174,7 +175,7 @@ void sdsfree(sds s) { * the output will be "6" as the string was modified but the logical length * remains 6 bytes. */ void sdsupdatelen(sds s) { - int reallen = strlen(s); + size_t reallen = strlen(s); sdssetlen(s, reallen); } @@ -196,7 +197,7 @@ void sdsclear(sds s) { sds sdsMakeRoomFor(sds s, size_t addlen) { void *sh, *newsh; size_t avail = sdsavail(s); - size_t len, newlen; + size_t len, newlen, reqlen; char type, oldtype = s[-1] & SDS_TYPE_MASK; int hdrlen; @@ -205,7 +206,8 @@ sds sdsMakeRoomFor(sds s, size_t addlen) { len = sdslen(s); sh = (char*)s-sdsHdrSize(oldtype); - newlen = (len+addlen); + reqlen = newlen = (len+addlen); + if (newlen <= len) return NULL; /* Catch size_t overflow */ if (newlen < SDS_MAX_PREALLOC) newlen *= 2; else @@ -219,6 +221,7 @@ sds sdsMakeRoomFor(sds s, size_t addlen) { if (type == SDS_TYPE_5) type = SDS_TYPE_8; hdrlen = sdsHdrSize(type); + if (hdrlen+newlen+1 <= reqlen) return NULL; /* Catch size_t overflow */ if (oldtype==type) { newsh = s_realloc(sh, hdrlen+newlen+1); if (newsh == NULL) return NULL; @@ -580,7 +583,7 @@ sds sdscatprintf(sds s, const char *fmt, ...) { */ sds sdscatfmt(sds s, char const *fmt, ...) { const char *f = fmt; - int i; + long i; va_list ap; va_start(ap,fmt); @@ -755,14 +758,14 @@ int sdsrange(sds s, ssize_t start, ssize_t end) { /* Apply tolower() to every character of the sds string 's'. */ void sdstolower(sds s) { - int len = sdslen(s), j; + size_t len = sdslen(s), j; for (j = 0; j < len; j++) s[j] = tolower(s[j]); } /* Apply toupper() to every character of the sds string 's'. */ void sdstoupper(sds s) { - int len = sdslen(s), j; + size_t len = sdslen(s), j; for (j = 0; j < len; j++) s[j] = toupper(s[j]); } diff --git a/sds.h b/sds.h index eda8833b5..d9b67610f 100644 --- a/sds.h +++ b/sds.h @@ -35,9 +35,11 @@ #define SDS_MAX_PREALLOC (1024*1024) #ifdef _MSC_VER -#define __attribute__(x) typedef long long ssize_t; #define SSIZE_MAX (LLONG_MAX >> 1) +#ifndef __clang__ +#define __attribute__(x) +#endif #endif #include diff --git a/sockcompat.c b/sockcompat.c index f99d14b05..378745f4b 100644 --- a/sockcompat.c +++ b/sockcompat.c @@ -180,10 +180,17 @@ int win32_connect(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen) /* For Winsock connect(), the WSAEWOULDBLOCK error means the same thing as * EINPROGRESS for POSIX connect(), so we do that translation to keep POSIX - * logic consistent. */ - if (errno == EWOULDBLOCK) { + * logic consistent. + * Additionally, WSAALREADY is can be reported as WSAEINVAL to and this is + * translated to EIO. Convert appropriately + */ + int err = errno; + if (err == EWOULDBLOCK) { errno = EINPROGRESS; } + else if (err == EIO) { + errno = EALREADY; + } return ret != SOCKET_ERROR ? ret : -1; } @@ -205,6 +212,14 @@ int win32_getsockopt(SOCKET sockfd, int level, int optname, void *optval, sockle } else { ret = getsockopt(sockfd, level, optname, (char*)optval, optlen); } + if (ret != SOCKET_ERROR && level == SOL_SOCKET && optname == SO_ERROR) { + /* translate SO_ERROR codes, if non-zero */ + int err = *(int*)optval; + if (err != 0) { + err = _wsaErrorToErrno(err); + *(int*)optval = err; + } + } _updateErrno(ret != SOCKET_ERROR); return ret != SOCKET_ERROR ? ret : -1; } @@ -245,4 +260,21 @@ int win32_poll(struct pollfd *fds, nfds_t nfds, int timeout) { _updateErrno(ret != SOCKET_ERROR); return ret != SOCKET_ERROR ? ret : -1; } + +int win32_redisKeepAlive(SOCKET sockfd, int interval_ms) { + struct tcp_keepalive cfg; + DWORD bytes_in; + int res; + + cfg.onoff = 1; + cfg.keepaliveinterval = interval_ms; + cfg.keepalivetime = interval_ms; + + res = WSAIoctl(sockfd, SIO_KEEPALIVE_VALS, &cfg, + sizeof(struct tcp_keepalive), NULL, 0, + &bytes_in, NULL, NULL); + + return res == 0 ? 0 : _wsaErrorToErrno(res); +} + #endif /* _WIN32 */ diff --git a/sockcompat.h b/sockcompat.h index 85810e848..6ca5d9fca 100644 --- a/sockcompat.h +++ b/sockcompat.h @@ -50,6 +50,7 @@ #include #include #include +#include #ifdef _MSC_VER typedef long long ssize_t; @@ -71,6 +72,8 @@ ssize_t win32_send(SOCKET sockfd, const void *buf, size_t len, int flags); typedef ULONG nfds_t; int win32_poll(struct pollfd *fds, nfds_t nfds, int timeout); +int win32_redisKeepAlive(SOCKET sockfd, int interval_ms); + #ifndef REDIS_SOCKCOMPAT_IMPLEMENTATION #define getaddrinfo(node, service, hints, res) win32_getaddrinfo(node, service, hints, res) #undef gai_strerror diff --git a/ssl.c b/ssl.c index c581f63dc..88bd9f324 100644 --- a/ssl.c +++ b/ssl.c @@ -32,6 +32,7 @@ #include "hiredis.h" #include "async.h" +#include "net.h" #include #include @@ -39,6 +40,14 @@ #ifdef _WIN32 #include #include +#ifdef OPENSSL_IS_BORINGSSL +#undef X509_NAME +#undef X509_EXTENSIONS +#undef PKCS7_ISSUER_AND_SERIAL +#undef PKCS7_SIGNER_INFO +#undef OCSP_REQUEST +#undef OCSP_RESPONSE +#endif #else #include #endif @@ -184,7 +193,7 @@ const char *redisSSLContextGetError(redisSSLContextError error) case REDIS_SSL_CTX_PRIVATE_KEY_LOAD_FAILED: return "Failed to load private key"; case REDIS_SSL_CTX_OS_CERTSTORE_OPEN_FAILED: - return "Failed to open system certifcate store"; + return "Failed to open system certificate store"; case REDIS_SSL_CTX_OS_CERT_ADD_FAILED: return "Failed to add CA certificates obtained from system to the SSL context"; default: @@ -219,6 +228,25 @@ redisSSLContext *redisCreateSSLContext(const char *cacert_filename, const char * const char *cert_filename, const char *private_key_filename, const char *server_name, redisSSLContextError *error) { + redisSSLOptions options = { + .cacert_filename = cacert_filename, + .capath = capath, + .cert_filename = cert_filename, + .private_key_filename = private_key_filename, + .server_name = server_name, + .verify_mode = REDIS_SSL_VERIFY_PEER, + }; + + return redisCreateSSLContextWithOptions(&options, error); +} + +redisSSLContext *redisCreateSSLContextWithOptions(redisSSLOptions *options, redisSSLContextError *error) { + const char *cacert_filename = options->cacert_filename; + const char *capath = options->capath; + const char *cert_filename = options->cert_filename; + const char *private_key_filename = options->private_key_filename; + const char *server_name = options->server_name; + #ifdef _WIN32 HCERTSTORE win_store = NULL; PCCERT_CONTEXT win_ctx = NULL; @@ -235,7 +263,7 @@ redisSSLContext *redisCreateSSLContext(const char *cacert_filename, const char * } SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); - SSL_CTX_set_verify(ctx->ssl_ctx, SSL_VERIFY_PEER, NULL); + SSL_CTX_set_verify(ctx->ssl_ctx, options->verify_mode, NULL); if ((cert_filename != NULL && private_key_filename == NULL) || (private_key_filename != NULL && cert_filename == NULL)) { @@ -273,6 +301,11 @@ redisSSLContext *redisCreateSSLContext(const char *cacert_filename, const char * if (error) *error = REDIS_SSL_CTX_CA_CERT_LOAD_FAILED; goto error; } + } else { + if (!SSL_CTX_set_default_verify_paths(ctx->ssl_ctx)) { + if (error) *error = REDIS_SSL_CTX_CLIENT_DEFAULT_CERT_FAILED; + goto error; + } } if (cert_filename) { @@ -560,6 +593,7 @@ static void redisSSLAsyncWrite(redisAsyncContext *ac) { } redisContextFuncs redisContextSSLFuncs = { + .close = redisNetClose, .free_privctx = redisSSLFree, .async_read = redisSSLAsyncRead, .async_write = redisSSLAsyncWrite, diff --git a/test.c b/test.c index f991ef1e7..f3cb73434 100644 --- a/test.c +++ b/test.c @@ -15,6 +15,7 @@ #include "hiredis.h" #include "async.h" +#include "adapters/poll.h" #ifdef HIREDIS_TEST_SSL #include "hiredis_ssl.h" #endif @@ -34,11 +35,11 @@ enum connection_type { struct config { enum connection_type type; + struct timeval connect_timeout; struct { const char *host; int port; - struct timeval timeout; } tcp; struct { @@ -75,6 +76,15 @@ static int tests = 0, fails = 0, skips = 0; #define test_cond(_c) if(_c) printf("\033[0;32mPASSED\033[0;0m\n"); else {printf("\033[0;31mFAILED\033[0;0m\n"); fails++;} #define test_skipped() { printf("\033[01;33mSKIPPED\033[0;0m\n"); skips++; } +static void millisleep(int ms) +{ +#if _MSC_VER + Sleep(ms); +#else + usleep(ms*1000); +#endif +} + static long long usec(void) { #ifndef _MSC_VER struct timeval tv; @@ -329,10 +339,14 @@ static void test_format_commands(void) { FLOAT_WIDTH_TEST(float); FLOAT_WIDTH_TEST(double); - test("Format command with invalid printf format: "); + test("Format command with unhandled printf format (specifier 'p' not supported): "); len = redisFormatCommand(&cmd,"key:%08p %b",(void*)1234,"foo",(size_t)3); test_cond(len == -1); + test("Format command with invalid printf format (specifier missing): "); + len = redisFormatCommand(&cmd,"%-"); + test_cond(len == -1); + const char *argv[3]; argv[0] = "SET"; argv[1] = "foo\0xxx"; @@ -391,6 +405,16 @@ static void test_append_formatted_commands(struct config config) { disconnect(c, 0); } +static void test_tcp_options(struct config cfg) { + redisContext *c; + + c = do_connect(cfg); + test("We can enable TCP_KEEPALIVE: "); + test_cond(redisEnableKeepAlive(c) == REDIS_OK); + + disconnect(c, 0); +} + static void test_reply_reader(void) { redisReader *reader; void *reply, *root; @@ -568,6 +592,19 @@ static void test_reply_reader(void) { test_cond(ret == REDIS_ERR && reply == NULL); redisReaderFree(reader); + test("Don't reset state after protocol error(not segfault): "); + reader = redisReaderCreate(); + redisReaderFeed(reader,(char*)"*3\r\n$3\r\nSET\r\n$5\r\nhello\r\n$", 25); + ret = redisReaderGetReply(reader,&reply); + assert(ret == REDIS_OK); + redisReaderFeed(reader,(char*)"3\r\nval\r\n", 8); + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_OK && + ((redisReply*)reply)->type == REDIS_REPLY_ARRAY && + ((redisReply*)reply)->elements == 3); + freeReplyObject(reply); + redisReaderFree(reader); + /* Regression test for issue #45 on GitHub. */ test("Don't do empty allocation for empty multi bulk: "); reader = redisReaderCreate(); @@ -637,12 +674,23 @@ static void test_reply_reader(void) { freeReplyObject(reply); redisReaderFree(reader); - test("Set error when RESP3 double is NaN: "); + test("Correctly parses RESP3 double NaN: "); reader = redisReaderCreate(); redisReaderFeed(reader, ",nan\r\n",6); ret = redisReaderGetReply(reader,&reply); - test_cond(ret == REDIS_ERR && - strcasecmp(reader->errstr,"Bad double value") == 0); + test_cond(ret == REDIS_OK && + ((redisReply*)reply)->type == REDIS_REPLY_DOUBLE && + isnan(((redisReply*)reply)->dval)); + freeReplyObject(reply); + redisReaderFree(reader); + + test("Correctly parses RESP3 double -Nan: "); + reader = redisReaderCreate(); + redisReaderFeed(reader, ",-nan\r\n", 7); + ret = redisReaderGetReply(reader, &reply); + test_cond(ret == REDIS_OK && + ((redisReply*)reply)->type == REDIS_REPLY_DOUBLE && + isnan(((redisReply*)reply)->dval)); freeReplyObject(reply); redisReaderFree(reader); @@ -745,6 +793,20 @@ static void test_reply_reader(void) { !strcmp(((redisReply*)reply)->str,"3492890328409238509324850943850943825024385")); freeReplyObject(reply); redisReaderFree(reader); + + test("Can parse RESP3 doubles in an array: "); + reader = redisReaderCreate(); + redisReaderFeed(reader, "*1\r\n,3.14159265358979323846\r\n",31); + ret = redisReaderGetReply(reader,&reply); + test_cond(ret == REDIS_OK && + ((redisReply*)reply)->type == REDIS_REPLY_ARRAY && + ((redisReply*)reply)->elements == 1 && + ((redisReply*)reply)->element[0]->type == REDIS_REPLY_DOUBLE && + fabs(((redisReply*)reply)->element[0]->dval - 3.14159265358979323846) < 0.00000001 && + ((redisReply*)reply)->element[0]->len == 22 && + strcmp(((redisReply*)reply)->element[0]->str, "3.14159265358979323846") == 0); + freeReplyObject(reply); + redisReaderFree(reader); } static void test_free_null(void) { @@ -819,9 +881,9 @@ static void test_allocator_injection(void) { #define HIREDIS_BAD_DOMAIN "idontexist-noreally.com" static void test_blocking_connection_errors(void) { - redisContext *c; struct addrinfo hints = {.ai_family = AF_INET}; struct addrinfo *ai_tmp = NULL; + redisContext *c; int rv = getaddrinfo(HIREDIS_BAD_DOMAIN, "6379", &hints, &ai_tmp); if (rv != 0) { @@ -835,6 +897,7 @@ static void test_blocking_connection_errors(void) { strcmp(c->errstr, "Can't resolve: " HIREDIS_BAD_DOMAIN) == 0 || strcmp(c->errstr, "Name does not resolve") == 0 || strcmp(c->errstr, "nodename nor servname provided, or not known") == 0 || + strcmp(c->errstr, "node name or service name not known") == 0 || strcmp(c->errstr, "No address associated with hostname") == 0 || strcmp(c->errstr, "Temporary failure in name resolution") == 0 || strcmp(c->errstr, "hostname nor servname provided, or not known") == 0 || @@ -847,12 +910,26 @@ static void test_blocking_connection_errors(void) { } #ifndef _WIN32 + redisOptions opt = {0}; + struct timeval tv; + test("Returns error when the port is not open: "); c = redisConnect((char*)"localhost", 1); test_cond(c->err == REDIS_ERR_IO && strcmp(c->errstr,"Connection refused") == 0); redisFree(c); + + /* Verify we don't regress from the fix in PR #1180 */ + test("We don't clobber connection exception with setsockopt error: "); + tv = (struct timeval){.tv_sec = 0, .tv_usec = 500000}; + opt.command_timeout = opt.connect_timeout = &tv; + REDIS_OPTIONS_SET_TCP(&opt, "localhost", 10337); + c = redisConnectWithOptions(&opt); + test_cond(c->err == REDIS_ERR_IO && + strcmp(c->errstr, "Connection refused") == 0); + redisFree(c); + test("Returns error when the unix_sock socket path doesn't accept connections: "); c = redisConnectUnix((char*)"/tmp/idontexist.sock"); test_cond(c->err == REDIS_ERR_IO); /* Don't care about the message... */ @@ -914,11 +991,19 @@ static void test_resp3_push_handler(redisContext *c) { old = redisSetPushCallback(c, push_handler); test("We can set a custom RESP3 PUSH handler: "); reply = redisCommand(c, "SET key:0 val:0"); + /* We need another command because depending on the version of Redis, the + * notification may be delivered after the command's reply. */ + assert(reply != NULL); + freeReplyObject(reply); + reply = redisCommand(c, "PING"); test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && pc.str == 1); freeReplyObject(reply); test("We properly handle a NIL invalidation payload: "); reply = redisCommand(c, "FLUSHDB"); + assert(reply != NULL); + freeReplyObject(reply); + reply = redisCommand(c, "PING"); test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && pc.nil == 1); freeReplyObject(reply); @@ -929,6 +1014,12 @@ static void test_resp3_push_handler(redisContext *c) { assert((reply = redisCommand(c, "GET key:0")) != NULL); freeReplyObject(reply); assert((reply = redisCommand(c, "SET key:0 invalid")) != NULL); + /* Depending on Redis version, we may receive either push notification or + * status reply. Both cases are valid. */ + if (reply->type == REDIS_REPLY_STATUS) { + freeReplyObject(reply); + reply = redisCommand(c, "PING"); + } test_cond(reply->type == REDIS_REPLY_PUSH); freeReplyObject(reply); @@ -1089,6 +1180,13 @@ static void test_blocking_connection(struct config config) { strcasecmp(reply->element[1]->str,"pong") == 0); freeReplyObject(reply); + test("Send command by passing argc/argv: "); + const char *argv[3] = {"SET", "foo", "bar"}; + size_t argvlen[3] = {3, 3, 3}; + reply = redisCommandArgv(c,3,argv,argvlen); + test_cond(reply->type == REDIS_REPLY_STATUS); + freeReplyObject(reply); + /* Make sure passing NULL to redisGetReply is safe */ test("Can pass NULL to redisGetReply: "); assert(redisAppendCommand(c, "PING") == REDIS_OK); @@ -1143,7 +1241,13 @@ static void test_blocking_connection_timeouts(struct config config) { test("Does not return a reply when the command times out: "); if (detect_debug_sleep(c)) { redisAppendFormattedCommand(c, sleep_cmd, strlen(sleep_cmd)); + + // flush connection buffer without waiting for the reply s = c->funcs->write(c); + assert(s == (ssize_t)sdslen(c->obuf)); + sdsfree(c->obuf); + c->obuf = sdsempty(); + tv.tv_sec = 0; tv.tv_usec = 10000; redisSetTimeout(c, tv); @@ -1156,6 +1260,9 @@ static void test_blocking_connection_timeouts(struct config config) { strcmp(c->errstr, "recv timeout") == 0); #endif freeReplyObject(reply); + + // wait for the DEBUG SLEEP to complete so that Redis server is unblocked for the following tests + millisleep(3000); } else { test_skipped(); } @@ -1226,22 +1333,34 @@ static void test_blocking_io_errors(struct config config) { static void test_invalid_timeout_errors(struct config config) { redisContext *c; - test("Set error when an invalid timeout usec value is given to redisConnectWithTimeout: "); + test("Set error when an invalid timeout usec value is used during connect: "); - config.tcp.timeout.tv_sec = 0; - config.tcp.timeout.tv_usec = 10000001; + config.connect_timeout.tv_sec = 0; + config.connect_timeout.tv_usec = 10000001; - c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout); + if (config.type == CONN_TCP || config.type == CONN_SSL) { + c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.connect_timeout); + } else if(config.type == CONN_UNIX) { + c = redisConnectUnixWithTimeout(config.unix_sock.path, config.connect_timeout); + } else { + assert(NULL); + } test_cond(c->err == REDIS_ERR_IO && strcmp(c->errstr, "Invalid timeout specified") == 0); redisFree(c); - test("Set error when an invalid timeout sec value is given to redisConnectWithTimeout: "); + test("Set error when an invalid timeout sec value is used during connect: "); - config.tcp.timeout.tv_sec = (((LONG_MAX) - 999) / 1000) + 1; - config.tcp.timeout.tv_usec = 0; + config.connect_timeout.tv_sec = (((LONG_MAX) - 999) / 1000) + 1; + config.connect_timeout.tv_usec = 0; - c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout); + if (config.type == CONN_TCP || config.type == CONN_SSL) { + c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.connect_timeout); + } else if(config.type == CONN_UNIX) { + c = redisConnectUnixWithTimeout(config.unix_sock.path, config.connect_timeout); + } else { + assert(NULL); + } test_cond(c->err == REDIS_ERR_IO && strcmp(c->errstr, "Invalid timeout specified") == 0); redisFree(c); @@ -1729,10 +1848,14 @@ void subscribe_channel_a_cb(redisAsyncContext *ac, void *r, void *privdata) { strcmp(reply->element[2]->str,"Hello!") == 0); state->checkpoint++; - /* Unsubscribe to channels, including a channel X which we don't subscribe to */ + /* Unsubscribe to channels, including channel X & Z which we don't subscribe to */ redisAsyncCommand(ac,unexpected_cb, (void*)"unsubscribe should not call unexpected_cb()", - "unsubscribe B X A"); + "unsubscribe B X A A Z"); + /* Unsubscribe to patterns, none which we subscribe to */ + redisAsyncCommand(ac,unexpected_cb, + (void*)"punsubscribe should not call unexpected_cb()", + "punsubscribe"); /* Send a regular command after unsubscribing, then disconnect */ state->disconnect = 1; redisAsyncCommand(ac,integer_cb,state,"LPUSH mylist foo"); @@ -1749,6 +1872,7 @@ void subscribe_channel_a_cb(redisAsyncContext *ac, void *r, void *privdata) { void subscribe_channel_b_cb(redisAsyncContext *ac, void *r, void *privdata) { redisReply *reply = r; TestState *state = privdata; + (void)ac; assert(reply != NULL && reply->type == REDIS_REPLY_ARRAY && reply->elements == 3); @@ -1767,8 +1891,10 @@ void subscribe_channel_b_cb(redisAsyncContext *ac, void *r, void *privdata) { /* Test handling of multiple channels * - subscribe to channel A and B - * - a published message on A triggers an unsubscribe of channel B, X and A - * where channel X is not subscribed to. + * - a published message on A triggers an unsubscribe of channel B, X, A and Z + * where channel X and Z are not subscribed to. + * - the published message also triggers an unsubscribe to patterns. Since no + * pattern is subscribed to the responded pattern element type is NIL. * - a command sent after unsubscribe triggers a disconnect */ static void test_pubsub_multiple_channels(struct config config) { test("Subscribe to multiple channels: "); @@ -1881,6 +2007,250 @@ static void test_monitor(struct config config) { } #endif /* HIREDIS_TEST_ASYNC */ +/* tests for async api using polling adapter, requires no extra libraries*/ + +/* enum for the test cases, the callbacks have different logic based on them */ +typedef enum astest_no +{ + ASTEST_CONNECT=0, + ASTEST_CONN_TIMEOUT, + ASTEST_PINGPONG, + ASTEST_PINGPONG_TIMEOUT, + ASTEST_ISSUE_931, + ASTEST_ISSUE_931_PING +}astest_no; + +/* a static context for the async tests */ +struct _astest { + redisAsyncContext *ac; + astest_no testno; + int counter; + int connects; + int connect_status; + int disconnects; + int pongs; + int disconnect_status; + int connected; + int err; + char errstr[256]; +}; +static struct _astest astest; + +/* async callbacks */ +static void asCleanup(void* data) +{ + struct _astest *t = (struct _astest *)data; + t->ac = NULL; +} + +static void commandCallback(struct redisAsyncContext *ac, void* _reply, void* _privdata); + +static void connectCallback(redisAsyncContext *c, int status) { + struct _astest *t = (struct _astest *)c->data; + assert(t == &astest); + assert(t->connects == 0); + t->err = c->err; + strcpy(t->errstr, c->errstr); + t->connects++; + t->connect_status = status; + t->connected = status == REDIS_OK ? 1 : -1; + + if (t->testno == ASTEST_ISSUE_931) { + /* disconnect again */ + redisAsyncDisconnect(c); + } + else if (t->testno == ASTEST_ISSUE_931_PING) + { + redisAsyncCommand(c, commandCallback, NULL, "PING"); + } +} +static void disconnectCallback(const redisAsyncContext *c, int status) { + assert(c->data == (void*)&astest); + assert(astest.disconnects == 0); + astest.err = c->err; + strcpy(astest.errstr, c->errstr); + astest.disconnects++; + astest.disconnect_status = status; + astest.connected = 0; +} + +static void commandCallback(struct redisAsyncContext *ac, void* _reply, void* _privdata) +{ + redisReply *reply = (redisReply*)_reply; + struct _astest *t = (struct _astest *)ac->data; + assert(t == &astest); + (void)_privdata; + t->err = ac->err; + strcpy(t->errstr, ac->errstr); + t->counter++; + if (t->testno == ASTEST_PINGPONG ||t->testno == ASTEST_ISSUE_931_PING) + { + assert(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "PONG") == 0); + t->pongs++; + redisAsyncFree(ac); + } + if (t->testno == ASTEST_PINGPONG_TIMEOUT) + { + /* two ping pongs */ + assert(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "PONG") == 0); + t->pongs++; + if (t->counter == 1) { + int status = redisAsyncCommand(ac, commandCallback, NULL, "PING"); + assert(status == REDIS_OK); + } else { + redisAsyncFree(ac); + } + } +} + +static redisAsyncContext *do_aconnect(struct config config, astest_no testno) +{ + redisOptions options = {0}; + memset(&astest, 0, sizeof(astest)); + + astest.testno = testno; + astest.connect_status = astest.disconnect_status = -2; + + if (config.type == CONN_TCP) { + options.type = REDIS_CONN_TCP; + options.connect_timeout = &config.connect_timeout; + REDIS_OPTIONS_SET_TCP(&options, config.tcp.host, config.tcp.port); + } else if (config.type == CONN_SSL) { + options.type = REDIS_CONN_TCP; + options.connect_timeout = &config.connect_timeout; + REDIS_OPTIONS_SET_TCP(&options, config.ssl.host, config.ssl.port); + } else if (config.type == CONN_UNIX) { + options.type = REDIS_CONN_UNIX; + options.endpoint.unix_socket = config.unix_sock.path; + } else if (config.type == CONN_FD) { + options.type = REDIS_CONN_USERFD; + /* Create a dummy connection just to get an fd to inherit */ + redisContext *dummy_ctx = redisConnectUnix(config.unix_sock.path); + if (dummy_ctx) { + redisFD fd = disconnect(dummy_ctx, 1); + printf("Connecting to inherited fd %d\n", (int)fd); + options.endpoint.fd = fd; + } + } + redisAsyncContext *c = redisAsyncConnectWithOptions(&options); + assert(c); + astest.ac = c; + c->data = &astest; + c->dataCleanup = asCleanup; + redisPollAttach(c); + redisAsyncSetConnectCallbackNC(c, connectCallback); + redisAsyncSetDisconnectCallback(c, disconnectCallback); + return c; +} + +static void as_printerr(void) { + printf("Async err %d : %s\n", astest.err, astest.errstr); +} + +#define ASASSERT(e) do { \ + if (!(e)) \ + as_printerr(); \ + assert(e); \ +} while (0); + +static void test_async_polling(struct config config) { + int status; + redisAsyncContext *c; + struct config defaultconfig = config; + + test("Async connect: "); + c = do_aconnect(config, ASTEST_CONNECT); + assert(c); + while(astest.connected == 0) + redisPollTick(c, 0.1); + assert(astest.connects == 1); + ASASSERT(astest.connect_status == REDIS_OK); + assert(astest.disconnects == 0); + test_cond(astest.connected == 1); + + test("Async free after connect: "); + assert(astest.ac != NULL); + redisAsyncFree(c); + assert(astest.disconnects == 1); + assert(astest.ac == NULL); + test_cond(astest.disconnect_status == REDIS_OK); + + if (config.type == CONN_TCP || config.type == CONN_SSL) { + /* timeout can only be simulated with network */ + test("Async connect timeout: "); + config.tcp.host = "192.168.254.254"; /* blackhole ip */ + config.connect_timeout.tv_usec = 100000; + c = do_aconnect(config, ASTEST_CONN_TIMEOUT); + assert(c); + assert(c->err == 0); + while(astest.connected == 0) + redisPollTick(c, 0.1); + assert(astest.connected == -1); + /* + * freeing should not be done, clearing should have happened. + *redisAsyncFree(c); + */ + assert(astest.ac == NULL); + test_cond(astest.connect_status == REDIS_ERR); + config = defaultconfig; + } + + /* Test a ping/pong after connection */ + test("Async PING/PONG: "); + c = do_aconnect(config, ASTEST_PINGPONG); + while(astest.connected == 0) + redisPollTick(c, 0.1); + status = redisAsyncCommand(c, commandCallback, NULL, "PING"); + assert(status == REDIS_OK); + while(astest.ac) + redisPollTick(c, 0.1); + test_cond(astest.pongs == 1); + + /* Test a ping/pong after connection that didn't time out. + * see https://github.com/redis/hiredis/issues/945 + */ + if (config.type == CONN_TCP || config.type == CONN_SSL) { + test("Async PING/PONG after connect timeout: "); + config.connect_timeout.tv_usec = 10000; /* 10ms */ + c = do_aconnect(config, ASTEST_PINGPONG_TIMEOUT); + while(astest.connected == 0) + redisPollTick(c, 0.1); + /* sleep 0.1 s, allowing old timeout to arrive */ + millisleep(10); + status = redisAsyncCommand(c, commandCallback, NULL, "PING"); + assert(status == REDIS_OK); + while(astest.ac) + redisPollTick(c, 0.1); + test_cond(astest.pongs == 2); + config = defaultconfig; + } + + /* Test disconnect from an on_connect callback + * see https://github.com/redis/hiredis/issues/931 + */ + test("Disconnect from onConnected callback (Issue #931): "); + c = do_aconnect(config, ASTEST_ISSUE_931); + while(astest.disconnects == 0) + redisPollTick(c, 0.1); + assert(astest.connected == 0); + assert(astest.connects == 1); + test_cond(astest.disconnects == 1); + + /* Test ping/pong from an on_connect callback + * see https://github.com/redis/hiredis/issues/931 + */ + test("Ping/Pong from onConnected callback (Issue #931): "); + c = do_aconnect(config, ASTEST_ISSUE_931_PING); + /* connect callback issues ping, reponse callback destroys context */ + while(astest.ac) + redisPollTick(c, 0.1); + assert(astest.connected == 0); + assert(astest.connects == 1); + assert(astest.disconnects == 1); + test_cond(astest.pongs == 1); +} +/* End of Async polling_adapter driven tests */ + int main(int argc, char **argv) { struct config cfg = { .tcp = { @@ -1963,6 +2333,7 @@ int main(int argc, char **argv) { test_blocking_io_errors(cfg); test_invalid_timeout_errors(cfg); test_append_formatted_commands(cfg); + test_tcp_options(cfg); if (throughput) test_throughput(cfg); printf("\nTesting against Unix socket connection (%s): ", cfg.unix_sock.path); @@ -1972,6 +2343,7 @@ int main(int argc, char **argv) { test_blocking_connection(cfg); test_blocking_connection_timeouts(cfg); test_blocking_io_errors(cfg); + test_invalid_timeout_errors(cfg); if (throughput) test_throughput(cfg); } else { test_skipped(); @@ -2000,6 +2372,7 @@ int main(int argc, char **argv) { #endif #ifdef HIREDIS_TEST_ASYNC + cfg.type = CONN_TCP; printf("\nTesting asynchronous API against TCP connection (%s:%d):\n", cfg.tcp.host, cfg.tcp.port); cfg.type = CONN_TCP; @@ -2017,6 +2390,15 @@ int main(int argc, char **argv) { } #endif /* HIREDIS_TEST_ASYNC */ + cfg.type = CONN_TCP; + printf("\nTesting asynchronous API using polling_adapter TCP (%s:%d):\n", cfg.tcp.host, cfg.tcp.port); + test_async_polling(cfg); + if (test_unix_socket) { + cfg.type = CONN_UNIX; + printf("\nTesting asynchronous API using polling_adapter UNIX (%s):\n", cfg.unix_sock.path); + test_async_polling(cfg); + } + if (test_inherit_fd) { printf("\nTesting against inherited fd (%s): ", cfg.unix_sock.path); if (test_unix_socket) { diff --git a/test.sh b/test.sh index c72bcb0dc..0a1afb923 100755 --- a/test.sh +++ b/test.sh @@ -4,9 +4,17 @@ REDIS_SERVER=${REDIS_SERVER:-redis-server} REDIS_PORT=${REDIS_PORT:-56379} REDIS_SSL_PORT=${REDIS_SSL_PORT:-56443} TEST_SSL=${TEST_SSL:-0} -SKIPS_AS_FAILS=${SKIPS_AS_FAILS-:0} +SKIPS_AS_FAILS=${SKIPS_AS_FAILS:-0} +ENABLE_DEBUG_CMD= SSL_TEST_ARGS= -SKIPS_ARG= +SKIPS_ARG=${SKIPS_ARG:-} +REDIS_DOCKER=${REDIS_DOCKER:-} + +# We need to enable the DEBUG command for redis-server >= 7.0.0 +REDIS_MAJOR_VERSION="$(redis-server --version|awk -F'[^0-9]+' '{ print $2 }')" +if [ "$REDIS_MAJOR_VERSION" -gt "6" ]; then + ENABLE_DEBUG_CMD="enable-debug-command local" +fi tmpdir=$(mktemp -d) PID_FILE=${tmpdir}/hiredis-test-redis.pid @@ -43,20 +51,34 @@ if [ "$TEST_SSL" = "1" ]; then fi cleanup() { - set +e - kill $(cat ${PID_FILE}) + if [ -n "${REDIS_DOCKER}" ] ; then + docker kill redis-test-server + else + set +e + kill $(cat ${PID_FILE}) + fi rm -rf ${tmpdir} } trap cleanup INT TERM EXIT +# base config cat > ${tmpdir}/redis.conf <> ${tmpdir}/redis.conf <> ${tmpdir}/redis.conf <