Skip to content

Commit

Permalink
build cleaner interface dtls
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasKarel committed Oct 9, 2021
1 parent 6c43ae2 commit 18bda34
Show file tree
Hide file tree
Showing 20 changed files with 693 additions and 1,114 deletions.
8 changes: 1 addition & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,5 @@ if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
enable_testing()
endif()

if(DTLS_MBEDTLS)
message("Skipping tests")
else()
message("Adding tests")
add_subdirectory(tests)
endif()

add_subdirectory(tests)
add_subdirectory(examples)
14 changes: 5 additions & 9 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ add_compile_options(-pedantic)
if(DTLS_MBEDTLS)
option(ENABLE_PROGRAMS "Build mbed TLS programs." OFF)
option(ENABLE_TESTING "Build mbed TLS tests." OFF)
include_directories(${CMAKE_CURRENT_LIST_DIR}/shared/dtls/mbedtls/include)
# Use custom config file for Mbed TLS
add_definitions(-DMBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/shared/dtls/config-ccm-psk-tls1_2.h")
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/shared/dtls/mbedtls)

include_directories(${CMAKE_CURRENT_LIST_DIR}/client)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/client)
else()
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/bootstrap_server)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/client)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/lightclient)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/server)
endif()

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/bootstrap_server)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/client)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/server)

30 changes: 12 additions & 18 deletions examples/bootstrap_server/bootstrap_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ typedef struct _endpoint_

typedef struct
{
int sock;
connection_t * connList;
bs_info_t * bsInfo;
endpoint_t * endpointList;
int addressFamily;
int sock;
lwm2m_connection_layer_t * connLayer;
bs_info_t * bsInfo;
endpoint_t * endpointList;
int addressFamily;
} internal_data_t;

#define MAX_PACKET_SIZE 2048
Expand Down Expand Up @@ -536,12 +536,11 @@ static void prv_bootstrap_client(lwm2m_context_t *lwm2mH,
port++;

fprintf(stderr, "Trying to connect to LWM2M CLient at %s:%s\r\n", host, port);
newConnP = connection_create(dataP->connList, dataP->sock, host, port, dataP->addressFamily);
newConnP = connection_create(dataP->connLayer, dataP->sock, host, port, dataP->addressFamily);
if (newConnP == NULL) {
fprintf(stderr, "Connection creation failed.\r\n");
return;
}
dataP->connList = newConnP;

// simulate a client bootstrap request.
// Only LWM2M 1.0 clients support this method of bootstrap. For them, TLV
Expand Down Expand Up @@ -654,6 +653,8 @@ int main(int argc, char *argv[])
return -1;
}

data.connLayer = connectionlayer_create(lwm2mH);

signal(SIGINT, handle_sigint);

fd = fopen(filename, "r");
Expand Down Expand Up @@ -749,19 +750,12 @@ int main(int argc, char *argv[])

output_buffer(stderr, buffer, numBytes, 0);

connP = connection_find(data.connList, &addr, addrLen);
connP = connectionlayer_find_connection(data.connLayer, &addr, addrLen);
if (connP == NULL)
{
connP = connection_new_incoming(data.connList, data.sock, (struct sockaddr *)&addr, addrLen);
if (connP != NULL)
{
data.connList = connP;
}
}
if (connP != NULL)
{
lwm2m_handle_packet(lwm2mH, buffer, numBytes, connP);
connP = connection_new_incoming(data.connLayer, data.sock, &addr, addrLen);
}
connectionlayer_handle_packet(data.connLayer, &addr, addrLen, buffer, numBytes);
}
}
// command line input
Expand Down Expand Up @@ -834,7 +828,7 @@ int main(int argc, char *argv[])
prv_endpoint_free(endP);
}
close(data.sock);
connection_free(data.connList);
connectionlayer_free(data.connLayer);

return 0;
}
141 changes: 25 additions & 116 deletions examples/client/lwm2mclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@
#include "lwm2mclient.h"
#include "liblwm2m.h"
#include "commandline.h"
#ifdef WITH_TINYDTLS
#include "tinydtlsconnection.h"
#elif defined(WITH_MBEDTLS)
#include "mbedtlsconnection.h"
#include "connection.h"
#if defined(WITH_TINYDTLS) || defined(WITH_MBEDTLS)
#include "dtlsconnection.h"
#endif

