From 4d6384c7583b42bb38aa2274bc01875aa2cfd86f Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 5 Nov 2020 10:23:01 +0000 Subject: [PATCH] Build and conversion fixes for build variants. --- apps/mosquitto_ctrl/Makefile | 8 +++++ lib/srv_mosq.c | 6 +++- .../mosquitto_payload_modification.c | 6 ++-- src/bridge.c | 2 +- src/conf.c | 7 ++++- src/logging.c | 6 ++-- src/mosquitto.c | 4 +++ src/mosquitto_broker_internal.h | 2 +- src/mux_epoll.c | 4 +-- src/mux_poll.c | 6 ++-- src/security_default.c | 2 ++ src/sys_tree.c | 1 + src/websockets.c | 31 ++++++++++++------- 13 files changed, 58 insertions(+), 27 deletions(-) diff --git a/apps/mosquitto_ctrl/Makefile b/apps/mosquitto_ctrl/Makefile index 08e2b3db..f0723264 100644 --- a/apps/mosquitto_ctrl/Makefile +++ b/apps/mosquitto_ctrl/Makefile @@ -26,10 +26,18 @@ EXAMPLE_OBJS= example.o ifeq ($(WITH_TLS),yes) ifeq ($(WITH_CJSON),yes) + +ifeq ($(WITH_SHARED_LIBRARIES),yes) +TARGET:=mosquitto_ctrl mosquitto_ctrl_example.so +else +ifeq ($(WITH_STATIC_LIBRARIES),yes) TARGET:=mosquitto_ctrl mosquitto_ctrl_example.so endif endif +endif +endif + all : $(TARGET) mosquitto_ctrl : ${OBJS} diff --git a/lib/srv_mosq.c b/lib/srv_mosq.c index a73e82c6..fd50d464 100644 --- a/lib/srv_mosq.c +++ b/lib/srv_mosq.c @@ -67,6 +67,10 @@ int mosquitto_connect_srv(struct mosquitto *mosq, const char *host, int keepaliv int rc; if(!mosq) return MOSQ_ERR_INVAL; + if(keepalive < 0 || keepalive > UINT16_MAX){ + return MOSQ_ERR_INVAL; + } + rc = ares_init(&mosq->achan); if(rc != ARES_SUCCESS){ return MOSQ_ERR_UNKNOWN; @@ -94,7 +98,7 @@ int mosquitto_connect_srv(struct mosquitto *mosq, const char *host, int keepaliv mosquitto__set_state(mosq, mosq_cs_connect_srv); - mosq->keepalive = keepalive; + mosq->keepalive = (uint16_t)keepalive; return MOSQ_ERR_SUCCESS; diff --git a/plugins/payload-modification/mosquitto_payload_modification.c b/plugins/payload-modification/mosquitto_payload_modification.c index fc7811c8..e46b9745 100644 --- a/plugins/payload-modification/mosquitto_payload_modification.c +++ b/plugins/payload-modification/mosquitto_payload_modification.c @@ -46,13 +46,13 @@ static int callback_message(int event, void *event_data, void *userdata) { struct mosquitto_evt_message *ed = event_data; char *new_payload; - int new_payloadlen; + uint32_t new_payloadlen; /* This simply adds "hello " to the front of every payload. You can of * course do much more complicated message processing if needed. */ /* Calculate the length of our new payload */ - new_payloadlen = ed->payloadlen + strlen("hello ")+1; + new_payloadlen = ed->payloadlen + (uint32_t)strlen("hello ")+1; /* Allocate some memory - use * mosquitto_calloc/mosquitto_malloc/mosquitto_strdup when allocating, to @@ -64,7 +64,7 @@ static int callback_message(int event, void *event_data, void *userdata) /* Print "hello " to the payload */ snprintf(new_payload, new_payloadlen, "hello "); - memcpy(new_payload+strlen("hello "), ed->payload, ed->payloadlen); + memcpy(new_payload+(uint32_t)strlen("hello "), ed->payload, ed->payloadlen); /* Assign the new payload and payloadlen to the event data structure. You * must *not* free the original payload, it will be handled by the diff --git a/src/bridge.c b/src/bridge.c index 0df4f8b2..6496b6fa 100644 --- a/src/bridge.c +++ b/src/bridge.c @@ -147,7 +147,7 @@ int bridge__connect_step1(struct mosquitto_db *db, struct mosquitto *context) { int rc; char *notification_topic; - int notification_topic_len; + size_t notification_topic_len; uint8_t notification_payload; int i; diff --git a/src/conf.c b/src/conf.c index cb292c75..8ade727f 100644 --- a/src/conf.c +++ b/src/conf.c @@ -2151,7 +2151,12 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct }else if(!strcmp(token, "websockets_headers_size")){ #ifdef WITH_WEBSOCKETS # if defined(LWS_LIBRARY_VERSION_NUMBER) && LWS_LIBRARY_VERSION_NUMBER>=1007000 - if(conf__parse_int(&token, "websockets_headers_size", &config->websockets_headers_size, saveptr)) return MOSQ_ERR_INVAL; + if(conf__parse_int(&token, "websockets_headers_size", &tmp_int, saveptr)) return MOSQ_ERR_INVAL; + if(tmp_int < 0 || tmp_int > UINT16_MAX){ + log__printf(NULL, MOSQ_LOG_WARNING, "Error: Websockets headers size must be between 0 and 65535 inclusive."); + return MOSQ_ERR_INVAL; + } + config->websockets_headers_size = (uint16_t)tmp_int; # else log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Websockets headers size require libwebsocket 1.7+"); # endif diff --git a/src/logging.c b/src/logging.c index c3f2f47c..3c26d8fe 100644 --- a/src/logging.c +++ b/src/logging.c @@ -207,7 +207,7 @@ int log__vprintf(unsigned int priority, const char *fmt, va_list va) int syslog_priority; time_t now = time(NULL); char log_line[1000]; - int log_line_pos; + size_t log_line_pos; #ifdef WIN32 char *sp; #endif @@ -303,10 +303,10 @@ int log__vprintf(unsigned int priority, const char *fmt, va_list va) get_time(&ti); log_line_pos = strftime(log_line, sizeof(log_line), log_timestamp_format, ti); if(log_line_pos == 0){ - log_line_pos = snprintf(log_line, sizeof(log_line), "Time error"); + log_line_pos = (size_t)snprintf(log_line, sizeof(log_line), "Time error"); } }else{ - log_line_pos = snprintf(log_line, sizeof(log_line), "%d", (int)now); + log_line_pos = (size_t)snprintf(log_line, sizeof(log_line), "%d", (int)now); } if(log_line_pos < sizeof(log_line)-3){ log_line[log_line_pos] = ':'; diff --git a/src/mosquitto.c b/src/mosquitto.c index 007a868e..427bd715 100644 --- a/src/mosquitto.c +++ b/src/mosquitto.c @@ -219,6 +219,7 @@ void listener__set_defaults(struct mosquitto__listener *listener) void listeners__reload_all_certificates(struct mosquitto_db *db) { +#ifdef WITH_TLS int i; int rc; struct mosquitto__listener *listener; @@ -233,6 +234,7 @@ void listeners__reload_all_certificates(struct mosquitto_db *db) } } } +#endif } @@ -435,7 +437,9 @@ int main(int argc, char *argv[]) mosq_sock_t *listensock = NULL; int listensock_count = 0; struct mosquitto__config config; +#ifdef WITH_BRIDGE int i; +#endif int rc; #ifdef WIN32 SYSTEMTIME st; diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index 408e48fa..d2c318c9 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -347,7 +347,7 @@ struct mosquitto__config { char *user; #ifdef WITH_WEBSOCKETS int websockets_log_level; - int websockets_headers_size; + uint16_t websockets_headers_size; bool have_websockets_listener; #endif #ifdef WITH_BRIDGE diff --git a/src/mux_epoll.c b/src/mux_epoll.c index 785beecd..17a3cb23 100644 --- a/src/mux_epoll.c +++ b/src/mux_epoll.c @@ -252,8 +252,8 @@ static void loop_handle_reads_writes(struct mosquitto_db *db, mosq_sock_t sock, if(context->wsi){ struct lws_pollfd wspoll; wspoll.fd = context->sock; - wspoll.events = context->events; - wspoll.revents = events; + wspoll.events = (int16_t)context->events; + wspoll.revents = (int16_t)events; #ifdef LWS_LIBRARY_VERSION_NUMBER lws_service_fd(lws_get_context(context->wsi), &wspoll); #else diff --git a/src/mux_poll.c b/src/mux_poll.c index 25b61bed..cf0ff4f6 100644 --- a/src/mux_poll.c +++ b/src/mux_poll.c @@ -58,7 +58,7 @@ Contributors: static void loop_handle_reads_writes(struct mosquitto_db *db, struct pollfd *pollfds); static struct pollfd *pollfds = NULL; -static int pollfd_max; +static size_t pollfd_max; #ifndef WIN32 static sigset_t my_sigblock; #endif @@ -78,9 +78,9 @@ int mux_poll__init(struct mosquitto_db *db, mosq_sock_t *listensock, int listens #endif #ifdef WIN32 - pollfd_max = _getmaxstdio(); + pollfd_max = (size_t)_getmaxstdio(); #else - pollfd_max = sysconf(_SC_OPEN_MAX); + pollfd_max = (size_t)sysconf(_SC_OPEN_MAX); #endif pollfds = mosquitto__malloc(sizeof(struct pollfd)*pollfd_max); diff --git a/src/security_default.c b/src/security_default.c index 5a71a1b8..648424fb 100644 --- a/src/security_default.c +++ b/src/security_default.c @@ -820,7 +820,9 @@ void unpwd__free_item(struct mosquitto__unpwd **unpwd, struct mosquitto__unpwd * { mosquitto__free(item->username); mosquitto__free(item->password); +#ifdef WITH_TLS mosquitto__free(item->salt); +#endif HASH_DEL(*unpwd, item); mosquitto__free(item); } diff --git a/src/sys_tree.c b/src/sys_tree.c index 33eae9de..03c96bd1 100644 --- a/src/sys_tree.c +++ b/src/sys_tree.c @@ -20,6 +20,7 @@ Contributors: #include #include +#include #include "mosquitto_broker_internal.h" #include "memory_mosq.h" diff --git a/src/websockets.c b/src/websockets.c index 8fd7123a..35d81054 100644 --- a/src/websockets.c +++ b/src/websockets.c @@ -189,6 +189,7 @@ static int callback_mqtt(struct libwebsocket_context *context, struct mosquitto__packet *packet; size_t txlen; int count; + unsigned int ucount; const struct libwebsocket_protocols *p; struct libws_mqtt_data *u = (struct libws_mqtt_data *)user; size_t pos; @@ -315,11 +316,12 @@ static int callback_mqtt(struct libwebsocket_context *context, } return 0; } + ucount = (unsigned int)count; #ifdef WITH_SYS_TREE - g_bytes_sent += count; + g_bytes_sent += ucount; #endif - packet->to_process -= count; - packet->pos += count; + packet->to_process -= ucount; + packet->pos += ucount; if(packet->to_process > 0){ if (mosq->state == mosq_cs_disconnect_ws || mosq->state == mosq_cs_disconnecting @@ -398,7 +400,7 @@ static int callback_mqtt(struct libwebsocket_context *context, mosq->in_packet.remaining_length += (byte & 127) * mosq->in_packet.remaining_mult; mosq->in_packet.remaining_mult *= 128; }while((byte & 128) != 0); - mosq->in_packet.remaining_count *= -1; + mosq->in_packet.remaining_count = (int8_t)(mosq->in_packet.remaining_count -1); if(mosq->in_packet.remaining_length > 0){ mosq->in_packet.payload = mosquitto__malloc(mosq->in_packet.remaining_length*sizeof(uint8_t)); @@ -409,15 +411,15 @@ static int callback_mqtt(struct libwebsocket_context *context, } } if(mosq->in_packet.to_process>0){ - if(len - pos >= mosq->in_packet.to_process){ + if((uint32_t)len - pos >= mosq->in_packet.to_process){ memcpy(&mosq->in_packet.payload[mosq->in_packet.pos], &buf[pos], mosq->in_packet.to_process); mosq->in_packet.pos += mosq->in_packet.to_process; pos += mosq->in_packet.to_process; mosq->in_packet.to_process = 0; }else{ memcpy(&mosq->in_packet.payload[mosq->in_packet.pos], &buf[pos], len-pos); - mosq->in_packet.pos += len-pos; - mosq->in_packet.to_process -= len-pos; + mosq->in_packet.pos += (uint32_t)(len-pos); + mosq->in_packet.to_process -= (uint32_t)(len-pos); return 0; } } @@ -537,6 +539,7 @@ static int callback_http(struct libwebsocket_context *context, char *http_dir; size_t buflen; size_t wlen; + int rc; char *filename_canonical; unsigned char buf[4096]; struct stat filestat; @@ -601,7 +604,7 @@ static int callback_http(struct libwebsocket_context *context, free(filename_canonical); /* FIXME - use header functions from lws 2.x */ - buflen = snprintf((char *)buf, 4096, "HTTP/1.0 302 OK\r\n" + buflen = (size_t)snprintf((char *)buf, 4096, "HTTP/1.0 302 OK\r\n" "Location: %s/\r\n\r\n", (char *)in); return libwebsocket_write(wsi, buf, buflen, LWS_WRITE_HTTP); @@ -618,7 +621,7 @@ static int callback_http(struct libwebsocket_context *context, log__printf(NULL, MOSQ_LOG_DEBUG, "http serving file \"%s\".", filename_canonical); free(filename_canonical); /* FIXME - use header functions from lws 2.x */ - buflen = snprintf((char *)buf, 4096, "HTTP/1.0 200 OK\r\n" + buflen = (size_t)snprintf((char *)buf, 4096, "HTTP/1.0 200 OK\r\n" "Server: mosquitto\r\n" "Content-Length: %u\r\n\r\n", (unsigned int)filestat.st_size); @@ -652,9 +655,13 @@ static int callback_http(struct libwebsocket_context *context, u->fptr = NULL; return -1; } - wlen = libwebsocket_write(wsi, buf, buflen, LWS_WRITE_HTTP); + rc = libwebsocket_write(wsi, buf, buflen, LWS_WRITE_HTTP); + if(rc < 0){ + return -1; + } + wlen = (size_t)rc; if(wlen < buflen){ - if(fseek(u->fptr, buflen-wlen, SEEK_CUR) < 0){ + if(fseek(u->fptr, (long)(buflen-wlen), SEEK_CUR) < 0){ fclose(u->fptr); u->fptr = NULL; return -1; @@ -721,7 +728,7 @@ struct libwebsocket_context *mosq_websockets_init(struct mosquitto__listener *li { struct lws_context_creation_info info; struct libwebsocket_protocols *p; - int protocol_count; + size_t protocol_count; int i; struct libws_mqtt_hack *user;