aboutsummaryrefslogtreecommitdiff
path: root/client_shared.c
diff options
context:
space:
mode:
authorV.Krishn <vkrishn4@gmail.com>2019-12-04 11:46:44 +0530
committerV.Krishn <vkrishn4@gmail.com>2019-12-04 11:46:44 +0530
commitc85e24e701bc5d818f8b1066ca1cb983256f2921 (patch)
treec37891ad05b99beb3b62fd6df84f32698a98a208 /client_shared.c
parent0c40d0ef3546e3210df9deab7454e81a694c93f5 (diff)
downloadmqtt-dirpub-c85e24e701bc5d818f8b1066ca1cb983256f2921.tar.bz2
update to mosquitto-v1.6.3
Diffstat (limited to 'client_shared.c')
-rw-r--r--client_shared.c115
1 files changed, 61 insertions, 54 deletions
diff --git a/client_shared.c b/client_shared.c
index 4f47937..8923f40 100644
--- a/client_shared.c
+++ b/client_shared.c
@@ -20,6 +20,7 @@ Contributors:
#include <errno.h>
#include <fcntl.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -237,7 +238,7 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
len = strlen(env) + strlen("/mosquitto_pub") + 1;
loc = malloc(len);
if(!loc){
- fprintf(stderr, "Error: Out of memory.\n");
+ err_printf(cfg, "Error: Out of memory.\n");
return 1;
}
if(pub_or_sub == CLIENT_PUB){
@@ -254,7 +255,7 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
len = strlen(env) + strlen("/.config/mosquitto_pub") + 1;
loc = malloc(len);
if(!loc){
- fprintf(stderr, "Error: Out of memory.\n");
+ err_printf(cfg, "Error: Out of memory.\n");
return 1;
}
if(pub_or_sub == CLIENT_PUB){
@@ -265,8 +266,6 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
snprintf(loc, len, "%s/.config/mosquitto_rr", env);
}
loc[len-1] = '\0';
- }else{
- fprintf(stderr, "Warning: Unable to locate configuration directory, default config not loaded.\n");
}
}
@@ -276,7 +275,7 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
len = strlen(env) + strlen("\\mosquitto_pub.conf") + 1;
loc = malloc(len);
if(!loc){
- fprintf(stderr, "Error: Out of memory.\n");
+ err_printf(cfg, "Error: Out of memory.\n");
return 1;
}
if(pub_or_sub == CLIENT_PUB){
@@ -287,8 +286,6 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
snprintf(loc, len, "%s\\mosquitto_rr.conf", env);
}
loc[len-1] = '\0';
- }else{
- fprintf(stderr, "Warning: Unable to locate configuration directory, default config not loaded.\n");
}
#endif
@@ -336,9 +333,6 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
fprintf(stderr, "Error: Will retain given, but no will topic given.\n");
return 1;
}
- if(cfg->password && !cfg->username){
- if(!cfg->quiet) fprintf(stderr, "Warning: Not using password since username not set.\n");
- }
#ifdef WITH_TLS
if((cfg->certfile && !cfg->keyfile) || (cfg->keyfile && !cfg->certfile)){
fprintf(stderr, "Error: Both certfile and keyfile must be provided if one of them is set.\n");
@@ -355,23 +349,23 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
#endif
#ifdef FINAL_WITH_TLS_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;
}
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;
}
#endif
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;
}
if(pub_or_sub == CLIENT_SUB){
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;
}
}
@@ -379,39 +373,39 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
if(!cfg->host){
cfg->host = strdup("localhost");
if(!cfg->host){
- if(!cfg->quiet) fprintf(stderr, "Error: Out of memory.\n");
+ err_printf(cfg, "Error: Out of memory.\n");
return 1;
}
}
rc = mosquitto_property_check_all(CMD_CONNECT, cfg->connect_props);
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;
}
rc = mosquitto_property_check_all(CMD_PUBLISH, cfg->publish_props);
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;
}
rc = mosquitto_property_check_all(CMD_SUBSCRIBE, cfg->subscribe_props);
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;
}
rc = mosquitto_property_check_all(CMD_UNSUBSCRIBE, cfg->unsubscribe_props);
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;
}
rc = mosquitto_property_check_all(CMD_DISCONNECT, cfg->disconnect_props);
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;
}
rc = mosquitto_property_check_all(CMD_WILL, cfg->will_props);
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;
}
@@ -580,7 +574,7 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c
cfg->pub_mode = MSGMODE_FILE;
cfg->file_input = strdup(argv[i+1]);
if(!cfg->file_input){
- fprintf(stderr, "Error: Out of memory.\n");
+ err_printf(cfg, "Error: Out of memory.\n");
return 1;
}
}
@@ -691,6 +685,10 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c
return 1;
}
topic = strchr(url, '/');
+ if(!topic){
+ fprintf(stderr, "Error: Invalid URL for -L argument specified - topic missing.\n");
+ return 1;
+ }
*topic++ = 0;
if(cfg_add_topic(cfg, pub_or_sub, topic, "-L topic"))
@@ -1106,14 +1104,14 @@ int client_opts_set(struct mosquitto *mosq, struct mosq_config *cfg)
cfg->will_payloadlen, cfg->will_payload, cfg->will_qos,
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();
return 1;
}
cfg->will_props = NULL;
- if(cfg->username && mosquitto_username_pw_set(mosq, cfg->username, cfg->password)){
- if(!cfg->quiet) fprintf(stderr, "Error: Problem setting username and password.\n");
+ if((cfg->username || cfg->password) && mosquitto_username_pw_set(mosq, cfg->username, cfg->password)){
+ err_printf(cfg, "Error: Problem setting username and/or password.\n");
mosquitto_lib_cleanup();
return 1;
}
@@ -1122,48 +1120,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);
if(rc){
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{
- 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();
return 1;
}
}
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();
return 1;
}
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();
return 1;
}
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();
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->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();
return 1;
}
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();
return 1;
}
# ifdef FINAL_WITH_TLS_PSK
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();
return 1;
}
# endif
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();
return 1;
}
@@ -1186,7 +1184,7 @@ int client_id_generate(struct mosq_config *cfg)
if(cfg->id_prefix){
cfg->id = malloc(strlen(cfg->id_prefix)+10);
if(!cfg->id){
- if(!cfg->quiet) fprintf(stderr, "Error: Out of memory.\n");
+ err_printf(cfg, "Error: Out of memory.\n");
mosquitto_lib_cleanup();
return 1;
}
@@ -1232,17 +1230,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);
#endif
if(rc>0){
- if(!cfg->quiet){
- if(rc == MOSQ_ERR_ERRNO){
+ if(rc == MOSQ_ERR_ERRNO){
#ifndef WIN32
- err = strerror(errno);
+ err = strerror(errno);
#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
- fprintf(stderr, "Error: %s\n", err);
- }else{
- fprintf(stderr, "Unable to connect (%s).\n", mosquitto_strerror(rc));
- }
+ err_printf(cfg, "Error: %s\n", err);
+ }else{
+ err_printf(cfg, "Unable to connect (%s).\n", mosquitto_strerror(rc));
}
mosquitto_lib_cleanup();
return rc;
@@ -1309,7 +1305,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
if(!strncmp(url, "socks5h://", strlen("socks5h://"))){
str = url + strlen("socks5h://");
}else{
- fprintf(stderr, "Error: Unsupported proxy protocol: %s\n", url);
+ err_printf(cfg, "Error: Unsupported proxy protocol: %s\n", url);
return 1;
}
@@ -1336,7 +1332,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
len = i-start;
host = malloc(len + 1);
if(!host){
- fprintf(stderr, "Error: Out of memory.\n");
+ err_printf(cfg, "Error: Out of memory.\n");
goto cleanup;
}
memcpy(host, &(str[start]), len);
@@ -1349,7 +1345,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
len = i-start;
username_or_host = malloc(len + 1);
if(!username_or_host){
- fprintf(stderr, "Error: Out of memory.\n");
+ err_printf(cfg, "Error: Out of memory.\n");
goto cleanup;
}
memcpy(username_or_host, &(str[start]), len);
@@ -1369,7 +1365,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
len = i-start;
password = malloc(len + 1);
if(!password){
- fprintf(stderr, "Error: Out of memory.\n");
+ err_printf(cfg, "Error: Out of memory.\n");
goto cleanup;
}
memcpy(password, &(str[start]), len);
@@ -1385,7 +1381,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
len = i-start;
username = malloc(len + 1);
if(!username){
- fprintf(stderr, "Error: Out of memory.\n");
+ err_printf(cfg, "Error: Out of memory.\n");
goto cleanup;
}
memcpy(username, &(str[start]), len);
@@ -1403,7 +1399,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
* socks5h://username[:password]@host:port */
port = malloc(len + 1);
if(!port){
- fprintf(stderr, "Error: Out of memory.\n");
+ err_printf(cfg, "Error: Out of memory.\n");
goto cleanup;
}
memcpy(port, &(str[start]), len);
@@ -1415,7 +1411,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
username_or_host = NULL;
port = malloc(len + 1);
if(!port){
- fprintf(stderr, "Error: Out of memory.\n");
+ err_printf(cfg, "Error: Out of memory.\n");
goto cleanup;
}
memcpy(port, &(str[start]), len);
@@ -1423,7 +1419,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
}else{
host = malloc(len + 1);
if(!host){
- fprintf(stderr, "Error: Out of memory.\n");
+ err_printf(cfg, "Error: Out of memory.\n");
goto cleanup;
}
memcpy(host, &(str[start]), len);
@@ -1432,7 +1428,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
}
if(!host){
- fprintf(stderr, "Error: Invalid proxy.\n");
+ err_printf(cfg, "Error: Invalid proxy.\n");
goto cleanup;
}
@@ -1445,7 +1441,7 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url)
if(port){
port_int = atoi(port);
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;
}
free(port);
@@ -1467,5 +1463,16 @@ cleanup:
if(port) free(port);
return 1;
}
-
#endif
+
+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);
+}
+