1 /*
2  * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>
3  * Copyright (c) 2010-2014, Pieter Noordhuis <pcnoordhuis at gmail dot com>
4  * Copyright (c) 2015, Matt Stancliff <matt at genges dot com>,
5  *                     Jan-Erik Rediger <janerik at fnordig dot com>
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  *   * Redistributions of source code must retain the above copyright notice,
13  *     this list of conditions and the following disclaimer.
14  *   * Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *   * Neither the name of Redis nor the names of its contributors may be used
18  *     to endorse or promote products derived from this software without
19  *     specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef __HIREDIS_H
35 #define __HIREDIS_H
36 #include "read.h"
37 #include <stdarg.h> /* for va_list */
38 #include <sys/time.h> /* for struct timeval */
39 #include <stdint.h> /* uintXX_t, etc */
40 #include "sds.h" /* for sds */
41 
42 #define HIREDIS_MAJOR 0
43 #define HIREDIS_MINOR 14
44 #define HIREDIS_PATCH 0
45 #define HIREDIS_SONAME 0.14
46 
47 /* Connection type can be blocking or non-blocking and is set in the
48  * least significant bit of the flags field in redisContext. */
49 #define REDIS_BLOCK 0x1
50 
51 /* Connection may be disconnected before being free'd. The second bit
52  * in the flags field is set when the context is connected. */
53 #define REDIS_CONNECTED 0x2
54 
55 /* The async API might try to disconnect cleanly and flush the output
56  * buffer and read all subsequent replies before disconnecting.
57  * This flag means no new commands can come in and the connection
58  * should be terminated once all replies have been read. */
59 #define REDIS_DISCONNECTING 0x4
60 
61 /* Flag specific to the async API which means that the context should be clean
62  * up as soon as possible. */
63 #define REDIS_FREEING 0x8
64 
65 /* Flag that is set when an async callback is executed. */
66 #define REDIS_IN_CALLBACK 0x10
67 
68 /* Flag that is set when the async context has one or more subscriptions. */
69 #define REDIS_SUBSCRIBED 0x20
70 
71 /* Flag that is set when monitor mode is active */
72 #define REDIS_MONITORING 0x40
73 
74 /* Flag that is set when we should set SO_REUSEADDR before calling bind() */
75 #define REDIS_REUSEADDR 0x80
76 
77 #define REDIS_KEEPALIVE_INTERVAL 15 /* seconds */
78 
79 /* number of times we retry to connect in the case of EADDRNOTAVAIL and
80  * SO_REUSEADDR is being used. */
81 #define REDIS_CONNECT_RETRIES  10
82 
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86 
87 /* This is the reply object returned by redisCommand() */
88 typedef struct redisReply {
89     int type; /* REDIS_REPLY_* */
90     long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
91     size_t len; /* Length of string */
92     char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
93     size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
94     struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
95 } redisReply;
96 
97 redisReader *redisReaderCreate(void);
98 
99 /* Function to free the reply objects hiredis returns by default. */
100 void freeReplyObject(void *reply);
101 
102 /* Functions to format a command according to the protocol. */
103 int redisvFormatCommand(char **target, const char *format, va_list ap);
104 int redisFormatCommand(char **target, const char *format, ...);
105 int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen);
106 int redisFormatSdsCommandArgv(sds *target, int argc, const char ** argv, const size_t *argvlen);
107 void redisFreeCommand(char *cmd);
108 void redisFreeSdsCommand(sds cmd);
109 
110 enum redisConnectionType {
111     REDIS_CONN_TCP,
112     REDIS_CONN_UNIX
113 };
114 
115 /* Context for a connection to Redis */
116 typedef struct redisContext {
117     int err; /* Error flags, 0 when there is no error */
118     char errstr[128]; /* String representation of error when applicable */
119     int fd;
120     int flags;
121     char *obuf; /* Write buffer */
122     redisReader *reader; /* Protocol reader */
123 
124     enum redisConnectionType connection_type;
125     struct timeval *timeout;
126 
127     struct {
128         char *host;
129         char *source_addr;
130         int port;
131     } tcp;
132 
133     struct {
134         char *path;
135     } unix_sock;
136 
137 } redisContext;
138 
139 redisContext *redisConnect(const char *ip, int port);
140 redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv);
141 redisContext *redisConnectNonBlock(const char *ip, int port);
142 redisContext *redisConnectBindNonBlock(const char *ip, int port,
143                                        const char *source_addr);
144 redisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port,
145                                                 const char *source_addr);
146 redisContext *redisConnectUnix(const char *path);
147 redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv);
148 redisContext *redisConnectUnixNonBlock(const char *path);
149 redisContext *redisConnectFd(int fd);
150 
151 /**
152  * Reconnect the given context using the saved information.
153  *
154  * This re-uses the exact same connect options as in the initial connection.
155  * host, ip (or path), timeout and bind address are reused,
156  * flags are used unmodified from the existing context.
157  *
158  * Returns REDIS_OK on successful connect or REDIS_ERR otherwise.
159  */
160 int redisReconnect(redisContext *c);
161 
162 int redisSetTimeout(redisContext *c, const struct timeval tv);
163 int redisEnableKeepAlive(redisContext *c);
164 void redisFree(redisContext *c);
165 int redisFreeKeepFd(redisContext *c);
166 int redisBufferRead(redisContext *c);
167 int redisBufferWrite(redisContext *c, int *done);
168 
169 /* In a blocking context, this function first checks if there are unconsumed
170  * replies to return and returns one if so. Otherwise, it flushes the output
171  * buffer to the socket and reads until it has a reply. In a non-blocking
172  * context, it will return unconsumed replies until there are no more. */
173 int redisGetReply(redisContext *c, void **reply);
174 int redisGetReplyFromReader(redisContext *c, void **reply);
175 
176 /* Write a formatted command to the output buffer. Use these functions in blocking mode
177  * to get a pipeline of commands. */
178 int redisAppendFormattedCommand(redisContext *c, const char *cmd, size_t len);
179 
180 /* Write a command to the output buffer. Use these functions in blocking mode
181  * to get a pipeline of commands. */
182 int redisvAppendCommand(redisContext *c, const char *format, va_list ap);
183 int redisAppendCommand(redisContext *c, const char *format, ...);
184 int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
185 
186 /* Issue a command to Redis. In a blocking context, it is identical to calling
187  * redisAppendCommand, followed by redisGetReply. The function will return
188  * NULL if there was an error in performing the request, otherwise it will
189  * return the reply. In a non-blocking context, it is identical to calling
190  * only redisAppendCommand and will always return NULL. */
191 void *redisvCommand(redisContext *c, const char *format, va_list ap);
192 void *redisCommand(redisContext *c, const char *format, ...);
193 void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
194 
195 #ifdef __cplusplus
196 }
197 #endif
198 
199 #endif
200