Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 | /*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#if defined(CONFIG_NET_DEBUG_COAP)
#define SYS_LOG_DOMAIN "coap"
#define NET_LOG_ENABLED 1
#endif
#include <stdlib.h>
#include <stddef.h>
#include <zephyr/types.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <misc/byteorder.h>
#include <net/buf.h>
#include <net/net_pkt.h>
#include <misc/printk.h>
#include <net/coap.h>
#include <net/coap_link_format.h>
#define PKT_WAIT_TIME K_SECONDS(1)
static bool match_path_uri(const char * const *path,
const char *uri, u16_t len)
{
const char * const *p = NULL;
int i, j, k, plen;
if (!path) {
return false;
}
if (len <= 1 || uri[0] != '/') {
return false;
}
p = path;
plen = *p ? strlen(*p) : 0;
j = 0;
if (plen == 0) {
return false;
}
/* Go through uri and try to find a matching path */
for (i = 1; i < len; i++) {
while (*p) {
plen = strlen(*p);
k = i;
for (j = 0; j < plen; j++) {
if (uri[k] == '*') {
if ((k + 1) == len) {
return true;
}
}
if (uri[k] != (*p)[j]) {
goto next;
}
k++;
}
if (i == (k - 1) && j == plen) {
return true;
}
if (k == len && j == plen) {
return true;
}
next:
p++;
}
}
/* Did we find the resource or not */
if (i == len && !*p) {
return false;
}
return true;
}
static bool match_attributes(const char * const *attributes,
const struct coap_option *query)
{
const char * const *attr;
/* FIXME: deal with the case when there are multiple options in a
* query, for example: 'rt=lux temperature', if I want to list
* resources with resource type lux or temperature.
*/
for (attr = attributes; attr && *attr; attr++) {
u16_t attr_len = strlen(*attr);
if (query->len != attr_len) {
continue;
}
if (!strncmp((char *) query->value, *attr, attr_len)) {
return true;
}
}
return false;
}
static bool match_queries_resource(const struct coap_resource *resource,
const struct coap_option *query,
int num_queries)
{
struct coap_core_metadata *meta = resource->user_data;
const char * const *attributes = NULL;
const int href_len = strlen("href");
if (num_queries == 0) {
return true;
}
if (meta && meta->attributes) {
attributes = meta->attributes;
}
if (!attributes) {
return false;
}
if (query->len > href_len + 1 &&
!strncmp((char *) query->value, "href", href_len)) {
/* The stuff after 'href=' */
const char *uri = (char *) query->value + href_len + 1;
u16_t uri_len = query->len - (href_len + 1);
return match_path_uri(resource->path, uri, uri_len);
}
return match_attributes(attributes, query);
}
#if defined(CONFIG_COAP_WELL_KNOWN_BLOCK_WISE)
#define MAX_BLOCK_WISE_TRANSFER_SIZE 2048
enum coap_block_size default_block_size(void)
{
switch (CONFIG_COAP_WELL_KNOWN_BLOCK_WISE_SIZE) {
case 16:
return COAP_BLOCK_16;
case 32:
return COAP_BLOCK_32;
case 64:
return COAP_BLOCK_64;
case 128:
return COAP_BLOCK_128;
case 256:
return COAP_BLOCK_256;
case 512:
return COAP_BLOCK_512;
case 1024:
return COAP_BLOCK_1024;
}
return COAP_BLOCK_64;
}
static bool append_to_net_pkt(struct net_pkt *pkt, const char *str, u16_t len,
u16_t *remaining, size_t *offset, size_t current)
{
u16_t pos = 0;
bool res;
if (!*remaining) {
return true;
}
if (*offset < current) {
pos = current - *offset;
if (len >= pos) {
len -= pos;
*offset += pos;
} else {
*offset += len;
return true;
}
}
if (len > *remaining) {
len = *remaining;
}
res = net_pkt_append_all(pkt, len, str + pos, PKT_WAIT_TIME);
*remaining -= len;
*offset += len;
return res;
}
static int format_uri(const char * const *path, struct net_pkt *pkt,
u16_t *remaining, size_t *offset,
size_t current, bool *more)
{
static const char prefix[] = "</";
const char * const *p;
bool res;
if (!path) {
return -EINVAL;
}
res = append_to_net_pkt(pkt, &prefix[0], sizeof(prefix) - 1,
remaining, offset, current);
if (!res) {
return -ENOMEM;
}
if (!*remaining) {
*more = true;
return 0;
}
for (p = path; *p; ) {
u16_t path_len = strlen(*p);
res = append_to_net_pkt(pkt, *p, path_len, remaining, offset,
current);
if (!res) {
return -ENOMEM;
}
if (!*remaining) {
*more = true;
return 0;
}
p++;
if (!*p) {
continue;
}
res = append_to_net_pkt(pkt, "/", 1, remaining, offset,
current);
if (!res) {
return -ENOMEM;
}
if (!*remaining) {
*more = true;
return 0;
}
}
res = append_to_net_pkt(pkt, ">", 1, remaining, offset, current);
if (!res) {
return -ENOMEM;
}
if (!*remaining) {
*more = true;
return 0;
}
*more = false;
return 0;
}
static int format_attributes(const char * const *attributes,
struct net_pkt *pkt,
u16_t *remaining, size_t *offset,
size_t current, bool *more)
{
const char * const *attr;
bool res;
if (!attributes) {
goto terminator;
}
for (attr = attributes; *attr; ) {
int attr_len = strlen(*attr);
res = append_to_net_pkt(pkt, *attr, attr_len,
remaining, offset, current);
if (!res) {
return -ENOMEM;
}
if (!*remaining) {
*more = true;
return 0;
}
attr++;
if (!*attr) {
continue;
}
res = append_to_net_pkt(pkt, ";", 1,
remaining, offset, current);
if (!res) {
return -ENOMEM;
}
if (!*remaining) {
*more = true;
return 0;
}
}
terminator:
res = append_to_net_pkt(pkt, ";", 1, remaining, offset, current);
if (!res) {
return -ENOMEM;
}
if (!*remaining) {
*more = true;
return 0;
}
*more = false;
return 0;
}
static int format_resource(const struct coap_resource *resource,
struct net_pkt *pkt,
u16_t *remaining, size_t *offset,
size_t current, bool *more)
{
struct coap_core_metadata *meta = resource->user_data;
const char * const *attributes = NULL;
int r;
r = format_uri(resource->path, pkt, remaining, offset, current, more);
if (r < 0) {
return r;
}
if (!*remaining) {
*more = true;
return 0;
}
if (meta && meta->attributes) {
attributes = meta->attributes;
}
return format_attributes(attributes, pkt, remaining, offset, current,
more);
}
int clear_more_flag(struct coap_packet *cpkt)
{
struct net_buf *frag;
u16_t offset;
u8_t opt;
u8_t delta;
u8_t len;
frag = net_frag_skip(cpkt->frag, 0, &offset, cpkt->hdr_len);
if (!frag && offset == 0xffff) {
return -EINVAL;
}
delta = 0;
/* Note: coap_well_known_core_get() added Option (delta and len) witho
* out any extended options so parsing will not consider at the moment.
*/
while (1) {
frag = net_frag_read_u8(frag, offset, &offset, &opt);
if (!frag && offset == 0xffff) {
return -EINVAL;
}
delta += ((opt & 0xF0) >> 4);
len = (opt & 0xF);
if (delta == COAP_OPTION_BLOCK2) {
break;
}
frag = net_frag_skip(frag, offset, &offset, len);
if (!frag && offset == 0xffff) {
return -EINVAL;
}
}
/* As per RFC 7959 Sec 2.2 : NUM filed can be on 0-3 bytes.
* Skip NUM field to update M bit.
*/
if (len > 1) {
frag = net_frag_skip(frag, offset, &offset, len - 1);
if (!frag && offset == 0xffff) {
return -EINVAL;
}
}
frag->data[offset] = frag->data[offset] & 0xF7;
return 0;
}
int coap_well_known_core_get(struct coap_resource *resource,
struct coap_packet *request,
struct coap_packet *response,
struct net_pkt *pkt)
{
static struct coap_block_context ctx;
struct coap_option query;
unsigned int num_queries;
size_t offset;
u8_t token[8];
u16_t remaining;
u16_t id;
u8_t tkl;
u8_t format;
int r;
bool more = false;
if (ctx.total_size == 0) {
/* We have to iterate through resources and it's attributes,
* total size is unknown, so initialize it to
* MAX_BLOCK_WISE_TRANSFER_SIZE and update it according to
* offset.
*/
coap_block_transfer_init(&ctx, default_block_size(),
MAX_BLOCK_WISE_TRANSFER_SIZE);
}
r = coap_update_from_block(request, &ctx);
if (r < 0) {
goto end;
}
id = coap_header_get_id(request);
tkl = coap_header_get_token(request, token);
/* Per RFC 6690, Section 4.1, only one (or none) query parameter may be
* provided, use the first if multiple.
*/
r = coap_find_options(request, COAP_OPTION_URI_QUERY, &query, 1);
if (r < 0) {
goto end;
}
num_queries = r;
r = coap_packet_init(response, pkt, 1, COAP_TYPE_ACK,
tkl, token, COAP_RESPONSE_CODE_CONTENT, id);
if (r < 0) {
goto end;
}
format = 40; /* application/link-format */
r = coap_packet_append_option(response, COAP_OPTION_CONTENT_FORMAT,
&format, sizeof(format));
if (r < 0) {
goto end;
}
r = coap_append_block2_option(response, &ctx);
if (r < 0) {
goto end;
}
r = coap_packet_append_payload_marker(response);
if (r < 0) {
goto end;
}
offset = 0;
remaining = coap_block_size_to_bytes(ctx.block_size);
while (resource++ && resource->path) {
if (!remaining) {
more = true;
break;
}
if (!match_queries_resource(resource, &query, num_queries)) {
continue;
}
r = format_resource(resource, pkt, &remaining, &offset,
ctx.current, &more);
if (r < 0) {
goto end;
}
}
/* Offset is the total size now, but block2 option is already
* appended. So update only 'more' flag.
*/
if (!more) {
ctx.total_size = offset;
r = clear_more_flag(response);
}
end:
/* So it's a last block, reset context */
if (!more) {
memset(&ctx, 0, sizeof(ctx));
}
return r;
}
#else
static int format_uri(const char * const *path, struct net_pkt *pkt)
{
const char * const *p;
char *prefix = "</";
bool res;
if (!path) {
return -EINVAL;
}
res = net_pkt_append_all(pkt, strlen(prefix), (u8_t *) prefix,
PKT_WAIT_TIME);
if (!res) {
return -ENOMEM;
}
for (p = path; *p; ) {
res = net_pkt_append_all(pkt, strlen(*p), (u8_t *) *p,
PKT_WAIT_TIME);
if (!res) {
return -ENOMEM;
}
p++;
if (*p) {
res = net_pkt_append_u8_timeout(pkt, (u8_t) '/',
PKT_WAIT_TIME);
if (!res) {
return -ENOMEM;
}
}
}
res = net_pkt_append_u8_timeout(pkt, (u8_t) '>', PKT_WAIT_TIME);
if (!res) {
return -ENOMEM;
}
return 0;
}
static int format_attributes(const char * const *attributes,
struct net_pkt *pkt)
{
const char * const *attr;
bool res;
if (!attributes) {
goto terminator;
}
for (attr = attributes; *attr; ) {
res = net_pkt_append_all(pkt, strlen(*attr), (u8_t *) *attr,
PKT_WAIT_TIME);
if (!res) {
return -ENOMEM;
}
attr++;
if (*attr) {
res = net_pkt_append_u8_timeout(pkt, (u8_t) ';',
PKT_WAIT_TIME);
if (!res) {
return -ENOMEM;
}
}
}
terminator:
res = net_pkt_append_u8_timeout(pkt, (u8_t) ';', PKT_WAIT_TIME);
if (!res) {
return -ENOMEM;
}
return 0;
}
static int format_resource(const struct coap_resource *resource,
struct net_pkt *pkt)
{
struct coap_core_metadata *meta = resource->user_data;
const char * const *attributes = NULL;
int r;
r = format_uri(resource->path, pkt);
if (r < 0) {
return r;
}
if (meta && meta->attributes) {
attributes = meta->attributes;
}
return format_attributes(attributes, pkt);
}
int coap_well_known_core_get(struct coap_resource *resource,
struct coap_packet *request,
struct coap_packet *response,
struct net_pkt *pkt)
{
struct coap_option query;
u8_t token[8];
u16_t id;
u8_t tkl;
u8_t format;
u8_t num_queries;
int r;
if (!resource || !request || !response || !pkt) {
return -EINVAL;
}
id = coap_header_get_id(request);
tkl = coap_header_get_token(request, token);
/* Per RFC 6690, Section 4.1, only one (or none) query parameter may be
* provided, use the first if multiple.
*/
r = coap_find_options(request, COAP_OPTION_URI_QUERY, &query, 1);
if (r < 0) {
return r;
}
num_queries = r;
r = coap_packet_init(response, pkt, 1, COAP_TYPE_ACK,
tkl, token, COAP_RESPONSE_CODE_CONTENT, id);
if (r < 0) {
return r;
}
format = 40; /* application/link-format */
r = coap_packet_append_option(response, COAP_OPTION_CONTENT_FORMAT,
&format, sizeof(format));
if (r < 0) {
return -EINVAL;
}
r = coap_packet_append_payload_marker(response);
if (r < 0) {
return -EINVAL;
}
while (resource++ && resource->path) {
if (!match_queries_resource(resource, &query, num_queries)) {
continue;
}
r = format_resource(resource, pkt);
if (r < 0) {
return r;
}
}
return 0;
}
#endif
/* Exposing some of the APIs to CoAP unit tests in tests/net/lib/coap */
#if defined(CONFIG_COAP_TEST_API_ENABLE)
bool _coap_match_path_uri(const char * const *path,
const char *uri, u16_t len)
{
return match_path_uri(path, uri, len);
}
#endif
|