207 Commits

Author SHA1 Message Date
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
Azat Khuzhin
8cbe65d5f4 evdns: export cancel via callbacks in util (like async lib core/extra issues) 2016-03-23 12:46:47 +03:00
Azat Khuzhin
329acc18a0 evutil_parse_sockaddr_port(): fix buffer overflow
@asn-the-goblin-slayer:
  "Length between '[' and ']' is cast to signed 32 bit integer on line 1815. Is
   the length is more than 2<<31 (INT_MAX), len will hold a negative value.
   Consequently, it will pass the check at line 1816. Segfault happens at line
   1819.

   Generate a resolv.conf with generate-resolv.conf, then compile and run
   poc.c. See entry-functions.txt for functions in tor that might be
   vulnerable.

   Please credit 'Guido Vranken' for this discovery through the Tor bug bounty
   program."

Reproducer for gdb (https://gist.github.com/azat/be2b0d5e9417ba0dfe2c):
  start
  p (1ULL<<31)+1ULL
  # $1 = 2147483649
  p malloc(sizeof(struct sockaddr))
  # $2 = (void *) 0x646010
  p malloc(sizeof(int))
  # $3 = (void *) 0x646030
  p malloc($1)
  # $4 = (void *) 0x7fff76a2a010
  p memset($4, 1, $1)
  # $5 = 1990369296
  p (char *)$4
  # $6 = 0x7fff76a2a010 '\001' <repeats 200 times>...
  set $6[0]='['
  set $6[$1]=']'
  p evutil_parse_sockaddr_port($4, $2, $3)
  # $7 = -1

Before:
  $ gdb bin/http-connect < gdb
  (gdb) $1 = 2147483649
  (gdb) (gdb) $2 = (void *) 0x646010
  (gdb) (gdb) $3 = (void *) 0x646030
  (gdb) (gdb) $4 = (void *) 0x7fff76a2a010
  (gdb) (gdb) $5 = 1990369296
  (gdb) (gdb) $6 = 0x7fff76a2a010 '\001' <repeats 200 times>...
  (gdb) (gdb) (gdb) (gdb)
  Program received signal SIGSEGV, Segmentation fault.
  __memcpy_sse2_unaligned () at memcpy-sse2-unaligned.S:36

After:
  $ gdb bin/http-connect < gdb
  (gdb) $1 = 2147483649
  (gdb) (gdb) $2 = (void *) 0x646010
  (gdb) (gdb) $3 = (void *) 0x646030
  (gdb) (gdb) $4 = (void *) 0x7fff76a2a010
  (gdb) (gdb) $5 = 1990369296
  (gdb) (gdb) $6 = 0x7fff76a2a010 '\001' <repeats 200 times>...
  (gdb) (gdb) (gdb) (gdb) $7 = -1
  (gdb) (gdb) quit

Fixes: #318
2016-02-01 16:37:16 +03:00
Mark Ellzey
1c17cfdd2b Merge pull request #296 from lzmths/master
Refactoring conditional directives that break parts of statements.
2015-12-14 12:31:08 -08:00
lzmths
4b41eebeb2 Refactoring conditional directives that break parts of statements. 2015-12-14 11:04:00 -03:00
Mattes D
e8b78957ea Fixed Unicode issue in error messages. 2015-12-14 11:51:35 +01:00
Ed Schouten
ce1776c96d Test against SO_REUSEADDR (along with _WIN32).
This makes the code build on other systems that also don't have
SO_REUSEADDR without requiring special code.

[ azat: partially revert WIN32 check since SO_REUSEADDR on win32 differs from
unix semantics. ]

Closes #275
2015-09-02 19:01:48 +03:00
Ed Schouten
fd36647af1 Don't use BSD u_* types.
These types are not part of POSIX. As we only use them in a small number
of places, we'd better replace them by C standard types. This makes a
larger part of the code build for CloudABI.
2015-08-25 19:15:50 +03:00
Azat Khuzhin
a8d32c2353 util: make @sa const for evutil_socket_connect_() 2015-08-18 20:06:52 +03:00
Mark Ellzey
3499ad9729 Set correct socklen for PF_INET6 sockaddr len
Reported and patched by Pankaj Sharma
2015-06-01 06:55:24 -07:00
Mark Ellzey
043ae7481f Fix garbage value in socketpair util function, stdint?
* Fixed an issue with evutil_ersatz_socketpair_, listen_addr could all
  be compared against with agarbage values. So just memset it before
  using it anywhere.

* Nick might punch me in the face, but if we have stdint.h; (as in
  EVENT__HAVE_STDINT_H is defined), might as well use those instead of
  the manual [U]INT[X}_MAX/MIN muck in there now.
2015-05-11 12:06:01 -04:00
Maciej Soltysiak
b625361a6a Provide support for SO_REUSEPORT through LEV_OPT_REUSABLE_PORT 2014-10-13 17:28:14 +02:00
Nick Mathewson
f691389e82 Change return type of evutil_load_windows_system_library_ to HMODULE
Noted by Miles Chan as issue #146
2014-08-29 14:28:09 -04:00
Nick Mathewson
58fc9b6c0a Fix ubsan warnings when parsing ipv4/ipv6 addrs
left-shifting a one-byte integer by 24 invokes undefined behavior.
Let's not do that.
2014-03-18 11:35:50 -04:00
Joakim Söderberg
4e1439588f Change all uses of WIN32 to _WIN32 2014-01-21 11:30:27 +01:00
Nick Mathewson
3807a30b03 Merge remote-tracking branch 'origin/patches-2.0'
Conflicts:
	util-internal.h
2013-08-19 10:05:14 -04:00
Nick Mathewson
f5ced88cec Really remove RNG seeds from the stack 2013-08-19 10:02:26 -04:00
Nick Mathewson
c307a250d4 Merge remote-tracking branch 'origin/patches-2.0' 2013-05-29 13:32:47 -04:00
Nick Mathewson
e826f19e05 Use windows vsnprintf fixup logic on all windows environments
Previously I'd relied on mingw to provide a vsnprintf with a
conformant return value.  But it appears that some mingw
environments don't do that.
2013-05-29 13:31:53 -04:00
Nick Mathewson
1f29b18f51 Avoid redundant syscall if making a socket cloexec twice
I got the idea from Maxime's last patch.
2013-05-20 12:30:40 -04:00
Maxime Henrion
42c03da9b9 Avoid redundant syscall to make a nonblocking socket nonblocking 2013-05-20 12:23:53 -04:00
Nick Mathewson
ed26561b0c Fix another warning introduced in 0c6ec5d8 2013-04-25 14:48:56 -04:00
Nick Mathewson
eeb700ce31 Fix a warning introduced in 0c6ec5d8 2013-04-25 14:47:26 -04:00
Patrick Pelletier
4ccdd53f78 use hashtable instead of linked list to cache winsock errors
as discussed here:
https://github.com/libevent/libevent/pull/41#issuecomment-13611817
2013-02-25 19:02:32 -08:00