Stop some error messages being printed even when --quiet was used.

Thanks to Rob de Jonge.

Closes #1284.
This commit is contained in:
Roger A. Light 2019-05-21 11:23:00 +01:00
parent d05bd95881
commit 7a33a129d6
8 changed files with 133 additions and 126 deletions

View File

@ -13,6 +13,8 @@ Broker:
Clients: Clients:
- Fix -L url parsing when `/topic` part is missing. - Fix -L url parsing when `/topic` part is missing.
- Stop some error messages being printed even when `--quiet` was used.
Closes #1284.
1.6.2 - 20190430 1.6.2 - 20190430

View File

@ -18,6 +18,7 @@ Contributors:
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -229,7 +230,7 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
len = strlen(env) + strlen("/mosquitto_pub") + 1; len = strlen(env) + strlen("/mosquitto_pub") + 1;
loc = malloc(len); loc = malloc(len);
if(!loc){ if(!loc){
fprintf(stderr, "Error: Out of memory.\n"); err_printf(cfg, "Error: Out of memory.\n");
return 1; return 1;
} }
if(pub_or_sub == CLIENT_PUB){ if(pub_or_sub == CLIENT_PUB){
@ -246,7 +247,7 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
len = strlen(env) + strlen("/.config/mosquitto_pub") + 1; len = strlen(env) + strlen("/.config/mosquitto_pub") + 1;
loc = malloc(len); loc = malloc(len);
if(!loc){ if(!loc){
fprintf(stderr, "Error: Out of memory.\n"); err_printf(cfg, "Error: Out of memory.\n");
return 1; return 1;
} }
if(pub_or_sub == CLIENT_PUB){ if(pub_or_sub == CLIENT_PUB){
@ -257,8 +258,6 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
snprintf(loc, len, "%s/.config/mosquitto_rr", env); snprintf(loc, len, "%s/.config/mosquitto_rr", env);
} }
loc[len-1] = '\0'; loc[len-1] = '\0';
}else{
fprintf(stderr, "Warning: Unable to locate configuration directory, default config not loaded.\n");
} }
} }
@ -268,7 +267,7 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
len = strlen(env) + strlen("\\mosquitto_pub.conf") + 1; len = strlen(env) + strlen("\\mosquitto_pub.conf") + 1;
loc = malloc(len); loc = malloc(len);
if(!loc){ if(!loc){
fprintf(stderr, "Error: Out of memory.\n"); err_printf(cfg, "Error: Out of memory.\n");
return 1; return 1;
} }
if(pub_or_sub == CLIENT_PUB){ if(pub_or_sub == CLIENT_PUB){
@ -279,8 +278,6 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
snprintf(loc, len, "%s\\mosquitto_rr.conf", env); snprintf(loc, len, "%s\\mosquitto_rr.conf", env);
} }
loc[len-1] = '\0'; loc[len-1] = '\0';
}else{
fprintf(stderr, "Warning: Unable to locate configuration directory, default config not loaded.\n");
} }
#endif #endif
@ -329,7 +326,7 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
return 1; return 1;
} }
if(cfg->password && !cfg->username){ if(cfg->password && !cfg->username){
if(!cfg->quiet) fprintf(stderr, "Warning: Not using password since username not set.\n"); err_printf(cfg, "Warning: Not using password since username not set.\n");
} }
#ifdef WITH_TLS #ifdef WITH_TLS
if((cfg->certfile && !cfg->keyfile) || (cfg->keyfile && !cfg->certfile)){ if((cfg->certfile && !cfg->keyfile) || (cfg->keyfile && !cfg->certfile)){
@ -347,23 +344,23 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
#endif #endif
#ifdef FINAL_WITH_TLS_PSK #ifdef FINAL_WITH_TLS_PSK
if((cfg->cafile || cfg->capath) && cfg->psk){ if((cfg->cafile || cfg->capath) && cfg->psk){
if(!cfg->quiet) fprintf(stderr, "Error: Only one of --psk or --cafile/--capath may be used at once.\n"); fprintf(stderr, "Error: Only one of --psk or --cafile/--capath may be used at once.\n");
return 1; return 1;
} }
if(cfg->psk && !cfg->psk_identity){ if(cfg->psk && !cfg->psk_identity){
if(!cfg->quiet) fprintf(stderr, "Error: --psk-identity required if --psk used.\n"); fprintf(stderr, "Error: --psk-identity required if --psk used.\n");
return 1; return 1;
} }
#endif #endif
if(cfg->clean_session == false && (cfg->id_prefix || !cfg->id)){ if(cfg->clean_session == false && (cfg->id_prefix || !cfg->id)){
if(!cfg->quiet) fprintf(stderr, "Error: You must provide a client id if you are using the -c option.\n"); fprintf(stderr, "Error: You must provide a client id if you are using the -c option.\n");
return 1; return 1;
} }
if(pub_or_sub == CLIENT_SUB){ if(pub_or_sub == CLIENT_SUB){
if(cfg->topic_count == 0){ if(cfg->topic_count == 0){
if(!cfg->quiet) fprintf(stderr, "Error: You must specify a topic to subscribe to.\n"); fprintf(stderr, "Error: You must specify a topic to subscribe to.\n");
return 1; return 1;
} }
} }
@ -371,39 +368,39 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
if(!cfg->host){ if(!cfg->host){
cfg->host = strdup("localhost"); cfg->host = strdup("localhost");
if(!cfg->host){ if(!cfg->host){
if(!cfg->quiet) fprintf(stderr, "Error: Out of memory.\n"); err_printf(cfg, "Error: Out of memory.\n");
return 1; return 1;
} }
} }
rc = mosquitto_property_check_all(CMD_CONNECT, cfg->connect_props); rc = mosquitto_property_check_all(CMD_CONNECT, cfg->connect_props);
if(rc){ if(rc){
if(!cfg->quiet) fprintf(stderr, "Error in CONNECT properties: %s\n", mosquitto_strerror(rc)); err_printf(cfg, "Error in CONNECT properties: %s\n", mosquitto_strerror(rc));
return 1; return 1;
} }
rc = mosquitto_property_check_all(CMD_PUBLISH, cfg->publish_props); rc = mosquitto_property_check_all(CMD_PUBLISH, cfg->publish_props);
if(rc){ if(rc){
if(!cfg->quiet) fprintf(stderr, "Error in PUBLISH properties: %s\n", mosquitto_strerror(rc)); err_printf(cfg, "Error in PUBLISH properties: %s\n", mosquitto_strerror(rc));
return 1; return 1;
} }
rc = mosquitto_property_check_all(CMD_SUBSCRIBE, cfg->subscribe_props); rc = mosquitto_property_check_all(CMD_SUBSCRIBE, cfg->subscribe_props);
if(rc){ if(rc){
if(!cfg->quiet) fprintf(stderr, "Error in SUBSCRIBE properties: %s\n", mosquitto_strerror(rc)); err_printf(cfg, "Error in SUBSCRIBE properties: %s\n", mosquitto_strerror(rc));
return 1; return 1;
} }
rc = mosquitto_property_check_all(CMD_UNSUBSCRIBE, cfg->unsubscribe_props); rc = mosquitto_property_check_all(CMD_UNSUBSCRIBE, cfg->unsubscribe_props);
if(rc){ if(rc){
if(!cfg->quiet) fprintf(stderr, "Error in UNSUBSCRIBE properties: %s\n", mosquitto_strerror(rc)); err_printf(cfg, "Error in UNSUBSCRIBE properties: %s\n", mosquitto_strerror(rc));
return 1; return 1;
} }
rc = mosquitto_property_check_all(CMD_DISCONNECT, cfg->disconnect_props); rc = mosquitto_property_check_all(CMD_DISCONNECT, cfg->disconnect_props);
if(rc){ if(rc){
if(!cfg->quiet) fprintf(stderr, "Error in DISCONNECT properties: %s\n", mosquitto_strerror(rc)); err_printf(cfg, "Error in DISCONNECT properties: %s\n", mosquitto_strerror(rc));
return 1; return 1;
} }
rc = mosquitto_property_check_all(CMD_WILL, cfg->will_props); rc = mosquitto_property_check_all(CMD_WILL, cfg->will_props);
if(rc){ if(rc){
if(!cfg->quiet) fprintf(stderr, "Error in Will properties: %s\n", mosquitto_strerror(rc)); err_printf(cfg, "Error in Will properties: %s\n", mosquitto_strerror(rc));
return 1; return 1;
} }
@ -436,7 +433,7 @@ int cfg_add_topic(struct mosq_config *cfg, int type, char *topic, const char *ar
cfg->topic_count++; cfg->topic_count++;
cfg->topics = realloc(cfg->topics, cfg->topic_count*sizeof(char *)); cfg->topics = realloc(cfg->topics, cfg->topic_count*sizeof(char *));
if(!cfg->topics){ if(!cfg->topics){
fprintf(stderr, "Error: Out of memory.\n"); err_printf(cfg, "Error: Out of memory.\n");
return 1; return 1;
} }
cfg->topics[cfg->topic_count-1] = strdup(topic); cfg->topics[cfg->topic_count-1] = strdup(topic);
@ -551,7 +548,7 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c
cfg->pub_mode = MSGMODE_FILE; cfg->pub_mode = MSGMODE_FILE;
cfg->file_input = strdup(argv[i+1]); cfg->file_input = strdup(argv[i+1]);
if(!cfg->file_input){ if(!cfg->file_input){
fprintf(stderr, "Error: Out of memory.\n"); err_printf(cfg, "Error: Out of memory.\n");
return 1; return 1;
} }
} }
@ -1081,14 +1078,14 @@ int client_opts_set(struct mosquitto *mosq, struct mosq_config *cfg)
cfg->will_payloadlen, cfg->will_payload, cfg->will_qos, cfg->will_payloadlen, cfg->will_payload, cfg->will_qos,
cfg->will_retain, cfg->will_props)){ cfg->will_retain, cfg->will_props)){
if(!cfg->quiet) fprintf(stderr, "Error: Problem setting will.\n"); err_printf(cfg, "Error: Problem setting will.\n");
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
return 1; return 1;
} }
cfg->will_props = NULL; cfg->will_props = NULL;
if(cfg->username && mosquitto_username_pw_set(mosq, cfg->username, cfg->password)){ if(cfg->username && mosquitto_username_pw_set(mosq, cfg->username, cfg->password)){
if(!cfg->quiet) fprintf(stderr, "Error: Problem setting username and password.\n"); err_printf(cfg, "Error: Problem setting username and password.\n");
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
return 1; return 1;
} }
@ -1097,48 +1094,48 @@ int client_opts_set(struct mosquitto *mosq, struct mosq_config *cfg)
rc = mosquitto_tls_set(mosq, cfg->cafile, cfg->capath, cfg->certfile, cfg->keyfile, NULL); rc = mosquitto_tls_set(mosq, cfg->cafile, cfg->capath, cfg->certfile, cfg->keyfile, NULL);
if(rc){ if(rc){
if(rc == MOSQ_ERR_INVAL){ if(rc == MOSQ_ERR_INVAL){
if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS options: File not found.\n"); err_printf(cfg, "Error: Problem setting TLS options: File not found.\n");
}else{ }else{
if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS options: %s.\n", mosquitto_strerror(rc)); err_printf(cfg, "Error: Problem setting TLS options: %s.\n", mosquitto_strerror(rc));
} }
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
return 1; return 1;
} }
} }
if(cfg->insecure && mosquitto_tls_insecure_set(mosq, true)){ if(cfg->insecure && mosquitto_tls_insecure_set(mosq, true)){
if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS insecure option.\n"); err_printf(cfg, "Error: Problem setting TLS insecure option.\n");
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
return 1; return 1;
} }
if(cfg->tls_engine && mosquitto_string_option(mosq, MOSQ_OPT_TLS_ENGINE, cfg->tls_engine)){ if(cfg->tls_engine && mosquitto_string_option(mosq, MOSQ_OPT_TLS_ENGINE, cfg->tls_engine)){
if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS engine, is %s a valid engine?\n", cfg->tls_engine); err_printf(cfg, "Error: Problem setting TLS engine, is %s a valid engine?\n", cfg->tls_engine);
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
return 1; return 1;
} }
if(cfg->keyform && mosquitto_string_option(mosq, MOSQ_OPT_TLS_KEYFORM, cfg->keyform)){ if(cfg->keyform && mosquitto_string_option(mosq, MOSQ_OPT_TLS_KEYFORM, cfg->keyform)){
if(!cfg->quiet) fprintf(stderr, "Error: Problem setting key form, it must be one of 'pem' or 'engine'.\n"); err_printf(cfg, "Error: Problem setting key form, it must be one of 'pem' or 'engine'.\n");
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
return 1; return 1;
} }
if(cfg->tls_engine_kpass_sha1 && mosquitto_string_option(mosq, MOSQ_OPT_TLS_ENGINE_KPASS_SHA1, cfg->tls_engine_kpass_sha1)){ if(cfg->tls_engine_kpass_sha1 && mosquitto_string_option(mosq, MOSQ_OPT_TLS_ENGINE_KPASS_SHA1, cfg->tls_engine_kpass_sha1)){
if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS engine key pass sha, is it a 40 character hex string?\n"); err_printf(cfg, "Error: Problem setting TLS engine key pass sha, is it a 40 character hex string?\n");
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
return 1; return 1;
} }
if(cfg->tls_alpn && mosquitto_string_option(mosq, MOSQ_OPT_TLS_ALPN, cfg->tls_alpn)){ if(cfg->tls_alpn && mosquitto_string_option(mosq, MOSQ_OPT_TLS_ALPN, cfg->tls_alpn)){
if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS ALPN protocol.\n"); err_printf(cfg, "Error: Problem setting TLS ALPN protocol.\n");
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
return 1; return 1;
} }
# ifdef FINAL_WITH_TLS_PSK # ifdef FINAL_WITH_TLS_PSK
if(cfg->psk && mosquitto_tls_psk_set(mosq, cfg->psk, cfg->psk_identity, NULL)){ if(cfg->psk && mosquitto_tls_psk_set(mosq, cfg->psk, cfg->psk_identity, NULL)){
if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS-PSK options.\n"); err_printf(cfg, "Error: Problem setting TLS-PSK options.\n");
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
return 1; return 1;
} }
# endif # endif
if((cfg->tls_version || cfg->ciphers) && 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, check the options are valid.\n"); err_printf(cfg, "Error: Problem setting TLS options, check the options are valid.\n");
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
return 1; return 1;
} }
@ -1161,7 +1158,7 @@ int client_id_generate(struct mosq_config *cfg)
if(cfg->id_prefix){ if(cfg->id_prefix){
cfg->id = malloc(strlen(cfg->id_prefix)+10); cfg->id = malloc(strlen(cfg->id_prefix)+10);
if(!cfg->id){ if(!cfg->id){
if(!cfg->quiet) fprintf(stderr, "Error: Out of memory.\n"); err_printf(cfg, "Error: Out of memory.\n");
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
return 1; return 1;
} }
@ -1207,17 +1204,15 @@ int client_connect(struct mosquitto *mosq, struct mosq_config *cfg)
rc = mosquitto_connect_bind_v5(mosq, cfg->host, port, cfg->keepalive, cfg->bind_address, cfg->connect_props); rc = mosquitto_connect_bind_v5(mosq, cfg->host, port, cfg->keepalive, cfg->bind_address, cfg->connect_props);
#endif #endif
if(rc>0){ if(rc>0){
if(!cfg->quiet){ if(rc == MOSQ_ERR_ERRNO){
if(rc == MOSQ_ERR_ERRNO){
#ifndef WIN32 #ifndef WIN32
err = strerror(errno); err = strerror(errno);
#else #else
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errno, 0, (LPTSTR)&err, 1024, NULL); FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errno, 0, (LPTSTR)&err, 1024, NULL);
#endif #endif
fprintf(stderr, "Error: %s\n", err); err_printf(cfg, "Error: %s\n", err);
}else{ }else{
fprintf(stderr, "Unable to connect (%s).\n", mosquitto_strerror(rc)); err_printf(cfg, "Unable to connect (%s).\n", mosquitto_strerror(rc));
}
} }
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
return rc; return rc;
@ -1284,7 +1279,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
if(!strncmp(url, "socks5h://", strlen("socks5h://"))){ if(!strncmp(url, "socks5h://", strlen("socks5h://"))){
str = url + strlen("socks5h://"); str = url + strlen("socks5h://");
}else{ }else{
fprintf(stderr, "Error: Unsupported proxy protocol: %s\n", url); err_printf(cfg, "Error: Unsupported proxy protocol: %s\n", url);
return 1; return 1;
} }
@ -1311,7 +1306,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
len = i-start; len = i-start;
host = malloc(len + 1); host = malloc(len + 1);
if(!host){ if(!host){
fprintf(stderr, "Error: Out of memory.\n"); err_printf(cfg, "Error: Out of memory.\n");
goto cleanup; goto cleanup;
} }
memcpy(host, &(str[start]), len); memcpy(host, &(str[start]), len);
@ -1324,7 +1319,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
len = i-start; len = i-start;
username_or_host = malloc(len + 1); username_or_host = malloc(len + 1);
if(!username_or_host){ if(!username_or_host){
fprintf(stderr, "Error: Out of memory.\n"); err_printf(cfg, "Error: Out of memory.\n");
goto cleanup; goto cleanup;
} }
memcpy(username_or_host, &(str[start]), len); memcpy(username_or_host, &(str[start]), len);
@ -1344,7 +1339,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
len = i-start; len = i-start;
password = malloc(len + 1); password = malloc(len + 1);
if(!password){ if(!password){
fprintf(stderr, "Error: Out of memory.\n"); err_printf(cfg, "Error: Out of memory.\n");
goto cleanup; goto cleanup;
} }
memcpy(password, &(str[start]), len); memcpy(password, &(str[start]), len);
@ -1360,7 +1355,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
len = i-start; len = i-start;
username = malloc(len + 1); username = malloc(len + 1);
if(!username){ if(!username){
fprintf(stderr, "Error: Out of memory.\n"); err_printf(cfg, "Error: Out of memory.\n");
goto cleanup; goto cleanup;
} }
memcpy(username, &(str[start]), len); memcpy(username, &(str[start]), len);
@ -1378,7 +1373,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
* socks5h://username[:password]@host:port */ * socks5h://username[:password]@host:port */
port = malloc(len + 1); port = malloc(len + 1);
if(!port){ if(!port){
fprintf(stderr, "Error: Out of memory.\n"); err_printf(cfg, "Error: Out of memory.\n");
goto cleanup; goto cleanup;
} }
memcpy(port, &(str[start]), len); memcpy(port, &(str[start]), len);
@ -1390,7 +1385,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
username_or_host = NULL; username_or_host = NULL;
port = malloc(len + 1); port = malloc(len + 1);
if(!port){ if(!port){
fprintf(stderr, "Error: Out of memory.\n"); err_printf(cfg, "Error: Out of memory.\n");
goto cleanup; goto cleanup;
} }
memcpy(port, &(str[start]), len); memcpy(port, &(str[start]), len);
@ -1398,7 +1393,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
}else{ }else{
host = malloc(len + 1); host = malloc(len + 1);
if(!host){ if(!host){
fprintf(stderr, "Error: Out of memory.\n"); err_printf(cfg, "Error: Out of memory.\n");
goto cleanup; goto cleanup;
} }
memcpy(host, &(str[start]), len); memcpy(host, &(str[start]), len);
@ -1407,7 +1402,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
} }
if(!host){ if(!host){
fprintf(stderr, "Error: Invalid proxy.\n"); err_printf(cfg, "Error: Invalid proxy.\n");
goto cleanup; goto cleanup;
} }
@ -1420,7 +1415,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
if(port){ if(port){
port_int = atoi(port); port_int = atoi(port);
if(port_int < 1 || port_int > 65535){ if(port_int < 1 || port_int > 65535){
fprintf(stderr, "Error: Invalid proxy port %d\n", port_int); err_printf(cfg, "Error: Invalid proxy port %d\n", port_int);
goto cleanup; goto cleanup;
} }
free(port); free(port);
@ -1443,4 +1438,15 @@ cleanup:
return 1; return 1;
} }
void err_printf(const struct mosq_config *cfg, const char *fmt, ...)
{
va_list va;
if(cfg->quiet) return;
va_start(va, fmt);
vfprintf(stderr, fmt, va);
va_end(va);
}
#endif #endif

