example-ssl.c 3.0 KB

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