08-ssl-connect-cert-auth-enc.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <cstring>
  2. #include <mosquittopp.h>
  3. static int run = -1;
  4. static int password_callback(char* buf, int size, int rwflag, void* userdata)
  5. {
  6. strncpy(buf, "password", size);
  7. buf[size-1] = '\0';
  8. return strlen(buf);
  9. }
  10. class mosquittopp_test : public mosqpp::mosquittopp
  11. {
  12. public:
  13. mosquittopp_test(const char *id);
  14. void on_connect(int rc);
  15. void on_disconnect(int rc);
  16. };
  17. mosquittopp_test::mosquittopp_test(const char *id) : mosqpp::mosquittopp(id)
  18. {
  19. }
  20. void mosquittopp_test::on_connect(int rc)
  21. {
  22. if(rc){
  23. exit(1);
  24. }else{
  25. disconnect();
  26. }
  27. }
  28. void mosquittopp_test::on_disconnect(int rc)
  29. {
  30. run = rc;
  31. }
  32. int main(int argc, char *argv[])
  33. {
  34. struct mosquittopp_test *mosq;
  35. int port = atoi(argv[1]);
  36. mosqpp::lib_init();
  37. mosq = new mosquittopp_test("08-ssl-connect-crt-auth-enc");
  38. mosq->tls_opts_set(1, "tlsv1", NULL);
  39. //mosq->tls_set("../ssl/test-ca.crt", NULL, "../ssl/client.crt", "../ssl/client.key");
  40. mosq->tls_set("../ssl/all-ca.crt", NULL, "../ssl/client-encrypted.crt", "../ssl/client-encrypted.key", password_callback);
  41. mosq->connect("localhost", port, 60);
  42. while(run == -1){
  43. mosq->loop();
  44. }
  45. delete mosq;
  46. delete mosq;
  47. mosqpp::lib_cleanup();
  48. return run;
  49. }