Skip to content

Commit

Permalink
samples: cellular: modem_shell: Add DTLS connection ID
Browse files Browse the repository at this point in the history
Add support for using DTLS connection ID. Show DTLS handshake status
and DTLS connection ID status after successful connect.

Signed-off-by: Stig Bjørlykke <[email protected]>
  • Loading branch information
stig-bjorlykke committed Jan 8, 2025
1 parent 5a083ee commit db75268
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
75 changes: 72 additions & 3 deletions samples/cellular/modem_shell/src/sock/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ static int sock_set_tls_options(
uint32_t sec_tag,
bool session_cache,
int peer_verify,
char *peer_hostname)
char *peer_hostname,
int dtls_connection_id)
{
int err;
uint32_t sec_tag_list[] = { sec_tag };
Expand Down Expand Up @@ -409,6 +410,65 @@ static int sock_set_tls_options(
return errno;
}
}

/* DTLS connection ID */
if (dtls_connection_id) {
err = setsockopt(fd, SOL_TLS, TLS_DTLS_CID, &dtls_connection_id,
sizeof(dtls_connection_id));
if (err) {
mosh_error("Unable to set DTLS connection ID, errno %d", errno);
return errno;
}
}

return 0;
}

static int sock_get_dtls_status(int fd, bool session_cache, int dtls_connection_id)
{
int err;
int status;
int len = sizeof(status);
char status_str[64];

if (session_cache) {
err = getsockopt(fd, SOL_TLS, TLS_DTLS_HANDSHAKE_STATUS, &status, &len);
if (err) {
mosh_error("Unable to get DTLS handshake status, errno %d", errno);
return -errno;
}

if (status == TLS_DTLS_HANDSHAKE_STATUS_FULL) {
sprintf(status_str, "Full");
} else if (status == TLS_DTLS_HANDSHAKE_STATUS_CACHED) {
sprintf(status_str, "Cached");
} else {
sprintf(status_str, "Unknown (%d)", status);
}
mosh_print("Handshake status: %s", status_str);
}

if (dtls_connection_id) {
err = getsockopt(fd, SOL_TLS, TLS_DTLS_CID_STATUS, &status, &len);
if (err) {
mosh_error("Unable to get DTLS connection ID status, errno %d", errno);
return -errno;
}

if (status == TLS_DTLS_CID_STATUS_DISABLED) {
sprintf(status_str, "Disabled");
} else if (status == TLS_DTLS_CID_STATUS_DOWNLINK) {
sprintf(status_str, "Downlink");
} else if (status == TLS_DTLS_CID_STATUS_UPLINK) {
sprintf(status_str, "Uplink");
} else if (status == TLS_DTLS_CID_STATUS_BIDIRECTIONAL) {
sprintf(status_str, "Bidirectional");
} else {
sprintf(status_str, "Unknown (%d)", status);
}
mosh_print("Connection ID status: %s", status_str);
}

return 0;
}

