Skip to content

Commit

Permalink
coap_block.c: Support server setting max block size for Block1/Block2
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdeep1 committed Nov 29, 2023
1 parent 8596191 commit ac9009e
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 28 deletions.
14 changes: 8 additions & 6 deletions examples/coap-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ usage(const char *program, const char *version) {
"General Options\n"
"\t-a addr\t\tThe local interface address to use\n"
"\t-b [num,]size\tBlock size to be used in GET/PUT/POST requests\n"
"\t \t\t(value must be a multiple of 16 not larger than 1024)\n"
"\t \t\t(value must be 16, 32, 64, 128, 256, 512 or 1024)\n"
"\t \t\tIf num is present, the request chain will start at\n"
"\t \t\tblock num\n"
"\t-e text\t\tInclude text as payload (use percent-encoding for\n"
Expand Down Expand Up @@ -591,7 +591,8 @@ usage(const char *program, const char *version) {
"\t \t\tcertificate in '-c certfile' if the parameter is\n"
"\t \t\tdifferent from certfile in '-c certfile'\n"
"\t-n \t\tDisable remote peer certificate checking\n"
"\t-C cafile\tPEM file or PKCS11 URI for the CA certificate that was\n"
"\t-C cafile\tPEM file or PKCS11 URI for the CA certificate and any\n"
"\t \t\tintermediate CAs that was\n"
"\t \t\tused to sign the server certfile. Ideally the client\n"
"\t \t\tcertificate should be signed by the same CA so that\n"
"\t \t\tmutual authentication can take place. The contents of\n"
Expand All @@ -612,7 +613,7 @@ usage(const char *program, const char *version) {
"\t \t\tUsing '-R trust_casfile' disables common CA mutual\n"
"\t \t\tauthentication which can only be done by using\n"
"\t \t\t'-C cafile'.\n"
"\t \t\tUsing the -C or -R options will will trigger the\n"
"\t \t\tUsing the -C or -R options will trigger the\n"
"\t \t\tvalidation of the server certificate unless overridden\n"
"\t \t\tby the -n option\n"
);
Expand Down Expand Up @@ -888,8 +889,8 @@ cmdline_blocksize(char *arg) {
} else if (size > 1024) {
coap_log_warn("Maximum block size is 1024\n");
return 0;
} else if ((size % 16) != 0) {
coap_log_warn("Block size %u is not a multiple of 16\n", size);
} else if (size != ((1 << (coap_fls(size >> 4) - 1) << 4))) {
coap_log_warn("Block size %u invalid\n", size);
return 0;
}
if (size)
Expand Down Expand Up @@ -1617,7 +1618,8 @@ main(int argc, char **argv) {
node_str[NI_MAXHOST - 1] = '\0';
break;
case 'b':
cmdline_blocksize(optarg);
if (!cmdline_blocksize(optarg))
goto failed;
break;
case 'B':
wait_seconds = atoi(optarg);
Expand Down
18 changes: 13 additions & 5 deletions examples/coap-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -2132,10 +2132,10 @@ usage(const char *program, const char *version) {
coap_string_tls_version(buffer, sizeof(buffer)));
fprintf(stderr, "%s\n", coap_string_tls_support(buffer, sizeof(buffer)));
fprintf(stderr, "\n"
"Usage: %s [-a priority] [-d max] [-e] [-g group] [-l loss] [-p port]\n"
"\t\t[-r] [-v num] [-w [port][,secure_port]] [-A address]\n"
"\t\t[-E oscore_conf_file[,seq_file]] [-G group_if] [-L value] [-N]\n"
"\t\t[-P scheme://address[:port],[name1[,name2..]]]\n"
"Usage: %s [-a priority] [-b max_block_size] [-d max] [-e] [-g group]\n"
"\t\t[-l loss] [-p port] [-r] [-v num] [-w [port][,secure_port]]\n"
"\t\t[-A address] [-E oscore_conf_file[,seq_file]] [-G group_if]\n"
"\t\t[-L value] [-N] [-P scheme://address[:port],[name1[,name2..]]]\n"
"\t\t[-T max_token_size] [-U type] [-V num] [-X size]\n"
"\t\t[[-h hint] [-i match_identity_file] [-k key]\n"
"\t\t[-s match_psk_sni_file] [-u user]]\n"
Expand All @@ -2144,6 +2144,9 @@ usage(const char *program, const char *version) {
"\t\t[-S match_pki_sni_file]]\n"
"General Options\n"
"\t-a priority\tSend logging output to syslog at priority (0-7) level\n"
"\t-b max_block_size\n"
"\t \t\tMaximum block size server supports (16, 32, 64,\n"
"\t \t\t128, 256, 512 or 1024) in bytes\n"
"\t-d max \t\tAllow dynamic creation of up to a total of max\n"
"\t \t\tresources. If max is reached, a 4.06 code is returned\n"
"\t \t\tuntil one of the dynamic resources has been deleted\n"
Expand Down Expand Up @@ -2742,6 +2745,7 @@ main(int argc, char **argv) {
int nfds = 0;
size_t i;
int exit_code = 0;
uint32_t max_block_size = 0;
#ifndef _WIN32
int use_syslog = 0;
#endif /* ! _WIN32 */
Expand All @@ -2762,7 +2766,7 @@ main(int argc, char **argv) {
clock_offset = time(NULL);

while ((opt = getopt(argc, argv,
"a:c:d:eg:G:h:i:j:J:k:l:mnp:rs:tu:v:w:A:C:E:L:M:NP:R:S:T:U:V:X:")) != -1) {
"a:b:c:d:eg:G:h:i:j:J:k:l:mnp:rs:tu:v:w:A:C:E:L:M:NP:R:S:T:U:V:X:")) != -1) {
switch (opt) {
#ifndef _WIN32
case 'a':
Expand All @@ -2776,6 +2780,9 @@ main(int argc, char **argv) {
strncpy(addr_str, optarg, NI_MAXHOST-1);
addr_str[NI_MAXHOST - 1] = '\0';
break;
case 'b':
max_block_size = atoi(optarg);
break;
case 'c' :
cert_file = optarg;
break;
Expand Down Expand Up @@ -2961,6 +2968,7 @@ main(int argc, char **argv) {
if (mcast_per_resource)
coap_mcast_per_resource(ctx);
coap_context_set_block_mode(ctx, block_mode);
coap_context_set_max_block_size(ctx, max_block_size);
if (csm_max_message_size)
coap_context_set_csm_max_message_size(ctx, csm_max_message_size);
if (doing_oscore) {
Expand Down
37 changes: 37 additions & 0 deletions include/coap3/coap_block.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ typedef struct {
#define COAP_BLOCK_NO_PREEMPTIVE_RTAG 0x10 /* (cl) Don't use pre-emptive Request-Tags */
#define COAP_BLOCK_STLESS_FETCH 0x20 /* (cl) Assume server supports stateless FETCH */
#define COAP_BLOCK_STLESS_BLOCK2 0x40 /* (svr)Server is stateless for handling Block2 */

#if COAP_Q_BLOCK_SUPPORT
#define COAP_BLOCK_SET_MASK (COAP_BLOCK_USE_LIBCOAP | \
COAP_BLOCK_SINGLE_BODY | \
COAP_BLOCK_TRY_Q_BLOCK | \
COAP_BLOCK_USE_M_Q_BLOCK | \
COAP_BLOCK_NO_PREEMPTIVE_RTAG | \
COAP_BLOCK_STLESS_FETCH | \
COAP_BLOCK_STLESS_BLOCK2)
#else /* ! COAP_Q_BLOCK_SUPPORT */
#define COAP_BLOCK_SET_MASK (COAP_BLOCK_USE_LIBCOAP | \
COAP_BLOCK_SINGLE_BODY | \
COAP_BLOCK_NO_PREEMPTIVE_RTAG | \
COAP_BLOCK_STLESS_FETCH | \
COAP_BLOCK_STLESS_BLOCK2)
#endif /* ! COAP_Q_BLOCK_SUPPORT */

#define COAP_BLOCK_MAX_SIZE_MASK 0x700 /* (svr)Mask to get the max supported block size */
#define COAP_BLOCK_MAX_SIZE_SHIFT 8 /* (svr)Mask shift to get the max supported block size */
#define COAP_BLOCK_MAX_SIZE_GET(a) (((a) & COAP_BLOCK_MAX_SIZE_MASK) >> COAP_BLOCK_MAX_SIZE_SHIFT)
#define COAP_BLOCK_MAX_SIZE_SET(a) (((a) << COAP_BLOCK_MAX_SIZE_SHIFT) & COAP_BLOCK_MAX_SIZE_MASK)
/* Note 0x4000 and 0x8000 are internally defined elsewhere */

/**
Expand Down Expand Up @@ -424,6 +445,22 @@ int coap_add_data_large_response(coap_resource_t *resource,
void coap_context_set_block_mode(coap_context_t *context,
uint32_t block_mode);

/**
* Set the context level maximum block size that the server supports when sending
* or receiving packets with Block1 or Block2 options.
* This maximum block size flows down to a session when a session is created.
*
* Note: This function must be called before the session is set up.
*
* Note: COAP_BLOCK_USE_LIBCOAP must be set using coap_context_set_block_mode()
* if libcoap is to do this work.
*
* @param context The coap_context_t object.
* @param max_block_size The maximum block size a server supports. Can be 0
* (reset), or must be 16, 32, 64, 128, 256, 512 or 1024.
*/
int coap_context_set_max_block_size(coap_context_t *context, size_t max_block_size);

/**
* Cancel an observe that is being tracked by the client large receive logic.
* (coap_context_set_block_mode() has to be called)
Expand Down
3 changes: 3 additions & 0 deletions include/coap3/coap_threadsafe_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#define coap_check_notify(s) coap_check_notify_locked(s)
#define coap_context_oscore_server(c,o) coap_context_oscore_server_locked(c,o)
#define coap_context_set_block_mode(c,b) coap_context_set_block_mode_locked(c,b)
#define coap_context_set_max_block_size(c,m) coap_context_set_max_block_size_locked(c,m)
#define coap_context_set_pki(c,s) coap_context_set_pki_locked(c,s)
#define coap_context_set_pki_root_cas(c,f,d) coap_context_set_pki_root_cas_locked(c,f,d)
#define coap_context_set_psk(c,h,k,l) coap_context_set_psk_locked(c,h,k,l)
Expand Down Expand Up @@ -125,6 +126,8 @@ int coap_context_oscore_server_locked(coap_context_t *context,
coap_oscore_conf_t *oscore_conf);
void coap_context_set_block_mode_locked(coap_context_t *context,
uint32_t block_mode);
int coap_context_set_max_block_size_locked(coap_context_t *context,
size_t max_block_size);
int coap_context_set_pki_locked(coap_context_t *ctx,
const coap_dtls_pki_t *setup_data);
int coap_context_set_pki_root_cas_locked(coap_context_t *ctx,
Expand Down
1 change: 1 addition & 0 deletions libcoap-3.map
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ global:
coap_context_set_csm_timeout;
coap_context_set_csm_timeout_ms;
coap_context_set_keepalive;
coap_context_set_max_block_size;
coap_context_set_max_handshake_sessions;
coap_context_set_max_idle_sessions;
coap_context_set_max_token_size;
Expand Down
1 change: 1 addition & 0 deletions libcoap-3.sym
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ coap_context_set_csm_max_message_size
coap_context_set_csm_timeout
coap_context_set_csm_timeout_ms
coap_context_set_keepalive
coap_context_set_max_block_size
coap_context_set_max_handshake_sessions
coap_context_set_max_idle_sessions
coap_context_set_max_token_size
Expand Down
10 changes: 5 additions & 5 deletions man/coap-client.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ OPTIONS - General
cause "No such device" errors on transmission.

*-b* [num,]size::
The block size to be used in GET/PUT/POST requests (value must be a
multiple of 16 not larger than 1024 as libcoap uses a fixed maximum
PDU size of 1400 bytes). If 'num' is present, the request
The block size to be used in GET/PUT/POST requests (value must be 16, 32,
64, 128, 256, 512 or 1024). If 'num' is present, the request
chain will start at block 'num'. When the server includes a Block2
option in its response to a GET request, coap-client will automatically
retrieve the subsequent block from the server until there are no more
Expand Down Expand Up @@ -232,7 +231,8 @@ definitions have to be in DER, not PEM, format. Otherwise all of
Disable remote peer certificate checking.

*-C* cafile::
PEM file or PKCS11 URI for the CA certificate that was used to sign the server
PEM file or PKCS11 URI for the CA certificate and any intermediate CAs that was
used to sign the server
certfile. Ideally the client certificate should be signed by the same CA so
that mutual authentication can take place. The contents of cafile are added
to the trusted store of root CAs. Using the *-C* or *-R* options will trigger
Expand All @@ -252,7 +252,7 @@ PEM file or PKCS11 URI for the CA certificate that was used to sign the server
directory containing a set of CA PEM files. The *-C cafile* CA does not have
to be in this list and is trusted for the validation. Using
*-R trust_casfile* disables common CA mutual authentication which can only
be done by using *-C cafile*. Using the *-C* or *-R* options will will
be done by using *-C cafile*. Using the *-C* or *-R* options will
trigger the validation of the server certificate unless overridden by the
*-n* option.

Expand Down
7 changes: 6 additions & 1 deletion man/coap-server.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ coap-server-notls

SYNOPSIS
--------
*coap-server* [*-a* priority] [*-d* max] [*-e*] [*-g* group] [*-l* loss] [*-p* port]
*coap-server* [*-a* priority] [*-b* max_block_size] [*-d* max] [*-e*]
[*-g* group] [*-l* loss] [*-p* port]
[*-r*] [*-t*] [*-v* num] [*-w* [port][,secure_port]]
[*-A* address] [*-E* oscore_conf_file[,seq_file]]
[*-G* group_if] [*-L* value] [*-N*]
Expand All @@ -46,6 +47,10 @@ OPTIONS - General
*-a* priority::
Send logging output to syslog at 'priority' (0-7) level.

*-b* max_block_size::
Maximum block size server supports (16, 32, 64, 128, 256, 512 or 1024) in
bytes.

*-d* max::
Enable support for creation of dynamic resources when doing a PUT up to a
limit of 'max'. If 'max' is reached, a 4.06 code is returned until one of
Expand Down
12 changes: 12 additions & 0 deletions man/coap_block.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ NAME
----
coap_block,
coap_context_set_block_mode,
coap_context_set_max_block_size,
coap_add_data_large_request,
coap_add_data_large_response,
coap_get_data_large,
Expand All @@ -26,6 +27,9 @@ SYNOPSIS
*void coap_context_set_block_mode(coap_context_t *_context_,
uint32_t _block_mode_);*

*int coap_context_set_max_block_size(coap_context_t *_context_,
size_t _max_block_size_);*

*int coap_add_data_large_request(coap_session_t *_session_,
coap_pdu_t *_pdu_, size_t _length_, const uint8_t *_data_,
coap_release_large_data_t _release_func_, void *_app_ptr_);*
Expand Down Expand Up @@ -209,6 +213,12 @@ for any data returned that uses Block2 to split up the data chunks.
The application is called for every request to get the complete set of data which
is then split into the separate Block2 responses as appropriate.

*Function: coap_context_set_max_block_size()*

The *coap_context_set_max_block_size*() function is used to set the
_max_block_size_ in the _context_ that a server supports when the Block1 or
Block2 options are used. This must be set before a server session is created.

*Function: coap_add_data_large_request()*

The *coap_add_data_large_request*() function is similar to *coap_add_data*(),
Expand Down Expand Up @@ -316,6 +326,8 @@ RETURN VALUES
*coap_block_build_body*() returns the current state of the body's data
(which may have some missing gaps) or NULL on error.

*coap_context_set_max_block_size*() returns 0 on failure, 1 on success.

*coap_q_block_is_supported*() returns 0 on failure, 1 on success.

EXAMPLES
Expand Down
Loading

0 comments on commit ac9009e

Please sign in to comment.