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 "azure_c_shared_utility/gballoc.h"
|
---|
6 | #include "azure_c_shared_utility/wsio.h"
|
---|
7 | #include "azure_prov_client/prov_transport_mqtt_client.h"
|
---|
8 | #include "azure_prov_client/internal/prov_transport_mqtt_common.h"
|
---|
9 | #include "azure_c_shared_utility/xio.h"
|
---|
10 | #include "azure_c_shared_utility/tlsio.h"
|
---|
11 | #include "azure_c_shared_utility/http_proxy_io.h"
|
---|
12 | #include "azure_c_shared_utility/platform.h"
|
---|
13 | #include "azure_c_shared_utility/shared_util_options.h"
|
---|
14 |
|
---|
15 | #define MQTT_WS_PORT_NUM 443
|
---|
16 | #define MQTT_WS_PROTOCOL_NAME "MQTT"
|
---|
17 | #define MQTT_WS_RELATIVE_PATH "/$iothub/websocket"
|
---|
18 |
|
---|
19 | static XIO_HANDLE mqtt_transport_ws_io(const char* fqdn, const HTTP_PROXY_OPTIONS* proxy_info)
|
---|
20 | {
|
---|
21 | XIO_HANDLE result;
|
---|
22 | HTTP_PROXY_IO_CONFIG proxy_config;
|
---|
23 | TLSIO_CONFIG tls_io_config;
|
---|
24 | WSIO_CONFIG ws_io_config;
|
---|
25 | const IO_INTERFACE_DESCRIPTION* ws_io_interface;
|
---|
26 | const IO_INTERFACE_DESCRIPTION* tlsio_interface;
|
---|
27 |
|
---|
28 | if ((ws_io_interface = wsio_get_interface_description()) == NULL)
|
---|
29 | {
|
---|
30 | /* Codes_PROV_TRANSPORT_MQTT_WS_CLIENT_07_013: [ If any failure is encountered amqp_transport_ws_io shall return NULL ] */
|
---|
31 | LogError("wsio_get_interface_description return NULL IO Interface");
|
---|
32 | result = NULL;
|
---|
33 | }
|
---|
34 | else if ((tlsio_interface = platform_get_default_tlsio()) == NULL)
|
---|
35 | {
|
---|
36 | /* Codes_PROV_TRANSPORT_MQTT_WS_CLIENT_07_013: [ If any failure is encountered amqp_transport_ws_io shall return NULL ] */
|
---|
37 | LogError("platform_get_default_tlsio return NULL IO Interface");
|
---|
38 | result = NULL;
|
---|
39 | }
|
---|
40 | else
|
---|
41 | {
|
---|
42 | memset(&tls_io_config, 0, sizeof(TLSIO_CONFIG));
|
---|
43 |
|
---|
44 | ws_io_config.hostname = fqdn;
|
---|
45 | ws_io_config.port = MQTT_WS_PORT_NUM;
|
---|
46 | ws_io_config.protocol = MQTT_WS_PROTOCOL_NAME;
|
---|
47 | ws_io_config.resource_name = MQTT_WS_RELATIVE_PATH;
|
---|
48 | ws_io_config.underlying_io_interface = tlsio_interface;
|
---|
49 | ws_io_config.underlying_io_parameters = &tls_io_config;
|
---|
50 |
|
---|
51 | tls_io_config.hostname = fqdn;
|
---|
52 | tls_io_config.port = MQTT_WS_PORT_NUM;
|
---|
53 | if (proxy_info != NULL)
|
---|
54 | {
|
---|
55 | /* Codes_PROV_TRANSPORT_MQTT_CLIENT_07_012: [ If proxy_info is not NULL, amqp_transport_io shall construct a HTTP_PROXY_IO_CONFIG object and assign it to TLSIO_CONFIG underlying_io_parameters ] */
|
---|
56 | proxy_config.hostname = tls_io_config.hostname;
|
---|
57 | proxy_config.port = MQTT_WS_PORT_NUM;
|
---|
58 | proxy_config.proxy_hostname = proxy_info->host_address;
|
---|
59 | proxy_config.proxy_port = proxy_info->port;
|
---|
60 | proxy_config.username = proxy_info->username;
|
---|
61 | proxy_config.password = proxy_info->password;
|
---|
62 |
|
---|
63 | tls_io_config.underlying_io_interface = http_proxy_io_get_interface_description();
|
---|
64 | tls_io_config.underlying_io_parameters = &proxy_config;
|
---|
65 | }
|
---|
66 |
|
---|
67 | result = xio_create(ws_io_interface, &ws_io_config);
|
---|
68 | if (result == NULL)
|
---|
69 | {
|
---|
70 | LogError("failed calling xio_create on underlying io");
|
---|
71 | }
|
---|
72 | }
|
---|
73 | /* Codes_PROV_TRANSPORT_MQTT_WS_CLIENT_07_014: [ On success mqtt_transport_ws_io shall return an allocated XIO_HANDLE. ] */
|
---|
74 | return result;
|
---|
75 | }
|
---|
76 |
|
---|
77 | PROV_DEVICE_TRANSPORT_HANDLE prov_transport_mqtt_ws_create(const char* uri, TRANSPORT_HSM_TYPE type, const char* scope_id, const char* api_version, PROV_TRANSPORT_ERROR_CALLBACK error_cb, void* error_ctx)
|
---|
78 | {
|
---|
79 | /* Codes_PROV_TRANSPORT_MQTT_WS_CLIENT_07_001: [ prov_transport_mqtt_ws_create shall call the prov_trans_common_mqtt_create function with mqtt_transport_ws_io transport IO estabishment. ] */
|
---|
80 | return prov_transport_common_mqtt_create(uri, type, scope_id, api_version, mqtt_transport_ws_io, error_cb, error_ctx);
|
---|
81 | }
|
---|
82 |
|
---|
83 | void prov_transport_mqtt_ws_destroy(PROV_DEVICE_TRANSPORT_HANDLE handle)
|
---|
84 | {
|
---|
85 | /* Codes_PROV_TRANSPORT_MQTT_WS_CLIENT_07_002: [ prov_transport_mqtt_ws_destroy shall invoke the prov_transport_common_mqtt_destroy method ] */
|
---|
86 | prov_transport_common_mqtt_destroy(handle);
|
---|
87 | }
|
---|
88 |
|
---|
89 | int prov_transport_mqtt_ws_open(PROV_DEVICE_TRANSPORT_HANDLE handle, const char* registration_id, BUFFER_HANDLE ek, BUFFER_HANDLE srk, PROV_DEVICE_TRANSPORT_REGISTER_CALLBACK data_callback, void* user_ctx, PROV_DEVICE_TRANSPORT_STATUS_CALLBACK status_cb, void* status_ctx, PROV_TRANSPORT_CHALLENGE_CALLBACK reg_challenge_cb, void* challenge_ctx)
|
---|
90 | {
|
---|
91 | /* Codes_PROV_TRANSPORT_MQTT_WS_CLIENT_07_003: [ prov_transport_mqtt_ws_open shall invoke the prov_transport_common_mqtt_open method ] */
|
---|
92 | return prov_transport_common_mqtt_open(handle, registration_id, ek, srk, data_callback, user_ctx, status_cb, status_ctx, reg_challenge_cb, challenge_ctx);
|
---|
93 | }
|
---|
94 |
|
---|
95 | int prov_transport_mqtt_ws_close(PROV_DEVICE_TRANSPORT_HANDLE handle)
|
---|
96 | {
|
---|
97 | /* Codes_PROV_TRANSPORT_MQTT_WS_CLIENT_07_004: [ prov_transport_mqtt_ws_close shall invoke the prov_transport_common_mqtt_close method ] */
|
---|
98 | return prov_transport_common_mqtt_close(handle);
|
---|
99 | }
|
---|
100 |
|
---|
101 | int prov_transport_mqtt_ws_register_device(PROV_DEVICE_TRANSPORT_HANDLE handle, PROV_TRANSPORT_JSON_PARSE json_parse_cb, PROV_TRANSPORT_CREATE_JSON_PAYLOAD json_create_cb, void* json_ctx)
|
---|
102 | {
|
---|
103 | /* Codes_PROV_TRANSPORT_MQTT_WS_CLIENT_07_005: [ prov_transport_mqtt_ws_register_device shall invoke the prov_transport_common_mqtt_register_device method ] */
|
---|
104 | return prov_transport_common_mqtt_register_device(handle, json_parse_cb, json_create_cb, json_ctx);
|
---|
105 | }
|
---|
106 |
|
---|
107 | int prov_transport_mqtt_ws_get_operation_status(PROV_DEVICE_TRANSPORT_HANDLE handle)
|
---|
108 | {
|
---|
109 | /* Codes_PROV_TRANSPORT_MQTT_WS_CLIENT_07_006: [ prov_transport_mqtt_ws_get_operation_status shall invoke the prov_transport_common_mqtt_get_operation_status method ] */
|
---|
110 | return prov_transport_common_mqtt_get_operation_status(handle);
|
---|
111 | }
|
---|
112 |
|
---|
113 | void prov_transport_mqtt_ws_dowork(PROV_DEVICE_TRANSPORT_HANDLE handle)
|
---|
114 | {
|
---|
115 | /* Codes_PROV_TRANSPORT_MQTT_WS_CLIENT_07_007: [ prov_transport_mqtt_ws_dowork shall invoke the prov_transport_common_mqtt_dowork method ] */
|
---|
116 | prov_transport_common_mqtt_dowork(handle);
|
---|
117 | }
|
---|
118 |
|
---|
119 | int prov_transport_mqtt_ws_set_trace(PROV_DEVICE_TRANSPORT_HANDLE handle, bool trace_on)
|
---|
120 | {
|
---|
121 | /* Codes_PROV_TRANSPORT_MQTT_WS_CLIENT_07_008: [ prov_transport_mqtt_ws_set_trace shall invoke the prov_transport_common_mqtt_set_trace method ] */
|
---|
122 | return prov_transport_common_mqtt_set_trace(handle, trace_on);
|
---|
123 | }
|
---|
124 |
|
---|
125 | static int prov_transport_mqtt_ws_x509_cert(PROV_DEVICE_TRANSPORT_HANDLE handle, const char* certificate, const char* private_key)
|
---|
126 | {
|
---|
127 | /* Codes_PROV_TRANSPORT_MQTT_WS_CLIENT_07_009: [ prov_transport_mqtt_ws_x509_cert shall invoke the prov_trans_common_mqtt_x509_cert method ] */
|
---|
128 | return prov_transport_common_mqtt_x509_cert(handle, certificate, private_key);
|
---|
129 | }
|
---|
130 |
|
---|
131 | static int prov_transport_mqtt_ws_set_trusted_cert(PROV_DEVICE_TRANSPORT_HANDLE handle, const char* certificate)
|
---|
132 | {
|
---|
133 | /* Codes_PROV_TRANSPORT_MQTT_WS_CLIENT_07_010: [ prov_transport_mqtt_ws_set_trusted_cert shall invoke the prov_transport_common_mqtt_set_trusted_cert method ] */
|
---|
134 | return prov_transport_common_mqtt_set_trusted_cert(handle, certificate);
|
---|
135 | }
|
---|
136 |
|
---|
137 | static int prov_transport_mqtt_ws_set_proxy(PROV_DEVICE_TRANSPORT_HANDLE handle, const HTTP_PROXY_OPTIONS* proxy_options)
|
---|
138 | {
|
---|
139 | /* Codes_PROV_TRANSPORT_MQTT_WS_CLIENT_07_011: [ prov_transport_mqtt_ws_set_proxy shall invoke the prov_transport_common_mqtt_set_proxy method ] */
|
---|
140 | return prov_transport_common_mqtt_set_proxy(handle, proxy_options);
|
---|
141 | }
|
---|
142 |
|
---|
143 | static int prov_transport_mqtt_ws_set_option(PROV_DEVICE_TRANSPORT_HANDLE handle, const char* option, const void* value)
|
---|
144 | {
|
---|
145 | return prov_transport_common_mqtt_set_option(handle, option, value);
|
---|
146 | }
|
---|
147 |
|
---|
148 | static PROV_DEVICE_TRANSPORT_PROVIDER prov_mqtt_func =
|
---|
149 | {
|
---|
150 | prov_transport_mqtt_ws_create,
|
---|
151 | prov_transport_mqtt_ws_destroy,
|
---|
152 | prov_transport_mqtt_ws_open,
|
---|
153 | prov_transport_mqtt_ws_close,
|
---|
154 | prov_transport_mqtt_ws_register_device,
|
---|
155 | prov_transport_mqtt_ws_get_operation_status,
|
---|
156 | prov_transport_mqtt_ws_dowork,
|
---|
157 | prov_transport_mqtt_ws_set_trace,
|
---|
158 | prov_transport_mqtt_ws_x509_cert,
|
---|
159 | prov_transport_mqtt_ws_set_trusted_cert,
|
---|
160 | prov_transport_mqtt_ws_set_proxy,
|
---|
161 | prov_transport_mqtt_ws_set_option
|
---|
162 | };
|
---|
163 |
|
---|
164 | const PROV_DEVICE_TRANSPORT_PROVIDER* Prov_Device_MQTT_WS_Protocol(void)
|
---|
165 | {
|
---|
166 | return &prov_mqtt_func;
|
---|
167 | }
|
---|