diff options
author | V.Krishn <vkrishn4@gmail.com> | 2017-09-11 07:57:15 +0530 |
---|---|---|
committer | V.Krishn <vkrishn4@gmail.com> | 2017-09-11 07:57:15 +0530 |
commit | 4debde5be57b6321b6ab17c59c86164adaa66045 (patch) | |
tree | cd5d7f3700aea21d3bea490b41bd7c12de7a7712 | |
parent | 57e6dbc4d3ae64aa614f177f66a7f0c9992cc255 (diff) | |
download | mqtt-dirpub-4debde5be57b6321b6ab17c59c86164adaa66045.tar.bz2 |
Update from mosquitto v1.4.14
* Add option `--nodesuffix` - suffix for leaf/text.
* Changed ouput of fmask `@date` from eg. 2010-12-21 to 20101221
* Changed ouput of fmask `@datetime` from eg. 20101221-053020 to 20101221.053020
* Changed ouput of fmask `@date` from eg. 05-30-20 to 053020
-rw-r--r-- | client_shared.c | 84 | ||||
-rw-r--r-- | client_shared.h | 2 | ||||
-rw-r--r-- | sub_client.c | 48 |
3 files changed, 87 insertions, 47 deletions
diff --git a/client_shared.c b/client_shared.c index 9974199..a446218 100644 --- a/client_shared.c +++ b/client_shared.c @@ -54,27 +54,27 @@ void init_config(struct mosq_config *cfg) void client_config_cleanup(struct mosq_config *cfg) { int i; - if(cfg->id) free(cfg->id); - if(cfg->id_prefix) free(cfg->id_prefix); - if(cfg->host) free(cfg->host); - if(cfg->file_input) free(cfg->file_input); - if(cfg->message) free(cfg->message); - if(cfg->topic) free(cfg->topic); - if(cfg->bind_address) free(cfg->bind_address); - if(cfg->username) free(cfg->username); - if(cfg->password) free(cfg->password); - if(cfg->will_topic) free(cfg->will_topic); - if(cfg->will_payload) free(cfg->will_payload); + free(cfg->id); + free(cfg->id_prefix); + free(cfg->host); + free(cfg->file_input); + free(cfg->message); + free(cfg->topic); + free(cfg->bind_address); + free(cfg->username); + free(cfg->password); + free(cfg->will_topic); + free(cfg->will_payload); #ifdef WITH_TLS - if(cfg->cafile) free(cfg->cafile); - if(cfg->capath) free(cfg->capath); - if(cfg->certfile) free(cfg->certfile); - if(cfg->keyfile) free(cfg->keyfile); - if(cfg->ciphers) free(cfg->ciphers); - if(cfg->tls_version) free(cfg->tls_version); + free(cfg->cafile); + free(cfg->capath); + free(cfg->certfile); + free(cfg->keyfile); + free(cfg->ciphers); + free(cfg->tls_version); # ifdef WITH_TLS_PSK - if(cfg->psk) free(cfg->psk); - if(cfg->psk_identity) free(cfg->psk_identity); + free(cfg->psk); + free(cfg->psk_identity); # endif #endif if(cfg->topics){ @@ -90,14 +90,14 @@ void client_config_cleanup(struct mosq_config *cfg) free(cfg->filter_outs); } #ifdef WITH_SOCKS - if(cfg->socks5_host) free(cfg->socks5_host); - if(cfg->socks5_username) free(cfg->socks5_username); - if(cfg->socks5_password) free(cfg->socks5_password); + free(cfg->socks5_host); + free(cfg->socks5_username); + free(cfg->socks5_password); #endif - //if(cfg->ffmask) free(cfg->ffmask); - //if(cfg->ftoken) free(cfg->ftoken); - if(cfg->fmask_topic) free(cfg->fmask_topic); + //free(cfg->ffmask); + //free(cfg->ftoken); + free(cfg->fmask_topic); } @@ -126,6 +126,10 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char * if(env){ len = strlen(env) + strlen("/mosquitto_pub") + 1; loc = malloc(len); + if(!loc){ + fprintf(stderr, "Error: Out of memory.\n"); + return 1; + } if(pub_or_sub == CLIENT_PUB){ snprintf(loc, len, "%s/mosquitto_pub", env); }else{ @@ -137,6 +141,10 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char * if(env){ len = strlen(env) + strlen("/.config/mosquitto_pub") + 1; loc = malloc(len); + if(!loc){ + fprintf(stderr, "Error: Out of memory.\n"); + return 1; + } if(pub_or_sub == CLIENT_PUB){ snprintf(loc, len, "%s/.config/mosquitto_pub", env); }else{ @@ -153,6 +161,10 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char * if(rc > 0 && rc < 1024){ len = strlen(env) + strlen("\\mosquitto_pub.conf") + 1; loc = malloc(len); + if(!loc){ + fprintf(stderr, "Error: Out of memory.\n"); + return 1; + } if(pub_or_sub == CLIENT_PUB){ snprintf(loc, len, "%s\\mosquitto_pub.conf", env); }else{ @@ -281,6 +293,14 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c cfg->isfmask = true; } i++; + }else if(!strcmp(argv[i], "--nodesuffix")){ + if(i==argc-1){ + fprintf(stderr, "Error: --nodesuffix argument given but no text specified.\n\n"); + return 1; + }else{ + cfg->nodesuffix = argv[i+1]; + } + i++; }else if(!strcmp(argv[i], "--overwrite")){ cfg->overwrite = true; @@ -702,7 +722,7 @@ int client_opts_set(struct mosquitto *mosq, struct mosq_config *cfg) return 1; } # endif - if(cfg->tls_version && mosquitto_tls_opts_set(mosq, 1, cfg->tls_version, cfg->ciphers)){ + if((cfg->tls_version || cfg->ciphers) && mosquitto_tls_opts_set(mosq, 1, cfg->tls_version, cfg->ciphers)){ if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS options.\n"); mosquitto_lib_cleanup(); return 1; @@ -739,14 +759,14 @@ int client_id_generate(struct mosq_config *cfg, const char *id_base) hostname[0] = '\0'; gethostname(hostname, 256); hostname[255] = '\0'; - len = strlen(id_base) + strlen("/-") + 6 + strlen(hostname); + len = strlen(id_base) + strlen("|-") + 6 + strlen(hostname); cfg->id = malloc(len); if(!cfg->id){ if(!cfg->quiet) fprintf(stderr, "Error: Out of memory.\n"); mosquitto_lib_cleanup(); return 1; } - snprintf(cfg->id, len, "%s/%d-%s", id_base, getpid(), hostname); + snprintf(cfg->id, len, "%s|%d-%s", id_base, getpid(), hostname); if(strlen(cfg->id) > MOSQ_MQTT_ID_MAX_LENGTH){ /* Enforce maximum client id length of 23 characters */ cfg->id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; @@ -769,7 +789,7 @@ int client_connect(struct mosquitto *mosq, struct mosq_config *cfg) #else rc = mosquitto_connect_bind(mosq, cfg->host, cfg->port, cfg->keepalive, cfg->bind_address); #endif - if(rc){ + if(rc>0){ if(!cfg->quiet){ if(rc == MOSQ_ERR_ERRNO){ #ifndef WIN32 @@ -779,7 +799,7 @@ int client_connect(struct mosquitto *mosq, struct mosq_config *cfg) #endif fprintf(stderr, "Error: %s\n", err); }else{ - fprintf(stderr, "Unable to connect (%d).\n", rc); + fprintf(stderr, "Unable to connect (%s).\n", mosquitto_strerror(rc)); } } mosquitto_lib_cleanup(); @@ -936,6 +956,10 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) port[len] = '\0'; }else{ host = malloc(len + 1); + if(!host){ + fprintf(stderr, "Error: Out of memory.\n"); + goto cleanup; + } memcpy(host, &(str[start]), len); host[len] = '\0'; } diff --git a/client_shared.h b/client_shared.h index f4a5c22..12d2301 100644 --- a/client_shared.h +++ b/client_shared.h @@ -97,6 +97,8 @@ struct mosq_config { char ftoken[1000]; /* limit 1000 bytes. */ char *idtext; char *fmask_topic; + char *nodesuffix; + char nsuffix[16]; /* limit 16 bytes. */ }; diff --git a/sub_client.c b/sub_client.c index 382cc13..dcba57d 100644 --- a/sub_client.c +++ b/sub_client.c @@ -122,7 +122,7 @@ const char *datetime(int fmt) n = snprintf(dt, size, "%02d", (int)current); break; case FMASK_DATE: - n = snprintf(dt, size, "%02d-%02d-%02d", + n = snprintf(dt, size, "%02d%02d%02d", now->tm_year+1900, now->tm_mon+1, now->tm_mday); break; case FMASK_YEAR: @@ -135,12 +135,12 @@ const char *datetime(int fmt) n = snprintf(dt, size, "%02d", now->tm_mday); break; case FMASK_DATETIME: - n = snprintf(dt, size, "%02d%02d%02d-%02d%02d%02d", + n = snprintf(dt, size, "%02d%02d%02d.%02d%02d%02d", now->tm_year+1900, now->tm_mon+1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); break; case FMASK_TIME: - n = snprintf(dt, size, "%02d-%02d-%02d", + n = snprintf(dt, size, "%02d%02d%02d", now->tm_hour, now->tm_min, now->tm_sec); break; case FMASK_HOUR: @@ -231,14 +231,12 @@ void _setfmask(char *token, void *obj) } else if(!strcmp(subtoken, "id")) { dt = cfg->idtext; } else { - dt = strdup (subtoken); + dt = strdup(subtoken); } - to = stpcpy (to, dt); - to = stpcpy (to, "-"); + to = stpcpy(to, dt); } - to[strlen(to)-1] = '\0'; } @@ -255,10 +253,10 @@ void _fmask(char *fmask, void *obj) char *saveptr1; char *path; - path = strdup (fmask); + path = strdup(fmask); char *to = cfg->ffmask; /* limit 1000 bytes. */ - to = stpcpy (to, "/"); + to = stpcpy(to, "/"); for (str1 = path; ; str1 = NULL) { token = strtok_r(str1, "/", &saveptr1); if (token == NULL) @@ -266,8 +264,8 @@ void _fmask(char *fmask, void *obj) /* format type */ _setfmask(token, cfg); - to = stpcpy (to, cfg->ftoken); - to = stpcpy (to, "/"); + to = stpcpy(to, cfg->ftoken); + to = stpcpy(to, "/"); } to[strlen(to)-1] = '\0'; @@ -320,12 +318,27 @@ void my_message_file_callback(struct mosquitto *mosq, void *obj, const struct mo _fmask(cfg->fmask, cfg); char *path, *prog; - path = strdup (cfg->ffmask); - prog = strdup (cfg->ffmask); - path = dirname (path); - prog = basename (prog); + path = dirname(strdup(cfg->ffmask)); + prog = basename(strdup(cfg->ffmask)); mkpath(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + + /* reasonable method to distinguish between directory + * and a writable node (by default is off) */ + if(cfg->nodesuffix) { + char *sf = cfg->nsuffix; /* limit 16 bytes. */ + sf = stpcpy(sf, cfg->nodesuffix); + if(cfg->nsuffix) { + char *to = cfg->ffmask; + to = stpcpy(to, path); + to = stpcpy(to, "/"); + if(prog) { + to = stpcpy(to, prog); + } + to = stpcpy(to, "."); + to = stpcpy(to, cfg->nsuffix); + } + } if(cfg->overwrite) { fptr = _mosquitto_fopen(cfg->ffmask, "w"); @@ -334,7 +347,7 @@ void my_message_file_callback(struct mosquitto *mosq, void *obj, const struct mo } if(!fptr){ - fprintf(stderr, "Error: cannot open outfile, using stdout.\n"); + fprintf(stderr, "Error: cannot open outfile, using stdout - %s\n", cfg->ffmask); // need to do normal stdout //mosquitto_message_callback_set(mosq, "my_message_callback"); } else{ @@ -517,7 +530,8 @@ void print_usage(void) printf(" --fmask : path to message outfile\n"); printf(" allowed masks are:\n"); printf(" @[epoch|date|year|month|day|datetime|hour|min|sec|id|topic[1-9]] \n"); - printf(" eg. --fmask='@id@date@topic' for file id-2010-12-21-topicname\n"); + printf(" eg. --fmask='@id@-@date@-@topic' for file id-20101221-topicname\n"); + printf(" --nodesuffix : suffix for leaf/text node, when --fmask is provided\n"); printf(" --overwrite : overwrite the existing output file, can be used with --fmask only.\n"); printf(" --will-payload : payload for the client Will, which is sent by the broker in case of\n"); printf(" unexpected disconnection. If not given and will-topic is set, a zero\n"); |