Onenet
Dependents: K64F_eCompass_OneNET_JW
lwip/api/api_msg.c@0:b2805b6888dc, 2016-06-20 (annotated)
- Committer:
- robert_jw
- Date:
- Mon Jun 20 01:40:20 2016 +0000
- Revision:
- 0:b2805b6888dc
ADS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robert_jw | 0:b2805b6888dc | 1 | /** |
robert_jw | 0:b2805b6888dc | 2 | * @file |
robert_jw | 0:b2805b6888dc | 3 | * Sequential API Internal module |
robert_jw | 0:b2805b6888dc | 4 | * |
robert_jw | 0:b2805b6888dc | 5 | */ |
robert_jw | 0:b2805b6888dc | 6 | |
robert_jw | 0:b2805b6888dc | 7 | /* |
robert_jw | 0:b2805b6888dc | 8 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
robert_jw | 0:b2805b6888dc | 9 | * All rights reserved. |
robert_jw | 0:b2805b6888dc | 10 | * |
robert_jw | 0:b2805b6888dc | 11 | * Redistribution and use in source and binary forms, with or without modification, |
robert_jw | 0:b2805b6888dc | 12 | * are permitted provided that the following conditions are met: |
robert_jw | 0:b2805b6888dc | 13 | * |
robert_jw | 0:b2805b6888dc | 14 | * 1. Redistributions of source code must retain the above copyright notice, |
robert_jw | 0:b2805b6888dc | 15 | * this list of conditions and the following disclaimer. |
robert_jw | 0:b2805b6888dc | 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
robert_jw | 0:b2805b6888dc | 17 | * this list of conditions and the following disclaimer in the documentation |
robert_jw | 0:b2805b6888dc | 18 | * and/or other materials provided with the distribution. |
robert_jw | 0:b2805b6888dc | 19 | * 3. The name of the author may not be used to endorse or promote products |
robert_jw | 0:b2805b6888dc | 20 | * derived from this software without specific prior written permission. |
robert_jw | 0:b2805b6888dc | 21 | * |
robert_jw | 0:b2805b6888dc | 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
robert_jw | 0:b2805b6888dc | 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
robert_jw | 0:b2805b6888dc | 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
robert_jw | 0:b2805b6888dc | 25 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
robert_jw | 0:b2805b6888dc | 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
robert_jw | 0:b2805b6888dc | 27 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
robert_jw | 0:b2805b6888dc | 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
robert_jw | 0:b2805b6888dc | 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
robert_jw | 0:b2805b6888dc | 30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
robert_jw | 0:b2805b6888dc | 31 | * OF SUCH DAMAGE. |
robert_jw | 0:b2805b6888dc | 32 | * |
robert_jw | 0:b2805b6888dc | 33 | * This file is part of the lwIP TCP/IP stack. |
robert_jw | 0:b2805b6888dc | 34 | * |
robert_jw | 0:b2805b6888dc | 35 | * Author: Adam Dunkels <adam@sics.se> |
robert_jw | 0:b2805b6888dc | 36 | * |
robert_jw | 0:b2805b6888dc | 37 | */ |
robert_jw | 0:b2805b6888dc | 38 | |
robert_jw | 0:b2805b6888dc | 39 | #include "lwip/opt.h" |
robert_jw | 0:b2805b6888dc | 40 | |
robert_jw | 0:b2805b6888dc | 41 | #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */ |
robert_jw | 0:b2805b6888dc | 42 | |
robert_jw | 0:b2805b6888dc | 43 | #include "lwip/api_msg.h" |
robert_jw | 0:b2805b6888dc | 44 | |
robert_jw | 0:b2805b6888dc | 45 | #include "lwip/ip.h" |
robert_jw | 0:b2805b6888dc | 46 | #include "lwip/udp.h" |
robert_jw | 0:b2805b6888dc | 47 | #include "lwip/tcp.h" |
robert_jw | 0:b2805b6888dc | 48 | #include "lwip/raw.h" |
robert_jw | 0:b2805b6888dc | 49 | |
robert_jw | 0:b2805b6888dc | 50 | #include "lwip/memp.h" |
robert_jw | 0:b2805b6888dc | 51 | #include "lwip/tcpip.h" |
robert_jw | 0:b2805b6888dc | 52 | #include "lwip/igmp.h" |
robert_jw | 0:b2805b6888dc | 53 | #include "lwip/dns.h" |
robert_jw | 0:b2805b6888dc | 54 | |
robert_jw | 0:b2805b6888dc | 55 | #include <string.h> |
robert_jw | 0:b2805b6888dc | 56 | |
robert_jw | 0:b2805b6888dc | 57 | #define SET_NONBLOCKING_CONNECT(conn, val) do { if(val) { \ |
robert_jw | 0:b2805b6888dc | 58 | (conn)->flags |= NETCONN_FLAG_IN_NONBLOCKING_CONNECT; \ |
robert_jw | 0:b2805b6888dc | 59 | } else { \ |
robert_jw | 0:b2805b6888dc | 60 | (conn)->flags &= ~ NETCONN_FLAG_IN_NONBLOCKING_CONNECT; }} while(0) |
robert_jw | 0:b2805b6888dc | 61 | #define IN_NONBLOCKING_CONNECT(conn) (((conn)->flags & NETCONN_FLAG_IN_NONBLOCKING_CONNECT) != 0) |
robert_jw | 0:b2805b6888dc | 62 | |
robert_jw | 0:b2805b6888dc | 63 | /* forward declarations */ |
robert_jw | 0:b2805b6888dc | 64 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 65 | static err_t do_writemore(struct netconn *conn); |
robert_jw | 0:b2805b6888dc | 66 | static void do_close_internal(struct netconn *conn); |
robert_jw | 0:b2805b6888dc | 67 | #endif |
robert_jw | 0:b2805b6888dc | 68 | |
robert_jw | 0:b2805b6888dc | 69 | #if LWIP_RAW |
robert_jw | 0:b2805b6888dc | 70 | /** |
robert_jw | 0:b2805b6888dc | 71 | * Receive callback function for RAW netconns. |
robert_jw | 0:b2805b6888dc | 72 | * Doesn't 'eat' the packet, only references it and sends it to |
robert_jw | 0:b2805b6888dc | 73 | * conn->recvmbox |
robert_jw | 0:b2805b6888dc | 74 | * |
robert_jw | 0:b2805b6888dc | 75 | * @see raw.h (struct raw_pcb.recv) for parameters and return value |
robert_jw | 0:b2805b6888dc | 76 | */ |
robert_jw | 0:b2805b6888dc | 77 | static u8_t |
robert_jw | 0:b2805b6888dc | 78 | recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p, |
robert_jw | 0:b2805b6888dc | 79 | ip_addr_t *addr) |
robert_jw | 0:b2805b6888dc | 80 | { |
robert_jw | 0:b2805b6888dc | 81 | struct pbuf *q; |
robert_jw | 0:b2805b6888dc | 82 | struct netbuf *buf; |
robert_jw | 0:b2805b6888dc | 83 | struct netconn *conn; |
robert_jw | 0:b2805b6888dc | 84 | |
robert_jw | 0:b2805b6888dc | 85 | LWIP_UNUSED_ARG(addr); |
robert_jw | 0:b2805b6888dc | 86 | conn = (struct netconn *)arg; |
robert_jw | 0:b2805b6888dc | 87 | |
robert_jw | 0:b2805b6888dc | 88 | if ((conn != NULL) && sys_mbox_valid(&conn->recvmbox)) { |
robert_jw | 0:b2805b6888dc | 89 | #if LWIP_SO_RCVBUF |
robert_jw | 0:b2805b6888dc | 90 | int recv_avail; |
robert_jw | 0:b2805b6888dc | 91 | SYS_ARCH_GET(conn->recv_avail, recv_avail); |
robert_jw | 0:b2805b6888dc | 92 | if ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize) { |
robert_jw | 0:b2805b6888dc | 93 | return 0; |
robert_jw | 0:b2805b6888dc | 94 | } |
robert_jw | 0:b2805b6888dc | 95 | #endif /* LWIP_SO_RCVBUF */ |
robert_jw | 0:b2805b6888dc | 96 | /* copy the whole packet into new pbufs */ |
robert_jw | 0:b2805b6888dc | 97 | q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM); |
robert_jw | 0:b2805b6888dc | 98 | if(q != NULL) { |
robert_jw | 0:b2805b6888dc | 99 | if (pbuf_copy(q, p) != ERR_OK) { |
robert_jw | 0:b2805b6888dc | 100 | pbuf_free(q); |
robert_jw | 0:b2805b6888dc | 101 | q = NULL; |
robert_jw | 0:b2805b6888dc | 102 | } |
robert_jw | 0:b2805b6888dc | 103 | } |
robert_jw | 0:b2805b6888dc | 104 | |
robert_jw | 0:b2805b6888dc | 105 | if (q != NULL) { |
robert_jw | 0:b2805b6888dc | 106 | u16_t len; |
robert_jw | 0:b2805b6888dc | 107 | buf = (struct netbuf *)memp_malloc(MEMP_NETBUF); |
robert_jw | 0:b2805b6888dc | 108 | if (buf == NULL) { |
robert_jw | 0:b2805b6888dc | 109 | pbuf_free(q); |
robert_jw | 0:b2805b6888dc | 110 | return 0; |
robert_jw | 0:b2805b6888dc | 111 | } |
robert_jw | 0:b2805b6888dc | 112 | |
robert_jw | 0:b2805b6888dc | 113 | buf->p = q; |
robert_jw | 0:b2805b6888dc | 114 | buf->ptr = q; |
robert_jw | 0:b2805b6888dc | 115 | ip_addr_copy(buf->addr, *ip_current_src_addr()); |
robert_jw | 0:b2805b6888dc | 116 | buf->port = pcb->protocol; |
robert_jw | 0:b2805b6888dc | 117 | |
robert_jw | 0:b2805b6888dc | 118 | len = q->tot_len; |
robert_jw | 0:b2805b6888dc | 119 | if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) { |
robert_jw | 0:b2805b6888dc | 120 | netbuf_delete(buf); |
robert_jw | 0:b2805b6888dc | 121 | return 0; |
robert_jw | 0:b2805b6888dc | 122 | } else { |
robert_jw | 0:b2805b6888dc | 123 | #if LWIP_SO_RCVBUF |
robert_jw | 0:b2805b6888dc | 124 | SYS_ARCH_INC(conn->recv_avail, len); |
robert_jw | 0:b2805b6888dc | 125 | #endif /* LWIP_SO_RCVBUF */ |
robert_jw | 0:b2805b6888dc | 126 | /* Register event with callback */ |
robert_jw | 0:b2805b6888dc | 127 | API_EVENT(conn, NETCONN_EVT_RCVPLUS, len); |
robert_jw | 0:b2805b6888dc | 128 | } |
robert_jw | 0:b2805b6888dc | 129 | } |
robert_jw | 0:b2805b6888dc | 130 | } |
robert_jw | 0:b2805b6888dc | 131 | |
robert_jw | 0:b2805b6888dc | 132 | return 0; /* do not eat the packet */ |
robert_jw | 0:b2805b6888dc | 133 | } |
robert_jw | 0:b2805b6888dc | 134 | #endif /* LWIP_RAW*/ |
robert_jw | 0:b2805b6888dc | 135 | |
robert_jw | 0:b2805b6888dc | 136 | #if LWIP_UDP |
robert_jw | 0:b2805b6888dc | 137 | /** |
robert_jw | 0:b2805b6888dc | 138 | * Receive callback function for UDP netconns. |
robert_jw | 0:b2805b6888dc | 139 | * Posts the packet to conn->recvmbox or deletes it on memory error. |
robert_jw | 0:b2805b6888dc | 140 | * |
robert_jw | 0:b2805b6888dc | 141 | * @see udp.h (struct udp_pcb.recv) for parameters |
robert_jw | 0:b2805b6888dc | 142 | */ |
robert_jw | 0:b2805b6888dc | 143 | static void |
robert_jw | 0:b2805b6888dc | 144 | recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p, |
robert_jw | 0:b2805b6888dc | 145 | ip_addr_t *addr, u16_t port) |
robert_jw | 0:b2805b6888dc | 146 | { |
robert_jw | 0:b2805b6888dc | 147 | struct netbuf *buf; |
robert_jw | 0:b2805b6888dc | 148 | struct netconn *conn; |
robert_jw | 0:b2805b6888dc | 149 | u16_t len; |
robert_jw | 0:b2805b6888dc | 150 | #if LWIP_SO_RCVBUF |
robert_jw | 0:b2805b6888dc | 151 | int recv_avail; |
robert_jw | 0:b2805b6888dc | 152 | #endif /* LWIP_SO_RCVBUF */ |
robert_jw | 0:b2805b6888dc | 153 | |
robert_jw | 0:b2805b6888dc | 154 | LWIP_UNUSED_ARG(pcb); /* only used for asserts... */ |
robert_jw | 0:b2805b6888dc | 155 | LWIP_ASSERT("recv_udp must have a pcb argument", pcb != NULL); |
robert_jw | 0:b2805b6888dc | 156 | LWIP_ASSERT("recv_udp must have an argument", arg != NULL); |
robert_jw | 0:b2805b6888dc | 157 | conn = (struct netconn *)arg; |
robert_jw | 0:b2805b6888dc | 158 | LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn->pcb.udp == pcb); |
robert_jw | 0:b2805b6888dc | 159 | |
robert_jw | 0:b2805b6888dc | 160 | #if LWIP_SO_RCVBUF |
robert_jw | 0:b2805b6888dc | 161 | SYS_ARCH_GET(conn->recv_avail, recv_avail); |
robert_jw | 0:b2805b6888dc | 162 | if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox) || |
robert_jw | 0:b2805b6888dc | 163 | ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize)) { |
robert_jw | 0:b2805b6888dc | 164 | #else /* LWIP_SO_RCVBUF */ |
robert_jw | 0:b2805b6888dc | 165 | if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox)) { |
robert_jw | 0:b2805b6888dc | 166 | #endif /* LWIP_SO_RCVBUF */ |
robert_jw | 0:b2805b6888dc | 167 | pbuf_free(p); |
robert_jw | 0:b2805b6888dc | 168 | return; |
robert_jw | 0:b2805b6888dc | 169 | } |
robert_jw | 0:b2805b6888dc | 170 | |
robert_jw | 0:b2805b6888dc | 171 | buf = (struct netbuf *)memp_malloc(MEMP_NETBUF); |
robert_jw | 0:b2805b6888dc | 172 | if (buf == NULL) { |
robert_jw | 0:b2805b6888dc | 173 | pbuf_free(p); |
robert_jw | 0:b2805b6888dc | 174 | return; |
robert_jw | 0:b2805b6888dc | 175 | } else { |
robert_jw | 0:b2805b6888dc | 176 | buf->p = p; |
robert_jw | 0:b2805b6888dc | 177 | buf->ptr = p; |
robert_jw | 0:b2805b6888dc | 178 | ip_addr_set(&buf->addr, addr); |
robert_jw | 0:b2805b6888dc | 179 | buf->port = port; |
robert_jw | 0:b2805b6888dc | 180 | #if LWIP_NETBUF_RECVINFO |
robert_jw | 0:b2805b6888dc | 181 | { |
robert_jw | 0:b2805b6888dc | 182 | const struct ip_hdr* iphdr = ip_current_header(); |
robert_jw | 0:b2805b6888dc | 183 | /* get the UDP header - always in the first pbuf, ensured by udp_input */ |
robert_jw | 0:b2805b6888dc | 184 | const struct udp_hdr* udphdr = (void*)(((char*)iphdr) + IPH_LEN(iphdr)); |
robert_jw | 0:b2805b6888dc | 185 | #if LWIP_CHECKSUM_ON_COPY |
robert_jw | 0:b2805b6888dc | 186 | buf->flags = NETBUF_FLAG_DESTADDR; |
robert_jw | 0:b2805b6888dc | 187 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
robert_jw | 0:b2805b6888dc | 188 | ip_addr_set(&buf->toaddr, ip_current_dest_addr()); |
robert_jw | 0:b2805b6888dc | 189 | buf->toport_chksum = udphdr->dest; |
robert_jw | 0:b2805b6888dc | 190 | } |
robert_jw | 0:b2805b6888dc | 191 | #endif /* LWIP_NETBUF_RECVINFO */ |
robert_jw | 0:b2805b6888dc | 192 | } |
robert_jw | 0:b2805b6888dc | 193 | |
robert_jw | 0:b2805b6888dc | 194 | len = p->tot_len; |
robert_jw | 0:b2805b6888dc | 195 | if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) { |
robert_jw | 0:b2805b6888dc | 196 | netbuf_delete(buf); |
robert_jw | 0:b2805b6888dc | 197 | return; |
robert_jw | 0:b2805b6888dc | 198 | } else { |
robert_jw | 0:b2805b6888dc | 199 | #if LWIP_SO_RCVBUF |
robert_jw | 0:b2805b6888dc | 200 | SYS_ARCH_INC(conn->recv_avail, len); |
robert_jw | 0:b2805b6888dc | 201 | #endif /* LWIP_SO_RCVBUF */ |
robert_jw | 0:b2805b6888dc | 202 | /* Register event with callback */ |
robert_jw | 0:b2805b6888dc | 203 | API_EVENT(conn, NETCONN_EVT_RCVPLUS, len); |
robert_jw | 0:b2805b6888dc | 204 | } |
robert_jw | 0:b2805b6888dc | 205 | } |
robert_jw | 0:b2805b6888dc | 206 | #endif /* LWIP_UDP */ |
robert_jw | 0:b2805b6888dc | 207 | |
robert_jw | 0:b2805b6888dc | 208 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 209 | /** |
robert_jw | 0:b2805b6888dc | 210 | * Receive callback function for TCP netconns. |
robert_jw | 0:b2805b6888dc | 211 | * Posts the packet to conn->recvmbox, but doesn't delete it on errors. |
robert_jw | 0:b2805b6888dc | 212 | * |
robert_jw | 0:b2805b6888dc | 213 | * @see tcp.h (struct tcp_pcb.recv) for parameters and return value |
robert_jw | 0:b2805b6888dc | 214 | */ |
robert_jw | 0:b2805b6888dc | 215 | static err_t |
robert_jw | 0:b2805b6888dc | 216 | recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) |
robert_jw | 0:b2805b6888dc | 217 | { |
robert_jw | 0:b2805b6888dc | 218 | struct netconn *conn; |
robert_jw | 0:b2805b6888dc | 219 | u16_t len; |
robert_jw | 0:b2805b6888dc | 220 | |
robert_jw | 0:b2805b6888dc | 221 | LWIP_UNUSED_ARG(pcb); |
robert_jw | 0:b2805b6888dc | 222 | LWIP_ASSERT("recv_tcp must have a pcb argument", pcb != NULL); |
robert_jw | 0:b2805b6888dc | 223 | LWIP_ASSERT("recv_tcp must have an argument", arg != NULL); |
robert_jw | 0:b2805b6888dc | 224 | conn = (struct netconn *)arg; |
robert_jw | 0:b2805b6888dc | 225 | LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn->pcb.tcp == pcb); |
robert_jw | 0:b2805b6888dc | 226 | |
robert_jw | 0:b2805b6888dc | 227 | if (conn == NULL) { |
robert_jw | 0:b2805b6888dc | 228 | return ERR_VAL; |
robert_jw | 0:b2805b6888dc | 229 | } |
robert_jw | 0:b2805b6888dc | 230 | if (!sys_mbox_valid(&conn->recvmbox)) { |
robert_jw | 0:b2805b6888dc | 231 | /* recvmbox already deleted */ |
robert_jw | 0:b2805b6888dc | 232 | if (p != NULL) { |
robert_jw | 0:b2805b6888dc | 233 | tcp_recved(pcb, p->tot_len); |
robert_jw | 0:b2805b6888dc | 234 | pbuf_free(p); |
robert_jw | 0:b2805b6888dc | 235 | } |
robert_jw | 0:b2805b6888dc | 236 | return ERR_OK; |
robert_jw | 0:b2805b6888dc | 237 | } |
robert_jw | 0:b2805b6888dc | 238 | /* Unlike for UDP or RAW pcbs, don't check for available space |
robert_jw | 0:b2805b6888dc | 239 | using recv_avail since that could break the connection |
robert_jw | 0:b2805b6888dc | 240 | (data is already ACKed) */ |
robert_jw | 0:b2805b6888dc | 241 | |
robert_jw | 0:b2805b6888dc | 242 | /* don't overwrite fatal errors! */ |
robert_jw | 0:b2805b6888dc | 243 | NETCONN_SET_SAFE_ERR(conn, err); |
robert_jw | 0:b2805b6888dc | 244 | |
robert_jw | 0:b2805b6888dc | 245 | if (p != NULL) { |
robert_jw | 0:b2805b6888dc | 246 | len = p->tot_len; |
robert_jw | 0:b2805b6888dc | 247 | } else { |
robert_jw | 0:b2805b6888dc | 248 | len = 0; |
robert_jw | 0:b2805b6888dc | 249 | } |
robert_jw | 0:b2805b6888dc | 250 | |
robert_jw | 0:b2805b6888dc | 251 | if (sys_mbox_trypost(&conn->recvmbox, p) != ERR_OK) { |
robert_jw | 0:b2805b6888dc | 252 | /* don't deallocate p: it is presented to us later again from tcp_fasttmr! */ |
robert_jw | 0:b2805b6888dc | 253 | return ERR_MEM; |
robert_jw | 0:b2805b6888dc | 254 | } else { |
robert_jw | 0:b2805b6888dc | 255 | #if LWIP_SO_RCVBUF |
robert_jw | 0:b2805b6888dc | 256 | SYS_ARCH_INC(conn->recv_avail, len); |
robert_jw | 0:b2805b6888dc | 257 | #endif /* LWIP_SO_RCVBUF */ |
robert_jw | 0:b2805b6888dc | 258 | /* Register event with callback */ |
robert_jw | 0:b2805b6888dc | 259 | API_EVENT(conn, NETCONN_EVT_RCVPLUS, len); |
robert_jw | 0:b2805b6888dc | 260 | } |
robert_jw | 0:b2805b6888dc | 261 | |
robert_jw | 0:b2805b6888dc | 262 | return ERR_OK; |
robert_jw | 0:b2805b6888dc | 263 | } |
robert_jw | 0:b2805b6888dc | 264 | |
robert_jw | 0:b2805b6888dc | 265 | /** |
robert_jw | 0:b2805b6888dc | 266 | * Poll callback function for TCP netconns. |
robert_jw | 0:b2805b6888dc | 267 | * Wakes up an application thread that waits for a connection to close |
robert_jw | 0:b2805b6888dc | 268 | * or data to be sent. The application thread then takes the |
robert_jw | 0:b2805b6888dc | 269 | * appropriate action to go on. |
robert_jw | 0:b2805b6888dc | 270 | * |
robert_jw | 0:b2805b6888dc | 271 | * Signals the conn->sem. |
robert_jw | 0:b2805b6888dc | 272 | * netconn_close waits for conn->sem if closing failed. |
robert_jw | 0:b2805b6888dc | 273 | * |
robert_jw | 0:b2805b6888dc | 274 | * @see tcp.h (struct tcp_pcb.poll) for parameters and return value |
robert_jw | 0:b2805b6888dc | 275 | */ |
robert_jw | 0:b2805b6888dc | 276 | static err_t |
robert_jw | 0:b2805b6888dc | 277 | poll_tcp(void *arg, struct tcp_pcb *pcb) |
robert_jw | 0:b2805b6888dc | 278 | { |
robert_jw | 0:b2805b6888dc | 279 | struct netconn *conn = (struct netconn *)arg; |
robert_jw | 0:b2805b6888dc | 280 | |
robert_jw | 0:b2805b6888dc | 281 | LWIP_UNUSED_ARG(pcb); |
robert_jw | 0:b2805b6888dc | 282 | LWIP_ASSERT("conn != NULL", (conn != NULL)); |
robert_jw | 0:b2805b6888dc | 283 | |
robert_jw | 0:b2805b6888dc | 284 | if (conn->state == NETCONN_WRITE) { |
robert_jw | 0:b2805b6888dc | 285 | do_writemore(conn); |
robert_jw | 0:b2805b6888dc | 286 | } else if (conn->state == NETCONN_CLOSE) { |
robert_jw | 0:b2805b6888dc | 287 | do_close_internal(conn); |
robert_jw | 0:b2805b6888dc | 288 | } |
robert_jw | 0:b2805b6888dc | 289 | /* @todo: implement connect timeout here? */ |
robert_jw | 0:b2805b6888dc | 290 | |
robert_jw | 0:b2805b6888dc | 291 | /* Did a nonblocking write fail before? Then check available write-space. */ |
robert_jw | 0:b2805b6888dc | 292 | if (conn->flags & NETCONN_FLAG_CHECK_WRITESPACE) { |
robert_jw | 0:b2805b6888dc | 293 | /* If the queued byte- or pbuf-count drops below the configured low-water limit, |
robert_jw | 0:b2805b6888dc | 294 | let select mark this pcb as writable again. */ |
robert_jw | 0:b2805b6888dc | 295 | if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) && |
robert_jw | 0:b2805b6888dc | 296 | (tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) { |
robert_jw | 0:b2805b6888dc | 297 | conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE; |
robert_jw | 0:b2805b6888dc | 298 | API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0); |
robert_jw | 0:b2805b6888dc | 299 | } |
robert_jw | 0:b2805b6888dc | 300 | } |
robert_jw | 0:b2805b6888dc | 301 | |
robert_jw | 0:b2805b6888dc | 302 | return ERR_OK; |
robert_jw | 0:b2805b6888dc | 303 | } |
robert_jw | 0:b2805b6888dc | 304 | |
robert_jw | 0:b2805b6888dc | 305 | /** |
robert_jw | 0:b2805b6888dc | 306 | * Sent callback function for TCP netconns. |
robert_jw | 0:b2805b6888dc | 307 | * Signals the conn->sem and calls API_EVENT. |
robert_jw | 0:b2805b6888dc | 308 | * netconn_write waits for conn->sem if send buffer is low. |
robert_jw | 0:b2805b6888dc | 309 | * |
robert_jw | 0:b2805b6888dc | 310 | * @see tcp.h (struct tcp_pcb.sent) for parameters and return value |
robert_jw | 0:b2805b6888dc | 311 | */ |
robert_jw | 0:b2805b6888dc | 312 | static err_t |
robert_jw | 0:b2805b6888dc | 313 | sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len) |
robert_jw | 0:b2805b6888dc | 314 | { |
robert_jw | 0:b2805b6888dc | 315 | struct netconn *conn = (struct netconn *)arg; |
robert_jw | 0:b2805b6888dc | 316 | |
robert_jw | 0:b2805b6888dc | 317 | LWIP_UNUSED_ARG(pcb); |
robert_jw | 0:b2805b6888dc | 318 | LWIP_ASSERT("conn != NULL", (conn != NULL)); |
robert_jw | 0:b2805b6888dc | 319 | |
robert_jw | 0:b2805b6888dc | 320 | if (conn->state == NETCONN_WRITE) { |
robert_jw | 0:b2805b6888dc | 321 | do_writemore(conn); |
robert_jw | 0:b2805b6888dc | 322 | } else if (conn->state == NETCONN_CLOSE) { |
robert_jw | 0:b2805b6888dc | 323 | do_close_internal(conn); |
robert_jw | 0:b2805b6888dc | 324 | } |
robert_jw | 0:b2805b6888dc | 325 | |
robert_jw | 0:b2805b6888dc | 326 | if (conn) { |
robert_jw | 0:b2805b6888dc | 327 | /* If the queued byte- or pbuf-count drops below the configured low-water limit, |
robert_jw | 0:b2805b6888dc | 328 | let select mark this pcb as writable again. */ |
robert_jw | 0:b2805b6888dc | 329 | if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) && |
robert_jw | 0:b2805b6888dc | 330 | (tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) { |
robert_jw | 0:b2805b6888dc | 331 | conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE; |
robert_jw | 0:b2805b6888dc | 332 | API_EVENT(conn, NETCONN_EVT_SENDPLUS, len); |
robert_jw | 0:b2805b6888dc | 333 | } |
robert_jw | 0:b2805b6888dc | 334 | } |
robert_jw | 0:b2805b6888dc | 335 | |
robert_jw | 0:b2805b6888dc | 336 | return ERR_OK; |
robert_jw | 0:b2805b6888dc | 337 | } |
robert_jw | 0:b2805b6888dc | 338 | |
robert_jw | 0:b2805b6888dc | 339 | /** |
robert_jw | 0:b2805b6888dc | 340 | * Error callback function for TCP netconns. |
robert_jw | 0:b2805b6888dc | 341 | * Signals conn->sem, posts to all conn mboxes and calls API_EVENT. |
robert_jw | 0:b2805b6888dc | 342 | * The application thread has then to decide what to do. |
robert_jw | 0:b2805b6888dc | 343 | * |
robert_jw | 0:b2805b6888dc | 344 | * @see tcp.h (struct tcp_pcb.err) for parameters |
robert_jw | 0:b2805b6888dc | 345 | */ |
robert_jw | 0:b2805b6888dc | 346 | static void |
robert_jw | 0:b2805b6888dc | 347 | err_tcp(void *arg, err_t err) |
robert_jw | 0:b2805b6888dc | 348 | { |
robert_jw | 0:b2805b6888dc | 349 | struct netconn *conn; |
robert_jw | 0:b2805b6888dc | 350 | enum netconn_state old_state; |
robert_jw | 0:b2805b6888dc | 351 | SYS_ARCH_DECL_PROTECT(lev); |
robert_jw | 0:b2805b6888dc | 352 | |
robert_jw | 0:b2805b6888dc | 353 | conn = (struct netconn *)arg; |
robert_jw | 0:b2805b6888dc | 354 | LWIP_ASSERT("conn != NULL", (conn != NULL)); |
robert_jw | 0:b2805b6888dc | 355 | |
robert_jw | 0:b2805b6888dc | 356 | conn->pcb.tcp = NULL; |
robert_jw | 0:b2805b6888dc | 357 | |
robert_jw | 0:b2805b6888dc | 358 | /* no check since this is always fatal! */ |
robert_jw | 0:b2805b6888dc | 359 | SYS_ARCH_PROTECT(lev); |
robert_jw | 0:b2805b6888dc | 360 | conn->last_err = err; |
robert_jw | 0:b2805b6888dc | 361 | SYS_ARCH_UNPROTECT(lev); |
robert_jw | 0:b2805b6888dc | 362 | |
robert_jw | 0:b2805b6888dc | 363 | /* reset conn->state now before waking up other threads */ |
robert_jw | 0:b2805b6888dc | 364 | old_state = conn->state; |
robert_jw | 0:b2805b6888dc | 365 | conn->state = NETCONN_NONE; |
robert_jw | 0:b2805b6888dc | 366 | |
robert_jw | 0:b2805b6888dc | 367 | /* Notify the user layer about a connection error. Used to signal |
robert_jw | 0:b2805b6888dc | 368 | select. */ |
robert_jw | 0:b2805b6888dc | 369 | API_EVENT(conn, NETCONN_EVT_ERROR, 0); |
robert_jw | 0:b2805b6888dc | 370 | /* Try to release selects pending on 'read' or 'write', too. |
robert_jw | 0:b2805b6888dc | 371 | They will get an error if they actually try to read or write. */ |
robert_jw | 0:b2805b6888dc | 372 | API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0); |
robert_jw | 0:b2805b6888dc | 373 | API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0); |
robert_jw | 0:b2805b6888dc | 374 | |
robert_jw | 0:b2805b6888dc | 375 | /* pass NULL-message to recvmbox to wake up pending recv */ |
robert_jw | 0:b2805b6888dc | 376 | if (sys_mbox_valid(&conn->recvmbox)) { |
robert_jw | 0:b2805b6888dc | 377 | /* use trypost to prevent deadlock */ |
robert_jw | 0:b2805b6888dc | 378 | sys_mbox_trypost(&conn->recvmbox, NULL); |
robert_jw | 0:b2805b6888dc | 379 | } |
robert_jw | 0:b2805b6888dc | 380 | /* pass NULL-message to acceptmbox to wake up pending accept */ |
robert_jw | 0:b2805b6888dc | 381 | if (sys_mbox_valid(&conn->acceptmbox)) { |
robert_jw | 0:b2805b6888dc | 382 | /* use trypost to preven deadlock */ |
robert_jw | 0:b2805b6888dc | 383 | sys_mbox_trypost(&conn->acceptmbox, NULL); |
robert_jw | 0:b2805b6888dc | 384 | } |
robert_jw | 0:b2805b6888dc | 385 | |
robert_jw | 0:b2805b6888dc | 386 | if ((old_state == NETCONN_WRITE) || (old_state == NETCONN_CLOSE) || |
robert_jw | 0:b2805b6888dc | 387 | (old_state == NETCONN_CONNECT)) { |
robert_jw | 0:b2805b6888dc | 388 | /* calling do_writemore/do_close_internal is not necessary |
robert_jw | 0:b2805b6888dc | 389 | since the pcb has already been deleted! */ |
robert_jw | 0:b2805b6888dc | 390 | int was_nonblocking_connect = IN_NONBLOCKING_CONNECT(conn); |
robert_jw | 0:b2805b6888dc | 391 | SET_NONBLOCKING_CONNECT(conn, 0); |
robert_jw | 0:b2805b6888dc | 392 | |
robert_jw | 0:b2805b6888dc | 393 | if (!was_nonblocking_connect) { |
robert_jw | 0:b2805b6888dc | 394 | /* set error return code */ |
robert_jw | 0:b2805b6888dc | 395 | LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL); |
robert_jw | 0:b2805b6888dc | 396 | conn->current_msg->err = err; |
robert_jw | 0:b2805b6888dc | 397 | conn->current_msg = NULL; |
robert_jw | 0:b2805b6888dc | 398 | /* wake up the waiting task */ |
robert_jw | 0:b2805b6888dc | 399 | sys_sem_signal(&conn->op_completed); |
robert_jw | 0:b2805b6888dc | 400 | } |
robert_jw | 0:b2805b6888dc | 401 | } else { |
robert_jw | 0:b2805b6888dc | 402 | LWIP_ASSERT("conn->current_msg == NULL", conn->current_msg == NULL); |
robert_jw | 0:b2805b6888dc | 403 | } |
robert_jw | 0:b2805b6888dc | 404 | } |
robert_jw | 0:b2805b6888dc | 405 | |
robert_jw | 0:b2805b6888dc | 406 | /** |
robert_jw | 0:b2805b6888dc | 407 | * Setup a tcp_pcb with the correct callback function pointers |
robert_jw | 0:b2805b6888dc | 408 | * and their arguments. |
robert_jw | 0:b2805b6888dc | 409 | * |
robert_jw | 0:b2805b6888dc | 410 | * @param conn the TCP netconn to setup |
robert_jw | 0:b2805b6888dc | 411 | */ |
robert_jw | 0:b2805b6888dc | 412 | static void |
robert_jw | 0:b2805b6888dc | 413 | setup_tcp(struct netconn *conn) |
robert_jw | 0:b2805b6888dc | 414 | { |
robert_jw | 0:b2805b6888dc | 415 | struct tcp_pcb *pcb; |
robert_jw | 0:b2805b6888dc | 416 | |
robert_jw | 0:b2805b6888dc | 417 | pcb = conn->pcb.tcp; |
robert_jw | 0:b2805b6888dc | 418 | tcp_arg(pcb, conn); |
robert_jw | 0:b2805b6888dc | 419 | tcp_recv(pcb, recv_tcp); |
robert_jw | 0:b2805b6888dc | 420 | tcp_sent(pcb, sent_tcp); |
robert_jw | 0:b2805b6888dc | 421 | tcp_poll(pcb, poll_tcp, 4); |
robert_jw | 0:b2805b6888dc | 422 | tcp_err(pcb, err_tcp); |
robert_jw | 0:b2805b6888dc | 423 | } |
robert_jw | 0:b2805b6888dc | 424 | |
robert_jw | 0:b2805b6888dc | 425 | /** |
robert_jw | 0:b2805b6888dc | 426 | * Accept callback function for TCP netconns. |
robert_jw | 0:b2805b6888dc | 427 | * Allocates a new netconn and posts that to conn->acceptmbox. |
robert_jw | 0:b2805b6888dc | 428 | * |
robert_jw | 0:b2805b6888dc | 429 | * @see tcp.h (struct tcp_pcb_listen.accept) for parameters and return value |
robert_jw | 0:b2805b6888dc | 430 | */ |
robert_jw | 0:b2805b6888dc | 431 | static err_t |
robert_jw | 0:b2805b6888dc | 432 | accept_function(void *arg, struct tcp_pcb *newpcb, err_t err) |
robert_jw | 0:b2805b6888dc | 433 | { |
robert_jw | 0:b2805b6888dc | 434 | struct netconn *newconn; |
robert_jw | 0:b2805b6888dc | 435 | struct netconn *conn = (struct netconn *)arg; |
robert_jw | 0:b2805b6888dc | 436 | |
robert_jw | 0:b2805b6888dc | 437 | LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: newpcb->tate: %s\n", tcp_debug_state_str(newpcb->state))); |
robert_jw | 0:b2805b6888dc | 438 | |
robert_jw | 0:b2805b6888dc | 439 | if (!sys_mbox_valid(&conn->acceptmbox)) { |
robert_jw | 0:b2805b6888dc | 440 | LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: acceptmbox already deleted\n")); |
robert_jw | 0:b2805b6888dc | 441 | return ERR_VAL; |
robert_jw | 0:b2805b6888dc | 442 | } |
robert_jw | 0:b2805b6888dc | 443 | |
robert_jw | 0:b2805b6888dc | 444 | /* We have to set the callback here even though |
robert_jw | 0:b2805b6888dc | 445 | * the new socket is unknown. conn->socket is marked as -1. */ |
robert_jw | 0:b2805b6888dc | 446 | newconn = netconn_alloc(conn->type, conn->callback); |
robert_jw | 0:b2805b6888dc | 447 | if (newconn == NULL) { |
robert_jw | 0:b2805b6888dc | 448 | return ERR_MEM; |
robert_jw | 0:b2805b6888dc | 449 | } |
robert_jw | 0:b2805b6888dc | 450 | newconn->pcb.tcp = newpcb; |
robert_jw | 0:b2805b6888dc | 451 | setup_tcp(newconn); |
robert_jw | 0:b2805b6888dc | 452 | /* no protection: when creating the pcb, the netconn is not yet known |
robert_jw | 0:b2805b6888dc | 453 | to the application thread */ |
robert_jw | 0:b2805b6888dc | 454 | newconn->last_err = err; |
robert_jw | 0:b2805b6888dc | 455 | |
robert_jw | 0:b2805b6888dc | 456 | if (sys_mbox_trypost(&conn->acceptmbox, newconn) != ERR_OK) { |
robert_jw | 0:b2805b6888dc | 457 | /* When returning != ERR_OK, the pcb is aborted in tcp_process(), |
robert_jw | 0:b2805b6888dc | 458 | so do nothing here! */ |
robert_jw | 0:b2805b6888dc | 459 | newconn->pcb.tcp = NULL; |
robert_jw | 0:b2805b6888dc | 460 | /* no need to drain since we know the recvmbox is empty. */ |
robert_jw | 0:b2805b6888dc | 461 | sys_mbox_free(&newconn->recvmbox); |
robert_jw | 0:b2805b6888dc | 462 | sys_mbox_set_invalid(&newconn->recvmbox); |
robert_jw | 0:b2805b6888dc | 463 | netconn_free(newconn); |
robert_jw | 0:b2805b6888dc | 464 | return ERR_MEM; |
robert_jw | 0:b2805b6888dc | 465 | } else { |
robert_jw | 0:b2805b6888dc | 466 | /* Register event with callback */ |
robert_jw | 0:b2805b6888dc | 467 | API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0); |
robert_jw | 0:b2805b6888dc | 468 | } |
robert_jw | 0:b2805b6888dc | 469 | |
robert_jw | 0:b2805b6888dc | 470 | return ERR_OK; |
robert_jw | 0:b2805b6888dc | 471 | } |
robert_jw | 0:b2805b6888dc | 472 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 473 | |
robert_jw | 0:b2805b6888dc | 474 | /** |
robert_jw | 0:b2805b6888dc | 475 | * Create a new pcb of a specific type. |
robert_jw | 0:b2805b6888dc | 476 | * Called from do_newconn(). |
robert_jw | 0:b2805b6888dc | 477 | * |
robert_jw | 0:b2805b6888dc | 478 | * @param msg the api_msg_msg describing the connection type |
robert_jw | 0:b2805b6888dc | 479 | * @return msg->conn->err, but the return value is currently ignored |
robert_jw | 0:b2805b6888dc | 480 | */ |
robert_jw | 0:b2805b6888dc | 481 | static void |
robert_jw | 0:b2805b6888dc | 482 | pcb_new(struct api_msg_msg *msg) |
robert_jw | 0:b2805b6888dc | 483 | { |
robert_jw | 0:b2805b6888dc | 484 | LWIP_ASSERT("pcb_new: pcb already allocated", msg->conn->pcb.tcp == NULL); |
robert_jw | 0:b2805b6888dc | 485 | |
robert_jw | 0:b2805b6888dc | 486 | /* Allocate a PCB for this connection */ |
robert_jw | 0:b2805b6888dc | 487 | switch(NETCONNTYPE_GROUP(msg->conn->type)) { |
robert_jw | 0:b2805b6888dc | 488 | #if LWIP_RAW |
robert_jw | 0:b2805b6888dc | 489 | case NETCONN_RAW: |
robert_jw | 0:b2805b6888dc | 490 | msg->conn->pcb.raw = raw_new(msg->msg.n.proto); |
robert_jw | 0:b2805b6888dc | 491 | if(msg->conn->pcb.raw == NULL) { |
robert_jw | 0:b2805b6888dc | 492 | msg->err = ERR_MEM; |
robert_jw | 0:b2805b6888dc | 493 | break; |
robert_jw | 0:b2805b6888dc | 494 | } |
robert_jw | 0:b2805b6888dc | 495 | raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn); |
robert_jw | 0:b2805b6888dc | 496 | break; |
robert_jw | 0:b2805b6888dc | 497 | #endif /* LWIP_RAW */ |
robert_jw | 0:b2805b6888dc | 498 | #if LWIP_UDP |
robert_jw | 0:b2805b6888dc | 499 | case NETCONN_UDP: |
robert_jw | 0:b2805b6888dc | 500 | msg->conn->pcb.udp = udp_new(); |
robert_jw | 0:b2805b6888dc | 501 | if(msg->conn->pcb.udp == NULL) { |
robert_jw | 0:b2805b6888dc | 502 | msg->err = ERR_MEM; |
robert_jw | 0:b2805b6888dc | 503 | break; |
robert_jw | 0:b2805b6888dc | 504 | } |
robert_jw | 0:b2805b6888dc | 505 | #if LWIP_UDPLITE |
robert_jw | 0:b2805b6888dc | 506 | if (msg->conn->type==NETCONN_UDPLITE) { |
robert_jw | 0:b2805b6888dc | 507 | udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE); |
robert_jw | 0:b2805b6888dc | 508 | } |
robert_jw | 0:b2805b6888dc | 509 | #endif /* LWIP_UDPLITE */ |
robert_jw | 0:b2805b6888dc | 510 | if (msg->conn->type==NETCONN_UDPNOCHKSUM) { |
robert_jw | 0:b2805b6888dc | 511 | udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM); |
robert_jw | 0:b2805b6888dc | 512 | } |
robert_jw | 0:b2805b6888dc | 513 | udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn); |
robert_jw | 0:b2805b6888dc | 514 | break; |
robert_jw | 0:b2805b6888dc | 515 | #endif /* LWIP_UDP */ |
robert_jw | 0:b2805b6888dc | 516 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 517 | case NETCONN_TCP: |
robert_jw | 0:b2805b6888dc | 518 | msg->conn->pcb.tcp = tcp_new(); |
robert_jw | 0:b2805b6888dc | 519 | if(msg->conn->pcb.tcp == NULL) { |
robert_jw | 0:b2805b6888dc | 520 | msg->err = ERR_MEM; |
robert_jw | 0:b2805b6888dc | 521 | break; |
robert_jw | 0:b2805b6888dc | 522 | } |
robert_jw | 0:b2805b6888dc | 523 | setup_tcp(msg->conn); |
robert_jw | 0:b2805b6888dc | 524 | break; |
robert_jw | 0:b2805b6888dc | 525 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 526 | default: |
robert_jw | 0:b2805b6888dc | 527 | /* Unsupported netconn type, e.g. protocol disabled */ |
robert_jw | 0:b2805b6888dc | 528 | msg->err = ERR_VAL; |
robert_jw | 0:b2805b6888dc | 529 | break; |
robert_jw | 0:b2805b6888dc | 530 | } |
robert_jw | 0:b2805b6888dc | 531 | } |
robert_jw | 0:b2805b6888dc | 532 | |
robert_jw | 0:b2805b6888dc | 533 | /** |
robert_jw | 0:b2805b6888dc | 534 | * Create a new pcb of a specific type inside a netconn. |
robert_jw | 0:b2805b6888dc | 535 | * Called from netconn_new_with_proto_and_callback. |
robert_jw | 0:b2805b6888dc | 536 | * |
robert_jw | 0:b2805b6888dc | 537 | * @param msg the api_msg_msg describing the connection type |
robert_jw | 0:b2805b6888dc | 538 | */ |
robert_jw | 0:b2805b6888dc | 539 | void |
robert_jw | 0:b2805b6888dc | 540 | do_newconn(struct api_msg_msg *msg) |
robert_jw | 0:b2805b6888dc | 541 | { |
robert_jw | 0:b2805b6888dc | 542 | msg->err = ERR_OK; |
robert_jw | 0:b2805b6888dc | 543 | if(msg->conn->pcb.tcp == NULL) { |
robert_jw | 0:b2805b6888dc | 544 | pcb_new(msg); |
robert_jw | 0:b2805b6888dc | 545 | } |
robert_jw | 0:b2805b6888dc | 546 | /* Else? This "new" connection already has a PCB allocated. */ |
robert_jw | 0:b2805b6888dc | 547 | /* Is this an error condition? Should it be deleted? */ |
robert_jw | 0:b2805b6888dc | 548 | /* We currently just are happy and return. */ |
robert_jw | 0:b2805b6888dc | 549 | |
robert_jw | 0:b2805b6888dc | 550 | TCPIP_APIMSG_ACK(msg); |
robert_jw | 0:b2805b6888dc | 551 | } |
robert_jw | 0:b2805b6888dc | 552 | |
robert_jw | 0:b2805b6888dc | 553 | /** |
robert_jw | 0:b2805b6888dc | 554 | * Create a new netconn (of a specific type) that has a callback function. |
robert_jw | 0:b2805b6888dc | 555 | * The corresponding pcb is NOT created! |
robert_jw | 0:b2805b6888dc | 556 | * |
robert_jw | 0:b2805b6888dc | 557 | * @param t the type of 'connection' to create (@see enum netconn_type) |
robert_jw | 0:b2805b6888dc | 558 | * @param proto the IP protocol for RAW IP pcbs |
robert_jw | 0:b2805b6888dc | 559 | * @param callback a function to call on status changes (RX available, TX'ed) |
robert_jw | 0:b2805b6888dc | 560 | * @return a newly allocated struct netconn or |
robert_jw | 0:b2805b6888dc | 561 | * NULL on memory error |
robert_jw | 0:b2805b6888dc | 562 | */ |
robert_jw | 0:b2805b6888dc | 563 | struct netconn* |
robert_jw | 0:b2805b6888dc | 564 | netconn_alloc(enum netconn_type t, netconn_callback callback) |
robert_jw | 0:b2805b6888dc | 565 | { |
robert_jw | 0:b2805b6888dc | 566 | struct netconn *conn; |
robert_jw | 0:b2805b6888dc | 567 | int size; |
robert_jw | 0:b2805b6888dc | 568 | |
robert_jw | 0:b2805b6888dc | 569 | conn = (struct netconn *)memp_malloc(MEMP_NETCONN); |
robert_jw | 0:b2805b6888dc | 570 | if (conn == NULL) { |
robert_jw | 0:b2805b6888dc | 571 | return NULL; |
robert_jw | 0:b2805b6888dc | 572 | } |
robert_jw | 0:b2805b6888dc | 573 | |
robert_jw | 0:b2805b6888dc | 574 | conn->last_err = ERR_OK; |
robert_jw | 0:b2805b6888dc | 575 | conn->type = t; |
robert_jw | 0:b2805b6888dc | 576 | conn->pcb.tcp = NULL; |
robert_jw | 0:b2805b6888dc | 577 | |
robert_jw | 0:b2805b6888dc | 578 | #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \ |
robert_jw | 0:b2805b6888dc | 579 | (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE) |
robert_jw | 0:b2805b6888dc | 580 | size = DEFAULT_RAW_RECVMBOX_SIZE; |
robert_jw | 0:b2805b6888dc | 581 | #else |
robert_jw | 0:b2805b6888dc | 582 | switch(NETCONNTYPE_GROUP(t)) { |
robert_jw | 0:b2805b6888dc | 583 | #if LWIP_RAW |
robert_jw | 0:b2805b6888dc | 584 | case NETCONN_RAW: |
robert_jw | 0:b2805b6888dc | 585 | size = DEFAULT_RAW_RECVMBOX_SIZE; |
robert_jw | 0:b2805b6888dc | 586 | break; |
robert_jw | 0:b2805b6888dc | 587 | #endif /* LWIP_RAW */ |
robert_jw | 0:b2805b6888dc | 588 | #if LWIP_UDP |
robert_jw | 0:b2805b6888dc | 589 | case NETCONN_UDP: |
robert_jw | 0:b2805b6888dc | 590 | size = DEFAULT_UDP_RECVMBOX_SIZE; |
robert_jw | 0:b2805b6888dc | 591 | break; |
robert_jw | 0:b2805b6888dc | 592 | #endif /* LWIP_UDP */ |
robert_jw | 0:b2805b6888dc | 593 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 594 | case NETCONN_TCP: |
robert_jw | 0:b2805b6888dc | 595 | size = DEFAULT_TCP_RECVMBOX_SIZE; |
robert_jw | 0:b2805b6888dc | 596 | break; |
robert_jw | 0:b2805b6888dc | 597 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 598 | default: |
robert_jw | 0:b2805b6888dc | 599 | LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0); |
robert_jw | 0:b2805b6888dc | 600 | break; |
robert_jw | 0:b2805b6888dc | 601 | } |
robert_jw | 0:b2805b6888dc | 602 | #endif |
robert_jw | 0:b2805b6888dc | 603 | |
robert_jw | 0:b2805b6888dc | 604 | if (sys_sem_new(&conn->op_completed, 0) != ERR_OK) { |
robert_jw | 0:b2805b6888dc | 605 | memp_free(MEMP_NETCONN, conn); |
robert_jw | 0:b2805b6888dc | 606 | return NULL; |
robert_jw | 0:b2805b6888dc | 607 | } |
robert_jw | 0:b2805b6888dc | 608 | if (sys_mbox_new(&conn->recvmbox, size) != ERR_OK) { |
robert_jw | 0:b2805b6888dc | 609 | sys_sem_free(&conn->op_completed); |
robert_jw | 0:b2805b6888dc | 610 | memp_free(MEMP_NETCONN, conn); |
robert_jw | 0:b2805b6888dc | 611 | return NULL; |
robert_jw | 0:b2805b6888dc | 612 | } |
robert_jw | 0:b2805b6888dc | 613 | |
robert_jw | 0:b2805b6888dc | 614 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 615 | sys_mbox_set_invalid(&conn->acceptmbox); |
robert_jw | 0:b2805b6888dc | 616 | #endif |
robert_jw | 0:b2805b6888dc | 617 | conn->state = NETCONN_NONE; |
robert_jw | 0:b2805b6888dc | 618 | #if LWIP_SOCKET |
robert_jw | 0:b2805b6888dc | 619 | /* initialize socket to -1 since 0 is a valid socket */ |
robert_jw | 0:b2805b6888dc | 620 | conn->socket = -1; |
robert_jw | 0:b2805b6888dc | 621 | #endif /* LWIP_SOCKET */ |
robert_jw | 0:b2805b6888dc | 622 | conn->callback = callback; |
robert_jw | 0:b2805b6888dc | 623 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 624 | conn->current_msg = NULL; |
robert_jw | 0:b2805b6888dc | 625 | conn->write_offset = 0; |
robert_jw | 0:b2805b6888dc | 626 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 627 | #if LWIP_SO_RCVTIMEO |
robert_jw | 0:b2805b6888dc | 628 | conn->recv_timeout = 0; |
robert_jw | 0:b2805b6888dc | 629 | #endif /* LWIP_SO_RCVTIMEO */ |
robert_jw | 0:b2805b6888dc | 630 | #if LWIP_SO_RCVBUF |
robert_jw | 0:b2805b6888dc | 631 | conn->recv_bufsize = RECV_BUFSIZE_DEFAULT; |
robert_jw | 0:b2805b6888dc | 632 | conn->recv_avail = 0; |
robert_jw | 0:b2805b6888dc | 633 | #endif /* LWIP_SO_RCVBUF */ |
robert_jw | 0:b2805b6888dc | 634 | conn->flags = 0; |
robert_jw | 0:b2805b6888dc | 635 | return conn; |
robert_jw | 0:b2805b6888dc | 636 | } |
robert_jw | 0:b2805b6888dc | 637 | |
robert_jw | 0:b2805b6888dc | 638 | /** |
robert_jw | 0:b2805b6888dc | 639 | * Delete a netconn and all its resources. |
robert_jw | 0:b2805b6888dc | 640 | * The pcb is NOT freed (since we might not be in the right thread context do this). |
robert_jw | 0:b2805b6888dc | 641 | * |
robert_jw | 0:b2805b6888dc | 642 | * @param conn the netconn to free |
robert_jw | 0:b2805b6888dc | 643 | */ |
robert_jw | 0:b2805b6888dc | 644 | void |
robert_jw | 0:b2805b6888dc | 645 | netconn_free(struct netconn *conn) |
robert_jw | 0:b2805b6888dc | 646 | { |
robert_jw | 0:b2805b6888dc | 647 | LWIP_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL); |
robert_jw | 0:b2805b6888dc | 648 | LWIP_ASSERT("recvmbox must be deallocated before calling this function", |
robert_jw | 0:b2805b6888dc | 649 | !sys_mbox_valid(&conn->recvmbox)); |
robert_jw | 0:b2805b6888dc | 650 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 651 | LWIP_ASSERT("acceptmbox must be deallocated before calling this function", |
robert_jw | 0:b2805b6888dc | 652 | !sys_mbox_valid(&conn->acceptmbox)); |
robert_jw | 0:b2805b6888dc | 653 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 654 | |
robert_jw | 0:b2805b6888dc | 655 | sys_sem_free(&conn->op_completed); |
robert_jw | 0:b2805b6888dc | 656 | sys_sem_set_invalid(&conn->op_completed); |
robert_jw | 0:b2805b6888dc | 657 | |
robert_jw | 0:b2805b6888dc | 658 | memp_free(MEMP_NETCONN, conn); |
robert_jw | 0:b2805b6888dc | 659 | } |
robert_jw | 0:b2805b6888dc | 660 | |
robert_jw | 0:b2805b6888dc | 661 | /** |
robert_jw | 0:b2805b6888dc | 662 | * Delete rcvmbox and acceptmbox of a netconn and free the left-over data in |
robert_jw | 0:b2805b6888dc | 663 | * these mboxes |
robert_jw | 0:b2805b6888dc | 664 | * |
robert_jw | 0:b2805b6888dc | 665 | * @param conn the netconn to free |
robert_jw | 0:b2805b6888dc | 666 | * @bytes_drained bytes drained from recvmbox |
robert_jw | 0:b2805b6888dc | 667 | * @accepts_drained pending connections drained from acceptmbox |
robert_jw | 0:b2805b6888dc | 668 | */ |
robert_jw | 0:b2805b6888dc | 669 | static void |
robert_jw | 0:b2805b6888dc | 670 | netconn_drain(struct netconn *conn) |
robert_jw | 0:b2805b6888dc | 671 | { |
robert_jw | 0:b2805b6888dc | 672 | void *mem; |
robert_jw | 0:b2805b6888dc | 673 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 674 | struct pbuf *p; |
robert_jw | 0:b2805b6888dc | 675 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 676 | |
robert_jw | 0:b2805b6888dc | 677 | /* This runs in tcpip_thread, so we don't need to lock against rx packets */ |
robert_jw | 0:b2805b6888dc | 678 | |
robert_jw | 0:b2805b6888dc | 679 | /* Delete and drain the recvmbox. */ |
robert_jw | 0:b2805b6888dc | 680 | if (sys_mbox_valid(&conn->recvmbox)) { |
robert_jw | 0:b2805b6888dc | 681 | while (sys_mbox_tryfetch(&conn->recvmbox, &mem) != SYS_MBOX_EMPTY) { |
robert_jw | 0:b2805b6888dc | 682 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 683 | if (conn->type == NETCONN_TCP) { |
robert_jw | 0:b2805b6888dc | 684 | if(mem != NULL) { |
robert_jw | 0:b2805b6888dc | 685 | p = (struct pbuf*)mem; |
robert_jw | 0:b2805b6888dc | 686 | /* pcb might be set to NULL already by err_tcp() */ |
robert_jw | 0:b2805b6888dc | 687 | if (conn->pcb.tcp != NULL) { |
robert_jw | 0:b2805b6888dc | 688 | tcp_recved(conn->pcb.tcp, p->tot_len); |
robert_jw | 0:b2805b6888dc | 689 | } |
robert_jw | 0:b2805b6888dc | 690 | pbuf_free(p); |
robert_jw | 0:b2805b6888dc | 691 | } |
robert_jw | 0:b2805b6888dc | 692 | } else |
robert_jw | 0:b2805b6888dc | 693 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 694 | { |
robert_jw | 0:b2805b6888dc | 695 | netbuf_delete((struct netbuf *)mem); |
robert_jw | 0:b2805b6888dc | 696 | } |
robert_jw | 0:b2805b6888dc | 697 | } |
robert_jw | 0:b2805b6888dc | 698 | sys_mbox_free(&conn->recvmbox); |
robert_jw | 0:b2805b6888dc | 699 | sys_mbox_set_invalid(&conn->recvmbox); |
robert_jw | 0:b2805b6888dc | 700 | } |
robert_jw | 0:b2805b6888dc | 701 | |
robert_jw | 0:b2805b6888dc | 702 | /* Delete and drain the acceptmbox. */ |
robert_jw | 0:b2805b6888dc | 703 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 704 | if (sys_mbox_valid(&conn->acceptmbox)) { |
robert_jw | 0:b2805b6888dc | 705 | while (sys_mbox_tryfetch(&conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) { |
robert_jw | 0:b2805b6888dc | 706 | struct netconn *newconn = (struct netconn *)mem; |
robert_jw | 0:b2805b6888dc | 707 | /* Only tcp pcbs have an acceptmbox, so no need to check conn->type */ |
robert_jw | 0:b2805b6888dc | 708 | /* pcb might be set to NULL already by err_tcp() */ |
robert_jw | 0:b2805b6888dc | 709 | if (conn->pcb.tcp != NULL) { |
robert_jw | 0:b2805b6888dc | 710 | tcp_accepted(conn->pcb.tcp); |
robert_jw | 0:b2805b6888dc | 711 | } |
robert_jw | 0:b2805b6888dc | 712 | /* drain recvmbox */ |
robert_jw | 0:b2805b6888dc | 713 | netconn_drain(newconn); |
robert_jw | 0:b2805b6888dc | 714 | if (newconn->pcb.tcp != NULL) { |
robert_jw | 0:b2805b6888dc | 715 | tcp_abort(newconn->pcb.tcp); |
robert_jw | 0:b2805b6888dc | 716 | newconn->pcb.tcp = NULL; |
robert_jw | 0:b2805b6888dc | 717 | } |
robert_jw | 0:b2805b6888dc | 718 | netconn_free(newconn); |
robert_jw | 0:b2805b6888dc | 719 | } |
robert_jw | 0:b2805b6888dc | 720 | sys_mbox_free(&conn->acceptmbox); |
robert_jw | 0:b2805b6888dc | 721 | sys_mbox_set_invalid(&conn->acceptmbox); |
robert_jw | 0:b2805b6888dc | 722 | } |
robert_jw | 0:b2805b6888dc | 723 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 724 | } |
robert_jw | 0:b2805b6888dc | 725 | |
robert_jw | 0:b2805b6888dc | 726 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 727 | /** |
robert_jw | 0:b2805b6888dc | 728 | * Internal helper function to close a TCP netconn: since this sometimes |
robert_jw | 0:b2805b6888dc | 729 | * doesn't work at the first attempt, this function is called from multiple |
robert_jw | 0:b2805b6888dc | 730 | * places. |
robert_jw | 0:b2805b6888dc | 731 | * |
robert_jw | 0:b2805b6888dc | 732 | * @param conn the TCP netconn to close |
robert_jw | 0:b2805b6888dc | 733 | */ |
robert_jw | 0:b2805b6888dc | 734 | static void |
robert_jw | 0:b2805b6888dc | 735 | do_close_internal(struct netconn *conn) |
robert_jw | 0:b2805b6888dc | 736 | { |
robert_jw | 0:b2805b6888dc | 737 | err_t err; |
robert_jw | 0:b2805b6888dc | 738 | u8_t shut, shut_rx, shut_tx, close; |
robert_jw | 0:b2805b6888dc | 739 | |
robert_jw | 0:b2805b6888dc | 740 | LWIP_ASSERT("invalid conn", (conn != NULL)); |
robert_jw | 0:b2805b6888dc | 741 | LWIP_ASSERT("this is for tcp netconns only", (conn->type == NETCONN_TCP)); |
robert_jw | 0:b2805b6888dc | 742 | LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE)); |
robert_jw | 0:b2805b6888dc | 743 | LWIP_ASSERT("pcb already closed", (conn->pcb.tcp != NULL)); |
robert_jw | 0:b2805b6888dc | 744 | LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL); |
robert_jw | 0:b2805b6888dc | 745 | |
robert_jw | 0:b2805b6888dc | 746 | shut = conn->current_msg->msg.sd.shut; |
robert_jw | 0:b2805b6888dc | 747 | shut_rx = shut & NETCONN_SHUT_RD; |
robert_jw | 0:b2805b6888dc | 748 | shut_tx = shut & NETCONN_SHUT_WR; |
robert_jw | 0:b2805b6888dc | 749 | /* shutting down both ends is the same as closing */ |
robert_jw | 0:b2805b6888dc | 750 | close = shut == NETCONN_SHUT_RDWR; |
robert_jw | 0:b2805b6888dc | 751 | |
robert_jw | 0:b2805b6888dc | 752 | /* Set back some callback pointers */ |
robert_jw | 0:b2805b6888dc | 753 | if (close) { |
robert_jw | 0:b2805b6888dc | 754 | tcp_arg(conn->pcb.tcp, NULL); |
robert_jw | 0:b2805b6888dc | 755 | } |
robert_jw | 0:b2805b6888dc | 756 | if (conn->pcb.tcp->state == LISTEN) { |
robert_jw | 0:b2805b6888dc | 757 | tcp_accept(conn->pcb.tcp, NULL); |
robert_jw | 0:b2805b6888dc | 758 | } else { |
robert_jw | 0:b2805b6888dc | 759 | /* some callbacks have to be reset if tcp_close is not successful */ |
robert_jw | 0:b2805b6888dc | 760 | if (shut_rx) { |
robert_jw | 0:b2805b6888dc | 761 | tcp_recv(conn->pcb.tcp, NULL); |
robert_jw | 0:b2805b6888dc | 762 | tcp_accept(conn->pcb.tcp, NULL); |
robert_jw | 0:b2805b6888dc | 763 | } |
robert_jw | 0:b2805b6888dc | 764 | if (shut_tx) { |
robert_jw | 0:b2805b6888dc | 765 | tcp_sent(conn->pcb.tcp, NULL); |
robert_jw | 0:b2805b6888dc | 766 | } |
robert_jw | 0:b2805b6888dc | 767 | if (close) { |
robert_jw | 0:b2805b6888dc | 768 | tcp_poll(conn->pcb.tcp, NULL, 4); |
robert_jw | 0:b2805b6888dc | 769 | tcp_err(conn->pcb.tcp, NULL); |
robert_jw | 0:b2805b6888dc | 770 | } |
robert_jw | 0:b2805b6888dc | 771 | } |
robert_jw | 0:b2805b6888dc | 772 | /* Try to close the connection */ |
robert_jw | 0:b2805b6888dc | 773 | if (shut == NETCONN_SHUT_RDWR) { |
robert_jw | 0:b2805b6888dc | 774 | err = tcp_close(conn->pcb.tcp); |
robert_jw | 0:b2805b6888dc | 775 | } else { |
robert_jw | 0:b2805b6888dc | 776 | err = tcp_shutdown(conn->pcb.tcp, shut & NETCONN_SHUT_RD, shut & NETCONN_SHUT_WR); |
robert_jw | 0:b2805b6888dc | 777 | } |
robert_jw | 0:b2805b6888dc | 778 | if (err == ERR_OK) { |
robert_jw | 0:b2805b6888dc | 779 | /* Closing succeeded */ |
robert_jw | 0:b2805b6888dc | 780 | conn->current_msg->err = ERR_OK; |
robert_jw | 0:b2805b6888dc | 781 | conn->current_msg = NULL; |
robert_jw | 0:b2805b6888dc | 782 | conn->state = NETCONN_NONE; |
robert_jw | 0:b2805b6888dc | 783 | /* Set back some callback pointers as conn is going away */ |
robert_jw | 0:b2805b6888dc | 784 | conn->pcb.tcp = NULL; |
robert_jw | 0:b2805b6888dc | 785 | /* Trigger select() in socket layer. Make sure everybody notices activity |
robert_jw | 0:b2805b6888dc | 786 | on the connection, error first! */ |
robert_jw | 0:b2805b6888dc | 787 | if (close) { |
robert_jw | 0:b2805b6888dc | 788 | API_EVENT(conn, NETCONN_EVT_ERROR, 0); |
robert_jw | 0:b2805b6888dc | 789 | } |
robert_jw | 0:b2805b6888dc | 790 | if (shut_rx) { |
robert_jw | 0:b2805b6888dc | 791 | API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0); |
robert_jw | 0:b2805b6888dc | 792 | } |
robert_jw | 0:b2805b6888dc | 793 | if (shut_tx) { |
robert_jw | 0:b2805b6888dc | 794 | API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0); |
robert_jw | 0:b2805b6888dc | 795 | } |
robert_jw | 0:b2805b6888dc | 796 | /* wake up the application task */ |
robert_jw | 0:b2805b6888dc | 797 | sys_sem_signal(&conn->op_completed); |
robert_jw | 0:b2805b6888dc | 798 | } else { |
robert_jw | 0:b2805b6888dc | 799 | /* Closing failed, restore some of the callbacks */ |
robert_jw | 0:b2805b6888dc | 800 | /* Closing of listen pcb will never fail! */ |
robert_jw | 0:b2805b6888dc | 801 | LWIP_ASSERT("Closing a listen pcb may not fail!", (conn->pcb.tcp->state != LISTEN)); |
robert_jw | 0:b2805b6888dc | 802 | tcp_sent(conn->pcb.tcp, sent_tcp); |
robert_jw | 0:b2805b6888dc | 803 | tcp_poll(conn->pcb.tcp, poll_tcp, 4); |
robert_jw | 0:b2805b6888dc | 804 | tcp_err(conn->pcb.tcp, err_tcp); |
robert_jw | 0:b2805b6888dc | 805 | tcp_arg(conn->pcb.tcp, conn); |
robert_jw | 0:b2805b6888dc | 806 | /* don't restore recv callback: we don't want to receive any more data */ |
robert_jw | 0:b2805b6888dc | 807 | } |
robert_jw | 0:b2805b6888dc | 808 | /* If closing didn't succeed, we get called again either |
robert_jw | 0:b2805b6888dc | 809 | from poll_tcp or from sent_tcp */ |
robert_jw | 0:b2805b6888dc | 810 | } |
robert_jw | 0:b2805b6888dc | 811 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 812 | |
robert_jw | 0:b2805b6888dc | 813 | /** |
robert_jw | 0:b2805b6888dc | 814 | * Delete the pcb inside a netconn. |
robert_jw | 0:b2805b6888dc | 815 | * Called from netconn_delete. |
robert_jw | 0:b2805b6888dc | 816 | * |
robert_jw | 0:b2805b6888dc | 817 | * @param msg the api_msg_msg pointing to the connection |
robert_jw | 0:b2805b6888dc | 818 | */ |
robert_jw | 0:b2805b6888dc | 819 | void |
robert_jw | 0:b2805b6888dc | 820 | do_delconn(struct api_msg_msg *msg) |
robert_jw | 0:b2805b6888dc | 821 | { |
robert_jw | 0:b2805b6888dc | 822 | /* @todo TCP: abort running write/connect? */ |
robert_jw | 0:b2805b6888dc | 823 | if ((msg->conn->state != NETCONN_NONE) && |
robert_jw | 0:b2805b6888dc | 824 | (msg->conn->state != NETCONN_LISTEN) && |
robert_jw | 0:b2805b6888dc | 825 | (msg->conn->state != NETCONN_CONNECT)) { |
robert_jw | 0:b2805b6888dc | 826 | /* this only happens for TCP netconns */ |
robert_jw | 0:b2805b6888dc | 827 | LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP); |
robert_jw | 0:b2805b6888dc | 828 | msg->err = ERR_INPROGRESS; |
robert_jw | 0:b2805b6888dc | 829 | } else { |
robert_jw | 0:b2805b6888dc | 830 | LWIP_ASSERT("blocking connect in progress", |
robert_jw | 0:b2805b6888dc | 831 | (msg->conn->state != NETCONN_CONNECT) || IN_NONBLOCKING_CONNECT(msg->conn)); |
robert_jw | 0:b2805b6888dc | 832 | /* Drain and delete mboxes */ |
robert_jw | 0:b2805b6888dc | 833 | netconn_drain(msg->conn); |
robert_jw | 0:b2805b6888dc | 834 | |
robert_jw | 0:b2805b6888dc | 835 | if (msg->conn->pcb.tcp != NULL) { |
robert_jw | 0:b2805b6888dc | 836 | |
robert_jw | 0:b2805b6888dc | 837 | switch (NETCONNTYPE_GROUP(msg->conn->type)) { |
robert_jw | 0:b2805b6888dc | 838 | #if LWIP_RAW |
robert_jw | 0:b2805b6888dc | 839 | case NETCONN_RAW: |
robert_jw | 0:b2805b6888dc | 840 | raw_remove(msg->conn->pcb.raw); |
robert_jw | 0:b2805b6888dc | 841 | break; |
robert_jw | 0:b2805b6888dc | 842 | #endif /* LWIP_RAW */ |
robert_jw | 0:b2805b6888dc | 843 | #if LWIP_UDP |
robert_jw | 0:b2805b6888dc | 844 | case NETCONN_UDP: |
robert_jw | 0:b2805b6888dc | 845 | msg->conn->pcb.udp->recv_arg = NULL; |
robert_jw | 0:b2805b6888dc | 846 | udp_remove(msg->conn->pcb.udp); |
robert_jw | 0:b2805b6888dc | 847 | break; |
robert_jw | 0:b2805b6888dc | 848 | #endif /* LWIP_UDP */ |
robert_jw | 0:b2805b6888dc | 849 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 850 | case NETCONN_TCP: |
robert_jw | 0:b2805b6888dc | 851 | LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL && |
robert_jw | 0:b2805b6888dc | 852 | msg->conn->write_offset == 0); |
robert_jw | 0:b2805b6888dc | 853 | msg->conn->state = NETCONN_CLOSE; |
robert_jw | 0:b2805b6888dc | 854 | msg->msg.sd.shut = NETCONN_SHUT_RDWR; |
robert_jw | 0:b2805b6888dc | 855 | msg->conn->current_msg = msg; |
robert_jw | 0:b2805b6888dc | 856 | do_close_internal(msg->conn); |
robert_jw | 0:b2805b6888dc | 857 | /* API_EVENT is called inside do_close_internal, before releasing |
robert_jw | 0:b2805b6888dc | 858 | the application thread, so we can return at this point! */ |
robert_jw | 0:b2805b6888dc | 859 | return; |
robert_jw | 0:b2805b6888dc | 860 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 861 | default: |
robert_jw | 0:b2805b6888dc | 862 | break; |
robert_jw | 0:b2805b6888dc | 863 | } |
robert_jw | 0:b2805b6888dc | 864 | msg->conn->pcb.tcp = NULL; |
robert_jw | 0:b2805b6888dc | 865 | } |
robert_jw | 0:b2805b6888dc | 866 | /* tcp netconns don't come here! */ |
robert_jw | 0:b2805b6888dc | 867 | |
robert_jw | 0:b2805b6888dc | 868 | /* @todo: this lets select make the socket readable and writable, |
robert_jw | 0:b2805b6888dc | 869 | which is wrong! errfd instead? */ |
robert_jw | 0:b2805b6888dc | 870 | API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0); |
robert_jw | 0:b2805b6888dc | 871 | API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0); |
robert_jw | 0:b2805b6888dc | 872 | } |
robert_jw | 0:b2805b6888dc | 873 | if (sys_sem_valid(&msg->conn->op_completed)) { |
robert_jw | 0:b2805b6888dc | 874 | sys_sem_signal(&msg->conn->op_completed); |
robert_jw | 0:b2805b6888dc | 875 | } |
robert_jw | 0:b2805b6888dc | 876 | } |
robert_jw | 0:b2805b6888dc | 877 | |
robert_jw | 0:b2805b6888dc | 878 | /** |
robert_jw | 0:b2805b6888dc | 879 | * Bind a pcb contained in a netconn |
robert_jw | 0:b2805b6888dc | 880 | * Called from netconn_bind. |
robert_jw | 0:b2805b6888dc | 881 | * |
robert_jw | 0:b2805b6888dc | 882 | * @param msg the api_msg_msg pointing to the connection and containing |
robert_jw | 0:b2805b6888dc | 883 | * the IP address and port to bind to |
robert_jw | 0:b2805b6888dc | 884 | */ |
robert_jw | 0:b2805b6888dc | 885 | void |
robert_jw | 0:b2805b6888dc | 886 | do_bind(struct api_msg_msg *msg) |
robert_jw | 0:b2805b6888dc | 887 | { |
robert_jw | 0:b2805b6888dc | 888 | if (ERR_IS_FATAL(msg->conn->last_err)) { |
robert_jw | 0:b2805b6888dc | 889 | msg->err = msg->conn->last_err; |
robert_jw | 0:b2805b6888dc | 890 | } else { |
robert_jw | 0:b2805b6888dc | 891 | msg->err = ERR_VAL; |
robert_jw | 0:b2805b6888dc | 892 | if (msg->conn->pcb.tcp != NULL) { |
robert_jw | 0:b2805b6888dc | 893 | switch (NETCONNTYPE_GROUP(msg->conn->type)) { |
robert_jw | 0:b2805b6888dc | 894 | #if LWIP_RAW |
robert_jw | 0:b2805b6888dc | 895 | case NETCONN_RAW: |
robert_jw | 0:b2805b6888dc | 896 | msg->err = raw_bind(msg->conn->pcb.raw, msg->msg.bc.ipaddr); |
robert_jw | 0:b2805b6888dc | 897 | break; |
robert_jw | 0:b2805b6888dc | 898 | #endif /* LWIP_RAW */ |
robert_jw | 0:b2805b6888dc | 899 | #if LWIP_UDP |
robert_jw | 0:b2805b6888dc | 900 | case NETCONN_UDP: |
robert_jw | 0:b2805b6888dc | 901 | msg->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port); |
robert_jw | 0:b2805b6888dc | 902 | break; |
robert_jw | 0:b2805b6888dc | 903 | #endif /* LWIP_UDP */ |
robert_jw | 0:b2805b6888dc | 904 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 905 | case NETCONN_TCP: |
robert_jw | 0:b2805b6888dc | 906 | msg->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port); |
robert_jw | 0:b2805b6888dc | 907 | break; |
robert_jw | 0:b2805b6888dc | 908 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 909 | default: |
robert_jw | 0:b2805b6888dc | 910 | break; |
robert_jw | 0:b2805b6888dc | 911 | } |
robert_jw | 0:b2805b6888dc | 912 | } |
robert_jw | 0:b2805b6888dc | 913 | } |
robert_jw | 0:b2805b6888dc | 914 | TCPIP_APIMSG_ACK(msg); |
robert_jw | 0:b2805b6888dc | 915 | } |
robert_jw | 0:b2805b6888dc | 916 | |
robert_jw | 0:b2805b6888dc | 917 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 918 | /** |
robert_jw | 0:b2805b6888dc | 919 | * TCP callback function if a connection (opened by tcp_connect/do_connect) has |
robert_jw | 0:b2805b6888dc | 920 | * been established (or reset by the remote host). |
robert_jw | 0:b2805b6888dc | 921 | * |
robert_jw | 0:b2805b6888dc | 922 | * @see tcp.h (struct tcp_pcb.connected) for parameters and return values |
robert_jw | 0:b2805b6888dc | 923 | */ |
robert_jw | 0:b2805b6888dc | 924 | static err_t |
robert_jw | 0:b2805b6888dc | 925 | do_connected(void *arg, struct tcp_pcb *pcb, err_t err) |
robert_jw | 0:b2805b6888dc | 926 | { |
robert_jw | 0:b2805b6888dc | 927 | struct netconn *conn; |
robert_jw | 0:b2805b6888dc | 928 | int was_blocking; |
robert_jw | 0:b2805b6888dc | 929 | |
robert_jw | 0:b2805b6888dc | 930 | LWIP_UNUSED_ARG(pcb); |
robert_jw | 0:b2805b6888dc | 931 | |
robert_jw | 0:b2805b6888dc | 932 | conn = (struct netconn *)arg; |
robert_jw | 0:b2805b6888dc | 933 | |
robert_jw | 0:b2805b6888dc | 934 | if (conn == NULL) { |
robert_jw | 0:b2805b6888dc | 935 | return ERR_VAL; |
robert_jw | 0:b2805b6888dc | 936 | } |
robert_jw | 0:b2805b6888dc | 937 | |
robert_jw | 0:b2805b6888dc | 938 | LWIP_ASSERT("conn->state == NETCONN_CONNECT", conn->state == NETCONN_CONNECT); |
robert_jw | 0:b2805b6888dc | 939 | LWIP_ASSERT("(conn->current_msg != NULL) || conn->in_non_blocking_connect", |
robert_jw | 0:b2805b6888dc | 940 | (conn->current_msg != NULL) || IN_NONBLOCKING_CONNECT(conn)); |
robert_jw | 0:b2805b6888dc | 941 | |
robert_jw | 0:b2805b6888dc | 942 | if (conn->current_msg != NULL) { |
robert_jw | 0:b2805b6888dc | 943 | conn->current_msg->err = err; |
robert_jw | 0:b2805b6888dc | 944 | } |
robert_jw | 0:b2805b6888dc | 945 | if ((conn->type == NETCONN_TCP) && (err == ERR_OK)) { |
robert_jw | 0:b2805b6888dc | 946 | setup_tcp(conn); |
robert_jw | 0:b2805b6888dc | 947 | } |
robert_jw | 0:b2805b6888dc | 948 | was_blocking = !IN_NONBLOCKING_CONNECT(conn); |
robert_jw | 0:b2805b6888dc | 949 | SET_NONBLOCKING_CONNECT(conn, 0); |
robert_jw | 0:b2805b6888dc | 950 | conn->current_msg = NULL; |
robert_jw | 0:b2805b6888dc | 951 | conn->state = NETCONN_NONE; |
robert_jw | 0:b2805b6888dc | 952 | if (!was_blocking) { |
robert_jw | 0:b2805b6888dc | 953 | NETCONN_SET_SAFE_ERR(conn, ERR_OK); |
robert_jw | 0:b2805b6888dc | 954 | } |
robert_jw | 0:b2805b6888dc | 955 | API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0); |
robert_jw | 0:b2805b6888dc | 956 | |
robert_jw | 0:b2805b6888dc | 957 | if (was_blocking) { |
robert_jw | 0:b2805b6888dc | 958 | sys_sem_signal(&conn->op_completed); |
robert_jw | 0:b2805b6888dc | 959 | } |
robert_jw | 0:b2805b6888dc | 960 | return ERR_OK; |
robert_jw | 0:b2805b6888dc | 961 | } |
robert_jw | 0:b2805b6888dc | 962 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 963 | |
robert_jw | 0:b2805b6888dc | 964 | /** |
robert_jw | 0:b2805b6888dc | 965 | * Connect a pcb contained inside a netconn |
robert_jw | 0:b2805b6888dc | 966 | * Called from netconn_connect. |
robert_jw | 0:b2805b6888dc | 967 | * |
robert_jw | 0:b2805b6888dc | 968 | * @param msg the api_msg_msg pointing to the connection and containing |
robert_jw | 0:b2805b6888dc | 969 | * the IP address and port to connect to |
robert_jw | 0:b2805b6888dc | 970 | */ |
robert_jw | 0:b2805b6888dc | 971 | void |
robert_jw | 0:b2805b6888dc | 972 | do_connect(struct api_msg_msg *msg) |
robert_jw | 0:b2805b6888dc | 973 | { |
robert_jw | 0:b2805b6888dc | 974 | if (msg->conn->pcb.tcp == NULL) { |
robert_jw | 0:b2805b6888dc | 975 | /* This may happen when calling netconn_connect() a second time */ |
robert_jw | 0:b2805b6888dc | 976 | msg->err = ERR_CLSD; |
robert_jw | 0:b2805b6888dc | 977 | } else { |
robert_jw | 0:b2805b6888dc | 978 | switch (NETCONNTYPE_GROUP(msg->conn->type)) { |
robert_jw | 0:b2805b6888dc | 979 | #if LWIP_RAW |
robert_jw | 0:b2805b6888dc | 980 | case NETCONN_RAW: |
robert_jw | 0:b2805b6888dc | 981 | msg->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr); |
robert_jw | 0:b2805b6888dc | 982 | break; |
robert_jw | 0:b2805b6888dc | 983 | #endif /* LWIP_RAW */ |
robert_jw | 0:b2805b6888dc | 984 | #if LWIP_UDP |
robert_jw | 0:b2805b6888dc | 985 | case NETCONN_UDP: |
robert_jw | 0:b2805b6888dc | 986 | msg->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port); |
robert_jw | 0:b2805b6888dc | 987 | break; |
robert_jw | 0:b2805b6888dc | 988 | #endif /* LWIP_UDP */ |
robert_jw | 0:b2805b6888dc | 989 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 990 | case NETCONN_TCP: |
robert_jw | 0:b2805b6888dc | 991 | /* Prevent connect while doing any other action. */ |
robert_jw | 0:b2805b6888dc | 992 | if (msg->conn->state != NETCONN_NONE) { |
robert_jw | 0:b2805b6888dc | 993 | msg->err = ERR_ISCONN; |
robert_jw | 0:b2805b6888dc | 994 | } else { |
robert_jw | 0:b2805b6888dc | 995 | setup_tcp(msg->conn); |
robert_jw | 0:b2805b6888dc | 996 | msg->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, |
robert_jw | 0:b2805b6888dc | 997 | msg->msg.bc.port, do_connected); |
robert_jw | 0:b2805b6888dc | 998 | if (msg->err == ERR_OK) { |
robert_jw | 0:b2805b6888dc | 999 | u8_t non_blocking = netconn_is_nonblocking(msg->conn); |
robert_jw | 0:b2805b6888dc | 1000 | msg->conn->state = NETCONN_CONNECT; |
robert_jw | 0:b2805b6888dc | 1001 | SET_NONBLOCKING_CONNECT(msg->conn, non_blocking); |
robert_jw | 0:b2805b6888dc | 1002 | if (non_blocking) { |
robert_jw | 0:b2805b6888dc | 1003 | msg->err = ERR_INPROGRESS; |
robert_jw | 0:b2805b6888dc | 1004 | } else { |
robert_jw | 0:b2805b6888dc | 1005 | msg->conn->current_msg = msg; |
robert_jw | 0:b2805b6888dc | 1006 | /* sys_sem_signal() is called from do_connected (or err_tcp()), |
robert_jw | 0:b2805b6888dc | 1007 | * when the connection is established! */ |
robert_jw | 0:b2805b6888dc | 1008 | return; |
robert_jw | 0:b2805b6888dc | 1009 | } |
robert_jw | 0:b2805b6888dc | 1010 | } |
robert_jw | 0:b2805b6888dc | 1011 | } |
robert_jw | 0:b2805b6888dc | 1012 | break; |
robert_jw | 0:b2805b6888dc | 1013 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 1014 | default: |
robert_jw | 0:b2805b6888dc | 1015 | LWIP_ERROR("Invalid netconn type", 0, do{ msg->err = ERR_VAL; }while(0)); |
robert_jw | 0:b2805b6888dc | 1016 | break; |
robert_jw | 0:b2805b6888dc | 1017 | } |
robert_jw | 0:b2805b6888dc | 1018 | } |
robert_jw | 0:b2805b6888dc | 1019 | sys_sem_signal(&msg->conn->op_completed); |
robert_jw | 0:b2805b6888dc | 1020 | } |
robert_jw | 0:b2805b6888dc | 1021 | |
robert_jw | 0:b2805b6888dc | 1022 | /** |
robert_jw | 0:b2805b6888dc | 1023 | * Connect a pcb contained inside a netconn |
robert_jw | 0:b2805b6888dc | 1024 | * Only used for UDP netconns. |
robert_jw | 0:b2805b6888dc | 1025 | * Called from netconn_disconnect. |
robert_jw | 0:b2805b6888dc | 1026 | * |
robert_jw | 0:b2805b6888dc | 1027 | * @param msg the api_msg_msg pointing to the connection to disconnect |
robert_jw | 0:b2805b6888dc | 1028 | */ |
robert_jw | 0:b2805b6888dc | 1029 | void |
robert_jw | 0:b2805b6888dc | 1030 | do_disconnect(struct api_msg_msg *msg) |
robert_jw | 0:b2805b6888dc | 1031 | { |
robert_jw | 0:b2805b6888dc | 1032 | #if LWIP_UDP |
robert_jw | 0:b2805b6888dc | 1033 | if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) { |
robert_jw | 0:b2805b6888dc | 1034 | udp_disconnect(msg->conn->pcb.udp); |
robert_jw | 0:b2805b6888dc | 1035 | msg->err = ERR_OK; |
robert_jw | 0:b2805b6888dc | 1036 | } else |
robert_jw | 0:b2805b6888dc | 1037 | #endif /* LWIP_UDP */ |
robert_jw | 0:b2805b6888dc | 1038 | { |
robert_jw | 0:b2805b6888dc | 1039 | msg->err = ERR_VAL; |
robert_jw | 0:b2805b6888dc | 1040 | } |
robert_jw | 0:b2805b6888dc | 1041 | TCPIP_APIMSG_ACK(msg); |
robert_jw | 0:b2805b6888dc | 1042 | } |
robert_jw | 0:b2805b6888dc | 1043 | |
robert_jw | 0:b2805b6888dc | 1044 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 1045 | /** |
robert_jw | 0:b2805b6888dc | 1046 | * Set a TCP pcb contained in a netconn into listen mode |
robert_jw | 0:b2805b6888dc | 1047 | * Called from netconn_listen. |
robert_jw | 0:b2805b6888dc | 1048 | * |
robert_jw | 0:b2805b6888dc | 1049 | * @param msg the api_msg_msg pointing to the connection |
robert_jw | 0:b2805b6888dc | 1050 | */ |
robert_jw | 0:b2805b6888dc | 1051 | void |
robert_jw | 0:b2805b6888dc | 1052 | do_listen(struct api_msg_msg *msg) |
robert_jw | 0:b2805b6888dc | 1053 | { |
robert_jw | 0:b2805b6888dc | 1054 | if (ERR_IS_FATAL(msg->conn->last_err)) { |
robert_jw | 0:b2805b6888dc | 1055 | msg->err = msg->conn->last_err; |
robert_jw | 0:b2805b6888dc | 1056 | } else { |
robert_jw | 0:b2805b6888dc | 1057 | msg->err = ERR_CONN; |
robert_jw | 0:b2805b6888dc | 1058 | if (msg->conn->pcb.tcp != NULL) { |
robert_jw | 0:b2805b6888dc | 1059 | if (msg->conn->type == NETCONN_TCP) { |
robert_jw | 0:b2805b6888dc | 1060 | if (msg->conn->state == NETCONN_NONE) { |
robert_jw | 0:b2805b6888dc | 1061 | #if TCP_LISTEN_BACKLOG |
robert_jw | 0:b2805b6888dc | 1062 | struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog); |
robert_jw | 0:b2805b6888dc | 1063 | #else /* TCP_LISTEN_BACKLOG */ |
robert_jw | 0:b2805b6888dc | 1064 | struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp); |
robert_jw | 0:b2805b6888dc | 1065 | #endif /* TCP_LISTEN_BACKLOG */ |
robert_jw | 0:b2805b6888dc | 1066 | if (lpcb == NULL) { |
robert_jw | 0:b2805b6888dc | 1067 | /* in this case, the old pcb is still allocated */ |
robert_jw | 0:b2805b6888dc | 1068 | msg->err = ERR_MEM; |
robert_jw | 0:b2805b6888dc | 1069 | } else { |
robert_jw | 0:b2805b6888dc | 1070 | /* delete the recvmbox and allocate the acceptmbox */ |
robert_jw | 0:b2805b6888dc | 1071 | if (sys_mbox_valid(&msg->conn->recvmbox)) { |
robert_jw | 0:b2805b6888dc | 1072 | /** @todo: should we drain the recvmbox here? */ |
robert_jw | 0:b2805b6888dc | 1073 | sys_mbox_free(&msg->conn->recvmbox); |
robert_jw | 0:b2805b6888dc | 1074 | sys_mbox_set_invalid(&msg->conn->recvmbox); |
robert_jw | 0:b2805b6888dc | 1075 | } |
robert_jw | 0:b2805b6888dc | 1076 | msg->err = ERR_OK; |
robert_jw | 0:b2805b6888dc | 1077 | if (!sys_mbox_valid(&msg->conn->acceptmbox)) { |
robert_jw | 0:b2805b6888dc | 1078 | msg->err = sys_mbox_new(&msg->conn->acceptmbox, DEFAULT_ACCEPTMBOX_SIZE); |
robert_jw | 0:b2805b6888dc | 1079 | } |
robert_jw | 0:b2805b6888dc | 1080 | if (msg->err == ERR_OK) { |
robert_jw | 0:b2805b6888dc | 1081 | msg->conn->state = NETCONN_LISTEN; |
robert_jw | 0:b2805b6888dc | 1082 | msg->conn->pcb.tcp = lpcb; |
robert_jw | 0:b2805b6888dc | 1083 | tcp_arg(msg->conn->pcb.tcp, msg->conn); |
robert_jw | 0:b2805b6888dc | 1084 | tcp_accept(msg->conn->pcb.tcp, accept_function); |
robert_jw | 0:b2805b6888dc | 1085 | } else { |
robert_jw | 0:b2805b6888dc | 1086 | /* since the old pcb is already deallocated, free lpcb now */ |
robert_jw | 0:b2805b6888dc | 1087 | tcp_close(lpcb); |
robert_jw | 0:b2805b6888dc | 1088 | msg->conn->pcb.tcp = NULL; |
robert_jw | 0:b2805b6888dc | 1089 | } |
robert_jw | 0:b2805b6888dc | 1090 | } |
robert_jw | 0:b2805b6888dc | 1091 | } |
robert_jw | 0:b2805b6888dc | 1092 | } |
robert_jw | 0:b2805b6888dc | 1093 | } |
robert_jw | 0:b2805b6888dc | 1094 | } |
robert_jw | 0:b2805b6888dc | 1095 | TCPIP_APIMSG_ACK(msg); |
robert_jw | 0:b2805b6888dc | 1096 | } |
robert_jw | 0:b2805b6888dc | 1097 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 1098 | |
robert_jw | 0:b2805b6888dc | 1099 | /** |
robert_jw | 0:b2805b6888dc | 1100 | * Send some data on a RAW or UDP pcb contained in a netconn |
robert_jw | 0:b2805b6888dc | 1101 | * Called from netconn_send |
robert_jw | 0:b2805b6888dc | 1102 | * |
robert_jw | 0:b2805b6888dc | 1103 | * @param msg the api_msg_msg pointing to the connection |
robert_jw | 0:b2805b6888dc | 1104 | */ |
robert_jw | 0:b2805b6888dc | 1105 | void |
robert_jw | 0:b2805b6888dc | 1106 | do_send(struct api_msg_msg *msg) |
robert_jw | 0:b2805b6888dc | 1107 | { |
robert_jw | 0:b2805b6888dc | 1108 | if (ERR_IS_FATAL(msg->conn->last_err)) { |
robert_jw | 0:b2805b6888dc | 1109 | msg->err = msg->conn->last_err; |
robert_jw | 0:b2805b6888dc | 1110 | } else { |
robert_jw | 0:b2805b6888dc | 1111 | msg->err = ERR_CONN; |
robert_jw | 0:b2805b6888dc | 1112 | if (msg->conn->pcb.tcp != NULL) { |
robert_jw | 0:b2805b6888dc | 1113 | switch (NETCONNTYPE_GROUP(msg->conn->type)) { |
robert_jw | 0:b2805b6888dc | 1114 | #if LWIP_RAW |
robert_jw | 0:b2805b6888dc | 1115 | case NETCONN_RAW: |
robert_jw | 0:b2805b6888dc | 1116 | if (ip_addr_isany(&msg->msg.b->addr)) { |
robert_jw | 0:b2805b6888dc | 1117 | msg->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p); |
robert_jw | 0:b2805b6888dc | 1118 | } else { |
robert_jw | 0:b2805b6888dc | 1119 | msg->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, &msg->msg.b->addr); |
robert_jw | 0:b2805b6888dc | 1120 | } |
robert_jw | 0:b2805b6888dc | 1121 | break; |
robert_jw | 0:b2805b6888dc | 1122 | #endif |
robert_jw | 0:b2805b6888dc | 1123 | #if LWIP_UDP |
robert_jw | 0:b2805b6888dc | 1124 | case NETCONN_UDP: |
robert_jw | 0:b2805b6888dc | 1125 | #if LWIP_CHECKSUM_ON_COPY |
robert_jw | 0:b2805b6888dc | 1126 | if (ip_addr_isany(&msg->msg.b->addr)) { |
robert_jw | 0:b2805b6888dc | 1127 | msg->err = udp_send_chksum(msg->conn->pcb.udp, msg->msg.b->p, |
robert_jw | 0:b2805b6888dc | 1128 | msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum); |
robert_jw | 0:b2805b6888dc | 1129 | } else { |
robert_jw | 0:b2805b6888dc | 1130 | msg->err = udp_sendto_chksum(msg->conn->pcb.udp, msg->msg.b->p, |
robert_jw | 0:b2805b6888dc | 1131 | &msg->msg.b->addr, msg->msg.b->port, |
robert_jw | 0:b2805b6888dc | 1132 | msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum); |
robert_jw | 0:b2805b6888dc | 1133 | } |
robert_jw | 0:b2805b6888dc | 1134 | #else /* LWIP_CHECKSUM_ON_COPY */ |
robert_jw | 0:b2805b6888dc | 1135 | if (ip_addr_isany(&msg->msg.b->addr)) { |
robert_jw | 0:b2805b6888dc | 1136 | msg->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p); |
robert_jw | 0:b2805b6888dc | 1137 | } else { |
robert_jw | 0:b2805b6888dc | 1138 | msg->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, &msg->msg.b->addr, msg->msg.b->port); |
robert_jw | 0:b2805b6888dc | 1139 | } |
robert_jw | 0:b2805b6888dc | 1140 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
robert_jw | 0:b2805b6888dc | 1141 | break; |
robert_jw | 0:b2805b6888dc | 1142 | #endif /* LWIP_UDP */ |
robert_jw | 0:b2805b6888dc | 1143 | default: |
robert_jw | 0:b2805b6888dc | 1144 | break; |
robert_jw | 0:b2805b6888dc | 1145 | } |
robert_jw | 0:b2805b6888dc | 1146 | } |
robert_jw | 0:b2805b6888dc | 1147 | } |
robert_jw | 0:b2805b6888dc | 1148 | TCPIP_APIMSG_ACK(msg); |
robert_jw | 0:b2805b6888dc | 1149 | } |
robert_jw | 0:b2805b6888dc | 1150 | |
robert_jw | 0:b2805b6888dc | 1151 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 1152 | /** |
robert_jw | 0:b2805b6888dc | 1153 | * Indicate data has been received from a TCP pcb contained in a netconn |
robert_jw | 0:b2805b6888dc | 1154 | * Called from netconn_recv |
robert_jw | 0:b2805b6888dc | 1155 | * |
robert_jw | 0:b2805b6888dc | 1156 | * @param msg the api_msg_msg pointing to the connection |
robert_jw | 0:b2805b6888dc | 1157 | */ |
robert_jw | 0:b2805b6888dc | 1158 | void |
robert_jw | 0:b2805b6888dc | 1159 | do_recv(struct api_msg_msg *msg) |
robert_jw | 0:b2805b6888dc | 1160 | { |
robert_jw | 0:b2805b6888dc | 1161 | msg->err = ERR_OK; |
robert_jw | 0:b2805b6888dc | 1162 | if (msg->conn->pcb.tcp != NULL) { |
robert_jw | 0:b2805b6888dc | 1163 | if (msg->conn->type == NETCONN_TCP) { |
robert_jw | 0:b2805b6888dc | 1164 | #if TCP_LISTEN_BACKLOG |
robert_jw | 0:b2805b6888dc | 1165 | if (msg->conn->pcb.tcp->state == LISTEN) { |
robert_jw | 0:b2805b6888dc | 1166 | tcp_accepted(msg->conn->pcb.tcp); |
robert_jw | 0:b2805b6888dc | 1167 | } else |
robert_jw | 0:b2805b6888dc | 1168 | #endif /* TCP_LISTEN_BACKLOG */ |
robert_jw | 0:b2805b6888dc | 1169 | { |
robert_jw | 0:b2805b6888dc | 1170 | u32_t remaining = msg->msg.r.len; |
robert_jw | 0:b2805b6888dc | 1171 | do { |
robert_jw | 0:b2805b6888dc | 1172 | u16_t recved = (remaining > 0xffff) ? 0xffff : (u16_t)remaining; |
robert_jw | 0:b2805b6888dc | 1173 | tcp_recved(msg->conn->pcb.tcp, recved); |
robert_jw | 0:b2805b6888dc | 1174 | remaining -= recved; |
robert_jw | 0:b2805b6888dc | 1175 | }while(remaining != 0); |
robert_jw | 0:b2805b6888dc | 1176 | } |
robert_jw | 0:b2805b6888dc | 1177 | } |
robert_jw | 0:b2805b6888dc | 1178 | } |
robert_jw | 0:b2805b6888dc | 1179 | TCPIP_APIMSG_ACK(msg); |
robert_jw | 0:b2805b6888dc | 1180 | } |
robert_jw | 0:b2805b6888dc | 1181 | |
robert_jw | 0:b2805b6888dc | 1182 | /** |
robert_jw | 0:b2805b6888dc | 1183 | * See if more data needs to be written from a previous call to netconn_write. |
robert_jw | 0:b2805b6888dc | 1184 | * Called initially from do_write. If the first call can't send all data |
robert_jw | 0:b2805b6888dc | 1185 | * (because of low memory or empty send-buffer), this function is called again |
robert_jw | 0:b2805b6888dc | 1186 | * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the |
robert_jw | 0:b2805b6888dc | 1187 | * blocking application thread (waiting in netconn_write) is released. |
robert_jw | 0:b2805b6888dc | 1188 | * |
robert_jw | 0:b2805b6888dc | 1189 | * @param conn netconn (that is currently in state NETCONN_WRITE) to process |
robert_jw | 0:b2805b6888dc | 1190 | * @return ERR_OK |
robert_jw | 0:b2805b6888dc | 1191 | * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished |
robert_jw | 0:b2805b6888dc | 1192 | */ |
robert_jw | 0:b2805b6888dc | 1193 | static err_t |
robert_jw | 0:b2805b6888dc | 1194 | do_writemore(struct netconn *conn) |
robert_jw | 0:b2805b6888dc | 1195 | { |
robert_jw | 0:b2805b6888dc | 1196 | err_t err = ERR_OK; |
robert_jw | 0:b2805b6888dc | 1197 | void *dataptr; |
robert_jw | 0:b2805b6888dc | 1198 | u16_t len, available; |
robert_jw | 0:b2805b6888dc | 1199 | u8_t write_finished = 0; |
robert_jw | 0:b2805b6888dc | 1200 | size_t diff; |
robert_jw | 0:b2805b6888dc | 1201 | u8_t dontblock = netconn_is_nonblocking(conn) || |
robert_jw | 0:b2805b6888dc | 1202 | (conn->current_msg->msg.w.apiflags & NETCONN_DONTBLOCK); |
robert_jw | 0:b2805b6888dc | 1203 | u8_t apiflags = conn->current_msg->msg.w.apiflags; |
robert_jw | 0:b2805b6888dc | 1204 | |
robert_jw | 0:b2805b6888dc | 1205 | LWIP_ASSERT("conn != NULL", conn != NULL); |
robert_jw | 0:b2805b6888dc | 1206 | LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE)); |
robert_jw | 0:b2805b6888dc | 1207 | LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL); |
robert_jw | 0:b2805b6888dc | 1208 | LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL); |
robert_jw | 0:b2805b6888dc | 1209 | LWIP_ASSERT("conn->write_offset < conn->current_msg->msg.w.len", |
robert_jw | 0:b2805b6888dc | 1210 | conn->write_offset < conn->current_msg->msg.w.len); |
robert_jw | 0:b2805b6888dc | 1211 | |
robert_jw | 0:b2805b6888dc | 1212 | dataptr = (u8_t*)conn->current_msg->msg.w.dataptr + conn->write_offset; |
robert_jw | 0:b2805b6888dc | 1213 | diff = conn->current_msg->msg.w.len - conn->write_offset; |
robert_jw | 0:b2805b6888dc | 1214 | if (diff > 0xffffUL) { /* max_u16_t */ |
robert_jw | 0:b2805b6888dc | 1215 | len = 0xffff; |
robert_jw | 0:b2805b6888dc | 1216 | #if LWIP_TCPIP_CORE_LOCKING |
robert_jw | 0:b2805b6888dc | 1217 | conn->flags |= NETCONN_FLAG_WRITE_DELAYED; |
robert_jw | 0:b2805b6888dc | 1218 | #endif |
robert_jw | 0:b2805b6888dc | 1219 | apiflags |= TCP_WRITE_FLAG_MORE; |
robert_jw | 0:b2805b6888dc | 1220 | } else { |
robert_jw | 0:b2805b6888dc | 1221 | len = (u16_t)diff; |
robert_jw | 0:b2805b6888dc | 1222 | } |
robert_jw | 0:b2805b6888dc | 1223 | available = tcp_sndbuf(conn->pcb.tcp); |
robert_jw | 0:b2805b6888dc | 1224 | if (available < len) { |
robert_jw | 0:b2805b6888dc | 1225 | /* don't try to write more than sendbuf */ |
robert_jw | 0:b2805b6888dc | 1226 | len = available; |
robert_jw | 0:b2805b6888dc | 1227 | #if LWIP_TCPIP_CORE_LOCKING |
robert_jw | 0:b2805b6888dc | 1228 | conn->flags |= NETCONN_FLAG_WRITE_DELAYED; |
robert_jw | 0:b2805b6888dc | 1229 | #endif |
robert_jw | 0:b2805b6888dc | 1230 | apiflags |= TCP_WRITE_FLAG_MORE; |
robert_jw | 0:b2805b6888dc | 1231 | } |
robert_jw | 0:b2805b6888dc | 1232 | if (dontblock && (len < conn->current_msg->msg.w.len)) { |
robert_jw | 0:b2805b6888dc | 1233 | /* failed to send all data at once -> nonblocking write not possible */ |
robert_jw | 0:b2805b6888dc | 1234 | err = ERR_MEM; |
robert_jw | 0:b2805b6888dc | 1235 | } |
robert_jw | 0:b2805b6888dc | 1236 | if (err == ERR_OK) { |
robert_jw | 0:b2805b6888dc | 1237 | LWIP_ASSERT("do_writemore: invalid length!", ((conn->write_offset + len) <= conn->current_msg->msg.w.len)); |
robert_jw | 0:b2805b6888dc | 1238 | err = tcp_write(conn->pcb.tcp, dataptr, len, apiflags); |
robert_jw | 0:b2805b6888dc | 1239 | } |
robert_jw | 0:b2805b6888dc | 1240 | if (dontblock && (err == ERR_MEM)) { |
robert_jw | 0:b2805b6888dc | 1241 | /* nonblocking write failed */ |
robert_jw | 0:b2805b6888dc | 1242 | write_finished = 1; |
robert_jw | 0:b2805b6888dc | 1243 | err = ERR_WOULDBLOCK; |
robert_jw | 0:b2805b6888dc | 1244 | /* let poll_tcp check writable space to mark the pcb |
robert_jw | 0:b2805b6888dc | 1245 | writable again */ |
robert_jw | 0:b2805b6888dc | 1246 | conn->flags |= NETCONN_FLAG_CHECK_WRITESPACE; |
robert_jw | 0:b2805b6888dc | 1247 | /* let select mark this pcb as non-writable. */ |
robert_jw | 0:b2805b6888dc | 1248 | API_EVENT(conn, NETCONN_EVT_SENDMINUS, len); |
robert_jw | 0:b2805b6888dc | 1249 | } else { |
robert_jw | 0:b2805b6888dc | 1250 | /* if OK or memory error, check available space */ |
robert_jw | 0:b2805b6888dc | 1251 | if (((err == ERR_OK) || (err == ERR_MEM)) && |
robert_jw | 0:b2805b6888dc | 1252 | ((tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT) || |
robert_jw | 0:b2805b6888dc | 1253 | (tcp_sndqueuelen(conn->pcb.tcp) >= TCP_SNDQUEUELOWAT))) { |
robert_jw | 0:b2805b6888dc | 1254 | /* The queued byte- or pbuf-count exceeds the configured low-water limit, |
robert_jw | 0:b2805b6888dc | 1255 | let select mark this pcb as non-writable. */ |
robert_jw | 0:b2805b6888dc | 1256 | API_EVENT(conn, NETCONN_EVT_SENDMINUS, len); |
robert_jw | 0:b2805b6888dc | 1257 | } |
robert_jw | 0:b2805b6888dc | 1258 | |
robert_jw | 0:b2805b6888dc | 1259 | if (err == ERR_OK) { |
robert_jw | 0:b2805b6888dc | 1260 | conn->write_offset += len; |
robert_jw | 0:b2805b6888dc | 1261 | if (conn->write_offset == conn->current_msg->msg.w.len) { |
robert_jw | 0:b2805b6888dc | 1262 | /* everything was written */ |
robert_jw | 0:b2805b6888dc | 1263 | write_finished = 1; |
robert_jw | 0:b2805b6888dc | 1264 | conn->write_offset = 0; |
robert_jw | 0:b2805b6888dc | 1265 | } |
robert_jw | 0:b2805b6888dc | 1266 | tcp_output(conn->pcb.tcp); |
robert_jw | 0:b2805b6888dc | 1267 | } else if (err == ERR_MEM) { |
robert_jw | 0:b2805b6888dc | 1268 | /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called |
robert_jw | 0:b2805b6888dc | 1269 | we do NOT return to the application thread, since ERR_MEM is |
robert_jw | 0:b2805b6888dc | 1270 | only a temporary error! */ |
robert_jw | 0:b2805b6888dc | 1271 | |
robert_jw | 0:b2805b6888dc | 1272 | /* tcp_write returned ERR_MEM, try tcp_output anyway */ |
robert_jw | 0:b2805b6888dc | 1273 | tcp_output(conn->pcb.tcp); |
robert_jw | 0:b2805b6888dc | 1274 | |
robert_jw | 0:b2805b6888dc | 1275 | #if LWIP_TCPIP_CORE_LOCKING |
robert_jw | 0:b2805b6888dc | 1276 | conn->flags |= NETCONN_FLAG_WRITE_DELAYED; |
robert_jw | 0:b2805b6888dc | 1277 | #endif |
robert_jw | 0:b2805b6888dc | 1278 | } else { |
robert_jw | 0:b2805b6888dc | 1279 | /* On errors != ERR_MEM, we don't try writing any more but return |
robert_jw | 0:b2805b6888dc | 1280 | the error to the application thread. */ |
robert_jw | 0:b2805b6888dc | 1281 | write_finished = 1; |
robert_jw | 0:b2805b6888dc | 1282 | } |
robert_jw | 0:b2805b6888dc | 1283 | } |
robert_jw | 0:b2805b6888dc | 1284 | |
robert_jw | 0:b2805b6888dc | 1285 | if (write_finished) { |
robert_jw | 0:b2805b6888dc | 1286 | /* everything was written: set back connection state |
robert_jw | 0:b2805b6888dc | 1287 | and back to application task */ |
robert_jw | 0:b2805b6888dc | 1288 | conn->current_msg->err = err; |
robert_jw | 0:b2805b6888dc | 1289 | conn->current_msg = NULL; |
robert_jw | 0:b2805b6888dc | 1290 | conn->state = NETCONN_NONE; |
robert_jw | 0:b2805b6888dc | 1291 | #if LWIP_TCPIP_CORE_LOCKING |
robert_jw | 0:b2805b6888dc | 1292 | if ((conn->flags & NETCONN_FLAG_WRITE_DELAYED) != 0) |
robert_jw | 0:b2805b6888dc | 1293 | #endif |
robert_jw | 0:b2805b6888dc | 1294 | { |
robert_jw | 0:b2805b6888dc | 1295 | sys_sem_signal(&conn->op_completed); |
robert_jw | 0:b2805b6888dc | 1296 | } |
robert_jw | 0:b2805b6888dc | 1297 | } |
robert_jw | 0:b2805b6888dc | 1298 | #if LWIP_TCPIP_CORE_LOCKING |
robert_jw | 0:b2805b6888dc | 1299 | else |
robert_jw | 0:b2805b6888dc | 1300 | return ERR_MEM; |
robert_jw | 0:b2805b6888dc | 1301 | #endif |
robert_jw | 0:b2805b6888dc | 1302 | return ERR_OK; |
robert_jw | 0:b2805b6888dc | 1303 | } |
robert_jw | 0:b2805b6888dc | 1304 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 1305 | |
robert_jw | 0:b2805b6888dc | 1306 | /** |
robert_jw | 0:b2805b6888dc | 1307 | * Send some data on a TCP pcb contained in a netconn |
robert_jw | 0:b2805b6888dc | 1308 | * Called from netconn_write |
robert_jw | 0:b2805b6888dc | 1309 | * |
robert_jw | 0:b2805b6888dc | 1310 | * @param msg the api_msg_msg pointing to the connection |
robert_jw | 0:b2805b6888dc | 1311 | */ |
robert_jw | 0:b2805b6888dc | 1312 | void |
robert_jw | 0:b2805b6888dc | 1313 | do_write(struct api_msg_msg *msg) |
robert_jw | 0:b2805b6888dc | 1314 | { |
robert_jw | 0:b2805b6888dc | 1315 | if (ERR_IS_FATAL(msg->conn->last_err)) { |
robert_jw | 0:b2805b6888dc | 1316 | msg->err = msg->conn->last_err; |
robert_jw | 0:b2805b6888dc | 1317 | } else { |
robert_jw | 0:b2805b6888dc | 1318 | if (msg->conn->type == NETCONN_TCP) { |
robert_jw | 0:b2805b6888dc | 1319 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 1320 | if (msg->conn->state != NETCONN_NONE) { |
robert_jw | 0:b2805b6888dc | 1321 | /* netconn is connecting, closing or in blocking write */ |
robert_jw | 0:b2805b6888dc | 1322 | msg->err = ERR_INPROGRESS; |
robert_jw | 0:b2805b6888dc | 1323 | } else if (msg->conn->pcb.tcp != NULL) { |
robert_jw | 0:b2805b6888dc | 1324 | msg->conn->state = NETCONN_WRITE; |
robert_jw | 0:b2805b6888dc | 1325 | /* set all the variables used by do_writemore */ |
robert_jw | 0:b2805b6888dc | 1326 | LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL && |
robert_jw | 0:b2805b6888dc | 1327 | msg->conn->write_offset == 0); |
robert_jw | 0:b2805b6888dc | 1328 | LWIP_ASSERT("msg->msg.w.len != 0", msg->msg.w.len != 0); |
robert_jw | 0:b2805b6888dc | 1329 | msg->conn->current_msg = msg; |
robert_jw | 0:b2805b6888dc | 1330 | msg->conn->write_offset = 0; |
robert_jw | 0:b2805b6888dc | 1331 | #if LWIP_TCPIP_CORE_LOCKING |
robert_jw | 0:b2805b6888dc | 1332 | msg->conn->flags &= ~NETCONN_FLAG_WRITE_DELAYED; |
robert_jw | 0:b2805b6888dc | 1333 | if (do_writemore(msg->conn) != ERR_OK) { |
robert_jw | 0:b2805b6888dc | 1334 | LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE); |
robert_jw | 0:b2805b6888dc | 1335 | UNLOCK_TCPIP_CORE(); |
robert_jw | 0:b2805b6888dc | 1336 | sys_arch_sem_wait(&msg->conn->op_completed, 0); |
robert_jw | 0:b2805b6888dc | 1337 | LOCK_TCPIP_CORE(); |
robert_jw | 0:b2805b6888dc | 1338 | LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE); |
robert_jw | 0:b2805b6888dc | 1339 | } |
robert_jw | 0:b2805b6888dc | 1340 | #else /* LWIP_TCPIP_CORE_LOCKING */ |
robert_jw | 0:b2805b6888dc | 1341 | do_writemore(msg->conn); |
robert_jw | 0:b2805b6888dc | 1342 | #endif /* LWIP_TCPIP_CORE_LOCKING */ |
robert_jw | 0:b2805b6888dc | 1343 | /* for both cases: if do_writemore was called, don't ACK the APIMSG |
robert_jw | 0:b2805b6888dc | 1344 | since do_writemore ACKs it! */ |
robert_jw | 0:b2805b6888dc | 1345 | return; |
robert_jw | 0:b2805b6888dc | 1346 | } else { |
robert_jw | 0:b2805b6888dc | 1347 | msg->err = ERR_CONN; |
robert_jw | 0:b2805b6888dc | 1348 | } |
robert_jw | 0:b2805b6888dc | 1349 | #else /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 1350 | msg->err = ERR_VAL; |
robert_jw | 0:b2805b6888dc | 1351 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 1352 | #if (LWIP_UDP || LWIP_RAW) |
robert_jw | 0:b2805b6888dc | 1353 | } else { |
robert_jw | 0:b2805b6888dc | 1354 | msg->err = ERR_VAL; |
robert_jw | 0:b2805b6888dc | 1355 | #endif /* (LWIP_UDP || LWIP_RAW) */ |
robert_jw | 0:b2805b6888dc | 1356 | } |
robert_jw | 0:b2805b6888dc | 1357 | } |
robert_jw | 0:b2805b6888dc | 1358 | TCPIP_APIMSG_ACK(msg); |
robert_jw | 0:b2805b6888dc | 1359 | } |
robert_jw | 0:b2805b6888dc | 1360 | |
robert_jw | 0:b2805b6888dc | 1361 | /** |
robert_jw | 0:b2805b6888dc | 1362 | * Return a connection's local or remote address |
robert_jw | 0:b2805b6888dc | 1363 | * Called from netconn_getaddr |
robert_jw | 0:b2805b6888dc | 1364 | * |
robert_jw | 0:b2805b6888dc | 1365 | * @param msg the api_msg_msg pointing to the connection |
robert_jw | 0:b2805b6888dc | 1366 | */ |
robert_jw | 0:b2805b6888dc | 1367 | void |
robert_jw | 0:b2805b6888dc | 1368 | do_getaddr(struct api_msg_msg *msg) |
robert_jw | 0:b2805b6888dc | 1369 | { |
robert_jw | 0:b2805b6888dc | 1370 | if (msg->conn->pcb.ip != NULL) { |
robert_jw | 0:b2805b6888dc | 1371 | *(msg->msg.ad.ipaddr) = (msg->msg.ad.local ? msg->conn->pcb.ip->local_ip : |
robert_jw | 0:b2805b6888dc | 1372 | msg->conn->pcb.ip->remote_ip); |
robert_jw | 0:b2805b6888dc | 1373 | |
robert_jw | 0:b2805b6888dc | 1374 | msg->err = ERR_OK; |
robert_jw | 0:b2805b6888dc | 1375 | switch (NETCONNTYPE_GROUP(msg->conn->type)) { |
robert_jw | 0:b2805b6888dc | 1376 | #if LWIP_RAW |
robert_jw | 0:b2805b6888dc | 1377 | case NETCONN_RAW: |
robert_jw | 0:b2805b6888dc | 1378 | if (msg->msg.ad.local) { |
robert_jw | 0:b2805b6888dc | 1379 | *(msg->msg.ad.port) = msg->conn->pcb.raw->protocol; |
robert_jw | 0:b2805b6888dc | 1380 | } else { |
robert_jw | 0:b2805b6888dc | 1381 | /* return an error as connecting is only a helper for upper layers */ |
robert_jw | 0:b2805b6888dc | 1382 | msg->err = ERR_CONN; |
robert_jw | 0:b2805b6888dc | 1383 | } |
robert_jw | 0:b2805b6888dc | 1384 | break; |
robert_jw | 0:b2805b6888dc | 1385 | #endif /* LWIP_RAW */ |
robert_jw | 0:b2805b6888dc | 1386 | #if LWIP_UDP |
robert_jw | 0:b2805b6888dc | 1387 | case NETCONN_UDP: |
robert_jw | 0:b2805b6888dc | 1388 | if (msg->msg.ad.local) { |
robert_jw | 0:b2805b6888dc | 1389 | *(msg->msg.ad.port) = msg->conn->pcb.udp->local_port; |
robert_jw | 0:b2805b6888dc | 1390 | } else { |
robert_jw | 0:b2805b6888dc | 1391 | if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) { |
robert_jw | 0:b2805b6888dc | 1392 | msg->err = ERR_CONN; |
robert_jw | 0:b2805b6888dc | 1393 | } else { |
robert_jw | 0:b2805b6888dc | 1394 | *(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port; |
robert_jw | 0:b2805b6888dc | 1395 | } |
robert_jw | 0:b2805b6888dc | 1396 | } |
robert_jw | 0:b2805b6888dc | 1397 | break; |
robert_jw | 0:b2805b6888dc | 1398 | #endif /* LWIP_UDP */ |
robert_jw | 0:b2805b6888dc | 1399 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 1400 | case NETCONN_TCP: |
robert_jw | 0:b2805b6888dc | 1401 | *(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port); |
robert_jw | 0:b2805b6888dc | 1402 | break; |
robert_jw | 0:b2805b6888dc | 1403 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 1404 | default: |
robert_jw | 0:b2805b6888dc | 1405 | LWIP_ASSERT("invalid netconn_type", 0); |
robert_jw | 0:b2805b6888dc | 1406 | break; |
robert_jw | 0:b2805b6888dc | 1407 | } |
robert_jw | 0:b2805b6888dc | 1408 | } else { |
robert_jw | 0:b2805b6888dc | 1409 | msg->err = ERR_CONN; |
robert_jw | 0:b2805b6888dc | 1410 | } |
robert_jw | 0:b2805b6888dc | 1411 | TCPIP_APIMSG_ACK(msg); |
robert_jw | 0:b2805b6888dc | 1412 | } |
robert_jw | 0:b2805b6888dc | 1413 | |
robert_jw | 0:b2805b6888dc | 1414 | /** |
robert_jw | 0:b2805b6888dc | 1415 | * Close a TCP pcb contained in a netconn |
robert_jw | 0:b2805b6888dc | 1416 | * Called from netconn_close |
robert_jw | 0:b2805b6888dc | 1417 | * |
robert_jw | 0:b2805b6888dc | 1418 | * @param msg the api_msg_msg pointing to the connection |
robert_jw | 0:b2805b6888dc | 1419 | */ |
robert_jw | 0:b2805b6888dc | 1420 | void |
robert_jw | 0:b2805b6888dc | 1421 | do_close(struct api_msg_msg *msg) |
robert_jw | 0:b2805b6888dc | 1422 | { |
robert_jw | 0:b2805b6888dc | 1423 | #if LWIP_TCP |
robert_jw | 0:b2805b6888dc | 1424 | /* @todo: abort running write/connect? */ |
robert_jw | 0:b2805b6888dc | 1425 | if ((msg->conn->state != NETCONN_NONE) && (msg->conn->state != NETCONN_LISTEN)) { |
robert_jw | 0:b2805b6888dc | 1426 | /* this only happens for TCP netconns */ |
robert_jw | 0:b2805b6888dc | 1427 | LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP); |
robert_jw | 0:b2805b6888dc | 1428 | msg->err = ERR_INPROGRESS; |
robert_jw | 0:b2805b6888dc | 1429 | } else if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) { |
robert_jw | 0:b2805b6888dc | 1430 | if ((msg->msg.sd.shut != NETCONN_SHUT_RDWR) && (msg->conn->state == NETCONN_LISTEN)) { |
robert_jw | 0:b2805b6888dc | 1431 | /* LISTEN doesn't support half shutdown */ |
robert_jw | 0:b2805b6888dc | 1432 | msg->err = ERR_CONN; |
robert_jw | 0:b2805b6888dc | 1433 | } else { |
robert_jw | 0:b2805b6888dc | 1434 | if (msg->msg.sd.shut & NETCONN_SHUT_RD) { |
robert_jw | 0:b2805b6888dc | 1435 | /* Drain and delete mboxes */ |
robert_jw | 0:b2805b6888dc | 1436 | netconn_drain(msg->conn); |
robert_jw | 0:b2805b6888dc | 1437 | } |
robert_jw | 0:b2805b6888dc | 1438 | LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL && |
robert_jw | 0:b2805b6888dc | 1439 | msg->conn->write_offset == 0); |
robert_jw | 0:b2805b6888dc | 1440 | msg->conn->state = NETCONN_CLOSE; |
robert_jw | 0:b2805b6888dc | 1441 | msg->conn->current_msg = msg; |
robert_jw | 0:b2805b6888dc | 1442 | do_close_internal(msg->conn); |
robert_jw | 0:b2805b6888dc | 1443 | /* for tcp netconns, do_close_internal ACKs the message */ |
robert_jw | 0:b2805b6888dc | 1444 | return; |
robert_jw | 0:b2805b6888dc | 1445 | } |
robert_jw | 0:b2805b6888dc | 1446 | } else |
robert_jw | 0:b2805b6888dc | 1447 | #endif /* LWIP_TCP */ |
robert_jw | 0:b2805b6888dc | 1448 | { |
robert_jw | 0:b2805b6888dc | 1449 | msg->err = ERR_VAL; |
robert_jw | 0:b2805b6888dc | 1450 | } |
robert_jw | 0:b2805b6888dc | 1451 | sys_sem_signal(&msg->conn->op_completed); |
robert_jw | 0:b2805b6888dc | 1452 | } |
robert_jw | 0:b2805b6888dc | 1453 | |
robert_jw | 0:b2805b6888dc | 1454 | #if LWIP_IGMP |
robert_jw | 0:b2805b6888dc | 1455 | /** |
robert_jw | 0:b2805b6888dc | 1456 | * Join multicast groups for UDP netconns. |
robert_jw | 0:b2805b6888dc | 1457 | * Called from netconn_join_leave_group |
robert_jw | 0:b2805b6888dc | 1458 | * |
robert_jw | 0:b2805b6888dc | 1459 | * @param msg the api_msg_msg pointing to the connection |
robert_jw | 0:b2805b6888dc | 1460 | */ |
robert_jw | 0:b2805b6888dc | 1461 | void |
robert_jw | 0:b2805b6888dc | 1462 | do_join_leave_group(struct api_msg_msg *msg) |
robert_jw | 0:b2805b6888dc | 1463 | { |
robert_jw | 0:b2805b6888dc | 1464 | if (ERR_IS_FATAL(msg->conn->last_err)) { |
robert_jw | 0:b2805b6888dc | 1465 | msg->err = msg->conn->last_err; |
robert_jw | 0:b2805b6888dc | 1466 | } else { |
robert_jw | 0:b2805b6888dc | 1467 | if (msg->conn->pcb.tcp != NULL) { |
robert_jw | 0:b2805b6888dc | 1468 | if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) { |
robert_jw | 0:b2805b6888dc | 1469 | #if LWIP_UDP |
robert_jw | 0:b2805b6888dc | 1470 | if (msg->msg.jl.join_or_leave == NETCONN_JOIN) { |
robert_jw | 0:b2805b6888dc | 1471 | msg->err = igmp_joingroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr); |
robert_jw | 0:b2805b6888dc | 1472 | } else { |
robert_jw | 0:b2805b6888dc | 1473 | msg->err = igmp_leavegroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr); |
robert_jw | 0:b2805b6888dc | 1474 | } |
robert_jw | 0:b2805b6888dc | 1475 | #endif /* LWIP_UDP */ |
robert_jw | 0:b2805b6888dc | 1476 | #if (LWIP_TCP || LWIP_RAW) |
robert_jw | 0:b2805b6888dc | 1477 | } else { |
robert_jw | 0:b2805b6888dc | 1478 | msg->err = ERR_VAL; |
robert_jw | 0:b2805b6888dc | 1479 | #endif /* (LWIP_TCP || LWIP_RAW) */ |
robert_jw | 0:b2805b6888dc | 1480 | } |
robert_jw | 0:b2805b6888dc | 1481 | } else { |
robert_jw | 0:b2805b6888dc | 1482 | msg->err = ERR_CONN; |
robert_jw | 0:b2805b6888dc | 1483 | } |
robert_jw | 0:b2805b6888dc | 1484 | } |
robert_jw | 0:b2805b6888dc | 1485 | TCPIP_APIMSG_ACK(msg); |
robert_jw | 0:b2805b6888dc | 1486 | } |
robert_jw | 0:b2805b6888dc | 1487 | #endif /* LWIP_IGMP */ |
robert_jw | 0:b2805b6888dc | 1488 | |
robert_jw | 0:b2805b6888dc | 1489 | #if LWIP_DNS |
robert_jw | 0:b2805b6888dc | 1490 | /** |
robert_jw | 0:b2805b6888dc | 1491 | * Callback function that is called when DNS name is resolved |
robert_jw | 0:b2805b6888dc | 1492 | * (or on timeout). A waiting application thread is waked up by |
robert_jw | 0:b2805b6888dc | 1493 | * signaling the semaphore. |
robert_jw | 0:b2805b6888dc | 1494 | */ |
robert_jw | 0:b2805b6888dc | 1495 | static void |
robert_jw | 0:b2805b6888dc | 1496 | do_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) |
robert_jw | 0:b2805b6888dc | 1497 | { |
robert_jw | 0:b2805b6888dc | 1498 | struct dns_api_msg *msg = (struct dns_api_msg*)arg; |
robert_jw | 0:b2805b6888dc | 1499 | |
robert_jw | 0:b2805b6888dc | 1500 | LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0); |
robert_jw | 0:b2805b6888dc | 1501 | LWIP_UNUSED_ARG(name); |
robert_jw | 0:b2805b6888dc | 1502 | |
robert_jw | 0:b2805b6888dc | 1503 | if (ipaddr == NULL) { |
robert_jw | 0:b2805b6888dc | 1504 | /* timeout or memory error */ |
robert_jw | 0:b2805b6888dc | 1505 | *msg->err = ERR_VAL; |
robert_jw | 0:b2805b6888dc | 1506 | } else { |
robert_jw | 0:b2805b6888dc | 1507 | /* address was resolved */ |
robert_jw | 0:b2805b6888dc | 1508 | *msg->err = ERR_OK; |
robert_jw | 0:b2805b6888dc | 1509 | *msg->addr = *ipaddr; |
robert_jw | 0:b2805b6888dc | 1510 | } |
robert_jw | 0:b2805b6888dc | 1511 | /* wake up the application task waiting in netconn_gethostbyname */ |
robert_jw | 0:b2805b6888dc | 1512 | sys_sem_signal(msg->sem); |
robert_jw | 0:b2805b6888dc | 1513 | } |
robert_jw | 0:b2805b6888dc | 1514 | |
robert_jw | 0:b2805b6888dc | 1515 | /** |
robert_jw | 0:b2805b6888dc | 1516 | * Execute a DNS query |
robert_jw | 0:b2805b6888dc | 1517 | * Called from netconn_gethostbyname |
robert_jw | 0:b2805b6888dc | 1518 | * |
robert_jw | 0:b2805b6888dc | 1519 | * @param arg the dns_api_msg pointing to the query |
robert_jw | 0:b2805b6888dc | 1520 | */ |
robert_jw | 0:b2805b6888dc | 1521 | void |
robert_jw | 0:b2805b6888dc | 1522 | do_gethostbyname(void *arg) |
robert_jw | 0:b2805b6888dc | 1523 | { |
robert_jw | 0:b2805b6888dc | 1524 | struct dns_api_msg *msg = (struct dns_api_msg*)arg; |
robert_jw | 0:b2805b6888dc | 1525 | |
robert_jw | 0:b2805b6888dc | 1526 | *msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg); |
robert_jw | 0:b2805b6888dc | 1527 | if (*msg->err != ERR_INPROGRESS) { |
robert_jw | 0:b2805b6888dc | 1528 | /* on error or immediate success, wake up the application |
robert_jw | 0:b2805b6888dc | 1529 | * task waiting in netconn_gethostbyname */ |
robert_jw | 0:b2805b6888dc | 1530 | sys_sem_signal(msg->sem); |
robert_jw | 0:b2805b6888dc | 1531 | } |
robert_jw | 0:b2805b6888dc | 1532 | } |
robert_jw | 0:b2805b6888dc | 1533 | #endif /* LWIP_DNS */ |
robert_jw | 0:b2805b6888dc | 1534 | |
robert_jw | 0:b2805b6888dc | 1535 | #endif /* LWIP_NETCONN */ |