Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
aziz111
Date:
Fri Mar 08 17:15:02 2019 +0000
Revision:
5:569a4894abc1
Parent:
3:78f223d34f36
Final

Who changed what in which revision?

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