#include "connection.h"
#include "object_utils.h"
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
Expand Down Expand Up @@ -191,35 +189,12 @@ void handle_value_changed(lwm2m_context_t * lwm2mH,
fprintf(stderr, "Object not found !\n");
}
}
#ifndef WITH_MBEDTLS
#ifdef WITH_TINYDTLS
void * lwm2m_connect_server(uint16_t secObjInstID,
void * userData)
{
client_data_t * dataP;
lwm2m_list_t * instance;
dtls_connection_t * newConnP = NULL;
dataP = (client_data_t *)userData;
lwm2m_object_t * securityObj = dataP->securityObjP;

instance = LWM2M_LIST_FIND(dataP->securityObjP->instanceList, secObjInstID);
if (instance == NULL) return NULL;


newConnP = connection_create(dataP->connList, dataP->sock, securityObj, instance->id, dataP->lwm2mH, dataP->addressFamily);
if (newConnP == NULL)
{
fprintf(stderr, "Connection creation failed.\n");
return NULL;
}

dataP->connList = newConnP;
return (void *)newConnP;
}
#else
void * lwm2m_connect_server(uint16_t secObjInstID,
void * userData)
{
int securityMode = 0;
int ret = 0;
client_data_t * dataP;
char * uri;
char * host;
Expand Down Expand Up @@ -259,63 +234,38 @@ void * lwm2m_connect_server(uint16_t secObjInstID,
port++;

fprintf(stderr, "Opening connection to server at %s:%s\r\n", host, port);
newConnP = connection_create(dataP->connList, dataP->sock, host, port, dataP->addressFamily);
ret = security_get_security_mode(dataP->ctx, secObjInstID, &securityMode);
if(ret <= 0){
goto exit;
}
if(securityMode == LWM2M_SECURITY_MODE_PRE_SHARED_KEY) {
#if defined(WITH_TINYDTLS) || defined(WITH_MBEDTLS)
newConnP = (connection_t*)dtlsconnection_create(dataP->connLayer, secObjInstID, dataP->sock, host, port, dataP->addressFamily);
#endif
}
else if(securityMode == LWM2M_SECURITY_MODE_NONE) {
newConnP = connection_create(dataP->connLayer, dataP->sock, host, port, dataP->addressFamily);
}

if (newConnP == NULL) {
fprintf(stderr, "Connection creation failed.\r\n");
}
else {
dataP->connList = newConnP;
}

exit:
lwm2m_free(uri);
return (void *)newConnP;
}
#endif

void lwm2m_close_connection(void * sessionH,
void * userData)
{
client_data_t * app_data;
#ifdef WITH_TINYDTLS
dtls_connection_t * targetP;
#else
connection_t * targetP;
#endif

app_data = (client_data_t *)userData;
#ifdef WITH_TINYDTLS
targetP = (dtls_connection_t *)sessionH;
#else
targetP = (connection_t *)sessionH;
#endif

if (targetP == app_data->connList)
{
app_data->connList = targetP->next;
lwm2m_free(targetP);
}
else
{
#ifdef WITH_TINYDTLS
dtls_connection_t * parentP;
#else
connection_t * parentP;
#endif

parentP = app_data->connList;
while (parentP != NULL && parentP->next != targetP)
{
parentP = parentP->next;
}
if (parentP != NULL)
{
parentP->next = targetP->next;
lwm2m_free(targetP);
}
}
connectionlayer_free_connection(app_data->connLayer, targetP);
}
#endif

static void prv_output_servers(lwm2m_context_t * lwm2mH,
char * buffer,
Expand Down Expand Up @@ -1031,7 +981,6 @@ int main(int argc, char *argv[])
server = (AF_INET == data.addressFamily ? DEFAULT_SERVER_IPV4 : DEFAULT_SERVER_IPV6);
}

#ifndef WITH_MBEDTLS
/*
*This call an internal function that create an IPV6 socket on the port 5683.
*/
Expand All @@ -1042,7 +991,6 @@ int main(int argc, char *argv[])
fprintf(stderr, "Failed to open socket: %d %s\r\n", errno, strerror(errno));
return -1;
}
#endif

