#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#define LIFETIME (60)
#define SERVER_OBJ_ID (1)
#define SERVER_OBJ_SHORT_RES_ID (0)
#define SERVER_OBJ_LIFETIME_RES_ID (1)
#define SERVER_OBJ_BINDING_RES_ID (7)
#define SERVER_OBJ_REGISTRATION_UPDATE_RES_ID (8)
#define ACCESS_CONTROL_OBJ_ID (2)
#define ACCESS_CONTROL_OBJ_OBJECT_RES_ID (0)
#define ACCESS_CONTROL_OBJ_INSTANCE_RES_ID (1)
#define ACCESS_CONTROL_OBJ_ACL_RES_ID (2)
#define ACCESS_CONTROL_OBJ_OWNER_RES_ID (3)
#define SECURITY_OBJ_ID (0)
#define SECURITY_SERVER_URI_RES_ID (0)
#define SECURITY_IS_BOOTSTRAP_RES_ID (1)
#define SECURITY_SECURITY_MODE_RES_ID (2)
#define SECURITY_PUBLIC_KEY_OR_IDENTITY_RES_ID (3)
#define SECURITY_SERVER_PUBLIC_KEY_RES_ID (4)
#define SECURITY_SECRET_KEY_RES_ID (5)
#define SECURITY_SERVER_ID_RES_ID (10)
#define SECURITY_CLIENT_HOLD_OFF_TIME_RES_ID (11)
#define SECURITY_BOOTSTRAP_SERVER_ACCOUNT_TIMEOUT_RES_ID (12)
#define PSK_KEY_LEN 16
#define RPK_PRIVATE_KEY_LEN 32
#define RPK_PUBLIC_KEY_LEN (2 * RPK_PRIVATE_KEY_LEN)
#define CLIENT_BS_PSK_ID ("cli1-bs")
#define CLIENT_BS_PSK_KEY ("FEDCBA9876543210")
#define CLIENT_SERVER_PSK_ID ("cli1")
#define CLIENT_SERVER_PSK_KEY ("0123456789ABCDEF")
#define CLIENT_PUBLIC_KEY ("D055EE14084D6E0615599DB583913E4A3E4526A2704D61F27A4CCFBA9758EF9A" \
"B418B64AFE8030DA1DDCF4F42E2F2631D043B1FB03E22F4D17DE43F9F9ADEE70")
#define BS_SERVER_PRIVATE_KEY ("9b7dfec20e49fe2cacf23fb21d06a8dc496530c695ec24cdf6c002ce44afa5fb")
#define BS_SERVER_PUBLIC_KEY ("cd4110e97bbd6e7e5a800028079d02915c70b915ea4596402098deea585eb7ad" \
"f3e080487327f70758b13bc0583f4293d13288a0164a8e324779aa4f7ada26c1")
.parent = NULL,
.mem = (void *)"coaps://localhost:5684",
.
size =
sizeof(
"coaps://localhost:5684") - 1,
.refcnt = 1
};
.parent = NULL,
.mem = (void *)"U",
.refcnt = 1
};
.parent = NULL,
.refcnt = 1
};
.parent = NULL,
.refcnt = 1
};
static void
{
int r;
fprintf(stderr, "The client %s could not write the object(s)/resource at %s.\n",
name, path);
return;
}
printf("The client %s wrote the object(s)/resource at %s.\n", name, path);
printf("Sending Bootstrap Finish to %s!\n", name);
if (r < 0)
fprintf(stderr, "Could not send Bootstrap Finish\n");
}
static void
{
int r;
fprintf(stderr, "The client %s could not write the object(s)/resource at %s.\n",
name, path);
return;
}
printf("The client %s wrote the object(s)/resource at %s.\n", name, path);
if (r < 0) {
fprintf(stderr, "Could not init Server Object's [Lifetime] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not send Bootstrap Write to /1/0/1\n");
}
}
static void
{
int r;
uint16_t i, j;
server_two, server_three
};
size_t servers_len[2] = {
};
uint16_t servers_ids[2] = {
0, 4
};
fprintf(stderr, "The client %s could not write the object(s)/resource at %s.\n",
name, path);
return;
}
printf("The client %s wrote the object(s)/resource at %s.\n", name, path);
if (r < 0) {
fprintf(stderr, "Could not init Server Object's [Short Server ID] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not init Server Object's [Lifetime] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not init Server Object's [Binding] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not init Server Object's [Short Server ID] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not init Server Object's [Lifetime] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not init Server Object's [Binding] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not send Bootstrap Write to /1\n");
}
}
static void
{
int r;
uint16_t i;
fprintf(stderr, "The client %s could not write the object(s)/resource at %s.\n",
name, path);
return;
}
printf("The client %s wrote the object(s)/resource at %s.\n", name, path);
if (r < 0) {
fprintf(stderr, "Could not init Server Object's [Short Server ID] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not init Server Object's [Lifetime] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not init Server Object's [Binding] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not send Bootstrap Write to /1/0\n");
}
}
static void
{
int r;
uint16_t i;
fprintf(stderr, "The client %s could not delete the object at %s.\n",
name, path);
return;
}
printf("The client %s deleted the object at %s.\n", name, path);
if (r < 0) {
fprintf(stderr, "Could not init Security Object's [Server URI] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not init Security Object's [Bootstrap Server] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not init Security Object's [Security Mode] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not init Security Object's [Public Key or Identity] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not init Security Object's [Secret Key] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not init Security Object's [Short Server ID] resource\n");
return;
}
if (r < 0) {
fprintf(stderr, "Could not send Bootstrap Write to /0/0\n");
}
}
static void
{
int r;
printf("Client-initiated Bootstrap from %s starting!\n", name);
if (r < 0) {
fprintf(stderr, "Could not send Bootstrap Delete to /\n");
}
}
int
main(
int argc,
char *argv[])
{
int r, sec_modes_len = 0;
.id = NULL,
.key = NULL
}),
NULL
};
struct sol_blob *known_pub_keys[] = { NULL, NULL };
snprintf(usage, sizeof(usage), "Usage: ./lwm2m-sample-bs-server [-p PORT] -s SEC_MODE...\n"
"Where default PORT=%" PRIu16 " and SEC_MODE is an integer as per:\n"
"\tPRE_SHARED_KEY=%d\n"
"\tRAW_PUBLIC_KEY=%d\n"
"\tCERTIFICATE=%d\n\n"
"For multiple SEC_MODEs, they must be passed in ascending order.\n",
port,
while ((r = getopt(argc, argv, "p:s:")) != -1) {
switch (r) {
case 'p':
port = atoi(optarg);
break;
case 's':
if (sec_modes_len < 2) {
sec_modes[sec_modes_len] = atoi(optarg);
if (sec_modes[sec_modes_len] < 0 || sec_modes[sec_modes_len] > 2) {
fprintf(stderr, "%s", usage);
return -1;
}
sec_modes_len++;
} else {
fprintf(stderr, "%s", usage);
return -1;
}
break;
default:
fprintf(stderr, "%s", usage);
return -1;
}
}
printf("Using port %" PRIu16 " for DTLS\n", port);
for (i = 0; i < sec_modes_len; i++) {
}
}
if (sec_modes_len == 1) {
sec_modes_len, sec_modes[0], known_keys);
sec_modes_len, sec_modes[0], &my_rpk, known_pub_keys);
else {
fprintf(stderr, "%s", usage);
return -1;
}
} else if (sec_modes_len == 2) {
sec_modes_len, sec_modes[0], known_keys,
sec_modes[1], &my_rpk, known_pub_keys);
} else {
fprintf(stderr, "%s", usage);
return -1;
}
if (!server) {
r = -1;
fprintf(stderr, "Could not create the LWM2M bootstrap server\n");
goto exit;
}
NULL);
if (r < 0) {
fprintf(stderr, "Could not add a bootstrap monitor\n");
goto exit_del;
}
r = 0;
exit_del:
exit:
for (i = 0; i < sec_modes_len; i++) {
}
}
return r;
}