io: fix io_connect().

How embarrassing: we weren't actually doing a non-blocking connect because
I was using F_SETFD instead of F_SETFL...

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
net: use freeaddrinfo() in _info example.

In fact, almost everyone will want to do this, so include the required
headers in net.h.  This makes usage simpler.

Reported-by: Jeremy Visser <jeremy@visser.name>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
opt/helpers: fix out-of-range check in opt_set_floatval()

opt_set_floatval() uses opt_set_doubleval() to do initial conversion
and bounds checking before casting the result to float.  Previously
the out of bounds check compared the original and float values for
equality and declared anything unequal to be out of bounds. While this
trick works well in the orderly integer world, it can backfire with
floating point. For example, 3.1 resolves to the double more precisely
known as 3.100000000000000088817841970012523233890533447265625, while
3.1f resolves to 3.099999904632568359375.  These are not equal, though
3.1 is generally regarded as being in bounds for a float.  There are
around 8 billion other doubles (i.e. 3.1 +/- a little bit) that map to
the same 3.1f value, of which only one is strictly equal to it.

Why wasn't this discovered by the tests? It turns out that neither
set_floatval nor set_doubleval were tested against non-integral
numbers.  This is slightly improved here.

This patch uses the arguably more reasonable definition of bounds that
is found in opt_set_doubleval(): it excludes numbers that would get
rounded to zero or an infinity. One subtlety is that the double
version allows `--foo=INF` for an explicit infinity without overflow.
This is possibly useful, and there is some fiddling to allow this for
floats. Likewise an explicit zero is allowed, as you would expect.

It is perhaps worth noting that the `*f = d` cast/assignment at the
heart of it all can crash and core dump on overflow or underflow if
the floating point exception flags are in an unexpected state.

Signed-off-by: Douglas Bagnall <douglas@halo.gen.nz>
configurator: add BUILTIN_CTZ*, BUILTIN_FFS, and ICCARM_INTRINSICS

Realistically, the ICCARM_INTRINSICS probably won't ever be generated
by configurator, but will show up in manually created or alternately
generated config.h's.

Signed-off-by: Cody P Schafer <dev@codyps.com>
tools/ccanlint: add global .valgrind_suppressions file.

Seems like my local Ubuntu install is giving bogus warnings.  Allow
local valgrind overrides.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/io: fix io_connect.

getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len) gives err == ECONNREFUSED
when connection is refused.  Handle this (and other error cases).

And we need F_SETFL not F_SETFD to restore blocking on the socket!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/io: rewrite.

I found it difficult to use myself, particularly io_duplex().

So this removes that, as well as timers and debug (for the moment).

API changes:
1) An io_plan is passed by pointer, rather than copied on the stack.
3) All io_plans are generated using the struct io_conn.
3) tal is the allocator.
4) A new connection must be set up with a callback, so this is now the
   same as one generated from a listener.
5) io_read_partial and io_write_partial take an explicit length.
6) io_always() and io_wait() take an explit in/out arg.
7) io_break() does not return a plan.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/io: eliminate dir argument from io_wait and io_always.

We can use either empty slot for this, so figure it out internally.

This could cause problems if you want to use it with io_duplex, so
document that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/io: duplex support.

This is actually pretty simple now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/io: implement timeouts.

We do this by the simplest method: return from io_loop() and let the caller
sort them out.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/io: implement debug.

Now a simple flag, with an external toggle (no compile time DEBUG
define required).  But it's completely synchronous.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/io: keep more io_plan details internal.

To write a normal helper you only need access to the args, so only
expose that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/io: io_halfclose.

Helper for a common case.  Replace all but 1 in tests.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/io: add io_out_wait() and io_out_always().

This specificity is required, for example, when doing:

	return io_duplex(conn, io_read(...), io_always(...));

The workaround suggested doesn't work, because io_duplex_prepare()
asserts() if the plans aren't UNSET to start.  And pettycoin needs this.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/io: use shutdown() before close.

During a debugging session, I wondered why poll() wasn't exiting
when I killed a (local) peer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
io: don't do wakeup immediately.

ccan/io users don't expect to deal with callbacks inside each other; we should
just mark woken connections as if they were io_always.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
io: always make fds O_NONBLOCK.

Debugging an issue where pettycoin would become unresponsive, I discovered
this:

poll([{fd=5, events=POLLIN}, {fd=19, events=POLLIN}, {fd=6, events=POLLIN}, {fd=15, events=POLLIN}, {fd=8, events=POLLIN}, {fd=-11}, {fd=7, events=POLL
OUT}], 7, -1) = 1 ([{fd=7, revents=POLLOUT}]) <0.000014>
write(7, "\224]\4\0\4\0\0\0\200\203\16\234\v\262\276\321h\357\217Y\0\204\21\31\253\304#U\0206}\20"..., 286100) = 159280 <98.894019>

Despite poll saying the (TCP socket) fd was ready, the write took 98 seconds!
The results were far more reasonable with O_NONBLOCK:

write(9, "%\247l0\337^\216\24\323\2705\203Y\340h\2767/bM\373?dM\254\22g\310\v\0\0\0"..., 206460) = 40544 <0.000052>

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
darray: Add darray_remove()

It is useful to be able to remove elements from other than the end, even
if it is slow.

Signed-off-by: Andy Grover <agrover@redhat.com>
endian: Wrap arguments in braces to silence warnings

When using the endian swapping marcos with multiple arguments that are
or'd together:

  CPU_TO_BE64(THIS_THING | THAT_THING)

gcc will emit this warning:

  warning: suggest parentheses around arithmetic in operand
  of ‘|’ [-Wparentheses]

Wrap the arugments in braces to avoid this.

Signed-off-by: Joel Stanley <joel@jms.id.au>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan_tokenizer: switch example from grab_file to ccan/grab_file.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>