View File

@ -126,4 +126,6 @@ int client_connect(struct mosquitto *mosq, struct mosq_config *cfg);
int cfg_parse_property(struct mosq_config *cfg, int argc, char *argv[], int *idx); int cfg_parse_property(struct mosq_config *cfg, int argc, char *argv[], int *idx);
void err_printf(const struct mosq_config *cfg, const char *fmt, ...);
#endif #endif

View File

@ -142,36 +142,34 @@ void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flag
break; break;
} }
if(rc){ if(rc){
if(!cfg.quiet){ switch(rc){
switch(rc){ case MOSQ_ERR_INVAL:
case MOSQ_ERR_INVAL: err_printf(&cfg, "Error: Invalid input. Does your topic contain '+' or '#'?\n");
fprintf(stderr, "Error: Invalid input. Does your topic contain '+' or '#'?\n"); break;
break; case MOSQ_ERR_NOMEM:
case MOSQ_ERR_NOMEM: err_printf(&cfg, "Error: Out of memory when trying to publish message.\n");
fprintf(stderr, "Error: Out of memory when trying to publish message.\n"); break;
break; case MOSQ_ERR_NO_CONN:
case MOSQ_ERR_NO_CONN: err_printf(&cfg, "Error: Client not connected when trying to publish.\n");
fprintf(stderr, "Error: Client not connected when trying to publish.\n"); break;
break; case MOSQ_ERR_PROTOCOL:
case MOSQ_ERR_PROTOCOL: err_printf(&cfg, "Error: Protocol error when communicating with broker.\n");
fprintf(stderr, "Error: Protocol error when communicating with broker.\n"); break;
break; case MOSQ_ERR_PAYLOAD_SIZE:
case MOSQ_ERR_PAYLOAD_SIZE: err_printf(&cfg, "Error: Message payload is too large.\n");
fprintf(stderr, "Error: Message payload is too large.\n"); break;
break; case MOSQ_ERR_QOS_NOT_SUPPORTED:
case MOSQ_ERR_QOS_NOT_SUPPORTED: err_printf(&cfg, "Error: Message QoS not supported on broker, try a lower QoS.\n");
fprintf(stderr, "Error: Message QoS not supported on broker, try a lower QoS.\n"); break;
break;
}
} }
mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props); mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props);
} }
}else{ }else{
if(result && !cfg.quiet){ if(result){
if(cfg.protocol_version == MQTT_PROTOCOL_V5){ if(cfg.protocol_version == MQTT_PROTOCOL_V5){
fprintf(stderr, "%s\n", mosquitto_reason_string(result)); err_printf(&cfg, "%s\n", mosquitto_reason_string(result));
}else{ }else{
fprintf(stderr, "%s\n", mosquitto_connack_string(result)); err_printf(&cfg, "%s\n", mosquitto_connack_string(result));
} }
} }
} }
@ -185,7 +183,7 @@ void my_publish_callback(struct mosquitto *mosq, void *obj, int mid, int reason_
last_mid_sent = mid; last_mid_sent = mid;
if(reason_code > 127){ if(reason_code > 127){
if(!cfg.quiet) fprintf(stderr, "Warning: Publish %d failed: %s.\n", mid, mosquitto_reason_string(reason_code)); err_printf(&cfg, "Warning: Publish %d failed: %s.\n", mid, mosquitto_reason_string(reason_code));
} }
publish_count++; publish_count++;
@ -208,7 +206,7 @@ int pub_shared_init(void)
{ {
line_buf = malloc(line_buf_len); line_buf = malloc(line_buf_len);
if(!line_buf){ if(!line_buf){
fprintf(stderr, "Error: Out of memory.\n"); err_printf(&cfg, "Error: Out of memory.\n");
return 1; return 1;
} }
return 0; return 0;
@ -246,7 +244,7 @@ int pub_shared_loop(struct mosquitto *mosq)
line_buf[buf_len_actual-1] = '\0'; line_buf[buf_len_actual-1] = '\0';
rc2 = my_publish(mosq, &mid_sent, cfg.topic, buf_len_actual-1, line_buf, cfg.qos, cfg.retain); rc2 = my_publish(mosq, &mid_sent, cfg.topic, buf_len_actual-1, line_buf, cfg.qos, cfg.retain);
if(rc2){ if(rc2){
if(!cfg.quiet) fprintf(stderr, "Error: Publish returned %d, disconnecting.\n", rc2); err_printf(&cfg, "Error: Publish returned %d, disconnecting.\n", rc2);
mosquitto_disconnect_v5(mosq, MQTT_RC_DISCONNECT_WITH_WILL_MSG, cfg.disconnect_props); mosquitto_disconnect_v5(mosq, MQTT_RC_DISCONNECT_WITH_WILL_MSG, cfg.disconnect_props);
} }
break; break;
@ -256,7 +254,7 @@ int pub_shared_loop(struct mosquitto *mosq)
read_len = 1024; read_len = 1024;
buf2 = realloc(line_buf, line_buf_len); buf2 = realloc(line_buf, line_buf_len);
if(!buf2){ if(!buf2){
fprintf(stderr, "Error: Out of memory.\n"); err_printf(&cfg, "Error: Out of memory.\n");
return MOSQ_ERR_NOMEM; return MOSQ_ERR_NOMEM;
} }
line_buf = buf2; line_buf = buf2;
@ -305,7 +303,7 @@ int pub_shared_loop(struct mosquitto *mosq)
break; break;
} }
if(rc){ if(rc){
fprintf(stderr, "Error sending repeat publish: %s", mosquitto_strerror(rc)); err_printf(&cfg, "Error sending repeat publish: %s", mosquitto_strerror(rc));
} }
} }
} }
@ -457,12 +455,12 @@ int main(int argc, char *argv[])
if(cfg.pub_mode == MSGMODE_STDIN_FILE){ if(cfg.pub_mode == MSGMODE_STDIN_FILE){
if(load_stdin()){ if(load_stdin()){
fprintf(stderr, "Error loading input from stdin.\n"); err_printf(&cfg, "Error loading input from stdin.\n");
goto cleanup; goto cleanup;
} }
}else if(cfg.file_input){ }else if(cfg.file_input){
if(load_file(cfg.file_input)){ if(load_file(cfg.file_input)){
fprintf(stderr, "Error loading input file \"%s\".\n", cfg.file_input); err_printf(&cfg, "Error loading input file \"%s\".\n", cfg.file_input);
goto cleanup; goto cleanup;
} }
} }
@ -482,10 +480,10 @@ int main(int argc, char *argv[])
if(!mosq){ if(!mosq){
switch(errno){ switch(errno){
case ENOMEM: case ENOMEM:
if(!cfg.quiet) fprintf(stderr, "Error: Out of memory.\n"); err_printf(&cfg, "Error: Out of memory.\n");
break; break;
case EINVAL: case EINVAL:
if(!cfg.quiet) fprintf(stderr, "Error: Invalid id.\n"); err_printf(&cfg, "Error: Invalid id.\n");
break; break;
} }
goto cleanup; goto cleanup;
@ -517,7 +515,7 @@ int main(int argc, char *argv[])
pub_shared_cleanup(); pub_shared_cleanup();
if(rc){ if(rc){
fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc)); err_printf(&cfg, "Error: %s\n", mosquitto_strerror(rc));
} }
return rc; return rc;

