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 #include "alloc.h" /* for allocation wrappers */
42 
43 #define HIREDIS_MAJOR 0
44 #define HIREDIS_MINOR 14
45 #define HIREDIS_PATCH 1
46 #define HIREDIS_SONAME 0.14
47 
48 /* Connection type can be blocking or non-blocking and is set in the
49  * least significant bit of the flags field in redisContext. */
50 #define REDIS_BLOCK 0x1
51 
52 /* Connection may be disconnected before being free'd. The second bit
53  * in the flags field is set when the context is connected. */
54 #define REDIS_CONNECTED 0x2
55 
56 /* The async API might try to disconnect cleanly and flush the output
57  * buffer and read all subsequent replies before disconnecting.
58  * This flag means no new commands can come in and the connection
59  * should be terminated once all replies have been read. */
60 #define REDIS_DISCONNECTING 0x4
61 
62 /* Flag specific to the async API which means that the context should be clean
63  * up as soon as possible. */
64 #define REDIS_FREEING 0x8
65 
66 /* Flag that is set when an async callback is executed. */
67 #define REDIS_IN_CALLBACK 0x10
68 
69 /* Flag that is set when the async context has one or more subscriptions. */
70 #define REDIS_SUBSCRIBED 0x20
71 
72 /* Flag that is set when monitor mode is active */
73 #define REDIS_MONITORING 0x40
74 
75 /* Flag that is set when we should set SO_REUSEADDR before calling bind() */
76 #define REDIS_REUSEADDR 0x80
77 
78 #define REDIS_KEEPALIVE_INTERVAL 15 /* seconds */
79 
80 /* number of times we retry to connect in the case of EADDRNOTAVAIL and
81  * SO_REUSEADDR is being used. */
82 #define REDIS_CONNECT_RETRIES  10
83 
84 #ifdef __cplusplus
85 extern "C" {
86 #endif
87 
88 /* This is the reply object returned by redisCommand() */
89 typedef struct redisReply {
90     int type; /* REDIS_REPLY_* */
91     long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
92     size_t len; /* Length of string */
93     char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
94     size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
95     struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
96 } redisReply;
97 
98 redisReader *redisReaderCreate(void);
99 
100 /* Function to free the reply objects hiredis returns by default. */
101 void freeReplyObject(void *reply);
102 
103 /* Functions to format a command according to the protocol. */
104 int redisvFormatCommand(char **target, const char *format, va_list ap);
105 int redisFormatCommand(char **target, const char *format, ...);
106 int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen);
107 int redisFormatSdsCommandArgv(sds *target, int argc, const char ** argv, const size_t *argvlen);
108 void redisFreeCommand(char *cmd);
109 void redisFreeSdsCommand(sds cmd);
110 
111 enum redisConnectionType {
112     REDIS_CONN_TCP,
113     REDIS_CONN_UNIX
114 };
115 
116 /* Context for a connection to Redis */
117 typedef struct redisContext {
118     int err; /* Error flags, 0 when there is no error */
119     char errstr[128]; /* String representation of error when applicable */
120     int fd;
121     int flags;
122     char *obuf; /* Write buffer */
123     redisReader *reader; /* Protocol reader */
124 
125     enum redisConnectionType connection_type;
126     struct timeval *timeout;
127 
128     struct {
129         char *host;
130         char *source_addr;
131         int port;
132     } tcp;
133 
134     struct {
135         char *path;
136     } unix_sock;
137 
138 } redisContext;
139 
140 redisContext *redisConnect(const char *ip, int port);
141 redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv);
142 redisContext *redisConnectNonBlock(const char *ip, int port);
143 redisContext *redisConnectBindNonBlock(const char *ip, int port,
144                                        const char *source_addr);
145 redisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port,
146                                                 const char *source_addr);
147 redisContext *redisConnectUnix(const char *path);
148 redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv);
149 redisContext *redisConnectUnixNonBlock(const char *path);
150 redisContext *redisConnectFd(int fd);
151 
152 /**
153  * Reconnect the given context using the saved information.
154  *
155  * This re-uses the exact same connect options as in the initial connection.
156  * host, ip (or path), timeout and bind address are reused,
157  * flags are used unmodified from the existing context.
158  *
159  * Returns REDIS_OK on successful connect or REDIS_ERR otherwise.
160  */
161 int redisReconnect(redisContext *c);
162 
163 int redisSetTimeout(redisContext *c, const struct timeval tv);
164 int redisEnableKeepAlive(redisContext *c);
165 void redisFree(redisContext *c);
166 int redisFreeKeepFd(redisContext *c);
167 int redisBufferRead(redisContext *c);
168 int redisBufferWrite(redisContext *c, int *done);
169 
170 /* In a blocking context, this function first checks if there are unconsumed
171  * replies to return and returns one if so. Otherwise, it flushes the output
172  * buffer to the socket and reads until it has a reply. In a non-blocking
173  * context, it will return unconsumed replies until there are no more. */
174 int redisGetReply(redisContext *c, void **reply);
175 int redisGetReplyFromReader(redisContext *c, void **reply);
176 
177 /* Write a formatted command to the output buffer. Use these functions in blocking mode
178  * to get a pipeline of commands. */
179 int redisAppendFormattedCommand(redisContext *c, const char *cmd, size_t len);
180 
181 /* Write a command to the output buffer. Use these functions in blocking mode
182  * to get a pipeline of commands. */
183 int redisvAppendCommand(redisContext *c, const char *format, va_list ap);
184 int redisAppendCommand(redisContext *c, const char *format, ...);
185 int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
186 
187 /* Issue a command to Redis. In a blocking context, it is identical to calling
188  * redisAppendCommand, followed by redisGetReply. The function will return
189  * NULL if there was an error in performing the request, otherwise it will
190  * return the reply. In a non-blocking context, it is identical to calling
191  * only redisAppendCommand and will always return NULL. */
192 void *redisvCommand(redisContext *c, const char *format, va_list ap);
193 void *redisCommand(redisContext *c, const char *format, ...);
194 void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
195 
196 #ifdef __cplusplus
197 }
198 #endif
199 
200 #endif
201