2
0

example-ssl.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <hiredis.h>
  5. #include <hiredis_ssl.h>
  6. int main(int argc, char **argv) {
  7. unsigned int j;
  8. redisContext *c;
  9. redisReply *reply;
  10. if (argc < 4) {
  11. printf("Usage: %s <host> <port> <cert> <key> [ca]\n", argv[0]);
  12. exit(1);
  13. }
  14. const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";
  15. int port = atoi(argv[2]);
  16. const char *cert = argv[3];
  17. const char *key = argv[4];
  18. const char *ca = argc > 4 ? argv[5] : NULL;
  19. struct timeval tv = { 1, 500000 }; // 1.5 seconds
  20. redisOptions options = {0};
  21. REDIS_OPTIONS_SET_TCP(&options, hostname, port);
  22. options.timeout = &tv;
  23. c = redisConnectWithOptions(&options);
  24. if (c == NULL || c->err) {
  25. if (c) {
  26. printf("Connection error: %s\n", c->errstr);
  27. redisFree(c);
  28. } else {
  29. printf("Connection error: can't allocate redis context\n");
  30. }
  31. exit(1);
  32. }
  33. if (redisSecureConnection(c, ca, cert, key, "sni") != REDIS_OK) {
  34. printf("Couldn't initialize SSL!\n");
  35. printf("Error: %s\n", c->errstr);
  36. redisFree(c);
  37. exit(1);
  38. }
  39. /* PING server */
  40. reply = redisCommand(c,"PING");
  41. printf("PING: %s\n", reply->str);
  42. freeReplyObject(reply);
  43. /* Set a key */
  44. reply = redisCommand(c,"SET %s %s", "foo", "hello world");
  45. printf("SET: %s\n", reply->str);
  46. freeReplyObject(reply);
  47. /* Set a key using binary safe API */
  48. reply = redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5);
  49. printf("SET (binary API): %s\n", reply->str);
  50. freeReplyObject(reply);
  51. /* Try a GET and two INCR */
  52. reply = redisCommand(c,"GET foo");
  53. printf("GET foo: %s\n", reply->str);
  54. freeReplyObject(reply);
  55. reply = redisCommand(c,"INCR counter");
  56. printf("INCR counter: %lld\n", reply->integer);
  57. freeReplyObject(reply);
  58. /* again ... */
  59. reply = redisCommand(c,"INCR counter");
  60. printf("INCR counter: %lld\n", reply->integer);
  61. freeReplyObject(reply);
  62. /* Create a list of numbers, from 0 to 9 */
  63. reply = redisCommand(c,"DEL mylist");
  64. freeReplyObject(reply);
  65. for (j = 0; j < 10; j++) {
  66. char buf[64];
  67. snprintf(buf,64,"%u",j);
  68. reply = redisCommand(c,"LPUSH mylist element-%s", buf);
  69. freeReplyObject(reply);
  70. }
  71. /* Let's check what we have inside the list */
  72. reply = redisCommand(c,"LRANGE mylist 0 -1");
  73. if (reply->type == REDIS_REPLY_ARRAY) {
  74. for (j = 0; j < reply->elements; j++) {
  75. printf("%u) %s\n", j, reply->element[j]->str);
  76. }
  77. }
  78. freeReplyObject(reply);
  79. /* Disconnects and frees the context */
  80. redisFree(c);
  81. return 0;
  82. }