Skip to content

Commit

Permalink
Add option to build embedd broker as shared library
Browse files Browse the repository at this point in the history
Signed-off-by: Norbert Heusser <[email protected]>
  • Loading branch information
NorbertHeusser committed Nov 15, 2024
1 parent 9fddc7c commit d32e8bc
Show file tree
Hide file tree
Showing 17 changed files with 264 additions and 90 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ man/mqtt.7
out/

src/mosquitto
src/mosquitto_broker.a
src/libmosquitto_broker.a
src/libmosquitto_broker.so*

test/broker/broker.pid
test/test_client
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ if(WITH_THREADING)
endif()
endif()

option(WITH_EMBEDD_BROKER, "Create a library mosquitto_broker to embedd the broker" OFF)

option(WITH_DLT "Include DLT support?" OFF)
message(STATUS "WITH_DLT = ${WITH_DLT}")
if(WITH_DLT)
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ endif
$(INSTALL) include/mosquitto_plugin.h "${DESTDIR}${prefix}/include/mosquitto_plugin.h"
$(INSTALL) include/mosquittopp.h "${DESTDIR}${prefix}/include/mosquittopp.h"
$(INSTALL) include/mqtt_protocol.h "${DESTDIR}${prefix}/include/mqtt_protocol.h"
ifeq ($(WITH_EMBEDD_BROKER),yes)
$(INSTALL) include/mosquitto/embedded_broker.h "${DESTDIR}${prefix}/include/mosquitto/"
endif

uninstall :
set -e; for d in ${DIRS}; do $(MAKE) -C $${d} uninstall; done
Expand All @@ -133,6 +136,7 @@ uninstall :
rm -f "${DESTDIR}${prefix}/include/mosquitto_plugin.h"
rm -f "${DESTDIR}${prefix}/include/mosquittopp.h"
rm -f "${DESTDIR}${prefix}/include/mqtt_protocol.h"
rm -f "${DESTDIR}${prefix}/include/mosquitto/embedded_broker.h"

dist : reallyclean
set -e; for d in ${DISTDIRS}; do $(MAKE) -C $${d} dist; done
Expand Down
9 changes: 9 additions & 0 deletions config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ CLIENT_STATIC_LDADD:=
# Build shared libraries
WITH_SHARED_LIBRARIES:=yes

# Build the broker additionally as a library, which can be used to
# embedd a broker into an own executable.
WITH_EMBEDD_BROKER:=no

# Build with async dns lookup support for bridges (temporary). Requires glibc.
#WITH_ADNS:=yes

Expand Down Expand Up @@ -232,6 +236,7 @@ ifeq ($(WITH_SHARED_LIBRARIES),yes)
else
LIBMOSQ:=${R}/lib/libmosquitto.a
endif

LIBMOSQ_COMMON:=-Wl,--whole-archive ${R}/libcommon/libmosquitto_common.a -Wl,--no-whole-archive -lcjson

ifeq ($(WITH_TLS),yes)
Expand Down Expand Up @@ -288,6 +293,10 @@ ifeq ($(WITH_COVERAGE),yes)
LOCAL_LDFLAGS+=-coverage
endif

ifeq ($(WITH_SHARED_LIBRARIES),yes)
LOCAL_CFLAGS+=-fPIC
endif