View File

@ -61,7 +61,7 @@ int load_stdin(void)
rlen = fread(buf, 1, 1024, stdin); rlen = fread(buf, 1, 1024, stdin);
aux_message = realloc(cfg.message, pos+rlen); aux_message = realloc(cfg.message, pos+rlen);
if(!aux_message){ if(!aux_message){
if(!cfg.quiet) fprintf(stderr, "Error: Out of memory.\n"); err_printf(&cfg, "Error: Out of memory.\n");
free(cfg.message); free(cfg.message);
return 1; return 1;
} else } else
@ -74,7 +74,7 @@ int load_stdin(void)
cfg.msglen = pos; cfg.msglen = pos;
if(!cfg.msglen){ if(!cfg.msglen){
if(!cfg.quiet) fprintf(stderr, "Error: Zero length input.\n"); err_printf(&cfg, "Error: Zero length input.\n");
return 1; return 1;
} }
@ -88,7 +88,7 @@ int load_file(const char *filename)
fptr = fopen(filename, "rb"); fptr = fopen(filename, "rb");
if(!fptr){ if(!fptr){
if(!cfg.quiet) fprintf(stderr, "Error: Unable to open file \"%s\".\n", filename); err_printf(&cfg, "Error: Unable to open file \"%s\".\n", filename);
return 1; return 1;
} }
cfg.pub_mode = MSGMODE_FILE; cfg.pub_mode = MSGMODE_FILE;
@ -96,22 +96,22 @@ int load_file(const char *filename)
cfg.msglen = ftell(fptr); cfg.msglen = ftell(fptr);
if(cfg.msglen > 268435455){ if(cfg.msglen > 268435455){
fclose(fptr); fclose(fptr);
if(!cfg.quiet) fprintf(stderr, "Error: File \"%s\" is too large (>268,435,455 bytes).\n", filename); err_printf(&cfg, "Error: File \"%s\" is too large (>268,435,455 bytes).\n", filename);
return 1; return 1;
}else if(cfg.msglen == 0){ }else if(cfg.msglen == 0){
fclose(fptr); fclose(fptr);
if(!cfg.quiet) fprintf(stderr, "Error: File \"%s\" is empty.\n", filename); err_printf(&cfg, "Error: File \"%s\" is empty.\n", filename);
return 1; return 1;
}else if(cfg.msglen < 0){ }else if(cfg.msglen < 0){
fclose(fptr); fclose(fptr);
if(!cfg.quiet) fprintf(stderr, "Error: Unable to determine size of file \"%s\".\n", filename); err_printf(&cfg, "Error: Unable to determine size of file \"%s\".\n", filename);
return 1; return 1;
} }
fseek(fptr, 0, SEEK_SET); fseek(fptr, 0, SEEK_SET);
cfg.message = malloc(cfg.msglen); cfg.message = malloc(cfg.msglen);
if(!cfg.message){ if(!cfg.message){
fclose(fptr); fclose(fptr);
if(!cfg.quiet) fprintf(stderr, "Error: Out of memory.\n"); err_printf(&cfg, "Error: Out of memory.\n");
return 1; return 1;
} }
pos = 0; pos = 0;

