57 #define SET_NONBLOCKING_CONNECT(conn, val) do { if(val) { \
58 (conn)->flags |= NETCONN_FLAG_IN_NONBLOCKING_CONNECT; \
60 (conn)->flags &= ~ NETCONN_FLAG_IN_NONBLOCKING_CONNECT; }} while(0)
61 #define IN_NONBLOCKING_CONNECT(conn) (((conn)->flags & NETCONN_FLAG_IN_NONBLOCKING_CONNECT) != 0)
65 static err_t do_writemore(
struct netconn *conn);
66 static void do_close_internal(
struct netconn *conn);
78 recv_raw(
void *arg,
struct raw_pcb *pcb,
struct pbuf *p,
86 conn = (
struct netconn *)arg;
92 if ((recv_avail + (
int)(p->
tot_len)) > conn->recv_bufsize) {
116 buf->
port = pcb->protocol;
127 API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
144 recv_udp(
void *arg,
struct udp_pcb *pcb,
struct pbuf *p,
148 struct netconn *conn;
157 conn = (
struct netconn *)arg;
158 LWIP_ASSERT(
"recv_udp: recv for wrong pcb!", conn->pcb.udp == pcb);
163 ((recv_avail + (
int)(p->
tot_len)) > conn->recv_bufsize)) {
180 #if LWIP_NETBUF_RECVINFO
184 const struct udp_hdr* udphdr = (
void*)(((
char*)iphdr) +
IPH_LEN(iphdr));
185 #if LWIP_CHECKSUM_ON_COPY
189 buf->toport_chksum = udphdr->dest;
203 API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
216 recv_tcp(
void *arg,
struct tcp_pcb *pcb,
struct pbuf *p,
err_t err)
218 struct netconn *conn;
224 conn = (
struct netconn *)arg;
225 LWIP_ASSERT(
"recv_tcp: recv for wrong pcb!", conn->pcb.tcp == pcb);
243 NETCONN_SET_SAFE_ERR(conn, err);
259 API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
277 poll_tcp(
void *arg,
struct tcp_pcb *pcb)
279 struct netconn *conn = (
struct netconn *)arg;
284 if (conn->state == NETCONN_WRITE) {
286 }
else if (conn->state == NETCONN_CLOSE) {
287 do_close_internal(conn);
292 if (conn->flags & NETCONN_FLAG_CHECK_WRITESPACE) {
295 if ((conn->pcb.tcp !=
NULL) && (tcp_sndbuf(conn->pcb.tcp) >
TCP_SNDLOWAT) &&
297 conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE;
298 API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
313 sent_tcp(
void *arg,
struct tcp_pcb *pcb,
u16_t len)
315 struct netconn *conn = (
struct netconn *)arg;
320 if (conn->state == NETCONN_WRITE) {
322 }
else if (conn->state == NETCONN_CLOSE) {
323 do_close_internal(conn);
329 if ((conn->pcb.tcp !=
NULL) && (tcp_sndbuf(conn->pcb.tcp) >
TCP_SNDLOWAT) &&
331 conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE;
332 API_EVENT(conn, NETCONN_EVT_SENDPLUS, len);
347 err_tcp(
void *arg,
err_t err)
349 struct netconn *conn;
350 enum netconn_state old_state;
353 conn = (
struct netconn *)arg;
356 conn->pcb.tcp =
NULL;
360 conn->last_err = err;
364 old_state = conn->state;
365 conn->state = NETCONN_NONE;
369 API_EVENT(conn, NETCONN_EVT_ERROR, 0);
372 API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
373 API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
386 if ((old_state == NETCONN_WRITE) || (old_state == NETCONN_CLOSE) ||
387 (old_state == NETCONN_CONNECT)) {
390 int was_nonblocking_connect = IN_NONBLOCKING_CONNECT(conn);
391 SET_NONBLOCKING_CONNECT(conn, 0);
393 if (!was_nonblocking_connect) {
396 conn->current_msg->err = err;
397 conn->current_msg =
NULL;
413 setup_tcp(
struct netconn *conn)
419 tcp_recv(pcb, recv_tcp);
420 tcp_sent(pcb, sent_tcp);
421 tcp_poll(pcb, poll_tcp, 4);
422 tcp_err(pcb, err_tcp);
432 accept_function(
void *arg,
struct tcp_pcb *newpcb,
err_t err)
434 struct netconn *newconn;
435 struct netconn *conn = (
struct netconn *)arg;
446 newconn = netconn_alloc(conn->type, conn->callback);
447 if (newconn ==
NULL) {
450 newconn->pcb.tcp = newpcb;
454 newconn->last_err = err;
459 newconn->pcb.tcp =
NULL;
463 netconn_free(newconn);
467 API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
482 pcb_new(
struct api_msg_msg *msg)
484 LWIP_ASSERT(
"pcb_new: pcb already allocated", msg->conn->pcb.tcp ==
NULL);
487 switch(NETCONNTYPE_GROUP(msg->conn->type)) {
490 msg->conn->pcb.raw = raw_new(msg->msg.n.proto);
491 if(msg->conn->pcb.raw ==
NULL) {
495 raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
500 msg->conn->pcb.udp = udp_new();
501 if(msg->conn->pcb.udp ==
NULL) {
506 if (msg->conn->type==NETCONN_UDPLITE) {
507 udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
510 if (msg->conn->type==NETCONN_UDPNOCHKSUM) {
511 udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
513 udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
518 msg->conn->pcb.tcp = tcp_new();
519 if(msg->conn->pcb.tcp ==
NULL) {
523 setup_tcp(msg->conn);
540 do_newconn(
struct api_msg_msg *msg)
543 if(msg->conn->pcb.tcp ==
NULL) {
564 netconn_alloc(
enum netconn_type t, netconn_callback callback)
566 struct netconn *conn;
569 conn = (
struct netconn *)
memp_malloc(MEMP_NETCONN);
576 conn->pcb.tcp =
NULL;
578 #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
579 (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
582 switch(NETCONNTYPE_GROUP(t)) {
599 LWIP_ASSERT(
"netconn_alloc: undefined netconn_type", 0);
617 conn->state = NETCONN_NONE;
622 conn->callback = callback;
624 conn->current_msg =
NULL;
625 conn->write_offset = 0;
628 conn->recv_timeout = 0;
632 conn->recv_avail = 0;
645 netconn_free(
struct netconn *conn)
647 LWIP_ASSERT(
"PCB must be deallocated outside this function", conn->pcb.tcp ==
NULL);
648 LWIP_ASSERT(
"recvmbox must be deallocated before calling this function",
651 LWIP_ASSERT(
"acceptmbox must be deallocated before calling this function",
670 netconn_drain(
struct netconn *conn)
683 if (conn->type == NETCONN_TCP) {
685 p = (
struct pbuf*)mem;
687 if (conn->pcb.tcp !=
NULL) {
688 tcp_recved(conn->pcb.tcp, p->
tot_len);
706 struct netconn *newconn = (
struct netconn *)mem;
709 if (conn->pcb.tcp !=
NULL) {
710 tcp_accepted(conn->pcb.tcp);
713 netconn_drain(newconn);
714 if (newconn->pcb.tcp !=
NULL) {
715 tcp_abort(newconn->pcb.tcp);
716 newconn->pcb.tcp =
NULL;
718 netconn_free(newconn);
735 do_close_internal(
struct netconn *conn)
741 LWIP_ASSERT(
"this is for tcp netconns only", (conn->type == NETCONN_TCP));
742 LWIP_ASSERT(
"conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE));
746 shut = conn->current_msg->msg.sd.shut;
747 shut_rx = shut & NETCONN_SHUT_RD;
748 shut_tx = shut & NETCONN_SHUT_WR;
750 close = shut == NETCONN_SHUT_RDWR;
754 tcp_arg(conn->pcb.tcp,
NULL);
756 if (conn->pcb.tcp->state == LISTEN) {
757 tcp_accept(conn->pcb.tcp,
NULL);
761 tcp_recv(conn->pcb.tcp,
NULL);
762 tcp_accept(conn->pcb.tcp,
NULL);
765 tcp_sent(conn->pcb.tcp,
NULL);
768 tcp_poll(conn->pcb.tcp,
NULL, 4);
769 tcp_err(conn->pcb.tcp,
NULL);
773 if (shut == NETCONN_SHUT_RDWR) {
774 err = tcp_close(conn->pcb.tcp);
776 err = tcp_shutdown(conn->pcb.tcp, shut & NETCONN_SHUT_RD, shut & NETCONN_SHUT_WR);
780 conn->current_msg->err =
ERR_OK;
781 conn->current_msg =
NULL;
782 conn->state = NETCONN_NONE;
784 conn->pcb.tcp =
NULL;
788 API_EVENT(conn, NETCONN_EVT_ERROR, 0);
791 API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
794 API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
801 LWIP_ASSERT(
"Closing a listen pcb may not fail!", (conn->pcb.tcp->state != LISTEN));
802 tcp_sent(conn->pcb.tcp, sent_tcp);
803 tcp_poll(conn->pcb.tcp, poll_tcp, 4);
804 tcp_err(conn->pcb.tcp, err_tcp);
805 tcp_arg(conn->pcb.tcp, conn);
820 do_delconn(
struct api_msg_msg *msg)
823 if ((msg->conn->state != NETCONN_NONE) &&
824 (msg->conn->state != NETCONN_LISTEN) &&
825 (msg->conn->state != NETCONN_CONNECT)) {
827 LWIP_ASSERT(
"msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
831 (msg->conn->state != NETCONN_CONNECT) || IN_NONBLOCKING_CONNECT(msg->conn));
833 netconn_drain(msg->conn);
835 if (msg->conn->pcb.tcp !=
NULL) {
837 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
840 raw_remove(msg->conn->pcb.raw);
845 msg->conn->pcb.udp->recv_arg =
NULL;
846 udp_remove(msg->conn->pcb.udp);
851 LWIP_ASSERT(
"already writing or closing", msg->conn->current_msg ==
NULL &&
852 msg->conn->write_offset == 0);
853 msg->conn->state = NETCONN_CLOSE;
854 msg->msg.sd.shut = NETCONN_SHUT_RDWR;
855 msg->conn->current_msg = msg;
856 do_close_internal(msg->conn);
864 msg->conn->pcb.tcp =
NULL;
870 API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
871 API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
886 do_bind(
struct api_msg_msg *msg)
889 msg->err = msg->conn->last_err;
892 if (msg->conn->pcb.tcp !=
NULL) {
893 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
896 msg->err = raw_bind(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
901 msg->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
906 msg->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port);
925 do_connected(
void *arg,
struct tcp_pcb *pcb,
err_t err)
927 struct netconn *conn;
932 conn = (
struct netconn *)arg;
938 LWIP_ASSERT(
"conn->state == NETCONN_CONNECT", conn->state == NETCONN_CONNECT);
939 LWIP_ASSERT(
"(conn->current_msg != NULL) || conn->in_non_blocking_connect",
940 (conn->current_msg !=
NULL) || IN_NONBLOCKING_CONNECT(conn));
942 if (conn->current_msg !=
NULL) {
943 conn->current_msg->err = err;
945 if ((conn->type == NETCONN_TCP) && (err ==
ERR_OK)) {
948 was_blocking = !IN_NONBLOCKING_CONNECT(conn);
949 SET_NONBLOCKING_CONNECT(conn, 0);
950 conn->current_msg =
NULL;
951 conn->state = NETCONN_NONE;
960 API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
977 do_connect(
struct api_msg_msg *msg)
979 if (msg->conn->pcb.tcp ==
NULL) {
983 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
986 msg->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
991 msg->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
997 if (msg->conn->state != NETCONN_NONE) {
1000 setup_tcp(msg->conn);
1001 msg->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr,
1002 msg->msg.bc.port, do_connected);
1003 if (msg->err ==
ERR_OK) {
1004 u8_t non_blocking = netconn_is_nonblocking(msg->conn);
1005 msg->conn->state = NETCONN_CONNECT;
1006 SET_NONBLOCKING_CONNECT(msg->conn, non_blocking);
1010 msg->conn->current_msg = msg;
1035 do_disconnect(
struct api_msg_msg *msg)
1038 if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
1039 udp_disconnect(msg->conn->pcb.udp);
1057 do_listen(
struct api_msg_msg *msg)
1060 msg->err = msg->conn->last_err;
1063 if (msg->conn->pcb.tcp !=
NULL) {
1064 if (msg->conn->type == NETCONN_TCP) {
1065 if (msg->conn->state == NETCONN_NONE) {
1066 #if TCP_LISTEN_BACKLOG
1067 struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
1069 struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
1085 if (msg->err ==
ERR_OK) {
1086 msg->conn->state = NETCONN_LISTEN;
1087 msg->conn->pcb.tcp = lpcb;
1088 tcp_arg(msg->conn->pcb.tcp, msg->conn);
1089 tcp_accept(msg->conn->pcb.tcp, accept_function);
1093 msg->conn->pcb.tcp =
NULL;
1111 do_send(
struct api_msg_msg *msg)
1114 msg->err = msg->conn->last_err;
1117 if (msg->conn->pcb.tcp !=
NULL) {
1118 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
1122 msg->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
1124 msg->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, &msg->msg.b->addr);
1130 #if LWIP_CHECKSUM_ON_COPY
1132 msg->err = udp_send_chksum(msg->conn->pcb.udp, msg->msg.b->p,
1135 msg->err = udp_sendto_chksum(msg->conn->pcb.udp, msg->msg.b->p,
1136 &msg->msg.b->addr, msg->msg.b->port,
1141 msg->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
1143 msg->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, &msg->msg.b->addr, msg->msg.b->port);
1164 do_recv(
struct api_msg_msg *msg)
1167 if (msg->conn->pcb.tcp !=
NULL) {
1168 if (msg->conn->type == NETCONN_TCP) {
1169 #if TCP_LISTEN_BACKLOG
1170 if (msg->conn->pcb.tcp->state == LISTEN) {
1171 tcp_accepted(msg->conn->pcb.tcp);
1175 u32_t remaining = msg->msg.r.len;
1177 u16_t recved = (remaining > 0xffff) ? 0xffff : (
u16_t)remaining;
1178 tcp_recved(msg->conn->pcb.tcp, recved);
1179 remaining -= recved;
1180 }
while(remaining != 0);
1199 do_writemore(
struct netconn *conn)
1203 u16_t len, available;
1204 u8_t write_finished = 0;
1206 u8_t dontblock = netconn_is_nonblocking(conn) ||
1207 (conn->current_msg->msg.w.apiflags & NETCONN_DONTBLOCK);
1208 u8_t apiflags = conn->current_msg->msg.w.apiflags;
1211 LWIP_ASSERT(
"conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
1214 LWIP_ASSERT(
"conn->write_offset < conn->current_msg->msg.w.len",
1215 conn->write_offset < conn->current_msg->msg.w.len);
1217 dataptr = (
u8_t*)conn->current_msg->msg.w.dataptr + conn->write_offset;
1218 diff = conn->current_msg->msg.w.len - conn->write_offset;
1219 if (diff > 0xffffUL) {
1221 #if LWIP_TCPIP_CORE_LOCKING
1222 conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1224 apiflags |= TCP_WRITE_FLAG_MORE;
1228 available = tcp_sndbuf(conn->pcb.tcp);
1229 if (available < len) {
1232 #if LWIP_TCPIP_CORE_LOCKING
1233 conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1235 apiflags |= TCP_WRITE_FLAG_MORE;
1237 if (dontblock && (len < conn->current_msg->msg.w.len)) {
1242 LWIP_ASSERT(
"do_writemore: invalid length!", ((conn->write_offset + len) <= conn->current_msg->msg.w.len));
1243 err = tcp_write(conn->pcb.tcp, dataptr, len, apiflags);
1245 if (dontblock && (err ==
ERR_MEM)) {
1251 conn->flags |= NETCONN_FLAG_CHECK_WRITESPACE;
1253 API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
1261 API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
1265 conn->write_offset += len;
1266 if (conn->write_offset == conn->current_msg->msg.w.len) {
1269 conn->write_offset = 0;
1271 tcp_output(conn->pcb.tcp);
1278 tcp_output(conn->pcb.tcp);
1280 #if LWIP_TCPIP_CORE_LOCKING
1281 conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1290 if (write_finished) {
1293 conn->current_msg->err = err;
1294 conn->current_msg =
NULL;
1295 conn->state = NETCONN_NONE;
1296 #if LWIP_TCPIP_CORE_LOCKING
1297 if ((conn->flags & NETCONN_FLAG_WRITE_DELAYED) != 0)
1303 #if LWIP_TCPIP_CORE_LOCKING
1318 do_write(
struct api_msg_msg *msg)
1321 msg->err = msg->conn->last_err;
1323 if (msg->conn->type == NETCONN_TCP) {
1325 if (msg->conn->state != NETCONN_NONE) {
1328 }
else if (msg->conn->pcb.tcp !=
NULL) {
1329 msg->conn->state = NETCONN_WRITE;
1331 LWIP_ASSERT(
"already writing or closing", msg->conn->current_msg ==
NULL &&
1332 msg->conn->write_offset == 0);
1333 LWIP_ASSERT(
"msg->msg.w.len != 0", msg->msg.w.len != 0);
1334 msg->conn->current_msg = msg;
1335 msg->conn->write_offset = 0;
1336 #if LWIP_TCPIP_CORE_LOCKING
1337 msg->conn->flags &= ~NETCONN_FLAG_WRITE_DELAYED;
1338 if (do_writemore(msg->conn) !=
ERR_OK) {
1339 LWIP_ASSERT(
"state!", msg->conn->state == NETCONN_WRITE);
1343 LWIP_ASSERT(
"state!", msg->conn->state == NETCONN_NONE);
1346 do_writemore(msg->conn);
1357 #if (LWIP_UDP || LWIP_RAW)
1373 do_getaddr(
struct api_msg_msg *msg)
1375 if (msg->conn->pcb.ip !=
NULL) {
1376 *(msg->msg.ad.ipaddr) = (msg->msg.ad.local ? msg->conn->pcb.ip->local_ip :
1377 msg->conn->pcb.ip->remote_ip);
1380 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
1383 if (msg->msg.ad.local) {
1384 *(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
1393 if (msg->msg.ad.local) {
1394 *(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
1396 if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
1399 *(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
1406 *(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port);
1426 do_close(
struct api_msg_msg *msg)
1430 if ((msg->conn->state != NETCONN_NONE) && (msg->conn->state != NETCONN_LISTEN)) {
1432 LWIP_ASSERT(
"msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
1434 }
else if ((msg->conn->pcb.tcp !=
NULL) && (msg->conn->type == NETCONN_TCP)) {
1435 if ((msg->msg.sd.shut != NETCONN_SHUT_RDWR) && (msg->conn->state == NETCONN_LISTEN)) {
1439 if (msg->msg.sd.shut & NETCONN_SHUT_RD) {
1441 netconn_drain(msg->conn);
1443 LWIP_ASSERT(
"already writing or closing", msg->conn->current_msg ==
NULL &&
1444 msg->conn->write_offset == 0);
1445 msg->conn->state = NETCONN_CLOSE;
1446 msg->conn->current_msg = msg;
1447 do_close_internal(msg->conn);
1467 do_join_leave_group(
struct api_msg_msg *msg)
1470 msg->err = msg->conn->last_err;
1472 if (msg->conn->pcb.tcp !=
NULL) {
1473 if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
1475 if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
1476 msg->err = igmp_joingroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
1478 msg->err = igmp_leavegroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
1481 #if (LWIP_TCP || LWIP_RAW)
1501 do_dns_found(
const char *name,
ip_addr_t *ipaddr,
void *arg)
1503 struct dns_api_msg *msg = (
struct dns_api_msg*)arg;
1505 LWIP_ASSERT(
"DNS response for wrong host name", strcmp(msg->name, name) == 0);
1508 if (ipaddr ==
NULL) {
1514 *msg->addr = *ipaddr;
1527 do_gethostbyname(
void *arg)
1529 struct dns_api_msg *msg = (
struct dns_api_msg*)arg;
1531 *msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg);