1
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
66
67
68
69
70
71
72
73
74
75
76
85
86
87
88
89
90
91
92
93
94
95
96
107
108
109
110
111
112
113
114
115
129
130
131
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
177
178
179
180
181
182
186
187
188
189
190
191
192
193
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
237
238
239
240
241
242
243
244
245
250
251
252
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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
309
310
311
319
322
323
330
331
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
374
375
376
377
386
387
388
389
390
397
398
399
400
401
402
403
404
405
420
421
422
423
424
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
461
462
463
464
465
466
470
471
475
476
477
478
480
481
482
483
484
485
486
487
494
495
496
497
498
499
502
505
506
507
513
514
515
516
517
518
519
520
521
522
523
524
525
526
532
533
534
535
536
537
538
541
545
546
554
555
556
557
558
559
561
562
570
571
572
573
574
575
576
577
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
597
598
599
600
601
602
608
609
610
611
612
613
614
615
616
621
622
623
668
671
672
673
679
680
681
682
683
684
685
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
715
716
717
718
719
720
721
722
723
728
729
730
731
732
733
734
735
736
740
743
744
745
748
749
754
755
756
757
758
759
760
762
763
764
765
766
767
768
769
770
771
772
773
774
776
777
778
779
780
781
782
783
784
785
786
787
793
794
795
796
797
798
799
801
802
803
804
805
806
808
809
810
811
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
833
834
835
836
837
838
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
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
942
954
955
956
959
975
976
1011
1021
1022
1027
1028
1029
1030
1034
1035
1040
1055
1056
1057
1058
1059
1060
1061
1062
1077
1078
1079
1080
1084
1085
1086
1087
1088
1089
1090
1093
1094
1095
1096
1097
1098
1099
1101
1102
1103
1104
1105
1106
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1142
1143
1144
1145
1146
1147
1148
1149
1150
1152
1153
1154
1155
1156
1157
1158
1159
1160
1165
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1189
1190
1191
1193
1194
1195
1196
1197
1198
1200
1201
1202
1203
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1229
1234
1238
1241
1242
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1273
1278
1282
1285
1286
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1314
1315
1316
1317
1318
1319
1323
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1359
1360
1361
1366
1371
1372
1373
1378
1379
1380
1381
1382
1383
1384
1385
1388
1389
1391
1392
1397
1398
1399
1400
1401
1402
1403
1404
1409
1410
1411
1412
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1462
1463
1464
1465
1466
1467
1468
1469
1479
1480
1481
1482
1485
1486
1491
1492
1495
1505
1506
1507
1514
1517
1520
1521
1522
1523
1524
1525
1531
1532
1533
1534
1535
1536
1537
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1674
1675
1676
1677
1678
1679
1685
1686
1699
1700
1701
1705
1712
1713
1726
1727
1728
1742
1743
1758
1762
1763
1764
1765
1777
1782
1783
1784
1786
1787
1788
1789
1790
1791
1792
1793
1796
1797
1798
1802
1803
1804
1805
1806
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1843
1844
1847
1848
1849
1850
1851
1855
1856
1857
1858
1859
1860
1866
1867
1868
1869
1870
1871
1872
1879
1880
1881
1882
1883
1893
1906
1916
1919
1920
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1942
1943
1944
1945
1946
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1967
1968
1972
1973
1974
1975
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2051
2052
2053
2059
2060
2061
2062
2063
2064
2065
2066
2070
2071
2072
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2132
2133
2134
2135
2136
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2164
2170
2171
2172
2173
/* ... */
/* ... */
#include "lwip/opt.h"
#if LWIP_NETCONN
#include "lwip/priv/api_msg.h"
#include "lwip/ip.h"
#include "lwip/ip_addr.h"
#include "lwip/udp.h"
#include "lwip/tcp.h"
#include "lwip/raw.h"
#include "lwip/memp.h"
#include "lwip/igmp.h"
#include "lwip/dns.h"
#include "lwip/mld6.h"
#include "lwip/priv/tcpip_priv.h"
#include <string.h>
12 includes
#define NETCONN_TCP_POLL_INTERVAL 2
#define SET_NONBLOCKING_CONNECT(conn, val) do { if (val) { \
netconn_set_flags(conn, NETCONN_FLAG_IN_NONBLOCKING_CONNECT); \
}if (val) { ... } else { \
netconn_clear_flags(conn, NETCONN_FLAG_IN_NONBLOCKING_CONNECT); }else { ... }}... while(0)...
#define IN_NONBLOCKING_CONNECT(conn) netconn_is_flag_set(conn, NETCONN_FLAG_IN_NONBLOCKING_CONNECT)
#if LWIP_NETCONN_FULLDUPLEX
#define NETCONN_MBOX_VALID(conn, mbox) (sys_mbox_valid(mbox) && ((conn->flags & NETCONN_FLAG_MBOXINVALID) == 0))
#else
#define NETCONN_MBOX_VALID(conn, mbox) sys_mbox_valid(mbox)
#endif
#if LWIP_TCP
#if LWIP_TCPIP_CORE_LOCKING
#define WRITE_DELAYED , 1
#define WRITE_DELAYED_PARAM , u8_t delayed
/* ... */#else
#define WRITE_DELAYED
#define WRITE_DELAYED_PARAM
/* ... */#endif
static err_t lwip_netconn_do_writemore(struct netconn *conn WRITE_DELAYED_PARAM);
static err_t lwip_netconn_do_close_internal(struct netconn *conn WRITE_DELAYED_PARAM);/* ... */
#endif
static void netconn_drain(struct netconn *conn);
#if LWIP_TCPIP_CORE_LOCKING
#define TCPIP_APIMSG_ACK(m)
#else
#define TCPIP_APIMSG_ACK(m) do { sys_sem_signal(LWIP_API_MSG_SEM(m)); } while(0)
#endif
#if LWIP_NETCONN_FULLDUPLEX
const u8_t netconn_deleted = 0;
int
lwip_netconn_is_deallocated_msg(void *msg)
{
if (msg == &netconn_deleted) {
return 1;
}if (msg == &netconn_deleted) { ... }
return 0;
}lwip_netconn_is_deallocated_msg (void *msg) { ... }
/* ... */#endif
#if LWIP_TCP
const u8_t netconn_aborted = 0;
const u8_t netconn_reset = 0;
const u8_t netconn_closed = 0;
static void *
lwip_netconn_err_to_msg(err_t err)
{
switch (err) {
case ERR_ABRT:
return LWIP_CONST_CAST(void *, &netconn_aborted);case ERR_ABRT:
case ERR_RST:
return LWIP_CONST_CAST(void *, &netconn_reset);case ERR_RST:
case ERR_CLSD:
return LWIP_CONST_CAST(void *, &netconn_closed);case ERR_CLSD:
default:
LWIP_ASSERT("unhandled error", err == ERR_OK);
return NULL;default
}switch (err) { ... }
}{ ... }
int
lwip_netconn_is_err_msg(void *msg, err_t *err)
{
LWIP_ASSERT("err != NULL", err != NULL);
if (msg == &netconn_aborted) {
*err = ERR_ABRT;
return 1;
}if (msg == &netconn_aborted) { ... } else if (msg == &netconn_reset) {
*err = ERR_RST;
return 1;
}else if (msg == &netconn_reset) { ... } else if (msg == &netconn_closed) {
*err = ERR_CLSD;
return 1;
}else if (msg == &netconn_closed) { ... }
return 0;
}{ ... }
#endif/* ... */
#if LWIP_RAW
/* ... */
static u8_t
recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
const ip_addr_t *addr)
{
struct pbuf *q;
struct netbuf *buf;
struct netconn *conn;
LWIP_UNUSED_ARG(addr);
conn = (struct netconn *)arg;
if ((conn != NULL) && NETCONN_MBOX_VALID(conn, &conn->recvmbox)) {
#if LWIP_SO_RCVBUF
int recv_avail;
SYS_ARCH_GET(conn->recv_avail, recv_avail);
if ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize) {
return 0;
}if ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize) { ... }
/* ... */#endif
q = pbuf_clone(PBUF_RAW, PBUF_RAM, p);
if (q != NULL) {
u16_t len;
buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
if (buf == NULL) {
pbuf_free(q);
return 0;
}if (buf == NULL) { ... }
buf->p = q;
buf->ptr = q;
ip_addr_copy(buf->addr, *ip_current_src_addr());
buf->port = pcb->protocol;
len = q->tot_len;
if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
netbuf_delete(buf);
return 0;
}if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) { ... } else {
#if LWIP_SO_RCVBUF
SYS_ARCH_INC(conn->recv_avail, len);
#endif
API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
}else { ... }
}if (q != NULL) { ... }
}if ((conn != NULL) && NETCONN_MBOX_VALID(conn, &conn->recvmbox)) { ... }
return 0;
}recv_raw (void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr) { ... }
/* ... */#endif
#if LWIP_UDP
/* ... */
static void
recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
const ip_addr_t *addr, u16_t port)
{
struct netbuf *buf;
struct netconn *conn;
u16_t len;
#if LWIP_SO_RCVBUF
int recv_avail;
#endif
LWIP_UNUSED_ARG(pcb);
LWIP_ASSERT("recv_udp must have a pcb argument", pcb != NULL);
LWIP_ASSERT("recv_udp must have an argument", arg != NULL);
conn = (struct netconn *)arg;
if (conn == NULL) {
pbuf_free(p);
return;
}if (conn == NULL) { ... }
LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn->pcb.udp == pcb);
#if LWIP_SO_RCVBUF
SYS_ARCH_GET(conn->recv_avail, recv_avail);
if (!NETCONN_MBOX_VALID(conn, &conn->recvmbox) ||
((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize)) {
#else
if (!NETCONN_MBOX_VALID(conn, &conn->recvmbox)) {
#endif
pbuf_free(p);
return;
}if (!NETCONN_MBOX_VALID(conn, &conn->recvmbox)) { ... }
buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
if (buf == NULL) {
pbuf_free(p);
return;
}if (buf == NULL) { ... } else {
buf->p = p;
buf->ptr = p;
ip_addr_set(&buf->addr, addr);
buf->port = port;
#if LWIP_NETBUF_RECVINFO
if (conn->flags & NETCONN_FLAG_PKTINFO) {
const struct udp_hdr *udphdr = (const struct udp_hdr *)ip_next_header_ptr();
buf->flags = NETBUF_FLAG_DESTADDR;
ip_addr_set(&buf->toaddr, ip_current_dest_addr());
buf->toport_chksum = udphdr->dest;
}if (conn->flags & NETCONN_FLAG_PKTINFO) { ... }
/* ... */#endif
}else { ... }
len = p->tot_len;
if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
netbuf_delete(buf);
return;
}if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) { ... } else {
#if LWIP_SO_RCVBUF
SYS_ARCH_INC(conn->recv_avail, len);
#endif
API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
}else { ... }
}{ ... }
#endif/* ... */
#if LWIP_TCP
/* ... */
static err_t
recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
struct netconn *conn;
u16_t len;
void *msg;
LWIP_UNUSED_ARG(pcb);
LWIP_ASSERT("recv_tcp must have a pcb argument", pcb != NULL);
LWIP_ASSERT("recv_tcp must have an argument", arg != NULL);
LWIP_ASSERT("err != ERR_OK unhandled", err == ERR_OK);
LWIP_UNUSED_ARG(err);
conn = (struct netconn *)arg;
if (conn == NULL) {
return ERR_VAL;
}if (conn == NULL) { ... }
LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn->pcb.tcp == pcb);
if (!NETCONN_MBOX_VALID(conn, &conn->recvmbox)) {
if (p != NULL) {
tcp_recved(pcb, p->tot_len);
pbuf_free(p);
}if (p != NULL) { ... }
return ERR_OK;
}if (!NETCONN_MBOX_VALID(conn, &conn->recvmbox)) { ... }
/* ... */
if (p != NULL) {
msg = p;
len = p->tot_len;
}if (p != NULL) { ... } else {
msg = LWIP_CONST_CAST(void *, &netconn_closed);
len = 0;
}else { ... }
if (sys_mbox_trypost(&conn->recvmbox, msg) != ERR_OK) {
return ERR_MEM;
}if (sys_mbox_trypost(&conn->recvmbox, msg) != ERR_OK) { ... } else {
#if LWIP_SO_RCVBUF
SYS_ARCH_INC(conn->recv_avail, len);
#endif
API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
}else { ... }
return ERR_OK;
}{ ... }
/* ... */
static err_t
poll_tcp(void *arg, struct tcp_pcb *pcb)
{
struct netconn *conn = (struct netconn *)arg;
LWIP_UNUSED_ARG(pcb);
LWIP_ASSERT("conn != NULL", (conn != NULL));
if (conn->state == NETCONN_WRITE) {
lwip_netconn_do_writemore(conn WRITE_DELAYED);
}if (conn->state == NETCONN_WRITE) { ... } else if (conn->state == NETCONN_CLOSE) {
#if !LWIP_SO_SNDTIMEO && !LWIP_SO_LINGER
if (conn->current_msg && conn->current_msg->msg.sd.polls_left) {
conn->current_msg->msg.sd.polls_left--;
}if (conn->current_msg && conn->current_msg->msg.sd.polls_left) { ... }
/* ... */#endif
lwip_netconn_do_close_internal(conn WRITE_DELAYED);
}else if (conn->state == NETCONN_CLOSE) { ... }
if (conn->flags & NETCONN_FLAG_CHECK_WRITESPACE) {
/* ... */
if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) &&
(tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) {
netconn_clear_flags(conn, NETCONN_FLAG_CHECK_WRITESPACE);
API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
}if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) && (tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) { ... }
}if (conn->flags & NETCONN_FLAG_CHECK_WRITESPACE) { ... }
return ERR_OK;
}{ ... }
/* ... */
static err_t
sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len)
{
struct netconn *conn = (struct netconn *)arg;
LWIP_UNUSED_ARG(pcb);
LWIP_ASSERT("conn != NULL", (conn != NULL));
if (conn) {
if (conn->state == NETCONN_WRITE) {
lwip_netconn_do_writemore(conn WRITE_DELAYED);
}if (conn->state == NETCONN_WRITE) { ... } else if (conn->state == NETCONN_CLOSE) {
lwip_netconn_do_close_internal(conn WRITE_DELAYED);
}else if (conn->state == NETCONN_CLOSE) { ... }
/* ... */
if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) &&
(tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) {
netconn_clear_flags(conn, NETCONN_FLAG_CHECK_WRITESPACE);
API_EVENT(conn, NETCONN_EVT_SENDPLUS, len);
}if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) && (tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) { ... }
}if (conn) { ... }
return ERR_OK;
}{ ... }
/* ... */
static void
err_tcp(void *arg, err_t err)
{
struct netconn *conn;
enum netconn_state old_state;
void *mbox_msg;
SYS_ARCH_DECL_PROTECT(lev);
conn = (struct netconn *)arg;
LWIP_ASSERT("conn != NULL", (conn != NULL));
SYS_ARCH_PROTECT(lev);
conn->pcb.tcp = NULL;
conn->pending_err = err;
conn->flags |= NETCONN_FLAG_MBOXCLOSED;
old_state = conn->state;
conn->state = NETCONN_NONE;
SYS_ARCH_UNPROTECT(lev);
API_EVENT(conn, NETCONN_EVT_ERROR, 0);
/* ... */
API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
mbox_msg = lwip_netconn_err_to_msg(err);
if (NETCONN_MBOX_VALID(conn, &conn->recvmbox)) {
sys_mbox_trypost(&conn->recvmbox, mbox_msg);
}if (NETCONN_MBOX_VALID(conn, &conn->recvmbox)) { ... }
if (NETCONN_MBOX_VALID(conn, &conn->acceptmbox)) {
sys_mbox_trypost(&conn->acceptmbox, mbox_msg);
}if (NETCONN_MBOX_VALID(conn, &conn->acceptmbox)) { ... }
if ((old_state == NETCONN_WRITE) || (old_state == NETCONN_CLOSE) ||
(old_state == NETCONN_CONNECT)) {
/* ... */
int was_nonblocking_connect = IN_NONBLOCKING_CONNECT(conn);
SET_NONBLOCKING_CONNECT(conn, 0);
if (!was_nonblocking_connect) {
sys_sem_t *op_completed_sem;
LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
if (old_state == NETCONN_CLOSE) {
conn->current_msg->err = ERR_OK;
}if (old_state == NETCONN_CLOSE) { ... } else {
conn->current_msg->err = err;
}else { ... }
op_completed_sem = LWIP_API_MSG_SEM(conn->current_msg);
LWIP_ASSERT("inavlid op_completed_sem", sys_sem_valid(op_completed_sem));
conn->current_msg = NULL;
sys_sem_signal(op_completed_sem);
}if (!was_nonblocking_connect) { ... } else {
}else { ... }
}if ((old_state == NETCONN_WRITE) || (old_state == NETCONN_CLOSE) || (old_state == NETCONN_CONNECT)) { ... } else {
LWIP_ASSERT("conn->current_msg == NULL", conn->current_msg == NULL);
}else { ... }
}{ ... }
/* ... */
static void
setup_tcp(struct netconn *conn)
{
struct tcp_pcb *pcb;
pcb = conn->pcb.tcp;
tcp_arg(pcb, conn);
tcp_recv(pcb, recv_tcp);
tcp_sent(pcb, sent_tcp);
tcp_poll(pcb, poll_tcp, NETCONN_TCP_POLL_INTERVAL);
tcp_err(pcb, err_tcp);
}{ ... }
/* ... */
static err_t
accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
{
struct netconn *newconn;
struct netconn *conn = (struct netconn *)arg;
if (conn == NULL) {
return ERR_VAL;
}if (conn == NULL) { ... }
if (!NETCONN_MBOX_VALID(conn, &conn->acceptmbox)) {
LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: acceptmbox already deleted\n"));
return ERR_VAL;
}if (!NETCONN_MBOX_VALID(conn, &conn->acceptmbox)) { ... }
if (newpcb == NULL) {
if (sys_mbox_trypost(&conn->acceptmbox, lwip_netconn_err_to_msg(ERR_ABRT)) == ERR_OK) {
API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
}if (sys_mbox_trypost(&conn->acceptmbox, lwip_netconn_err_to_msg(ERR_ABRT)) == ERR_OK) { ... }
return ERR_VAL;
}if (newpcb == NULL) { ... }
LWIP_ASSERT("expect newpcb == NULL or err == ERR_OK", err == ERR_OK);
LWIP_UNUSED_ARG(err);
LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: newpcb->state: %s\n", tcp_debug_state_str(newpcb->state)));
/* ... */
newconn = netconn_alloc(conn->type, conn->callback);
if (newconn == NULL) {
if (sys_mbox_trypost(&conn->acceptmbox, lwip_netconn_err_to_msg(ERR_ABRT)) == ERR_OK) {
API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
}if (sys_mbox_trypost(&conn->acceptmbox, lwip_netconn_err_to_msg(ERR_ABRT)) == ERR_OK) { ... }
return ERR_MEM;
}if (newconn == NULL) { ... }
newconn->pcb.tcp = newpcb;
setup_tcp(newconn);
tcp_backlog_delayed(newpcb);
if (sys_mbox_trypost(&conn->acceptmbox, newconn) != ERR_OK) {
/* ... */
struct tcp_pcb *pcb = newconn->pcb.tcp;
tcp_arg(pcb, NULL);
tcp_recv(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_poll(pcb, NULL, 0);
tcp_err(pcb, NULL);
newconn->pcb.tcp = NULL;
sys_mbox_free(&newconn->recvmbox);
sys_mbox_set_invalid(&newconn->recvmbox);
netconn_free(newconn);
return ERR_MEM;
}if (sys_mbox_trypost(&conn->acceptmbox, newconn) != ERR_OK) { ... } else {
API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
}else { ... }
return ERR_OK;
}{ ... }
#endif/* ... */
/* ... */
static void
pcb_new(struct api_msg *msg)
{
enum lwip_ip_addr_type iptype = IPADDR_TYPE_V4;
LWIP_ASSERT("pcb_new: pcb already allocated", msg->conn->pcb.tcp == NULL);
#if LWIP_IPV6 && LWIP_IPV4
if (NETCONNTYPE_ISIPV6(netconn_type(msg->conn))) {
iptype = IPADDR_TYPE_ANY;
}if (NETCONNTYPE_ISIPV6(netconn_type(msg->conn))) { ... }
/* ... */#endif
switch (NETCONNTYPE_GROUP(msg->conn->type)) {
#if LWIP_RAW
case NETCONN_RAW:
msg->conn->pcb.raw = raw_new_ip_type(iptype, msg->msg.n.proto);
if (msg->conn->pcb.raw != NULL) {
#if LWIP_IPV6
if (NETCONNTYPE_ISIPV6(msg->conn->type) && msg->conn->pcb.raw->protocol == IP6_NEXTH_ICMP6) {
msg->conn->pcb.raw->chksum_reqd = 1;
msg->conn->pcb.raw->chksum_offset = 2;
}if (NETCONNTYPE_ISIPV6(msg->conn->type) && msg->conn->pcb.raw->protocol == IP6_NEXTH_ICMP6) { ... }
/* ... */#endif
raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
}if (msg->conn->pcb.raw != NULL) { ... }
break;/* ... */
#endif
#if LWIP_UDPcase NETCONN_RAW:
case NETCONN_UDP:
msg->conn->pcb.udp = udp_new_ip_type(iptype);
if (msg->conn->pcb.udp != NULL) {
#if LWIP_UDPLITE
if (NETCONNTYPE_ISUDPLITE(msg->conn->type)) {
udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
}if (NETCONNTYPE_ISUDPLITE(msg->conn->type)) { ... }
/* ... */#endif
if (NETCONNTYPE_ISUDPNOCHKSUM(msg->conn->type)) {
udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
}if (NETCONNTYPE_ISUDPNOCHKSUM(msg->conn->type)) { ... }
udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
}if (msg->conn->pcb.udp != NULL) { ... }
break;/* ... */
#endif
#if LWIP_TCPcase NETCONN_UDP:
case NETCONN_TCP:
msg->conn->pcb.tcp = tcp_new_ip_type(iptype);
if (msg->conn->pcb.tcp != NULL) {
setup_tcp(msg->conn);
}if (msg->conn->pcb.tcp != NULL) { ... }
break;/* ... */
#endif case NETCONN_TCP:
default:
msg->err = ERR_VAL;
return;default
}switch (NETCONNTYPE_GROUP(msg->conn->type)) { ... }
if (msg->conn->pcb.ip == NULL) {
msg->err = ERR_MEM;
}if (msg->conn->pcb.ip == NULL) { ... }
}{ ... }
/* ... */
void
lwip_netconn_do_newconn(void *m)
{
struct api_msg *msg = (struct api_msg *)m;
msg->err = ERR_OK;
if (msg->conn->pcb.tcp == NULL) {
pcb_new(msg);
}if (msg->conn->pcb.tcp == NULL) { ... }
TCPIP_APIMSG_ACK(msg);
}{ ... }
/* ... */
struct netconn *
netconn_alloc(enum netconn_type t, netconn_callback callback)
{
struct netconn *conn;
int size;
u8_t init_flags = 0;
conn = (struct netconn *)memp_malloc(MEMP_NETCONN);
if (conn == NULL) {
return NULL;
}if (conn == NULL) { ... }
conn->pending_err = ERR_OK;
conn->type = t;
conn->pcb.tcp = NULL;
switch (NETCONNTYPE_GROUP(t)) {
#if LWIP_RAW
case NETCONN_RAW:
size = DEFAULT_RAW_RECVMBOX_SIZE;
break;/* ... */
#endif
#if LWIP_UDPcase NETCONN_RAW:
case NETCONN_UDP:
size = DEFAULT_UDP_RECVMBOX_SIZE;
#if LWIP_NETBUF_RECVINFO
init_flags |= NETCONN_FLAG_PKTINFO;
#endif
break;/* ... */
#endif
#if LWIP_TCPcase NETCONN_UDP:
case NETCONN_TCP:
size = DEFAULT_TCP_RECVMBOX_SIZE;
break;/* ... */
#endif case NETCONN_TCP:
default:
LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
goto free_and_return;default
}switch (NETCONNTYPE_GROUP(t)) { ... }
if (sys_mbox_new(&conn->recvmbox, size) != ERR_OK) {
goto free_and_return;
}if (sys_mbox_new(&conn->recvmbox, size) != ERR_OK) { ... }
#if !LWIP_NETCONN_SEM_PER_THREAD
if (sys_sem_new(&conn->op_completed, 0) != ERR_OK) {
sys_mbox_free(&conn->recvmbox);
goto free_and_return;
}if (sys_sem_new(&conn->op_completed, 0) != ERR_OK) { ... }
/* ... */#endif
#if LWIP_TCP
sys_mbox_set_invalid(&conn->acceptmbox);
#endif
conn->state = NETCONN_NONE;
#if LWIP_SOCKET
conn->socket = -1;/* ... */
#endif
conn->callback = callback;
#if LWIP_TCP
conn->current_msg = NULL;
#endif
#if LWIP_SO_SNDTIMEO
conn->send_timeout = 0;
#endif
#if LWIP_SO_RCVTIMEO
conn->recv_timeout = 0;
#endif
#if LWIP_SO_RCVBUF
conn->recv_bufsize = RECV_BUFSIZE_DEFAULT;
conn->recv_avail = 0;/* ... */
#endif
#if LWIP_SO_LINGER
conn->linger = -1;
#endif
conn->flags = init_flags;
return conn;
free_and_return:
memp_free(MEMP_NETCONN, conn);
return NULL;
}{ ... }
/* ... */
void
netconn_free(struct netconn *conn)
{
LWIP_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL);
#if LWIP_NETCONN_FULLDUPLEX
netconn_drain(conn);/* ... */
#endif
LWIP_ASSERT("recvmbox must be deallocated before calling this function",
!sys_mbox_valid(&conn->recvmbox));
#if LWIP_TCP
LWIP_ASSERT("acceptmbox must be deallocated before calling this function",
!sys_mbox_valid(&conn->acceptmbox));/* ... */
#endif
#if !LWIP_NETCONN_SEM_PER_THREAD
sys_sem_free(&conn->op_completed);
sys_sem_set_invalid(&conn->op_completed);/* ... */
#endif
memp_free(MEMP_NETCONN, conn);
}{ ... }
/* ... */
static void
netconn_drain(struct netconn *conn)
{
void *mem;
/* ... */
#if LWIP_NETCONN_FULLDUPLEX
LWIP_ASSERT("netconn marked closed", conn->flags & NETCONN_FLAG_MBOXINVALID);
#endif
if (sys_mbox_valid(&conn->recvmbox)) {
while (sys_mbox_tryfetch(&conn->recvmbox, &mem) != SYS_MBOX_EMPTY) {
#if LWIP_NETCONN_FULLDUPLEX
if (!lwip_netconn_is_deallocated_msg(mem))
#endif
{
#if LWIP_TCP
if (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP) {
err_t err;
if (!lwip_netconn_is_err_msg(mem, &err)) {
pbuf_free((struct pbuf *)mem);
}if (!lwip_netconn_is_err_msg(mem, &err)) { ... }
}if (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP) { ... } else
#endif
{
netbuf_delete((struct netbuf *)mem);
}else { ... }
...}
}while (sys_mbox_tryfetch(&conn->recvmbox, &mem) != SYS_MBOX_EMPTY) { ... }
sys_mbox_free(&conn->recvmbox);
sys_mbox_set_invalid(&conn->recvmbox);
}if (sys_mbox_valid(&conn->recvmbox)) { ... }
#if LWIP_TCP
if (sys_mbox_valid(&conn->acceptmbox)) {
while (sys_mbox_tryfetch(&conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) {
#if LWIP_NETCONN_FULLDUPLEX
if (!lwip_netconn_is_deallocated_msg(mem))
#endif
{
err_t err;
if (!lwip_netconn_is_err_msg(mem, &err)) {
struct netconn *newconn = (struct netconn *)mem;
netconn_drain(newconn);
if (newconn->pcb.tcp != NULL) {
tcp_abort(newconn->pcb.tcp);
newconn->pcb.tcp = NULL;
}if (newconn->pcb.tcp != NULL) { ... }
netconn_free(newconn);
}if (!lwip_netconn_is_err_msg(mem, &err)) { ... }
...}
}while (sys_mbox_tryfetch(&conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) { ... }
sys_mbox_free(&conn->acceptmbox);
sys_mbox_set_invalid(&conn->acceptmbox);
}if (sys_mbox_valid(&conn->acceptmbox)) { ... }
/* ... */#endif
}{ ... }
#if LWIP_NETCONN_FULLDUPLEX
static void
netconn_mark_mbox_invalid(struct netconn *conn)
{
int i, num_waiting;
void *msg = LWIP_CONST_CAST(void *, &netconn_deleted);
conn->flags |= NETCONN_FLAG_MBOXINVALID;
SYS_ARCH_LOCKED(num_waiting = conn->mbox_threads_waiting);
for (i = 0; i < num_waiting; i++) {
if (sys_mbox_valid_val(conn->recvmbox)) {
sys_mbox_trypost(&conn->recvmbox, msg);
}if (sys_mbox_valid_val(conn->recvmbox)) { ... } else {
sys_mbox_trypost(&conn->acceptmbox, msg);
}else { ... }
}for (i = 0; i < num_waiting; i++) { ... }
}netconn_mark_mbox_invalid (struct netconn *conn) { ... }
/* ... */#endif
#if LWIP_TCP
/* ... */
static err_t
lwip_netconn_do_close_internal(struct netconn *conn WRITE_DELAYED_PARAM)
{
err_t err;
u8_t shut, shut_rx, shut_tx, shut_close;
u8_t close_finished = 0;
struct tcp_pcb *tpcb;
#if LWIP_SO_LINGER
u8_t linger_wait_required = 0;
#endif
LWIP_ASSERT("invalid conn", (conn != NULL));
LWIP_ASSERT("this is for tcp netconns only", (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP));
LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE));
LWIP_ASSERT("pcb already closed", (conn->pcb.tcp != NULL));
LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
tpcb = conn->pcb.tcp;
shut = conn->current_msg->msg.sd.shut;
shut_rx = shut & NETCONN_SHUT_RD;
shut_tx = shut & NETCONN_SHUT_WR;
/* ... */
if (shut == NETCONN_SHUT_RDWR) {
shut_close = 1;
}if (shut == NETCONN_SHUT_RDWR) { ... } else if (shut_rx &&
((tpcb->state == FIN_WAIT_1) ||
(tpcb->state == FIN_WAIT_2) ||
(tpcb->state == CLOSING))) {
shut_close = 1;
}else if (shut_rx && ((tpcb->state == FIN_WAIT_1) || (tpcb->state == FIN_WAIT_2) || (tpcb->state == CLOSING))) { ... } else if (shut_tx && ((tpcb->flags & TF_RXCLOSED) != 0)) {
shut_close = 1;
}else if (shut_tx && ((tpcb->flags & TF_RXCLOSED) != 0)) { ... } else {
shut_close = 0;
}else { ... }
if (shut_close) {
tcp_arg(tpcb, NULL);
}if (shut_close) { ... }
if (tpcb->state == LISTEN) {
tcp_accept(tpcb, NULL);
}if (tpcb->state == LISTEN) { ... } else {
if (shut_rx) {
tcp_recv(tpcb, NULL);
tcp_accept(tpcb, NULL);
}if (shut_rx) { ... }
if (shut_tx) {
tcp_sent(tpcb, NULL);
}if (shut_tx) { ... }
if (shut_close) {
tcp_poll(tpcb, NULL, 0);
tcp_err(tpcb, NULL);
}if (shut_close) { ... }
}else { ... }
if (shut_close) {
#if LWIP_SO_LINGER
err = ERR_OK;
if ((conn->linger >= 0) && (conn->pcb.tcp->unsent || conn->pcb.tcp->unacked)) {
if ((conn->linger == 0)) {
tcp_abort(tpcb);
tpcb = NULL;
}if ((conn->linger == 0)) { ... } else if (conn->linger > 0) {
if (netconn_is_nonblocking(conn)) {
err = ERR_WOULDBLOCK;
}if (netconn_is_nonblocking(conn)) { ... } else if ((s32_t)(sys_now() - conn->current_msg->msg.sd.time_started) >=
(conn->linger * 1000)) {
/* ... */
tcp_abort(tpcb);
tpcb = NULL;
}else if ((s32_t)(sys_now() - conn->current_msg->msg.sd.time_started) >= (conn->linger * 1000)) { ... } else {
linger_wait_required = 1;
}else { ... }
}else if (conn->linger > 0) { ... }
}if ((conn->linger >= 0) && (conn->pcb.tcp->unsent || conn->pcb.tcp->unacked)) { ... }
if ((err == ERR_OK) && (tpcb != NULL))/* ... */
#endif
{
err = tcp_close(tpcb);
...}
}if (shut_close) { ... } else {
err = tcp_shutdown(tpcb, shut_rx, shut_tx);
}else { ... }
if (err == ERR_OK) {
close_finished = 1;
#if LWIP_SO_LINGER
if (linger_wait_required) {
close_finished = 0;
err = ERR_INPROGRESS;
}if (linger_wait_required) { ... }
/* ... */#endif
}if (err == ERR_OK) { ... } else {
if (err == ERR_MEM) {
/* ... */
#if LWIP_SO_SNDTIMEO || LWIP_SO_LINGER
s32_t close_timeout = LWIP_TCP_CLOSE_TIMEOUT_MS_DEFAULT;
#if LWIP_SO_SNDTIMEO
if (conn->send_timeout > 0) {
close_timeout = conn->send_timeout;
}if (conn->send_timeout > 0) { ... }
/* ... */#endif
#if LWIP_SO_LINGER
if (conn->linger >= 0) {
close_timeout = conn->linger * 1000U;
}if (conn->linger >= 0) { ... }
/* ... */#endif
if ((s32_t)(sys_now() - conn->current_msg->msg.sd.time_started) >= close_timeout) {
#else
if (conn->current_msg->msg.sd.polls_left == 0) {
#endif
close_finished = 1;
if (shut_close) {
tcp_abort(tpcb);
err = ERR_OK;
}if (shut_close) { ... }
}if (conn->current_msg->msg.sd.polls_left == 0) { ... }
}if ((s32_t)(sys_now() - conn->current_msg->msg.sd.time_started) >= close_timeout) { ... } else {
close_finished = 1;
}else { ... }
}if (err == ERR_MEM) { ... }
if (close_finished) {
sys_sem_t *op_completed_sem = LWIP_API_MSG_SEM(conn->current_msg);
conn->current_msg->err = err;
conn->current_msg = NULL;
conn->state = NETCONN_NONE;
if (err == ERR_OK) {
if (shut_close) {
conn->pcb.tcp = NULL;
/* ... */
API_EVENT(conn, NETCONN_EVT_ERROR, 0);
}if (shut_close) { ... }
if (shut_rx) {
API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
}if (shut_rx) { ... }
if (shut_tx) {
API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
}if (shut_tx) { ... }
}if (err == ERR_OK) { ... }
#if LWIP_TCPIP_CORE_LOCKING
if (delayed)
#endif
{
sys_sem_signal(op_completed_sem);
...}
return ERR_OK;
}if (close_finished) { ... }
if (!close_finished) {
LWIP_ASSERT("Closing a listen pcb may not fail!", (tpcb->state != LISTEN));
if (shut_tx) {
tcp_sent(tpcb, sent_tcp);
}if (shut_tx) { ... }
tcp_poll(tpcb, poll_tcp, 1);
tcp_err(tpcb, err_tcp);
tcp_arg(tpcb, conn);
}if (!close_finished) { ... }
/* ... */
LWIP_ASSERT("err != ERR_OK", err != ERR_OK);
return err;
}{ ... }
#endif/* ... */
/* ... */
void
lwip_netconn_do_delconn(void *m)
{
struct api_msg *msg = (struct api_msg *)m;
enum netconn_state state = msg->conn->state;
LWIP_ASSERT("netconn state error",
(state == NETCONN_NONE) || (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP));
#if LWIP_NETCONN_FULLDUPLEX
if (state != NETCONN_NONE) {
if ((state == NETCONN_WRITE) ||
((state == NETCONN_CONNECT) && !IN_NONBLOCKING_CONNECT(msg->conn))) {
sys_sem_t *op_completed_sem;
LWIP_ASSERT("msg->conn->current_msg != NULL", msg->conn->current_msg != NULL);
op_completed_sem = LWIP_API_MSG_SEM(msg->conn->current_msg);
msg->conn->current_msg->err = ERR_CLSD;
msg->conn->current_msg = NULL;
msg->conn->state = NETCONN_NONE;
sys_sem_signal(op_completed_sem);
}if ((state == NETCONN_WRITE) || ((state == NETCONN_CONNECT) && !IN_NONBLOCKING_CONNECT(msg->conn))) { ... }
}if (state != NETCONN_NONE) { ... }
/* ... */#else
if (((state != NETCONN_NONE) &&
(state != NETCONN_LISTEN) &&
(state != NETCONN_CONNECT)) ||
((state == NETCONN_CONNECT) && !IN_NONBLOCKING_CONNECT(msg->conn))) {
/* ... */
msg->err = ERR_INPROGRESS;
}if (((state != NETCONN_NONE) && (state != NETCONN_LISTEN) && (state != NETCONN_CONNECT)) || ((state == NETCONN_CONNECT) && !IN_NONBLOCKING_CONNECT(msg->conn))) { ... } else
#endif
{
LWIP_ASSERT("blocking connect in progress",
(state != NETCONN_CONNECT) || IN_NONBLOCKING_CONNECT(msg->conn));
msg->err = ERR_OK;
#if LWIP_NETCONN_FULLDUPLEX
netconn_mark_mbox_invalid(msg->conn);/* ... */
#else
netconn_drain(msg->conn);
#endif
if (msg->conn->pcb.tcp != NULL) {
switch (NETCONNTYPE_GROUP(msg->conn->type)) {
#if LWIP_RAW
case NETCONN_RAW:
raw_remove(msg->conn->pcb.raw);
break;/* ... */
#endif
#if LWIP_UDPcase NETCONN_RAW:
case NETCONN_UDP:
msg->conn->pcb.udp->recv_arg = NULL;
udp_remove(msg->conn->pcb.udp);
break;/* ... */
#endif
#if LWIP_TCPcase NETCONN_UDP:
case NETCONN_TCP:
LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL);
msg->conn->state = NETCONN_CLOSE;
msg->msg.sd.shut = NETCONN_SHUT_RDWR;
msg->conn->current_msg = msg;
#if LWIP_TCPIP_CORE_LOCKING
if (lwip_netconn_do_close_internal(msg->conn, 0) != ERR_OK) {
LWIP_ASSERT("state!", msg->conn->state == NETCONN_CLOSE);
UNLOCK_TCPIP_CORE();
sys_arch_sem_wait(LWIP_API_MSG_SEM(msg), 0);
LOCK_TCPIP_CORE();
LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
}if (lwip_netconn_do_close_internal(msg->conn, 0) != ERR_OK) { ... }
/* ... */#else
lwip_netconn_do_close_internal(msg->conn);
#endif
/* ... */
return;/* ... */
#endif case NETCONN_TCP:
default:
break;default
}switch (NETCONNTYPE_GROUP(msg->conn->type)) { ... }
msg->conn->pcb.tcp = NULL;
}if (msg->conn->pcb.tcp != NULL) { ... }
/* ... */
API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
}else { ... }
if (sys_sem_valid(LWIP_API_MSG_SEM(msg))) {
TCPIP_APIMSG_ACK(msg);
}if (sys_sem_valid(LWIP_API_MSG_SEM(msg))) { ... }
}{ ... }
/* ... */
void
lwip_netconn_do_bind(void *m)
{
struct api_msg *msg = (struct api_msg *)m;
err_t err;
if (msg->conn->pcb.tcp != NULL) {
switch (NETCONNTYPE_GROUP(msg->conn->type)) {
#if LWIP_RAW
case NETCONN_RAW:
err = raw_bind(msg->conn->pcb.raw, API_EXPR_REF(msg->msg.bc.ipaddr));
break;/* ... */
#endif
#if LWIP_UDPcase NETCONN_RAW:
case NETCONN_UDP:
err = udp_bind(msg->conn->pcb.udp, API_EXPR_REF(msg->msg.bc.ipaddr), msg->msg.bc.port);
break;/* ... */
#endif
#if LWIP_TCPcase NETCONN_UDP:
case NETCONN_TCP:
err = tcp_bind(msg->conn->pcb.tcp, API_EXPR_REF(msg->msg.bc.ipaddr), msg->msg.bc.port);
break;/* ... */
#endif case NETCONN_TCP:
default:
err = ERR_VAL;
break;default
}switch (NETCONNTYPE_GROUP(msg->conn->type)) { ... }
}if (msg->conn->pcb.tcp != NULL) { ... } else {
err = ERR_VAL;
}else { ... }
msg->err = err;
TCPIP_APIMSG_ACK(msg);
}{ ... }
/* ... */
void
lwip_netconn_do_bind_if(void *m)
{
struct netif *netif;
struct api_msg *msg = (struct api_msg *)m;
err_t err;
netif = netif_get_by_index(msg->msg.bc.if_idx);
if ((netif != NULL) && (msg->conn->pcb.tcp != NULL)) {
err = ERR_OK;
switch (NETCONNTYPE_GROUP(msg->conn->type)) {
#if LWIP_RAW
case NETCONN_RAW:
raw_bind_netif(msg->conn->pcb.raw, netif);
break;/* ... */
#endif
#if LWIP_UDPcase NETCONN_RAW:
case NETCONN_UDP:
udp_bind_netif(msg->conn->pcb.udp, netif);
break;/* ... */
#endif
#if LWIP_TCPcase NETCONN_UDP:
case NETCONN_TCP:
tcp_bind_netif(msg->conn->pcb.tcp, netif);
break;/* ... */
#endif case NETCONN_TCP:
default:
err = ERR_VAL;
break;default
}switch (NETCONNTYPE_GROUP(msg->conn->type)) { ... }
}if ((netif != NULL) && (msg->conn->pcb.tcp != NULL)) { ... } else {
err = ERR_VAL;
}else { ... }
msg->err = err;
TCPIP_APIMSG_ACK(msg);
}{ ... }
#if LWIP_TCP
/* ... */
static err_t
lwip_netconn_do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
{
struct netconn *conn;
int was_blocking;
sys_sem_t *op_completed_sem = NULL;
LWIP_UNUSED_ARG(pcb);
conn = (struct netconn *)arg;
if (conn == NULL) {
return ERR_VAL;
}if (conn == NULL) { ... }
LWIP_ASSERT("conn->state == NETCONN_CONNECT", conn->state == NETCONN_CONNECT);
LWIP_ASSERT("(conn->current_msg != NULL) || conn->in_non_blocking_connect",
(conn->current_msg != NULL) || IN_NONBLOCKING_CONNECT(conn));
if (conn->current_msg != NULL) {
conn->current_msg->err = err;
op_completed_sem = LWIP_API_MSG_SEM(conn->current_msg);
}if (conn->current_msg != NULL) { ... }
if ((NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP) && (err == ERR_OK)) {
setup_tcp(conn);
}if ((NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP) && (err == ERR_OK)) { ... }
was_blocking = !IN_NONBLOCKING_CONNECT(conn);
SET_NONBLOCKING_CONNECT(conn, 0);
LWIP_ASSERT("blocking connect state error",
(was_blocking && op_completed_sem != NULL) ||
(!was_blocking && op_completed_sem == NULL));
conn->current_msg = NULL;
conn->state = NETCONN_NONE;
API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
if (was_blocking) {
sys_sem_signal(op_completed_sem);
}if (was_blocking) { ... }
return ERR_OK;
}{ ... }
#endif/* ... */
/* ... */
void
lwip_netconn_do_connect(void *m)
{
struct api_msg *msg = (struct api_msg *)m;
err_t err;
if (msg->conn->pcb.tcp == NULL) {
err = ERR_CLSD;
}if (msg->conn->pcb.tcp == NULL) { ... } else {
switch (NETCONNTYPE_GROUP(msg->conn->type)) {
#if LWIP_RAW
case NETCONN_RAW:
err = raw_connect(msg->conn->pcb.raw, API_EXPR_REF(msg->msg.bc.ipaddr));
break;/* ... */
#endif
#if LWIP_UDPcase NETCONN_RAW:
case NETCONN_UDP:
err = udp_connect(msg->conn->pcb.udp, API_EXPR_REF(msg->msg.bc.ipaddr), msg->msg.bc.port);
break;/* ... */
#endif
#if LWIP_TCPcase NETCONN_UDP:
case NETCONN_TCP:
if (msg->conn->state == NETCONN_CONNECT) {
err = ERR_ALREADY;
}if (msg->conn->state == NETCONN_CONNECT) { ... } else if (msg->conn->state != NETCONN_NONE) {
err = ERR_ISCONN;
}else if (msg->conn->state != NETCONN_NONE) { ... } else {
setup_tcp(msg->conn);
err = tcp_connect(msg->conn->pcb.tcp, API_EXPR_REF(msg->msg.bc.ipaddr),
msg->msg.bc.port, lwip_netconn_do_connected);
if (err == ERR_OK) {
u8_t non_blocking = netconn_is_nonblocking(msg->conn);
msg->conn->state = NETCONN_CONNECT;
SET_NONBLOCKING_CONNECT(msg->conn, non_blocking);
if (non_blocking) {
err = ERR_INPROGRESS;
}if (non_blocking) { ... } else {
msg->conn->current_msg = msg;
/* ... */
#if LWIP_TCPIP_CORE_LOCKING
LWIP_ASSERT("state!", msg->conn->state == NETCONN_CONNECT);
UNLOCK_TCPIP_CORE();
sys_arch_sem_wait(LWIP_API_MSG_SEM(msg), 0);
LOCK_TCPIP_CORE();
LWIP_ASSERT("state!", msg->conn->state != NETCONN_CONNECT);/* ... */
#endif
return;
}else { ... }
}if (err == ERR_OK) { ... }
}else { ... }
break;/* ... */
#endif case NETCONN_TCP:
default:
LWIP_ERROR("Invalid netconn type", 0, do {
err = ERR_VAL;
...} while (0));
break;default
}switch (NETCONNTYPE_GROUP(msg->conn->type)) { ... }
}else { ... }
msg->err = err;
/* ... */
TCPIP_APIMSG_ACK(msg);
}{ ... }
/* ... */
void
lwip_netconn_do_disconnect(void *m)
{
struct api_msg *msg = (struct api_msg *)m;
#if LWIP_UDP
if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
udp_disconnect(msg->conn->pcb.udp);
msg->err = ERR_OK;
}if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) { ... } else
#endif
{
msg->err = ERR_VAL;
}else { ... }
TCPIP_APIMSG_ACK(msg);
}{ ... }
#if LWIP_TCP
/* ... */
void
lwip_netconn_do_listen(void *m)
{
struct api_msg *msg = (struct api_msg *)m;
err_t err;
if (msg->conn->pcb.tcp != NULL) {
if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) {
if (msg->conn->state == NETCONN_NONE) {
struct tcp_pcb *lpcb;
if (msg->conn->pcb.tcp->state != CLOSED) {
err = ERR_VAL;
}if (msg->conn->pcb.tcp->state != CLOSED) { ... } else {
u8_t backlog;
#if TCP_LISTEN_BACKLOG
backlog = msg->msg.lb.backlog;
#else
backlog = TCP_DEFAULT_LISTEN_BACKLOG;
#endif
#if LWIP_IPV4 && LWIP_IPV6
/* ... */
if (ip_addr_cmp(&msg->conn->pcb.ip->local_ip, IP6_ADDR_ANY) &&
(netconn_get_ipv6only(msg->conn) == 0)) {
IP_SET_TYPE_VAL(msg->conn->pcb.tcp->local_ip, IPADDR_TYPE_ANY);
IP_SET_TYPE_VAL(msg->conn->pcb.tcp->remote_ip, IPADDR_TYPE_ANY);
}if (ip_addr_cmp(&msg->conn->pcb.ip->local_ip, IP6_ADDR_ANY) && (netconn_get_ipv6only(msg->conn) == 0)) { ... }
/* ... */#endif
lpcb = tcp_listen_with_backlog_and_err(msg->conn->pcb.tcp, backlog, &err);
if (lpcb == NULL) {
}if (lpcb == NULL) { ... } else {
if (sys_mbox_valid(&msg->conn->recvmbox)) {
sys_mbox_free(&msg->conn->recvmbox);
sys_mbox_set_invalid(&msg->conn->recvmbox);
}if (sys_mbox_valid(&msg->conn->recvmbox)) { ... }
err = ERR_OK;
if (!sys_mbox_valid(&msg->conn->acceptmbox)) {
err = sys_mbox_new(&msg->conn->acceptmbox, DEFAULT_ACCEPTMBOX_SIZE);
}if (!sys_mbox_valid(&msg->conn->acceptmbox)) { ... }
if (err == ERR_OK) {
msg->conn->state = NETCONN_LISTEN;
msg->conn->pcb.tcp = lpcb;
tcp_arg(msg->conn->pcb.tcp, msg->conn);
tcp_accept(msg->conn->pcb.tcp, accept_function);
}if (err == ERR_OK) { ... } else {
tcp_close(lpcb);
msg->conn->pcb.tcp = NULL;
}else { ... }
}else { ... }
}else { ... }
}if (msg->conn->state == NETCONN_NONE) { ... } else if (msg->conn->state == NETCONN_LISTEN) {
err = ERR_OK;
tcp_backlog_set(msg->conn->pcb.tcp, msg->msg.lb.backlog);
}else if (msg->conn->state == NETCONN_LISTEN) { ... } else {
err = ERR_CONN;
}else { ... }
}if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) { ... } else {
err = ERR_ARG;
}else { ... }
}if (msg->conn->pcb.tcp != NULL) { ... } else {
err = ERR_CONN;
}else { ... }
msg->err = err;
TCPIP_APIMSG_ACK(msg);
}{ ... }
#endif/* ... */
/* ... */
void
lwip_netconn_do_send(void *m)
{
struct api_msg *msg = (struct api_msg *)m;
err_t err = netconn_err(msg->conn);
if (err == ERR_OK) {
if (msg->conn->pcb.tcp != NULL) {
switch (NETCONNTYPE_GROUP(msg->conn->type)) {
#if LWIP_RAW
case NETCONN_RAW:
if (ip_addr_isany(&msg->msg.b->addr) || IP_IS_ANY_TYPE_VAL(msg->msg.b->addr)) {
err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
}if (ip_addr_isany(&msg->msg.b->addr) || IP_IS_ANY_TYPE_VAL(msg->msg.b->addr)) { ... } else {
err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, &msg->msg.b->addr);
}else { ... }
break;/* ... */
#endif
#if LWIP_UDPcase NETCONN_RAW:
case NETCONN_UDP:
#if LWIP_CHECKSUM_ON_COPY
if (ip_addr_isany(&msg->msg.b->addr) || IP_IS_ANY_TYPE_VAL(msg->msg.b->addr)) {
err = udp_send_chksum(msg->conn->pcb.udp, msg->msg.b->p,
msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
}if (ip_addr_isany(&msg->msg.b->addr) || IP_IS_ANY_TYPE_VAL(msg->msg.b->addr)) { ... } else {
err = udp_sendto_chksum(msg->conn->pcb.udp, msg->msg.b->p,
&msg->msg.b->addr, msg->msg.b->port,
msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
}else { ... }
/* ... */#else
if (ip_addr_isany_val(msg->msg.b->addr) || IP_IS_ANY_TYPE_VAL(msg->msg.b->addr)) {
err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
}if (ip_addr_isany_val(msg->msg.b->addr) || IP_IS_ANY_TYPE_VAL(msg->msg.b->addr)) { ... } else {
err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, &msg->msg.b->addr, msg->msg.b->port);
}else { ... }
/* ... */#endif
break;/* ... */
#endif case NETCONN_UDP:
default:
err = ERR_CONN;
break;default
}switch (NETCONNTYPE_GROUP(msg->conn->type)) { ... }
}if (msg->conn->pcb.tcp != NULL) { ... } else {
err = ERR_CONN;
}else { ... }
}if (err == ERR_OK) { ... }
msg->err = err;
TCPIP_APIMSG_ACK(msg);
}{ ... }
#if LWIP_TCP
/* ... */
void
lwip_netconn_do_recv(void *m)
{
struct api_msg *msg = (struct api_msg *)m;
msg->err = ERR_OK;
if (msg->conn->pcb.tcp != NULL) {
if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) {
size_t remaining = msg->msg.r.len;
do {
u16_t recved = (u16_t)((remaining > 0xffff) ? 0xffff : remaining);
tcp_recved(msg->conn->pcb.tcp, recved);
remaining -= recved;
...} while (remaining != 0);
}if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) { ... }
}if (msg->conn->pcb.tcp != NULL) { ... }
TCPIP_APIMSG_ACK(msg);
}{ ... }
#if TCP_LISTEN_BACKLOG
/* ... */
void
lwip_netconn_do_accepted(void *m)
{
struct api_msg *msg = (struct api_msg *)m;
msg->err = ERR_OK;
if (msg->conn->pcb.tcp != NULL) {
if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) {
tcp_backlog_accepted(msg->conn->pcb.tcp);
}if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) { ... }
}if (msg->conn->pcb.tcp != NULL) { ... }
TCPIP_APIMSG_ACK(msg);
}lwip_netconn_do_accepted (void *m) { ... }
/* ... */#endif
/* ... */
static err_t
lwip_netconn_do_writemore(struct netconn *conn WRITE_DELAYED_PARAM)
{
err_t err;
const void *dataptr;
u16_t len, available;
u8_t write_finished = 0;
size_t diff;
u8_t dontblock;
u8_t apiflags;
u8_t write_more;
LWIP_ASSERT("conn != NULL", conn != NULL);
LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL);
LWIP_ASSERT("conn->current_msg->msg.w.offset < conn->current_msg->msg.w.len",
conn->current_msg->msg.w.offset < conn->current_msg->msg.w.len);
LWIP_ASSERT("conn->current_msg->msg.w.vector_cnt > 0", conn->current_msg->msg.w.vector_cnt > 0);
apiflags = conn->current_msg->msg.w.apiflags;
dontblock = netconn_is_nonblocking(conn) || (apiflags & NETCONN_DONTBLOCK);
#if LWIP_SO_SNDTIMEO
if ((conn->send_timeout != 0) &&
((s32_t)(sys_now() - conn->current_msg->msg.w.time_started) >= conn->send_timeout)) {
write_finished = 1;
if (conn->current_msg->msg.w.offset == 0) {
err = ERR_WOULDBLOCK;
}if (conn->current_msg->msg.w.offset == 0) { ... } else {
err = ERR_OK;
}else { ... }
}if ((conn->send_timeout != 0) && ((s32_t)(sys_now() - conn->current_msg->msg.w.time_started) >= conn->send_timeout)) { ... } else
#endif
{
do {
dataptr = (const u8_t *)conn->current_msg->msg.w.vector->ptr + conn->current_msg->msg.w.vector_off;
diff = conn->current_msg->msg.w.vector->len - conn->current_msg->msg.w.vector_off;
if (diff > 0xffffUL) {
len = 0xffff;
apiflags |= TCP_WRITE_FLAG_MORE;
}if (diff > 0xffffUL) { ... } else {
len = (u16_t)diff;
}else { ... }
available = tcp_sndbuf(conn->pcb.tcp);
if (available < len) {
len = available;
if (dontblock) {
if (!len) {
err = (conn->current_msg->msg.w.offset == 0) ? ERR_WOULDBLOCK : ERR_OK;
goto err_mem;
}if (!len) { ... }
}if (dontblock) { ... } else {
apiflags |= TCP_WRITE_FLAG_MORE;
}else { ... }
}if (available < len) { ... }
LWIP_ASSERT("lwip_netconn_do_writemore: invalid length!",
((conn->current_msg->msg.w.vector_off + len) <= conn->current_msg->msg.w.vector->len));
/* ... */
if ((len == 0xffff && diff > 0xffffUL) ||
(len == (u16_t)diff && conn->current_msg->msg.w.vector_cnt > 1)) {
write_more = 1;
apiflags |= TCP_WRITE_FLAG_MORE;
}if ((len == 0xffff && diff > 0xffffUL) || (len == (u16_t)diff && conn->current_msg->msg.w.vector_cnt > 1)) { ... } else {
write_more = 0;
}else { ... }
err = tcp_write(conn->pcb.tcp, dataptr, len, apiflags);
if (err == ERR_OK) {
conn->current_msg->msg.w.offset += len;
conn->current_msg->msg.w.vector_off += len;
if (conn->current_msg->msg.w.vector_off == conn->current_msg->msg.w.vector->len) {
conn->current_msg->msg.w.vector_cnt--;
if (conn->current_msg->msg.w.vector_cnt > 0) {
conn->current_msg->msg.w.vector++;
conn->current_msg->msg.w.vector_off = 0;
}if (conn->current_msg->msg.w.vector_cnt > 0) { ... }
}if (conn->current_msg->msg.w.vector_off == conn->current_msg->msg.w.vector->len) { ... }
}if (err == ERR_OK) { ... }
...} while (write_more && err == ERR_OK);
if ((err == ERR_OK) || (err == ERR_MEM)) {
err_mem:
if (dontblock && (conn->current_msg->msg.w.offset < conn->current_msg->msg.w.len)) {
/* ... */
API_EVENT(conn, NETCONN_EVT_SENDMINUS, 0);
conn->flags |= NETCONN_FLAG_CHECK_WRITESPACE;
}if (dontblock && (conn->current_msg->msg.w.offset < conn->current_msg->msg.w.len)) { ... } else if ((tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT) ||
(tcp_sndqueuelen(conn->pcb.tcp) >= TCP_SNDQUEUELOWAT)) {
/* ... */
API_EVENT(conn, NETCONN_EVT_SENDMINUS, 0);
}else if ((tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT) || (tcp_sndqueuelen(conn->pcb.tcp) >= TCP_SNDQUEUELOWAT)) { ... }
}if ((err == ERR_OK) || (err == ERR_MEM)) { ... }
if (err == ERR_OK) {
err_t out_err;
if ((conn->current_msg->msg.w.offset == conn->current_msg->msg.w.len) || dontblock) {
write_finished = 1;
}if ((conn->current_msg->msg.w.offset == conn->current_msg->msg.w.len) || dontblock) { ... }
out_err = tcp_output(conn->pcb.tcp);
if (out_err == ERR_RTE) {
/* ... */
err = out_err;
write_finished = 1;
}if (out_err == ERR_RTE) { ... }
}if (err == ERR_OK) { ... } else if (err == ERR_MEM) {
/* ... */
err_t out_err = tcp_output(conn->pcb.tcp);
if (out_err == ERR_RTE) {
/* ... */
err = out_err;
write_finished = 1;
}if (out_err == ERR_RTE) { ... } else if (dontblock) {
/* ... */
err = (conn->current_msg->msg.w.offset == 0) ? ERR_WOULDBLOCK : ERR_OK;
write_finished = 1;
}else if (dontblock) { ... }
}else if (err == ERR_MEM) { ... } else {
/* ... */
write_finished = 1;
}else { ... }
}else { ... }
if (write_finished) {
/* ... */
sys_sem_t *op_completed_sem = LWIP_API_MSG_SEM(conn->current_msg);
conn->current_msg->err = err;
conn->current_msg = NULL;
conn->state = NETCONN_NONE;
#if LWIP_TCPIP_CORE_LOCKING
if (delayed)
#endif
{
sys_sem_signal(op_completed_sem);
...}
}if (write_finished) { ... }
#if LWIP_TCPIP_CORE_LOCKING
else {
return ERR_MEM;
}else { ... }
/* ... */#endif
return ERR_OK;
}{ ... }
#endif/* ... */
/* ... */
void
lwip_netconn_do_write(void *m)
{
struct api_msg *msg = (struct api_msg *)m;
err_t err = netconn_err(msg->conn);
if (err == ERR_OK) {
if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) {
#if LWIP_TCP
if (msg->conn->state != NETCONN_NONE) {
err = ERR_INPROGRESS;
}if (msg->conn->state != NETCONN_NONE) { ... } else if (msg->conn->pcb.tcp != NULL) {
msg->conn->state = NETCONN_WRITE;
LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL);
LWIP_ASSERT("msg->msg.w.len != 0", msg->msg.w.len != 0);
msg->conn->current_msg = msg;
#if LWIP_TCPIP_CORE_LOCKING
if (lwip_netconn_do_writemore(msg->conn, 0) != ERR_OK) {
LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
UNLOCK_TCPIP_CORE();
sys_arch_sem_wait(LWIP_API_MSG_SEM(msg), 0);
LOCK_TCPIP_CORE();
LWIP_ASSERT("state!", msg->conn->state != NETCONN_WRITE);
}if (lwip_netconn_do_writemore(msg->conn, 0) != ERR_OK) { ... }
/* ... */#else
lwip_netconn_do_writemore(msg->conn);
#endif
/* ... */
return;
}else if (msg->conn->pcb.tcp != NULL) { ... } else {
err = ERR_CONN;
}else { ... }
/* ... */#else
err = ERR_VAL;
#endif
#if (LWIP_UDP || LWIP_RAW)
}if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) { ... } else {
err = ERR_VAL;
#endif
}else { ... }
}if (err == ERR_OK) { ... }
msg->err = err;
TCPIP_APIMSG_ACK(msg);
}{ ... }
/* ... */
void
lwip_netconn_do_getaddr(void *m)
{
struct api_msg *msg = (struct api_msg *)m;
if (msg->conn->pcb.ip != NULL) {
if (msg->msg.ad.local) {
ip_addr_copy(API_EXPR_DEREF(msg->msg.ad.ipaddr),
msg->conn->pcb.ip->local_ip);
}if (msg->msg.ad.local) { ... } else {
ip_addr_copy(API_EXPR_DEREF(msg->msg.ad.ipaddr),
msg->conn->pcb.ip->remote_ip);
}else { ... }
msg->err = ERR_OK;
switch (NETCONNTYPE_GROUP(msg->conn->type)) {
#if LWIP_RAW
case NETCONN_RAW:
if (msg->msg.ad.local) {
API_EXPR_DEREF(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
}if (msg->msg.ad.local) { ... } else {
msg->err = ERR_CONN;
}else { ... }
break;/* ... */
#endif
#if LWIP_UDPcase NETCONN_RAW:
case NETCONN_UDP:
if (msg->msg.ad.local) {
API_EXPR_DEREF(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
}if (msg->msg.ad.local) { ... } else {
if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
msg->err = ERR_CONN;
}if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) { ... } else {
API_EXPR_DEREF(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
}else { ... }
}else { ... }
break;/* ... */
#endif
#if LWIP_TCPcase NETCONN_UDP:
case NETCONN_TCP:
if ((msg->msg.ad.local == 0) &&
((msg->conn->pcb.tcp->state == CLOSED) || (msg->conn->pcb.tcp->state == LISTEN))) {
msg->err = ERR_CONN;
}if ((msg->msg.ad.local == 0) && ((msg->conn->pcb.tcp->state == CLOSED) || (msg->conn->pcb.tcp->state == LISTEN))) { ... } else {
API_EXPR_DEREF(msg->msg.ad.port) = (msg->msg.ad.local ? msg->conn->pcb.tcp->local_port : msg->conn->pcb.tcp->remote_port);
}else { ... }
break;/* ... */
#endif case NETCONN_TCP:
default:
LWIP_ASSERT("invalid netconn_type", 0);
break;default
}switch (NETCONNTYPE_GROUP(msg->conn->type)) { ... }
}if (msg->conn->pcb.ip != NULL) { ... } else {
msg->err = ERR_CONN;
}else { ... }
TCPIP_APIMSG_ACK(msg);
}{ ... }
/* ... */
void
lwip_netconn_do_close(void *m)
{
struct api_msg *msg = (struct api_msg *)m;
#if LWIP_TCP
enum netconn_state state = msg->conn->state;
/* ... */
if ((msg->conn->pcb.tcp != NULL) &&
(NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) &&
((msg->msg.sd.shut == NETCONN_SHUT_RDWR) || (state != NETCONN_LISTEN))) {
if (state == NETCONN_CONNECT) {
msg->err = ERR_CONN;
}if (state == NETCONN_CONNECT) { ... } else if (state == NETCONN_WRITE) {
#if LWIP_NETCONN_FULLDUPLEX
if (msg->msg.sd.shut & NETCONN_SHUT_WR) {
sys_sem_t *write_completed_sem;
LWIP_ASSERT("msg->conn->current_msg != NULL", msg->conn->current_msg != NULL);
write_completed_sem = LWIP_API_MSG_SEM(msg->conn->current_msg);
msg->conn->current_msg->err = ERR_CLSD;
msg->conn->current_msg = NULL;
msg->conn->state = NETCONN_NONE;
state = NETCONN_NONE;
sys_sem_signal(write_completed_sem);
}if (msg->msg.sd.shut & NETCONN_SHUT_WR) { ... } else {
LWIP_ASSERT("msg->msg.sd.shut == NETCONN_SHUT_RD", msg->msg.sd.shut == NETCONN_SHUT_RD);
/* ... */
msg->err = tcp_shutdown(msg->conn->pcb.tcp, 1, 0);
}else { ... }
}else if (state == NETCONN_WRITE) { ... }
if (state == NETCONN_NONE) {
#else
msg->err = ERR_INPROGRESS;
}if (state == NETCONN_NONE) { ... } else {
#endif
if (msg->msg.sd.shut & NETCONN_SHUT_RD) {
#if LWIP_NETCONN_FULLDUPLEX
netconn_mark_mbox_invalid(msg->conn);/* ... */
#else
netconn_drain(msg->conn);
#endif
}if (msg->msg.sd.shut & NETCONN_SHUT_RD) { ... }
LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL);
msg->conn->state = NETCONN_CLOSE;
msg->conn->current_msg = msg;
#if LWIP_TCPIP_CORE_LOCKING
if (lwip_netconn_do_close_internal(msg->conn, 0) != ERR_OK) {
LWIP_ASSERT("state!", msg->conn->state == NETCONN_CLOSE);
UNLOCK_TCPIP_CORE();
sys_arch_sem_wait(LWIP_API_MSG_SEM(msg), 0);
LOCK_TCPIP_CORE();
LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
}if (lwip_netconn_do_close_internal(msg->conn, 0) != ERR_OK) { ... }
/* ... */#else
lwip_netconn_do_close_internal(msg->conn);
#endif
return;
}else { ... }
}if ((msg->conn->pcb.tcp != NULL) && (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) && ((msg->msg.sd.shut == NETCONN_SHUT_RDWR) || (state != NETCONN_LISTEN))) { ... } else
#endif
{
msg->err = ERR_CONN;
}else { ... }
TCPIP_APIMSG_ACK(msg);
}{ ... }
#if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
/* ... */
void
lwip_netconn_do_join_leave_group(void *m)
{
struct api_msg *msg = (struct api_msg *)m;
msg->err = ERR_CONN;
if (msg->conn->pcb.tcp != NULL) {
if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
#if LWIP_UDP
#if LWIP_IPV6 && LWIP_IPV6_MLD
if (NETCONNTYPE_ISIPV6(msg->conn->type)) {
if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
msg->err = mld6_joingroup(ip_2_ip6(API_EXPR_REF(msg->msg.jl.netif_addr)),
ip_2_ip6(API_EXPR_REF(msg->msg.jl.multiaddr)));
}if (msg->msg.jl.join_or_leave == NETCONN_JOIN) { ... } else {
msg->err = mld6_leavegroup(ip_2_ip6(API_EXPR_REF(msg->msg.jl.netif_addr)),
ip_2_ip6(API_EXPR_REF(msg->msg.jl.multiaddr)));
}else { ... }
}if (NETCONNTYPE_ISIPV6(msg->conn->type)) { ... } else
#endif
{
#if LWIP_IGMP
if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
msg->err = igmp_joingroup(ip_2_ip4(API_EXPR_REF(msg->msg.jl.netif_addr)),
ip_2_ip4(API_EXPR_REF(msg->msg.jl.multiaddr)));
}if (msg->msg.jl.join_or_leave == NETCONN_JOIN) { ... } else {
msg->err = igmp_leavegroup(ip_2_ip4(API_EXPR_REF(msg->msg.jl.netif_addr)),
ip_2_ip4(API_EXPR_REF(msg->msg.jl.multiaddr)));
}else { ... }
/* ... */#endif
}else { ... }
/* ... */#endif
#if (LWIP_TCP || LWIP_RAW)
}if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) { ... } else {
msg->err = ERR_VAL;
#endif
}else { ... }
}if (msg->conn->pcb.tcp != NULL) { ... }
TCPIP_APIMSG_ACK(msg);
}lwip_netconn_do_join_leave_group (void *m) { ... }
/* ... */
void
lwip_netconn_do_join_leave_group_netif(void *m)
{
struct api_msg *msg = (struct api_msg *)m;
struct netif *netif;
netif = netif_get_by_index(msg->msg.jl.if_idx);
if (netif == NULL) {
msg->err = ERR_IF;
goto done;
}if (netif == NULL) { ... }
msg->err = ERR_CONN;
if (msg->conn->pcb.tcp != NULL) {
if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
#if LWIP_UDP
#if LWIP_IPV6 && LWIP_IPV6_MLD
if (NETCONNTYPE_ISIPV6(msg->conn->type)) {
if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
msg->err = mld6_joingroup_netif(netif,
ip_2_ip6(API_EXPR_REF(msg->msg.jl.multiaddr)));
}if (msg->msg.jl.join_or_leave == NETCONN_JOIN) { ... } else {
msg->err = mld6_leavegroup_netif(netif,
ip_2_ip6(API_EXPR_REF(msg->msg.jl.multiaddr)));
}else { ... }
}if (NETCONNTYPE_ISIPV6(msg->conn->type)) { ... } else
#endif
{
#if LWIP_IGMP
if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
msg->err = igmp_joingroup_netif(netif,
ip_2_ip4(API_EXPR_REF(msg->msg.jl.multiaddr)));
}if (msg->msg.jl.join_or_leave == NETCONN_JOIN) { ... } else {
msg->err = igmp_leavegroup_netif(netif,
ip_2_ip4(API_EXPR_REF(msg->msg.jl.multiaddr)));
}else { ... }
/* ... */#endif
}else { ... }
/* ... */#endif
#if (LWIP_TCP || LWIP_RAW)
}if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) { ... } else {
msg->err = ERR_VAL;
#endif
}else { ... }
}if (msg->conn->pcb.tcp != NULL) { ... }
done:
TCPIP_APIMSG_ACK(msg);
}lwip_netconn_do_join_leave_group_netif (void *m) { ... }
/* ... */#endif
#if LWIP_DNS
/* ... */
static void
lwip_netconn_do_dns_found(const char *name, const ip_addr_t *ipaddr, void *arg)
{
struct dns_api_msg *msg = (struct dns_api_msg *)arg;
LWIP_UNUSED_ARG(name);
if (ipaddr == NULL) {
API_EXPR_DEREF(msg->err) = ERR_VAL;
}if (ipaddr == NULL) { ... } else {
API_EXPR_DEREF(msg->err) = ERR_OK;
API_EXPR_DEREF(msg->addr) = *ipaddr;
}else { ... }
sys_sem_signal(API_EXPR_REF_SEM(msg->sem));
}lwip_netconn_do_dns_found (const char *name, const ip_addr_t *ipaddr, void *arg) { ... }
/* ... */
void
lwip_netconn_do_gethostbyname(void *arg)
{
struct dns_api_msg *msg = (struct dns_api_msg *)arg;
u8_t addrtype =
#if LWIP_IPV4 && LWIP_IPV6
msg->dns_addrtype;
#else
LWIP_DNS_ADDRTYPE_DEFAULT;
#endif
API_EXPR_DEREF(msg->err) = dns_gethostbyname_addrtype(msg->name,
API_EXPR_REF(msg->addr), lwip_netconn_do_dns_found, msg, addrtype);
#if LWIP_TCPIP_CORE_LOCKING
if (API_EXPR_DEREF(msg->err) == ERR_INPROGRESS) {
UNLOCK_TCPIP_CORE();
sys_sem_wait(API_EXPR_REF_SEM(msg->sem));
LOCK_TCPIP_CORE();
LWIP_ASSERT("do_gethostbyname still in progress!!", API_EXPR_DEREF(msg->err) != ERR_INPROGRESS);
}if (API_EXPR_DEREF(msg->err) == ERR_INPROGRESS) { ... }
/* ... */#else
if (API_EXPR_DEREF(msg->err) != ERR_INPROGRESS) {
/* ... */
sys_sem_signal(API_EXPR_REF_SEM(msg->sem));
}if (API_EXPR_DEREF(msg->err) != ERR_INPROGRESS) { ... }
/* ... */#endif
}lwip_netconn_do_gethostbyname (void *arg) { ... }
/* ... */#endif
/* ... */
#endif