| 277 | } |
| 278 | |
| 279 | static redisContext *getRedisContext(const char *ip, int port, |
| 280 | const char *hostsocket) |
| 281 | { |
| 282 | redisContext *ctx = NULL; |
| 283 | redisReply *reply = NULL; |
| 284 | if (hostsocket == NULL) |
| 285 | ctx = redisConnect(ip, port); |
| 286 | else |
| 287 | ctx = redisConnectUnix(hostsocket); |
| 288 | if (ctx == NULL || ctx->err) { |
| 289 | fprintf(stderr,"Could not connect to Redis at "); |
| 290 | const char *err = (ctx != NULL ? ctx->errstr : ""); |
| 291 | if (hostsocket == NULL) |
| 292 | fprintf(stderr,"%s:%d: %s\n",ip,port,err); |
| 293 | else |
| 294 | fprintf(stderr,"%s: %s\n",hostsocket,err); |
| 295 | goto cleanup; |
| 296 | } |
| 297 | if (config.tls==1) { |
| 298 | const char *err = NULL; |
| 299 | if (cliSecureConnection(ctx, config.sslconfig, &err) == REDIS_ERR && err) { |
| 300 | fprintf(stderr, "Could not negotiate a TLS connection: %s\n", err); |
| 301 | goto cleanup; |
| 302 | } |
| 303 | } |
| 304 | if (config.auth == NULL) |
| 305 | return ctx; |
| 306 | if (config.user == NULL) |
| 307 | reply = (redisReply*)redisCommand(ctx,"AUTH %s", config.auth); |
| 308 | else |
| 309 | reply = (redisReply*)redisCommand(ctx,"AUTH %s %s", config.user, config.auth); |
| 310 | if (reply != NULL) { |
| 311 | if (reply->type == REDIS_REPLY_ERROR) { |
| 312 | if (hostsocket == NULL) |
| 313 | fprintf(stderr, "Node %s:%d replied with error:\n%s\n", ip, port, reply->str); |
| 314 | else |
| 315 | fprintf(stderr, "Node %s replied with error:\n%s\n", hostsocket, reply->str); |
| 316 | freeReplyObject(reply); |
| 317 | redisFree(ctx); |
| 318 | exit(1); |
| 319 | } |
| 320 | freeReplyObject(reply); |
| 321 | return ctx; |
| 322 | } |
| 323 | fprintf(stderr, "ERROR: failed to fetch reply from "); |
| 324 | if (hostsocket == NULL) |
| 325 | fprintf(stderr, "%s:%d\n", ip, port); |
| 326 | else |
| 327 | fprintf(stderr, "%s\n", hostsocket); |
| 328 | cleanup: |
| 329 | freeReplyObject(reply); |
| 330 | redisFree(ctx); |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | static redisConfig *getRedisConfig(const char *ip, int port, |
| 335 | const char *hostsocket) |
no test coverage detected