Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [mosquitto-dev] [PATCH] Add MQTT URL scheme support

Hi Matteo,

Thanks for this, it looks good. Could you please submit it as an
attachment to a bug on the eclipse bugzilla[1]?

You'll also need to have signed the Eclipse CLA [2] and add a comment
to the bug as described here [3].

One other point - the "-x" argument is already taken by mosquitto_sub
so that can't be used. I do very much regret mixing up the case of -u
for username and -P for password. Using -U for username and -u for url
would work very well. Any thoughts on this anybody else?

[1] https://bugs.eclipse.org/bugs/enter_bug.cgi?product=Mosquitto
[2] https://projects.eclipse.org/user/sign/cla
[3] https://wiki.eclipse.org/Development_Resources/Contributing_via_Git#via_Bugzilla

Cheers,

Roger

On Wed, Feb 10, 2016 at 4:41 PM, Matteo Croce <matteo@xxxxxxxxxxx> wrote:
> 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
>
> _______________________________________________
> mosquitto-dev mailing list
> mosquitto-dev@xxxxxxxxxxx
> To change your delivery options, retrieve your password, or unsubscribe from this list, visit
> https://dev.eclipse.org/mailman/listinfo/mosquitto-dev


Back to the top