Debian Codegraph @

examples/amqp_ssl_connect.c

raw symbols SCIP
1 // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
2 // SPDX-License-Identifier: mit
3
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include <rabbitmq-c/amqp.h>
10 #include <rabbitmq-c/ssl_socket.h>
11
12 #include <assert.h>
13
14 #ifdef _WIN32
15 #ifndef WIN32_LEAN_AND_MEAN
16 #define WIN32_LEAN_AND_MEAN
17 #endif
18 #include <Winsock2.h>
19 #else
20 #include <sys/time.h>
21 #endif
22
23 #include "utils.h"
24
25 int main(int argc, char const *const *argv) {
26 char const *hostname;
27 int port;
28 int timeout;
29 amqp_socket_t *socket;
30 amqp_connection_state_t conn;
31 struct timeval tval;
32 struct timeval *tv;
33
34 if (argc < 3) {
35 fprintf(stderr,
36 "Usage: amqp_ssl_connect host port timeout_sec "
37 "[cacert.pem [engine engine_ID] [verifypeer] [verifyhostname] "
38 "[key.pem cert.pem]]\n");
39 return 1;
40 }
41
42 hostname = argv[1];
43 port = atoi(argv[2]);
44
45 timeout = atoi(argv[3]);
46 if (timeout > 0) {
47 tv = &tval;
48
49 tv->tv_sec = timeout;
50 tv->tv_usec = 0;
51 } else {
52 tv = NULL;
53 }
54
55 conn = amqp_new_connection();
56
57 socket = amqp_ssl_socket_new(conn);
58 if (!socket) {
59 die("creating SSL/TLS socket");
60 }
61
62 amqp_ssl_socket_set_verify_peer(socket, 0);
63 amqp_ssl_socket_set_verify_hostname(socket, 0);
64
65 if (argc > 5) {
66 int nextarg = 5;
67 die_on_error(amqp_ssl_socket_set_cacert(socket, argv[4]),
68 "setting CA certificate");
69 if (argc > nextarg && !strcmp("engine", argv[nextarg])) {
70 amqp_set_ssl_engine(argv[++nextarg]);
71 nextarg++;
72 }
73 if (argc > nextarg && !strcmp("verifypeer", argv[nextarg])) {
74 amqp_ssl_socket_set_verify_peer(socket, 1);
75 nextarg++;
76 }
77 if (argc > nextarg && !strcmp("verifyhostname", argv[nextarg])) {
78 amqp_ssl_socket_set_verify_hostname(socket, 1);
79 nextarg++;
80 }
81 if (argc > nextarg + 1) {
82 die_on_error(
83 amqp_ssl_socket_set_key(socket, argv[nextarg + 1], argv[nextarg]),
84 "setting client key");
85 }
86 }
87
88 die_on_error(amqp_socket_open_noblock(socket, hostname, port, tv),
89 "opening SSL/TLS connection");
90
91 die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
92 "guest", "guest"),
93 "Logging in");
94
95 die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
96 "Closing connection");
97 die_on_error(amqp_destroy_connection(conn), "Ending connection");
98 die_on_error(amqp_uninitialize_ssl_library(), "Uninitializing SSL library");
99
100 printf("Done\n");
101 return 0;
102 }