forked from droe/sslsplit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.c
202 lines (165 loc) · 4.04 KB
/
redis.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "redis.h"
#include <hiredis/hiredis.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
typedef struct redis_conn_ctx {
pthread_mutex_t mutex;
char* server;
unsigned short port;
char* password;
redisContext* ctx;
int is_connected;
} redis_conn_ctx_t;
static redis_conn_ctx_t* redis_ctx = NULL;
static void redis_ctx_free(redis_conn_ctx_t* ctx)
{
pthread_mutex_lock(&ctx->mutex);
if(ctx->server) {
free(ctx->server);
ctx->server = NULL;
}
if(ctx->password) {
free(ctx->password);
ctx->password = NULL;
}
if(ctx->ctx) {
redisFree(ctx->ctx);
ctx->ctx = NULL;
}
pthread_mutex_unlock(&ctx->mutex);
pthread_mutex_destroy(&ctx->mutex);
free(ctx);
}
static redis_conn_ctx_t* redis_ctx_new(const char* server, unsigned short port, const char* password)
{
redis_conn_ctx_t* ctx = malloc(sizeof(redis_conn_ctx_t));
if(!ctx) {
return NULL;
}
memset(ctx, 0, sizeof(redis_conn_ctx_t));
pthread_mutex_init(&ctx->mutex, NULL);
// TODO: check for errors
unsigned long server_len = strlen(server);
unsigned long password_len = password ? strlen(password) : 0;
ctx->server = malloc(server_len+1);
ctx->password = password_len ? malloc(password_len+1) : NULL;
if(!ctx->server || (password_len && !ctx->password)) {
redis_ctx_free(ctx);
return NULL;
}
strncpy(ctx->server, server, server_len+1);
if(password_len) {
strncpy(ctx->password, password, password_len+1);
}
ctx->port = port;
ctx->is_connected = 0;
return ctx;
}
static int redis_connect(redis_conn_ctx_t* ctx)
{
struct timeval timeout = {1, 500000};
if(ctx->is_connected) {
return 1;
}
pthread_mutex_lock(&ctx->mutex);
if(ctx->ctx) {
redisFree(ctx->ctx);
}
ctx->ctx = redisConnectWithTimeout(ctx->server, ctx->port, timeout);
if(!ctx->ctx || ctx->ctx->err) {
if(ctx->ctx) {
// Should log redis conn error
redisFree(ctx->ctx);
ctx->ctx = NULL;
} else {
// Should log redis ctx allocation failure
}
pthread_mutex_unlock(&ctx->mutex);
return 0;
}
if(ctx->password) {
redisReply* pwd_reply = redisCommand(ctx->ctx, "AUTH %s", ctx->password);
if(!pwd_reply || pwd_reply->type == REDIS_REPLY_ERROR) {
// Should log redis error
if(pwd_reply) {
freeReplyObject(pwd_reply);
}
redisFree(ctx->ctx);
ctx->ctx = NULL;
pthread_mutex_unlock(&ctx->mutex);
return 0;
}
freeReplyObject(pwd_reply);
}
ctx->is_connected = 1;
pthread_mutex_unlock(&ctx->mutex);
return 1;
}
int redis_setup(const char* server, unsigned short port, const char* password)
{
if(redis_ctx) {
redis_ctx_free(redis_ctx);
}
redis_ctx = redis_ctx_new(server, port, password);
if(!redis_ctx) {
return 0;
}
return redis_connect(redis_ctx);
}
void redis_set(const char* key, const char* value, int length, long ttl)
{
if(!redis_is_connected()) {
return;
}
pthread_mutex_lock(&redis_ctx->mutex);
redisReply* reply = NULL;
if(ttl > 0) {
reply = redisCommand(redis_ctx->ctx, "SETEX %s %d %b", key, ttl, value, length);
} else {
reply = redisCommand(redis_ctx->ctx, "SET %s %b", key, value, length);
}
if(reply) {
freeReplyObject(reply);
}
pthread_mutex_unlock(&redis_ctx->mutex);
}
int redis_get(const char* key, char* buffer, int buffer_length)
{
if(!redis_is_connected()) {
return 0;
}
pthread_mutex_lock(&redis_ctx->mutex);
int result = 0;
redisReply* reply = redisCommand(redis_ctx->ctx, "GET %s", key);
if(reply && reply->type != REDIS_REPLY_ERROR && reply->len < buffer_length) {
strncpy(reply->str, buffer, reply->len+1);
result = 1;
}
if(reply) {
freeReplyObject(reply);
}
pthread_mutex_unlock(&redis_ctx->mutex);
return result;
}
int redis_exists(const char* key)
{
if(!redis_is_connected()) {
return 0;
}
pthread_mutex_lock(&redis_ctx->mutex);
int result = 0;
redisReply* reply = redisCommand(redis_ctx->ctx, "EXISTS %s", key);
if(reply && reply->type == REDIS_REPLY_INTEGER) {
result = reply->integer;
}
if(reply) {
freeReplyObject(reply);
}
pthread_mutex_unlock(&redis_ctx->mutex);
return result;
}
int redis_is_connected(void)
{
return redis_ctx && redis_ctx->is_connected && redis_ctx->ctx;
}