Skip to content

Commit 89d779e

Browse files
committed
Use random int as registration ID
Previously the internal client ID was used as the registration ID. As that ID is recycled it can cause problems due to collisions. Moreover exposing the internal ID does make it public and therefore not internal anymore.
1 parent d9f3866 commit 89d779e

7 files changed

Lines changed: 54 additions & 8 deletions

File tree

core/registration.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,7 @@ uint8_t registration_handleRequest(lwm2m_context_t * contextP,
18841884
}
18851885
memset(clientP, 0, sizeof(lwm2m_client_t));
18861886
clientP->internalID = lwm2m_list_newId((lwm2m_list_t *)contextP->clientList);
1887+
clientP->registration_id = utils_random_registration_id(contextP->clientList);
18871888
contextP->clientList = (lwm2m_client_t *)LWM2M_LIST_ADD(contextP->clientList, clientP);
18881889
}
18891890
clientP->name = name;
@@ -1897,8 +1898,7 @@ uint8_t registration_handleRequest(lwm2m_context_t * contextP,
18971898
clientP->objectList = objects;
18981899
clientP->sessionH = fromSessionH;
18991900

1900-
if (prv_getLocationString(clientP->internalID, location) == 0)
1901-
{
1901+
if (prv_getLocationString(clientP->registration_id, location) == 0) {
19021902
registration_freeClient(contextP, clientP);
19031903
return COAP_500_INTERNAL_SERVER_ERROR;
19041904
}
@@ -1919,8 +1919,9 @@ uint8_t registration_handleRequest(lwm2m_context_t * contextP,
19191919
// Registration update
19201920
if (LWM2M_URI_IS_SET_INSTANCE(uriP)) return COAP_400_BAD_REQUEST;
19211921

1922-
clientP = (lwm2m_client_t *)lwm2m_list_find((lwm2m_list_t *)contextP->clientList, uriP->objectId);
1923-
if (clientP == NULL) return COAP_404_NOT_FOUND;
1922+
clientP = utils_find_client_by_registration_id(contextP->clientList, uriP->objectId);
1923+
if (clientP == NULL)
1924+
return COAP_404_NOT_FOUND;
19241925

19251926
// Endpoint client name MUST NOT be present
19261927
if (name != NULL)
@@ -2018,7 +2019,7 @@ uint8_t registration_handleRequest(lwm2m_context_t * contextP,
20182019
if (!LWM2M_URI_IS_SET_OBJECT(uriP)) return COAP_400_BAD_REQUEST;
20192020
if (LWM2M_URI_IS_SET_INSTANCE(uriP)) return COAP_400_BAD_REQUEST;
20202021

2021-
clientP = (lwm2m_client_t *)LWM2M_LIST_FIND(contextP->clientList, uriP->objectId);
2022+
clientP = utils_find_client_by_registration_id(contextP->clientList, uriP->objectId);
20222023

20232024
if (clientP == NULL) {
20242025
return COAP_400_BAD_REQUEST;

core/utils.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,19 @@ lwm2m_client_t * utils_findClient(lwm2m_context_t * contextP,
903903

904904
return targetP;
905905
}
906+
907+
lwm2m_client_t *utils_find_client_by_registration_id(lwm2m_client_t *head, const uint16_t registration_id) {
908+
while (NULL != head) {
909+
if (head->registration_id == registration_id) {
910+
return head;
911+
}
912+
913+
head = head->next;
914+
}
915+
916+
return NULL;
917+
}
918+
906919
#endif
907920

908921
int utils_isAltPathValid(const char * altPath)
@@ -1155,3 +1168,29 @@ lwm2m_data_type_t utils_depthToDatatype(uri_depth_t depth)
11551168

11561169
return LWM2M_TYPE_UNDEFINED;
11571170
}
1171+
1172+
static inline bool reg_id_in_use(const lwm2m_client_t *const client_list, const uint16_t reg_id) {
1173+
const lwm2m_client_t *head = client_list;
1174+
bool reg_id_already_in_use = false;
1175+
while (head != NULL) {
1176+
if (head->registration_id == reg_id) {
1177+
reg_id_already_in_use = true;
1178+
break;
1179+
}
1180+
1181+
head = head->next;
1182+
}
1183+
1184+
return reg_id_already_in_use;
1185+
}
1186+
1187+
uint16_t utils_random_registration_id(const lwm2m_client_t *const client_list) {
1188+
uint16_t reg_id = (uint16_t)(rand() % LWM2M_MAX_ID); // NOSONAR
1189+
bool reg_id_already_in_use = reg_id_in_use(client_list, reg_id);
1190+
1191+
if (reg_id_already_in_use) {
1192+
return utils_random_registration_id(client_list);
1193+
}
1194+
1195+
return reg_id;
1196+
}

core/utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
#include "internals.h"
2121

2222
lwm2m_data_type_t utils_depthToDatatype(uri_depth_t depth);
23+
uint16_t utils_random_registration_id(const lwm2m_client_t *client_list);
2324
lwm2m_version_t utils_stringToVersion(uint8_t *buffer, size_t length);
2425
lwm2m_binding_t utils_stringToBinding(uint8_t *buffer, size_t length);
2526
lwm2m_media_type_t utils_convertMediaType(coap_content_type_t type);
2627
uint8_t utils_getResponseFormat(uint8_t accept_num, const uint16_t *accept, int numData, const lwm2m_data_t *dataP,
2728
bool singleResource, lwm2m_media_type_t *format);
29+
lwm2m_client_t *utils_find_client_by_registration_id(lwm2m_client_t *head, uint16_t registration_id);
2830
int utils_isAltPathValid(const char *altPath);
2931
int utils_stringCopy(char *buffer, size_t length, const char *str);
3032
size_t utils_intToText(int64_t data, uint8_t *string, size_t length);

examples/server/lwm2mserver.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ static void prv_dump_client(lwm2m_client_t * targetP)
147147
lwm2m_client_object_t * objectP;
148148

149149
fprintf(stdout, "Client #%d:\r\n", targetP->internalID);
150+
fprintf(stdout, "\tregistration id: \"%d\"\r\n", targetP->registration_id);
150151
fprintf(stdout, "\tname: \"%s\"\r\n", targetP->name);
151152
fprintf(stdout, "\tversion: \"%s\"\r\n", prv_dump_version(targetP->version));
152153
prv_dump_binding(targetP->binding);

include/liblwm2m.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ typedef struct _lwm2m_client_
722722
{
723723
struct _lwm2m_client_ * next; // matches lwm2m_list_t::next
724724
uint16_t internalID; // matches lwm2m_list_t::id
725+
uint16_t registration_id;
725726
char * name;
726727
lwm2m_version_t version;
727728
lwm2m_binding_t binding;

tests/core_registration_tests.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static void test_registration_message_to_server(void) {
8787
size_t send_buffer_len;
8888
uint8_t *send_buffer = test_get_response_buffer(&send_buffer_len);
8989
coap_packet_t actual_response_packet;
90-
CU_ASSERT_EQUAL(14, send_buffer_len);
90+
CU_ASSERT(18 >= send_buffer_len && send_buffer_len >= 14);
9191
coap_status_t status = coap_parse_message(&actual_response_packet, send_buffer, send_buffer_len);
9292
CU_ASSERT_EQUAL(status, NO_ERROR);
9393

tests/integration/test_registration_interface.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ def parse_client_registration(server_output):
2020
"""
2121
client_id, = re.findall(r".lient #([0-9]+) ", server_output)
2222
event, = re.findall(r"(registered|updated)", server_output)
23+
registration_id, = re.findall(r"registration id: \"([0-9]+)\"\r\r\n", server_output)
2324
endpoint, = re.findall(r"name: \"([\w-]+)\"\r\r\n", server_output)
2425
version, = re.findall(r"version: \"([0-9.]+)\"\r\r\n", server_output)
2526
binding, = re.findall(r"binding: \"([A-Z]+)\"\r\r\n", server_output)
2627
lifetime, = re.findall(r"lifetime: ([0-9]+) sec\r\r\n", server_output)
2728
objects = re.search(r"objects: ([\/0-9]+ ?(\([0-9.]+\))?, ?)+\r\r\n", server_output)
28-
return int(client_id), event, endpoint, version, binding, int(lifetime), objects.string
29+
return int(client_id), registration_id, event, endpoint, version, binding, int(lifetime), objects.string
2930

3031

3132
def test_registration_interface(lwm2mserver, lwm2mclient):
@@ -34,9 +35,10 @@ def test_registration_interface(lwm2mserver, lwm2mclient):
3435

3536
lwm2mclient.wait_for_text("STATE_READY")
3637
text = lwm2mserver.wait_for_packet()
37-
client_id, event, endpoint, version, binding, lifetime, objects = \
38+
client_id, registration_id, event, endpoint, version, binding, lifetime, objects = \
3839
parse_client_registration(text)
3940
assert client_id == 0
41+
assert int(registration_id) < 0xFFFE # UINT16_MAX - 1
4042
assert event == "registered"
4143
assert endpoint == "testlwm2mclient"
4244
assert version == "1.1"

0 commit comments

Comments
 (0)