Add fclose() support

Fixes #29

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: strict aliasing in library

Fixes #28

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Update version to v2.0.0-rc5

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: use socket fd and NOT sockaddr in accept

Major mistake in accept() which was checking the given sockaddr
structure instead of the given socket fd. The address structure passed
to accept is meant to be filled up by the accept function thus not
containing any usable data.

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: set addr len for getsockname in accept

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Update version to v2.0.0-rc6

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: support kfreebsd for mmap()

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: change TSOCKS_LOOPBACK bitness

This changes the loopback constant to be in host byte order and use
htonl() when needed in network byte order.

With this, tests pass on powerpc.

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: add autogen.sh to installation procedures

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: socket() type check SOCK_STREAM

Even though connect() makes a check, deny socket creation that are
INET/INET6 but NOT of type SOCK_STREAM. This fix makes our wrapper
handle socket type flags that can be passed to the kernel such as
SOCK_NONBLOCK and SOCK_CLOEXEC.

Furthermore, the type check was *not* right since having a type set to
SOCK_DGRAM also matches SOCK_STREAM when using the & operator.

A unit test is added for the IS_SOCK_STREAM(type) macro that test if a
socket type is a SOCK_STREAM.

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: socketpair() denied for INET[6] socket

The SOCK_STREAM check was wrong since you could pass SOCK_DGRAM and
match the check so the socketpair() now denies INET/INET6 socket.

Note that socketpair() only supports AF_UNIX and AF_LOCAL on most *nix
system.

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Tests: add socket() test

Add unit test for socket() and socketpair().

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Tests: add connect() test

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Refactor the connect() code flow for clarity

This adds a "validate_socket()" function that is called first to make
sure all criteria of a valid socket that torsocks can handle are met.
This has been done to have a single callsite that can do this validation
thus improving the clarity and flow of the code.

It now also returns EPERM for things that we deny instead of EINVAL or
EBADF. Because of that, the connect test has been changed to handle this
new errno value.

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: nullify constant that might be undefined

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Delete old source directory

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: memory leak in connect error path

Fixes coverity issue 1072757.

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: fix NULL dereference on error

Fixes coverity issue 1195182.

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Update version to v2.0.0-rc7

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: add compilation requirements to README.md

Reported-by: Jacob Appelbaum <jacob@appelbaum.net>
Signed-off-by: David Goulet <dgoulet@ev0ke.net>