Expand Down Expand Up @@ -529,7 +589,8 @@ int sock_open_and_connect(
bool session_cache,
bool keep_open,
int peer_verify,
char *peer_hostname)
char *peer_hostname,
int dtls_connection_id)
{
int err = -EINVAL;
int proto = 0;
Expand Down Expand Up @@ -632,7 +693,8 @@ int sock_open_and_connect(

/* Set (D)TLS options */
if (secure) {
err = sock_set_tls_options(fd, sec_tag, session_cache, peer_verify, peer_hostname);
err = sock_set_tls_options(fd, sec_tag, session_cache, peer_verify, peer_hostname,
dtls_connection_id);
if (err) {
goto connect_error;
}
Expand All @@ -653,6 +715,13 @@ int sock_open_and_connect(
}
}

if (secure && type == SOCK_DGRAM) {
err = sock_get_dtls_status(fd, session_cache, dtls_connection_id);
if (err) {
goto connect_error;
}
}

/* Set socket to non-blocking mode to make sure receiving
* is not blocking polling of all sockets
*/
Expand Down
2 changes: 1 addition & 1 deletion samples/cellular/modem_shell/src/sock/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int sock_open_and_connect(
int family, int type, char *address, int port,
int bind_port, int pdn_cid, bool secure, uint32_t sec_tag,
bool session_cache, bool keep_open, int peer_verify,
char *peer_hostname);
char *peer_hostname, int dtls_connection_id);

int sock_send_data(
int socket_id, char *data, int data_length, int interval, bool packet_number_prefix,
Expand Down
19 changes: 17 additions & 2 deletions samples/cellular/modem_shell/src/sock/sock_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static const char sock_connect_usage_str[] =
"Usage: sock connect -a <address> -p <port>\n"
" [-f <family>] [-t <type>] [-b <port>] [-I <cid>] [-K]\n"
" [-S] [-T <sec_tag>] [-c] [-V <level>] [-H <hostname>]\n"
" [-C <dtls_cid>]\n"
"Options:\n"
" -a, --address, [str] Address as ip address or hostname\n"
" -p, --port, [int] Port\n"
Expand All @@ -56,6 +57,8 @@ static const char sock_connect_usage_str[] =
" -V, --peer_verify, [int] TLS peer verification level. None (0),\n"
" optional (1) or required (2). Default value is 2.\n"
" -H, --hostname, [str] Hostname for TLS peer verification.\n"
" -C, --dtls_cid, [int] Enable DTLS connection ID. Disabled (0),\n"
" supported (1) or enabled (2). Default value is 0.\n"
" -h, --help, Shows this help information";

static const char sock_close_usage_str[] =
Expand Down Expand Up @@ -197,6 +200,7 @@ static struct option long_options[] = {
{ "wait_ack", no_argument, 0, 'W' },
{ "keep_open", no_argument, 0, 'K' },
{ "print_format", required_argument, 0, 'P' },
{ "dtls_cid", required_argument, 0, 'C' },
{ "packet_number_prefix", no_argument, 0, SOCK_SHELL_OPT_PACKET_NUMBER_PREFIX },
{ "rai_last", no_argument, 0, SOCK_SHELL_OPT_RAI_LAST },
{ "rai_no_data", no_argument, 0, SOCK_SHELL_OPT_RAI_NO_DATA },
Expand All @@ -207,7 +211,7 @@ static struct option long_options[] = {
{ 0, 0, 0, 0 }
};

static const char short_options[] = "i:I:a:p:f:t:b:ST:cV:H:d:l:e:s:xrB:WKP:h";
static const char short_options[] = "i:I:a:p:f:t:b:ST:cV:H:d:l:e:s:xrB:WKP:C:h";

static void sock_print_usage(enum sock_shell_command command)
{
Expand Down Expand Up @@ -352,6 +356,7 @@ static int cmd_sock_connect(const struct shell *shell, size_t argc, char **argv)
bool arg_keep_open = false;
int arg_peer_verify = 2;
char arg_peer_hostname[SOCK_MAX_ADDR_LEN + 1];
int arg_dtls_connection_id = TLS_DTLS_CID_DISABLED;

memset(arg_address, 0, SOCK_MAX_ADDR_LEN + 1);
memset(arg_peer_hostname, 0, SOCK_MAX_ADDR_LEN + 1);
Expand Down Expand Up @@ -470,6 +475,15 @@ static int cmd_sock_connect(const struct shell *shell, size_t argc, char **argv)
}
strcpy(arg_peer_hostname, optarg);
break;
case 'C': /* DTLS connection ID */
arg_dtls_connection_id = atoi(optarg);
if (arg_dtls_connection_id < 0 || arg_dtls_connection_id > 2) {
mosh_error(
"Valid values for connection ID (%d) are 0, 1 and 2.",
arg_dtls_connection_id);
return -EINVAL;
}
break;

case 'h':
goto show_usage;
Expand Down Expand Up @@ -497,7 +511,8 @@ static int cmd_sock_connect(const struct shell *shell, size_t argc, char **argv)
arg_session_cache,
arg_keep_open,
arg_peer_verify,
arg_peer_hostname);
arg_peer_hostname,
arg_dtls_connection_id);

return err;

Expand Down

0 comments on commit db75268

Please sign in to comment.