Commit Graph

66 Commits

Author SHA1 Message Date
Quentin Young
83d3c097ef Remove unnecessary null check before free 2019-07-03 21:36:48 +00:00
m
dc6d19b9ec Port network layer to Winsock
With this change, Hiredis builds with MinGW and runs on Windows.
2019-04-01 11:54:37 +02:00
m
1d092a235a Use recv/send instead of read/write
The recv/send calls are more portable than read/write, since unlike the
latter, the former work with Windows sockets.

We also check for EWOULDBLOCK instead of EAGAIN. On most Unices, EAGAIN
and EWOULDBLBOCK are the same thing. However, on Windows they are
different, and send/recv are expected to give EWOULDBLOCK for
non-blocking sockets.
2019-04-01 11:52:24 +02:00
m
e84086cb92 Introduce a redisFD type
The redisFD type should be equal to the system native socket file
desciptor type (for POSIX, this is a plain int).

We also introduce the REDIS_INVALID_FD value, which maps to -1 on POSIX
systems.
2019-04-01 11:52:24 +02:00
m
1788f41f16 Move network I/O calls to net.c
This makes hiredis.c free from system calls related to socket I/O. This
is also makes the treatment of raw socket connections more similar to
the SSL backend.
2019-04-01 11:52:24 +02:00
Mark Nunberg
4b90429640 Remove redundant line after rebase 2019-02-20 09:11:10 -05:00
Mark Nunberg
3a547b8ec0 Unix: set addrlen so async reconnect uses proper size 2019-02-20 09:10:10 -05:00
Code Hz
be6bb39fda
Fix Invalid argument after redisAsyncConnectUnix 2019-01-28 21:54:42 +08:00
Mark Nunberg
088d1469b3 Fix regression when connecting with Unix sockets (#629) 2018-12-05 07:47:27 -05:00
Mark Nunberg
cbe4ae63ae Handle connection errors better in blocking mode as well 2018-09-25 20:21:40 -04:00
Mark Nunberg
5e6bbf8c60 saddr should be addrlen bytes
Not sizeof saddr.
2018-09-25 20:21:40 -04:00
Mark Nunberg
49974c9359 Call connect(2) again for non-blocking connect
This retrieves the actual error which occurred, as getsockopt is not
always reliable in this regard.
2018-09-25 20:21:37 -04:00
Justin Brewer
58e6b87f51 Remove redundant NULL checks
free(NULL) is a valid NOP. Most of the hiredis free functions behave the
same way. redisReaderFree is updated to also be NULL-safe.

There is one redundant NULL check at sds.c:1036, but it's left as is
since sds is imported from upstream.

Signed-off-by: Justin Brewer <jzb0012@auburn.edu>
2018-04-30 21:45:13 -05:00
Justin Brewer
546d9415e1 Fix a segfault on *BSD
freeaddrinfo is not required by POSIX to be NULL-safe. OpenBSD will
SIGSEGV. NetBSD will assert. FreeBSD up to 11.1 will SIGSEGV, while in
future versions, it will be a silent NOP [1].

Commit d4b715f0aa ("Fix potential race in 'invalid timeout' tests")
added a code path to _redisContextConnectTcp which calls
freeaddrinfo(NULL), triggering the segfault. Put a NULL check around the
call to freeaddrinfo.

[1] - e916723903

Signed-off-by: Justin Brewer <jzb0012@auburn.edu>
2018-04-30 21:45:13 -05:00
Justin Brewer
d1c1b668c1 Drop __redis_strerror_r
Since _GNU_SOURCE is now guaranteed to be unset, it is no longer
necessary to support the GNU-specific version of strerror_r.

Drop __redis_strerror_r from the header, and call strerror_r directly.
This breaks any external users of this macro, but they shouldn't have
been using it in  the first place.

Signed-off-by: Justin Brewer <jzb0012@auburn.edu>
2018-04-30 21:45:13 -05:00
Justin Brewer
49bbaacc79 Strip down fmacros.h
strerror_r and addrinfo require _POSIX_C_SOURCE >= 200112L,  which is
implied by _XOPEN_SOURCE >= 600. With the removal of AF_LOCAL usage,
the only non-standard features being used are the TCP_KEEP* socket
flags. _DARWIN_C_SOURCE is required to expose TCP_KEEPALIVE.

Fall back to using _XOPEN_SOURCE 600 for all platforms, and
additionally define _DARWIN_C_SOURCE for Darwin.

Signed-off-by: Justin Brewer <jzb0012@auburn.edu>
2018-04-28 16:35:40 -05:00
Justin Brewer
bbeab80090 Use AF_UNIX
AF_LOCAL is the old, non-standardized name for AF_UNIX. Just use
AF_UNIX, rather than wrestling with platform specifics of AF_LOCAL
definitions.

Signed-off-by: Justin Brewer <jzb0012@auburn.edu>
2018-04-28 16:35:38 -05:00
not-a-robot[bot]
97d611e9b5 Merge #533
533: Small fixes r=badboy
2017-07-15 14:50:49 +00:00
amallia
3c32344a41 Small fixes 2017-06-15 20:59:37 +01:00
Frederik Deweerdt
e21c9c6729 Fix leak if setsockopt fails 2017-05-15 09:14:12 -07:00
Jin Qing
25cd884f6b Fix __redisSetErrorFromErrno() can not get error string.
snprintf() may change errno.
2016-12-30 23:13:02 +08:00
WSL
64d1ec8342 fix: should close socket fd when retry connet remote (tcp) 2016-07-07 17:02:06 +08:00
Tom Lee
d4b715f0aa Fix potential race in 'invalid timeout' tests
It's possible for the call to connect() to succeed on the very first
try, in which case the logic for checking for invalid timeout fields is
never executed. When this happens, the tests fail because they expect a
REDIS_ERR_IO but no such failure has occurred.

Tests aside, this is a potential source of irritating and hard-to-find
intermittent bugs.

This patch forces the validation to occur early so that we get
predictable behavior whenever an invalid timeout is specified.
2015-11-18 00:36:29 -08:00
Alex Balashov
d132d676e9 Renamed redisContext struct member 'unix' to 'unix_sock' to avoid encountering defined constant 'unix' in GNU C environment (see commit d8145d79ce).
Not all code using hiredis can compile using '-std=c99', and/or not all users are able to easily make that change to the build process of various open-source projects, so it is more pragmatic to choose a different identifier that does not impose this requirement.
2015-04-30 15:01:31 -04:00
Jan-Erik Rediger
af598dbce5 Change copyright date and add copyright holder 2015-04-16 21:29:41 +02:00
Jan-Erik Rediger
d9e0b0f6ab Implement a reconnect method for the client context
Originally implemented by @abedra as part of #306.

In case a write or read times out, we force an error state, because we
can't guarantuee that the next read will get the right data.
Instead we need to reconnect to have a clean-state connection, which is
now easily possible with this method.
2015-04-16 21:00:30 +02:00
Matt Stancliff
cc20232406 Fix errno error buffers to not clobber errors
The strerror_r API has two flavors depending on system options.

The bad flavor uses a static buffer for returning results, so if
you save the pointer from strerror_r, the string you're referencing
becomes useless if anybody else calls strerror_r again

The good flavor does what you expect: it writes the error to your buffer.

This commit uses strerror_r directly if it's a good version or copies
the static buffer into our private buffer if it's a bad version.

Thanks to gemorin for explaining the problem and drafting a fix.

Fixes #239
2015-01-05 16:53:23 -05:00
Chris Lamb
85c6bfb02a Fix build under kfreebsd
Signed-off-by: Chris Lamb <chris@chris-lamb.co.uk>

[Instead of checking for "not solaris" we feature detect
for availability of what we want, then remove the system
that advertises compatability but doesn't actually provide it
(given our assumptions about what we're guarding).]

Closes #254
2015-01-05 16:39:30 -05:00
mike
7c4d2557c4 Add support for SO_REUSEADDR
[This introduces some new API functions.]

* Adds new flag to the connection context indicating SO_REUSEADDR
  should be set.
* Adds max number of retries constant for when connect() hits
  EADDRNOTAVAIL.
* Adds new function, redisAsyncConnectBindWithReuse(), letting
  clients enable this functionality.

[Removed trailing whitespace in new header lines.]

Closes #264
2015-01-05 16:39:30 -05:00
Matt Stancliff
537a8845d1 Fix getaddrinfo() memory leak
See antirez/redis#2012 for the leak causing unbounded memory growth.
2014-09-18 14:51:27 -04:00
Pieter Noordhuis
8332a88393 File descriptors can be 0 2014-04-09 22:56:16 -07:00
Pieter Noordhuis
a9c21e4d48 Fix const correctness 2014-04-09 22:30:04 -07:00
Matt Stancliff
4369ee5d08 Fix build under Solaris
Solaris doesn't define the TCP options we try to set.  Let's
ignore those under Solaris.

Closes #207
2014-04-09 17:02:43 -04:00
Matt Stancliff
37d25a392c Add ability to bind source address on connect
Some environments require binding to specific source addresses instead
of letting the system determine which IP a connection should originate
from.

Closes #233
2014-04-08 19:37:54 -04:00
Matt Stancliff
e35266e68f Stop redisCheckSocketError from more than checking
redisCheckSocketError should only CheckSocketError and not
close any misbehaving sockets.  If CheckSocketError discovers
a problem, the caller will discover the contest is in ERR
and will start destroying the context (which involves
finalizing all callbacks (which may still be using
fd for something, so we should not close fd until all
callbacks have been told we are failing) and eventually
close the fd in redisFree() immediately before the
context is released).
2014-04-08 19:37:45 -04:00
Matt Stancliff
7f0c7a29fd Remove possiblity of multiple close on same fd
With all the async connects and disconnects and error handling
going on in hiredis, we need to centralize how we close our fd
and set it so it doesn't get re-closed.  Prior to this commit,
sometimes we'd close(fd), run an async error handler, then
call close(fd) again.

To stop multiple closes, we now set fd to -1 after we free it,
but that requires not passing fd as an independent argument to
functions.

This commit moves all fd usage to c->fd.  Since the context
has a fd field and all functions receive the context, it makes
more sense to use the fd inside of c instead of passing along fd
as an independent argument.

Also, by only using c->fd, we can set c->fd after we close it to
signify we shouldn't re-close the same fd again.

This does change one semi-public interface function redisCheckSocketError()
to only take (context) instead of (context, fd).  A search on github
returned zero occasions of people using redisCheckSocketError()
outside of net.{c,h} in hiredis itself.

Commit inspired by the bug report at:
https://groups.google.com/forum/#!topic/redis-db/mQm46XkIPOY

Thanks go out to Thijs for trying high-frequency reconnects on
a host that isn't there.

Closes #230
2014-04-08 19:37:29 -04:00
antirez
06919b3f86 Minimal IPv6 support.
redisContextConnectTcp() is now able to use IPv6 addresses if there is
no IPv4 address found resolving the specified hostname.
2013-07-11 11:48:13 +02:00
Noah Williamsson
fbf1bb648e Mark the timeout parameter as const in various functions
The struct timeval argument in redisConnectWithTimeout(),
redisConnectUnixWithTimeout(), redisSetTimeout(),
redisContextSetTimeout(), redisContextConnectTcp()
and redisContextConnectUnix() is never modified and can
therefore be marked as const.

Signed-off-by: Noah Williamsson <noah.williamsson@gmail.com>
2013-07-10 23:34:49 -07:00
Pieter Noordhuis
f9ce2111bb Whitespace 2013-07-10 22:37:47 -07:00
Eugene Bolotin
637f54b92c Fix possible uninitialized value access due to strerror_r error 2013-05-07 18:26:05 +04:00
Pieter Noordhuis
bb3c4c17c4 Make redisKeepAlive work on OSX 2013-05-01 09:23:06 -07:00
Allen.Dou
9dff5105fc Make KeepAlive optional
Make Connection KeepAlive being optional instead of default.
2013-04-30 00:11:57 +08:00
Allen.Dou
8d5bc445e5 SetKeepAlive
Keep client alive even though no command was sent to server for a long time.
2013-04-19 11:26:43 +08:00
Aaron Bedra
fca66b9e8b Set error when invalid timeout value is given to redisConnectWithTimeout
Closes #154

This commit properly sets the error value inside of
redisContextWaitReady when an invalid sec or usec value is provided.
Tests for each case are provided to demonstrate that the issue is
properly fixed and to avoid regression.

Signed-off-by: Aaron Bedra <aaron@aaronbedra.com>
2013-03-14 21:34:21 -05:00
Mark Ellzey
f8debbfdbe Use poll() instead of select() inside redisContextWaitReady()
The current select() is limiting in instances where the fd num is > FD_SETSIZE.
Since redisContextWaitReady() only processes a single fd, select would still
fail.

For compatibility reasons I have converted select() over to poll(), eliminating
this problem.
2012-04-18 12:18:07 -07:00
Pieter Noordhuis
7bc9f54134 Merge pull request #47 from geoffgarside/addrinfo
Use getaddrinfo
2011-07-20 05:02:08 -07:00
Pieter Noordhuis
b3290392d9 Put back missing socket error check after select(2) 2011-07-10 17:51:48 +02:00
Pieter Noordhuis
e6d997a96f Extract function to check a socket for errors 2011-06-27 23:42:18 +02:00
Geoff Garside
c4ed06d90c Fix incorrect "no route to host" errors.
If getaddrinfo(3) includes an AF_INET6 address before an AF_INET
address on a host with only IPv4 network connectivity then the
redisContextConnectTcp call would fail with "no route to host".

This commit fixes this issue by specifically handling the errno
EHOSTUNREACH error and entering another iteration of the addrinfo
loop. This will allow following AF_INET addresses to be attempted.
2011-06-18 14:08:25 +01:00
Geoff Garside
3afe2585de Use getaddrinfo(3) in redisContextConnectTcp.
Change redisContextConnectTcp() function to use getaddrinfo(3) to
perform address resolution, socket creation and connection. Resolved
addresses are limited to those reachable by the AF_INET family.
2011-06-17 19:26:46 +01:00