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 13 44 #define HIREDIS_PATCH 3 45 #define HIREDIS_SONAME 0.13 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 /* strerror_r has two completely different prototypes and behaviors 84 * depending on system issues, so we need to operate on the error buffer 85 * differently depending on which strerror_r we're using. */ 86 #ifndef _GNU_SOURCE 87 /* "regular" POSIX strerror_r that does the right thing. */ 88 #define __redis_strerror_r(errno, buf, len) \ 89 do { \ 90 strerror_r((errno), (buf), (len)); \ 91 } while (0) 92 #else 93 /* "bad" GNU strerror_r we need to clean up after. */ 94 #define __redis_strerror_r(errno, buf, len) \ 95 do { \ 96 char *err_str = strerror_r((errno), (buf), (len)); \ 97 /* If return value _isn't_ the start of the buffer we passed in, \ 98 * then GNU strerror_r returned an internal static buffer and we \ 99 * need to copy the result into our private buffer. */ \ 100 if (err_str != (buf)) { \ 101 strncpy((buf), err_str, ((len) - 1)); \ 102 buf[(len)-1] = '\0'; \ 103 } \ 104 } while (0) 105 #endif 106 107 #ifdef __cplusplus 108 extern "C" { 109 #endif 110 111 /* This is the reply object returned by redisCommand() */ 112 typedef struct redisReply { 113 int type; /* REDIS_REPLY_* */ 114 long long integer; /* The integer when type is REDIS_REPLY_INTEGER */ 115 size_t len; /* Length of string */ 116 char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */ 117 size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */ 118 struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */ 119 } redisReply; 120 121 redisReader *redisReaderCreate(void); 122 123 /* Function to free the reply objects hiredis returns by default. */ 124 void freeReplyObject(void *reply); 125 126 /* Functions to format a command according to the protocol. */ 127 int redisvFormatCommand(char **target, const char *format, va_list ap); 128 int redisFormatCommand(char **target, const char *format, ...); 129 int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen); 130 int redisFormatSdsCommandArgv(sds *target, int argc, const char ** argv, const size_t *argvlen); 131 void redisFreeCommand(char *cmd); 132 void redisFreeSdsCommand(sds cmd); 133 134 enum redisConnectionType { 135 REDIS_CONN_TCP, 136 REDIS_CONN_UNIX 137 }; 138 139 /* Context for a connection to Redis */ 140 typedef struct redisContext { 141 int err; /* Error flags, 0 when there is no error */ 142 char errstr[128]; /* String representation of error when applicable */ 143 int fd; 144 int flags; 145 char *obuf; /* Write buffer */ 146 redisReader *reader; /* Protocol reader */ 147 148 enum redisConnectionType connection_type; 149 struct timeval *timeout; 150 151 struct { 152 char *host; 153 char *source_addr; 154 int port; 155 } tcp; 156 157 struct { 158 char *path; 159 } unix_sock; 160 161 } redisContext; 162 163 redisContext *redisConnect(const char *ip, int port); 164 redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv); 165 redisContext *redisConnectNonBlock(const char *ip, int port); 166 redisContext *redisConnectBindNonBlock(const char *ip, int port, 167 const char *source_addr); 168 redisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port, 169 const char *source_addr); 170 redisContext *redisConnectUnix(const char *path); 171 redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv); 172 redisContext *redisConnectUnixNonBlock(const char *path); 173 redisContext *redisConnectFd(int fd); 174 175 /** 176 * Reconnect the given context using the saved information. 177 * 178 * This re-uses the exact same connect options as in the initial connection. 179 * host, ip (or path), timeout and bind address are reused, 180 * flags are used unmodified from the existing context. 181 * 182 * Returns REDIS_OK on successful connect or REDIS_ERR otherwise. 183 */ 184 int redisReconnect(redisContext *c); 185 186 int redisSetTimeout(redisContext *c, const struct timeval tv); 187 int redisEnableKeepAlive(redisContext *c); 188 void redisFree(redisContext *c); 189 int redisFreeKeepFd(redisContext *c); 190 int redisBufferRead(redisContext *c); 191 int redisBufferWrite(redisContext *c, int *done); 192 193 /* In a blocking context, this function first checks if there are unconsumed 194 * replies to return and returns one if so. Otherwise, it flushes the output 195 * buffer to the socket and reads until it has a reply. In a non-blocking 196 * context, it will return unconsumed replies until there are no more. */ 197 int redisGetReply(redisContext *c, void **reply); 198 int redisGetReplyFromReader(redisContext *c, void **reply); 199 200 /* Write a formatted command to the output buffer. Use these functions in blocking mode 201 * to get a pipeline of commands. */ 202 int redisAppendFormattedCommand(redisContext *c, const char *cmd, size_t len); 203 204 /* Write a command to the output buffer. Use these functions in blocking mode 205 * to get a pipeline of commands. */ 206 int redisvAppendCommand(redisContext *c, const char *format, va_list ap); 207 int redisAppendCommand(redisContext *c, const char *format, ...); 208 int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen); 209 210 /* Issue a command to Redis. In a blocking context, it is identical to calling 211 * redisAppendCommand, followed by redisGetReply. The function will return 212 * NULL if there was an error in performing the request, otherwise it will 213 * return the reply. In a non-blocking context, it is identical to calling 214 * only redisAppendCommand and will always return NULL. */ 215 void *redisvCommand(redisContext *c, const char *format, va_list ap); 216 void *redisCommand(redisContext *c, const char *format, ...); 217 void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen); 218 219 #ifdef __cplusplus 220 } 221 #endif 222 223 #endif 224