Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new feature: SCRAM is supported. And provide a demo to use SCRAM. #233

Merged
merged 10 commits into from
Jul 29, 2024
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,18 @@ endif ()
if(NNG_ENABLE_QUIC)
set(NNG_PROTO_MQTT_QUIC_CLIENT ON)
add_definitions(-DSUPP_QUIC)
endif()

if (NNG_ENABLE_SCRAM)
add_definitions(-DSUPP_SCRAM)
find_package(OpenSSL)
target_link_libraries(nng PRIVATE OpenSSL::SSL OpenSSL::Crypto)
endif ()

add_subdirectory(src)

if (NNG_TESTS)
add_subdirectory(tests)
add_subdirectory(tests)
endif ()

# Build the tools
Expand Down Expand Up @@ -334,6 +340,7 @@ if (BUILD_DEMO)
add_subdirectory(demo/mqtt)
add_subdirectory(demo/mqttv5)
add_subdirectory(demo/mqtt_async)
add_subdirectory(demo/mqttv5_scram)
JaylinYu marked this conversation as resolved.
Show resolved Hide resolved

add_subdirectory(demo/rest)
add_subdirectory(demo/http_client)
Expand Down
6 changes: 6 additions & 0 deletions cmake/NNGOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ else ()
set(NNG_QUIC_LIB none)
endif ()

option(NNG_ENABLE_SCRAM "Enable SCRAM support." OFF)
if (NNG_ENABLE_SCRAM)
set(SUPP_SCRAM ON)
endif ()


# TLS support.

# Enabling TLS is required to enable support for the TLS transport
Expand Down
20 changes: 13 additions & 7 deletions demo/mqtt/mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ int keepRunning = 1;
void
intHandler(int dummy)
{
(void) dummy;
keepRunning = 0;
fprintf(stderr, "\nclient exit(0).\n");
// nng_closeall();
Expand Down Expand Up @@ -89,6 +90,8 @@ disconnect_cb(nng_pipe p, nng_pipe_ev ev, void *arg)
// nng_pipe_get_ptr(p, NNG_OPT_MQTT_DISCONNECT_PROPERTY, &prop);
// nng_socket_get?
printf("%s: disconnected!\n", __FUNCTION__);
(void) ev;
(void) arg;
}

static void
Expand All @@ -101,6 +104,8 @@ connect_cb(nng_pipe p, nng_pipe_ev ev, void *arg)
// property *prop;
// nng_pipe_get_ptr(p, NNG_OPT_MQTT_CONNECT_PROPERTY, &prop);
printf("%s: connected!\n", __FUNCTION__);
(void) ev;
(void) arg;
}

