190 Commits

Author SHA1 Message Date
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
Patrick Pelletier
0c6ec5d816 use FormatMessage for winsock errors
as discussed here:
http://archives.seul.org/libevent/users/Feb-2013/msg00004.html
2013-02-14 20:26:38 -08:00
Nick Mathewson
21c962e087 Merge remote-tracking branch 'origin/patches-2.0' 2012-12-20 11:50:23 -05:00
Nick Mathewson
0a822a640e Avoid double-close on getsockname error in evutil_ersatz_socketpair
Found by coverity; CID 739726
2012-12-20 11:48:05 -05:00
Nick Mathewson
61b93af561 Move evutil_rtrim_lws_ to evutil.c where it belongs 2012-11-16 11:41:59 -05:00
Nick Mathewson
6810908a5f Fix some warnings found cross-compiling with mingw32 2012-11-01 18:05:27 -04:00
Nick Mathewson
3d83f94dfa Add process.h include to evutil.c for _getpid 2012-07-23 16:32:43 -04:00
Nick Mathewson
0de587f47e Simplify and correct evutil_open_closeonexec_
The original code needlessly called open in its two- or three-
argument format depending on the O_CREAT flag; this should not be
needed.

The code also leaked an fd if fcntl() failed.

Reported by Dave Hart.
2012-06-25 12:19:26 -04:00
Nick Mathewson
9fbfe9b948 Correctly handle running on a system where accept4 doesn't work.
Previously, we treated EINVAL as the only errno that indicated a
broken accept4.  But EINVAL only appears when one of the SOCK_*
options isn't supported.  If the accept4 syscall itself isn't there,
we'll get an ENOSYS.

Reported by Azat Khuzhin.
2012-05-01 14:34:31 -04:00
Nick Mathewson
c419485442 Split out time-related evutil functions into a new evutil_time.c 2012-04-20 12:19:03 -04:00
Nick Mathewson
3aa44159c5 Tweak the new evutil_weakrand_() code
Make its state actually get seeded.

Document it more thoroughly.

Turn its state into a structure.

Fix a bug in evutil_weakrand_range_() where it could return the top of
the range.

Change its return type to ev_int32_t.

Add a quick unit test to make sure that the value of
evutil_weakrand_range_() is in range.
2012-04-09 11:30:46 -04:00
Nicholas Marriott
e86af4b7e5 Change evutil_weakrand_() to avoid platform random()
This change allows us to avoid perturbing the platform's random(), and
to avoid hitting locks on random() in the platform's libc.

evutil_weakrand_() is, well, weak, so we choose here an algorithm that
favors speed over a number of other possibly desirable properties.
We're using a linear congruential generator, and taking our parameters
from those shared by the OpenBSD random() implementation, and
Glibc's fastest random() implementation.

The low bits of a LCG of modulus 2^32 are (notoriously) less random
than the higher bits.  So to generate a random value in a range, using
the % operator is no good; we ought to divide.  We add an
evutil_weakrand_range_() function to do that.

This code also changes the interface of evutil_weakrand_() so that it
now manipulates an explicit seed, rather than having the seed in a
static variable.  This change enables us to use existing locks to
achieve thread-safety, rather than having to rely on an additional lock.

(Patch by Nicholas Marriott; commit message by Nick Mathewson.)
2012-04-09 10:46:32 -04:00
Nick Mathewson
dcab134708 Merge remote-tracking branch 'origin/patches-2.0' 2012-03-30 10:29:08 -04:00
Nick Mathewson
336dcaeaef Fix a compilation error with MSVC 2005 due to use of mode_t
MSVC apparently doesn't have a mode_t defined, though mingw does.

Found by Savg He.
2012-03-30 10:26:50 -04:00
Mark Ellzey
041ca00c75 Add a new libevent_global_shutdown() to free all globals before exiting.
Mark Ellzey added a function libevent_shutdown() which calls a set of
private functions:

       * event_free_globals()
       * event_free_debug_globals()
       * event_free_debug_globals_locks()
       * event_free_evsig_globals()
       * evsig_free_globals()
       * evsig_free_globals_locks()
       * evutil_free_globals()
       * evutil_free_secure_rng_globals()
       * evutil_free_secure_rng_globals_lock()

Nick tweaked this libevent global shutdown code:

  - rename the function to emphasize that it's for global resources
  - write more in the doxygen
  - make function brace style consistent
  - add a missing void in a function definition.
2012-03-22 11:57:04 -04:00
Nick Mathewson
0d1611dc3e Restore accidentally disabled nanosleep implementation of evutil_usleep. 2012-02-29 17:36:12 -05:00
Nick Mathewson
946b5841c2 Clean up lingering _identifiers. 2012-02-29 15:47:16 -05:00
Nick Mathewson
c7848fa019 Apply the naming convention to our EVUTIL_IS* functions 2012-02-29 15:07:33 -05:00