V1

Dependents:   EthernetInterface

Fork of lwip by mbed official

Committer:
mbed_official
Date:
Fri Jun 22 09:25:39 2012 +0000
Revision:
0:51ac1d130fd4
Initial import from lwip-1.4.0: http://download.savannah.gnu.org/releases/lwip/lwip-1.4.0.zip

Who changed what in which revision?

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