Skip to content

Commit 72623a2

Browse files
committed
samples: cellular: modem_shell: Add support for DTLS frag extension
Added new command line option to the "socket connect" command for configuring the DTLS fragmentation extension. Signed-off-by: Tommi Kangas <[email protected]>
1 parent 18ee80d commit 72623a2

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

samples/cellular/modem_shell/src/sock/sock.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ static int sock_set_tls_options(
352352
bool session_cache,
353353
int peer_verify,
354354
char *peer_hostname,
355-
int dtls_cid)
355+
int dtls_cid,
356+
int dtls_frag_ext)
356357
{
357358
int err;
358359
uint32_t sec_tag_list[] = { sec_tag };
@@ -409,14 +410,25 @@ static int sock_set_tls_options(
409410
}
410411

411412
/* DTLS CID */
412-
if (dtls_cid != NRF_SO_SEC_DTLS_CID_DISABLED) {
413+
if (dtls_cid != TLS_DTLS_CID_STATUS_DISABLED) {
413414
err = setsockopt(fd, SOL_TLS, TLS_DTLS_CID, &dtls_cid, sizeof(dtls_cid));
414415
if (err) {
415416
mosh_error("Unable to set DTLS CID option, errno %d", errno);
416417
return errno;
417418
}
418419
}
419420

421+
/* DTLS fragmentation extension */
422+
if (dtls_frag_ext != DTLS_FRAG_EXT_DISABLED) {
423+
err = setsockopt(fd, SOL_TLS, TLS_DTLS_FRAG_EXT, &dtls_frag_ext,
424+
sizeof(dtls_frag_ext));
425+
if (err) {
426+
mosh_error("Unable to set DTLS fragmentation extension option, errno %d",
427+
errno);
428+
return errno;
429+
}
430+
}
431+
420432
return 0;
421433
}
422434

@@ -538,7 +550,8 @@ int sock_open_and_connect(
538550
bool keep_open,
539551
int peer_verify,
540552
char *peer_hostname,
541-
int dtls_cid)
553+
int dtls_cid,
554+
int dtls_frag_ext)
542555
{
543556
int err = -EINVAL;
544557
int proto = 0;
@@ -549,8 +562,9 @@ int sock_open_and_connect(
549562
family, type, port, bind_port, pdn_cid, address);
550563
if (secure) {
551564
mosh_print(" secure=%d, sec_tag=%u, session_cache=%d, "
552-
"peer_verify=%d, peer_hostname=%s, dtls_cid=%d",
553-
secure, sec_tag, session_cache, peer_verify, peer_hostname, dtls_cid);
565+
"peer_verify=%d, peer_hostname=%s, dtls_cid=%d, dtls_frag_ext=%d",
566+
secure, sec_tag, session_cache, peer_verify, peer_hostname, dtls_cid,
567+
dtls_frag_ext);
554568
}
555569

556570
/* Reserve socket ID and structure for a new connection */
@@ -642,7 +656,7 @@ int sock_open_and_connect(
642656
/* Set (D)TLS options */
643657
if (secure) {
644658
err = sock_set_tls_options(fd, sec_tag, session_cache, peer_verify,
645-
peer_hostname, dtls_cid);
659+
peer_hostname, dtls_cid, dtls_frag_ext);
646660
if (err) {
647661
goto connect_error;
648662
}

samples/cellular/modem_shell/src/sock/sock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int sock_open_and_connect(
2727
int family, int type, char *address, int port,
2828
int bind_port, int pdn_cid, bool secure, uint32_t sec_tag,
2929
bool session_cache, bool keep_open, int peer_verify,
30-
char *peer_hostname, int dtls_cid);
30+
char *peer_hostname, int dtls_cid, int dtls_frag_ext);
3131

