281 Commits

Author SHA1 Message Date
Nick Mathewson
86f5742015 Add two implementations of getaddrinfo: one blocking and one nonblocking.
The entry points are evutil_getaddrinfo and evdns_getaddrinfo respectively.
There are fairly extensive unit tests.

I believe this code conforms to RFC3493 pretty closely, but there are
probably more issues.  It should get tested on more platforms.

This code means we can dump the well-intentioned but weirdly-implemented
bufferevent_evdns and evutil_resolve code.

svn:r1537
2009-11-16 22:25:46 +00:00
Nick Mathewson
72bafc175a Remove the stupid brokenness where DNS option names needed to end with a
colon.

svn:r1536
2009-11-16 22:23:55 +00:00
Nick Mathewson
629a613398 When running set[ug]id, don't check the environment.
Idea from OpenBSD, but made a bit more generic to handle uncivilized lands
that do not define issetugid.

svn:r1530
2009-11-15 18:59:59 +00:00
Nick Mathewson
e2b2de79bf Use arc4random() for dns transaction ids where available. Patch taken from OpenBSD
svn:r1528
2009-11-15 18:59:48 +00:00
Nick Mathewson
74871cacb8 Change event_base.activequeues to "array of eventlist".
Previously, event_base.activequeues was of type "array of pointers to
eventlist."  This was pointless: none of the eventlists were allowed
to be NULL.  Worse, it was inefficient:

  - It made looking up an active event queue take two pointer
    deferences instead of one, thus risking extra cache misses.
  - It used more RAM than it needed to, because of the extra pointer
    and the malloc overhead.

Also, this patch fixes a bug where we were saying
calloc(N,N*sizeof(X)) instead of calloc(N,sizeof(X)) when allocating
activequeues.  That part, I'll backport.

Also, we warn and return -1 on failure to allocate activequeues,
rather than calling event_err.

svn:r1525
2009-11-09 19:37:27 +00:00
Nick Mathewson
37e23f806b Patch from Ryan Phillips: accept ipv6 addresses returned by getaddrinfo in http.c
svn:r1522
2009-11-09 18:50:20 +00:00
Nick Mathewson
e88079a82c Make persistent timeouts more accurate.
Previously, if the user scheduled a persistent timeout for {1,0}, we
would schedule the first one at "now+one second", and then when we
were about to run its callback, we would schedule it again for one
second after that.  This would introduce creeping delays to the event
that was supposed to run every second.

Now, we schedule the event for one second after it was _last
scheduled_.  To do this, we introduce internal code to add an event at
an _absolute_ tv rather than at now+tv.

svn:r1520
2009-11-09 18:30:57 +00:00
Nick Mathewson
ab96b5f3f5 Add an option to disable the timeval cache.
svn:r1518
2009-11-09 18:30:33 +00:00
Nick Mathewson
693c24ef9d Implement queued timeouts for case where many timeouts are the same.
Libevent's current timeout code is relatively optimized for the
randomly scattered timeout case, where events are added with their
timeouts in no particular order.  We add and remove timeouts with
O(lg n) behavior.

Frequently, however, an application will want to have many timeouts
of the same value.  For example, we might have 1000 bufferevents,
each with a 2 second timeout on reading or writing.  If we knew this
were always the case, we could just put timeouts in a queue and get
O(1) add and remove behavior.  Of course, a queue would give O(n)
performance for a scattered timeout pattern, so we don't want to
just switch the implementation.

This patch gives the user the ability to explicitly tag certain
timeout values as being "very common".  These timeout values have a
cookie encoded in the high bits of their tv_usec field to indicate
which queue they belong on.  The queues themselves are each
triggered by an entry in the minheap.

See the regress_main.c code for an example use.

svn:r1517
2009-11-09 17:16:30 +00:00
Nick Mathewson
4d48cf61a2 Fix kqueue.c build on GNU/kFreeBSD systems.
Yes, some people like to have a BSD-family kernel (thus getting
kqueue) with a GNU-family libc (thus occasionally mandating
_GNU_SOURCE).

Thanks to Debian for noticing this.

svn:r1514
2009-11-06 21:13:25 +00:00
Nick Mathewson
0b9eb1bffb Add a bufferevent function to resolve a name then connect to it.
This function, bufferevent_socket_connect_hostname() can either use
evdns to do the resolve, or use a new function (evutil_resolve) that
uses getaddrinfo or gethostbyname, like http.c does now.