ifeq ($(WITH_FUZZING),yes)
MAKE_ALL+=fuzzing
LOCAL_CPPFLAGS+=-DWITH_FUZZING
Expand Down
15 changes: 7 additions & 8 deletions fuzzing/broker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,36 @@ LOCAL_CPPFLAGS+=-I${R}/include/ -I${R}/src -I${R}/lib -I${R} -I${R}/common -I${R
-DWITH_SYS_TREE -DWITH_TLS -DWITH_TLS_PSK -DWITH_UNIX_SOCKETS -DWITH_WEBSOCKETS=WS_IS_BUILTIN
LOCAL_CXXFLAGS+=-g -Wall -Werror -pthread
LOCAL_LDFLAGS+=
LOCAL_LIBADD+=$(LIB_FUZZING_ENGINE) ${R}/src/mosquitto_broker.a -lssl -lcrypto -lcjson -lm ${R}/libcommon/libmosquitto_common.a -Wl,-Bdynamic -Wl,-Bstatic -largon2 -Wl,-Bdynamic

LOCAL_LIBADD+=$(LIB_FUZZING_ENGINE) ${R}/src/libmosquitto_broker.a -lssl -lcrypto -lcjson -lm ${R}/libcommon/libmosquitto_common.a -Wl,-Bdynamic -Wl,-Bstatic -largon2 -Wl,-Bdynamic
all: $(FUZZERS)

broker_fuzz_acl_file : broker_fuzz_acl_file.cpp ${R}/src/mosquitto_broker.a
broker_fuzz_acl_file : broker_fuzz_acl_file.cpp ${R}/src/libmosquitto_broker.a
$(CXX) $(LOCAL_CXXFLAGS) $(LOCAL_CPPFLAGS) $(LOCAL_LDFLAGS) -o $@ $< $(LOCAL_LIBADD)
install $@ ${OUT}/$@
cp ${R}/fuzzing/corpora/broker_acl_file_seed_corpus.zip ${OUT}/$@_seed_corpus.zip
cp ${R}/fuzzing/corpora/broker_acl_file.dict ${OUT}/$@.dict

broker_fuzz_password_file : broker_fuzz_password_file.cpp ${R}/src/mosquitto_broker.a
broker_fuzz_password_file : broker_fuzz_password_file.cpp ${R}/src/libmosquitto_broker.a
$(CXX) $(LOCAL_CXXFLAGS) $(LOCAL_CPPFLAGS) $(LOCAL_LDFLAGS) -o $@ $< $(LOCAL_LIBADD)
install $@ ${OUT}/$@
cp ${R}/fuzzing/corpora/broker_password_file_seed_corpus.zip ${OUT}/$@_seed_corpus.zip

broker_fuzz_psk_file : broker_fuzz_psk_file.cpp ${R}/src/mosquitto_broker.a
broker_fuzz_psk_file : broker_fuzz_psk_file.cpp ${R}/src/libmosquitto_broker.a
$(CXX) $(LOCAL_CXXFLAGS) $(LOCAL_CPPFLAGS) $(LOCAL_LDFLAGS) -o $@ $< $(LOCAL_LIBADD)
install $@ ${OUT}/$@
cp ${R}/fuzzing/corpora/broker_psk_file_seed_corpus.zip ${OUT}/$@_seed_corpus.zip

broker_fuzz_queue_msg : broker_fuzz_queue_msg.cpp ${R}/src/mosquitto_broker.a
broker_fuzz_queue_msg : broker_fuzz_queue_msg.cpp ${R}/src/libmosquitto_broker.a
$(CXX) $(LOCAL_CXXFLAGS) $(LOCAL_CPPFLAGS) $(LOCAL_LDFLAGS) -o $@ $< $(LOCAL_LIBADD)
install $@ ${OUT}/$@
cp ${R}/fuzzing/corpora/broker_queue_msg_seed_corpus.zip ${OUT}/$@_seed_corpus.zip

broker_fuzz_read_handle : broker_fuzz_read_handle.cpp ${R}/src/mosquitto_broker.a
broker_fuzz_read_handle : broker_fuzz_read_handle.cpp ${R}/src/libmosquitto_broker.a
$(CXX) $(LOCAL_CXXFLAGS) $(LOCAL_CPPFLAGS) $(LOCAL_LDFLAGS) -o $@ $< $(LOCAL_LIBADD)
install $@ ${OUT}/$@
cp ${R}/fuzzing/corpora/broker_packet_seed_corpus.zip ${OUT}/$@_seed_corpus.zip

broker_fuzz_test_config : broker_fuzz_test_config.cpp ${R}/src/mosquitto_broker.a
broker_fuzz_test_config : broker_fuzz_test_config.cpp ${R}/src/libmosquitto_broker.a
$(CXX) $(LOCAL_CXXFLAGS) $(LOCAL_CPPFLAGS) $(LOCAL_LDFLAGS) -o $@ $< $(LOCAL_LIBADD)
install $@ ${OUT}/$@
cp ${R}/fuzzing/corpora/broker_fuzz_test_config_seed_corpus.zip ${OUT}/$@_seed_corpus.zip
Expand Down
4 changes: 2 additions & 2 deletions fuzzing/broker/broker_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
#define PORT 1883

/* The broker fuzz-only main function. */
extern "C" int mosquitto_fuzz_main(int argc, char *argv[]);
extern "C" int mosquitto_broker_main(int argc, char *argv[]);

void *run_broker(void *args)
{
Expand All @@ -45,7 +45,7 @@ void *run_broker(void *args)
argv[2] = strdup("-c");
argv[3] = strdup("/tmp/mosquitto.conf");

mosquitto_fuzz_main(argc, argv);
mosquitto_broker_main(argc, argv);

for(int i=0; i<argc; i++){
free(argv[i]);
Expand Down
4 changes: 2 additions & 2 deletions fuzzing/broker/broker_fuzz_test_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

/* The broker fuzz-only main function. */
extern "C" int mosquitto_fuzz_main(int argc, char *argv[]);
extern "C" int mosquitto_broker_main(int argc, char *argv[]);

void run_broker(char *filename)
{
Expand All @@ -43,7 +43,7 @@ void run_broker(char *filename)
argv[3] = strdup("-c");
argv[4] = strdup(filename);

mosquitto_fuzz_main(argc, argv);
mosquitto_broker_main(argc, argv);

for(int i=0; i<argc; i++){
free(argv[i]);
Expand Down
4 changes: 2 additions & 2 deletions fuzzing/broker/broker_fuzz_with_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
#define PORT 1883

/* The broker fuzz-only main function. */
extern "C" int mosquitto_fuzz_main(int argc, char *argv[]);
extern "C" int mosquitto_broker_main(int argc, char *argv[]);

void *run_broker(void *args)
{
Expand All @@ -45,7 +45,7 @@ void *run_broker(void *args)
argv[2] = strdup("-c");
argv[3] = strdup("/tmp/mosquitto.conf");

mosquitto_fuzz_main(argc, argv);
mosquitto_broker_main(argc, argv);

for(int i=0; i<argc; i++){
free(argv[i]);
Expand Down
2 changes: 1 addition & 1 deletion fuzzing/plugins/dynamic-security/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LOCAL_CXXFLAGS+=-g -Wall -Werror -pthread
LOCAL_LDFLAGS+=
LOCAL_LIBADD+=$(LIB_FUZZING_ENGINE) \
${R}/plugins/dynamic-security/mosquitto_dynamic_security.a \
${R}/src/mosquitto_broker.a \
${R}/src/libmosquitto_broker.a \
${R}/libcommon/libmosquitto_common.a \
-lssl -lcrypto -lcjson -lm \
-Wl,-Bdynamic -Wl,-Bstatic -largon2 -Wl,-Bdynamic
Expand Down
44 changes: 44 additions & 0 deletions include/mosquitto/embedded_broker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright (c) 2009-2021 Roger Light <[email protected]>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
*/

/*
* File: mosquitto_broker.h
*
* This header contains functions for use by plugins.
*/
#ifndef MOSQUITTO_EMBEDDED_BROKER_H
#define MOSQUITTO_EMBEDDED_BROKER_H

#ifdef __cplusplus
extern "C" {
#endif

#if defined(WIN32) && defined(mosquitto_EXPORTS)
# define mosq_EXPORT __declspec(dllexport)
#else
# define mosq_EXPORT
#endif

mosq_EXPORT int mosquitto_broker_main(int argc, char *argv[]);

mosq_EXPORT void mosquitto_broker_stop();

#ifdef __cplusplus
}
#endif

#endif
Loading

0 comments on commit d32e8bc

Please sign in to comment.