amqp_ssl_connect.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * ***** BEGIN LICENSE BLOCK *****
  3. * Version: MIT
  4. *
  5. * Portions created by Alan Antonuk are Copyright (c) 2012-2013
  6. * Alan Antonuk. All Rights Reserved.
  7. *
  8. * Portions created by Mike Steinert are Copyright (c) 2012-2013
  9. * Mike Steinert. All Rights Reserved.
  10. *
  11. * Portions created by Bogdan Padalko are Copyright (c) 2013.
  12. * Bogdan Padalko. All Rights Reserved.
  13. *
  14. * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
  15. * All Rights Reserved.
  16. *
  17. * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
  18. * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
  19. *
  20. * Permission is hereby granted, free of charge, to any person
  21. * obtaining a copy of this software and associated documentation
  22. * files (the "Software"), to deal in the Software without
  23. * restriction, including without limitation the rights to use, copy,
  24. * modify, merge, publish, distribute, sublicense, and/or sell copies
  25. * of the Software, and to permit persons to whom the Software is
  26. * furnished to do so, subject to the following conditions:
  27. *
  28. * The above copyright notice and this permission notice shall be
  29. * included in all copies or substantial portions of the Software.
  30. *
  31. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  32. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  33. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  34. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  35. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  36. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  37. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  38. * SOFTWARE.
  39. * ***** END LICENSE BLOCK *****
  40. */
  41. #include <stdint.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <amqp.h>
  46. #include <amqp_ssl_socket.h>
  47. #include <assert.h>
  48. #ifdef _WIN32
  49. #ifndef WIN32_LEAN_AND_MEAN
  50. #define WIN32_LEAN_AND_MEAN
  51. #endif
  52. #include <Winsock2.h>
  53. #else
  54. #include <sys/time.h>
  55. #endif
  56. #include "utils.h"
  57. int main(int argc, char const *const *argv) {
  58. char const *hostname;
  59. int port;
  60. int timeout;
  61. amqp_socket_t *socket;
  62. amqp_connection_state_t conn;
  63. struct timeval tval;
  64. struct timeval *tv;
  65. if (argc < 3) {
  66. fprintf(stderr,
  67. "Usage: amqps_connect_timeout host port timeout_sec "
  68. "[cacert.pem [verifypeer] [verifyhostname] [key.pem cert.pem]]\n");
  69. return 1;
  70. }
  71. hostname = argv[1];
  72. port = atoi(argv[2]);
  73. timeout = atoi(argv[3]);
  74. if (timeout > 0) {
  75. tv = &tval;
  76. tv->tv_sec = timeout;
  77. tv->tv_usec = 0;
  78. } else {
  79. tv = NULL;
  80. }
  81. conn = amqp_new_connection();
  82. socket = amqp_ssl_socket_new(conn);
  83. if (!socket) {
  84. die("creating SSL/TLS socket");
  85. }
  86. amqp_ssl_socket_set_verify_peer(socket, 0);
  87. amqp_ssl_socket_set_verify_hostname(socket, 0);
  88. if (argc > 5) {
  89. int nextarg = 5;
  90. die_on_error(amqp_ssl_socket_set_cacert(socket, argv[4]),
  91. "setting CA certificate");
  92. if (argc > nextarg && !strcmp("verifypeer", argv[nextarg])) {
  93. amqp_ssl_socket_set_verify_peer(socket, 1);
  94. nextarg++;
  95. }
  96. if (argc > nextarg && !strcmp("verifyhostname", argv[nextarg])) {
  97. amqp_ssl_socket_set_verify_hostname(socket, 1);
  98. nextarg++;
  99. }
  100. if (argc > nextarg + 1) {
  101. die_on_error(
  102. amqp_ssl_socket_set_key(socket, argv[nextarg + 1], argv[nextarg]),
  103. "setting client key");
  104. }
  105. }
  106. die_on_error(amqp_socket_open_noblock(socket, hostname, port, tv),
  107. "opening SSL/TLS connection");
  108. die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
  109. "guest", "guest"),
  110. "Logging in");
  111. die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
  112. "Closing connection");
  113. die_on_error(amqp_destroy_connection(conn), "Ending connection");
  114. die_on_error(amqp_uninitialize_ssl_library(), "Uninitializing SSL library");
  115. printf("Done\n");
  116. return 0;
  117. }