// Connect to the given address.
Expand Down Expand Up @@ -194,7 +199,6 @@ struct pub_params {
void
publish_cb(void *args)
{
int rv;
struct pub_params *params = args;
do {
client_publish(*params->sock, params->topic, params->data,
Expand Down Expand Up @@ -228,6 +232,8 @@ sqlite_config(nng_socket *sock, uint8_t proto_ver)
// set sqlite option pointer to socket
return nng_socket_set_ptr(*sock, NNG_OPT_MQTT_SQLITE, sqlite);
#else
(void) sock;
(void) proto_ver;
return (0);
#endif
}
Expand All @@ -237,24 +243,23 @@ send_callback (nng_mqtt_client *client, nng_msg *msg, void *arg) {
nng_aio * aio = client->send_aio;
uint32_t count;
uint8_t * code;
uint8_t type;

if (msg == NULL)
return;
switch (nng_mqtt_msg_get_packet_type(msg)) {
case NNG_MQTT_SUBACK:
code = (reason_code *) nng_mqtt_msg_get_suback_return_codes(
code = nng_mqtt_msg_get_suback_return_codes(
msg, &count);
printf("SUBACK reason codes are");
for (int i = 0; i < count; ++i)
for (int i = 0; i < (int)count; ++i)
printf("%d ", code[i]);
printf("\n");
break;
case NNG_MQTT_UNSUBACK:
code = (reason_code *) nng_mqtt_msg_get_unsuback_return_codes(
code = nng_mqtt_msg_get_unsuback_return_codes(
msg, &count);
printf("UNSUBACK reason codes are");
for (int i = 0; i < count; ++i)
for (int i = 0; i < (int)count; ++i)
printf("%d ", code[i]);
printf("\n");
break;
Expand All @@ -268,6 +273,7 @@ send_callback (nng_mqtt_client *client, nng_msg *msg, void *arg) {
printf("aio mqtt result %d \n", nng_aio_result(aio));
// printf("suback %d \n", *code);
nng_msg_free(msg);
(void) arg;
}

int
Expand Down Expand Up @@ -319,7 +325,7 @@ main(const int argc, const char **argv)
params.interval = interval;
params.verbose = verbose;

char thread_name[20];
// char thread_name[20];

sqlite_config(params.sock, MQTT_PROTOCOL_VERSION_v311);

Expand Down
30 changes: 18 additions & 12 deletions demo/mqttv5/mqttv5_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ int keepRunning = 1;
void
intHandler(int dummy)
{
(void) dummy;
keepRunning = 0;
fprintf(stderr, "\nclient exit(0).\n");
exit(0);
Expand Down Expand Up @@ -88,6 +89,8 @@ disconnect_cb(nng_pipe p, nng_pipe_ev ev, void *arg)
// nng_pipe_get_ptr(p, NNG_OPT_MQTT_DISCONNECT_PROPERTY, &prop);
// nng_socket_get?
printf("%s: disconnected! RC [%d] \n", __FUNCTION__, reason);
(void) ev;
(void) arg;
}

static void
Expand All @@ -100,6 +103,8 @@ connect_cb(nng_pipe p, nng_pipe_ev ev, void *arg)
// property *prop;
// nng_pipe_get_ptr(p, NNG_OPT_MQTT_CONNECT_PROPERTY, &prop);
printf("%s: connected! RC [%d] \n", __FUNCTION__, reason);
(void) ev;
(void) arg;
}

// Connect to the given address.
Expand Down Expand Up @@ -216,25 +221,24 @@ send_callback(nng_mqtt_client *client, nng_msg *msg, void *arg) {
nng_aio * aio = client->send_aio;
uint32_t count;
uint8_t * code;
uint8_t type;

if (msg == NULL)
return;
switch (nng_mqtt_msg_get_packet_type(msg)) {
case NNG_MQTT_SUBACK:
code = (reason_code *) nng_mqtt_msg_get_suback_return_codes(
code = nng_mqtt_msg_get_suback_return_codes(
msg, &count);
printf("SUBACK reason codes are");
for (int i = 0; i < count; ++i)
printf("%d ", code[i]);
printf("SUBACK reason codes are: ");
for (int i = 0; i < (int)count; ++i)
wanghaEMQ marked this conversation as resolved.
Show resolved Hide resolved
printf("[%d] ", code[i]);
printf("\n");
break;
case NNG_MQTT_UNSUBACK:
code = (reason_code *) nng_mqtt_msg_get_unsuback_return_codes(
code = nng_mqtt_msg_get_unsuback_return_codes(
msg, &count);
printf("UNSUBACK reason codes are");
for (int i = 0; i < count; ++i)
printf("%d ", code[i]);
printf("UNSUBACK reason codes are: ");
for (int i = 0; i < (int)count; ++i)
printf("[%d] ", code[i]);
printf("\n");
break;
case NNG_MQTT_PUBACK:
Expand All @@ -244,9 +248,10 @@ send_callback(nng_mqtt_client *client, nng_msg *msg, void *arg) {
printf("Sending in async way is done.\n");
break;
}
printf("aio mqtt result %d \n", nng_aio_result(aio));
printf("Aio mqtt result %d \n", nng_aio_result(aio));
// printf("suback %d \n", *code);
nng_msg_free(msg);
(void) arg;
}

// Publish a message to the given topic and with the given QoS.
Expand Down Expand Up @@ -343,7 +348,6 @@ void msg_recv_deal(nng_msg *msg, bool verbose)
void
publish_cb(void *args)
{
int rv;
struct pub_params *params = args;
do {
client_publish(*params->sock, params->topic, params->data,
Expand Down Expand Up @@ -377,6 +381,8 @@ sqlite_config(nng_socket *sock, uint8_t proto_ver)
// set sqlite option pointer to socket
return nng_socket_set_ptr(*sock, NNG_OPT_MQTT_SQLITE, sqlite);
#else
(void) sock;
(void) proto_ver;
return (0);
#endif
}
Expand Down Expand Up @@ -432,7 +438,7 @@ main(const int argc, const char **argv)
params.interval = interval;
params.verbose = verbose;

char thread_name[20];
// char thread_name[20];

sqlite_config(params.sock, MQTT_PROTOCOL_VERSION_v5);

Expand Down
33 changes: 33 additions & 0 deletions demo/mqttv5_scram/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# This software is supplied under the terms of the MIT License, a
# copy of which should be located in the distribution where this
# file was obtained (LICENSE.txt). A copy of the license may also be
# found online at https://opensource.org/licenses/MIT.

cmake_minimum_required(VERSION 3.13)

project(mqttv5_scram)

if (BUILD_DEMO)
else ()
# Call this from your own project's makefile.
find_package(nng CONFIG REQUIRED)
endif (BUILD_DEMO)

find_package(Threads)

if (DEBUG)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
if (ASAN)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
endif (ASAN)
if (TSAN)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
endif (TSAN)
endif (DEBUG)

add_executable(mqttv5_scram mqttv5_scram.c)
target_link_libraries(mqttv5_scram nng)
target_link_libraries(mqttv5_scram ${CMAKE_THREAD_LIBS_INIT})

target_compile_definitions(mqttv5_scram PRIVATE NNG_ELIDE_DEPRECATED)
Loading
Loading