forked from obgm/libcoap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoap_session.c
2240 lines (2021 loc) · 67.5 KB
/
coap_session.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* coap_session.c -- Session management for libcoap
*
* Copyright (C) 2017 Jean-Claue Michelou <[email protected]>
* Copyright (C) 2022-2024 Jon Shallow <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
/**
* @file coap_session.c
* @brief Session handling functions
*/
#include "coap3/coap_libcoap_build.h"
#ifndef COAP_SESSION_C_
#define COAP_SESSION_C_
#include <stdio.h>
#ifdef COAP_EPOLL_SUPPORT
#include <sys/epoll.h>
#include <sys/timerfd.h>
#endif /* COAP_EPOLL_SUPPORT */
coap_fixed_point_t
coap_multi_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2) {
coap_fixed_point_t res;
uint32_t fr = fp1.fractional_part * fp2.fractional_part;
res.integer_part = fp1.integer_part * fp2.integer_part + fr/1000;
res.fractional_part = fr % 1000;
return res;
}
coap_fixed_point_t
coap_multi_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) {
coap_fixed_point_t res;
uint32_t fr = fp1.fractional_part * u2;
res.integer_part = fp1.integer_part * u2 + fr/1000;
res.fractional_part = fr % 1000;
return res;
}
coap_fixed_point_t
coap_add_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2) {
coap_fixed_point_t res;
uint32_t fr = fp1.fractional_part + fp2.fractional_part;
res.integer_part = fp1.integer_part + fp2.integer_part + fr/1000;
res.fractional_part = fr % 1000;
return res;
}
coap_fixed_point_t
coap_add_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) {
coap_fixed_point_t res = fp1;
res.integer_part += u2;
return res;
}
coap_fixed_point_t
coap_sub_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) {
coap_fixed_point_t res = fp1;
res.integer_part -= u2;
return res;
}
coap_fixed_point_t
coap_div_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) {
coap_fixed_point_t res;
uint32_t num = (fp1.integer_part * 1000 + fp1.fractional_part) / u2;
res.integer_part = num / 1000;
res.fractional_part = num % 1000;
return res;
}
#if COAP_Q_BLOCK_SUPPORT
coap_fixed_point_t
coap_get_non_timeout_random(coap_session_t *session) {
coap_fixed_point_t res;
uint8_t ran;
coap_prng(&ran, sizeof(ran));
res = coap_sub_fixed_uint(COAP_ACK_RANDOM_FACTOR(session), 1);
res = coap_multi_fixed_uint(res, ran);
res = coap_div_fixed_uint(res, 0xff);
res = coap_add_fixed_fixed(COAP_NON_TIMEOUT(session), res);
return res;
}
coap_tick_t
coap_get_non_timeout_random_ticks(coap_session_t *session) {
coap_fixed_point_t res = coap_get_non_timeout_random(session);
coap_tick_t ticks = (res.integer_part * COAP_TICKS_PER_SECOND +
res.fractional_part * COAP_TICKS_PER_SECOND / 1000);
return ticks;
}
/*
* Save away derived Congestion Control parameters for speed of access.
* They will get updated whenever a component variable is updated.
*/
/*
* NON_PROBING_WAIT = NON_TIMEOUT * ((2 ** NON_MAX_RETRANSMIT) - 1) *
* ACK_RANDOM_FACTOR + (2 * MAX_LATENCY) + NON_TIMEOUT_RANDOM
*
* Do not include NON_TIMEOUT_RANDOM as that changes
*/
static void
coap_session_fix_non_probing_wait_base(coap_session_t *s) {
coap_fixed_point_t res;
res = coap_multi_fixed_uint(COAP_NON_TIMEOUT(s),
((1 << (COAP_NON_MAX_RETRANSMIT(s) + 1)) -1));
res = coap_multi_fixed_fixed(res, COAP_ACK_RANDOM_FACTOR(s));
res = coap_add_fixed_uint(res, 2 * COAP_DEFAULT_MAX_LATENCY);
COAP_NON_PROBING_WAIT_BASE(s) = res;
}
/*
* NON_PARTIAL_TIMEOUT = NON_TIMEOUT * ((2 ** NON_MAX_RETRANSMIT) - 1) *
* ACK_RANDOM_FACTOR + (2 * MAX_LATENCY) + NON_TIMEOUT
*/
static void
coap_session_fix_non_partial_timeout(coap_session_t *s) {
coap_fixed_point_t res;
res = coap_multi_fixed_uint(COAP_NON_TIMEOUT(s),
((1 << (COAP_NON_MAX_RETRANSMIT(s) + 1)) -1));
res = coap_multi_fixed_fixed(res, COAP_ACK_RANDOM_FACTOR(s));
res = coap_add_fixed_uint(res, 2 * COAP_DEFAULT_MAX_LATENCY);
res = coap_add_fixed_fixed(res, COAP_NON_TIMEOUT(s));
COAP_NON_PARTIAL_TIMEOUT(s) = res;
}
#endif /* COAP_Q_BLOCK_SUPPORT */
void
coap_session_set_ack_timeout(coap_session_t *session, coap_fixed_point_t value) {
if (value.integer_part > 0 && value.fractional_part < 1000) {
session->ack_timeout = value;
coap_log_debug("***%s: session ack_timeout set to %u.%03u\n",
coap_session_str(session), session->ack_timeout.integer_part,
session->ack_timeout.fractional_part);
}
}
void
coap_session_set_ack_random_factor(coap_session_t *session,
coap_fixed_point_t value) {
if (value.integer_part > 0 && value.fractional_part < 1000) {
session->ack_random_factor = value;
coap_log_debug("***%s: session ack_random_factor set to %u.%03u\n",
coap_session_str(session), session->ack_random_factor.integer_part,
session->ack_random_factor.fractional_part);
#if COAP_Q_BLOCK_SUPPORT
coap_session_fix_non_probing_wait_base(session);
coap_session_fix_non_partial_timeout(session);
#endif /* COAP_Q_BLOCK_SUPPORT */
}
return;
}
void
coap_session_set_max_retransmit(coap_session_t *session, uint16_t value) {
if (value > 0) {
session->max_retransmit = value;
coap_log_debug("***%s: session max_retransmit set to %u\n",
coap_session_str(session), session->max_retransmit);
}
}
void
coap_session_set_nstart(coap_session_t *session, uint16_t value) {
if (value > 0) {
session->nstart = value;
coap_log_debug("***%s: session nstart set to %u\n",
coap_session_str(session), session->nstart);
}
}
void
coap_session_set_default_leisure(coap_session_t *session,
coap_fixed_point_t value) {
if (value.integer_part > 0 && value.fractional_part < 1000) {
session->default_leisure = value;
coap_log_debug("***%s: session default_leisure set to %u.%03u\n",
coap_session_str(session), session->default_leisure.integer_part,
session->default_leisure.fractional_part);
}
}
void
coap_session_set_probing_rate(coap_session_t *session, uint32_t value) {
if (value > 0) {
session->probing_rate = value;
coap_log_debug("***%s: session probing_rate set to %" PRIu32 "\n",
coap_session_str(session), session->probing_rate);
}
}
void
coap_session_set_max_payloads(coap_session_t *session, uint16_t value) {
#if COAP_Q_BLOCK_SUPPORT
if (value > 0) {
session->max_payloads = value;
coap_log_debug("***%s: session max_payloads set to %u\n",
coap_session_str(session), session->max_payloads);
coap_session_fix_non_probing_wait_base(session);
coap_session_fix_non_partial_timeout(session);
}
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
(void)value;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
void
coap_session_set_non_max_retransmit(coap_session_t *session, uint16_t value) {
#if COAP_Q_BLOCK_SUPPORT
if (value > 0) {
session->non_max_retransmit = value;
coap_log_debug("***%s: session non_max_retransmit set to %u\n",
coap_session_str(session), session->non_max_retransmit);
coap_session_fix_non_probing_wait_base(session);
coap_session_fix_non_partial_timeout(session);
}
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
(void)value;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
void
coap_session_set_non_timeout(coap_session_t *session,
coap_fixed_point_t value) {
#if COAP_Q_BLOCK_SUPPORT
if (value.integer_part > 0 && value.fractional_part < 1000) {
session->non_timeout = value;
coap_log_debug("***%s: session non_timeout set to %u.%03u\n",
coap_session_str(session), session->non_timeout.integer_part,
session->non_timeout.fractional_part);
coap_session_fix_non_probing_wait_base(session);
coap_session_fix_non_partial_timeout(session);
}
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
(void)value;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
void
coap_session_set_non_receive_timeout(coap_session_t *session,
coap_fixed_point_t value) {
#if COAP_Q_BLOCK_SUPPORT
if (value.integer_part > 0 && value.fractional_part < 1000)
session->non_receive_timeout = value;
coap_log_debug("***%s: session non_receive_timeout set to %u.%03u\n",
coap_session_str(session),
session->non_receive_timeout.integer_part,
session->non_receive_timeout.fractional_part);
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
(void)value;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
coap_fixed_point_t
coap_session_get_ack_timeout(const coap_session_t *session) {
return session->ack_timeout;
}
coap_fixed_point_t
coap_session_get_ack_random_factor(const coap_session_t *session) {
return session->ack_random_factor;
}
uint16_t
coap_session_get_max_retransmit(const coap_session_t *session) {
return session->max_retransmit;
}
uint16_t
coap_session_get_nstart(const coap_session_t *session) {
return session->nstart;
}
coap_fixed_point_t
coap_session_get_default_leisure(const coap_session_t *session) {
return session->default_leisure;
}
uint32_t
coap_session_get_probing_rate(const coap_session_t *session) {
return session->probing_rate;
}
uint16_t
coap_session_get_max_payloads(const coap_session_t *session) {
#if COAP_Q_BLOCK_SUPPORT
return session->max_payloads;
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
return COAP_DEFAULT_MAX_PAYLOADS;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
uint16_t
coap_session_get_non_max_retransmit(const coap_session_t *session) {
#if COAP_Q_BLOCK_SUPPORT
return session->non_max_retransmit;
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
return COAP_DEFAULT_NON_MAX_RETRANSMIT;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
coap_fixed_point_t
coap_session_get_non_timeout(const coap_session_t *session) {
#if COAP_Q_BLOCK_SUPPORT
return session->non_timeout;
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
return COAP_DEFAULT_NON_TIMEOUT;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
coap_fixed_point_t
coap_session_get_non_receive_timeout(const coap_session_t *session) {
#if COAP_Q_BLOCK_SUPPORT
return session->non_receive_timeout;
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
return COAP_DEFAULT_NON_RECEIVE_TIMEOUT;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
COAP_API coap_session_t *
coap_session_reference(coap_session_t *session) {
coap_lock_lock(session->context, return NULL);
coap_session_reference_lkd(session);
coap_lock_unlock(session->context);
return session;
}
coap_session_t *
coap_session_reference_lkd(coap_session_t *session) {
++session->ref;
return session;
}
COAP_API void
coap_session_release(coap_session_t *session) {
if (session) {
#if COAP_THREAD_SAFE
coap_context_t *context = session->context;
(void)context;
#endif /* COAP_THREAD_SAFE */
coap_lock_lock(context, return);
coap_session_release_lkd(session);
coap_lock_unlock(context);
}
}
void
coap_session_release_lkd(coap_session_t *session) {
if (session) {
coap_lock_check_locked(session->context);
#ifndef __COVERITY__
assert(session->ref > 0);
if (session->ref > 0)
--session->ref;
if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT)
coap_session_free(session);
#else /* __COVERITY__ */
/* Coverity scan is fooled by the reference counter leading to
* false positives for USE_AFTER_FREE. */
--session->ref;
__coverity_negative_sink__(session->ref);
/* Indicate that resources are released properly. */
if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT) {
__coverity_free__(session);
}
#endif /* __COVERITY__ */
}
}
void
coap_session_set_app_data(coap_session_t *session, void *app_data) {
assert(session);
session->app = app_data;
}
void *
coap_session_get_app_data(const coap_session_t *session) {
assert(session);
return session->app;
}
static coap_session_t *
coap_make_session(coap_proto_t proto, coap_session_type_t type,
const coap_addr_hash_t *addr_hash,
const coap_address_t *local_addr,
const coap_address_t *remote_addr, int ifindex,
coap_context_t *context, coap_endpoint_t *endpoint) {
coap_session_t *session = (coap_session_t *)coap_malloc_type(COAP_SESSION,
sizeof(coap_session_t));
#if ! COAP_SERVER_SUPPORT
(void)endpoint;
#endif /* ! COAP_SERVER_SUPPORT */
if (!session)
return NULL;
memset(session, 0, sizeof(*session));
session->proto = proto;
session->type = type;
if (addr_hash)
memcpy(&session->addr_hash, addr_hash, sizeof(session->addr_hash));
else
memset(&session->addr_hash, 0, sizeof(session->addr_hash));
if (local_addr)
coap_address_copy(&session->addr_info.local, local_addr);
else
coap_address_init(&session->addr_info.local);
if (remote_addr)
coap_address_copy(&session->addr_info.remote, remote_addr);
else
coap_address_init(&session->addr_info.remote);
session->ifindex = ifindex;
session->context = context;
#if COAP_SERVER_SUPPORT
session->endpoint = endpoint;
if (endpoint)
session->mtu = endpoint->default_mtu;
else
#endif /* COAP_SERVER_SUPPORT */
session->mtu = COAP_DEFAULT_MTU;
session->block_mode = context->block_mode;
if (proto == COAP_PROTO_DTLS) {
session->tls_overhead = 29;
if (session->tls_overhead >= session->mtu) {
session->tls_overhead = session->mtu;
coap_log_err("DTLS overhead exceeds MTU\n");
}
}
session->ack_timeout = COAP_DEFAULT_ACK_TIMEOUT;
session->ack_random_factor = COAP_DEFAULT_ACK_RANDOM_FACTOR;
session->max_retransmit = COAP_DEFAULT_MAX_RETRANSMIT;
session->nstart = COAP_DEFAULT_NSTART;
session->default_leisure = COAP_DEFAULT_DEFAULT_LEISURE;
session->probing_rate = COAP_DEFAULT_PROBING_RATE;
#if COAP_Q_BLOCK_SUPPORT
session->max_payloads = COAP_DEFAULT_MAX_PAYLOADS;
session->non_max_retransmit = COAP_DEFAULT_NON_MAX_RETRANSMIT;
session->non_timeout = COAP_DEFAULT_NON_TIMEOUT;
session->non_receive_timeout = COAP_DEFAULT_NON_RECEIVE_TIMEOUT;
coap_session_fix_non_probing_wait_base(session);
coap_session_fix_non_partial_timeout(session);
#endif /* COAP_Q_BLOCK_SUPPORT */
session->dtls_event = -1;
session->last_ping_mid = COAP_INVALID_MID;
session->last_ack_mid = COAP_INVALID_MID;
session->last_con_mid = COAP_INVALID_MID;
session->last_con_handler_res = COAP_RESPONSE_OK;
session->max_token_size = context->max_token_size; /* RFC8974 */
if (session->type != COAP_SESSION_TYPE_CLIENT)
session->max_token_checked = COAP_EXT_T_CHECKED;
/* Randomly initialize */
/* TCP/TLS have no notion of mid */
if (COAP_PROTO_NOT_RELIABLE(session->proto))
coap_prng((unsigned char *)&session->tx_mid, sizeof(session->tx_mid));
coap_prng((unsigned char *)&session->tx_rtag, sizeof(session->tx_rtag));
return session;
}
void
coap_session_mfree(coap_session_t *session) {
coap_queue_t *q, *tmp;
coap_lg_xmit_t *lq, *ltmp;
#if COAP_CLIENT_SUPPORT
coap_lg_crcv_t *lg_crcv, *etmp;
/* Need to do this before (D)TLS and socket is closed down */
LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) {
if (lg_crcv->observe_set && session->no_observe_cancel == 0) {
/* Need to close down observe */
if (coap_cancel_observe_lkd(session, lg_crcv->app_token, COAP_MESSAGE_NON)) {
/* Need to delete node we set up for NON */
coap_queue_t *queue = session->context->sendqueue;
while (queue) {
if (queue->session == session) {
coap_delete_node_lkd(queue);
break;
}
queue = queue->next;
}
}
}
LL_DELETE(session->lg_crcv, lg_crcv);
coap_block_delete_lg_crcv(session, lg_crcv);
}
#endif /* COAP_CLIENT_SUPPORT */
if (session->partial_pdu)
coap_delete_pdu(session->partial_pdu);
if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
if (session->psk_identity)
coap_delete_bin_const(session->psk_identity);
if (session->psk_key)
coap_delete_bin_const(session->psk_key);
if (session->psk_hint)
coap_delete_bin_const(session->psk_hint);
#if COAP_SERVER_SUPPORT
coap_cache_entry_t *cp, *ctmp;
HASH_ITER(hh, session->context->cache, cp, ctmp) {
/* cp->session is NULL if not session based */
if (cp->session == session) {
coap_delete_cache_entry(session->context, cp);
}
}
#endif /* COAP_SERVER_SUPPORT */
LL_FOREACH_SAFE(session->delayqueue, q, tmp) {
if (q->pdu->type==COAP_MESSAGE_CON && session->context->nack_handler) {
coap_check_update_token(session, q->pdu);
coap_lock_callback(session->context,
session->context->nack_handler(session, q->pdu,
session->proto == COAP_PROTO_DTLS ?
COAP_NACK_TLS_FAILED : COAP_NACK_NOT_DELIVERABLE,
q->id));
}
coap_delete_node_lkd(q);
}
LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
LL_DELETE(session->lg_xmit, lq);
coap_block_delete_lg_xmit(session, lq);
}
#if COAP_SERVER_SUPPORT
coap_lg_srcv_t *sq, *stmp;
LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
LL_DELETE(session->lg_srcv, sq);
coap_block_delete_lg_srcv(session, sq);
}
#endif /* COAP_SERVER_SUPPORT */
#if COAP_OSCORE_SUPPORT
coap_delete_oscore_associations(session);
#endif /* COAP_OSCORE_SUPPORT */
#if COAP_WS_SUPPORT
coap_free_type(COAP_STRING, session->ws);
coap_delete_str_const(session->ws_host);
#endif /* COAP_WS_SUPPORT */
}
void
coap_session_free(coap_session_t *session) {
if (!session)
return;
coap_lock_check_locked(session->context);
assert(session->ref == 0);
if (session->ref)
return;
/* Make sure nothing gets deleted under our feet */
coap_session_reference_lkd(session);
coap_session_mfree(session);
#if COAP_SERVER_SUPPORT
if (session->endpoint) {
if (session->endpoint->sessions)
SESSIONS_DELETE(session->endpoint->sessions, session);
} else
#endif /* COAP_SERVER_SUPPORT */
#if COAP_CLIENT_SUPPORT
if (session->context) {
if (session->context->sessions)
SESSIONS_DELETE(session->context->sessions, session);
}
#endif /* COAP_CLIENT_SUPPORT */
coap_delete_bin_const(session->last_token);
coap_delete_bin_const(session->echo);
coap_log_debug("***%s: session %p: closed\n", coap_session_str(session),
(void *)session);
assert(session->ref == 1);
coap_free_type(COAP_SESSION, session);
}
static size_t
coap_session_max_pdu_size_internal(const coap_session_t *session,
size_t max_with_header) {
#if COAP_DISABLE_TCP
(void)session;
return max_with_header > 4 ? max_with_header - 4 : 0;
#else /* !COAP_DISABLE_TCP */
if (COAP_PROTO_NOT_RELIABLE(session->proto))
return max_with_header > 4 ? max_with_header - 4 : 0;
/* we must assume there is no token to be on the safe side */
if (max_with_header <= 2)
return 0;
else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2)
return max_with_header - 2;
else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3)
return max_with_header - 3;
else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
return max_with_header - 4;
else
return max_with_header - 6;
#endif /* !COAP_DISABLE_TCP */
}
size_t
coap_session_max_pdu_rcv_size(const coap_session_t *session) {
if (session->csm_rcv_mtu)
return coap_session_max_pdu_size_internal(session,
(size_t)(session->csm_rcv_mtu));
return coap_session_max_pdu_size_internal(session,
(size_t)(session->mtu - session->tls_overhead));
}
COAP_API size_t
coap_session_max_pdu_size(const coap_session_t *session) {
size_t size;
coap_session_t *session_rw;
/*
* Need to do this to not get a compiler warning about const parameters
* but need to maintain source code backward compatibility
*/
memcpy(&session_rw, &session, sizeof(session_rw));
coap_lock_lock(session_rw->context, return 0);
size = coap_session_max_pdu_size_lkd(session_rw);
coap_lock_unlock(session_rw->context);
return size;
}
size_t
coap_session_max_pdu_size_lkd(const coap_session_t *session) {
size_t max_with_header;
coap_lock_check_locked(session->context);
#if COAP_CLIENT_SUPPORT
/*
* Delay if session->doing_first is set.
* E.g. Reliable and CSM not in yet for checking block support
*/
coap_session_t *session_rw;
/*
* Need to do this to not get a compiler warning about const parameters
* but need to maintain source code backward compatibility
*/
memcpy(&session_rw, &session, sizeof(session_rw));
if (coap_client_delay_first(session_rw) == 0) {
coap_log_debug("coap_client_delay_first: timeout\n");
/* Have to go with the defaults */
}
#endif /* COAP_CLIENT_SUPPORT */
max_with_header = (size_t)(session->mtu - session->tls_overhead);
return coap_session_max_pdu_size_internal(session, max_with_header);
}
void
coap_session_set_mtu(coap_session_t *session, unsigned mtu) {
#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
if (mtu > COAP_DEFAULT_MAX_PDU_RX_SIZE)
mtu = COAP_DEFAULT_MAX_PDU_RX_SIZE;
#endif
if (mtu < 64)
mtu = 64;
session->mtu = mtu;
if (session->tls_overhead >= session->mtu) {
session->tls_overhead = session->mtu;
coap_log_err("DTLS overhead exceeds MTU\n");
}
}
ssize_t
coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu,
coap_queue_t *node) {
if (node) {
coap_queue_t *removed = NULL;
coap_remove_from_queue(&session->context->sendqueue, session, node->id, &removed);
assert(removed == node);
coap_session_release_lkd(node->session);
node->session = NULL;
node->t = 0;
} else {
if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
coap_queue_t *q = NULL;
/* Check same mid is not getting re-used in violation of RFC7252 */
LL_FOREACH(session->delayqueue, q) {
if (q->id == pdu->mid) {
coap_log_err("** %s: mid=0x%04x: already in-use - dropped\n",
coap_session_str(session), pdu->mid);
return COAP_INVALID_MID;
}
}
}
node = coap_new_node();
if (node == NULL)
return COAP_INVALID_MID;
node->id = pdu->mid;
node->pdu = pdu;
if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
uint8_t r;
coap_prng(&r, sizeof(r));
/* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
node->timeout = coap_calc_timeout(session, r);
}
}
LL_APPEND(session->delayqueue, node);
coap_log_debug("** %s: mid=0x%04x: delayed\n",
coap_session_str(session), node->id);
return COAP_PDU_DELAYED;
}
#if !COAP_DISABLE_TCP
void
coap_session_send_csm(coap_session_t *session) {
coap_pdu_t *pdu;
uint8_t buf[4];
assert(COAP_PROTO_RELIABLE(session->proto));
coap_log_debug("***%s: sending CSM\n", coap_session_str(session));
session->state = COAP_SESSION_STATE_CSM;
session->partial_write = 0;
if (session->mtu == 0)
session->mtu = COAP_DEFAULT_MTU; /* base value */
pdu = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CODE_CSM, 0, 20);
if (pdu == NULL
|| coap_add_option_internal(pdu, COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE,
coap_encode_var_safe(buf, sizeof(buf),
session->context->csm_max_message_size), buf) == 0
|| coap_add_option_internal(pdu, COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER,
coap_encode_var_safe(buf, sizeof(buf),
0), buf) == 0
|| (session->max_token_size > COAP_TOKEN_DEFAULT_MAX &&
coap_add_option_internal(pdu,
COAP_SIGNALING_OPTION_EXTENDED_TOKEN_LENGTH,
coap_encode_var_safe(buf, sizeof(buf),
session->max_token_size),
buf) == 0)
|| coap_pdu_encode_header(pdu, session->proto) == 0
) {
coap_session_disconnected_lkd(session, COAP_NACK_NOT_DELIVERABLE);
} else {
ssize_t bytes_written;
pdu->session = session;
bytes_written = coap_session_send_pdu(session, pdu);
if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) {
coap_session_disconnected_lkd(session, COAP_NACK_NOT_DELIVERABLE);
} else {
session->csm_rcv_mtu = session->context->csm_max_message_size;
if (session->csm_rcv_mtu > COAP_BERT_BASE)
session->csm_bert_loc_support = 1;
else
session->csm_bert_loc_support = 0;
}
}
if (pdu)
coap_delete_pdu(pdu);
}
#endif /* !COAP_DISABLE_TCP */
COAP_API coap_mid_t
coap_session_send_ping(coap_session_t *session) {
coap_mid_t mid;
coap_lock_lock(session->context, return COAP_INVALID_MID);
mid = coap_session_send_ping_lkd(session);
coap_lock_unlock(session->context);
return mid;
}
coap_mid_t
coap_session_send_ping_lkd(coap_session_t *session) {
coap_pdu_t *ping = NULL;
coap_lock_check_locked(session->context);
if (session->state != COAP_SESSION_STATE_ESTABLISHED ||
session->con_active)
return COAP_INVALID_MID;
if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
uint16_t mid = coap_new_message_id_lkd(session);
ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
}
#if !COAP_DISABLE_TCP
else {
ping = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CODE_PING, 0, 1);
}
#endif /* !COAP_DISABLE_TCP */
if (!ping)
return COAP_INVALID_MID;
return coap_send_internal(session, ping);
}
void
coap_session_connected(coap_session_t *session) {
if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
coap_log_debug("***%s: session connected\n",
coap_session_str(session));
if (session->state == COAP_SESSION_STATE_CSM) {
coap_handle_event_lkd(session->context, COAP_EVENT_SESSION_CONNECTED, session);
if (session->doing_first)
session->doing_first = 0;
}
}
session->state = COAP_SESSION_STATE_ESTABLISHED;
session->partial_write = 0;
if (session->proto==COAP_PROTO_DTLS) {
session->tls_overhead = coap_dtls_get_overhead(session);
if (session->tls_overhead >= session->mtu) {
session->tls_overhead = session->mtu;
coap_log_err("DTLS overhead exceeds MTU\n");
}
}
while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) {
ssize_t bytes_written;
coap_queue_t *q = session->delayqueue;
if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
if (session->con_active >= COAP_NSTART(session))
break;
session->con_active++;
}
/* Take entry off the queue */
session->delayqueue = q->next;
q->next = NULL;
coap_log_debug("** %s: mid=0x%04x: transmitted after delay\n",
coap_session_str(session), (int)q->pdu->mid);
bytes_written = coap_session_send_pdu(session, q->pdu);
if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
if (coap_wait_ack(session->context, session, q) >= 0)
q = NULL;
}
if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
if (q)
coap_delete_node_lkd(q);
if (bytes_written < 0)
break;
} else if (q) {
if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) {
q->next = session->delayqueue;
session->delayqueue = q;
if (bytes_written > 0)
session->partial_write = (size_t)bytes_written;
break;
} else {
coap_delete_node_lkd(q);
}
}
}
}
#if COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG
static const char *
coap_nack_name(coap_nack_reason_t reason) {
switch (reason) {
case COAP_NACK_TOO_MANY_RETRIES:
return "COAP_NACK_TOO_MANY_RETRIES";
case COAP_NACK_NOT_DELIVERABLE:
return "COAP_NACK_NOT_DELIVERABLE";
case COAP_NACK_RST:
return "COAP_NACK_RST";
case COAP_NACK_TLS_FAILED:
return "COAP_NACK_TLS_FAILED";
case COAP_NACK_ICMP_ISSUE:
return "COAP_NACK_ICMP_ISSUE";
case COAP_NACK_BAD_RESPONSE:
return "COAP_NACK_BAD_RESPONSE";
case COAP_NACK_TLS_LAYER_FAILED:
return "COAP_NACK_TLS_LAYER_FAILED";
case COAP_NACK_WS_LAYER_FAILED:
return "COAP_NACK_WS_LAYER_FAILED";
case COAP_NACK_WS_FAILED:
return "COAP_NACK_WS_FAILED";
default:
return "???";
}
}
#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG */
COAP_API void
coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason) {
coap_lock_lock(session->context, return);
coap_session_disconnected_lkd(session, reason);
coap_lock_unlock(session->context);
}
void
coap_session_disconnected_lkd(coap_session_t *session, coap_nack_reason_t reason) {
#if !COAP_DISABLE_TCP
coap_session_state_t state = session->state;
#endif /* !COAP_DISABLE_TCP */
coap_lg_xmit_t *lq, *ltmp;
#if COAP_SERVER_SUPPORT
coap_lg_srcv_t *sq, *stmp;
#endif /* COAP_SERVER_SUPPORT */
#if COAP_CLIENT_SUPPORT
coap_lg_crcv_t *cq, *etmp;
#endif /* COAP_CLIENT_SUPPORT */
coap_lock_check_locked(session->context);
if (session->context->nack_handler) {
int sent_nack = 0;
coap_queue_t *q = session->context->sendqueue;
while (q) {
if (q->session == session) {
/* Take the first one */
coap_bin_const_t token = q->pdu->actual_token;
coap_check_update_token(session, q->pdu);
coap_lock_callback(session->context,
session->context->nack_handler(session, q->pdu, reason, q->id));
coap_update_token(q->pdu, token.length, token.s);
sent_nack = 1;
break;
}
q = q->next;
}
if (reason != COAP_NACK_ICMP_ISSUE) {
while (session->delayqueue) {
q = session->delayqueue;
session->delayqueue = q->next;
q->next = NULL;
coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
coap_session_str(session), q->id);
if (q->pdu->type == COAP_MESSAGE_CON) {
coap_check_update_token(session, q->pdu);
coap_lock_callback(session->context,
session->context->nack_handler(session, q->pdu, reason, q->id));
sent_nack = 1;
}
coap_delete_node_lkd(q);
}
}
#if COAP_CLIENT_SUPPORT
if (!sent_nack && session->lg_crcv) {
/* Take the first one */
coap_lock_callback(session->context,
session->context->nack_handler(session, &session->lg_crcv->pdu, reason,
session->lg_crcv->pdu.mid));
sent_nack = 1;
}
#endif /* COAP_CLIENT_SUPPORT */
if (!sent_nack) {
/* Unable to determine which request disconnection was for */
coap_lock_callback(session->context,
session->context->nack_handler(session, NULL, reason, 0));
}
}
if (reason == COAP_NACK_ICMP_ISSUE) {
coap_log_debug("***%s: session ICMP issue (%s)\n",
coap_session_str(session), coap_nack_name(reason));
return;
}
coap_log_debug("***%s: session disconnected (%s)\n",
coap_session_str(session), coap_nack_name(reason));
#if COAP_SERVER_SUPPORT
coap_delete_observers(session->context, session);
#endif /* COAP_SERVER_SUPPORT */
if (session->proto == COAP_PROTO_UDP)
session->state = COAP_SESSION_STATE_ESTABLISHED;
else
session->state = COAP_SESSION_STATE_NONE;
session->con_active = 0;
if (session->partial_pdu) {
coap_delete_pdu(session->partial_pdu);
session->partial_pdu = NULL;
}
session->partial_read = 0;
/* Not done if nack handler called above */
while (session->delayqueue) {
coap_queue_t *q = session->delayqueue;
session->delayqueue = q->next;