/*
* Now the main function fill an array with each object, this list will be later passed to liblwm2m.
Expand Down Expand Up @@ -1181,7 +1129,7 @@ int main(int argc, char *argv[])
return -1;
}
data.ctx = lwm2mH;

data.connLayer = connectionlayer_create(lwm2mH);
/*
* We configure the liblwm2m library with the name of the client - which shall be unique for each client -
* the number of objects we will be passing through and the objects array
Expand Down Expand Up @@ -1220,7 +1168,7 @@ int main(int argc, char *argv[])
{
reboot_time = tv_sec + 5;
}
if (reboot_time < tv_sec)
if (reboot_time <= tv_sec)
{
/*
* Message should normally be lost with reboot ...
Expand All @@ -1245,17 +1193,7 @@ int main(int argc, char *argv[])
tv.tv_usec = 0;

FD_ZERO(&readfds);
#ifndef WITH_MBEDTLS
FD_SET(data.sock, &readfds);
#else
int sockSize = 0;
int * socks = mbedtls_get_sockets(lwm2mH, &sockSize);
if(socks != NULL) {
for(int i = 0; i < sockSize; i++) {
FD_SET(socks[i], &readfds);
}
}
#endif
FD_SET(STDIN_FILENO, &readfds);

/*
Expand Down Expand Up @@ -1328,7 +1266,6 @@ int main(int argc, char *argv[])
{
uint8_t buffer[MAX_PACKET_SIZE];
int numBytes;
#ifndef WITH_MBEDTLS
/*
* If an event happens on the socket
*/
Expand Down Expand Up @@ -1356,12 +1293,7 @@ int main(int argc, char *argv[])
{
char s[INET6_ADDRSTRLEN];
in_port_t port;

#ifdef WITH_TINYDTLS
dtls_connection_t * connP;
#else
connection_t * connP;
#endif
if (AF_INET == addr.ss_family)
{
struct sockaddr_in *saddr = (struct sockaddr_in *)&addr;
Expand All @@ -1381,21 +1313,13 @@ int main(int argc, char *argv[])
*/
output_buffer(stderr, buffer, numBytes, 0);

connP = connection_find(data.connList, &addr, addrLen);
connP = connectionlayer_find_connection(data.connLayer, &addr, addrLen);
if (connP != NULL)
{
/*
* Let liblwm2m respond to the query depending on the context
*/
#ifdef WITH_TINYDTLS
int result = connection_handle_packet(connP, buffer, numBytes);
if (0 != result)
{
printf("error handling message %d\n",result);
}
#else
lwm2m_handle_packet(lwm2mH, buffer, numBytes, connP);
#endif
connectionlayer_handle_packet(data.connLayer, &addr, addrLen, buffer, numBytes);
conn_s_updateRxStatistic(objArray[7], numBytes, false);
}
else
Expand All @@ -1409,19 +1333,6 @@ int main(int argc, char *argv[])
* If the event happened on the SDTIN
*/
else if (FD_ISSET(STDIN_FILENO, &readfds))
#else
for(int i = 0; i < sockSize; i++) {
if(FD_ISSET(socks[i], &readfds)) {
void * connection = NULL;
numBytes = mbedtls_receive(lwm2mH, socks[i], buffer, MAX_PACKET_SIZE, &connection);
if(numBytes > 0) {
lwm2m_handle_packet(lwm2mH, buffer, numBytes, connection);
}
}
lwm2m_free(socks);
}
if (FD_ISSET(STDIN_FILENO, &readfds))
#endif
{
numBytes = read(STDIN_FILENO, buffer, MAX_PACKET_SIZE - 1);

Expand Down Expand Up @@ -1460,10 +1371,8 @@ int main(int argc, char *argv[])
#endif
lwm2m_close(lwm2mH);
}
#ifndef WITH_MBEDTLS
close(data.sock);
connection_free(data.connList);
#endif
connectionlayer_free(data.connLayer);

clean_security_object(objArray[0]);
lwm2m_free(objArray[0]);
Expand Down
10 changes: 2 additions & 8 deletions examples/client/lwm2mclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define LWM2MCLIENT_H_

#include "liblwm2m.h"
#include "connection.h"

extern int g_reboot;

Expand Down Expand Up @@ -113,15 +114,8 @@ typedef struct
lwm2m_object_t * securityObjP;
lwm2m_object_t * serverObject;
lwm2m_context_t * ctx;
#ifndef WITH_MBEDTLS
int sock;
#ifdef WITH_TINYDTLS
dtls_connection_t * connList;
lwm2m_context_t * lwm2mH;
#else
connection_t * connList;
#endif
#endif
lwm2m_connection_layer_t * connLayer;
int addressFamily;
} client_data_t;

Expand Down
Loading

0 comments on commit 18bda34

Please sign in to comment.