View File

@ -121,8 +121,8 @@ void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flag
mosquitto_subscribe_v5(mosq, NULL, cfg.response_topic, cfg.qos, 0, cfg.subscribe_props); mosquitto_subscribe_v5(mosq, NULL, cfg.response_topic, cfg.qos, 0, cfg.subscribe_props);
}else{ }else{
client_state = rr_s_disconnect; client_state = rr_s_disconnect;
if(result && !cfg.quiet){ if(result){
fprintf(stderr, "%s\n", mosquitto_connack_string(result)); err_printf(&cfg, "%s\n", mosquitto_connack_string(result));
} }
mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props); mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props);
} }
@ -135,10 +135,8 @@ void my_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_c
client_state = rr_s_ready_to_publish; client_state = rr_s_ready_to_publish;
}else{ }else{
client_state = rr_s_disconnect; client_state = rr_s_disconnect;
if(!cfg.quiet){ err_printf(&cfg, "%s\n", mosquitto_reason_string(granted_qos[0]));
fprintf(stderr, "%s\n", mosquitto_reason_string(granted_qos[0])); mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props);
mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props);
}
} }
} }
@ -282,7 +280,7 @@ int main(int argc, char *argv[])
} }
rc = mosquitto_property_check_all(CMD_PUBLISH, cfg.publish_props); rc = mosquitto_property_check_all(CMD_PUBLISH, cfg.publish_props);
if(rc){ if(rc){
if(!cfg.quiet) fprintf(stderr, "Error in PUBLISH properties: Duplicate response topic.\n"); err_printf(&cfg, "Error in PUBLISH properties: Duplicate response topic.\n");
goto cleanup; goto cleanup;
} }
@ -294,10 +292,10 @@ int main(int argc, char *argv[])
if(!mosq){ if(!mosq){
switch(errno){ switch(errno){
case ENOMEM: case ENOMEM:
if(!cfg.quiet) fprintf(stderr, "Error: Out of memory.\n"); err_printf(&cfg, "Error: Out of memory.\n");
break; break;
case EINVAL: case EINVAL:
if(!cfg.quiet) fprintf(stderr, "Error: Invalid id and/or clean_session.\n"); err_printf(&cfg, "Error: Invalid id and/or clean_session.\n");
break; break;
} }
goto cleanup; goto cleanup;

View File

@ -35,7 +35,7 @@ Contributors:
#include <mqtt_protocol.h> #include <mqtt_protocol.h>
#include "client_shared.h" #include "client_shared.h"
static struct mosq_config cfg; struct mosq_config cfg;
bool process_messages = true; bool process_messages = true;
int msg_count = 0; int msg_count = 0;
struct mosquitto *mosq = NULL; struct mosquitto *mosq = NULL;
@ -124,11 +124,11 @@ void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flag
mosquitto_unsubscribe_v5(mosq, NULL, cfg.unsub_topics[i], cfg.unsubscribe_props); mosquitto_unsubscribe_v5(mosq, NULL, cfg.unsub_topics[i], cfg.unsubscribe_props);
} }
}else{ }else{
if(result && !cfg.quiet){ if(result){
if(cfg.protocol_version == MQTT_PROTOCOL_V5){ if(cfg.protocol_version == MQTT_PROTOCOL_V5){
fprintf(stderr, "%s\n", mosquitto_reason_string(result)); err_printf(&cfg, "%s\n", mosquitto_reason_string(result));
}else{ }else{
fprintf(stderr, "%s\n", mosquitto_connack_string(result)); err_printf(&cfg, "%s\n", mosquitto_connack_string(result));
} }
} }
mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props); mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props);
@ -307,10 +307,10 @@ int main(int argc, char *argv[])
if(!mosq){ if(!mosq){
switch(errno){ switch(errno){
case ENOMEM: case ENOMEM:
if(!cfg.quiet) fprintf(stderr, "Error: Out of memory.\n"); err_printf(&cfg, "Error: Out of memory.\n");
break; break;
case EINVAL: case EINVAL:
if(!cfg.quiet) fprintf(stderr, "Error: Invalid id and/or clean_session.\n"); err_printf(&cfg, "Error: Invalid id and/or clean_session.\n");
break; break;
} }
goto cleanup; goto cleanup;

View File

@ -37,6 +37,7 @@ Contributors:
#include <mosquitto.h> #include <mosquitto.h>
#include "client_shared.h" #include "client_shared.h"
extern struct mosq_config cfg;
static int get_time(struct tm **ti, long *ns) static int get_time(struct tm **ti, long *ns)
{ {
@ -60,7 +61,7 @@ static int get_time(struct tm **ti, long *ns)
*ns = tv.tv_usec*1000; *ns = tv.tv_usec*1000;
#else #else
if(clock_gettime(CLOCK_REALTIME, &ts) != 0){ if(clock_gettime(CLOCK_REALTIME, &ts) != 0){
fprintf(stderr, "Error obtaining system time.\n"); err_printf(&cfg, "Error obtaining system time.\n");
return 1; return 1;
} }
s = ts.tv_sec; s = ts.tv_sec;
@ -69,7 +70,7 @@ static int get_time(struct tm **ti, long *ns)
*ti = localtime(&s); *ti = localtime(&s);
if(!(*ti)){ if(!(*ti)){
fprintf(stderr, "Error obtaining system time.\n"); err_printf(&cfg, "Error obtaining system time.\n");
return 1; return 1;
} }
@ -130,7 +131,7 @@ static void json_print(const struct mosquitto_message *message, const struct tm
} }
static void formatted_print(const struct mosq_config *cfg, const struct mosquitto_message *message) static void formatted_print(const struct mosq_config *lcfg, const struct mosquitto_message *message)
{ {
int len; int len;
int i; int i;
@ -139,13 +140,13 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt
char strf[3]; char strf[3];
char buf[100]; char buf[100];
len = strlen(cfg->format); len = strlen(lcfg->format);
for(i=0; i<len; i++){ for(i=0; i<len; i++){
if(cfg->format[i] == '%'){ if(lcfg->format[i] == '%'){
if(i < len-1){ if(i < len-1){
i++; i++;
switch(cfg->format[i]){ switch(lcfg->format[i]){
case '%': case '%':
fputc('%', stdout); fputc('%', stdout);
break; break;
@ -153,7 +154,7 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt
case 'I': case 'I':
if(!ti){ if(!ti){
if(get_time(&ti, &ns)){ if(get_time(&ti, &ns)){
fprintf(stderr, "Error obtaining system time.\n"); err_printf(lcfg, "Error obtaining system time.\n");
return; return;
} }
} }
@ -165,7 +166,7 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt
case 'j': case 'j':
if(!ti){ if(!ti){
if(get_time(&ti, &ns)){ if(get_time(&ti, &ns)){
fprintf(stderr, "Error obtaining system time.\n"); err_printf(lcfg, "Error obtaining system time.\n");
return; return;
} }
} }
@ -175,7 +176,7 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt
case 'J': case 'J':
if(!ti){ if(!ti){
if(get_time(&ti, &ns)){ if(get_time(&ti, &ns)){
fprintf(stderr, "Error obtaining system time.\n"); err_printf(lcfg, "Error obtaining system time.\n");
return; return;
} }
} }
@ -213,7 +214,7 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt
case 'U': case 'U':
if(!ti){ if(!ti){
if(get_time(&ti, &ns)){ if(get_time(&ti, &ns)){
fprintf(stderr, "Error obtaining system time.\n"); err_printf(lcfg, "Error obtaining system time.\n");
return; return;
} }
} }
@ -231,24 +232,24 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt
break; break;
} }
} }
}else if(cfg->format[i] == '@'){ }else if(lcfg->format[i] == '@'){
if(i < len-1){ if(i < len-1){
i++; i++;
if(cfg->format[i] == '@'){ if(lcfg->format[i] == '@'){
fputc('@', stdout); fputc('@', stdout);
}else{ }else{
if(!ti){ if(!ti){
if(get_time(&ti, &ns)){ if(get_time(&ti, &ns)){
fprintf(stderr, "Error obtaining system time.\n"); err_printf(lcfg, "Error obtaining system time.\n");
return; return;
} }
} }
strf[0] = '%'; strf[0] = '%';
strf[1] = cfg->format[i]; strf[1] = lcfg->format[i];
strf[2] = 0; strf[2] = 0;
if(cfg->format[i] == 'N'){ if(lcfg->format[i] == 'N'){
printf("%09ld", ns); printf("%09ld", ns);
}else{ }else{
if(strftime(buf, 100, strf, ti) != 0){ if(strftime(buf, 100, strf, ti) != 0){
@ -257,10 +258,10 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt
} }
} }
} }
}else if(cfg->format[i] == '\\'){ }else if(lcfg->format[i] == '\\'){
if(i < len-1){ if(i < len-1){
i++; i++;
switch(cfg->format[i]){ switch(lcfg->format[i]){
case '\\': case '\\':
fputc('\\', stdout); fputc('\\', stdout);
break; break;
@ -295,10 +296,10 @@ static void formatted_print(const struct mosq_config *cfg, const struct mosquitt
} }
} }
}else{ }else{
fputc(cfg->format[i], stdout); fputc(lcfg->format[i], stdout);
} }
} }
if(cfg->eol){ if(lcfg->eol){
fputc('\n', stdout); fputc('\n', stdout);
} }
fflush(stdout); fflush(stdout);