Debian Codegraph @

src/vendor/azure-uamqp-c/samples/websockets_sample/main.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <stdbool.h>
7 #include "azure_c_shared_utility/gballoc.h"
8 #include "azure_c_shared_utility/platform.h"
9 #include "azure_c_shared_utility/sastoken.h"
10 #include "azure_c_shared_utility/wsio.h"
11 #include "azure_c_shared_utility/tlsio.h"
12 #include "azure_uamqp_c/uamqp.h"
13 #include "iothub_certs.h"
14
15 /* Replace the below settings with your own.*/
16
17 #define IOT_HUB_HOST "<<<Replace with your own IoTHub host (like myiothub.azure-devices.net)>>>"
18 #define IOT_HUB_DEVICE_NAME "<<<Replace with your device Id (like test_Device)>>>"
19 #define IOT_HUB_DEVICE_KEY "<<<Replace with your own device key>>>"
20
21 static const size_t msg_count = 1000;
22 static unsigned int sent_messages = 0;
23 static bool auth = false;
24
25 static void on_cbs_open_complete(void* context, CBS_OPEN_COMPLETE_RESULT open_complete_result)
26 {
27 (void)context;
28 switch (open_complete_result)
29 {
30 default:
31 (void)printf("CBS instance open failed.\r\n");
32 break;
33
34 case CBS_OPEN_OK:
35 (void)printf("CBS instance open.\r\n");
36 break;
37 }
38 }
39
40 static void on_cbs_error(void* context)
41 {
42 (void)context;
43 (void)printf("CBS error.\r\n");
44 }
45
46 static void on_message_send_complete(void* context, MESSAGE_SEND_RESULT send_result, AMQP_VALUE delivery_state)
47 {
48 (void)send_result;
49 (void)context;
50 (void)delivery_state;
51
52 printf("Sent.\r\n");
53 sent_messages++;
54 }
55
56 void on_cbs_put_token_complete(void* context, CBS_OPERATION_RESULT cbs_operation_result, unsigned int status_code, const char* status_description)
57 {
58 (void)context;
59 (void)status_code;
60 (void)status_description;
61
62 if (cbs_operation_result == CBS_OPERATION_RESULT_OK)
63 {
64 auth = true;
65 }
66 }
67
68 int main(int argc, char** argv)
69 {
70 int result;
71
72 (void)argc;
73 (void)argv;
74
75 if (platform_init() != 0)
76 {
77 result = -1;
78 }
79 else
80 {
81 XIO_HANDLE sasl_io;
82 CONNECTION_HANDLE connection;
83 SESSION_HANDLE session;
84 LINK_HANDLE link;
85 MESSAGE_SENDER_HANDLE message_sender;
86 MESSAGE_HANDLE message;
87 SASLCLIENTIO_CONFIG sasl_io_config;
88 STRING_HANDLE keyname_string;
89
90 size_t last_memory_used = 0;
91
92 /* create SASL MSSBCBS handler */
93 SASL_MECHANISM_HANDLE sasl_mechanism_handle = saslmechanism_create(saslmssbcbs_get_interface(), NULL);
94 XIO_HANDLE ws_io;
95 WSIO_CONFIG ws_io_config;
96 TLSIO_CONFIG tls_io_config;
97 const IO_INTERFACE_DESCRIPTION* tlsio_interface;
98 STRING_HANDLE key_string;
99 STRING_HANDLE scope_string;
100 STRING_HANDLE sas_token;
101 CBS_HANDLE cbs;
102 AMQP_VALUE source;
103 AMQP_VALUE target;
104 unsigned char hello[5] = { 'h', 'e', 'l', 'l', 'o' };
105 BINARY_DATA binary_data;
106
107 gballoc_init();
108
109 tls_io_config.hostname = IOT_HUB_HOST;
110 tls_io_config.port = 443;
111 tls_io_config.underlying_io_interface = NULL;
112 tls_io_config.underlying_io_parameters = NULL;
113
114 /* create the TLS IO */
115 ws_io_config.hostname = IOT_HUB_HOST;
116 ws_io_config.port = 443;
117 ws_io_config.protocol = "AMQPWSB10";
118 ws_io_config.resource_name = "/$iothub/websocket";
119 ws_io_config.underlying_io_interface = platform_get_default_tlsio();
120 ws_io_config.underlying_io_parameters = &tls_io_config;
121
122 tlsio_interface = wsio_get_interface_description();
123 ws_io = xio_create(tlsio_interface, &ws_io_config);
124
125 /* the websockets library uses OpenSSL and on Windows the certs need to be pushed down */
126 (void)xio_setoption(ws_io, "TrustedCerts", iothub_certs);
127
128 /* create the SASL IO using the WS IO */
129 sasl_io_config.underlying_io = ws_io;
130 sasl_io_config.sasl_mechanism = sasl_mechanism_handle;
131
132 sasl_io = xio_create(saslclientio_get_interface_description(), &sasl_io_config);
133
134 /* create the connection, session and link */
135 connection = connection_create(sasl_io, IOT_HUB_HOST, "some", NULL, NULL);
136 connection_set_trace(connection, true);
137 session = session_create(connection, NULL, NULL);
138 session_set_incoming_window(session, 2147483647);
139 session_set_outgoing_window(session, 65536);
140
141 key_string = STRING_new();
142 STRING_concat(key_string, IOT_HUB_DEVICE_KEY);
143 scope_string = STRING_new();
144 STRING_concat(scope_string, IOT_HUB_HOST "/devices/" IOT_HUB_DEVICE_NAME);
145 keyname_string = STRING_new();
146
147 sas_token = SASToken_Create(key_string, scope_string, keyname_string, (size_t)time(NULL) + 3600);
148
149 STRING_delete(key_string);
150 STRING_delete(scope_string);
151 STRING_delete(keyname_string);
152
153 cbs = cbs_create(session);
154 if (cbs_open_async(cbs, on_cbs_open_complete, cbs, on_cbs_error, cbs) == 0)
155 {
156 (void)cbs_put_token_async(cbs, "servicebus.windows.net:sastoken", IOT_HUB_HOST "/devices/" IOT_HUB_DEVICE_NAME, STRING_c_str(sas_token), on_cbs_put_token_complete, cbs);
157
158 while (!auth)
159 {
160 size_t current_memory_used;
161 size_t maximum_memory_used;
162 connection_dowork(connection);
163
164 current_memory_used = gballoc_getCurrentMemoryUsed();
165 maximum_memory_used = gballoc_getMaximumMemoryUsed();
166
167 if (current_memory_used != last_memory_used)
168 {
169 printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used);
170 last_memory_used = current_memory_used;
171 }
172 }
173 }
174
175 source = messaging_create_source("ingress");
176 target = messaging_create_target("amqps://" IOT_HUB_HOST "/devices/" IOT_HUB_DEVICE_NAME "/messages/events");
177 link = link_create(session, "sender-link", role_sender, source, target);
178 (void)link_set_max_message_size(link, 65536);
179
180 amqpvalue_destroy(source);
181 amqpvalue_destroy(target);
182
183 message = message_create();
184
185 binary_data.bytes = hello;
186 binary_data.length = sizeof(hello);
187 message_add_body_amqp_data(message, binary_data);
188
189 /* create a message sender */
190 message_sender = messagesender_create(link, NULL, NULL);
191 if (messagesender_open(message_sender) == 0)
192 {
193 uint32_t i;
194 bool keep_running = true;
195
196 for (i = 0; i < msg_count; i++)
197 {
198 (void)messagesender_send_async(message_sender, message, on_message_send_complete, message, 0);
199 }
200
201 message_destroy(message);
202
203 while (keep_running)
204 {
205 size_t current_memory_used;
206 size_t maximum_memory_used;
207 connection_dowork(connection);
208
209 current_memory_used = gballoc_getCurrentMemoryUsed();
210 maximum_memory_used = gballoc_getMaximumMemoryUsed();
211
212 if (current_memory_used != last_memory_used)
213 {
214 printf("Current memory usage:%lu (max:%lu)\r\n", (unsigned long)current_memory_used, (unsigned long)maximum_memory_used);
215 last_memory_used = current_memory_used;
216 }
217
218 if (sent_messages == msg_count)
219 {
220 break;
221 }
222 }
223 }
224 else
225 {
226 message_destroy(message);
227 }
228
229 STRING_delete(sas_token);
230
231 messagesender_destroy(message_sender);
232 cbs_destroy(cbs);
233 link_destroy(link);
234 session_destroy(session);
235 connection_destroy(connection);
236 xio_destroy(sasl_io);
237 xio_destroy(ws_io);
238 saslmechanism_destroy(sasl_mechanism_handle);
239 platform_deinit();
240
241 printf("Max memory usage:%lu\r\n", (unsigned long)gballoc_getCurrentMemoryUsed());
242 printf("Current memory usage:%lu\r\n", (unsigned long)gballoc_getMaximumMemoryUsed());
243
244 gballoc_deinit();
245
246 result = 0;
247 }
248
249 return result;
250 }