This function is meant to eventually replace the hostname resolution mess in
http.c.

svn:r1496
2009-11-03 20:40:48 +00:00
Nick Mathewson
0fd0255fa4 Remove compat/sys/_time.h
I've gone through everything that it declared to see where it was used,
and it seems that we probably don't need it anywhere.

Here's what it declared, and why I think we're okay dropping it.

o struct timeval {}
  (Used all over, and we can't really get away with declaring it ourselves;
  we need the same definition the system uses.  If we can't find struct
  timeval, we're pretty much sunk.)

o struct timespec {}
  (Used in event.c, evdns.c, kqueue.c, evport.c.  Of these,
   kqueue.c and event.c include sys/_time.h.  event.c conditions its use on
   _EVENT_HAVE_CLOCK_GETTIME, and kqueue() only works if timespec is defined.)

o TIMEVAL_TO_TIMESPEC
  (Used in kqueue.c, but every place with kqueue has sys/time.h)

o struct timezone {}
  (event2/util.h has a forward declaration; only evutil.c references it and
   doesn't look at its contents.)

o timerclear, timerisset, timercmp, timeradd, timersub
  (Everything now uses the evutil_timer* variants.)

o ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF, struct itemerval
  (These are only used in test/regress.c, which does not include _time.h)

o CLOCK_REALTIME
  (Only used in evdns.c, which does not include _time.h)

o TIMESPEC_TO_TIMEVAL
o DST_*
o timespecclear, timespecisset, timespeccmp, timespecadd, timespecsub
o struct clockinfo {}
o CLOCK_VIRTUAL, CLOCK_PROF
o TIMER_RELTIME, TIMER_ABSTIME
  (unused)

svn:r1494
2009-11-03 19:54:56 +00:00
Nick Mathewson
904b5721cb Avoid calling exit() during event_base_new*()
Previously, each of the three make-an-event-base functions would exit
under different, weird circumstances, but return NULL on others.
  - All three would exit on OOM sometimes.
  - event_base_new() and event_init() would die	if all backends	were
    disabled.
  - None of them would die if the socketpair() call failed.

Now, only event_init() exits on failure, and it exits on every kind of
failure.  event_base_new() and event_base_new_with_config() never do.

svn:r1472
2009-10-27 06:47:25 +00:00
Nick Mathewson
e9ee1057e6 Give event_assign a return value, and make it less inclined to exit().
We also refactor event_assign so that it is the core function, and
event_set() is only the wrapper.

svn:r1469
2009-10-27 04:25:45 +00:00
Nick Mathewson
7f10fac342 Note assert-related change in changelog
svn:r1465
2009-10-26 20:07:06 +00:00
Nick Mathewson
a8267663de API to replace all calls to exit() with a user-supplied fatal-error handler.
Also, add unit tests for logging.

svn:r1462
2009-10-26 19:59:51 +00:00
Nick Mathewson
b73ad7bc45 Treat the bitwise OR of two enum values as an int.
This makes our interfaces usable from C++, which doesn't believe
you can say    "bufferevent_socket_nase(base, -1,
BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS)" but which instead
would demand "static_cast<bufferevent_options>(BEV_OPT_CLOSE_ON_FREE|
BEV_OPT_DEFER_CALLBACKS))" for the last	argument.

Diagnosis and patch from Chris Davis.

svn:r1456
2009-10-21 18:48:22 +00:00
Nick Mathewson
d8164d0cfc Fix win32 connect() event handling.
Christopher Davis reported:

    Connection failures aren't reported on Windows when
    using bufferevent_socket_connect, because Windows uses
    select's exceptfds to notify of failure, and libevent
    treats them like read events. Only the write event
    handler is currently used to handle connection events.

We should think hard about this one, since it changes
behavior from 1.4.x.  Anything that worked on Mac/Unix before
will work more consistently on Windows now... but this might
break stuff that worked only on Windows, but nowhere else.

Patch from Chris Davis.

svn:r1454
2009-10-21 07:00:14 +00:00
Nick Mathewson
6b22e74aa1 Add locking to event_base_loop.
This is harder than it sounds, since we need to make sure to
release the lock around the key call to the kernel (e.g.,
select, epoll_wait, kevent), AND we need to make sure that
none of the fields that are used in that call are touched by
anything that might be running concurrently in another
thread.  I managed to do this pretty well for everything but
poll().  With poll, I needed to introduce a copy of the
event_set structure.

