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 | #ifndef _MSC_VER |
39 | #include <sys/time.h> /* for struct timeval */ |
40 | #else |
41 | struct timeval; /* forward declaration */ |
42 | typedef long long ssize_t; |
43 | #endif |
44 | #include <stdint.h> /* uintXX_t, etc */ |
45 | #include "sds.h" /* for hisds */ |
46 | #include "alloc.h" /* for allocation wrappers */ |
47 | |
48 | #define HIREDIS_MAJOR 1 |
49 | #define HIREDIS_MINOR 0 |
50 | #define HIREDIS_PATCH 3 |
51 | #define HIREDIS_SONAME 1.0.3-dev |
52 | |
53 | /* Connection type can be blocking or non-blocking and is set in the |
54 | * least significant bit of the flags field in redisContext. */ |
55 | #define REDIS_BLOCK 0x1 |
56 | |
57 | /* Connection may be disconnected before being free'd. The second bit |
58 | * in the flags field is set when the context is connected. */ |
59 | #define REDIS_CONNECTED 0x2 |
60 | |
61 | /* The async API might try to disconnect cleanly and flush the output |
62 | * buffer and read all subsequent replies before disconnecting. |
63 | * This flag means no new commands can come in and the connection |
64 | * should be terminated once all replies have been read. */ |
65 | #define REDIS_DISCONNECTING 0x4 |
66 | |
67 | /* Flag specific to the async API which means that the context should be clean |
68 | * up as soon as possible. */ |
69 | #define REDIS_FREEING 0x8 |
70 | |
71 | /* Flag that is set when an async callback is executed. */ |
72 | #define REDIS_IN_CALLBACK 0x10 |
73 | |
74 | /* Flag that is set when the async context has one or more subscriptions. */ |
75 | #define REDIS_SUBSCRIBED 0x20 |
76 | |
77 | /* Flag that is set when monitor mode is active */ |
78 | #define REDIS_MONITORING 0x40 |
79 | |
80 | /* Flag that is set when we should set SO_REUSEADDR before calling bind() */ |
81 | #define REDIS_REUSEADDR 0x80 |
82 | |
83 | /* Flag that is set when the async connection supports push replies. */ |
84 | #define REDIS_SUPPORTS_PUSH 0x100 |
85 | |
86 | /** |
87 | * Flag that indicates the user does not want the context to |
88 | * be automatically freed upon error |
89 | */ |
90 | #define REDIS_NO_AUTO_FREE 0x200 |
91 | |
92 | /* Flag that indicates the user does not want replies to be automatically freed */ |
93 | #define REDIS_NO_AUTO_FREE_REPLIES 0x400 |
94 | |
95 | #define REDIS_KEEPALIVE_INTERVAL 15 /* seconds */ |
96 | |
97 | /* number of times we retry to connect in the case of EADDRNOTAVAIL and |
98 | * SO_REUSEADDR is being used. */ |
99 | #define REDIS_CONNECT_RETRIES 10 |
100 | |
101 | /* Forward declarations for structs defined elsewhere */ |
102 | struct redisAsyncContext; |
103 | struct redisContext; |
104 | |
105 | /* RESP3 push helpers and callback prototypes */ |
106 | #define redisIsPushReply(r) (((redisReply*)(r))->type == REDIS_REPLY_PUSH) |
107 | typedef void (redisPushFn)(void *, void *); |
108 | typedef void (redisAsyncPushFn)(struct redisAsyncContext *, void *); |
109 | |
110 | #ifdef __cplusplus |
111 | extern "C" { |
112 | #endif |
113 | |
114 | /* This is the reply object returned by redisCommand() */ |
115 | typedef struct redisReply { |
116 | int type; /* REDIS_REPLY_* */ |
117 | long long integer; /* The integer when type is REDIS_REPLY_INTEGER */ |
118 | double dval; /* The double when type is REDIS_REPLY_DOUBLE */ |
119 | size_t len; /* Length of string */ |
120 | char *str; /* Used for REDIS_REPLY_ERROR, REDIS_REPLY_STRING |
121 | REDIS_REPLY_VERB, REDIS_REPLY_DOUBLE (in additional to dval), |
122 | and REDIS_REPLY_BIGNUM. */ |
123 | char vtype[4]; /* Used for REDIS_REPLY_VERB, contains the null |
124 | terminated 3 character content type, such as "txt". */ |
125 | size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */ |
126 | struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */ |
127 | } redisReply; |
128 | |
129 | redisReader *redisReaderCreate(void); |
130 | |
131 | /* Function to free the reply objects hiredis returns by default. */ |
132 | void freeReplyObject(void *reply); |
133 | |
134 | /* Functions to format a command according to the protocol. */ |
135 | int redisvFormatCommand(char **target, const char *format, va_list ap); |
136 | int redisFormatCommand(char **target, const char *format, ...); |
137 | long long redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen); |
138 | long long redisFormatSdsCommandArgv(hisds *target, int argc, const char ** argv, const size_t *argvlen); |
139 | void redisFreeCommand(char *cmd); |
140 | void redisFreeSdsCommand(hisds cmd); |
141 | |
142 | enum redisConnectionType { |
143 | REDIS_CONN_TCP, |
144 | REDIS_CONN_UNIX, |
145 | REDIS_CONN_USERFD |
146 | }; |
147 | |
148 | struct redisSsl; |
149 | |
150 | #define REDIS_OPT_NONBLOCK 0x01 |
151 | #define REDIS_OPT_REUSEADDR 0x02 |
152 | |
153 | /** |
154 | * Don't automatically free the async object on a connection failure, |
155 | * or other implicit conditions. Only free on an explicit call to disconnect() or free() |
156 | */ |
157 | #define REDIS_OPT_NOAUTOFREE 0x04 |
158 | |
159 | /* Don't automatically intercept and free RESP3 PUSH replies. */ |
160 | #define REDIS_OPT_NO_PUSH_AUTOFREE 0x08 |
161 | |
162 | /** |
163 | * Don't automatically free replies |
164 | */ |
165 | #define REDIS_OPT_NOAUTOFREEREPLIES 0x10 |
166 | |
167 | /* In Unix systems a file descriptor is a regular signed int, with -1 |
168 | * representing an invalid descriptor. In Windows it is a SOCKET |
169 | * (32- or 64-bit unsigned integer depending on the architecture), where |
170 | * all bits set (~0) is INVALID_SOCKET. */ |
171 | #ifndef _WIN32 |
172 | typedef int redisFD; |
173 | #define REDIS_INVALID_FD -1 |
174 | #else |
175 | #ifdef _WIN64 |
176 | typedef unsigned long long redisFD; /* SOCKET = 64-bit UINT_PTR */ |
177 | #else |
178 | typedef unsigned long redisFD; /* SOCKET = 32-bit UINT_PTR */ |
179 | #endif |
180 | #define REDIS_INVALID_FD ((redisFD)(~0)) /* INVALID_SOCKET */ |
181 | #endif |
182 | |
183 | typedef struct { |
184 | /* |
185 | * the type of connection to use. This also indicates which |
186 | * `endpoint` member field to use |
187 | */ |
188 | int type; |
189 | /* bit field of REDIS_OPT_xxx */ |
190 | int options; |
191 | /* timeout value for connect operation. If NULL, no timeout is used */ |
192 | const struct timeval *connect_timeout; |
193 | /* timeout value for commands. If NULL, no timeout is used. This can be |
194 | * updated at runtime with redisSetTimeout/redisAsyncSetTimeout. */ |
195 | const struct timeval *command_timeout; |
196 | union { |
197 | /** use this field for tcp/ip connections */ |
198 | struct { |
199 | const char *source_addr; |
200 | const char *ip; |
201 | int port; |
202 | } tcp; |
203 | /** use this field for unix domain sockets */ |
204 | const char *unix_socket; |
205 | /** |
206 | * use this field to have hiredis operate an already-open |
207 | * file descriptor */ |
208 | redisFD fd; |
209 | } endpoint; |
210 | |
211 | /* Optional user defined data/destructor */ |
212 | void *privdata; |
213 | void (*free_privdata)(void *); |
214 | |
215 | /* A user defined PUSH message callback */ |
216 | redisPushFn *push_cb; |
217 | redisAsyncPushFn *async_push_cb; |
218 | } redisOptions; |
219 | |
220 | /** |
221 | * Helper macros to initialize options to their specified fields. |
222 | */ |
223 | #define REDIS_OPTIONS_SET_TCP(opts, ip_, port_) \ |
224 | (opts)->type = REDIS_CONN_TCP; \ |
225 | (opts)->endpoint.tcp.ip = ip_; \ |
226 | (opts)->endpoint.tcp.port = port_; |
227 | |
228 | #define REDIS_OPTIONS_SET_UNIX(opts, path) \ |
229 | (opts)->type = REDIS_CONN_UNIX; \ |
230 | (opts)->endpoint.unix_socket = path; |
231 | |
232 | #define REDIS_OPTIONS_SET_PRIVDATA(opts, data, dtor) \ |
233 | (opts)->privdata = data; \ |
234 | (opts)->free_privdata = dtor; \ |
235 | |
236 | typedef struct redisContextFuncs { |
237 | void (*free_privctx)(void *); |
238 | void (*async_read)(struct redisAsyncContext *); |
239 | void (*async_write)(struct redisAsyncContext *); |
240 | ssize_t (*read)(struct redisContext *, char *, size_t); |
241 | ssize_t (*write)(struct redisContext *); |
242 | } redisContextFuncs; |
243 | |
244 | /* Context for a connection to Redis */ |
245 | typedef struct redisContext { |
246 | const redisContextFuncs *funcs; /* Function table */ |
247 | |
248 | int err; /* Error flags, 0 when there is no error */ |
249 | char errstr[128]; /* String representation of error when applicable */ |
250 | redisFD fd; |
251 | int flags; |
252 | char *obuf; /* Write buffer */ |
253 | redisReader *reader; /* Protocol reader */ |
254 | |
255 | enum redisConnectionType connection_type; |
256 | struct timeval *connect_timeout; |
257 | struct timeval *command_timeout; |
258 | |
259 | struct { |
260 | char *host; |
261 | char *source_addr; |
262 | int port; |
263 | } tcp; |
264 | |
265 | struct { |
266 | char *path; |
267 | } unix_sock; |
268 | |
269 | /* For non-blocking connect */ |
270 | struct sockaddr *saddr; |
271 | size_t addrlen; |
272 | |
273 | /* Optional data and corresponding destructor users can use to provide |
274 | * context to a given redisContext. Not used by hiredis. */ |
275 | void *privdata; |
276 | void (*free_privdata)(void *); |
277 | |
278 | /* Internal context pointer presently used by hiredis to manage |
279 | * SSL connections. */ |
280 | void *privctx; |
281 | |
282 | /* An optional RESP3 PUSH handler */ |
283 | redisPushFn *push_cb; |
284 | } redisContext; |
285 | |
286 | redisContext *redisConnectWithOptions(const redisOptions *options); |
287 | redisContext *redisConnect(const char *ip, int port); |
288 | redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv); |
289 | redisContext *redisConnectNonBlock(const char *ip, int port); |
290 | redisContext *redisConnectBindNonBlock(const char *ip, int port, |
291 | const char *source_addr); |
292 | redisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port, |
293 | const char *source_addr); |
294 | redisContext *redisConnectUnix(const char *path); |
295 | redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv); |
296 | redisContext *redisConnectUnixNonBlock(const char *path); |
297 | redisContext *redisConnectFd(redisFD fd); |
298 | |
299 | /** |
300 | * Reconnect the given context using the saved information. |
301 | * |
302 | * This re-uses the exact same connect options as in the initial connection. |
303 | * host, ip (or path), timeout and bind address are reused, |
304 | * flags are used unmodified from the existing context. |
305 | * |
306 | * Returns REDIS_OK on successful connect or REDIS_ERR otherwise. |
307 | */ |
308 | int redisReconnect(redisContext *c); |
309 | |
310 | redisPushFn *redisSetPushCallback(redisContext *c, redisPushFn *fn); |
311 | int redisSetTimeout(redisContext *c, const struct timeval tv); |
312 | int redisEnableKeepAlive(redisContext *c); |
313 | void redisFree(redisContext *c); |
314 | redisFD redisFreeKeepFd(redisContext *c); |
315 | int redisBufferRead(redisContext *c); |
316 | int redisBufferWrite(redisContext *c, int *done); |
317 | |
318 | /* In a blocking context, this function first checks if there are unconsumed |
319 | * replies to return and returns one if so. Otherwise, it flushes the output |
320 | * buffer to the socket and reads until it has a reply. In a non-blocking |
321 | * context, it will return unconsumed replies until there are no more. */ |
322 | int redisGetReply(redisContext *c, void **reply); |
323 | int redisGetReplyFromReader(redisContext *c, void **reply); |
324 | |
325 | /* Write a formatted command to the output buffer. Use these functions in blocking mode |
326 | * to get a pipeline of commands. */ |
327 | int redisAppendFormattedCommand(redisContext *c, const char *cmd, size_t len); |
328 | |
329 | /* Write a command to the output buffer. Use these functions in blocking mode |
330 | * to get a pipeline of commands. */ |
331 | int redisvAppendCommand(redisContext *c, const char *format, va_list ap); |
332 | int redisAppendCommand(redisContext *c, const char *format, ...); |
333 | int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen); |
334 | |
335 | /* Issue a command to Redis. In a blocking context, it is identical to calling |
336 | * redisAppendCommand, followed by redisGetReply. The function will return |
337 | * NULL if there was an error in performing the request, otherwise it will |
338 | * return the reply. In a non-blocking context, it is identical to calling |
339 | * only redisAppendCommand and will always return NULL. */ |
340 | void *redisvCommand(redisContext *c, const char *format, va_list ap); |
341 | void *redisCommand(redisContext *c, const char *format, ...); |
342 | void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen); |
343 | |
344 | #ifdef __cplusplus |
345 | } |
346 | #endif |
347 | |
348 | #endif |
349 | |