[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[mosquitto-dev] [PATCH] Add MQTT URL scheme support
|
Add option -x to specify user, password, hostname, port and topic at once.
The URL must provided with the -x or --url in the form:
mqtt(s)://user:pass@xxxxxxxxxxx:port/topic
---
client/client_shared.c | 81 ++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 66 insertions(+), 15 deletions(-)
diff --git a/client/client_shared.c b/client/client_shared.c
index 7a1fe0d..46c829c 100644
--- a/client/client_shared.c
+++ b/client/client_shared.c
@@ -234,6 +234,26 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
return MOSQ_ERR_SUCCESS;
}
+int cfg_add_topic(struct mosq_config *cfg, int pub_or_sub, char *topic)
+{
+ if(pub_or_sub == CLIENT_PUB){
+ if(mosquitto_pub_topic_check(topic) == MOSQ_ERR_INVAL){
+ fprintf(stderr, "Error: Invalid publish topic '%s', does it contain '+' or '#'?\n", topic);
+ return 1;
+ }
+ cfg->topic = strdup(topic);
+ } else {
+ if(mosquitto_sub_topic_check(topic) == MOSQ_ERR_INVAL){
+ fprintf(stderr, "Error: Invalid subscription topic '%s', are all '+' and '#' wildcards correct?\n", topic);
+ return 1;
+ }
+ cfg->topic_count++;
+ cfg->topics = realloc(cfg->topics, cfg->topic_count*sizeof(char *));
+ cfg->topics[cfg->topic_count-1] = strdup(topic);
+ }
+ return 0;
+}
+
/* Process a tokenised single line from a file or set of real argc/argv */
int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, char *argv[])
{
@@ -514,21 +534,8 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c
fprintf(stderr, "Error: -t argument given but no topic specified.\n\n");
return 1;
}else{
- if(pub_or_sub == CLIENT_PUB){
- if(mosquitto_pub_topic_check(argv[i+1]) == MOSQ_ERR_INVAL){
- fprintf(stderr, "Error: Invalid publish topic '%s', does it contain '+' or '#'?\n", argv[i+1]);
- return 1;
- }
- cfg->topic = strdup(argv[i+1]);
- }else{
- if(mosquitto_sub_topic_check(argv[i+1]) == MOSQ_ERR_INVAL){
- fprintf(stderr, "Error: Invalid subscription topic '%s', are all '+' and '#' wildcards correct?\n", argv[i+1]);
- return 1;
- }
- cfg->topic_count++;
- cfg->topics = realloc(cfg->topics, cfg->topic_count*sizeof(char *));
- cfg->topics[cfg->topic_count-1] = strdup(argv[i+1]);
- }
+ if(cfg_add_topic(cfg, pub_or_sub, argv[i + 1]))
+ return 1;
i++;
}
}else if(!strcmp(argv[i], "-T") || !strcmp(argv[i], "--filter-out")){
@@ -629,6 +636,50 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c
goto unknown_option;
}
cfg->verbose = 1;
+ }else if(!strcmp(argv[i], "-x") || !strcmp(argv[i], "--url")){
+ if(i==argc-1){
+ fprintf(stderr, "Error: -x argument given but no URL specified.\n\n");
+ return 1;
+ }else{
+ char *url = argv[i+1];
+ char *topic;
+ char *tmp;
+
+ if(!strncasecmp(url, "mqtt://", 7)) {
+ url += 7;
+ } else if(!strncasecmp(url, "mqtts://", 8)) {
+ url += 8;
+ cfg->port = 8883;
+ } else {
+ fprintf(stderr, "Error: unsupported URL scheme.\n\n");
+ return 1;
+ }
+ topic = strchr(url, '/');
+ *topic++ = 0;
+
+ if(cfg_add_topic(cfg, pub_or_sub, topic))
+ return 1;
+
+ tmp = strchr(url, '@');
+ if(tmp) {
+ char *colon = strchr(url, ':');
+ *tmp++ = 0;
+ if(colon) {
+ *colon = 0;
+ cfg->password = colon + 1;
+ }
+ cfg->username = url;
+ url = tmp;
+ }
+ cfg->host = url;
+
+ tmp = strchr(url, ':');
+ if(tmp) {
+ *tmp++ = 0;
+ cfg->port = atoi(tmp);
+ }
+ }
+ i++;
}else{
goto unknown_option;
}
--
2.5.0