3232
int sock_send_data(
3333
int socket_id, char *data, int data_length, int interval, bool packet_number_prefix,

samples/cellular/modem_shell/src/sock/sock_shell.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static const char sock_connect_usage_str[] =
4343
"Usage: sock connect -a <address> -p <port>\n"
4444
" [-f <family>] [-t <type>] [-b <port>] [-I <cid>] [-K]\n"
4545
" [-S] [-T <sec_tag>] [-c] [-V <level>] [-H <hostname>]\n"
46+
" [-C <dtls_cid>] [-F <dtls_frag_ext>]\n"
4647
"Options:\n"
4748
" -a, --address, [str] Address as ip address or hostname\n"
4849
" -p, --port, [int] Port\n"
@@ -57,10 +58,14 @@ static const char sock_connect_usage_str[] =
5758
" -S, --secure, Enable secure connection (TLS 1.2/DTLS 1.2).\n"
5859
" -T, --sec_tag, [int] Security tag for TLS certificate(s).\n"
5960
" -c, --cache, Enable TLS session cache.\n"
60-
" -V, --peer_verify, [int] TLS peer verification level. None (0),\n"
61-
" optional (1) or required (2). Default value is 2.\n"
61+
" -V, --peer_verify, [int] TLS peer verification level: 0 (none), 1 (optional) or\n"
62+
" 2 (required, default).\n"
6263
" -H, --hostname, [str] Hostname for TLS peer verification.\n"
63-
" -C, --dtls_cid, [int] DTLS CID setting: 0 (disabled), 1 (supported), 2 (enabled).\n"
64+
" -C, --dtls_cid, [int] DTLS CID setting: 0 (disabled, default), 1 (supported) or\n"
65+
" 2 (enabled).\n"
66+
" -F, --dtls_frag_ext, [int]\n"
67+
" DTLS fragmentation extension setting:\n"
68+
" 0 (disabled, default), 1 (512 bytes) or 2 (1024 bytes).\n"
6469
" -h, --help, Shows this help information";
6570

6671
static const char sock_close_usage_str[] =
@@ -259,6 +264,7 @@ static struct option long_options[] = {
259264
{ "peer_verify", required_argument, 0, 'V' },
260265
{ "hostname", required_argument, 0, 'H' },
261266
{ "dtls_cid", required_argument, 0, 'C' },
267+
{ "dtls_frag_ext", required_argument, 0, 'F' },
262268
{ "data", required_argument, 0, 'd' },
263269
{ "length", required_argument, 0, 'l' },
264270
{ "period", required_argument, 0, 'e' },
@@ -282,7 +288,7 @@ static struct option long_options[] = {
282288
{ 0, 0, 0, 0 }
283289
};
284290

285-
static const char short_options[] = "i:I:a:p:f:t:b:ST:cV:H:C:d:l:e:s:xrB:WKP:o:v:h";
291+
static const char short_options[] = "i:I:a:p:f:t:b:ST:cV:H:C:F:d:l:e:s:xrB:WKP:o:v:h";
286292

287293
static void sock_print_usage(enum sock_shell_command command)
288294
{
@@ -438,6 +444,7 @@ static int cmd_sock_connect(const struct shell *shell, size_t argc, char **argv)
438444
int arg_peer_verify = 2;
439445
char arg_peer_hostname[SOCK_MAX_ADDR_LEN + 1];
440446
int arg_dtls_cid = 0;
447+
int arg_dtls_frag_ext = 0;
441448

442449
memset(arg_address, 0, SOCK_MAX_ADDR_LEN + 1);
443450
memset(arg_peer_hostname, 0, SOCK_MAX_ADDR_LEN + 1);
@@ -565,7 +572,15 @@ static int cmd_sock_connect(const struct shell *shell, size_t argc, char **argv)
565572
return -EINVAL;
566573
}
567574
break;
568-
575+
case 'F': /* DTLS fragmentation extension */
576+
arg_dtls_frag_ext = atoi(optarg);
577+
if (arg_dtls_frag_ext < 0 || arg_dtls_frag_ext > 2) {
578+
mosh_error(
579+
"Valid values for DTLS fragmentation extension (%d) are "
580+
"0, 1 and 2.", arg_dtls_frag_ext);
581+
return -EINVAL;
582+
}
583+
break;
569584
case 'h':
570585
goto show_usage;
571586
case '?':
@@ -593,7 +608,8 @@ static int cmd_sock_connect(const struct shell *shell, size_t argc, char **argv)
593608
arg_keep_open,
594609
arg_peer_verify,
595610
arg_peer_hostname,
596-
arg_dtls_cid);
611+
arg_dtls_cid,
612+
arg_dtls_frag_ext);
597613

598614
return err;
599615

0 commit comments

Comments
 (0)