Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.

Dependencies:   C12832 MQTT LM75B MMA7660

Dependents:   MFT_IoT_demo_USB400 IBM_RFID

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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