This patch also fixes a bug in win32.c where we called
realloc() instead of mm_realloc().

svn:r1450
2009-10-21 03:54:00 +00:00
Niels Provos
e1c9b84ae6 Fix compilation for listener.h for C++ - missing extern "C". Patch from Ferenc Szalai.
svn:r1448
2009-10-19 16:20:12 +00:00
Nick Mathewson
25af695441 When a bufferevent_connect() call fails, give the client an error callback.
Patch from Christopher Davis.

svn:r1444
2009-10-14 00:46:47 +00:00
Nick Mathewson
fc83ca3c70 Fix some crash bugs when initializing evdns
svn:r1443
2009-10-14 00:46:40 +00:00
Nick Mathewson
633f3fb7aa Add changelog for last commit
svn:r1440
2009-10-02 03:07:29 +00:00
Nick Mathewson
d5b640fc16 Apply Ka-Hing Cheung's event_base_got_[break|exit] patch, with locking and whitespace fixes.
svn:r1438
2009-10-01 15:29:08 +00:00
Niels Provos
8e8d94a3e0 Do not drop data from evbuffer when out of memory; reported by Jacek Masiulaniec
svn:r1436
2009-09-24 22:18:19 +00:00
Nick Mathewson
18fe400805 Forward-port: fix android compilation
svn:r1435
2009-09-23 23:51:26 +00:00
Nick Mathewson
c2ead9f173 Treat events with fd == -1 as addable.
This turns out to simplify a fair bit of logic, including the bufferevent
code, and should fix bug 2850656.

svn:r1431
2009-09-11 21:02:19 +00:00
Nick Mathewson
85255a6397 Make epoll use less RAM.
We do this by not allocating the maximum epoll_event array for the epoll
backend at startup.  Instead, we start out accepting 32 events at a time, and
double the array's size when it seems that the OS is generating events faster
than we're requesting them.  This saves up to 374K per epoll-based
event_base.  Resolves bug 2839240.

svn:r1428
2009-09-11 18:47:35 +00:00
Nick Mathewson
e3f89fa275 Add a trivial race-fix from Chromium: do not try to re-detect whether we have a monotonic clock every time we make a new event_base.
svn:r1427
2009-09-11 18:21:57 +00:00
Nick Mathewson
3b461a6d03 Treat a negative number of bytes to read as the kernel saying "I don't know."
svn:r1426
2009-09-11 18:21:37 +00:00
Nick Mathewson
f65b8b0964 On connect, call only one of BEV_EVENT_CONNECTED or writecb.
Previously, if we had a socket bufferevent in connect state, we'd send
both of these to indicate that the connection was done.  That was broken
since the point of adding BEV_EVENT_CONNECTED was so that we could
distinguish "we're connected" and "we wrote something".

Now, writecb is called only when
   A) the connection finished but the user never put the socket into a
      "connecting" state, or
   B) data was actually written.

svn:r1425
2009-08-19 20:55:25 +00:00
Nick Mathewson
2c1b0e4428 Fix build warnings and add changelog entry for evhttp patches.
svn:r1424
2009-08-16 19:22:15 +00:00
Nick Mathewson
22bd5b4287 Support sendfile on solaris: patch from Caitlin Mercer.
svn:r1419
2009-08-16 16:40:42 +00:00
Nick Mathewson
7a55c48d77 Add a few missing changelog entries
svn:r1412
2009-08-09 20:18:00 +00:00
Nick Mathewson
d41347722a Refactor evbuffer_readln() into a search-for-eol function and an extract-line function.
svn:r1404
2009-07-31 17:35:42 +00:00
Nick Mathewson
7c688dd9a2 New function to expose bufferevent.enabled
svn:r1401
2009-07-31 14:41:45 +00:00
Nick Mathewson
621aafd27a Export sockaddr comparison functionality.
svn:r1400
2009-07-30 22:11:23 +00:00
Nick Mathewson
d1a2254bf2 Fix some bugs in bufferevent_socket_connect
svn:r1398
2009-07-30 20:41:31 +00:00
Nick Mathewson
7c20a6ae52 Export an ev_socklen_t.
svn:r1391
2009-07-30 17:01:21 +00:00
Nick Mathewson
75fe762e03 Accessor function to get a listener's associated fd
svn:r1390
2009-07-30 17:00:56 +00:00
Nick Mathewson
3c99c79df9 Changelog entry for msvc fixes.
svn:r1388
2009-07-28 19:45:54 +00:00
Nick Mathewson
72ea534f8e Export evutil_str[n]casecmp as evutil_ascii_str[n]casecmp.
svn:r1387
2009-07-28 19:41:57 +00:00
Nick Mathewson
12199fa7a5 Fix segfault during failed allocatino of locked evdns base.
We need to comb the rest of the code to make sure that we don't blindly wrap
functions in LOCK(x), UNLOCK(x) when those functions might contain a FREE(x)
in the middle.

