Skip to content

Commit 2f8c53c

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 2f8c53c

File tree

3 files changed

+57
-28
lines changed

3 files changed

+57
-28
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: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,29 @@ 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"
47-
" -a, --address, [str] Address as ip address or hostname\n"
48-
" -p, --port, [int] Port\n"
49-
" -f, --family, [str] Address family: 'inet' (ipv4, default),\n"
50-
" 'inet6' (ipv6) or 'packet'\n"
51-
" -t, --type, [str] Address type: 'stream' (tcp, default),\n"
52-
" 'dgram' (udp) or 'raw'\n"
53-
" -b, --bind_port, [int] Local port to bind the socket to\n"
54-
" -I, --cid, [int] Use this option to bind socket to specific\n"
55-
" PDN CID. See link command for available CIDs.\n"
56-
" -K, --keep_open Keep socket open when its PDN connection is lost.\n"
57-
" -S, --secure, Enable secure connection (TLS 1.2/DTLS 1.2).\n"
58-
" -T, --sec_tag, [int] Security tag for TLS certificate(s).\n"
59-
" -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"
62-
" -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-
" -h, --help, Shows this help information";
48+
" -a, --address, [str] Address as ip address or hostname\n"
49+
" -p, --port, [int] Port\n"
50+
" -f, --family, [str] Address family: 'inet' (ipv4, default),\n"
51+
" 'inet6' (ipv6) or 'packet'\n"
52+
" -t, --type, [str] Address type: 'stream' (tcp, default),\n"
53+
" 'dgram' (udp) or 'raw'\n"
54+
" -b, --bind_port, [int] Local port to bind the socket to\n"
55+
" -I, --cid, [int] Use this option to bind socket to specific\n"
56+
" PDN CID. See link command for available CIDs.\n"
57+
" -K, --keep_open Keep socket open when its PDN connection is lost.\n"
58+
" -S, --secure, Enable secure connection (TLS 1.2/DTLS 1.2).\n"
59+
" -T, --sec_tag, [int] Security tag for TLS certificate(s).\n"
60+
" -c, --cache, Enable TLS session cache.\n"
61+
" -V, --peer_verify, [int] TLS peer verification level: 0 (none), 1 (optional) or\n"
62+
" 2 (required, default).\n"
63+
" -H, --hostname, [str] Hostname for TLS peer verification.\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] DTLS fragmentation extension setting:\n"
67+
" 0 (disabled, default), 1 (512 bytes) or 2 (1024 bytes).\n"
68+
" -h, --help, Shows this help information";
6569

6670
static const char sock_close_usage_str[] =
6771
"Usage: sock close -i <socket id>\n"
@@ -259,6 +263,7 @@ static struct option long_options[] = {
259263
{ "peer_verify", required_argument, 0, 'V' },
260264
{ "hostname", required_argument, 0, 'H' },
261265
{ "dtls_cid", required_argument, 0, 'C' },
266+
{ "dtls_frag_ext", required_argument, 0, 'F' },
262267
{ "data", required_argument, 0, 'd' },
263268
{ "length", required_argument, 0, 'l' },
264269
{ "period", required_argument, 0, 'e' },
@@ -282,7 +287,7 @@ static struct option long_options[] = {
282287
{ 0, 0, 0, 0 }
283288
};
284289

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";
290+
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";
286291

287292
static void sock_print_usage(enum sock_shell_command command)
288293
{
@@ -438,6 +443,7 @@ static int cmd_sock_connect(const struct shell *shell, size_t argc, char **argv)
438443
int arg_peer_verify = 2;
439444
char arg_peer_hostname[SOCK_MAX_ADDR_LEN + 1];
440445
int arg_dtls_cid = 0;
446+
int arg_dtls_frag_ext = 0;
441447

442448
memset(arg_address, 0, SOCK_MAX_ADDR_LEN + 1);
443449
memset(arg_peer_hostname, 0, SOCK_MAX_ADDR_LEN + 1);
@@ -565,7 +571,15 @@ static int cmd_sock_connect(const struct shell *shell, size_t argc, char **argv)
565571
return -EINVAL;
566572
}
567573
break;
568-
574+
case 'F': /* DTLS fragmentation extension */
575+
arg_dtls_frag_ext = atoi(optarg);
576+
if (arg_dtls_frag_ext < 0 || arg_dtls_frag_ext > 2) {
577+
mosh_error(
578+
"Valid values for DTLS fragmentation extension (%d) are "
579+
"0, 1 and 2.", arg_dtls_frag_ext);
580+
return -EINVAL;
581+
}
582+
break;
569583
case 'h':
570584
goto show_usage;
571585
case '?':
@@ -593,7 +607,8 @@ static int cmd_sock_connect(const struct shell *shell, size_t argc, char **argv)
593607
arg_keep_open,
594608
arg_peer_verify,
595609
arg_peer_hostname,
596-
arg_dtls_cid);
610+
arg_dtls_cid,
611+
arg_dtls_frag_ext);
597612

598613
return err;
599614

0 commit comments

Comments
 (0)