I'm trying to run hiredis
in C
on CentOS
.
The following code seems to run fine:
...
const char *hostname = "my.redis-as-a-service.com";
int port = 8765;
const char *cert = "-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----\n";
const char *key = "-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----\n";
const char *ca = "-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----\n";
struct timeval tv = { 1, 500000 }; // 1.5 seconds
redisOptions options = {0};
REDIS_OPTIONS_SET_TCP(&options, hostname, port);
options.timeout = &tv;
c = redisConnectWithOptions(&options);
if (c == NULL || c->err) {
if (c) {
printf("Connection error: %s\n", c->errstr);
redisFree(c);
} else {
printf("Connection error: can't allocate redis context\n");
}
exit(1);
}
if (redisSecureConnection(c, ca, cert, key, "sni") != REDIS_OK) {
printf("Couldn't initialize SSL!\n");
printf("Error: %s\n", c->errstr);
redisFree(c);
exit(1);
}
But when I try to run it against Compose.com
which require username
and password
in the url, like:
const char *hostname = "USERNAME:PASSWORD@my.redis-as-a-service.com";
then it fails without a specific error. Simple:
Connection error: Name or service not known
You cannot specify a username and passwort in the hostname string. This whole string is treated as the hostname and DNS lookup fails.
Instead you have to do it this way:
First, connect normally without authentication credentials.
After this succeeded, you can authenticate with
redisCommand(c, "AUTH password");
Note that usernames are not supported with redis
, so you cannot specify one.