5107 Commits

Author SHA1 Message Date
Ben Kallus
665d79f176
Fix integer overflow in HTTP version (#1541)
Currently, when libevent parses requests with version `HTTP/4294967295.255`, you end up with `req->major == req->minor == (char)-1`. (At least on linux-gnu-x86_64, where `char` is signed.)

This is sort of weird.

This patch changes the version parser to match the grammar in RFCs 7230 and 9112. (i.e. `HTTP/[0-9].[0-9]`)

EDIT: Technically, a little stronger than the RFC requires, since this patch continues to block major versions greater than 1, which was already what libevent was doing.
2024-01-19 00:42:52 +03:00
Andy Pan
55899c2fcb Use read/write instead of readv/writev for single buffer
The `readv`/`writev` functions are designed for scattered I/O optimally,
their logic in the kernel is more sophisticated, compared to read/write,
which includes extra on-stack `iovec` in the kernel space, importing `iovec`
array from user space to kernel space, reading/writing with `iov_iter`, etc.
As a result, using `readv`/`writev` on single-segment `iovec` will fall into
the special branch in the kernel where it is imported as `ITER_UBUF` differed
from `ITER_IOVEC` for multiple-segments `iovec`.

Thus, it is just not worth calling `readv`/`writev` for single-segment `iovec`,
we should use `read`/`write` instead, to save it from going through the
sophisticated yet unnecessary kernel code path, circumvent a waste of
kernel on-stack memory, copying `iovec` between user space and kernel space, etc.
2024-01-19 00:41:03 +03:00
Andy Pan
3f508af6e2 Ignore the project files from IDE or editors
I sometimes browse and edit C/C++ code with VSCode,
and it would generate some project files in .vscode
which could easily be added and even pushed to a git repo by accident.

It's sort of annoying for programmers who work with VS/VSCode,
IntelliJ/Clion, or other widely-used IDE or editors to prevent
themself from using git command like `git add .` in lib event project.

Therefore, I propose leaving the project files generated by VS/VSCode
and IntelliJ/Clion untracked for libevent project. Hopefully, this can be accepted.
2024-01-15 10:50:48 +03:00
Andy Pan
23cdd61063 Use evutil_set_tcp_keepalive for sample/becat.c
Since #1532 introduced the util function for consolidating TPC keep-alive settings,
there is no need to write other handmade functions elsewhere.
2024-01-15 10:49:55 +03:00
Azat Khuzhin
cffb7c03f1 ci: disable ABI check temporary (I hope that "temporary")
Refs: https://github.com/libevent/libevent/issues/1463
2024-01-13 15:55:12 +03:00
Andy Pan
9d4853b363
Optimize preprocessor directives on Solaris for evutil_set_tcp_keepalive (#1536)
Changes:
- Optimize preprocessor directives on Solaris for evutil_set_tcp_keepalive
- Reduce the amount of code being compiled and trim trailing whitespace in passing.
- Move comments for Solaris under its macro
- Refactor the control flow
2024-01-13 15:51:49 +03:00
Azat Khuzhin
76460fbe66 Fix windows CI (#1533)
But note, that there is still issues with MbedTLS and ZLIB:

    CMake Error at C:/Program Files/CMake/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
    Error:   Could NOT find MbedTLS (missing: MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARY
      MBEDTLS_CRYPTO_LIBRARY MBEDTLS_X509_LIBRARY MBEDTLS_LIBRARIES
      MBEDTLS_VERSION)
    Call Stack (most recent call first):
      C:/Program Files/CMake/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
      D:/a/libevent/libevent/cmake/FindMbedTLS.cmake:142 (find_package_handle_standard_args)
      D:/a/libevent/vcpkg/scripts/buildsystems/vcpkg.cmake:859 (_find_package)
      C:/Program Files/CMake/share/cmake-3.27/Modules/CMakeFindDependencyMacro.cmake:76 (find_package)
      D:/a/libevent/libevent/build/LibeventConfig.cmake:43 (find_dependency)
      D:/a/libevent/vcpkg/scripts/buildsystems/vcpkg.cmake:859 (_find_package)
      CMakeLists.txt:11 (find_package)

But by some reason it is not a fatal error on some builds:

    -- OpenSSL lib: optimized;C:/Program Files/OpenSSL/lib/VC/libssl64MD.lib;debug;C:/Program Files/OpenSSL/lib/VC/libssl64MDd.lib;optimized;C:/Program Files/OpenSSL/lib/VC/libcrypto64MD.lib;debug;C:/Program Files/OpenSSL/lib/VC/libcrypto64MDd.lib
    -- Could NOT find MbedTLS (missing: MBEDTLS_INCLUDE_DIR MBEDTLS_LIBRARY MBEDTLS_CRYPTO_LIBRARY MBEDTLS_X509_LIBRARY MBEDTLS_LIBRARIES MBEDTLS_VERSION)
    -- Could NOT find ZLIB (missing: ZLIB_LIBRARY) (found version "1.2.11")

This will be fixed separatelly - #1534, partially worked CI better then
fully broken anyway.

* ci/windows:
  Update vcpkg to fix windows CI
  Switch to windows-latest (just in case)
2024-01-06 14:29:22 +01:00
Azat Khuzhin
3e01178b1b Update vcpkg to fix windows CI
Previusly it got lots of 404 errors during downloading packages, this
was due to too old repository had been used.
2024-01-06 14:28:26 +01:00
Azat Khuzhin
85c09bbfa6 Switch to windows-latest (just in case) 2024-01-06 14:28:20 +01:00
Andy Pan
7658b78853
Implement full support of TCP Keep-Alives across most Unix-like OS's (#1532)
Default settings of TCP keep-alive are not always feasible,
take Linux for example, the duration a connection needs to be
idle before sending out the first keep-alive probe is two hours,
which makes detecting dead connections extremely deferred. Thus,
enabling TCP keep-alive will be to little avail.

This sets TCP_KEEPIDLE, TCP_KEEPINTVL, and TCP_KEEPCNT when enabling TCP
keep-alive mechanism to make it practicable.
2024-01-05 19:52:43 +03:00
Hemanth Nandish
0c54433c12
time-test: use event_new for compatibility with future libevent (#1525) 2023-11-26 21:53:23 +01:00
Sam James
4c38de8cb3
Fix -Walloc-size (#1526)
Co-authored-by: Azat Khuzhin <azat@libevent.org>
2023-11-26 21:52:32 +01:00
Azat Khuzhin
4881b5928e
Merge pull request #1519 from cculianu/ws_c_fixes 2023-11-25 19:13:35 +01:00
Faraz Vahedi
7f5b8f1909 Fix documentation inconsistency in evhttp callbacks
This commit addresses a minor inconsistency identified in the
documentation of multiple `evhttp` callback functions within
the `event2/http.h` header file. It was observed that the word
"additional" was presumably missing in the description of the
`arg` parameter for several functions. This oversight led to
an incorrect phrasing: "an context argument for the callback."

The documentation for the following functions has been updated
to rectify this issue:

  - `evhttp_set_gencb`
  - `evhttp_set_bevcb`
  - `evhttp_set_newreqcb`
  - `evhttp_set_errorcb`

This commit solely improves the readability of the function
descriptions without altering any functional aspects of the
code.
2023-11-25 18:34:32 +01:00
Ramon Ortega de Voor
cfb2b89a1d fix ERR macro geting redefined
In evutil.c a macro ERR gets defined,
this is a very generic name in case that
in a included header file ERR is used too,
the compiler will fail complaining
about the redefinition of the macro ERR.
To fix this make sure ERR is undefined,
before defining it in evutil.c
2023-10-23 17:15:34 +02:00
Ramon Ortega de Voor
a579b1d77c Fix compile error in evbuffer_read()
The variable chainp is only used if USE_IOVEC_IMPL is defined.
This makes a strict compiler complain about unused variables,
since chainp is declared outside of an USE_IOVEC_IMPL block.
2023-10-21 23:20:47 +03:00
Calin Culianu
15b9b6f019
Apply suggestions from code review
Co-authored-by: Azat Khuzhin <a3at.mail@gmail.com>
2023-10-17 12:45:33 +03:00
Calin Culianu
b828c4ab41
Added 10MiB recv limit for WS frames, also a small nit
- Added WS_MAX_RECV_FRAME_SZ, if a received frame exceeds this limit (10
  MiB), error out. This is a DoS prevention measure.
- Also redid the unmasking code in get_ws_frame(), to avoid assumptions
  about the size of an unsigned int and some casting, and avoid
  memcpy(), just use the mask bytes from the buffer that are already there
  to unmask.
2023-10-16 15:25:12 +03:00
Calin Culianu
7fbbe86ff3
Fix unaligned access + missing frame length
get_ws_frame:
  - had potentially unaligned access; fixed.
  - was not reading the full 8 bytes in the 64-bit case (was using the
    incorrect htons); fixed.
make_ws_frame:
  - wasn't writing the length in the 8-byte case.

Also switched both functions to use size_t rather than int where
appropriate.
2023-10-15 23:50:50 +03:00
binarycraft007
c9af024585 wepoll api: fix definition of EPOLLONESHOT to match Linux 2023-10-12 23:02:38 +03:00
Gordon Bergling
23685cfbd1
Fix some typos (#1515) 2023-09-28 11:18:10 +02:00
Azat Khuzhin
d30ad0a7fd Merge branch 'becat-fixes-v2'
* becat-fixes-v2:
  becat: change -R to -b (since it is more of a buffer size)
  becat: add ability to set TCP keepalive
2023-09-22 10:21:15 +02:00
Azat Khuzhin
9e4693ed07 becat: change -R to -b (since it is more of a buffer size) 2023-09-22 10:21:06 +02:00
Azat Khuzhin
617cf5efa8 becat: add ability to set TCP keepalive 2023-09-22 10:21:06 +02:00
Azat Khuzhin
c15ba75d18 Merge branch 'evbuffer_add_reference_with_offset' - #1513
* evbuffer_add_reference_with_offset:
  Add a comment for evbuffer_ref_cleanup_cb
  tests: simplify test_evbuffer_add_reference_with_offset
  Add function evbuffer_add_reference_with_offset()
2023-09-22 09:35:42 +02:00
Azat Khuzhin
c2a935b5c7 Add a comment for evbuffer_ref_cleanup_cb 2023-09-22 09:35:07 +02:00
Azat Khuzhin
d9780cf96a Fix -Wsingle-bit-bitfield-constant-conversion warning in clang 16
report:

    /src/le/libevent/sample/becat.c:304:29: warning: implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1 [-Wsingle-bit-bitfield-constant-conversion]
                            case 'k': o.extra.keep   = 1; break;
2023-09-21 22:04:17 +02:00
Azat Khuzhin
cc3a920073 tests: simplify test_evbuffer_add_reference_with_offset 2023-09-21 22:02:48 +02:00
MBeanwenshengming
648ec50e11 Add function evbuffer_add_reference_with_offset()
This is the same as evbuffer_add_reference(), but allows to specify
offset in the @data

v2: rename evbuffer_add_reference_misalign() to evbuffer_add_reference_with_offset()
2023-09-21 22:02:26 +02:00
Gordon Bergling
8728c98311 evthread: Fix typos in error messages
- s/probaby/probably/
2023-09-02 21:57:32 +02:00
DmiTriy Fedchenko
0797e20783 Fixed misaligned address access 2023-09-02 21:57:09 +02:00
Dmitry Ilyin
f39ad1c494
ws: replace evws_send with evws_send_text/evws_send_binary (ABI breakage) (#1500)
Replace evws_send with evws_send_text, and introduce new API -
evws_send_binary, that can be used to send binary frames.

But note, that this commit breaks the ABI compatibility, but it should be OK,
since there was only alpha release with evws_send, and nobody should rely on
this, and I hope nobody does (we decided to go this way to avoid supporting
deprecated API).
2023-08-31 21:38:41 +02:00
Michael Davidsaver
21d2f5a415 Fixes #include of winsock.h before winsock2.h 2023-08-27 09:12:27 +03:00
Michael Davidsaver
c9ec6aafb6 Update cmake policy wrt. RPATH on OSX
Previous settings caused install outside CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES
to use install_name "lib/" in place of the expected (and useful) "@rpath/".
2023-08-02 21:05:54 +02:00
Michael Davidsaver
3c29a81b8b fixup RPATH computation
Handle cases where ${CMAKE_INSTALL_LIBDIR} is not ${CMAKE_INSTALL_PREFIX}/lib .
Also, preserve user provided ${CMAKE_INSTALL_RPATH} .

fixup for 41a7393f3ecd1e9f58331df4653dac2e6739304e
2023-07-31 10:51:20 +02:00
Azat Khuzhin
66b3904b30
Merge pull request #1498 from mdavidsaver/cleanup-_WIN32_WINNT 2023-07-29 22:27:31 +02:00
Michael Davidsaver
f76bd641f2 increment _WIN32_WINNT in evutil.c 2023-07-29 08:12:33 -07:00
Michael Davidsaver
126523f9ce fix prototype of evutil_check_working_afunix_()
mingw w/ gcc 12 defaults to -Wstrict-prototype which complains
about K&R style argument lists.
2023-07-25 11:01:41 -07:00
Michael Davidsaver
a14ff91254 move _WIN32_WINNT defintions before first #include
_WIN32_WINNT and WIN32_LEAN_AND_MEAN need to be defined
before the windows.h is included for the first time.
Avoid the confusion of indirect #include by defining
before any.
2023-07-25 10:52:10 -07:00
Azat Khuzhin
6db587dedb Merge branch 'scorecard-action' (#1494)
* scorecard-action:
  docs(readme): add scorecard badge
  Enable Scorecard GitHub Action
2023-07-16 15:23:17 +02:00
Diogo Teles Sant'Anna
e26e064762 docs(readme): add scorecard badge 2023-07-16 15:22:38 +02:00
Diogo Teles Sant'Anna
613581bbcf Enable Scorecard GitHub Action 2023-07-16 15:22:38 +02:00
Azat Khuzhin
7b302e7dbe Merge branch 'disable-signalfd'
* disable-signalfd:
  Increase timeout for tests
  Disable signalfd by default
2023-07-13 21:21:58 +02:00
Azat Khuzhin
3d1d7548cd Increase timeout for tests
Now, it 20min for sequential runs is not enough already, I've tested it
on t3.medium manually and got the same:

    Total Test time (real) = 1275.03 sec

And also if I run tests in parallel then I had few failures:

    bufferevent/bufferevent_connect_fail:
      FAIL /root/libevent/test/regress_bufferevent.c:865: didn't fail? what 128
      FAIL /root/libevent/test/regress_bufferevent.c:906: assert(test_ok == 1): 0 vs 1
      FAIL /root/libevent/test/regress_bufferevent.c:839: assert(n_events_invoked == 1): 2 vs 1bufferevent/bufferevent_connect_fail_eventcb_defer:
      FAIL /root/libevent/test/regress_bufferevent.c:839: assert(n_events_invoked == 1): 2 vs 1bufferevent/bufferevent_connect_fail_eventcb: 3/386 TESTS FAILED. (45 skipped)

v2: Increase timeout more (for netbsd)
2023-07-13 21:21:26 +02:00
Azat Khuzhin
57d9eec641 Disable signalfd by default
signalfd may behave differently to sigaction/signal, so to avoid
breaking libevent users (like [1], [2]) disable it by default.

  [1]: https://github.com/tmux/tmux/pull/3621
  [2]: https://github.com/tmux/tmux/pull/3626

Also signalfd is not that perfect:
- you need to SIG_BLOCK the signal before
  - blocked signals are not reset on exec
  - blocked signals are allowed to coalesce - so in case of multiple
    signals sent you may get the signal only once (ok for most of the
    signals, but may be a problem for SIGCHLD, though you may call
    waitpid() in a loop or use pidfd)
- and also one implementation problem -
  sigprocmask is unspecified in a multithreaded process

Refs:
- https://lwn.net/Articles/415684/
- https://ldpreload.com/blog/signalfd-is-useless

Refs: https://github.com/libevent/libevent/issues/1460
Refs: #1342 (cc @dmantipov)
2023-07-13 21:21:09 +02:00
Vladislav Gusev
227510d577
Fix EVDNS_BASE_DISABLE_WHEN_INACTIVE (#1493)
I faced with strange problem: event loop doesn't exit after dns resolving with
`EVDNS_BASE_DISABLE_WHEN_INACTIVE`.

Stand:
- Ubuntu 22;
- libevent release-2.1.12-stable
- `resolve.conf` contains 2 nameservers;
- I use `evdns_base_new` with `EVDNS_BASE_DISABLE_WHEN_INACTIVE | EVDNS_BASE_INITIALIZE_NAMESERVERS` to avoid OS specific code.

After small investigation, look like events related with dns sockets added to
event_base before `evdns->disable_when_inactive` was initialized. `libevent`
did epoll_ctl(DEL) after resolving completed on the first socket, but the
second socket remained in the `epoll` interest list.
2023-07-13 21:20:33 +02:00
mareksm
36ad1006c9 #1479: GCC12 error for EVUTIL_FALLTHROUGH 2023-07-07 20:26:58 +02:00
Azat Khuzhin
5ab24bddbe Merge branch 'becat-fixes'
* becat-fixes:
  becat: fix handling positional arguments
  becat: handle -T correctly
2023-06-28 21:33:47 +02:00
Azat Khuzhin
2b729238e5 becat: fix handling positional arguments 2023-06-28 21:33:41 +02:00
Azat Khuzhin
687124d018 becat: handle -T correctly 2023-06-28 21:33:41 +02:00