/src/libcoap/src/coap_subscribe.c
Line | Count | Source |
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 | | |
12 | | /** |
13 | | * @file coap_subscribe.c |
14 | | * @brief Subscription handling functions |
15 | | */ |
16 | | |
17 | | #include "coap3/coap_libcoap_build.h" |
18 | | |
19 | | #ifndef min |
20 | | #define min(a,b) ((a) < (b) ? (a) : (b)) |
21 | | #endif |
22 | | |
23 | | int |
24 | 2.52k | coap_observe_persist_is_supported(void) { |
25 | 2.52k | #if COAP_SERVER_SUPPORT && COAP_WITH_OBSERVE_PERSIST |
26 | 2.52k | return 1; |
27 | | #else /* ! (COAP_SERVER_SUPPORT && COAP_WITH_OBSERVE_PERSIST) */ |
28 | | return 0; |
29 | | #endif /* ! (COAP_SERVER_SUPPORT && COAP_WITH_OBSERVE_PERSIST) */ |
30 | 2.52k | } |
31 | | |
32 | | #if COAP_SERVER_SUPPORT |
33 | | void |
34 | 2.52k | coap_subscription_init(coap_subscription_t *s) { |
35 | 2.52k | assert(s); |
36 | 2.52k | memset(s, 0, sizeof(coap_subscription_t)); |
37 | 2.52k | } |
38 | | |
39 | | void |
40 | | coap_persist_track_funcs(coap_context_t *context, |
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 | 5.05k | void *user_data) { |
48 | 5.05k | context->observe_added_cb = observe_added; |
49 | 5.05k | context->observe_deleted_cb = observe_deleted; |
50 | 5.05k | context->observe_user_data = user_data; |
51 | 5.05k | context->observe_save_freq = save_freq ? save_freq : 1; |
52 | 5.05k | context->track_observe_value_cb = track_observe_value; |
53 | 5.05k | context->dyn_resource_added_cb = dyn_resource_added; |
54 | 5.05k | context->resource_deleted_cb = resource_deleted; |
55 | 5.05k | } |
56 | | |
57 | | COAP_API coap_subscription_t * |
58 | | coap_persist_observe_add(coap_context_t *context, |
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 | 0 | const coap_bin_const_t *oscore_info) { |
64 | 0 | coap_subscription_t *subs; |
65 | |
|
66 | 0 | coap_lock_lock(return NULL); |
67 | 0 | subs = coap_persist_observe_add_lkd(context, |
68 | 0 | e_proto, |
69 | 0 | e_listen_addr, |
70 | 0 | s_addr_info, |
71 | 0 | raw_packet, |
72 | 0 | oscore_info); |
73 | 0 | coap_lock_unlock(); |
74 | 0 | return subs; |
75 | 0 | } |
76 | | |
77 | | coap_subscription_t * |
78 | | coap_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 | 2.03k | const coap_bin_const_t *oscore_info) { |
84 | 2.03k | coap_session_t *session = NULL; |
85 | 2.03k | const uint8_t *data; |
86 | 2.03k | size_t data_len; |
87 | 2.03k | 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 | 2.03k | coap_packet_t e_packet; |
93 | 2.03k | #endif /* ! COAP_CONSTRAINED_STACK */ |
94 | 2.03k | coap_packet_t *packet = &e_packet; |
95 | 2.03k | coap_tick_t now; |
96 | 2.03k | coap_string_t *uri_path = NULL; |
97 | 2.03k | coap_opt_iterator_t opt_iter; |
98 | 2.03k | coap_opt_t *observe; |
99 | 2.03k | int observe_action; |
100 | 2.03k | coap_resource_t *r; |
101 | 2.03k | coap_subscription_t *s = NULL; |
102 | 2.03k | coap_endpoint_t *ep; |
103 | | |
104 | 2.03k | coap_lock_check_locked(); |
105 | 2.03k | if (e_listen_addr == NULL || s_addr_info == NULL || raw_packet == NULL) |
106 | 0 | return NULL; |
107 | | |
108 | | /* Will be creating a local 'open' session */ |
109 | 2.03k | if (e_proto != COAP_PROTO_UDP) |
110 | 1.03k | return NULL; |
111 | | |
112 | 1.00k | ep = context->endpoint; |
113 | 2.00k | while (ep) { |
114 | 1.00k | if (ep->proto == e_proto && |
115 | 1.00k | memcmp(e_listen_addr, &ep->bind_addr, sizeof(ep->bind_addr)) == 0) |
116 | 0 | break; |
117 | 1.00k | ep = ep->next; |
118 | 1.00k | } |
119 | 1.00k | if (!ep) { |
120 | 1.00k | if (COAP_LOG_WARN <= coap_get_log_level()) { |
121 | | #ifndef INET6_ADDRSTRLEN |
122 | | #define INET6_ADDRSTRLEN 40 |
123 | | #endif |
124 | 0 | unsigned char addr_str[INET6_ADDRSTRLEN + 8]; |
125 | |
|
126 | 0 | if (coap_print_addr(e_listen_addr, addr_str, INET6_ADDRSTRLEN + 8)) { |
127 | 0 | coap_log_warn("coap_persist_observe_add: Endpoint %s not defined\n", |
128 | 0 | addr_str); |
129 | 0 | } |
130 | 0 | } |
131 | 1.00k | return NULL; |
132 | 1.00k | } |
133 | | |
134 | | /* Build up packet */ |
135 | 0 | memcpy(&packet->addr_info, s_addr_info, sizeof(packet->addr_info)); |
136 | 0 | packet->ifindex = 0; |
137 | 0 | memcpy(&packet->payload, &raw_packet->s, sizeof(packet->payload)); |
138 | 0 | packet->length = raw_packet->length; |
139 | |
|
140 | 0 | data = raw_packet->s; |
141 | 0 | data_len = raw_packet->length; |
142 | 0 | if (data_len < 4) |
143 | 0 | goto malformed; |
144 | | |
145 | | /* Get the session */ |
146 | | |
147 | 0 | coap_ticks(&now); |
148 | 0 | session = coap_endpoint_get_session(ep, packet, now); |
149 | 0 | if (session == NULL) |
150 | 0 | goto fail; |
151 | | /* Need max space in case PDU is updated with updated token, huge size etc. */ |
152 | 0 | pdu = coap_pdu_init(0, 0, 0, 0); |
153 | 0 | if (!pdu) |
154 | 0 | goto fail; |
155 | | |
156 | 0 | if (!coap_pdu_parse(session->proto, data, data_len, pdu)) { |
157 | 0 | goto malformed; |
158 | 0 | } |
159 | 0 | pdu->max_size = pdu->used_size; |
160 | |
|
161 | 0 | if (pdu->code != COAP_REQUEST_CODE_GET && |
162 | 0 | pdu->code != COAP_REQUEST_CODE_FETCH) |
163 | 0 | goto malformed; |
164 | | |
165 | 0 | observe = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter); |
166 | 0 | if (observe == NULL) |
167 | 0 | goto malformed; |
168 | 0 | observe_action = coap_decode_var_bytes(coap_opt_value(observe), |
169 | 0 | coap_opt_length(observe)); |
170 | 0 | if (observe_action != COAP_OBSERVE_ESTABLISH) |
171 | 0 | goto malformed; |
172 | | |
173 | | /* Get the resource */ |
174 | | |
175 | 0 | uri_path = coap_get_uri_path(pdu); |
176 | 0 | if (!uri_path) |
177 | 0 | goto malformed; |
178 | | |
179 | 0 | r = coap_get_resource_from_uri_path_lkd(session->context, |
180 | 0 | (coap_str_const_t *)uri_path); |
181 | 0 | if (r == NULL) { |
182 | 0 | coap_log_warn("coap_persist_observe_add: resource '%s' not defined\n", |
183 | 0 | uri_path->s); |
184 | 0 | goto fail; |
185 | 0 | } |
186 | 0 | if (!r->observable) { |
187 | 0 | coap_log_warn("coap_persist_observe_add: resource '%s' not observable\n", |
188 | 0 | uri_path->s); |
189 | 0 | goto fail; |
190 | 0 | } |
191 | 0 | coap_delete_string(uri_path); |
192 | 0 | uri_path = NULL; |
193 | | |
194 | | /* Create / update subscription for observing */ |
195 | | /* Now set up the subscription */ |
196 | 0 | s = coap_add_observer(r, session, &pdu->actual_token, pdu); |
197 | 0 | if (s == NULL) |
198 | 0 | goto fail; |
199 | | |
200 | 0 | #if COAP_OSCORE_SUPPORT |
201 | 0 | if (oscore_info) { |
202 | 0 | 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 | 0 | oscore_ctx_t *osc_ctx; |
217 | 0 | const uint8_t *info_buf = oscore_info->s; |
218 | 0 | size_t info_buf_len = oscore_info->length; |
219 | 0 | size_t ret = 0; |
220 | 0 | coap_bin_const_t oscore_key_id; |
221 | 0 | coap_bin_const_t partial_iv; |
222 | 0 | coap_bin_const_t aad; |
223 | 0 | coap_bin_const_t id_context; |
224 | 0 | coap_bin_const_t nonce; |
225 | 0 | int have_aad = 0; |
226 | 0 | int have_partial_iv = 0; |
227 | 0 | int have_id_context = 0; |
228 | 0 | int have_nonce = 0; |
229 | |
|
230 | 0 | ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len); |
231 | 0 | if (ret != CBOR_ARRAY) |
232 | 0 | goto oscore_fail; |
233 | 0 | if (oscore_cbor_get_element_size(&info_buf, &info_buf_len) != 5) |
234 | 0 | goto oscore_fail; |
235 | | |
236 | | /* recipient_id */ |
237 | 0 | ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len); |
238 | 0 | if (ret != CBOR_BYTE_STRING) |
239 | 0 | goto oscore_fail; |
240 | 0 | oscore_key_id.length = oscore_cbor_get_element_size(&info_buf, |
241 | 0 | &info_buf_len); |
242 | 0 | if (oscore_key_id.length > info_buf_len) |
243 | 0 | goto oscore_fail; |
244 | 0 | oscore_key_id.s = info_buf; |
245 | 0 | info_buf += oscore_key_id.length; |
246 | 0 | info_buf_len -= oscore_key_id.length; |
247 | | |
248 | | /* id_context */ |
249 | 0 | ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len); |
250 | 0 | if (ret == CBOR_BYTE_STRING) { |
251 | 0 | id_context.length = oscore_cbor_get_element_size(&info_buf, |
252 | 0 | &info_buf_len); |
253 | 0 | if (id_context.length > info_buf_len) |
254 | 0 | goto oscore_fail; |
255 | 0 | id_context.s = info_buf; |
256 | 0 | info_buf += id_context.length; |
257 | 0 | info_buf_len -= id_context.length; |
258 | 0 | have_id_context = 1; |
259 | 0 | } else if (ret == CBOR_SIMPLE_VALUE && |
260 | 0 | oscore_cbor_get_element_size(&info_buf, |
261 | 0 | &info_buf_len) == CBOR_NULL) { |
262 | 0 | } else |
263 | 0 | goto oscore_fail; |
264 | | |
265 | | /* aad */ |
266 | 0 | ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len); |
267 | 0 | if (ret == CBOR_BYTE_STRING) { |
268 | 0 | aad.length = oscore_cbor_get_element_size(&info_buf, &info_buf_len); |
269 | 0 | if (aad.length > info_buf_len) |
270 | 0 | goto oscore_fail; |
271 | 0 | aad.s = info_buf; |
272 | 0 | info_buf += aad.length; |
273 | 0 | info_buf_len -= aad.length; |
274 | 0 | have_aad = 1; |
275 | 0 | } else if (ret == CBOR_SIMPLE_VALUE && |
276 | 0 | oscore_cbor_get_element_size(&info_buf, |
277 | 0 | &info_buf_len) == CBOR_NULL) { |
278 | 0 | } else |
279 | 0 | goto oscore_fail; |
280 | | |
281 | | /* partial_iv */ |
282 | 0 | ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len); |
283 | 0 | if (ret == CBOR_BYTE_STRING) { |
284 | 0 | partial_iv.length = oscore_cbor_get_element_size(&info_buf, |
285 | 0 | &info_buf_len); |
286 | 0 | if (partial_iv.length > info_buf_len) |
287 | 0 | goto oscore_fail; |
288 | 0 | partial_iv.s = info_buf; |
289 | 0 | info_buf += partial_iv.length; |
290 | 0 | info_buf_len -= partial_iv.length; |
291 | 0 | have_partial_iv = 1; |
292 | 0 | } else if (ret == CBOR_SIMPLE_VALUE && |
293 | 0 | oscore_cbor_get_element_size(&info_buf, |
294 | 0 | &info_buf_len) == CBOR_NULL) { |
295 | 0 | } else |
296 | 0 | goto oscore_fail; |
297 | | |
298 | | /* nonce */ |
299 | 0 | ret = oscore_cbor_get_next_element(&info_buf, &info_buf_len); |
300 | 0 | if (ret == CBOR_BYTE_STRING) { |
301 | 0 | nonce.length = oscore_cbor_get_element_size(&info_buf, |
302 | 0 | &info_buf_len); |
303 | 0 | if (nonce.length > info_buf_len) |
304 | 0 | goto oscore_fail; |
305 | 0 | nonce.s = info_buf; |
306 | 0 | info_buf += nonce.length; |
307 | 0 | info_buf_len -= nonce.length; |
308 | 0 | have_nonce = 1; |
309 | 0 | } else if (ret == CBOR_SIMPLE_VALUE && |
310 | 0 | oscore_cbor_get_element_size(&info_buf, |
311 | 0 | &info_buf_len) == CBOR_NULL) { |
312 | 0 | } else |
313 | 0 | goto oscore_fail; |
314 | | |
315 | 0 | osc_ctx = oscore_find_context(session, oscore_key_id, |
316 | 0 | have_id_context ? &id_context : NULL, NULL, |
317 | 0 | &session->recipient_ctx); |
318 | 0 | if (osc_ctx) { |
319 | 0 | session->oscore_encryption = 1; |
320 | 0 | oscore_new_association(session, pdu, &pdu->actual_token, |
321 | 0 | session->recipient_ctx, |
322 | 0 | have_aad ? &aad : NULL, |
323 | 0 | have_nonce ? &nonce : NULL, |
324 | 0 | have_partial_iv ? &partial_iv : NULL, |
325 | 0 | 1); |
326 | 0 | coap_log_debug("persist: OSCORE association added\n"); |
327 | 0 | oscore_log_hex_value(COAP_LOG_OSCORE, "partial_iv", |
328 | 0 | have_partial_iv ? &partial_iv : NULL); |
329 | 0 | } |
330 | 0 | } |
331 | 0 | oscore_fail: |
332 | | #else /* ! COAP_OSCORE_SUPPORT */ |
333 | | (void)oscore_info; |
334 | | #endif /* ! COAP_OSCORE_SUPPORT */ |
335 | 0 | coap_delete_pdu_lkd(pdu); |
336 | 0 | return s; |
337 | | |
338 | 0 | malformed: |
339 | 0 | coap_log_warn("coap_persist_observe_add: discard malformed PDU\n"); |
340 | 0 | fail: |
341 | 0 | coap_delete_string(uri_path); |
342 | 0 | coap_delete_pdu_lkd(pdu); |
343 | 0 | return NULL; |
344 | 0 | } |
345 | | |
346 | | #if COAP_WITH_OBSERVE_PERSIST |
347 | | #include <stdio.h> |
348 | | |
349 | | /* |
350 | | * read in active observe entry. |
351 | | */ |
352 | | static int |
353 | | coap_op_observe_read(FILE *fp, coap_subscription_t **observe_key, |
354 | | coap_proto_t *e_proto, coap_address_t *e_listen_addr, |
355 | | coap_addr_tuple_t *s_addr_info, |
356 | 9.30k | coap_bin_const_t **raw_packet, coap_bin_const_t **oscore_info) { |
357 | 9.30k | ssize_t size; |
358 | 9.30k | coap_binary_t *scratch = NULL; |
359 | | |
360 | 9.30k | assert(fp && observe_key && e_proto && e_listen_addr && s_addr_info && |
361 | 9.30k | raw_packet && oscore_info); |
362 | | |
363 | 9.30k | *raw_packet = NULL; |
364 | 9.30k | *oscore_info = NULL; |
365 | | |
366 | 9.30k | if (fread(observe_key, sizeof(*observe_key), 1, fp) == 1) { |
367 | | /* New record 'key proto listen addr_info len raw_packet len oscore' */ |
368 | 5.50k | if (fread(e_proto, sizeof(*e_proto), 1, fp) != 1) |
369 | 144 | goto fail; |
370 | 5.36k | if (fread(e_listen_addr, sizeof(*e_listen_addr), 1, fp) != 1) |
371 | 982 | goto fail; |
372 | 4.38k | if (fread(s_addr_info, sizeof(*s_addr_info), 1, fp) != 1) |
373 | 347 | goto fail; |
374 | 4.03k | if (fread(&size, sizeof(size), 1, fp) != 1) |
375 | 37 | goto fail; |
376 | 3.99k | if (size < 0 || size > 0x10000) |
377 | 597 | goto fail; |
378 | 3.40k | scratch = coap_new_binary(size); |
379 | 3.40k | if ((scratch) == NULL) |
380 | 0 | goto fail; |
381 | 3.40k | if (fread(scratch->s, scratch->length, 1, fp) != 1) |
382 | 90 | goto fail; |
383 | 3.31k | *raw_packet = (coap_bin_const_t *)scratch; |
384 | 3.31k | scratch = NULL; |
385 | 3.31k | if (fread(&size, sizeof(size), 1, fp) != 1) |
386 | 1 | goto fail; |
387 | | /* If size == -1, then no oscore information */ |
388 | 3.31k | if (size == -1) |
389 | 3.06k | return 1; |
390 | 242 | else if (size < 0 || size > 0x10000) |
391 | 141 | goto fail; |
392 | 101 | else { |
393 | 101 | scratch = coap_new_binary(size); |
394 | 101 | if (scratch == NULL) |
395 | 0 | goto fail; |
396 | 101 | if (fread(scratch->s, scratch->length, 1, fp) != 1) |
397 | 26 | goto fail; |
398 | 75 | *oscore_info = (coap_bin_const_t *)scratch; |
399 | 75 | } |
400 | 75 | return 1; |
401 | 3.31k | } |
402 | 6.15k | fail: |
403 | 6.15k | coap_delete_bin_const(*raw_packet); |
404 | 6.15k | coap_delete_binary(scratch); |
405 | | |
406 | 6.15k | *observe_key = NULL; |
407 | 6.15k | memset(e_proto, 0, sizeof(*e_proto)); |
408 | 6.15k | memset(e_listen_addr, 0, sizeof(*e_listen_addr)); |
409 | 6.15k | memset(s_addr_info, 0, sizeof(*s_addr_info)); |
410 | 6.15k | *raw_packet = NULL; |
411 | 6.15k | return 0; |
412 | 9.30k | } |
413 | | |
414 | | /* |
415 | | * write out active observe entry. |
416 | | */ |
417 | | static int |
418 | | coap_op_observe_write(FILE *fp, coap_subscription_t *observe_key, |
419 | | coap_proto_t e_proto, coap_address_t e_listen_addr, |
420 | | coap_addr_tuple_t s_addr_info, |
421 | 2.52k | coap_bin_const_t *raw_packet, coap_bin_const_t *oscore_info) { |
422 | 2.52k | if (fwrite(&observe_key, sizeof(observe_key), 1, fp) != 1) |
423 | 0 | goto fail; |
424 | 2.52k | if (fwrite(&e_proto, sizeof(e_proto), 1, fp) != 1) |
425 | 0 | goto fail; |
426 | 2.52k | if (fwrite(&e_listen_addr, sizeof(e_listen_addr), |
427 | 2.52k | 1, fp) != 1) |
428 | 0 | goto fail; |
429 | 2.52k | if (fwrite(&s_addr_info, sizeof(s_addr_info), 1, fp) != 1) |
430 | 0 | goto fail; |
431 | 2.52k | if (fwrite(&raw_packet->length, sizeof(raw_packet->length), 1, fp) != 1) |
432 | 0 | goto fail; |
433 | 2.52k | if (fwrite(raw_packet->s, raw_packet->length, 1, fp) != 1) |
434 | 0 | goto fail; |
435 | 2.52k | if (oscore_info) { |
436 | 0 | if (fwrite(&oscore_info->length, sizeof(oscore_info->length), 1, fp) != 1) |
437 | 0 | goto fail; |
438 | 0 | if (fwrite(oscore_info->s, oscore_info->length, 1, fp) != 1) |
439 | 0 | goto fail; |
440 | 2.52k | } else { |
441 | 2.52k | ssize_t not_defined = -1; |
442 | | |
443 | 2.52k | if (fwrite(¬_defined, sizeof(not_defined), 1, fp) != 1) |
444 | 0 | goto fail; |
445 | 2.52k | } |
446 | 2.52k | return 1; |
447 | 0 | fail: |
448 | 0 | return 0; |
449 | 2.52k | } |
450 | | |
451 | | /* |
452 | | * This should be called before coap_persist_track_funcs() to prevent |
453 | | * coap_op_observe_added() getting unnecessarily called. |
454 | | * It should be called after init_resources() and coap_op_resource_load_disk() |
455 | | * so that all the resources are in place. |
456 | | */ |
457 | | static void |
458 | 2.52k | coap_op_observe_load_disk(coap_context_t *ctx) { |
459 | 2.52k | FILE *fp_orig = fopen((const char *)ctx->observe_save_file->s, "r"); |
460 | 2.52k | FILE *fp_new = NULL; |
461 | 2.52k | coap_subscription_t *observe_key = NULL; |
462 | 2.52k | coap_proto_t e_proto; |
463 | 2.52k | coap_address_t e_listen_addr; |
464 | 2.52k | coap_addr_tuple_t s_addr_info; |
465 | 2.52k | coap_bin_const_t *raw_packet = NULL; |
466 | 2.52k | coap_bin_const_t *oscore_info = NULL; |
467 | 2.52k | char *new = NULL; |
468 | | |
469 | 2.52k | if (fp_orig == NULL) |
470 | 0 | goto fail; |
471 | | |
472 | 2.52k | new = coap_malloc_type(COAP_STRING, ctx->observe_save_file->length + 5); |
473 | 2.52k | if (!new) |
474 | 0 | goto fail; |
475 | | |
476 | 2.52k | strcpy(new, (const char *)ctx->observe_save_file->s); |
477 | 2.52k | strcat(new, ".tmp"); |
478 | 2.52k | fp_new = fopen(new, "w+"); |
479 | 2.52k | if (fp_new == NULL) |
480 | 0 | goto fail; |
481 | | |
482 | | /* Go through and load oscore entry, updating key on the way */ |
483 | 4.56k | while (1) { |
484 | 4.56k | if (!coap_op_observe_read(fp_orig, &observe_key, &e_proto, &e_listen_addr, |
485 | 4.56k | &s_addr_info, &raw_packet, &oscore_info)) |
486 | 2.52k | break; |
487 | 2.03k | coap_log_debug("persist: New session/observe being created\n"); |
488 | 2.03k | observe_key = coap_persist_observe_add_lkd(ctx, e_proto, |
489 | 2.03k | &e_listen_addr, |
490 | 2.03k | &s_addr_info, |
491 | 2.03k | raw_packet, |
492 | 2.03k | oscore_info); |
493 | 2.03k | if (observe_key) { |
494 | 0 | if (!coap_op_observe_write(fp_new, observe_key, e_proto, e_listen_addr, |
495 | 0 | s_addr_info, raw_packet, oscore_info)) |
496 | 0 | goto fail; |
497 | 0 | } |
498 | 2.03k | coap_delete_bin_const(raw_packet); |
499 | 2.03k | raw_packet = NULL; |
500 | 2.03k | coap_delete_bin_const(oscore_info); |
501 | 2.03k | oscore_info = NULL; |
502 | 2.03k | } |
503 | 2.52k | coap_delete_bin_const(raw_packet); |
504 | 2.52k | raw_packet = NULL; |
505 | 2.52k | coap_delete_bin_const(oscore_info); |
506 | 2.52k | oscore_info = NULL; |
507 | | |
508 | 2.52k | if (fflush(fp_new) == EOF) |
509 | 0 | goto fail; |
510 | 2.52k | fclose(fp_new); |
511 | 2.52k | fclose(fp_orig); |
512 | | /* Either old or new is in place */ |
513 | 2.52k | if (rename(new, (const char *)ctx->observe_save_file->s) == -1) { |
514 | 0 | coap_log_warn("rename %s -> %s: failed: %s (%d)\n", |
515 | 0 | new, (const char *)ctx->observe_save_file->s, |
516 | 0 | coap_socket_strerror(), errno); |
517 | 0 | } |
518 | 2.52k | coap_free_type(COAP_STRING, new); |
519 | 2.52k | return; |
520 | | |
521 | 0 | fail: |
522 | 0 | coap_delete_bin_const(raw_packet); |
523 | 0 | coap_delete_bin_const(oscore_info); |
524 | 0 | if (fp_new) |
525 | 0 | fclose(fp_new); |
526 | 0 | if (fp_orig) |
527 | 0 | fclose(fp_orig); |
528 | 0 | if (new) { |
529 | 0 | (void)remove(new); |
530 | 0 | } |
531 | 0 | coap_free_type(COAP_STRING, new); |
532 | 0 | return; |
533 | 2.52k | } |
534 | | |
535 | | /* |
536 | | * client has registered a new observe subscription request. |
537 | | */ |
538 | | static int |
539 | | coap_op_observe_added(coap_session_t *session, |
540 | | coap_subscription_t *a_observe_key, |
541 | | coap_proto_t a_e_proto, coap_address_t *a_e_listen_addr, |
542 | | coap_addr_tuple_t *a_s_addr_info, |
543 | | coap_bin_const_t *a_raw_packet, |
544 | 2.52k | coap_bin_const_t *a_oscore_info, void *user_data) { |
545 | 2.52k | FILE *fp_orig = fopen((const char *)session->context->observe_save_file->s, |
546 | 2.52k | "r"); |
547 | 2.52k | FILE *fp_new = NULL; |
548 | 2.52k | coap_subscription_t *observe_key = NULL; |
549 | 2.52k | coap_proto_t e_proto; |
550 | 2.52k | coap_address_t e_listen_addr; |
551 | 2.52k | coap_addr_tuple_t s_addr_info; |
552 | 2.52k | coap_bin_const_t *raw_packet = NULL; |
553 | 2.52k | coap_bin_const_t *oscore_info = NULL; |
554 | 2.52k | char *new = NULL; |
555 | | |
556 | 2.52k | (void)user_data; |
557 | | |
558 | 2.52k | new = coap_malloc_type(COAP_STRING, |
559 | 2.52k | session->context->observe_save_file->length + 5); |
560 | 2.52k | if (!new) |
561 | 0 | goto fail; |
562 | | |
563 | 2.52k | strcpy(new, (const char *)session->context->observe_save_file->s); |
564 | 2.52k | strcat(new, ".tmp"); |
565 | 2.52k | fp_new = fopen(new, "w+"); |
566 | 2.52k | if (fp_new == NULL) |
567 | 0 | goto fail; |
568 | | |
569 | | /* Go through and delete observe entry if it exists */ |
570 | 2.52k | while (fp_orig) { |
571 | 2.52k | if (!coap_op_observe_read(fp_orig, &observe_key, &e_proto, &e_listen_addr, |
572 | 2.52k | &s_addr_info, &raw_packet, &oscore_info)) |
573 | 2.52k | break; |
574 | 0 | if (observe_key != a_observe_key) { |
575 | 0 | if (!coap_op_observe_write(fp_new, observe_key, e_proto, e_listen_addr, |
576 | 0 | s_addr_info, raw_packet, oscore_info)) |
577 | 0 | goto fail; |
578 | 0 | } |
579 | 0 | coap_delete_bin_const(raw_packet); |
580 | 0 | raw_packet = NULL; |
581 | 0 | coap_delete_bin_const(oscore_info); |
582 | 0 | oscore_info = NULL; |
583 | 0 | } |
584 | 2.52k | coap_delete_bin_const(raw_packet); |
585 | 2.52k | raw_packet = NULL; |
586 | 2.52k | coap_delete_bin_const(oscore_info); |
587 | 2.52k | oscore_info = NULL; |
588 | | |
589 | | /* Add in new entry to the end */ |
590 | 2.52k | if (!coap_op_observe_write(fp_new, a_observe_key, a_e_proto, *a_e_listen_addr, |
591 | 2.52k | *a_s_addr_info, a_raw_packet, a_oscore_info)) |
592 | 0 | goto fail; |
593 | | |
594 | 2.52k | if (fflush(fp_new) == EOF) |
595 | 0 | goto fail; |
596 | 2.52k | fclose(fp_new); |
597 | 2.52k | if (fp_orig) |
598 | 2.52k | fclose(fp_orig); |
599 | | /* Either old or new is in place */ |
600 | 2.52k | (void)rename(new, (const char *)session->context->observe_save_file->s); |
601 | 2.52k | coap_free_type(COAP_STRING, new); |
602 | 2.52k | return 1; |
603 | | |
604 | 0 | fail: |
605 | 0 | coap_delete_bin_const(raw_packet); |
606 | 0 | coap_delete_bin_const(oscore_info); |
607 | 0 | if (fp_new) |
608 | 0 | fclose(fp_new); |
609 | 0 | if (fp_orig) |
610 | 0 | fclose(fp_orig); |
611 | 0 | if (new) { |
612 | 0 | (void)remove(new); |
613 | 0 | } |
614 | 0 | coap_free_type(COAP_STRING, new); |
615 | 0 | return 0; |
616 | 2.52k | } |
617 | | |
618 | | /* |
619 | | * client has de-registered a observe subscription request. |
620 | | */ |
621 | | static int |
622 | | coap_op_observe_deleted(coap_session_t *session, |
623 | | coap_subscription_t *d_observe_key, |
624 | 1.10k | void *user_data) { |
625 | 1.10k | FILE *fp_orig = fopen((const char *)session->context->observe_save_file->s, |
626 | 1.10k | "r"); |
627 | 1.10k | FILE *fp_new = NULL; |
628 | 1.10k | coap_subscription_t *observe_key = NULL; |
629 | 1.10k | coap_proto_t e_proto; |
630 | 1.10k | coap_address_t e_listen_addr; |
631 | 1.10k | coap_addr_tuple_t s_addr_info; |
632 | 1.10k | coap_bin_const_t *raw_packet = NULL; |
633 | 1.10k | coap_bin_const_t *oscore_info = NULL; |
634 | 1.10k | char *new = NULL; |
635 | | |
636 | 1.10k | (void)user_data; |
637 | | |
638 | 1.10k | if (fp_orig == NULL) |
639 | 0 | goto fail; |
640 | 1.10k | new = coap_malloc_type(COAP_STRING, |
641 | 1.10k | session->context->observe_save_file->length + 5); |
642 | 1.10k | if (!new) |
643 | 0 | goto fail; |
644 | | |
645 | 1.10k | strcpy(new, (const char *)session->context->observe_save_file->s); |
646 | 1.10k | strcat(new, ".tmp"); |
647 | 1.10k | fp_new = fopen(new, "w+"); |
648 | 1.10k | if (fp_new == NULL) |
649 | 0 | goto fail; |
650 | | |
651 | | /* Go through and locate observe entry to delete and not copy it across */ |
652 | 2.21k | while (1) { |
653 | 2.21k | if (!coap_op_observe_read(fp_orig, &observe_key, &e_proto, &e_listen_addr, |
654 | 2.21k | &s_addr_info, &raw_packet, &oscore_info)) |
655 | 1.10k | break; |
656 | 1.10k | if (observe_key != d_observe_key) { |
657 | 0 | if (!coap_op_observe_write(fp_new, observe_key, e_proto, e_listen_addr, |
658 | 0 | s_addr_info, (coap_bin_const_t *)raw_packet, |
659 | 0 | (coap_bin_const_t *)oscore_info)) |
660 | 0 | goto fail; |
661 | 0 | } |
662 | 1.10k | coap_delete_bin_const(raw_packet); |
663 | 1.10k | raw_packet = NULL; |
664 | 1.10k | coap_delete_bin_const(oscore_info); |
665 | 1.10k | oscore_info = NULL; |
666 | 1.10k | } |
667 | 1.10k | coap_delete_bin_const(raw_packet); |
668 | 1.10k | raw_packet = NULL; |
669 | 1.10k | coap_delete_bin_const(oscore_info); |
670 | 1.10k | oscore_info = NULL; |
671 | | |
672 | 1.10k | if (fflush(fp_new) == EOF) |
673 | 0 | goto fail; |
674 | 1.10k | fclose(fp_new); |
675 | 1.10k | fclose(fp_orig); |
676 | | /* Either old or new is in place */ |
677 | 1.10k | (void)rename(new, (const char *)session->context->observe_save_file->s); |
678 | 1.10k | coap_free_type(COAP_STRING, new); |
679 | 1.10k | return 1; |
680 | | |
681 | 0 | fail: |
682 | 0 | coap_delete_bin_const(raw_packet); |
683 | 0 | coap_delete_bin_const(oscore_info); |
684 | 0 | if (fp_new) |
685 | 0 | fclose(fp_new); |
686 | 0 | if (fp_orig) |
687 | 0 | fclose(fp_orig); |
688 | 0 | if (new) { |
689 | 0 | (void)remove(new); |
690 | 0 | } |
691 | 0 | coap_free_type(COAP_STRING, new); |
692 | 0 | return 0; |
693 | 1.10k | } |
694 | | |
695 | | /* |
696 | | * This should be called before coap_persist_track_funcs() to prevent |
697 | | * coap_op_obs_cnt_track_observe() getting unnecessarily called. |
698 | | * Should be called after coap_op_dyn_resource_load_disk() to make sure that |
699 | | * all the resources are in the right place. |
700 | | */ |
701 | | static void |
702 | 2.52k | coap_op_obs_cnt_load_disk(coap_context_t *context) { |
703 | 2.52k | FILE *fp = fopen((const char *)context->obs_cnt_save_file->s, "r"); |
704 | 2.52k | char buf[1500]; |
705 | | |
706 | 2.52k | if (fp == NULL) |
707 | 0 | return; |
708 | | |
709 | 8.22k | while (fgets(buf, sizeof(buf), fp) != NULL) { |
710 | 7.93k | char *cp = strchr(buf, ' '); |
711 | 7.93k | coap_str_const_t resource_key; |
712 | 7.93k | uint32_t observe_num; |
713 | 7.93k | coap_resource_t *r; |
714 | | |
715 | 7.93k | if (!cp) |
716 | 2.23k | break; |
717 | | |
718 | 5.70k | *cp = '\000'; |
719 | 5.70k | cp++; |
720 | 5.70k | observe_num = atoi(cp); |
721 | | /* |
722 | | * Need to assume 0 .. (context->observe_save_freq-1) have in addition |
723 | | * been sent so need to round up to latest possible send value |
724 | | */ |
725 | 5.70k | observe_num = ((observe_num + context->observe_save_freq) / |
726 | 5.70k | context->observe_save_freq) * |
727 | 5.70k | context->observe_save_freq - 1; |
728 | 5.70k | resource_key.s = (uint8_t *)buf; |
729 | 5.70k | resource_key.length = strlen(buf); |
730 | 5.70k | r = coap_get_resource_from_uri_path_lkd(context, &resource_key); |
731 | 5.70k | if (r) { |
732 | 253 | coap_log_debug("persist: Initial observe number being updated '%s' %u\n", |
733 | 253 | buf, observe_num); |
734 | 253 | coap_persist_set_observe_num(r, observe_num); |
735 | 253 | } |
736 | 5.70k | } |
737 | 2.52k | fclose(fp); |
738 | 2.52k | } |
739 | | |
740 | | /* |
741 | | * Called when the observe value of a resource has been changed, but limited |
742 | | * to be called every context->context->observe_save_freq to reduce update |
743 | | * overheads. |
744 | | */ |
745 | | static int |
746 | | coap_op_obs_cnt_track_observe(coap_context_t *context, |
747 | | coap_str_const_t *resource_name, |
748 | | uint32_t n_observe_num, |
749 | 5.05k | void *user_data) { |
750 | 5.05k | FILE *fp_orig = fopen((const char *)context->obs_cnt_save_file->s, "r"); |
751 | 5.05k | FILE *fp_new = NULL; |
752 | 5.05k | char buf[1500]; |
753 | 5.05k | char *new = NULL; |
754 | | |
755 | 5.05k | (void)user_data; |
756 | | |
757 | 5.05k | new = coap_malloc_type(COAP_STRING, context->obs_cnt_save_file->length + 5); |
758 | 5.05k | if (!new) |
759 | 0 | goto fail; |
760 | | |
761 | 5.05k | strcpy(new, (const char *)context->obs_cnt_save_file->s); |
762 | 5.05k | strcat(new, ".tmp"); |
763 | 5.05k | fp_new = fopen(new, "w+"); |
764 | 5.05k | if (fp_new == NULL) |
765 | 0 | goto fail; |
766 | | |
767 | | /* Go through and locate resource entry to update */ |
768 | 18.7k | while (fp_orig && fgets(buf, sizeof(buf), fp_orig) != NULL) { |
769 | 15.9k | char *cp = strchr(buf, ' '); |
770 | 15.9k | uint32_t observe_num; |
771 | 15.9k | coap_bin_const_t resource_key; |
772 | | |
773 | 15.9k | if (!cp) |
774 | 2.23k | break; |
775 | | |
776 | 13.6k | *cp = '\000'; |
777 | 13.6k | cp++; |
778 | 13.6k | observe_num = atoi(cp); |
779 | 13.6k | resource_key.s = (uint8_t *)buf; |
780 | 13.6k | resource_key.length = strlen(buf); |
781 | 13.6k | if (!coap_binary_equal(resource_name, &resource_key)) { |
782 | 10.8k | if (fprintf(fp_new, "%s %u\n", resource_key.s, observe_num) < 0) |
783 | 0 | goto fail; |
784 | 10.8k | } |
785 | 13.6k | } |
786 | 5.05k | if (fprintf(fp_new, "%s %u\n", resource_name->s, n_observe_num) < 0) |
787 | 0 | goto fail; |
788 | 5.05k | if (fflush(fp_new) == EOF) |
789 | 0 | goto fail; |
790 | 5.05k | fclose(fp_new); |
791 | 5.05k | if (fp_orig) |
792 | 5.05k | fclose(fp_orig); |
793 | | /* Either old or new is in place */ |
794 | 5.05k | (void)rename(new, (const char *)context->obs_cnt_save_file->s); |
795 | 5.05k | coap_free_type(COAP_STRING, new); |
796 | 5.05k | return 1; |
797 | | |
798 | 0 | fail: |
799 | 0 | if (fp_new) |
800 | 0 | fclose(fp_new); |
801 | 0 | if (fp_orig) |
802 | 0 | fclose(fp_orig); |
803 | 0 | if (new) { |
804 | 0 | (void)remove(new); |
805 | 0 | } |
806 | 0 | coap_free_type(COAP_STRING, new); |
807 | 0 | return 0; |
808 | 5.05k | } |
809 | | |
810 | | /* |
811 | | * Called when a resource has been deleted. |
812 | | */ |
813 | | static int |
814 | | coap_op_obs_cnt_deleted(coap_context_t *context, |
815 | 0 | coap_str_const_t *resource_name) { |
816 | 0 | FILE *fp_orig = fopen((const char *)context->obs_cnt_save_file->s, "r"); |
817 | 0 | FILE *fp_new = NULL; |
818 | 0 | char buf[1500]; |
819 | 0 | char *new = NULL; |
820 | |
|
821 | 0 | if (fp_orig == NULL) |
822 | 0 | goto fail; |
823 | 0 | new = coap_malloc_type(COAP_STRING, context->obs_cnt_save_file->length + 5); |
824 | 0 | if (!new) |
825 | 0 | goto fail; |
826 | | |
827 | 0 | strcpy(new, (const char *)context->obs_cnt_save_file->s); |
828 | 0 | strcat(new, ".tmp"); |
829 | 0 | fp_new = fopen(new, "w+"); |
830 | 0 | if (fp_new == NULL) |
831 | 0 | goto fail; |
832 | | |
833 | | /* Go through and locate resource entry to delete */ |
834 | 0 | while (fgets(buf, sizeof(buf), fp_orig) != NULL) { |
835 | 0 | char *cp = strchr(buf, ' '); |
836 | 0 | uint32_t observe_num; |
837 | 0 | coap_bin_const_t resource_key; |
838 | |
|
839 | 0 | if (!cp) |
840 | 0 | break; |
841 | | |
842 | 0 | *cp = '\000'; |
843 | 0 | cp++; |
844 | 0 | observe_num = atoi(cp); |
845 | 0 | resource_key.s = (uint8_t *)buf; |
846 | 0 | resource_key.length = strlen(buf); |
847 | 0 | if (!coap_binary_equal(resource_name, &resource_key)) { |
848 | 0 | if (fprintf(fp_new, "%s %u\n", resource_key.s, observe_num) < 0) |
849 | 0 | goto fail; |
850 | 0 | } |
851 | 0 | } |
852 | 0 | if (fflush(fp_new) == EOF) |
853 | 0 | goto fail; |
854 | 0 | fclose(fp_new); |
855 | 0 | fclose(fp_orig); |
856 | | /* Either old or new is in place */ |
857 | 0 | (void)rename(new, (const char *)context->obs_cnt_save_file->s); |
858 | 0 | coap_free_type(COAP_STRING, new); |
859 | 0 | return 1; |
860 | | |
861 | 0 | fail: |
862 | 0 | if (fp_new) |
863 | 0 | fclose(fp_new); |
864 | 0 | if (fp_orig) |
865 | 0 | fclose(fp_orig); |
866 | 0 | if (new) { |
867 | 0 | (void)remove(new); |
868 | 0 | } |
869 | 0 | coap_free_type(COAP_STRING, new); |
870 | 0 | return 0; |
871 | 0 | } |
872 | | |
873 | | /* |
874 | | * read in dynamic resource entry, allocating name & raw_packet |
875 | | * which need to be freed off by caller. |
876 | | */ |
877 | | static int |
878 | | coap_op_dyn_resource_read(FILE *fp, coap_proto_t *e_proto, |
879 | | coap_string_t **name, |
880 | 5.83k | coap_binary_t **raw_packet) { |
881 | 5.83k | ssize_t size; |
882 | | |
883 | 5.83k | *name = NULL; |
884 | 5.83k | *raw_packet = NULL; |
885 | | |
886 | 5.83k | if (fread(e_proto, sizeof(*e_proto), 1, fp) == 1) { |
887 | | /* New record 'proto len resource_name len raw_packet' */ |
888 | 5.33k | if (fread(&size, sizeof(size), 1, fp) != 1) |
889 | 52 | goto fail; |
890 | 5.28k | if (size < 0 || size > 0x10000) |
891 | 606 | goto fail; |
892 | 4.67k | *name = coap_new_string(size); |
893 | 4.67k | if (!(*name)) |
894 | 0 | goto fail; |
895 | 4.67k | if (fread((*name)->s, size, 1, fp) != 1) |
896 | 88 | goto fail; |
897 | 4.59k | if (fread(&size, sizeof(size), 1, fp) != 1) |
898 | 3 | goto fail; |
899 | 4.58k | if (size < 0 || size > 0x10000) |
900 | 139 | goto fail; |
901 | 4.44k | *raw_packet = coap_new_binary(size); |
902 | 4.44k | if (!(*raw_packet)) |
903 | 0 | goto fail; |
904 | 4.44k | if (fread((*raw_packet)->s, size, 1, fp) != 1) |
905 | 28 | goto fail; |
906 | 4.42k | return 1; |
907 | 4.44k | } |
908 | 1.41k | fail: |
909 | 1.41k | return 0; |
910 | 5.83k | } |
911 | | |
912 | | /* |
913 | | * write out dynamic resource entry. |
914 | | */ |
915 | | static int |
916 | | coap_op_dyn_resource_write(FILE *fp, coap_proto_t e_proto, |
917 | | coap_str_const_t *name, |
918 | 0 | coap_bin_const_t *raw_packet) { |
919 | 0 | if (fwrite(&e_proto, sizeof(e_proto), 1, fp) != 1) |
920 | 0 | goto fail; |
921 | 0 | if (fwrite(&name->length, sizeof(name->length), 1, fp) != 1) |
922 | 0 | goto fail; |
923 | 0 | if (fwrite(name->s, name->length, 1, fp) != 1) |
924 | 0 | goto fail; |
925 | 0 | if (fwrite(&raw_packet->length, sizeof(raw_packet->length), 1, fp) != 1) |
926 | 0 | goto fail; |
927 | 0 | if (fwrite(raw_packet->s, raw_packet->length, 1, fp) != 1) |
928 | 0 | goto fail; |
929 | 0 | return 1; |
930 | 0 | fail: |
931 | 0 | return 0; |
932 | 0 | } |
933 | | |
934 | | /* |
935 | | * This should be called before coap_persist_track_funcs() to prevent |
936 | | * coap_op_dyn_resource_added() getting unnecessarily called. |
937 | | * |
938 | | * Each record 'proto len resource_name len raw_packet' |
939 | | */ |
940 | | static void |
941 | 2.52k | coap_op_dyn_resource_load_disk(coap_context_t *ctx) { |
942 | 2.52k | FILE *fp_orig = NULL; |
943 | 2.52k | coap_proto_t e_proto; |
944 | 2.52k | coap_string_t *name = NULL; |
945 | 2.52k | coap_binary_t *raw_packet = NULL; |
946 | 2.52k | coap_resource_t *r; |
947 | 2.52k | coap_session_t *session = NULL; |
948 | 2.52k | coap_pdu_t *request = NULL; |
949 | 2.52k | coap_pdu_t *response = NULL; |
950 | 2.52k | coap_string_t *query = NULL; |
951 | | |
952 | 2.52k | if (!ctx->unknown_resource && !ctx->dyn_create_handler) |
953 | 0 | return; |
954 | | |
955 | 2.52k | fp_orig = fopen((const char *)ctx->dyn_resource_save_file->s, "r"); |
956 | 2.52k | if (fp_orig == NULL) |
957 | 0 | return; |
958 | 2.52k | session = (coap_session_t *)coap_malloc_type(COAP_SESSION, |
959 | 2.52k | sizeof(coap_session_t)); |
960 | 2.52k | if (!session) |
961 | 0 | goto fail; |
962 | 2.52k | memset(session, 0, sizeof(coap_session_t)); |
963 | 2.52k | session->context = ctx; |
964 | | |
965 | | /* Go through and create each dynamic resource if it does not exist*/ |
966 | 5.83k | while (1) { |
967 | 5.83k | if (!coap_op_dyn_resource_read(fp_orig, &e_proto, &name, &raw_packet)) |
968 | 1.41k | break; |
969 | 4.42k | r = coap_get_resource_from_uri_path_lkd(ctx, (coap_str_const_t *)name); |
970 | 4.42k | if (!r) { |
971 | | /* Create the new resource using the application logic */ |
972 | | |
973 | 4.42k | coap_log_debug("persist: dynamic resource '%s' being re-created\n", name->s); |
974 | | /* |
975 | | * Need max space in case PDU is updated with updated token, |
976 | | * huge size etc. |
977 | | * */ |
978 | 4.42k | request = coap_pdu_init(0, 0, 0, 0); |
979 | 4.42k | if (!request) |
980 | 0 | goto fail; |
981 | | |
982 | 4.42k | session->proto = e_proto; |
983 | 4.42k | if (!coap_pdu_parse(session->proto, raw_packet->s, |
984 | 4.42k | raw_packet->length, request)) { |
985 | 1.11k | goto fail; |
986 | 1.11k | } |
987 | 3.30k | if (request->code == 0 || request->code > 7) { |
988 | | /* Need to protect r->handler[request->code-1] */ |
989 | 1.34k | goto next; |
990 | 1.34k | } |
991 | 1.96k | r = coap_add_dynamic_resource(session, request); |
992 | 1.96k | if (!r) { |
993 | 1.96k | r = ctx->unknown_resource; |
994 | 1.96k | } |
995 | 1.96k | if (!r || !r->handler[request->code-1]) |
996 | 105 | goto next; |
997 | | |
998 | 1.85k | response = coap_pdu_init(0, 0, 0, 0); |
999 | 1.85k | if (!response) |
1000 | 0 | goto fail; |
1001 | 1.85k | query = coap_get_query(request); |
1002 | | /* Call the application handler to set up this dynamic resource */ |
1003 | 1.85k | if (r->flags & COAP_RESOURCE_SAFE_REQUEST_HANDLER) { |
1004 | 0 | coap_lock_callback_release(r->handler[request->code-1](r, |
1005 | 0 | session, request, |
1006 | 0 | query, response), |
1007 | | /* context is being freed off */ |
1008 | 0 | goto fail); |
1009 | 1.85k | } else { |
1010 | 1.85k | coap_lock_specific_callback_release(&r->lock, |
1011 | 1.85k | r->handler[request->code-1](r, |
1012 | 1.85k | session, request, |
1013 | 1.85k | query, response), |
1014 | | /* context is being freed off */ |
1015 | 1.85k | goto fail); |
1016 | 1.85k | } |
1017 | 1.85k | coap_delete_string(query); |
1018 | 1.85k | query = NULL; |
1019 | 1.85k | coap_delete_pdu_lkd(response); |
1020 | 1.85k | response = NULL; |
1021 | 3.30k | next: |
1022 | 3.30k | coap_delete_pdu_lkd(request); |
1023 | 3.30k | request = NULL; |
1024 | 3.30k | } |
1025 | 3.30k | coap_delete_string(name); |
1026 | 3.30k | coap_delete_binary(raw_packet); |
1027 | 3.30k | } |
1028 | 2.52k | fail: |
1029 | 2.52k | coap_delete_string(name); |
1030 | 2.52k | coap_delete_binary(raw_packet); |
1031 | 2.52k | coap_delete_string(query); |
1032 | 2.52k | coap_delete_pdu_lkd(request); |
1033 | 2.52k | coap_delete_pdu_lkd(response); |
1034 | 2.52k | fclose(fp_orig); |
1035 | 2.52k | coap_free_type(COAP_SESSION, session); |
1036 | 2.52k | } |
1037 | | |
1038 | | /* |
1039 | | * Server has set up a new dynamic resource against a request for an unknown |
1040 | | * resource. |
1041 | | */ |
1042 | | static int |
1043 | | coap_op_dyn_resource_added(coap_session_t *session, |
1044 | | coap_str_const_t *resource_name, |
1045 | | coap_bin_const_t *packet, |
1046 | 0 | void *user_data) { |
1047 | 0 | FILE *fp_orig; |
1048 | 0 | FILE *fp_new = NULL; |
1049 | 0 | char *new = NULL; |
1050 | 0 | coap_context_t *context = session->context; |
1051 | 0 | coap_string_t *name = NULL; |
1052 | 0 | coap_binary_t *raw_packet = NULL; |
1053 | 0 | coap_proto_t e_proto; |
1054 | |
|
1055 | 0 | (void)user_data; |
1056 | |
|
1057 | 0 | fp_orig = fopen((const char *)context->dyn_resource_save_file->s, "a"); |
1058 | 0 | if (fp_orig == NULL) |
1059 | 0 | return 0; |
1060 | | |
1061 | 0 | new = coap_malloc_type(COAP_STRING, |
1062 | 0 | context->dyn_resource_save_file->length + 5); |
1063 | 0 | if (!new) |
1064 | 0 | goto fail; |
1065 | | |
1066 | 0 | strcpy(new, (const char *)context->dyn_resource_save_file->s); |
1067 | 0 | strcat(new, ".tmp"); |
1068 | 0 | fp_new = fopen(new, "w+"); |
1069 | 0 | if (fp_new == NULL) |
1070 | 0 | goto fail; |
1071 | | |
1072 | | /* Go through and locate duplicate resource to delete */ |
1073 | 0 | while (1) { |
1074 | 0 | if (!coap_op_dyn_resource_read(fp_orig, &e_proto, &name, &raw_packet)) |
1075 | 0 | break; |
1076 | 0 | if (!coap_string_equal(resource_name, name)) { |
1077 | | /* Copy across non-matching entry */ |
1078 | 0 | if (!coap_op_dyn_resource_write(fp_new, e_proto, (coap_str_const_t *)name, |
1079 | 0 | (coap_bin_const_t *)raw_packet)) |
1080 | 0 | break; |
1081 | 0 | } |
1082 | 0 | coap_delete_string(name); |
1083 | 0 | name = NULL; |
1084 | 0 | coap_delete_binary(raw_packet); |
1085 | 0 | raw_packet = NULL; |
1086 | 0 | } |
1087 | 0 | coap_delete_string(name); |
1088 | 0 | coap_delete_binary(raw_packet); |
1089 | | /* Add new entry to the end */ |
1090 | 0 | if (!coap_op_dyn_resource_write(fp_new, session->proto, |
1091 | 0 | resource_name, packet)) |
1092 | 0 | goto fail; |
1093 | | |
1094 | 0 | if (fflush(fp_new) == EOF) |
1095 | 0 | goto fail; |
1096 | 0 | fclose(fp_new); |
1097 | 0 | fclose(fp_orig); |
1098 | | /* Either old or new is in place */ |
1099 | 0 | (void)rename(new, (const char *)context->dyn_resource_save_file->s); |
1100 | 0 | coap_free_type(COAP_STRING, new); |
1101 | 0 | return 1; |
1102 | | |
1103 | 0 | fail: |
1104 | 0 | if (fp_new) |
1105 | 0 | fclose(fp_new); |
1106 | 0 | if (fp_orig) |
1107 | 0 | fclose(fp_orig); |
1108 | 0 | if (new) { |
1109 | 0 | (void)remove(new); |
1110 | 0 | } |
1111 | 0 | coap_free_type(COAP_STRING, new); |
1112 | 0 | return 0; |
1113 | 0 | } |
1114 | | |
1115 | | /* |
1116 | | * Server has deleted a resource |
1117 | | */ |
1118 | | static int |
1119 | | coap_op_resource_deleted(coap_context_t *context, |
1120 | | coap_str_const_t *resource_name, |
1121 | 0 | void *user_data) { |
1122 | 0 | FILE *fp_orig = NULL; |
1123 | 0 | FILE *fp_new = NULL; |
1124 | 0 | char *new = NULL; |
1125 | 0 | coap_proto_t e_proto; |
1126 | 0 | coap_string_t *name = NULL; |
1127 | 0 | coap_binary_t *raw_packet = NULL; |
1128 | 0 | (void)user_data; |
1129 | |
|
1130 | 0 | coap_op_obs_cnt_deleted(context, resource_name); |
1131 | |
|
1132 | 0 | fp_orig = fopen((const char *)context->dyn_resource_save_file->s, "r"); |
1133 | 0 | if (fp_orig == NULL) |
1134 | 0 | return 1; |
1135 | | |
1136 | 0 | new = coap_malloc_type(COAP_STRING, |
1137 | 0 | context->dyn_resource_save_file->length + 5); |
1138 | 0 | if (!new) |
1139 | 0 | goto fail; |
1140 | | |
1141 | 0 | strcpy(new, (const char *)context->dyn_resource_save_file->s); |
1142 | 0 | strcat(new, ".tmp"); |
1143 | 0 | fp_new = fopen(new, "w+"); |
1144 | 0 | if (fp_new == NULL) |
1145 | 0 | goto fail; |
1146 | | |
1147 | | /* Go through and locate resource to delete and not copy it across */ |
1148 | 0 | while (1) { |
1149 | 0 | if (!coap_op_dyn_resource_read(fp_orig, &e_proto, &name, &raw_packet)) |
1150 | 0 | break; |
1151 | 0 | if (!coap_string_equal(resource_name, name)) { |
1152 | | /* Copy across non-matching entry */ |
1153 | 0 | if (!coap_op_dyn_resource_write(fp_new, e_proto, (coap_str_const_t *)name, |
1154 | 0 | (coap_bin_const_t *)raw_packet)) |
1155 | 0 | break; |
1156 | 0 | } |
1157 | 0 | coap_delete_string(name); |
1158 | 0 | name = NULL; |
1159 | 0 | coap_delete_binary(raw_packet); |
1160 | 0 | raw_packet = NULL; |
1161 | 0 | } |
1162 | 0 | coap_delete_string(name); |
1163 | 0 | coap_delete_binary(raw_packet); |
1164 | |
|
1165 | 0 | if (fflush(fp_new) == EOF) |
1166 | 0 | goto fail; |
1167 | 0 | fclose(fp_new); |
1168 | 0 | fclose(fp_orig); |
1169 | | /* Either old or new is in place */ |
1170 | 0 | (void)rename(new, (const char *)context->dyn_resource_save_file->s); |
1171 | 0 | coap_free_type(COAP_STRING, new); |
1172 | 0 | return 1; |
1173 | | |
1174 | 0 | fail: |
1175 | 0 | if (fp_new) |
1176 | 0 | fclose(fp_new); |
1177 | 0 | if (fp_orig) |
1178 | 0 | fclose(fp_orig); |
1179 | 0 | if (new) { |
1180 | 0 | (void)remove(new); |
1181 | 0 | } |
1182 | 0 | coap_free_type(COAP_STRING, new); |
1183 | 0 | return 0; |
1184 | 0 | } |
1185 | | |
1186 | | COAP_API int |
1187 | | coap_persist_startup(coap_context_t *context, |
1188 | | const char *dyn_resource_save_file, |
1189 | | const char *observe_save_file, |
1190 | | const char *obs_cnt_save_file, |
1191 | 2.52k | uint32_t save_freq) { |
1192 | 2.52k | int ret; |
1193 | | |
1194 | 2.52k | coap_lock_lock(return 0); |
1195 | 2.52k | ret = coap_persist_startup_lkd(context, |
1196 | 2.52k | dyn_resource_save_file, |
1197 | 2.52k | observe_save_file, |
1198 | 2.52k | obs_cnt_save_file, |
1199 | 2.52k | save_freq); |
1200 | 2.52k | coap_lock_unlock(); |
1201 | 2.52k | return ret; |
1202 | 2.52k | } |
1203 | | |
1204 | | int |
1205 | | coap_persist_startup_lkd(coap_context_t *context, |
1206 | | const char *dyn_resource_save_file, |
1207 | | const char *observe_save_file, |
1208 | | const char *obs_cnt_save_file, |
1209 | 2.52k | uint32_t save_freq) { |
1210 | 2.52k | coap_lock_check_locked(); |
1211 | 2.52k | if (dyn_resource_save_file) { |
1212 | 2.52k | context->dyn_resource_save_file = |
1213 | 2.52k | coap_new_bin_const((const uint8_t *)dyn_resource_save_file, |
1214 | 2.52k | strlen(dyn_resource_save_file)); |
1215 | 2.52k | if (!context->dyn_resource_save_file) |
1216 | 0 | return 0; |
1217 | 2.52k | coap_op_dyn_resource_load_disk(context); |
1218 | 2.52k | context->dyn_resource_added_cb = coap_op_dyn_resource_added; |
1219 | 2.52k | context->resource_deleted_cb = coap_op_resource_deleted; |
1220 | 2.52k | } |
1221 | 2.52k | if (obs_cnt_save_file) { |
1222 | 2.52k | context->obs_cnt_save_file = |
1223 | 2.52k | coap_new_bin_const((const uint8_t *)obs_cnt_save_file, |
1224 | 2.52k | strlen(obs_cnt_save_file)); |
1225 | 2.52k | if (!context->obs_cnt_save_file) |
1226 | 0 | return 0; |
1227 | 2.52k | context->observe_save_freq = save_freq ? save_freq : 1; |
1228 | 2.52k | coap_op_obs_cnt_load_disk(context); |
1229 | 2.52k | context->track_observe_value_cb = coap_op_obs_cnt_track_observe; |
1230 | 2.52k | context->resource_deleted_cb = coap_op_resource_deleted; |
1231 | 2.52k | } |
1232 | 2.52k | if (observe_save_file) { |
1233 | 2.52k | context->observe_save_file = |
1234 | 2.52k | coap_new_bin_const((const uint8_t *)observe_save_file, |
1235 | 2.52k | strlen(observe_save_file)); |
1236 | 2.52k | if (!context->observe_save_file) |
1237 | 0 | return 0; |
1238 | 2.52k | coap_op_observe_load_disk(context); |
1239 | 2.52k | context->observe_added_cb = coap_op_observe_added; |
1240 | 2.52k | context->observe_deleted_cb = coap_op_observe_deleted; |
1241 | 2.52k | } |
1242 | 2.52k | return 1; |
1243 | 2.52k | } |
1244 | | |
1245 | | void |
1246 | 5.05k | coap_persist_cleanup(coap_context_t *context) { |
1247 | 5.05k | coap_delete_bin_const(context->dyn_resource_save_file); |
1248 | 5.05k | coap_delete_bin_const(context->obs_cnt_save_file); |
1249 | 5.05k | coap_delete_bin_const(context->observe_save_file); |
1250 | 5.05k | context->dyn_resource_save_file = NULL; |
1251 | 5.05k | context->obs_cnt_save_file = NULL; |
1252 | 5.05k | context->observe_save_file = NULL; |
1253 | | |
1254 | | /* Close down any tracking */ |
1255 | 5.05k | coap_persist_track_funcs(context, NULL, NULL, NULL, NULL, |
1256 | 5.05k | NULL, 0, NULL); |
1257 | 5.05k | } |
1258 | | |
1259 | | COAP_API void |
1260 | 2.52k | coap_persist_stop(coap_context_t *context) { |
1261 | 2.52k | if (!context) |
1262 | 0 | return; |
1263 | 2.52k | coap_lock_lock(return); |
1264 | 2.52k | coap_persist_stop_lkd(context); |
1265 | 2.52k | coap_lock_unlock(); |
1266 | 2.52k | } |
1267 | | |
1268 | | void |
1269 | 2.52k | coap_persist_stop_lkd(coap_context_t *context) { |
1270 | 2.52k | if (context == NULL) |
1271 | 0 | return; |
1272 | 2.52k | coap_lock_check_locked(); |
1273 | 2.52k | context->observe_no_clear = 1; |
1274 | 2.52k | coap_persist_cleanup(context); |
1275 | 2.52k | } |
1276 | | #else /* ! COAP_WITH_OBSERVE_PERSIST */ |
1277 | | COAP_API int |
1278 | | coap_persist_startup(coap_context_t *context, |
1279 | | const char *dyn_resource_save_file, |
1280 | | const char *observe_save_file, |
1281 | | const char *obs_cnt_save_file, |
1282 | | uint32_t save_freq) { |
1283 | | (void)context; |
1284 | | (void)dyn_resource_save_file; |
1285 | | (void)observe_save_file; |
1286 | | (void)obs_cnt_save_file; |
1287 | | (void)save_freq; |
1288 | | return 0; |
1289 | | } |
1290 | | |
1291 | | COAP_API void |
1292 | | coap_persist_stop(coap_context_t *context) { |
1293 | | context->observe_no_clear = 1; |
1294 | | /* Close down any tracking */ |
1295 | | coap_persist_track_funcs(context, NULL, NULL, NULL, NULL, |
1296 | | NULL, 0, NULL); |
1297 | | } |
1298 | | |
1299 | | #endif /* ! COAP_WITH_OBSERVE_PERSIST */ |
1300 | | |
1301 | | #endif /* COAP_SERVER_SUPPORT */ |