Set SO_SNDBUF on DTLS socket and handle -EAGAIN on it

The UDP would otherwise get a huge backlog of queued packets, and VoIP
over the VPN would become unusable.

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
Clear peer_addr if aborting XML POST changes the hostname

peer_addr needs to be reset if the host changes, or else we will connect
to the wrong host, and probably trigger a certificate mismatch.

References:

Commit d9aa66ba2d (Clear cached peer_addr where necessary)
http://lists.infradead.org/pipermail/openconnect-devel/2013-August/001136.html

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
library: Remove declaration for nonexistent openconnect_get_vpn_name()

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
auth: Hack around const warnings

Seen on an NDK build:

    ../../../auth.c: In function 'prepare_stoken':
    ../../../auth.c:901:15: warning: assignment discards 'const' qualifier from pointer target type [enabled by default]
    ../../../auth.c:909:14: warning: assignment discards 'const' qualifier from pointer target type [enabled by default]
    ../../../auth.c:916:14: warning: assignment discards 'const' qualifier from pointer target type [enabled by default]
    ../../../auth.c:923:14: warning: assignment discards 'const' qualifier from pointer target type [enabled by default]

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
Introduce new helper functions for cancel_fd checking

The same code shows up in 12 places, so we'll move it into an inline
function.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
library: Add new openconnect_setup_cancel_pipe() call

Most callers will use a simple nonblocking UNIX pipe as their cancel_fd,
so provide a convenience function for creating it.  Also, libopenconnect
will take care of cleaning up pipes created in this manner when the
library instance is freed.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
Move signal handling out of mainloop

In order to make the mainloop a reusable library function, it should not
take over any POSIX signals.  Handle the signals in main.c instead, and
use the cancel_fd mechanism to tell the mainloop to exit.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
mainloop: Convert vpn_mainloop() into a library function

Rename to openconnect_mainloop() and allow the caller to specify
reconnection parameters.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
main: Introduce xstrdup() function

This will abort on error, instead of causing a segfault.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
tun: Export setup_tun() functionality

Create three new setup_tun() library functions:

    openconnect_setup_tun_device - create a tun device
    openconnect_setup_tun_script - pass traffic through a script
    openconnect_setup_tun_fd - pass traffic through an existing fd

The latter is needed to support Android's VpnService API.

In all cases, the caller no longer needs to directly manipulate
vpninfo->vpnc_script or vpninfo->script_tun.  The former is treated as
a standard caller-allocated, library-freed string.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
Move vpninfo default settings into library

This allows main.c to call openconnect_vpninfo_new() to create the
library instance.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
library: Check for failed allocations in openconnect_vpninfo_new()

This is probably fatal anyway, but it is better to fail in a predictable
manner.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
dtls: Export setup_dtls() function

This is an optional call; the default is "no DTLS."

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
cstp: Export make_cstp_connection()

Just rename it and add it to openconnect.h.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
library: Move the mainloop guts into libopenconnect

Now the standalone openconnect binary only consists of main.c and xml.c.

Unfortunately openconnect_CFLAGS still needs to include the whole kitchen
sink, until we can remove openconnect-internal.h as a dependency.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
library: Update openconnect.pc.in

Add openssl + gnutls to Requires.private for the ssl_library=both case.
Add liboath to Requires.private.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
tun, cstp: Don't exit() on failure

These files are part of a library now so they should report errors back
to the caller.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
library: Free zlib state

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
cstp: Clean up split include/exclude/DNS lists when freeing vpninfo

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
library: Add get/set functions for servercert, ifname, reqmtu

This allows all connection parameters used by nm-openconnect to be set
through the library API.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>