Rocco Carbone found and reported this bug.

svn:r1384
2009-07-28 17:11:03 +00:00
Nick Mathewson
f8b527e6a1 Fix a dumb bug where we would allocate too little memory in event_get_supported_methods().
svn:r1383
2009-07-28 05:09:06 +00:00
Nick Mathewson
709c21c48c Bufferevent support for openssl.
This code adds a new Bufferevent type that is only compiled when the
openssl library is present.  It supports using an SSL object and an
event alert mechanism, which can either be an fd or an underlying
bufferevent.

There is still more work to do: the unit tests are incomplete, and we
need to support flush and shutdown much better.  Sometimes events are
generated needlessly: this will hose performance.

There's a new encrypting proxy in sample/le-proxy.c.

This code has only been tested on OSX, and nowhere else.

svn:r1382
2009-07-28 04:03:57 +00:00
Nick Mathewson
e8400a43ca Rename encode_int(64) to avoid polluting the global namespace.
They're now called evtag_encode_int(64).  The old names are available
as macros in event2/tag_compat.h.

Also, add unit tests for encode/decode_int64.

svn:r1365
2009-07-20 14:55:35 +00:00
Nick Mathewson
9fcd84d196 Include disabled methods in event_get_supported_methods() output.
Previously, events that were disabled using EVENT_NO* were left out of
event_get_supported_methods().  This was wrong, broke unit tests
(under some circumstances) and left the user with no good way to tell
which methods were actually compiled in.

Fixes bug 2821015.

svn:r1344
2009-07-14 19:19:45 +00:00
Nick Mathewson
6b4b77a265 Make event_del(E) block while E is running in another thread.
This gives you the property that once you have called event_del(E),
you know that E is no longer running or pending or active at all, and
so it is safe to delete the resource used by E's callback.

svn:r1341
2009-07-14 16:54:48 +00:00
Nick Mathewson
d3a8ccb807 Change use of AC_CHECK_LIB to AC_SEARCH_LIBS.
Patch from Zack Weinberg.  His message:

  This one eliminates all use of AC_CHECK_LIB in the configure script.
  AC_CHECK_LIB has a serious flaw: if the library you mention *exists*
  but is not *necessary* for the function you want, it adds it to
  $(LIBS) anyway.  This was fine in the days of static libraries,
  because the linker would ignore an .a library that didn't contain
  anything you needed. However, ELF shared libraries are different
  (let's not get into why): the linker will by default record a
  DT_NEEDED entry for every shared object mentioned on the link
  command line. Thus, every use of AC_CHECK_LIB is a potential
  unnecessary DT_NEEDED, making extra work for the dynamic loader. The
  cure is simply to use AC_SEARCH_LIBS instead; it first tries to find
  the function you ask for in libc, and only if that doesn't work does
  it try to use the extra library you mention.

  For the same reasons, pkg-config .pc files should distinguish
  between the libraries to use for shared linkage (Libs:) and the
  additional libraries needed for static linkage (Libs.private:). I
  have also made that correction in this patch. I also took the
  opportunity to clean up the substitution variables a little and make
  absolutely sure that the core library does not get linked against
  zlib.

svn:r1338
2009-07-10 19:38:16 +00:00
Nick Mathewson
a501d6833b Add a lock/unlock pair inside the event callbacks in bufferevents.
This fixes part of bug 2800642, I believe, though there is still a
general race condition in multithreaded use of events that we need to
think about.

svn:r1337
2009-07-10 19:34:00 +00:00