forked from obgm/libcoap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoap_io_lwip.c
910 lines (782 loc) · 23.8 KB
/
coap_io_lwip.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
/*
* Copyright (C) 2012,2014 Olaf Bergmann <[email protected]>
* 2014 chrysn <[email protected]>
* 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_io_lwip.c
* @brief LwIP specific functions
*/
#include "coap3/coap_libcoap_build.h"
#include <lwip/udp.h>
#include <lwip/timeouts.h>
#include <lwip/tcpip.h>
void
coap_lwip_dump_memory_pools(coap_log_t log_level) {
#if MEMP_STATS && LWIP_STATS_DISPLAY
int i;
/* Save time if not needed */
if (log_level > coap_get_log_level())
return;
coap_log(log_level, "* LwIP custom memory pools information\n");
/*
* Make sure LwIP and libcoap have been built with the same
* -DCOAP_CLIENT_ONLY or -DCOAP_SERVER_ONLY options for
* MEMP_MAX to be correct.
*/
for (i = 0; i < MEMP_MAX; i++) {
coap_log(log_level, "* %-17s avail %3d in-use %3d peak %3d failed %3d\n",
memp_pools[i]->stats->name, memp_pools[i]->stats->avail,
memp_pools[i]->stats->used, memp_pools[i]->stats->max,
memp_pools[i]->stats->err);
}
#endif /* MEMP_STATS && LWIP_STATS_DISPLAY */
}
void
coap_lwip_set_input_wait_handler(coap_context_t *context,
coap_lwip_input_wait_handler_t handler,
void *input_arg) {
context->input_wait = handler;
context->input_arg = input_arg;
}
#if NO_SYS == 0
sys_sem_t coap_io_timeout_sem;
#endif /* NO_SYS == 0 */
void
coap_io_lwip_init(void) {
#if NO_SYS == 0
if (sys_sem_new(&coap_io_timeout_sem, 0) != ERR_OK)
coap_log_warn("coap_io_lwip_init: Failed to set up semaphore\n");
#endif /* NO_SYS == 0 */
}
void
coap_io_lwip_cleanup(void) {
#if NO_SYS == 0
sys_sem_free(&coap_io_timeout_sem);
#endif /* NO_SYS == 0 */
}
void
coap_io_process_timeout(void *arg) {
(void)arg;
#if NO_SYS == 0
sys_sem_signal(&coap_io_timeout_sem);
#endif /* NO_SYS == 0 */
}
int
coap_io_process(coap_context_t *ctx, uint32_t timeout_ms) {
int ret;
coap_lock_lock(ctx, return 0);
ret = coap_io_process_lkd(ctx, timeout_ms);
coap_lock_unlock(ctx);
return ret;
}
int
coap_io_process_lkd(coap_context_t *context, uint32_t timeout_ms) {
coap_tick_t before;
coap_tick_t now;
unsigned int num_sockets;
unsigned int timeout;
coap_lock_check_locked(context);
coap_ticks(&before);
timeout = coap_io_prepare_io_lkd(context, NULL, 0, &num_sockets, before);
if (timeout == 0 || (timeout_ms != COAP_IO_WAIT && timeout_ms < timeout))
timeout = timeout_ms;
if (timeout_ms == COAP_IO_NO_WAIT)
timeout = 1;
coap_lock_invert(context,
LOCK_TCPIP_CORE(),
UNLOCK_TCPIP_CORE(); return 0);
if (context->timer_configured) {
sys_untimeout(coap_io_process_timeout, (void *)context);
context->timer_configured = 0;
}
#ifdef COAP_DEBUG_WAKEUP_TIMES
coap_log_info("****** Next wakeup msecs %u (2)\n",
timeout);
#endif /* COAP_DEBUG_WAKEUP_TIMES */
if (timeout) {
sys_timeout(timeout, coap_io_process_timeout, context);
context->timer_configured = 1;
}
UNLOCK_TCPIP_CORE();
if (context->input_wait) {
coap_lock_callback_release(context,
context->input_wait(context->input_arg, timeout),
return 0);
#if NO_SYS == 0
} else {
coap_lock_callback_release(context,
sys_arch_sem_wait(&coap_io_timeout_sem, timeout),
return 0);
#endif /* NO_SYS == 0 */
}
coap_lock_invert(context,
LOCK_TCPIP_CORE(),
UNLOCK_TCPIP_CORE(); return 0);
sys_check_timeouts();
UNLOCK_TCPIP_CORE();
coap_ticks(&now);
return (int)(((now - before) * 1000) / COAP_TICKS_PER_SECOND);
}
/*
* Not used for LwIP (done with coap_recvc()), but need dummy function.
*/
ssize_t
coap_socket_recv(coap_socket_t *sock, coap_packet_t *packet) {
(void)sock;
(void)packet;
assert(0);
return -1;
}
#if COAP_CLIENT_SUPPORT
/** Callback from lwIP when a package was received for a client.
*
* The current implementation deals this to coap_dispatch immediately, but
* other mechanisms (as storing the package in a queue and later fetching it
* when coap_io_do_io is called) can be envisioned.
*
* It handles everything coap_io_do_io does on other implementations.
*/
static void
coap_recvc(void *arg, struct udp_pcb *upcb, struct pbuf *p,
const ip_addr_t *addr, u16_t port) {
coap_pdu_t *pdu = NULL;
coap_session_t *session = (coap_session_t *)arg;
int result = -1;
(void)upcb;
assert(session);
LWIP_ASSERT("Proto not supported for LWIP", COAP_PROTO_NOT_RELIABLE(session->proto));
if (p->len < 4) {
/* Minimum size of CoAP header - ignore runt */
return;
}
memcpy(&session->addr_info.remote.addr, addr, sizeof(session->addr_info.remote.addr));
coap_address_set_port(&session->addr_info.remote, port);
coap_log_debug("* %s: lwip: recv %4d bytes\n",
coap_session_str(session), p->len);
if (session->proto == COAP_PROTO_DTLS) {
if (session->tls) {
result = coap_dtls_receive(session, p->payload, p->len);
if (result < 0)
goto error;
}
pbuf_free(p);
} else {
pdu = coap_pdu_from_pbuf(p);
if (!pdu)
goto error;
if (!coap_pdu_parse(session->proto, p->payload, p->len, pdu)) {
goto error;
}
coap_lock_lock(session->context, return);
coap_dispatch(session->context, session, pdu);
coap_lock_unlock(session->context);
}
#if NO_SYS == 0
sys_sem_signal(&coap_io_timeout_sem);
#endif /* NO_SYS == 0 */
coap_delete_pdu(pdu);
return;
error:
/*
* https://rfc-editor.org/rfc/rfc7252#section-4.2 MUST send RST
* https://rfc-editor.org/rfc/rfc7252#section-4.3 MAY send RST
*/
if (session)
coap_send_rst_lkd(session, pdu);
coap_delete_pdu(pdu);
return;
}
#endif /* ! COAP_CLIENT_SUPPORT */
#if COAP_SERVER_SUPPORT
static void
coap_free_packet(coap_packet_t *packet) {
coap_free_type(COAP_PACKET, packet);
}
/** Callback from lwIP when a UDP packet was received for a server.
*
* The current implementation deals this to coap_dispatch immediately, but
* other mechanisms (as storing the package in a queue and later fetching it
* when coap_io_do_io is called) can be envisioned.
*
* It handles everything coap_io_do_io does on other implementations.
*/
static void
coap_udp_recvs(void *arg, struct udp_pcb *upcb, struct pbuf *p,
const ip_addr_t *addr, u16_t port) {
coap_endpoint_t *ep = (coap_endpoint_t *)arg;
coap_pdu_t *pdu = NULL;
coap_session_t *session = NULL;
coap_tick_t now;
coap_packet_t *packet = NULL;
int result = -1;
if (p->len < 4) {
/* Minimum size of CoAP header - ignore runt */
goto error_free_pbuf;
}
packet = coap_malloc_type(COAP_PACKET, sizeof(coap_packet_t));
/* this is fatal because due to the short life of the packet, never should
there be more than one coap_packet_t required */
LWIP_ASSERT("Insufficient coap_packet_t resources.", packet != NULL);
/* Need to do this as there may be holes in addr_info */
memset(&packet->addr_info, 0, sizeof(packet->addr_info));
packet->length = p->len;
packet->payload = p->payload;
packet->addr_info.remote.port = port;
packet->addr_info.remote.addr = *addr;
packet->addr_info.local.port = upcb->local_port;
packet->addr_info.local.addr = *ip_current_dest_addr();
packet->ifindex = netif_get_index(ip_current_netif());
coap_ticks(&now);
coap_lock_lock(ep->context, goto error_free_pbuf);
session = coap_endpoint_get_session(ep, packet, now);
if (!session)
goto error_free_pbuf;
LWIP_ASSERT("Proto not supported for LWIP", COAP_PROTO_NOT_RELIABLE(session->proto));
coap_log_debug("* %s: lwip: recv %4d bytes\n",
coap_session_str(session), p->len);
if (session->proto == COAP_PROTO_DTLS) {
if (session->type == COAP_SESSION_TYPE_HELLO)
result = coap_dtls_hello(session, p->payload, p->len);
else if (session->tls)
result = coap_dtls_receive(session, p->payload, p->len);
if (session->type == COAP_SESSION_TYPE_HELLO && result == 1)
coap_session_new_dtls_session(session, now);
pbuf_free(p);
} else {
pdu = coap_pdu_from_pbuf(p);
if (!pdu)
goto error;
if (!coap_pdu_parse(ep->proto, p->payload, p->len, pdu)) {
goto error;
}
coap_dispatch(ep->context, session, pdu);
}
coap_delete_pdu(pdu);
coap_free_packet(packet);
coap_lock_unlock(ep->context);
#if NO_SYS == 0
sys_sem_signal(&coap_io_timeout_sem);
#endif /* NO_SYS == 0 */
return;
error_free_pbuf:
pbuf_free(p);
error:
/*
* https://rfc-editor.org/rfc/rfc7252#section-4.2 MUST send RST
* https://rfc-editor.org/rfc/rfc7252#section-4.3 MAY send RST
*/
if (session && pdu)
coap_send_rst_lkd(session, pdu);
coap_delete_pdu(pdu);
coap_free_packet(packet);
coap_lock_unlock(ep->context);
return;
}
#endif /* ! COAP_SERVER_SUPPORT */
ssize_t
coap_socket_send_pdu(coap_socket_t *sock, coap_session_t *session,
coap_pdu_t *pdu) {
/* FIXME: we can't check this here with the existing infrastructure, but we
* should actually check that the pdu is not held by anyone but us. the
* respective pbuf is already exclusively owned by the pdu. */
struct pbuf *pbuf;
int err;
pbuf_realloc(pdu->pbuf, pdu->used_size + coap_pdu_parse_header_size(session->proto,
pdu->pbuf->payload));
if (coap_debug_send_packet()) {
/* Need to take a copy as we may be re-using the origin in a retransmit */
pbuf = pbuf_clone(PBUF_TRANSPORT, PBUF_RAM, pdu->pbuf);
if (pbuf == NULL)
return -1;
err = udp_sendto(sock->udp_pcb, pbuf, &session->addr_info.remote.addr,
session->addr_info.remote.port);
pbuf_free(pbuf);
if (err < 0)
return -1;
}
return pdu->used_size;
}
/*
* dgram
* return +ve Number of bytes written.
* -1 Error error in errno).
*/
ssize_t
coap_socket_send(coap_socket_t *sock, coap_session_t *session,
const uint8_t *data, size_t data_len) {
struct pbuf *pbuf;
int err;
if (coap_debug_send_packet()) {
pbuf = pbuf_alloc(PBUF_TRANSPORT, data_len, PBUF_RAM);
if (pbuf == NULL)
return -1;
memcpy(pbuf->payload, data, data_len);
coap_lock_invert(session->context,
LOCK_TCPIP_CORE(),
UNLOCK_TCPIP_CORE(); return -1);
err = udp_sendto(sock->udp_pcb, pbuf, &session->addr_info.remote.addr,
session->addr_info.remote.port);
UNLOCK_TCPIP_CORE();
pbuf_free(pbuf);
if (err < 0) {
if (err == ERR_RTE) {
coap_log_warn("** %s: udp_send: Packet not routable\n",
coap_session_str(session));
} else {
coap_log_warn("** %s: udp_send: error %d\n",
coap_session_str(session), err);
}
return -1;
}
}
return data_len;
}
#if COAP_SERVER_SUPPORT
int
coap_socket_bind_udp(coap_socket_t *sock,
const coap_address_t *listen_addr,
coap_address_t *bound_addr) {
int err;
coap_address_t l_listen = *listen_addr;
sock->udp_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
if (sock->udp_pcb == NULL)
return 0;
#if LWIP_IPV6 && LWIP_IPV4
if (l_listen.addr.type == IPADDR_TYPE_V6)
l_listen.addr.type = IPADDR_TYPE_ANY;
#endif /* LWIP_IPV6 && LWIP_IPV4 */
udp_recv(sock->udp_pcb, coap_udp_recvs, (void *)sock->endpoint);
err = udp_bind(sock->udp_pcb, &l_listen.addr, l_listen.port);
if (err) {
udp_remove(sock->udp_pcb);
sock->udp_pcb = NULL;
}
*bound_addr = l_listen;
return err ? 0 : 1;
}
#endif /* COAP_SERVER_SUPPORT */
#if COAP_CLIENT_SUPPORT
int
coap_socket_connect_udp(coap_socket_t *sock,
const coap_address_t *local_if,
const coap_address_t *server,
int default_port,
coap_address_t *local_addr,
coap_address_t *remote_addr) {
err_t err;
struct udp_pcb *pcb;
int is_mcast = coap_is_mcast(server);
coap_address_t connect_addr;
coap_address_copy(&connect_addr, server);
if (connect_addr.port == 0)
connect_addr.port = default_port;
coap_lock_invert(sock->session->context,
LOCK_TCPIP_CORE(),
goto err_unlock);
pcb = udp_new();
if (!pcb) {
goto err_unlock;
}
if (local_if) {
pcb->local_ip = local_if->addr;
pcb->local_port = local_if->port;
}
err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
if (err) {
LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
("coap_socket_connect_udp: port bind failed\n"));
goto err_udp_remove;
}
if (local_addr) {
local_addr->addr = pcb->local_ip;
local_addr->port = pcb->local_port;
}
sock->session->addr_info.local.port = pcb->local_port;
if (remote_addr) {
coap_address_copy(remote_addr, &connect_addr);
}
if (is_mcast) {
coap_address_copy(&sock->mcast_addr, &connect_addr);
sock->flags |= COAP_SOCKET_MULTICAST;
} else {
err = udp_connect(pcb, &connect_addr.addr, connect_addr.port);
if (err) {
goto err_udp_unbind;
}
}
#if LWIP_IPV6 && LWIP_IPV4
pcb->local_ip.type = pcb->remote_ip.type;
#endif /* LWIP_IPV6 && LWIP_IPV4 */
sock->udp_pcb = pcb;
udp_recv(sock->udp_pcb, coap_recvc, (void *)sock->session);
UNLOCK_TCPIP_CORE();
return 1;
err_udp_unbind:
err_udp_remove:
udp_remove(pcb);
err_unlock:
UNLOCK_TCPIP_CORE();
return 0;
}
#endif /* ! COAP_CLIENT_SUPPORT */
#if ! COAP_DISABLE_TCP
#include <lwip/tcp.h>
static void
do_tcp_err(void *arg, err_t err) {
coap_session_t *session = (coap_session_t *)arg;
(void)err;
coap_handle_event_lkd(session->context, COAP_EVENT_TCP_FAILED, session);
/*
* as per tcp_err() documentation, the corresponding pcb is already freed
* when this callback is called. So, stop a double free when
* coap_session_disconnected_lkd() eventually coap_socket_close() is called.
*/
session->sock.tcp_pcb = NULL;
coap_session_disconnected_lkd(session, COAP_NACK_NOT_DELIVERABLE);
}
/** Callback from lwIP when a TCP packet is received.
*
* The current implementation invokes coap_read_session() to do the bulk of the
* work.
*/
static err_t
coap_tcp_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) {
coap_session_t *session = (coap_session_t *)arg;
coap_socket_t *sock = &session->sock;
coap_tick_t now;
(void)tpcb;
if (p == NULL) {
/* remote host closed connection */
tcp_arg(sock->tcp_pcb, NULL);
tcp_recv(sock->tcp_pcb, NULL);
tcp_close(sock->tcp_pcb);
sock->tcp_pcb = NULL;
coap_session_disconnected_lkd(session, COAP_NACK_NOT_DELIVERABLE);
return ERR_OK;
} else if (err != ERR_OK) {
/* cleanup, for unknown reason */
if (p != NULL) {
pbuf_free(p);
}
return err;
}
sock->p = p;
coap_ticks(&now);
coap_read_session(session->context, session, now);
return ERR_OK;
}
#if COAP_CLIENT_SUPPORT
static err_t
do_tcp_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
coap_session_t *session = (coap_session_t *)arg;
coap_tick_t now;
if (err)
return err;
session->sock.flags |= COAP_SOCKET_CONNECTED;
session->addr_info.local.addr = tpcb->local_ip;
session->addr_info.local.port = tpcb->local_port;
tcp_recv(tpcb, coap_tcp_recv);
coap_ticks(&now);
coap_connect_session(session, now);
return ERR_OK;
}
int
coap_socket_connect_tcp1(coap_socket_t *sock,
const coap_address_t *local_if,
const coap_address_t *server,
int default_port,
coap_address_t *local_addr,
coap_address_t *remote_addr) {
coap_address_t connect_addr;
err_t err;
(void)local_addr;
(void)remote_addr;
sock->flags &= ~(COAP_SOCKET_WANT_CONNECT | COAP_SOCKET_CONNECTED);
sock->tcp_pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
if (sock->tcp_pcb == NULL)
return 0;
tcp_arg(sock->tcp_pcb, sock->session);
tcp_recv(sock->tcp_pcb, coap_tcp_recv);
tcp_err(sock->tcp_pcb, do_tcp_err);
if (local_if) {
coap_address_t l_local_if = *local_if;
#if LWIP_IPV6 && LWIP_IPV4
if (l_local_if.addr.type == IPADDR_TYPE_V6)
l_local_if.addr.type = IPADDR_TYPE_ANY;
#endif /* LWIP_IPV6 && LWIP_IPV4 */
err = tcp_bind(sock->tcp_pcb, &l_local_if.addr, l_local_if.port);
if (err != ERR_OK) {
tcp_arg(sock->tcp_pcb, NULL);
tcp_recv(sock->tcp_pcb, NULL);
tcp_close(sock->tcp_pcb);
sock->tcp_pcb = NULL;
return 0;
}
}
coap_address_copy(&connect_addr, server);
if (connect_addr.port == 0)
connect_addr.port = htons(default_port);
err = tcp_connect(sock->tcp_pcb, &connect_addr.addr, connect_addr.port,
do_tcp_connected);
if (err == ERR_OK)
sock->flags |= COAP_SOCKET_WANT_CONNECT | COAP_SOCKET_CONNECTED;
return err ? 0 : 1;
}
int
coap_socket_connect_tcp2(coap_socket_t *sock,
coap_address_t *local_addr,
coap_address_t *remote_addr) {
(void)sock;
(void)local_addr;
(void)remote_addr;
sock->flags &= ~(COAP_SOCKET_WANT_CONNECT | COAP_SOCKET_CAN_CONNECT);
return 1;
}
#endif /* COAP_CLIENT_SUPPORT */
#if COAP_SERVER_SUPPORT
static err_t
do_tcp_accept(void *arg, struct tcp_pcb *newpcb, err_t err) {
coap_endpoint_t *endpoint = arg;
coap_session_t *session;
coap_tick_t now;
err_t ret_err = ERR_OK;
if ((err != ERR_OK) || (newpcb == NULL)) {
return ERR_VAL;
}
coap_ticks(&now);
session = coap_new_server_session(endpoint->context, endpoint, newpcb);
if (session) {
session->sock.tcp_pcb = newpcb;
session->last_rx_tx = now;
tcp_arg(newpcb, session);
tcp_setprio(newpcb, TCP_PRIO_MIN);
tcp_recv(newpcb, coap_tcp_recv);
tcp_err(newpcb, do_tcp_err);
} else {
ret_err = ERR_MEM;
}
return ret_err;
}
int
coap_socket_bind_tcp(coap_socket_t *sock,
const coap_address_t *listen_addr,
coap_address_t *bound_addr) {
int err;
coap_address_t l_listen = *listen_addr;
struct tcp_pcb *tcp_pcb;
sock->tcp_pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
if (sock->tcp_pcb == NULL)
return 0;
#if LWIP_IPV6 && LWIP_IPV4
if (l_listen.addr.type == IPADDR_TYPE_V6)
l_listen.addr.type = IPADDR_TYPE_ANY;
#endif /* LWIP_IPV6 && LWIP_IPV4 */
tcp_arg(sock->tcp_pcb, sock->endpoint);
err = tcp_bind(sock->tcp_pcb, &l_listen.addr, l_listen.port);
if (err != ERR_OK) {
tcp_arg(sock->tcp_pcb, NULL);
tcp_recv(sock->tcp_pcb, NULL);
tcp_close(sock->tcp_pcb);
sock->tcp_pcb = NULL;
return 0;
} else {
tcp_pcb = tcp_listen(sock->tcp_pcb);
if (tcp_pcb) {
sock->tcp_pcb = tcp_pcb;
tcp_accept(sock->tcp_pcb, do_tcp_accept);
} else {
tcp_arg(sock->tcp_pcb, NULL);
tcp_recv(sock->tcp_pcb, NULL);
tcp_close(sock->tcp_pcb);
sock->tcp_pcb = NULL;
return 0;
}
}
*bound_addr = l_listen;
return err ? 0 : 1;
}
int
coap_socket_accept_tcp(coap_socket_t *server,
coap_socket_t *new_client,
coap_address_t *local_addr,
coap_address_t *remote_addr,
void *extra) {
struct tcp_pcb *tcp_pcb = (struct tcp_pcb *)extra;
(void)server;
new_client->tcp_pcb = tcp_pcb;
local_addr->addr = tcp_pcb->local_ip;
local_addr->port = tcp_pcb->local_port;
remote_addr->addr = tcp_pcb->remote_ip;
remote_addr->port = tcp_pcb->remote_port;
return 1;
}
#endif /* COAP_SERVER_SUPPORT */
/*
* strm
* return +ve Number of bytes written.
* -1 Error error in errno).
*/
ssize_t
coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len) {
struct pbuf *pbuf;
int err;
pbuf = pbuf_alloc(PBUF_TRANSPORT, data_len, PBUF_RAM);
if (pbuf == NULL)
return -1;
memcpy(pbuf->payload, data, data_len);
coap_lock_invert(context,
LOCK_TCPIP_CORE(),
UNLOCK_TCPIP_CORE(); return 0);
err = tcp_write(sock->tcp_pcb, pbuf->payload, pbuf->len, 1);
UNLOCK_TCPIP_CORE();
pbuf_free(pbuf);
if (err < 0)
return -1;
return data_len;
}
/*
* strm
* return >=0 Number of bytes read.
* -1 Error error in errno).
*/
ssize_t
coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len) {
if (sock->p) {
if (data_len < sock->p->len) {
uint8_t *ptr = (uint8_t *)sock->p->payload;
/* Handle partial read of data request */
memcpy(data, sock->p->payload, data_len);
sock->p->payload = &ptr[data_len];
sock->p->len -= data_len;
return data_len;
} else {
data_len = sock->p->len;
memcpy(data, sock->p->payload, sock->p->len);
pbuf_free(sock->p);
sock->p = NULL;
return data_len;
}
}
return 0;
}
#endif /* !COAP_DISABLE_TCP */
void
coap_socket_close(coap_socket_t *sock) {
if (sock->udp_pcb) {
if (sock->session) {
coap_lock_invert(sock->session->context,
LOCK_TCPIP_CORE(),
UNLOCK_TCPIP_CORE(); return);
} else {
LOCK_TCPIP_CORE();
}
udp_remove(sock->udp_pcb);
UNLOCK_TCPIP_CORE();
sock->udp_pcb = NULL;
}
#if ! COAP_DISABLE_TCP
if (sock->tcp_pcb) {
tcp_arg(sock->tcp_pcb, NULL);
#if COAP_SERVER_SUPPORT
if (!sock->endpoint)
#endif /* COAP_SERVER_SUPPORT */
tcp_recv(sock->tcp_pcb, NULL);
if (sock->session) {
coap_lock_invert(sock->session->context,
LOCK_TCPIP_CORE(),
UNLOCK_TCPIP_CORE(); return);
} else {
LOCK_TCPIP_CORE();
}
tcp_close(sock->tcp_pcb);
UNLOCK_TCPIP_CORE();
sock->tcp_pcb = NULL;
}
#endif /* !COAP_DISABLE_TCP */
return;
}
int
coap_is_mcast(const coap_address_t *a) {
if (!a)
return 0;
/* Treat broadcast in same way as multicast */
if (coap_is_bcast(a))
return 1;
return ip_addr_ismulticast(&(a)->addr);
}
#ifndef COAP_BCST_CNT
#define COAP_BCST_CNT 15
#endif /* COAP_BCST_CNT */
/* How frequently to refresh the list of valid IPv4 broadcast addresses */
#ifndef COAP_BCST_REFRESH_SECS
#define COAP_BCST_REFRESH_SECS 30
#endif /* COAP_BCST_REFRESH_SECS */
#if COAP_IPV4_SUPPORT
static int bcst_cnt = -1;
static coap_tick_t last_refresh;
static uint32_t b_ipv4[COAP_BCST_CNT];
#endif /* COAP_IPV4_SUPPORT */
int
coap_is_bcast(const coap_address_t *a) {
int i;
coap_tick_t now;
#if COAP_IPV4_SUPPORT
const ip4_addr_t *ipv4;
#endif /* COAP_IPV4_SUPPORT */
if (!a)
return 0;
if (IP_IS_V6(&(a)->addr))
return 0;
#if COAP_IPV4_SUPPORT
#ifndef INADDR_BROADCAST
#define INADDR_BROADCAST ((uint32_t)0xffffffffUL)
#endif /* !INADDR_BROADCAST */
ipv4 = ip_2_ip4(&(a)->addr);
if (ipv4->addr == INADDR_BROADCAST)
return 1;
coap_ticks(&now);
if (bcst_cnt == -1 ||
(now - last_refresh) > (COAP_BCST_REFRESH_SECS * COAP_TICKS_PER_SECOND)) {
/* Determine the list of broadcast interfaces */
struct netif *netif;
bcst_cnt = 0;
last_refresh = now;
LWIP_ASSERT_CORE_LOCKED();
NETIF_FOREACH(netif) {
if (bcst_cnt < COAP_BCST_CNT) {
const ip4_addr_t *ip_addr;
const ip4_addr_t *netmask;
ip_addr = ip_2_ip4(&netif->ip_addr);
netmask = ip_2_ip4(&netif->netmask);
if (netmask->addr != 0xffffffff) {
b_ipv4[bcst_cnt] = ip_addr->addr | ~(netmask->addr);
bcst_cnt++;
}
}
}
if (bcst_cnt == COAP_BCST_CNT) {
coap_log_warn("coap_is_bcst: Insufficient space for broadcast addresses\n");
}
}
for (i = 0; i < bcst_cnt; i++) {
if (ipv4->addr == b_ipv4[i])
return 1;
}
#endif /* COAP_IPV4_SUPPORT */
return 0;
}
/**
* Checks if given address @p a denotes a AF_UNIX address. This function
* returns @c 1 if @p a is of type AF_UNIX, @c 0 otherwise.
*/
int
coap_is_af_unix(const coap_address_t *a) {
(void)a;
return 0;
}