-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
bridge_topic.c
247 lines (205 loc) · 6.75 KB
/
bridge_topic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
Copyright (c) 2009-2019 Roger Light <[email protected]>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http:https://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include "mosquitto.h"
#include "mosquitto_broker_internal.h"
#include "memory_mosq.h"
#ifdef WITH_BRIDGE
static int bridge__create_remap_topic(const char *prefix, const char *topic, char **remap_topic)
{
size_t len;
if(prefix){
if(topic){
len = strlen(topic) + strlen(prefix)+1;
*remap_topic = mosquitto__malloc(len+1);
if(!(*remap_topic)){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
snprintf(*remap_topic, len+1, "%s%s", prefix, topic);
(*remap_topic)[len] = '\0';
}else{
*remap_topic = mosquitto__strdup(prefix);
if(!(*remap_topic)){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
}
}else{
*remap_topic = mosquitto__strdup(topic);
if(!(*remap_topic)){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
}
return MOSQ_ERR_SUCCESS;
}
static int bridge__create_prefix(char **full_prefix, const char *topic, const char *prefix, const char *direction)
{
size_t len;
if(mosquitto_pub_topic_check(prefix) != MOSQ_ERR_SUCCESS){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge topic local prefix '%s'.", prefix);
return MOSQ_ERR_INVAL;
}
if(topic){
len = strlen(topic) + strlen(prefix) + 1;
}else{
len = strlen(prefix) + 1;
}
*full_prefix = mosquitto__malloc(len);
if(*full_prefix == NULL){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
if(topic){
/* Print full_prefix+pattern to check for validity */
snprintf(*full_prefix, len, "%s%s", prefix, topic);
}else{
snprintf(*full_prefix, len, "%s", prefix);
}
if(mosquitto_sub_topic_check(*full_prefix) != MOSQ_ERR_SUCCESS){
log__printf(NULL, MOSQ_LOG_ERR,
"Error: Invalid bridge topic %s prefix and pattern combination '%s'.",
direction, *full_prefix);
return MOSQ_ERR_INVAL;
}
/* Print just the prefix for storage */
snprintf(*full_prefix, len, "%s", prefix);
return MOSQ_ERR_SUCCESS;
}
/* topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix] */
int bridge__add_topic(struct mosquitto__bridge *bridge, const char *topic, enum mosquitto__bridge_direction direction, uint8_t qos, const char *local_prefix, const char *remote_prefix)
{
struct mosquitto__bridge_topic *topics;
struct mosquitto__bridge_topic *cur_topic;
if(bridge == NULL) return MOSQ_ERR_INVAL;
if(direction != bd_out && direction != bd_in && direction != bd_both){
return MOSQ_ERR_INVAL;
}
if(qos > 2){
return MOSQ_ERR_INVAL;
}
if(local_prefix && mosquitto_pub_topic_check(local_prefix)){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge topic local prefix '%s'.", local_prefix);
return MOSQ_ERR_INVAL;
}
if(remote_prefix && mosquitto_pub_topic_check(remote_prefix)){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge topic remote prefix '%s'.", remote_prefix);
return MOSQ_ERR_INVAL;
}
if((topic == NULL || !strcmp(topic, "\"\"")) &&
(local_prefix == NULL || remote_prefix == NULL)){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge remapping.");
return MOSQ_ERR_INVAL;
}
bridge->topic_count++;
topics = mosquitto__realloc(bridge->topics,
sizeof(struct mosquitto__bridge_topic)*(size_t)bridge->topic_count);
if(topics == NULL){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
bridge->topics = topics;
cur_topic = &bridge->topics[bridge->topic_count-1];
cur_topic->direction = direction;
cur_topic->qos = qos;
cur_topic->local_prefix = NULL;
cur_topic->remote_prefix = NULL;
if(topic == NULL || !strcmp(topic, "\"\"")){
cur_topic->topic = NULL;
}else{
cur_topic->topic = mosquitto__strdup(topic);
if(cur_topic->topic == NULL){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
}
if(local_prefix || remote_prefix){
bridge->topic_remapping = true;
if(local_prefix){
if(bridge__create_prefix(&cur_topic->local_prefix, cur_topic->topic, local_prefix, "local")){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
}
if(remote_prefix){
if(bridge__create_prefix(&cur_topic->remote_prefix, cur_topic->topic, remote_prefix, "local")){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
}
}
if(bridge__create_remap_topic(cur_topic->local_prefix,
cur_topic->topic, &cur_topic->local_topic)){
return MOSQ_ERR_INVAL;
}
if(bridge__create_remap_topic(cur_topic->remote_prefix,
cur_topic->topic, &cur_topic->remote_topic)){
return MOSQ_ERR_INVAL;
}
return MOSQ_ERR_SUCCESS;
}
int bridge__remap_topic_in(struct mosquitto *context, char **topic)
{
struct mosquitto__bridge_topic *cur_topic;
char *topic_temp;
int i;
size_t len;
int rc;
bool match;
if(context->bridge && context->bridge->topics && context->bridge->topic_remapping){
for(i=0; i<context->bridge->topic_count; i++){
cur_topic = &context->bridge->topics[i];
if((cur_topic->direction == bd_both || cur_topic->direction == bd_in)
&& (cur_topic->remote_prefix || cur_topic->local_prefix)){
/* Topic mapping required on this topic if the message matches */
rc = mosquitto_topic_matches_sub(cur_topic->remote_topic, *topic, &match);
if(rc){
mosquitto__free(*topic);
return rc;
}
if(match){
if(cur_topic->remote_prefix){
/* This prefix needs removing. */
if(!strncmp(cur_topic->remote_prefix, *topic, strlen(cur_topic->remote_prefix))){
topic_temp = mosquitto__strdup((*topic)+strlen(cur_topic->remote_prefix));
if(!topic_temp){
mosquitto__free(*topic);
return MOSQ_ERR_NOMEM;
}
mosquitto__free(*topic);
*topic = topic_temp;
}
}
if(cur_topic->local_prefix){
/* This prefix needs adding. */
len = strlen(*topic) + strlen(cur_topic->local_prefix)+1;
topic_temp = mosquitto__malloc(len+1);
if(!topic_temp){
mosquitto__free(*topic);
return MOSQ_ERR_NOMEM;
}
snprintf(topic_temp, len, "%s%s", cur_topic->local_prefix, *topic);
topic_temp[len] = '\0';
mosquitto__free(*topic);
*topic = topic_temp;
}
break;
}
}
}
}
return MOSQ_ERR_SUCCESS;
}
#endif