libcoap 4.3.5-develop-5347a62
Loading...
Searching...
No Matches
coap_subscribe.c
Go to the documentation of this file.
1/* coap_subscribe.c -- subscription handling for CoAP
2 * see RFC7641
3 *
4 * Copyright (C) 2010-2019,2022-2026 Olaf Bergmann <bergmann@tzi.org>
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 *
8 * This file is part of the CoAP library libcoap. Please see
9 * README for terms of use.
10 */
11
16
18
19#ifndef min
20#define min(a,b) ((a) < (b) ? (a) : (b))
21#endif
22
23int
25#if COAP_SERVER_SUPPORT && COAP_WITH_OBSERVE_PERSIST
26 return 1;
27#else /* ! (COAP_SERVER_SUPPORT && COAP_WITH_OBSERVE_PERSIST) */
28 return 0;
29#endif /* ! (COAP_SERVER_SUPPORT && COAP_WITH_OBSERVE_PERSIST) */
30}
31
32#if COAP_SERVER_SUPPORT
33void
34coap_subscription_init(coap_subscription_t *s) {
35 assert(s);
36 memset(s, 0, sizeof(coap_subscription_t));
37}
38
39void
41 coap_observe_added_t observe_added,
42 coap_observe_deleted_t observe_deleted,
43 coap_track_observe_value_t track_observe_value,
44 coap_dyn_resource_added_t dyn_resource_added,
45 coap_resource_deleted_t resource_deleted,
46 uint32_t save_freq,
47 void *user_data) {
48 context->observe_added_cb = observe_added;
49 context->observe_deleted_cb = observe_deleted;
50 context->observe_user_data = user_data;
51 context->observe_save_freq = save_freq ? save_freq : 1;
52 context->track_observe_value_cb = track_observe_value;
53 context->dyn_resource_added_cb = dyn_resource_added;
54 context->resource_deleted_cb = resource_deleted;
55}
56
59 coap_proto_t e_proto,
60 const coap_address_t *e_listen_addr,
61 const coap_addr_tuple_t *s_addr_info,
62 const coap_bin_const_t *raw_packet,
63 const coap_bin_const_t *oscore_info) {
65
66 coap_lock_lock(return NULL);
67 subs = coap_persist_observe_add_lkd(context,
68 e_proto,
69 e_listen_addr,
70 s_addr_info,
71 raw_packet,
72 oscore_info);
74 return subs;
75}
76
78coap_persist_observe_add_lkd(coap_context_t *context,
79 coap_proto_t e_proto,
80 const coap_address_t *e_listen_addr,
81 const coap_addr_tuple_t *s_addr_info,
82 const coap_bin_const_t *raw_packet,
83 const coap_bin_const_t *oscore_info) {
84 coap_session_t *session = NULL;
85 const uint8_t *data;
86 size_t data_len;
87 coap_pdu_t *pdu = NULL;
88#if COAP_CONSTRAINED_STACK
89 /* e_packet can be protected by global_lock if needed */
90 static coap_packet_t e_packet;
91#else /* ! COAP_CONSTRAINED_STACK */
92 coap_packet_t e_packet;
93#endif /* ! COAP_CONSTRAINED_STACK */
94 coap_packet_t *packet = &e_packet;
95 coap_tick_t now;
96 coap_string_t *uri_path = NULL;
97 coap_opt_iterator_t opt_iter;
98 coap_opt_t *observe;
99 int observe_action;
102 coap_endpoint_t *ep;
103
105 if (e_listen_addr == NULL || s_addr_info == NULL || raw_packet == NULL)
106 return NULL;
107
108 /* Will be creating a local 'open' session */
109 if (e_proto != COAP_PROTO_UDP)
110 return NULL;
111
112 ep = context->endpoint;
113 while (ep) {
114 if (ep->proto == e_proto &&
115 memcmp(e_listen_addr, &ep->bind_addr, sizeof(ep->bind_addr)) == 0)
116 break;
117 ep = ep->next;
118 }
119 if (!ep) {
121#ifndef INET6_ADDRSTRLEN
122#define INET6_ADDRSTRLEN 40
123#endif
124 unsigned char addr_str[INET6_ADDRSTRLEN + 8];
125
126 if (coap_print_addr(e_listen_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
127 coap_log_warn("coap_persist_observe_add: Endpoint %s not defined\n",
128 addr_str);
129 }
130 }
131 return NULL;
132 }
133
134 /* Build up packet */
135 memcpy(&packet->addr_info, s_addr_info, sizeof(packet->addr_info));
136 packet->ifindex = 0;
137 memcpy(&packet->payload, &raw_packet->s, sizeof(packet->payload));
138 packet->length = raw_packet->length;
139
140 data = raw_packet->s;
141 data_len = raw_packet->length;
142 if (data_len < 4)
143 goto malformed;
144
145 /* Get the session */
146
147 coap_ticks(&now);
148 session = coap_endpoint_get_session(ep, packet, now);
149 if (session == NULL)
150 goto fail;
151 /* Need max space in case PDU is updated with updated token, huge size etc. */
152 pdu = coap_pdu_init(0, 0, 0, 0);
153 if (!pdu)
154 goto fail;
155
156 if (!coap_pdu_parse(session->proto, data, data_len, pdu)) {
157 goto malformed;
158 }
159 pdu->max_size = pdu->used_size;
160
161 if (pdu->code != COAP_REQUEST_CODE_GET &&
163 goto malformed;
164
165 observe = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
166 if (observe == NULL)
167 goto malformed;
168 observe_action = coap_decode_var_bytes(coap_opt_value(observe),
169 coap_opt_length(observe));
170 if (observe_action != COAP_OBSERVE_ESTABLISH)
171 goto malformed;
172
173 /* Get the resource */
174
175 uri_path = coap_get_uri_path(pdu);
176 if (!uri_path)
177 goto malformed;
178
179 r = coap_get_resource_from_uri_path_lkd(session->context,
180 (coap_str_const_t *)uri_path);
181 if (r == NULL) {
182 coap_log_warn("coap_persist_observe_add: resource '%s' not defined\n",
183 uri_path->s);
184 goto fail;
185 }
186 if (!r->observable) {
187 coap_log_warn("coap_persist_observe_add: resource '%s' not observable\n",
188 uri_path->s);
189 goto fail;
190 }
191 coap_delete_string(uri_path);
192 uri_path = NULL;
193
194 /* Create / update subscription for observing */
195 /* Now set up the subscription */
196 s = coap_add_observer(r, session, &pdu->actual_token, pdu);
197 if (s == NULL)
198 goto fail;
199
200#if COAP_OSCORE_SUPPORT
201 if (oscore_info) {
202 coap_log_debug("persist: OSCORE association being updated\n");
203 /*
204 * Need to track the association used for tracking this observe, done as
205 * a CBOR array. Written in coap_add_observer().
206 *
207 * If an entry is null, then use nil, else a set of bytes
208 *
209 * Currently tracking 5 items
210 * recipient_id
211 * id_context
212 * aad (from oscore_association_t)
213 * partial_iv (from oscore_association_t)
214 * nonce (from oscore_association_t)
215 */
216 oscore_ctx_t *osc_ctx;
217 const uint8_t *info_buf = oscore_info->s;
218 size_t info_buf_len = oscore_info->length;
219 size_t ret = 0;
220 coap_bin_const_t oscore_key_id;
221 coap_bin_const_t partial_iv;
223 coap_bin_const_t id_context;
224 coap_bin_const_t nonce;
225 int have_aad = 0;
226 int have_partial_iv = 0;
227 int have_id_context = 0;
228 int have_nonce = 0;
229
230 ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len);
231 if (ret != CBOR_ARRAY)
232 goto oscore_fail;
233 if (oscore_cbor_get_element_size(&info_buf, &info_buf_len) != 5)
234 goto oscore_fail;
235
236 /* recipient_id */
237 ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len);
238 if (ret != CBOR_BYTE_STRING)
239 goto oscore_fail;
240 oscore_key_id.length = oscore_cbor_get_element_size(&info_buf,
241 &info_buf_len);
242 if (oscore_key_id.length > info_buf_len)
243 goto oscore_fail;
244 oscore_key_id.s = info_buf;
245 info_buf += oscore_key_id.length;
246 info_buf_len -= oscore_key_id.length;
247
248 /* id_context */
249 ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len);
250 if (ret == CBOR_BYTE_STRING) {
251 id_context.length = oscore_cbor_get_element_size(&info_buf,
252 &info_buf_len);
253 if (id_context.length > info_buf_len)
254 goto oscore_fail;
255 id_context.s = info_buf;
256 info_buf += id_context.length;
257 info_buf_len -= id_context.length;
258 have_id_context = 1;
259 } else if (ret == CBOR_SIMPLE_VALUE &&
261 &info_buf_len) == CBOR_NULL) {
262 } else
263 goto oscore_fail;
264
265 /* aad */
266 ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len);
267 if (ret == CBOR_BYTE_STRING) {
268 aad.length = oscore_cbor_get_element_size(&info_buf, &info_buf_len);
269 if (aad.length > info_buf_len)
270 goto oscore_fail;
271 aad.s = info_buf;
272 info_buf += aad.length;
273 info_buf_len -= aad.length;
274 have_aad = 1;
275 } else if (ret == CBOR_SIMPLE_VALUE &&
277 &info_buf_len) == CBOR_NULL) {
278 } else
279 goto oscore_fail;
280
281 /* partial_iv */
282 ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len);
283 if (ret == CBOR_BYTE_STRING) {
284 partial_iv.length = oscore_cbor_get_element_size(&info_buf,
285 &info_buf_len);
286 if (partial_iv.length > info_buf_len)
287 goto oscore_fail;
288 partial_iv.s = info_buf;
289 info_buf += partial_iv.length;
290 info_buf_len -= partial_iv.length;
291 have_partial_iv = 1;
292 } else if (ret == CBOR_SIMPLE_VALUE &&
294 &info_buf_len) == CBOR_NULL) {
295 } else
296 goto oscore_fail;
297
298 /* nonce */
299 ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len);
300 if (ret == CBOR_BYTE_STRING) {
301 nonce.length = oscore_cbor_get_element_size(&info_buf,
302 &info_buf_len);
303 if (nonce.length > info_buf_len)
304 goto oscore_fail;
305 nonce.s = info_buf;
306 info_buf += nonce.length;
307 info_buf_len -= nonce.length;
308 have_nonce = 1;
309 } else if (ret == CBOR_SIMPLE_VALUE &&
311 &info_buf_len) == CBOR_NULL) {
312 } else
313 goto oscore_fail;
314
315 osc_ctx = oscore_find_context(session, oscore_key_id,
316 have_id_context ? &id_context : NULL, NULL,
317 &session->recipient_ctx);
318 if (osc_ctx) {
319 session->oscore_encryption = 1;
320 oscore_new_association(session, pdu, &pdu->actual_token,
321 session->recipient_ctx,
322 have_aad ? &aad : NULL,
323 have_nonce ? &nonce : NULL,
324 have_partial_iv ? &partial_iv : NULL,
325 1);
326 coap_log_debug("persist: OSCORE association added\n");
328 have_partial_iv ? &partial_iv : NULL);
329 }
330 }
331oscore_fail:
332#else /* ! COAP_OSCORE_SUPPORT */
333 (void)oscore_info;
334#endif /* ! COAP_OSCORE_SUPPORT */
336 return s;
337
338malformed:
339 coap_log_warn("coap_persist_observe_add: discard malformed PDU\n");
340fail:
341 coap_delete_string(uri_path);
343 return NULL;
344}
345
346#if COAP_WITH_OBSERVE_PERSIST
347#include <stdio.h>
348#ifdef _WIN32
349#include <windows.h>
350#endif
351
352/*
353 * read in active observe entry.
354 */
355static int
356coap_op_observe_read(FILE *fp, coap_subscription_t **observe_key,
357 coap_proto_t *e_proto, coap_address_t *e_listen_addr,
358 coap_addr_tuple_t *s_addr_info,
359 coap_bin_const_t **raw_packet, coap_bin_const_t **oscore_info) {
360 ssize_t size;
361 coap_binary_t *scratch = NULL;
362
363 assert(fp && observe_key && e_proto && e_listen_addr && s_addr_info &&
364 raw_packet && oscore_info);
365
366 *raw_packet = NULL;
367 *oscore_info = NULL;
368
369 if (fread(observe_key, sizeof(*observe_key), 1, fp) == 1) {
370 /* New record 'key proto listen addr_info len raw_packet len oscore' */
371 if (fread(e_proto, sizeof(*e_proto), 1, fp) != 1)
372 goto fail;
373 if (fread(e_listen_addr, sizeof(*e_listen_addr), 1, fp) != 1)
374 goto fail;
375 if (fread(s_addr_info, sizeof(*s_addr_info), 1, fp) != 1)
376 goto fail;
377 if (fread(&size, sizeof(size), 1, fp) != 1)
378 goto fail;
379 if (size < 0 || size > 0x10000)
380 goto fail;
381 scratch = coap_new_binary(size);
382 if ((scratch) == NULL)
383 goto fail;
384 if (fread(scratch->s, scratch->length, 1, fp) != 1)
385 goto fail;
386 *raw_packet = (coap_bin_const_t *)scratch;
387 scratch = NULL;
388 if (fread(&size, sizeof(size), 1, fp) != 1)
389 goto fail;
390 /* If size == -1, then no oscore information */
391 if (size == -1)
392 return 1;
393 else if (size < 0 || size > 0x10000)
394 goto fail;
395 else {
396 scratch = coap_new_binary(size);
397 if (scratch == NULL)
398 goto fail;
399 if (fread(scratch->s, scratch->length, 1, fp) != 1)
400 goto fail;
401 *oscore_info = (coap_bin_const_t *)scratch;
402 }
403 return 1;
404 }
405fail:
406 coap_delete_bin_const(*raw_packet);
407 coap_delete_binary(scratch);
408
409 *observe_key = NULL;
410 memset(e_proto, 0, sizeof(*e_proto));
411 memset(e_listen_addr, 0, sizeof(*e_listen_addr));
412 memset(s_addr_info, 0, sizeof(*s_addr_info));
413 *raw_packet = NULL;
414 return 0;
415}
416
417/*
418 * write out active observe entry.
419 */
420static int
421coap_op_observe_write(FILE *fp, coap_subscription_t *observe_key,
422 coap_proto_t e_proto, coap_address_t e_listen_addr,
423 coap_addr_tuple_t s_addr_info,
424 coap_bin_const_t *raw_packet, coap_bin_const_t *oscore_info) {
425 if (fwrite(&observe_key, sizeof(observe_key), 1, fp) != 1)
426 goto fail;
427 if (fwrite(&e_proto, sizeof(e_proto), 1, fp) != 1)
428 goto fail;
429 if (fwrite(&e_listen_addr, sizeof(e_listen_addr),
430 1, fp) != 1)
431 goto fail;
432 if (fwrite(&s_addr_info, sizeof(s_addr_info), 1, fp) != 1)
433 goto fail;
434 if (fwrite(&raw_packet->length, sizeof(raw_packet->length), 1, fp) != 1)
435 goto fail;
436 if (fwrite(raw_packet->s, raw_packet->length, 1, fp) != 1)
437 goto fail;
438 if (oscore_info) {
439 if (fwrite(&oscore_info->length, sizeof(oscore_info->length), 1, fp) != 1)
440 goto fail;
441 if (fwrite(oscore_info->s, oscore_info->length, 1, fp) != 1)
442 goto fail;
443 } else {
444 ssize_t not_defined = -1;
445
446 if (fwrite(&not_defined, sizeof(not_defined), 1, fp) != 1)
447 goto fail;
448 }
449 return 1;
450fail:
451 return 0;
452}
453
454/*
455 * This should be called before coap_persist_track_funcs() to prevent
456 * coap_op_observe_added() getting unnecessarily called.
457 * It should be called after init_resources() and coap_op_resource_load_disk()
458 * so that all the resources are in place.
459 */
460static void
461coap_op_observe_load_disk(coap_context_t *ctx) {
462 FILE *fp_orig = fopen((const char *)ctx->observe_save_file->s, "r");
463 FILE *fp_new = NULL;
464 coap_subscription_t *observe_key = NULL;
465 coap_proto_t e_proto;
466 coap_address_t e_listen_addr;
467 coap_addr_tuple_t s_addr_info;
468 coap_bin_const_t *raw_packet = NULL;
469 coap_bin_const_t *oscore_info = NULL;
470 char *new = NULL;
471
472 if (fp_orig == NULL)
473 goto fail;
474
475 new = coap_malloc_type(COAP_STRING, ctx->observe_save_file->length + 5);
476 if (!new)
477 goto fail;
478
479 strcpy(new, (const char *)ctx->observe_save_file->s);
480 strcat(new, ".tmp");
481 fp_new = fopen(new, "w+");
482 if (fp_new == NULL)
483 goto fail;
484
485 /* Go through and load oscore entry, updating key on the way */
486 while (1) {
487 if (!coap_op_observe_read(fp_orig, &observe_key, &e_proto, &e_listen_addr,
488 &s_addr_info, &raw_packet, &oscore_info))
489 break;
490 coap_log_debug("persist: New session/observe being created\n");
491 observe_key = coap_persist_observe_add_lkd(ctx, e_proto,
492 &e_listen_addr,
493 &s_addr_info,
494 raw_packet,
495 oscore_info);
496 if (observe_key) {
497 if (!coap_op_observe_write(fp_new, observe_key, e_proto, e_listen_addr,
498 s_addr_info, raw_packet, oscore_info))
499 goto fail;
500 }
501 coap_delete_bin_const(raw_packet);
502 raw_packet = NULL;
503 coap_delete_bin_const(oscore_info);
504 oscore_info = NULL;
505 }
506 coap_delete_bin_const(raw_packet);
507 raw_packet = NULL;
508 coap_delete_bin_const(oscore_info);
509 oscore_info = NULL;
510
511 if (fflush(fp_new) == EOF)
512 goto fail;
513 fclose(fp_new);
514 fclose(fp_orig);
515 /* Either old or new is in place */
516 if (rename(new, (const char *)ctx->observe_save_file->s) == -1) {
517 coap_log_warn("rename %s -> %s: failed: %s (%d)\n",
518 new, (const char *)ctx->observe_save_file->s,
519 coap_socket_strerror(), errno);
520 }
522 return;
523
524fail:
525 coap_delete_bin_const(raw_packet);
526 coap_delete_bin_const(oscore_info);
527 if (fp_new)
528 fclose(fp_new);
529 if (fp_orig)
530 fclose(fp_orig);
531 if (new) {
532 (void)remove(new);
533 }
535 return;
536}
537
538/*
539 * client has registered a new observe subscription request.
540 */
541static int
542coap_op_observe_added(coap_session_t *session,
543 coap_subscription_t *a_observe_key,
544 coap_proto_t a_e_proto, coap_address_t *a_e_listen_addr,
545 coap_addr_tuple_t *a_s_addr_info,
546 coap_bin_const_t *a_raw_packet,
547 coap_bin_const_t *a_oscore_info, void *user_data) {
548 FILE *fp_orig = fopen((const char *)session->context->observe_save_file->s,
549 "r");
550 FILE *fp_new = NULL;
551 coap_subscription_t *observe_key = NULL;
552 coap_proto_t e_proto;
553 coap_address_t e_listen_addr;
554 coap_addr_tuple_t s_addr_info;
555 coap_bin_const_t *raw_packet = NULL;
556 coap_bin_const_t *oscore_info = NULL;
557 char *new = NULL;
558
559 (void)user_data;
560
562 session->context->observe_save_file->length + 5);
563 if (!new)
564 goto fail;
565
566 strcpy(new, (const char *)session->context->observe_save_file->s);
567 strcat(new, ".tmp");
568 fp_new = fopen(new, "w+");
569 if (fp_new == NULL)
570 goto fail;
571
572 /* Go through and delete observe entry if it exists */
573 while (fp_orig) {
574 if (!coap_op_observe_read(fp_orig, &observe_key, &e_proto, &e_listen_addr,
575 &s_addr_info, &raw_packet, &oscore_info))
576 break;
577 if (observe_key != a_observe_key) {
578 if (!coap_op_observe_write(fp_new, observe_key, e_proto, e_listen_addr,
579 s_addr_info, raw_packet, oscore_info))
580 goto fail;
581 }
582 coap_delete_bin_const(raw_packet);
583 raw_packet = NULL;
584 coap_delete_bin_const(oscore_info);
585 oscore_info = NULL;
586 }
587 coap_delete_bin_const(raw_packet);
588 raw_packet = NULL;
589 coap_delete_bin_const(oscore_info);
590 oscore_info = NULL;
591
592 /* Add in new entry to the end */
593 if (!coap_op_observe_write(fp_new, a_observe_key, a_e_proto, *a_e_listen_addr,
594 *a_s_addr_info, a_raw_packet, a_oscore_info))
595 goto fail;
596
597 if (fflush(fp_new) == EOF)
598 goto fail;
599 fclose(fp_new);
600 if (fp_orig)
601 fclose(fp_orig);
602 /* Either old or new is in place */
603 (void)rename(new, (const char *)session->context->observe_save_file->s);
605 return 1;
606
607fail:
608 coap_delete_bin_const(raw_packet);
609 coap_delete_bin_const(oscore_info);
610 if (fp_new)
611 fclose(fp_new);
612 if (fp_orig)
613 fclose(fp_orig);
614 if (new) {
615 (void)remove(new);
616 }
618 return 0;
619}
620
621/*
622 * client has de-registered a observe subscription request.
623 */
624static int
625coap_op_observe_deleted(coap_session_t *session,
626 coap_subscription_t *d_observe_key,
627 void *user_data) {
628 FILE *fp_orig = fopen((const char *)session->context->observe_save_file->s,
629 "r");
630 FILE *fp_new = NULL;
631 coap_subscription_t *observe_key = NULL;
632 coap_proto_t e_proto;
633 coap_address_t e_listen_addr;
634 coap_addr_tuple_t s_addr_info;
635 coap_bin_const_t *raw_packet = NULL;
636 coap_bin_const_t *oscore_info = NULL;
637 char *new = NULL;
638
639 (void)user_data;
640
641 if (fp_orig == NULL)
642 goto fail;
644 session->context->observe_save_file->length + 5);
645 if (!new)
646 goto fail;
647
648 strcpy(new, (const char *)session->context->observe_save_file->s);
649 strcat(new, ".tmp");
650 fp_new = fopen(new, "w+");
651 if (fp_new == NULL)
652 goto fail;
653
654 /* Go through and locate observe entry to delete and not copy it across */
655 while (1) {
656 if (!coap_op_observe_read(fp_orig, &observe_key, &e_proto, &e_listen_addr,
657 &s_addr_info, &raw_packet, &oscore_info))
658 break;
659 if (observe_key != d_observe_key) {
660 if (!coap_op_observe_write(fp_new, observe_key, e_proto, e_listen_addr,
661 s_addr_info, (coap_bin_const_t *)raw_packet,
662 (coap_bin_const_t *)oscore_info))
663 goto fail;
664 }
665 coap_delete_bin_const(raw_packet);
666 raw_packet = NULL;
667 coap_delete_bin_const(oscore_info);
668 oscore_info = NULL;
669 }
670 coap_delete_bin_const(raw_packet);
671 raw_packet = NULL;
672 coap_delete_bin_const(oscore_info);
673 oscore_info = NULL;
674
675 if (fflush(fp_new) == EOF)
676 goto fail;
677 fclose(fp_new);
678 fclose(fp_orig);
679 /* Either old or new is in place */
680 (void)rename(new, (const char *)session->context->observe_save_file->s);
682 return 1;
683
684fail:
685 coap_delete_bin_const(raw_packet);
686 coap_delete_bin_const(oscore_info);
687 if (fp_new)
688 fclose(fp_new);
689 if (fp_orig)
690 fclose(fp_orig);
691 if (new) {
692 (void)remove(new);
693 }
695 return 0;
696}
697
698/*
699 * This should be called before coap_persist_track_funcs() to prevent
700 * coap_op_obs_cnt_track_observe() getting unnecessarily called.
701 * Should be called after coap_op_dyn_resource_load_disk() to make sure that
702 * all the resources are in the right place.
703 */
704static void
705coap_op_obs_cnt_load_disk(coap_context_t *context) {
706 FILE *fp = fopen((const char *)context->obs_cnt_save_file->s, "r");
707 char buf[1500];
708
709 if (fp == NULL)
710 return;
711
712 while (fgets(buf, sizeof(buf), fp) != NULL) {
713 char *cp = strchr(buf, ' ');
714 coap_str_const_t resource_key;
715 uint32_t observe_num;
717
718 if (!cp)
719 break;
720
721 *cp = '\000';
722 cp++;
723 observe_num = atoi(cp);
724 /*
725 * Need to assume 0 .. (context->observe_save_freq-1) have in addition
726 * been sent so need to round up to latest possible send value
727 */
728 observe_num = ((observe_num + context->observe_save_freq) /
729 context->observe_save_freq) *
730 context->observe_save_freq - 1;
731 resource_key.s = (uint8_t *)buf;
732 resource_key.length = strlen(buf);
733 r = coap_get_resource_from_uri_path_lkd(context, &resource_key);
734 if (r) {
735 coap_log_debug("persist: Initial observe number being updated '%s' %u\n",
736 buf, observe_num);
737 coap_persist_set_observe_num(r, observe_num);
738 }
739 }
740 fclose(fp);
741}
742
743/* Close both streams before replacing the counter file. */
744static int
745coap_op_obs_cnt_commit(FILE *src, FILE *dst, char *tmp, const char *path) {
746 int ok = !src || !ferror(src);
747
748 if (fclose(dst) != 0)
749 ok = 0;
750 if (src)
751 fclose(src);
752 if (ok) {
753#ifdef _WIN32
754 ok = MoveFileExA(tmp, path, MOVEFILE_REPLACE_EXISTING) != 0;
755#else
756 ok = rename(tmp, path) == 0;
757#endif
758 }
759 if (!ok) {
760 coap_log_warn("persist: could not update Observe counter file '%s'\n", path);
761 remove(tmp);
762 }
764 return ok;
765}
766
767/*
768 * Called when the observe value of a resource has been changed, but limited
769 * to be called every context->context->observe_save_freq to reduce update
770 * overheads.
771 */
772static int
773coap_op_obs_cnt_track_observe(coap_context_t *context,
774 coap_str_const_t *resource_name,
775 uint32_t n_observe_num,
776 void *user_data) {
777 FILE *fp_orig = fopen((const char *)context->obs_cnt_save_file->s, "r");
778 FILE *fp_new = NULL;
779 char buf[1500];
780 char *new = NULL;
781
782 (void)user_data;
783
784 if (!fp_orig && errno != ENOENT)
785 return 0;
786 new = coap_malloc_type(COAP_STRING, context->obs_cnt_save_file->length + 5);
787 if (!new)
788 goto fail;
789
790 strcpy(new, (const char *)context->obs_cnt_save_file->s);
791 strcat(new, ".tmp");
792 fp_new = fopen(new, "w+");
793 if (fp_new == NULL)
794 goto fail;
795
796 /* Go through and locate resource entry to update */
797 while (fp_orig && fgets(buf, sizeof(buf), fp_orig) != NULL) {
798 char *cp = strchr(buf, ' ');
799 uint32_t observe_num;
800 coap_bin_const_t resource_key;
801
802 if (!cp)
803 break;
804
805 *cp = '\000';
806 cp++;
807 observe_num = atoi(cp);
808 resource_key.s = (uint8_t *)buf;
809 resource_key.length = strlen(buf);
810 if (!coap_binary_equal(resource_name, &resource_key)) {
811 if (fprintf(fp_new, "%s %u\n", resource_key.s, observe_num) < 0)
812 goto fail;
813 }
814 }
815 if (fprintf(fp_new, "%s %u\n", resource_name->s, n_observe_num) < 0)
816 goto fail;
817 return coap_op_obs_cnt_commit(fp_orig, fp_new, new,
818 (const char *)context->obs_cnt_save_file->s);
819
820fail:
821 if (fp_new) {
822 fclose(fp_new);
823 (void)remove(new);
824 }
825 if (fp_orig)
826 fclose(fp_orig);
828 return 0;
829}
830
831/*
832 * Called when a resource has been deleted.
833 */
834static int
835coap_op_obs_cnt_deleted(coap_context_t *context,
836 coap_str_const_t *resource_name) {
837 FILE *fp_orig = fopen((const char *)context->obs_cnt_save_file->s, "r");
838 FILE *fp_new = NULL;
839 char buf[1500];
840 char *new = NULL;
841
842 if (fp_orig == NULL)
843 return errno == ENOENT;
844 new = coap_malloc_type(COAP_STRING, context->obs_cnt_save_file->length + 5);
845 if (!new)
846 goto fail;
847
848 strcpy(new, (const char *)context->obs_cnt_save_file->s);
849 strcat(new, ".tmp");
850 fp_new = fopen(new, "w+");
851 if (fp_new == NULL)
852 goto fail;
853
854 /* Go through and locate resource entry to delete */
855 while (fgets(buf, sizeof(buf), fp_orig) != NULL) {
856 char *cp = strchr(buf, ' ');
857 uint32_t observe_num;
858 coap_bin_const_t resource_key;
859
860 if (!cp)
861 break;
862
863 *cp = '\000';
864 cp++;
865 observe_num = atoi(cp);
866 resource_key.s = (uint8_t *)buf;
867 resource_key.length = strlen(buf);
868 if (!coap_binary_equal(resource_name, &resource_key)) {
869 if (fprintf(fp_new, "%s %u\n", resource_key.s, observe_num) < 0)
870 goto fail;
871 }
872 }
873 return coap_op_obs_cnt_commit(fp_orig, fp_new, new,
874 (const char *)context->obs_cnt_save_file->s);
875
876fail:
877 if (fp_new) {
878 fclose(fp_new);
879 (void)remove(new);
880 }
881 if (fp_orig)
882 fclose(fp_orig);
884 return 0;
885}
886
887#define COAP_DYN_RESOURCE_MAX_FIELD_SIZE 0x10000
888
889/*
890 * read in dynamic resource entry, allocating name & raw_packet
891 * which need to be freed off by caller. Return 1 for a record, 0 at EOF,
892 * or -1 on error (including an incomplete record).
893 */
894static int
895coap_op_dyn_resource_read(FILE *fp, coap_proto_t *e_proto,
896 coap_string_t **name,
897 coap_binary_t **raw_packet) {
898 ssize_t size;
899 size_t nread;
900
901 *name = NULL;
902 *raw_packet = NULL;
903
904 nread = fread(e_proto, 1, sizeof(*e_proto), fp);
905 if (nread == 0 && feof(fp) && !ferror(fp))
906 return 0;
907 if (nread == sizeof(*e_proto)) {
908 /* New record 'proto len resource_name len raw_packet' */
909 if (fread(&size, sizeof(size), 1, fp) != 1)
910 goto fail;
911 if (size < 0 || size > COAP_DYN_RESOURCE_MAX_FIELD_SIZE)
912 goto fail;
913 *name = coap_new_string(size);
914 if (!(*name))
915 goto fail;
916 if (fread((*name)->s, 1, size, fp) != (size_t)size)
917 goto fail;
918 if (fread(&size, sizeof(size), 1, fp) != 1)
919 goto fail;
920 if (size <= 0 || size > COAP_DYN_RESOURCE_MAX_FIELD_SIZE)
921 goto fail;
922 *raw_packet = coap_new_binary(size);
923 if (!(*raw_packet))
924 goto fail;
925 if (fread((*raw_packet)->s, size, 1, fp) != 1)
926 goto fail;
927 return 1;
928 }
929fail:
930 return -1;
931}
932
933/*
934 * write out dynamic resource entry.
935 */
936static int
937coap_op_dyn_resource_write(FILE *fp, coap_proto_t e_proto,
938 coap_str_const_t *name,
939 coap_bin_const_t *raw_packet) {
940 if (name->length > COAP_DYN_RESOURCE_MAX_FIELD_SIZE || !raw_packet->length ||
941 raw_packet->length > COAP_DYN_RESOURCE_MAX_FIELD_SIZE)
942 return 0;
943 if (fwrite(&e_proto, sizeof(e_proto), 1, fp) != 1)
944 goto fail;
945 if (fwrite(&name->length, sizeof(name->length), 1, fp) != 1)
946 goto fail;
947 if (name->length && fwrite(name->s, 1, name->length, fp) != name->length)
948 goto fail;
949 if (fwrite(&raw_packet->length, sizeof(raw_packet->length), 1, fp) != 1)
950 goto fail;
951 if (fwrite(raw_packet->s, raw_packet->length, 1, fp) != 1)
952 goto fail;
953 return 1;
954fail:
955 return 0;
956}
957
958/*
959 * This should be called before coap_persist_track_funcs() to prevent
960 * coap_op_dyn_resource_added() getting unnecessarily called.
961 *
962 * Each record 'proto len resource_name len raw_packet'
963 */
964static void
965coap_op_dyn_resource_load_disk(coap_context_t *ctx) {
966 FILE *fp_orig = NULL;
967 coap_proto_t e_proto;
968 coap_string_t *name = NULL;
969 coap_binary_t *raw_packet = NULL;
971 coap_session_t *session = NULL;
972 coap_pdu_t *request = NULL;
973 coap_pdu_t *response = NULL;
974 coap_string_t *query = NULL;
975
976 if (!ctx->unknown_resource && !ctx->dyn_create_handler)
977 return;
978
979 fp_orig = fopen((const char *)ctx->dyn_resource_save_file->s, "rb");
980 if (fp_orig == NULL)
981 return;
983 sizeof(coap_session_t));
984 if (!session)
985 goto fail;
986 memset(session, 0, sizeof(coap_session_t));
987 session->context = ctx;
988
989 /* Go through and create each dynamic resource if it does not exist*/
990 while (1) {
991 if (coap_op_dyn_resource_read(fp_orig, &e_proto, &name, &raw_packet) != 1)
992 break;
993 r = coap_get_resource_from_uri_path_lkd(ctx, (coap_str_const_t *)name);
994 if (!r) {
995 /* Create the new resource using the application logic */
996
997 coap_log_debug("persist: dynamic resource '%s' being re-created\n", name->s);
998 /*
999 * Need max space in case PDU is updated with updated token,
1000 * huge size etc.
1001 * */
1002 request = coap_pdu_init(0, 0, 0, 0);
1003 if (!request)
1004 goto fail;
1005
1006 session->proto = e_proto;
1007 if (!coap_pdu_parse(session->proto, raw_packet->s,
1008 raw_packet->length, request)) {
1009 goto fail;
1010 }
1011 if (request->code == 0 || request->code > 7) {
1012 /* Need to protect r->handler[request->code-1] */
1013 goto next;
1014 }
1015 r = coap_add_dynamic_resource(session, request);
1016 if (!r) {
1017 r = ctx->unknown_resource;
1018 }
1019 if (!r || !r->handler[request->code-1])
1020 goto next;
1021
1022 response = coap_pdu_init(0, 0, 0, 0);
1023 if (!response)
1024 goto fail;
1025 query = coap_get_query(request);
1026 /* Call the application handler to set up this dynamic resource */
1027 if (r->flags & COAP_RESOURCE_SAFE_REQUEST_HANDLER) {
1028 coap_lock_callback_release(r->handler[request->code-1](r,
1029 session, request,
1030 query, response),
1031 /* context is being freed off */
1032 goto fail);
1033 } else {
1035 r->handler[request->code-1](r,
1036 session, request,
1037 query, response),
1038 /* context is being freed off */
1039 goto fail);
1040 }
1041 coap_delete_string(query);
1042 query = NULL;
1043 coap_delete_pdu_lkd(response);
1044 response = NULL;
1045next:
1046 coap_delete_pdu_lkd(request);
1047 request = NULL;
1048 }
1049 coap_delete_string(name);
1050 coap_delete_binary(raw_packet);
1051 }
1052fail:
1053 coap_delete_string(name);
1054 coap_delete_binary(raw_packet);
1055 coap_delete_string(query);
1056 coap_delete_pdu_lkd(request);
1057 coap_delete_pdu_lkd(response);
1058 fclose(fp_orig);
1059 coap_free_type(COAP_SESSION, session);
1060}
1061
1062/* Copy the other resource records, leaving the original file intact on error. */
1063static int
1064coap_op_dyn_resource_copy(FILE *src, FILE *dst, coap_str_const_t *skip) {
1065 for (;;) {
1066 coap_proto_t proto;
1067 coap_string_t *name;
1068 coap_binary_t *packet;
1069 int status = coap_op_dyn_resource_read(src, &proto, &name, &packet);
1070
1071 if (status == 1 && !coap_string_equal(name, skip) &&
1072 !coap_op_dyn_resource_write(dst, proto, (coap_str_const_t *)name,
1073 (coap_bin_const_t *)packet))
1074 status = -1;
1075 coap_delete_string(name);
1076 coap_delete_binary(packet);
1077 if (status != 1)
1078 return status == 0;
1079 }
1080}
1081
1082static int
1083coap_op_dyn_resource_update(const coap_bin_const_t *file, coap_str_const_t *name,
1084 coap_proto_t proto, coap_bin_const_t *packet) {
1085 const char *path = (const char *)file->s;
1086 FILE *src = fopen(path, "rb");
1087 FILE *dst;
1088 char *tmp = NULL;
1089 int ok = 0;
1090
1091 if (!src) {
1092 if (errno != ENOENT)
1093 goto finish;
1094 if (!packet)
1095 return 1;
1096 }
1097
1098 tmp = coap_malloc_type(COAP_STRING, file->length + sizeof(".tmp"));
1099 if (!tmp)
1100 goto finish;
1101 memcpy(tmp, path, file->length);
1102 memcpy(tmp + file->length, ".tmp", sizeof(".tmp"));
1103 dst = fopen(tmp, "wb");
1104 if (!dst)
1105 goto finish;
1106
1107 ok = (!src || coap_op_dyn_resource_copy(src, dst, name)) &&
1108 (!packet || coap_op_dyn_resource_write(dst, proto, name, packet));
1109 if (fclose(dst) != 0)
1110 ok = 0;
1111 if (src) {
1112 fclose(src);
1113 src = NULL;
1114 }
1115 if (ok) {
1116#ifdef _WIN32
1117 ok = MoveFileExA(tmp, path, MOVEFILE_REPLACE_EXISTING) != 0;
1118#else
1119 ok = rename(tmp, path) == 0;
1120#endif
1121 }
1122 if (!ok)
1123 remove(tmp);
1124
1125finish:
1126 if (src)
1127 fclose(src);
1129 if (!ok)
1130 coap_log_warn("persist: could not update dynamic resource file '%s'\n", path);
1131 return ok;
1132}
1133
1134static int
1135coap_op_dyn_resource_added(coap_session_t *session,
1136 coap_str_const_t *resource_name,
1137 coap_bin_const_t *packet,
1138 void *user_data) {
1139 (void)user_data;
1140 return coap_op_dyn_resource_update(session->context->dyn_resource_save_file,
1141 resource_name, session->proto, packet);
1142}
1143
1144static int
1145coap_op_resource_deleted(coap_context_t *context,
1146 coap_str_const_t *resource_name,
1147 void *user_data) {
1148 int counter_ok = 1;
1149
1150 (void)user_data;
1151 if (context->obs_cnt_save_file)
1152 counter_ok = coap_op_obs_cnt_deleted(context, resource_name);
1153 if (context->dyn_resource_save_file &&
1154 !coap_op_dyn_resource_update(context->dyn_resource_save_file,
1155 resource_name, COAP_PROTO_NONE, NULL))
1156 return 0;
1157 return counter_ok;
1158}
1159
1160COAP_API int
1162 const char *dyn_resource_save_file,
1163 const char *observe_save_file,
1164 const char *obs_cnt_save_file,
1165 uint32_t save_freq) {
1166 int ret;
1167
1168 coap_lock_lock(return 0);
1169 ret = coap_persist_startup_lkd(context,
1170 dyn_resource_save_file,
1171 observe_save_file,
1172 obs_cnt_save_file,
1173 save_freq);
1175 return ret;
1176}
1177
1178int
1179coap_persist_startup_lkd(coap_context_t *context,
1180 const char *dyn_resource_save_file,
1181 const char *observe_save_file,
1182 const char *obs_cnt_save_file,
1183 uint32_t save_freq) {
1185 if (dyn_resource_save_file) {
1186 context->dyn_resource_save_file =
1187 coap_new_bin_const((const uint8_t *)dyn_resource_save_file,
1188 strlen(dyn_resource_save_file));
1189 if (!context->dyn_resource_save_file)
1190 return 0;
1191 coap_op_dyn_resource_load_disk(context);
1192 context->dyn_resource_added_cb = coap_op_dyn_resource_added;
1193 context->resource_deleted_cb = coap_op_resource_deleted;
1194 }
1195 if (obs_cnt_save_file) {
1196 context->obs_cnt_save_file =
1197 coap_new_bin_const((const uint8_t *)obs_cnt_save_file,
1198 strlen(obs_cnt_save_file));
1199 if (!context->obs_cnt_save_file)
1200 return 0;
1201 context->observe_save_freq = save_freq ? save_freq : 1;
1202 coap_op_obs_cnt_load_disk(context);
1203 context->track_observe_value_cb = coap_op_obs_cnt_track_observe;
1204 context->resource_deleted_cb = coap_op_resource_deleted;
1205 }
1206 if (observe_save_file) {
1207 context->observe_save_file =
1208 coap_new_bin_const((const uint8_t *)observe_save_file,
1209 strlen(observe_save_file));
1210 if (!context->observe_save_file)
1211 return 0;
1212 coap_op_observe_load_disk(context);
1213 context->observe_added_cb = coap_op_observe_added;
1214 context->observe_deleted_cb = coap_op_observe_deleted;
1215 }
1216 return 1;
1217}
1218
1219void
1220coap_persist_cleanup(coap_context_t *context) {
1221 coap_delete_bin_const(context->dyn_resource_save_file);
1222 coap_delete_bin_const(context->obs_cnt_save_file);
1223 coap_delete_bin_const(context->observe_save_file);
1224 context->dyn_resource_save_file = NULL;
1225 context->obs_cnt_save_file = NULL;
1226 context->observe_save_file = NULL;
1227
1228 /* Close down any tracking */
1230 NULL, 0, NULL);
1231}
1232
1233COAP_API void
1235 if (!context)
1236 return;
1237 coap_lock_lock(return);
1238 coap_persist_stop_lkd(context);
1240}
1241
1242void
1243coap_persist_stop_lkd(coap_context_t *context) {
1244 if (context == NULL)
1245 return;
1247 context->observe_no_clear = 1;
1248 coap_persist_cleanup(context);
1249}
1250#else /* ! COAP_WITH_OBSERVE_PERSIST */
1251COAP_API int
1253 const char *dyn_resource_save_file,
1254 const char *observe_save_file,
1255 const char *obs_cnt_save_file,
1256 uint32_t save_freq) {
1257 (void)context;
1258 (void)dyn_resource_save_file;
1259 (void)observe_save_file;
1260 (void)obs_cnt_save_file;
1261 (void)save_freq;
1262 return 0;
1263}
1264
1265COAP_API void
1267 context->observe_no_clear = 1;
1268 /* Close down any tracking */
1270 NULL, 0, NULL);
1271}
1272
1273#endif /* ! COAP_WITH_OBSERVE_PERSIST */
1274
1275#endif /* COAP_SERVER_SUPPORT */
#define INET6_ADDRSTRLEN
Definition coap_debug.c:259
struct coap_endpoint_t coap_endpoint_t
struct coap_subscription_t coap_subscription_t
struct coap_resource_t coap_resource_t
const char * coap_socket_strerror(void)
Definition coap_io.c:971
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_SESSION
Definition coap_mem.h:45
@ COAP_STRING
Definition coap_mem.h:33
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
#define NULL
Definition coap_option.h:30
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
@ COAP_OPTION_OBSERVE
Definition coap_option.h:75
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:149
#define COAP_RESOURCE_SAFE_REQUEST_HANDLER
Don't lock this resource when calling app call-back handler for requests as handler will not be manip...
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
Definition coap_time.c:90
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition coap_encode.c:38
#define coap_lock_specific_callback_release(lock, func, failed)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_callback_release(func, failed)
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:126
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition coap_debug.c:103
size_t coap_print_addr(const coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
Definition coap_debug.c:266
#define coap_log_warn(...)
Definition coap_debug.h:108
@ COAP_LOG_OSCORE
Definition coap_debug.h:65
@ COAP_LOG_WARN
Definition coap_debug.h:61
COAP_API coap_subscription_t * coap_persist_observe_add(coap_context_t *context, coap_proto_t e_proto, const coap_address_t *e_listen_addr, const coap_addr_tuple_t *s_addr_info, const coap_bin_const_t *raw_packet, const coap_bin_const_t *oscore_info)
Set up an active subscription for an observe that was previously active over a coap-server inadverten...
int(* coap_dyn_resource_added_t)(coap_session_t *session, coap_str_const_t *resource_name, coap_bin_const_t *raw_packet, void *user_data)
Callback handler definition called when a dynamic resource is getting created, as defined in coap_per...
void coap_persist_set_observe_num(coap_resource_t *resource, uint32_t observe_num)
Sets the current observe number value.
int(* coap_resource_deleted_t)(coap_context_t *context, coap_str_const_t *resource_name, void *user_data)
Callback handler definition called when resource is removed, as defined in coap_persist_track_funcs()...
int(* coap_observe_added_t)(coap_session_t *session, coap_subscription_t *observe_key, coap_proto_t e_proto, coap_address_t *e_listen_addr, coap_addr_tuple_t *s_addr_info, coap_bin_const_t *raw_packet, coap_bin_const_t *oscore_info, void *user_data)
Callback handler definition called when a new observe has been set up, as defined in coap_persist_tra...
#define COAP_OBSERVE_ESTABLISH
The value COAP_OBSERVE_ESTABLISH in a GET/FETCH request option COAP_OPTION_OBSERVE indicates a new ob...
COAP_API void coap_persist_stop(coap_context_t *context)
Stop tracking persist information, leaving the current persist information in the files defined in co...
void coap_persist_track_funcs(coap_context_t *context, coap_observe_added_t observe_added, coap_observe_deleted_t observe_deleted, coap_track_observe_value_t track_observe_value, coap_dyn_resource_added_t dyn_resource_added, coap_resource_deleted_t resource_deleted, uint32_t save_freq, void *user_data)
Set up callbacks to handle persist tracking so on a coap-server inadvertent restart,...
int(* coap_track_observe_value_t)(coap_context_t *context, coap_str_const_t *resource_name, uint32_t observe_num, void *user_data)
Callback handler definition called when an observe unsolicited response is being sent,...
int(* coap_observe_deleted_t)(coap_session_t *session, coap_subscription_t *observe_key, void *user_data)
Callback handler definition called when an observe is being removed, as defined in coap_persist_track...
COAP_API int coap_persist_startup(coap_context_t *context, const char *dyn_resource_save_file, const char *observe_save_file, const char *obs_cnt_save_file, uint32_t save_freq)
Start up persist tracking using the libcoap module.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
#define CBOR_BYTE_STRING
Definition oscore_cbor.h:64
#define CBOR_NULL
Definition oscore_cbor.h:74
uint64_t oscore_cbor_get_element_size(const uint8_t **buffer, size_t *buf_size)
#define CBOR_SIMPLE_VALUE
Definition oscore_cbor.h:69
uint8_t oscore_cbor_get_next_element(const uint8_t **buffer, size_t *buf_size)
#define CBOR_ARRAY
Definition oscore_cbor.h:66
int oscore_new_association(coap_session_t *session, coap_pdu_t *sent_pdu, coap_bin_const_t *token, oscore_recipient_ctx_t *recipient_ctx, coap_bin_const_t *aad, coap_bin_const_t *nonce, coap_bin_const_t *partial_iv, int is_observe)
oscore_ctx_t * oscore_find_context(coap_session_t *session, const coap_bin_const_t rcpkey_id, const coap_bin_const_t *ctxkey_id, uint8_t *oscore_r2, oscore_recipient_ctx_t **recipient_ctx)
oscore_find_context - Locate recipient context (and hence OSCORE context)
void oscore_log_hex_value(coap_log_t level, const char *name, coap_bin_const_t *value)
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:192
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:245
int coap_pdu_parse(coap_proto_t proto, const uint8_t *data, size_t length, coap_pdu_t *pdu)
Parses data into the CoAP PDU structure given in result.
Definition coap_pdu.c:1596
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition coap_pdu.c:104
@ COAP_PROTO_UDP
Definition coap_pdu.h:247
@ COAP_PROTO_NONE
Definition coap_pdu.h:246
@ COAP_REQUEST_CODE_GET
Definition coap_pdu.h:262
@ COAP_REQUEST_CODE_FETCH
Definition coap_pdu.h:266
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition coap_str.c:130
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition coap_str.c:81
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
Definition coap_str.c:119
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition coap_str.c:114
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition coap_str.h:222
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition coap_str.h:208
coap_string_t * coap_new_string(size_t size)
Returns a new string object with at least size+1 bytes storage allocated.
Definition coap_str.c:21
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition coap_str.c:50
int coap_observe_persist_is_supported(void)
Check whether Observe Persist is available.
coap_string_t * coap_get_uri_path(const coap_pdu_t *request)
Extract uri_path string from request PDU.
Definition coap_uri.c:1202
coap_string_t * coap_get_query(const coap_pdu_t *request)
Extract query string from request PDU according to escape rules in 6.5.8.
Definition coap_uri.c:1108
Multi-purpose address abstraction.
CoAP binary data definition with const data.
Definition coap_str.h:65
size_t length
length of binary data
Definition coap_str.h:66
const uint8_t * s
read-only binary data
Definition coap_str.h:67
CoAP binary data definition.
Definition coap_str.h:57
size_t length
length of binary data
Definition coap_str.h:58
uint8_t * s
binary data
Definition coap_str.h:59
The CoAP stack's global state is stored in a coap_context_t object.
coap_resource_dynamic_create_t dyn_create_handler
Dynamic resource create handler.
Iterator to run through PDU options.
size_t length
length of payload
coap_addr_tuple_t addr_info
local and remote addresses
unsigned char * payload
payload
int ifindex
the interface index
structure for CoAP PDUs
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
coap_bin_const_t actual_token
Actual token in pdu.
size_t used_size
used bytes of storage for token, options and payload
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_proto_t proto
protocol used
coap_context_t * context
session's context
CoAP string data definition with const data.
Definition coap_str.h:47
const uint8_t * s
read-only string data
Definition coap_str.h:49
size_t length
length of string
Definition coap_str.h:48
CoAP string data definition.
Definition coap_str.h:39
uint8_t * s
string data
Definition coap_str.h:41