230 Commits

Author SHA1 Message Date
Dmitry Ilyin
00c94beaf0 ws: allow Upgrade in Connection header anywhere
This will make it work for Firefox, which uses "Connection: keep-alive, Upgrade"
Reimplemented our own evutil_ascii_strcasestr function (same as
non-portable strcasestr)
2024-07-27 09:18:10 +03:00
Cœur
49d6b4b099
samples: use evutil_socket_t instead and handle 64 bit Windows (#1682)
* Use evutil_socket_t instead in http server sample and handle 64 bit Windows

* Update http-server.c

* consistently using EV_SOCK_FMT for Windows compatibility

* code review: fix missing symbol strsignal

* Add evutil_strsignal() helper instead of strsignal() macro

---------

Co-authored-by: Hernan Martinez <hernan.c.martinez@gmail.com>
Co-authored-by: Azat Khuzhin <azat@libevent.org>
2024-07-08 10:10:42 +02:00
Andy Pan
96c259f1d9
tcpkeepalive: distinguish OS versions and use proper time units (#1669)
DragonFly BSD changed the time unit for TCP keep-alive from milliseconds to seconds since v5.8 and Solaris 11.4 added `TCP_KEEPIDLE`, `TCP_KEEPINTVL`, and `TCP_KEEPCNT` with time units in second while Solaris <11.4 still use `TCP_KEEPALIVE_THRESHOLD` and `TCP_KEEPALIVE_ABORT_THRESHOLD` with time units in millisecond.

Currently, we don't differentiate among DragonFly BSD versions but set the keepalive options with seconds, which will result in unexpected behaviors on DragonFlyBSD <5.8. This PR intends to fix the wrong usage of time units of TCP keepalive options on DragonFly BSD <5.8 and consolidate the logic of time units conversion for TCP keepalive across platforms.

In addition, this PR introduces a new custom macro for determining Solaris 11.4. This macro is expected to help us implement some new features for `libuv` using some abilities that only exist on Solaris 11.4 and other mainstream platforms in the future, considering that Oracle developed and released Solaris 11.4 to replenish plenty of features on Solaris that have already been implemented on other UNIX-like OSs but missing from Solaris <11.4, also bring a good deal of new features.
### References

- [Change tcp keepalive options from ms to seconds (DISRUPTIVE)](https://lists.dragonflybsd.org/pipermail/commits/2019-July/719125.html)
- [DragonFly BSD 5.8 release notes](https://www.dragonflybsd.org/release58/)
- [DragonFly TCP](https://man.dragonflybsd.org/?command=tcp&section=4)
- [Solaris 11.3 TCP](https://docs.oracle.com/cd/E86824_01/html/E54777/tcp-7p.html)
- [Solaris 11.4 TCP](https://docs.oracle.com/cd/E88353_01/html/E37851/tcp-4p.html)
- [Solaris 11.4 release notes](https://docs.oracle.com/cd/E37838_01/html/E60973/)

Signed-off-by: Andy Pan <i@andypan.me>
2024-06-26 06:10:00 +02:00
Andy Pan
ceb6551fa2 gcc: fix the -Wincompatible-pointer-types errors
---------

Signed-off-by: Andy Pan <i@andypan.me>
2024-06-01 18:24:43 +02:00
Andy Pan
8976100a44 unix: fail the operation when SO_REUSEPORT has no load balancing
---------

Signed-off-by: Andy Pan <i@andypan.me>
2024-05-18 16:58:50 +03:00
Andy Pan
832f52692e aix: enable SO_REUSEPORT on AIX
AIX 7.2.5 added the feature that would add the capability
to distribute incoming connections across all listening ports.

https://www.ibm.com/support/pages/how-get-better-listening-performance-multiple-listening-sockets-using-same-port-number-soreuseport

---------

Signed-off-by: Andy Pan <i@andypan.me>
2024-05-18 16:57:54 +03:00
Cœur
6d125f5486
Fix evbuffer_file_segment_new 64-bit support on Win32 (#1637)
This fixes the problematic #define fstat _fstat which would only support files up to 2 GB.

Also refactored it as evutil_fd_filesize to avoid exposing stat when not necessary.
2024-05-06 09:28:48 +02:00
Andy Pan
cbbf209c08
Support SO_REUSEPORT on FreeBSD, DragonFly and Solaris (#1624)
## References

- [The SO_REUSEPORT socket option on Linux](https://lwn.net/Articles/542629/)
- [DragonFly Release 3.6](https://www.dragonflybsd.org/release36/)
- [FreeBSD 12.0-RELEASE Release Notes](https://www.freebsd.org/releases/12.0R/relnotes/)
- [SO_REUSEPORT on Solaris 11.4](https://docs.oracle.com/cd/E88353_01/html/E37843/setsockopt-3c.html)

Co-authored-by: Azat Khuzhin <azat@libevent.org>
2024-04-29 07:31:34 +02:00
dockercui
147298a2d1 Fix some comments
Signed-off-by: dockercui <dockercui@aliyun.com>
2024-04-24 08:27:12 +02:00
Liu Dongmiao
39073df831 evutil: don't call memset before memcpy
In `evutil_parse_sockaddr_port`, it would `memset` the `out` to zero,
however, the `memset` is unnecessary before `memcpy`, and may cause
undefined behavior if the `outlen` is invalid.

This should close #1573.
2024-03-31 18:32:36 +02:00
Andy Pan
0054b9aadb
Enable the full TCP KeepAlive mechanism on Windows (#1568)
#1532 implemented the full support of TCP Keep-Alives on UNIX-like OS's while leaving a `TODO` for Windows. This PR intends to resolve that `TODO`.

## References
 
- [SIO_KEEPALIVE_VALS Control Code](https://learn.microsoft.com/en-us/windows/win32/winsock/sio-keepalive-vals)
- [IPPROTO_TCP socket options](https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-tcp-socket-options)
- [TCP_KEEPINVTL and TCP_KEEPIDLE - Socket Keep Alives not working](https://cygwin.com/pipermail/cygwin/2020-June/245436.html)
- [Cygwin: tcp: Support TCP_KEEPIDLE, TCP_KEEPCNT, TCP_KEEPINTVL](https://sourceware.org/pipermail/cygwin-cvs/2020q3/014473.html)
- [Add cross-platform support for keep-alive socket options](https://github.com/dotnet/corefx/pull/29963/files)
2024-03-11 10:12:14 +01:00
Andy Pan
e66df92cfc
Accept SOCK_NONBLOCK/SOCK_CLOEXEC in type argument of socketpair (#1567)
Setting `SOCK_NONBLOCK` and `SOCK_CLOEXEC` in the `type` argument of `socketpair()` is widely supported across UNIX-like OS: Linux, *BSD, Solaris, etc., as is the `socket()`. This will conserve several extra system calls, we should use it where available.

### References

- [socketpair(2) on Linux](https://man7.org/linux/man-pages/man2/socketpair.2.html#HISTORY)
- [socketpair(2) on FreeBSD](https://man.freebsd.org/cgi/man.cgi?query=socketpair&sektion=2#DESCRIPTION)
- [socketpair(2) on DragonFly](https://man.dragonflybsd.org/?command=socketpair&section=2)
- [socketpair(2) on NetBSD](https://man.netbsd.org/socketpair.2#DESCRIPTION)
- [socketpair(2) on OpenBSD](https://man.openbsd.org/socketpair.2)
- [socketpair(3C) on Solaris](https://docs.oracle.com/cd/E88353_01/html/E37843/socketpair-3c.html)

Changelog:
- Set SOCK_NONBLOCK and SOCK_CLOEXEC in the type argument of socketpair
- Avoid EPROTOTYPE on macOS and OpenBSD
- Eliminate the warnings about unused variables
- Add some comments
2024-03-07 09:19:11 +01:00
Andy Pan
921097477e Simplify TCP_KEEPALIVE_ABORT_THRESHOLD settings on Solaris 2024-03-03 13:57:38 +01:00
Azat Khuzhin
5cec3417bf
Make Infer static analyzer happy (#1554) 2024-02-19 08:21:11 +01:00
Ramon Ortega de Voor
ed35b30feb Fix two compiler errors for unused variable and undefined function
In buffer.c a variable "flags" and a label "done" are defined but
never used if "EVENT__HAVEMMAP" is not defined.

The code does not work on platforms which do not provide
the function `socketpair()`. Introduce EVENT__HAVE_SOCKETPAIR flag
which determines if `socketpair()` or `evutil_ersatz_socketpair()`
is used.
2024-02-19 08:19:24 +01:00
Azat Khuzhin
3cf996f0ed evutil: add a comment why no NULL check in evutil_inet_pton_scope() 2024-02-18 19:53:58 +01: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
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
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
Michael Davidsaver
f76bd641f2 increment _WIN32_WINNT in evutil.c 2023-07-29 08:12:33 -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
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
FreeCipher
b30c0d0466
Use mm_* functions in evutil_inet_pton_scope (#1476) 2023-06-20 21:55:00 +02:00
Edoardo Lolletti
765fb4108d
Fix compilation on non recent windows SDKs (#1399)
In commit f8bb9d8 the header stringapiset.h was included, very likely because the user who made the change saw that the funciton WideCharToMultiByte is "declared" in there.
That header tho is a recent addition to the windows headers added in the last years in an attempt from microsoft to split the windows.h header in multiple files, so the inclusion fails when the library is not built with the latest visual studio using the latest windows 10 sdk.
That inclusion can be safely removed as in any case the function WideCharToMultiByte was already included by the windows.h header that is included few lines below.
2023-02-12 16:19:35 +01:00
Edoardo Lolletti
f9134df7d0
Add LEV_OPT_BIND_IPV4_AND_IPV6 flag (#1400)
Libevent introduced the LEV_OPT_BIND_IPV6ONLY to pass to evconnlistener_new_bind to make it automatically set the underlying socket as accepting ipv6 requests. This works fine on posix compliant platforms as by the standard every new AF_INET6 socket is created as both supporting ipv6 and ipv4 connections. But on windows the default is the opposite, with the flag IPV6_V6ONLY  being always enabled by default.

This makes creating a listener to supports both protocols a bit more tricky as winsock doesn't allow changing this flag after evconnlistener_new_bind does all the initial setup because as stated in the docs, you can't change it after the sonnect connected, so one would have to manually create the socket beforehand and set the flag and then call evconnlistener_new with the socket itself.

It would be nice to have libevent keep a consistent behaviour across the platforms in this scenario, maybe or by making it always set IPV6_V6ONLY  to false unless LEV_OPT_BIND_IPV6ONLY is passed, in which case it's set to true, or add another flag to forcefully set it to false and keep the system dependent behaviour as default.

So this patch add new option for libevent listeners to bind to both - LEV_OPT_BIND_IPV4_AND_IPV6
2023-02-04 15:00:48 +01:00
OgreTransporter
ef8f8caab6
Fix syntax error (#1369) 2022-11-04 23:52:01 +01:00
Michael Madsen
ff99f67a1a
fixed missing check for null after strdup in evutil_inet_pton_scope (#1366) 2022-11-04 11:59:17 +03:00
zhenhaonong
f8bb9d8484 Fix socketpair failure when temporary directory has non-latin character 2022-09-26 21:43:21 +03:00
Tomas Gonzalez
9a38bc5f73 evutil: Fix evutil_freeaddrinfo
During testing on win32, util/getaddrinfo failed with NULL hint info

     r = evutil_getaddrinfo("www.google.com", NULL, NULL, &ai);

throwing a critical heap exception when evutil_freeaddrinfo is called.
This is because of improper use of freeaddrinfo when nodes within the
ai structure are allocated using mm_malloc or mm_calloc
(EVUTIL_AI_LIBEVENT_ALLOCATED)

This adds the flag in apply_socktype_protocol_hack and walks the linked
list in evutil_freeaddrinfo removing linked list nodes that are custom
allocated before calling freeaddrinfo.
2021-11-04 15:46:59 -07:00
Azat Khuzhin
86ae44ce4d Fix O_RDONLY (_O_RDONLY) under mingw
O_RDONLY is defined only if [1]:

    !defined(NO_OLDNAMES) || defined(_POSIX)

  [1]: d0d7f78483/mingw-w64-headers/crt/fcntl.h (L35)
2021-04-27 09:04:01 +03:00
guoxiang1996
6c644949d5
Change log message in evutil_make_socket_nonblocking in win32
Co-authored-by: guoxiang2 <guoxiang2@yy.com>
2020-10-10 22:23:14 +03:00
Pierce Lopez
65199178c6 windows socketpair tmpfile: use random prefix
fixes #1058

GetTempFileNameA() takes an optional prefix, and a "unique" long value
which can optionally be zero, which causes it to automatically
increment until a not-yet-existing filename is found.

When libevent creates many AF_UNIX socketpairs on windows,
it slows down dramatically, due to always using the same blank prefix,
and GetTempFileNameA() needing to iterate through all the existing
socketpair filenames. With a present and varying prefix, it will have
much less need to iterate.

It was also possible for a race with other processes also using
blank-prefix tmpfile names to result in both trying to start using
the same name at the same time (because libevent deletes the file
and then re-creates it as a unix socket), which should now be
much less likely.

Unfortuantely, "much" is just a factor of 32k, because the prefix
is only 3 characters, and windows filesystems are case-insensitive,
so doing better would require more sophisticated windows API usage
and charset trickyness.
2020-07-26 18:01:35 -04:00
Azat Khuzhin
37dbb35080 http: fix undefined-shift in EVUTIL_IS*_ helpers
evutil.c:2559:1: runtime error: left shift of 1 by 31 places cannot be represented in type 'int'
    #0 0x4f2be0 in EVUTIL_ISXDIGIT_ libevent/evutil.c:2559:1
    #1 0x4bd689 in regname_ok libevent/http.c:4838:7
    #2 0x4bc16b in parse_authority libevent/http.c:4958:9
    #3 0x4bb8b5 in evhttp_uri_parse_with_flags libevent/http.c:5103:7
    #4 0x4bb762 in evhttp_uri_parse libevent/http.c:5050:9
    #5 0x4b8f41 in evhttp_parse_query_impl libevent/http.c:3505:9
    #6 0x4b8ed7 in evhttp_parse_query libevent/http.c:3569:9

Bug: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=23291
Report: https://oss-fuzz.com/testcase-detail/5670743106125824
2020-06-25 09:51:20 +03:00
Aleksandr-Melnikov
61fc2bf2e9
There is typo in GetAdaptersAddresses windows library. It should be iphlpapi.dll 2020-05-05 10:31:09 +01:00
Philip Homburg
9fecb59a94 Parse IPv6 scope IDs. 2019-11-06 21:07:16 +03:00
yuangongji
879d249619 evutil: make evutil_socketpair() have the same behavior on Windows with build number lower and higher than 17063 2019-10-18 21:03:40 +08:00
yuangongji
dda8968c71 evutil: implement socketpair with unix domain socket on Win10 2019-10-14 23:23:56 +03:00
yuangongji
7806f3ec78
Missing <winerror.h> on win7/MinGW(MINGW32_NT-6.1)/MSYS 2019-07-11 10:21:33 +03:00
Azat Khuzhin
4f6fc092b3
evutil: drop force_check from evutil_check_interfaces() (unused) 2019-06-15 00:21:04 +03:00
jeremyerb
0de2b1455b
evutil: set the have_checked_interfaces in evutil_check_interfaces()
Closes: #836 (cherry-picked)
Fixes: #834
2019-06-15 00:21:04 +03:00
Azat Khuzhin
23e79fd764
Check existence of IPV6_V6ONLY in evutil_make_listen_socket_ipv6only() (mingw32)
MinGW 32-bit 5.3.0 does not defines it and our appveyour [1] build
reports this instantly:
    evutil.c: In function 'evutil_make_listen_socket_ipv6only':
    evutil.c:392:40: error: 'IPV6_V6ONLY' undeclared (first use in this function)
      return setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*) &one,

  [1]: https://www.appveyor.com/docs/windows-images-software/#mingw-msys-cygwin

Another solution will be to use mingw64 which has it, but I guess we do
want that #ifdef anyway.
2018-10-28 18:39:20 +03:00
Murat Demirten
387d91f9ab
listener: ipv6only socket bind support
According to RFC3493 and most Linux distributions, default value is to
work in IPv4-mapped mode. If there is a requirement to bind same port
on same ip addresses but different handlers for both IPv4 and IPv6,
it is required to set IPV6_V6ONLY socket option to be sure that the
code works as expected without affected by bindv6only sysctl setting
in system.

See an example working with this patch:
https://gist.github.com/demirten/023008a63cd966e48b0ebcf9af7fc113

Closes: #640 (cherry-pick)
2018-10-26 07:24:29 +03:00
Azat Khuzhin
6966d39f38
Split evutil_found_ifaddr() into helpers (evutil_v{4,6}addr_is_local()) 2018-10-25 00:49:45 +03:00
Azat Khuzhin
d5f85257b7
Use INADDR_ANY over 0 in evutil_found_ifaddr() 2018-10-25 00:09:03 +03:00
Azat Khuzhin
ab406fca30
Replace EVUTIL_V4ADDR_IS_*() macroses with static inline functions
Macros over static inline over and over again:
- readability
- type safety
2018-10-25 00:08:38 +03:00
Azat Khuzhin
117dc92129
Filter link-local IPv4 addresses in evutil_found_ifaddr()
Fixes: #668
2018-10-25 00:06:47 +03:00
Xiang Zhang
2e52bace9f fix evutil_make_internal_pipe_'s comment that fd[0] for read and f[1] for write 2017-03-18 15:25:20 +03:00
tim-le
8df81546f3 evutil: fix a typo of comment
Fixes: #481
Fixes: #480
2017-03-05 18:14:02 +03:00
Azat Khuzhin
5a157c871c evutil: mark ai_find_protocol() static (prototype-less) 2016-08-10 16:09:45 +03:00
Azat Khuzhin
40730ae333 Fix getaddrinfo under solaris (for multiprotocol case)
During testing on solaris 11.3, util/getaddrinfo failed at:
  memset(&hints, 0, sizeof(hints));
  hints.ai_flags = EVUTIL_AI_NUMERICHOST;
  r = evutil_getaddrinfo("1.2.3.4", NULL, &hints, &ai);
  tt_assert(ai_find_by_protocol(ai, IPPROTO_TCP));

And this is because solaris's getaddrinfo() returns:
$6 = {
  ai_flags = 32,
  ai_family = 2,
  ai_socktype = 0,
  ai_protocol = 0, <-- no proto
  ai_addrlen = 16,
  ai_canonname = 0x0,
  ai_addr = 0x815d658,
  ai_next = 0x0 <-- nothing else
}

So we should emulate this too.

Plus introduce helper that will search through all results, not only first one.

Fixes: util/getaddrinfo
Fixes: #354
2016-08-10 11:15:15 +03:00